blob: 7509f1f42700f1730b542b9498a2fcf3d3d0f9ee [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// Process_inter_adapter_message -
212func (ta *Adapter) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500213 return nil
214}
215
npujar1d86a522019-11-14 17:11:16 +0530216// Download_image -
217func (ta *Adapter) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500218 return nil, nil
219}
220
npujar1d86a522019-11-14 17:11:16 +0530221// Get_image_download_status -
222func (ta *Adapter) Get_image_download_status(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// Cancel_image_download -
227func (ta *Adapter) Cancel_image_download(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// Activate_image_update -
232func (ta *Adapter) Activate_image_update(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// Revert_image_update -
237func (ta *Adapter) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint
khenaidooab1f7bd2019-11-14 14:00:27 -0500238 return nil, nil
239}
kesavandbc2d1622020-01-21 00:42:01 -0500240
241// Enable_port -
242func (ta *Adapter) Enable_port(deviceId string, port *voltha.Port) error { //nolint
243 return nil
244}
245
246// Disable_port -
247func (ta *Adapter) Disable_port(deviceId string, port *voltha.Port) error { //nolint
248 return nil
249}
Scott Baker0e78ba22020-02-24 17:58:47 -0800250
251// Child_device_lost -
252func (ta *Adapter) Child_device_lost(pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint
253 return nil
254}
Scott Baker432f9be2020-03-26 11:56:30 -0700255
256// Start_omci_test
257func (ta *Adapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { //nolint
258 return nil, nil
259}
Dinesh Belwalkarc1129f12020-02-27 10:41:33 -0800260
261func (ta *Adapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { //nolint
262 return nil, nil
263}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400264
265// GetFlowCount returns the total number of flows presently under this adapter
266func (ta *Adapter) GetFlowCount() int {
267 ta.flowLock.RLock()
268 defer ta.flowLock.RUnlock()
269
270 return len(ta.flows)
271}
272
273// ClearFlows removes all flows in this adapter
274func (ta *Adapter) ClearFlows() {
275 ta.flowLock.Lock()
276 defer ta.flowLock.Unlock()
277
278 ta.flows = map[uint64]*voltha.OfpFlowStats{}
279}
280
281// SetFlowAction sets the adapter action on addition and deletion of flows
282func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) {
283 ta.failFlowAdd = failFlowAdd
284 ta.failFlowDelete = failFlowDelete
285}