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