blob: 8b582b854e04fa0705fd751cc0604a952e1c57b8 [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) {
194 return new(empty.Empty), nil
195}
196
197func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) {
198 return new(empty.Empty), nil
199}
200
201func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) {
202 return new(empty.Empty), nil
203}
204
205func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400206 log.Debug("Update_flows_incrementally")
mkoodali252f7672019-03-25 12:13:12 +0530207 if len(args) < 3 {
208 log.Warn("Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args})
209 err := errors.New("invalid-number-of-args")
210 return nil, err
211 }
212 device := &voltha.Device{}
213 transactionID := &ic.StrType{}
khenaidoo2c6a0992019-04-29 13:46:56 -0400214 flows := &openflow_13.FlowChanges{}
215 groups := &openflow_13.FlowGroupChanges{}
mkoodali252f7672019-03-25 12:13:12 +0530216 for _, arg := range args {
217 switch arg.Key {
218 case "device":
219 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
220 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
221 return nil, err
222 }
223 case "flow_changes":
224 if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
225 log.Warnw("cannot-unmarshal-flows", log.Fields{"error": err})
226 return nil, err
227 }
228 case "group_changes":
229 if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
230 log.Warnw("cannot-unmarshal-groups", log.Fields{"error": err})
231 return nil, err
232 }
233 case kafka.TransactionKey:
234 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
235 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
236 return nil, err
237 }
238 }
239 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400240 log.Debugw("Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups})
241 //Invoke the adopt device on the adapter
242 if err := rhp.adapter.Update_flows_incrementally(device, flows, groups); err != nil {
243 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
244 }
khenaidood2b6df92018-12-13 16:37:20 -0500245 return new(empty.Empty), nil
246}
247
248func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) {
249 return new(empty.Empty), nil
250}
251
252func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) {
253 return new(empty.Empty), nil
254}
255
256func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
257 return new(empty.Empty), nil
258}
259
260func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
261 return new(empty.Empty), nil
262}
263
264func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500265 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500266 log.Warn("invalid-number-of-args", log.Fields{"args": args})
267 err := errors.New("invalid-number-of-args")
268 return nil, err
269 }
270 device := &voltha.Device{}
khenaidoo6d055132019-02-12 16:51:19 -0500271 transactionID := &ic.StrType{}
272 for _, arg := range args {
273 switch arg.Key {
274 case "device":
275 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
276 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
277 return nil, err
278 }
279 case kafka.TransactionKey:
280 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
281 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
282 return nil, err
283 }
284 }
khenaidood2b6df92018-12-13 16:37:20 -0500285 }
khenaidoo6d055132019-02-12 16:51:19 -0500286
khenaidood2b6df92018-12-13 16:37:20 -0500287 log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
288
289 var cap *ic.SwitchCapability
290 var err error
291 if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil {
292 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
293 }
khenaidoo6d055132019-02-12 16:51:19 -0500294 log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap})
khenaidood2b6df92018-12-13 16:37:20 -0500295 return cap, nil
296}
297
298func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500299 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -0500300 log.Warn("invalid-number-of-args", log.Fields{"args": args})
301 err := errors.New("invalid-number-of-args")
302 return nil, err
303 }
304 device := &voltha.Device{}
305 pNo := &ic.IntType{}
khenaidoo6d055132019-02-12 16:51:19 -0500306 transactionID := &ic.StrType{}
khenaidood2b6df92018-12-13 16:37:20 -0500307 for _, arg := range args {
308 switch arg.Key {
309 case "device":
310 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
311 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
312 return nil, err
313 }
314 case "port_no":
315 if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil {
316 log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err})
317 return nil, err
318 }
khenaidoo6d055132019-02-12 16:51:19 -0500319 case kafka.TransactionKey:
320 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
321 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
322 return nil, err
323 }
khenaidood2b6df92018-12-13 16:37:20 -0500324 }
325 }
326 log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val})
327 var cap *ic.PortCapability
328 var err error
329 if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil {
330 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
331 }
332 return cap, nil
333}
334
335func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500336 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500337 log.Warn("invalid-number-of-args", log.Fields{"args": args})
338 err := errors.New("invalid-number-of-args")
339 return nil, err
340 }
341 iaMsg := &ic.InterAdapterMessage{}
khenaidoo6d055132019-02-12 16:51:19 -0500342 transactionID := &ic.StrType{}
343 for _, arg := range args {
344 switch arg.Key {
345 case "msg":
346 if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil {
347 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
348 return nil, err
349 }
350 case kafka.TransactionKey:
351 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
352 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
353 return nil, err
354 }
355 }
khenaidood2b6df92018-12-13 16:37:20 -0500356 }
357
358 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
359
360 //Invoke the inter adapter API on the handler
361 if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil {
362 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
363 }
364
365 return new(empty.Empty), nil
366}
367
khenaidoof5a5bfa2019-01-23 22:20:29 -0500368func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) {
369 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500370}
371
khenaidoof5a5bfa2019-01-23 22:20:29 -0500372func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) {
373 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500374}
375
khenaidoof5a5bfa2019-01-23 22:20:29 -0500376func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) {
377 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500378}
379
khenaidoof5a5bfa2019-01-23 22:20:29 -0500380func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
381 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500382}
383
khenaidoof5a5bfa2019-01-23 22:20:29 -0500384func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
385 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500386}