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