blob: ce501ef72172862f5a757516e81565fc07a70a2d [file] [log] [blame]
Girish Gowdru0c588b22019-04-23 23:24:56 -04001/*
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 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
17//Package adaptercore provides the utility for olt devices, flows and statistics
Girish Gowdru0c588b22019-04-23 23:24:56 -040018package adaptercore
19
20import (
21 "context"
22 "errors"
23 "fmt"
24 "sync"
25
26 com "github.com/opencord/voltha-go/adapters/common"
27 "github.com/opencord/voltha-go/common/log"
28 "github.com/opencord/voltha-go/kafka"
29 ic "github.com/opencord/voltha-protos/go/inter_container"
30 "github.com/opencord/voltha-protos/go/openflow_13"
31 "github.com/opencord/voltha-protos/go/voltha"
32)
33
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070034//OpenOLT structure holds the OLT information
Girish Gowdru0c588b22019-04-23 23:24:56 -040035type OpenOLT struct {
36 deviceHandlers map[string]*DeviceHandler
37 coreProxy *com.CoreProxy
38 adapterProxy *com.AdapterProxy
39 kafkaICProxy *kafka.InterContainerProxy
40 numOnus int
41 KVStoreHost string
42 KVStorePort int
43 KVStoreType string
44 exitChannel chan int
45 lockDeviceHandlersMap sync.RWMutex
46}
47
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070048//NewOpenOLT returns a new instance of OpenOLT
Girish Gowdru0c588b22019-04-23 23:24:56 -040049func NewOpenOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy, adapterProxy *com.AdapterProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) *OpenOLT {
50 var openOLT OpenOLT
51 openOLT.exitChannel = make(chan int, 1)
52 openOLT.deviceHandlers = make(map[string]*DeviceHandler)
53 openOLT.kafkaICProxy = kafkaICProxy
54 openOLT.numOnus = onuNumber
55 openOLT.coreProxy = coreProxy
56 openOLT.adapterProxy = adapterProxy
57 openOLT.KVStoreHost = kvStoreHost
58 openOLT.KVStorePort = kvStorePort
59 openOLT.KVStoreType = KVStoreType
60 openOLT.lockDeviceHandlersMap = sync.RWMutex{}
61 return &openOLT
62}
63
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070064//Start starts (logs) the device manager
Girish Gowdru0c588b22019-04-23 23:24:56 -040065func (oo *OpenOLT) Start(ctx context.Context) error {
66 log.Info("starting-device-manager")
67 log.Info("device-manager-started")
68 return nil
69}
70
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070071//Stop terminates the session
Girish Gowdru0c588b22019-04-23 23:24:56 -040072func (oo *OpenOLT) Stop(ctx context.Context) error {
73 log.Info("stopping-device-manager")
74 oo.exitChannel <- 1
75 log.Info("device-manager-stopped")
76 return nil
77}
78
79func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) {
80 if ctx.Err() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070081 // Returned response only of the ctx has not been canceled/timeout/etc
Girish Gowdru0c588b22019-04-23 23:24:56 -040082 // Channel is automatically closed when a context is Done
83 ch <- result
84 log.Debugw("sendResponse", log.Fields{"result": result})
85 } else {
86 // Should the transaction be reverted back?
87 log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()})
88 }
89}
90
91func (oo *OpenOLT) addDeviceHandlerToMap(agent *DeviceHandler) {
92 oo.lockDeviceHandlersMap.Lock()
93 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070094 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
95 oo.deviceHandlers[agent.deviceID] = agent
Girish Gowdru0c588b22019-04-23 23:24:56 -040096 }
97}
98
99func (oo *OpenOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) {
100 oo.lockDeviceHandlersMap.Lock()
101 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700102 delete(oo.deviceHandlers, agent.deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400103}
104
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700105func (oo *OpenOLT) getDeviceHandler(deviceID string) *DeviceHandler {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400106 oo.lockDeviceHandlersMap.Lock()
107 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700108 if agent, ok := oo.deviceHandlers[deviceID]; ok {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400109 return agent
110 }
111 return nil
112}
113
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700114//createDeviceTopic returns
Girish Gowdru0c588b22019-04-23 23:24:56 -0400115func (oo *OpenOLT) createDeviceTopic(device *voltha.Device) error {
116 log.Infow("create-device-topic", log.Fields{"deviceId": device.Id})
117 deviceTopic := kafka.Topic{Name: oo.kafkaICProxy.DefaultTopic.Name + "_" + device.Id}
118 // TODO for the offset
119 if err := oo.kafkaICProxy.SubscribeWithDefaultRequestHandler(deviceTopic, 0); err != nil {
120 log.Infow("create-device-topic-failed", log.Fields{"deviceId": device.Id, "error": err})
121 return err
122 }
123 return nil
124}
125
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700126// Adopt_device creates a new device handler if not present already and then adopts the device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400127func (oo *OpenOLT) Adopt_device(device *voltha.Device) error {
128 if device == nil {
129 log.Warn("device-is-nil")
130 return errors.New("nil-device")
131 }
132 log.Infow("adopt-device", log.Fields{"deviceId": device.Id})
133 var handler *DeviceHandler
134 if handler = oo.getDeviceHandler(device.Id); handler == nil {
135 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, device, oo)
136 oo.addDeviceHandlerToMap(handler)
137 go handler.AdoptDevice(device)
138 // Launch the creation of the device topic
139 // go oo.createDeviceTopic(device)
140 }
141 return nil
142}
143
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700144//Get_ofp_device_info returns OFP information for the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400145func (oo *OpenOLT) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
146 log.Infow("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
147 if handler := oo.getDeviceHandler(device.Id); handler != nil {
148 return handler.GetOfpDeviceInfo(device)
149 }
150 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
151 return nil, errors.New("device-handler-not-set")
152}
153
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700154//Get_ofp_port_info returns OFP port information for the given device
155func (oo *OpenOLT) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400156 log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id})
157 if handler := oo.getDeviceHandler(device.Id); handler != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700158 return handler.GetOfpPortInfo(device, portNo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400159 }
160 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
161 return nil, errors.New("device-handler-not-set")
162}
163
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700164//Process_inter_adapter_message sends messages to a target device (between adapters)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400165func (oo *OpenOLT) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
166 log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
167 targetDevice := msg.Header.ProxyDeviceId // Request?
168 if targetDevice == "" && msg.Header.ToDeviceId != "" {
169 // Typical response
170 targetDevice = msg.Header.ToDeviceId
171 }
172 if handler := oo.getDeviceHandler(targetDevice); handler != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700173 return handler.ProcessInterAdapterMessage(msg)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400174 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", targetDevice))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176}
177
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700178//Adapter_descriptor not implemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400179func (oo *OpenOLT) Adapter_descriptor() error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700180 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400181}
182
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700183//Device_types unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400184func (oo *OpenOLT) Device_types() (*voltha.DeviceTypes, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186}
187
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700188//Health returns unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400189func (oo *OpenOLT) Health() (*voltha.HealthStatus, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700190 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191}
192
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700193//Reconcile_device unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400194func (oo *OpenOLT) Reconcile_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196}
197
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700198//Abandon_device unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400199func (oo *OpenOLT) Abandon_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201}
202
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700203//Disable_device disables the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400204func (oo *OpenOLT) Disable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400205 log.Infow("disable-device", log.Fields{"deviceId": device.Id})
206 if handler := oo.getDeviceHandler(device.Id); handler != nil {
207 return handler.DisableDevice(device)
208 }
209 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
210 return errors.New("device-handler-not-found")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400211}
212
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700213//Reenable_device enables the olt device after disable
Girish Gowdru0c588b22019-04-23 23:24:56 -0400214func (oo *OpenOLT) Reenable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400215 log.Infow("reenable-device", log.Fields{"deviceId": device.Id})
216 if handler := oo.getDeviceHandler(device.Id); handler != nil {
217 return handler.ReenableDevice(device)
218 }
219 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
220 return errors.New("device-handler-not-found")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400221}
222
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700223//Reboot_device reboots the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400224func (oo *OpenOLT) Reboot_device(device *voltha.Device) error {
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400225 log.Infow("reboot-device", log.Fields{"deviceId": device.Id})
226 if handler := oo.getDeviceHandler(device.Id); handler != nil {
227 return handler.RebootDevice(device)
228 }
229 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
230 return errors.New("device-handler-not-found")
231
Girish Gowdru0c588b22019-04-23 23:24:56 -0400232}
233
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700234//Self_test_device unimplented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400235func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237}
238
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700239//Delete_device unimplemented
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400240func (oo *OpenOLT) Delete_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700241 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242}
243
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700244//Get_device_details unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400245func (oo *OpenOLT) Get_device_details(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700246 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247}
248
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700249//Update_flows_bulk returns
Girish Gowdru0c588b22019-04-23 23:24:56 -0400250func (oo *OpenOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400252}
253
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700254//Update_flows_incrementally updates (add/remove) the flows on a given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255func (oo *OpenOLT) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges) error {
256 log.Debugw("Update_flows_incrementally", log.Fields{"deviceId": device.Id, "flows": flows})
257 if handler := oo.getDeviceHandler(device.Id); handler != nil {
258 return handler.UpdateFlowsIncrementally(device, flows, groups)
259 }
260 log.Errorw("Update_flows_incrementally failed-device-handler-not-set", log.Fields{"deviceId": device.Id})
261 return errors.New("device-handler-not-set")
262}
263
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700264//Update_pm_config returns PmConfigs nil or error
265func (oo *OpenOLT) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
266 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400267}
268
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700269//Receive_packet_out sends packet out to the device
270func (oo *OpenOLT) Receive_packet_out(deviceID string, egressPortNo int, packet *openflow_13.OfpPacketOut) error {
271 log.Debugw("Receive_packet_out", log.Fields{"deviceId": deviceID, "egress_port_no": egressPortNo, "pkt": packet})
272 if handler := oo.getDeviceHandler(deviceID); handler != nil {
273 return handler.PacketOut(egressPortNo, packet)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400274 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700275 log.Errorw("Receive_packet_out failed-device-handler-not-set", log.Fields{"deviceId": deviceID, "egressport": egressPortNo, "packet": packet})
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400276 return errors.New("device-handler-not-set")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400277}
278
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700279//Suppress_alarm unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400280func (oo *OpenOLT) Suppress_alarm(filter *voltha.AlarmFilter) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282}
283
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700284//Unsuppress_alarm unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400285func (oo *OpenOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700286 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287}
288
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700289//Download_image unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400290func (oo *OpenOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700291 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292}
293
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700294//Get_image_download_status unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400295func (oo *OpenOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400297}
298
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700299//Cancel_image_download unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400300func (oo *OpenOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302}
303
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700304//Activate_image_update unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400305func (oo *OpenOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700306 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307}
308
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700309//Revert_image_update unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400310func (oo *OpenOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700311 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400312}