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" |
| 25 | ic "github.com/opencord/voltha-go/protos/inter_container" |
| 26 | "github.com/opencord/voltha-go/protos/openflow_13" |
| 27 | "github.com/opencord/voltha-go/protos/voltha" |
| 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 | |
| 97 | func (so *SimulatedONU) createDeviceTopic(device *voltha.Device) error { |
| 98 | log.Infow("create-device-topic", log.Fields{"deviceId": device.Id}) |
| 99 | deviceTopic := kafka.Topic{Name: so.kafkaICProxy.DefaultTopic.Name + "_" + device.Id} |
| 100 | if err := so.kafkaICProxy.SubscribeWithDefaultRequestHandler(deviceTopic); err != nil { |
| 101 | log.Infow("create-device-topic-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 102 | return err |
| 103 | } |
| 104 | return nil |
| 105 | } |
| 106 | |
| 107 | func (so *SimulatedONU) Adopt_device(device *voltha.Device) error { |
| 108 | if device == nil { |
| 109 | log.Warn("device-is-nil") |
| 110 | return errors.New("nil-device") |
| 111 | } |
| 112 | log.Infow("adopt-device", log.Fields{"deviceId": device.Id}) |
| 113 | var handler *DeviceHandler |
| 114 | if handler = so.getDeviceHandler(device.Id); handler == nil { |
| 115 | handler := NewDeviceHandler(so.coreProxy, device, so) |
| 116 | so.addDeviceHandlerToMap(handler) |
| 117 | go handler.AdoptDevice(device) |
| 118 | // Launch the creation of the device topic |
| 119 | go so.createDeviceTopic(device) |
| 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | func (so *SimulatedONU) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { |
| 125 | log.Infow("not-implemented-for-onu", log.Fields{"deviceId": device.Id}) |
| 126 | return nil, nil |
| 127 | } |
| 128 | |
| 129 | func (so *SimulatedONU) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) { |
| 130 | log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id}) |
| 131 | if handler := so.getDeviceHandler(device.Id); handler != nil { |
| 132 | return handler.GetOfpPortInfo(device, port_no) |
| 133 | } |
| 134 | log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id}) |
| 135 | return nil, errors.New("device-handler-not-set") |
| 136 | } |
| 137 | |
| 138 | func (so *SimulatedONU) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { |
| 139 | log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id}) |
| 140 | targetDevice := msg.Header.ProxyDeviceId // Request? |
| 141 | if targetDevice == "" && msg.Header.ToDeviceId != "" { |
| 142 | // Typical response |
| 143 | targetDevice = msg.Header.ToDeviceId |
| 144 | } |
| 145 | if handler := so.getDeviceHandler(targetDevice); handler != nil { |
| 146 | return handler.Process_inter_adapter_message(msg) |
| 147 | } |
| 148 | return errors.New(fmt.Sprintf("handler-not-found-%s", targetDevice)) |
| 149 | } |
| 150 | |
| 151 | func (so *SimulatedONU) Adapter_descriptor() error { |
| 152 | return errors.New("UnImplemented") |
| 153 | } |
| 154 | |
| 155 | func (so *SimulatedONU) Device_types() (*voltha.DeviceTypes, error) { |
| 156 | return nil, errors.New("UnImplemented") |
| 157 | } |
| 158 | |
| 159 | func (so *SimulatedONU) Health() (*voltha.HealthStatus, error) { |
| 160 | return nil, errors.New("UnImplemented") |
| 161 | } |
| 162 | |
| 163 | func (so *SimulatedONU) Reconcile_device(device *voltha.Device) error { |
| 164 | return errors.New("UnImplemented") |
| 165 | } |
| 166 | |
| 167 | func (so *SimulatedONU) Abandon_device(device *voltha.Device) error { |
| 168 | return errors.New("UnImplemented") |
| 169 | } |
| 170 | |
| 171 | func (so *SimulatedONU) Disable_device(device *voltha.Device) error { |
| 172 | return errors.New("UnImplemented") |
| 173 | } |
| 174 | |
| 175 | func (so *SimulatedONU) Reenable_device(device *voltha.Device) error { |
| 176 | return errors.New("UnImplemented") |
| 177 | } |
| 178 | |
| 179 | func (so *SimulatedONU) Reboot_device(device *voltha.Device) error { |
| 180 | return errors.New("UnImplemented") |
| 181 | } |
| 182 | |
| 183 | func (so *SimulatedONU) Self_test_device(device *voltha.Device) error { |
| 184 | return errors.New("UnImplemented") |
| 185 | } |
| 186 | |
| 187 | func (so *SimulatedONU) Gelete_device(device *voltha.Device) error { |
| 188 | return errors.New("UnImplemented") |
| 189 | } |
| 190 | |
| 191 | func (so *SimulatedONU) Get_device_details(device *voltha.Device) error { |
| 192 | return errors.New("UnImplemented") |
| 193 | } |
| 194 | |
| 195 | func (so *SimulatedONU) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error { |
| 196 | return errors.New("UnImplemented") |
| 197 | } |
| 198 | |
| 199 | func (so *SimulatedONU) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges) error { |
| 200 | return errors.New("UnImplemented") |
| 201 | } |
| 202 | |
| 203 | func (so *SimulatedONU) Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error { |
| 204 | return errors.New("UnImplemented") |
| 205 | } |
| 206 | |
| 207 | func (so *SimulatedONU) Receive_packet_out(device *voltha.Device, egress_port_no int, msg openflow_13.PacketOut) error { |
| 208 | return errors.New("UnImplemented") |
| 209 | } |
| 210 | |
| 211 | func (so *SimulatedONU) Suppress_alarm(filter *voltha.AlarmFilter) error { |
| 212 | return errors.New("UnImplemented") |
| 213 | } |
| 214 | |
| 215 | func (so *SimulatedONU) Unsuppress_alarm(filter *voltha.AlarmFilter) error { |
| 216 | return errors.New("UnImplemented") |
| 217 | } |
| 218 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 219 | func (so *SimulatedONU) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 220 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 221 | } |
| 222 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 223 | func (so *SimulatedONU) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 224 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 225 | } |
| 226 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 227 | func (so *SimulatedONU) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 228 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 229 | } |
| 230 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 231 | func (so *SimulatedONU) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 232 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 233 | } |
| 234 | |
khenaidoo | f5a5bfa | 2019-01-23 22:20:29 -0500 | [diff] [blame] | 235 | func (so *SimulatedONU) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { |
| 236 | return nil, errors.New("UnImplemented") |
khenaidoo | d2b6df9 | 2018-12-13 16:37:20 -0500 | [diff] [blame] | 237 | } |