blob: b27c606c01928e7c0dbd9a8a80cd25187cc85ecb [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 (
Neha Sharmabe485932020-05-25 21:52:55 +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
Neha Sharmabe485932020-05-25 21:52:55 +000032func macAddressToUint32Array(ctx context.Context, mac string) []uint32 {
khenaidooab1f7bd2019-11-14 14:00:27 -050033 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
Neha Sharmabe485932020-05-25 21:52:55 +000058func NewAdapter(ctx context.Context, cp adapterif.CoreProxy) *Adapter {
khenaidooab1f7bd2019-11-14 14:00:27 -050059 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
Neha Sharmabe485932020-05-25 21:52:55 +000066func (ta *Adapter) storeDevice(ctx context.Context, 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
Neha Sharmabe485932020-05-25 21:52:55 +000074func (ta *Adapter) getDevice(ctx context.Context, 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
Neha Sharmabe485932020-05-25 21:52:55 +000080func (ta *Adapter) updateDevice(ctx context.Context, d *voltha.Device) {
81 ta.storeDevice(ctx, d)
khenaidooab1f7bd2019-11-14 14:00:27 -050082}
83
npujar1d86a522019-11-14 17:11:16 +053084// Adapter_descriptor -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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
Neha Sharmabe485932020-05-25 21:52:55 +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
154 if flows.ToAdd != nil {
155 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 }
162 if flows.ToRemove != nil {
163 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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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 -
Neha Sharmabe485932020-05-25 21:52:55 +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// Get_ofp_port_info -
Neha Sharmabe485932020-05-25 21:52:55 +0000213func (ta *Adapter) Get_ofp_port_info(ctx context.Context, device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500214 capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
215 return &ic.PortCapability{
216 Port: &voltha.LogicalPort{
217 OfpPort: &of.OfpPort{
Neha Sharmabe485932020-05-25 21:52:55 +0000218 HwAddr: macAddressToUint32Array(ctx, "11:11:33:44:55:66"),
khenaidooab1f7bd2019-11-14 14:00:27 -0500219 Config: 0,
220 State: uint32(of.OfpPortState_OFPPS_LIVE),
221 Curr: capability,
222 Advertised: capability,
223 Peer: capability,
224 CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
225 MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD),
226 },
227 DeviceId: device.Id,
npujar1d86a522019-11-14 17:11:16 +0530228 DevicePortNo: uint32(portNo),
khenaidooab1f7bd2019-11-14 14:00:27 -0500229 },
230 }, nil
231}
232
npujar1d86a522019-11-14 17:11:16 +0530233// Process_inter_adapter_message -
Neha Sharmabe485932020-05-25 21:52:55 +0000234func (ta *Adapter) Process_inter_adapter_message(ctx context.Context, msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500235 return nil
236}
237
npujar1d86a522019-11-14 17:11:16 +0530238// Download_image -
Neha Sharmabe485932020-05-25 21:52:55 +0000239func (ta *Adapter) Download_image(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500240 return nil, nil
241}
242
npujar1d86a522019-11-14 17:11:16 +0530243// Get_image_download_status -
Neha Sharmabe485932020-05-25 21:52:55 +0000244func (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 -0500245 return nil, nil
246}
247
npujar1d86a522019-11-14 17:11:16 +0530248// Cancel_image_download -
Neha Sharmabe485932020-05-25 21:52:55 +0000249func (ta *Adapter) Cancel_image_download(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500250 return nil, nil
251}
252
npujar1d86a522019-11-14 17:11:16 +0530253// Activate_image_update -
Neha Sharmabe485932020-05-25 21:52:55 +0000254func (ta *Adapter) Activate_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500255 return nil, nil
256}
257
npujar1d86a522019-11-14 17:11:16 +0530258// Revert_image_update -
Neha Sharmabe485932020-05-25 21:52:55 +0000259func (ta *Adapter) Revert_image_update(ctx context.Context, device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500260 return nil, nil
261}
kesavandbc2d1622020-01-21 00:42:01 -0500262
263// Enable_port -
Neha Sharmabe485932020-05-25 21:52:55 +0000264func (ta *Adapter) Enable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500265 return nil
266}
267
268// Disable_port -
Neha Sharmabe485932020-05-25 21:52:55 +0000269func (ta *Adapter) Disable_port(ctx context.Context, deviceId string, port *voltha.Port) error { //nolint
kesavandbc2d1622020-01-21 00:42:01 -0500270 return nil
271}
Scott Baker0e78ba22020-02-24 17:58:47 -0800272
273// Child_device_lost -
Neha Sharmabe485932020-05-25 21:52:55 +0000274func (ta *Adapter) Child_device_lost(ctx context.Context, pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint
Scott Baker0e78ba22020-02-24 17:58:47 -0800275 return nil
276}
Scott Baker432f9be2020-03-26 11:56:30 -0700277
278// Start_omci_test
Neha Sharmabe485932020-05-25 21:52:55 +0000279func (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 -0700280 return nil, nil
281}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800282
Neha Sharmabe485932020-05-25 21:52:55 +0000283func (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 -0800284 return nil, nil
285}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400286
287// GetFlowCount returns the total number of flows presently under this adapter
Neha Sharmabe485932020-05-25 21:52:55 +0000288func (ta *Adapter) GetFlowCount(ctx context.Context) int {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400289 ta.flowLock.RLock()
290 defer ta.flowLock.RUnlock()
291
292 return len(ta.flows)
293}
294
295// ClearFlows removes all flows in this adapter
Neha Sharmabe485932020-05-25 21:52:55 +0000296func (ta *Adapter) ClearFlows(ctx context.Context) {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400297 ta.flowLock.Lock()
298 defer ta.flowLock.Unlock()
299
300 ta.flows = map[uint64]*voltha.OfpFlowStats{}
301}
302
303// SetFlowAction sets the adapter action on addition and deletion of flows
Neha Sharmabe485932020-05-25 21:52:55 +0000304func (ta *Adapter) SetFlowAction(ctx context.Context, failFlowAdd, failFlowDelete bool) {
khenaidoo8b4abbf2020-04-24 17:04:30 -0400305 ta.failFlowAdd = failFlowAdd
306 ta.failFlowDelete = failFlowDelete
307}