blob: 3d10a6fb583012e987897056dff271c10620dda7 [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
Devmalya Paulfb990a52019-07-09 10:01:49 -040039 eventProxy *com.EventProxy
Girish Gowdru0c588b22019-04-23 23:24:56 -040040 kafkaICProxy *kafka.InterContainerProxy
41 numOnus int
42 KVStoreHost string
43 KVStorePort int
44 KVStoreType string
45 exitChannel chan int
46 lockDeviceHandlersMap sync.RWMutex
47}
48
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070049//NewOpenOLT returns a new instance of OpenOLT
Devmalya Paulfb990a52019-07-09 10:01:49 -040050func NewOpenOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy, adapterProxy *com.AdapterProxy, eventProxy *com.EventProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) *OpenOLT {
Girish Gowdru0c588b22019-04-23 23:24:56 -040051 var openOLT OpenOLT
52 openOLT.exitChannel = make(chan int, 1)
53 openOLT.deviceHandlers = make(map[string]*DeviceHandler)
54 openOLT.kafkaICProxy = kafkaICProxy
55 openOLT.numOnus = onuNumber
56 openOLT.coreProxy = coreProxy
57 openOLT.adapterProxy = adapterProxy
Devmalya Paulfb990a52019-07-09 10:01:49 -040058 openOLT.eventProxy = eventProxy
Girish Gowdru0c588b22019-04-23 23:24:56 -040059 openOLT.KVStoreHost = kvStoreHost
60 openOLT.KVStorePort = kvStorePort
61 openOLT.KVStoreType = KVStoreType
62 openOLT.lockDeviceHandlersMap = sync.RWMutex{}
63 return &openOLT
64}
65
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070066//Start starts (logs) the device manager
Girish Gowdru0c588b22019-04-23 23:24:56 -040067func (oo *OpenOLT) Start(ctx context.Context) error {
68 log.Info("starting-device-manager")
69 log.Info("device-manager-started")
70 return nil
71}
72
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070073//Stop terminates the session
Girish Gowdru0c588b22019-04-23 23:24:56 -040074func (oo *OpenOLT) Stop(ctx context.Context) error {
75 log.Info("stopping-device-manager")
76 oo.exitChannel <- 1
77 log.Info("device-manager-stopped")
78 return nil
79}
80
81func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) {
82 if ctx.Err() == nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070083 // Returned response only of the ctx has not been canceled/timeout/etc
Girish Gowdru0c588b22019-04-23 23:24:56 -040084 // Channel is automatically closed when a context is Done
85 ch <- result
86 log.Debugw("sendResponse", log.Fields{"result": result})
87 } else {
88 // Should the transaction be reverted back?
89 log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()})
90 }
91}
92
93func (oo *OpenOLT) addDeviceHandlerToMap(agent *DeviceHandler) {
94 oo.lockDeviceHandlersMap.Lock()
95 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070096 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
97 oo.deviceHandlers[agent.deviceID] = agent
Girish Gowdru0c588b22019-04-23 23:24:56 -040098 }
99}
100
101func (oo *OpenOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) {
102 oo.lockDeviceHandlersMap.Lock()
103 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700104 delete(oo.deviceHandlers, agent.deviceID)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400105}
106
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700107func (oo *OpenOLT) getDeviceHandler(deviceID string) *DeviceHandler {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400108 oo.lockDeviceHandlersMap.Lock()
109 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700110 if agent, ok := oo.deviceHandlers[deviceID]; ok {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400111 return agent
112 }
113 return nil
114}
115
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700116//createDeviceTopic returns
Girish Gowdru0c588b22019-04-23 23:24:56 -0400117func (oo *OpenOLT) createDeviceTopic(device *voltha.Device) error {
118 log.Infow("create-device-topic", log.Fields{"deviceId": device.Id})
119 deviceTopic := kafka.Topic{Name: oo.kafkaICProxy.DefaultTopic.Name + "_" + device.Id}
120 // TODO for the offset
121 if err := oo.kafkaICProxy.SubscribeWithDefaultRequestHandler(deviceTopic, 0); err != nil {
122 log.Infow("create-device-topic-failed", log.Fields{"deviceId": device.Id, "error": err})
123 return err
124 }
125 return nil
126}
127
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700128// Adopt_device creates a new device handler if not present already and then adopts the device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400129func (oo *OpenOLT) Adopt_device(device *voltha.Device) error {
130 if device == nil {
131 log.Warn("device-is-nil")
132 return errors.New("nil-device")
133 }
134 log.Infow("adopt-device", log.Fields{"deviceId": device.Id})
135 var handler *DeviceHandler
136 if handler = oo.getDeviceHandler(device.Id); handler == nil {
Devmalya Paulfb990a52019-07-09 10:01:49 -0400137 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400138 oo.addDeviceHandlerToMap(handler)
139 go handler.AdoptDevice(device)
140 // Launch the creation of the device topic
141 // go oo.createDeviceTopic(device)
142 }
143 return nil
144}
145
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700146//Get_ofp_device_info returns OFP information for the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400147func (oo *OpenOLT) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
148 log.Infow("Get_ofp_device_info", log.Fields{"deviceId": device.Id})
149 if handler := oo.getDeviceHandler(device.Id); handler != nil {
150 return handler.GetOfpDeviceInfo(device)
151 }
152 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
153 return nil, errors.New("device-handler-not-set")
154}
155
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700156//Get_ofp_port_info returns OFP port information for the given device
157func (oo *OpenOLT) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400158 log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id})
159 if handler := oo.getDeviceHandler(device.Id); handler != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700160 return handler.GetOfpPortInfo(device, portNo)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400161 }
162 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
163 return nil, errors.New("device-handler-not-set")
164}
165
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700166//Process_inter_adapter_message sends messages to a target device (between adapters)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400167func (oo *OpenOLT) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
168 log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
169 targetDevice := msg.Header.ProxyDeviceId // Request?
170 if targetDevice == "" && msg.Header.ToDeviceId != "" {
171 // Typical response
172 targetDevice = msg.Header.ToDeviceId
173 }
174 if handler := oo.getDeviceHandler(targetDevice); handler != nil {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700175 return handler.ProcessInterAdapterMessage(msg)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400176 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700177 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", targetDevice))
Girish Gowdru0c588b22019-04-23 23:24:56 -0400178}
179
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700180//Adapter_descriptor not implemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400181func (oo *OpenOLT) Adapter_descriptor() error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700182 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400183}
184
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700185//Device_types unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400186func (oo *OpenOLT) Device_types() (*voltha.DeviceTypes, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700187 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400188}
189
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700190//Health returns unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400191func (oo *OpenOLT) Health() (*voltha.HealthStatus, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700192 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400193}
194
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700195//Reconcile_device unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400196func (oo *OpenOLT) Reconcile_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700197 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400198}
199
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700200//Abandon_device unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400201func (oo *OpenOLT) Abandon_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700202 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400203}
204
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700205//Disable_device disables the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400206func (oo *OpenOLT) Disable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400207 log.Infow("disable-device", log.Fields{"deviceId": device.Id})
208 if handler := oo.getDeviceHandler(device.Id); handler != nil {
209 return handler.DisableDevice(device)
210 }
211 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
212 return errors.New("device-handler-not-found")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400213}
214
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700215//Reenable_device enables the olt device after disable
Girish Gowdru0c588b22019-04-23 23:24:56 -0400216func (oo *OpenOLT) Reenable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400217 log.Infow("reenable-device", log.Fields{"deviceId": device.Id})
218 if handler := oo.getDeviceHandler(device.Id); handler != nil {
219 return handler.ReenableDevice(device)
220 }
221 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
222 return errors.New("device-handler-not-found")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400223}
224
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700225//Reboot_device reboots the given device
Girish Gowdru0c588b22019-04-23 23:24:56 -0400226func (oo *OpenOLT) Reboot_device(device *voltha.Device) error {
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400227 log.Infow("reboot-device", log.Fields{"deviceId": device.Id})
228 if handler := oo.getDeviceHandler(device.Id); handler != nil {
229 return handler.RebootDevice(device)
230 }
231 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
232 return errors.New("device-handler-not-found")
233
Girish Gowdru0c588b22019-04-23 23:24:56 -0400234}
235
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700236//Self_test_device unimplented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400237func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700238 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400239}
240
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700241//Delete_device unimplemented
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400242func (oo *OpenOLT) Delete_device(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700243 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400244}
245
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700246//Get_device_details unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400247func (oo *OpenOLT) Get_device_details(device *voltha.Device) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700248 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400249}
250
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700251//Update_flows_bulk returns
Manikkaraj kb1d51442019-07-23 10:41:02 -0400252func (oo *OpenOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700253 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400254}
255
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700256//Update_flows_incrementally updates (add/remove) the flows on a given device
Manikkaraj kb1d51442019-07-23 10:41:02 -0400257func (oo *OpenOLT) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
258 log.Debugw("Update_flows_incrementally", log.Fields{"deviceId": device.Id, "flows": flows, "flowMetadata": flowMetadata})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400259 if handler := oo.getDeviceHandler(device.Id); handler != nil {
Manikkaraj kb1d51442019-07-23 10:41:02 -0400260 return handler.UpdateFlowsIncrementally(device, flows, groups, flowMetadata)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400261 }
262 log.Errorw("Update_flows_incrementally failed-device-handler-not-set", log.Fields{"deviceId": device.Id})
263 return errors.New("device-handler-not-set")
264}
265
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700266//Update_pm_config returns PmConfigs nil or error
267func (oo *OpenOLT) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
268 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400269}
270
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700271//Receive_packet_out sends packet out to the device
272func (oo *OpenOLT) Receive_packet_out(deviceID string, egressPortNo int, packet *openflow_13.OfpPacketOut) error {
273 log.Debugw("Receive_packet_out", log.Fields{"deviceId": deviceID, "egress_port_no": egressPortNo, "pkt": packet})
274 if handler := oo.getDeviceHandler(deviceID); handler != nil {
275 return handler.PacketOut(egressPortNo, packet)
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400276 }
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700277 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 -0400278 return errors.New("device-handler-not-set")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400279}
280
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700281//Suppress_alarm unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400282func (oo *OpenOLT) Suppress_alarm(filter *voltha.AlarmFilter) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700283 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400284}
285
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700286//Unsuppress_alarm unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400287func (oo *OpenOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700288 return errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400289}
290
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700291//Download_image unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400292func (oo *OpenOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700293 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400294}
295
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700296//Get_image_download_status unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400297func (oo *OpenOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700298 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299}
300
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700301//Cancel_image_download unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400302func (oo *OpenOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700303 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400304}
305
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700306//Activate_image_update unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400307func (oo *OpenOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700308 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400309}
310
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700311//Revert_image_update unimplemented
Girish Gowdru0c588b22019-04-23 23:24:56 -0400312func (oo *OpenOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700313 return nil, errors.New("unImplemented")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400314}