blob: 25c7c2066ec1bba17302f8d4f763615a39818b01 [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 //"sync"
24 //"time"
25
26 "github.com/looplab/fsm"
27 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
28
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
36type OnuDeviceEvent int
37
38const (
39 // Events of interest to Device Adapters and OpenOMCI State Machines
40 DeviceStatusInit OnuDeviceEvent = 0 // OnuDeviceEntry default start state
41 MibDatabaseSync OnuDeviceEvent = 1 // MIB database sync (upload done)
42 OmciCapabilitiesDone OnuDeviceEvent = 2 // OMCI ME and message type capabilities known
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000043 MibDownloadDone OnuDeviceEvent = 3 // MIB database sync (upload done)
44 PortLinkUp OnuDeviceEvent = 4 // Port link state change
45 PortLinkDw OnuDeviceEvent = 5 // Port link state change
Holger Hildebrandtfa074992020-03-27 15:42:06 +000046 // Add other events here as needed (alarms separate???)
47)
48
49type activityDescr struct {
50 databaseClass func() error
51 advertiseEvents bool
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000052 auditDelay uint16
Holger Hildebrandtfa074992020-03-27 15:42:06 +000053 //tasks map[string]func() error
54}
55type OmciDeviceFsms map[string]activityDescr
56
57//OntDeviceEntry structure holds information about the attached FSM'as and their communication
58type OnuDeviceEntry struct {
59 deviceID string
60 baseDeviceHandler *DeviceHandler
61 coreProxy adapterif.CoreProxy
62 adapterProxy adapterif.AdapterProxy
63 started bool
64 PDevOmciCC *OmciCC
Holger Hildebrandt24d51952020-05-04 14:03:42 +000065 pOnuDB *OnuDeviceDB
Holger Hildebrandtfa074992020-03-27 15:42:06 +000066 //lockDeviceEntries sync.RWMutex
67 mibDbClass func() error
68 supportedFsms OmciDeviceFsms
69 MibSyncFsm *fsm.FSM
70 MibSyncChan chan Message
71 devState OnuDeviceEvent
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000072 mibAuditDelay uint16
Holger Hildebrandtfa074992020-03-27 15:42:06 +000073}
74
75//OnuDeviceEntry returns a new instance of a OnuDeviceEntry
76//mib_db (as well as not inluded alarm_db not really used in this code? VERIFY!!)
77func NewOnuDeviceEntry(ctx context.Context,
78 device_id string, device_Handler *DeviceHandler,
79 core_proxy adapterif.CoreProxy, adapter_proxy adapterif.AdapterProxy,
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000080 supported_Fsms_Ptr *OmciDeviceFsms) *OnuDeviceEntry {
81 logger.Infow("init-onuDeviceEntry", log.Fields{"deviceId": device_id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +000082 var onuDeviceEntry OnuDeviceEntry
83 onuDeviceEntry.started = false
84 onuDeviceEntry.deviceID = device_id
85 onuDeviceEntry.baseDeviceHandler = device_Handler
86 onuDeviceEntry.coreProxy = core_proxy
87 onuDeviceEntry.adapterProxy = adapter_proxy
88 onuDeviceEntry.devState = DeviceStatusInit
89 //openomciagent.lockDeviceHandlersMap = sync.RWMutex{}
90 //OMCI related databases are on a per-agent basis. State machines and tasks
91 //are per ONU Vendor
92 //
93 // MIB Synchronization Database - possible overloading from arguments
94 if supported_Fsms_Ptr != nil {
95 onuDeviceEntry.supportedFsms = *supported_Fsms_Ptr
96 } else {
97 //var mibSyncFsm = NewMibSynchronizer()
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000098 // use some internaƶ defaults, if not defined from outside
Holger Hildebrandtfa074992020-03-27 15:42:06 +000099 onuDeviceEntry.supportedFsms = OmciDeviceFsms{
100 "mib-synchronizer": {
101 //mibSyncFsm, // Implements the MIB synchronization state machine
102 onuDeviceEntry.MibDbVolatileDict, // Implements volatile ME MIB database
103 true, // Advertise events on OpenOMCI event bus
104 60, // Time to wait between MIB audits. 0 to disable audits.
105 // map[string]func() error{
106 // "mib-upload": onuDeviceEntry.MibUploadTask,
107 // "mib-template": onuDeviceEntry.MibTemplateTask,
108 // "get-mds": onuDeviceEntry.GetMdsTask,
109 // "mib-audit": onuDeviceEntry.GetMdsTask,
110 // "mib-resync": onuDeviceEntry.MibResyncTask,
111 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
112 // },
113 },
114 }
115 }
116 onuDeviceEntry.mibDbClass = onuDeviceEntry.supportedFsms["mib-synchronizer"].databaseClass
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000117 logger.Debug("access2mibDbClass")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000118 go onuDeviceEntry.mibDbClass()
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000119 onuDeviceEntry.mibAuditDelay = onuDeviceEntry.supportedFsms["mib-synchronizer"].auditDelay
120 logger.Debugw("MibAudit is set to", log.Fields{"Delay": onuDeviceEntry.mibAuditDelay})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000121
122 // Omci related Mib sync state machine
123 onuDeviceEntry.MibSyncFsm = fsm.NewFSM(
124 "disabled",
125 fsm.Events{
126
127 {Name: "start", Src: []string{"disabled"}, Dst: "starting"},
128
129 {Name: "load_mib_template", Src: []string{"starting"}, Dst: "loading_mib_template"},
130 {Name: "upload_mib", Src: []string{"loading_mib_template"}, Dst: "uploading"},
131 {Name: "examine_mds", Src: []string{"starting"}, Dst: "examining_mds"},
132
133 {Name: "success", Src: []string{"loading_mib_template"}, Dst: "in_sync"},
134 {Name: "success", Src: []string{"uploading"}, Dst: "in_sync"},
135
136 {Name: "success", Src: []string{"examining_mds"}, Dst: "in_sync"},
137 {Name: "mismatch", Src: []string{"examining_mds"}, Dst: "resynchronizing"},
138
139 {Name: "audit_mib", Src: []string{"in_sync"}, Dst: "auditing"},
140
141 {Name: "success", Src: []string{"out_of_sync"}, Dst: "in_sync"},
142 {Name: "audit_mib", Src: []string{"out_of_sync"}, Dst: "auditing"},
143
144 {Name: "success", Src: []string{"auditing"}, Dst: "in_sync"},
145 {Name: "mismatch", Src: []string{"auditing"}, Dst: "resynchronizing"},
146 {Name: "force_resync", Src: []string{"auditing"}, Dst: "resynchronizing"},
147
148 {Name: "success", Src: []string{"resynchronizing"}, Dst: "in_sync"},
149 {Name: "diffs_found", Src: []string{"resynchronizing"}, Dst: "out_of_sync"},
150
151 {Name: "timeout", Src: []string{"loading_mib_template", "uploading", "resynchronizing", "examining_mds", "in_sync", "out_of_sync", "auditing"}, Dst: "starting"},
152
153 {Name: "stop", Src: []string{"starting", "loading_mib_template", "uploading", "resynchronizing", "examining_mds", "in_sync", "out_of_sync", "auditing"}, Dst: "disabled"},
154 },
155
156 fsm.Callbacks{
157 "enter_state": func(e *fsm.Event) { onuDeviceEntry.logStateChange(e) },
158 "enter_starting": func(e *fsm.Event) { onuDeviceEntry.enterStartingState(e) },
159 "enter_loading_mib_template": func(e *fsm.Event) { onuDeviceEntry.enterLoadingMibTemplateState(e) },
160 "enter_uploading": func(e *fsm.Event) { onuDeviceEntry.enterUploadingState(e) },
161 "enter_examining_mds": func(e *fsm.Event) { onuDeviceEntry.enterExaminingMdsState(e) },
162 "enter_resynchronizing": func(e *fsm.Event) { onuDeviceEntry.enterResynchronizingState(e) },
163 "enter_auditing": func(e *fsm.Event) { onuDeviceEntry.enterAuditingState(e) },
164 "enter_out_of_sync": func(e *fsm.Event) { onuDeviceEntry.enterOutOfSyncState(e) },
165 "enter_in_sync": func(e *fsm.Event) { onuDeviceEntry.enterInSyncState(e) },
166 },
167 )
168
169 // Alarm Synchronization Database
170 //self._alarm_db = None
171 //self._alarm_database_cls = support_classes['alarm-synchronizer']['database']
172 return &onuDeviceEntry
173}
174
175//Start starts (logs) the omci agent
176func (oo *OnuDeviceEntry) Start(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000177 logger.Info("starting-OnuDeviceEntry")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000178
179 oo.PDevOmciCC = NewOmciCC(ctx, oo, oo.deviceID, oo.baseDeviceHandler,
180 oo.coreProxy, oo.adapterProxy)
181
182 //TODO .....
183 //mib_db.start()
184 oo.started = true
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000185 logger.Info("OnuDeviceEntry-started, but not yet mib_db!!!")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000186 return nil
187}
188
189//Stop terminates the session
190func (oo *OnuDeviceEntry) Stop(ctx context.Context) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000191 logger.Info("stopping-OnuDeviceEntry")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000192 oo.started = false
193 //oo.exitChannel <- 1
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000194 logger.Info("OnuDeviceEntry-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000195 return nil
196}
197
198//Relay the InSync message via Handler to Rw core - Status update
199func (oo *OnuDeviceEntry) transferSystemEvent(dev_Event OnuDeviceEvent) error {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000200 logger.Debugw("relaying system-event", log.Fields{"Event": dev_Event})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000201 // decouple the handler transfer from further processing here
202 // TODO!!! check if really no synch is required within the system e.g. to ensure following steps ..
203 if dev_Event == MibDatabaseSync {
204 if oo.devState < MibDatabaseSync { //devState has not been synced yet
205 oo.devState = MibDatabaseSync
206 go oo.baseDeviceHandler.DeviceStateUpdate(dev_Event)
207 //TODO!!! device control: next step: start MIB capability verification from here ?!!!
208 } else {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000209 logger.Debugw("mibinsync-event in some already synced state - ignored", log.Fields{"state": oo.devState})
210 }
211 } else if dev_Event == MibDownloadDone {
212 if oo.devState < MibDownloadDone { //devState has not been synced yet
213 oo.devState = MibDownloadDone
214 go oo.baseDeviceHandler.DeviceStateUpdate(dev_Event)
215 } else {
216 logger.Debugw("mibdownloaddone-event was already seen - ignored", log.Fields{"state": oo.devState})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000217 }
218 } else {
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000219 logger.Warnw("device-event not yet handled", log.Fields{"state": dev_Event})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000220 }
221 return nil
222}