blob: 2e5174fc04403fc6f8295d967557b346fd2ecda8 [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 */
16package adaptercore
17
18import (
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
32type 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
45func 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
60func (oo *OpenOLT) Start(ctx context.Context) error {
61 log.Info("starting-device-manager")
62 log.Info("device-manager-started")
63 return nil
64}
65
66func (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
73func 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
85func (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
93func (oo *OpenOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) {
94 oo.lockDeviceHandlersMap.Lock()
95 defer oo.lockDeviceHandlersMap.Unlock()
96 delete(oo.deviceHandlers, agent.deviceId)
97}
98
99func (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
108func (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
119func (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
136func (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
145func (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
154func (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
167func (oo *OpenOLT) Adapter_descriptor() error {
168 return errors.New("UnImplemented")
169}
170
171func (oo *OpenOLT) Device_types() (*voltha.DeviceTypes, error) {
172 return nil, errors.New("UnImplemented")
173}
174
175func (oo *OpenOLT) Health() (*voltha.HealthStatus, error) {
176 return nil, errors.New("UnImplemented")
177}
178
179func (oo *OpenOLT) Reconcile_device(device *voltha.Device) error {
180 return errors.New("UnImplemented")
181}
182
183func (oo *OpenOLT) Abandon_device(device *voltha.Device) error {
184 return errors.New("UnImplemented")
185}
186
187func (oo *OpenOLT) Disable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400188 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 Gowdru0c588b22019-04-23 23:24:56 -0400194}
195
196func (oo *OpenOLT) Reenable_device(device *voltha.Device) error {
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400197 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 Gowdru0c588b22019-04-23 23:24:56 -0400203}
204
205func (oo *OpenOLT) Reboot_device(device *voltha.Device) error {
206 return errors.New("UnImplemented")
207}
208
209func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {
210 return errors.New("UnImplemented")
211}
212
213func (oo *OpenOLT) Gelete_device(device *voltha.Device) error {
214 return errors.New("UnImplemented")
215}
216
217func (oo *OpenOLT) Get_device_details(device *voltha.Device) error {
218 return errors.New("UnImplemented")
219}
220
221func (oo *OpenOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error {
222 return errors.New("UnImplemented")
223}
224
225func (oo *OpenOLT) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges) error {
226 log.Debugw("Update_flows_incrementally", log.Fields{"deviceId": device.Id, "flows": flows})
227 if handler := oo.getDeviceHandler(device.Id); handler != nil {
228 return handler.UpdateFlowsIncrementally(device, flows, groups)
229 }
230 log.Errorw("Update_flows_incrementally failed-device-handler-not-set", log.Fields{"deviceId": device.Id})
231 return errors.New("device-handler-not-set")
232}
233
234func (oo *OpenOLT) Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error {
235 return errors.New("UnImplemented")
236}
237
238func (oo *OpenOLT) Receive_packet_out(device *voltha.Device, egress_port_no int, msg openflow_13.PacketOut) error {
239 return errors.New("UnImplemented")
240}
241
242func (oo *OpenOLT) Suppress_alarm(filter *voltha.AlarmFilter) error {
243 return errors.New("UnImplemented")
244}
245
246func (oo *OpenOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
247 return errors.New("UnImplemented")
248}
249
250func (oo *OpenOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
251 return nil, errors.New("UnImplemented")
252}
253
254func (oo *OpenOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
255 return nil, errors.New("UnImplemented")
256}
257
258func (oo *OpenOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
259 return nil, errors.New("UnImplemented")
260}
261
262func (oo *OpenOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
263 return nil, errors.New("UnImplemented")
264}
265
266func (oo *OpenOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
267 return nil, errors.New("UnImplemented")
268}