blob: 61f431ad1e2f7bc8fcc416c311a713f6b46388b4 [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 }
63 nniPort := &voltha.Port{
64 PortNo: 2,
65 Label: fmt.Sprintf("nni-%d", 2),
66 Type: voltha.Port_ETHERNET_NNI,
67 OperStatus: voltha.OperStatus_ACTIVE,
68 }
69 var err error
70 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000071 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050072 }
73
74 ponPort := &voltha.Port{
75 PortNo: 1,
76 Label: fmt.Sprintf("pon-%d", 1),
77 Type: voltha.Port_PON_OLT,
78 OperStatus: voltha.OperStatus_ACTIVE,
79 }
80 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000081 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050082 }
83
84 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
85 d.OperStatus = voltha.OperStatus_ACTIVE
86
87 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000088 logger.Fatalf("Device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050089 }
90
91 //Get the latest device data from the Core
92 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000093 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050094 }
95
khenaidoo8b4abbf2020-04-24 17:04:30 -040096 oltA.updateDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -050097
98 // Register Child devices
khenaidoo67b22152020-03-02 16:01:25 -050099 initialUniPortNo := startingUNIPortNo
khenaidooab1f7bd2019-11-14 14:00:27 -0500100 for i := 0; i < numONUPerOLT; i++ {
101 go func(seqNo int) {
102 if _, err := oltA.coreProxy.ChildDeviceDetected(
103 context.TODO(),
104 d.Id,
105 1,
106 "onu_adapter_mock",
107 initialUniPortNo+seqNo,
108 "onu_adapter_mock",
109 com.GetRandomSerialNumber(),
110 int64(seqNo)); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000111 logger.Fatalf("failure-sending-child-device-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500112 }
113 }(i)
114 }
115 }()
116 return nil
117}
118
npujar1d86a522019-11-14 17:11:16 +0530119// Get_ofp_device_info returns ofp device info
120func (oltA *OLTAdapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000122 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500123 }
124 return &ic.SwitchCapability{
125 Desc: &of.OfpDesc{
126 HwDesc: "olt_adapter_mock",
127 SwDesc: "olt_adapter_mock",
128 SerialNum: "12345678",
129 },
130 SwitchFeatures: &of.OfpSwitchFeatures{
131 NBuffers: 256,
132 NTables: 2,
133 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
134 of.OfpCapabilities_OFPC_TABLE_STATS |
135 of.OfpCapabilities_OFPC_PORT_STATS |
136 of.OfpCapabilities_OFPC_GROUP_STATS),
137 },
138 }, nil
139}
140
npujar1d86a522019-11-14 17:11:16 +0530141// Get_ofp_port_info returns ofp port info
142func (oltA *OLTAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500143 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000144 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 }
146 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
147 return &ic.PortCapability{
148 Port: &voltha.LogicalPort{
149 OfpPort: &of.OfpPort{
150 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
151 Config: 0,
152 State: uint32(of.OfpPortState_OFPPS_LIVE),
153 Curr: capability,
154 Advertised: capability,
155 Peer: capability,
156 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
157 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
158 },
159 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530160 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500161 },
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
176func (oltA *OLTAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500177 go func() {
178 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000179 logger.Fatalf("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
184 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000185 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": 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
Girish Kumarf56a4682020-03-20 20:07:46 +0000194 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": 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
Girish Kumarf56a4682020-03-20 20:07:46 +0000203 logger.Warnw("lost-notif-of-child-devices-failed", log.Fields{"deviceId": 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
210func (oltA *OLTAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500211 go func() {
212 if d := oltA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000213 logger.Fatalf("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
218 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000219 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": 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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000226 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": 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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000231 logger.Fatalf("detection-notif-of-child-devices-failed", log.Fields{"deviceId": 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 -
238func (oltA *OLTAdapter) Enable_port(deviceId string, Port *voltha.Port) error { //nolint
239 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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000243 logger.Fatalf("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 -
252func (oltA *OLTAdapter) Disable_port(deviceId string, Port *voltha.Port) error { //nolint
253 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
Girish Kumarf56a4682020-03-20 20:07:46 +0000258 logger.Warnw("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
266func (oltA *OLTAdapter) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error { // nolint
267 return nil
268}
khenaidoo67b22152020-03-02 16:01:25 -0500269
Girish Gowdra408cd962020-03-11 14:31:31 -0700270// Reboot_device -
271func (oltA *OLTAdapter) Reboot_device(device *voltha.Device) error { // nolint
Girish Kumarf56a4682020-03-20 20:07:46 +0000272 logger.Infow("reboot-device", log.Fields{"deviceId": 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 {
Kent Hagerman45a13e42020-04-13 12:23:50 -0400276 logger.Fatalf("device-state-update-failed", log.Fields{"device-id": device.Id, "error": err})
Girish Gowdra408cd962020-03-11 14:31:31 -0700277 }
278 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), device.Id, 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
280 logger.Infow("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
287func (oltA *OLTAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
288 _ = device
289 return nil, errors.New("start-omci-test-not-implemented")
290}
291
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800292func (oltA *OLTAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
293 _ = deviceId
294 _ = device
295 _ = valueflag
296 return nil, errors.New("get-ext-value-not-implemented")
297}