blob: 83ed30f18c2e3611586194dcb55feeccd0f758e7 [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 Hildebrandt4b5e73f2021-08-19 06:51:21 +000017//Package core provides the utility for onu devices, flows and statistics
18package core
Holger Hildebrandtfa074992020-03-27 15:42:06 +000019
20import (
21 "context"
22 "errors"
23 "fmt"
24 "sync"
25 "time"
26
khenaidoo7d3c5582021-08-11 18:09:44 -040027 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
dbainbri4d3a0dc2020-12-02 00:33:42 +000028
khenaidoo7d3c5582021-08-11 18:09:44 -040029 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
30 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoo42dcdfd2021-10-19 17:34:12 -040031 "github.com/opencord/voltha-protos/v5/go/health"
32 "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service"
khenaidoo7d3c5582021-08-11 18:09:44 -040033 "google.golang.org/grpc"
34
35 "github.com/golang/protobuf/ptypes/empty"
36 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
37 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
38 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo42dcdfd2021-10-19 17:34:12 -040039 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo7d3c5582021-08-11 18:09:44 -040040 "github.com/opencord/voltha-protos/v5/go/extension"
khenaidoo42dcdfd2021-10-19 17:34:12 -040041 ia "github.com/opencord/voltha-protos/v5/go/inter_adapter"
42 "github.com/opencord/voltha-protos/v5/go/omci"
khenaidoo7d3c5582021-08-11 18:09:44 -040043 "github.com/opencord/voltha-protos/v5/go/voltha"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000044
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000045 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
Matteo Scandolo761f7512020-11-23 15:52:40 -080046 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000047 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/swupg"
48 uniprt "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/uniprt"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000049)
50
51//OpenONUAC structure holds the ONU core information
52type OpenONUAC struct {
Himani Chawla6d2ae152020-09-02 13:11:20 +053053 deviceHandlers map[string]*deviceHandler
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000054 deviceHandlersCreateChan map[string]chan bool //channels for deviceHandler create events
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000055 mutexDeviceHandlersMap sync.RWMutex
khenaidoo7d3c5582021-08-11 18:09:44 -040056 coreClient *vgrpc.Client
57 parentAdapterClients map[string]*vgrpc.Client
58 lockParentAdapterClients sync.RWMutex
Himani Chawlac07fda02020-12-09 16:21:21 +053059 eventProxy eventif.EventProxy
mpagenkoaf801632020-07-03 10:00:42 +000060 kvClient kvstore.Client
Matteo Scandolof1f39a72020-11-24 12:08:11 -080061 cm *conf.ConfigManager
Holger Hildebrandtfa074992020-03-27 15:42:06 +000062 config *config.AdapterFlags
63 numOnus int
Matteo Scandolo127c59d2021-01-28 11:31:18 -080064 KVStoreAddress string
Holger Hildebrandtfa074992020-03-27 15:42:06 +000065 KVStoreType string
mpagenkoaf801632020-07-03 10:00:42 +000066 KVStoreTimeout time.Duration
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000067 mibTemplatesGenerated map[string]bool
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000068 mutexMibTemplateGenerated sync.RWMutex
Holger Hildebrandtfa074992020-03-27 15:42:06 +000069 exitChannel chan int
70 HeartbeatCheckInterval time.Duration
71 HeartbeatFailReportInterval time.Duration
mpagenkodff5dda2020-08-28 11:52:01 +000072 AcceptIncrementalEvto bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000073 //GrpcTimeoutInterval time.Duration
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000074 pSupportedFsms *cmn.OmciDeviceFsms
Himani Chawlad96df182020-09-28 11:12:02 +053075 maxTimeoutInterAdapterComm time.Duration
Holger Hildebrandt38985dc2021-02-18 16:25:20 +000076 maxTimeoutReconciling time.Duration
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000077 pDownloadManager *swupg.AdapterDownloadManager
78 pFileManager *swupg.FileDownloadManager //let coexist 'old and new' DownloadManager as long as 'old' does not get obsolete
79 MetricsEnabled bool
Holger Hildebrandte3677f12021-02-05 14:50:56 +000080 mibAuditInterval time.Duration
Girish Gowdra0b235842021-03-09 13:06:46 -080081 omciTimeout int // in seconds
Himani Chawla075f1642021-03-15 19:23:24 +053082 alarmAuditInterval time.Duration
mpagenkoc26d4c02021-05-06 14:27:57 +000083 dlToOnuTimeout4M time.Duration
khenaidoo7d3c5582021-08-11 18:09:44 -040084 rpcTimeout time.Duration
Girish Gowdrae95687a2021-09-08 16:30:58 -070085 maxConcurrentFlowsPerUni int
Holger Hildebrandtfa074992020-03-27 15:42:06 +000086}
87
88//NewOpenONUAC returns a new instance of OpenONU_AC
khenaidoo7d3c5582021-08-11 18:09:44 -040089func NewOpenONUAC(ctx context.Context, coreClient *vgrpc.Client, eventProxy eventif.EventProxy,
90 kvClient kvstore.Client, cfg *config.AdapterFlags, cm *conf.ConfigManager) *OpenONUAC {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000091 var openOnuAc OpenONUAC
92 openOnuAc.exitChannel = make(chan int, 1)
Himani Chawla6d2ae152020-09-02 13:11:20 +053093 openOnuAc.deviceHandlers = make(map[string]*deviceHandler)
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000094 openOnuAc.deviceHandlersCreateChan = make(map[string]chan bool)
khenaidoo7d3c5582021-08-11 18:09:44 -040095 openOnuAc.parentAdapterClients = make(map[string]*vgrpc.Client)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000096 openOnuAc.mutexDeviceHandlersMap = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +000097 openOnuAc.config = cfg
Matteo Scandolof1f39a72020-11-24 12:08:11 -080098 openOnuAc.cm = cm
khenaidoo7d3c5582021-08-11 18:09:44 -040099 openOnuAc.coreClient = coreClient
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000100 openOnuAc.numOnus = cfg.OnuNumber
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000101 openOnuAc.eventProxy = eventProxy
mpagenkoaf801632020-07-03 10:00:42 +0000102 openOnuAc.kvClient = kvClient
Matteo Scandolo127c59d2021-01-28 11:31:18 -0800103 openOnuAc.KVStoreAddress = cfg.KVStoreAddress
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000104 openOnuAc.KVStoreType = cfg.KVStoreType
mpagenkoaf801632020-07-03 10:00:42 +0000105 openOnuAc.KVStoreTimeout = cfg.KVStoreTimeout
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000106 openOnuAc.mibTemplatesGenerated = make(map[string]bool)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000107 openOnuAc.mutexMibTemplateGenerated = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000108 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
109 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
mpagenkodff5dda2020-08-28 11:52:01 +0000110 openOnuAc.AcceptIncrementalEvto = cfg.AccIncrEvto
Himani Chawlad96df182020-09-28 11:12:02 +0530111 openOnuAc.maxTimeoutInterAdapterComm = cfg.MaxTimeoutInterAdapterComm
Holger Hildebrandt38985dc2021-02-18 16:25:20 +0000112 openOnuAc.maxTimeoutReconciling = cfg.MaxTimeoutReconciling
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000113 //openOnuAc.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000114 openOnuAc.MetricsEnabled = cfg.MetricsEnabled
Holger Hildebrandte3677f12021-02-05 14:50:56 +0000115 openOnuAc.mibAuditInterval = cfg.MibAuditInterval
Girish Gowdra0b235842021-03-09 13:06:46 -0800116 // since consumers of OMCI timeout value everywhere in code is in "int seconds", do this useful conversion
117 openOnuAc.omciTimeout = int(cfg.OmciTimeout.Seconds())
Himani Chawla075f1642021-03-15 19:23:24 +0530118 openOnuAc.alarmAuditInterval = cfg.AlarmAuditInterval
mpagenkoc26d4c02021-05-06 14:27:57 +0000119 openOnuAc.dlToOnuTimeout4M = cfg.DownloadToOnuTimeout4MB
khenaidoo7d3c5582021-08-11 18:09:44 -0400120 openOnuAc.rpcTimeout = cfg.RPCTimeout
Girish Gowdrae95687a2021-09-08 16:30:58 -0700121 openOnuAc.maxConcurrentFlowsPerUni = cfg.MaxConcurrentFlowsPerUni
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000122
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000123 openOnuAc.pSupportedFsms = &cmn.OmciDeviceFsms{
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000124 "mib-synchronizer": {
125 //mibSyncFsm, // Implements the MIB synchronization state machine
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000126 DatabaseClass: mibDbVolatileDictImpl, // Implements volatile ME MIB database
Himani Chawla4d908332020-08-31 12:30:20 +0530127 //true, // Advertise events on OpenOMCI event bus
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000128 AuditInterval: openOnuAc.mibAuditInterval, // Time to wait between MIB audits. 0 to disable audits.
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000129 // map[string]func() error{
130 // "mib-upload": onuDeviceEntry.MibUploadTask,
131 // "mib-template": onuDeviceEntry.MibTemplateTask,
132 // "get-mds": onuDeviceEntry.GetMdsTask,
133 // "mib-audit": onuDeviceEntry.GetMdsTask,
134 // "mib-resync": onuDeviceEntry.MibResyncTask,
135 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
136 // },
137 },
138 }
139
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000140 openOnuAc.pDownloadManager = swupg.NewAdapterDownloadManager(ctx)
141 openOnuAc.pFileManager = swupg.NewFileDownloadManager(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +0000142 openOnuAc.pFileManager.SetDownloadTimeout(ctx, cfg.DownloadToAdapterTimeout)
mpagenkoc8bba412021-01-15 15:38:44 +0000143
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000144 return &openOnuAc
145}
146
147//Start starts (logs) the adapter
148func (oo *OpenONUAC) Start(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000149 logger.Info(ctx, "starting-openonu-adapter")
mpagenkoc8bba412021-01-15 15:38:44 +0000150
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000151 return nil
152}
153
Himani Chawla6d2ae152020-09-02 13:11:20 +0530154/*
155//stop terminates the session
156func (oo *OpenONUAC) stop(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000157 logger.Info(ctx,"stopping-device-manager")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000158 oo.exitChannel <- 1
dbainbri4d3a0dc2020-12-02 00:33:42 +0000159 logger.Info(ctx,"device-manager-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000160 return nil
161}
Himani Chawla6d2ae152020-09-02 13:11:20 +0530162*/
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000163
Himani Chawla6d2ae152020-09-02 13:11:20 +0530164func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *deviceHandler) {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000165 oo.mutexDeviceHandlersMap.Lock()
166 defer oo.mutexDeviceHandlersMap.Unlock()
167 if _, exist := oo.deviceHandlers[agent.DeviceID]; !exist {
168 oo.deviceHandlers[agent.DeviceID] = agent
169 oo.deviceHandlers[agent.DeviceID].start(ctx)
170 if _, exist := oo.deviceHandlersCreateChan[agent.DeviceID]; exist {
171 logger.Debugw(ctx, "deviceHandler created - trigger processing of pending ONU_IND_REQUEST", log.Fields{"device-id": agent.DeviceID})
172 oo.deviceHandlersCreateChan[agent.DeviceID] <- true
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000173 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000174 }
175}
176
Himani Chawla6d2ae152020-09-02 13:11:20 +0530177func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *deviceHandler) {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000178 oo.mutexDeviceHandlersMap.Lock()
179 defer oo.mutexDeviceHandlersMap.Unlock()
180 delete(oo.deviceHandlers, agent.DeviceID)
181 delete(oo.deviceHandlersCreateChan, agent.DeviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000182}
183
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000184//getDeviceHandler gets the ONU deviceHandler and may wait until it is created
dbainbri4d3a0dc2020-12-02 00:33:42 +0000185func (oo *OpenONUAC) getDeviceHandler(ctx context.Context, deviceID string, aWait bool) *deviceHandler {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000186 oo.mutexDeviceHandlersMap.Lock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000187 agent, ok := oo.deviceHandlers[deviceID]
188 if aWait && !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000189 logger.Infow(ctx, "Race condition: deviceHandler not present - wait for creation or timeout",
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000190 log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000191 if _, exist := oo.deviceHandlersCreateChan[deviceID]; !exist {
192 oo.deviceHandlersCreateChan[deviceID] = make(chan bool, 1)
193 }
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800194 deviceCreateChan := oo.deviceHandlersCreateChan[deviceID]
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000195 //keep the read sema short to allow for subsequent write
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000196 oo.mutexDeviceHandlersMap.Unlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000197 // based on concurrent processing the deviceHandler creation may not yet be finished at his point
198 // so it might be needed to wait here for that event with some timeout
199 select {
200 case <-time.After(1 * time.Second): //timer may be discussed ...
dbainbri4d3a0dc2020-12-02 00:33:42 +0000201 logger.Warnw(ctx, "No valid deviceHandler created after max WaitTime", log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000202 return nil
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800203 case <-deviceCreateChan:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000204 logger.Debugw(ctx, "deviceHandler is ready now - continue", log.Fields{"device-id": deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000205 oo.mutexDeviceHandlersMap.RLock()
206 defer oo.mutexDeviceHandlersMap.RUnlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000207 return oo.deviceHandlers[deviceID]
208 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000209 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000210 oo.mutexDeviceHandlersMap.Unlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000211 return agent
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000212}
213
khenaidoo7d3c5582021-08-11 18:09:44 -0400214// GetHealthStatus is used as a service readiness validation as a grpc connection
khenaidoo42dcdfd2021-10-19 17:34:12 -0400215func (oo *OpenONUAC) GetHealthStatus(ctx context.Context, empty *empty.Empty) (*health.HealthStatus, error) {
216 return &health.HealthStatus{State: health.HealthStatus_HEALTHY}, nil
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000217}
218
khenaidoo7d3c5582021-08-11 18:09:44 -0400219// AdoptDevice creates a new device handler if not present already and then adopts the device
220func (oo *OpenONUAC) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000221 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000222 logger.Warn(ctx, "voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400223 return nil, errors.New("nil-device")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000224 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000225 logger.Infow(ctx, "adopt-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530226 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000227 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400228 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000229 oo.addDeviceHandlerToMap(ctx, handler)
khenaidoo7d3c5582021-08-11 18:09:44 -0400230
231 // Setup the grpc communication with the parent adapter
232 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
233 // TODO: Cleanup on failure needed
234 return nil, err
235 }
236
237 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), device)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000238 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400239 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000240}
241
khenaidoo7d3c5582021-08-11 18:09:44 -0400242//ReconcileDevice is called once when the adapter needs to re-create device - usually on core restart
243func (oo *OpenONUAC) ReconcileDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000244 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000245 logger.Warn(ctx, "reconcile-device-voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400246 return nil, errors.New("nil-device")
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000247 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000248 logger.Infow(ctx, "reconcile-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530249 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000250 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400251 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000252 oo.addDeviceHandlerToMap(ctx, handler)
253 handler.device = device
khenaidoo42dcdfd2021-10-19 17:34:12 -0400254 if err := handler.updateDeviceStateInCore(log.WithSpanFromContext(context.Background(), ctx), &ca.DeviceStateFilter{
khenaidoo7d3c5582021-08-11 18:09:44 -0400255 DeviceId: device.Id,
256 OperStatus: voltha.OperStatus_RECONCILING,
257 ConnStatus: device.ConnectStatus,
258 }); err != nil {
259 return nil, fmt.Errorf("not able to update device state to reconciling. Err : %s", err.Error())
Maninderb5187552021-03-23 22:23:42 +0530260 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400261 // Setup the grpc communication with the parent adapter
262 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
263 // TODO: Cleanup on failure needed
264 return nil, err
265 }
266
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000267 handler.StartReconciling(log.WithSpanFromContext(context.Background(), ctx), false)
khenaidoo7d3c5582021-08-11 18:09:44 -0400268 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), handler.device)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000269 // reconcilement will be continued after onu-device entry is added
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000270 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400271 return nil, fmt.Errorf(fmt.Sprintf("device-already-reconciled-or-active-%s", device.Id))
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000272 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400273 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000274}
275
khenaidoo7d3c5582021-08-11 18:09:44 -0400276//DisableDevice disables the given device
277func (oo *OpenONUAC) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000278 logger.Infow(ctx, "disable-device", log.Fields{"device-id": device.Id})
279 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400280 go handler.disableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
281 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300282 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000283 logger.Warnw(ctx, "no handler found for device-disable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400284 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000285}
286
khenaidoo7d3c5582021-08-11 18:09:44 -0400287//ReEnableDevice enables the onu device after disable
288func (oo *OpenONUAC) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000289 logger.Infow(ctx, "reenable-device", log.Fields{"device-id": device.Id})
290 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400291 go handler.reEnableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
292 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300293 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000294 logger.Warnw(ctx, "no handler found for device-reenable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400295 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000296}
297
khenaidoo7d3c5582021-08-11 18:09:44 -0400298//RebootDevice reboots the given device
299func (oo *OpenONUAC) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000300 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
301 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400302 go handler.rebootDevice(log.WithSpanFromContext(context.Background(), ctx), true, device) //reboot request with device checking
303 return &empty.Empty{}, nil
ozgecanetsiae11479f2020-07-06 09:44:47 +0300304 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000305 logger.Warnw(ctx, "no handler found for device-reboot", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400306 return nil, fmt.Errorf("handler-not-found-for-device: %s", device.Id)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000307}
308
khenaidoo7d3c5582021-08-11 18:09:44 -0400309// DeleteDevice deletes the given device
310func (oo *OpenONUAC) DeleteDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
311 nctx := log.WithSpanFromContext(context.Background(), ctx)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000312
khenaidoo7d3c5582021-08-11 18:09:44 -0400313 logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id, "SerialNumber": device.SerialNumber, "ctx": ctx, "nctx": nctx})
dbainbri4d3a0dc2020-12-02 00:33:42 +0000314 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
Girish Gowdra0e533642021-03-02 22:02:51 -0800315 var errorsList []error
Holger Hildebrandtff05b682021-03-16 15:02:05 +0000316
317 handler.mutexDeletionInProgressFlag.Lock()
318 handler.deletionInProgress = true
319 handler.mutexDeletionInProgressFlag.Unlock()
320
Girish Gowdra0e533642021-03-02 22:02:51 -0800321 if err := handler.deleteDevicePersistencyData(ctx); err != nil {
322 errorsList = append(errorsList, err)
323 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700324
325 // Stop PM, Alarm and Self Test event handler routines
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000326 if handler.GetCollectorIsRunning() {
Girish Gowdra10123c02021-08-30 11:52:06 -0700327 handler.stopCollector <- true
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700328 logger.Debugw(ctx, "sent stop signal to metric collector routine", log.Fields{"device-id": device.Id})
Girish Gowdra10123c02021-08-30 11:52:06 -0700329
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700330 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000331 if handler.GetAlarmManagerIsRunning(ctx) {
Girish Gowdra10123c02021-08-30 11:52:06 -0700332 handler.stopAlarmManager <- true
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700333 logger.Debugw(ctx, "sent stop signal to alarm manager", log.Fields{"device-id": device.Id})
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700334 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700335 if handler.pSelfTestHdlr.GetSelfTestHandlerIsRunning() {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000336 handler.pSelfTestHdlr.StopSelfTestModule <- true
Girish Gowdra10123c02021-08-30 11:52:06 -0700337 logger.Debugw(ctx, "sent stop signal to self test handler module", log.Fields{"device-id": device.Id})
338 }
Girish Gowdrae95687a2021-09-08 16:30:58 -0700339 for _, uni := range handler.uniEntityMap {
340 if handler.GetFlowMonitoringIsRunning(uni.UniID) {
341 handler.stopFlowMonitoringRoutine[uni.UniID] <- true
342 logger.Debugw(ctx, "sent stop signal to self flow monitoring routine", log.Fields{"device-id": device.Id})
343 }
344 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700345
346 // Clear PM data on the KV store
Girish Gowdra0e533642021-03-02 22:02:51 -0800347 if handler.pOnuMetricsMgr != nil {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000348 if err := handler.pOnuMetricsMgr.ClearAllPmData(ctx); err != nil {
Girish Gowdra0e533642021-03-02 22:02:51 -0800349 errorsList = append(errorsList, err)
350 }
351 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700352
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000353 //don't leave any garbage - even in error case
354 oo.deleteDeviceHandlerToMap(handler)
Girish Gowdra0e533642021-03-02 22:02:51 -0800355 if len(errorsList) > 0 {
356 logger.Errorw(ctx, "one-or-more-error-during-device-delete", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400357 return nil, fmt.Errorf("one-or-more-error-during-device-delete, errors:%v", errorsList)
Girish Gowdra0e533642021-03-02 22:02:51 -0800358 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400359 return &empty.Empty{}, nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000360 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000361 logger.Warnw(ctx, "no handler found for device-deletion", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400362 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000363}
364
khenaidoo7d3c5582021-08-11 18:09:44 -0400365//UpdateFlowsIncrementally updates (add/remove) the flows on a given device
khenaidoo42dcdfd2021-10-19 17:34:12 -0400366func (oo *OpenONUAC) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ca.IncrementalFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400367 logger.Infow(ctx, "update-flows-incrementally", log.Fields{"device-id": incrFlows.Device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000368
mpagenkofc4f56e2020-11-04 17:17:49 +0000369 //flow config is relayed to handler even if the device might be in some 'inactive' state
370 // let the handler or related FSM's decide, what to do with the modified flow state info
371 // at least the flow-remove must be done in respect to internal data, while OMCI activity might not be needed here
mpagenkodff5dda2020-08-28 11:52:01 +0000372
373 // For now, there is no support for group changes (as in the actual Py-adapter code)
mpagenkofc4f56e2020-11-04 17:17:49 +0000374 // but processing is continued for flowUpdate possibly also set in the request
khenaidoo7d3c5582021-08-11 18:09:44 -0400375 if incrFlows.Groups.ToAdd != nil && incrFlows.Groups.ToAdd.Items != nil {
376 logger.Warnw(ctx, "Update-flow-incr: group add not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000377 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400378 if incrFlows.Groups.ToRemove != nil && incrFlows.Groups.ToRemove.Items != nil {
379 logger.Warnw(ctx, "Update-flow-incr: group remove not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000380 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400381 if incrFlows.Groups.ToUpdate != nil && incrFlows.Groups.ToUpdate.Items != nil {
382 logger.Warnw(ctx, "Update-flow-incr: group update not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000383 }
384
khenaidoo7d3c5582021-08-11 18:09:44 -0400385 if handler := oo.getDeviceHandler(ctx, incrFlows.Device.Id, false); handler != nil {
386 if err := handler.FlowUpdateIncremental(log.WithSpanFromContext(context.Background(), ctx), incrFlows.Flows, incrFlows.Groups, incrFlows.FlowMetadata); err != nil {
387 return nil, err
388 }
389 return &empty.Empty{}, nil
mpagenkodff5dda2020-08-28 11:52:01 +0000390 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400391 logger.Warnw(ctx, "no handler found for incremental flow update", log.Fields{"device-id": incrFlows.Device.Id})
392 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", incrFlows.Device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000393}
394
khenaidoo7d3c5582021-08-11 18:09:44 -0400395//UpdatePmConfig returns PmConfigs nil or error
khenaidoo42dcdfd2021-10-19 17:34:12 -0400396func (oo *OpenONUAC) UpdatePmConfig(ctx context.Context, configs *ca.PmConfigsInfo) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400397 logger.Infow(ctx, "update-pm-config", log.Fields{"device-id": configs.DeviceId})
398 if handler := oo.getDeviceHandler(ctx, configs.DeviceId, false); handler != nil {
399 if err := handler.updatePmConfig(log.WithSpanFromContext(context.Background(), ctx), configs.PmConfigs); err != nil {
400 return nil, err
401 }
402 return &empty.Empty{}, nil
Girish Gowdrae09a6202021-01-12 18:10:59 -0800403 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400404 logger.Warnw(ctx, "no handler found for update-pm-config", log.Fields{"device-id": configs.DeviceId})
405 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", configs.DeviceId))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000406}
407
khenaidoo7d3c5582021-08-11 18:09:44 -0400408//DownloadImage requests downloading some image according to indications as given in request
Andrea Campanella71e546a2021-02-26 11:09:33 +0100409//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400410func (oo *OpenONUAC) DownloadImage(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400411 ctx = log.WithSpanFromContext(context.Background(), ctx)
412 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000413 if !oo.pDownloadManager.ImageExists(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400414 logger.Debugw(ctx, "start image download", log.Fields{"image-description": imageInfo.Image})
mpagenko15ff4a52021-03-02 10:09:20 +0000415 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
416 // the download itself is later done in background
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000417 if err := oo.pDownloadManager.StartDownload(ctx, imageInfo.Image); err != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400418 return nil, err
419 }
420 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000421 }
422 // image already exists
khenaidoo7d3c5582021-08-11 18:09:44 -0400423 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageInfo.Image})
424 return imageInfo.Image, nil
mpagenkoc8bba412021-01-15 15:38:44 +0000425 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400426
427 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000428}
429
mpagenko59862f02021-10-11 08:53:18 +0000430//ActivateImageUpdate requests downloading some Onu Software image to the ONU via OMCI
Andrea Campanella71e546a2021-02-26 11:09:33 +0100431// according to indications as given in request and on success activate the image on the ONU
432//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400433func (oo *OpenONUAC) ActivateImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400434 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000435 if oo.pDownloadManager.ImageLocallyDownloaded(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400436 if handler := oo.getDeviceHandler(ctx, imageInfo.Device.Id, false); handler != nil {
mpagenko15ff4a52021-03-02 10:09:20 +0000437 logger.Debugw(ctx, "image download on omci requested", log.Fields{
khenaidoo7d3c5582021-08-11 18:09:44 -0400438 "image-description": imageInfo.Image, "device-id": imageInfo.Device.Id})
439 if err := handler.doOnuSwUpgrade(ctx, imageInfo.Image, oo.pDownloadManager); err != nil {
440 return nil, err
441 }
442 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000443 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400444 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": imageInfo.Device.Id})
445 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found - device-id: %s", imageInfo.Device.Id))
mpagenko057889c2021-01-21 16:51:58 +0000446 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400447 logger.Debugw(ctx, "image not yet downloaded on activate request", log.Fields{"image-description": imageInfo.Image})
448 return nil, fmt.Errorf(fmt.Sprintf("image-not-yet-downloaded - device-id: %s", imageInfo.Device.Id))
mpagenkoc8bba412021-01-15 15:38:44 +0000449 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400450 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000451}
452
khenaidoo7d3c5582021-08-11 18:09:44 -0400453//GetSingleValue handles the core request to retrieve uni status
454func (oo *OpenONUAC) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
kesavandfdf77632021-01-26 23:40:33 -0500455 logger.Infow(ctx, "Single_get_value_request", log.Fields{"request": request})
456
457 if handler := oo.getDeviceHandler(ctx, request.TargetId, false); handler != nil {
458 switch reqType := request.GetRequest().GetRequest().(type) {
459 case *extension.GetValueRequest_UniInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000460 return handler.GetUniPortStatus(ctx, reqType.UniInfo), nil
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700461 case *extension.GetValueRequest_OnuOpticalInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000462 CommChan := make(chan cmn.Message)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700463 respChan := make(chan extension.SingleGetValueResponse)
464 // Initiate the self test request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000465 if err := handler.pSelfTestHdlr.SelfTestRequestStart(ctx, *request, CommChan, respChan); err != nil {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700466 return &extension.SingleGetValueResponse{
467 Response: &extension.GetValueResponse{
468 Status: extension.GetValueResponse_ERROR,
469 ErrReason: extension.GetValueResponse_INTERNAL_ERROR,
470 },
471 }, err
472 }
473 // The timeout handling is already implemented in omci_self_test_handler module
474 resp := <-respChan
475 return &resp, nil
Himani Chawla43f95ff2021-06-03 00:24:12 +0530476 case *extension.GetValueRequest_OnuInfo:
477 return handler.getOnuOMCICounters(ctx, reqType.OnuInfo), nil
kesavandfdf77632021-01-26 23:40:33 -0500478 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000479 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_UNSUPPORTED), nil
kesavandfdf77632021-01-26 23:40:33 -0500480
481 }
482 }
483 logger.Errorw(ctx, "Single_get_value_request failed ", log.Fields{"request": request})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000484 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_INVALID_DEVICE_ID), nil
mpagenkoc8bba412021-01-15 15:38:44 +0000485}
486
mpagenko83144272021-04-27 10:06:22 +0000487//if update >= 4.3.0
mpagenkoc26d4c02021-05-06 14:27:57 +0000488// Note: already with the implementation of the 'old' download interface problems were detected when the argument name used here is not the same
489// as defined in the adapter interface file. That sounds strange and the effects were strange as well.
490// The reason for that was never finally investigated.
491// To be on the safe side argument names are left here always as defined in iAdapter.go .
mpagenko83144272021-04-27 10:06:22 +0000492
khenaidoo7d3c5582021-08-11 18:09:44 -0400493// DownloadOnuImage downloads (and optionally activates and commits) the indicated ONU image to the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000494// if the image is not yet present on the adapter it has to be automatically downloaded
khenaidoo7d3c5582021-08-11 18:09:44 -0400495func (oo *OpenONUAC) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000496 if request != nil && len((*request).DeviceId) > 0 && (*request).Image.Version != "" {
497 loResponse := voltha.DeviceImageResponse{}
498 imageIdentifier := (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000499 downloadedToAdapter := false
mpagenkoc26d4c02021-05-06 14:27:57 +0000500 firstDevice := true
501 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000502 var onuVolthaDevice *voltha.Device
503 var devErr error
mpagenkoc26d4c02021-05-06 14:27:57 +0000504 for _, pCommonID := range (*request).DeviceId {
mpagenko38662d02021-08-11 09:45:19 +0000505 vendorIDMatch := true
mpagenkoc26d4c02021-05-06 14:27:57 +0000506 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000507 loDeviceImageState := voltha.DeviceImageState{}
508 loDeviceImageState.DeviceId = loDeviceID
509 loImageState := voltha.ImageState{}
510 loDeviceImageState.ImageState = &loImageState
511 loDeviceImageState.ImageState.Version = (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000512
mpagenko59862f02021-10-11 08:53:18 +0000513 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400514 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000515 if handler != nil {
516 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
517 } else {
518 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
519 devErr = errors.New("no handler found for device-id")
khenaidoo7d3c5582021-08-11 18:09:44 -0400520 }
mpagenko59862f02021-10-11 08:53:18 +0000521 if devErr != nil || onuVolthaDevice == nil {
522 logger.Warnw(ctx, "Failed to fetch ONU device for image download",
523 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000524 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
525 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //proto restriction, better option: 'INVALID_DEVICE'
mpagenkoc26d4c02021-05-06 14:27:57 +0000526 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000527 } else {
mpagenko38662d02021-08-11 09:45:19 +0000528 if firstDevice {
529 //start/verify download of the image to the adapter based on first found device only
530 // use the OnuVendor identification from first given device
531 firstDevice = false
532 vendorID = onuVolthaDevice.VendorId
533 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
534 logger.Debugw(ctx, "download request for file", log.Fields{"image-id": imageIdentifier})
535
536 if !oo.pFileManager.ImageExists(ctx, imageIdentifier) {
537 logger.Debugw(ctx, "start image download", log.Fields{"image-description": request})
538 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
539 // the download itself is later done in background
540 if err := oo.pFileManager.StartDownload(ctx, imageIdentifier, (*request).Image.Url); err == nil {
541 downloadedToAdapter = true
542 }
543 //else: treat any error here as 'INVALID_URL' (even though it might as well be some issue on local FS, eg. 'INSUFFICIENT_SPACE')
544 // otherwise a more sophisticated error evaluation is needed
545 } else {
546 // image already exists
547 downloadedToAdapter = true
548 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageIdentifier})
549 // note: If the image (with vendorId+name) has already been downloaded before from some other
550 // valid URL, the current URL is just ignored. If the operators want to ensure that the new URL
551 // is really used, then they first have to use the 'abort' API to remove the existing image!
552 // (abort API can be used also after some successful download to just remove the image from adapter)
553 }
554 } else {
555 //for all following devices verify the matching vendorID
556 if onuVolthaDevice.VendorId != vendorID {
557 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
558 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
559 vendorIDMatch = false
560 }
561 }
562 if downloadedToAdapter && vendorIDMatch {
563 // start the ONU download activity for each possible device
mpagenko59862f02021-10-11 08:53:18 +0000564 logger.Debugw(ctx, "image download on omci requested", log.Fields{
565 "image-id": imageIdentifier, "device-id": loDeviceID})
566 //onu upgrade handling called in background without immediate error evaluation here
567 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
568 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
569 go handler.onuSwUpgradeAfterDownload(ctx, request, oo.pFileManager, imageIdentifier)
570 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_STARTED
571 loDeviceImageState.ImageState.Reason = voltha.ImageState_NO_ERROR
572 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenko38662d02021-08-11 09:45:19 +0000573 } else {
574 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
575 if !downloadedToAdapter {
576 loDeviceImageState.ImageState.Reason = voltha.ImageState_INVALID_URL
577 } else { //only logical option is !vendorIDMatch
578 loDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
579 }
580 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
581 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000582 }
mpagenko2f2f2362021-06-07 08:25:22 +0000583 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000584 } //for all requested devices
mpagenkoc26d4c02021-05-06 14:27:57 +0000585 pImageResp := &loResponse
586 return pImageResp, nil
587 }
588 return nil, errors.New("invalid image download parameters")
mpagenko83144272021-04-27 10:06:22 +0000589}
590
khenaidoo7d3c5582021-08-11 18:09:44 -0400591// GetOnuImageStatus delivers the adapter-related information about the download/activation/commitment
mpagenko83144272021-04-27 10:06:22 +0000592// status for the requested image
khenaidoo7d3c5582021-08-11 18:09:44 -0400593func (oo *OpenONUAC) GetOnuImageStatus(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000594 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
595 loResponse := voltha.DeviceImageResponse{}
mpagenkoaa3afe92021-05-21 16:20:58 +0000596 imageIdentifier := (*in).Version
mpagenko38662d02021-08-11 09:45:19 +0000597 var vendorIDSet bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000598 firstDevice := true
599 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000600 var onuVolthaDevice *voltha.Device
601 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000602 for _, pCommonID := range (*in).DeviceId {
603 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400604 pDeviceImageState := &voltha.DeviceImageState{DeviceId: loDeviceID}
mpagenko59862f02021-10-11 08:53:18 +0000605 vendorIDSet = false
606 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400607 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000608 if handler != nil {
609 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
610 } else {
611 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
612 devErr = errors.New("no handler found for device-id")
mpagenko38662d02021-08-11 09:45:19 +0000613 }
mpagenko59862f02021-10-11 08:53:18 +0000614 if devErr != nil || onuVolthaDevice == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +0000615 logger.Warnw(ctx, "Failed to fetch Onu device to get image status",
mpagenko59862f02021-10-11 08:53:18 +0000616 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000617 pImageState := &voltha.ImageState{
618 Version: (*in).Version,
619 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //no statement about last activity possible
620 Reason: voltha.ImageState_UNKNOWN_ERROR, //something like "DEVICE_NOT_EXISTS" would be better (proto def)
621 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000622 }
mpagenko38662d02021-08-11 09:45:19 +0000623 pDeviceImageState.ImageState = pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000624 } else {
mpagenko38662d02021-08-11 09:45:19 +0000625 if firstDevice {
626 //start/verify download of the image to the adapter based on first found device only
627 // use the OnuVendor identification from first given device
628 firstDevice = false
629 vendorID = onuVolthaDevice.VendorId
630 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
631 vendorIDSet = true
632 logger.Debugw(ctx, "status request for image", log.Fields{"image-id": imageIdentifier})
633 } else {
634 //for all following devices verify the matching vendorID
635 if onuVolthaDevice.VendorId != vendorID {
636 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
637 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
638 } else {
639 vendorIDSet = true
640 }
641 }
642 if !vendorIDSet {
643 pImageState := &voltha.ImageState{
644 Version: (*in).Version,
645 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //can't be sure that download for this device was really tried
646 Reason: voltha.ImageState_VENDOR_DEVICE_MISMATCH,
647 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
648 }
649 pDeviceImageState.ImageState = pImageState
650 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400651 logger.Debugw(ctx, "image status request for", log.Fields{
652 "image-id": imageIdentifier, "device-id": loDeviceID})
653 //status request is called synchronously to collect the indications for all concerned devices
654 pDeviceImageState.ImageState = handler.requestOnuSwUpgradeState(ctx, imageIdentifier, (*in).Version)
mpagenko38662d02021-08-11 09:45:19 +0000655 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000656 }
657 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000658 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000659 pImageResp := &loResponse
660 return pImageResp, nil
661 }
662 return nil, errors.New("invalid image status request parameters")
mpagenko83144272021-04-27 10:06:22 +0000663}
664
khenaidoo7d3c5582021-08-11 18:09:44 -0400665// AbortOnuImageUpgrade stops the actual download/activation/commitment process (on next possibly step)
666func (oo *OpenONUAC) AbortOnuImageUpgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000667 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
668 loResponse := voltha.DeviceImageResponse{}
669 imageIdentifier := (*in).Version
670 firstDevice := true
671 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000672 var vendorIDSet bool
673 var onuVolthaDevice *voltha.Device
674 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000675 for _, pCommonID := range (*in).DeviceId {
676 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400677 pDeviceImageState := &voltha.DeviceImageState{}
678 loImageState := voltha.ImageState{}
679 pDeviceImageState.ImageState = &loImageState
mpagenko59862f02021-10-11 08:53:18 +0000680 vendorIDSet = false
681 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400682 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000683 if handler != nil {
684 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
685 } else {
686 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
687 devErr = errors.New("no handler found for device-id")
688 }
689 if devErr != nil || onuVolthaDevice == nil {
690 logger.Warnw(ctx, "Failed to fetch Onu device to abort its download",
691 log.Fields{"device-id": loDeviceID, "err": devErr})
khenaidoo7d3c5582021-08-11 18:09:44 -0400692 pDeviceImageState.DeviceId = loDeviceID
693 pDeviceImageState.ImageState.Version = (*in).Version
mpagenko59862f02021-10-11 08:53:18 +0000694 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
695 pDeviceImageState.ImageState.Reason = voltha.ImageState_CANCELLED_ON_REQUEST //something better could be considered (MissingHandler) - proto
khenaidoo7d3c5582021-08-11 18:09:44 -0400696 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoaa3afe92021-05-21 16:20:58 +0000697 } else {
mpagenko59862f02021-10-11 08:53:18 +0000698 if firstDevice {
699 //start/verify download of the image to the adapter based on first found device only
700 // use the OnuVendor identification from first given device
701 firstDevice = false
702 vendorID = onuVolthaDevice.VendorId
703 vendorIDSet = true
704 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
705 logger.Debugw(ctx, "abort request for file", log.Fields{"image-id": imageIdentifier})
706 } else {
707 //for all following devices verify the matching vendorID
708 if onuVolthaDevice.VendorId != vendorID {
709 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
710 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
711 pDeviceImageState.DeviceId = loDeviceID
712 pDeviceImageState.ImageState.Version = (*in).Version
713 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
714 pDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
715 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
716 } else {
717 vendorIDSet = true
718 }
719 }
720 if vendorIDSet {
721 // cancel the ONU upgrade activity for each possible device
722 logger.Debugw(ctx, "image upgrade abort requested", log.Fields{
723 "image-id": imageIdentifier, "device-id": loDeviceID})
724 //upgrade cancel is called synchronously to collect the imageResponse indications for all concerned devices
725 handler.cancelOnuSwUpgrade(ctx, imageIdentifier, (*in).Version, pDeviceImageState)
mpagenkoaa3afe92021-05-21 16:20:58 +0000726 }
727 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000728 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000729 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000730 if !firstDevice {
731 //if at least one valid device was found cancel also a possibly running download to adapter and remove the image
732 // this is to be done after the upgradeOnu cancel activities in order to not subduct the file for still running processes
733 oo.pFileManager.CancelDownload(ctx, imageIdentifier)
734 }
735 pImageResp := &loResponse
736 return pImageResp, nil
737 }
738 return nil, errors.New("invalid image upgrade abort parameters")
mpagenko83144272021-04-27 10:06:22 +0000739}
740
khenaidoo7d3c5582021-08-11 18:09:44 -0400741// GetOnuImages retrieves the ONU SW image status information via OMCI
742func (oo *OpenONUAC) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) {
743 logger.Infow(ctx, "Get_onu_images", log.Fields{"device-id": id.Id})
744 if handler := oo.getDeviceHandler(ctx, id.Id, false); handler != nil {
Himani Chawla69992ab2021-07-08 15:13:02 +0530745 images, err := handler.getOnuImages(ctx)
746 if err == nil {
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000747 return images, nil
748 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400749 return nil, fmt.Errorf(fmt.Sprintf("%s-%s", err, id.Id))
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000750 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400751 logger.Warnw(ctx, "no handler found for Get_onu_images", log.Fields{"device-id": id.Id})
752 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", id.Id))
mpagenko83144272021-04-27 10:06:22 +0000753}
754
khenaidoo7d3c5582021-08-11 18:09:44 -0400755// ActivateOnuImage initiates the activation of the image for the requested ONU(s)
mpagenkoc26d4c02021-05-06 14:27:57 +0000756// precondition: image downloaded and not yet activated or image refers to current inactive image
khenaidoo7d3c5582021-08-11 18:09:44 -0400757func (oo *OpenONUAC) ActivateOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000758 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
759 loResponse := voltha.DeviceImageResponse{}
760 imageIdentifier := (*in).Version
761 //let the deviceHandler find the adequate way of requesting the image activation
762 for _, pCommonID := range (*in).DeviceId {
763 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000764 loDeviceImageState := voltha.DeviceImageState{}
765 loDeviceImageState.DeviceId = loDeviceID
766 loImageState := voltha.ImageState{}
767 loDeviceImageState.ImageState = &loImageState
768 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000769 //compared to download procedure the vendorID (from device) is secondary here
770 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
771 // start image activation activity for each possible device
772 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
773 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
774 logger.Debugw(ctx, "onu image activation requested", log.Fields{
775 "image-id": imageIdentifier, "device-id": loDeviceID})
776 //onu activation handling called in background without immediate error evaluation here
777 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
778 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000779 if pImageStates, err := handler.onuSwActivateRequest(ctx, imageIdentifier, (*in).CommitOnSuccess); err != nil {
780 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
781 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
782 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
783 } else {
784 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
785 loDeviceImageState.ImageState.Reason = pImageStates.Reason
786 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
787 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000788 } else {
789 //cannot start SW activation for requested device
790 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000791 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000792 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
793 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000794 }
mpagenko2f2f2362021-06-07 08:25:22 +0000795 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000796 }
797 pImageResp := &loResponse
798 return pImageResp, nil
799 }
800 return nil, errors.New("invalid image activation parameters")
mpagenko83144272021-04-27 10:06:22 +0000801}
802
khenaidoo7d3c5582021-08-11 18:09:44 -0400803// CommitOnuImage enforces the commitment of the image for the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000804// precondition: image activated and not yet committed
khenaidoo7d3c5582021-08-11 18:09:44 -0400805func (oo *OpenONUAC) CommitOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000806 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
807 loResponse := voltha.DeviceImageResponse{}
808 imageIdentifier := (*in).Version
809 //let the deviceHandler find the adequate way of requesting the image activation
810 for _, pCommonID := range (*in).DeviceId {
811 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000812 loDeviceImageState := voltha.DeviceImageState{}
813 loDeviceImageState.DeviceId = loDeviceID
814 loImageState := voltha.ImageState{}
815 loDeviceImageState.ImageState = &loImageState
816 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000817 //compared to download procedure the vendorID (from device) is secondary here
818 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
819 // start image activation activity for each possible device
820 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
821 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
822 logger.Debugw(ctx, "onu image commitment requested", log.Fields{
823 "image-id": imageIdentifier, "device-id": loDeviceID})
824 //onu commitment handling called in background without immediate error evaluation here
825 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
826 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000827 if pImageStates, err := handler.onuSwCommitRequest(ctx, imageIdentifier); err != nil {
mpagenko38662d02021-08-11 09:45:19 +0000828 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
829 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //can be multiple reasons here
mpagenko183647c2021-06-08 15:25:04 +0000830 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
831 } else {
832 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
833 loDeviceImageState.ImageState.Reason = pImageStates.Reason
834 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
835 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000836 } else {
837 //cannot start SW commitment for requested device
838 logger.Warnw(ctx, "no handler found for image commitment", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000839 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000840 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
841 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000842 }
mpagenko2f2f2362021-06-07 08:25:22 +0000843 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000844 }
845 pImageResp := &loResponse
846 return pImageResp, nil
847 }
848 return nil, errors.New("invalid image commitment parameters")
mpagenko83144272021-04-27 10:06:22 +0000849}
850
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000851// Adapter interface required methods ################ end #########
852// #################################################################
khenaidoo7d3c5582021-08-11 18:09:44 -0400853
854/*
855 *
856 * ONU inter adapter service
857 *
858 */
859
860// OnuIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400861func (oo *OpenONUAC) OnuIndication(ctx context.Context, onuInd *ia.OnuIndicationMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400862 logger.Debugw(ctx, "onu-indication", log.Fields{"onu-indication": onuInd})
863
864 if onuInd == nil || onuInd.OnuIndication == nil {
865 return nil, fmt.Errorf("invalid-onu-indication-%v", onuInd)
866 }
867
868 onuIndication := onuInd.OnuIndication
869 onuOperstate := onuIndication.GetOperState()
870 waitForDhInstPresent := false
871 if onuOperstate == "up" {
872 //Race condition (relevant in BBSIM-environment only): Due to unsynchronized processing of olt-adapter and rw_core,
873 //ONU_IND_REQUEST msg by olt-adapter could arrive a little bit earlier than rw_core was able to announce the corresponding
874 //ONU by RPC of Adopt_device(). Therefore it could be necessary to wait with processing of ONU_IND_REQUEST until call of
875 //Adopt_device() arrived and DeviceHandler instance was created
876 waitForDhInstPresent = true
877 }
878 if handler := oo.getDeviceHandler(ctx, onuInd.DeviceId, waitForDhInstPresent); handler != nil {
879 logger.Infow(ctx, "onu-ind-request", log.Fields{"device-id": onuInd.DeviceId,
880 "OnuId": onuIndication.GetOnuId(),
881 "AdminState": onuIndication.GetAdminState(), "OperState": onuOperstate,
882 "SNR": onuIndication.GetSerialNumber()})
883
884 if onuOperstate == "up" {
885 if err := handler.createInterface(ctx, onuIndication); err != nil {
886 return nil, err
887 }
888 return &empty.Empty{}, nil
889 } else if (onuOperstate == "down") || (onuOperstate == "unreachable") {
890 return nil, handler.updateInterface(ctx, onuIndication)
891 } else {
892 logger.Errorw(ctx, "unknown-onu-ind-request operState", log.Fields{"OnuId": onuIndication.GetOnuId()})
893 return nil, fmt.Errorf("invalidOperState: %s, %s", onuOperstate, onuInd.DeviceId)
894 }
895 }
896 logger.Warnw(ctx, "no handler found for received onu-ind-request", log.Fields{
897 "msgToDeviceId": onuInd.DeviceId})
898 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", onuInd.DeviceId))
899}
900
901// OmciIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400902func (oo *OpenONUAC) OmciIndication(ctx context.Context, msg *ia.OmciMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400903 logger.Debugw(ctx, "omci-response", log.Fields{"parent-device-id": msg.ParentDeviceId, "child-device-id": msg.ChildDeviceId})
904
905 if handler := oo.getDeviceHandler(ctx, msg.ChildDeviceId, false); handler != nil {
906 if err := handler.handleOMCIIndication(log.WithSpanFromContext(context.Background(), ctx), msg); err != nil {
907 return nil, err
908 }
909 return &empty.Empty{}, nil
910 }
911 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", msg.ChildDeviceId))
912}
913
914// DownloadTechProfile is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400915func (oo *OpenONUAC) DownloadTechProfile(ctx context.Context, tProfile *ia.TechProfileDownloadMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400916 logger.Debugw(ctx, "download-tech-profile", log.Fields{"uni-id": tProfile.UniId})
917
918 if handler := oo.getDeviceHandler(ctx, tProfile.DeviceId, false); handler != nil {
919 if err := handler.handleTechProfileDownloadRequest(log.WithSpanFromContext(context.Background(), ctx), tProfile); err != nil {
920 return nil, err
921 }
922 return &empty.Empty{}, nil
923 }
924 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tProfile.DeviceId))
925}
926
927// DeleteGemPort is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400928func (oo *OpenONUAC) DeleteGemPort(ctx context.Context, gPort *ia.DeleteGemPortMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400929 logger.Debugw(ctx, "delete-gem-port", log.Fields{"device-id": gPort.DeviceId, "uni-id": gPort.UniId})
930
931 if handler := oo.getDeviceHandler(ctx, gPort.DeviceId, false); handler != nil {
932 if err := handler.handleDeleteGemPortRequest(log.WithSpanFromContext(context.Background(), ctx), gPort); err != nil {
933 return nil, err
934 }
935 return &empty.Empty{}, nil
936 }
937 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", gPort.DeviceId))
938}
939
940// DeleteTCont is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400941func (oo *OpenONUAC) DeleteTCont(ctx context.Context, tConf *ia.DeleteTcontMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400942 logger.Debugw(ctx, "delete-tcont", log.Fields{"tconf": tConf})
943
944 if handler := oo.getDeviceHandler(ctx, tConf.DeviceId, false); handler != nil {
945 if err := handler.handleDeleteTcontRequest(log.WithSpanFromContext(context.Background(), ctx), tConf); err != nil {
946 return nil, err
947 }
948 return &empty.Empty{}, nil
949 }
950 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tConf.DeviceId))
951}
952
953/*
954 * Parent GRPC clients
955 */
956
957func (oo *OpenONUAC) setupParentInterAdapterClient(ctx context.Context, endpoint string) error {
958 logger.Infow(ctx, "setting-parent-adapter-connection", log.Fields{"parent-endpoint": endpoint})
959 oo.lockParentAdapterClients.Lock()
960 defer oo.lockParentAdapterClients.Unlock()
961 if _, ok := oo.parentAdapterClients[endpoint]; ok {
962 return nil
963 }
964
965 childClient, err := vgrpc.NewClient(endpoint,
966 oo.oltAdapterRestarted,
967 vgrpc.ActivityCheck(true))
968
969 if err != nil {
970 return err
971 }
972
973 oo.parentAdapterClients[endpoint] = childClient
974
975 go oo.parentAdapterClients[endpoint].Start(log.WithSpanFromContext(context.TODO(), ctx), setAndTestAdapterServiceHandler)
976
977 // Wait until we have a connection to the child adapter.
978 // Unlimited retries or until context expires
979 subCtx := log.WithSpanFromContext(context.TODO(), ctx)
980 backoff := vgrpc.NewBackoff(oo.config.MinBackoffRetryDelay, oo.config.MaxBackoffRetryDelay, 0)
981 for {
982 client, err := oo.parentAdapterClients[endpoint].GetOltInterAdapterServiceClient()
983 if err == nil && client != nil {
984 logger.Infow(subCtx, "connected-to-parent-adapter", log.Fields{"parent-endpoint": endpoint})
985 break
986 }
987 logger.Warnw(subCtx, "connection-to-parent-adapter-not-ready", log.Fields{"error": err, "parent-endpoint": endpoint})
988 // Backoff
989 if err = backoff.Backoff(subCtx); err != nil {
990 logger.Errorw(subCtx, "received-error-on-backoff", log.Fields{"error": err, "parent-endpoint": endpoint})
991 break
992 }
993 }
994 return nil
995}
996
khenaidoo42dcdfd2021-10-19 17:34:12 -0400997func (oo *OpenONUAC) getParentAdapterServiceClient(endpoint string) (olt_inter_adapter_service.OltInterAdapterServiceClient, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400998 // First check from cache
999 oo.lockParentAdapterClients.RLock()
1000 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1001 oo.lockParentAdapterClients.RUnlock()
1002 return pgClient.GetOltInterAdapterServiceClient()
1003 }
1004 oo.lockParentAdapterClients.RUnlock()
1005
1006 // Set the parent connection - can occur on restarts
1007 ctx, cancel := context.WithTimeout(context.Background(), oo.config.RPCTimeout)
1008 err := oo.setupParentInterAdapterClient(ctx, endpoint)
1009 cancel()
1010 if err != nil {
1011 return nil, err
1012 }
1013
1014 // Get the parent client now
1015 oo.lockParentAdapterClients.RLock()
1016 defer oo.lockParentAdapterClients.RUnlock()
1017 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1018 return pgClient.GetOltInterAdapterServiceClient()
1019 }
1020
1021 return nil, fmt.Errorf("no-client-for-endpoint-%s", endpoint)
1022}
1023
1024// TODO: Any action the adapter needs to do following an olt adapter restart?
1025func (oo *OpenONUAC) oltAdapterRestarted(ctx context.Context, endPoint string) error {
1026 logger.Errorw(ctx, "olt-adapter-restarted", log.Fields{"endpoint": endPoint})
1027 return nil
1028}
1029
1030// setAndTestAdapterServiceHandler is used to test whether the remote gRPC service is up
1031func setAndTestAdapterServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
khenaidoo42dcdfd2021-10-19 17:34:12 -04001032 svc := olt_inter_adapter_service.NewOltInterAdapterServiceClient(conn)
1033 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != health.HealthStatus_HEALTHY {
khenaidoo7d3c5582021-08-11 18:09:44 -04001034 return nil
1035 }
1036 return svc
1037}
1038
1039/*
1040 *
1041 * Unimplemented APIs
1042 *
1043 */
1044
1045//GetOfpDeviceInfo returns OFP information for the given device. Method not implemented as per [VOL-3202].
1046// OF port info is now to be delivered within UniPort create cmp changes in onu_uni_port.go::CreateVolthaPort()
1047//
khenaidoo42dcdfd2021-10-19 17:34:12 -04001048func (oo *OpenONUAC) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001049 return nil, errors.New("unImplemented")
1050}
1051
1052//SimulateAlarm is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001053func (oo *OpenONUAC) SimulateAlarm(context.Context, *ca.SimulateAlarmMessage) (*common.OperationResp, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001054 return nil, errors.New("unImplemented")
1055}
1056
1057//SetExtValue is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001058func (oo *OpenONUAC) SetExtValue(context.Context, *ca.SetExtValueMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001059 return nil, errors.New("unImplemented")
1060}
1061
1062//SetSingleValue is unimplemented
1063func (oo *OpenONUAC) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
1064 return nil, errors.New("unImplemented")
1065}
1066
1067//StartOmciTest not implemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001068func (oo *OpenONUAC) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001069 return nil, errors.New("unImplemented")
1070}
1071
1072//SuppressEvent unimplemented
1073func (oo *OpenONUAC) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1074 return nil, errors.New("unImplemented")
1075}
1076
1077//UnSuppressEvent unimplemented
1078func (oo *OpenONUAC) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1079 return nil, errors.New("unImplemented")
1080}
1081
1082//GetImageDownloadStatus is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001083func (oo *OpenONUAC) GetImageDownloadStatus(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001084 return nil, errors.New("unImplemented")
1085}
1086
1087//CancelImageDownload is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001088func (oo *OpenONUAC) CancelImageDownload(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001089 return nil, errors.New("unImplemented")
1090}
1091
1092//RevertImageUpdate is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001093func (oo *OpenONUAC) RevertImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001094 return nil, errors.New("unImplemented")
1095}
1096
1097// UpdateFlowsBulk is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001098func (oo *OpenONUAC) UpdateFlowsBulk(ctx context.Context, flows *ca.BulkFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001099 return nil, errors.New("unImplemented")
1100}
1101
1102//SelfTestDevice unimplented
1103func (oo *OpenONUAC) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
1104 return nil, errors.New("unImplemented")
1105}
1106
1107//SendPacketOut sends packet out to the device
khenaidoo42dcdfd2021-10-19 17:34:12 -04001108func (oo *OpenONUAC) SendPacketOut(ctx context.Context, packet *ca.PacketOut) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001109 return nil, errors.New("unImplemented")
1110}
1111
1112// EnablePort to Enable PON/NNI interface - seems not to be used/required according to python code
1113func (oo *OpenONUAC) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1114 return nil, errors.New("unImplemented")
1115}
1116
1117// DisablePort to Disable pon/nni interface - seems not to be used/required according to python code
1118func (oo *OpenONUAC) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1119 return nil, errors.New("unImplemented")
1120}
1121
1122// GetExtValue - unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001123func (oo *OpenONUAC) GetExtValue(ctx context.Context, extInfo *ca.GetExtValueMessage) (*extension.ReturnValues, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001124 return nil, errors.New("unImplemented")
1125}
1126
1127// ChildDeviceLost - unimplemented
1128func (oo *OpenONUAC) ChildDeviceLost(ctx context.Context, childDevice *voltha.Device) (*empty.Empty, error) {
1129 return nil, errors.New("unImplemented")
1130}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001131
1132// GetSupportedFsms - TODO: add comment
1133func (oo *OpenONUAC) GetSupportedFsms() *cmn.OmciDeviceFsms {
1134 return oo.pSupportedFsms
1135}
1136
1137// LockMutexMibTemplateGenerated - TODO: add comment
1138func (oo *OpenONUAC) LockMutexMibTemplateGenerated() {
1139 oo.mutexMibTemplateGenerated.Lock()
1140}
1141
1142// UnlockMutexMibTemplateGenerated - TODO: add comment
1143func (oo *OpenONUAC) UnlockMutexMibTemplateGenerated() {
1144 oo.mutexMibTemplateGenerated.Unlock()
1145}
1146
1147// GetMibTemplatesGenerated - TODO: add comment
1148func (oo *OpenONUAC) GetMibTemplatesGenerated(mibTemplatePath string) (value bool, exist bool) {
1149 value, exist = oo.mibTemplatesGenerated[mibTemplatePath]
1150 return value, exist
1151}
1152
1153// SetMibTemplatesGenerated - TODO: add comment
1154func (oo *OpenONUAC) SetMibTemplatesGenerated(mibTemplatePath string, value bool) {
1155 oo.mibTemplatesGenerated[mibTemplatePath] = value
1156}
1157
1158// RLockMutexDeviceHandlersMap - TODO: add comment
1159func (oo *OpenONUAC) RLockMutexDeviceHandlersMap() {
1160 oo.mutexDeviceHandlersMap.RLock()
1161}
1162
1163// RUnlockMutexDeviceHandlersMap - TODO: add comment
1164func (oo *OpenONUAC) RUnlockMutexDeviceHandlersMap() {
1165 oo.mutexDeviceHandlersMap.RUnlock()
1166}
1167
1168// GetDeviceHandler - TODO: add comment
1169func (oo *OpenONUAC) GetDeviceHandler(deviceID string) (value cmn.IdeviceHandler, exist bool) {
1170 value, exist = oo.deviceHandlers[deviceID]
1171 return value, exist
1172}