blob: 7bad0c22fec7119309b66d565f0e99b7e8c8e68e [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 (
khenaidoo8b4abbf2020-04-24 17:04:30 -040020 "fmt"
npujar1d86a522019-11-14 17:11:16 +053021 "strconv"
22 "strings"
23 "sync"
24
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
26 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
27 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
28 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050029)
30
31func macAddressToUint32Array(mac string) []uint32 {
32 slist := strings.Split(mac, ":")
33 result := make([]uint32, len(slist))
34 var err error
35 var tmp int64
36 for index, val := range slist {
37 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
38 return []uint32{1, 2, 3, 4, 5, 6}
39 }
40 result[index] = uint32(tmp)
41 }
42 return result
43}
44
npujar1d86a522019-11-14 17:11:16 +053045// Adapter represents adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050046type Adapter struct {
khenaidoo8b4abbf2020-04-24 17:04:30 -040047 coreProxy adapterif.CoreProxy
48 flows map[uint64]*voltha.OfpFlowStats
49 flowLock sync.RWMutex
50 devices map[string]*voltha.Device
51 deviceLock sync.RWMutex
52 failFlowAdd bool
53 failFlowDelete bool
khenaidooab1f7bd2019-11-14 14:00:27 -050054}
55
npujar1d86a522019-11-14 17:11:16 +053056// NewAdapter creates adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050057func NewAdapter(cp adapterif.CoreProxy) *Adapter {
58 return &Adapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040059 flows: map[uint64]*voltha.OfpFlowStats{},
60 devices: map[string]*voltha.Device{},
khenaidooab1f7bd2019-11-14 14:00:27 -050061 coreProxy: cp,
62 }
63}
64
65func (ta *Adapter) storeDevice(d *voltha.Device) {
khenaidoo8b4abbf2020-04-24 17:04:30 -040066 ta.deviceLock.Lock()
67 defer ta.deviceLock.Unlock()
khenaidooab1f7bd2019-11-14 14:00:27 -050068 if d != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -040069 ta.devices[d.Id] = d
khenaidooab1f7bd2019-11-14 14:00:27 -050070 }
71}
72
khenaidooab1f7bd2019-11-14 14:00:27 -050073func (ta *Adapter) getDevice(id string) *voltha.Device {
khenaidoo8b4abbf2020-04-24 17:04:30 -040074 ta.deviceLock.RLock()
75 defer ta.deviceLock.RUnlock()
76 return ta.devices[id]
khenaidooab1f7bd2019-11-14 14:00:27 -050077}
78
khenaidoo8b4abbf2020-04-24 17:04:30 -040079func (ta *Adapter) updateDevice(d *voltha.Device) {
80 ta.storeDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -050081}
82
npujar1d86a522019-11-14 17:11:16 +053083// Adapter_descriptor -
84func (ta *Adapter) Adapter_descriptor() error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050085 return nil
86}
npujar1d86a522019-11-14 17:11:16 +053087
88// Device_types -
89func (ta *Adapter) Device_types() (*voltha.DeviceTypes, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050090 return nil, nil
91}
npujar1d86a522019-11-14 17:11:16 +053092
93// Health -
khenaidooab1f7bd2019-11-14 14:00:27 -050094func (ta *Adapter) Health() (*voltha.HealthStatus, error) {
95 return nil, nil
96}
npujar1d86a522019-11-14 17:11:16 +053097
98// Adopt_device -
99func (ta *Adapter) Adopt_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500100 return nil
101}
102
npujar1d86a522019-11-14 17:11:16 +0530103// Reconcile_device -
104func (ta *Adapter) Reconcile_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500105 return nil
106}
107
npujar1d86a522019-11-14 17:11:16 +0530108// Abandon_device -
109func (ta *Adapter) Abandon_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500110 return nil
111}
112
npujar1d86a522019-11-14 17:11:16 +0530113// Disable_device -
114func (ta *Adapter) Disable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500115 return nil
116}
117
npujar1d86a522019-11-14 17:11:16 +0530118// Reenable_device -
119func (ta *Adapter) Reenable_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500120 return nil
121}
122
npujar1d86a522019-11-14 17:11:16 +0530123// Reboot_device -
124func (ta *Adapter) Reboot_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500125 return nil
126}
127
npujar1d86a522019-11-14 17:11:16 +0530128// Self_test_device -
129func (ta *Adapter) Self_test_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500130 return nil
131}
132
npujar1d86a522019-11-14 17:11:16 +0530133// Delete_device -
134func (ta *Adapter) Delete_device(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500135 return nil
136}
137
npujar1d86a522019-11-14 17:11:16 +0530138// Get_device_details -
139func (ta *Adapter) Get_device_details(device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500140 return nil
141}
142
npujar1d86a522019-11-14 17:11:16 +0530143// Update_flows_bulk -
144func (ta *Adapter) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 return nil
146}
147
khenaidoo8b4abbf2020-04-24 17:04:30 -0400148// Update_flows_incrementally mocks the incremental flow update
npujar1d86a522019-11-14 17:11:16 +0530149func (ta *Adapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint
khenaidoo8b4abbf2020-04-24 17:04:30 -0400150 ta.flowLock.Lock()
151 defer ta.flowLock.Unlock()
152
khenaidoo0db4c812020-05-27 15:27:30 -0400153 if flows.ToAdd != nil && len(flows.ToAdd.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400154 if ta.failFlowAdd {
155 return fmt.Errorf("flow-add-error")
156 }
157 for _, f := range flows.ToAdd.Items {
158 ta.flows[f.Id] = f
159 }
160 }
khenaidoo0db4c812020-05-27 15:27:30 -0400161 if flows.ToRemove != nil && len(flows.ToRemove.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400162 if ta.failFlowDelete {
163 return fmt.Errorf("flow-delete-error")
164 }
165 for _, f := range flows.ToRemove.Items {
166 delete(ta.flows, f.Id)
167 }
168 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500169 return nil
170}
171
npujar1d86a522019-11-14 17:11:16 +0530172// Update_pm_config -
173func (ta *Adapter) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500174 return nil
175}
176
npujar1d86a522019-11-14 17:11:16 +0530177// Receive_packet_out -
178func (ta *Adapter) Receive_packet_out(deviceID string, egressPortNo int, msg *of.OfpPacketOut) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500179 return nil
180}
181
Devmalya Paulc594bb32019-11-06 07:34:27 +0000182// Suppress_event -
183func (ta *Adapter) Suppress_event(filter *voltha.EventFilter) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500184 return nil
185}
186
Devmalya Paulc594bb32019-11-06 07:34:27 +0000187// Unsuppress_event -
188func (ta *Adapter) Unsuppress_event(filter *voltha.EventFilter) error { // nolint
npujar1d86a522019-11-14 17:11:16 +0530189 return nil
190}
191
192// Get_ofp_device_info -
193func (ta *Adapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500194 return &ic.SwitchCapability{
195 Desc: &of.OfpDesc{
196 HwDesc: "adapter_mock",
197 SwDesc: "adapter_mock",
198 SerialNum: "000000000",
199 },
200 SwitchFeatures: &of.OfpSwitchFeatures{
201 NBuffers: 256,
202 NTables: 2,
203 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
204 of.OfpCapabilities_OFPC_TABLE_STATS |
205 of.OfpCapabilities_OFPC_PORT_STATS |
206 of.OfpCapabilities_OFPC_GROUP_STATS),
207 },
208 }, nil
209}
210
npujar1d86a522019-11-14 17:11:16 +0530211// Get_ofp_port_info -
212func (ta *Adapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500213 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
214 return &ic.PortCapability{
215 Port: &voltha.LogicalPort{
216 OfpPort: &of.OfpPort{
217 HwAddr: macAddressToUint32Array("11:11:33:44:55:66"),
218 Config: 0,
219 State: uint32(of.OfpPortState_OFPPS_LIVE),
220 Curr: capability,
221 Advertised: capability,
222 Peer: capability,
223 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
224 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
225 },
226 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530227 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500228 },
229 }, nil
230}
231
npujar1d86a522019-11-14 17:11:16 +0530232// Process_inter_adapter_message -
233func (ta *Adapter) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500234 return nil
235}
236
npujar1d86a522019-11-14 17:11:16 +0530237// Download_image -
238func (ta *Adapter) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500239 return nil, nil
240}
241
npujar1d86a522019-11-14 17:11:16 +0530242// Get_image_download_status -
243func (ta *Adapter) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500244 return nil, nil
245}
246
npujar1d86a522019-11-14 17:11:16 +0530247// Cancel_image_download -
248func (ta *Adapter) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500249 return nil, nil
250}
251
npujar1d86a522019-11-14 17:11:16 +0530252// Activate_image_update -
253func (ta *Adapter) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500254 return nil, nil
255}
256
npujar1d86a522019-11-14 17:11:16 +0530257// Revert_image_update -
258func (ta *Adapter) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500259 return nil, nil
260}
kesavandbc2d1622020-01-21 00:42:01 -0500261
262// Enable_port -
263func (ta *Adapter) Enable_port(deviceId string, port *voltha.Port) error { //nolint
264 return nil
265}
266
267// Disable_port -
268func (ta *Adapter) Disable_port(deviceId string, port *voltha.Port) error { //nolint
269 return nil
270}
Scott Baker0e78ba22020-02-24 17:58:47 -0800271
272// Child_device_lost -
273func (ta *Adapter) Child_device_lost(pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint
274 return nil
275}
Scott Baker432f9be2020-03-26 11:56:30 -0700276
277// Start_omci_test
278func (ta *Adapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { //nolint
279 return nil, nil
280}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800281
282func (ta *Adapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { //nolint
283 return nil, nil
284}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400285
286// GetFlowCount returns the total number of flows presently under this adapter
287func (ta *Adapter) GetFlowCount() int {
288 ta.flowLock.RLock()
289 defer ta.flowLock.RUnlock()
290
291 return len(ta.flows)
292}
293
294// ClearFlows removes all flows in this adapter
295func (ta *Adapter) ClearFlows() {
296 ta.flowLock.Lock()
297 defer ta.flowLock.Unlock()
298
299 ta.flows = map[uint64]*voltha.OfpFlowStats{}
300}
301
302// SetFlowAction sets the adapter action on addition and deletion of flows
303func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) {
304 ta.failFlowAdd = failFlowAdd
305 ta.failFlowDelete = failFlowDelete
306}