blob: 313ecc6f05449d8e5f9e5385b351bd074d42c7ea [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"
Kent Hagerman2a07b862020-06-19 15:23:07 -040023 "strings"
24
khenaidooab1f7bd2019-11-14 14:00:27 -050025 "github.com/gogo/protobuf/proto"
Kent Hagerman2a07b862020-06-19 15:23:07 -040026 "github.com/opencord/voltha-lib-go/v3/pkg/adapters"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
28 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
31 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
32 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050033)
34
35const (
khenaidoo67b22152020-03-02 16:01:25 -050036 numONUPerOLT = 4
37 startingUNIPortNo = 100
khenaidooab1f7bd2019-11-14 14:00:27 -050038)
39
Kent Hagerman2a07b862020-06-19 15:23:07 -040040// static implementation check
41var _ adapters.IAdapter = &OLTAdapter{}
42
npujar1d86a522019-11-14 17:11:16 +053043// OLTAdapter represent OLT adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050044type OLTAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040045 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050046}
47
npujar1d86a522019-11-14 17:11:16 +053048// NewOLTAdapter - creates OLT adapter instance
Rohan Agrawal31f21802020-06-12 05:38:46 +000049func NewOLTAdapter(ctx context.Context, cp adapterif.CoreProxy) *OLTAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050050 return &OLTAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040051 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050052 }
khenaidooab1f7bd2019-11-14 14:00:27 -050053}
54
npujar1d86a522019-11-14 17:11:16 +053055// Adopt_device creates new handler for added device
Rohan Agrawal31f21802020-06-12 05:38:46 +000056func (oltA *OLTAdapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050057 go func() {
58 d := proto.Clone(device).(*voltha.Device)
59 d.Root = true
60 d.Vendor = "olt_adapter_mock"
61 d.Model = "go-mock"
62 d.SerialNumber = com.GetRandomSerialNumber()
63 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
64 oltA.storeDevice(d)
65 if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000066 logger.Fatalf(ctx, "deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050067 }
khenaidooc6c7bda2020-06-17 17:20:18 -040068 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050069 nniPort := &voltha.Port{
70 PortNo: 2,
71 Label: fmt.Sprintf("nni-%d", 2),
72 Type: voltha.Port_ETHERNET_NNI,
73 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040074 OfpPort: &of.OfpPort{
75 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
76 Config: 0,
77 State: uint32(of.OfpPortState_OFPPS_LIVE),
78 Curr: capability,
79 Advertised: capability,
80 Peer: capability,
81 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
82 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
83 },
khenaidooab1f7bd2019-11-14 14:00:27 -050084 }
85 var err error
86 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000087 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050088 }
89
90 ponPort := &voltha.Port{
91 PortNo: 1,
92 Label: fmt.Sprintf("pon-%d", 1),
93 Type: voltha.Port_PON_OLT,
94 OperStatus: voltha.OperStatus_ACTIVE,
95 }
96 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +000097 logger.Fatalf(ctx, "PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050098 }
99
100 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
101 d.OperStatus = voltha.OperStatus_ACTIVE
102
103 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000104 logger.Fatalf(ctx, "Device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500105 }
106
107 //Get the latest device data from the Core
108 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000109 logger.Fatalf(ctx, "getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500110 }
111
khenaidoo8b4abbf2020-04-24 17:04:30 -0400112 oltA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500113
114 // Register Child devices
khenaidoo67b22152020-03-02 16:01:25 -0500115 initialUniPortNo := startingUNIPortNo
khenaidooab1f7bd2019-11-14 14:00:27 -0500116 for i := 0; i < numONUPerOLT; i++ {
117 go func(seqNo int) {
118 if _, err := oltA.coreProxy.ChildDeviceDetected(
119 context.TODO(),
120 d.Id,
121 1,
122 "onu_adapter_mock",
123 initialUniPortNo+seqNo,
124 "onu_adapter_mock",
125 com.GetRandomSerialNumber(),
126 int64(seqNo)); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000127 logger.Fatalf(ctx, "failure-sending-child-device-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500128 }
129 }(i)
130 }
131 }()
132 return nil
133}
134
npujar1d86a522019-11-14 17:11:16 +0530135// Get_ofp_device_info returns ofp device info
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136func (oltA *OLTAdapter) Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500137 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000138 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500139 }
140 return &ic.SwitchCapability{
141 Desc: &of.OfpDesc{
142 HwDesc: "olt_adapter_mock",
143 SwDesc: "olt_adapter_mock",
144 SerialNum: "12345678",
145 },
146 SwitchFeatures: &of.OfpSwitchFeatures{
147 NBuffers: 256,
148 NTables: 2,
149 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
150 of.OfpCapabilities_OFPC_TABLE_STATS |
151 of.OfpCapabilities_OFPC_PORT_STATS |
152 of.OfpCapabilities_OFPC_GROUP_STATS),
153 },
154 }, nil
155}
156
npujar1d86a522019-11-14 17:11:16 +0530157// GetNumONUPerOLT returns number of ONUs per OLT
khenaidooab1f7bd2019-11-14 14:00:27 -0500158func (oltA *OLTAdapter) GetNumONUPerOLT() int {
159 return numONUPerOLT
160}
161
khenaidoo67b22152020-03-02 16:01:25 -0500162// Returns the starting UNI port number
163func (oltA *OLTAdapter) GetStartingUNIPortNo() int {
164 return startingUNIPortNo
165}
166
npujar1d86a522019-11-14 17:11:16 +0530167// Disable_device disables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000168func (oltA *OLTAdapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500169 go func() {
170 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000171 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500172 }
173
174 cloned := proto.Clone(device).(*voltha.Device)
175 // Update the all ports state on that device to disable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400176 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000177 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500178 }
179
Girish Gowdra408cd962020-03-11 14:31:31 -0700180 //Update the device operational state
khenaidooab1f7bd2019-11-14 14:00:27 -0500181 cloned.OperStatus = voltha.OperStatus_UNKNOWN
Girish Gowdra408cd962020-03-11 14:31:31 -0700182 // The device is still reachable after it has been disabled, so the connection status should not be changed.
khenaidooab1f7bd2019-11-14 14:00:27 -0500183
184 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400185 // Device may already have been deleted in the core
Rohan Agrawal31f21802020-06-12 05:38:46 +0000186 logger.Warnw(ctx, "device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400187 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500188 }
189
khenaidoo8b4abbf2020-04-24 17:04:30 -0400190 oltA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500191
192 // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core
193 if err := oltA.coreProxy.ChildDevicesLost(context.TODO(), cloned.Id); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400194 // Device may already have been deleted in the core
Rohan Agrawal31f21802020-06-12 05:38:46 +0000195 logger.Warnw(ctx, "lost-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500196 }
197 }()
198 return nil
199}
200
npujar1d86a522019-11-14 17:11:16 +0530201// Reenable_device reenables device
Rohan Agrawal31f21802020-06-12 05:38:46 +0000202func (oltA *OLTAdapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500203 go func() {
204 if d := oltA.getDevice(device.Id); d == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000205 logger.Fatalf(ctx, "device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500206 }
207
208 cloned := proto.Clone(device).(*voltha.Device)
209 // Update the all ports state on that device to enable
Kent Hagerman2a07b862020-06-19 15:23:07 -0400210 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_ACTIVE); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000211 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500212 }
213
214 //Update the device state
khenaidooab1f7bd2019-11-14 14:00:27 -0500215 cloned.OperStatus = voltha.OperStatus_ACTIVE
216
217 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000218 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 }
220
221 // Tell the Core that all child devices have been enabled
222 if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000223 logger.Fatalf(ctx, "detection-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500224 }
225 }()
226 return nil
227}
kesavandbc2d1622020-01-21 00:42:01 -0500228
229// Enable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000230func (oltA *OLTAdapter) Enable_port(ctx context.Context, deviceId string, Port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500231 go func() {
232
233 if Port.Type == voltha.Port_PON_OLT {
234 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 +0000235 logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500236 }
237 }
238
239 }()
240 return nil
241}
242
243// Disable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000244func (oltA *OLTAdapter) Disable_port(ctx context.Context, deviceId string, Port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500245 go func() {
246
247 if Port.Type == voltha.Port_PON_OLT {
248 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 -0400249 // Corresponding device may have been deleted
Rohan Agrawal31f21802020-06-12 05:38:46 +0000250 logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500251 }
252 }
253 }()
254 return nil
255}
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500256
257// Child_device_lost deletes ONU and its references
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258func (oltA *OLTAdapter) Child_device_lost(ctx context.Context, deviceID string, pPortNo uint32, onuID uint32) error { // nolint
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500259 return nil
260}
khenaidoo67b22152020-03-02 16:01:25 -0500261
Girish Gowdra408cd962020-03-11 14:31:31 -0700262// Reboot_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000263func (oltA *OLTAdapter) Reboot_device(ctx context.Context, device *voltha.Device) error { // nolint
264 logger.Infow(ctx, "reboot-device", log.Fields{"deviceId": device.Id})
Girish Gowdra408cd962020-03-11 14:31:31 -0700265
266 go func() {
267 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), device.Id, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000268 logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700269 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400270 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), device.Id, 0, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400271 // Not an error as the previous command will start the process of clearing the OLT
Rohan Agrawal31f21802020-06-12 05:38:46 +0000272 logger.Infow(ctx, "port-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700273 }
274 }()
275 return nil
276}
277
Scott Baker432f9be2020-03-26 11:56:30 -0700278// TODO: REMOVE Start_omci_test begins an omci self-test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000279func (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 -0700280 _ = device
281 return nil, errors.New("start-omci-test-not-implemented")
282}
283
Rohan Agrawal31f21802020-06-12 05:38:46 +0000284func (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 -0800285 _ = deviceId
286 _ = device
287 _ = valueflag
288 return nil, errors.New("get-ext-value-not-implemented")
289}