khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package common |
| 17 | |
| 18 | import ( |
| 19 | "errors" |
| 20 | "github.com/golang/protobuf/ptypes" |
| 21 | "github.com/golang/protobuf/ptypes/empty" |
| 22 | "github.com/opencord/voltha-go/adapters" |
| 23 | "github.com/opencord/voltha-go/common/log" |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 24 | "github.com/opencord/voltha-go/kafka" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 25 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 26 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 27 | "google.golang.org/grpc/codes" |
| 28 | "google.golang.org/grpc/status" |
| 29 | ) |
| 30 | |
| 31 | type RequestHandlerProxy struct { |
| 32 | TestMode bool |
| 33 | coreInstanceId string |
| 34 | adapter adapters.IAdapter |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 35 | coreProxy *CoreProxy |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 36 | } |
| 37 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 38 | func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy *CoreProxy) *RequestHandlerProxy { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 39 | var proxy RequestHandlerProxy |
| 40 | proxy.coreInstanceId = coreInstanceId |
| 41 | proxy.adapter = iadapter |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 42 | proxy.coreProxy = cProxy |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 43 | return &proxy |
| 44 | } |
| 45 | |
| 46 | func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) { |
| 47 | return new(empty.Empty), nil |
| 48 | } |
| 49 | |
| 50 | func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) { |
| 51 | return nil, nil |
| 52 | } |
| 53 | |
| 54 | func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) { |
| 55 | return nil, nil |
| 56 | } |
| 57 | |
| 58 | func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 59 | if len(args) < 3 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 60 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 61 | err := errors.New("invalid-number-of-args") |
| 62 | return nil, err |
| 63 | } |
| 64 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 65 | transactionID := &ic.StrType{} |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 66 | fromTopic := &ic.StrType{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 67 | for _, arg := range args { |
| 68 | switch arg.Key { |
| 69 | case "device": |
| 70 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 71 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 72 | return nil, err |
| 73 | } |
| 74 | case kafka.TransactionKey: |
| 75 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 76 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 77 | return nil, err |
| 78 | } |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 79 | case kafka.FromTopic: |
| 80 | if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil { |
| 81 | log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err}) |
| 82 | return nil, err |
| 83 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 84 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 85 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 86 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 87 | log.Debugw("Adopt_device", log.Fields{"deviceId": device.Id}) |
| 88 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 89 | //Update the core reference for that device |
| 90 | rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val) |
| 91 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 92 | //Invoke the adopt device on the adapter |
| 93 | if err := rhp.adapter.Adopt_device(device); err != nil { |
| 94 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 95 | } |
| 96 | |
| 97 | return new(empty.Empty), nil |
| 98 | } |
| 99 | |
| 100 | func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) { |
| 101 | return new(empty.Empty), nil |
| 102 | } |
| 103 | |
| 104 | func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) { |
| 105 | return new(empty.Empty), nil |
| 106 | } |
| 107 | |
| 108 | func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 109 | return new(empty.Empty), nil |
| 110 | } |
| 111 | |
| 112 | func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 113 | return new(empty.Empty), nil |
| 114 | } |
| 115 | |
| 116 | func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) { |
| 117 | return new(empty.Empty), nil |
| 118 | } |
| 119 | |
| 120 | func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) { |
| 121 | return new(empty.Empty), nil |
| 122 | } |
| 123 | |
| 124 | func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) { |
| 125 | return new(empty.Empty), nil |
| 126 | } |
| 127 | |
| 128 | func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) { |
| 129 | return new(empty.Empty), nil |
| 130 | } |
| 131 | |
| 132 | func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) { |
| 133 | return new(empty.Empty), nil |
| 134 | } |
| 135 | |
| 136 | func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) { |
| 137 | return new(empty.Empty), nil |
| 138 | } |
| 139 | |
| 140 | func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) { |
| 141 | return new(empty.Empty), nil |
| 142 | } |
| 143 | |
| 144 | func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) { |
| 145 | return new(empty.Empty), nil |
| 146 | } |
| 147 | |
| 148 | func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 149 | return new(empty.Empty), nil |
| 150 | } |
| 151 | |
| 152 | func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 153 | return new(empty.Empty), nil |
| 154 | } |
| 155 | |
| 156 | func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 157 | if len(args) < 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 158 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 159 | err := errors.New("invalid-number-of-args") |
| 160 | return nil, err |
| 161 | } |
| 162 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 163 | transactionID := &ic.StrType{} |
| 164 | for _, arg := range args { |
| 165 | switch arg.Key { |
| 166 | case "device": |
| 167 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 168 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 169 | return nil, err |
| 170 | } |
| 171 | case kafka.TransactionKey: |
| 172 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 173 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 174 | return nil, err |
| 175 | } |
| 176 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 177 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 178 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 179 | log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id}) |
| 180 | |
| 181 | var cap *ic.SwitchCapability |
| 182 | var err error |
| 183 | if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil { |
| 184 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 185 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 186 | log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap}) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 187 | return cap, nil |
| 188 | } |
| 189 | |
| 190 | func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 191 | if len(args) < 3 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 192 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 193 | err := errors.New("invalid-number-of-args") |
| 194 | return nil, err |
| 195 | } |
| 196 | device := &voltha.Device{} |
| 197 | pNo := &ic.IntType{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 198 | transactionID := &ic.StrType{} |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 199 | for _, arg := range args { |
| 200 | switch arg.Key { |
| 201 | case "device": |
| 202 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 203 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 204 | return nil, err |
| 205 | } |
| 206 | case "port_no": |
| 207 | if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil { |
| 208 | log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err}) |
| 209 | return nil, err |
| 210 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 211 | case kafka.TransactionKey: |
| 212 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 213 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 214 | return nil, err |
| 215 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val}) |
| 219 | var cap *ic.PortCapability |
| 220 | var err error |
| 221 | if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil { |
| 222 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 223 | } |
| 224 | return cap, nil |
| 225 | } |
| 226 | |
| 227 | func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 228 | if len(args) < 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 229 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 230 | err := errors.New("invalid-number-of-args") |
| 231 | return nil, err |
| 232 | } |
| 233 | iaMsg := &ic.InterAdapterMessage{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 234 | transactionID := &ic.StrType{} |
| 235 | for _, arg := range args { |
| 236 | switch arg.Key { |
| 237 | case "msg": |
| 238 | if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil { |
| 239 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 240 | return nil, err |
| 241 | } |
| 242 | case kafka.TransactionKey: |
| 243 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 244 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 245 | return nil, err |
| 246 | } |
| 247 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id}) |
| 251 | |
| 252 | //Invoke the inter adapter API on the handler |
| 253 | if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil { |
| 254 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 255 | } |
| 256 | |
| 257 | return new(empty.Empty), nil |
| 258 | } |
| 259 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 260 | func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 261 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 262 | } |
| 263 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 264 | func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 265 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 266 | } |
| 267 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 268 | func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 269 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 270 | } |
| 271 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 272 | func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 273 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 274 | } |
| 275 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 276 | func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 277 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 278 | } |