blob: 5f4a5e638f9ca17ca8ecdacabd0381733df88b0f [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"
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -080021 "errors"
khenaidooab1f7bd2019-11-14 14:00:27 -050022 "fmt"
npujar1d86a522019-11-14 17:11:16 +053023 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050024 "sync"
npujar1d86a522019-11-14 17:11:16 +053025
khenaidooab1f7bd2019-11-14 14:00:27 -050026 "github.com/gogo/protobuf/proto"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080027 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
28 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
29 "github.com/opencord/voltha-lib-go/v3/pkg/log"
30 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
31 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
32 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050033)
34
npujar1d86a522019-11-14 17:11:16 +053035// ONUAdapter represent ONU adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050036type ONUAdapter struct {
khenaidoo67b22152020-03-02 16:01:25 -050037 flows map[uint64]*voltha.OfpFlowStats
38 lock sync.Mutex
khenaidooab1f7bd2019-11-14 14:00:27 -050039 Adapter
40}
41
npujar1d86a522019-11-14 17:11:16 +053042// NewONUAdapter creates ONU adapter
khenaidooab1f7bd2019-11-14 14:00:27 -050043func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter {
khenaidoo67b22152020-03-02 16:01:25 -050044 return &ONUAdapter{
45 flows: map[uint64]*voltha.OfpFlowStats{},
46 Adapter: Adapter{
47 coreProxy: cp,
48 },
49 }
khenaidooab1f7bd2019-11-14 14:00:27 -050050}
51
npujar1d86a522019-11-14 17:11:16 +053052// Adopt_device creates new handler for added device
53func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050054 go func() {
55 d := proto.Clone(device).(*voltha.Device)
56 d.Root = false
57 d.Vendor = "onu_adapter_mock"
58 d.Model = "go-mock"
59 d.SerialNumber = com.GetRandomSerialNumber()
60 d.MacAddress = strings.ToUpper(com.GetRandomMacAddress())
61 onuA.storeDevice(d)
62 if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000063 logger.Fatalf("deviceUpdate-failed-%s", res)
khenaidooab1f7bd2019-11-14 14:00:27 -050064 }
khenaidooab1f7bd2019-11-14 14:00:27 -050065
khenaidoo442e7c72020-03-10 16:13:48 -040066 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
67 d.OperStatus = voltha.OperStatus_DISCOVERED
khenaidoo93181522020-01-23 12:43:21 -050068
khenaidoo442e7c72020-03-10 16:13:48 -040069 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000070 logger.Fatalf("device-state-update-failed-%s", err)
khenaidoo442e7c72020-03-10 16:13:48 -040071 }
khenaidooab1f7bd2019-11-14 14:00:27 -050072
73 uniPortNo := uint32(2)
74 if device.ProxyAddress != nil {
75 if device.ProxyAddress.ChannelId != 0 {
76 uniPortNo = device.ProxyAddress.ChannelId
77 }
78 }
79
80 uniPort := &voltha.Port{
81 PortNo: uniPortNo,
82 Label: fmt.Sprintf("uni-%d", uniPortNo),
83 Type: voltha.Port_ETHERNET_UNI,
84 OperStatus: voltha.OperStatus_ACTIVE,
85 }
86 var err error
87 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000088 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -050089 }
90
91 ponPortNo := uint32(1)
92 if device.ParentPortNo != 0 {
93 ponPortNo = device.ParentPortNo
94 }
95
96 ponPort := &voltha.Port{
97 PortNo: ponPortNo,
98 Label: fmt.Sprintf("pon-%d", ponPortNo),
99 Type: voltha.Port_PON_ONU,
100 OperStatus: voltha.OperStatus_ACTIVE,
101 Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT
khenaidoo6e55d9e2019-12-12 18:26:26 -0500102 PortNo: device.ParentPortNo}}, // Peer port is parent's port number
khenaidooab1f7bd2019-11-14 14:00:27 -0500103 }
npujar1d86a522019-11-14 17:11:16 +0530104 if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000105 logger.Fatalf("PortCreated-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500106 }
107
108 d.ConnectStatus = voltha.ConnectStatus_REACHABLE
109 d.OperStatus = voltha.OperStatus_ACTIVE
110
111 if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000112 logger.Fatalf("device-state-update-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500113 }
114 //Get the latest device data from the Core
115 if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000116 logger.Fatalf("getting-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500117 }
118
119 if err = onuA.updateDevice(d); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000120 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 }
122 }()
123 return nil
124}
125
npujar1d86a522019-11-14 17:11:16 +0530126// Get_ofp_port_info returns ofp device info
127func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500128 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000129 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500130 }
131 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
132 return &ic.PortCapability{
133 Port: &voltha.LogicalPort{
134 OfpPort: &of.OfpPort{
135 HwAddr: macAddressToUint32Array("12:12:12:12:12:12"),
136 Config: 0,
137 State: uint32(of.OfpPortState_OFPPS_LIVE),
138 Curr: capability,
139 Advertised: capability,
140 Peer: capability,
141 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
142 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
143 },
144 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530145 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500146 },
147 }, nil
148}
149
npujar1d86a522019-11-14 17:11:16 +0530150// Disable_device disables device
151func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500152 go func() {
153 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000154 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500155 }
156 cloned := proto.Clone(device).(*voltha.Device)
157 // Update the all ports state on that device to disable
158 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil {
khenaidoo442e7c72020-03-10 16:13:48 -0400159 // Device may also have been deleted in the Core
Girish Kumarf56a4682020-03-20 20:07:46 +0000160 logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400161 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500162 }
163 //Update the device state
164 cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
165 cloned.OperStatus = voltha.OperStatus_UNKNOWN
166
167 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000168 logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidoo442e7c72020-03-10 16:13:48 -0400169 return
khenaidooab1f7bd2019-11-14 14:00:27 -0500170 }
171 if err := onuA.updateDevice(cloned); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000172 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500173 }
174 }()
175 return nil
176}
177
npujar1d86a522019-11-14 17:11:16 +0530178// Reenable_device reenables device
179func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500180 go func() {
181 if d := onuA.getDevice(device.Id); d == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000182 logger.Fatalf("device-not-found-%s", device.Id)
khenaidooab1f7bd2019-11-14 14:00:27 -0500183 }
184
185 cloned := proto.Clone(device).(*voltha.Device)
186 // Update the all ports state on that device to enable
187 if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000188 logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500189 }
190
191 //Update the device state
192 cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
193 cloned.OperStatus = voltha.OperStatus_ACTIVE
194
195 if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000196 logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err})
khenaidooab1f7bd2019-11-14 14:00:27 -0500197 }
198 if err := onuA.updateDevice(cloned); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000199 logger.Fatalf("saving-device-failed-%s", err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500200 }
201 }()
202 return nil
203}
khenaidoo67b22152020-03-02 16:01:25 -0500204
205// Update_flows_incrementally mocks the incremental flow update
206func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint
207 onuA.lock.Lock()
208 defer onuA.lock.Unlock()
209
210 if flows.ToAdd != nil {
211 for _, f := range flows.ToAdd.Items {
212 onuA.flows[f.Id] = f
213 }
214 }
215 if flows.ToRemove != nil {
216 for _, f := range flows.ToRemove.Items {
217 delete(onuA.flows, f.Id)
218 }
219 }
220 return nil
221}
222
Scott Baker432f9be2020-03-26 11:56:30 -0700223// Start_omci_test begins an omci self-test
224func (onuA *ONUAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint
225 _ = device
226 return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil
227}
228
khenaidoo67b22152020-03-02 16:01:25 -0500229// GetFlowCount returns the total number of flows presently under this adapter
230func (onuA *ONUAdapter) GetFlowCount() int {
231 onuA.lock.Lock()
232 defer onuA.lock.Unlock()
233
234 return len(onuA.flows)
235}
236
237// ClearFlows removes all flows in this adapter
238func (onuA *ONUAdapter) ClearFlows() {
239 onuA.lock.Lock()
240 defer onuA.lock.Unlock()
241
242 onuA.flows = map[uint64]*voltha.OfpFlowStats{}
243}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800244
245func (onuA *ONUAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint
246 _ = deviceId
247 _ = device
248 _ = valueflag
249 return nil, errors.New("get-ext-value-not-implemented")
250}