blob: a92ed51e51e61b7e6f0fff2b165718304e0853a6 [file] [log] [blame]
Scott Baker2c1c4822019-10-16 11:02:41 -07001/*
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 (
Neha Sharma94f16a92020-06-26 04:17:55 +000019 "context"
Scott Baker2c1c4822019-10-16 11:02:41 -070020 "errors"
serkant.uluderyab38671c2019-11-01 09:35:38 -070021
Scott Baker2c1c4822019-10-16 11:02:41 -070022 "github.com/golang/protobuf/ptypes"
23 "github.com/golang/protobuf/ptypes/empty"
serkant.uluderyab38671c2019-11-01 09:35:38 -070024 "github.com/opencord/voltha-lib-go/v3/pkg/adapters"
25 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
26 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
29 "github.com/opencord/voltha-protos/v3/go/openflow_13"
30 "github.com/opencord/voltha-protos/v3/go/voltha"
Scott Baker2c1c4822019-10-16 11:02:41 -070031 "google.golang.org/grpc/codes"
32 "google.golang.org/grpc/status"
33)
34
35type RequestHandlerProxy struct {
36 TestMode bool
37 coreInstanceId string
38 adapter adapters.IAdapter
39 coreProxy adapterif.CoreProxy
40}
41
42func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy adapterif.CoreProxy) *RequestHandlerProxy {
43 var proxy RequestHandlerProxy
44 proxy.coreInstanceId = coreInstanceId
45 proxy.adapter = iadapter
46 proxy.coreProxy = cProxy
47 return &proxy
48}
49
50func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) {
51 return new(empty.Empty), nil
52}
53
54func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) {
55 return nil, nil
56}
57
58func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) {
59 return nil, nil
60}
61
Neha Sharma94f16a92020-06-26 04:17:55 +000062func (rhp *RequestHandlerProxy) Adopt_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -070063 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +000064 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -070065 err := errors.New("invalid-number-of-args")
66 return nil, err
67 }
68 device := &voltha.Device{}
69 transactionID := &ic.StrType{}
70 fromTopic := &ic.StrType{}
71 for _, arg := range args {
72 switch arg.Key {
73 case "device":
74 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000075 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -070076 return nil, err
77 }
78 case kafka.TransactionKey:
79 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000080 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -070081 return nil, err
82 }
83 case kafka.FromTopic:
84 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +000085 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -070086 return nil, err
87 }
88 }
89 }
90
Neha Sharma94f16a92020-06-26 04:17:55 +000091 logger.Debugw(ctx, "Adopt_device", log.Fields{"deviceId": device.Id})
Scott Baker2c1c4822019-10-16 11:02:41 -070092
93 //Update the core reference for that device
94 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
95
96 //Invoke the adopt device on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +000097 if err := rhp.adapter.Adopt_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -070098 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
99 }
100
101 return new(empty.Empty), nil
102}
103
Neha Sharma94f16a92020-06-26 04:17:55 +0000104func (rhp *RequestHandlerProxy) Reconcile_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700105 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000106 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700107 err := errors.New("invalid-number-of-args")
108 return nil, err
109 }
110
111 device := &voltha.Device{}
112 transactionID := &ic.StrType{}
113 fromTopic := &ic.StrType{}
114 for _, arg := range args {
115 switch arg.Key {
116 case "device":
117 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000118 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700119 return nil, err
120 }
121 case kafka.TransactionKey:
122 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000123 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700124 return nil, err
125 }
126 case kafka.FromTopic:
127 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000128 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700129 return nil, err
130 }
131 }
132 }
133 //Update the core reference for that device
134 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
135
136 //Invoke the reconcile device API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000137 if err := rhp.adapter.Reconcile_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700138 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
139 }
140 return new(empty.Empty), nil
141}
142
143func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) {
144 return new(empty.Empty), nil
145}
146
Neha Sharma94f16a92020-06-26 04:17:55 +0000147func (rhp *RequestHandlerProxy) Disable_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700148 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000149 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700150 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 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000161 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700162 return nil, err
163 }
164 case kafka.TransactionKey:
165 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000166 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700167 return nil, err
168 }
169 case kafka.FromTopic:
170 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000171 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700172 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 Disable_device API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000179 if err := rhp.adapter.Disable_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700180 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
181 }
182 return new(empty.Empty), nil
183}
184
Neha Sharma94f16a92020-06-26 04:17:55 +0000185func (rhp *RequestHandlerProxy) Reenable_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700186 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000187 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700188 err := errors.New("invalid-number-of-args")
189 return nil, err
190 }
191
192 device := &voltha.Device{}
193 transactionID := &ic.StrType{}
194 fromTopic := &ic.StrType{}
195 for _, arg := range args {
196 switch arg.Key {
197 case "device":
198 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000199 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700200 return nil, err
201 }
202 case kafka.TransactionKey:
203 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000204 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700205 return nil, err
206 }
207 case kafka.FromTopic:
208 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000209 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700210 return nil, err
211 }
212 }
213 }
214 //Update the core reference for that device
215 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
216 //Invoke the Reenable_device API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000217 if err := rhp.adapter.Reenable_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700218 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
219 }
220 return new(empty.Empty), nil
221}
222
Neha Sharma94f16a92020-06-26 04:17:55 +0000223func (rhp *RequestHandlerProxy) Reboot_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700224 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000225 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700226 err := errors.New("invalid-number-of-args")
227 return nil, err
228 }
229
230 device := &voltha.Device{}
231 transactionID := &ic.StrType{}
232 fromTopic := &ic.StrType{}
233 for _, arg := range args {
234 switch arg.Key {
235 case "device":
236 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000237 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700238 return nil, err
239 }
240 case kafka.TransactionKey:
241 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000242 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700243 return nil, err
244 }
245 case kafka.FromTopic:
246 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000247 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700248 return nil, err
249 }
250 }
251 }
252 //Update the core reference for that device
253 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
254 //Invoke the Reboot_device API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000255 if err := rhp.adapter.Reboot_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700256 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
257 }
258 return new(empty.Empty), nil
259
260}
261
262func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) {
263 return new(empty.Empty), nil
264}
265
Neha Sharma94f16a92020-06-26 04:17:55 +0000266func (rhp *RequestHandlerProxy) Delete_device(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700267 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000268 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700269 err := errors.New("invalid-number-of-args")
270 return nil, err
271 }
272
273 device := &voltha.Device{}
274 transactionID := &ic.StrType{}
275 fromTopic := &ic.StrType{}
276 for _, arg := range args {
277 switch arg.Key {
278 case "device":
279 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000280 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700281 return nil, err
282 }
283 case kafka.TransactionKey:
284 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000285 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700286 return nil, err
287 }
288 case kafka.FromTopic:
289 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000290 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700291 return nil, err
292 }
293 }
294 }
295 //Update the core reference for that device
296 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
297 //Invoke the delete_device API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000298 if err := rhp.adapter.Delete_device(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700299 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
300 }
301 return new(empty.Empty), nil
302}
303
304func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) {
305 return new(empty.Empty), nil
306}
307
Neha Sharma94f16a92020-06-26 04:17:55 +0000308func (rhp *RequestHandlerProxy) Update_flows_bulk(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
309 logger.Debug(ctx, "Update_flows_bulk")
Scott Baker2c1c4822019-10-16 11:02:41 -0700310 if len(args) < 5 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000311 logger.Warn(ctx, "Update_flows_bulk-invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700312 err := errors.New("invalid-number-of-args")
313 return nil, err
314 }
315 device := &voltha.Device{}
316 transactionID := &ic.StrType{}
317 flows := &voltha.Flows{}
318 flowMetadata := &voltha.FlowMetadata{}
319 groups := &voltha.FlowGroups{}
320 for _, arg := range args {
321 switch arg.Key {
322 case "device":
323 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000324 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700325 return nil, err
326 }
327 case "flows":
328 if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000329 logger.Warnw(ctx, "cannot-unmarshal-flows", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700330 return nil, err
331 }
332 case "groups":
333 if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000334 logger.Warnw(ctx, "cannot-unmarshal-groups", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700335 return nil, err
336 }
337 case "flow_metadata":
338 if err := ptypes.UnmarshalAny(arg.Value, flowMetadata); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000339 logger.Warnw(ctx, "cannot-unmarshal-metadata", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700340 return nil, err
341 }
342 case kafka.TransactionKey:
343 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000344 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700345 return nil, err
346 }
347 }
348 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000349 logger.Debugw(ctx, "Update_flows_bulk", log.Fields{"flows": flows, "groups": groups})
Scott Baker2c1c4822019-10-16 11:02:41 -0700350 //Invoke the bulk flow update API of the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000351 if err := rhp.adapter.Update_flows_bulk(ctx, device, flows, groups, flowMetadata); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700352 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
353 }
354 return new(empty.Empty), nil
355}
356
Neha Sharma94f16a92020-06-26 04:17:55 +0000357func (rhp *RequestHandlerProxy) Update_flows_incrementally(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
358 logger.Debug(ctx, "Update_flows_incrementally")
Scott Baker2c1c4822019-10-16 11:02:41 -0700359 if len(args) < 5 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000360 logger.Warn(ctx, "Update_flows_incrementally-invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700361 err := errors.New("invalid-number-of-args")
362 return nil, err
363 }
364 device := &voltha.Device{}
365 transactionID := &ic.StrType{}
366 flows := &openflow_13.FlowChanges{}
367 flowMetadata := &voltha.FlowMetadata{}
368 groups := &openflow_13.FlowGroupChanges{}
369 for _, arg := range args {
370 switch arg.Key {
371 case "device":
372 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000373 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700374 return nil, err
375 }
376 case "flow_changes":
377 if err := ptypes.UnmarshalAny(arg.Value, flows); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000378 logger.Warnw(ctx, "cannot-unmarshal-flows", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700379 return nil, err
380 }
381 case "group_changes":
382 if err := ptypes.UnmarshalAny(arg.Value, groups); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000383 logger.Warnw(ctx, "cannot-unmarshal-groups", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700384 return nil, err
385 }
386 case "flow_metadata":
387 if err := ptypes.UnmarshalAny(arg.Value, flowMetadata); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000388 logger.Warnw(ctx, "cannot-unmarshal-metadata", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700389 return nil, err
390 }
391 case kafka.TransactionKey:
392 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000393 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700394 return nil, err
395 }
396 }
397 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000398 logger.Debugw(ctx, "Update_flows_incrementally", log.Fields{"flows": flows, "groups": groups})
Scott Baker2c1c4822019-10-16 11:02:41 -0700399 //Invoke the incremental flow update API of the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000400 if err := rhp.adapter.Update_flows_incrementally(ctx, device, flows, groups, flowMetadata); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700401 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
402 }
403 return new(empty.Empty), nil
404}
405
Neha Sharma94f16a92020-06-26 04:17:55 +0000406func (rhp *RequestHandlerProxy) Update_pm_config(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
407 logger.Debug(ctx, "Update_pm_config")
Scott Baker2c1c4822019-10-16 11:02:41 -0700408 if len(args) < 2 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000409 logger.Warn(ctx, "Update_pm_config-invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700410 err := errors.New("invalid-number-of-args")
411 return nil, err
412 }
413 device := &voltha.Device{}
414 transactionID := &ic.StrType{}
415 pmConfigs := &voltha.PmConfigs{}
416 for _, arg := range args {
417 switch arg.Key {
418 case "device":
419 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000420 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700421 return nil, err
422 }
423 case "pm_configs":
424 if err := ptypes.UnmarshalAny(arg.Value, pmConfigs); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000425 logger.Warnw(ctx, "cannot-unmarshal-pm-configs", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700426 return nil, err
427 }
428 case kafka.TransactionKey:
429 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000430 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700431 return nil, err
432 }
433 }
434 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000435 logger.Debugw(ctx, "Update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs})
Scott Baker2c1c4822019-10-16 11:02:41 -0700436 //Invoke the pm config update API of the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000437 if err := rhp.adapter.Update_pm_config(ctx, device, pmConfigs); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700438 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
439 }
440 return new(empty.Empty), nil
441}
442
Neha Sharma94f16a92020-06-26 04:17:55 +0000443func (rhp *RequestHandlerProxy) Receive_packet_out(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
444 logger.Debugw(ctx, "Receive_packet_out", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700445 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000446 logger.Warn(ctx, "Receive_packet_out-invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700447 err := errors.New("invalid-number-of-args")
448 return nil, err
449 }
450 deviceId := &ic.StrType{}
451 egressPort := &ic.IntType{}
452 packet := &openflow_13.OfpPacketOut{}
453 transactionID := &ic.StrType{}
454 for _, arg := range args {
455 switch arg.Key {
456 case "deviceId":
457 if err := ptypes.UnmarshalAny(arg.Value, deviceId); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000458 logger.Warnw(ctx, "cannot-unmarshal-deviceId", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700459 return nil, err
460 }
461 case "outPort":
462 if err := ptypes.UnmarshalAny(arg.Value, egressPort); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000463 logger.Warnw(ctx, "cannot-unmarshal-egressPort", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700464 return nil, err
465 }
466 case "packet":
467 if err := ptypes.UnmarshalAny(arg.Value, packet); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000468 logger.Warnw(ctx, "cannot-unmarshal-packet", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700469 return nil, err
470 }
471 case kafka.TransactionKey:
472 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000473 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700474 return nil, err
475 }
476 }
477 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000478 logger.Debugw(ctx, "Receive_packet_out", log.Fields{"deviceId": deviceId.Val, "outPort": egressPort, "packet": packet})
Scott Baker2c1c4822019-10-16 11:02:41 -0700479 //Invoke the adopt device on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000480 if err := rhp.adapter.Receive_packet_out(ctx, deviceId.Val, int(egressPort.Val), packet); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700481 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
482 }
483 return new(empty.Empty), nil
484}
485
486func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
487 return new(empty.Empty), nil
488}
489
490func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
491 return new(empty.Empty), nil
492}
493
Neha Sharma94f16a92020-06-26 04:17:55 +0000494func (rhp *RequestHandlerProxy) Get_ofp_device_info(ctx context.Context, args []*ic.Argument) (*ic.SwitchCapability, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700495 if len(args) < 2 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000496 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700497 err := errors.New("invalid-number-of-args")
498 return nil, err
499 }
500 device := &voltha.Device{}
501 transactionID := &ic.StrType{}
502 for _, arg := range args {
503 switch arg.Key {
504 case "device":
505 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000506 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700507 return nil, err
508 }
509 case kafka.TransactionKey:
510 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000511 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700512 return nil, err
513 }
514 }
515 }
516
Neha Sharma94f16a92020-06-26 04:17:55 +0000517 logger.Debugw(ctx, "Get_ofp_device_info", log.Fields{"deviceId": device.Id})
Scott Baker2c1c4822019-10-16 11:02:41 -0700518
519 var cap *ic.SwitchCapability
520 var err error
Neha Sharma94f16a92020-06-26 04:17:55 +0000521 if cap, err = rhp.adapter.Get_ofp_device_info(ctx, device); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700522 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
523 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000524 logger.Debugw(ctx, "Get_ofp_device_info", log.Fields{"cap": cap})
Scott Baker2c1c4822019-10-16 11:02:41 -0700525 return cap, nil
526}
527
Neha Sharma94f16a92020-06-26 04:17:55 +0000528func (rhp *RequestHandlerProxy) Process_inter_adapter_message(ctx context.Context, args []*ic.Argument) (*empty.Empty, error) {
Scott Baker2c1c4822019-10-16 11:02:41 -0700529 if len(args) < 2 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000530 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker2c1c4822019-10-16 11:02:41 -0700531 err := errors.New("invalid-number-of-args")
532 return nil, err
533 }
534 iaMsg := &ic.InterAdapterMessage{}
535 transactionID := &ic.StrType{}
536 for _, arg := range args {
537 switch arg.Key {
538 case "msg":
539 if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000540 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700541 return nil, err
542 }
543 case kafka.TransactionKey:
544 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000545 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Scott Baker2c1c4822019-10-16 11:02:41 -0700546 return nil, err
547 }
548 }
549 }
550
Neha Sharma94f16a92020-06-26 04:17:55 +0000551 logger.Debugw(ctx, "Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
Scott Baker2c1c4822019-10-16 11:02:41 -0700552
553 //Invoke the inter adapter API on the handler
Neha Sharma94f16a92020-06-26 04:17:55 +0000554 if err := rhp.adapter.Process_inter_adapter_message(ctx, iaMsg); err != nil {
Scott Baker2c1c4822019-10-16 11:02:41 -0700555 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
556 }
557
558 return new(empty.Empty), nil
559}
560
561func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) {
562 return &voltha.ImageDownload{}, nil
563}
564
565func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) {
566 return &voltha.ImageDownload{}, nil
567}
568
569func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) {
570 return &voltha.ImageDownload{}, nil
571}
572
573func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
574 return &voltha.ImageDownload{}, nil
575}
576
577func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
578 return &voltha.ImageDownload{}, nil
579}
kesavandf17f7922020-02-03 17:00:27 -0500580
Neha Sharma94f16a92020-06-26 04:17:55 +0000581func (rhp *RequestHandlerProxy) Enable_port(ctx context.Context, args []*ic.Argument) error {
582 logger.Debugw(ctx, "enable_port", log.Fields{"args": args})
583 deviceId, port, err := rhp.getEnableDisableParams(ctx, args)
kesavandf17f7922020-02-03 17:00:27 -0500584 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000585 logger.Warnw(ctx, "enable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
kesavandf17f7922020-02-03 17:00:27 -0500586 return err
587 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000588 return rhp.adapter.Enable_port(ctx, deviceId, port)
kesavandf17f7922020-02-03 17:00:27 -0500589}
590
Neha Sharma94f16a92020-06-26 04:17:55 +0000591func (rhp *RequestHandlerProxy) Disable_port(ctx context.Context, args []*ic.Argument) error {
592 logger.Debugw(ctx, "disable_port", log.Fields{"args": args})
593 deviceId, port, err := rhp.getEnableDisableParams(ctx, args)
kesavandf17f7922020-02-03 17:00:27 -0500594 if err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000595 logger.Warnw(ctx, "disable_port", log.Fields{"args": args, "deviceId": deviceId, "port": port})
kesavandf17f7922020-02-03 17:00:27 -0500596 return err
597 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000598 return rhp.adapter.Disable_port(ctx, deviceId, port)
kesavandf17f7922020-02-03 17:00:27 -0500599}
600
Neha Sharma94f16a92020-06-26 04:17:55 +0000601func (rhp *RequestHandlerProxy) getEnableDisableParams(ctx context.Context, args []*ic.Argument) (string, *voltha.Port, error) {
602 logger.Debugw(ctx, "getEnableDisableParams", log.Fields{"args": args})
kesavandf17f7922020-02-03 17:00:27 -0500603 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000604 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
kesavandf17f7922020-02-03 17:00:27 -0500605 return "", nil, errors.New("invalid-number-of-args")
606 }
607 deviceId := &ic.StrType{}
608 port := &voltha.Port{}
609 for _, arg := range args {
610 switch arg.Key {
611 case "deviceId":
612 if err := ptypes.UnmarshalAny(arg.Value, deviceId); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000613 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
kesavandf17f7922020-02-03 17:00:27 -0500614 return "", nil, err
615 }
616 case "port":
617 if err := ptypes.UnmarshalAny(arg.Value, port); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000618 logger.Warnw(ctx, "cannot-unmarshal-port", log.Fields{"error": err})
kesavandf17f7922020-02-03 17:00:27 -0500619 return "", nil, err
620 }
621 }
622 }
623 return deviceId.Val, port, nil
624}
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500625
Neha Sharma94f16a92020-06-26 04:17:55 +0000626func (rhp *RequestHandlerProxy) Child_device_lost(ctx context.Context, args []*ic.Argument) error {
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500627 if len(args) < 4 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000628 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500629 return errors.New("invalid-number-of-args")
630 }
631
632 pDeviceId := &ic.StrType{}
633 pPortNo := &ic.IntType{}
634 onuID := &ic.IntType{}
635 fromTopic := &ic.StrType{}
636 for _, arg := range args {
637 switch arg.Key {
638 case "pDeviceId":
639 if err := ptypes.UnmarshalAny(arg.Value, pDeviceId); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000640 logger.Warnw(ctx, "cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500641 return err
642 }
643 case "pPortNo":
644 if err := ptypes.UnmarshalAny(arg.Value, pPortNo); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000645 logger.Warnw(ctx, "cannot-unmarshal-port", log.Fields{"error": err})
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500646 return err
647 }
648 case "onuID":
649 if err := ptypes.UnmarshalAny(arg.Value, onuID); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000650 logger.Warnw(ctx, "cannot-unmarshal-transaction-ID", log.Fields{"error": err})
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500651 return err
652 }
653 case kafka.FromTopic:
654 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000655 logger.Warnw(ctx, "cannot-unmarshal-from-topic", log.Fields{"error": err})
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500656 return err
657 }
658 }
659 }
660 //Update the core reference for that device
661 rhp.coreProxy.UpdateCoreReference(pDeviceId.Val, fromTopic.Val)
662 //Invoke the Child_device_lost API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000663 if err := rhp.adapter.Child_device_lost(ctx, pDeviceId.Val, uint32(pPortNo.Val), uint32(onuID.Val)); err != nil {
Chaitrashree G Sc4a5c072020-02-04 17:08:40 -0500664 return status.Errorf(codes.NotFound, "%s", err.Error())
665 }
666 return nil
667}
Scott Baker04cb5da2020-03-26 15:52:22 -0700668
Neha Sharma94f16a92020-06-26 04:17:55 +0000669func (rhp *RequestHandlerProxy) Start_omci_test(ctx context.Context, args []*ic.Argument) (*ic.TestResponse, error) {
Scott Baker04cb5da2020-03-26 15:52:22 -0700670 if len(args) < 2 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000671 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Scott Baker04cb5da2020-03-26 15:52:22 -0700672 err := errors.New("invalid-number-of-args")
673 return nil, err
674 }
675
676 // TODO: See related comment in voltha-go:adapter_proxy_go:startOmciTest()
677 // Second argument should perhaps be uuid instead of omcitestrequest
678
679 device := &voltha.Device{}
680 request := &voltha.OmciTestRequest{}
681 for _, arg := range args {
682 switch arg.Key {
683 case "device":
684 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000685 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Scott Baker04cb5da2020-03-26 15:52:22 -0700686 return nil, err
687 }
688 case "omcitestrequest":
689 if err := ptypes.UnmarshalAny(arg.Value, request); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000690 logger.Warnw(ctx, "cannot-unmarshal-omcitestrequest", log.Fields{"error": err})
Scott Baker04cb5da2020-03-26 15:52:22 -0700691 return nil, err
692 }
693 }
694 }
Neha Sharma94f16a92020-06-26 04:17:55 +0000695 logger.Debugw(ctx, "Start_omci_test", log.Fields{"device-id": device.Id, "req": request})
696 result, err := rhp.adapter.Start_omci_test(ctx, device, request)
Scott Baker04cb5da2020-03-26 15:52:22 -0700697 if err != nil {
698 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
699 }
700 return result, nil
701}
Neha Sharma94f16a92020-06-26 04:17:55 +0000702func (rhp *RequestHandlerProxy) Get_ext_value(ctx context.Context, args []*ic.Argument) (*voltha.ReturnValues, error) {
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800703 if len(args) < 3 {
Neha Sharma94f16a92020-06-26 04:17:55 +0000704 logger.Warn(ctx, "invalid-number-of-args", log.Fields{"args": args})
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800705 return nil, errors.New("invalid-number-of-args")
706 }
707
708 pDeviceId := &ic.StrType{}
709 device := &voltha.Device{}
710 valuetype := &ic.IntType{}
711 for _, arg := range args {
712 switch arg.Key {
713 case "device":
714 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000715 logger.Warnw(ctx, "cannot-unmarshal-device", log.Fields{"error": err})
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800716 return nil, err
717 }
718 case "pDeviceId":
719 if err := ptypes.UnmarshalAny(arg.Value, pDeviceId); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000720 logger.Warnw(ctx, "cannot-unmarshal-parent-deviceId", log.Fields{"error": err})
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800721 return nil, err
722 }
723 case "valuetype":
724 if err := ptypes.UnmarshalAny(arg.Value, valuetype); err != nil {
Neha Sharma94f16a92020-06-26 04:17:55 +0000725 logger.Warnw(ctx, "cannot-unmarshal-valuetype", log.Fields{"error": err})
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800726 return nil, err
727 }
728 default:
Neha Sharma94f16a92020-06-26 04:17:55 +0000729 logger.Warnw(ctx, "key-not-found", log.Fields{"arg.Key": arg.Key})
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800730 }
731 }
732
733 //Invoke the Get_value API on the adapter
Neha Sharma94f16a92020-06-26 04:17:55 +0000734 return rhp.adapter.Get_ext_value(ctx, pDeviceId.Val, device, voltha.ValueType_Type(valuetype.Val))
Dinesh Belwalkar4c89a1d2020-02-28 16:05:32 -0800735}