blob: 5b839c6f9f2b491eff4f11f9d6c60f04b5576bb1 [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"
26 "github.com/opencord/voltha-protos/go/voltha"
khenaidood2b6df92018-12-13 16:37:20 -050027 "google.golang.org/grpc/codes"
28 "google.golang.org/grpc/status"
29)
30
31type RequestHandlerProxy struct {
32 TestMode bool
33 coreInstanceId string
34 adapter adapters.IAdapter
khenaidoo54e0ddf2019-02-27 16:21:33 -050035 coreProxy *CoreProxy
khenaidood2b6df92018-12-13 16:37:20 -050036}
37
khenaidoo54e0ddf2019-02-27 16:21:33 -050038func NewRequestHandlerProxy(coreInstanceId string, iadapter adapters.IAdapter, cProxy *CoreProxy) *RequestHandlerProxy {
khenaidood2b6df92018-12-13 16:37:20 -050039 var proxy RequestHandlerProxy
40 proxy.coreInstanceId = coreInstanceId
41 proxy.adapter = iadapter
khenaidoo54e0ddf2019-02-27 16:21:33 -050042 proxy.coreProxy = cProxy
khenaidood2b6df92018-12-13 16:37:20 -050043 return &proxy
44}
45
46func (rhp *RequestHandlerProxy) Adapter_descriptor() (*empty.Empty, error) {
47 return new(empty.Empty), nil
48}
49
50func (rhp *RequestHandlerProxy) Device_types() (*voltha.DeviceTypes, error) {
51 return nil, nil
52}
53
54func (rhp *RequestHandlerProxy) Health() (*voltha.HealthStatus, error) {
55 return nil, nil
56}
57
58func (rhp *RequestHandlerProxy) Adopt_device(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -050059 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -050060 log.Warn("invalid-number-of-args", log.Fields{"args": args})
61 err := errors.New("invalid-number-of-args")
62 return nil, err
63 }
64 device := &voltha.Device{}
khenaidoo6d055132019-02-12 16:51:19 -050065 transactionID := &ic.StrType{}
khenaidoo54e0ddf2019-02-27 16:21:33 -050066 fromTopic := &ic.StrType{}
khenaidoo6d055132019-02-12 16:51:19 -050067 for _, arg := range args {
68 switch arg.Key {
69 case "device":
70 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
71 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
72 return nil, err
73 }
74 case kafka.TransactionKey:
75 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
76 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
77 return nil, err
78 }
khenaidoo54e0ddf2019-02-27 16:21:33 -050079 case kafka.FromTopic:
80 if err := ptypes.UnmarshalAny(arg.Value, fromTopic); err != nil {
81 log.Warnw("cannot-unmarshal-from-topic", log.Fields{"error": err})
82 return nil, err
83 }
khenaidoo6d055132019-02-12 16:51:19 -050084 }
khenaidood2b6df92018-12-13 16:37:20 -050085 }
khenaidoo6d055132019-02-12 16:51:19 -050086
khenaidood2b6df92018-12-13 16:37:20 -050087 log.Debugw("Adopt_device", log.Fields{"deviceId": device.Id})
88
khenaidoo54e0ddf2019-02-27 16:21:33 -050089 //Update the core reference for that device
90 rhp.coreProxy.UpdateCoreReference(device.Id, fromTopic.Val)
91
khenaidood2b6df92018-12-13 16:37:20 -050092 //Invoke the adopt device on the adapter
93 if err := rhp.adapter.Adopt_device(device); err != nil {
94 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
95 }
96
97 return new(empty.Empty), nil
98}
99
100func (rhp *RequestHandlerProxy) Reconcile_device(args []*ic.Argument) (*empty.Empty, error) {
101 return new(empty.Empty), nil
102}
103
104func (rhp *RequestHandlerProxy) Abandon_device(args []*ic.Argument) (*empty.Empty, error) {
105 return new(empty.Empty), nil
106}
107
108func (rhp *RequestHandlerProxy) Disable_device(args []*ic.Argument) (*empty.Empty, error) {
109 return new(empty.Empty), nil
110}
111
112func (rhp *RequestHandlerProxy) Reenable_device(args []*ic.Argument) (*empty.Empty, error) {
113 return new(empty.Empty), nil
114}
115
116func (rhp *RequestHandlerProxy) Reboot_device(args []*ic.Argument) (*empty.Empty, error) {
117 return new(empty.Empty), nil
118}
119
120func (rhp *RequestHandlerProxy) Self_test_device(args []*ic.Argument) (*empty.Empty, error) {
121 return new(empty.Empty), nil
122}
123
124func (rhp *RequestHandlerProxy) Delete_device(args []*ic.Argument) (*empty.Empty, error) {
125 return new(empty.Empty), nil
126}
127
128func (rhp *RequestHandlerProxy) Get_device_details(args []*ic.Argument) (*empty.Empty, error) {
129 return new(empty.Empty), nil
130}
131
132func (rhp *RequestHandlerProxy) Update_flows_bulk(args []*ic.Argument) (*empty.Empty, error) {
133 return new(empty.Empty), nil
134}
135
136func (rhp *RequestHandlerProxy) Update_flows_incrementally(args []*ic.Argument) (*empty.Empty, error) {
137 return new(empty.Empty), nil
138}
139
140func (rhp *RequestHandlerProxy) Update_pm_config(args []*ic.Argument) (*empty.Empty, error) {
141 return new(empty.Empty), nil
142}
143
144func (rhp *RequestHandlerProxy) Receive_packet_out(args []*ic.Argument) (*empty.Empty, error) {
145 return new(empty.Empty), nil
146}
147
148func (rhp *RequestHandlerProxy) Suppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
149 return new(empty.Empty), nil
150}
151
152func (rhp *RequestHandlerProxy) Unsuppress_alarm(args []*ic.Argument) (*empty.Empty, error) {
153 return new(empty.Empty), nil
154}
155
156func (rhp *RequestHandlerProxy) Get_ofp_device_info(args []*ic.Argument) (*ic.SwitchCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500157 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500158 log.Warn("invalid-number-of-args", log.Fields{"args": args})
159 err := errors.New("invalid-number-of-args")
160 return nil, err
161 }
162 device := &voltha.Device{}
khenaidoo6d055132019-02-12 16:51:19 -0500163 transactionID := &ic.StrType{}
164 for _, arg := range args {
165 switch arg.Key {
166 case "device":
167 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
168 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
169 return nil, err
170 }
171 case kafka.TransactionKey:
172 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
173 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
174 return nil, err
175 }
176 }
khenaidood2b6df92018-12-13 16:37:20 -0500177 }
khenaidoo6d055132019-02-12 16:51:19 -0500178
khenaidood2b6df92018-12-13 16:37:20 -0500179 log.Debugw("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
180
181 var cap *ic.SwitchCapability
182 var err error
183 if cap, err = rhp.adapter.Get_ofp_device_info(device); err != nil {
184 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
185 }
khenaidoo6d055132019-02-12 16:51:19 -0500186 log.Debugw("Get_ofp_device_info", log.Fields{"cap": cap})
khenaidood2b6df92018-12-13 16:37:20 -0500187 return cap, nil
188}
189
190func (rhp *RequestHandlerProxy) Get_ofp_port_info(args []*ic.Argument) (*ic.PortCapability, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500191 if len(args) < 3 {
khenaidood2b6df92018-12-13 16:37:20 -0500192 log.Warn("invalid-number-of-args", log.Fields{"args": args})
193 err := errors.New("invalid-number-of-args")
194 return nil, err
195 }
196 device := &voltha.Device{}
197 pNo := &ic.IntType{}
khenaidoo6d055132019-02-12 16:51:19 -0500198 transactionID := &ic.StrType{}
khenaidood2b6df92018-12-13 16:37:20 -0500199 for _, arg := range args {
200 switch arg.Key {
201 case "device":
202 if err := ptypes.UnmarshalAny(arg.Value, device); err != nil {
203 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
204 return nil, err
205 }
206 case "port_no":
207 if err := ptypes.UnmarshalAny(arg.Value, pNo); err != nil {
208 log.Warnw("cannot-unmarshal-port-no", log.Fields{"error": err})
209 return nil, err
210 }
khenaidoo6d055132019-02-12 16:51:19 -0500211 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 }
khenaidood2b6df92018-12-13 16:37:20 -0500216 }
217 }
218 log.Debugw("Get_ofp_port_info", log.Fields{"deviceId": device.Id, "portNo": pNo.Val})
219 var cap *ic.PortCapability
220 var err error
221 if cap, err = rhp.adapter.Get_ofp_port_info(device, pNo.Val); err != nil {
222 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
223 }
224 return cap, nil
225}
226
227func (rhp *RequestHandlerProxy) Process_inter_adapter_message(args []*ic.Argument) (*empty.Empty, error) {
khenaidoo54e0ddf2019-02-27 16:21:33 -0500228 if len(args) < 2 {
khenaidood2b6df92018-12-13 16:37:20 -0500229 log.Warn("invalid-number-of-args", log.Fields{"args": args})
230 err := errors.New("invalid-number-of-args")
231 return nil, err
232 }
233 iaMsg := &ic.InterAdapterMessage{}
khenaidoo6d055132019-02-12 16:51:19 -0500234 transactionID := &ic.StrType{}
235 for _, arg := range args {
236 switch arg.Key {
237 case "msg":
238 if err := ptypes.UnmarshalAny(arg.Value, iaMsg); err != nil {
239 log.Warnw("cannot-unmarshal-device", log.Fields{"error": err})
240 return nil, err
241 }
242 case kafka.TransactionKey:
243 if err := ptypes.UnmarshalAny(arg.Value, transactionID); err != nil {
244 log.Warnw("cannot-unmarshal-transaction-ID", log.Fields{"error": err})
245 return nil, err
246 }
247 }
khenaidood2b6df92018-12-13 16:37:20 -0500248 }
249
250 log.Debugw("Process_inter_adapter_message", log.Fields{"msgId": iaMsg.Header.Id})
251
252 //Invoke the inter adapter API on the handler
253 if err := rhp.adapter.Process_inter_adapter_message(iaMsg); err != nil {
254 return nil, status.Errorf(codes.NotFound, "%s", err.Error())
255 }
256
257 return new(empty.Empty), nil
258}
259
khenaidoof5a5bfa2019-01-23 22:20:29 -0500260func (rhp *RequestHandlerProxy) Download_image(args []*ic.Argument) (*voltha.ImageDownload, error) {
261 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500262}
263
khenaidoof5a5bfa2019-01-23 22:20:29 -0500264func (rhp *RequestHandlerProxy) Get_image_download_status(args []*ic.Argument) (*voltha.ImageDownload, error) {
265 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500266}
267
khenaidoof5a5bfa2019-01-23 22:20:29 -0500268func (rhp *RequestHandlerProxy) Cancel_image_download(args []*ic.Argument) (*voltha.ImageDownload, error) {
269 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500270}
271
khenaidoof5a5bfa2019-01-23 22:20:29 -0500272func (rhp *RequestHandlerProxy) Activate_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
273 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500274}
275
khenaidoof5a5bfa2019-01-23 22:20:29 -0500276func (rhp *RequestHandlerProxy) Revert_image_update(args []*ic.Argument) (*voltha.ImageDownload, error) {
277 return &voltha.ImageDownload{}, nil
khenaidood2b6df92018-12-13 16:37:20 -0500278}