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" |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 26 | "github.com/opencord/voltha-protos/go/openflow_13" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 27 | "github.com/opencord/voltha-protos/go/voltha" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 28 | "google.golang.org/grpc/codes" |
| 29 | "google.golang.org/grpc/status" |
| 30 | ) |
| 31 | |
| 32 | type RequestHandlerProxy struct { |
| 33 | TestMode bool |
| 34 | coreInstanceId string |
| 35 | adapter adapters.IAdapter |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 36 | coreProxy *CoreProxy |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 37 | } |
| 38 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 39 | func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy *CoreProxy) *RequestHandlerProxy { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 40 | var proxy RequestHandlerProxy |
| 41 | proxy.coreInstanceId = coreInstanceId |
| 42 | proxy.adapter = iadapter |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 43 | proxy.coreProxy = cProxy |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 44 | return &proxy |
| 45 | } |
| 46 | |
| 47 | func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) { |
| 48 | return new(empty.Empty), nil |
| 49 | } |
| 50 | |
| 51 | func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) { |
| 52 | return nil, nil |
| 53 | } |
| 54 | |
| 55 | func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) { |
| 56 | return nil, nil |
| 57 | } |
| 58 | |
| 59 | func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 60 | if len(args) < 3 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 61 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 62 | err := errors.New("invalid-number-of-args") |
| 63 | return nil, err |
| 64 | } |
| 65 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 66 | transactionID := &ic.StrType{} |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 67 | fromTopic := &ic.StrType{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 68 | for _, arg := range args { |
| 69 | switch arg.Key { |
| 70 | case "device": |
| 71 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 72 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 73 | return nil, err |
| 74 | } |
| 75 | case kafka.TransactionKey: |
| 76 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 77 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 78 | return nil, err |
| 79 | } |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 80 | case kafka.FromTopic: |
| 81 | if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil { |
| 82 | log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err}) |
| 83 | return nil, err |
| 84 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 85 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 86 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 87 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 88 | log.Debugw("Adopt_device", log.Fields{"deviceId": device.Id}) |
| 89 | |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 90 | //Update the core reference for that device |
| 91 | rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val) |
| 92 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 93 | //Invoke the adopt device on the adapter |
| 94 | if err := rhp.adapter.Adopt_device(device); err != nil { |
| 95 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 96 | } |
| 97 | |
| 98 | return new(empty.Empty), nil |
| 99 | } |
| 100 | |
| 101 | func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) { |
| 102 | return new(empty.Empty), nil |
| 103 | } |
| 104 | |
| 105 | func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) { |
| 106 | return new(empty.Empty), nil |
| 107 | } |
| 108 | |
| 109 | func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 110 | return new(empty.Empty), nil |
| 111 | } |
| 112 | |
| 113 | func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 114 | return new(empty.Empty), nil |
| 115 | } |
| 116 | |
| 117 | func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) { |
| 118 | return new(empty.Empty), nil |
| 119 | } |
| 120 | |
| 121 | func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) { |
| 122 | return new(empty.Empty), nil |
| 123 | } |
| 124 | |
| 125 | func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) { |
| 126 | return new(empty.Empty), nil |
| 127 | } |
| 128 | |
| 129 | func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) { |
| 130 | return new(empty.Empty), nil |
| 131 | } |
| 132 | |
| 133 | func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) { |
| 134 | return new(empty.Empty), nil |
| 135 | } |
| 136 | |
| 137 | func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 138 | log.Debug("Update_flows_incrementally") |
mkoodali | 252f767 | 2019-03-25 12:13:12 +0530 | [diff] [blame] | 139 | if len(args) < 3 { |
| 140 | log.Warn("Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args}) |
| 141 | err := errors.New("invalid-number-of-args") |
| 142 | return nil, err |
| 143 | } |
| 144 | device := &voltha.Device{} |
| 145 | transactionID := &ic.StrType{} |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 146 | flows := &openflow_13.FlowChanges{} |
| 147 | groups := &openflow_13.FlowGroupChanges{} |
mkoodali | 252f767 | 2019-03-25 12:13:12 +0530 | [diff] [blame] | 148 | for _, arg := range args { |
| 149 | switch arg.Key { |
| 150 | case "device": |
| 151 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 152 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 153 | return nil, err |
| 154 | } |
| 155 | case "flow_changes": |
| 156 | if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil { |
| 157 | log.Warnw("cannot-unmarshal-flows", log.Fields{"error": err}) |
| 158 | return nil, err |
| 159 | } |
| 160 | case "group_changes": |
| 161 | if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil { |
| 162 | log.Warnw("cannot-unmarshal-groups", log.Fields{"error": err}) |
| 163 | return nil, err |
| 164 | } |
| 165 | case kafka.TransactionKey: |
| 166 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 167 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 168 | return nil, err |
| 169 | } |
| 170 | } |
| 171 | } |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 172 | log.Debugw("Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups}) |
| 173 | //Invoke the adopt device on the adapter |
| 174 | if err := rhp.adapter.Update_flows_incrementally(device, flows, groups); err != nil { |
| 175 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 176 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 177 | return new(empty.Empty), nil |
| 178 | } |
| 179 | |
| 180 | func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) { |
| 181 | return new(empty.Empty), nil |
| 182 | } |
| 183 | |
| 184 | func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) { |
| 185 | return new(empty.Empty), nil |
| 186 | } |
| 187 | |
| 188 | func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 189 | return new(empty.Empty), nil |
| 190 | } |
| 191 | |
| 192 | func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 193 | return new(empty.Empty), nil |
| 194 | } |
| 195 | |
| 196 | func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 197 | if len(args) < 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 198 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 199 | err := errors.New("invalid-number-of-args") |
| 200 | return nil, err |
| 201 | } |
| 202 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 203 | transactionID := &ic.StrType{} |
| 204 | for _, arg := range args { |
| 205 | switch arg.Key { |
| 206 | case "device": |
| 207 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 208 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 209 | return nil, err |
| 210 | } |
| 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 | } |
| 216 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 217 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 218 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 219 | log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id}) |
| 220 | |
| 221 | var cap *ic.SwitchCapability |
| 222 | var err error |
| 223 | if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil { |
| 224 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 225 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 226 | log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap}) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 227 | return cap, nil |
| 228 | } |
| 229 | |
| 230 | func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 231 | if len(args) < 3 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 232 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 233 | err := errors.New("invalid-number-of-args") |
| 234 | return nil, err |
| 235 | } |
| 236 | device := &voltha.Device{} |
| 237 | pNo := &ic.IntType{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 238 | transactionID := &ic.StrType{} |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 239 | for _, arg := range args { |
| 240 | switch arg.Key { |
| 241 | case "device": |
| 242 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 243 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 244 | return nil, err |
| 245 | } |
| 246 | case "port_no": |
| 247 | if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil { |
| 248 | log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err}) |
| 249 | return nil, err |
| 250 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 251 | case kafka.TransactionKey: |
| 252 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 253 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 254 | return nil, err |
| 255 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 256 | } |
| 257 | } |
| 258 | log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val}) |
| 259 | var cap *ic.PortCapability |
| 260 | var err error |
| 261 | if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil { |
| 262 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 263 | } |
| 264 | return cap, nil |
| 265 | } |
| 266 | |
| 267 | func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 54e0ddf | 2019-02-27 16:21:33 -0500 | [diff] [blame] | 268 | if len(args) < 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 269 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 270 | err := errors.New("invalid-number-of-args") |
| 271 | return nil, err |
| 272 | } |
| 273 | iaMsg := &ic.InterAdapterMessage{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 274 | transactionID := &ic.StrType{} |
| 275 | for _, arg := range args { |
| 276 | switch arg.Key { |
| 277 | case "msg": |
| 278 | if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil { |
| 279 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 280 | return nil, err |
| 281 | } |
| 282 | case kafka.TransactionKey: |
| 283 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 284 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 285 | return nil, err |
| 286 | } |
| 287 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id}) |
| 291 | |
| 292 | //Invoke the inter adapter API on the handler |
| 293 | if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil { |
| 294 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 295 | } |
| 296 | |
| 297 | return new(empty.Empty), nil |
| 298 | } |
| 299 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 300 | func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 301 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 302 | } |
| 303 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 304 | func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 305 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 306 | } |
| 307 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 308 | func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 309 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 310 | } |
| 311 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 312 | func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 313 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 314 | } |
| 315 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 316 | func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 317 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 318 | } |