blob: 358d9f7ffa96d6c4bde2e4a5eb71a2e0543a8e72 [file] [log] [blame]
Phaneendra Manda4c62c802019-03-06 21:37:49 +05301/*
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 (
cuilin20187b2a8c32019-03-26 19:52:28 -070019 "context"
20 "errors"
21 "fmt"
22 "sync"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053023
cuilin20187b2a8c32019-03-26 19:52:28 -070024 com "github.com/opencord/voltha-go/adapters/common"
25 "github.com/opencord/voltha-go/common/log"
26 "github.com/opencord/voltha-go/kafka"
manikkaraj kbf256be2019-03-25 00:13:48 +053027 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"
Phaneendra Manda4c62c802019-03-06 21:37:49 +053030)
31
32type OpenOLT struct {
cuilin20187b2a8c32019-03-26 19:52:28 -070033 deviceHandlers map[string]*DeviceHandler
34 coreProxy *com.CoreProxy
35 adapterProxy *com.AdapterProxy
36 kafkaICProxy *kafka.InterContainerProxy
37 numOnus int
manikkaraj kbf256be2019-03-25 00:13:48 +053038 KVStoreHost string
39 KVStorePort int
40 KVStoreType string
cuilin20187b2a8c32019-03-26 19:52:28 -070041 exitChannel chan int
42 lockDeviceHandlersMap sync.RWMutex
Phaneendra Manda4c62c802019-03-06 21:37:49 +053043}
44
manikkaraj kbf256be2019-03-25 00:13:48 +053045func NewOpenOLT(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy, adapterProxy *com.AdapterProxy, onuNumber int, kvStoreHost string, kvStorePort int, KVStoreType string) *OpenOLT {
cuilin20187b2a8c32019-03-26 19:52:28 -070046 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
manikkaraj kbf256be2019-03-25 00:13:48 +053053 openOLT.KVStoreHost = kvStoreHost
54 openOLT.KVStorePort = kvStorePort
55 openOLT.KVStoreType = KVStoreType
cuilin20187b2a8c32019-03-26 19:52:28 -070056 openOLT.lockDeviceHandlersMap = sync.RWMutex{}
57 return &openOLT
Phaneendra Manda4c62c802019-03-06 21:37:49 +053058}
59
60func (oo *OpenOLT) Start(ctx context.Context) error {
cuilin20187b2a8c32019-03-26 19:52:28 -070061 log.Info("starting-device-manager")
62 log.Info("device-manager-started")
63 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +053064}
65
66func (oo *OpenOLT) Stop(ctx context.Context) error {
cuilin20187b2a8c32019-03-26 19:52:28 -070067 log.Info("stopping-device-manager")
68 oo.exitChannel <- 1
69 log.Info("device-manager-stopped")
70 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +053071}
72
73func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) {
cuilin20187b2a8c32019-03-26 19:52:28 -070074 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 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +053083}
84
85func (oo *OpenOLT) addDeviceHandlerToMap(agent *DeviceHandler) {
manikkaraj kbf256be2019-03-25 00:13:48 +053086 oo.lockDeviceHandlersMap.Lock()
87 defer oo.lockDeviceHandlersMap.Unlock()
cuilin20187b2a8c32019-03-26 19:52:28 -070088 if _, exist := oo.deviceHandlers[agent.deviceId]; !exist {
89 oo.deviceHandlers[agent.deviceId] = agent
manikkaraj kbf256be2019-03-25 00:13:48 +053090 }
Phaneendra Manda4c62c802019-03-06 21:37:49 +053091}
92
93func (oo *OpenOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) {
manikkaraj kbf256be2019-03-25 00:13:48 +053094 oo.lockDeviceHandlersMap.Lock()
95 defer oo.lockDeviceHandlersMap.Unlock()
cuilin20187b2a8c32019-03-26 19:52:28 -070096 delete(oo.deviceHandlers, agent.deviceId)
Phaneendra Manda4c62c802019-03-06 21:37:49 +053097}
98
99func (oo *OpenOLT) getDeviceHandler(deviceId string) *DeviceHandler {
cuilin20187b2a8c32019-03-26 19:52:28 -0700100 oo.lockDeviceHandlersMap.Lock()
101 defer oo.lockDeviceHandlersMap.Unlock()
102 if agent, ok := oo.deviceHandlers[deviceId]; ok {
103 return agent
104 }
105 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530106}
107
108func (oo *OpenOLT) createDeviceTopic(device *voltha.Device) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700109 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
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530117}
118
119func (oo *OpenOLT) Adopt_device(device *voltha.Device) error {
manikkaraj kbf256be2019-03-25 00:13:48 +0530120 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 {
cuilin20187b2a8c32019-03-26 19:52:28 -0700127 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, device, oo)
manikkaraj kbf256be2019-03-25 00:13:48 +0530128 oo.addDeviceHandlerToMap(handler)
129 go handler.AdoptDevice(device)
130 // Launch the creation of the device topic
cuilin20187b2a8c32019-03-26 19:52:28 -0700131 // go oo.createDeviceTopic(device)
manikkaraj kbf256be2019-03-25 00:13:48 +0530132 }
133 return nil
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530134}
135
136func (oo *OpenOLT) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700137 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")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530143}
144
145func (oo *OpenOLT) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) {
cuilin20187b2a8c32019-03-26 19:52:28 -0700146 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")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530152}
153
154func (oo *OpenOLT) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
cuilin20187b2a8c32019-03-26 19:52:28 -0700155 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))
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530165}
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 {
manikkaraj kbf256be2019-03-25 00:13:48 +0530216 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")
Phaneendra Manda4c62c802019-03-06 21:37:49 +0530222}
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}