blob: bccb2270bcdfcf352c87258905cba731993efa79 [file] [log] [blame]
khenaidoob9203542018-09-17 22:56:37 -04001/*
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 core
17
18import (
19 "context"
20 "github.com/golang/protobuf/ptypes"
khenaidoo92e62c52018-10-03 14:02:54 -040021 a "github.com/golang/protobuf/ptypes/any"
khenaidoob9203542018-09-17 22:56:37 -040022 "github.com/opencord/voltha-go/common/log"
23 "github.com/opencord/voltha-go/kafka"
24 ca "github.com/opencord/voltha-go/protos/core_adapter"
25 "github.com/opencord/voltha-go/protos/voltha"
26 "google.golang.org/grpc/codes"
27 "google.golang.org/grpc/status"
28)
29
30type AdapterProxy struct {
31 TestMode bool
32 kafkaProxy *kafka.KafkaMessagingProxy
33}
34
35func NewAdapterProxy(kafkaProxy *kafka.KafkaMessagingProxy) *AdapterProxy {
36 var proxy AdapterProxy
37 proxy.kafkaProxy = kafkaProxy
38 return &proxy
39}
40
khenaidoo92e62c52018-10-03 14:02:54 -040041func unPackResponse(rpc string, deviceId string, success bool, response *a.Any) error {
42 if success {
43 return nil
44 } else {
45 unpackResult := &ca.Error{}
46 var err error
47 if err = ptypes.UnmarshalAny(response, unpackResult); err != nil {
48 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
49 }
50 log.Debugw("response", log.Fields{"rpc": rpc, "deviceId": deviceId, "success": success, "error": err})
51 // TODO: Need to get the real error code
52 return status.Errorf(codes.Canceled, "%s", unpackResult.Reason)
53 }
54}
55
khenaidoob9203542018-09-17 22:56:37 -040056func (ap *AdapterProxy) AdoptDevice(ctx context.Context, device *voltha.Device) error {
57 log.Debugw("AdoptDevice", log.Fields{"device": device})
khenaidoo92e62c52018-10-03 14:02:54 -040058 rpc := "adopt_device"
khenaidoob9203542018-09-17 22:56:37 -040059 topic := kafka.Topic{Name: device.Type}
60 args := make([]*kafka.KVArg, 1)
61 args[0] = &kafka.KVArg{
62 Key: "device",
63 Value: device,
64 }
khenaidoo92e62c52018-10-03 14:02:54 -040065 success, result := ap.kafkaProxy.InvokeRPC(ctx, rpc, &topic, true, args...)
66 log.Debugw("AdoptDevice-response", log.Fields{"deviceid": device.Id, "success": success})
67 return unPackResponse(rpc, device.Id, success, result)
68}
69
70func (ap *AdapterProxy) DisableDevice(ctx context.Context, device *voltha.Device) error {
71 log.Debugw("DisableDevice", log.Fields{"deviceId": device.Id})
72 rpc := "disable_device"
73 topic := kafka.Topic{Name: device.Type}
74 args := make([]*kafka.KVArg, 1)
75 args[0] = &kafka.KVArg{
76 Key: "device",
77 Value: device,
khenaidoob9203542018-09-17 22:56:37 -040078 }
khenaidoo92e62c52018-10-03 14:02:54 -040079 success, result := ap.kafkaProxy.InvokeRPC(nil, rpc, &topic, true, args...)
80 log.Debugw("DisableDevice-response", log.Fields{"deviceId": device.Id, "success": success})
81 return unPackResponse(rpc, device.Id, success, result)
khenaidoob9203542018-09-17 22:56:37 -040082}
83
84func (ap *AdapterProxy) AdapterDescriptor() (*voltha.Adapter, error) {
85 log.Debug("AdapterDescriptor")
86 return nil, nil
87}
88
89func (ap *AdapterProxy) DeviceTypes() (*voltha.DeviceType, error) {
90 log.Debug("DeviceTypes")
91 return nil, nil
92}
93
94func (ap *AdapterProxy) Health() (*voltha.HealthStatus, error) {
95 log.Debug("Health")
96 return nil, nil
97}
98
khenaidoo92e62c52018-10-03 14:02:54 -040099func (ap *AdapterProxy) ReconcileDevice(device *voltha.Device) error {
khenaidoob9203542018-09-17 22:56:37 -0400100 log.Debug("ReconcileDevice")
101 return nil
102}
103
104func (ap *AdapterProxy) AbandonDevice(device voltha.Device) error {
105 log.Debug("AbandonDevice")
106 return nil
107}
108
khenaidoo92e62c52018-10-03 14:02:54 -0400109func (ap *AdapterProxy) ReEnableDevice(ctx context.Context, device *voltha.Device) error {
110 log.Debugw("ReEnableDevice", log.Fields{"deviceId": device.Id})
111 rpc := "reenable_device"
112 topic := kafka.Topic{Name: device.Type}
113 args := make([]*kafka.KVArg, 1)
114 args[0] = &kafka.KVArg{
115 Key: "device",
116 Value: device,
117 }
118 success, result := ap.kafkaProxy.InvokeRPC(ctx, rpc, &topic, true, args...)
119 log.Debugw("ReEnableDevice-response", log.Fields{"deviceid": device.Id, "success": success})
120 return unPackResponse(rpc, device.Id, success, result)
khenaidoob9203542018-09-17 22:56:37 -0400121}
122
khenaidoo92e62c52018-10-03 14:02:54 -0400123func (ap *AdapterProxy) RebootDevice(device *voltha.Device) error {
khenaidoob9203542018-09-17 22:56:37 -0400124 log.Debug("RebootDevice")
125 return nil
126}
127
khenaidoo92e62c52018-10-03 14:02:54 -0400128func (ap *AdapterProxy) DeleteDevice(device *voltha.Device) error {
khenaidoob9203542018-09-17 22:56:37 -0400129 log.Debug("DeleteDevice")
130 return nil
131}
132
133func (ap *AdapterProxy) GetDeviceDetails(device voltha.Device) (*voltha.Device, error) {
134 log.Debug("GetDeviceDetails")
135 return nil, nil
136}
137
138func (ap *AdapterProxy) DownloadImage(device voltha.Device, download voltha.ImageDownload) error {
139 log.Debug("DownloadImage")
140 return nil
141}
142
143func (ap *AdapterProxy) GetImageDownloadStatus(device voltha.Device, download voltha.ImageDownload) error {
144 log.Debug("GetImageDownloadStatus")
145 return nil
146}
147
148func (ap *AdapterProxy) CancelImageDownload(device voltha.Device, download voltha.ImageDownload) error {
149 log.Debug("CancelImageDownload")
150 return nil
151}
152
153func (ap *AdapterProxy) ActivateImageUpdate(device voltha.Device, download voltha.ImageDownload) error {
154 log.Debug("ActivateImageUpdate")
155 return nil
156}
157
158func (ap *AdapterProxy) RevertImageUpdate(device voltha.Device, download voltha.ImageDownload) error {
159 log.Debug("RevertImageUpdate")
160 return nil
161}
162
163func (ap *AdapterProxy) SelfTestDevice(device voltha.Device) (*voltha.SelfTestResponse, error) {
164 log.Debug("SelfTestDevice")
165 return nil, nil
166}
167
168func (ap *AdapterProxy) UpdateFlowsBulk(device voltha.Device, flows voltha.Flows, groups voltha.FlowGroups) error {
169 log.Debug("UpdateFlowsBulk")
170 return nil
171}
172
173func (ap *AdapterProxy) UpdateFlowsIncremental(device voltha.Device, flowChanges voltha.Flows, groupChanges voltha.FlowGroups) error {
174 log.Debug("UpdateFlowsIncremental")
175 return nil
176}
177
178func (ap *AdapterProxy) UpdatePmConfig(device voltha.Device, pmConfigs voltha.PmConfigs) error {
179 log.Debug("UpdatePmConfig")
180 return nil
181}
182
183func (ap *AdapterProxy) ReceivePacketOut(deviceId voltha.ID, egressPortNo int, msg interface{}) error {
184 log.Debug("ReceivePacketOut")
185 return nil
186}
187
188func (ap *AdapterProxy) SuppressAlarm(filter voltha.AlarmFilter) error {
189 log.Debug("SuppressAlarm")
190 return nil
191}
192
193func (ap *AdapterProxy) UnSuppressAlarm(filter voltha.AlarmFilter) error {
194 log.Debug("UnSuppressAlarm")
195 return nil
196}
197
198func (ap *AdapterProxy) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
khenaidoo92e62c52018-10-03 14:02:54 -0400199 log.Debugw("GetOfpDeviceInfo", log.Fields{"deviceId": device.Id})
khenaidoob9203542018-09-17 22:56:37 -0400200 topic := kafka.Topic{Name: device.Type}
201 args := make([]*kafka.KVArg, 1)
202 args[0] = &kafka.KVArg{
203 Key: "device",
204 Value: device,
205 }
206 success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_device_info", &topic, true, args...)
khenaidoo92e62c52018-10-03 14:02:54 -0400207 log.Debugw("GetOfpDeviceInfo-response", log.Fields{"deviceId": device.Id, "success": success, "result": result})
khenaidoob9203542018-09-17 22:56:37 -0400208 if success {
209 unpackResult := &ca.SwitchCapability{}
210 if err := ptypes.UnmarshalAny(result, unpackResult); err != nil {
211 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
212 return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
213 }
214 return unpackResult, nil
215 } else {
216 unpackResult := &ca.Error{}
217 var err error
218 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
219 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
220 }
221 log.Debugw("GetOfpDeviceInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err})
222 // TODO: Need to get the real error code
223 return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
224 }
225}
226
227func (ap *AdapterProxy) GetOfpPortInfo(ctx context.Context, device *voltha.Device, portNo uint32) (*ca.PortCapability, error) {
khenaidoo92e62c52018-10-03 14:02:54 -0400228 log.Debugw("GetOfpPortInfo", log.Fields{"deviceId": device.Id})
khenaidoob9203542018-09-17 22:56:37 -0400229 topic := kafka.Topic{Name: device.Type}
230 args := make([]*kafka.KVArg, 2)
231 args[0] = &kafka.KVArg{
232 Key: "device",
233 Value: device,
234 }
235 pNo := &ca.IntType{Val: int64(portNo)}
236 args[1] = &kafka.KVArg{
237 Key: "port_no",
238 Value: pNo,
239 }
240
241 success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_port_info", &topic, true, args...)
khenaidoo92e62c52018-10-03 14:02:54 -0400242 log.Debugw("GetOfpPortInfo-response", log.Fields{"deviceid": device.Id, "success": success})
khenaidoob9203542018-09-17 22:56:37 -0400243 if success {
244 unpackResult := &ca.PortCapability{}
245 if err := ptypes.UnmarshalAny(result, unpackResult); err != nil {
246 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
247 return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
248 }
249 return unpackResult, nil
250 } else {
251 unpackResult := &ca.Error{}
252 var err error
253 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
254 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
255 }
256 log.Debugw("GetOfpPortInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err})
257 // TODO: Need to get the real error code
258 return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
259 }
260}