blob: 73ee74981096ea0fc7e932f6a2b15db50586162e [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"
khenaidoo67b22152020-03-02 16:01:25 -050023 "sync"
npujar1d86a522019-11-14 17:11:16 +053024
khenaidooab1f7bd2019-11-14 14:00:27 -050025 "github.com/gogo/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
27 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
28 "github.com/opencord/voltha-lib-go/v3/pkg/log"
29 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
30 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
31 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050032)
33
npujar1d86a522019-11-14 17:11:16 +053034// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050035type ONUAdapter struct {
khenaidoo67b22152020-03-02 16:01:25 -050036 flows map[uint64]*voltha.OfpFlowStats
37 lock sync.Mutex
khenaidooab1f7bd2019-11-14 14:00:27 -050038 Adapter
39}
40
npujar1d86a522019-11-14 17:11:16 +053041// NewONUAdapter creates ONU adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050042func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050043 return &ONUAdapter{
44 flows: map[uint64]*voltha.OfpFlowStats{},
45 Adapter: Adapter{
46 coreProxy: cp,
47 },
48 }
khenaidooab1f7bd2019-11-14 14:00:27 -050049}
50
npujar1d86a522019-11-14 17:11:16 +053051// Adopt_device creates new handler for added device
52func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050053 go func() {
54 d := proto.Clone(device).(*voltha.Device)
55 d.Root = false
56 d.Vendor = "onu_adapter_mock"
57 d.Model = "go-mock"
58 d.SerialNumber = com.GetRandomSerialNumber()
59 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
60 onuA.storeDevice(d)
61 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
62 log.Fatalf("deviceUpdate-failed-%s", res)
63 }
khenaidooab1f7bd2019-11-14 14:00:27 -050064
khenaidoo93181522020-01-23 12:43:21 -050065 // Updating the device states twice, once with oper status to discovered and followed by active may cause
66 // a failure for unit tests when these requests reaches the Core within a millisecond of each other (with real
67 // hardware will not happen as the time between these requests is much higher than 1 millisecond). For
68 // some reasons this issue is seen on Jenkins but not when running the tests locally. The issue
69 // in the core is triggered when these requests are processed out of order (an issue in the Core that is
70 // being handled by https://jira.opencord.org/browse/VOL-2164).
71 // TODO: Once the above change is completed then this code can be uncommented.
72
73 //d.ConnectStatus = voltha.ConnectStatus_REACHABLE
74 //d.OperStatus = voltha.OperStatus_DISCOVERED
75
76 //if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
77 // log.Fatalf("device-state-update-failed-%s", err)
78 //}
khenaidooab1f7bd2019-11-14 14:00:27 -050079
80 uniPortNo := uint32(2)
81 if device.ProxyAddress != nil {
82 if device.ProxyAddress.ChannelId != 0 {
83 uniPortNo = device.ProxyAddress.ChannelId
84 }
85 }
86
87 uniPort := &voltha.Port{
88 PortNo: uniPortNo,
89 Label: fmt.Sprintf("uni-%d", uniPortNo),
90 Type: voltha.Port_ETHERNET_UNI,
91 OperStatus: voltha.OperStatus_ACTIVE,
92 }
93 var err error
94 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
95 log.Fatalf("PortCreated-failed-%s", err)
96 }
97
98 ponPortNo := uint32(1)
99 if device.ParentPortNo != 0 {
100 ponPortNo = device.ParentPortNo
101 }
102
103 ponPort := &voltha.Port{
104 PortNo: ponPortNo,
105 Label: fmt.Sprintf("pon-%d", ponPortNo),
106 Type: voltha.Port_PON_ONU,
107 OperStatus: voltha.OperStatus_ACTIVE,
108 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500109 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500110 }
npujar1d86a522019-11-14 17:11:16 +0530111 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
khenaidooab1f7bd2019-11-14 14:00:27 -0500112 log.Fatalf("PortCreated-failed-%s", err)
113 }
114
115 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
116 d.OperStatus = voltha.OperStatus_ACTIVE
117
118 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
119 log.Fatalf("device-state-update-failed-%s", err)
120 }
121 //Get the latest device data from the Core
122 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
123 log.Fatalf("getting-device-failed-%s", err)
124 }
125
126 if err = onuA.updateDevice(d); err != nil {
127 log.Fatalf("saving-device-failed-%s", err)
128 }
129 }()
130 return nil
131}
132
npujar1d86a522019-11-14 17:11:16 +0530133// Get_ofp_port_info returns ofp device info
134func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500135 if d := onuA.getDevice(device.Id); d == nil {
136 log.Fatalf("device-not-found-%s", device.Id)
137 }
138 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
139 return &ic.PortCapability{
140 Port: &voltha.LogicalPort{
141 OfpPort: &of.OfpPort{
142 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
143 Config: 0,
144 State: uint32(of.OfpPortState_OFPPS_LIVE),
145 Curr: capability,
146 Advertised: capability,
147 Peer: capability,
148 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
149 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
150 },
151 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530152 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500153 },
154 }, nil
155}
156
npujar1d86a522019-11-14 17:11:16 +0530157// Disable_device disables device
158func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500159 go func() {
160 if d := onuA.getDevice(device.Id); d == nil {
161 log.Fatalf("device-not-found-%s", device.Id)
162 }
163 cloned := proto.Clone(device).(*voltha.Device)
164 // Update the all ports state on that device to disable
165 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
166 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
167 }
168 //Update the device state
169 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
170 cloned.OperStatus = voltha.OperStatus_UNKNOWN
171
172 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
173 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
174 }
175 if err := onuA.updateDevice(cloned); err != nil {
176 log.Fatalf("saving-device-failed-%s", err)
177 }
178 }()
179 return nil
180}
181
npujar1d86a522019-11-14 17:11:16 +0530182// Reenable_device reenables device
183func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500184 go func() {
185 if d := onuA.getDevice(device.Id); d == nil {
186 log.Fatalf("device-not-found-%s", device.Id)
187 }
188
189 cloned := proto.Clone(device).(*voltha.Device)
190 // Update the all ports state on that device to enable
191 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
192 log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
193 }
194
195 //Update the device state
196 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
197 cloned.OperStatus = voltha.OperStatus_ACTIVE
198
199 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
200 log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
201 }
202 if err := onuA.updateDevice(cloned); err != nil {
203 log.Fatalf("saving-device-failed-%s", err)
204 }
205 }()
206 return nil
207}
khenaidoo67b22152020-03-02 16:01:25 -0500208
209// Update_flows_incrementally mocks the incremental flow update
210func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint
211 onuA.lock.Lock()
212 defer onuA.lock.Unlock()
213
214 if flows.ToAdd != nil {
215 for _, f := range flows.ToAdd.Items {
216 onuA.flows[f.Id] = f
217 }
218 }
219 if flows.ToRemove != nil {
220 for _, f := range flows.ToRemove.Items {
221 delete(onuA.flows, f.Id)
222 }
223 }
224 return nil
225}
226
227// GetFlowCount returns the total number of flows presently under this adapter
228func (onuA *ONUAdapter) GetFlowCount() int {
229 onuA.lock.Lock()
230 defer onuA.lock.Unlock()
231
232 return len(onuA.flows)
233}
234
235// ClearFlows removes all flows in this adapter
236func (onuA *ONUAdapter) ClearFlows() {
237 onuA.lock.Lock()
238 defer onuA.lock.Unlock()
239
240 onuA.flows = map[uint64]*voltha.OfpFlowStats{}
241}