blob: 92dff06c35fd0816be1b93392352fb78b9293d90 [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 {
Girish Kumarf56a4682020-03-20 20:07:46 +000062 logger.Fatalf("deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050063 }
khenaidooab1f7bd2019-11-14 14:00:27 -050064
khenaidoo442e7c72020-03-10 16:13:48 -040065 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
66 d.OperStatus = voltha.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -050067
khenaidoo442e7c72020-03-10 16:13:48 -040068 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000069 logger.Fatalf("device-state-update-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -040070 }
khenaidooab1f7bd2019-11-14 14:00:27 -050071
72 uniPortNo := uint32(2)
73 if device.ProxyAddress != nil {
74 if device.ProxyAddress.ChannelId != 0 {
75 uniPortNo = device.ProxyAddress.ChannelId
76 }
77 }
78
79 uniPort := &voltha.Port{
80 PortNo: uniPortNo,
81 Label: fmt.Sprintf("uni-%d", uniPortNo),
82 Type: voltha.Port_ETHERNET_UNI,
83 OperStatus: voltha.OperStatus_ACTIVE,
84 }
85 var err error
86 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000087 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050088 }
89
90 ponPortNo := uint32(1)
91 if device.ParentPortNo != 0 {
92 ponPortNo = device.ParentPortNo
93 }
94
95 ponPort := &voltha.Port{
96 PortNo: ponPortNo,
97 Label: fmt.Sprintf("pon-%d", ponPortNo),
98 Type: voltha.Port_PON_ONU,
99 OperStatus: voltha.OperStatus_ACTIVE,
100 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500101 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500102 }
npujar1d86a522019-11-14 17:11:16 +0530103 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000104 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500105 }
106
107 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
108 d.OperStatus = voltha.OperStatus_ACTIVE
109
110 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000111 logger.Fatalf("device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500112 }
113 //Get the latest device data from the Core
114 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000115 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500116 }
117
118 if err = onuA.updateDevice(d); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000119 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500120 }
121 }()
122 return nil
123}
124
npujar1d86a522019-11-14 17:11:16 +0530125// Get_ofp_port_info returns ofp device info
126func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500127 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000128 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500129 }
130 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
131 return &ic.PortCapability{
132 Port: &voltha.LogicalPort{
133 OfpPort: &of.OfpPort{
134 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
135 Config: 0,
136 State: uint32(of.OfpPortState_OFPPS_LIVE),
137 Curr: capability,
138 Advertised: capability,
139 Peer: capability,
140 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
141 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
142 },
143 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530144 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 },
146 }, nil
147}
148
npujar1d86a522019-11-14 17:11:16 +0530149// Disable_device disables device
150func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500151 go func() {
152 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000153 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500154 }
155 cloned := proto.Clone(device).(*voltha.Device)
156 // Update the all ports state on that device to disable
157 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400158 // Device may also have been deleted in the Core
Girish Kumarf56a4682020-03-20 20:07:46 +0000159 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400160 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500161 }
162 //Update the device state
163 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
164 cloned.OperStatus = voltha.OperStatus_UNKNOWN
165
166 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000167 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400168 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500169 }
170 if err := onuA.updateDevice(cloned); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000171 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500172 }
173 }()
174 return nil
175}
176
npujar1d86a522019-11-14 17:11:16 +0530177// Reenable_device reenables device
178func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500179 go func() {
180 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000181 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500182 }
183
184 cloned := proto.Clone(device).(*voltha.Device)
185 // Update the all ports state on that device to enable
186 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000187 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500188 }
189
190 //Update the device state
191 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
192 cloned.OperStatus = voltha.OperStatus_ACTIVE
193
194 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000195 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500196 }
197 if err := onuA.updateDevice(cloned); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000198 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500199 }
200 }()
201 return nil
202}
khenaidoo67b22152020-03-02 16:01:25 -0500203
204// Update_flows_incrementally mocks the incremental flow update
205func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint
206 onuA.lock.Lock()
207 defer onuA.lock.Unlock()
208
209 if flows.ToAdd != nil {
210 for _, f := range flows.ToAdd.Items {
211 onuA.flows[f.Id] = f
212 }
213 }
214 if flows.ToRemove != nil {
215 for _, f := range flows.ToRemove.Items {
216 delete(onuA.flows, f.Id)
217 }
218 }
219 return nil
220}
221
222// GetFlowCount returns the total number of flows presently under this adapter
223func (onuA *ONUAdapter) GetFlowCount() int {
224 onuA.lock.Lock()
225 defer onuA.lock.Unlock()
226
227 return len(onuA.flows)
228}
229
230// ClearFlows removes all flows in this adapter
231func (onuA *ONUAdapter) ClearFlows() {
232 onuA.lock.Lock()
233 defer onuA.lock.Unlock()
234
235 onuA.flows = map[uint64]*voltha.OfpFlowStats{}
236}