blob: c567e4010bddb6d78b7c6bd3d98c797be3a13396 [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"
23 "github.com/gogo/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080024 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
25 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
26 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
28 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo8b4abbf2020-04-24 17:04:30 -040030 "strings"
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
32
33const (
khenaidoo67b22152020-03-02 16:01:25 -050034 numONUPerOLT = 4
35 startingUNIPortNo = 100
khenaidooab1f7bd2019-11-14 14:00:27 -050036)
37
npujar1d86a522019-11-14 17:11:16 +053038// OLTAdapter represent OLT adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050039type OLTAdapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040040 *Adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050041}
42
npujar1d86a522019-11-14 17:11:16 +053043// NewOLTAdapter - creates OLT adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050044func NewOLTAdapter(cp adapterif.CoreProxy) *OLTAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050045 return &OLTAdapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040046 Adapter: NewAdapter(cp),
khenaidoo67b22152020-03-02 16:01:25 -050047 }
khenaidooab1f7bd2019-11-14 14:00:27 -050048}
49
npujar1d86a522019-11-14 17:11:16 +053050// Adopt_device creates new handler for added device
51func (oltA *OLTAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050052 go func() {
53 d := proto.Clone(device).(*voltha.Device)
54 d.Root = true
55 d.Vendor = "olt_adapter_mock"
56 d.Model = "go-mock"
57 d.SerialNumber = com.GetRandomSerialNumber()
58 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
59 oltA.storeDevice(d)
60 if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000061 logger.Fatalf("deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050062 }
khenaidooc6c7bda2020-06-17 17:20:18 -040063 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
khenaidooab1f7bd2019-11-14 14:00:27 -050064 nniPort := &voltha.Port{
65 PortNo: 2,
66 Label: fmt.Sprintf("nni-%d", 2),
67 Type: voltha.Port_ETHERNET_NNI,
68 OperStatus: voltha.OperStatus_ACTIVE,
khenaidooc6c7bda2020-06-17 17:20:18 -040069 OfpPort: &of.OfpPort{
70 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
71 Config: 0,
72 State: uint32(of.OfpPortState_OFPPS_LIVE),
73 Curr: capability,
74 Advertised: capability,
75 Peer: capability,
76 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
77 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
78 },
khenaidooab1f7bd2019-11-14 14:00:27 -050079 }
80 var err error
81 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000082 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050083 }
84
85 ponPort := &voltha.Port{
86 PortNo: 1,
87 Label: fmt.Sprintf("pon-%d", 1),
88 Type: voltha.Port_PON_OLT,
89 OperStatus: voltha.OperStatus_ACTIVE,
90 }
91 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000092 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050093 }
94
95 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
96 d.OperStatus = voltha.OperStatus_ACTIVE
97
98 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000099 logger.Fatalf("Device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500100 }
101
102 //Get the latest device data from the Core
103 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000104 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500105 }
106
khenaidoo8b4abbf2020-04-24 17:04:30 -0400107 oltA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -0500108
109 // Register Child devices
khenaidoo67b22152020-03-02 16:01:25 -0500110 initialUniPortNo := startingUNIPortNo
khenaidooab1f7bd2019-11-14 14:00:27 -0500111 for i := 0; i < numONUPerOLT; i++ {
112 go func(seqNo int) {
113 if _, err := oltA.coreProxy.ChildDeviceDetected(
114 context.TODO(),
115 d.Id,
116 1,
117 "onu_adapter_mock",
118 initialUniPortNo+seqNo,
119 "onu_adapter_mock",
120 com.GetRandomSerialNumber(),
121 int64(seqNo)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000122 logger.Fatalf("failure-sending-child-device-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500123 }
124 }(i)
125 }
126 }()
127 return nil
128}
129
npujar1d86a522019-11-14 17:11:16 +0530130// Get_ofp_device_info returns ofp device info
131func (oltA *OLTAdapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500132 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000133 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500134 }
135 return &ic.SwitchCapability{
136 Desc: &of.OfpDesc{
137 HwDesc: "olt_adapter_mock",
138 SwDesc: "olt_adapter_mock",
139 SerialNum: "12345678",
140 },
141 SwitchFeatures: &of.OfpSwitchFeatures{
142 NBuffers: 256,
143 NTables: 2,
144 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
145 of.OfpCapabilities_OFPC_TABLE_STATS |
146 of.OfpCapabilities_OFPC_PORT_STATS |
147 of.OfpCapabilities_OFPC_GROUP_STATS),
148 },
149 }, nil
150}
151
npujar1d86a522019-11-14 17:11:16 +0530152// GetNumONUPerOLT returns number of ONUs per OLT
khenaidooab1f7bd2019-11-14 14:00:27 -0500153func (oltA *OLTAdapter) GetNumONUPerOLT() int {
154 return numONUPerOLT
155}
156
khenaidoo67b22152020-03-02 16:01:25 -0500157// Returns the starting UNI port number
158func (oltA *OLTAdapter) GetStartingUNIPortNo() int {
159 return startingUNIPortNo
160}
161
npujar1d86a522019-11-14 17:11:16 +0530162// Disable_device disables device
163func (oltA *OLTAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500164 go func() {
165 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000166 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500167 }
168
169 cloned := proto.Clone(device).(*voltha.Device)
170 // Update the all ports state on that device to disable
171 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000172 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500173 }
174
Girish Gowdra408cd962020-03-11 14:31:31 -0700175 //Update the device operational state
khenaidooab1f7bd2019-11-14 14:00:27 -0500176 cloned.OperStatus = voltha.OperStatus_UNKNOWN
Girish Gowdra408cd962020-03-11 14:31:31 -0700177 // The device is still reachable after it has been disabled, so the connection status should not be changed.
khenaidooab1f7bd2019-11-14 14:00:27 -0500178
179 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400180 // Device may already have been deleted in the core
Girish Kumarf56a4682020-03-20 20:07:46 +0000181 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400182 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500183 }
184
khenaidoo8b4abbf2020-04-24 17:04:30 -0400185 oltA.updateDevice(cloned)
khenaidooab1f7bd2019-11-14 14:00:27 -0500186
187 // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core
188 if err := oltA.coreProxy.ChildDevicesLost(context.TODO(), cloned.Id); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400189 // Device may already have been deleted in the core
Girish Kumarf56a4682020-03-20 20:07:46 +0000190 logger.Warnw("lost-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500191 }
192 }()
193 return nil
194}
195
npujar1d86a522019-11-14 17:11:16 +0530196// Reenable_device reenables device
197func (oltA *OLTAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500198 go func() {
199 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000200 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500201 }
202
203 cloned := proto.Clone(device).(*voltha.Device)
204 // Update the all ports state on that device to enable
205 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000206 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500207 }
208
209 //Update the device state
khenaidooab1f7bd2019-11-14 14:00:27 -0500210 cloned.OperStatus = voltha.OperStatus_ACTIVE
211
212 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000213 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500214 }
215
216 // Tell the Core that all child devices have been enabled
217 if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000218 logger.Fatalf("detection-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 }
220 }()
221 return nil
222}
kesavandbc2d1622020-01-21 00:42:01 -0500223
224// Enable_port -
225func (oltA *OLTAdapter) Enable_port(deviceId string, Port *voltha.Port) error { //nolint
226 go func() {
227
228 if Port.Type == voltha.Port_PON_OLT {
229 if err := oltA.coreProxy.PortStateUpdate(context.TODO(), deviceId, voltha.Port_PON_OLT, Port.PortNo, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000230 logger.Fatalf("updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500231 }
232 }
233
234 }()
235 return nil
236}
237
238// Disable_port -
239func (oltA *OLTAdapter) Disable_port(deviceId string, Port *voltha.Port) error { //nolint
240 go func() {
241
242 if Port.Type == voltha.Port_PON_OLT {
243 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 -0400244 // Corresponding device may have been deleted
Girish Kumarf56a4682020-03-20 20:07:46 +0000245 logger.Warnw("updating-ports-failed", log.Fields{"device-id": deviceId, "error": err})
kesavandbc2d1622020-01-21 00:42:01 -0500246 }
247 }
248 }()
249 return nil
250}
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500251
252// Child_device_lost deletes ONU and its references
253func (oltA *OLTAdapter) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error { // nolint
254 return nil
255}
khenaidoo67b22152020-03-02 16:01:25 -0500256
Girish Gowdra408cd962020-03-11 14:31:31 -0700257// Reboot_device -
258func (oltA *OLTAdapter) Reboot_device(device *voltha.Device) error { // nolint
Girish Kumarf56a4682020-03-20 20:07:46 +0000259 logger.Infow("reboot-device", log.Fields{"deviceId": device.Id})
Girish Gowdra408cd962020-03-11 14:31:31 -0700260
261 go func() {
262 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), device.Id, voltha.ConnectStatus_UNREACHABLE, voltha.OperStatus_UNKNOWN); err != nil {
Kent Hagerman45a13e42020-04-13 12:23:50 -0400263 logger.Fatalf("device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700264 }
265 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), device.Id, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400266 // Not an error as the previous command will start the process of clearing the OLT
267 logger.Infow("port-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700268 }
269 }()
270 return nil
271}
272
Scott Baker432f9be2020-03-26 11:56:30 -0700273// TODO: REMOVE Start_omci_test begins an omci self-test
274func (oltA *OLTAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
275 _ = device
276 return nil, errors.New("start-omci-test-not-implemented")
277}
278
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800279func (oltA *OLTAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
280 _ = deviceId
281 _ = device
282 _ = valueflag
283 return nil, errors.New("get-ext-value-not-implemented")
284}