blob: a07d073fbdd37b2d30365c670b862b0b5d6e73de [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001/*
2 * Copyright 2019-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 */
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"
Andrea Campanella025667e2021-01-14 11:50:07 +010023 "github.com/opencord/voltha-protos/v4/go/extension"
Kent Hagerman2a07b862020-06-19 15:23:07 -040024 "strings"
25
khenaidooab1f7bd2019-11-14 14:00:27 -050026 "github.com/gogo/protobuf/proto"
Maninderdfadc982020-10-28 14:04:33 +053027 "github.com/opencord/voltha-lib-go/v4/pkg/adapters"
28 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
29 com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common"
30 "github.com/opencord/voltha-lib-go/v4/pkg/log"
31 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
32 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
33 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050034)
35
36const (
khenaidoo67b22152020-03-02 16:01:25 -050037 numONUPerOLT = 4
38 startingUNIPortNo = 100
khenaidooab1f7bd2019-11-14 14:00:27 -050039)
40
Kent Hagerman2a07b862020-06-19 15:23:07 -040041// static implementation check
42var _ adapters.IAdapter = &OLTAdapter{}
43
npujar1d86a522019-11-14 17:11:16 +053044// OLTAdapter represent OLT adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050045type OLTAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040046 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050047}
48
npujar1d86a522019-11-14 17:11:16 +053049// NewOLTAdapter - creates OLT adapter instance
Rohan Agrawal31f21802020-06-12 05:38:46 +000050func NewOLTAdapter(ctx context.Context, cp adapterif.CoreProxy) *OLTAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050051 return &OLTAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040052 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050053 }
khenaidooab1f7bd2019-11-14 14:00:27 -050054}
55
npujar1d86a522019-11-14 17:11:16 +053056// Adopt_device creates new handler for added device
Rohan Agrawal31f21802020-06-12 05:38:46 +000057func (oltA *OLTAdapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050058 go func() {
59 d := proto.Clone(device).(*voltha.Device)
60 d.Root = true
61 d.Vendor = "olt_adapter_mock"
62 d.Model = "go-mock"
63 d.SerialNumber = com.GetRandomSerialNumber()
64 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
65 oltA.storeDevice(d)
66 if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000067 logger.Fatalf(ctx, "deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050068 }
khenaidooc6c7bda2020-06-17 17:20:18 -040069 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050070 nniPort := &voltha.Port{
71 PortNo: 2,
72 Label: fmt.Sprintf("nni-%d", 2),
73 Type: voltha.Port_ETHERNET_NNI,
74 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040075 OfpPort: &of.OfpPort{
76 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
77 Config: 0,
78 State: uint32(of.OfpPortState_OFPPS_LIVE),
79 Curr: capability,
80 Advertised: capability,
81 Peer: capability,
82 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
83 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
84 },
khenaidooab1f7bd2019-11-14 14:00:27 -050085 }
86 var err error
87 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000088 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050089 }
90
91 ponPort := &voltha.Port{
92 PortNo: 1,
93 Label: fmt.Sprintf("pon-%d", 1),
94 Type: voltha.Port_PON_OLT,
95 OperStatus: voltha.OperStatus_ACTIVE,
96 }
97 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000098 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050099 }
100
101 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
102 d.OperStatus = voltha.OperStatus_ACTIVE
103
104 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000105 logger.Fatalf(ctx, "Device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500106 }
107
108 //Get the latest device data from the Core
109 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000110 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500111 }
112
khenaidoo8b4abbf2020-04-24 17:04:30 -0400113 oltA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500114
115 // Register Child devices
khenaidoo67b22152020-03-02 16:01:25 -0500116 initialUniPortNo := startingUNIPortNo
khenaidooab1f7bd2019-11-14 14:00:27 -0500117 for i := 0; i < numONUPerOLT; i++ {
118 go func(seqNo int) {
119 if _, err := oltA.coreProxy.ChildDeviceDetected(
120 context.TODO(),
121 d.Id,
122 1,
123 "onu_adapter_mock",
124 initialUniPortNo+seqNo,
125 "onu_adapter_mock",
126 com.GetRandomSerialNumber(),
127 int64(seqNo)); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128 logger.Fatalf(ctx, "failure-sending-child-device-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500129 }
130 }(i)
131 }
132 }()
133 return nil
134}
135
Andrea Campanella025667e2021-01-14 11:50:07 +0100136// Single_get_value_request retrieves a single value.
137func (oltA *OLTAdapter) Single_get_value_request(ctx context.Context, // nolint
138 request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
139 logger.Fatalf(ctx, "Single_get_value_request unimplemented")
140 return nil, nil
141}
142
npujar1d86a522019-11-14 17:11:16 +0530143// Get_ofp_device_info returns ofp device info
Rohan Agrawal31f21802020-06-12 05:38:46 +0000144func (oltA *OLTAdapter) Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000146 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500147 }
148 return &ic.SwitchCapability{
149 Desc: &of.OfpDesc{
150 HwDesc: "olt_adapter_mock",
151 SwDesc: "olt_adapter_mock",
152 SerialNum: "12345678",
153 },
154 SwitchFeatures: &of.OfpSwitchFeatures{
155 NBuffers: 256,
156 NTables: 2,
157 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
158 of.OfpCapabilities_OFPC_TABLE_STATS |
159 of.OfpCapabilities_OFPC_PORT_STATS |
160 of.OfpCapabilities_OFPC_GROUP_STATS),
161 },
162 }, nil
163}
164
npujar1d86a522019-11-14 17:11:16 +0530165// GetNumONUPerOLT returns number of ONUs per OLT
khenaidooab1f7bd2019-11-14 14:00:27 -0500166func (oltA *OLTAdapter) GetNumONUPerOLT() int {
167 return numONUPerOLT
168}
169
khenaidoo67b22152020-03-02 16:01:25 -0500170// Returns the starting UNI port number
171func (oltA *OLTAdapter) GetStartingUNIPortNo() int {
172 return startingUNIPortNo
173}
174
npujar1d86a522019-11-14 17:11:16 +0530175// Disable_device disables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000176func (oltA *OLTAdapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500177 go func() {
178 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000179 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500180 }
181
182 cloned := proto.Clone(device).(*voltha.Device)
183 // Update the all ports state on that device to disable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400184 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000185 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500186 }
187
Girish Gowdra408cd962020-03-11 14:31:31 -0700188 //Update the device operational state
khenaidooab1f7bd2019-11-14 14:00:27 -0500189 cloned.OperStatus = voltha.OperStatus_UNKNOWN
Girish Gowdra408cd962020-03-11 14:31:31 -0700190 // The device is still reachable after it has been disabled, so the connection status should not be changed.
khenaidooab1f7bd2019-11-14 14:00:27 -0500191
192 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400193 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000194 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400195 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500196 }
197
khenaidoo8b4abbf2020-04-24 17:04:30 -0400198 oltA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500199
200 // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core
201 if err := oltA.coreProxy.ChildDevicesLost(context.TODO(), cloned.Id); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400202 // Device may already have been deleted in the core
divyadesaicb8b59d2020-08-18 09:55:47 +0000203 logger.Warnw(ctx, "lost-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500204 }
205 }()
206 return nil
207}
208
npujar1d86a522019-11-14 17:11:16 +0530209// Reenable_device reenables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000210func (oltA *OLTAdapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500211 go func() {
212 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000213 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500214 }
215
216 cloned := proto.Clone(device).(*voltha.Device)
217 // Update the all ports state on that device to enable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400218 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_ACTIVE); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000219 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500220 }
221
222 //Update the device state
khenaidooab1f7bd2019-11-14 14:00:27 -0500223 cloned.OperStatus = voltha.OperStatus_ACTIVE
224
225 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000226 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500227 }
228
229 // Tell the Core that all child devices have been enabled
230 if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil {
divyadesaicb8b59d2020-08-18 09:55:47 +0000231 logger.Fatalf(ctx, "detection-notif-of-child-devices-failed", log.Fields{"device-id": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500232 }
233 }()
234 return nil
235}
kesavandbc2d1622020-01-21 00:42:01 -0500236
237// Enable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000238func (oltA *OLTAdapter) Enable_port(ctx context.Context, deviceId string, Port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500239 go func() {
240
241 if Port.Type == voltha.Port_PON_OLT {
242 if err := oltA.coreProxy.PortStateUpdate(context.TODO(), deviceId, voltha.Port_PON_OLT, Port.PortNo, voltha.OperStatus_ACTIVE); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000243 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500244 }
245 }
246
247 }()
248 return nil
249}
250
251// Disable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000252func (oltA *OLTAdapter) Disable_port(ctx context.Context, deviceId string, Port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500253 go func() {
254
255 if Port.Type == voltha.Port_PON_OLT {
256 if err := oltA.coreProxy.PortStateUpdate(context.TODO(), deviceId, voltha.Port_PON_OLT, Port.PortNo, voltha.OperStatus_DISCOVERED); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400257 // Corresponding device may have been deleted
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500259 }
260 }
261 }()
262 return nil
263}
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500264
265// Child_device_lost deletes ONU and its references
Girish Gowdra6f9b10e2021-03-11 14:36:39 -0800266func (oltA *OLTAdapter) Child_device_lost(ctx context.Context, childDevice *voltha.Device) error { // nolint
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500267 return nil
268}
khenaidoo67b22152020-03-02 16:01:25 -0500269
Girish Gowdra408cd962020-03-11 14:31:31 -0700270// Reboot_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000271func (oltA *OLTAdapter) Reboot_device(ctx context.Context, device *voltha.Device) error { // nolint
divyadesaicb8b59d2020-08-18 09:55:47 +0000272 logger.Infow(ctx, "reboot-device", log.Fields{"device-id": device.Id})
Girish Gowdra408cd962020-03-11 14:31:31 -0700273
274 go func() {
275 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), device.Id, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000276 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700277 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400278 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), device.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400279 // Not an error as the previous command will start the process of clearing the OLT
Rohan Agrawal31f21802020-06-12 05:38:46 +0000280 logger.Infow(ctx, "port-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700281 }
282 }()
283 return nil
284}
285
Scott Baker432f9be2020-03-26 11:56:30 -0700286// TODO: REMOVE Start_omci_test begins an omci self-test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000287func (oltA *OLTAdapter) Start_omci_test(ctx context.Context, device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
Scott Baker432f9be2020-03-26 11:56:30 -0700288 _ = device
289 return nil, errors.New("start-omci-test-not-implemented")
290}
291
Rohan Agrawal31f21802020-06-12 05:38:46 +0000292func (oltA *OLTAdapter) Get_ext_value(ctx context.Context, deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800293 _ = deviceId
294 _ = device
295 _ = valueflag
296 return nil, errors.New("get-ext-value-not-implemented")
297}
ssiddiquif076cb82021-04-23 10:47:04 +0530298
299func (oltA *OLTAdapter) Download_onu_image(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) { //nolint
300 _ = request
301 return nil, errors.New("download-onu-image-not-implemented")
302}
303
304func (oltA *OLTAdapter) Get_onu_image_status(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
305 _ = in
306 return nil, errors.New("get-onu-image-not-implemented")
307}
308
309func (oltA *OLTAdapter) Abort_onu_image_upgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
310 _ = in
311 return nil, errors.New("abort-onu-image-upgrade-not-implemented")
312}
313
314func (oltA *OLTAdapter) Get_onu_images(ctx context.Context, deviceID string) (*voltha.OnuImages, error) { //nolint
315 _ = deviceID
316 return nil, errors.New("get-onu-images-not-implemented")
317}
318
319func (oltA *OLTAdapter) Activate_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
320 _ = in
321 return nil, errors.New("activate-onu-image-not-implemented")
322}
323
324func (oltA *OLTAdapter) Commit_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
325 _ = in
326 return nil, errors.New("commit-onu-image-not-implemented")
327}