blob: 3ac5c4ffb62a4cd84053552c31525c89101ecad6 [file] [log] [blame]
khenaidood2b6df92018-12-13 16:37:20 -05001/*
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 */
16package common
17
18import (
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"
khenaidoo6d055132019-02-12 16:51:19 -050024 "github.com/opencord/voltha-go/kafka"
William Kurkiandaa6bb22019-03-07 12:26:28 -050025 ic "github.com/opencord/voltha-protos/go/inter_container"
khenaidoo2c6a0992019-04-29 13:46:56 -040026 "github.com/opencord/voltha-protos/go/openflow_13"
William Kurkiandaa6bb22019-03-07 12:26:28 -050027 "github.com/opencord/voltha-protos/go/voltha"
khenaidood2b6df92018-12-13 16:37:20 -050028 "google.golang.org/grpc/codes"
29 "google.golang.org/grpc/status"
30)
31
32type RequestHandlerProxy struct {
33 TestMode bool
34 coreInstanceId string
35 adapter adapters.IAdapter
khenaidoo2c6a0992019-04-29 13:46:56 -040036 coreProxy *CoreProxy
khenaidood2b6df92018-12-13 16:37:20 -050037}
38
khenaidoo54e0ddf2019-02-27 16:21:33 -050039func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy *CoreProxy) *RequestHandlerProxy {
khenaidood2b6df92018-12-13 16:37:20 -050040 var proxy RequestHandlerProxy
41 proxy.coreInstanceId = coreInstanceId
42 proxy.adapter = iadapter
khenaidoo54e0ddf2019-02-27 16:21:33 -050043 proxy.coreProxy = cProxy
khenaidood2b6df92018-12-13 16:37:20 -050044 return &proxy
45}
46
47func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) {
48 return new(empty.Empty), nil
49}
50
51func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) {
52 return nil, nil
53}
54
55func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) {
56 return nil, nil
57}
58
59func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -050060 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -050061 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{}
khenaidoo6d055132019-02-12 16:51:19 -050066 transactionID := &ic.StrType{}
khenaidoo54e0ddf2019-02-27 16:21:33 -050067 fromTopic := &ic.StrType{}
khenaidoo6d055132019-02-12 16:51:19 -050068 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 }
khenaidoo54e0ddf2019-02-27 16:21:33 -050080 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 }
khenaidoo6d055132019-02-12 16:51:19 -050085 }
khenaidood2b6df92018-12-13 16:37:20 -050086 }
khenaidoo6d055132019-02-12 16:51:19 -050087
khenaidood2b6df92018-12-13 16:37:20 -050088 log.Debugw("Adopt_device", log.Fields{"deviceId": device.Id})
89
khenaidoo54e0ddf2019-02-27 16:21:33 -050090 //Update the core reference for that device
91 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
92
khenaidood2b6df92018-12-13 16:37:20 -050093 //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
101func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) {
102 return new(empty.Empty), nil
103}
104
105func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) {
106 return new(empty.Empty), nil
107}
108
109func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo3ab34882019-05-02 21:33:30 -0400110 if len(args) < 3 {
111 log.Warn("invalid-number-of-args", log.Fields{"args": args})
112 err := errors.New("invalid-number-of-args")
113 return nil, err
114 }
115
116 device := &voltha.Device{}
117 transactionID := &ic.StrType{}
118 fromTopic := &ic.StrType{}
119 for _, arg := range args {
120 switch arg.Key {
121 case "device":
122 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
123 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
124 return nil, err
125 }
126 case kafka.TransactionKey:
127 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
128 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
129 return nil, err
130 }
131 case kafka.FromTopic:
132 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
133 log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
134 return nil, err
135 }
136 }
137 }
138 //Update the core reference for that device
139 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
140 //Invoke the Disable_device API on the adapter
141 if err := rhp.adapter.Disable_device(device); err != nil {
142 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
143 }
khenaidood2b6df92018-12-13 16:37:20 -0500144 return new(empty.Empty), nil
145}
146
147func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo3ab34882019-05-02 21:33:30 -0400148 if len(args) < 3 {
149 log.Warn("invalid-number-of-args", log.Fields{"args": args})
150 err := errors.New("invalid-number-of-args")
151 return nil, err
152 }
153
154 device := &voltha.Device{}
155 transactionID := &ic.StrType{}
156 fromTopic := &ic.StrType{}
157 for _, arg := range args {
158 switch arg.Key {
159 case "device":
160 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
161 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
162 return nil, err
163 }
164 case kafka.TransactionKey:
165 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
166 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
167 return nil, err
168 }
169 case kafka.FromTopic:
170 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
171 log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
172 return nil, err
173 }
174 }
175 }
176 //Update the core reference for that device
177 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
178 //Invoke the Reenable_device API on the adapter
179 if err := rhp.adapter.Reenable_device(device); err != nil {
180 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
181 }
khenaidood2b6df92018-12-13 16:37:20 -0500182 return new(empty.Empty), nil
183}
184
185func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) {
186 return new(empty.Empty), nil
187}
188
189func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) {
190 return new(empty.Empty), nil
191}
192
193func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo0a822f92019-05-08 15:15:57 -0400194 if len(args) < 3 {
195 log.Warn("invalid-number-of-args", log.Fields{"args": args})
196 err := errors.New("invalid-number-of-args")
197 return nil, err
198 }
199
200 device := &voltha.Device{}
201 transactionID := &ic.StrType{}
202 fromTopic := &ic.StrType{}
203 for _, arg := range args {
204 switch arg.Key {
205 case "device":
206 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
207 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
208 return nil, err
209 }
210 case kafka.TransactionKey:
211 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
212 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
213 return nil, err
214 }
215 case kafka.FromTopic:
216 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
217 log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
218 return nil, err
219 }
220 }
221 }
222 //Update the core reference for that device
223 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
224 //Invoke the Disable_device API on the adapter
225 if err := rhp.adapter.Delete_device(device); err != nil {
226 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
227 }
khenaidood2b6df92018-12-13 16:37:20 -0500228 return new(empty.Empty), nil
229}
230
231func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) {
232 return new(empty.Empty), nil
233}
234
235func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) {
236 return new(empty.Empty), nil
237}
238
239func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400240 log.Debug("Update_flows_incrementally")
mkoodali252f7672019-03-25 12:13:12 +0530241 if len(args) < 3 {
242 log.Warn("Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args})
243 err := errors.New("invalid-number-of-args")
244 return nil, err
245 }
246 device := &voltha.Device{}
247 transactionID := &ic.StrType{}
khenaidoo2c6a0992019-04-29 13:46:56 -0400248 flows := &openflow_13.FlowChanges{}
249 groups := &openflow_13.FlowGroupChanges{}
mkoodali252f7672019-03-25 12:13:12 +0530250 for _, arg := range args {
251 switch arg.Key {
252 case "device":
253 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
254 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
255 return nil, err
256 }
257 case "flow_changes":
258 if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
259 log.Warnw("cannot-unmarshal-flows", log.Fields{"error": err})
260 return nil, err
261 }
262 case "group_changes":
263 if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
264 log.Warnw("cannot-unmarshal-groups", log.Fields{"error": err})
265 return nil, err
266 }
267 case kafka.TransactionKey:
268 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
269 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
270 return nil, err
271 }
272 }
273 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400274 log.Debugw("Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups})
275 //Invoke the adopt device on the adapter
276 if err := rhp.adapter.Update_flows_incrementally(device, flows, groups); err != nil {
277 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
278 }
khenaidood2b6df92018-12-13 16:37:20 -0500279 return new(empty.Empty), nil
280}
281
282func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) {
283 return new(empty.Empty), nil
284}
285
286func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) {
manikkaraj k6c9689d2019-05-09 12:59:52 -0400287 log.Debugw("Receive_packet_out", log.Fields{"args": args})
288 if len(args) < 3 {
289 log.Warn("Receive_packet_out-invalid-number-of-args", log.Fields{"args": args})
290 err := errors.New("invalid-number-of-args")
291 return nil, err
292 }
293 deviceId := &ic.StrType{}
294 egressPort := &ic.IntType{}
295 packet := &openflow_13.OfpPacketOut{}
296 transactionID := &ic.StrType{}
297 for _, arg := range args {
298 switch arg.Key {
299 case "deviceId":
300 if err := ptypes.UnmarshalAny(arg.Value, deviceId); err != nil {
301 log.Warnw("cannot-unmarshal-deviceId", log.Fields{"error": err})
302 return nil, err
303 }
304 case "outPort":
305 if err := ptypes.UnmarshalAny(arg.Value, egressPort); err != nil {
306 log.Warnw("cannot-unmarshal-egressPort", log.Fields{"error": err})
307 return nil, err
308 }
309 case "packet":
310 if err := ptypes.UnmarshalAny(arg.Value, packet); err != nil {
311 log.Warnw("cannot-unmarshal-packet", log.Fields{"error": err})
312 return nil, err
313 }
314 case kafka.TransactionKey:
315 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
316 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
317 return nil, err
318 }
319 }
320 }
321 log.Debugw("Receive_packet_out", log.Fields{"deviceId": deviceId.Val, "outPort": egressPort, "packet": packet})
322 //Invoke the adopt device on the adapter
323 if err := rhp.adapter.Receive_packet_out(deviceId.Val, int(egressPort.Val), packet); err != nil {
324 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
325 }
khenaidood2b6df92018-12-13 16:37:20 -0500326 return new(empty.Empty), nil
327}
328
329func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
330 return new(empty.Empty), nil
331}
332
333func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
334 return new(empty.Empty), nil
335}
336
337func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500338 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500339 log.Warn("invalid-number-of-args", log.Fields{"args": args})
340 err := errors.New("invalid-number-of-args")
341 return nil, err
342 }
343 device := &voltha.Device{}
khenaidoo6d055132019-02-12 16:51:19 -0500344 transactionID := &ic.StrType{}
345 for _, arg := range args {
346 switch arg.Key {
347 case "device":
348 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
349 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
350 return nil, err
351 }
352 case kafka.TransactionKey:
353 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
354 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
355 return nil, err
356 }
357 }
khenaidood2b6df92018-12-13 16:37:20 -0500358 }
khenaidoo6d055132019-02-12 16:51:19 -0500359
khenaidood2b6df92018-12-13 16:37:20 -0500360 log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
361
362 var cap *ic.SwitchCapability
363 var err error
364 if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil {
365 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
366 }
khenaidoo6d055132019-02-12 16:51:19 -0500367 log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap})
khenaidood2b6df92018-12-13 16:37:20 -0500368 return cap, nil
369}
370
371func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500372 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -0500373 log.Warn("invalid-number-of-args", log.Fields{"args": args})
374 err := errors.New("invalid-number-of-args")
375 return nil, err
376 }
377 device := &voltha.Device{}
378 pNo := &ic.IntType{}
khenaidoo6d055132019-02-12 16:51:19 -0500379 transactionID := &ic.StrType{}
khenaidood2b6df92018-12-13 16:37:20 -0500380 for _, arg := range args {
381 switch arg.Key {
382 case "device":
383 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
384 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
385 return nil, err
386 }
387 case "port_no":
388 if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil {
389 log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err})
390 return nil, err
391 }
khenaidoo6d055132019-02-12 16:51:19 -0500392 case kafka.TransactionKey:
393 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
394 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
395 return nil, err
396 }
khenaidood2b6df92018-12-13 16:37:20 -0500397 }
398 }
399 log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val})
400 var cap *ic.PortCapability
401 var err error
402 if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil {
403 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
404 }
405 return cap, nil
406}
407
408func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500409 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500410 log.Warn("invalid-number-of-args", log.Fields{"args": args})
411 err := errors.New("invalid-number-of-args")
412 return nil, err
413 }
414 iaMsg := &ic.InterAdapterMessage{}
khenaidoo6d055132019-02-12 16:51:19 -0500415 transactionID := &ic.StrType{}
416 for _, arg := range args {
417 switch arg.Key {
418 case "msg":
419 if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil {
420 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
421 return nil, err
422 }
423 case kafka.TransactionKey:
424 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
425 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
426 return nil, err
427 }
428 }
khenaidood2b6df92018-12-13 16:37:20 -0500429 }
430
431 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
432
433 //Invoke the inter adapter API on the handler
434 if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil {
435 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
436 }
437
438 return new(empty.Empty), nil
439}
440
khenaidoof5a5bfa2019-01-23 22:20:29 -0500441func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) {
442 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500443}
444
khenaidoof5a5bfa2019-01-23 22:20:29 -0500445func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) {
446 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500447}
448
khenaidoof5a5bfa2019-01-23 22:20:29 -0500449func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) {
450 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500451}
452
khenaidoof5a5bfa2019-01-23 22:20:29 -0500453func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
454 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500455}
456
khenaidoof5a5bfa2019-01-23 22:20:29 -0500457func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
458 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500459}