khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "github.com/golang/protobuf/ptypes" |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 21 | a "github.com/golang/protobuf/ptypes/any" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 22 | "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 | |
| 30 | type AdapterProxy struct { |
| 31 | TestMode bool |
| 32 | kafkaProxy *kafka.KafkaMessagingProxy |
| 33 | } |
| 34 | |
| 35 | func NewAdapterProxy(kafkaProxy *kafka.KafkaMessagingProxy) *AdapterProxy { |
| 36 | var proxy AdapterProxy |
| 37 | proxy.kafkaProxy = kafkaProxy |
| 38 | return &proxy |
| 39 | } |
| 40 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 41 | func 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 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 56 | func (ap *AdapterProxy) AdoptDevice(ctx context.Context, device *voltha.Device) error { |
| 57 | log.Debugw("AdoptDevice", log.Fields{"device": device}) |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 58 | rpc := "adopt_device" |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 59 | topic := kafka.Topic{Name: device.Type} |
| 60 | args := make([]*kafka.KVArg, 1) |
| 61 | args[0] = &kafka.KVArg{ |
| 62 | Key: "device", |
| 63 | Value: device, |
| 64 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 65 | 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 | |
| 70 | func (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, |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 78 | } |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 79 | 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) |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 82 | } |
| 83 | |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 84 | |
| 85 | |
| 86 | func (ap *AdapterProxy) ReEnableDevice(ctx context.Context, device *voltha.Device) error { |
| 87 | log.Debugw("ReEnableDevice", log.Fields{"deviceId": device.Id}) |
| 88 | rpc := "reenable_device" |
| 89 | topic := kafka.Topic{Name: device.Type} |
| 90 | args := make([]*kafka.KVArg, 1) |
| 91 | args[0] = &kafka.KVArg{ |
| 92 | Key: "device", |
| 93 | Value: device, |
| 94 | } |
| 95 | success, result := ap.kafkaProxy.InvokeRPC(ctx, rpc, &topic, true, args...) |
| 96 | log.Debugw("ReEnableDevice-response", log.Fields{"deviceid": device.Id, "success": success}) |
| 97 | return unPackResponse(rpc, device.Id, success, result) |
| 98 | } |
| 99 | |
| 100 | func (ap *AdapterProxy) RebootDevice(ctx context.Context, device *voltha.Device) error { |
| 101 | log.Debugw("RebootDevice", log.Fields{"deviceId": device.Id}) |
| 102 | rpc := "reboot_device" |
| 103 | topic := kafka.Topic{Name: device.Type} |
| 104 | args := make([]*kafka.KVArg, 1) |
| 105 | args[0] = &kafka.KVArg{ |
| 106 | Key: "device", |
| 107 | Value: device, |
| 108 | } |
| 109 | success, result := ap.kafkaProxy.InvokeRPC(ctx, rpc, &topic, true, args...) |
| 110 | log.Debugw("RebootDevice-response", log.Fields{"deviceid": device.Id, "success": success}) |
| 111 | return unPackResponse(rpc, device.Id, success, result) |
| 112 | } |
| 113 | |
| 114 | func (ap *AdapterProxy) DeleteDevice(ctx context.Context, device *voltha.Device) error { |
| 115 | log.Debugw("DeleteDevice", log.Fields{"deviceId": device.Id}) |
| 116 | rpc := "delete_device" |
| 117 | topic := kafka.Topic{Name: device.Type} |
| 118 | args := make([]*kafka.KVArg, 1) |
| 119 | args[0] = &kafka.KVArg{ |
| 120 | Key: "device", |
| 121 | Value: device, |
| 122 | } |
| 123 | success, result := ap.kafkaProxy.InvokeRPC(ctx, rpc, &topic, true, args...) |
| 124 | log.Debugw("DeleteDevice-response", log.Fields{"deviceid": device.Id, "success": success}) |
| 125 | return unPackResponse(rpc, device.Id, success, result) |
| 126 | } |
| 127 | |
| 128 | func (ap *AdapterProxy) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) { |
| 129 | log.Debugw("GetOfpDeviceInfo", log.Fields{"deviceId": device.Id}) |
| 130 | topic := kafka.Topic{Name: device.Type} |
| 131 | args := make([]*kafka.KVArg, 1) |
| 132 | args[0] = &kafka.KVArg{ |
| 133 | Key: "device", |
| 134 | Value: device, |
| 135 | } |
| 136 | success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_device_info", &topic, true, args...) |
| 137 | log.Debugw("GetOfpDeviceInfo-response", log.Fields{"deviceId": device.Id, "success": success, "result": result}) |
| 138 | if success { |
| 139 | unpackResult := &ca.SwitchCapability{} |
| 140 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
| 141 | log.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
| 142 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 143 | } |
| 144 | return unpackResult, nil |
| 145 | } else { |
| 146 | unpackResult := &ca.Error{} |
| 147 | var err error |
| 148 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
| 149 | log.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
| 150 | } |
| 151 | log.Debugw("GetOfpDeviceInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err}) |
| 152 | // TODO: Need to get the real error code |
| 153 | return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func (ap *AdapterProxy) GetOfpPortInfo(ctx context.Context, device *voltha.Device, portNo uint32) (*ca.PortCapability, error) { |
| 158 | log.Debugw("GetOfpPortInfo", log.Fields{"deviceId": device.Id}) |
| 159 | topic := kafka.Topic{Name: device.Type} |
| 160 | args := make([]*kafka.KVArg, 2) |
| 161 | args[0] = &kafka.KVArg{ |
| 162 | Key: "device", |
| 163 | Value: device, |
| 164 | } |
| 165 | pNo := &ca.IntType{Val: int64(portNo)} |
| 166 | args[1] = &kafka.KVArg{ |
| 167 | Key: "port_no", |
| 168 | Value: pNo, |
| 169 | } |
| 170 | |
| 171 | success, result := ap.kafkaProxy.InvokeRPC(ctx, "get_ofp_port_info", &topic, true, args...) |
| 172 | log.Debugw("GetOfpPortInfo-response", log.Fields{"deviceid": device.Id, "success": success}) |
| 173 | if success { |
| 174 | unpackResult := &ca.PortCapability{} |
| 175 | if err := ptypes.UnmarshalAny(result, unpackResult); err != nil { |
| 176 | log.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
| 177 | return nil, status.Errorf(codes.InvalidArgument, "%s", err.Error()) |
| 178 | } |
| 179 | return unpackResult, nil |
| 180 | } else { |
| 181 | unpackResult := &ca.Error{} |
| 182 | var err error |
| 183 | if err = ptypes.UnmarshalAny(result, unpackResult); err != nil { |
| 184 | log.Warnw("cannot-unmarshal-response", log.Fields{"error": err}) |
| 185 | } |
| 186 | log.Debugw("GetOfpPortInfo-return", log.Fields{"deviceid": device.Id, "success": success, "error": err}) |
| 187 | // TODO: Need to get the real error code |
| 188 | return nil, status.Errorf(codes.Internal, "%s", unpackResult.Reason) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | //TODO: Implement the functions below |
| 193 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 194 | func (ap *AdapterProxy) AdapterDescriptor() (*voltha.Adapter, error) { |
| 195 | log.Debug("AdapterDescriptor") |
| 196 | return nil, nil |
| 197 | } |
| 198 | |
| 199 | func (ap *AdapterProxy) DeviceTypes() (*voltha.DeviceType, error) { |
| 200 | log.Debug("DeviceTypes") |
| 201 | return nil, nil |
| 202 | } |
| 203 | |
| 204 | func (ap *AdapterProxy) Health() (*voltha.HealthStatus, error) { |
| 205 | log.Debug("Health") |
| 206 | return nil, nil |
| 207 | } |
| 208 | |
khenaidoo | 92e62c5 | 2018-10-03 14:02:54 -0400 | [diff] [blame] | 209 | func (ap *AdapterProxy) ReconcileDevice(device *voltha.Device) error { |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 210 | log.Debug("ReconcileDevice") |
| 211 | return nil |
| 212 | } |
| 213 | |
| 214 | func (ap *AdapterProxy) AbandonDevice(device voltha.Device) error { |
| 215 | log.Debug("AbandonDevice") |
| 216 | return nil |
| 217 | } |
| 218 | |
khenaidoo | b920354 | 2018-09-17 22:56:37 -0400 | [diff] [blame] | 219 | func (ap *AdapterProxy) GetDeviceDetails(device voltha.Device) (*voltha.Device, error) { |
| 220 | log.Debug("GetDeviceDetails") |
| 221 | return nil, nil |
| 222 | } |
| 223 | |
| 224 | func (ap *AdapterProxy) DownloadImage(device voltha.Device, download voltha.ImageDownload) error { |
| 225 | log.Debug("DownloadImage") |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | func (ap *AdapterProxy) GetImageDownloadStatus(device voltha.Device, download voltha.ImageDownload) error { |
| 230 | log.Debug("GetImageDownloadStatus") |
| 231 | return nil |
| 232 | } |
| 233 | |
| 234 | func (ap *AdapterProxy) CancelImageDownload(device voltha.Device, download voltha.ImageDownload) error { |
| 235 | log.Debug("CancelImageDownload") |
| 236 | return nil |
| 237 | } |
| 238 | |
| 239 | func (ap *AdapterProxy) ActivateImageUpdate(device voltha.Device, download voltha.ImageDownload) error { |
| 240 | log.Debug("ActivateImageUpdate") |
| 241 | return nil |
| 242 | } |
| 243 | |
| 244 | func (ap *AdapterProxy) RevertImageUpdate(device voltha.Device, download voltha.ImageDownload) error { |
| 245 | log.Debug("RevertImageUpdate") |
| 246 | return nil |
| 247 | } |
| 248 | |
| 249 | func (ap *AdapterProxy) SelfTestDevice(device voltha.Device) (*voltha.SelfTestResponse, error) { |
| 250 | log.Debug("SelfTestDevice") |
| 251 | return nil, nil |
| 252 | } |
| 253 | |
| 254 | func (ap *AdapterProxy) UpdateFlowsBulk(device voltha.Device, flows voltha.Flows, groups voltha.FlowGroups) error { |
| 255 | log.Debug("UpdateFlowsBulk") |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | func (ap *AdapterProxy) UpdateFlowsIncremental(device voltha.Device, flowChanges voltha.Flows, groupChanges voltha.FlowGroups) error { |
| 260 | log.Debug("UpdateFlowsIncremental") |
| 261 | return nil |
| 262 | } |
| 263 | |
| 264 | func (ap *AdapterProxy) UpdatePmConfig(device voltha.Device, pmConfigs voltha.PmConfigs) error { |
| 265 | log.Debug("UpdatePmConfig") |
| 266 | return nil |
| 267 | } |
| 268 | |
| 269 | func (ap *AdapterProxy) ReceivePacketOut(deviceId voltha.ID, egressPortNo int, msg interface{}) error { |
| 270 | log.Debug("ReceivePacketOut") |
| 271 | return nil |
| 272 | } |
| 273 | |
| 274 | func (ap *AdapterProxy) SuppressAlarm(filter voltha.AlarmFilter) error { |
| 275 | log.Debug("SuppressAlarm") |
| 276 | return nil |
| 277 | } |
| 278 | |
| 279 | func (ap *AdapterProxy) UnSuppressAlarm(filter voltha.AlarmFilter) error { |
| 280 | log.Debug("UnSuppressAlarm") |
| 281 | return nil |
khenaidoo | 4d4802d | 2018-10-04 21:59:49 -0400 | [diff] [blame] | 282 | } |