blob: c356d06e8f0bc2d5b6886631bf34963f536986d8 [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)
khenaidoo7d3c5582021-08-11 18:09:44 -0400316 logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id, "SerialNumber": device.SerialNumber, "ctx": ctx, "nctx": nctx})
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000317
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
321 handler.mutexDeletionInProgressFlag.Lock()
322 handler.deletionInProgress = true
323 handler.mutexDeletionInProgressFlag.Unlock()
324
Holger Hildebrandt60652202021-11-02 11:09:36 +0000325 if err := handler.resetFsms(ctx, true); err != nil {
326 errorsList = append(errorsList, err)
327 }
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000328 forceKvDelete := false
329
Holger Hildebrandt60652202021-11-02 11:09:36 +0000330 // Clear PM data on the KV store
331 if handler.pOnuMetricsMgr != nil {
332 if err := handler.pOnuMetricsMgr.ClearAllPmData(ctx); err != nil {
333 errorsList = append(errorsList, err)
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000334 forceKvDelete = true
Holger Hildebrandt60652202021-11-02 11:09:36 +0000335 }
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000336 } else {
337 forceKvDelete = true
338 }
339 if err := handler.deleteDevicePersistencyData(ctx); err != nil {
340 errorsList = append(errorsList, err)
341 forceKvDelete = true
Girish Gowdra10123c02021-08-30 11:52:06 -0700342 }
Girish Gowdrae95687a2021-09-08 16:30:58 -0700343 for _, uni := range handler.uniEntityMap {
344 if handler.GetFlowMonitoringIsRunning(uni.UniID) {
345 handler.stopFlowMonitoringRoutine[uni.UniID] <- true
346 logger.Debugw(ctx, "sent stop signal to self flow monitoring routine", log.Fields{"device-id": device.Id})
347 }
348 }
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000349 //don't leave any garbage - even in error case
350 oo.deleteDeviceHandlerToMap(handler)
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000351 if forceKvDelete {
352 if err := oo.forceDeleteDeviceKvData(ctx, device.Id); err != nil {
353 errorsList = append(errorsList, err)
354 }
355 }
Holger Hildebrandt60652202021-11-02 11:09:36 +0000356
Girish Gowdra0e533642021-03-02 22:02:51 -0800357 if len(errorsList) > 0 {
358 logger.Errorw(ctx, "one-or-more-error-during-device-delete", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400359 return nil, fmt.Errorf("one-or-more-error-during-device-delete, errors:%v", errorsList)
Girish Gowdra0e533642021-03-02 22:02:51 -0800360 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400361 return &empty.Empty{}, nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000362 }
Holger Hildebrandt60652202021-11-02 11:09:36 +0000363 logger.Infow(ctx, "no handler found for device-deletion - trying to delete remaining data in the kv-store ", log.Fields{"device-id": device.Id})
364
Holger Hildebrandtc69f0742021-11-16 13:48:00 +0000365 if err := oo.forceDeleteDeviceKvData(ctx, device.Id); err != nil {
366 return nil, err
Holger Hildebrandt60652202021-11-02 11:09:36 +0000367 }
368 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000369}
370
khenaidoo7d3c5582021-08-11 18:09:44 -0400371//UpdateFlowsIncrementally updates (add/remove) the flows on a given device
khenaidoo42dcdfd2021-10-19 17:34:12 -0400372func (oo *OpenONUAC) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ca.IncrementalFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400373 logger.Infow(ctx, "update-flows-incrementally", log.Fields{"device-id": incrFlows.Device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000374
mpagenkofc4f56e2020-11-04 17:17:49 +0000375 //flow config is relayed to handler even if the device might be in some 'inactive' state
376 // let the handler or related FSM's decide, what to do with the modified flow state info
377 // 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 +0000378
379 // For now, there is no support for group changes (as in the actual Py-adapter code)
mpagenkofc4f56e2020-11-04 17:17:49 +0000380 // but processing is continued for flowUpdate possibly also set in the request
khenaidoo7d3c5582021-08-11 18:09:44 -0400381 if incrFlows.Groups.ToAdd != nil && incrFlows.Groups.ToAdd.Items != nil {
382 logger.Warnw(ctx, "Update-flow-incr: group add not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000383 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400384 if incrFlows.Groups.ToRemove != nil && incrFlows.Groups.ToRemove.Items != nil {
385 logger.Warnw(ctx, "Update-flow-incr: group remove not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000386 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400387 if incrFlows.Groups.ToUpdate != nil && incrFlows.Groups.ToUpdate.Items != nil {
388 logger.Warnw(ctx, "Update-flow-incr: group update not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000389 }
390
khenaidoo7d3c5582021-08-11 18:09:44 -0400391 if handler := oo.getDeviceHandler(ctx, incrFlows.Device.Id, false); handler != nil {
392 if err := handler.FlowUpdateIncremental(log.WithSpanFromContext(context.Background(), ctx), incrFlows.Flows, incrFlows.Groups, incrFlows.FlowMetadata); err != nil {
393 return nil, err
394 }
395 return &empty.Empty{}, nil
mpagenkodff5dda2020-08-28 11:52:01 +0000396 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400397 logger.Warnw(ctx, "no handler found for incremental flow update", log.Fields{"device-id": incrFlows.Device.Id})
398 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", incrFlows.Device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000399}
400
khenaidoo7d3c5582021-08-11 18:09:44 -0400401//UpdatePmConfig returns PmConfigs nil or error
khenaidoo42dcdfd2021-10-19 17:34:12 -0400402func (oo *OpenONUAC) UpdatePmConfig(ctx context.Context, configs *ca.PmConfigsInfo) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400403 logger.Infow(ctx, "update-pm-config", log.Fields{"device-id": configs.DeviceId})
404 if handler := oo.getDeviceHandler(ctx, configs.DeviceId, false); handler != nil {
405 if err := handler.updatePmConfig(log.WithSpanFromContext(context.Background(), ctx), configs.PmConfigs); err != nil {
406 return nil, err
407 }
408 return &empty.Empty{}, nil
Girish Gowdrae09a6202021-01-12 18:10:59 -0800409 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400410 logger.Warnw(ctx, "no handler found for update-pm-config", log.Fields{"device-id": configs.DeviceId})
411 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", configs.DeviceId))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000412}
413
khenaidoo7d3c5582021-08-11 18:09:44 -0400414//DownloadImage requests downloading some image according to indications as given in request
Andrea Campanella71e546a2021-02-26 11:09:33 +0100415//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400416func (oo *OpenONUAC) DownloadImage(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400417 ctx = log.WithSpanFromContext(context.Background(), ctx)
418 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000419 if !oo.pDownloadManager.ImageExists(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400420 logger.Debugw(ctx, "start image download", log.Fields{"image-description": imageInfo.Image})
mpagenko15ff4a52021-03-02 10:09:20 +0000421 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
422 // the download itself is later done in background
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000423 if err := oo.pDownloadManager.StartDownload(ctx, imageInfo.Image); err != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400424 return nil, err
425 }
426 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000427 }
428 // image already exists
khenaidoo7d3c5582021-08-11 18:09:44 -0400429 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageInfo.Image})
430 return imageInfo.Image, nil
mpagenkoc8bba412021-01-15 15:38:44 +0000431 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400432
433 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000434}
435
mpagenko59862f02021-10-11 08:53:18 +0000436//ActivateImageUpdate requests downloading some Onu Software image to the ONU via OMCI
Andrea Campanella71e546a2021-02-26 11:09:33 +0100437// according to indications as given in request and on success activate the image on the ONU
438//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo42dcdfd2021-10-19 17:34:12 -0400439func (oo *OpenONUAC) ActivateImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400440 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000441 if oo.pDownloadManager.ImageLocallyDownloaded(ctx, imageInfo.Image) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400442 if handler := oo.getDeviceHandler(ctx, imageInfo.Device.Id, false); handler != nil {
mpagenko15ff4a52021-03-02 10:09:20 +0000443 logger.Debugw(ctx, "image download on omci requested", log.Fields{
khenaidoo7d3c5582021-08-11 18:09:44 -0400444 "image-description": imageInfo.Image, "device-id": imageInfo.Device.Id})
445 if err := handler.doOnuSwUpgrade(ctx, imageInfo.Image, oo.pDownloadManager); err != nil {
446 return nil, err
447 }
448 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000449 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400450 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": imageInfo.Device.Id})
451 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found - device-id: %s", imageInfo.Device.Id))
mpagenko057889c2021-01-21 16:51:58 +0000452 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400453 logger.Debugw(ctx, "image not yet downloaded on activate request", log.Fields{"image-description": imageInfo.Image})
454 return nil, fmt.Errorf(fmt.Sprintf("image-not-yet-downloaded - device-id: %s", imageInfo.Device.Id))
mpagenkoc8bba412021-01-15 15:38:44 +0000455 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400456 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000457}
458
khenaidoo7d3c5582021-08-11 18:09:44 -0400459//GetSingleValue handles the core request to retrieve uni status
460func (oo *OpenONUAC) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
kesavandfdf77632021-01-26 23:40:33 -0500461 logger.Infow(ctx, "Single_get_value_request", log.Fields{"request": request})
462
463 if handler := oo.getDeviceHandler(ctx, request.TargetId, false); handler != nil {
464 switch reqType := request.GetRequest().GetRequest().(type) {
465 case *extension.GetValueRequest_UniInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000466 return handler.GetUniPortStatus(ctx, reqType.UniInfo), nil
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700467 case *extension.GetValueRequest_OnuOpticalInfo:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000468 CommChan := make(chan cmn.Message)
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700469 respChan := make(chan extension.SingleGetValueResponse)
470 // Initiate the self test request
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000471 if err := handler.pSelfTestHdlr.SelfTestRequestStart(ctx, *request, CommChan, respChan); err != nil {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700472 return &extension.SingleGetValueResponse{
473 Response: &extension.GetValueResponse{
474 Status: extension.GetValueResponse_ERROR,
475 ErrReason: extension.GetValueResponse_INTERNAL_ERROR,
476 },
477 }, err
478 }
479 // The timeout handling is already implemented in omci_self_test_handler module
480 resp := <-respChan
481 return &resp, nil
Himani Chawla43f95ff2021-06-03 00:24:12 +0530482 case *extension.GetValueRequest_OnuInfo:
483 return handler.getOnuOMCICounters(ctx, reqType.OnuInfo), nil
kesavandfdf77632021-01-26 23:40:33 -0500484 default:
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000485 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_UNSUPPORTED), nil
kesavandfdf77632021-01-26 23:40:33 -0500486
487 }
488 }
489 logger.Errorw(ctx, "Single_get_value_request failed ", log.Fields{"request": request})
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +0000490 return uniprt.PostUniStatusErrResponse(extension.GetValueResponse_INVALID_DEVICE_ID), nil
mpagenkoc8bba412021-01-15 15:38:44 +0000491}
492
mpagenko83144272021-04-27 10:06:22 +0000493//if update >= 4.3.0
mpagenkoc26d4c02021-05-06 14:27:57 +0000494// Note: already with the implementation of the 'old' download interface problems were detected when the argument name used here is not the same
495// as defined in the adapter interface file. That sounds strange and the effects were strange as well.
496// The reason for that was never finally investigated.
497// To be on the safe side argument names are left here always as defined in iAdapter.go .
mpagenko83144272021-04-27 10:06:22 +0000498
khenaidoo7d3c5582021-08-11 18:09:44 -0400499// DownloadOnuImage downloads (and optionally activates and commits) the indicated ONU image to the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000500// if the image is not yet present on the adapter it has to be automatically downloaded
khenaidoo7d3c5582021-08-11 18:09:44 -0400501func (oo *OpenONUAC) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000502 if request != nil && len((*request).DeviceId) > 0 && (*request).Image.Version != "" {
503 loResponse := voltha.DeviceImageResponse{}
504 imageIdentifier := (*request).Image.Version
mpagenkoc497ee32021-11-10 17:30:20 +0000505 downloadStartDone := false
mpagenkoc26d4c02021-05-06 14:27:57 +0000506 firstDevice := true
507 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000508 var onuVolthaDevice *voltha.Device
509 var devErr error
mpagenkoc26d4c02021-05-06 14:27:57 +0000510 for _, pCommonID := range (*request).DeviceId {
mpagenko38662d02021-08-11 09:45:19 +0000511 vendorIDMatch := true
mpagenkoc26d4c02021-05-06 14:27:57 +0000512 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000513 loDeviceImageState := voltha.DeviceImageState{}
514 loDeviceImageState.DeviceId = loDeviceID
515 loImageState := voltha.ImageState{}
516 loDeviceImageState.ImageState = &loImageState
517 loDeviceImageState.ImageState.Version = (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000518
mpagenko59862f02021-10-11 08:53:18 +0000519 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400520 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000521 if handler != nil {
522 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
523 } else {
524 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
525 devErr = errors.New("no handler found for device-id")
khenaidoo7d3c5582021-08-11 18:09:44 -0400526 }
mpagenko59862f02021-10-11 08:53:18 +0000527 if devErr != nil || onuVolthaDevice == nil {
528 logger.Warnw(ctx, "Failed to fetch ONU device for image download",
529 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000530 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
531 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //proto restriction, better option: 'INVALID_DEVICE'
mpagenkoc26d4c02021-05-06 14:27:57 +0000532 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000533 } else {
mpagenko38662d02021-08-11 09:45:19 +0000534 if firstDevice {
535 //start/verify download of the image to the adapter based on first found device only
536 // use the OnuVendor identification from first given device
mpagenkoc497ee32021-11-10 17:30:20 +0000537
538 // note: if the request was done for a list of devices on the Voltha interface, rwCore
539 // translates that into a new rpc for each device, hence each device will be the first device in parallel requests!
mpagenko38662d02021-08-11 09:45:19 +0000540 firstDevice = false
541 vendorID = onuVolthaDevice.VendorId
542 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
mpagenkoc497ee32021-11-10 17:30:20 +0000543 logger.Infow(ctx, "download request for file",
544 log.Fields{"device-id": loDeviceID, "image-id": imageIdentifier})
mpagenko38662d02021-08-11 09:45:19 +0000545
mpagenkoc497ee32021-11-10 17:30:20 +0000546 // call the StartDownload synchronously to detect 'immediate' download problems
547 // the real download itself is later done in background
548 if fileState, err := oo.pFileManager.StartDownload(ctx, imageIdentifier, (*request).Image.Url); err == nil {
mpagenko38662d02021-08-11 09:45:19 +0000549 // note: If the image (with vendorId+name) has already been downloaded before from some other
mpagenkoc497ee32021-11-10 17:30:20 +0000550 // valid URL, the current download request is not executed (current code delivers URL error).
551 // If the operators want to ensure that the new URL
mpagenko38662d02021-08-11 09:45:19 +0000552 // is really used, then they first have to use the 'abort' API to remove the existing image!
553 // (abort API can be used also after some successful download to just remove the image from adapter)
mpagenkoc497ee32021-11-10 17:30:20 +0000554 if fileState == swupg.CFileStateDlSucceeded || fileState == swupg.CFileStateDlStarted {
555 downloadStartDone = true
556 } //else fileState may also indicate error situation, where the requested image is not ready to be used for other devices
mpagenko38662d02021-08-11 09:45:19 +0000557 }
558 } else {
559 //for all following devices verify the matching vendorID
560 if onuVolthaDevice.VendorId != vendorID {
561 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
562 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
563 vendorIDMatch = false
564 }
565 }
mpagenkoc497ee32021-11-10 17:30:20 +0000566 if downloadStartDone && vendorIDMatch {
mpagenko38662d02021-08-11 09:45:19 +0000567 // start the ONU download activity for each possible device
mpagenkoc497ee32021-11-10 17:30:20 +0000568 logger.Infow(ctx, "request image download to ONU on omci ", log.Fields{
mpagenko59862f02021-10-11 08:53:18 +0000569 "image-id": imageIdentifier, "device-id": loDeviceID})
570 //onu upgrade handling called in background without immediate error evaluation here
571 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
572 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
573 go handler.onuSwUpgradeAfterDownload(ctx, request, oo.pFileManager, imageIdentifier)
574 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_STARTED
575 loDeviceImageState.ImageState.Reason = voltha.ImageState_NO_ERROR
576 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenko38662d02021-08-11 09:45:19 +0000577 } else {
578 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
mpagenkoc497ee32021-11-10 17:30:20 +0000579 if !downloadStartDone {
580 //based on above fileState more descriptive error codes would be possible, e.g
581 // IMAGE_EXISTS_WITH_DIFFERENT_URL - would require proto buf update
mpagenko38662d02021-08-11 09:45:19 +0000582 loDeviceImageState.ImageState.Reason = voltha.ImageState_INVALID_URL
583 } else { //only logical option is !vendorIDMatch
584 loDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
585 }
586 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
587 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000588 }
mpagenko2f2f2362021-06-07 08:25:22 +0000589 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000590 } //for all requested devices
mpagenkoc26d4c02021-05-06 14:27:57 +0000591 pImageResp := &loResponse
592 return pImageResp, nil
593 }
594 return nil, errors.New("invalid image download parameters")
mpagenko83144272021-04-27 10:06:22 +0000595}
596
khenaidoo7d3c5582021-08-11 18:09:44 -0400597// GetOnuImageStatus delivers the adapter-related information about the download/activation/commitment
mpagenko83144272021-04-27 10:06:22 +0000598// status for the requested image
khenaidoo7d3c5582021-08-11 18:09:44 -0400599func (oo *OpenONUAC) GetOnuImageStatus(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000600 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
601 loResponse := voltha.DeviceImageResponse{}
mpagenkoaa3afe92021-05-21 16:20:58 +0000602 imageIdentifier := (*in).Version
mpagenko38662d02021-08-11 09:45:19 +0000603 var vendorIDSet bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000604 firstDevice := true
605 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000606 var onuVolthaDevice *voltha.Device
607 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000608 for _, pCommonID := range (*in).DeviceId {
609 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400610 pDeviceImageState := &voltha.DeviceImageState{DeviceId: loDeviceID}
mpagenko59862f02021-10-11 08:53:18 +0000611 vendorIDSet = false
612 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400613 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000614 if handler != nil {
615 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
616 } else {
617 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
618 devErr = errors.New("no handler found for device-id")
mpagenko38662d02021-08-11 09:45:19 +0000619 }
mpagenko59862f02021-10-11 08:53:18 +0000620 if devErr != nil || onuVolthaDevice == nil {
mpagenkoaa3afe92021-05-21 16:20:58 +0000621 logger.Warnw(ctx, "Failed to fetch Onu device to get image status",
mpagenko59862f02021-10-11 08:53:18 +0000622 log.Fields{"device-id": loDeviceID, "err": devErr})
mpagenko38662d02021-08-11 09:45:19 +0000623 pImageState := &voltha.ImageState{
624 Version: (*in).Version,
625 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //no statement about last activity possible
626 Reason: voltha.ImageState_UNKNOWN_ERROR, //something like "DEVICE_NOT_EXISTS" would be better (proto def)
627 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000628 }
mpagenko38662d02021-08-11 09:45:19 +0000629 pDeviceImageState.ImageState = pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000630 } else {
mpagenko38662d02021-08-11 09:45:19 +0000631 if firstDevice {
632 //start/verify download of the image to the adapter based on first found device only
633 // use the OnuVendor identification from first given device
634 firstDevice = false
635 vendorID = onuVolthaDevice.VendorId
636 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
637 vendorIDSet = true
638 logger.Debugw(ctx, "status request for image", log.Fields{"image-id": imageIdentifier})
639 } else {
640 //for all following devices verify the matching vendorID
641 if onuVolthaDevice.VendorId != vendorID {
642 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
643 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
644 } else {
645 vendorIDSet = true
646 }
647 }
648 if !vendorIDSet {
649 pImageState := &voltha.ImageState{
650 Version: (*in).Version,
651 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //can't be sure that download for this device was really tried
652 Reason: voltha.ImageState_VENDOR_DEVICE_MISMATCH,
653 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
654 }
655 pDeviceImageState.ImageState = pImageState
656 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400657 logger.Debugw(ctx, "image status request for", log.Fields{
658 "image-id": imageIdentifier, "device-id": loDeviceID})
659 //status request is called synchronously to collect the indications for all concerned devices
660 pDeviceImageState.ImageState = handler.requestOnuSwUpgradeState(ctx, imageIdentifier, (*in).Version)
mpagenko38662d02021-08-11 09:45:19 +0000661 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000662 }
663 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000664 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000665 pImageResp := &loResponse
666 return pImageResp, nil
667 }
668 return nil, errors.New("invalid image status request parameters")
mpagenko83144272021-04-27 10:06:22 +0000669}
670
khenaidoo7d3c5582021-08-11 18:09:44 -0400671// AbortOnuImageUpgrade stops the actual download/activation/commitment process (on next possibly step)
672func (oo *OpenONUAC) AbortOnuImageUpgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000673 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
674 loResponse := voltha.DeviceImageResponse{}
675 imageIdentifier := (*in).Version
676 firstDevice := true
677 var vendorID string
mpagenko59862f02021-10-11 08:53:18 +0000678 var vendorIDSet bool
679 var onuVolthaDevice *voltha.Device
680 var devErr error
mpagenkoaa3afe92021-05-21 16:20:58 +0000681 for _, pCommonID := range (*in).DeviceId {
682 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400683 pDeviceImageState := &voltha.DeviceImageState{}
684 loImageState := voltha.ImageState{}
685 pDeviceImageState.ImageState = &loImageState
mpagenko59862f02021-10-11 08:53:18 +0000686 vendorIDSet = false
687 onuVolthaDevice = nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400688 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
mpagenko59862f02021-10-11 08:53:18 +0000689 if handler != nil {
690 onuVolthaDevice, devErr = handler.getDeviceFromCore(ctx, loDeviceID)
691 } else {
692 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
693 devErr = errors.New("no handler found for device-id")
694 }
695 if devErr != nil || onuVolthaDevice == nil {
696 logger.Warnw(ctx, "Failed to fetch Onu device to abort its download",
697 log.Fields{"device-id": loDeviceID, "err": devErr})
khenaidoo7d3c5582021-08-11 18:09:44 -0400698 pDeviceImageState.DeviceId = loDeviceID
699 pDeviceImageState.ImageState.Version = (*in).Version
mpagenko59862f02021-10-11 08:53:18 +0000700 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
701 pDeviceImageState.ImageState.Reason = voltha.ImageState_CANCELLED_ON_REQUEST //something better could be considered (MissingHandler) - proto
khenaidoo7d3c5582021-08-11 18:09:44 -0400702 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoaa3afe92021-05-21 16:20:58 +0000703 } else {
mpagenko59862f02021-10-11 08:53:18 +0000704 if firstDevice {
705 //start/verify download of the image to the adapter based on first found device only
706 // use the OnuVendor identification from first given device
707 firstDevice = false
708 vendorID = onuVolthaDevice.VendorId
709 vendorIDSet = true
710 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
711 logger.Debugw(ctx, "abort request for file", log.Fields{"image-id": imageIdentifier})
712 } else {
713 //for all following devices verify the matching vendorID
714 if onuVolthaDevice.VendorId != vendorID {
715 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
716 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
717 pDeviceImageState.DeviceId = loDeviceID
718 pDeviceImageState.ImageState.Version = (*in).Version
719 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
720 pDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
721 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
722 } else {
723 vendorIDSet = true
724 }
725 }
726 if vendorIDSet {
727 // cancel the ONU upgrade activity for each possible device
728 logger.Debugw(ctx, "image upgrade abort requested", log.Fields{
729 "image-id": imageIdentifier, "device-id": loDeviceID})
730 //upgrade cancel is called synchronously to collect the imageResponse indications for all concerned devices
731 handler.cancelOnuSwUpgrade(ctx, imageIdentifier, (*in).Version, pDeviceImageState)
mpagenkoaa3afe92021-05-21 16:20:58 +0000732 }
733 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000734 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
mpagenko59862f02021-10-11 08:53:18 +0000735 } //for all requested devices
mpagenkoaa3afe92021-05-21 16:20:58 +0000736 if !firstDevice {
737 //if at least one valid device was found cancel also a possibly running download to adapter and remove the image
738 // this is to be done after the upgradeOnu cancel activities in order to not subduct the file for still running processes
739 oo.pFileManager.CancelDownload(ctx, imageIdentifier)
740 }
741 pImageResp := &loResponse
742 return pImageResp, nil
743 }
744 return nil, errors.New("invalid image upgrade abort parameters")
mpagenko83144272021-04-27 10:06:22 +0000745}
746
khenaidoo7d3c5582021-08-11 18:09:44 -0400747// GetOnuImages retrieves the ONU SW image status information via OMCI
748func (oo *OpenONUAC) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) {
749 logger.Infow(ctx, "Get_onu_images", log.Fields{"device-id": id.Id})
750 if handler := oo.getDeviceHandler(ctx, id.Id, false); handler != nil {
Himani Chawla69992ab2021-07-08 15:13:02 +0530751 images, err := handler.getOnuImages(ctx)
752 if err == nil {
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000753 return images, nil
754 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400755 return nil, fmt.Errorf(fmt.Sprintf("%s-%s", err, id.Id))
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000756 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400757 logger.Warnw(ctx, "no handler found for Get_onu_images", log.Fields{"device-id": id.Id})
758 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", id.Id))
mpagenko83144272021-04-27 10:06:22 +0000759}
760
khenaidoo7d3c5582021-08-11 18:09:44 -0400761// ActivateOnuImage initiates the activation of the image for the requested ONU(s)
mpagenkoc26d4c02021-05-06 14:27:57 +0000762// precondition: image downloaded and not yet activated or image refers to current inactive image
khenaidoo7d3c5582021-08-11 18:09:44 -0400763func (oo *OpenONUAC) ActivateOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000764 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
765 loResponse := voltha.DeviceImageResponse{}
766 imageIdentifier := (*in).Version
767 //let the deviceHandler find the adequate way of requesting the image activation
768 for _, pCommonID := range (*in).DeviceId {
769 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000770 loDeviceImageState := voltha.DeviceImageState{}
771 loDeviceImageState.DeviceId = loDeviceID
772 loImageState := voltha.ImageState{}
773 loDeviceImageState.ImageState = &loImageState
774 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000775 //compared to download procedure the vendorID (from device) is secondary here
776 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
777 // start image activation activity for each possible device
778 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
779 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
780 logger.Debugw(ctx, "onu image activation requested", log.Fields{
781 "image-id": imageIdentifier, "device-id": loDeviceID})
782 //onu activation handling called in background without immediate error evaluation here
783 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
784 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000785 if pImageStates, err := handler.onuSwActivateRequest(ctx, imageIdentifier, (*in).CommitOnSuccess); err != nil {
786 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
787 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
788 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
789 } else {
790 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
791 loDeviceImageState.ImageState.Reason = pImageStates.Reason
792 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
793 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000794 } else {
795 //cannot start SW activation for requested device
796 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000797 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000798 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
799 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000800 }
mpagenko2f2f2362021-06-07 08:25:22 +0000801 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000802 }
803 pImageResp := &loResponse
804 return pImageResp, nil
805 }
806 return nil, errors.New("invalid image activation parameters")
mpagenko83144272021-04-27 10:06:22 +0000807}
808
khenaidoo7d3c5582021-08-11 18:09:44 -0400809// CommitOnuImage enforces the commitment of the image for the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000810// precondition: image activated and not yet committed
khenaidoo7d3c5582021-08-11 18:09:44 -0400811func (oo *OpenONUAC) CommitOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000812 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
813 loResponse := voltha.DeviceImageResponse{}
814 imageIdentifier := (*in).Version
815 //let the deviceHandler find the adequate way of requesting the image activation
816 for _, pCommonID := range (*in).DeviceId {
817 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000818 loDeviceImageState := voltha.DeviceImageState{}
819 loDeviceImageState.DeviceId = loDeviceID
820 loImageState := voltha.ImageState{}
821 loDeviceImageState.ImageState = &loImageState
822 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000823 //compared to download procedure the vendorID (from device) is secondary here
824 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
825 // start image activation activity for each possible device
826 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
827 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
828 logger.Debugw(ctx, "onu image commitment requested", log.Fields{
829 "image-id": imageIdentifier, "device-id": loDeviceID})
830 //onu commitment handling called in background without immediate error evaluation here
831 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
832 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000833 if pImageStates, err := handler.onuSwCommitRequest(ctx, imageIdentifier); err != nil {
mpagenko38662d02021-08-11 09:45:19 +0000834 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
835 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //can be multiple reasons here
mpagenko183647c2021-06-08 15:25:04 +0000836 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
837 } else {
838 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
839 loDeviceImageState.ImageState.Reason = pImageStates.Reason
840 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
841 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000842 } else {
843 //cannot start SW commitment for requested device
844 logger.Warnw(ctx, "no handler found for image commitment", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000845 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000846 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
847 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000848 }
mpagenko2f2f2362021-06-07 08:25:22 +0000849 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000850 }
851 pImageResp := &loResponse
852 return pImageResp, nil
853 }
854 return nil, errors.New("invalid image commitment parameters")
mpagenko83144272021-04-27 10:06:22 +0000855}
856
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000857// Adapter interface required methods ################ end #########
858// #################################################################
khenaidoo7d3c5582021-08-11 18:09:44 -0400859
860/*
861 *
862 * ONU inter adapter service
863 *
864 */
865
866// OnuIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400867func (oo *OpenONUAC) OnuIndication(ctx context.Context, onuInd *ia.OnuIndicationMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400868 logger.Debugw(ctx, "onu-indication", log.Fields{"onu-indication": onuInd})
869
870 if onuInd == nil || onuInd.OnuIndication == nil {
871 return nil, fmt.Errorf("invalid-onu-indication-%v", onuInd)
872 }
873
874 onuIndication := onuInd.OnuIndication
875 onuOperstate := onuIndication.GetOperState()
876 waitForDhInstPresent := false
877 if onuOperstate == "up" {
878 //Race condition (relevant in BBSIM-environment only): Due to unsynchronized processing of olt-adapter and rw_core,
879 //ONU_IND_REQUEST msg by olt-adapter could arrive a little bit earlier than rw_core was able to announce the corresponding
880 //ONU by RPC of Adopt_device(). Therefore it could be necessary to wait with processing of ONU_IND_REQUEST until call of
881 //Adopt_device() arrived and DeviceHandler instance was created
882 waitForDhInstPresent = true
883 }
884 if handler := oo.getDeviceHandler(ctx, onuInd.DeviceId, waitForDhInstPresent); handler != nil {
885 logger.Infow(ctx, "onu-ind-request", log.Fields{"device-id": onuInd.DeviceId,
886 "OnuId": onuIndication.GetOnuId(),
887 "AdminState": onuIndication.GetAdminState(), "OperState": onuOperstate,
888 "SNR": onuIndication.GetSerialNumber()})
889
890 if onuOperstate == "up" {
891 if err := handler.createInterface(ctx, onuIndication); err != nil {
892 return nil, err
893 }
894 return &empty.Empty{}, nil
895 } else if (onuOperstate == "down") || (onuOperstate == "unreachable") {
896 return nil, handler.updateInterface(ctx, onuIndication)
897 } else {
898 logger.Errorw(ctx, "unknown-onu-ind-request operState", log.Fields{"OnuId": onuIndication.GetOnuId()})
899 return nil, fmt.Errorf("invalidOperState: %s, %s", onuOperstate, onuInd.DeviceId)
900 }
901 }
902 logger.Warnw(ctx, "no handler found for received onu-ind-request", log.Fields{
903 "msgToDeviceId": onuInd.DeviceId})
904 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", onuInd.DeviceId))
905}
906
907// OmciIndication is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400908func (oo *OpenONUAC) OmciIndication(ctx context.Context, msg *ia.OmciMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400909 logger.Debugw(ctx, "omci-response", log.Fields{"parent-device-id": msg.ParentDeviceId, "child-device-id": msg.ChildDeviceId})
910
911 if handler := oo.getDeviceHandler(ctx, msg.ChildDeviceId, false); handler != nil {
912 if err := handler.handleOMCIIndication(log.WithSpanFromContext(context.Background(), ctx), msg); err != nil {
913 return nil, err
914 }
915 return &empty.Empty{}, nil
916 }
917 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", msg.ChildDeviceId))
918}
919
920// DownloadTechProfile is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400921func (oo *OpenONUAC) DownloadTechProfile(ctx context.Context, tProfile *ia.TechProfileDownloadMessage) (*empty.Empty, error) {
Holger Hildebrandtba6fbe82021-12-03 14:29:42 +0000922 logger.Debugw(ctx, "download-tech-profile", log.Fields{"device-id": tProfile.DeviceId, "uni-id": tProfile.UniId})
khenaidoo7d3c5582021-08-11 18:09:44 -0400923
924 if handler := oo.getDeviceHandler(ctx, tProfile.DeviceId, false); handler != nil {
925 if err := handler.handleTechProfileDownloadRequest(log.WithSpanFromContext(context.Background(), ctx), tProfile); err != nil {
926 return nil, err
927 }
928 return &empty.Empty{}, nil
929 }
930 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tProfile.DeviceId))
931}
932
933// DeleteGemPort is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400934func (oo *OpenONUAC) DeleteGemPort(ctx context.Context, gPort *ia.DeleteGemPortMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -0400935 logger.Debugw(ctx, "delete-gem-port", log.Fields{"device-id": gPort.DeviceId, "uni-id": gPort.UniId})
936
937 if handler := oo.getDeviceHandler(ctx, gPort.DeviceId, false); handler != nil {
938 if err := handler.handleDeleteGemPortRequest(log.WithSpanFromContext(context.Background(), ctx), gPort); err != nil {
939 return nil, err
940 }
Holger Hildebrandt5b774062021-11-10 10:24:29 +0000941 } else {
942 logger.Debugw(ctx, "deviceHandler not found", log.Fields{"device-id": gPort.DeviceId})
943 // delete requests for objects of an already deleted ONU should be acknowledged positively - continue
khenaidoo7d3c5582021-08-11 18:09:44 -0400944 }
Holger Hildebrandt5b774062021-11-10 10:24:29 +0000945 return &empty.Empty{}, nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400946}
947
948// DeleteTCont is part of the ONU Inter-adapter service API.
khenaidoo42dcdfd2021-10-19 17:34:12 -0400949func (oo *OpenONUAC) DeleteTCont(ctx context.Context, tConf *ia.DeleteTcontMessage) (*empty.Empty, error) {
Holger Hildebrandtba6fbe82021-12-03 14:29:42 +0000950 logger.Debugw(ctx, "delete-tcont", log.Fields{"device-id": tConf.DeviceId, "tconf": tConf})
khenaidoo7d3c5582021-08-11 18:09:44 -0400951
952 if handler := oo.getDeviceHandler(ctx, tConf.DeviceId, false); handler != nil {
953 if err := handler.handleDeleteTcontRequest(log.WithSpanFromContext(context.Background(), ctx), tConf); err != nil {
954 return nil, err
955 }
Holger Hildebrandt5b774062021-11-10 10:24:29 +0000956 } else {
957 logger.Debugw(ctx, "deviceHandler not found", log.Fields{"device-id": tConf.DeviceId})
958 // delete requests for objects of an already deleted ONU should be acknowledged positively - continue
khenaidoo7d3c5582021-08-11 18:09:44 -0400959 }
Holger Hildebrandt5b774062021-11-10 10:24:29 +0000960 return &empty.Empty{}, nil
khenaidoo7d3c5582021-08-11 18:09:44 -0400961}
962
963/*
964 * Parent GRPC clients
965 */
966
967func (oo *OpenONUAC) setupParentInterAdapterClient(ctx context.Context, endpoint string) error {
968 logger.Infow(ctx, "setting-parent-adapter-connection", log.Fields{"parent-endpoint": endpoint})
969 oo.lockParentAdapterClients.Lock()
970 defer oo.lockParentAdapterClients.Unlock()
971 if _, ok := oo.parentAdapterClients[endpoint]; ok {
972 return nil
973 }
974
975 childClient, err := vgrpc.NewClient(endpoint,
976 oo.oltAdapterRestarted,
977 vgrpc.ActivityCheck(true))
978
979 if err != nil {
980 return err
981 }
982
983 oo.parentAdapterClients[endpoint] = childClient
984
985 go oo.parentAdapterClients[endpoint].Start(log.WithSpanFromContext(context.TODO(), ctx), setAndTestAdapterServiceHandler)
986
987 // Wait until we have a connection to the child adapter.
988 // Unlimited retries or until context expires
989 subCtx := log.WithSpanFromContext(context.TODO(), ctx)
990 backoff := vgrpc.NewBackoff(oo.config.MinBackoffRetryDelay, oo.config.MaxBackoffRetryDelay, 0)
991 for {
992 client, err := oo.parentAdapterClients[endpoint].GetOltInterAdapterServiceClient()
993 if err == nil && client != nil {
994 logger.Infow(subCtx, "connected-to-parent-adapter", log.Fields{"parent-endpoint": endpoint})
995 break
996 }
997 logger.Warnw(subCtx, "connection-to-parent-adapter-not-ready", log.Fields{"error": err, "parent-endpoint": endpoint})
998 // Backoff
999 if err = backoff.Backoff(subCtx); err != nil {
1000 logger.Errorw(subCtx, "received-error-on-backoff", log.Fields{"error": err, "parent-endpoint": endpoint})
1001 break
1002 }
1003 }
1004 return nil
1005}
1006
khenaidoo42dcdfd2021-10-19 17:34:12 -04001007func (oo *OpenONUAC) getParentAdapterServiceClient(endpoint string) (olt_inter_adapter_service.OltInterAdapterServiceClient, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001008 // First check from cache
1009 oo.lockParentAdapterClients.RLock()
1010 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1011 oo.lockParentAdapterClients.RUnlock()
1012 return pgClient.GetOltInterAdapterServiceClient()
1013 }
1014 oo.lockParentAdapterClients.RUnlock()
1015
1016 // Set the parent connection - can occur on restarts
1017 ctx, cancel := context.WithTimeout(context.Background(), oo.config.RPCTimeout)
1018 err := oo.setupParentInterAdapterClient(ctx, endpoint)
1019 cancel()
1020 if err != nil {
1021 return nil, err
1022 }
1023
1024 // Get the parent client now
1025 oo.lockParentAdapterClients.RLock()
1026 defer oo.lockParentAdapterClients.RUnlock()
1027 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1028 return pgClient.GetOltInterAdapterServiceClient()
1029 }
1030
1031 return nil, fmt.Errorf("no-client-for-endpoint-%s", endpoint)
1032}
1033
1034// TODO: Any action the adapter needs to do following an olt adapter restart?
1035func (oo *OpenONUAC) oltAdapterRestarted(ctx context.Context, endPoint string) error {
1036 logger.Errorw(ctx, "olt-adapter-restarted", log.Fields{"endpoint": endPoint})
1037 return nil
1038}
1039
1040// setAndTestAdapterServiceHandler is used to test whether the remote gRPC service is up
1041func setAndTestAdapterServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
khenaidoo42dcdfd2021-10-19 17:34:12 -04001042 svc := olt_inter_adapter_service.NewOltInterAdapterServiceClient(conn)
1043 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != health.HealthStatus_HEALTHY {
khenaidoo7d3c5582021-08-11 18:09:44 -04001044 return nil
1045 }
1046 return svc
1047}
1048
Holger Hildebrandtc69f0742021-11-16 13:48:00 +00001049func (oo *OpenONUAC) forceDeleteDeviceKvData(ctx context.Context, aDeviceID string) error {
1050 logger.Debugw(ctx, "force deletion of ONU device specific data in kv store", log.Fields{"device-id": aDeviceID})
1051
1052 for i := range onuKvStorePathPrefixes {
1053 baseKvStorePath := fmt.Sprintf(onuKvStorePathPrefixes[i], oo.cm.Backend.PathPrefix)
1054 logger.Debugw(ctx, "SetKVStoreBackend", log.Fields{"IpTarget": oo.KVStoreAddress, "BasePathKvStore": baseKvStorePath,
1055 "device-id": aDeviceID})
1056 kvbackend := &db.Backend{
1057 Client: oo.kvClient,
1058 StoreType: oo.KVStoreType,
1059 Address: oo.KVStoreAddress,
1060 Timeout: oo.KVStoreTimeout,
1061 PathPrefix: baseKvStorePath}
1062
1063 if kvbackend == nil {
1064 logger.Errorw(ctx, "Can't access onuKVStore - no backend connection to service", log.Fields{"service": baseKvStorePath, "device-id": aDeviceID})
1065 return fmt.Errorf("can-not-access-onuKVStore-no-backend-connection-to-service")
1066 }
1067 err := kvbackend.DeleteWithPrefix(ctx, aDeviceID)
1068 if err != nil {
1069 logger.Errorw(ctx, "unable to delete in KVstore", log.Fields{"service": baseKvStorePath, "device-id": aDeviceID, "err": err})
1070 return fmt.Errorf("unable-to-delete-in-KVstore")
1071 }
1072 }
1073 return nil
1074}
1075
khenaidoo7d3c5582021-08-11 18:09:44 -04001076/*
1077 *
1078 * Unimplemented APIs
1079 *
1080 */
1081
1082//GetOfpDeviceInfo returns OFP information for the given device. Method not implemented as per [VOL-3202].
1083// OF port info is now to be delivered within UniPort create cmp changes in onu_uni_port.go::CreateVolthaPort()
1084//
khenaidoo42dcdfd2021-10-19 17:34:12 -04001085func (oo *OpenONUAC) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001086 return nil, errors.New("unImplemented")
1087}
1088
1089//SimulateAlarm is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001090func (oo *OpenONUAC) SimulateAlarm(context.Context, *ca.SimulateAlarmMessage) (*common.OperationResp, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001091 return nil, errors.New("unImplemented")
1092}
1093
1094//SetExtValue is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001095func (oo *OpenONUAC) SetExtValue(context.Context, *ca.SetExtValueMessage) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001096 return nil, errors.New("unImplemented")
1097}
1098
1099//SetSingleValue is unimplemented
1100func (oo *OpenONUAC) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
1101 return nil, errors.New("unImplemented")
1102}
1103
1104//StartOmciTest not implemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001105func (oo *OpenONUAC) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001106 return nil, errors.New("unImplemented")
1107}
1108
1109//SuppressEvent unimplemented
1110func (oo *OpenONUAC) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1111 return nil, errors.New("unImplemented")
1112}
1113
1114//UnSuppressEvent unimplemented
1115func (oo *OpenONUAC) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1116 return nil, errors.New("unImplemented")
1117}
1118
1119//GetImageDownloadStatus is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001120func (oo *OpenONUAC) GetImageDownloadStatus(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001121 return nil, errors.New("unImplemented")
1122}
1123
1124//CancelImageDownload is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001125func (oo *OpenONUAC) CancelImageDownload(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001126 return nil, errors.New("unImplemented")
1127}
1128
1129//RevertImageUpdate is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001130func (oo *OpenONUAC) RevertImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001131 return nil, errors.New("unImplemented")
1132}
1133
1134// UpdateFlowsBulk is unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001135func (oo *OpenONUAC) UpdateFlowsBulk(ctx context.Context, flows *ca.BulkFlows) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001136 return nil, errors.New("unImplemented")
1137}
1138
1139//SelfTestDevice unimplented
1140func (oo *OpenONUAC) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
1141 return nil, errors.New("unImplemented")
1142}
1143
1144//SendPacketOut sends packet out to the device
khenaidoo42dcdfd2021-10-19 17:34:12 -04001145func (oo *OpenONUAC) SendPacketOut(ctx context.Context, packet *ca.PacketOut) (*empty.Empty, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001146 return nil, errors.New("unImplemented")
1147}
1148
1149// EnablePort to Enable PON/NNI interface - seems not to be used/required according to python code
1150func (oo *OpenONUAC) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1151 return nil, errors.New("unImplemented")
1152}
1153
1154// DisablePort to Disable pon/nni interface - seems not to be used/required according to python code
1155func (oo *OpenONUAC) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1156 return nil, errors.New("unImplemented")
1157}
1158
1159// GetExtValue - unimplemented
khenaidoo42dcdfd2021-10-19 17:34:12 -04001160func (oo *OpenONUAC) GetExtValue(ctx context.Context, extInfo *ca.GetExtValueMessage) (*extension.ReturnValues, error) {
khenaidoo7d3c5582021-08-11 18:09:44 -04001161 return nil, errors.New("unImplemented")
1162}
1163
1164// ChildDeviceLost - unimplemented
1165func (oo *OpenONUAC) ChildDeviceLost(ctx context.Context, childDevice *voltha.Device) (*empty.Empty, error) {
1166 return nil, errors.New("unImplemented")
1167}
Holger Hildebrandt4b5e73f2021-08-19 06:51:21 +00001168
1169// GetSupportedFsms - TODO: add comment
1170func (oo *OpenONUAC) GetSupportedFsms() *cmn.OmciDeviceFsms {
1171 return oo.pSupportedFsms
1172}
1173
1174// LockMutexMibTemplateGenerated - TODO: add comment
1175func (oo *OpenONUAC) LockMutexMibTemplateGenerated() {
1176 oo.mutexMibTemplateGenerated.Lock()
1177}
1178
1179// UnlockMutexMibTemplateGenerated - TODO: add comment
1180func (oo *OpenONUAC) UnlockMutexMibTemplateGenerated() {
1181 oo.mutexMibTemplateGenerated.Unlock()
1182}
1183
1184// GetMibTemplatesGenerated - TODO: add comment
1185func (oo *OpenONUAC) GetMibTemplatesGenerated(mibTemplatePath string) (value bool, exist bool) {
1186 value, exist = oo.mibTemplatesGenerated[mibTemplatePath]
1187 return value, exist
1188}
1189
1190// SetMibTemplatesGenerated - TODO: add comment
1191func (oo *OpenONUAC) SetMibTemplatesGenerated(mibTemplatePath string, value bool) {
1192 oo.mibTemplatesGenerated[mibTemplatePath] = value
1193}
1194
1195// RLockMutexDeviceHandlersMap - TODO: add comment
1196func (oo *OpenONUAC) RLockMutexDeviceHandlersMap() {
1197 oo.mutexDeviceHandlersMap.RLock()
1198}
1199
1200// RUnlockMutexDeviceHandlersMap - TODO: add comment
1201func (oo *OpenONUAC) RUnlockMutexDeviceHandlersMap() {
1202 oo.mutexDeviceHandlersMap.RUnlock()
1203}
1204
1205// GetDeviceHandler - TODO: add comment
1206func (oo *OpenONUAC) GetDeviceHandler(deviceID string) (value cmn.IdeviceHandler, exist bool) {
1207 value, exist = oo.deviceHandlers[deviceID]
1208 return value, exist
1209}