blob: c01fc70f4cde0581db5fef49bf137ab93d92f06d [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"
npujar1d86a522019-11-14 17:11:16 +053022 "strconv"
23 "strings"
24 "sync"
25
Maninderdfadc982020-10-28 14:04:33 +053026 "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif"
27 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
28 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
29 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidooab1f7bd2019-11-14 14:00:27 -050030)
31
32func macAddressToUint32Array(mac string) []uint32 {
33 slist := strings.Split(mac, ":")
34 result := make([]uint32, len(slist))
35 var err error
36 var tmp int64
37 for index, val := range slist {
38 if tmp, err = strconv.ParseInt(val, 16, 32); err != nil {
39 return []uint32{1, 2, 3, 4, 5, 6}
40 }
41 result[index] = uint32(tmp)
42 }
43 return result
44}
45
npujar1d86a522019-11-14 17:11:16 +053046// Adapter represents adapter attributes
khenaidooab1f7bd2019-11-14 14:00:27 -050047type Adapter struct {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +053048 coreProxy adapterif.CoreProxy
49 flows map[uint64]*voltha.OfpFlowStats
50 flowLock sync.RWMutex
51 devices map[string]*voltha.Device
52 deviceLock sync.RWMutex
53 failFlowAdd bool
54 failFlowDelete bool
55 failDeleteDevice bool
khenaidooab1f7bd2019-11-14 14:00:27 -050056}
57
npujar1d86a522019-11-14 17:11:16 +053058// NewAdapter creates adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050059func NewAdapter(cp adapterif.CoreProxy) *Adapter {
60 return &Adapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040061 flows: map[uint64]*voltha.OfpFlowStats{},
62 devices: map[string]*voltha.Device{},
khenaidooab1f7bd2019-11-14 14:00:27 -050063 coreProxy: cp,
64 }
65}
66
67func (ta *Adapter) storeDevice(d *voltha.Device) {
khenaidoo8b4abbf2020-04-24 17:04:30 -040068 ta.deviceLock.Lock()
69 defer ta.deviceLock.Unlock()
khenaidooab1f7bd2019-11-14 14:00:27 -050070 if d != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -040071 ta.devices[d.Id] = d
khenaidooab1f7bd2019-11-14 14:00:27 -050072 }
73}
74
khenaidooab1f7bd2019-11-14 14:00:27 -050075func (ta *Adapter) getDevice(id string) *voltha.Device {
khenaidoo8b4abbf2020-04-24 17:04:30 -040076 ta.deviceLock.RLock()
77 defer ta.deviceLock.RUnlock()
78 return ta.devices[id]
khenaidooab1f7bd2019-11-14 14:00:27 -050079}
80
khenaidoo8b4abbf2020-04-24 17:04:30 -040081func (ta *Adapter) updateDevice(d *voltha.Device) {
82 ta.storeDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -050083}
84
npujar1d86a522019-11-14 17:11:16 +053085// Adapter_descriptor -
Rohan Agrawal31f21802020-06-12 05:38:46 +000086func (ta *Adapter) Adapter_descriptor(ctx context.Context) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050087 return nil
88}
npujar1d86a522019-11-14 17:11:16 +053089
90// Device_types -
Rohan Agrawal31f21802020-06-12 05:38:46 +000091func (ta *Adapter) Device_types(ctx context.Context) (*voltha.DeviceTypes, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050092 return nil, nil
93}
npujar1d86a522019-11-14 17:11:16 +053094
95// Health -
Rohan Agrawal31f21802020-06-12 05:38:46 +000096func (ta *Adapter) Health(ctx context.Context) (*voltha.HealthStatus, error) {
khenaidooab1f7bd2019-11-14 14:00:27 -050097 return nil, nil
98}
npujar1d86a522019-11-14 17:11:16 +053099
100// Adopt_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000101func (ta *Adapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500102 return nil
103}
104
npujar1d86a522019-11-14 17:11:16 +0530105// Reconcile_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000106func (ta *Adapter) Reconcile_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500107 return nil
108}
109
npujar1d86a522019-11-14 17:11:16 +0530110// Abandon_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000111func (ta *Adapter) Abandon_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500112 return nil
113}
114
npujar1d86a522019-11-14 17:11:16 +0530115// Disable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000116func (ta *Adapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500117 return nil
118}
119
npujar1d86a522019-11-14 17:11:16 +0530120// Reenable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000121func (ta *Adapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500122 return nil
123}
124
npujar1d86a522019-11-14 17:11:16 +0530125// Reboot_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000126func (ta *Adapter) Reboot_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500127 return nil
128}
129
npujar1d86a522019-11-14 17:11:16 +0530130// Self_test_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000131func (ta *Adapter) Self_test_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500132 return nil
133}
134
npujar1d86a522019-11-14 17:11:16 +0530135// Delete_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000136func (ta *Adapter) Delete_device(ctx context.Context, device *voltha.Device) error { // nolint
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530137 if ta.failDeleteDevice {
138 return fmt.Errorf("delete-device-failure")
139 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500140 return nil
141}
142
npujar1d86a522019-11-14 17:11:16 +0530143// Get_device_details -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000144func (ta *Adapter) Get_device_details(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500145 return nil
146}
147
npujar1d86a522019-11-14 17:11:16 +0530148// Update_flows_bulk -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000149func (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 -0500150 return nil
151}
152
khenaidoo8b4abbf2020-04-24 17:04:30 -0400153// Update_flows_incrementally mocks the incremental flow update
Rohan Agrawal31f21802020-06-12 05:38:46 +0000154func (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 -0400155 ta.flowLock.Lock()
156 defer ta.flowLock.Unlock()
157
khenaidoo0db4c812020-05-27 15:27:30 -0400158 if flows.ToAdd != nil && len(flows.ToAdd.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400159 if ta.failFlowAdd {
160 return fmt.Errorf("flow-add-error")
161 }
162 for _, f := range flows.ToAdd.Items {
163 ta.flows[f.Id] = f
164 }
165 }
khenaidoo0db4c812020-05-27 15:27:30 -0400166 if flows.ToRemove != nil && len(flows.ToRemove.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400167 if ta.failFlowDelete {
168 return fmt.Errorf("flow-delete-error")
169 }
170 for _, f := range flows.ToRemove.Items {
171 delete(ta.flows, f.Id)
172 }
173 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500174 return nil
175}
176
npujar1d86a522019-11-14 17:11:16 +0530177// Update_pm_config -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000178func (ta *Adapter) Update_pm_config(ctx context.Context, device *voltha.Device, pmConfigs *voltha.PmConfigs) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500179 return nil
180}
181
npujar1d86a522019-11-14 17:11:16 +0530182// Receive_packet_out -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000183func (ta *Adapter) Receive_packet_out(ctx context.Context, deviceID string, egressPortNo int, msg *of.OfpPacketOut) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500184 return nil
185}
186
Devmalya Paulc594bb32019-11-06 07:34:27 +0000187// Suppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000188func (ta *Adapter) Suppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500189 return nil
190}
191
Devmalya Paulc594bb32019-11-06 07:34:27 +0000192// Unsuppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000193func (ta *Adapter) Unsuppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
npujar1d86a522019-11-14 17:11:16 +0530194 return nil
195}
196
197// Get_ofp_device_info -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000198func (ta *Adapter) Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500199 return &ic.SwitchCapability{
200 Desc: &of.OfpDesc{
201 HwDesc: "adapter_mock",
202 SwDesc: "adapter_mock",
203 SerialNum: "000000000",
204 },
205 SwitchFeatures: &of.OfpSwitchFeatures{
206 NBuffers: 256,
207 NTables: 2,
208 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
209 of.OfpCapabilities_OFPC_TABLE_STATS |
210 of.OfpCapabilities_OFPC_PORT_STATS |
211 of.OfpCapabilities_OFPC_GROUP_STATS),
212 },
213 }, nil
214}
215
npujar1d86a522019-11-14 17:11:16 +0530216// Process_inter_adapter_message -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000217func (ta *Adapter) Process_inter_adapter_message(ctx context.Context, msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500218 return nil
219}
220
npujar1d86a522019-11-14 17:11:16 +0530221// Download_image -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000222func (ta *Adapter) Download_image(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500223 return nil, nil
224}
225
npujar1d86a522019-11-14 17:11:16 +0530226// Get_image_download_status -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000227func (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 -0500228 return nil, nil
229}
230
npujar1d86a522019-11-14 17:11:16 +0530231// Cancel_image_download -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000232func (ta *Adapter) Cancel_image_download(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500233 return nil, nil
234}
235
npujar1d86a522019-11-14 17:11:16 +0530236// Activate_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000237func (ta *Adapter) Activate_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500238 return nil, nil
239}
240
npujar1d86a522019-11-14 17:11:16 +0530241// Revert_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000242func (ta *Adapter) Revert_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500243 return nil, nil
244}
kesavandbc2d1622020-01-21 00:42:01 -0500245
246// Enable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000247func (ta *Adapter) Enable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500248 return nil
249}
250
251// Disable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000252func (ta *Adapter) Disable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500253 return nil
254}
Scott Baker0e78ba22020-02-24 17:58:47 -0800255
256// Child_device_lost -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000257func (ta *Adapter) Child_device_lost(ctx context.Context, pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint
Scott Baker0e78ba22020-02-24 17:58:47 -0800258 return nil
259}
Scott Baker432f9be2020-03-26 11:56:30 -0700260
261// Start_omci_test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000262func (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 -0700263 return nil, nil
264}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800265
Rohan Agrawal31f21802020-06-12 05:38:46 +0000266func (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 -0800267 return nil, nil
268}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400269
270// GetFlowCount returns the total number of flows presently under this adapter
271func (ta *Adapter) GetFlowCount() int {
272 ta.flowLock.RLock()
273 defer ta.flowLock.RUnlock()
274
275 return len(ta.flows)
276}
277
278// ClearFlows removes all flows in this adapter
279func (ta *Adapter) ClearFlows() {
280 ta.flowLock.Lock()
281 defer ta.flowLock.Unlock()
282
283 ta.flows = map[uint64]*voltha.OfpFlowStats{}
284}
285
286// SetFlowAction sets the adapter action on addition and deletion of flows
287func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) {
288 ta.failFlowAdd = failFlowAdd
289 ta.failFlowDelete = failFlowDelete
290}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530291
292// SetDeleteAction sets the adapter action on delete device
293func (ta *Adapter) SetDeleteAction(failDeleteDevice bool) {
294 ta.failDeleteDevice = failDeleteDevice
295}