blob: ea5cfe4ee0736462c178663538e5b0f3cf1d77d7 [file] [log] [blame]
Scott Bakereee8dd82019-09-24 12:52:34 -07001/*
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"
Scott Bakerd5057192019-10-24 14:03:35 -070022 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
23 "github.com/opencord/voltha-lib-go/v2/pkg/kafka"
24 "github.com/opencord/voltha-lib-go/v2/pkg/log"
Scott Bakereee8dd82019-09-24 12:52:34 -070025 ic "github.com/opencord/voltha-protos/go/inter_container"
26 "github.com/opencord/voltha-protos/go/openflow_13"
27 "github.com/opencord/voltha-protos/go/voltha"
28 "sync"
29)
30
31type SimulatedONU struct {
32 deviceHandlers map[string]*DeviceHandler
33 coreProxy *com.CoreProxy
34 kafkaICProxy *kafka.InterContainerProxy
35 exitChannel chan int
36 lockDeviceHandlersMap sync.RWMutex
37}
38
39func NewSimulatedONU(ctx context.Context, kafkaICProxy *kafka.InterContainerProxy, coreProxy *com.CoreProxy) *SimulatedONU {
40 var simulatedOLT SimulatedONU
41 simulatedOLT.exitChannel = make(chan int, 1)
42 simulatedOLT.deviceHandlers = make(map[string]*DeviceHandler)
43 simulatedOLT.kafkaICProxy = kafkaICProxy
44 simulatedOLT.coreProxy = coreProxy
45 simulatedOLT.lockDeviceHandlersMap = sync.RWMutex{}
46 return &simulatedOLT
47}
48
49func (so *SimulatedONU) Start(ctx context.Context) error {
50 log.Info("starting-device-manager")
51 log.Info("device-manager-started")
52 return nil
53}
54
55func (so *SimulatedONU) Stop(ctx context.Context) error {
56 log.Info("stopping-device-manager")
57 so.exitChannel <- 1
58 log.Info("device-manager-stopped")
59 return nil
60}
61
62func sendResponse(ctx context.Context, ch chan interface{}, result interface{}) {
63 if ctx.Err() == nil {
64 // Returned response only of the ctx has not been cancelled/timeout/etc
65 // Channel is automatically closed when a context is Done
66 ch <- result
67 log.Debugw("sendResponse", log.Fields{"result": result})
68 } else {
69 // Should the transaction be reverted back?
70 log.Debugw("sendResponse-context-error", log.Fields{"context-error": ctx.Err()})
71 }
72}
73
74func (so *SimulatedONU) addDeviceHandlerToMap(agent *DeviceHandler) {
75 so.lockDeviceHandlersMap.Lock()
76 defer so.lockDeviceHandlersMap.Unlock()
77 if _, exist := so.deviceHandlers[agent.deviceId]; !exist {
78 so.deviceHandlers[agent.deviceId] = agent
79 }
80}
81
82func (so *SimulatedONU) deleteDeviceHandlerToMap(agent *DeviceHandler) {
83 so.lockDeviceHandlersMap.Lock()
84 defer so.lockDeviceHandlersMap.Unlock()
85 delete(so.deviceHandlers, agent.deviceId)
86}
87
88func (so *SimulatedONU) getDeviceHandler(deviceId string) *DeviceHandler {
89 so.lockDeviceHandlersMap.Lock()
90 defer so.lockDeviceHandlersMap.Unlock()
91 if agent, ok := so.deviceHandlers[deviceId]; ok {
92 return agent
93 }
94 return nil
95}
96
97func (so *SimulatedONU) Adopt_device(device *voltha.Device) error {
98 if device == nil {
99 log.Warn("device-is-nil")
100 return errors.New("nil-device")
101 }
102 log.Infow("adopt-device", log.Fields{"deviceId": device.Id})
103 var handler *DeviceHandler
104 if handler = so.getDeviceHandler(device.Id); handler == nil {
105 handler := NewDeviceHandler(so.coreProxy, device, so)
106 so.addDeviceHandlerToMap(handler)
107 go handler.AdoptDevice(device)
108 }
109 return nil
110}
111
112func (so *SimulatedONU) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
113 log.Infow("not-implemented-for-onu", log.Fields{"deviceId": device.Id})
114 return nil, nil
115}
116
117func (so *SimulatedONU) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) {
118 log.Infow("Get_ofp_port_info", log.Fields{"deviceId": device.Id})
119 if handler := so.getDeviceHandler(device.Id); handler != nil {
120 return handler.GetOfpPortInfo(device, port_no)
121 }
122 log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
123 return nil, errors.New("device-handler-not-set")
124}
125
126func (so *SimulatedONU) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
127 log.Infow("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id})
128 targetDevice := msg.Header.ProxyDeviceId // Request?
129 if targetDevice == "" && msg.Header.ToDeviceId != "" {
130 // Typical response
131 targetDevice = msg.Header.ToDeviceId
132 }
133 if handler := so.getDeviceHandler(targetDevice); handler != nil {
134 return handler.Process_inter_adapter_message(msg)
135 }
136 return errors.New(fmt.Sprintf("handler-not-found-%s", targetDevice))
137}
138
139func (so *SimulatedONU) Adapter_descriptor() error {
140 return errors.New("UnImplemented")
141}
142
143func (so *SimulatedONU) Device_types() (*voltha.DeviceTypes, error) {
144 return nil, errors.New("UnImplemented")
145}
146
147func (so *SimulatedONU) Health() (*voltha.HealthStatus, error) {
148 return nil, errors.New("UnImplemented")
149}
150
151func (so *SimulatedONU) Reconcile_device(device *voltha.Device) error {
152 if device == nil {
153 log.Warn("device-is-nil")
154 return errors.New("nil-device")
155 }
156 log.Infow("reconcile-device", log.Fields{"deviceId": device.Id})
157 var handler *DeviceHandler
158 handler = so.getDeviceHandler(device.Id)
159 if handler == nil {
160 // Adapter has restarted
161 handler = NewDeviceHandler(so.coreProxy, device, so)
162 so.addDeviceHandlerToMap(handler)
163 }
164 go handler.ReconcileDevice(device)
165
166 return nil
167}
168
169func (so *SimulatedONU) Abandon_device(device *voltha.Device) error {
170 return errors.New("UnImplemented")
171}
172
173func (so *SimulatedONU) Disable_device(device *voltha.Device) error {
174 if device == nil {
175 log.Warn("device-is-nil")
176 return errors.New("nil-device")
177 }
178 log.Infow("disable-device", log.Fields{"deviceId": device.Id})
179 var handler *DeviceHandler
180 if handler = so.getDeviceHandler(device.Id); handler != nil {
181 go handler.DisableDevice(device)
182 }
183 return nil
184}
185
186func (so *SimulatedONU) Reenable_device(device *voltha.Device) error {
187 if device == nil {
188 log.Warn("device-is-nil")
189 return errors.New("nil-device")
190 }
191 log.Infow("reenable-device", log.Fields{"deviceId": device.Id})
192 var handler *DeviceHandler
193 if handler = so.getDeviceHandler(device.Id); handler != nil {
194 go handler.ReEnableDevice(device)
195 }
196 return nil
197}
198
199func (so *SimulatedONU) Reboot_device(device *voltha.Device) error {
200 return errors.New("UnImplemented")
201}
202
203func (so *SimulatedONU) Self_test_device(device *voltha.Device) error {
204 return errors.New("UnImplemented")
205}
206
207func (so *SimulatedONU) Delete_device(device *voltha.Device) error {
208 if device == nil {
209 log.Warn("device-is-nil")
210 return errors.New("nil-device")
211 }
212 log.Infow("delete-device", log.Fields{"deviceId": device.Id})
213 var handler *DeviceHandler
214 if handler = so.getDeviceHandler(device.Id); handler != nil {
215 go handler.DeleteDevice(device)
216 }
217 return nil
218}
219
220func (so *SimulatedONU) Get_device_details(device *voltha.Device) error {
221 return errors.New("UnImplemented")
222}
223
224func (so *SimulatedONU) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, metadata *voltha.FlowMetadata) error {
225 if device == nil {
226 log.Warn("device-is-nil")
227 return errors.New("nil-device")
228 }
229 log.Debugw("bulk-flow-updates", log.Fields{"deviceId": device.Id, "flows": flows, "groups": groups})
230 var handler *DeviceHandler
231 if handler = so.getDeviceHandler(device.Id); handler != nil {
232 go handler.UpdateFlowsBulk(device, flows, groups, metadata)
233 }
234 return nil
235}
236
237func (so *SimulatedONU) Update_flows_incrementally(device *voltha.Device, flowChanges *openflow_13.FlowChanges, groupChanges *openflow_13.FlowGroupChanges, metadata *voltha.FlowMetadata) error {
238 if device == nil {
239 log.Warn("device-is-nil")
240 return errors.New("nil-device")
241 }
242 log.Debugw("incremental-flow-update", log.Fields{"deviceId": device.Id, "flowChanges": flowChanges, "groupChanges": groupChanges})
243 var handler *DeviceHandler
244 if handler = so.getDeviceHandler(device.Id); handler != nil {
245 go handler.UpdateFlowsIncremental(device, flowChanges, groupChanges, metadata)
246 }
247 return nil
248}
249
250func (so *SimulatedONU) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
251 if device == nil {
252 log.Warn("device-is-nil")
253 return errors.New("nil-device")
254 }
255 log.Debugw("update_pm_config", log.Fields{"deviceId": device.Id, "pmConfigs": pmConfigs})
256 var handler *DeviceHandler
257 if handler = so.getDeviceHandler(device.Id); handler != nil {
258 go handler.UpdatePmConfigs(device, pmConfigs)
259 }
260 return nil
261}
262
263func (so *SimulatedONU) Receive_packet_out(deviceId string, egress_port_no int, msg *openflow_13.OfpPacketOut) error {
264 return errors.New("UnImplemented")
265}
266
267func (so *SimulatedONU) Suppress_alarm(filter *voltha.AlarmFilter) error {
268 return errors.New("UnImplemented")
269}
270
271func (so *SimulatedONU) Unsuppress_alarm(filter *voltha.AlarmFilter) error {
272 return errors.New("UnImplemented")
273}
274
275func (so *SimulatedONU) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
276 return nil, errors.New("UnImplemented")
277}
278
279func (so *SimulatedONU) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
280 return nil, errors.New("UnImplemented")
281}
282
283func (so *SimulatedONU) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
284 return nil, errors.New("UnImplemented")
285}
286
287func (so *SimulatedONU) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
288 return nil, errors.New("UnImplemented")
289}
290
291func (so *SimulatedONU) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
292 return nil, errors.New("UnImplemented")
293}