blob: a8a84d47e06567cba733db31f98acb3ee0457939 [file] [log] [blame]
Takahiro Suzuki241c10e2020-12-17 20:17:57 +09001/*
2 * Copyright 2020-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 */
16
17//Package adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
19
20import (
21 "context"
22 "errors"
23 "fmt"
24 "sync"
25 "time"
26
27 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
28 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
29 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
30 "github.com/opencord/voltha-lib-go/v3/pkg/log"
31 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
32 "github.com/opencord/voltha-protos/v3/go/openflow_13"
33 "github.com/opencord/voltha-protos/v3/go/voltha"
34
35 "test.internal/openadapter/internal/pkg/config"
36)
37
38//OpenONUAC structure holds the ONU core information
39type OpenONUAC struct {
40 deviceHandlers map[string]*deviceHandler
41 coreProxy adapterif.CoreProxy
42 adapterProxy adapterif.AdapterProxy
43 eventProxy adapterif.EventProxy
44 kafkaICProxy kafka.InterContainerProxy
45 kvClient kvstore.Client
46 config *config.AdapterFlags
47 numOnus int
48 KVStoreHost string
49 KVStorePort int
50 KVStoreType string
51 KVStoreTimeout time.Duration
52 exitChannel chan int
53 HeartbeatCheckInterval time.Duration
54 HeartbeatFailReportInterval time.Duration
55 AcceptIncrementalEvto bool
56 lockDeviceHandlersMap sync.RWMutex
57 pSupportedFsms *OmciDeviceFsms
58}
59
60//NewOpenONUAC returns a new instance of OpenONU_AC
61func NewOpenONUAC(ctx context.Context, kafkaICProxy kafka.InterContainerProxy,
62 coreProxy adapterif.CoreProxy, adapterProxy adapterif.AdapterProxy,
63 eventProxy adapterif.EventProxy, kvClient kvstore.Client, cfg *config.AdapterFlags) *OpenONUAC {
64 var openOnuAc OpenONUAC
65 openOnuAc.exitChannel = make(chan int, 1)
66 openOnuAc.deviceHandlers = make(map[string]*deviceHandler)
67 openOnuAc.kafkaICProxy = kafkaICProxy
68 openOnuAc.config = cfg
69 openOnuAc.numOnus = cfg.OnuNumber
70 openOnuAc.coreProxy = coreProxy
71 openOnuAc.adapterProxy = adapterProxy
72 openOnuAc.eventProxy = eventProxy
73 openOnuAc.kvClient = kvClient
74 openOnuAc.KVStoreHost = cfg.KVStoreHost
75 openOnuAc.KVStorePort = cfg.KVStorePort
76 openOnuAc.KVStoreType = cfg.KVStoreType
77 openOnuAc.KVStoreTimeout = cfg.KVStoreTimeout
78 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
79 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
80 openOnuAc.AcceptIncrementalEvto = cfg.AccIncrEvto
81 openOnuAc.lockDeviceHandlersMap = sync.RWMutex{}
82
83 openOnuAc.pSupportedFsms = &OmciDeviceFsms{
84 "mib-synchronizer": {
85 //mibSyncFsm, // Implements the MIB synchronization state machine
86 mibDbVolatileDictImpl, // Implements volatile ME MIB database
87 //true, // Advertise events on OpenOMCI event bus
88 cMibAuditDelayImpl, // Time to wait between MIB audits. 0 to disable audits.
89 // map[string]func() error{
90 // "mib-upload": onuDeviceEntry.MibUploadTask,
91 // "mib-template": onuDeviceEntry.MibTemplateTask,
92 // "get-mds": onuDeviceEntry.GetMdsTask,
93 // "mib-audit": onuDeviceEntry.GetMdsTask,
94 // "mib-resync": onuDeviceEntry.MibResyncTask,
95 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
96 // },
97 },
98 }
99
100 return &openOnuAc
101}
102
103//Start starts (logs) the adapter
104func (oo *OpenONUAC) Start(ctx context.Context) error {
105 logger.Info("starting-openonu-adapter")
106 logger.Info("openonu-adapter-started")
107 return nil
108}
109
110/*
111//stop terminates the session
112func (oo *OpenONUAC) stop(ctx context.Context) error {
113 logger.Info("stopping-device-manager")
114 oo.exitChannel <- 1
115 logger.Info("device-manager-stopped")
116 return nil
117}
118*/
119
120func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *deviceHandler) {
121 oo.lockDeviceHandlersMap.Lock()
122 defer oo.lockDeviceHandlersMap.Unlock()
123 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
124 oo.deviceHandlers[agent.deviceID] = agent
125 oo.deviceHandlers[agent.deviceID].start(ctx)
126 }
127}
128
129/*
130func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *deviceHandler) {
131 oo.lockDeviceHandlersMap.Lock()
132 defer oo.lockDeviceHandlersMap.Unlock()
133 delete(oo.deviceHandlers, agent.deviceID)
134}
135*/
136
137func (oo *OpenONUAC) getDeviceHandler(deviceID string) *deviceHandler {
138 oo.lockDeviceHandlersMap.Lock()
139 defer oo.lockDeviceHandlersMap.Unlock()
140 if agent, ok := oo.deviceHandlers[deviceID]; ok {
141 return agent
142 }
143 return nil
144}
145
146// Adapter interface required methods ############## begin #########
147// #################################################################
148
149// for original content compare: (needs according deviceHandler methods)
150// /voltha-openolt-adapter/adaptercore/openolt.go
151
152// Adopt_device creates a new device handler if not present already and then adopts the device
153func (oo *OpenONUAC) Adopt_device(device *voltha.Device) error {
154 if device == nil {
155 logger.Warn("voltha-device-is-nil")
156 return errors.New("nil-device")
157 }
158 ctx := context.Background()
159 logger.Infow("adopt-device", log.Fields{"device-id": device.Id})
160 var handler *deviceHandler
161 if handler = oo.getDeviceHandler(device.Id); handler == nil {
162 handler := newDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
163 oo.addDeviceHandlerToMap(ctx, handler)
164 go handler.adoptOrReconcileDevice(ctx, device)
165 // Launch the creation of the device topic
166 // go oo.createDeviceTopic(device)
167 }
168 return nil
169}
170
171//Get_ofp_device_info returns OFP information for the given device
172func (oo *OpenONUAC) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
173 logger.Errorw("device-handler-not-set", log.Fields{"device-id": device.Id})
174 return nil, errors.New("device-handler-not-set")
175}
176
177//Get_ofp_port_info returns OFP port information for the given device
178//200630: method removed as per [VOL-3202]: OF port info is now to be delivered within UniPort create
179// cmp changes in onu_uni_port.go::CreateVolthaPort()
180
181//Process_inter_adapter_message sends messages to a target device (between adapters)
182func (oo *OpenONUAC) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
183 logger.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id,
184 "msgProxyDeviceId": msg.Header.ProxyDeviceId, "msgToDeviceId": msg.Header.ToDeviceId})
185
186 targetDevice := msg.Header.ToDeviceId
187 if handler := oo.getDeviceHandler(targetDevice); handler != nil {
188 /* 200724: modification towards synchronous implementation - possible errors within processing shall be
189 * in the accordingly delayed response, some timing effect might result in Techprofile processing for multiple UNI's
190 */
191 return handler.processInterAdapterMessage(msg)
192 /* so far the processing has been in background with according commented error treatment restrictions:
193 go handler.ProcessInterAdapterMessage(msg)
194 // error treatment might be more sophisticated
195 // by now let's just accept the message on 'communication layer'
196 // message content problems have to be evaluated then in the handler
197 // and are by now not reported to the calling party (to force what reaction there?)
198 return nil
199 */
200 }
201 logger.Warnw("no handler found for received Inter-Proxy-message", log.Fields{
202 "msgToDeviceId": targetDevice})
203 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", targetDevice))
204}
205
206//Adapter_descriptor not implemented
207func (oo *OpenONUAC) Adapter_descriptor() error {
208 return errors.New("unImplemented")
209}
210
211//Device_types unimplemented
212func (oo *OpenONUAC) Device_types() (*voltha.DeviceTypes, error) {
213 return nil, errors.New("unImplemented")
214}
215
216//Health returns unimplemented
217func (oo *OpenONUAC) Health() (*voltha.HealthStatus, error) {
218 return nil, errors.New("unImplemented")
219}
220
221//Reconcile_device is called once when the adapter needs to re-create device - usually on core restart
222func (oo *OpenONUAC) Reconcile_device(device *voltha.Device) error {
223 if device == nil {
224 logger.Warn("voltha-device-is-nil")
225 return errors.New("nil-device")
226 }
227 ctx := context.Background()
228 logger.Infow("Reconcile_device", log.Fields{"device-id": device.Id})
229 var handler *deviceHandler
230 if handler = oo.getDeviceHandler(device.Id); handler == nil {
231 handler := newDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
232 oo.addDeviceHandlerToMap(ctx, handler)
233 handler.device = device
234 handler.reconciling = true
235 go handler.adoptOrReconcileDevice(ctx, handler.device)
236 // reconcilement will be continued after onu-device entry is added
237 } else {
238 return fmt.Errorf(fmt.Sprintf("device-already-reconciled-or-active-%s", device.Id))
239 }
240 return nil
241}
242
243//Abandon_device unimplemented
244func (oo *OpenONUAC) Abandon_device(device *voltha.Device) error {
245 return errors.New("unImplemented")
246}
247
248var disableOnuFunc bool = true
249
250//Disable_device disables the given device
251func (oo *OpenONUAC) Disable_device(device *voltha.Device) error {
252 logger.Debugw("Disable_device", log.Fields{"device-id": device.Id})
253 if handler := oo.getDeviceHandler(device.Id); handler != nil {
254 if err := handler.sendMsgToOlt(context.TODO(), "disable", nil); err != nil {
255 logger.Debugw("Disable_device", log.Fields{"error": err})
256 }
257 if disableOnuFunc {
258 return nil
259 }
260 //go handler.disableDevice(device)
261 //return nil
262 }
263 logger.Warnw("no handler found for device-disable", log.Fields{"device-id": device.Id})
264 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
265}
266
267//Reenable_device enables the onu device after disable
268func (oo *OpenONUAC) Reenable_device(device *voltha.Device) error {
269 logger.Debugw("Reenable_device", log.Fields{"device-id": device.Id})
270 if handler := oo.getDeviceHandler(device.Id); handler != nil {
271 if err := handler.sendMsgToOlt(context.TODO(), "reenable", nil); err != nil {
272 logger.Debugw("Disable_device", log.Fields{"error": err})
273 }
274 if disableOnuFunc {
275 return nil
276 }
277 go handler.reEnableDevice(device)
278 return nil
279 }
280 logger.Warnw("no handler found for device-reenable", log.Fields{"device-id": device.Id})
281 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
282}
283
284//Reboot_device reboots the given device
285func (oo *OpenONUAC) Reboot_device(device *voltha.Device) error {
286 logger.Debugw("Reboot-device", log.Fields{"device-id": device.Id})
287 if handler := oo.getDeviceHandler(device.Id); handler != nil {
288 go handler.rebootDevice(device)
289 return nil
290 }
291 logger.Warnw("no handler found for device-reboot", log.Fields{"device-id": device.Id})
292 return fmt.Errorf(fmt.Sprintf("handler-not-found-#{device.Id}"))
293}
294
295//Self_test_device unimplemented
296func (oo *OpenONUAC) Self_test_device(device *voltha.Device) error {
297 return errors.New("unImplemented")
298}
299
300// Delete_device deletes the given device
301func (oo *OpenONUAC) Delete_device(device *voltha.Device) error {
302 logger.Debugw("Delete_device", log.Fields{"device-id": device.Id})
303 if handler := oo.getDeviceHandler(device.Id); handler != nil {
304 if err := handler.deleteDevice(device); err != nil {
305 return err
306 }
307 } else {
308 logger.Warnw("no handler found for device-reconcilement", log.Fields{"device-id": device.Id})
309 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
310 }
311 return nil
312}
313
314//Get_device_details unimplemented
315func (oo *OpenONUAC) Get_device_details(device *voltha.Device) error {
316 return errors.New("unImplemented")
317}
318
319//Update_flows_bulk returns
320func (oo *OpenONUAC) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error {
321 return errors.New("unImplemented")
322}
323
324var disableUpdateFlows = true
325
326//Update_flows_incrementally updates (add/remove) the flows on a given device
327func (oo *OpenONUAC) Update_flows_incrementally(device *voltha.Device,
328 flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
329 if disableUpdateFlows {
330 return nil
331 }
332 if device.ConnectStatus != voltha.ConnectStatus_REACHABLE ||
333 device.AdminState != voltha.AdminState_ENABLED {
334 logger.Warnw("device disabled or offline - skipping flow-update", log.Fields{"deviceId": device.Id})
335 return errors.New("non-matching device state")
336 }
337
338 if groups.ToAdd != nil && groups.ToAdd.Items != nil {
339 logger.Debugw("Update-flow-incr: group add not supported (ignored)", log.Fields{"deviceId": device.Id})
340 }
341 if groups.ToRemove != nil && groups.ToRemove.Items != nil {
342 logger.Debugw("Update-flow-incr: group remove not supported (ignored)", log.Fields{"deviceId": device.Id})
343 }
344 if groups.ToUpdate != nil && groups.ToUpdate.Items != nil {
345 logger.Debugw("Update-flow-incr: group update not supported (ignored)", log.Fields{"deviceId": device.Id})
346 }
347
348 if handler := oo.getDeviceHandler(device.Id); handler != nil {
349 err := handler.FlowUpdateIncremental(flows, groups, flowMetadata)
350 return err
351 }
352 logger.Warnw("no handler found for incremental flow update", log.Fields{"deviceId": device.Id})
353 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
354}
355
356//Update_pm_config returns PmConfigs nil or error
357func (oo *OpenONUAC) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
358 return errors.New("unImplemented")
359}
360
361//Receive_packet_out sends packet out to the device
362func (oo *OpenONUAC) Receive_packet_out(deviceID string, egressPortNo int, packet *openflow_13.OfpPacketOut) error {
363 return errors.New("unImplemented")
364}
365
366//Suppress_event unimplemented
367func (oo *OpenONUAC) Suppress_event(filter *voltha.EventFilter) error {
368 return errors.New("unImplemented")
369}
370
371//Unsuppress_event unimplemented
372func (oo *OpenONUAC) Unsuppress_event(filter *voltha.EventFilter) error {
373 return errors.New("unImplemented")
374}
375
376//Download_image unimplemented
377func (oo *OpenONUAC) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
378 return nil, errors.New("unImplemented")
379}
380
381//Get_image_download_status unimplemented
382func (oo *OpenONUAC) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
383 return nil, errors.New("unImplemented")
384}
385
386//Cancel_image_download unimplemented
387func (oo *OpenONUAC) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
388 return nil, errors.New("unImplemented")
389}
390
391//Activate_image_update unimplemented
392func (oo *OpenONUAC) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
393 return nil, errors.New("unImplemented")
394}
395
396//Revert_image_update unimplemented
397func (oo *OpenONUAC) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
398 return nil, errors.New("unImplemented")
399}
400
401// Enable_port to Enable PON/NNI interface - seems not to be used/required according to python code
402func (oo *OpenONUAC) Enable_port(deviceID string, port *voltha.Port) error {
403 return errors.New("unImplemented")
404}
405
406// Disable_port to Disable pon/nni interface - seems not to be used/required according to python code
407func (oo *OpenONUAC) Disable_port(deviceID string, port *voltha.Port) error {
408 return errors.New("unImplemented")
409}
410
411//Child_device_lost - unimplemented
412//needed for if update >= 3.1.x
413func (oo *OpenONUAC) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error {
414 return errors.New("unImplemented")
415}
416
417// Start_omci_test unimplemented
418func (oo *OpenONUAC) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
419 return nil, errors.New("unImplemented")
420}
421
422// Get_ext_value - unimplemented
423func (oo *OpenONUAC) Get_ext_value(deviceID string, device *voltha.Device, valueparam voltha.ValueType_Type) (*voltha.ReturnValues, error) {
424 return nil, errors.New("unImplemented")
425}
426
427// Adapter interface required methods ################ end #########
428// #################################################################