blob: 5dcf108c71bbd3864cab86c90895fa283f5ec1b9 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
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
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000017//Package adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
Holger Hildebrandtfa074992020-03-27 15:42:06 +000019
20import (
21 "context"
22 "errors"
23 "fmt"
24 "sync"
25 "time"
26
27 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
mpagenkoaf801632020-07-03 10:00:42 +000028 "github.com/opencord/voltha-lib-go/v3/pkg/db/kvstore"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000029 "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
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000035 "test.internal/openadapter/internal/pkg/config"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000036)
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
mpagenkoaf801632020-07-03 10:00:42 +000045 kvClient kvstore.Client
Holger Hildebrandtfa074992020-03-27 15:42:06 +000046 config *config.AdapterFlags
47 numOnus int
48 KVStoreHost string
49 KVStorePort int
50 KVStoreType string
mpagenkoaf801632020-07-03 10:00:42 +000051 KVStoreTimeout time.Duration
Holger Hildebrandtfa074992020-03-27 15:42:06 +000052 exitChannel chan int
53 HeartbeatCheckInterval time.Duration
54 HeartbeatFailReportInterval time.Duration
55 //GrpcTimeoutInterval time.Duration
56 lockDeviceHandlersMap sync.RWMutex
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000057 pSupportedFsms *OmciDeviceFsms
Holger Hildebrandtfa074992020-03-27 15:42:06 +000058}
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,
mpagenkoaf801632020-07-03 10:00:42 +000063 eventProxy adapterif.EventProxy, kvClient kvstore.Client, cfg *config.AdapterFlags) *OpenONUAC {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000064 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
mpagenkoaf801632020-07-03 10:00:42 +000073 openOnuAc.kvClient = kvClient
Holger Hildebrandtfa074992020-03-27 15:42:06 +000074 openOnuAc.KVStoreHost = cfg.KVStoreHost
75 openOnuAc.KVStorePort = cfg.KVStorePort
76 openOnuAc.KVStoreType = cfg.KVStoreType
mpagenkoaf801632020-07-03 10:00:42 +000077 openOnuAc.KVStoreTimeout = cfg.KVStoreTimeout
Holger Hildebrandtfa074992020-03-27 15:42:06 +000078 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
79 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
80 //openOnuAc.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
81 openOnuAc.lockDeviceHandlersMap = sync.RWMutex{}
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000082
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
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000100 return &openOnuAc
101}
102
103//Start starts (logs) the adapter
104func (oo *OpenONUAC) Start(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000105 logger.Info("starting-openonu-adapter")
106 logger.Info("openonu-adapter-started")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000107 return nil
108}
109
110//Stop terminates the session
111func (oo *OpenONUAC) Stop(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000112 logger.Info("stopping-device-manager")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000113 oo.exitChannel <- 1
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000114 logger.Info("device-manager-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000115 return nil
116}
117
118func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *DeviceHandler) {
119 oo.lockDeviceHandlersMap.Lock()
120 defer oo.lockDeviceHandlersMap.Unlock()
121 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
122 oo.deviceHandlers[agent.deviceID] = agent
123 oo.deviceHandlers[agent.deviceID].Start(ctx)
124 }
125}
126
127func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *DeviceHandler) {
128 oo.lockDeviceHandlersMap.Lock()
129 defer oo.lockDeviceHandlersMap.Unlock()
130 delete(oo.deviceHandlers, agent.deviceID)
131}
132
133func (oo *OpenONUAC) getDeviceHandler(deviceID string) *DeviceHandler {
134 oo.lockDeviceHandlersMap.Lock()
135 defer oo.lockDeviceHandlersMap.Unlock()
136 if agent, ok := oo.deviceHandlers[deviceID]; ok {
137 return agent
138 }
139 return nil
140}
141
142// Adapter interface required methods ############## begin #########
143// #################################################################
144
145// for original content compare: (needs according deviceHandler methods)
146// /voltha-openolt-adapter/adaptercore/openolt.go
147
148// Adopt_device creates a new device handler if not present already and then adopts the device
149func (oo *OpenONUAC) Adopt_device(device *voltha.Device) error {
150 if device == nil {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000151 logger.Warn("voltha-device-is-nil")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000152 return errors.New("nil-device")
153 }
154 ctx := context.Background()
divyadesai4d299552020-08-18 07:13:49 +0000155 logger.Infow("adopt-device", log.Fields{"device-id": device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000156 var handler *DeviceHandler
157 if handler = oo.getDeviceHandler(device.Id); handler == nil {
158 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
159 oo.addDeviceHandlerToMap(ctx, handler)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000160 go handler.AdoptOrReconcileDevice(ctx, device)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000161 // Launch the creation of the device topic
162 // go oo.createDeviceTopic(device)
163 }
164 return nil
165}
166
167//Get_ofp_device_info returns OFP information for the given device
168func (oo *OpenONUAC) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
divyadesai4d299552020-08-18 07:13:49 +0000169 logger.Errorw("device-handler-not-set", log.Fields{"device-id": device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000170 return nil, errors.New("device-handler-not-set")
171}
172
173//Get_ofp_port_info returns OFP port information for the given device
mpagenkoaf801632020-07-03 10:00:42 +0000174//200630: method removed as per [VOL-3202]: OF port info is now to be delivered within UniPort create
175// cmp changes in onu_uni_port.go::CreateVolthaPort()
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000176
177//Process_inter_adapter_message sends messages to a target device (between adapters)
178func (oo *OpenONUAC) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000179 logger.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000180 "msgProxyDeviceId": msg.Header.ProxyDeviceId, "msgToDeviceId": msg.Header.ToDeviceId})
181
182 targetDevice := msg.Header.ToDeviceId
183 //ToDeviceId should address an DeviceHandler instance
184 if handler := oo.getDeviceHandler(targetDevice); handler != nil {
mpagenko1cc3cb42020-07-27 15:24:38 +0000185 /* 200724: modification towards synchronous implementation - possible errors within processing shall be
186 * in the accordingly delayed response, some timing effect might result in Techprofile processing for multiple UNI's
187 */
188 return handler.ProcessInterAdapterMessage(msg)
189 /* so far the processing has been in background with according commented error treatment restrictions:
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000190 go handler.ProcessInterAdapterMessage(msg)
191 // error treatment might be more sophisticated
192 // by now let's just accept the message on 'communication layer'
193 // message content problems have to be evaluated then in the handler
194 // and are by now not reported to the calling party (to force what reaction there?)
195 return nil
mpagenko1cc3cb42020-07-27 15:24:38 +0000196 */
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000197 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000198 logger.Warnw("no handler found for received Inter-Proxy-message", log.Fields{
199 "msgToDeviceId": targetDevice})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000200 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", targetDevice))
201}
202
203//Adapter_descriptor not implemented
204func (oo *OpenONUAC) Adapter_descriptor() error {
205 return errors.New("unImplemented")
206}
207
208//Device_types unimplemented
209func (oo *OpenONUAC) Device_types() (*voltha.DeviceTypes, error) {
210 return nil, errors.New("unImplemented")
211}
212
213//Health returns unimplemented
214func (oo *OpenONUAC) Health() (*voltha.HealthStatus, error) {
215 return nil, errors.New("unImplemented")
216}
217
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000218//Reconcile_device is called once when the adapter needs to re-create device - usually on core restart
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000219func (oo *OpenONUAC) Reconcile_device(device *voltha.Device) error {
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000220 if device == nil {
221 logger.Warn("voltha-device-is-nil")
222 return errors.New("nil-device")
223 }
224 ctx := context.Background()
225 logger.Infow("Reconcile_device", log.Fields{"device-id": device.Id})
226 var handler *DeviceHandler
227 if handler = oo.getDeviceHandler(device.Id); handler == nil {
228 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
229 oo.addDeviceHandlerToMap(ctx, handler)
230 handler.device = device
231 handler.reconciling = true
232 go handler.AdoptOrReconcileDevice(ctx, handler.device)
233 // reconcilement will be continued after onu-device entry is added
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000234 } else {
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000235 return fmt.Errorf(fmt.Sprintf("device-already-reconciled-or-active-%s", device.Id))
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000236 }
237 return nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000238}
239
240//Abandon_device unimplemented
241func (oo *OpenONUAC) Abandon_device(device *voltha.Device) error {
242 return errors.New("unImplemented")
243}
244
245//Disable_device disables the given device
246func (oo *OpenONUAC) Disable_device(device *voltha.Device) error {
divyadesai4d299552020-08-18 07:13:49 +0000247 logger.Debugw("Disable_device", log.Fields{"device-id": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300248 if handler := oo.getDeviceHandler(device.Id); handler != nil {
249 go handler.DisableDevice(device)
250 return nil
251 }
divyadesai4d299552020-08-18 07:13:49 +0000252 logger.Warnw("no handler found for device-disable", log.Fields{"device-id": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300253 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000254}
255
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000256//Reenable_device enables the onu device after disable
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000257func (oo *OpenONUAC) Reenable_device(device *voltha.Device) error {
divyadesai4d299552020-08-18 07:13:49 +0000258 logger.Debugw("Reenable_device", log.Fields{"device-id": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300259 if handler := oo.getDeviceHandler(device.Id); handler != nil {
260 go handler.ReenableDevice(device)
261 return nil
262 }
divyadesai4d299552020-08-18 07:13:49 +0000263 logger.Warnw("no handler found for device-reenable", log.Fields{"device-id": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300264 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000265}
266
267//Reboot_device reboots the given device
268func (oo *OpenONUAC) Reboot_device(device *voltha.Device) error {
divyadesai4d299552020-08-18 07:13:49 +0000269 logger.Debugw("Reboot-device", log.Fields{"device-id": device.Id})
ozgecanetsiae11479f2020-07-06 09:44:47 +0300270 if handler := oo.getDeviceHandler(device.Id); handler != nil {
271 go handler.RebootDevice(device)
272 return nil
273 }
divyadesai4d299552020-08-18 07:13:49 +0000274 logger.Warnw("no handler found for device-reboot", log.Fields{"device-id": device.Id})
ozgecanetsiae11479f2020-07-06 09:44:47 +0300275 return fmt.Errorf(fmt.Sprintf("handler-not-found-#{device.Id}"))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000276}
277
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000278//Self_test_device unimplemented
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000279func (oo *OpenONUAC) Self_test_device(device *voltha.Device) error {
280 return errors.New("unImplemented")
281}
282
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000283func (oo *OpenONUAC) Delete_device(device *voltha.Device) error {
divyadesai4d299552020-08-18 07:13:49 +0000284 logger.Debugw("Delete_device", log.Fields{"device-id": device.Id})
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000285 if handler := oo.getDeviceHandler(device.Id); handler != nil {
286 if err := handler.DeleteDevice(device); err != nil {
287 return err
288 }
289 } else {
divyadesai4d299552020-08-18 07:13:49 +0000290 logger.Warnw("no handler found for device-reconcilement", log.Fields{"device-id": device.Id})
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000291 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
292 }
293 return nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000294}
295
296//Get_device_details unimplemented
297func (oo *OpenONUAC) Get_device_details(device *voltha.Device) error {
298 return errors.New("unImplemented")
299}
300
301//Update_flows_bulk returns
302func (oo *OpenONUAC) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error {
303 return errors.New("unImplemented")
304}
305
306//Update_flows_incrementally updates (add/remove) the flows on a given device
307func (oo *OpenONUAC) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
mpagenko3dbcdd22020-07-22 07:38:45 +0000308 // return errors.New("unImplemented")
309 // testwise: just acknowledge to see, if that avoids ongoing TechProfile config sequences ...
310 return nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000311}
312
313//Update_pm_config returns PmConfigs nil or error
314func (oo *OpenONUAC) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
315 return errors.New("unImplemented")
316}
317
318//Receive_packet_out sends packet out to the device
319func (oo *OpenONUAC) Receive_packet_out(deviceID string, egressPortNo int, packet *openflow_13.OfpPacketOut) error {
320 return errors.New("unImplemented")
321}
322
323//Suppress_event unimplemented
324func (oo *OpenONUAC) Suppress_event(filter *voltha.EventFilter) error {
325 return errors.New("unImplemented")
326}
327
328//Unsuppress_event unimplemented
329func (oo *OpenONUAC) Unsuppress_event(filter *voltha.EventFilter) error {
330 return errors.New("unImplemented")
331}
332
333//Download_image unimplemented
334func (oo *OpenONUAC) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
335 return nil, errors.New("unImplemented")
336}
337
338//Get_image_download_status unimplemented
339func (oo *OpenONUAC) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
340 return nil, errors.New("unImplemented")
341}
342
343//Cancel_image_download unimplemented
344func (oo *OpenONUAC) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
345 return nil, errors.New("unImplemented")
346}
347
348//Activate_image_update unimplemented
349func (oo *OpenONUAC) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
350 return nil, errors.New("unImplemented")
351}
352
353//Revert_image_update unimplemented
354func (oo *OpenONUAC) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
355 return nil, errors.New("unImplemented")
356}
357
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000358// Enable_port to Enable PON/NNI interface - seems not to be used/required according to python code
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000359func (oo *OpenONUAC) Enable_port(deviceID string, port *voltha.Port) error {
360 return errors.New("unImplemented")
361}
362
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000363// Disable_port to Disable pon/nni interface - seems not to be used/required according to python code
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000364func (oo *OpenONUAC) Disable_port(deviceID string, port *voltha.Port) error {
365 return errors.New("unImplemented")
366}
367
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000368//needed for if update >= 3.1.x
Matteo Scandolo2e6f1e32020-04-15 11:28:45 -0700369func (oo *OpenONUAC) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error {
370 return errors.New("unImplemented")
371}
372
373func (oo *OpenONUAC) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
374 return nil, errors.New("unImplemented")
375}
376
Matteo Scandolod132c0e2020-04-24 17:06:25 -0700377func (oo *OpenONUAC) Get_ext_value(deviceID string, device *voltha.Device, valueparam voltha.ValueType_Type) (*voltha.ReturnValues, error) {
378 return nil, errors.New("unImplemented")
379}
380
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000381// Adapter interface required methods ################ end #########
382// #################################################################