Scott Baker | 2d89798 | 2019-09-24 11:50:08 -0700 | [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 adaptercore |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
| 22 | com "github.com/opencord/voltha-go/adapters/common" |
| 23 | "github.com/opencord/voltha-go/common/log" |
| 24 | "github.com/opencord/voltha-go/kafka" |
| 25 | ic "github.com/opencord/voltha-protos/go/inter_container" |
| 26 | "github.com/opencord/voltha-protos/go/openflow_13" |
| 27 | "github.com/opencord/voltha-protos/go/voltha" |
| 28 | "sync" |
| 29 | ) |
| 30 | |
| 31 | type SimulatedOLT struct { |
| 32 | deviceHandlers map[string]*DeviceHandler |
| 33 | coreProxy *com.CoreProxy |
| 34 | kafkaICProxy *kafka.InterContainerProxy |
| 35 | numOnus int |
| 36 | exitChannel chan int |
| 37 | lockDeviceHandlersMap sync.RWMutex |
| 38 | } |
| 39 | |
| 40 | func NewSimulatedOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy, onuNumber int) *SimulatedOLT { |
| 41 | var simulatedOLT SimulatedOLT |
| 42 | simulatedOLT.exitChannel = make(chan int, 1) |
| 43 | simulatedOLT.deviceHandlers = make(map[string]*DeviceHandler) |
| 44 | simulatedOLT.kafkaICProxy = kafkaICProxy |
| 45 | simulatedOLT.numOnus = onuNumber |
| 46 | simulatedOLT.coreProxy = coreProxy |
| 47 | simulatedOLT.lockDeviceHandlersMap = sync.RWMutex{} |
| 48 | return &simulatedOLT |
| 49 | } |
| 50 | |
| 51 | func (so *SimulatedOLT) Start(ctx context.Context) error { |
| 52 | log.Info("starting-device-manager") |
| 53 | log.Info("device-manager-started") |
| 54 | return nil |
| 55 | } |
| 56 | |
| 57 | func (so *SimulatedOLT) Stop(ctx context.Context) error { |
| 58 | log.Info("stopping-device-manager") |
| 59 | so.exitChannel <- 1 |
| 60 | log.Info("device-manager-stopped") |
| 61 | return nil |
| 62 | } |
| 63 | |
| 64 | func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 65 | if ctx.Err() == nil { |
| 66 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 67 | // Channel is automatically closed when a context is Done |
| 68 | ch <- result |
| 69 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 70 | } else { |
| 71 | // Should the transaction be reverted back? |
| 72 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (so *SimulatedOLT) addDeviceHandlerToMap(agent *DeviceHandler) { |
| 77 | so.lockDeviceHandlersMap.Lock() |
| 78 | defer so.lockDeviceHandlersMap.Unlock() |
| 79 | if _, exist := so.deviceHandlers[agent.deviceId]; !exist { |
| 80 | so.deviceHandlers[agent.deviceId] = agent |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func (so *SimulatedOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) { |
| 85 | so.lockDeviceHandlersMap.Lock() |
| 86 | defer so.lockDeviceHandlersMap.Unlock() |
| 87 | delete(so.deviceHandlers, agent.deviceId) |
| 88 | } |
| 89 | |
| 90 | func (so *SimulatedOLT) getDeviceHandler(deviceId string) *DeviceHandler { |
| 91 | so.lockDeviceHandlersMap.Lock() |
| 92 | defer so.lockDeviceHandlersMap.Unlock() |
| 93 | if agent, ok := so.deviceHandlers[deviceId]; ok { |
| 94 | return agent |
| 95 | } |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | func (so *SimulatedOLT) Adopt_device(device *voltha.Device) error { |
| 100 | if device == nil { |
| 101 | log.Warn("device-is-nil") |
| 102 | return errors.New("nil-device") |
| 103 | } |
| 104 | log.Infow("adopt-device", log.Fields{"deviceId": device.Id}) |
| 105 | var handler *DeviceHandler |
| 106 | if handler = so.getDeviceHandler(device.Id); handler == nil { |
| 107 | handler := NewDeviceHandler(so.coreProxy, device, so) |
| 108 | so.addDeviceHandlerToMap(handler) |
| 109 | go handler.AdoptDevice(device) |
| 110 | } |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func (so *SimulatedOLT) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { |
| 115 | log.Infow("Get_ofp_device_info", log.Fields{"deviceId": device.Id}) |
| 116 | if handler := so.getDeviceHandler(device.Id); handler != nil { |
| 117 | info, err := handler.GetOfpDeviceInfo(device) |
| 118 | log.Infow("Get_ofp_device_info-resp", log.Fields{"switch": info}) |
| 119 | return info, err |
| 120 | } |
| 121 | log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id}) |
| 122 | return nil, errors.New("device-handler-not-set") |
| 123 | } |
| 124 | |
| 125 | func (so *SimulatedOLT) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) { |
| 126 | log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id}) |
| 127 | if handler := so.getDeviceHandler(device.Id); handler != nil { |
| 128 | return handler.GetOfpPortInfo(device, port_no) |
| 129 | } |
| 130 | log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id}) |
| 131 | return nil, errors.New("device-handler-not-set") |
| 132 | } |
| 133 | |
| 134 | func (so *SimulatedOLT) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 135 | log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 136 | targetDevice := msg.Header.ProxyDeviceId // Request? |
| 137 | if targetDevice == "" && msg.Header.ToDeviceId != "" { |
| 138 | // Typical response |
| 139 | targetDevice = msg.Header.ToDeviceId |
| 140 | } |
| 141 | if handler := so.getDeviceHandler(targetDevice); handler != nil { |
| 142 | return handler.Process_inter_adapter_message(msg) |
| 143 | } |
| 144 | return errors.New(fmt.Sprintf("handler-not-found-%s", targetDevice)) |
| 145 | } |
| 146 | |
| 147 | func (so *SimulatedOLT) Adapter_descriptor() error { |
| 148 | return errors.New("UnImplemented") |
| 149 | } |
| 150 | |
| 151 | func (so *SimulatedOLT) Device_types() (*voltha.DeviceTypes, error) { |
| 152 | return nil, errors.New("UnImplemented") |
| 153 | } |
| 154 | |
| 155 | func (so *SimulatedOLT) Health() (*voltha.HealthStatus, error) { |
| 156 | return nil, errors.New("UnImplemented") |
| 157 | } |
| 158 | |
| 159 | func (so *SimulatedOLT) Reconcile_device(device *voltha.Device) error { |
| 160 | if device == nil { |
| 161 | log.Warn("device-is-nil") |
| 162 | return errors.New("nil-device") |
| 163 | } |
| 164 | log.Infow("reconcile-device", log.Fields{"deviceId": device.Id}) |
| 165 | var handler *DeviceHandler |
| 166 | handler = so.getDeviceHandler(device.Id) |
| 167 | if handler == nil { |
| 168 | // Adapter has restarted |
| 169 | handler = NewDeviceHandler(so.coreProxy, device, so) |
| 170 | so.addDeviceHandlerToMap(handler) |
| 171 | } |
| 172 | go handler.ReconcileDevice(device) |
| 173 | return nil |
| 174 | } |
| 175 | |
| 176 | func (so *SimulatedOLT) Abandon_device(device *voltha.Device) error { |
| 177 | return errors.New("UnImplemented") |
| 178 | } |
| 179 | |
| 180 | func (so *SimulatedOLT) Disable_device(device *voltha.Device) error { |
| 181 | if device == nil { |
| 182 | log.Warn("device-is-nil") |
| 183 | return errors.New("nil-device") |
| 184 | } |
| 185 | log.Infow("disable-device", log.Fields{"deviceId": device.Id}) |
| 186 | var handler *DeviceHandler |
| 187 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 188 | go handler.DisableDevice(device) |
| 189 | } |
| 190 | return nil |
| 191 | } |
| 192 | |
| 193 | func (so *SimulatedOLT) Reenable_device(device *voltha.Device) error { |
| 194 | if device == nil { |
| 195 | log.Warn("device-is-nil") |
| 196 | return errors.New("nil-device") |
| 197 | } |
| 198 | log.Infow("reenable-device", log.Fields{"deviceId": device.Id}) |
| 199 | var handler *DeviceHandler |
| 200 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 201 | go handler.ReEnableDevice(device) |
| 202 | } |
| 203 | return nil |
| 204 | } |
| 205 | |
| 206 | func (so *SimulatedOLT) Reboot_device(device *voltha.Device) error { |
| 207 | return errors.New("UnImplemented") |
| 208 | } |
| 209 | |
| 210 | func (so *SimulatedOLT) Self_test_device(device *voltha.Device) error { |
| 211 | return errors.New("UnImplemented") |
| 212 | } |
| 213 | |
| 214 | func (so *SimulatedOLT) Delete_device(device *voltha.Device) error { |
| 215 | if device == nil { |
| 216 | log.Warn("device-is-nil") |
| 217 | return errors.New("nil-device") |
| 218 | } |
| 219 | log.Infow("delete-device", log.Fields{"deviceId": device.Id}) |
| 220 | var handler *DeviceHandler |
| 221 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 222 | go handler.DeleteDevice(device) |
| 223 | } |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | func (so *SimulatedOLT) Get_device_details(device *voltha.Device) error { |
| 228 | return errors.New("UnImplemented") |
| 229 | } |
| 230 | |
| 231 | func (so *SimulatedOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, metadata *voltha.FlowMetadata) error { |
| 232 | if device == nil { |
| 233 | log.Warn("device-is-nil") |
| 234 | return errors.New("nil-device") |
| 235 | } |
| 236 | log.Debugw("bulk-flow-updates", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups}) |
| 237 | var handler *DeviceHandler |
| 238 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 239 | go handler.UpdateFlowsBulk(device, flows, groups, metadata) |
| 240 | } |
| 241 | return nil |
| 242 | } |
| 243 | |
| 244 | func (so *SimulatedOLT) Update_flows_incrementally(device *voltha.Device, flowChanges *openflow_13.FlowChanges, groupChanges *openflow_13.FlowGroupChanges, metadata *voltha.FlowMetadata) error { |
| 245 | if device == nil { |
| 246 | log.Warn("device-is-nil") |
| 247 | return errors.New("nil-device") |
| 248 | } |
| 249 | log.Debugw("incremental-flow-update", log.Fields{"deviceId": device.Id, "flowChanges": flowChanges, "groupChanges": groupChanges}) |
| 250 | var handler *DeviceHandler |
| 251 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 252 | go handler.UpdateFlowsIncremental(device, flowChanges, groupChanges, metadata) |
| 253 | } |
| 254 | return nil |
| 255 | } |
| 256 | |
| 257 | func (so *SimulatedOLT) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error { |
| 258 | if device == nil { |
| 259 | log.Warn("device-is-nil") |
| 260 | return errors.New("nil-device") |
| 261 | } |
| 262 | log.Debugw("update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs}) |
| 263 | var handler *DeviceHandler |
| 264 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 265 | go handler.UpdatePmConfigs(device, pmConfigs) |
| 266 | } |
| 267 | return nil |
| 268 | } |
| 269 | |
| 270 | func (so *SimulatedOLT) Receive_packet_out(deviceId string, egress_port_no int, msg *openflow_13.OfpPacketOut) error { |
| 271 | return errors.New("UnImplemented") |
| 272 | } |
| 273 | |
| 274 | func (so *SimulatedOLT) Suppress_alarm(filter *voltha.AlarmFilter) error { |
| 275 | return errors.New("UnImplemented") |
| 276 | } |
| 277 | |
| 278 | func (so *SimulatedOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error { |
| 279 | return errors.New("UnImplemented") |
| 280 | } |
| 281 | |
| 282 | func (so *SimulatedOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 283 | return nil, errors.New("UnImplemented") |
| 284 | } |
| 285 | |
| 286 | func (so *SimulatedOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 287 | return nil, errors.New("UnImplemented") |
| 288 | } |
| 289 | |
| 290 | func (so *SimulatedOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 291 | return nil, errors.New("UnImplemented") |
| 292 | } |
| 293 | |
| 294 | func (so *SimulatedOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 295 | return nil, errors.New("UnImplemented") |
| 296 | } |
| 297 | |
| 298 | func (so *SimulatedOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 299 | return nil, errors.New("UnImplemented") |
| 300 | } |