khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 1 | /* |
| 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 | */ |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 16 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 17 | package mocks |
| 18 | |
| 19 | import ( |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 20 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
| 23 | "sync" |
| 24 | |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 25 | "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" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func 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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 45 | // Adapter represents adapter attributes |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 46 | type Adapter struct { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 47 | 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 |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 54 | } |
| 55 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 56 | // NewAdapter creates adapter instance |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 57 | func NewAdapter(cp adapterif.CoreProxy) *Adapter { |
| 58 | return &Adapter{ |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 59 | flows: map[uint64]*voltha.OfpFlowStats{}, |
| 60 | devices: map[string]*voltha.Device{}, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 61 | coreProxy: cp, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func (ta *Adapter) storeDevice(d *voltha.Device) { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 66 | ta.deviceLock.Lock() |
| 67 | defer ta.deviceLock.Unlock() |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 68 | if d != nil { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 69 | ta.devices[d.Id] = d |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 73 | func (ta *Adapter) getDevice(id string) *voltha.Device { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 74 | ta.deviceLock.RLock() |
| 75 | defer ta.deviceLock.RUnlock() |
| 76 | return ta.devices[id] |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 77 | } |
| 78 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 79 | func (ta *Adapter) updateDevice(d *voltha.Device) { |
| 80 | ta.storeDevice(d) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 81 | } |
| 82 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 83 | // Adapter_descriptor - |
| 84 | func (ta *Adapter) Adapter_descriptor() error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 85 | return nil |
| 86 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 87 | |
| 88 | // Device_types - |
| 89 | func (ta *Adapter) Device_types() (*voltha.DeviceTypes, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 90 | return nil, nil |
| 91 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 92 | |
| 93 | // Health - |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 94 | func (ta *Adapter) Health() (*voltha.HealthStatus, error) { |
| 95 | return nil, nil |
| 96 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 97 | |
| 98 | // Adopt_device - |
| 99 | func (ta *Adapter) Adopt_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 100 | return nil |
| 101 | } |
| 102 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 103 | // Reconcile_device - |
| 104 | func (ta *Adapter) Reconcile_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 105 | return nil |
| 106 | } |
| 107 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 108 | // Abandon_device - |
| 109 | func (ta *Adapter) Abandon_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 110 | return nil |
| 111 | } |
| 112 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 113 | // Disable_device - |
| 114 | func (ta *Adapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 115 | return nil |
| 116 | } |
| 117 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 118 | // Reenable_device - |
| 119 | func (ta *Adapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 120 | return nil |
| 121 | } |
| 122 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 123 | // Reboot_device - |
| 124 | func (ta *Adapter) Reboot_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 125 | return nil |
| 126 | } |
| 127 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 128 | // Self_test_device - |
| 129 | func (ta *Adapter) Self_test_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 130 | return nil |
| 131 | } |
| 132 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 133 | // Delete_device - |
| 134 | func (ta *Adapter) Delete_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 135 | return nil |
| 136 | } |
| 137 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 138 | // Get_device_details - |
| 139 | func (ta *Adapter) Get_device_details(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 140 | return nil |
| 141 | } |
| 142 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 143 | // Update_flows_bulk - |
| 144 | func (ta *Adapter) Update_flows_bulk(device *voltha.Device, flows *voltha.Flows, groups *voltha.FlowGroups, flowMetadata *voltha.FlowMetadata) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 145 | return nil |
| 146 | } |
| 147 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 148 | // Update_flows_incrementally mocks the incremental flow update |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 149 | func (ta *Adapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 150 | ta.flowLock.Lock() |
| 151 | defer ta.flowLock.Unlock() |
| 152 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 153 | if flows.ToAdd != nil && len(flows.ToAdd.Items) > 0 { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 154 | 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 | } |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 161 | if flows.ToRemove != nil && len(flows.ToRemove.Items) > 0 { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 162 | 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 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 169 | return nil |
| 170 | } |
| 171 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 172 | // Update_pm_config - |
| 173 | func (ta *Adapter) Update_pm_config(device *voltha.Device, pmConfigs *voltha.PmConfigs) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 174 | return nil |
| 175 | } |
| 176 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 177 | // Receive_packet_out - |
| 178 | func (ta *Adapter) Receive_packet_out(deviceID string, egressPortNo int, msg *of.OfpPacketOut) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 179 | return nil |
| 180 | } |
| 181 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 182 | // Suppress_event - |
| 183 | func (ta *Adapter) Suppress_event(filter *voltha.EventFilter) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 184 | return nil |
| 185 | } |
| 186 | |
Devmalya Paul | c594bb3 | 2019-11-06 07:34:27 +0000 | [diff] [blame] | 187 | // Unsuppress_event - |
| 188 | func (ta *Adapter) Unsuppress_event(filter *voltha.EventFilter) error { // nolint |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 189 | return nil |
| 190 | } |
| 191 | |
| 192 | // Get_ofp_device_info - |
| 193 | func (ta *Adapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 194 | 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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 211 | // Get_ofp_port_info - |
| 212 | func (ta *Adapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 213 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 214 | return &ic.PortCapability{ |
| 215 | Port: &voltha.LogicalPort{ |
| 216 | OfpPort: &of.OfpPort{ |
| 217 | HwAddr: macAddressToUint32Array("11:11:33:44:55:66"), |
| 218 | Config: 0, |
| 219 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 220 | Curr: capability, |
| 221 | Advertised: capability, |
| 222 | Peer: capability, |
| 223 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 224 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 225 | }, |
| 226 | DeviceId: device.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 227 | DevicePortNo: uint32(portNo), |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 228 | }, |
| 229 | }, nil |
| 230 | } |
| 231 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 232 | // Process_inter_adapter_message - |
| 233 | func (ta *Adapter) Process_inter_adapter_message(msg *ic.InterAdapterMessage) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 234 | return nil |
| 235 | } |
| 236 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 237 | // Download_image - |
| 238 | func (ta *Adapter) Download_image(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 239 | return nil, nil |
| 240 | } |
| 241 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 242 | // Get_image_download_status - |
| 243 | func (ta *Adapter) Get_image_download_status(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 244 | return nil, nil |
| 245 | } |
| 246 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 247 | // Cancel_image_download - |
| 248 | func (ta *Adapter) Cancel_image_download(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 249 | return nil, nil |
| 250 | } |
| 251 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 252 | // Activate_image_update - |
| 253 | func (ta *Adapter) Activate_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 254 | return nil, nil |
| 255 | } |
| 256 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 257 | // Revert_image_update - |
| 258 | func (ta *Adapter) Revert_image_update(device *voltha.Device, request *voltha.ImageDownload) (*voltha.ImageDownload, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 259 | return nil, nil |
| 260 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 261 | |
| 262 | // Enable_port - |
| 263 | func (ta *Adapter) Enable_port(deviceId string, port *voltha.Port) error { //nolint |
| 264 | return nil |
| 265 | } |
| 266 | |
| 267 | // Disable_port - |
| 268 | func (ta *Adapter) Disable_port(deviceId string, port *voltha.Port) error { //nolint |
| 269 | return nil |
| 270 | } |
Scott Baker | 0e78ba2 | 2020-02-24 17:58:47 -0800 | [diff] [blame] | 271 | |
| 272 | // Child_device_lost - |
| 273 | func (ta *Adapter) Child_device_lost(pDeviceID string, pPortNo uint32, onuID uint32) error { //nolint |
| 274 | return nil |
| 275 | } |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 276 | |
| 277 | // Start_omci_test |
| 278 | func (ta *Adapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*voltha.TestResponse, error) { //nolint |
| 279 | return nil, nil |
| 280 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 281 | |
| 282 | func (ta *Adapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { //nolint |
| 283 | return nil, nil |
| 284 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 285 | |
| 286 | // GetFlowCount returns the total number of flows presently under this adapter |
| 287 | func (ta *Adapter) GetFlowCount() int { |
| 288 | ta.flowLock.RLock() |
| 289 | defer ta.flowLock.RUnlock() |
| 290 | |
| 291 | return len(ta.flows) |
| 292 | } |
| 293 | |
| 294 | // ClearFlows removes all flows in this adapter |
| 295 | func (ta *Adapter) ClearFlows() { |
| 296 | ta.flowLock.Lock() |
| 297 | defer ta.flowLock.Unlock() |
| 298 | |
| 299 | ta.flows = map[uint64]*voltha.OfpFlowStats{} |
| 300 | } |
| 301 | |
| 302 | // SetFlowAction sets the adapter action on addition and deletion of flows |
| 303 | func (ta *Adapter) SetFlowAction(failFlowAdd, failFlowDelete bool) { |
| 304 | ta.failFlowAdd = failFlowAdd |
| 305 | ta.failFlowDelete = failFlowDelete |
| 306 | } |