blob: 81639bec293e69a1fec4d17cd9b5ec7ad7058075 [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 {
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400206 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 Gowdru0c588b22019-04-23 23:24:56 -0400213}
214
215func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {
216 return errors.New("UnImplemented")
217}
218
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400219func (oo *OpenOLT) Delete_device(device *voltha.Device) error {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400220 return errors.New("UnImplemented")
221}
222
223func (oo *OpenOLT) Get_device_details(device *voltha.Device) error {
224 return errors.New("UnImplemented")
225}
226
227func (oo *OpenOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error {
228 return errors.New("UnImplemented")
229}
230
231func (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
240func (oo *OpenOLT) Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error {
241 return errors.New("UnImplemented")
242}
243
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400244func (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 Gowdru0c588b22019-04-23 23:24:56 -0400251}
252
253func (oo *OpenOLT) Suppress_alarm(filter *voltha.AlarmFilter) error {
254 return errors.New("UnImplemented")
255}
256
257func (oo *OpenOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
258 return errors.New("UnImplemented")
259}
260
261func (oo *OpenOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
262 return nil, errors.New("UnImplemented")
263}
264
265func (oo *OpenOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
266 return nil, errors.New("UnImplemented")
267}
268
269func (oo *OpenOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
270 return nil, errors.New("UnImplemented")
271}
272
273func (oo *OpenOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
274 return nil, errors.New("UnImplemented")
275}
276
277func (oo *OpenOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
278 return nil, errors.New("UnImplemented")
279}