blob: 230b7c5472e96c13244e4250dff910fb5db5c16e [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
Holger Hildebrandt60652202021-11-02 11:09:36 +000027 "github.com/opencord/voltha-lib-go/v7/pkg/db"
khenaidoo7d3c5582021-08-11 18:09:44 -040028 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
dbainbri4d3a0dc2020-12-02 00:33:42 +000029
khenaidoo7d3c5582021-08-11 18:09:44 -040030 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
31 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoo42dcdfd2021-10-19 17:34:12 -040032 "github.com/opencord/voltha-protos/v5/go/health"
33 "github.com/opencord/voltha-protos/v5/go/olt_inter_adapter_service"
khenaidoo7d3c5582021-08-11 18:09:44 -040034 "google.golang.org/grpc"
35
36 "github.com/golang/protobuf/ptypes/empty"
37 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
38 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
39 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo42dcdfd2021-10-19 17:34:12 -040040 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo7d3c5582021-08-11 18:09:44 -040041 "github.com/opencord/voltha-protos/v5/go/extension"
khenaidoo42dcdfd2021-10-19 17:34:12 -040042 ia "github.com/opencord/voltha-protos/v5/go/inter_adapter"
43 "github.com/opencord/voltha-protos/v5/go/omci"
khenaidoo7d3c5582021-08-11 18:09:44 -040044 "github.com/opencord/voltha-protos/v5/go/voltha"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000045
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000046 cmn "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/common"
Matteo Scandolo761f7512020-11-23 15:52:40 -080047 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config"
Holger Hildebrandt60652202021-11-02 11:09:36 +000048 pmmgr "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/pmmgr"
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000049 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/swupg"
50 uniprt "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/uniprt"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000051)
52
Holger Hildebrandt60652202021-11-02 11:09:36 +000053var onuKvStorePathPrefixes = []string{cmn.CBasePathOnuKVStore, pmmgr.CPmKvStorePrefixBase}
54
Holger Hildebrandtfa074992020-03-27 15:42:06 +000055//OpenONUAC structure holds the ONU core information
56type OpenONUAC struct {
Himani Chawla6d2ae152020-09-02 13:11:20 +053057 deviceHandlers map[string]*deviceHandler
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000058 deviceHandlersCreateChan map[string]chan bool //channels for deviceHandler create events
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000059 mutexDeviceHandlersMap sync.RWMutex
khenaidoo7d3c5582021-08-11 18:09:44 -040060 coreClient *vgrpc.Client
61 parentAdapterClients map[string]*vgrpc.Client
62 lockParentAdapterClients sync.RWMutex
Himani Chawlac07fda02020-12-09 16:21:21 +053063 eventProxy eventif.EventProxy
mpagenkoaf801632020-07-03 10:00:42 +000064 kvClient kvstore.Client
Matteo Scandolof1f39a72020-11-24 12:08:11 -080065 cm *conf.ConfigManager
Holger Hildebrandtfa074992020-03-27 15:42:06 +000066 config *config.AdapterFlags
67 numOnus int
Matteo Scandolo127c59d2021-01-28 11:31:18 -080068 KVStoreAddress string
Holger Hildebrandtfa074992020-03-27 15:42:06 +000069 KVStoreType string
mpagenkoaf801632020-07-03 10:00:42 +000070 KVStoreTimeout time.Duration
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000071 mibTemplatesGenerated map[string]bool
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000072 mutexMibTemplateGenerated sync.RWMutex
Holger Hildebrandtfa074992020-03-27 15:42:06 +000073 exitChannel chan int
74 HeartbeatCheckInterval time.Duration
75 HeartbeatFailReportInterval time.Duration
mpagenkodff5dda2020-08-28 11:52:01 +000076 AcceptIncrementalEvto bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000077 //GrpcTimeoutInterval time.Duration
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000078 pSupportedFsms *cmn.OmciDeviceFsms
Himani Chawlad96df182020-09-28 11:12:02 +053079 maxTimeoutInterAdapterComm time.Duration
Holger Hildebrandt38985dc2021-02-18 16:25:20 +000080 maxTimeoutReconciling time.Duration
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +000081 pDownloadManager *swupg.AdapterDownloadManager
82 pFileManager *swupg.FileDownloadManager //let coexist 'old and new' DownloadManager as long as 'old' does not get obsolete
83 MetricsEnabled bool
Holger Hildebrandte3677f12021-02-05 14:50:56 +000084 mibAuditInterval time.Duration
Girish Gowdra0b235842021-03-09 13:06:46 -080085 omciTimeout int // in seconds
Himani Chawla075f1642021-03-15 19:23:24 +053086 alarmAuditInterval time.Duration
mpagenkoc26d4c02021-05-06 14:27:57 +000087 dlToOnuTimeout4M time.Duration
khenaidoo7d3c5582021-08-11 18:09:44 -040088 rpcTimeout time.Duration
Girish Gowdrae95687a2021-09-08 16:30:58 -070089 maxConcurrentFlowsPerUni int
Holger Hildebrandtfa074992020-03-27 15:42:06 +000090}
91
92//NewOpenONUAC returns a new instance of OpenONU_AC
khenaidoo7d3c5582021-08-11 18:09:44 -040093func NewOpenONUAC(ctx context.Context, coreClient *vgrpc.Client, eventProxy eventif.EventProxy,
94 kvClient kvstore.Client, cfg *config.AdapterFlags, cm *conf.ConfigManager) *OpenONUAC {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000095 var openOnuAc OpenONUAC
96 openOnuAc.exitChannel = make(chan int, 1)
Himani Chawla6d2ae152020-09-02 13:11:20 +053097 openOnuAc.deviceHandlers = make(map[string]*deviceHandler)
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000098 openOnuAc.deviceHandlersCreateChan = make(map[string]chan bool)
khenaidoo7d3c5582021-08-11 18:09:44 -040099 openOnuAc.parentAdapterClients = make(map[string]*vgrpc.Client)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000100 openOnuAc.mutexDeviceHandlersMap = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000101 openOnuAc.config = cfg
Matteo Scandolof1f39a72020-11-24 12:08:11 -0800102 openOnuAc.cm = cm
khenaidoo7d3c5582021-08-11 18:09:44 -0400103 openOnuAc.coreClient = coreClient
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000104 openOnuAc.numOnus = cfg.OnuNumber
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000105 openOnuAc.eventProxy = eventProxy
mpagenkoaf801632020-07-03 10:00:42 +0000106 openOnuAc.kvClient = kvClient
Matteo Scandolo127c59d2021-01-28 11:31:18 -0800107 openOnuAc.KVStoreAddress = cfg.KVStoreAddress
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000108 openOnuAc.KVStoreType = cfg.KVStoreType
mpagenkoaf801632020-07-03 10:00:42 +0000109 openOnuAc.KVStoreTimeout = cfg.KVStoreTimeout
Holger Hildebrandt61b24d02020-11-16 13:36:40 +0000110 openOnuAc.mibTemplatesGenerated = make(map[string]bool)
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000111 openOnuAc.mutexMibTemplateGenerated = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000112 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
113 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
mpagenkodff5dda2020-08-28 11:52:01 +0000114 openOnuAc.AcceptIncrementalEvto = cfg.AccIncrEvto
Himani Chawlad96df182020-09-28 11:12:02 +0530115 openOnuAc.maxTimeoutInterAdapterComm = cfg.MaxTimeoutInterAdapterComm
Holger Hildebrandt38985dc2021-02-18 16:25:20 +0000116 openOnuAc.maxTimeoutReconciling = cfg.MaxTimeoutReconciling
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000117 //openOnuAc.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000118 openOnuAc.MetricsEnabled = cfg.MetricsEnabled
Holger Hildebrandte3677f12021-02-05 14:50:56 +0000119 openOnuAc.mibAuditInterval = cfg.MibAuditInterval
Girish Gowdra0b235842021-03-09 13:06:46 -0800120 // since consumers of OMCI timeout value everywhere in code is in "int seconds", do this useful conversion
121 openOnuAc.omciTimeout = int(cfg.OmciTimeout.Seconds())
Himani Chawla075f1642021-03-15 19:23:24 +0530122 openOnuAc.alarmAuditInterval = cfg.AlarmAuditInterval
mpagenkoc26d4c02021-05-06 14:27:57 +0000123 openOnuAc.dlToOnuTimeout4M = cfg.DownloadToOnuTimeout4MB
khenaidoo7d3c5582021-08-11 18:09:44 -0400124 openOnuAc.rpcTimeout = cfg.RPCTimeout
Girish Gowdrae95687a2021-09-08 16:30:58 -0700125 openOnuAc.maxConcurrentFlowsPerUni = cfg.MaxConcurrentFlowsPerUni
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000126
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000127 openOnuAc.pSupportedFsms = &cmn.OmciDeviceFsms{
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000128 "mib-synchronizer": {
129 //mibSyncFsm, // Implements the MIB synchronization state machine
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000130 DatabaseClass: mibDbVolatileDictImpl, // Implements volatile ME MIB database
Himani Chawla4d908332020-08-31 12:30:20 +0530131 //true, // Advertise events on OpenOMCI event bus
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000132 AuditInterval: openOnuAc.mibAuditInterval, // Time to wait between MIB audits. 0 to disable audits.
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000133 // map[string]func() error{
134 // "mib-upload": onuDeviceEntry.MibUploadTask,
135 // "mib-template": onuDeviceEntry.MibTemplateTask,
136 // "get-mds": onuDeviceEntry.GetMdsTask,
137 // "mib-audit": onuDeviceEntry.GetMdsTask,
138 // "mib-resync": onuDeviceEntry.MibResyncTask,
139 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
140 // },
141 },
142 }
143
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000144 openOnuAc.pDownloadManager = swupg.NewAdapterDownloadManager(ctx)
145 openOnuAc.pFileManager = swupg.NewFileDownloadManager(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +0000146 openOnuAc.pFileManager.SetDownloadTimeout(ctx, cfg.DownloadToAdapterTimeout)
mpagenkoc8bba412021-01-15 15:38:44 +0000147
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000148 return &openOnuAc
149}
150
151//Start starts (logs) the adapter
152func (oo *OpenONUAC) Start(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000153 logger.Info(ctx, "starting-openonu-adapter")
mpagenkoc8bba412021-01-15 15:38:44 +0000154
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000155 return nil
156}
157
Himani Chawla6d2ae152020-09-02 13:11:20 +0530158/*
159//stop terminates the session
160func (oo *OpenONUAC) stop(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000161 logger.Info(ctx,"stopping-device-manager")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000162 oo.exitChannel <- 1
dbainbri4d3a0dc2020-12-02 00:33:42 +0000163 logger.Info(ctx,"device-manager-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000164 return nil
165}
Himani Chawla6d2ae152020-09-02 13:11:20 +0530166*/
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000167
Himani Chawla6d2ae152020-09-02 13:11:20 +0530168func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *deviceHandler) {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000169 oo.mutexDeviceHandlersMap.Lock()
170 defer oo.mutexDeviceHandlersMap.Unlock()
171 if _, exist := oo.deviceHandlers[agent.DeviceID]; !exist {
172 oo.deviceHandlers[agent.DeviceID] = agent
173 oo.deviceHandlers[agent.DeviceID].start(ctx)
174 if _, exist := oo.deviceHandlersCreateChan[agent.DeviceID]; exist {
175 logger.Debugw(ctx, "deviceHandler created - trigger processing of pending ONU_IND_REQUEST", log.Fields{"device-id": agent.DeviceID})
176 oo.deviceHandlersCreateChan[agent.DeviceID] <- true
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000177 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000178 }
179}
180
Himani Chawla6d2ae152020-09-02 13:11:20 +0530181func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *deviceHandler) {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000182 oo.mutexDeviceHandlersMap.Lock()
183 defer oo.mutexDeviceHandlersMap.Unlock()
184 delete(oo.deviceHandlers, agent.DeviceID)
185 delete(oo.deviceHandlersCreateChan, agent.DeviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000186}
187
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000188//getDeviceHandler gets the ONU deviceHandler and may wait until it is created
dbainbri4d3a0dc2020-12-02 00:33:42 +0000189func (oo *OpenONUAC) getDeviceHandler(ctx context.Context, deviceID string, aWait bool) *deviceHandler {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000190 oo.mutexDeviceHandlersMap.Lock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000191 agent, ok := oo.deviceHandlers[deviceID]
192 if aWait && !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000193 logger.Infow(ctx, "Race condition: deviceHandler not present - wait for creation or timeout",
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000194 log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000195 if _, exist := oo.deviceHandlersCreateChan[deviceID]; !exist {
196 oo.deviceHandlersCreateChan[deviceID] = make(chan bool, 1)
197 }
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800198 deviceCreateChan := oo.deviceHandlersCreateChan[deviceID]
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000199 //keep the read sema short to allow for subsequent write
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000200 oo.mutexDeviceHandlersMap.Unlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000201 // based on concurrent processing the deviceHandler creation may not yet be finished at his point
202 // so it might be needed to wait here for that event with some timeout
203 select {
204 case <-time.After(1 * time.Second): //timer may be discussed ...
dbainbri4d3a0dc2020-12-02 00:33:42 +0000205 logger.Warnw(ctx, "No valid deviceHandler created after max WaitTime", log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000206 return nil
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800207 case <-deviceCreateChan:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000208 logger.Debugw(ctx, "deviceHandler is ready now - continue", log.Fields{"device-id": deviceID})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000209 oo.mutexDeviceHandlersMap.RLock()
210 defer oo.mutexDeviceHandlersMap.RUnlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000211 return oo.deviceHandlers[deviceID]
212 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000213 }
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000214 oo.mutexDeviceHandlersMap.Unlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000215 return agent
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000216}
217
khenaidoo7d3c5582021-08-11 18:09:44 -0400218// GetHealthStatus is used as a service readiness validation as a grpc connection
khenaidoo42dcdfd2021-10-19 17:34:12 -0400219func (oo *OpenONUAC) GetHealthStatus(ctx context.Context, empty *empty.Empty) (*health.HealthStatus, error) {
220 return &health.HealthStatus{State: health.HealthStatus_HEALTHY}, nil
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000221}
222
khenaidoo7d3c5582021-08-11 18:09:44 -0400223// AdoptDevice creates a new device handler if not present already and then adopts the device
224func (oo *OpenONUAC) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000225 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000226 logger.Warn(ctx, "voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400227 return nil, errors.New("nil-device")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000228 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000229 logger.Infow(ctx, "adopt-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530230 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000231 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400232 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000233 oo.addDeviceHandlerToMap(ctx, handler)
khenaidoo7d3c5582021-08-11 18:09:44 -0400234
235 // Setup the grpc communication with the parent adapter
236 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
237 // TODO: Cleanup on failure needed
238 return nil, err
239 }
240
241 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), device)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000242 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400243 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000244}
245
khenaidoo7d3c5582021-08-11 18:09:44 -0400246//ReconcileDevice is called once when the adapter needs to re-create device - usually on core restart
247func (oo *OpenONUAC) ReconcileDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000248 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000249 logger.Warn(ctx, "reconcile-device-voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400250 return nil, errors.New("nil-device")
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000251 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000252 logger.Infow(ctx, "reconcile-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530253 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000254 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400255 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000256 oo.addDeviceHandlerToMap(ctx, handler)
257 handler.device = device
khenaidoo42dcdfd2021-10-19 17:34:12 -0400258 if err := handler.updateDeviceStateInCore(log.WithSpanFromContext(context.Background(), ctx), &ca.DeviceStateFilter{
khenaidoo7d3c5582021-08-11 18:09:44 -0400259 DeviceId: device.Id,
260 OperStatus: voltha.OperStatus_RECONCILING,
261 ConnStatus: device.ConnectStatus,
262 }); err != nil {
263 return nil, fmt.Errorf("not able to update device state to reconciling. Err : %s", err.Error())
Maninderb5187552021-03-23 22:23:42 +0530264 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400265 // Setup the grpc communication with the parent adapter
266 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
267 // TODO: Cleanup on failure needed
268 return nil, err
269 }
270
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000271 handler.StartReconciling(log.WithSpanFromContext(context.Background(), ctx), false)
khenaidoo7d3c5582021-08-11 18:09:44 -0400272 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), handler.device)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000273 // reconcilement will be continued after onu-device entry is added
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000274 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400275 return nil, fmt.Errorf(fmt.Sprintf("device-already-reconciled-or-active-%s", device.Id))
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000276 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400277 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000278}
279
khenaidoo7d3c5582021-08-11 18:09:44 -0400280//DisableDevice disables the given device
281func (oo *OpenONUAC) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000282 logger.Infow(ctx, "disable-device", log.Fields{"device-id": device.Id})
283 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400284 go handler.disableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
285 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300286 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000287 logger.Warnw(ctx, "no handler found for device-disable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400288 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000289}
290
khenaidoo7d3c5582021-08-11 18:09:44 -0400291//ReEnableDevice enables the onu device after disable
292func (oo *OpenONUAC) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000293 logger.Infow(ctx, "reenable-device", log.Fields{"device-id": device.Id})
294 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400295 go handler.reEnableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
296 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300297 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000298 logger.Warnw(ctx, "no handler found for device-reenable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400299 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000300}
301
khenaidoo7d3c5582021-08-11 18:09:44 -0400302//RebootDevice reboots the given device
303func (oo *OpenONUAC) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000304 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
305 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400306 go handler.rebootDevice(log.WithSpanFromContext(context.Background(), ctx), true, device) //reboot request with device checking
307 return &empty.Empty{}, nil
ozgecanetsiae11479f2020-07-06 09:44:47 +0300308 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000309 logger.Warnw(ctx, "no handler found for device-reboot", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400310 return nil, fmt.Errorf("handler-not-found-for-device: %s", device.Id)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000311}
312
khenaidoo7d3c5582021-08-11 18:09:44 -0400313// DeleteDevice deletes the given device
314func (oo *OpenONUAC) DeleteDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
315 nctx := log.WithSpanFromContext(context.Background(), ctx)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000316
khenaidoo7d3c5582021-08-11 18:09:44 -0400317 logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id, "SerialNumber": device.SerialNumber, "ctx": ctx, "nctx": nctx})
dbainbri4d3a0dc2020-12-02 00:33:42 +0000318 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
Girish Gowdra0e533642021-03-02 22:02:51 -0800319 var errorsList []error
Holger Hildebrandtff05b682021-03-16 15:02:05 +0000320
Holger Hildebrandt60652202021-11-02 11:09:36 +0000321 handler.StopReconciling(ctx, false)
322
Holger Hildebrandtff05b682021-03-16 15:02:05 +0000323 handler.mutexDeletionInProgressFlag.Lock()
324 handler.deletionInProgress = true
325 handler.mutexDeletionInProgressFlag.Unlock()
326
Holger Hildebrandt60652202021-11-02 11:09:36 +0000327 if err := handler.resetFsms(ctx, true); err != nil {
328 errorsList = append(errorsList, err)
329 }
Girish Gowdra0e533642021-03-02 22:02:51 -0800330 if err := handler.deleteDevicePersistencyData(ctx); err != nil {
331 errorsList = append(errorsList, err)
332 }
Holger Hildebrandt60652202021-11-02 11:09:36 +0000333 // Clear PM data on the KV store
334 if handler.pOnuMetricsMgr != nil {
335 if err := handler.pOnuMetricsMgr.ClearAllPmData(ctx); err != nil {
336 errorsList = append(errorsList, err)
337 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700338 }
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 }
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000345 //don't leave any garbage - even in error case
346 oo.deleteDeviceHandlerToMap(handler)
Holger Hildebrandt60652202021-11-02 11:09:36 +0000347
Girish Gowdra0e533642021-03-02 22:02:51 -0800348 if len(errorsList) > 0 {
349 logger.Errorw(ctx, "one-or-more-error-during-device-delete", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400350 return nil, fmt.Errorf("one-or-more-error-during-device-delete, errors:%v", errorsList)
Girish Gowdra0e533642021-03-02 22:02:51 -0800351 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400352 return &empty.Empty{}, nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000353 }
Holger Hildebrandt60652202021-11-02 11:09:36 +0000354 logger.Infow(ctx, "no handler found for device-deletion - trying to delete remaining data in the kv-store ", log.Fields{"device-id": device.Id})
355
356 // delete ONU specific avcfg and pm data in kv store
357 for i := range onuKvStorePathPrefixes {
358 baseKvStorePath := fmt.Sprintf(onuKvStorePathPrefixes[i], oo.cm.Backend.PathPrefix)
359 logger.Debugw(ctx, "SetKVStoreBackend", log.Fields{"IpTarget": oo.KVStoreAddress, "BasePathKvStore": baseKvStorePath})
360 kvbackend := &db.Backend{
361 Client: oo.kvClient,
362 StoreType: oo.KVStoreType,
363 Address: oo.KVStoreAddress,
364 Timeout: oo.KVStoreTimeout,
365 PathPrefix: baseKvStorePath}
366
367 if kvbackend == nil {
368 logger.Errorw(ctx, "Can't access onuKVStore - no backend connection to service", log.Fields{"service": baseKvStorePath, "device-id": device.Id})
369 return nil, fmt.Errorf("can-not-access-onuKVStore-no-backend-connection-to-service")
370 }
371 err := kvbackend.Delete(ctx, device.Id)
372 if err != nil {
373 logger.Errorw(ctx, "unable to delete in KVstore", log.Fields{"service": baseKvStorePath, "device-id": device.Id, "err": err})
374 return nil, fmt.Errorf("unable-to-delete-in-KVstore")
375 }
376 }
377 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000378}
379
khenaidoo7d3c5582021-08-11 18:09:44 -0400380//UpdateFlowsIncrementally updates (add/remove) the flows on a given device
khenaidoo42dcdfd2021-10-19 17:34:12 -0400381func (oo *OpenONUAC) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ca.IncrementalFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400382 logger.Infow(ctx, "update-flows-incrementally", log.Fields{"device-id": incrFlows.Device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000383
mpagenkofc4f56e2020-11-04 17:17:49 +0000384 //flow config is relayed to handler even if the device might be in some 'inactive' state
385 // let the handler or related FSM's decide, what to do with the modified flow state info
386 // 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 +0000387
388 // For now, there is no support for group changes (as in the actual Py-adapter code)
mpagenkofc4f56e2020-11-04 17:17:49 +0000389 // but processing is continued for flowUpdate possibly also set in the request
khenaidoo7d3c5582021-08-11 18:09:44 -0400390 if incrFlows.Groups.ToAdd != nil && incrFlows.Groups.ToAdd.Items != nil {
391 logger.Warnw(ctx, "Update-flow-incr: group add not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000392 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400393 if incrFlows.Groups.ToRemove != nil && incrFlows.Groups.ToRemove.Items != nil {
394 logger.Warnw(ctx, "Update-flow-incr: group remove not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000395 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400396 if incrFlows.Groups.ToUpdate != nil && incrFlows.Groups.ToUpdate.Items != nil {
397 logger.Warnw(ctx, "Update-flow-incr: group update not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000398 }
399
khenaidoo7d3c5582021-08-11 18:09:44 -0400400 if handler := oo.getDeviceHandler(ctx, incrFlows.Device.Id, false); handler != nil {
401 if err := handler.FlowUpdateIncremental(log.WithSpanFromContext(context.Background(), ctx), incrFlows.Flows, incrFlows.Groups, incrFlows.FlowMetadata); err != nil {
402 return nil, err
403 }
404 return &empty.Empty{}, nil
mpagenkodff5dda2020-08-28 11:52:01 +0000405 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400406 logger.Warnw(ctx, "no handler found for incremental flow update", log.Fields{"device-id": incrFlows.Device.Id})
407 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", incrFlows.Device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000408}
409
khenaidoo7d3c5582021-08-11 18:09:44 -0400410//UpdatePmConfig returns PmConfigs nil or error
khenaidoo42dcdfd2021-10-19 17:34:12 -0400411func (oo *OpenONUAC) UpdatePmConfig(ctx context.Context, configs *ca.PmConfigsInfo) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400412 logger.Infow(ctx, "update-pm-config", log.Fields{"device-id": configs.DeviceId})
413 if handler := oo.getDeviceHandler(ctx, configs.DeviceId, false); handler != nil {
414 if err := handler.updatePmConfig(log.WithSpanFromContext(context.Background(), ctx), configs.PmConfigs); err != nil {
415 return nil, err
416 }
417 return &empty.Empty{}, nil
Girish Gowdrae09a6202021-01-12 18:10:59 -0800418 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400419 logger.Warnw(ctx, "no handler found for update-pm-config", log.Fields{"device-id": configs.DeviceId})
420 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", configs.DeviceId))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000421}
422
khenaidoo7d3c5582021-08-11 18:09:44 -0400423//DownloadImage requests downloading some image according to indications as given in request
Andrea Campanella71e546a2021-02-26 11:09:33 +0100424//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400425func (oo *OpenONUAC) DownloadImage(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400426 ctx = log.WithSpanFromContext(context.Background(), ctx)
427 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000428 if !oo.pDownloadManager.ImageExists(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400429 logger.Debugw(ctx, "start image download", log.Fields{"image-description": imageInfo.Image})
mpagenko15ff4a52021-03-02 10:09:20 +0000430 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
431 // the download itself is later done in background
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000432 if err := oo.pDownloadManager.StartDownload(ctx, imageInfo.Image); err != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400433 return nil, err
434 }
435 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000436 }
437 // image already exists
khenaidoo7d3c5582021-08-11 18:09:44 -0400438 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageInfo.Image})
439 return imageInfo.Image, nil
mpagenkoc8bba412021-01-15 15:38:44 +0000440 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400441
442 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000443}
444
mpagenko59862f02021-10-11 08:53:18 +0000445//ActivateImageUpdate requests downloading some Onu Software image to the ONU via OMCI
Andrea Campanella71e546a2021-02-26 11:09:33 +0100446// according to indications as given in request and on success activate the image on the ONU
447//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400448func (oo *OpenONUAC) ActivateImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400449 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000450 if oo.pDownloadManager.ImageLocallyDownloaded(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400451 if handler := oo.getDeviceHandler(ctx, imageInfo.Device.Id, false); handler != nil {
mpagenko15ff4a52021-03-02 10:09:20 +0000452 logger.Debugw(ctx, "image download on omci requested", log.Fields{
khenaidoo7d3c5582021-08-11 18:09:44 -0400453 "image-description": imageInfo.Image, "device-id": imageInfo.Device.Id})
454 if err := handler.doOnuSwUpgrade(ctx, imageInfo.Image, oo.pDownloadManager); err != nil {
455 return nil, err
456 }
457 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000458 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400459 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": imageInfo.Device.Id})
460 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found - device-id: %s", imageInfo.Device.Id))
mpagenko057889c2021-01-21 16:51:58 +0000461 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400462 logger.Debugw(ctx, "image not yet downloaded on activate request", log.Fields{"image-description": imageInfo.Image})
463 return nil, fmt.Errorf(fmt.Sprintf("image-not-yet-downloaded - device-id: %s", imageInfo.Device.Id))
mpagenkoc8bba412021-01-15 15:38:44 +0000464 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400465 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000466}
467
khenaidoo7d3c5582021-08-11 18:09:44 -0400468//GetSingleValue handles the core request to retrieve uni status
469func (oo *OpenONUAC) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
kesavandfdf77632021-01-26 23:40:33 -0500470 logger.Infow(ctx, "Single_get_value_request", log.Fields{"request": request})
471
472 if handler := oo.getDeviceHandler(ctx, request.TargetId, false); handler != nil {
473 switch reqType := request.GetRequest().GetRequest().(type) {
474 case *extension.GetValueRequest_UniInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000475 return handler.GetUniPortStatus(ctx, reqType.UniInfo), nil
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700476 case *extension.GetValueRequest_OnuOpticalInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000477 CommChan := make(chan cmn.Message)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700478 respChan := make(chan extension.SingleGetValueResponse)
479 // Initiate the self test request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000480 if err := handler.pSelfTestHdlr.SelfTestRequestStart(ctx, *request, CommChan, respChan); err != nil {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700481 return &extension.SingleGetValueResponse{
482 Response: &extension.GetValueResponse{
483 Status: extension.GetValueResponse_ERROR,
484 ErrReason: extension.GetValueResponse_INTERNAL_ERROR,
485 },
486 }, err
487 }
488 // The timeout handling is already implemented in omci_self_test_handler module
489 resp := <-respChan
490 return &resp, nil
Himani Chawla43f95ff2021-06-03 00:24:12 +0530491 case *extension.GetValueRequest_OnuInfo:
492 return handler.getOnuOMCICounters(ctx, reqType.OnuInfo), nil
kesavandfdf77632021-01-26 23:40:33 -0500493 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000494 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_UNSUPPORTED), nil
kesavandfdf77632021-01-26 23:40:33 -0500495
496 }
497 }
498 logger.Errorw(ctx, "Single_get_value_request failed ", log.Fields{"request": request})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000499 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_INVALID_DEVICE_ID), nil
mpagenkoc8bba412021-01-15 15:38:44 +0000500}
501
mpagenko83144272021-04-27 10:06:22 +0000502//if update >= 4.3.0
mpagenkoc26d4c02021-05-06 14:27:57 +0000503// Note: already with the implementation of the 'old' download interface problems were detected when the argument name used here is not the same
504// as defined in the adapter interface file. That sounds strange and the effects were strange as well.
505// The reason for that was never finally investigated.
506// To be on the safe side argument names are left here always as defined in iAdapter.go .
mpagenko83144272021-04-27 10:06:22 +0000507
khenaidoo7d3c5582021-08-11 18:09:44 -0400508// DownloadOnuImage downloads (and optionally activates and commits) the indicated ONU image to the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000509// if the image is not yet present on the adapter it has to be automatically downloaded
khenaidoo7d3c5582021-08-11 18:09:44 -0400510func (oo *OpenONUAC) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000511 if request != nil && len((*request).DeviceId) > 0 && (*request).Image.Version != "" {
512 loResponse := voltha.DeviceImageResponse{}
513 imageIdentifier := (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000514 downloadedToAdapter := false
mpagenkoc26d4c02021-05-06 14:27:57 +0000515 firstDevice := true
516 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000517 var onuVolthaDevice *voltha.Device
518 var devErr error
mpagenkoc26d4c02021-05-06 14:27:57 +0000519 for _, pCommonID := range (*request).DeviceId {
mpagenko38662d02021-08-11 09:45:19 +0000520 vendorIDMatch := true
mpagenkoc26d4c02021-05-06 14:27:57 +0000521 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000522 loDeviceImageState := voltha.DeviceImageState{}
523 loDeviceImageState.DeviceId = loDeviceID
524 loImageState := voltha.ImageState{}
525 loDeviceImageState.ImageState = &loImageState
526 loDeviceImageState.ImageState.Version = (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000527
mpagenko59862f02021-10-11 08:53:18 +0000528 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400529 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000530 if handler != nil {
531 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
532 } else {
533 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
534 devErr = errors.New("no handler found for device-id")
khenaidoo7d3c5582021-08-11 18:09:44 -0400535 }
mpagenko59862f02021-10-11 08:53:18 +0000536 if devErr != nil || onuVolthaDevice == nil {
537 logger.Warnw(ctx, "Failed to fetch ONU device for image download",
538 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000539 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
540 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //proto restriction, better option: 'INVALID_DEVICE'
mpagenkoc26d4c02021-05-06 14:27:57 +0000541 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000542 } else {
mpagenko38662d02021-08-11 09:45:19 +0000543 if firstDevice {
544 //start/verify download of the image to the adapter based on first found device only
545 // use the OnuVendor identification from first given device
546 firstDevice = false
547 vendorID = onuVolthaDevice.VendorId
548 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
549 logger.Debugw(ctx, "download request for file", log.Fields{"image-id": imageIdentifier})
550
551 if !oo.pFileManager.ImageExists(ctx, imageIdentifier) {
552 logger.Debugw(ctx, "start image download", log.Fields{"image-description": request})
553 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
554 // the download itself is later done in background
555 if err := oo.pFileManager.StartDownload(ctx, imageIdentifier, (*request).Image.Url); err == nil {
556 downloadedToAdapter = true
557 }
558 //else: treat any error here as 'INVALID_URL' (even though it might as well be some issue on local FS, eg. 'INSUFFICIENT_SPACE')
559 // otherwise a more sophisticated error evaluation is needed
560 } else {
561 // image already exists
562 downloadedToAdapter = true
563 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageIdentifier})
564 // note: If the image (with vendorId+name) has already been downloaded before from some other
565 // valid URL, the current URL is just ignored. If the operators want to ensure that the new URL
566 // is really used, then they first have to use the 'abort' API to remove the existing image!
567 // (abort API can be used also after some successful download to just remove the image from adapter)
568 }
569 } else {
570 //for all following devices verify the matching vendorID
571 if onuVolthaDevice.VendorId != vendorID {
572 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
573 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
574 vendorIDMatch = false
575 }
576 }
577 if downloadedToAdapter && vendorIDMatch {
578 // start the ONU download activity for each possible device
mpagenko59862f02021-10-11 08:53:18 +0000579 logger.Debugw(ctx, "image download on omci requested", log.Fields{
580 "image-id": imageIdentifier, "device-id": loDeviceID})
581 //onu upgrade handling called in background without immediate error evaluation here
582 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
583 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
584 go handler.onuSwUpgradeAfterDownload(ctx, request, oo.pFileManager, imageIdentifier)
585 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_STARTED
586 loDeviceImageState.ImageState.Reason = voltha.ImageState_NO_ERROR
587 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenko38662d02021-08-11 09:45:19 +0000588 } else {
589 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
590 if !downloadedToAdapter {
591 loDeviceImageState.ImageState.Reason = voltha.ImageState_INVALID_URL
592 } else { //only logical option is !vendorIDMatch
593 loDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
594 }
595 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
596 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000597 }
mpagenko2f2f2362021-06-07 08:25:22 +0000598 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000599 } //for all requested devices
mpagenkoc26d4c02021-05-06 14:27:57 +0000600 pImageResp := &loResponse
601 return pImageResp, nil
602 }
603 return nil, errors.New("invalid image download parameters")
mpagenko83144272021-04-27 10:06:22 +0000604}
605
khenaidoo7d3c5582021-08-11 18:09:44 -0400606// GetOnuImageStatus delivers the adapter-related information about the download/activation/commitment
mpagenko83144272021-04-27 10:06:22 +0000607// status for the requested image
khenaidoo7d3c5582021-08-11 18:09:44 -0400608func (oo *OpenONUAC) GetOnuImageStatus(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000609 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
610 loResponse := voltha.DeviceImageResponse{}
mpagenkoaa3afe92021-05-21 16:20:58 +0000611 imageIdentifier := (*in).Version
mpagenko38662d02021-08-11 09:45:19 +0000612 var vendorIDSet bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000613 firstDevice := true
614 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000615 var onuVolthaDevice *voltha.Device
616 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000617 for _, pCommonID := range (*in).DeviceId {
618 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400619 pDeviceImageState := &voltha.DeviceImageState{DeviceId: loDeviceID}
mpagenko59862f02021-10-11 08:53:18 +0000620 vendorIDSet = false
621 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400622 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000623 if handler != nil {
624 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
625 } else {
626 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
627 devErr = errors.New("no handler found for device-id")
mpagenko38662d02021-08-11 09:45:19 +0000628 }
mpagenko59862f02021-10-11 08:53:18 +0000629 if devErr != nil || onuVolthaDevice == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +0000630 logger.Warnw(ctx, "Failed to fetch Onu device to get image status",
mpagenko59862f02021-10-11 08:53:18 +0000631 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000632 pImageState := &voltha.ImageState{
633 Version: (*in).Version,
634 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //no statement about last activity possible
635 Reason: voltha.ImageState_UNKNOWN_ERROR, //something like "DEVICE_NOT_EXISTS" would be better (proto def)
636 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000637 }
mpagenko38662d02021-08-11 09:45:19 +0000638 pDeviceImageState.ImageState = pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000639 } else {
mpagenko38662d02021-08-11 09:45:19 +0000640 if firstDevice {
641 //start/verify download of the image to the adapter based on first found device only
642 // use the OnuVendor identification from first given device
643 firstDevice = false
644 vendorID = onuVolthaDevice.VendorId
645 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
646 vendorIDSet = true
647 logger.Debugw(ctx, "status request for image", log.Fields{"image-id": imageIdentifier})
648 } else {
649 //for all following devices verify the matching vendorID
650 if onuVolthaDevice.VendorId != vendorID {
651 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
652 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
653 } else {
654 vendorIDSet = true
655 }
656 }
657 if !vendorIDSet {
658 pImageState := &voltha.ImageState{
659 Version: (*in).Version,
660 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //can't be sure that download for this device was really tried
661 Reason: voltha.ImageState_VENDOR_DEVICE_MISMATCH,
662 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
663 }
664 pDeviceImageState.ImageState = pImageState
665 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400666 logger.Debugw(ctx, "image status request for", log.Fields{
667 "image-id": imageIdentifier, "device-id": loDeviceID})
668 //status request is called synchronously to collect the indications for all concerned devices
669 pDeviceImageState.ImageState = handler.requestOnuSwUpgradeState(ctx, imageIdentifier, (*in).Version)
mpagenko38662d02021-08-11 09:45:19 +0000670 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000671 }
672 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000673 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000674 pImageResp := &loResponse
675 return pImageResp, nil
676 }
677 return nil, errors.New("invalid image status request parameters")
mpagenko83144272021-04-27 10:06:22 +0000678}
679
khenaidoo7d3c5582021-08-11 18:09:44 -0400680// AbortOnuImageUpgrade stops the actual download/activation/commitment process (on next possibly step)
681func (oo *OpenONUAC) AbortOnuImageUpgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000682 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
683 loResponse := voltha.DeviceImageResponse{}
684 imageIdentifier := (*in).Version
685 firstDevice := true
686 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000687 var vendorIDSet bool
688 var onuVolthaDevice *voltha.Device
689 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000690 for _, pCommonID := range (*in).DeviceId {
691 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400692 pDeviceImageState := &voltha.DeviceImageState{}
693 loImageState := voltha.ImageState{}
694 pDeviceImageState.ImageState = &loImageState
mpagenko59862f02021-10-11 08:53:18 +0000695 vendorIDSet = false
696 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400697 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000698 if handler != nil {
699 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
700 } else {
701 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
702 devErr = errors.New("no handler found for device-id")
703 }
704 if devErr != nil || onuVolthaDevice == nil {
705 logger.Warnw(ctx, "Failed to fetch Onu device to abort its download",
706 log.Fields{"device-id": loDeviceID, "err": devErr})
khenaidoo7d3c5582021-08-11 18:09:44 -0400707 pDeviceImageState.DeviceId = loDeviceID
708 pDeviceImageState.ImageState.Version = (*in).Version
mpagenko59862f02021-10-11 08:53:18 +0000709 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
710 pDeviceImageState.ImageState.Reason = voltha.ImageState_CANCELLED_ON_REQUEST //something better could be considered (MissingHandler) - proto
khenaidoo7d3c5582021-08-11 18:09:44 -0400711 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoaa3afe92021-05-21 16:20:58 +0000712 } else {
mpagenko59862f02021-10-11 08:53:18 +0000713 if firstDevice {
714 //start/verify download of the image to the adapter based on first found device only
715 // use the OnuVendor identification from first given device
716 firstDevice = false
717 vendorID = onuVolthaDevice.VendorId
718 vendorIDSet = true
719 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
720 logger.Debugw(ctx, "abort request for file", log.Fields{"image-id": imageIdentifier})
721 } else {
722 //for all following devices verify the matching vendorID
723 if onuVolthaDevice.VendorId != vendorID {
724 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
725 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
726 pDeviceImageState.DeviceId = loDeviceID
727 pDeviceImageState.ImageState.Version = (*in).Version
728 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
729 pDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
730 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
731 } else {
732 vendorIDSet = true
733 }
734 }
735 if vendorIDSet {
736 // cancel the ONU upgrade activity for each possible device
737 logger.Debugw(ctx, "image upgrade abort requested", log.Fields{
738 "image-id": imageIdentifier, "device-id": loDeviceID})
739 //upgrade cancel is called synchronously to collect the imageResponse indications for all concerned devices
740 handler.cancelOnuSwUpgrade(ctx, imageIdentifier, (*in).Version, pDeviceImageState)
mpagenkoaa3afe92021-05-21 16:20:58 +0000741 }
742 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000743 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000744 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000745 if !firstDevice {
746 //if at least one valid device was found cancel also a possibly running download to adapter and remove the image
747 // this is to be done after the upgradeOnu cancel activities in order to not subduct the file for still running processes
748 oo.pFileManager.CancelDownload(ctx, imageIdentifier)
749 }
750 pImageResp := &loResponse
751 return pImageResp, nil
752 }
753 return nil, errors.New("invalid image upgrade abort parameters")
mpagenko83144272021-04-27 10:06:22 +0000754}
755
khenaidoo7d3c5582021-08-11 18:09:44 -0400756// GetOnuImages retrieves the ONU SW image status information via OMCI
757func (oo *OpenONUAC) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) {
758 logger.Infow(ctx, "Get_onu_images", log.Fields{"device-id": id.Id})
759 if handler := oo.getDeviceHandler(ctx, id.Id, false); handler != nil {
Himani Chawla69992ab2021-07-08 15:13:02 +0530760 images, err := handler.getOnuImages(ctx)
761 if err == nil {
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000762 return images, nil
763 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400764 return nil, fmt.Errorf(fmt.Sprintf("%s-%s", err, id.Id))
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000765 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400766 logger.Warnw(ctx, "no handler found for Get_onu_images", log.Fields{"device-id": id.Id})
767 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", id.Id))
mpagenko83144272021-04-27 10:06:22 +0000768}
769
khenaidoo7d3c5582021-08-11 18:09:44 -0400770// ActivateOnuImage initiates the activation of the image for the requested ONU(s)
mpagenkoc26d4c02021-05-06 14:27:57 +0000771// precondition: image downloaded and not yet activated or image refers to current inactive image
khenaidoo7d3c5582021-08-11 18:09:44 -0400772func (oo *OpenONUAC) ActivateOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000773 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
774 loResponse := voltha.DeviceImageResponse{}
775 imageIdentifier := (*in).Version
776 //let the deviceHandler find the adequate way of requesting the image activation
777 for _, pCommonID := range (*in).DeviceId {
778 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000779 loDeviceImageState := voltha.DeviceImageState{}
780 loDeviceImageState.DeviceId = loDeviceID
781 loImageState := voltha.ImageState{}
782 loDeviceImageState.ImageState = &loImageState
783 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000784 //compared to download procedure the vendorID (from device) is secondary here
785 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
786 // start image activation activity for each possible device
787 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
788 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
789 logger.Debugw(ctx, "onu image activation requested", log.Fields{
790 "image-id": imageIdentifier, "device-id": loDeviceID})
791 //onu activation handling called in background without immediate error evaluation here
792 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
793 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000794 if pImageStates, err := handler.onuSwActivateRequest(ctx, imageIdentifier, (*in).CommitOnSuccess); err != nil {
795 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
796 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
797 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
798 } else {
799 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
800 loDeviceImageState.ImageState.Reason = pImageStates.Reason
801 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
802 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000803 } else {
804 //cannot start SW activation for requested device
805 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000806 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000807 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
808 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000809 }
mpagenko2f2f2362021-06-07 08:25:22 +0000810 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000811 }
812 pImageResp := &loResponse
813 return pImageResp, nil
814 }
815 return nil, errors.New("invalid image activation parameters")
mpagenko83144272021-04-27 10:06:22 +0000816}
817
khenaidoo7d3c5582021-08-11 18:09:44 -0400818// CommitOnuImage enforces the commitment of the image for the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000819// precondition: image activated and not yet committed
khenaidoo7d3c5582021-08-11 18:09:44 -0400820func (oo *OpenONUAC) CommitOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000821 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
822 loResponse := voltha.DeviceImageResponse{}
823 imageIdentifier := (*in).Version
824 //let the deviceHandler find the adequate way of requesting the image activation
825 for _, pCommonID := range (*in).DeviceId {
826 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000827 loDeviceImageState := voltha.DeviceImageState{}
828 loDeviceImageState.DeviceId = loDeviceID
829 loImageState := voltha.ImageState{}
830 loDeviceImageState.ImageState = &loImageState
831 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000832 //compared to download procedure the vendorID (from device) is secondary here
833 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
834 // start image activation activity for each possible device
835 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
836 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
837 logger.Debugw(ctx, "onu image commitment requested", log.Fields{
838 "image-id": imageIdentifier, "device-id": loDeviceID})
839 //onu commitment handling called in background without immediate error evaluation here
840 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
841 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000842 if pImageStates, err := handler.onuSwCommitRequest(ctx, imageIdentifier); err != nil {
mpagenko38662d02021-08-11 09:45:19 +0000843 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
844 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //can be multiple reasons here
mpagenko183647c2021-06-08 15:25:04 +0000845 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
846 } else {
847 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
848 loDeviceImageState.ImageState.Reason = pImageStates.Reason
849 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
850 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000851 } else {
852 //cannot start SW commitment for requested device
853 logger.Warnw(ctx, "no handler found for image commitment", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000854 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000855 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
856 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000857 }
mpagenko2f2f2362021-06-07 08:25:22 +0000858 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000859 }
860 pImageResp := &loResponse
861 return pImageResp, nil
862 }
863 return nil, errors.New("invalid image commitment parameters")
mpagenko83144272021-04-27 10:06:22 +0000864}
865
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000866// Adapter interface required methods ################ end #########
867// #################################################################
khenaidoo7d3c5582021-08-11 18:09:44 -0400868
869/*
870 *
871 * ONU inter adapter service
872 *
873 */
874
875// OnuIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400876func (oo *OpenONUAC) OnuIndication(ctx context.Context, onuInd *ia.OnuIndicationMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400877 logger.Debugw(ctx, "onu-indication", log.Fields{"onu-indication": onuInd})
878
879 if onuInd == nil || onuInd.OnuIndication == nil {
880 return nil, fmt.Errorf("invalid-onu-indication-%v", onuInd)
881 }
882
883 onuIndication := onuInd.OnuIndication
884 onuOperstate := onuIndication.GetOperState()
885 waitForDhInstPresent := false
886 if onuOperstate == "up" {
887 //Race condition (relevant in BBSIM-environment only): Due to unsynchronized processing of olt-adapter and rw_core,
888 //ONU_IND_REQUEST msg by olt-adapter could arrive a little bit earlier than rw_core was able to announce the corresponding
889 //ONU by RPC of Adopt_device(). Therefore it could be necessary to wait with processing of ONU_IND_REQUEST until call of
890 //Adopt_device() arrived and DeviceHandler instance was created
891 waitForDhInstPresent = true
892 }
893 if handler := oo.getDeviceHandler(ctx, onuInd.DeviceId, waitForDhInstPresent); handler != nil {
894 logger.Infow(ctx, "onu-ind-request", log.Fields{"device-id": onuInd.DeviceId,
895 "OnuId": onuIndication.GetOnuId(),
896 "AdminState": onuIndication.GetAdminState(), "OperState": onuOperstate,
897 "SNR": onuIndication.GetSerialNumber()})
898
899 if onuOperstate == "up" {
900 if err := handler.createInterface(ctx, onuIndication); err != nil {
901 return nil, err
902 }
903 return &empty.Empty{}, nil
904 } else if (onuOperstate == "down") || (onuOperstate == "unreachable") {
905 return nil, handler.updateInterface(ctx, onuIndication)
906 } else {
907 logger.Errorw(ctx, "unknown-onu-ind-request operState", log.Fields{"OnuId": onuIndication.GetOnuId()})
908 return nil, fmt.Errorf("invalidOperState: %s, %s", onuOperstate, onuInd.DeviceId)
909 }
910 }
911 logger.Warnw(ctx, "no handler found for received onu-ind-request", log.Fields{
912 "msgToDeviceId": onuInd.DeviceId})
913 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", onuInd.DeviceId))
914}
915
916// OmciIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400917func (oo *OpenONUAC) OmciIndication(ctx context.Context, msg *ia.OmciMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400918 logger.Debugw(ctx, "omci-response", log.Fields{"parent-device-id": msg.ParentDeviceId, "child-device-id": msg.ChildDeviceId})
919
920 if handler := oo.getDeviceHandler(ctx, msg.ChildDeviceId, false); handler != nil {
921 if err := handler.handleOMCIIndication(log.WithSpanFromContext(context.Background(), ctx), msg); err != nil {
922 return nil, err
923 }
924 return &empty.Empty{}, nil
925 }
926 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", msg.ChildDeviceId))
927}
928
929// DownloadTechProfile is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400930func (oo *OpenONUAC) DownloadTechProfile(ctx context.Context, tProfile *ia.TechProfileDownloadMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400931 logger.Debugw(ctx, "download-tech-profile", log.Fields{"uni-id": tProfile.UniId})
932
933 if handler := oo.getDeviceHandler(ctx, tProfile.DeviceId, false); handler != nil {
934 if err := handler.handleTechProfileDownloadRequest(log.WithSpanFromContext(context.Background(), ctx), tProfile); err != nil {
935 return nil, err
936 }
937 return &empty.Empty{}, nil
938 }
939 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tProfile.DeviceId))
940}
941
942// DeleteGemPort is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400943func (oo *OpenONUAC) DeleteGemPort(ctx context.Context, gPort *ia.DeleteGemPortMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400944 logger.Debugw(ctx, "delete-gem-port", log.Fields{"device-id": gPort.DeviceId, "uni-id": gPort.UniId})
945
946 if handler := oo.getDeviceHandler(ctx, gPort.DeviceId, false); handler != nil {
947 if err := handler.handleDeleteGemPortRequest(log.WithSpanFromContext(context.Background(), ctx), gPort); err != nil {
948 return nil, err
949 }
950 return &empty.Empty{}, nil
951 }
952 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", gPort.DeviceId))
953}
954
955// DeleteTCont is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400956func (oo *OpenONUAC) DeleteTCont(ctx context.Context, tConf *ia.DeleteTcontMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400957 logger.Debugw(ctx, "delete-tcont", log.Fields{"tconf": tConf})
958
959 if handler := oo.getDeviceHandler(ctx, tConf.DeviceId, false); handler != nil {
960 if err := handler.handleDeleteTcontRequest(log.WithSpanFromContext(context.Background(), ctx), tConf); err != nil {
961 return nil, err
962 }
963 return &empty.Empty{}, nil
964 }
965 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tConf.DeviceId))
966}
967
968/*
969 * Parent GRPC clients
970 */
971
972func (oo *OpenONUAC) setupParentInterAdapterClient(ctx context.Context, endpoint string) error {
973 logger.Infow(ctx, "setting-parent-adapter-connection", log.Fields{"parent-endpoint": endpoint})
974 oo.lockParentAdapterClients.Lock()
975 defer oo.lockParentAdapterClients.Unlock()
976 if _, ok := oo.parentAdapterClients[endpoint]; ok {
977 return nil
978 }
979
980 childClient, err := vgrpc.NewClient(endpoint,
981 oo.oltAdapterRestarted,
982 vgrpc.ActivityCheck(true))
983
984 if err != nil {
985 return err
986 }
987
988 oo.parentAdapterClients[endpoint] = childClient
989
990 go oo.parentAdapterClients[endpoint].Start(log.WithSpanFromContext(context.TODO(), ctx), setAndTestAdapterServiceHandler)
991
992 // Wait until we have a connection to the child adapter.
993 // Unlimited retries or until context expires
994 subCtx := log.WithSpanFromContext(context.TODO(), ctx)
995 backoff := vgrpc.NewBackoff(oo.config.MinBackoffRetryDelay, oo.config.MaxBackoffRetryDelay, 0)
996 for {
997 client, err := oo.parentAdapterClients[endpoint].GetOltInterAdapterServiceClient()
998 if err == nil && client != nil {
999 logger.Infow(subCtx, "connected-to-parent-adapter", log.Fields{"parent-endpoint": endpoint})
1000 break
1001 }
1002 logger.Warnw(subCtx, "connection-to-parent-adapter-not-ready", log.Fields{"error": err, "parent-endpoint": endpoint})
1003 // Backoff
1004 if err = backoff.Backoff(subCtx); err != nil {
1005 logger.Errorw(subCtx, "received-error-on-backoff", log.Fields{"error": err, "parent-endpoint": endpoint})
1006 break
1007 }
1008 }
1009 return nil
1010}
1011
khenaidoo42dcdfd2021-10-19 17:34:12 -04001012func (oo *OpenONUAC) getParentAdapterServiceClient(endpoint string) (olt_inter_adapter_service.OltInterAdapterServiceClient, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001013 // First check from cache
1014 oo.lockParentAdapterClients.RLock()
1015 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1016 oo.lockParentAdapterClients.RUnlock()
1017 return pgClient.GetOltInterAdapterServiceClient()
1018 }
1019 oo.lockParentAdapterClients.RUnlock()
1020
1021 // Set the parent connection - can occur on restarts
1022 ctx, cancel := context.WithTimeout(context.Background(), oo.config.RPCTimeout)
1023 err := oo.setupParentInterAdapterClient(ctx, endpoint)
1024 cancel()
1025 if err != nil {
1026 return nil, err
1027 }
1028
1029 // Get the parent client now
1030 oo.lockParentAdapterClients.RLock()
1031 defer oo.lockParentAdapterClients.RUnlock()
1032 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1033 return pgClient.GetOltInterAdapterServiceClient()
1034 }
1035
1036 return nil, fmt.Errorf("no-client-for-endpoint-%s", endpoint)
1037}
1038
1039// TODO: Any action the adapter needs to do following an olt adapter restart?
1040func (oo *OpenONUAC) oltAdapterRestarted(ctx context.Context, endPoint string) error {
1041 logger.Errorw(ctx, "olt-adapter-restarted", log.Fields{"endpoint": endPoint})
1042 return nil
1043}
1044
1045// setAndTestAdapterServiceHandler is used to test whether the remote gRPC service is up
1046func setAndTestAdapterServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
khenaidoo42dcdfd2021-10-19 17:34:12 -04001047 svc := olt_inter_adapter_service.NewOltInterAdapterServiceClient(conn)
1048 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != health.HealthStatus_HEALTHY {
khenaidoo7d3c5582021-08-11 18:09:44 -04001049 return nil
1050 }
1051 return svc
1052}
1053
1054/*
1055 *
1056 * Unimplemented APIs
1057 *
1058 */
1059
1060//GetOfpDeviceInfo returns OFP information for the given device. Method not implemented as per [VOL-3202].
1061// OF port info is now to be delivered within UniPort create cmp changes in onu_uni_port.go::CreateVolthaPort()
1062//
khenaidoo42dcdfd2021-10-19 17:34:12 -04001063func (oo *OpenONUAC) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001064 return nil, errors.New("unImplemented")
1065}
1066
1067//SimulateAlarm is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001068func (oo *OpenONUAC) SimulateAlarm(context.Context, *ca.SimulateAlarmMessage) (*common.OperationResp, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001069 return nil, errors.New("unImplemented")
1070}
1071
1072//SetExtValue is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001073func (oo *OpenONUAC) SetExtValue(context.Context, *ca.SetExtValueMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001074 return nil, errors.New("unImplemented")
1075}
1076
1077//SetSingleValue is unimplemented
1078func (oo *OpenONUAC) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
1079 return nil, errors.New("unImplemented")
1080}
1081
1082//StartOmciTest not implemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001083func (oo *OpenONUAC) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001084 return nil, errors.New("unImplemented")
1085}
1086
1087//SuppressEvent unimplemented
1088func (oo *OpenONUAC) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1089 return nil, errors.New("unImplemented")
1090}
1091
1092//UnSuppressEvent unimplemented
1093func (oo *OpenONUAC) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1094 return nil, errors.New("unImplemented")
1095}
1096
1097//GetImageDownloadStatus is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001098func (oo *OpenONUAC) GetImageDownloadStatus(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001099 return nil, errors.New("unImplemented")
1100}
1101
1102//CancelImageDownload is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001103func (oo *OpenONUAC) CancelImageDownload(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001104 return nil, errors.New("unImplemented")
1105}
1106
1107//RevertImageUpdate is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001108func (oo *OpenONUAC) RevertImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001109 return nil, errors.New("unImplemented")
1110}
1111
1112// UpdateFlowsBulk is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001113func (oo *OpenONUAC) UpdateFlowsBulk(ctx context.Context, flows *ca.BulkFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001114 return nil, errors.New("unImplemented")
1115}
1116
1117//SelfTestDevice unimplented
1118func (oo *OpenONUAC) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
1119 return nil, errors.New("unImplemented")
1120}
1121
1122//SendPacketOut sends packet out to the device
khenaidoo42dcdfd2021-10-19 17:34:12 -04001123func (oo *OpenONUAC) SendPacketOut(ctx context.Context, packet *ca.PacketOut) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001124 return nil, errors.New("unImplemented")
1125}
1126
1127// EnablePort to Enable PON/NNI interface - seems not to be used/required according to python code
1128func (oo *OpenONUAC) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1129 return nil, errors.New("unImplemented")
1130}
1131
1132// DisablePort to Disable pon/nni interface - seems not to be used/required according to python code
1133func (oo *OpenONUAC) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1134 return nil, errors.New("unImplemented")
1135}
1136
1137// GetExtValue - unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001138func (oo *OpenONUAC) GetExtValue(ctx context.Context, extInfo *ca.GetExtValueMessage) (*extension.ReturnValues, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001139 return nil, errors.New("unImplemented")
1140}
1141
1142// ChildDeviceLost - unimplemented
1143func (oo *OpenONUAC) ChildDeviceLost(ctx context.Context, childDevice *voltha.Device) (*empty.Empty, error) {
1144 return nil, errors.New("unImplemented")
1145}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001146
1147// GetSupportedFsms - TODO: add comment
1148func (oo *OpenONUAC) GetSupportedFsms() *cmn.OmciDeviceFsms {
1149 return oo.pSupportedFsms
1150}
1151
1152// LockMutexMibTemplateGenerated - TODO: add comment
1153func (oo *OpenONUAC) LockMutexMibTemplateGenerated() {
1154 oo.mutexMibTemplateGenerated.Lock()
1155}
1156
1157// UnlockMutexMibTemplateGenerated - TODO: add comment
1158func (oo *OpenONUAC) UnlockMutexMibTemplateGenerated() {
1159 oo.mutexMibTemplateGenerated.Unlock()
1160}
1161
1162// GetMibTemplatesGenerated - TODO: add comment
1163func (oo *OpenONUAC) GetMibTemplatesGenerated(mibTemplatePath string) (value bool, exist bool) {
1164 value, exist = oo.mibTemplatesGenerated[mibTemplatePath]
1165 return value, exist
1166}
1167
1168// SetMibTemplatesGenerated - TODO: add comment
1169func (oo *OpenONUAC) SetMibTemplatesGenerated(mibTemplatePath string, value bool) {
1170 oo.mibTemplatesGenerated[mibTemplatePath] = value
1171}
1172
1173// RLockMutexDeviceHandlersMap - TODO: add comment
1174func (oo *OpenONUAC) RLockMutexDeviceHandlersMap() {
1175 oo.mutexDeviceHandlersMap.RLock()
1176}
1177
1178// RUnlockMutexDeviceHandlersMap - TODO: add comment
1179func (oo *OpenONUAC) RUnlockMutexDeviceHandlersMap() {
1180 oo.mutexDeviceHandlersMap.RUnlock()
1181}
1182
1183// GetDeviceHandler - TODO: add comment
1184func (oo *OpenONUAC) GetDeviceHandler(deviceID string) (value cmn.IdeviceHandler, exist bool) {
1185 value, exist = oo.deviceHandlers[deviceID]
1186 return value, exist
1187}