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