blob: a8117fe874671a3c01d8263194972dd4291f299f [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 (
Rohan Agrawal31f21802020-06-12 05:38:46 +000020 "context"
khenaidoo8b4abbf2020-04-24 17:04:30 -040021 "fmt"
Andrea Campanella025667e2021-01-14 11:50:07 +010022 "github.com/opencord/voltha-protos/v4/go/extension"
npujar1d86a522019-11-14 17:11:16 +053023 "strconv"
24 "strings"
25 "sync"
26
Maninderdfadc982020-10-28 14:04:33 +053027 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
28 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
29 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
30 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
32
33func macAddressToUint32Array(mac string) []uint32 {
34 slist := strings.Split(mac, ":")
35 result := make([]uint32, len(slist))
36 var err error
37 var tmp int64
38 for index, val := range slist {
39 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
40 return []uint32{1, 2, 3, 4, 5, 6}
41 }
42 result[index] = uint32(tmp)
43 }
44 return result
45}
46
npujar1d86a522019-11-14 17:11:16 +053047// Adapter represents adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050048type Adapter struct {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053049 coreProxy adapterif.CoreProxy
50 flows map[uint64]*voltha.OfpFlowStats
51 flowLock sync.RWMutex
52 devices map[string]*voltha.Device
53 deviceLock sync.RWMutex
54 failFlowAdd bool
55 failFlowDelete bool
56 failDeleteDevice bool
khenaidooab1f7bd2019-11-14 14:00:27 -050057}
58
npujar1d86a522019-11-14 17:11:16 +053059// NewAdapter creates adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050060func NewAdapter(cp adapterif.CoreProxy) *Adapter {
61 return &Adapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040062 flows: map[uint64]*voltha.OfpFlowStats{},
63 devices: map[string]*voltha.Device{},
khenaidooab1f7bd2019-11-14 14:00:27 -050064 coreProxy: cp,
65 }
66}
67
68func (ta *Adapter) storeDevice(d *voltha.Device) {
khenaidoo8b4abbf2020-04-24 17:04:30 -040069 ta.deviceLock.Lock()
70 defer ta.deviceLock.Unlock()
khenaidooab1f7bd2019-11-14 14:00:27 -050071 if d != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -040072 ta.devices[d.Id] = d
khenaidooab1f7bd2019-11-14 14:00:27 -050073 }
74}
75
khenaidooab1f7bd2019-11-14 14:00:27 -050076func (ta *Adapter) getDevice(id string) *voltha.Device {
khenaidoo8b4abbf2020-04-24 17:04:30 -040077 ta.deviceLock.RLock()
78 defer ta.deviceLock.RUnlock()
79 return ta.devices[id]
khenaidooab1f7bd2019-11-14 14:00:27 -050080}
81
khenaidoo8b4abbf2020-04-24 17:04:30 -040082func (ta *Adapter) updateDevice(d *voltha.Device) {
83 ta.storeDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -050084}
85
npujar1d86a522019-11-14 17:11:16 +053086// Adapter_descriptor -
Rohan Agrawal31f21802020-06-12 05:38:46 +000087func (ta *Adapter) Adapter_descriptor(ctx context.Context) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050088 return nil
89}
npujar1d86a522019-11-14 17:11:16 +053090
91// Device_types -
Rohan Agrawal31f21802020-06-12 05:38:46 +000092func (ta *Adapter) Device_types(ctx context.Context) (*voltha.DeviceTypes, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050093 return nil, nil
94}
npujar1d86a522019-11-14 17:11:16 +053095
96// Health -
Rohan Agrawal31f21802020-06-12 05:38:46 +000097func (ta *Adapter) Health(ctx context.Context) (*voltha.HealthStatus, error) {
khenaidooab1f7bd2019-11-14 14:00:27 -050098 return nil, nil
99}
npujar1d86a522019-11-14 17:11:16 +0530100
101// Adopt_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000102func (ta *Adapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500103 return nil
104}
105
npujar1d86a522019-11-14 17:11:16 +0530106// Reconcile_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000107func (ta *Adapter) Reconcile_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500108 return nil
109}
110
npujar1d86a522019-11-14 17:11:16 +0530111// Abandon_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000112func (ta *Adapter) Abandon_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500113 return nil
114}
115
npujar1d86a522019-11-14 17:11:16 +0530116// Disable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000117func (ta *Adapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500118 return nil
119}
120
npujar1d86a522019-11-14 17:11:16 +0530121// Reenable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000122func (ta *Adapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500123 return nil
124}
125
npujar1d86a522019-11-14 17:11:16 +0530126// Reboot_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000127func (ta *Adapter) Reboot_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500128 return nil
129}
130
npujar1d86a522019-11-14 17:11:16 +0530131// Self_test_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000132func (ta *Adapter) Self_test_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500133 return nil
134}
135
npujar1d86a522019-11-14 17:11:16 +0530136// Delete_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000137func (ta *Adapter) Delete_device(ctx context.Context, device *voltha.Device) error { // nolint
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530138 if ta.failDeleteDevice {
139 return fmt.Errorf("delete-device-failure")
140 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500141 return nil
142}
143
npujar1d86a522019-11-14 17:11:16 +0530144// Get_device_details -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000145func (ta *Adapter) Get_device_details(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500146 return nil
147}
148
npujar1d86a522019-11-14 17:11:16 +0530149// Update_flows_bulk -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000150func (ta *Adapter) Update_flows_bulk(ctx context.Context, device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500151 return nil
152}
153
khenaidoo8b4abbf2020-04-24 17:04:30 -0400154// Update_flows_incrementally mocks the incremental flow update
Rohan Agrawal31f21802020-06-12 05:38:46 +0000155func (ta *Adapter) Update_flows_incrementally(ctx context.Context, device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint
khenaidoo8b4abbf2020-04-24 17:04:30 -0400156 ta.flowLock.Lock()
157 defer ta.flowLock.Unlock()
158
khenaidoo0db4c812020-05-27 15:27:30 -0400159 if flows.ToAdd != nil && len(flows.ToAdd.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400160 if ta.failFlowAdd {
161 return fmt.Errorf("flow-add-error")
162 }
163 for _, f := range flows.ToAdd.Items {
164 ta.flows[f.Id] = f
165 }
166 }
khenaidoo0db4c812020-05-27 15:27:30 -0400167 if flows.ToRemove != nil && len(flows.ToRemove.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400168 if ta.failFlowDelete {
169 return fmt.Errorf("flow-delete-error")
170 }
171 for _, f := range flows.ToRemove.Items {
172 delete(ta.flows, f.Id)
173 }
174 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500175 return nil
176}
177
npujar1d86a522019-11-14 17:11:16 +0530178// Update_pm_config -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000179func (ta *Adapter) Update_pm_config(ctx context.Context, device *voltha.Device, pmConfigs *voltha.PmConfigs) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500180 return nil
181}
182
npujar1d86a522019-11-14 17:11:16 +0530183// Receive_packet_out -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000184func (ta *Adapter) Receive_packet_out(ctx context.Context, deviceID string, egressPortNo int, msg *of.OfpPacketOut) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500185 return nil
186}
187
Devmalya Paulc594bb32019-11-06 07:34:27 +0000188// Suppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189func (ta *Adapter) Suppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500190 return nil
191}
192
Devmalya Paulc594bb32019-11-06 07:34:27 +0000193// Unsuppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000194func (ta *Adapter) Unsuppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
npujar1d86a522019-11-14 17:11:16 +0530195 return nil
196}
197
198// Get_ofp_device_info -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000199func (ta *Adapter) Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500200 return &ic.SwitchCapability{
201 Desc: &of.OfpDesc{
202 HwDesc: "adapter_mock",
203 SwDesc: "adapter_mock",
204 SerialNum: "000000000",
205 },
206 SwitchFeatures: &of.OfpSwitchFeatures{
207 NBuffers: 256,
208 NTables: 2,
209 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
210 of.OfpCapabilities_OFPC_TABLE_STATS |
211 of.OfpCapabilities_OFPC_PORT_STATS |
212 of.OfpCapabilities_OFPC_GROUP_STATS),
213 },
214 }, nil
215}
216
npujar1d86a522019-11-14 17:11:16 +0530217// Process_inter_adapter_message -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000218func (ta *Adapter) Process_inter_adapter_message(ctx context.Context, msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 return nil
220}
221
npujar1d86a522019-11-14 17:11:16 +0530222// Download_image -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000223func (ta *Adapter) Download_image(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500224 return nil, nil
225}
226
npujar1d86a522019-11-14 17:11:16 +0530227// Get_image_download_status -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000228func (ta *Adapter) Get_image_download_status(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500229 return nil, nil
230}
231
npujar1d86a522019-11-14 17:11:16 +0530232// Cancel_image_download -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000233func (ta *Adapter) Cancel_image_download(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500234 return nil, nil
235}
236
npujar1d86a522019-11-14 17:11:16 +0530237// Activate_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000238func (ta *Adapter) Activate_image_update(ctx context.Context, 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// Revert_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000243func (ta *Adapter) Revert_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500244 return nil, nil
245}
kesavandbc2d1622020-01-21 00:42:01 -0500246
247// Enable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000248func (ta *Adapter) Enable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500249 return nil
250}
251
252// Disable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000253func (ta *Adapter) Disable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500254 return nil
255}
Scott Baker0e78ba22020-02-24 17:58:47 -0800256
257// Child_device_lost -
Girish Gowdra6f9b10e2021-03-11 14:36:39 -0800258func (ta *Adapter) Child_device_lost(ctx context.Context, childDevice *voltha.Device) error { //nolint
Scott Baker0e78ba22020-02-24 17:58:47 -0800259 return nil
260}
Scott Baker432f9be2020-03-26 11:56:30 -0700261
262// Start_omci_test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000263func (ta *Adapter) Start_omci_test(ctx context.Context, device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { //nolint
Scott Baker432f9be2020-03-26 11:56:30 -0700264 return nil, nil
265}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800266
Rohan Agrawal31f21802020-06-12 05:38:46 +0000267func (ta *Adapter) Get_ext_value(ctx context.Context, deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { //nolint
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800268 return nil, nil
269}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400270
Andrea Campanella025667e2021-01-14 11:50:07 +0100271// Single_get_value_request retrieves a single value.
272func (ta *Adapter) Single_get_value_request(ctx context.Context, // nolint
273 request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
274 return nil, nil
275}
276
khenaidoo8b4abbf2020-04-24 17:04:30 -0400277// GetFlowCount returns the total number of flows presently under this adapter
278func (ta *Adapter) GetFlowCount() int {
279 ta.flowLock.RLock()
280 defer ta.flowLock.RUnlock()
281
282 return len(ta.flows)
283}
284
285// ClearFlows removes all flows in this adapter
286func (ta *Adapter) ClearFlows() {
287 ta.flowLock.Lock()
288 defer ta.flowLock.Unlock()
289
290 ta.flows = map[uint64]*voltha.OfpFlowStats{}
291}
292
293// SetFlowAction sets the adapter action on addition and deletion of flows
294func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) {
295 ta.failFlowAdd = failFlowAdd
296 ta.failFlowDelete = failFlowDelete
297}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530298
299// SetDeleteAction sets the adapter action on delete device
300func (ta *Adapter) SetDeleteAction(failDeleteDevice bool) {
301 ta.failDeleteDevice = failDeleteDevice
302}
ssiddiquif076cb82021-04-23 10:47:04 +0530303
304// Download_onu_image -
305func (ta *Adapter) Download_onu_image(ctx context.Context, request *voltha.DeviceImageDownloadRequest) (*voltha.DeviceImageResponse, error) { //nolint
306 return nil, nil
307}
308
309// Get_onu_image_status -
310func (ta *Adapter) Get_onu_image_status(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
311 return nil, nil
312}
313
314// Abort_onu_image_upgrade -
315func (ta *Adapter) Abort_onu_image_upgrade(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
316 return nil, nil
317}
318
319// Get_onu_images -
320func (ta *Adapter) Get_onu_images(ctx context.Context, deviceID string) (*voltha.OnuImages, error) { //nolint
321 return nil, nil
322}
323
324// Activate_onu_image -
325func (ta *Adapter) Activate_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
326 return nil, nil
327}
328
329// Commit_onu_image -
330func (ta *Adapter) Commit_onu_image(ctx context.Context, in *voltha.DeviceImageRequest) (*voltha.DeviceImageResponse, error) { //nolint
331 return nil, nil
332}