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 SimulatedONU struct { |
| 32 | deviceHandlers map[string]*DeviceHandler |
| 33 | coreProxy *com.CoreProxy |
| 34 | kafkaICProxy *kafka.InterContainerProxy |
| 35 | exitChannel chan int |
| 36 | lockDeviceHandlersMap sync.RWMutex |
| 37 | } |
| 38 | |
| 39 | func NewSimulatedONU(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy) *SimulatedONU { |
| 40 | var simulatedOLT SimulatedONU |
| 41 | simulatedOLT.exitChannel = make(chan int, 1) |
| 42 | simulatedOLT.deviceHandlers = make(map[string]*DeviceHandler) |
| 43 | simulatedOLT.kafkaICProxy = kafkaICProxy |
| 44 | simulatedOLT.coreProxy = coreProxy |
| 45 | simulatedOLT.lockDeviceHandlersMap = sync.RWMutex{} |
| 46 | return &simulatedOLT |
| 47 | } |
| 48 | |
| 49 | func (so *SimulatedONU) Start(ctx context.Context) error { |
| 50 | log.Info("starting-device-manager") |
| 51 | log.Info("device-manager-started") |
| 52 | return nil |
| 53 | } |
| 54 | |
| 55 | func (so *SimulatedONU) Stop(ctx context.Context) error { |
| 56 | log.Info("stopping-device-manager") |
| 57 | so.exitChannel <- 1 |
| 58 | log.Info("device-manager-stopped") |
| 59 | return nil |
| 60 | } |
| 61 | |
| 62 | func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) { |
| 63 | if ctx.Err() == nil { |
| 64 | // Returned response only of the ctx has not been cancelled/timeout/etc |
| 65 | // Channel is automatically closed when a context is Done |
| 66 | ch <- result |
| 67 | log.Debugw("sendResponse", log.Fields{"result": result}) |
| 68 | } else { |
| 69 | // Should the transaction be reverted back? |
| 70 | log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()}) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func (so *SimulatedONU) addDeviceHandlerToMap(agent *DeviceHandler) { |
| 75 | so.lockDeviceHandlersMap.Lock() |
| 76 | defer so.lockDeviceHandlersMap.Unlock() |
| 77 | if _, exist := so.deviceHandlers[agent.deviceId]; !exist { |
| 78 | so.deviceHandlers[agent.deviceId] = agent |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (so *SimulatedONU) deleteDeviceHandlerToMap(agent *DeviceHandler) { |
| 83 | so.lockDeviceHandlersMap.Lock() |
| 84 | defer so.lockDeviceHandlersMap.Unlock() |
| 85 | delete(so.deviceHandlers, agent.deviceId) |
| 86 | } |
| 87 | |
| 88 | func (so *SimulatedONU) getDeviceHandler(deviceId string) *DeviceHandler { |
| 89 | so.lockDeviceHandlersMap.Lock() |
| 90 | defer so.lockDeviceHandlersMap.Unlock() |
| 91 | if agent, ok := so.deviceHandlers[deviceId]; ok { |
| 92 | return agent |
| 93 | } |
| 94 | return nil |
| 95 | } |
| 96 | |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 97 | func (so *SimulatedONU) Adopt_device(device *voltha.Device) error { |
| 98 | if device == nil { |
| 99 | log.Warn("device-is-nil") |
| 100 | return errors.New("nil-device") |
| 101 | } |
| 102 | log.Infow("adopt-device", log.Fields{"deviceId": device.Id}) |
| 103 | var handler *DeviceHandler |
| 104 | if handler = so.getDeviceHandler(device.Id); handler == nil { |
| 105 | handler := NewDeviceHandler(so.coreProxy, device, so) |
| 106 | so.addDeviceHandlerToMap(handler) |
| 107 | go handler.AdoptDevice(device) |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 108 | } |
| 109 | return nil |
| 110 | } |
| 111 | |
| 112 | func (so *SimulatedONU) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { |
| 113 | log.Infow("not-implemented-for-onu", log.Fields{"deviceId": device.Id}) |
| 114 | return nil, nil |
| 115 | } |
| 116 | |
| 117 | func (so *SimulatedONU) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) { |
| 118 | log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id}) |
| 119 | if handler := so.getDeviceHandler(device.Id); handler != nil { |
| 120 | return handler.GetOfpPortInfo(device, port_no) |
| 121 | } |
| 122 | log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id}) |
| 123 | return nil, errors.New("device-handler-not-set") |
| 124 | } |
| 125 | |
| 126 | func (so *SimulatedONU) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 127 | log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 128 | targetDevice := msg.Header.ProxyDeviceId // Request? |
| 129 | if targetDevice == "" && msg.Header.ToDeviceId != "" { |
| 130 | // Typical response |
| 131 | targetDevice = msg.Header.ToDeviceId |
| 132 | } |
| 133 | if handler := so.getDeviceHandler(targetDevice); handler != nil { |
| 134 | return handler.Process_inter_adapter_message(msg) |
| 135 | } |
| 136 | return errors.New(fmt.Sprintf("handler-not-found-%s", targetDevice)) |
| 137 | } |
| 138 | |
| 139 | func (so *SimulatedONU) Adapter_descriptor() error { |
| 140 | return errors.New("UnImplemented") |
| 141 | } |
| 142 | |
| 143 | func (so *SimulatedONU) Device_types() (*voltha.DeviceTypes, error) { |
| 144 | return nil, errors.New("UnImplemented") |
| 145 | } |
| 146 | |
| 147 | func (so *SimulatedONU) Health() (*voltha.HealthStatus, error) { |
| 148 | return nil, errors.New("UnImplemented") |
| 149 | } |
| 150 | |
| 151 | func (so *SimulatedONU) Reconcile_device(device *voltha.Device) error { |
| 152 | return errors.New("UnImplemented") |
| 153 | } |
| 154 | |
| 155 | func (so *SimulatedONU) Abandon_device(device *voltha.Device) error { |
| 156 | return errors.New("UnImplemented") |
| 157 | } |
| 158 | |
| 159 | func (so *SimulatedONU) Disable_device(device *voltha.Device) error { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 160 | if device == nil { |
| 161 | log.Warn("device-is-nil") |
| 162 | return errors.New("nil-device") |
| 163 | } |
| 164 | log.Infow("disable-device", log.Fields{"deviceId": device.Id}) |
| 165 | var handler *DeviceHandler |
| 166 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 167 | go handler.DisableDevice(device) |
| 168 | } |
| 169 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | func (so *SimulatedONU) Reenable_device(device *voltha.Device) error { |
khenaidoo | 3ab3488 | 2019-05-02 21:33:30 -0400 | [diff] [blame] | 173 | if device == nil { |
| 174 | log.Warn("device-is-nil") |
| 175 | return errors.New("nil-device") |
| 176 | } |
| 177 | log.Infow("reenable-device", log.Fields{"deviceId": device.Id}) |
| 178 | var handler *DeviceHandler |
| 179 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 180 | go handler.ReEnableDevice(device) |
| 181 | } |
| 182 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | func (so *SimulatedONU) Reboot_device(device *voltha.Device) error { |
| 186 | return errors.New("UnImplemented") |
| 187 | } |
| 188 | |
| 189 | func (so *SimulatedONU) Self_test_device(device *voltha.Device) error { |
| 190 | return errors.New("UnImplemented") |
| 191 | } |
| 192 | |
khenaidoo | 0a822f9 | 2019-05-08 15:15:57 -0400 | [diff] [blame^] | 193 | func (so *SimulatedONU) Delete_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("delete-device", log.Fields{"deviceId": device.Id}) |
| 199 | var handler *DeviceHandler |
| 200 | if handler = so.getDeviceHandler(device.Id); handler != nil { |
| 201 | go handler.DeleteDevice(device) |
| 202 | } |
| 203 | return nil |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | func (so *SimulatedONU) Get_device_details(device *voltha.Device) error { |
| 207 | return errors.New("UnImplemented") |
| 208 | } |
| 209 | |
| 210 | func (so *SimulatedONU) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error { |
| 211 | return errors.New("UnImplemented") |
| 212 | } |
| 213 | |
| 214 | func (so *SimulatedONU) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges) error { |
| 215 | return errors.New("UnImplemented") |
| 216 | } |
| 217 | |
| 218 | func (so *SimulatedONU) Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error { |
| 219 | return errors.New("UnImplemented") |
| 220 | } |
| 221 | |
| 222 | func (so *SimulatedONU) Receive_packet_out(device *voltha.Device, egress_port_no int, msg openflow_13.PacketOut) error { |
| 223 | return errors.New("UnImplemented") |
| 224 | } |
| 225 | |
| 226 | func (so *SimulatedONU) Suppress_alarm(filter *voltha.AlarmFilter) error { |
| 227 | return errors.New("UnImplemented") |
| 228 | } |
| 229 | |
| 230 | func (so *SimulatedONU) Unsuppress_alarm(filter *voltha.AlarmFilter) error { |
| 231 | return errors.New("UnImplemented") |
| 232 | } |
| 233 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 234 | func (so *SimulatedONU) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 235 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 236 | } |
| 237 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 238 | func (so *SimulatedONU) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 239 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 240 | } |
| 241 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 242 | func (so *SimulatedONU) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 243 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 244 | } |
| 245 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 246 | func (so *SimulatedONU) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 247 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 248 | } |
| 249 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 250 | func (so *SimulatedONU) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 251 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 252 | } |