khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [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" |
William Kurkian | daa6bb2 | 2019-03-07 12:26:28 -0500 | [diff] [blame] | 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" |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 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 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 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) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 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 { |
khenaidoo | 6d05513 | 2019-02-12 16:51:19 -0500 | [diff] [blame] | 117 | info, err := handler.GetOfpDeviceInfo(device) |
| 118 | log.Infow("Get_ofp_device_info-resp", log.Fields{"switch": info}) |
| 119 | return info, err |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 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 | return errors.New("UnImplemented") |
| 161 | } |
| 162 | |
| 163 | func (so *SimulatedOLT) Abandon_device(device *voltha.Device) error { |
| 164 | return errors.New("UnImplemented") |
| 165 | } |
| 166 | |
| 167 | func (so *SimulatedOLT) Disable_device(device *voltha.Device) error { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 168 | if device == nil { |
| 169 | log.Warn("device-is-nil") |
| 170 | return errors.New("nil-device") |
| 171 | } |
| 172 | log.Infow("disable-device", log.Fields{"deviceId": device.Id}) |
| 173 | var handler *DeviceHandler |
| 174 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 175 | go handler.DisableDevice(device) |
| 176 | } |
| 177 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | func (so *SimulatedOLT) Reenable_device(device *voltha.Device) error { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 181 | if device == nil { |
| 182 | log.Warn("device-is-nil") |
| 183 | return errors.New("nil-device") |
| 184 | } |
| 185 | log.Infow("reenable-device", log.Fields{"deviceId": device.Id}) |
| 186 | var handler *DeviceHandler |
| 187 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 188 | go handler.ReEnableDevice(device) |
| 189 | } |
| 190 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | func (so *SimulatedOLT) Reboot_device(device *voltha.Device) error { |
| 194 | return errors.New("UnImplemented") |
| 195 | } |
| 196 | |
| 197 | func (so *SimulatedOLT) Self_test_device(device *voltha.Device) error { |
| 198 | return errors.New("UnImplemented") |
| 199 | } |
| 200 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame] | 201 | func (so *SimulatedOLT) Delete_device(device *voltha.Device) error { |
| 202 | if device == nil { |
| 203 | log.Warn("device-is-nil") |
| 204 | return errors.New("nil-device") |
| 205 | } |
| 206 | log.Infow("delete-device", log.Fields{"deviceId": device.Id}) |
| 207 | var handler *DeviceHandler |
| 208 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 209 | go handler.DeleteDevice(device) |
| 210 | } |
| 211 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | func (so *SimulatedOLT) Get_device_details(device *voltha.Device) error { |
| 215 | return errors.New("UnImplemented") |
| 216 | } |
| 217 | |
| 218 | func (so *SimulatedOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error { |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 219 | if device == nil { |
| 220 | log.Warn("device-is-nil") |
| 221 | return errors.New("nil-device") |
| 222 | } |
| 223 | log.Debugw("bulk-flow-updates", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups}) |
| 224 | var handler *DeviceHandler |
| 225 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 226 | go handler.UpdateFlowsBulk(device, flows, groups) |
| 227 | } |
| 228 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 229 | } |
| 230 | |
khenaidoo | 0458db6 | 2019-06-20 08:50:36 -0400 | [diff] [blame] | 231 | func (so *SimulatedOLT) Update_flows_incrementally(device *voltha.Device, flowChanges *openflow_13.FlowChanges, groupChanges *openflow_13.FlowGroupChanges) error { |
| 232 | if device == nil { |
| 233 | log.Warn("device-is-nil") |
| 234 | return errors.New("nil-device") |
| 235 | } |
| 236 | log.Debugw("incremental-flow-update", log.Fields{"deviceId": device.Id, "flowChanges": flowChanges, "groupChanges": groupChanges}) |
| 237 | var handler *DeviceHandler |
| 238 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 239 | go handler.UpdateFlowsIncremental(device, flowChanges, groupChanges) |
| 240 | } |
| 241 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 242 | } |
| 243 | |
khenaidoo | b312747 | 2019-07-24 21:04:55 -0400 | [diff] [blame] | 244 | func (so *SimulatedOLT) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error { |
| 245 | if device == nil { |
| 246 | log.Warn("device-is-nil") |
| 247 | return errors.New("nil-device") |
| 248 | } |
| 249 | log.Debugw("update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs}) |
| 250 | var handler *DeviceHandler |
| 251 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 252 | go handler.UpdatePmConfigs(device, pmConfigs) |
| 253 | } |
| 254 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 255 | } |
| 256 | |
manikkaraj k | 6c9689d | 2019-05-09 12:59:52 -0400 | [diff] [blame] | 257 | func (so *SimulatedOLT) Receive_packet_out(deviceId string, egress_port_no int, msg *openflow_13.OfpPacketOut) error { |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 258 | return errors.New("UnImplemented") |
| 259 | } |
| 260 | |
| 261 | func (so *SimulatedOLT) Suppress_alarm(filter *voltha.AlarmFilter) error { |
| 262 | return errors.New("UnImplemented") |
| 263 | } |
| 264 | |
| 265 | func (so *SimulatedOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error { |
| 266 | return errors.New("UnImplemented") |
| 267 | } |
| 268 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 269 | func (so *SimulatedOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 270 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 271 | } |
| 272 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 273 | func (so *SimulatedOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 274 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 275 | } |
| 276 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 277 | func (so *SimulatedOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 278 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 279 | } |
| 280 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 281 | func (so *SimulatedOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 282 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 283 | } |
| 284 | |
khenaidoo | 2c6a099 | 2019-04-29 13:46:56 -0400 | [diff] [blame] | 285 | func (so *SimulatedOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 286 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 287 | } |