blob: dd8df8c7be53ec49e9dd6d2e2c72572f8ff78e42 [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) {
110 return new(empty.Empty), nil
111}
112
113func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) {
114 return new(empty.Empty), nil
115}
116
117func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) {
118 return new(empty.Empty), nil
119}
120
121func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) {
122 return new(empty.Empty), nil
123}
124
125func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) {
126 return new(empty.Empty), nil
127}
128
129func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) {
130 return new(empty.Empty), nil
131}
132
133func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) {
134 return new(empty.Empty), nil
135}
136
137func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo2c6a0992019-04-29 13:46:56 -0400138 log.Debug("Update_flows_incrementally")
mkoodali252f7672019-03-25 12:13:12 +0530139 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{}
khenaidoo2c6a0992019-04-29 13:46:56 -0400146 flows := &openflow_13.FlowChanges{}
147 groups := &openflow_13.FlowGroupChanges{}
mkoodali252f7672019-03-25 12:13:12 +0530148 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 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400172 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 }
khenaidood2b6df92018-12-13 16:37:20 -0500177 return new(empty.Empty), nil
178}
179
180func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) {
181 return new(empty.Empty), nil
182}
183
184func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) {
185 return new(empty.Empty), nil
186}
187
188func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
189 return new(empty.Empty), nil
190}
191
192func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
193 return new(empty.Empty), nil
194}
195
196func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500197 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500198 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{}
khenaidoo6d055132019-02-12 16:51:19 -0500203 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 }
khenaidood2b6df92018-12-13 16:37:20 -0500217 }
khenaidoo6d055132019-02-12 16:51:19 -0500218
khenaidood2b6df92018-12-13 16:37:20 -0500219 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 }
khenaidoo6d055132019-02-12 16:51:19 -0500226 log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap})
khenaidood2b6df92018-12-13 16:37:20 -0500227 return cap, nil
228}
229
230func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500231 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -0500232 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{}
khenaidoo6d055132019-02-12 16:51:19 -0500238 transactionID := &ic.StrType{}
khenaidood2b6df92018-12-13 16:37:20 -0500239 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 }
khenaidoo6d055132019-02-12 16:51:19 -0500251 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 }
khenaidood2b6df92018-12-13 16:37:20 -0500256 }
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
267func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500268 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500269 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{}
khenaidoo6d055132019-02-12 16:51:19 -0500274 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 }
khenaidood2b6df92018-12-13 16:37:20 -0500288 }
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
khenaidoof5a5bfa2019-01-23 22:20:29 -0500300func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) {
301 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500302}
303
khenaidoof5a5bfa2019-01-23 22:20:29 -0500304func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) {
305 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500306}
307
khenaidoof5a5bfa2019-01-23 22:20:29 -0500308func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) {
309 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500310}
311
khenaidoof5a5bfa2019-01-23 22:20:29 -0500312func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
313 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500314}
315
khenaidoof5a5bfa2019-01-23 22:20:29 -0500316func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
317 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500318}