blob: 0d96d4f4942022008afc47b1f80460b8dc2235d9 [file] [log] [blame]
Girish Gowdru0c588b22019-04-23 23:24:56 -04001/*
Joey Armstrong11f5a572024-01-12 19:11:32 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Girish Gowdru0c588b22019-04-23 23:24:56 -04003
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 */
Girish Gowdru6a80bbd2019-07-02 07:36:09 -070016
praneeth nalmas55616d62023-02-06 09:19:18 +053017// Package core provides the utility for olt devices, flows and statistics
Scott Bakerdbd960e2020-02-28 08:57:51 -080018package core
Girish Gowdru0c588b22019-04-23 23:24:56 -040019
20import (
21 "context"
khenaidooefff76e2021-12-15 16:51:30 -050022 "fmt"
khenaidoo106c61a2021-08-11 18:05:46 -040023 "github.com/golang/protobuf/ptypes/empty"
24 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
25 "github.com/opencord/voltha-lib-go/v7/pkg/events/eventif"
26 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
27 "github.com/opencord/voltha-lib-go/v7/pkg/log"
Scott Bakerdbd960e2020-02-28 08:57:51 -080028 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053029 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
khenaidooefff76e2021-12-15 16:51:30 -050030 "github.com/opencord/voltha-protos/v5/go/adapter_service"
khenaidoo106c61a2021-08-11 18:05:46 -040031 "github.com/opencord/voltha-protos/v5/go/common"
khenaidoodc2116e2021-10-19 17:33:19 -040032 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidoo106c61a2021-08-11 18:05:46 -040033 "github.com/opencord/voltha-protos/v5/go/extension"
khenaidoodc2116e2021-10-19 17:33:19 -040034 "github.com/opencord/voltha-protos/v5/go/health"
35 ia "github.com/opencord/voltha-protos/v5/go/inter_adapter"
36 "github.com/opencord/voltha-protos/v5/go/omci"
khenaidoo106c61a2021-08-11 18:05:46 -040037 "github.com/opencord/voltha-protos/v5/go/voltha"
nikesh.krishnan4d644532023-12-16 00:21:07 +053038 "google.golang.org/grpc/codes"
39 "google.golang.org/grpc/status"
40 "sync"
41 "time"
Girish Gowdru0c588b22019-04-23 23:24:56 -040042)
43
praneeth nalmas55616d62023-02-06 09:19:18 +053044// OpenOLT structure holds the OLT information
Girish Gowdru0c588b22019-04-23 23:24:56 -040045type OpenOLT struct {
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +053046 configManager *conf.ConfigManager
47 deviceHandlers map[string]*DeviceHandler
48 coreClient *vgrpc.Client
49 eventProxy eventif.EventProxy
50 config *config.AdapterFlags
51 numOnus int
52 KVStoreAddress string
53 KVStoreType string
54 exitChannel chan struct{}
55 HeartbeatCheckInterval time.Duration
56 HeartbeatFailReportInterval time.Duration
57 GrpcTimeoutInterval time.Duration
58 lockDeviceHandlersMap sync.RWMutex
59 enableONUStats bool
60 enableGemStats bool
61 rpcTimeout time.Duration
62 CheckOnuDevExistenceAtOnuDiscovery bool
Girish Gowdru0c588b22019-04-23 23:24:56 -040063}
64
praneeth nalmas55616d62023-02-06 09:19:18 +053065// NewOpenOLT returns a new instance of OpenOLT
khenaidoo106c61a2021-08-11 18:05:46 -040066func NewOpenOLT(ctx context.Context,
67 coreClient *vgrpc.Client,
Himani Chawlacd407802020-12-10 12:08:59 +053068 eventProxy eventif.EventProxy, cfg *config.AdapterFlags, cm *conf.ConfigManager) *OpenOLT {
Girish Gowdru0c588b22019-04-23 23:24:56 -040069 var openOLT OpenOLT
khenaidooefff76e2021-12-15 16:51:30 -050070 openOLT.exitChannel = make(chan struct{})
Girish Gowdru0c588b22019-04-23 23:24:56 -040071 openOLT.deviceHandlers = make(map[string]*DeviceHandler)
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053072 openOLT.config = cfg
73 openOLT.numOnus = cfg.OnuNumber
khenaidoo106c61a2021-08-11 18:05:46 -040074 openOLT.coreClient = coreClient
Devmalya Paulfb990a52019-07-09 10:01:49 -040075 openOLT.eventProxy = eventProxy
Neha Sharma3f221ae2020-04-29 19:02:12 +000076 openOLT.KVStoreAddress = cfg.KVStoreAddress
Abhilash Laxmeshwarf9942e92020-01-07 15:32:44 +053077 openOLT.KVStoreType = cfg.KVStoreType
78 openOLT.HeartbeatCheckInterval = cfg.HeartbeatCheckInterval
79 openOLT.HeartbeatFailReportInterval = cfg.HeartbeatFailReportInterval
80 openOLT.GrpcTimeoutInterval = cfg.GrpcTimeoutInterval
Girish Gowdru0c588b22019-04-23 23:24:56 -040081 openOLT.lockDeviceHandlersMap = sync.RWMutex{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -080082 openOLT.configManager = cm
Gamze Abakafcbd6e72020-12-17 13:25:16 +000083 openOLT.enableONUStats = cfg.EnableONUStats
84 openOLT.enableGemStats = cfg.EnableGEMStats
khenaidoo106c61a2021-08-11 18:05:46 -040085 openOLT.rpcTimeout = cfg.RPCTimeout
Abhilash Laxmeshward0f58cf2022-06-01 12:15:19 +053086 openOLT.CheckOnuDevExistenceAtOnuDiscovery = cfg.CheckOnuDevExistenceAtOnuDiscovery
Girish Gowdru0c588b22019-04-23 23:24:56 -040087 return &openOLT
88}
89
praneeth nalmas55616d62023-02-06 09:19:18 +053090// Start starts (logs) the device manager
Girish Gowdru0c588b22019-04-23 23:24:56 -040091func (oo *OpenOLT) Start(ctx context.Context) error {
Neha Sharma96b7bf22020-06-15 10:37:32 +000092 logger.Info(ctx, "starting-device-manager")
93 logger.Info(ctx, "device-manager-started")
Girish Gowdru0c588b22019-04-23 23:24:56 -040094 return nil
95}
96
praneeth nalmas55616d62023-02-06 09:19:18 +053097// Stop terminates the session
Girish Gowdru0c588b22019-04-23 23:24:56 -040098func (oo *OpenOLT) Stop(ctx context.Context) error {
Neha Sharma96b7bf22020-06-15 10:37:32 +000099 logger.Info(ctx, "stopping-device-manager")
khenaidooefff76e2021-12-15 16:51:30 -0500100 close(oo.exitChannel)
101 // Stop the device handlers
102 oo.stopAllDeviceHandlers(ctx)
103
104 // Stop the core grpc client connection
105 if oo.coreClient != nil {
106 oo.coreClient.Stop(ctx)
107 }
108
Neha Sharma96b7bf22020-06-15 10:37:32 +0000109 logger.Info(ctx, "device-manager-stopped")
Girish Gowdru0c588b22019-04-23 23:24:56 -0400110 return nil
111}
112
Girish Gowdru0c588b22019-04-23 23:24:56 -0400113func (oo *OpenOLT) addDeviceHandlerToMap(agent *DeviceHandler) {
114 oo.lockDeviceHandlersMap.Lock()
115 defer oo.lockDeviceHandlersMap.Unlock()
Thomas Lee S985938d2020-05-04 11:40:41 +0530116 if _, exist := oo.deviceHandlers[agent.device.Id]; !exist {
117 oo.deviceHandlers[agent.device.Id] = agent
Girish Gowdru0c588b22019-04-23 23:24:56 -0400118 }
119}
120
121func (oo *OpenOLT) deleteDeviceHandlerToMap(agent *DeviceHandler) {
122 oo.lockDeviceHandlersMap.Lock()
123 defer oo.lockDeviceHandlersMap.Unlock()
Thomas Lee S985938d2020-05-04 11:40:41 +0530124 delete(oo.deviceHandlers, agent.device.Id)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400125}
126
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700127func (oo *OpenOLT) getDeviceHandler(deviceID string) *DeviceHandler {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400128 oo.lockDeviceHandlersMap.Lock()
129 defer oo.lockDeviceHandlersMap.Unlock()
Girish Gowdru6a80bbd2019-07-02 07:36:09 -0700130 if agent, ok := oo.deviceHandlers[deviceID]; ok {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400131 return agent
132 }
133 return nil
134}
135
khenaidooefff76e2021-12-15 16:51:30 -0500136func (oo *OpenOLT) stopAllDeviceHandlers(ctx context.Context) {
137 oo.lockDeviceHandlersMap.Lock()
138 defer oo.lockDeviceHandlersMap.Unlock()
139 for _, handler := range oo.deviceHandlers {
140 handler.Stop(ctx)
141 }
khenaidoo106c61a2021-08-11 18:05:46 -0400142}
143
144// AdoptDevice creates a new device handler if not present already and then adopts the device
145func (oo *OpenOLT) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Girish Gowdru0c588b22019-04-23 23:24:56 -0400146 if device == nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400147 return nil, olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil).Log()
Girish Gowdru0c588b22019-04-23 23:24:56 -0400148 }
divyadesai3af43e12020-08-18 07:10:54 +0000149 logger.Infow(ctx, "adopt-device", log.Fields{"device-id": device.Id})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400150 var handler *DeviceHandler
151 if handler = oo.getDeviceHandler(device.Id); handler == nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400152 handler := NewDeviceHandler(oo.coreClient, oo.eventProxy, device, oo, oo.configManager, oo.config)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400153 oo.addDeviceHandlerToMap(handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400154 go handler.AdoptDevice(log.WithSpanFromContext(context.Background(), ctx), device)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400155 }
khenaidoo106c61a2021-08-11 18:05:46 -0400156 return &empty.Empty{}, nil
Girish Gowdru0c588b22019-04-23 23:24:56 -0400157}
158
praneeth nalmas55616d62023-02-06 09:19:18 +0530159// GetOfpDeviceInfo returns OFP information for the given device
khenaidoodc2116e2021-10-19 17:33:19 -0400160func (oo *OpenOLT) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400161 logger.Infow(ctx, "get_ofp_device_info", log.Fields{"device-id": device.Id})
Girish Gowdru0c588b22019-04-23 23:24:56 -0400162 if handler := oo.getDeviceHandler(device.Id); handler != nil {
163 return handler.GetOfpDeviceInfo(device)
164 }
Girish Kumarf26e4882020-03-05 06:49:10 +0000165 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400166}
167
praneeth nalmas55616d62023-02-06 09:19:18 +0530168// ReconcileDevice unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400169func (oo *OpenOLT) ReconcileDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530170 if device == nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400171 return nil, olterrors.NewErrInvalidValue(log.Fields{"device": nil}, nil)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530172 }
divyadesai3af43e12020-08-18 07:10:54 +0000173 logger.Infow(ctx, "reconcile-device", log.Fields{"device-id": device.Id})
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530174 var handler *DeviceHandler
175 if handler = oo.getDeviceHandler(device.Id); handler == nil {
Manindere49938d2021-03-19 00:23:24 +0530176 //Setting state to RECONCILING
khenaidoo106c61a2021-08-11 18:05:46 -0400177 cgClient, err := oo.coreClient.GetCoreServiceClient()
Manindere49938d2021-03-19 00:23:24 +0530178 if err != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400179 return nil, err
Manindere49938d2021-03-19 00:23:24 +0530180 }
khenaidoo106c61a2021-08-11 18:05:46 -0400181 subCtx, cancel := context.WithTimeout(log.WithSpanFromContext(context.Background(), ctx), oo.rpcTimeout)
182 defer cancel()
khenaidoodc2116e2021-10-19 17:33:19 -0400183 if _, err := cgClient.DeviceStateUpdate(subCtx, &ca.DeviceStateFilter{
khenaidoo106c61a2021-08-11 18:05:46 -0400184 DeviceId: device.Id,
185 OperStatus: voltha.OperStatus_RECONCILING,
186 ConnStatus: device.ConnectStatus,
187 }); err != nil {
188 return nil, olterrors.NewErrAdapter("device-update-failed", log.Fields{"device-id": device.Id}, err)
189 }
190
Girish Gowdra0fb24a32021-10-27 15:15:27 -0700191 // The OperState of the device is set to RECONCILING in the previous section. This also needs to be set on the
192 // locally cached copy of the device struct.
193 device.OperStatus = voltha.OperStatus_RECONCILING
194 handler := NewDeviceHandler(oo.coreClient, oo.eventProxy, device, oo, oo.configManager, oo.config)
195 handler.adapterPreviouslyConnected = true
196 oo.addDeviceHandlerToMap(handler)
197 handler.transitionMap = NewTransitionMap(handler)
198
khenaidoo106c61a2021-08-11 18:05:46 -0400199 handler.transitionMap.Handle(log.WithSpanFromContext(context.Background(), ctx), DeviceInit)
nikesh.krishnan4d644532023-12-16 00:21:07 +0530200 } else {
201 logger.Warnf(ctx, "device-already-reconciled-or-active", log.Fields{"device-id": device.Id})
202 return &empty.Empty{}, status.Errorf(codes.AlreadyExists, "handler exists: %s", device.Id)
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530203 }
khenaidoo106c61a2021-08-11 18:05:46 -0400204 return &empty.Empty{}, nil
Girish Gowdru0c588b22019-04-23 23:24:56 -0400205}
206
praneeth nalmas55616d62023-02-06 09:19:18 +0530207// DisableDevice disables the given device
khenaidoo106c61a2021-08-11 18:05:46 -0400208func (oo *OpenOLT) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
divyadesai3af43e12020-08-18 07:10:54 +0000209 logger.Infow(ctx, "disable-device", log.Fields{"device-id": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400210 if handler := oo.getDeviceHandler(device.Id); handler != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400211 if err := handler.DisableDevice(log.WithSpanFromContext(context.Background(), ctx), device); err != nil {
212 return nil, err
213 }
214 return &empty.Empty{}, nil
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400215 }
khenaidoo106c61a2021-08-11 18:05:46 -0400216 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400217}
218
praneeth nalmas55616d62023-02-06 09:19:18 +0530219// ReEnableDevice enables the olt device after disable
khenaidoo106c61a2021-08-11 18:05:46 -0400220func (oo *OpenOLT) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
divyadesai3af43e12020-08-18 07:10:54 +0000221 logger.Infow(ctx, "reenable-device", log.Fields{"device-id": device.Id})
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400222 if handler := oo.getDeviceHandler(device.Id); handler != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400223 if err := handler.ReenableDevice(log.WithSpanFromContext(context.Background(), ctx), device); err != nil {
224 return nil, err
225 }
226 return &empty.Empty{}, nil
Girish Gowdru5ba46c92019-04-25 05:00:05 -0400227 }
khenaidoo106c61a2021-08-11 18:05:46 -0400228 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400229}
230
praneeth nalmas55616d62023-02-06 09:19:18 +0530231// RebootDevice reboots the given device
khenaidoo106c61a2021-08-11 18:05:46 -0400232func (oo *OpenOLT) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
divyadesai3af43e12020-08-18 07:10:54 +0000233 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400234 if handler := oo.getDeviceHandler(device.Id); handler != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400235 if err := handler.RebootDevice(log.WithSpanFromContext(context.Background(), ctx), device); err != nil {
236 return nil, err
237 }
238 return &empty.Empty{}, nil
Girish Gowdru0fe5f7e2019-05-28 05:12:27 -0400239 }
khenaidoo106c61a2021-08-11 18:05:46 -0400240 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
241
Girish Gowdru0c588b22019-04-23 23:24:56 -0400242}
243
praneeth nalmas55616d62023-02-06 09:19:18 +0530244// DeleteDevice deletes a device
khenaidoo106c61a2021-08-11 18:05:46 -0400245func (oo *OpenOLT) DeleteDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
divyadesai3af43e12020-08-18 07:10:54 +0000246 logger.Infow(ctx, "delete-device", log.Fields{"device-id": device.Id})
Devmalya Paul495b94a2019-08-27 19:42:00 -0400247 if handler := oo.getDeviceHandler(device.Id); handler != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400248 if err := handler.DeleteDevice(log.WithSpanFromContext(context.Background(), ctx), device); err != nil {
Gustavo Silva41af9122022-10-11 11:05:13 -0300249 return nil, err
Devmalya Paul495b94a2019-08-27 19:42:00 -0400250 }
251 oo.deleteDeviceHandlerToMap(handler)
khenaidoo106c61a2021-08-11 18:05:46 -0400252 return &empty.Empty{}, nil
Devmalya Paul495b94a2019-08-27 19:42:00 -0400253 }
khenaidoo106c61a2021-08-11 18:05:46 -0400254 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": device.Id}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400255}
256
praneeth nalmas55616d62023-02-06 09:19:18 +0530257// UpdateFlowsIncrementally updates (add/remove) the flows on a given device
khenaidoodc2116e2021-10-19 17:33:19 -0400258func (oo *OpenOLT) UpdateFlowsIncrementally(ctx context.Context, incrFlows *ca.IncrementalFlows) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400259 logger.Infow(ctx, "update_flows_incrementally", log.Fields{"device-id": incrFlows.Device.Id, "flows": incrFlows.Flows, "flowMetadata": incrFlows.FlowMetadata})
260 if handler := oo.getDeviceHandler(incrFlows.Device.Id); handler != nil {
261 if err := handler.UpdateFlowsIncrementally(log.WithSpanFromContext(context.Background(), ctx), incrFlows.Device, incrFlows.Flows, incrFlows.Groups, incrFlows.FlowMetadata); err != nil {
262 return nil, err
263 }
264 return &empty.Empty{}, nil
Girish Gowdru0c588b22019-04-23 23:24:56 -0400265 }
khenaidoo106c61a2021-08-11 18:05:46 -0400266 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": incrFlows.Device.Id}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400267}
268
praneeth nalmas55616d62023-02-06 09:19:18 +0530269// UpdatePmConfig returns PmConfigs nil or error
khenaidoodc2116e2021-10-19 17:33:19 -0400270func (oo *OpenOLT) UpdatePmConfig(ctx context.Context, configs *ca.PmConfigsInfo) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400271 logger.Debugw(ctx, "update_pm_config", log.Fields{"device-id": configs.DeviceId, "pm-configs": configs.PmConfigs})
272 if handler := oo.getDeviceHandler(configs.DeviceId); handler != nil {
273 handler.UpdatePmConfig(log.WithSpanFromContext(context.Background(), ctx), configs.PmConfigs)
274 return &empty.Empty{}, nil
Rohan Agrawalda5e0b22020-05-20 11:10:26 +0000275 }
khenaidoo106c61a2021-08-11 18:05:46 -0400276 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": configs.DeviceId}, nil)
Girish Gowdru0c588b22019-04-23 23:24:56 -0400277}
278
praneeth nalmas55616d62023-02-06 09:19:18 +0530279// SendPacketOut sends packet out to the device
khenaidoodc2116e2021-10-19 17:33:19 -0400280func (oo *OpenOLT) SendPacketOut(ctx context.Context, packet *ca.PacketOut) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400281 logger.Debugw(ctx, "send_packet_out", log.Fields{"device-id": packet.DeviceId, "egress_port_no": packet.EgressPortNo, "pkt": packet.Packet})
282 if handler := oo.getDeviceHandler(packet.DeviceId); handler != nil {
283 if err := handler.PacketOut(log.WithSpanFromContext(context.Background(), ctx), packet.EgressPortNo, packet.Packet); err != nil {
284 return nil, err
285 }
286 return &empty.Empty{}, nil
manikkaraj k9eb6cac2019-05-09 12:32:03 -0400287 }
khenaidoo106c61a2021-08-11 18:05:46 -0400288 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"device-id": packet.DeviceId}, nil)
289
Girish Gowdru0c588b22019-04-23 23:24:56 -0400290}
291
khenaidoo106c61a2021-08-11 18:05:46 -0400292// EnablePort to Enable PON/NNI interface
293func (oo *OpenOLT) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
294 logger.Infow(ctx, "enable_port", log.Fields{"device-id": port.DeviceId, "port": port})
295 if err := oo.enableDisablePort(log.WithSpanFromContext(context.Background(), ctx), port.DeviceId, port, true); err != nil {
296 return nil, err
297 }
298 return &empty.Empty{}, nil
Girish Gowdru0c588b22019-04-23 23:24:56 -0400299}
300
khenaidoo106c61a2021-08-11 18:05:46 -0400301// DisablePort to Disable pon/nni interface
302func (oo *OpenOLT) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) {
303 logger.Infow(ctx, "disable_port", log.Fields{"device-id": port.DeviceId, "port": port})
304 if err := oo.enableDisablePort(log.WithSpanFromContext(context.Background(), ctx), port.DeviceId, port, false); err != nil {
305 return nil, err
306 }
307 return &empty.Empty{}, nil
kesavand39e0aa32020-01-28 20:58:50 -0500308}
309
310// enableDisablePort to Disable pon or Enable PON interface
Neha Sharma96b7bf22020-06-15 10:37:32 +0000311func (oo *OpenOLT) enableDisablePort(ctx context.Context, deviceID string, port *voltha.Port, enablePort bool) error {
divyadesai3af43e12020-08-18 07:10:54 +0000312 logger.Infow(ctx, "enableDisablePort", log.Fields{"device-id": deviceID, "port": port})
kesavand39e0aa32020-01-28 20:58:50 -0500313 if port == nil {
Thomas Lee S94109f12020-03-03 16:39:29 +0530314 return olterrors.NewErrInvalidValue(log.Fields{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800315 "reason": "port cannot be nil",
316 "device-id": deviceID,
Girish Kumarf26e4882020-03-05 06:49:10 +0000317 "port": nil}, nil)
kesavand39e0aa32020-01-28 20:58:50 -0500318 }
319 if handler := oo.getDeviceHandler(deviceID); handler != nil {
divyadesai3af43e12020-08-18 07:10:54 +0000320 logger.Debugw(ctx, "Enable_Disable_Port", log.Fields{"device-id": deviceID, "port": port})
kesavand39e0aa32020-01-28 20:58:50 -0500321 if enablePort {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000322 if err := handler.EnablePort(ctx, port); err != nil {
divyadesai3af43e12020-08-18 07:10:54 +0000323 return olterrors.NewErrAdapter("error-occurred-during-enable-port", log.Fields{"device-id": deviceID, "port": port}, err)
kesavand39e0aa32020-01-28 20:58:50 -0500324 }
325 } else {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000326 if err := handler.DisablePort(ctx, port); err != nil {
divyadesai3af43e12020-08-18 07:10:54 +0000327 return olterrors.NewErrAdapter("error-occurred-during-disable-port", log.Fields{"device-id": deviceID, "port": port}, err)
kesavand39e0aa32020-01-28 20:58:50 -0500328 }
329 }
330 }
331 return nil
332}
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500333
praneeth nalmas55616d62023-02-06 09:19:18 +0530334// ChildDeviceLost deletes the ONU and its references from PONResources
khenaidoo106c61a2021-08-11 18:05:46 -0400335func (oo *OpenOLT) ChildDeviceLost(ctx context.Context, childDevice *voltha.Device) (*empty.Empty, error) {
Girish Gowdraa0870562021-03-11 14:30:14 -0800336 logger.Infow(ctx, "Child-device-lost", log.Fields{"parent-device-id": childDevice.ParentId, "child-device-id": childDevice.Id})
337 if handler := oo.getDeviceHandler(childDevice.ParentId); handler != nil {
khenaidoo106c61a2021-08-11 18:05:46 -0400338 if err := handler.ChildDeviceLost(log.WithSpanFromContext(context.Background(), ctx), childDevice.ParentPortNo, childDevice.ProxyAddress.OnuId, childDevice.SerialNumber); err != nil {
339 return nil, err
340 }
341 return &empty.Empty{}, nil
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500342 }
khenaidoo106c61a2021-08-11 18:05:46 -0400343 return nil, olterrors.NewErrNotFound("device-handler", log.Fields{"parent-device-id": childDevice.ParentId}, nil).Log()
Chaitrashree G S1a55b882020-02-04 17:35:35 -0500344}
Scott Baker24f83e22020-03-30 16:14:28 -0700345
khenaidoo106c61a2021-08-11 18:05:46 -0400346// GetExtValue retrieves a value on a particular ONU
khenaidoodc2116e2021-10-19 17:33:19 -0400347func (oo *OpenOLT) GetExtValue(ctx context.Context, extInfo *ca.GetExtValueMessage) (*extension.ReturnValues, error) {
Dinesh Belwalkardb587af2020-02-27 15:37:16 -0800348 var err error
khenaidoodc2116e2021-10-19 17:33:19 -0400349 resp := new(extension.ReturnValues)
khenaidoo106c61a2021-08-11 18:05:46 -0400350 logger.Infow(ctx, "get_ext_value", log.Fields{"parent-device-id": extInfo.ParentDevice.Id, "onu-id": extInfo.ChildDevice.Id})
351 if handler := oo.getDeviceHandler(extInfo.ParentDevice.Id); handler != nil {
352 if resp, err = handler.getExtValue(ctx, extInfo.ChildDevice, extInfo.ValueType); err != nil {
353 logger.Errorw(ctx, "error-occurred-during-get-ext-value",
354 log.Fields{"parent-device-id": extInfo.ParentDevice.Id, "onu-id": extInfo.ChildDevice.Id, "error": err})
Dinesh Belwalkardb587af2020-02-27 15:37:16 -0800355 return nil, err
356 }
357 }
358 return resp, nil
359}
kesavand62126212021-01-12 04:56:06 -0500360
praneeth nalmas55616d62023-02-06 09:19:18 +0530361// GetSingleValue handles get uni status on ONU and ondemand metric on OLT
khenaidoo106c61a2021-08-11 18:05:46 -0400362func (oo *OpenOLT) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
363 logger.Infow(ctx, "single_get_value_request", log.Fields{"request": request})
kesavand62126212021-01-12 04:56:06 -0500364
365 errResp := func(status extension.GetValueResponse_Status,
366 reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
367 return &extension.SingleGetValueResponse{
368 Response: &extension.GetValueResponse{
369 Status: status,
370 ErrReason: reason,
371 },
372 }
373 }
374 if handler := oo.getDeviceHandler(request.TargetId); handler != nil {
375 switch reqType := request.GetRequest().GetRequest().(type) {
376 case *extension.GetValueRequest_OltPortInfo:
377 return handler.getOltPortCounters(ctx, reqType.OltPortInfo), nil
Himani Chawla2c8ae0f2021-05-18 23:27:00 +0530378 case *extension.GetValueRequest_OnuPonInfo:
379 return handler.getOnuPonCounters(ctx, reqType.OnuPonInfo), nil
Gamze Abaka85e9a142021-05-26 13:41:39 +0000380 case *extension.GetValueRequest_RxPower:
381 return handler.getRxPower(ctx, reqType.RxPower), nil
praneeth nalmas55616d62023-02-06 09:19:18 +0530382 case *extension.GetValueRequest_OltRxPower:
383 return handler.getPONRxPower(ctx, reqType.OltRxPower), nil
kesavand62126212021-01-12 04:56:06 -0500384 default:
385 return errResp(extension.GetValueResponse_ERROR, extension.GetValueResponse_UNSUPPORTED), nil
386 }
387 }
388
389 logger.Infow(ctx, "Single_get_value_request failed ", log.Fields{"request": request})
390 return errResp(extension.GetValueResponse_ERROR, extension.GetValueResponse_INVALID_DEVICE_ID), nil
391}
khenaidoo106c61a2021-08-11 18:05:46 -0400392
393/*
394 * OLT Inter-adapter service
395 */
396
kesavandb9f54fd2021-11-25 20:08:04 +0530397// ProxyOmciRequests proxies an onu sw download OMCI request from the child adapter
398func (oo *OpenOLT) ProxyOmciRequests(ctx context.Context, request *ia.OmciMessages) (*empty.Empty, error) {
399 if handler := oo.getDeviceHandler(request.ParentDeviceId); handler != nil {
400 if err := handler.ProxyOmciRequests(ctx, request); err != nil {
Abhilash Laxmeshwar092e6ca2022-12-08 19:51:27 +0530401 return nil, err
kesavandb9f54fd2021-11-25 20:08:04 +0530402 }
403 return &empty.Empty{}, nil
404 }
405 return nil, olterrors.NewErrNotFound("no-device-handler", log.Fields{"parent-device-id": request.ParentDeviceId, "child-device-id": request.ChildDeviceId}, nil).Log()
406}
407
khenaidoo106c61a2021-08-11 18:05:46 -0400408// ProxyOmciRequest proxies an OMCI request from the child adapter
khenaidoodc2116e2021-10-19 17:33:19 -0400409func (oo *OpenOLT) ProxyOmciRequest(ctx context.Context, request *ia.OmciMessage) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400410 logger.Debugw(ctx, "proxy-omci-request", log.Fields{"request": request})
411
412 if handler := oo.getDeviceHandler(request.ParentDeviceId); handler != nil {
413 if err := handler.ProxyOmciMessage(ctx, request); err != nil {
Abhilash Laxmeshwar092e6ca2022-12-08 19:51:27 +0530414 return nil, err
khenaidoo106c61a2021-08-11 18:05:46 -0400415 }
416 return &empty.Empty{}, nil
417 }
418 return nil, olterrors.NewErrNotFound("no-device-handler", log.Fields{"parent-device-id": request.ParentDeviceId, "child-device-id": request.ChildDeviceId}, nil).Log()
419}
420
421// GetTechProfileInstance returns an instance of a tech profile
khenaidoodc2116e2021-10-19 17:33:19 -0400422func (oo *OpenOLT) GetTechProfileInstance(ctx context.Context, request *ia.TechProfileInstanceRequestMessage) (*ia.TechProfileDownloadMessage, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400423 logger.Debugw(ctx, "getting-tech-profile-request", log.Fields{"request": request})
424
425 targetDeviceID := request.ParentDeviceId
426 if targetDeviceID == "" {
427 return nil, olterrors.NewErrNotFound("parent-id-empty", log.Fields{"parent-device-id": request.ParentDeviceId, "child-device-id": request.DeviceId}, nil).Log()
428 }
429 if handler := oo.getDeviceHandler(targetDeviceID); handler != nil {
430 return handler.GetTechProfileDownloadMessage(ctx, request)
431 }
432 return nil, olterrors.NewErrNotFound("no-device-handler", log.Fields{"parent-device-id": request.ParentDeviceId, "child-device-id": request.DeviceId}, nil).Log()
433
434}
435
khenaidooefff76e2021-12-15 16:51:30 -0500436// GetHealthStatus is used by a OltAdapterService client to detect a connection
437// lost with the gRPC server hosting the OltAdapterService service
438func (oo *OpenOLT) GetHealthStatus(stream adapter_service.AdapterService_GetHealthStatusServer) error {
439 ctx := context.Background()
440 logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream})
441
442 if stream == nil {
443 return fmt.Errorf("conn-is-nil %v", stream)
444 }
445 initialRequestTime := time.Now()
446 var remoteClient *common.Connection
447 var tempClient *common.Connection
448 var err error
449loop:
450 for {
451 tempClient, err = stream.Recv()
452 if err != nil {
453 logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
454 break loop
455 }
456 // Send a response back
457 err = stream.Send(&health.HealthStatus{State: health.HealthStatus_HEALTHY})
458 if err != nil {
459 logger.Warnw(ctx, "sending-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
460 break loop
461 }
462
463 remoteClient = tempClient
464 logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient})
465
466 select {
467 case <-stream.Context().Done():
468 logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()})
469 break loop
470 case <-oo.exitChannel:
471 logger.Warnw(ctx, "received-stop", log.Fields{"remote-client": remoteClient, "initial-conn-time": initialRequestTime})
472 break loop
473 default:
474 }
475 }
476 logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime})
477 return err
478}
479
khenaidoo106c61a2021-08-11 18:05:46 -0400480/*
481 *
482 * Unimplemented APIs
483 *
484 */
485
praneeth nalmas55616d62023-02-06 09:19:18 +0530486// SimulateAlarm is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400487func (oo *OpenOLT) SimulateAlarm(context.Context, *ca.SimulateAlarmMessage) (*voltha.OperationResp, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400488 return nil, olterrors.ErrNotImplemented
489}
490
praneeth nalmas55616d62023-02-06 09:19:18 +0530491// SetExtValue is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400492func (oo *OpenOLT) SetExtValue(context.Context, *ca.SetExtValueMessage) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400493 return nil, olterrors.ErrNotImplemented
494}
495
praneeth nalmas55616d62023-02-06 09:19:18 +0530496// SetSingleValue is unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400497func (oo *OpenOLT) SetSingleValue(context.Context, *extension.SingleSetValueRequest) (*extension.SingleSetValueResponse, error) {
498 return nil, olterrors.ErrNotImplemented
499}
500
praneeth nalmas55616d62023-02-06 09:19:18 +0530501// StartOmciTest not implemented
khenaidoodc2116e2021-10-19 17:33:19 -0400502func (oo *OpenOLT) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400503 return nil, olterrors.ErrNotImplemented
504}
505
praneeth nalmas55616d62023-02-06 09:19:18 +0530506// SuppressEvent unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400507func (oo *OpenOLT) SuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
508 return nil, olterrors.ErrNotImplemented
509}
510
praneeth nalmas55616d62023-02-06 09:19:18 +0530511// UnSuppressEvent unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400512func (oo *OpenOLT) UnSuppressEvent(ctx context.Context, filter *voltha.EventFilter) (*empty.Empty, error) {
513 return nil, olterrors.ErrNotImplemented
514}
515
praneeth nalmas55616d62023-02-06 09:19:18 +0530516// DownloadImage is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400517func (oo *OpenOLT) DownloadImage(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400518 return nil, olterrors.ErrNotImplemented
519}
520
praneeth nalmas55616d62023-02-06 09:19:18 +0530521// GetImageDownloadStatus is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400522func (oo *OpenOLT) GetImageDownloadStatus(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400523 return nil, olterrors.ErrNotImplemented
524}
525
praneeth nalmas55616d62023-02-06 09:19:18 +0530526// CancelImageDownload is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400527func (oo *OpenOLT) CancelImageDownload(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400528 return nil, olterrors.ErrNotImplemented
529}
530
praneeth nalmas55616d62023-02-06 09:19:18 +0530531// ActivateImageUpdate is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400532func (oo *OpenOLT) ActivateImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400533 return nil, olterrors.ErrNotImplemented
534}
535
praneeth nalmas55616d62023-02-06 09:19:18 +0530536// RevertImageUpdate is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400537func (oo *OpenOLT) RevertImageUpdate(ctx context.Context, imageInfo *ca.ImageDownloadMessage) (*voltha.ImageDownload, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400538 return nil, olterrors.ErrNotImplemented
539}
540
praneeth nalmas55616d62023-02-06 09:19:18 +0530541// DownloadOnuImage unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400542func (oo *OpenOLT) DownloadOnuImage(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) {
543 return nil, olterrors.ErrNotImplemented
544}
545
praneeth nalmas55616d62023-02-06 09:19:18 +0530546// GetOnuImageStatus unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400547func (oo *OpenOLT) GetOnuImageStatus(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
548 return nil, olterrors.ErrNotImplemented
549}
550
praneeth nalmas55616d62023-02-06 09:19:18 +0530551// AbortOnuImageUpgrade unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400552func (oo *OpenOLT) AbortOnuImageUpgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
553 return nil, olterrors.ErrNotImplemented
554}
555
praneeth nalmas55616d62023-02-06 09:19:18 +0530556// GetOnuImages unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400557func (oo *OpenOLT) GetOnuImages(ctx context.Context, deviceID *common.ID) (*voltha.OnuImages, error) {
558 return nil, olterrors.ErrNotImplemented
559}
560
praneeth nalmas55616d62023-02-06 09:19:18 +0530561// ActivateOnuImage unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400562func (oo *OpenOLT) ActivateOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
563 return nil, olterrors.ErrNotImplemented
564}
565
praneeth nalmas55616d62023-02-06 09:19:18 +0530566// CommitOnuImage unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400567func (oo *OpenOLT) CommitOnuImage(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) {
568 return nil, olterrors.ErrNotImplemented
569}
570
571// UpdateFlowsBulk is unimplemented
khenaidoodc2116e2021-10-19 17:33:19 -0400572func (oo *OpenOLT) UpdateFlowsBulk(ctx context.Context, flows *ca.BulkFlows) (*empty.Empty, error) {
khenaidoo106c61a2021-08-11 18:05:46 -0400573 return nil, olterrors.ErrNotImplemented
574}
575
praneeth nalmas55616d62023-02-06 09:19:18 +0530576// SelfTestDevice unimplemented
khenaidoo106c61a2021-08-11 18:05:46 -0400577func (oo *OpenOLT) SelfTestDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
578 return nil, olterrors.ErrNotImplemented
579}