blob: 7cf0d470678acf320068d37320583914f717e3e8 [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"
21 "fmt"
npujar1d86a522019-11-14 17:11:16 +053022 "strings"
23
khenaidooab1f7bd2019-11-14 14:00:27 -050024 "github.com/gogo/protobuf/proto"
25 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
26 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
27 "github.com/opencord/voltha-lib-go/v2/pkg/log"
28 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
29 of "github.com/opencord/voltha-protos/v2/go/openflow_13"
30 "github.com/opencord/voltha-protos/v2/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
32
33const (
34 numONUPerOLT = 4
35)
36
npujar1d86a522019-11-14 17:11:16 +053037// OLTAdapter represent OLT adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050038type OLTAdapter struct {
39 Adapter
40}
41
npujar1d86a522019-11-14 17:11:16 +053042// NewOLTAdapter - creates OLT adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050043func NewOLTAdapter(cp adapterif.CoreProxy) *OLTAdapter {
44 a := &OLTAdapter{}
45 a.coreProxy = cp
46 return a
47}
48
npujar1d86a522019-11-14 17:11:16 +053049// Adopt_device creates new handler for added device
50func (oltA *OLTAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050051 go func() {
52 d := proto.Clone(device).(*voltha.Device)
53 d.Root = true
54 d.Vendor = "olt_adapter_mock"
55 d.Model = "go-mock"
56 d.SerialNumber = com.GetRandomSerialNumber()
57 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
58 oltA.storeDevice(d)
59 if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
60 log.Fatalf("deviceUpdate-failed-%s", res)
61 }
62 nniPort := &voltha.Port{
63 PortNo: 2,
64 Label: fmt.Sprintf("nni-%d", 2),
65 Type: voltha.Port_ETHERNET_NNI,
66 OperStatus: voltha.OperStatus_ACTIVE,
67 }
68 var err error
69 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
70 log.Fatalf("PortCreated-failed-%s", err)
71 }
72
73 ponPort := &voltha.Port{
74 PortNo: 1,
75 Label: fmt.Sprintf("pon-%d", 1),
76 Type: voltha.Port_PON_OLT,
77 OperStatus: voltha.OperStatus_ACTIVE,
78 }
79 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
80 log.Fatalf("PortCreated-failed-%s", err)
81 }
82
83 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
84 d.OperStatus = voltha.OperStatus_ACTIVE
85
86 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
87 log.Fatalf("Device-state-update-failed-%s", err)
88 }
89
90 //Get the latest device data from the Core
91 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
92 log.Fatalf("getting-device-failed-%s", err)
93 }
94
95 if err = oltA.updateDevice(d); err != nil {
96 log.Fatalf("saving-device-failed-%s", err)
97 }
98
99 // Register Child devices
100 initialUniPortNo := 100
101 for i := 0; i < numONUPerOLT; i++ {
102 go func(seqNo int) {
103 if _, err := oltA.coreProxy.ChildDeviceDetected(
104 context.TODO(),
105 d.Id,
106 1,
107 "onu_adapter_mock",
108 initialUniPortNo+seqNo,
109 "onu_adapter_mock",
110 com.GetRandomSerialNumber(),
111 int64(seqNo)); err != nil {
112 log.Fatalf("failure-sending-child-device-%s", err)
113 }
114 }(i)
115 }
116 }()
117 return nil
118}
119
npujar1d86a522019-11-14 17:11:16 +0530120// Get_ofp_device_info returns ofp device info
121func (oltA *OLTAdapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500122 if d := oltA.getDevice(device.Id); d == nil {
123 log.Fatalf("device-not-found-%s", device.Id)
124 }
125 return &ic.SwitchCapability{
126 Desc: &of.OfpDesc{
127 HwDesc: "olt_adapter_mock",
128 SwDesc: "olt_adapter_mock",
129 SerialNum: "12345678",
130 },
131 SwitchFeatures: &of.OfpSwitchFeatures{
132 NBuffers: 256,
133 NTables: 2,
134 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
135 of.OfpCapabilities_OFPC_TABLE_STATS |
136 of.OfpCapabilities_OFPC_PORT_STATS |
137 of.OfpCapabilities_OFPC_GROUP_STATS),
138 },
139 }, nil
140}
141
npujar1d86a522019-11-14 17:11:16 +0530142// Get_ofp_port_info returns ofp port info
143func (oltA *OLTAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500144 if d := oltA.getDevice(device.Id); d == nil {
145 log.Fatalf("device-not-found-%s", device.Id)
146 }
147 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
148 return &ic.PortCapability{
149 Port: &voltha.LogicalPort{
150 OfpPort: &of.OfpPort{
151 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
152 Config: 0,
153 State: uint32(of.OfpPortState_OFPPS_LIVE),
154 Curr: capability,
155 Advertised: capability,
156 Peer: capability,
157 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
158 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
159 },
160 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530161 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500162 },
163 }, nil
164}
165
npujar1d86a522019-11-14 17:11:16 +0530166// GetNumONUPerOLT returns number of ONUs per OLT
khenaidooab1f7bd2019-11-14 14:00:27 -0500167func (oltA *OLTAdapter) GetNumONUPerOLT() int {
168 return numONUPerOLT
169}
170
npujar1d86a522019-11-14 17:11:16 +0530171// Disable_device disables device
172func (oltA *OLTAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500173 go func() {
174 if d := oltA.getDevice(device.Id); d == nil {
175 log.Fatalf("device-not-found-%s", device.Id)
176 }
177
178 cloned := proto.Clone(device).(*voltha.Device)
179 // Update the all ports state on that device to disable
180 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
181 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
182 }
183
184 //Update the device state
185 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
186 cloned.OperStatus = voltha.OperStatus_UNKNOWN
187
188 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
189 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
190 }
191
192 if err := oltA.updateDevice(cloned); err != nil {
193 log.Fatalf("saving-device-failed-%s", err)
194 }
195
196 // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core
197 if err := oltA.coreProxy.ChildDevicesLost(context.TODO(), cloned.Id); err != nil {
198 log.Fatalf("lost-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
199 }
200 }()
201 return nil
202}
203
npujar1d86a522019-11-14 17:11:16 +0530204// Reenable_device reenables device
205func (oltA *OLTAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500206 go func() {
207 if d := oltA.getDevice(device.Id); d == nil {
208 log.Fatalf("device-not-found-%s", device.Id)
209 }
210
211 cloned := proto.Clone(device).(*voltha.Device)
212 // Update the all ports state on that device to enable
213 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
214 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
215 }
216
217 //Update the device state
218 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
219 cloned.OperStatus = voltha.OperStatus_ACTIVE
220
221 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
222 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
223 }
224
225 // Tell the Core that all child devices have been enabled
226 if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil {
227 log.Fatalf("detection-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
228 }
229 }()
230 return nil
231}