blob: eecd002177c5e2a401fa230787865d623f05b400 [file] [log] [blame]
Holger Hildebrandtfa074992020-03-27 15:42:06 +00001/*
2 * Copyright 2020-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +000017//Package adaptercoreonu provides the utility for onu devices, flows and statistics
18package adaptercoreonu
Holger Hildebrandtfa074992020-03-27 15:42:06 +000019
20import (
21 "context"
22 "errors"
23 "fmt"
24 "sync"
25 "time"
26
khenaidoo7d3c5582021-08-11 18:09:44 -040027 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
28 "github.com/opencord/voltha-protos/v5/go/adapter_services"
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"
32 "google.golang.org/grpc"
33
34 "github.com/golang/protobuf/ptypes/empty"
35 "github.com/opencord/voltha-lib-go/v7/pkg/db/kvstore"
36 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
37 "github.com/opencord/voltha-lib-go/v7/pkg/log"
38 "github.com/opencord/voltha-protos/v5/go/extension"
39 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
40 "github.com/opencord/voltha-protos/v5/go/voltha"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000041
Matteo Scandolo761f7512020-11-23 15:52:40 -080042 "github.com/opencord/voltha-openonu-adapter-go/internal/pkg/config"
Holger Hildebrandtfa074992020-03-27 15:42:06 +000043)
44
45//OpenONUAC structure holds the ONU core information
46type OpenONUAC struct {
Himani Chawla6d2ae152020-09-02 13:11:20 +053047 deviceHandlers map[string]*deviceHandler
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000048 deviceHandlersCreateChan map[string]chan bool //channels for deviceHandler create events
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000049 lockDeviceHandlersMap sync.RWMutex
khenaidoo7d3c5582021-08-11 18:09:44 -040050 coreClient *vgrpc.Client
51 parentAdapterClients map[string]*vgrpc.Client
52 lockParentAdapterClients sync.RWMutex
Himani Chawlac07fda02020-12-09 16:21:21 +053053 eventProxy eventif.EventProxy
mpagenkoaf801632020-07-03 10:00:42 +000054 kvClient kvstore.Client
Matteo Scandolof1f39a72020-11-24 12:08:11 -080055 cm *conf.ConfigManager
Holger Hildebrandtfa074992020-03-27 15:42:06 +000056 config *config.AdapterFlags
57 numOnus int
Matteo Scandolo127c59d2021-01-28 11:31:18 -080058 KVStoreAddress string
Holger Hildebrandtfa074992020-03-27 15:42:06 +000059 KVStoreType string
mpagenkoaf801632020-07-03 10:00:42 +000060 KVStoreTimeout time.Duration
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000061 mibTemplatesGenerated map[string]bool
62 lockMibTemplateGenerated sync.RWMutex
Holger Hildebrandtfa074992020-03-27 15:42:06 +000063 exitChannel chan int
64 HeartbeatCheckInterval time.Duration
65 HeartbeatFailReportInterval time.Duration
mpagenkodff5dda2020-08-28 11:52:01 +000066 AcceptIncrementalEvto bool
Holger Hildebrandtfa074992020-03-27 15:42:06 +000067 //GrpcTimeoutInterval time.Duration
Himani Chawlad96df182020-09-28 11:12:02 +053068 pSupportedFsms *OmciDeviceFsms
69 maxTimeoutInterAdapterComm time.Duration
Holger Hildebrandt38985dc2021-02-18 16:25:20 +000070 maxTimeoutReconciling time.Duration
mpagenkoc8bba412021-01-15 15:38:44 +000071 pDownloadManager *adapterDownloadManager
mpagenkoc26d4c02021-05-06 14:27:57 +000072 pFileManager *fileDownloadManager //let coexist 'old and new' DownloadManager as long as 'old' does not get obsolete
Girish Gowdraaf0ad632021-01-27 13:00:01 -080073 metricsEnabled bool
Holger Hildebrandte3677f12021-02-05 14:50:56 +000074 mibAuditInterval time.Duration
Girish Gowdra0b235842021-03-09 13:06:46 -080075 omciTimeout int // in seconds
Himani Chawla075f1642021-03-15 19:23:24 +053076 alarmAuditInterval time.Duration
mpagenkoc26d4c02021-05-06 14:27:57 +000077 dlToOnuTimeout4M time.Duration
khenaidoo7d3c5582021-08-11 18:09:44 -040078 rpcTimeout time.Duration
Holger Hildebrandtfa074992020-03-27 15:42:06 +000079}
80
81//NewOpenONUAC returns a new instance of OpenONU_AC
khenaidoo7d3c5582021-08-11 18:09:44 -040082func NewOpenONUAC(ctx context.Context, coreClient *vgrpc.Client, eventProxy eventif.EventProxy,
83 kvClient kvstore.Client, cfg *config.AdapterFlags, cm *conf.ConfigManager) *OpenONUAC {
Holger Hildebrandtfa074992020-03-27 15:42:06 +000084 var openOnuAc OpenONUAC
85 openOnuAc.exitChannel = make(chan int, 1)
Himani Chawla6d2ae152020-09-02 13:11:20 +053086 openOnuAc.deviceHandlers = make(map[string]*deviceHandler)
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +000087 openOnuAc.deviceHandlersCreateChan = make(map[string]chan bool)
khenaidoo7d3c5582021-08-11 18:09:44 -040088 openOnuAc.parentAdapterClients = make(map[string]*vgrpc.Client)
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000089 openOnuAc.lockDeviceHandlersMap = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +000090 openOnuAc.config = cfg
Matteo Scandolof1f39a72020-11-24 12:08:11 -080091 openOnuAc.cm = cm
khenaidoo7d3c5582021-08-11 18:09:44 -040092 openOnuAc.coreClient = coreClient
Holger Hildebrandtfa074992020-03-27 15:42:06 +000093 openOnuAc.numOnus = cfg.OnuNumber
Holger Hildebrandtfa074992020-03-27 15:42:06 +000094 openOnuAc.eventProxy = eventProxy
mpagenkoaf801632020-07-03 10:00:42 +000095 openOnuAc.kvClient = kvClient
Matteo Scandolo127c59d2021-01-28 11:31:18 -080096 openOnuAc.KVStoreAddress = cfg.KVStoreAddress
Holger Hildebrandtfa074992020-03-27 15:42:06 +000097 openOnuAc.KVStoreType = cfg.KVStoreType
mpagenkoaf801632020-07-03 10:00:42 +000098 openOnuAc.KVStoreTimeout = cfg.KVStoreTimeout
Holger Hildebrandt61b24d02020-11-16 13:36:40 +000099 openOnuAc.mibTemplatesGenerated = make(map[string]bool)
100 openOnuAc.lockMibTemplateGenerated = sync.RWMutex{}
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000101 openOnuAc.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
102 openOnuAc.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
mpagenkodff5dda2020-08-28 11:52:01 +0000103 openOnuAc.AcceptIncrementalEvto = cfg.AccIncrEvto
Himani Chawlad96df182020-09-28 11:12:02 +0530104 openOnuAc.maxTimeoutInterAdapterComm = cfg.MaxTimeoutInterAdapterComm
Holger Hildebrandt38985dc2021-02-18 16:25:20 +0000105 openOnuAc.maxTimeoutReconciling = cfg.MaxTimeoutReconciling
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000106 //openOnuAc.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
Girish Gowdraaf0ad632021-01-27 13:00:01 -0800107 openOnuAc.metricsEnabled = cfg.MetricsEnabled
Holger Hildebrandte3677f12021-02-05 14:50:56 +0000108 openOnuAc.mibAuditInterval = cfg.MibAuditInterval
Girish Gowdra0b235842021-03-09 13:06:46 -0800109 // since consumers of OMCI timeout value everywhere in code is in "int seconds", do this useful conversion
110 openOnuAc.omciTimeout = int(cfg.OmciTimeout.Seconds())
Himani Chawla075f1642021-03-15 19:23:24 +0530111 openOnuAc.alarmAuditInterval = cfg.AlarmAuditInterval
mpagenkoc26d4c02021-05-06 14:27:57 +0000112 openOnuAc.dlToOnuTimeout4M = cfg.DownloadToOnuTimeout4MB
khenaidoo7d3c5582021-08-11 18:09:44 -0400113 openOnuAc.rpcTimeout = cfg.RPCTimeout
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000114
115 openOnuAc.pSupportedFsms = &OmciDeviceFsms{
116 "mib-synchronizer": {
117 //mibSyncFsm, // Implements the MIB synchronization state machine
Himani Chawla6d2ae152020-09-02 13:11:20 +0530118 mibDbVolatileDictImpl, // Implements volatile ME MIB database
Himani Chawla4d908332020-08-31 12:30:20 +0530119 //true, // Advertise events on OpenOMCI event bus
Holger Hildebrandte3677f12021-02-05 14:50:56 +0000120 openOnuAc.mibAuditInterval, // Time to wait between MIB audits. 0 to disable audits.
Holger Hildebrandt0f9b88d2020-04-20 13:33:25 +0000121 // map[string]func() error{
122 // "mib-upload": onuDeviceEntry.MibUploadTask,
123 // "mib-template": onuDeviceEntry.MibTemplateTask,
124 // "get-mds": onuDeviceEntry.GetMdsTask,
125 // "mib-audit": onuDeviceEntry.GetMdsTask,
126 // "mib-resync": onuDeviceEntry.MibResyncTask,
127 // "mib-reconcile": onuDeviceEntry.MibReconcileTask,
128 // },
129 },
130 }
131
mpagenkoc8bba412021-01-15 15:38:44 +0000132 openOnuAc.pDownloadManager = newAdapterDownloadManager(ctx)
mpagenkoc26d4c02021-05-06 14:27:57 +0000133 openOnuAc.pFileManager = newFileDownloadManager(ctx)
134 openOnuAc.pFileManager.SetDownloadTimeout(ctx, cfg.DownloadToAdapterTimeout)
mpagenkoc8bba412021-01-15 15:38:44 +0000135
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000136 return &openOnuAc
137}
138
139//Start starts (logs) the adapter
140func (oo *OpenONUAC) Start(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000141 logger.Info(ctx, "starting-openonu-adapter")
mpagenkoc8bba412021-01-15 15:38:44 +0000142
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000143 return nil
144}
145
Himani Chawla6d2ae152020-09-02 13:11:20 +0530146/*
147//stop terminates the session
148func (oo *OpenONUAC) stop(ctx context.Context) error {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000149 logger.Info(ctx,"stopping-device-manager")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000150 oo.exitChannel <- 1
dbainbri4d3a0dc2020-12-02 00:33:42 +0000151 logger.Info(ctx,"device-manager-stopped")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000152 return nil
153}
Himani Chawla6d2ae152020-09-02 13:11:20 +0530154*/
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000155
Himani Chawla6d2ae152020-09-02 13:11:20 +0530156func (oo *OpenONUAC) addDeviceHandlerToMap(ctx context.Context, agent *deviceHandler) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000157 oo.lockDeviceHandlersMap.Lock()
158 defer oo.lockDeviceHandlersMap.Unlock()
159 if _, exist := oo.deviceHandlers[agent.deviceID]; !exist {
160 oo.deviceHandlers[agent.deviceID] = agent
Himani Chawla6d2ae152020-09-02 13:11:20 +0530161 oo.deviceHandlers[agent.deviceID].start(ctx)
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000162 if _, exist := oo.deviceHandlersCreateChan[agent.deviceID]; exist {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000163 logger.Debugw(ctx, "deviceHandler created - trigger processing of pending ONU_IND_REQUEST", log.Fields{"device-id": agent.deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000164 oo.deviceHandlersCreateChan[agent.deviceID] <- true
165 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000166 }
167}
168
Himani Chawla6d2ae152020-09-02 13:11:20 +0530169func (oo *OpenONUAC) deleteDeviceHandlerToMap(agent *deviceHandler) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000170 oo.lockDeviceHandlersMap.Lock()
171 defer oo.lockDeviceHandlersMap.Unlock()
172 delete(oo.deviceHandlers, agent.deviceID)
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000173 delete(oo.deviceHandlersCreateChan, agent.deviceID)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000174}
175
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000176//getDeviceHandler gets the ONU deviceHandler and may wait until it is created
dbainbri4d3a0dc2020-12-02 00:33:42 +0000177func (oo *OpenONUAC) getDeviceHandler(ctx context.Context, deviceID string, aWait bool) *deviceHandler {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000178 oo.lockDeviceHandlersMap.Lock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000179 agent, ok := oo.deviceHandlers[deviceID]
180 if aWait && !ok {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000181 logger.Infow(ctx, "Race condition: deviceHandler not present - wait for creation or timeout",
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000182 log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000183 if _, exist := oo.deviceHandlersCreateChan[deviceID]; !exist {
184 oo.deviceHandlersCreateChan[deviceID] = make(chan bool, 1)
185 }
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800186 deviceCreateChan := oo.deviceHandlersCreateChan[deviceID]
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000187 //keep the read sema short to allow for subsequent write
188 oo.lockDeviceHandlersMap.Unlock()
189 // based on concurrent processing the deviceHandler creation may not yet be finished at his point
190 // so it might be needed to wait here for that event with some timeout
191 select {
192 case <-time.After(1 * time.Second): //timer may be discussed ...
dbainbri4d3a0dc2020-12-02 00:33:42 +0000193 logger.Warnw(ctx, "No valid deviceHandler created after max WaitTime", log.Fields{"device-id": deviceID})
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000194 return nil
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800195 case <-deviceCreateChan:
dbainbri4d3a0dc2020-12-02 00:33:42 +0000196 logger.Debugw(ctx, "deviceHandler is ready now - continue", log.Fields{"device-id": deviceID})
Girish Gowdra7407a4d2020-11-12 12:44:53 -0800197 oo.lockDeviceHandlersMap.RLock()
198 defer oo.lockDeviceHandlersMap.RUnlock()
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000199 return oo.deviceHandlers[deviceID]
200 }
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000201 }
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000202 oo.lockDeviceHandlersMap.Unlock()
203 return agent
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000204}
205
khenaidoo7d3c5582021-08-11 18:09:44 -0400206// GetHealthStatus is used as a service readiness validation as a grpc connection
207func (oo *OpenONUAC) GetHealthStatus(ctx context.Context, empty *empty.Empty) (*voltha.HealthStatus, error) {
208 return &voltha.HealthStatus{State: voltha.HealthStatus_HEALTHY}, nil
Holger Hildebrandt6c1fb0a2020-11-25 15:41:01 +0000209}
210
khenaidoo7d3c5582021-08-11 18:09:44 -0400211// AdoptDevice creates a new device handler if not present already and then adopts the device
212func (oo *OpenONUAC) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000213 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000214 logger.Warn(ctx, "voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400215 return nil, errors.New("nil-device")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000216 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000217 logger.Infow(ctx, "adopt-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530218 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000219 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400220 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000221 oo.addDeviceHandlerToMap(ctx, handler)
khenaidoo7d3c5582021-08-11 18:09:44 -0400222
223 // Setup the grpc communication with the parent adapter
224 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
225 // TODO: Cleanup on failure needed
226 return nil, err
227 }
228
229 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), device)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000230 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400231 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000232}
233
khenaidoo7d3c5582021-08-11 18:09:44 -0400234//ReconcileDevice is called once when the adapter needs to re-create device - usually on core restart
235func (oo *OpenONUAC) ReconcileDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000236 if device == nil {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000237 logger.Warn(ctx, "reconcile-device-voltha-device-is-nil")
khenaidoo7d3c5582021-08-11 18:09:44 -0400238 return nil, errors.New("nil-device")
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000239 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000240 logger.Infow(ctx, "reconcile-device", log.Fields{"device-id": device.Id})
Himani Chawla6d2ae152020-09-02 13:11:20 +0530241 var handler *deviceHandler
dbainbri4d3a0dc2020-12-02 00:33:42 +0000242 if handler = oo.getDeviceHandler(ctx, device.Id, false); handler == nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400243 handler := newDeviceHandler(ctx, oo.coreClient, oo.eventProxy, device, oo)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000244 oo.addDeviceHandlerToMap(ctx, handler)
245 handler.device = device
khenaidoo7d3c5582021-08-11 18:09:44 -0400246 if err := handler.updateDeviceStateInCore(log.WithSpanFromContext(context.Background(), ctx), &ic.DeviceStateFilter{
247 DeviceId: device.Id,
248 OperStatus: voltha.OperStatus_RECONCILING,
249 ConnStatus: device.ConnectStatus,
250 }); err != nil {
251 return nil, fmt.Errorf("not able to update device state to reconciling. Err : %s", err.Error())
Maninderb5187552021-03-23 22:23:42 +0530252 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400253 // Setup the grpc communication with the parent adapter
254 if err := oo.setupParentInterAdapterClient(ctx, device.ProxyAddress.AdapterEndpoint); err != nil {
255 // TODO: Cleanup on failure needed
256 return nil, err
257 }
258
259 handler.startReconciling(log.WithSpanFromContext(context.Background(), ctx), false)
260 go handler.adoptOrReconcileDevice(log.WithSpanFromContext(context.Background(), ctx), handler.device)
Holger Hildebrandtf41a1602020-08-19 09:52:50 +0000261 // reconcilement will be continued after onu-device entry is added
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000262 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400263 return nil, fmt.Errorf(fmt.Sprintf("device-already-reconciled-or-active-%s", device.Id))
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000264 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400265 return &empty.Empty{}, nil
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000266}
267
khenaidoo7d3c5582021-08-11 18:09:44 -0400268//DisableDevice disables the given device
269func (oo *OpenONUAC) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000270 logger.Infow(ctx, "disable-device", log.Fields{"device-id": device.Id})
271 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400272 go handler.disableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
273 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300274 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000275 logger.Warnw(ctx, "no handler found for device-disable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400276 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000277}
278
khenaidoo7d3c5582021-08-11 18:09:44 -0400279//ReEnableDevice enables the onu device after disable
280func (oo *OpenONUAC) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000281 logger.Infow(ctx, "reenable-device", log.Fields{"device-id": device.Id})
282 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400283 go handler.reEnableDevice(log.WithSpanFromContext(context.Background(), ctx), device)
284 return &empty.Empty{}, nil
ozgecanetsiafce57b12020-05-25 14:39:35 +0300285 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000286 logger.Warnw(ctx, "no handler found for device-reenable", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400287 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000288}
289
khenaidoo7d3c5582021-08-11 18:09:44 -0400290//RebootDevice reboots the given device
291func (oo *OpenONUAC) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
dbainbri4d3a0dc2020-12-02 00:33:42 +0000292 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
293 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
khenaidoo7d3c5582021-08-11 18:09:44 -0400294 go handler.rebootDevice(log.WithSpanFromContext(context.Background(), ctx), true, device) //reboot request with device checking
295 return &empty.Empty{}, nil
ozgecanetsiae11479f2020-07-06 09:44:47 +0300296 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000297 logger.Warnw(ctx, "no handler found for device-reboot", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400298 return nil, fmt.Errorf("handler-not-found-for-device: %s", device.Id)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000299}
300
khenaidoo7d3c5582021-08-11 18:09:44 -0400301// DeleteDevice deletes the given device
302func (oo *OpenONUAC) DeleteDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
303 nctx := log.WithSpanFromContext(context.Background(), ctx)
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000304
khenaidoo7d3c5582021-08-11 18:09:44 -0400305 logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id, "SerialNumber": device.SerialNumber, "ctx": ctx, "nctx": nctx})
dbainbri4d3a0dc2020-12-02 00:33:42 +0000306 if handler := oo.getDeviceHandler(ctx, device.Id, false); handler != nil {
Girish Gowdra0e533642021-03-02 22:02:51 -0800307 var errorsList []error
Holger Hildebrandtff05b682021-03-16 15:02:05 +0000308
309 handler.mutexDeletionInProgressFlag.Lock()
310 handler.deletionInProgress = true
311 handler.mutexDeletionInProgressFlag.Unlock()
312
Girish Gowdra0e533642021-03-02 22:02:51 -0800313 if err := handler.deleteDevicePersistencyData(ctx); err != nil {
314 errorsList = append(errorsList, err)
315 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700316
317 // Stop PM, Alarm and Self Test event handler routines
318 if handler.getCollectorIsRunning() {
319 handler.stopCollector <- true
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700320 logger.Debugw(ctx, "sent stop signal to metric collector routine", log.Fields{"device-id": device.Id})
Girish Gowdra10123c02021-08-30 11:52:06 -0700321
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700322 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700323 if handler.getAlarmManagerIsRunning(ctx) {
324 handler.stopAlarmManager <- true
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700325 logger.Debugw(ctx, "sent stop signal to alarm manager", log.Fields{"device-id": device.Id})
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700326 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700327 if handler.pSelfTestHdlr.GetSelfTestHandlerIsRunning() {
328 handler.pSelfTestHdlr.stopSelfTestModule <- true
329 logger.Debugw(ctx, "sent stop signal to self test handler module", log.Fields{"device-id": device.Id})
330 }
331
332 // Clear PM data on the KV store
Girish Gowdra0e533642021-03-02 22:02:51 -0800333 if handler.pOnuMetricsMgr != nil {
334 if err := handler.pOnuMetricsMgr.clearAllPmData(ctx); err != nil {
335 errorsList = append(errorsList, err)
336 }
337 }
Girish Gowdra10123c02021-08-30 11:52:06 -0700338
Holger Hildebrandtf07b44a2020-11-10 13:07:54 +0000339 //don't leave any garbage - even in error case
340 oo.deleteDeviceHandlerToMap(handler)
Girish Gowdra0e533642021-03-02 22:02:51 -0800341 if len(errorsList) > 0 {
342 logger.Errorw(ctx, "one-or-more-error-during-device-delete", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400343 return nil, fmt.Errorf("one-or-more-error-during-device-delete, errors:%v", errorsList)
Girish Gowdra0e533642021-03-02 22:02:51 -0800344 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400345 return &empty.Empty{}, nil
Holger Hildebrandt9ca8b132020-08-07 14:45:03 +0000346 }
dbainbri4d3a0dc2020-12-02 00:33:42 +0000347 logger.Warnw(ctx, "no handler found for device-deletion", log.Fields{"device-id": device.Id})
khenaidoo7d3c5582021-08-11 18:09:44 -0400348 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000349}
350
khenaidoo7d3c5582021-08-11 18:09:44 -0400351//UpdateFlowsIncrementally updates (add/remove) the flows on a given device
352func (oo *OpenONUAC) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ic.IncrementalFlows) (*empty.Empty, error) {
353 logger.Infow(ctx, "update-flows-incrementally", log.Fields{"device-id": incrFlows.Device.Id})
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000354
mpagenkofc4f56e2020-11-04 17:17:49 +0000355 //flow config is relayed to handler even if the device might be in some 'inactive' state
356 // let the handler or related FSM's decide, what to do with the modified flow state info
357 // 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 +0000358
359 // For now, there is no support for group changes (as in the actual Py-adapter code)
mpagenkofc4f56e2020-11-04 17:17:49 +0000360 // but processing is continued for flowUpdate possibly also set in the request
khenaidoo7d3c5582021-08-11 18:09:44 -0400361 if incrFlows.Groups.ToAdd != nil && incrFlows.Groups.ToAdd.Items != nil {
362 logger.Warnw(ctx, "Update-flow-incr: group add not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000363 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400364 if incrFlows.Groups.ToRemove != nil && incrFlows.Groups.ToRemove.Items != nil {
365 logger.Warnw(ctx, "Update-flow-incr: group remove not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000366 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400367 if incrFlows.Groups.ToUpdate != nil && incrFlows.Groups.ToUpdate.Items != nil {
368 logger.Warnw(ctx, "Update-flow-incr: group update not supported (ignored)", log.Fields{"device-id": incrFlows.Device.Id})
mpagenkodff5dda2020-08-28 11:52:01 +0000369 }
370
khenaidoo7d3c5582021-08-11 18:09:44 -0400371 if handler := oo.getDeviceHandler(ctx, incrFlows.Device.Id, false); handler != nil {
372 if err := handler.FlowUpdateIncremental(log.WithSpanFromContext(context.Background(), ctx), incrFlows.Flows, incrFlows.Groups, incrFlows.FlowMetadata); err != nil {
373 return nil, err
374 }
375 return &empty.Empty{}, nil
mpagenkodff5dda2020-08-28 11:52:01 +0000376 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400377 logger.Warnw(ctx, "no handler found for incremental flow update", log.Fields{"device-id": incrFlows.Device.Id})
378 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", incrFlows.Device.Id))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000379}
380
khenaidoo7d3c5582021-08-11 18:09:44 -0400381//UpdatePmConfig returns PmConfigs nil or error
382func (oo *OpenONUAC) UpdatePmConfig(ctx context.Context, configs *ic.PmConfigsInfo) (*empty.Empty, error) {
383 logger.Infow(ctx, "update-pm-config", log.Fields{"device-id": configs.DeviceId})
384 if handler := oo.getDeviceHandler(ctx, configs.DeviceId, false); handler != nil {
385 if err := handler.updatePmConfig(log.WithSpanFromContext(context.Background(), ctx), configs.PmConfigs); err != nil {
386 return nil, err
387 }
388 return &empty.Empty{}, nil
Girish Gowdrae09a6202021-01-12 18:10:59 -0800389 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400390 logger.Warnw(ctx, "no handler found for update-pm-config", log.Fields{"device-id": configs.DeviceId})
391 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", configs.DeviceId))
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000392}
393
khenaidoo7d3c5582021-08-11 18:09:44 -0400394//DownloadImage requests downloading some image according to indications as given in request
Andrea Campanella71e546a2021-02-26 11:09:33 +0100395//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo7d3c5582021-08-11 18:09:44 -0400396func (oo *OpenONUAC) DownloadImage(ctx context.Context, imageInfo *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
397 ctx = log.WithSpanFromContext(context.Background(), ctx)
398 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
399 if !oo.pDownloadManager.imageExists(ctx, imageInfo.Image) {
400 logger.Debugw(ctx, "start image download", log.Fields{"image-description": imageInfo.Image})
mpagenko15ff4a52021-03-02 10:09:20 +0000401 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
402 // the download itself is later done in background
khenaidoo7d3c5582021-08-11 18:09:44 -0400403 if err := oo.pDownloadManager.startDownload(ctx, imageInfo.Image); err != nil {
404 return nil, err
405 }
406 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000407 }
408 // image already exists
khenaidoo7d3c5582021-08-11 18:09:44 -0400409 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageInfo.Image})
410 return imageInfo.Image, nil
mpagenkoc8bba412021-01-15 15:38:44 +0000411 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400412
413 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000414}
415
khenaidoo7d3c5582021-08-11 18:09:44 -0400416//ActivateImageUpdate requests downloading some Onu Software image to the INU via OMCI
Andrea Campanella71e546a2021-02-26 11:09:33 +0100417// according to indications as given in request and on success activate the image on the ONU
418//The ImageDownload needs to be called `request`due to library reflection requirements
khenaidoo7d3c5582021-08-11 18:09:44 -0400419func (oo *OpenONUAC) ActivateImageUpdate(ctx context.Context, imageInfo *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
420 if imageInfo != nil && imageInfo.Image != nil && imageInfo.Image.Name != "" {
421 if oo.pDownloadManager.imageLocallyDownloaded(ctx, imageInfo.Image) {
422 if handler := oo.getDeviceHandler(ctx, imageInfo.Device.Id, false); handler != nil {
mpagenko15ff4a52021-03-02 10:09:20 +0000423 logger.Debugw(ctx, "image download on omci requested", log.Fields{
khenaidoo7d3c5582021-08-11 18:09:44 -0400424 "image-description": imageInfo.Image, "device-id": imageInfo.Device.Id})
425 if err := handler.doOnuSwUpgrade(ctx, imageInfo.Image, oo.pDownloadManager); err != nil {
426 return nil, err
427 }
428 return imageInfo.Image, nil
mpagenko15ff4a52021-03-02 10:09:20 +0000429 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400430 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": imageInfo.Device.Id})
431 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found - device-id: %s", imageInfo.Device.Id))
mpagenko057889c2021-01-21 16:51:58 +0000432 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400433 logger.Debugw(ctx, "image not yet downloaded on activate request", log.Fields{"image-description": imageInfo.Image})
434 return nil, fmt.Errorf(fmt.Sprintf("image-not-yet-downloaded - device-id: %s", imageInfo.Device.Id))
mpagenkoc8bba412021-01-15 15:38:44 +0000435 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400436 return nil, errors.New("invalid image definition")
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000437}
438
khenaidoo7d3c5582021-08-11 18:09:44 -0400439//GetSingleValue handles the core request to retrieve uni status
440func (oo *OpenONUAC) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
kesavandfdf77632021-01-26 23:40:33 -0500441 logger.Infow(ctx, "Single_get_value_request", log.Fields{"request": request})
442
443 if handler := oo.getDeviceHandler(ctx, request.TargetId, false); handler != nil {
444 switch reqType := request.GetRequest().GetRequest().(type) {
445 case *extension.GetValueRequest_UniInfo:
446 return handler.getUniPortStatus(ctx, reqType.UniInfo), nil
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700447 case *extension.GetValueRequest_OnuOpticalInfo:
448 commChan := make(chan Message)
449 respChan := make(chan extension.SingleGetValueResponse)
450 // Initiate the self test request
khenaidoo7d3c5582021-08-11 18:09:44 -0400451 if err := handler.pSelfTestHdlr.SelfTestRequestStart(ctx, *request, commChan, respChan); err != nil {
Girish Gowdra6afb56a2021-04-27 17:47:57 -0700452 return &extension.SingleGetValueResponse{
453 Response: &extension.GetValueResponse{
454 Status: extension.GetValueResponse_ERROR,
455 ErrReason: extension.GetValueResponse_INTERNAL_ERROR,
456 },
457 }, err
458 }
459 // The timeout handling is already implemented in omci_self_test_handler module
460 resp := <-respChan
461 return &resp, nil
Himani Chawla43f95ff2021-06-03 00:24:12 +0530462 case *extension.GetValueRequest_OnuInfo:
463 return handler.getOnuOMCICounters(ctx, reqType.OnuInfo), nil
kesavandfdf77632021-01-26 23:40:33 -0500464 default:
465 return postUniStatusErrResponse(extension.GetValueResponse_UNSUPPORTED), nil
466
467 }
468 }
469 logger.Errorw(ctx, "Single_get_value_request failed ", log.Fields{"request": request})
470 return postUniStatusErrResponse(extension.GetValueResponse_INVALID_DEVICE_ID), nil
mpagenkoc8bba412021-01-15 15:38:44 +0000471}
472
mpagenko83144272021-04-27 10:06:22 +0000473//if update >= 4.3.0
mpagenkoc26d4c02021-05-06 14:27:57 +0000474// Note: already with the implementation of the 'old' download interface problems were detected when the argument name used here is not the same
475// as defined in the adapter interface file. That sounds strange and the effects were strange as well.
476// The reason for that was never finally investigated.
477// To be on the safe side argument names are left here always as defined in iAdapter.go .
mpagenko83144272021-04-27 10:06:22 +0000478
khenaidoo7d3c5582021-08-11 18:09:44 -0400479// DownloadOnuImage downloads (and optionally activates and commits) the indicated ONU image to the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000480// if the image is not yet present on the adapter it has to be automatically downloaded
khenaidoo7d3c5582021-08-11 18:09:44 -0400481func (oo *OpenONUAC) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000482 if request != nil && len((*request).DeviceId) > 0 && (*request).Image.Version != "" {
483 loResponse := voltha.DeviceImageResponse{}
484 imageIdentifier := (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000485 downloadedToAdapter := false
mpagenkoc26d4c02021-05-06 14:27:57 +0000486 firstDevice := true
487 var vendorID string
488 for _, pCommonID := range (*request).DeviceId {
mpagenko38662d02021-08-11 09:45:19 +0000489 vendorIDMatch := true
mpagenkoc26d4c02021-05-06 14:27:57 +0000490 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000491 loDeviceImageState := voltha.DeviceImageState{}
492 loDeviceImageState.DeviceId = loDeviceID
493 loImageState := voltha.ImageState{}
494 loDeviceImageState.ImageState = &loImageState
495 loDeviceImageState.ImageState.Version = (*request).Image.Version
mpagenko38662d02021-08-11 09:45:19 +0000496
khenaidoo7d3c5582021-08-11 18:09:44 -0400497 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
498 if handler == nil {
499 //cannot start ONU download for requested device
500 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
501 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
502 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
503 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
504 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
505 continue
506 }
507
508 onuVolthaDevice, err := handler.getDeviceFromCore(ctx, loDeviceID)
mpagenko38662d02021-08-11 09:45:19 +0000509 if err != nil || onuVolthaDevice == nil {
510 logger.Warnw(ctx, "Failed to fetch Onu device for image download",
511 log.Fields{"device-id": loDeviceID, "err": err})
512 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
513 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //proto restriction, better option: 'INVALID_DEVICE'
mpagenkoc26d4c02021-05-06 14:27:57 +0000514 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000515 } else {
mpagenko38662d02021-08-11 09:45:19 +0000516 if firstDevice {
517 //start/verify download of the image to the adapter based on first found device only
518 // use the OnuVendor identification from first given device
519 firstDevice = false
520 vendorID = onuVolthaDevice.VendorId
521 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
522 logger.Debugw(ctx, "download request for file", log.Fields{"image-id": imageIdentifier})
523
524 if !oo.pFileManager.ImageExists(ctx, imageIdentifier) {
525 logger.Debugw(ctx, "start image download", log.Fields{"image-description": request})
526 // Download_image is not supposed to be blocking, anyway let's call the DownloadManager still synchronously to detect 'fast' problems
527 // the download itself is later done in background
528 if err := oo.pFileManager.StartDownload(ctx, imageIdentifier, (*request).Image.Url); err == nil {
529 downloadedToAdapter = true
530 }
531 //else: treat any error here as 'INVALID_URL' (even though it might as well be some issue on local FS, eg. 'INSUFFICIENT_SPACE')
532 // otherwise a more sophisticated error evaluation is needed
533 } else {
534 // image already exists
535 downloadedToAdapter = true
536 logger.Debugw(ctx, "image already downloaded", log.Fields{"image-description": imageIdentifier})
537 // note: If the image (with vendorId+name) has already been downloaded before from some other
538 // valid URL, the current URL is just ignored. If the operators want to ensure that the new URL
539 // is really used, then they first have to use the 'abort' API to remove the existing image!
540 // (abort API can be used also after some successful download to just remove the image from adapter)
541 }
542 } else {
543 //for all following devices verify the matching vendorID
544 if onuVolthaDevice.VendorId != vendorID {
545 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
546 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
547 vendorIDMatch = false
548 }
549 }
550 if downloadedToAdapter && vendorIDMatch {
551 // start the ONU download activity for each possible device
552 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
553 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
554 logger.Debugw(ctx, "image download on omci requested", log.Fields{
555 "image-id": imageIdentifier, "device-id": loDeviceID})
556 //onu upgrade handling called in background without immediate error evaluation here
557 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
558 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
559 go handler.onuSwUpgradeAfterDownload(ctx, request, oo.pFileManager, imageIdentifier)
560 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_STARTED
561 loDeviceImageState.ImageState.Reason = voltha.ImageState_NO_ERROR
562 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
563 } else {
564 //cannot start ONU download for requested device
565 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
566 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
567 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //proto restriction, better option: 'INVALID_DEVICE'
568 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
569 }
570 } else {
571 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
572 if !downloadedToAdapter {
573 loDeviceImageState.ImageState.Reason = voltha.ImageState_INVALID_URL
574 } else { //only logical option is !vendorIDMatch
575 loDeviceImageState.ImageState.Reason = voltha.ImageState_VENDOR_DEVICE_MISMATCH
576 }
577 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
578 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000579 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400580
581 // start the ONU download activity for each possible device
582 logger.Debugw(ctx, "image download on omci requested", log.Fields{
583 "image-id": imageIdentifier, "device-id": loDeviceID})
584 //onu upgrade handling called in background without immediate error evaluation here
585 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
586 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
587 go handler.onuSwUpgradeAfterDownload(ctx, request, oo.pFileManager, imageIdentifier)
588 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_STARTED
589 loDeviceImageState.ImageState.Reason = voltha.ImageState_NO_ERROR
590 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
mpagenko2f2f2362021-06-07 08:25:22 +0000591 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000592 }
593 pImageResp := &loResponse
594 return pImageResp, nil
595 }
596 return nil, errors.New("invalid image download parameters")
mpagenko83144272021-04-27 10:06:22 +0000597}
598
khenaidoo7d3c5582021-08-11 18:09:44 -0400599// GetOnuImageStatus delivers the adapter-related information about the download/activation/commitment
mpagenko83144272021-04-27 10:06:22 +0000600// status for the requested image
khenaidoo7d3c5582021-08-11 18:09:44 -0400601func (oo *OpenONUAC) GetOnuImageStatus(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000602 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
603 loResponse := voltha.DeviceImageResponse{}
mpagenkoaa3afe92021-05-21 16:20:58 +0000604 imageIdentifier := (*in).Version
mpagenko38662d02021-08-11 09:45:19 +0000605 var vendorIDSet bool
mpagenkoaa3afe92021-05-21 16:20:58 +0000606 firstDevice := true
607 var vendorID string
608 for _, pCommonID := range (*in).DeviceId {
609 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400610 pDeviceImageState := &voltha.DeviceImageState{DeviceId: loDeviceID}
611 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
612 if handler == nil {
613 //cannot get the handler
614 logger.Warnw(ctx, "no handler found for image status request ", log.Fields{"device-id": loDeviceID})
615 pDeviceImageState.DeviceId = loDeviceID
616 pDeviceImageState.ImageState.Version = (*in).Version
617 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
618 pDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
619 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
620 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
621 continue
mpagenko38662d02021-08-11 09:45:19 +0000622 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400623 onuVolthaDevice, err := handler.getDeviceFromCore(ctx, loDeviceID)
mpagenkoaa3afe92021-05-21 16:20:58 +0000624 if err != nil || onuVolthaDevice == nil {
625 logger.Warnw(ctx, "Failed to fetch Onu device to get image status",
626 log.Fields{"device-id": loDeviceID, "err": err})
mpagenko38662d02021-08-11 09:45:19 +0000627 pImageState := &voltha.ImageState{
628 Version: (*in).Version,
629 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //no statement about last activity possible
630 Reason: voltha.ImageState_UNKNOWN_ERROR, //something like "DEVICE_NOT_EXISTS" would be better (proto def)
631 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
mpagenkoaa3afe92021-05-21 16:20:58 +0000632 }
mpagenko38662d02021-08-11 09:45:19 +0000633 pDeviceImageState.ImageState = pImageState
mpagenkoaa3afe92021-05-21 16:20:58 +0000634 } else {
mpagenko38662d02021-08-11 09:45:19 +0000635 if firstDevice {
636 //start/verify download of the image to the adapter based on first found device only
637 // use the OnuVendor identification from first given device
638 firstDevice = false
639 vendorID = onuVolthaDevice.VendorId
640 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
641 vendorIDSet = true
642 logger.Debugw(ctx, "status request for image", log.Fields{"image-id": imageIdentifier})
643 } else {
644 //for all following devices verify the matching vendorID
645 if onuVolthaDevice.VendorId != vendorID {
646 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
647 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
648 } else {
649 vendorIDSet = true
650 }
651 }
652 if !vendorIDSet {
653 pImageState := &voltha.ImageState{
654 Version: (*in).Version,
655 DownloadState: voltha.ImageState_DOWNLOAD_UNKNOWN, //can't be sure that download for this device was really tried
656 Reason: voltha.ImageState_VENDOR_DEVICE_MISMATCH,
657 ImageState: voltha.ImageState_IMAGE_UNKNOWN,
658 }
659 pDeviceImageState.ImageState = pImageState
660 } else {
khenaidoo7d3c5582021-08-11 18:09:44 -0400661 logger.Debugw(ctx, "image status request for", log.Fields{
662 "image-id": imageIdentifier, "device-id": loDeviceID})
663 //status request is called synchronously to collect the indications for all concerned devices
664 pDeviceImageState.ImageState = handler.requestOnuSwUpgradeState(ctx, imageIdentifier, (*in).Version)
mpagenko38662d02021-08-11 09:45:19 +0000665 }
mpagenkoaa3afe92021-05-21 16:20:58 +0000666 }
667 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
668 }
669 pImageResp := &loResponse
670 return pImageResp, nil
671 }
672 return nil, errors.New("invalid image status request parameters")
mpagenko83144272021-04-27 10:06:22 +0000673}
674
khenaidoo7d3c5582021-08-11 18:09:44 -0400675// AbortOnuImageUpgrade stops the actual download/activation/commitment process (on next possibly step)
676func (oo *OpenONUAC) AbortOnuImageUpgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoaa3afe92021-05-21 16:20:58 +0000677 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
678 loResponse := voltha.DeviceImageResponse{}
679 imageIdentifier := (*in).Version
680 firstDevice := true
681 var vendorID string
682 for _, pCommonID := range (*in).DeviceId {
683 loDeviceID := (*pCommonID).Id
khenaidoo7d3c5582021-08-11 18:09:44 -0400684 pDeviceImageState := &voltha.DeviceImageState{}
685 loImageState := voltha.ImageState{}
686 pDeviceImageState.ImageState = &loImageState
687
688 handler := oo.getDeviceHandler(ctx, loDeviceID, false)
689 if handler == nil {
690 //cannot start ONU download for requested device
691 logger.Warnw(ctx, "no handler found for aborting upgrade ", log.Fields{"device-id": loDeviceID})
692 pDeviceImageState.DeviceId = loDeviceID
693 pDeviceImageState.ImageState.Version = (*in).Version
694 //nolint:misspell
695 pDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_CANCELLED
696 //nolint:misspell
697 pDeviceImageState.ImageState.Reason = voltha.ImageState_CANCELLED_ON_REQUEST
698 pDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_UNKNOWN
699 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
700 continue
701 }
702 onuVolthaDevice, err := handler.getDeviceFromCore(ctx, loDeviceID)
mpagenkoaa3afe92021-05-21 16:20:58 +0000703 if err != nil || onuVolthaDevice == nil {
704 logger.Warnw(ctx, "Failed to fetch Onu device to abort its download",
705 log.Fields{"device-id": loDeviceID, "err": err})
706 continue //try the work with next deviceId
707 }
mpagenko2f2f2362021-06-07 08:25:22 +0000708
mpagenkoaa3afe92021-05-21 16:20:58 +0000709 if firstDevice {
710 //start/verify download of the image to the adapter based on first found device only
711 // use the OnuVendor identification from first given device
712 firstDevice = false
713 vendorID = onuVolthaDevice.VendorId
714 imageIdentifier = vendorID + imageIdentifier //head on vendor ID of the ONU
715 logger.Debugw(ctx, "abort request for file", log.Fields{"image-id": imageIdentifier})
716 } else {
717 //for all following devices verify the matching vendorID
718 if onuVolthaDevice.VendorId != vendorID {
719 logger.Warnw(ctx, "onu vendor id does not match image vendor id, device ignored",
720 log.Fields{"onu-vendor-id": onuVolthaDevice.VendorId, "image-vendor-id": vendorID})
721 continue //try the work with next deviceId
722 }
723 }
724
725 // cancel the ONU upgrade activity for each possible device
khenaidoo7d3c5582021-08-11 18:09:44 -0400726 logger.Debugw(ctx, "image upgrade abort requested", log.Fields{
727 "image-id": imageIdentifier, "device-id": loDeviceID})
728 //upgrade cancel is called synchronously to collect the imageResponse indications for all concerned devices
729 handler.cancelOnuSwUpgrade(ctx, imageIdentifier, (*in).Version, pDeviceImageState)
mpagenkoaa3afe92021-05-21 16:20:58 +0000730 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, pDeviceImageState)
731 }
732 if !firstDevice {
733 //if at least one valid device was found cancel also a possibly running download to adapter and remove the image
734 // this is to be done after the upgradeOnu cancel activities in order to not subduct the file for still running processes
735 oo.pFileManager.CancelDownload(ctx, imageIdentifier)
736 }
737 pImageResp := &loResponse
738 return pImageResp, nil
739 }
740 return nil, errors.New("invalid image upgrade abort parameters")
mpagenko83144272021-04-27 10:06:22 +0000741}
742
khenaidoo7d3c5582021-08-11 18:09:44 -0400743// GetOnuImages retrieves the ONU SW image status information via OMCI
744func (oo *OpenONUAC) GetOnuImages(ctx context.Context, id *common.ID) (*voltha.OnuImages, error) {
745 logger.Infow(ctx, "Get_onu_images", log.Fields{"device-id": id.Id})
746 if handler := oo.getDeviceHandler(ctx, id.Id, false); handler != nil {
Himani Chawla69992ab2021-07-08 15:13:02 +0530747 images, err := handler.getOnuImages(ctx)
748 if err == nil {
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000749 return images, nil
750 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400751 return nil, fmt.Errorf(fmt.Sprintf("%s-%s", err, id.Id))
Holger Hildebrandtfb402a62021-05-26 14:40:49 +0000752 }
khenaidoo7d3c5582021-08-11 18:09:44 -0400753 logger.Warnw(ctx, "no handler found for Get_onu_images", log.Fields{"device-id": id.Id})
754 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", id.Id))
mpagenko83144272021-04-27 10:06:22 +0000755}
756
khenaidoo7d3c5582021-08-11 18:09:44 -0400757// ActivateOnuImage initiates the activation of the image for the requested ONU(s)
mpagenkoc26d4c02021-05-06 14:27:57 +0000758// precondition: image downloaded and not yet activated or image refers to current inactive image
khenaidoo7d3c5582021-08-11 18:09:44 -0400759func (oo *OpenONUAC) ActivateOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000760 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
761 loResponse := voltha.DeviceImageResponse{}
762 imageIdentifier := (*in).Version
763 //let the deviceHandler find the adequate way of requesting the image activation
764 for _, pCommonID := range (*in).DeviceId {
765 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000766 loDeviceImageState := voltha.DeviceImageState{}
767 loDeviceImageState.DeviceId = loDeviceID
768 loImageState := voltha.ImageState{}
769 loDeviceImageState.ImageState = &loImageState
770 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000771 //compared to download procedure the vendorID (from device) is secondary here
772 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
773 // start image activation activity for each possible device
774 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
775 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
776 logger.Debugw(ctx, "onu image activation requested", log.Fields{
777 "image-id": imageIdentifier, "device-id": loDeviceID})
778 //onu activation handling called in background without immediate error evaluation here
779 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
780 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000781 if pImageStates, err := handler.onuSwActivateRequest(ctx, imageIdentifier, (*in).CommitOnSuccess); err != nil {
782 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
783 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
784 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
785 } else {
786 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
787 loDeviceImageState.ImageState.Reason = pImageStates.Reason
788 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
789 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000790 } else {
791 //cannot start SW activation for requested device
792 logger.Warnw(ctx, "no handler found for image activation", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000793 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000794 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
795 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_ACTIVATION_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000796 }
mpagenko2f2f2362021-06-07 08:25:22 +0000797 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000798 }
799 pImageResp := &loResponse
800 return pImageResp, nil
801 }
802 return nil, errors.New("invalid image activation parameters")
mpagenko83144272021-04-27 10:06:22 +0000803}
804
khenaidoo7d3c5582021-08-11 18:09:44 -0400805// CommitOnuImage enforces the commitment of the image for the requested ONU(s)
mpagenko83144272021-04-27 10:06:22 +0000806// precondition: image activated and not yet committed
khenaidoo7d3c5582021-08-11 18:09:44 -0400807func (oo *OpenONUAC) CommitOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
mpagenkoc26d4c02021-05-06 14:27:57 +0000808 if in != nil && len((*in).DeviceId) > 0 && (*in).Version != "" {
809 loResponse := voltha.DeviceImageResponse{}
810 imageIdentifier := (*in).Version
811 //let the deviceHandler find the adequate way of requesting the image activation
812 for _, pCommonID := range (*in).DeviceId {
813 loDeviceID := (*pCommonID).Id
mpagenko2f2f2362021-06-07 08:25:22 +0000814 loDeviceImageState := voltha.DeviceImageState{}
815 loDeviceImageState.DeviceId = loDeviceID
816 loImageState := voltha.ImageState{}
817 loDeviceImageState.ImageState = &loImageState
818 loDeviceImageState.ImageState.Version = imageIdentifier
mpagenkoc26d4c02021-05-06 14:27:57 +0000819 //compared to download procedure the vendorID (from device) is secondary here
820 // and only needed in case the upgrade process is based on some ongoing download process (and can be retrieved in deviceHandler if needed)
821 // start image activation activity for each possible device
822 // assumption here is, that the concerned device was already created (automatic start after device creation not supported)
823 if handler := oo.getDeviceHandler(ctx, loDeviceID, false); handler != nil {
824 logger.Debugw(ctx, "onu image commitment requested", log.Fields{
825 "image-id": imageIdentifier, "device-id": loDeviceID})
826 //onu commitment handling called in background without immediate error evaluation here
827 // as the processing can be done for multiple ONU's and an error on one ONU should not stop processing for others
828 // state/progress/success of the request has to be verified using the Get_onu_image_status() API
mpagenko183647c2021-06-08 15:25:04 +0000829 if pImageStates, err := handler.onuSwCommitRequest(ctx, imageIdentifier); err != nil {
mpagenko38662d02021-08-11 09:45:19 +0000830 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_FAILED
831 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR //can be multiple reasons here
mpagenko183647c2021-06-08 15:25:04 +0000832 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
833 } else {
834 loDeviceImageState.ImageState.DownloadState = pImageStates.DownloadState
835 loDeviceImageState.ImageState.Reason = pImageStates.Reason
836 loDeviceImageState.ImageState.ImageState = pImageStates.ImageState
837 }
mpagenkoc26d4c02021-05-06 14:27:57 +0000838 } else {
839 //cannot start SW commitment for requested device
840 logger.Warnw(ctx, "no handler found for image commitment", log.Fields{"device-id": loDeviceID})
mpagenko183647c2021-06-08 15:25:04 +0000841 loDeviceImageState.ImageState.DownloadState = voltha.ImageState_DOWNLOAD_UNKNOWN
mpagenkoc26d4c02021-05-06 14:27:57 +0000842 loDeviceImageState.ImageState.Reason = voltha.ImageState_UNKNOWN_ERROR
843 loDeviceImageState.ImageState.ImageState = voltha.ImageState_IMAGE_COMMIT_ABORTED
mpagenkoc26d4c02021-05-06 14:27:57 +0000844 }
mpagenko2f2f2362021-06-07 08:25:22 +0000845 loResponse.DeviceImageStates = append(loResponse.DeviceImageStates, &loDeviceImageState)
mpagenkoc26d4c02021-05-06 14:27:57 +0000846 }
847 pImageResp := &loResponse
848 return pImageResp, nil
849 }
850 return nil, errors.New("invalid image commitment parameters")
mpagenko83144272021-04-27 10:06:22 +0000851}
852
Holger Hildebrandtfa074992020-03-27 15:42:06 +0000853// Adapter interface required methods ################ end #########
854// #################################################################
khenaidoo7d3c5582021-08-11 18:09:44 -0400855
856/*
857 *
858 * ONU inter adapter service
859 *
860 */
861
862// OnuIndication is part of the ONU Inter-adapter service API.
863func (oo *OpenONUAC) OnuIndication(ctx context.Context, onuInd *ic.OnuIndicationMessage) (*empty.Empty, error) {
864 logger.Debugw(ctx, "onu-indication", log.Fields{"onu-indication": onuInd})
865
866 if onuInd == nil || onuInd.OnuIndication == nil {
867 return nil, fmt.Errorf("invalid-onu-indication-%v", onuInd)
868 }
869
870 onuIndication := onuInd.OnuIndication
871 onuOperstate := onuIndication.GetOperState()
872 waitForDhInstPresent := false
873 if onuOperstate == "up" {
874 //Race condition (relevant in BBSIM-environment only): Due to unsynchronized processing of olt-adapter and rw_core,
875 //ONU_IND_REQUEST msg by olt-adapter could arrive a little bit earlier than rw_core was able to announce the corresponding
876 //ONU by RPC of Adopt_device(). Therefore it could be necessary to wait with processing of ONU_IND_REQUEST until call of
877 //Adopt_device() arrived and DeviceHandler instance was created
878 waitForDhInstPresent = true
879 }
880 if handler := oo.getDeviceHandler(ctx, onuInd.DeviceId, waitForDhInstPresent); handler != nil {
881 logger.Infow(ctx, "onu-ind-request", log.Fields{"device-id": onuInd.DeviceId,
882 "OnuId": onuIndication.GetOnuId(),
883 "AdminState": onuIndication.GetAdminState(), "OperState": onuOperstate,
884 "SNR": onuIndication.GetSerialNumber()})
885
886 if onuOperstate == "up" {
887 if err := handler.createInterface(ctx, onuIndication); err != nil {
888 return nil, err
889 }
890 return &empty.Empty{}, nil
891 } else if (onuOperstate == "down") || (onuOperstate == "unreachable") {
892 return nil, handler.updateInterface(ctx, onuIndication)
893 } else {
894 logger.Errorw(ctx, "unknown-onu-ind-request operState", log.Fields{"OnuId": onuIndication.GetOnuId()})
895 return nil, fmt.Errorf("invalidOperState: %s, %s", onuOperstate, onuInd.DeviceId)
896 }
897 }
898 logger.Warnw(ctx, "no handler found for received onu-ind-request", log.Fields{
899 "msgToDeviceId": onuInd.DeviceId})
900 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", onuInd.DeviceId))
901}
902
903// OmciIndication is part of the ONU Inter-adapter service API.
904func (oo *OpenONUAC) OmciIndication(ctx context.Context, msg *ic.OmciMessage) (*empty.Empty, error) {
905 logger.Debugw(ctx, "omci-response", log.Fields{"parent-device-id": msg.ParentDeviceId, "child-device-id": msg.ChildDeviceId})
906
907 if handler := oo.getDeviceHandler(ctx, msg.ChildDeviceId, false); handler != nil {
908 if err := handler.handleOMCIIndication(log.WithSpanFromContext(context.Background(), ctx), msg); err != nil {
909 return nil, err
910 }
911 return &empty.Empty{}, nil
912 }
913 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", msg.ChildDeviceId))
914}
915
916// DownloadTechProfile is part of the ONU Inter-adapter service API.
917func (oo *OpenONUAC) DownloadTechProfile(ctx context.Context, tProfile *ic.TechProfileDownloadMessage) (*empty.Empty, error) {
918 logger.Debugw(ctx, "download-tech-profile", log.Fields{"uni-id": tProfile.UniId})
919
920 if handler := oo.getDeviceHandler(ctx, tProfile.DeviceId, false); handler != nil {
921 if err := handler.handleTechProfileDownloadRequest(log.WithSpanFromContext(context.Background(), ctx), tProfile); err != nil {
922 return nil, err
923 }
924 return &empty.Empty{}, nil
925 }
926 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tProfile.DeviceId))
927}
928
929// DeleteGemPort is part of the ONU Inter-adapter service API.
930func (oo *OpenONUAC) DeleteGemPort(ctx context.Context, gPort *ic.DeleteGemPortMessage) (*empty.Empty, error) {
931 logger.Debugw(ctx, "delete-gem-port", log.Fields{"device-id": gPort.DeviceId, "uni-id": gPort.UniId})
932
933 if handler := oo.getDeviceHandler(ctx, gPort.DeviceId, false); handler != nil {
934 if err := handler.handleDeleteGemPortRequest(log.WithSpanFromContext(context.Background(), ctx), gPort); err != nil {
935 return nil, err
936 }
937 return &empty.Empty{}, nil
938 }
939 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", gPort.DeviceId))
940}
941
942// DeleteTCont is part of the ONU Inter-adapter service API.
943func (oo *OpenONUAC) DeleteTCont(ctx context.Context, tConf *ic.DeleteTcontMessage) (*empty.Empty, error) {
944 logger.Debugw(ctx, "delete-tcont", log.Fields{"tconf": tConf})
945
946 if handler := oo.getDeviceHandler(ctx, tConf.DeviceId, false); handler != nil {
947 if err := handler.handleDeleteTcontRequest(log.WithSpanFromContext(context.Background(), ctx), tConf); err != nil {
948 return nil, err
949 }
950 return &empty.Empty{}, nil
951 }
952 return nil, fmt.Errorf(fmt.Sprintf("handler-not-found-%s", tConf.DeviceId))
953}
954
955/*
956 * Parent GRPC clients
957 */
958
959func (oo *OpenONUAC) setupParentInterAdapterClient(ctx context.Context, endpoint string) error {
960 logger.Infow(ctx, "setting-parent-adapter-connection", log.Fields{"parent-endpoint": endpoint})
961 oo.lockParentAdapterClients.Lock()
962 defer oo.lockParentAdapterClients.Unlock()
963 if _, ok := oo.parentAdapterClients[endpoint]; ok {
964 return nil
965 }
966
967 childClient, err := vgrpc.NewClient(endpoint,
968 oo.oltAdapterRestarted,
969 vgrpc.ActivityCheck(true))
970
971 if err != nil {
972 return err
973 }
974
975 oo.parentAdapterClients[endpoint] = childClient
976
977 go oo.parentAdapterClients[endpoint].Start(log.WithSpanFromContext(context.TODO(), ctx), setAndTestAdapterServiceHandler)
978
979 // Wait until we have a connection to the child adapter.
980 // Unlimited retries or until context expires
981 subCtx := log.WithSpanFromContext(context.TODO(), ctx)
982 backoff := vgrpc.NewBackoff(oo.config.MinBackoffRetryDelay, oo.config.MaxBackoffRetryDelay, 0)
983 for {
984 client, err := oo.parentAdapterClients[endpoint].GetOltInterAdapterServiceClient()
985 if err == nil && client != nil {
986 logger.Infow(subCtx, "connected-to-parent-adapter", log.Fields{"parent-endpoint": endpoint})
987 break
988 }
989 logger.Warnw(subCtx, "connection-to-parent-adapter-not-ready", log.Fields{"error": err, "parent-endpoint": endpoint})
990 // Backoff
991 if err = backoff.Backoff(subCtx); err != nil {
992 logger.Errorw(subCtx, "received-error-on-backoff", log.Fields{"error": err, "parent-endpoint": endpoint})
993 break
994 }
995 }
996 return nil
997}
998
999func (oo *OpenONUAC) getParentAdapterServiceClient(endpoint string) (adapter_services.OltInterAdapterServiceClient, error) {
1000 // First check from cache
1001 oo.lockParentAdapterClients.RLock()
1002 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1003 oo.lockParentAdapterClients.RUnlock()
1004 return pgClient.GetOltInterAdapterServiceClient()
1005 }
1006 oo.lockParentAdapterClients.RUnlock()
1007
1008 // Set the parent connection - can occur on restarts
1009 ctx, cancel := context.WithTimeout(context.Background(), oo.config.RPCTimeout)
1010 err := oo.setupParentInterAdapterClient(ctx, endpoint)
1011 cancel()
1012 if err != nil {
1013 return nil, err
1014 }
1015
1016 // Get the parent client now
1017 oo.lockParentAdapterClients.RLock()
1018 defer oo.lockParentAdapterClients.RUnlock()
1019 if pgClient, ok := oo.parentAdapterClients[endpoint]; ok {
1020 return pgClient.GetOltInterAdapterServiceClient()
1021 }
1022
1023 return nil, fmt.Errorf("no-client-for-endpoint-%s", endpoint)
1024}
1025
1026// TODO: Any action the adapter needs to do following an olt adapter restart?
1027func (oo *OpenONUAC) oltAdapterRestarted(ctx context.Context, endPoint string) error {
1028 logger.Errorw(ctx, "olt-adapter-restarted", log.Fields{"endpoint": endPoint})
1029 return nil
1030}
1031
1032// setAndTestAdapterServiceHandler is used to test whether the remote gRPC service is up
1033func setAndTestAdapterServiceHandler(ctx context.Context, conn *grpc.ClientConn) interface{} {
1034 svc := adapter_services.NewOltInterAdapterServiceClient(conn)
1035 if h, err := svc.GetHealthStatus(ctx, &empty.Empty{}); err != nil || h.State != voltha.HealthStatus_HEALTHY {
1036 return nil
1037 }
1038 return svc
1039}
1040
1041/*
1042 *
1043 * Unimplemented APIs
1044 *
1045 */
1046
1047//GetOfpDeviceInfo returns OFP information for the given device. Method not implemented as per [VOL-3202].
1048// OF port info is now to be delivered within UniPort create cmp changes in onu_uni_port.go::CreateVolthaPort()
1049//
1050func (oo *OpenONUAC) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) {
1051 return nil, errors.New("unImplemented")
1052}
1053
1054//SimulateAlarm is unimplemented
1055func (oo *OpenONUAC) SimulateAlarm(context.Context, *ic.SimulateAlarmMessage) (*common.OperationResp, error) {
1056 return nil, errors.New("unImplemented")
1057}
1058
1059//SetExtValue is unimplemented
1060func (oo *OpenONUAC) SetExtValue(context.Context, *ic.SetExtValueMessage) (*empty.Empty, error) {
1061 return nil, errors.New("unImplemented")
1062}
1063
1064//SetSingleValue is unimplemented
1065func (oo *OpenONUAC) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
1066 return nil, errors.New("unImplemented")
1067}
1068
1069//StartOmciTest not implemented
1070func (oo *OpenONUAC) StartOmciTest(ctx context.Context, test *ic.OMCITest) (*voltha.TestResponse, error) {
1071 return nil, errors.New("unImplemented")
1072}
1073
1074//SuppressEvent unimplemented
1075func (oo *OpenONUAC) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1076 return nil, errors.New("unImplemented")
1077}
1078
1079//UnSuppressEvent unimplemented
1080func (oo *OpenONUAC) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
1081 return nil, errors.New("unImplemented")
1082}
1083
1084//GetImageDownloadStatus is unimplemented
1085func (oo *OpenONUAC) GetImageDownloadStatus(ctx context.Context, imageInfo *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
1086 return nil, errors.New("unImplemented")
1087}
1088
1089//CancelImageDownload is unimplemented
1090func (oo *OpenONUAC) CancelImageDownload(ctx context.Context, imageInfo *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
1091 return nil, errors.New("unImplemented")
1092}
1093
1094//RevertImageUpdate is unimplemented
1095func (oo *OpenONUAC) RevertImageUpdate(ctx context.Context, imageInfo *ic.ImageDownloadMessage) (*voltha.ImageDownload, error) {
1096 return nil, errors.New("unImplemented")
1097}
1098
1099// UpdateFlowsBulk is unimplemented
1100func (oo *OpenONUAC) UpdateFlowsBulk(ctx context.Context, flows *ic.BulkFlows) (*empty.Empty, error) {
1101 return nil, errors.New("unImplemented")
1102}
1103
1104//SelfTestDevice unimplented
1105func (oo *OpenONUAC) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
1106 return nil, errors.New("unImplemented")
1107}
1108
1109//SendPacketOut sends packet out to the device
1110func (oo *OpenONUAC) SendPacketOut(ctx context.Context, packet *ic.PacketOut) (*empty.Empty, error) {
1111 return nil, errors.New("unImplemented")
1112}
1113
1114// EnablePort to Enable PON/NNI interface - seems not to be used/required according to python code
1115func (oo *OpenONUAC) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1116 return nil, errors.New("unImplemented")
1117}
1118
1119// DisablePort to Disable pon/nni interface - seems not to be used/required according to python code
1120func (oo *OpenONUAC) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
1121 return nil, errors.New("unImplemented")
1122}
1123
1124// GetExtValue - unimplemented
1125func (oo *OpenONUAC) GetExtValue(ctx context.Context, extInfo *ic.GetExtValueMessage) (*voltha.ReturnValues, error) {
1126 return nil, errors.New("unImplemented")
1127}
1128
1129// ChildDeviceLost - unimplemented
1130func (oo *OpenONUAC) ChildDeviceLost(ctx context.Context, childDevice *voltha.Device) (*empty.Empty, error) {
1131 return nil, errors.New("unImplemented")
1132}