blob: 89f6c2098a9b94d1b9e4ac9c52e5237d1a299f4e [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 */
16package mocks
17
18import (
19 "context"
20 "fmt"
21 "github.com/gogo/protobuf/proto"
22 "github.com/opencord/voltha-lib-go/v2/pkg/adapters/adapterif"
23 com "github.com/opencord/voltha-lib-go/v2/pkg/adapters/common"
24 "github.com/opencord/voltha-lib-go/v2/pkg/log"
25 ic "github.com/opencord/voltha-protos/v2/go/inter_container"
26 of "github.com/opencord/voltha-protos/v2/go/openflow_13"
27 "github.com/opencord/voltha-protos/v2/go/voltha"
28 "strings"
29)
30
31const (
32 numONUPerOLT = 4
33)
34
35type OLTAdapter struct {
36 Adapter
37}
38
39func NewOLTAdapter(cp adapterif.CoreProxy) *OLTAdapter {
40 a := &OLTAdapter{}
41 a.coreProxy = cp
42 return a
43}
44
45func (oltA *OLTAdapter) Adopt_device(device *voltha.Device) error {
46 go func() {
47 d := proto.Clone(device).(*voltha.Device)
48 d.Root = true
49 d.Vendor = "olt_adapter_mock"
50 d.Model = "go-mock"
51 d.SerialNumber = com.GetRandomSerialNumber()
52 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
53 oltA.storeDevice(d)
54 if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
55 log.Fatalf("deviceUpdate-failed-%s", res)
56 }
57 nniPort := &voltha.Port{
58 PortNo: 2,
59 Label: fmt.Sprintf("nni-%d", 2),
60 Type: voltha.Port_ETHERNET_NNI,
61 OperStatus: voltha.OperStatus_ACTIVE,
62 }
63 var err error
64 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil {
65 log.Fatalf("PortCreated-failed-%s", err)
66 }
67
68 ponPort := &voltha.Port{
69 PortNo: 1,
70 Label: fmt.Sprintf("pon-%d", 1),
71 Type: voltha.Port_PON_OLT,
72 OperStatus: voltha.OperStatus_ACTIVE,
73 }
74 if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
75 log.Fatalf("PortCreated-failed-%s", err)
76 }
77
78 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
79 d.OperStatus = voltha.OperStatus_ACTIVE
80
81 if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
82 log.Fatalf("Device-state-update-failed-%s", err)
83 }
84
85 //Get the latest device data from the Core
86 if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
87 log.Fatalf("getting-device-failed-%s", err)
88 }
89
90 if err = oltA.updateDevice(d); err != nil {
91 log.Fatalf("saving-device-failed-%s", err)
92 }
93
94 // Register Child devices
95 initialUniPortNo := 100
96 for i := 0; i < numONUPerOLT; i++ {
97 go func(seqNo int) {
98 if _, err := oltA.coreProxy.ChildDeviceDetected(
99 context.TODO(),
100 d.Id,
101 1,
102 "onu_adapter_mock",
103 initialUniPortNo+seqNo,
104 "onu_adapter_mock",
105 com.GetRandomSerialNumber(),
106 int64(seqNo)); err != nil {
107 log.Fatalf("failure-sending-child-device-%s", err)
108 }
109 }(i)
110 }
111 }()
112 return nil
113}
114
115func (oltA *OLTAdapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) {
116 if d := oltA.getDevice(device.Id); d == nil {
117 log.Fatalf("device-not-found-%s", device.Id)
118 }
119 return &ic.SwitchCapability{
120 Desc: &of.OfpDesc{
121 HwDesc: "olt_adapter_mock",
122 SwDesc: "olt_adapter_mock",
123 SerialNum: "12345678",
124 },
125 SwitchFeatures: &of.OfpSwitchFeatures{
126 NBuffers: 256,
127 NTables: 2,
128 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
129 of.OfpCapabilities_OFPC_TABLE_STATS |
130 of.OfpCapabilities_OFPC_PORT_STATS |
131 of.OfpCapabilities_OFPC_GROUP_STATS),
132 },
133 }, nil
134}
135
136func (oltA *OLTAdapter) Get_ofp_port_info(device *voltha.Device, port_no int64) (*ic.PortCapability, error) {
137 if d := oltA.getDevice(device.Id); d == nil {
138 log.Fatalf("device-not-found-%s", device.Id)
139 }
140 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
141 return &ic.PortCapability{
142 Port: &voltha.LogicalPort{
143 OfpPort: &of.OfpPort{
144 HwAddr: macAddressToUint32Array("11:22:33:44:55:66"),
145 Config: 0,
146 State: uint32(of.OfpPortState_OFPPS_LIVE),
147 Curr: capability,
148 Advertised: capability,
149 Peer: capability,
150 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
151 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
152 },
153 DeviceId: device.Id,
154 DevicePortNo: uint32(port_no),
155 },
156 }, nil
157}
158
159func (oltA *OLTAdapter) GetNumONUPerOLT() int {
160 return numONUPerOLT
161}
162
163func (oltA *OLTAdapter) Disable_device(device *voltha.Device) error {
164 go func() {
165 if d := oltA.getDevice(device.Id); d == nil {
166 log.Fatalf("device-not-found-%s", device.Id)
167 }
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 {
172 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
173 }
174
175 //Update the device state
176 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
177 cloned.OperStatus = voltha.OperStatus_UNKNOWN
178
179 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
180 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
181 }
182
183 if err := oltA.updateDevice(cloned); err != nil {
184 log.Fatalf("saving-device-failed-%s", err)
185 }
186
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 {
189 log.Fatalf("lost-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
190 }
191 }()
192 return nil
193}
194
195func (oltA *OLTAdapter) Reenable_device(device *voltha.Device) error {
196 go func() {
197 if d := oltA.getDevice(device.Id); d == nil {
198 log.Fatalf("device-not-found-%s", device.Id)
199 }
200
201 cloned := proto.Clone(device).(*voltha.Device)
202 // Update the all ports state on that device to enable
203 if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
204 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
205 }
206
207 //Update the device state
208 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
209 cloned.OperStatus = voltha.OperStatus_ACTIVE
210
211 if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
212 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
213 }
214
215 // Tell the Core that all child devices have been enabled
216 if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil {
217 log.Fatalf("detection-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err})
218 }
219 }()
220 return nil
221}