blob: ccbeea4036f93f4f8e6244e219e24a0ea1b6f377 [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
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif"
27 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
28 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/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 {
khenaidoo8b4abbf2020-04-24 17:04:30 -040048 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
khenaidooab1f7bd2019-11-14 14:00:27 -050055}
56
npujar1d86a522019-11-14 17:11:16 +053057// NewAdapter creates adapter instance
khenaidooab1f7bd2019-11-14 14:00:27 -050058func NewAdapter(cp adapterif.CoreProxy) *Adapter {
59 return &Adapter{
khenaidoo8b4abbf2020-04-24 17:04:30 -040060 flows: map[uint64]*voltha.OfpFlowStats{},
61 devices: map[string]*voltha.Device{},
khenaidooab1f7bd2019-11-14 14:00:27 -050062 coreProxy: cp,
63 }
64}
65
66func (ta *Adapter) storeDevice(d *voltha.Device) {
khenaidoo8b4abbf2020-04-24 17:04:30 -040067 ta.deviceLock.Lock()
68 defer ta.deviceLock.Unlock()
khenaidooab1f7bd2019-11-14 14:00:27 -050069 if d != nil {
khenaidoo8b4abbf2020-04-24 17:04:30 -040070 ta.devices[d.Id] = d
khenaidooab1f7bd2019-11-14 14:00:27 -050071 }
72}
73
khenaidooab1f7bd2019-11-14 14:00:27 -050074func (ta *Adapter) getDevice(id string) *voltha.Device {
khenaidoo8b4abbf2020-04-24 17:04:30 -040075 ta.deviceLock.RLock()
76 defer ta.deviceLock.RUnlock()
77 return ta.devices[id]
khenaidooab1f7bd2019-11-14 14:00:27 -050078}
79
khenaidoo8b4abbf2020-04-24 17:04:30 -040080func (ta *Adapter) updateDevice(d *voltha.Device) {
81 ta.storeDevice(d)
khenaidooab1f7bd2019-11-14 14:00:27 -050082}
83
npujar1d86a522019-11-14 17:11:16 +053084// Adapter_descriptor -
Rohan Agrawal31f21802020-06-12 05:38:46 +000085func (ta *Adapter) Adapter_descriptor(ctx context.Context) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050086 return nil
87}
npujar1d86a522019-11-14 17:11:16 +053088
89// Device_types -
Rohan Agrawal31f21802020-06-12 05:38:46 +000090func (ta *Adapter) Device_types(ctx context.Context) (*voltha.DeviceTypes, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -050091 return nil, nil
92}
npujar1d86a522019-11-14 17:11:16 +053093
94// Health -
Rohan Agrawal31f21802020-06-12 05:38:46 +000095func (ta *Adapter) Health(ctx context.Context) (*voltha.HealthStatus, error) {
khenaidooab1f7bd2019-11-14 14:00:27 -050096 return nil, nil
97}
npujar1d86a522019-11-14 17:11:16 +053098
99// Adopt_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000100func (ta *Adapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500101 return nil
102}
103
npujar1d86a522019-11-14 17:11:16 +0530104// Reconcile_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000105func (ta *Adapter) Reconcile_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500106 return nil
107}
108
npujar1d86a522019-11-14 17:11:16 +0530109// Abandon_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000110func (ta *Adapter) Abandon_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500111 return nil
112}
113
npujar1d86a522019-11-14 17:11:16 +0530114// Disable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000115func (ta *Adapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500116 return nil
117}
118
npujar1d86a522019-11-14 17:11:16 +0530119// Reenable_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000120func (ta *Adapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500121 return nil
122}
123
npujar1d86a522019-11-14 17:11:16 +0530124// Reboot_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000125func (ta *Adapter) Reboot_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500126 return nil
127}
128
npujar1d86a522019-11-14 17:11:16 +0530129// Self_test_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000130func (ta *Adapter) Self_test_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500131 return nil
132}
133
npujar1d86a522019-11-14 17:11:16 +0530134// Delete_device -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000135func (ta *Adapter) Delete_device(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500136 return nil
137}
138
npujar1d86a522019-11-14 17:11:16 +0530139// Get_device_details -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000140func (ta *Adapter) Get_device_details(ctx context.Context, device *voltha.Device) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500141 return nil
142}
143
npujar1d86a522019-11-14 17:11:16 +0530144// Update_flows_bulk -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000145func (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 -0500146 return nil
147}
148
khenaidoo8b4abbf2020-04-24 17:04:30 -0400149// Update_flows_incrementally mocks the incremental flow update
Rohan Agrawal31f21802020-06-12 05:38:46 +0000150func (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 -0400151 ta.flowLock.Lock()
152 defer ta.flowLock.Unlock()
153
khenaidoo0db4c812020-05-27 15:27:30 -0400154 if flows.ToAdd != nil && len(flows.ToAdd.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400155 if ta.failFlowAdd {
156 return fmt.Errorf("flow-add-error")
157 }
158 for _, f := range flows.ToAdd.Items {
159 ta.flows[f.Id] = f
160 }
161 }
khenaidoo0db4c812020-05-27 15:27:30 -0400162 if flows.ToRemove != nil && len(flows.ToRemove.Items) > 0 {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400163 if ta.failFlowDelete {
164 return fmt.Errorf("flow-delete-error")
165 }
166 for _, f := range flows.ToRemove.Items {
167 delete(ta.flows, f.Id)
168 }
169 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500170 return nil
171}
172
npujar1d86a522019-11-14 17:11:16 +0530173// Update_pm_config -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000174func (ta *Adapter) Update_pm_config(ctx context.Context, device *voltha.Device, pmConfigs *voltha.PmConfigs) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500175 return nil
176}
177
npujar1d86a522019-11-14 17:11:16 +0530178// Receive_packet_out -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000179func (ta *Adapter) Receive_packet_out(ctx context.Context, deviceID string, egressPortNo int, msg *of.OfpPacketOut) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500180 return nil
181}
182
Devmalya Paulc594bb32019-11-06 07:34:27 +0000183// Suppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000184func (ta *Adapter) Suppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500185 return nil
186}
187
Devmalya Paulc594bb32019-11-06 07:34:27 +0000188// Unsuppress_event -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189func (ta *Adapter) Unsuppress_event(ctx context.Context, filter *voltha.EventFilter) error { // nolint
npujar1d86a522019-11-14 17:11:16 +0530190 return nil
191}
192
193// Get_ofp_device_info -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000194func (ta *Adapter) Get_ofp_device_info(ctx context.Context, device *voltha.Device) (*ic.SwitchCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500195 return &ic.SwitchCapability{
196 Desc: &of.OfpDesc{
197 HwDesc: "adapter_mock",
198 SwDesc: "adapter_mock",
199 SerialNum: "000000000",
200 },
201 SwitchFeatures: &of.OfpSwitchFeatures{
202 NBuffers: 256,
203 NTables: 2,
204 Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS |
205 of.OfpCapabilities_OFPC_TABLE_STATS |
206 of.OfpCapabilities_OFPC_PORT_STATS |
207 of.OfpCapabilities_OFPC_GROUP_STATS),
208 },
209 }, nil
210}
211
npujar1d86a522019-11-14 17:11:16 +0530212// Process_inter_adapter_message -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000213func (ta *Adapter) Process_inter_adapter_message(ctx context.Context, msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500214 return nil
215}
216
npujar1d86a522019-11-14 17:11:16 +0530217// Download_image -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000218func (ta *Adapter) Download_image(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 return nil, nil
220}
221
npujar1d86a522019-11-14 17:11:16 +0530222// Get_image_download_status -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000223func (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 -0500224 return nil, nil
225}
226
npujar1d86a522019-11-14 17:11:16 +0530227// Cancel_image_download -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000228func (ta *Adapter) Cancel_image_download(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// Activate_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000233func (ta *Adapter) Activate_image_update(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// Revert_image_update -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000238func (ta *Adapter) Revert_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}
kesavandbc2d1622020-01-21 00:42:01 -0500241
242// Enable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000243func (ta *Adapter) Enable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500244 return nil
245}
246
247// Disable_port -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000248func (ta *Adapter) Disable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500249 return nil
250}
Scott Baker0e78ba22020-02-24 17:58:47 -0800251
252// Child_device_lost -
Rohan Agrawal31f21802020-06-12 05:38:46 +0000253func (ta *Adapter) Child_device_lost(ctx context.Context, pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint
Scott Baker0e78ba22020-02-24 17:58:47 -0800254 return nil
255}
Scott Baker432f9be2020-03-26 11:56:30 -0700256
257// Start_omci_test
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258func (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 -0700259 return nil, nil
260}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800261
Rohan Agrawal31f21802020-06-12 05:38:46 +0000262func (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 -0800263 return nil, nil
264}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400265
266// GetFlowCount returns the total number of flows presently under this adapter
267func (ta *Adapter) GetFlowCount() int {
268 ta.flowLock.RLock()
269 defer ta.flowLock.RUnlock()
270
271 return len(ta.flows)
272}
273
274// ClearFlows removes all flows in this adapter
275func (ta *Adapter) ClearFlows() {
276 ta.flowLock.Lock()
277 defer ta.flowLock.Unlock()
278
279 ta.flows = map[uint64]*voltha.OfpFlowStats{}
280}
281
282// SetFlowAction sets the adapter action on addition and deletion of flows
283func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) {
284 ta.failFlowAdd = failFlowAdd
285 ta.failFlowDelete = failFlowDelete
286}