blob: a0e25f382bc34b2fb93645817f7d2deae900906c [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"
21 "github.com/opencord/voltha-go/common/log"
22 "github.com/opencord/voltha-go/kafka"
23 ca "github.com/opencord/voltha-go/protos/core_adapter"
24 "github.com/opencord/voltha-go/protos/voltha"
25 "google.golang.org/grpc/codes"
26 "google.golang.org/grpc/status"
27)
28
29type AdapterProxy struct {
30 TestMode bool
31 kafkaProxy *kafka.KafkaMessagingProxy
32}
33
34func NewAdapterProxy(kafkaProxy *kafka.KafkaMessagingProxy) *AdapterProxy {
35 var proxy AdapterProxy
36 proxy.kafkaProxy = kafkaProxy
37 return &proxy
38}
39
40func (ap *AdapterProxy) AdoptDevice(ctx context.Context, device *voltha.Device) error {
41 log.Debugw("AdoptDevice", log.Fields{"device": device})
42 topic := kafka.Topic{Name: device.Type}
43 args := make([]*kafka.KVArg, 1)
44 args[0] = &kafka.KVArg{
45 Key: "device",
46 Value: device,
47 }
48 success, result := ap.kafkaProxy.InvokeRPC(ctx, "adopt_device", &topic, true, args...)
49 log.Debugw("AdoptDevice-response", log.Fields{"deviceid": device.Id, "success": success, "result": result})
50 if success {
51 return nil
52 } else {
53 unpackResult := &ca.Error{}
54 var err error
55 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
56 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
57 }
58 log.Debugw("AdoptDevice-return", log.Fields{"deviceid": device.Id, "success": success, "error": err})
59 // TODO: Need to get the real error code
60 return status.Errorf(codes.Canceled, "%s", unpackResult.Reason)
61 }
62}
63
64func (ap *AdapterProxy) AdapterDescriptor() (*voltha.Adapter, error) {
65 log.Debug("AdapterDescriptor")
66 return nil, nil
67}
68
69func (ap *AdapterProxy) DeviceTypes() (*voltha.DeviceType, error) {
70 log.Debug("DeviceTypes")
71 return nil, nil
72}
73
74func (ap *AdapterProxy) Health() (*voltha.HealthStatus, error) {
75 log.Debug("Health")
76 return nil, nil
77}
78
79func (ap *AdapterProxy) ReconcileDevice(device voltha.Device) error {
80 log.Debug("ReconcileDevice")
81 return nil
82}
83
84func (ap *AdapterProxy) AbandonDevice(device voltha.Device) error {
85 log.Debug("AbandonDevice")
86 return nil
87}
88
89func (ap *AdapterProxy) DisableDevice(device voltha.Device) error {
90 log.Debug("DisableDevice")
91 return nil
92}
93
94func (ap *AdapterProxy) ReEnableDevice(device voltha.Device) error {
95 log.Debug("ReEnableDevice")
96 return nil
97}
98
99func (ap *AdapterProxy) RebootDevice(device voltha.Device) error {
100 log.Debug("RebootDevice")
101 return nil
102}
103
104func (ap *AdapterProxy) DeleteDevice(device voltha.Device) error {
105 log.Debug("DeleteDevice")
106 return nil
107}
108
109func (ap *AdapterProxy) GetDeviceDetails(device voltha.Device) (*voltha.Device, error) {
110 log.Debug("GetDeviceDetails")
111 return nil, nil
112}
113
114func (ap *AdapterProxy) DownloadImage(device voltha.Device, download voltha.ImageDownload) error {
115 log.Debug("DownloadImage")
116 return nil
117}
118
119func (ap *AdapterProxy) GetImageDownloadStatus(device voltha.Device, download voltha.ImageDownload) error {
120 log.Debug("GetImageDownloadStatus")
121 return nil
122}
123
124func (ap *AdapterProxy) CancelImageDownload(device voltha.Device, download voltha.ImageDownload) error {
125 log.Debug("CancelImageDownload")
126 return nil
127}
128
129func (ap *AdapterProxy) ActivateImageUpdate(device voltha.Device, download voltha.ImageDownload) error {
130 log.Debug("ActivateImageUpdate")
131 return nil
132}
133
134func (ap *AdapterProxy) RevertImageUpdate(device voltha.Device, download voltha.ImageDownload) error {
135 log.Debug("RevertImageUpdate")
136 return nil
137}
138
139func (ap *AdapterProxy) SelfTestDevice(device voltha.Device) (*voltha.SelfTestResponse, error) {
140 log.Debug("SelfTestDevice")
141 return nil, nil
142}
143
144func (ap *AdapterProxy) UpdateFlowsBulk(device voltha.Device, flows voltha.Flows, groups voltha.FlowGroups) error {
145 log.Debug("UpdateFlowsBulk")
146 return nil
147}
148
149func (ap *AdapterProxy) UpdateFlowsIncremental(device voltha.Device, flowChanges voltha.Flows, groupChanges voltha.FlowGroups) error {
150 log.Debug("UpdateFlowsIncremental")
151 return nil
152}
153
154func (ap *AdapterProxy) UpdatePmConfig(device voltha.Device, pmConfigs voltha.PmConfigs) error {
155 log.Debug("UpdatePmConfig")
156 return nil
157}
158
159func (ap *AdapterProxy) ReceivePacketOut(deviceId voltha.ID, egressPortNo int, msg interface{}) error {
160 log.Debug("ReceivePacketOut")
161 return nil
162}
163
164func (ap *AdapterProxy) SuppressAlarm(filter voltha.AlarmFilter) error {
165 log.Debug("SuppressAlarm")
166 return nil
167}
168
169func (ap *AdapterProxy) UnSuppressAlarm(filter voltha.AlarmFilter) error {
170 log.Debug("UnSuppressAlarm")
171 return nil
172}
173
174func (ap *AdapterProxy) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
175 log.Debugw("GetOfpDeviceInfo", log.Fields{"device": device})
176 topic := kafka.Topic{Name: device.Type}
177 args := make([]*kafka.KVArg, 1)
178 args[0] = &kafka.KVArg{
179 Key: "device",
180 Value: device,
181 }
182 success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_device_info", &topic, true, args...)
183 log.Debugw("GetOfpDeviceInfo-response", log.Fields{"device": device, "success": success, "result": result})
184 if success {
185 unpackResult := &ca.SwitchCapability{}
186 if err := ptypes.UnmarshalAny(result, unpackResult); err != nil {
187 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
188 return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
189 }
190 return unpackResult, nil
191 } else {
192 unpackResult := &ca.Error{}
193 var err error
194 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
195 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
196 }
197 log.Debugw("GetOfpDeviceInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err})
198 // TODO: Need to get the real error code
199 return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
200 }
201}
202
203func (ap *AdapterProxy) GetOfpPortInfo(ctx context.Context, device *voltha.Device, portNo uint32) (*ca.PortCapability, error) {
204 log.Debug("GetOfpPortInfo", log.Fields{"device": device})
205 topic := kafka.Topic{Name: device.Type}
206 args := make([]*kafka.KVArg, 2)
207 args[0] = &kafka.KVArg{
208 Key: "device",
209 Value: device,
210 }
211 pNo := &ca.IntType{Val: int64(portNo)}
212 args[1] = &kafka.KVArg{
213 Key: "port_no",
214 Value: pNo,
215 }
216
217 success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_port_info", &topic, true, args...)
218 log.Debugw("GetOfpPortInfo-response", log.Fields{"deviceid": device.Id, "device": device, "success": success, "result": result})
219 if success {
220 unpackResult := &ca.PortCapability{}
221 if err := ptypes.UnmarshalAny(result, unpackResult); err != nil {
222 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
223 return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error())
224 }
225 return unpackResult, nil
226 } else {
227 unpackResult := &ca.Error{}
228 var err error
229 if err = ptypes.UnmarshalAny(result, unpackResult); err != nil {
230 log.Warnw("cannot-unmarshal-response", log.Fields{"error": err})
231 }
232 log.Debugw("GetOfpPortInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err})
233 // TODO: Need to get the real error code
234 return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason)
235 }
236}