blob: 5a9d7794c5a62eaf4bb14d540691facc4542c5a2 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
Joey Armstrong7a9af442024-01-03 19:26:36 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidooab1f7bd2019-11-14 14:00:27 -05003
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 */
npujar1d86a522019-11-14 17:11:16 +053016
khenaidooab1f7bd2019-11-14 14:00:27 -050017package mocks
18
19import (
20 "context"
Scott Baker432f9be2020-03-26 11:56:30 -070021 "errors"
khenaidooab1f7bd2019-11-14 14:00:27 -050022 "fmt"
khenaidood948f772021-08-11 17:49:24 -040023 "strconv"
Kent Hagerman2a07b862020-06-19 15:23:07 -040024 "strings"
khenaidooa46458b2021-12-15 16:50:44 -050025 "time"
Kent Hagerman2a07b862020-06-19 15:23:07 -040026
khenaidood948f772021-08-11 17:49:24 -040027 "github.com/golang/protobuf/ptypes/empty"
28 "github.com/opencord/voltha-lib-go/v7/pkg/probe"
khenaidooa46458b2021-12-15 16:50:44 -050029 "github.com/opencord/voltha-protos/v5/go/adapter_service"
khenaidood948f772021-08-11 17:49:24 -040030 "github.com/opencord/voltha-protos/v5/go/common"
31 "github.com/opencord/voltha-protos/v5/go/extension"
khenaidooa46458b2021-12-15 16:50:44 -050032 "github.com/opencord/voltha-protos/v5/go/health"
khenaidoo9beaaf12021-10-19 17:32:01 -040033 "github.com/opencord/voltha-protos/v5/go/omci"
khenaidood948f772021-08-11 17:49:24 -040034 "github.com/phayes/freeport"
35
khenaidooab1f7bd2019-11-14 14:00:27 -050036 "github.com/gogo/protobuf/proto"
khenaidood948f772021-08-11 17:49:24 -040037 com "github.com/opencord/voltha-lib-go/v7/pkg/adapters/common"
38 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
39 "github.com/opencord/voltha-lib-go/v7/pkg/log"
khenaidoo9beaaf12021-10-19 17:32:01 -040040 ca "github.com/opencord/voltha-protos/v5/go/core_adapter"
khenaidood948f772021-08-11 17:49:24 -040041 of "github.com/opencord/voltha-protos/v5/go/openflow_13"
42 "github.com/opencord/voltha-protos/v5/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050043)
44
npujar1d86a522019-11-14 17:11:16 +053045// OLTAdapter represent OLT adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050046type OLTAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040047 *Adapter
khenaidood948f772021-08-11 17:49:24 -040048 ChildDeviceType string
49 childVendor string
50 grpcServer *vgrpc.GrpcServer
khenaidooab1f7bd2019-11-14 14:00:27 -050051}
52
npujar1d86a522019-11-14 17:11:16 +053053// NewOLTAdapter - creates OLT adapter instance
khenaidood948f772021-08-11 17:49:24 -040054func NewOLTAdapter(ctx context.Context, coreEndpoint string, deviceType string, vendor string, childDeviceType, childVendor string) *OLTAdapter {
55 // Get an available port
56 grpcPort, err := freeport.GetFreePort()
57 if err != nil {
58 logger.Fatalw(ctx, "no-free-port", log.Fields{"error": err})
khenaidoo67b22152020-03-02 16:01:25 -050059 }
khenaidood948f772021-08-11 17:49:24 -040060
61 // start gRPC handler
62 listeningAddress := fmt.Sprintf("127.0.0.1:%s", strconv.Itoa(grpcPort))
63 oltAdapter := &OLTAdapter{Adapter: NewAdapter(listeningAddress, coreEndpoint, deviceType, vendor),
64 ChildDeviceType: childDeviceType, childVendor: childVendor}
65
66 oltAdapter.start(ctx)
67 return oltAdapter
68}
69
70func (oltA *OLTAdapter) oltRestarted(ctx context.Context, endPoint string) error {
71 logger.Errorw(ctx, "remote-restarted", log.Fields{"endpoint": endPoint})
72 return nil
73}
74
75func (oltA *OLTAdapter) start(ctx context.Context) {
76
77 // Set up the probe service
78 oltA.Probe = &probe.Probe{}
79 probePort, err := freeport.GetFreePort()
80 if err != nil {
81 logger.Fatal(ctx, "Cannot get a freeport for probePort")
82 }
83 probeAddress := "127.0.0.1:" + strconv.Itoa(probePort)
84 go oltA.Probe.ListenAndServe(ctx, probeAddress)
85
86 probeCtx := context.WithValue(ctx, probe.ProbeContextKey, oltA.Probe)
87
88 oltA.Probe.RegisterService(ctx, "olt-grpc-service", oltA.coreEnpoint)
89
90 oltA.grpcServer = vgrpc.NewGrpcServer(oltA.serviceEndpoint, nil, false, nil)
91
92 logger.Debugw(ctx, "OLTAdapter-address", log.Fields{"address": oltA.serviceEndpoint})
93
94 go oltA.startGRPCService(ctx, oltA.grpcServer, oltA, "olt-grpc-service")
95
96 // Establish grpc connection to Core
khenaidoo25057da2021-12-08 14:40:45 -050097 if oltA.coreClient, err = vgrpc.NewClient(
khenaidooa46458b2021-12-15 16:50:44 -050098 "mock-olt-endpoint",
khenaidoo25057da2021-12-08 14:40:45 -050099 oltA.coreEnpoint,
khenaidooa46458b2021-12-15 16:50:44 -0500100 "core_service.CoreService",
khenaidoo25057da2021-12-08 14:40:45 -0500101 oltA.oltRestarted); err != nil {
khenaidood948f772021-08-11 17:49:24 -0400102 logger.Fatal(ctx, "grpc-client-not-created")
103 }
104
khenaidooa46458b2021-12-15 16:50:44 -0500105 go oltA.coreClient.Start(probeCtx, setCoreServiceHandler)
khenaidood948f772021-08-11 17:49:24 -0400106
107 logger.Debugw(ctx, "OLTAdapter-started", log.Fields{"grpc-address": oltA.serviceEndpoint})
108
109}
110
111// Stop brings down core services
112func (oltA *OLTAdapter) StopGrpcClient() {
113 // Stop the grpc clients
114 oltA.coreClient.Stop(context.Background())
115}
116
117// Stop brings down core services
118func (oltA *OLTAdapter) Stop() {
119
120 // Stop the grpc
121 if oltA.grpcServer != nil {
122 oltA.grpcServer.Stop()
123 }
124 logger.Debugw(context.Background(), "OLTAdapter-stopped", log.Fields{"grpc-address": oltA.serviceEndpoint})
125
khenaidooab1f7bd2019-11-14 14:00:27 -0500126}
127
npujar1d86a522019-11-14 17:11:16 +0530128// Adopt_device creates new handler for added device
khenaidood948f772021-08-11 17:49:24 -0400129func (oltA *OLTAdapter) AdoptDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) {
130 logger.Debugw(ctx, "AdoptDevice", log.Fields{"device": device.AdapterEndpoint, "device-type": oltA.DeviceType})
khenaidooab1f7bd2019-11-14 14:00:27 -0500131 go func() {
132 d := proto.Clone(device).(*voltha.Device)
133 d.Root = true
khenaidood948f772021-08-11 17:49:24 -0400134 d.Vendor = oltA.vendor
khenaidooab1f7bd2019-11-14 14:00:27 -0500135 d.Model = "go-mock"
136 d.SerialNumber = com.GetRandomSerialNumber()
137 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
138 oltA.storeDevice(d)
khenaidood948f772021-08-11 17:49:24 -0400139 c, err := oltA.GetCoreClient()
140 if err != nil {
141 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500142 }
khenaidood948f772021-08-11 17:49:24 -0400143 if _, err := c.DeviceUpdate(context.TODO(), d); err != nil {
144 logger.Fatalf(ctx, "deviceUpdate-failed-%s", err)
145 }
146
khenaidooc6c7bda2020-06-17 17:20:18 -0400147 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -0500148 nniPort := &voltha.Port{
khenaidood948f772021-08-11 17:49:24 -0400149 DeviceId: device.Id,
khenaidooab1f7bd2019-11-14 14:00:27 -0500150 PortNo: 2,
151 Label: fmt.Sprintf("nni-%d", 2),
152 Type: voltha.Port_ETHERNET_NNI,
khenaidood948f772021-08-11 17:49:24 -0400153 OperStatus: common.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -0400154 OfpPort: &of.OfpPort{
155 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
156 Config: 0,
157 State: uint32(of.OfpPortState_OFPPS_LIVE),
158 Curr: capability,
159 Advertised: capability,
160 Peer: capability,
161 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
162 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
163 },
khenaidooab1f7bd2019-11-14 14:00:27 -0500164 }
khenaidood948f772021-08-11 17:49:24 -0400165 if _, err = c.PortCreated(context.TODO(), nniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000166 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500167 }
168
169 ponPort := &voltha.Port{
khenaidood948f772021-08-11 17:49:24 -0400170 DeviceId: device.Id,
khenaidooab1f7bd2019-11-14 14:00:27 -0500171 PortNo: 1,
172 Label: fmt.Sprintf("pon-%d", 1),
173 Type: voltha.Port_PON_OLT,
khenaidood948f772021-08-11 17:49:24 -0400174 OperStatus: common.OperStatus_ACTIVE,
khenaidooab1f7bd2019-11-14 14:00:27 -0500175 }
khenaidood948f772021-08-11 17:49:24 -0400176 if _, err = c.PortCreated(context.TODO(), ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000177 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500178 }
179
khenaidood948f772021-08-11 17:49:24 -0400180 d.ConnectStatus = common.ConnectStatus_REACHABLE
181 d.OperStatus = common.OperStatus_ACTIVE
khenaidooab1f7bd2019-11-14 14:00:27 -0500182
khenaidoo9beaaf12021-10-19 17:32:01 -0400183 if _, err = c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{DeviceId: d.Id, OperStatus: d.OperStatus, ConnStatus: d.ConnectStatus}); err != nil {
khenaidood948f772021-08-11 17:49:24 -0400184 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500185 }
186
187 //Get the latest device data from the Core
khenaidood948f772021-08-11 17:49:24 -0400188 if d, err = c.GetDevice(context.TODO(), &common.ID{Id: d.Id}); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500190 }
191
khenaidoo8b4abbf2020-04-24 17:04:30 -0400192 oltA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500193
194 // Register Child devices
khenaidoo67b22152020-03-02 16:01:25 -0500195 initialUniPortNo := startingUNIPortNo
khenaidooab1f7bd2019-11-14 14:00:27 -0500196 for i := 0; i < numONUPerOLT; i++ {
197 go func(seqNo int) {
khenaidood948f772021-08-11 17:49:24 -0400198 if _, err := c.ChildDeviceDetected(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400199 &ca.DeviceDiscovery{
khenaidood948f772021-08-11 17:49:24 -0400200 ParentId: d.Id,
201 ParentPortNo: 1,
202 ChildDeviceType: oltA.ChildDeviceType,
203 ChannelId: uint32(initialUniPortNo + seqNo),
204 VendorId: oltA.childVendor,
205 SerialNumber: com.GetRandomSerialNumber(),
206 OnuId: uint32(seqNo),
207 }); err != nil {
208 logger.Fatalw(ctx, "failure-sending-child-device", log.Fields{"error": err, "parent-id": d.Id, "child-device-type": oltA.ChildDeviceType})
khenaidooab1f7bd2019-11-14 14:00:27 -0500209 }
210 }(i)
211 }
212 }()
khenaidood948f772021-08-11 17:49:24 -0400213 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500214}
215
Andrea Campanella025667e2021-01-14 11:50:07 +0100216// Single_get_value_request retrieves a single value.
217func (oltA *OLTAdapter) Single_get_value_request(ctx context.Context, // nolint
218 request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
219 logger.Fatalf(ctx, "Single_get_value_request unimplemented")
220 return nil, nil
221}
222
npujar1d86a522019-11-14 17:11:16 +0530223// Get_ofp_device_info returns ofp device info
khenaidoo9beaaf12021-10-19 17:32:01 -0400224func (oltA *OLTAdapter) GetOfpDeviceInfo(ctx context.Context, device *voltha.Device) (*ca.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500225 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000226 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500227 }
khenaidoo9beaaf12021-10-19 17:32:01 -0400228 return &ca.SwitchCapability{
khenaidooab1f7bd2019-11-14 14:00:27 -0500229 Desc: &of.OfpDesc{
230 HwDesc: "olt_adapter_mock",
231 SwDesc: "olt_adapter_mock",
232 SerialNum: "12345678",
233 },
234 SwitchFeatures: &of.OfpSwitchFeatures{
235 NBuffers: 256,
236 NTables: 2,
237 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
238 of.OfpCapabilities_OFPC_TABLE_STATS |
239 of.OfpCapabilities_OFPC_PORT_STATS |
240 of.OfpCapabilities_OFPC_GROUP_STATS),
241 },
242 }, nil
243}
244
npujar1d86a522019-11-14 17:11:16 +0530245// Disable_device disables device
khenaidood948f772021-08-11 17:49:24 -0400246func (oltA *OLTAdapter) DisableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500247 go func() {
248 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000249 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500250 }
251
252 cloned := proto.Clone(device).(*voltha.Device)
253 // Update the all ports state on that device to disable
khenaidood948f772021-08-11 17:49:24 -0400254 c, err := oltA.GetCoreClient()
255 if err != nil {
256 return
257 }
258
259 if _, err := c.PortsStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400260 &ca.PortStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400261 DeviceId: cloned.Id,
262 PortTypeFilter: 0,
263 OperStatus: common.OperStatus_UNKNOWN,
264 }); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000265 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500266 }
267
Girish Gowdra408cd962020-03-11 14:31:31 -0700268 //Update the device operational state
khenaidood948f772021-08-11 17:49:24 -0400269 cloned.OperStatus = common.OperStatus_UNKNOWN
Girish Gowdra408cd962020-03-11 14:31:31 -0700270 // The device is still reachable after it has been disabled, so the connection status should not be changed.
khenaidooab1f7bd2019-11-14 14:00:27 -0500271
khenaidoo9beaaf12021-10-19 17:32:01 -0400272 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400273 DeviceId: cloned.Id,
274 OperStatus: cloned.OperStatus,
275 ConnStatus: cloned.ConnectStatus,
276 }); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400277 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000278 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400279 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500280 }
281
khenaidoo8b4abbf2020-04-24 17:04:30 -0400282 oltA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500283
284 // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core
khenaidood948f772021-08-11 17:49:24 -0400285 if _, err := c.ChildDevicesLost(context.TODO(), &common.ID{Id: cloned.Id}); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400286 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000287 logger.Warnw(ctx, "lost-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500288 }
289 }()
khenaidood948f772021-08-11 17:49:24 -0400290 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500291}
292
npujar1d86a522019-11-14 17:11:16 +0530293// Reenable_device reenables device
khenaidood948f772021-08-11 17:49:24 -0400294func (oltA *OLTAdapter) ReEnableDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500295 go func() {
296 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000297 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500298 }
299
300 cloned := proto.Clone(device).(*voltha.Device)
khenaidood948f772021-08-11 17:49:24 -0400301
302 c, err := oltA.GetCoreClient()
303 if err != nil {
304 return
305 }
306
khenaidooab1f7bd2019-11-14 14:00:27 -0500307 // Update the all ports state on that device to enable
khenaidood948f772021-08-11 17:49:24 -0400308 if _, err := c.PortsStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400309 &ca.PortStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400310 DeviceId: cloned.Id,
311 PortTypeFilter: 0,
312 OperStatus: common.OperStatus_ACTIVE,
313 }); err != nil {
314 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500315 }
316
317 //Update the device state
khenaidood948f772021-08-11 17:49:24 -0400318 cloned.OperStatus = common.OperStatus_ACTIVE
khenaidooab1f7bd2019-11-14 14:00:27 -0500319
khenaidoo9beaaf12021-10-19 17:32:01 -0400320 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400321 DeviceId: cloned.Id,
322 OperStatus: cloned.OperStatus,
323 ConnStatus: cloned.ConnectStatus,
324 }); err != nil {
325 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000326 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidood948f772021-08-11 17:49:24 -0400327 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500328 }
329
330 // Tell the Core that all child devices have been enabled
khenaidood948f772021-08-11 17:49:24 -0400331 if _, err := c.ChildDevicesDetected(context.TODO(), &common.ID{Id: cloned.Id}); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000332 logger.Fatalf(ctx, "detection-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500333 }
334 }()
khenaidood948f772021-08-11 17:49:24 -0400335 return &empty.Empty{}, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500336}
kesavandbc2d1622020-01-21 00:42:01 -0500337
338// Enable_port -
khenaidood948f772021-08-11 17:49:24 -0400339func (oltA *OLTAdapter) EnablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500340 go func() {
khenaidood948f772021-08-11 17:49:24 -0400341 c, err := oltA.GetCoreClient()
342 if err != nil {
343 return
344 }
kesavandbc2d1622020-01-21 00:42:01 -0500345
khenaidood948f772021-08-11 17:49:24 -0400346 if port.Type == voltha.Port_PON_OLT {
347 if _, err := c.PortStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400348 &ca.PortState{
khenaidood948f772021-08-11 17:49:24 -0400349 DeviceId: port.DeviceId,
350 PortType: voltha.Port_ETHERNET_NNI,
351 PortNo: port.PortNo,
352 OperStatus: common.OperStatus_ACTIVE,
353 }); err != nil {
354 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": port.DeviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500355 }
356 }
357
358 }()
khenaidood948f772021-08-11 17:49:24 -0400359 return &empty.Empty{}, nil
kesavandbc2d1622020-01-21 00:42:01 -0500360}
361
362// Disable_port -
khenaidood948f772021-08-11 17:49:24 -0400363func (oltA *OLTAdapter) DisablePort(ctx context.Context, port *voltha.Port) (*empty.Empty, error) { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500364 go func() {
khenaidood948f772021-08-11 17:49:24 -0400365 c, err := oltA.GetCoreClient()
366 if err != nil {
367 return
368 }
369 if port.Type == voltha.Port_PON_OLT {
370 if _, err := c.PortStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400371 &ca.PortState{
khenaidood948f772021-08-11 17:49:24 -0400372 DeviceId: port.DeviceId,
373 PortType: voltha.Port_PON_OLT,
374 PortNo: port.PortNo,
375 OperStatus: common.OperStatus_DISCOVERED,
376 }); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400377 // Corresponding device may have been deleted
khenaidood948f772021-08-11 17:49:24 -0400378 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": port.DeviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500379 }
380 }
381 }()
khenaidood948f772021-08-11 17:49:24 -0400382 return &empty.Empty{}, nil
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500383}
khenaidoo67b22152020-03-02 16:01:25 -0500384
Girish Gowdra408cd962020-03-11 14:31:31 -0700385// Reboot_device -
khenaidood948f772021-08-11 17:49:24 -0400386func (oltA *OLTAdapter) RebootDevice(ctx context.Context, device *voltha.Device) (*empty.Empty, error) { // nolint
divyadesaicb8b59d2020-08-18 09:55:47 +0000387 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
Girish Gowdra408cd962020-03-11 14:31:31 -0700388
389 go func() {
khenaidood948f772021-08-11 17:49:24 -0400390 c, err := oltA.GetCoreClient()
391 if err != nil {
392 return
Girish Gowdra408cd962020-03-11 14:31:31 -0700393 }
khenaidood948f772021-08-11 17:49:24 -0400394
khenaidoo9beaaf12021-10-19 17:32:01 -0400395 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400396 DeviceId: device.Id,
397 OperStatus: common.OperStatus_UNKNOWN,
398 ConnStatus: common.ConnectStatus_UNREACHABLE,
399 }); err != nil {
400 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
401 return
402 }
403
404 if _, err := c.PortsStateUpdate(context.TODO(),
khenaidoo9beaaf12021-10-19 17:32:01 -0400405 &ca.PortStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400406 DeviceId: device.Id,
407 PortTypeFilter: 0,
408 OperStatus: common.OperStatus_UNKNOWN,
409 }); err != nil {
410 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700411 }
412 }()
khenaidood948f772021-08-11 17:49:24 -0400413 return &empty.Empty{}, nil
Girish Gowdra408cd962020-03-11 14:31:31 -0700414}
415
Scott Baker432f9be2020-03-26 11:56:30 -0700416// TODO: REMOVE Start_omci_test begins an omci self-test
khenaidoo9beaaf12021-10-19 17:32:01 -0400417func (oltA *OLTAdapter) StartOmciTest(ctx context.Context, test *ca.OMCITest) (*omci.TestResponse, error) { // nolint
Scott Baker432f9be2020-03-26 11:56:30 -0700418 return nil, errors.New("start-omci-test-not-implemented")
419}
420
khenaidood948f772021-08-11 17:49:24 -0400421// Helper for test only
422func (oltA *OLTAdapter) SetDeviceActive(deviceID string) {
423 c, err := oltA.GetCoreClient()
424 if err != nil {
425 return
426 }
ssiddiquif076cb82021-04-23 10:47:04 +0530427
khenaidoo9beaaf12021-10-19 17:32:01 -0400428 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
khenaidood948f772021-08-11 17:49:24 -0400429 DeviceId: deviceID,
430 OperStatus: common.OperStatus_ACTIVE,
431 ConnStatus: common.ConnectStatus_REACHABLE,
432 }); err != nil {
433 logger.Warnw(context.Background(), "device-state-update-failed", log.Fields{"device-id": deviceID, "error": err})
434 return
435 }
ssiddiquif076cb82021-04-23 10:47:04 +0530436
yasin sapli5458a1c2021-06-14 22:24:38 +0000437}
khenaidooa46458b2021-12-15 16:50:44 -0500438
439func (oltA *OLTAdapter) GetHealthStatus(stream adapter_service.AdapterService_GetHealthStatusServer) error {
440 ctx := context.Background()
441 logger.Debugw(ctx, "receive-stream-connection", log.Fields{"stream": stream})
442
443 if stream == nil {
444 return fmt.Errorf("conn-is-nil %v", stream)
445 }
446 initialRequestTime := time.Now()
447 var remoteClient *common.Connection
448 var tempClient *common.Connection
449 var err error
450loop:
451 for {
452 tempClient, err = stream.Recv()
453 if err != nil {
454 logger.Warnw(ctx, "received-stream-error", log.Fields{"remote-client": remoteClient, "error": err})
455 break loop
456 }
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 remoteClient = tempClient
463 logger.Debugw(ctx, "received-keep-alive", log.Fields{"remote-client": remoteClient})
464
465 select {
466 case <-stream.Context().Done():
467 logger.Infow(ctx, "stream-keep-alive-context-done", log.Fields{"remote-client": remoteClient, "error": stream.Context().Err()})
468 break loop
469 default:
470 }
471 }
472 logger.Errorw(ctx, "connection-down", log.Fields{"remote-client": remoteClient, "error": err, "initial-conn-time": initialRequestTime})
473 return err
474}
Abhilash Laxmeshwar723ed742022-06-01 12:24:18 +0530475
476func (oltA *OLTAdapter) SetDeviceRebooted(deviceID string) {
477 c, err := oltA.GetCoreClient()
478 if err != nil {
479 return
480 }
481
482 if _, err := c.DeviceStateUpdate(context.TODO(), &ca.DeviceStateFilter{
483 DeviceId: deviceID,
484 OperStatus: common.OperStatus_REBOOTED,
485 ConnStatus: common.ConnectStatus_REACHABLE,
486 }); err != nil {
487 logger.Warnw(context.Background(), "device-state-update-failed", log.Fields{"device-id": deviceID, "error": err})
488 return
489 }
490
491}