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" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 25 | ic "github.com/opencord/voltha-go/protos/inter_container" |
| 26 | "github.com/opencord/voltha-go/protos/voltha" |
| 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 |
| 35 | } |
| 36 | |
| 37 | func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter) *RequestHandlerProxy { |
| 38 | var proxy RequestHandlerProxy |
| 39 | proxy.coreInstanceId = coreInstanceId |
| 40 | proxy.adapter = iadapter |
| 41 | return &proxy |
| 42 | } |
| 43 | |
| 44 | func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) { |
| 45 | return new(empty.Empty), nil |
| 46 | } |
| 47 | |
| 48 | func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) { |
| 49 | return nil, nil |
| 50 | } |
| 51 | |
| 52 | func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) { |
| 53 | return nil, nil |
| 54 | } |
| 55 | |
| 56 | func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 57 | if len(args) != 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 58 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 59 | err := errors.New("invalid-number-of-args") |
| 60 | return nil, err |
| 61 | } |
| 62 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 63 | transactionID := &ic.StrType{} |
| 64 | for _, arg := range args { |
| 65 | switch arg.Key { |
| 66 | case "device": |
| 67 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 68 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 69 | return nil, err |
| 70 | } |
| 71 | case kafka.TransactionKey: |
| 72 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 73 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 74 | return nil, err |
| 75 | } |
| 76 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 77 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 78 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 79 | log.Debugw("Adopt_device", log.Fields{"deviceId": device.Id}) |
| 80 | |
| 81 | //Invoke the adopt device on the adapter |
| 82 | if err := rhp.adapter.Adopt_device(device); err != nil { |
| 83 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 84 | } |
| 85 | |
| 86 | return new(empty.Empty), nil |
| 87 | } |
| 88 | |
| 89 | func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) { |
| 90 | return new(empty.Empty), nil |
| 91 | } |
| 92 | |
| 93 | func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) { |
| 94 | return new(empty.Empty), nil |
| 95 | } |
| 96 | |
| 97 | func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 98 | return new(empty.Empty), nil |
| 99 | } |
| 100 | |
| 101 | func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) { |
| 102 | return new(empty.Empty), nil |
| 103 | } |
| 104 | |
| 105 | func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) { |
| 106 | return new(empty.Empty), nil |
| 107 | } |
| 108 | |
| 109 | func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) { |
| 110 | return new(empty.Empty), nil |
| 111 | } |
| 112 | |
| 113 | func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) { |
| 114 | return new(empty.Empty), nil |
| 115 | } |
| 116 | |
| 117 | func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) { |
| 118 | return new(empty.Empty), nil |
| 119 | } |
| 120 | |
| 121 | func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) { |
| 122 | return new(empty.Empty), nil |
| 123 | } |
| 124 | |
| 125 | func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) { |
| 126 | return new(empty.Empty), nil |
| 127 | } |
| 128 | |
| 129 | func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) { |
| 130 | return new(empty.Empty), nil |
| 131 | } |
| 132 | |
| 133 | func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) { |
| 134 | return new(empty.Empty), nil |
| 135 | } |
| 136 | |
| 137 | func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 138 | return new(empty.Empty), nil |
| 139 | } |
| 140 | |
| 141 | func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) { |
| 142 | return new(empty.Empty), nil |
| 143 | } |
| 144 | |
| 145 | func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) { |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 146 | if len(args) != 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 147 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 148 | err := errors.New("invalid-number-of-args") |
| 149 | return nil, err |
| 150 | } |
| 151 | device := &voltha.Device{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 152 | transactionID := &ic.StrType{} |
| 153 | for _, arg := range args { |
| 154 | switch arg.Key { |
| 155 | case "device": |
| 156 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 157 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 158 | return nil, err |
| 159 | } |
| 160 | case kafka.TransactionKey: |
| 161 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 162 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 163 | return nil, err |
| 164 | } |
| 165 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 166 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 167 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 168 | log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id}) |
| 169 | |
| 170 | var cap *ic.SwitchCapability |
| 171 | var err error |
| 172 | if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil { |
| 173 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 174 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 175 | log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap}) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 176 | return cap, nil |
| 177 | } |
| 178 | |
| 179 | func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) { |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 180 | if len(args) != 3 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 181 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 182 | err := errors.New("invalid-number-of-args") |
| 183 | return nil, err |
| 184 | } |
| 185 | device := &voltha.Device{} |
| 186 | pNo := &ic.IntType{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 187 | transactionID := &ic.StrType{} |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 188 | for _, arg := range args { |
| 189 | switch arg.Key { |
| 190 | case "device": |
| 191 | if err := ptypes.UnmarshalAny(arg.Value, device); err != nil { |
| 192 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 193 | return nil, err |
| 194 | } |
| 195 | case "port_no": |
| 196 | if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil { |
| 197 | log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err}) |
| 198 | return nil, err |
| 199 | } |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 200 | case kafka.TransactionKey: |
| 201 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 202 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 203 | return nil, err |
| 204 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val}) |
| 208 | var cap *ic.PortCapability |
| 209 | var err error |
| 210 | if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil { |
| 211 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 212 | } |
| 213 | return cap, nil |
| 214 | } |
| 215 | |
| 216 | func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) { |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 217 | if len(args) != 2 { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 218 | log.Warn("invalid-number-of-args", log.Fields{"args": args}) |
| 219 | err := errors.New("invalid-number-of-args") |
| 220 | return nil, err |
| 221 | } |
| 222 | iaMsg := &ic.InterAdapterMessage{} |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 223 | transactionID := &ic.StrType{} |
| 224 | for _, arg := range args { |
| 225 | switch arg.Key { |
| 226 | case "msg": |
| 227 | if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil { |
| 228 | log.Warnw("cannot-unmarshal-device", log.Fields{"error": err}) |
| 229 | return nil, err |
| 230 | } |
| 231 | case kafka.TransactionKey: |
| 232 | if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil { |
| 233 | log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err}) |
| 234 | return nil, err |
| 235 | } |
| 236 | } |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id}) |
| 240 | |
| 241 | //Invoke the inter adapter API on the handler |
| 242 | if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil { |
| 243 | return nil, status.Errorf(codes.NotFound, "%s", err.Error()) |
| 244 | } |
| 245 | |
| 246 | return new(empty.Empty), nil |
| 247 | } |
| 248 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 249 | func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 250 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 251 | } |
| 252 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 253 | func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 254 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 255 | } |
| 256 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 257 | func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 258 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 259 | } |
| 260 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 261 | func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 262 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 263 | } |
| 264 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 265 | func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) { |
| 266 | return &voltha.ImageDownload{}, nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 267 | } |