blob: 267898766adbdbde5d31c4ad3dbadc949d0c3bd3 [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 {
188 return errors.New("UnImplemented")
189}
190
191func (oo *OpenOLT) Reenable_device(device *voltha.Device) error {
192 return errors.New("UnImplemented")
193}
194
195func (oo *OpenOLT) Reboot_device(device *voltha.Device) error {
196 return errors.New("UnImplemented")
197}
198
199func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {
200 return errors.New("UnImplemented")
201}
202
203func (oo *OpenOLT) Gelete_device(device *voltha.Device) error {
204 return errors.New("UnImplemented")
205}
206
207func (oo *OpenOLT) Get_device_details(device *voltha.Device) error {
208 return errors.New("UnImplemented")
209}
210
211func (oo *OpenOLT) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups) error {
212 return errors.New("UnImplemented")
213}
214
215func (oo *OpenOLT) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges) error {
216 log.Debugw("Update_flows_incrementally", log.Fields{"deviceId": device.Id, "flows": flows})
217 if handler := oo.getDeviceHandler(device.Id); handler != nil {
218 return handler.UpdateFlowsIncrementally(device, flows, groups)
219 }
220 log.Errorw("Update_flows_incrementally failed-device-handler-not-set", log.Fields{"deviceId": device.Id})
221 return errors.New("device-handler-not-set")
222}
223
224func (oo *OpenOLT) Update_pm_config(device *voltha.Device, pm_configs *voltha.PmConfigs) error {
225 return errors.New("UnImplemented")
226}
227
228func (oo *OpenOLT) Receive_packet_out(device *voltha.Device, egress_port_no int, msg openflow_13.PacketOut) error {
229 return errors.New("UnImplemented")
230}
231
232func (oo *OpenOLT) Suppress_alarm(filter *voltha.AlarmFilter) error {
233 return errors.New("UnImplemented")
234}
235
236func (oo *OpenOLT) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
237 return errors.New("UnImplemented")
238}
239
240func (oo *OpenOLT) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
241 return nil, errors.New("UnImplemented")
242}
243
244func (oo *OpenOLT) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
245 return nil, errors.New("UnImplemented")
246}
247
248func (oo *OpenOLT) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
249 return nil, errors.New("UnImplemented")
250}
251
252func (oo *OpenOLT) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
253 return nil, errors.New("UnImplemented")
254}
255
256func (oo *OpenOLT) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
257 return nil, errors.New("UnImplemented")
258}