blob: 253f901daf0176a26e8921a7d6ba4c0fef876447 [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"
28 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
31 "github.com/opencord/voltha-protos/v3/go/openflow_13"
32 "github.com/opencord/voltha-protos/v3/go/voltha"
33
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000034 "test.internal/openadapter/internal/pkg/config"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000035)
36
37//OpenONUAC structure holds the ONU core information
38type OpenONUAC struct {
39 deviceHandlers map[string]*DeviceHandler
40 coreProxy adapterif.CoreProxy
41 adapterProxy adapterif.AdapterProxy
42 eventProxy adapterif.EventProxy
43 kafkaICProxy kafka.InterContainerProxy
44 config *config.AdapterFlags
45 numOnus int
46 KVStoreHost string
47 KVStorePort int
48 KVStoreType string
49 exitChannel chan int
50 HeartbeatCheckInterval time.Duration
51 HeartbeatFailReportInterval time.Duration
52 //GrpcTimeoutInterval time.Duration
53 lockDeviceHandlersMap sync.RWMutex
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000054 pSupportedFsms *OmciDeviceFsms
Holger Hildebrandtfa074992020-03-27 15:42:06 +000055}
56
57//NewOpenONUAC returns a new instance of OpenONU_AC
58func NewOpenONUAC(ctx context.Context, kafkaICProxy kafka.InterContainerProxy,
59 coreProxy adapterif.CoreProxy, adapterProxy adapterif.AdapterProxy,
60 eventProxy adapterif.EventProxy, cfg *config.AdapterFlags) *OpenONUAC {
61 var openOnuAc OpenONUAC
62 openOnuAc.exitChannel = make(chan int, 1)
63 openOnuAc.deviceHandlers = make(map[string]*DeviceHandler)
64 openOnuAc.kafkaICProxy = kafkaICProxy
65 openOnuAc.config = cfg
66 openOnuAc.numOnus = cfg.OnuNumber
67 openOnuAc.coreProxy = coreProxy
68 openOnuAc.adapterProxy = adapterProxy
69 openOnuAc.eventProxy = eventProxy
70 openOnuAc.KVStoreHost = cfg.KVStoreHost
71 openOnuAc.KVStorePort = cfg.KVStorePort
72 openOnuAc.KVStoreType = cfg.KVStoreType
73 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
74 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
75 //openOnuAc.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
76 openOnuAc.lockDeviceHandlersMap = sync.RWMutex{}
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000077
78 openOnuAc.pSupportedFsms = &OmciDeviceFsms{
79 "mib-synchronizer": {
80 //mibSyncFsm, // Implements the MIB synchronization state machine
81 MibDbVolatileDictImpl, // Implements volatile ME MIB database
82 true, // Advertise events on OpenOMCI event bus
83 cMibAuditDelayImpl, // Time to wait between MIB audits. 0 to disable audits.
84 // map[string]func() error{
85 // "mib-upload": onuDeviceEntry.MibUploadTask,
86 // "mib-template": onuDeviceEntry.MibTemplateTask,
87 // "get-mds": onuDeviceEntry.GetMdsTask,
88 // "mib-audit": onuDeviceEntry.GetMdsTask,
89 // "mib-resync": onuDeviceEntry.MibResyncTask,
90 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
91 // },
92 },
93 }
94
Holger Hildebrandtfa074992020-03-27 15:42:06 +000095 return &openOnuAc
96}
97
98//Start starts (logs) the adapter
99func (oo *OpenONUAC) Start(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000100 logger.Info("starting-openonu-adapter")
101 logger.Info("openonu-adapter-started")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000102 return nil
103}
104
105//Stop terminates the session
106func (oo *OpenONUAC) Stop(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000107 logger.Info("stopping-device-manager")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000108 oo.exitChannel <- 1
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000109 logger.Info("device-manager-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000110 return nil
111}
112
113func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *DeviceHandler) {
114 oo.lockDeviceHandlersMap.Lock()
115 defer oo.lockDeviceHandlersMap.Unlock()
116 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
117 oo.deviceHandlers[agent.deviceID] = agent
118 oo.deviceHandlers[agent.deviceID].Start(ctx)
119 }
120}
121
122func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *DeviceHandler) {
123 oo.lockDeviceHandlersMap.Lock()
124 defer oo.lockDeviceHandlersMap.Unlock()
125 delete(oo.deviceHandlers, agent.deviceID)
126}
127
128func (oo *OpenONUAC) getDeviceHandler(deviceID string) *DeviceHandler {
129 oo.lockDeviceHandlersMap.Lock()
130 defer oo.lockDeviceHandlersMap.Unlock()
131 if agent, ok := oo.deviceHandlers[deviceID]; ok {
132 return agent
133 }
134 return nil
135}
136
137// Adapter interface required methods ############## begin #########
138// #################################################################
139
140// for original content compare: (needs according deviceHandler methods)
141// /voltha-openolt-adapter/adaptercore/openolt.go
142
143// Adopt_device creates a new device handler if not present already and then adopts the device
144func (oo *OpenONUAC) Adopt_device(device *voltha.Device) error {
145 if device == nil {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000146 logger.Warn("voltha-device-is-nil")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000147 return errors.New("nil-device")
148 }
149 ctx := context.Background()
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000150 logger.Infow("adopt-device", log.Fields{"deviceId": device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000151 var handler *DeviceHandler
152 if handler = oo.getDeviceHandler(device.Id); handler == nil {
153 handler := NewDeviceHandler(oo.coreProxy, oo.adapterProxy, oo.eventProxy, device, oo)
154 oo.addDeviceHandlerToMap(ctx, handler)
155 go handler.AdoptDevice(ctx, device)
156 // Launch the creation of the device topic
157 // go oo.createDeviceTopic(device)
158 }
159 return nil
160}
161
162//Get_ofp_device_info returns OFP information for the given device
163func (oo *OpenONUAC) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000164 logger.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000165 return nil, errors.New("device-handler-not-set")
166}
167
168//Get_ofp_port_info returns OFP port information for the given device
169func (oo *OpenONUAC) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) {
Holger Hildebrandt24d51952020-05-04 14:03:42 +0000170 //this method expects a return value to be sent to the core
171 // and internal processing should not take that long
172 // so it makes no sense to try to work asynchronously here
173 logger.Infow("get-ofp-port-info started", log.Fields{"deviceId": device.Id, "portNo": portNo})
174 // basically the same code as in openOlt.go - unify???
175 if handler := oo.getDeviceHandler(device.Id); handler != nil {
176 return handler.GetOfpPortInfo(device, portNo)
177 // error treatment might be more sophisticated, but indeed it would be logged within handler
178 }
179 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found for deviceId %s", device.Id))
180 //return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000181}
182
183//Process_inter_adapter_message sends messages to a target device (between adapters)
184func (oo *OpenONUAC) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000185 logger.Debugw("Process_inter_adapter_message", log.Fields{"msgId": msg.Header.Id,
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000186 "msgProxyDeviceId": msg.Header.ProxyDeviceId, "msgToDeviceId": msg.Header.ToDeviceId})
187
188 targetDevice := msg.Header.ToDeviceId
189 //ToDeviceId should address an DeviceHandler instance
190 if handler := oo.getDeviceHandler(targetDevice); handler != nil {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000191 go handler.ProcessInterAdapterMessage(msg)
192 // error treatment might be more sophisticated
193 // by now let's just accept the message on 'communication layer'
194 // message content problems have to be evaluated then in the handler
195 // and are by now not reported to the calling party (to force what reaction there?)
196 return nil
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
218//Reconcile_device unimplemented
219func (oo *OpenONUAC) Reconcile_device(device *voltha.Device) error {
220 return errors.New("unImplemented")
221}
222
223//Abandon_device unimplemented
224func (oo *OpenONUAC) Abandon_device(device *voltha.Device) error {
225 return errors.New("unImplemented")
226}
227
228//Disable_device disables the given device
229func (oo *OpenONUAC) Disable_device(device *voltha.Device) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000230 logger.Debugw("Disable_device", log.Fields{"deviceId": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300231 if handler := oo.getDeviceHandler(device.Id); handler != nil {
232 go handler.DisableDevice(device)
233 return nil
234 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000235 logger.Warnw("no handler found for device-disable", log.Fields{"deviceId": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300236 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000237}
238
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000239//Reenable_device enables the onu device after disable
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000240func (oo *OpenONUAC) Reenable_device(device *voltha.Device) error {
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000241 logger.Debugw("Reenable_device", log.Fields{"deviceId": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300242 if handler := oo.getDeviceHandler(device.Id); handler != nil {
243 go handler.ReenableDevice(device)
244 return nil
245 }
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000246 logger.Warnw("no handler found for device-reenable", log.Fields{"deviceId": device.Id})
ozgecanetsiafce57b12020-05-25 14:39:35 +0300247 return fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000248}
249
250//Reboot_device reboots the given device
251func (oo *OpenONUAC) Reboot_device(device *voltha.Device) error {
252 return errors.New("unImplemented")
253}
254
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000255//Self_test_device unimplemented
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000256func (oo *OpenONUAC) Self_test_device(device *voltha.Device) error {
257 return errors.New("unImplemented")
258}
259
260//Delete_device unimplemented
261func (oo *OpenONUAC) Delete_device(device *voltha.Device) error {
262 return errors.New("unImplemented")
263}
264
265//Get_device_details unimplemented
266func (oo *OpenONUAC) Get_device_details(device *voltha.Device) error {
267 return errors.New("unImplemented")
268}
269
270//Update_flows_bulk returns
271func (oo *OpenONUAC) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error {
272 return errors.New("unImplemented")
273}
274
275//Update_flows_incrementally updates (add/remove) the flows on a given device
276func (oo *OpenONUAC) Update_flows_incrementally(device *voltha.Device, flows *openflow_13.FlowChanges, groups *openflow_13.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
277 return errors.New("unImplemented")
278}
279
280//Update_pm_config returns PmConfigs nil or error
281func (oo *OpenONUAC) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error {
282 return errors.New("unImplemented")
283}
284
285//Receive_packet_out sends packet out to the device
286func (oo *OpenONUAC) Receive_packet_out(deviceID string, egressPortNo int, packet *openflow_13.OfpPacketOut) error {
287 return errors.New("unImplemented")
288}
289
290//Suppress_event unimplemented
291func (oo *OpenONUAC) Suppress_event(filter *voltha.EventFilter) error {
292 return errors.New("unImplemented")
293}
294
295//Unsuppress_event unimplemented
296func (oo *OpenONUAC) Unsuppress_event(filter *voltha.EventFilter) error {
297 return errors.New("unImplemented")
298}
299
300//Download_image unimplemented
301func (oo *OpenONUAC) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
302 return nil, errors.New("unImplemented")
303}
304
305//Get_image_download_status unimplemented
306func (oo *OpenONUAC) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
307 return nil, errors.New("unImplemented")
308}
309
310//Cancel_image_download unimplemented
311func (oo *OpenONUAC) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
312 return nil, errors.New("unImplemented")
313}
314
315//Activate_image_update unimplemented
316func (oo *OpenONUAC) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
317 return nil, errors.New("unImplemented")
318}
319
320//Revert_image_update unimplemented
321func (oo *OpenONUAC) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) {
322 return nil, errors.New("unImplemented")
323}
324
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000325// Enable_port to Enable PON/NNI interface - seems not to be used/required according to python code
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000326func (oo *OpenONUAC) Enable_port(deviceID string, port *voltha.Port) error {
327 return errors.New("unImplemented")
328}
329
Holger Hildebrandtccd390c2020-05-29 13:49:04 +0000330// Disable_port to Disable pon/nni interface - seems not to be used/required according to python code
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000331func (oo *OpenONUAC) Disable_port(deviceID string, port *voltha.Port) error {
332 return errors.New("unImplemented")
333}
334
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000335//needed for if update >= 3.1.x
Matteo Scandolo2e6f1e32020-04-15 11:28:45 -0700336func (oo *OpenONUAC) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error {
337 return errors.New("unImplemented")
338}
339
340func (oo *OpenONUAC) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) {
341 return nil, errors.New("unImplemented")
342}
343
Matteo Scandolod132c0e2020-04-24 17:06:25 -0700344func (oo *OpenONUAC) Get_ext_value(deviceID string, device *voltha.Device, valueparam voltha.ValueType_Type) (*voltha.ReturnValues, error) {
345 return nil, errors.New("unImplemented")
346}
347
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000348// Adapter interface required methods ################ end #########
349// #################################################################