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 ( |
| 20 | "context" |
| 21 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 22 | "strings" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 23 | "sync" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 24 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 25 | "github.com/gogo/protobuf/proto" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 26 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 27 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 28 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 29 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 30 | of "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 31 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | const ( |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 35 | numONUPerOLT = 4 |
| 36 | startingUNIPortNo = 100 |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 37 | ) |
| 38 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 39 | // OLTAdapter represent OLT adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 40 | type OLTAdapter struct { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 41 | flows map[uint64]*voltha.OfpFlowStats |
| 42 | lock sync.Mutex |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 43 | Adapter |
| 44 | } |
| 45 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 46 | // NewOLTAdapter - creates OLT adapter instance |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 47 | func NewOLTAdapter(cp adapterif.CoreProxy) *OLTAdapter { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 48 | return &OLTAdapter{ |
| 49 | flows: map[uint64]*voltha.OfpFlowStats{}, |
| 50 | Adapter: Adapter{ |
| 51 | coreProxy: cp, |
| 52 | }, |
| 53 | } |
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 | // Adopt_device creates new handler for added device |
| 57 | func (oltA *OLTAdapter) Adopt_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 58 | go func() { |
| 59 | d := proto.Clone(device).(*voltha.Device) |
| 60 | d.Root = true |
| 61 | d.Vendor = "olt_adapter_mock" |
| 62 | d.Model = "go-mock" |
| 63 | d.SerialNumber = com.GetRandomSerialNumber() |
| 64 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 65 | oltA.storeDevice(d) |
| 66 | if res := oltA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil { |
| 67 | log.Fatalf("deviceUpdate-failed-%s", res) |
| 68 | } |
| 69 | nniPort := &voltha.Port{ |
| 70 | PortNo: 2, |
| 71 | Label: fmt.Sprintf("nni-%d", 2), |
| 72 | Type: voltha.Port_ETHERNET_NNI, |
| 73 | OperStatus: voltha.OperStatus_ACTIVE, |
| 74 | } |
| 75 | var err error |
| 76 | if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, nniPort); err != nil { |
| 77 | log.Fatalf("PortCreated-failed-%s", err) |
| 78 | } |
| 79 | |
| 80 | ponPort := &voltha.Port{ |
| 81 | PortNo: 1, |
| 82 | Label: fmt.Sprintf("pon-%d", 1), |
| 83 | Type: voltha.Port_PON_OLT, |
| 84 | OperStatus: voltha.OperStatus_ACTIVE, |
| 85 | } |
| 86 | if err = oltA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil { |
| 87 | log.Fatalf("PortCreated-failed-%s", err) |
| 88 | } |
| 89 | |
| 90 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 91 | d.OperStatus = voltha.OperStatus_ACTIVE |
| 92 | |
| 93 | if err = oltA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil { |
| 94 | log.Fatalf("Device-state-update-failed-%s", err) |
| 95 | } |
| 96 | |
| 97 | //Get the latest device data from the Core |
| 98 | if d, err = oltA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil { |
| 99 | log.Fatalf("getting-device-failed-%s", err) |
| 100 | } |
| 101 | |
| 102 | if err = oltA.updateDevice(d); err != nil { |
| 103 | log.Fatalf("saving-device-failed-%s", err) |
| 104 | } |
| 105 | |
| 106 | // Register Child devices |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 107 | initialUniPortNo := startingUNIPortNo |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 108 | for i := 0; i < numONUPerOLT; i++ { |
| 109 | go func(seqNo int) { |
| 110 | if _, err := oltA.coreProxy.ChildDeviceDetected( |
| 111 | context.TODO(), |
| 112 | d.Id, |
| 113 | 1, |
| 114 | "onu_adapter_mock", |
| 115 | initialUniPortNo+seqNo, |
| 116 | "onu_adapter_mock", |
| 117 | com.GetRandomSerialNumber(), |
| 118 | int64(seqNo)); err != nil { |
| 119 | log.Fatalf("failure-sending-child-device-%s", err) |
| 120 | } |
| 121 | }(i) |
| 122 | } |
| 123 | }() |
| 124 | return nil |
| 125 | } |
| 126 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 127 | // Get_ofp_device_info returns ofp device info |
| 128 | func (oltA *OLTAdapter) Get_ofp_device_info(device *voltha.Device) (*ic.SwitchCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 129 | if d := oltA.getDevice(device.Id); d == nil { |
| 130 | log.Fatalf("device-not-found-%s", device.Id) |
| 131 | } |
| 132 | return &ic.SwitchCapability{ |
| 133 | Desc: &of.OfpDesc{ |
| 134 | HwDesc: "olt_adapter_mock", |
| 135 | SwDesc: "olt_adapter_mock", |
| 136 | SerialNum: "12345678", |
| 137 | }, |
| 138 | SwitchFeatures: &of.OfpSwitchFeatures{ |
| 139 | NBuffers: 256, |
| 140 | NTables: 2, |
| 141 | Capabilities: uint32(of.OfpCapabilities_OFPC_FLOW_STATS | |
| 142 | of.OfpCapabilities_OFPC_TABLE_STATS | |
| 143 | of.OfpCapabilities_OFPC_PORT_STATS | |
| 144 | of.OfpCapabilities_OFPC_GROUP_STATS), |
| 145 | }, |
| 146 | }, nil |
| 147 | } |
| 148 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 149 | // Get_ofp_port_info returns ofp port info |
| 150 | func (oltA *OLTAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 151 | if d := oltA.getDevice(device.Id); d == nil { |
| 152 | log.Fatalf("device-not-found-%s", device.Id) |
| 153 | } |
| 154 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 155 | return &ic.PortCapability{ |
| 156 | Port: &voltha.LogicalPort{ |
| 157 | OfpPort: &of.OfpPort{ |
| 158 | HwAddr: macAddressToUint32Array("11:22:33:44:55:66"), |
| 159 | Config: 0, |
| 160 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 161 | Curr: capability, |
| 162 | Advertised: capability, |
| 163 | Peer: capability, |
| 164 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 165 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 166 | }, |
| 167 | DeviceId: device.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 168 | DevicePortNo: uint32(portNo), |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 169 | }, |
| 170 | }, nil |
| 171 | } |
| 172 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 173 | // GetNumONUPerOLT returns number of ONUs per OLT |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 174 | func (oltA *OLTAdapter) GetNumONUPerOLT() int { |
| 175 | return numONUPerOLT |
| 176 | } |
| 177 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 178 | // Returns the starting UNI port number |
| 179 | func (oltA *OLTAdapter) GetStartingUNIPortNo() int { |
| 180 | return startingUNIPortNo |
| 181 | } |
| 182 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 183 | // Disable_device disables device |
| 184 | func (oltA *OLTAdapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 185 | go func() { |
| 186 | if d := oltA.getDevice(device.Id); d == nil { |
| 187 | log.Fatalf("device-not-found-%s", device.Id) |
| 188 | } |
| 189 | |
| 190 | cloned := proto.Clone(device).(*voltha.Device) |
| 191 | // Update the all ports state on that device to disable |
| 192 | if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame^] | 193 | log.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | //Update the device state |
| 197 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 198 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 199 | |
| 200 | if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame^] | 201 | // Device may already have been deleted in the core |
| 202 | log.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 203 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | if err := oltA.updateDevice(cloned); err != nil { |
| 207 | log.Fatalf("saving-device-failed-%s", err) |
| 208 | } |
| 209 | |
| 210 | // Tell the Core that all child devices have been disabled (by default it's an action already taken by the Core |
| 211 | if err := oltA.coreProxy.ChildDevicesLost(context.TODO(), cloned.Id); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame^] | 212 | // Device may already have been deleted in the core |
| 213 | log.Warnw("lost-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 214 | } |
| 215 | }() |
| 216 | return nil |
| 217 | } |
| 218 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 219 | // Reenable_device reenables device |
| 220 | func (oltA *OLTAdapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 221 | go func() { |
| 222 | if d := oltA.getDevice(device.Id); d == nil { |
| 223 | log.Fatalf("device-not-found-%s", device.Id) |
| 224 | } |
| 225 | |
| 226 | cloned := proto.Clone(device).(*voltha.Device) |
| 227 | // Update the all ports state on that device to enable |
| 228 | if err := oltA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 229 | log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 230 | } |
| 231 | |
| 232 | //Update the device state |
| 233 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 234 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 235 | |
| 236 | if err := oltA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 237 | log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 238 | } |
| 239 | |
| 240 | // Tell the Core that all child devices have been enabled |
| 241 | if err := oltA.coreProxy.ChildDevicesDetected(context.TODO(), cloned.Id); err != nil { |
| 242 | log.Fatalf("detection-notif-of-child-devices-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 243 | } |
| 244 | }() |
| 245 | return nil |
| 246 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 247 | |
| 248 | // Enable_port - |
| 249 | func (oltA *OLTAdapter) Enable_port(deviceId string, Port *voltha.Port) error { //nolint |
| 250 | go func() { |
| 251 | |
| 252 | if Port.Type == voltha.Port_PON_OLT { |
| 253 | if err := oltA.coreProxy.PortStateUpdate(context.TODO(), deviceId, voltha.Port_PON_OLT, Port.PortNo, voltha.OperStatus_ACTIVE); err != nil { |
| 254 | log.Fatalf("updating-ports-failed", log.Fields{"device-id": deviceId, "error": err}) |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | }() |
| 259 | return nil |
| 260 | } |
| 261 | |
| 262 | // Disable_port - |
| 263 | func (oltA *OLTAdapter) Disable_port(deviceId string, Port *voltha.Port) error { //nolint |
| 264 | go func() { |
| 265 | |
| 266 | if Port.Type == voltha.Port_PON_OLT { |
| 267 | if err := oltA.coreProxy.PortStateUpdate(context.TODO(), deviceId, voltha.Port_PON_OLT, Port.PortNo, voltha.OperStatus_DISCOVERED); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame^] | 268 | // Corresponding device may have been deleted |
| 269 | log.Warnw("updating-ports-failed", log.Fields{"device-id": deviceId, "error": err}) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | }() |
| 273 | return nil |
| 274 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 275 | |
| 276 | // Child_device_lost deletes ONU and its references |
| 277 | func (oltA *OLTAdapter) Child_device_lost(deviceID string, pPortNo uint32, onuID uint32) error { // nolint |
| 278 | return nil |
| 279 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 280 | |
| 281 | // Update_flows_incrementally mocks the incremental flow update |
| 282 | func (oltA *OLTAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint |
| 283 | oltA.lock.Lock() |
| 284 | defer oltA.lock.Unlock() |
| 285 | |
| 286 | if flows.ToAdd != nil { |
| 287 | for _, f := range flows.ToAdd.Items { |
| 288 | oltA.flows[f.Id] = f |
| 289 | } |
| 290 | } |
| 291 | if flows.ToRemove != nil { |
| 292 | for _, f := range flows.ToRemove.Items { |
| 293 | delete(oltA.flows, f.Id) |
| 294 | } |
| 295 | } |
| 296 | return nil |
| 297 | } |
| 298 | |
| 299 | // GetFlowCount returns the total number of flows presently under this adapter |
| 300 | func (oltA *OLTAdapter) GetFlowCount() int { |
| 301 | oltA.lock.Lock() |
| 302 | defer oltA.lock.Unlock() |
| 303 | |
| 304 | return len(oltA.flows) |
| 305 | } |
| 306 | |
| 307 | // ClearFlows removes all flows in this adapter |
| 308 | func (oltA *OLTAdapter) ClearFlows() { |
| 309 | oltA.lock.Lock() |
| 310 | defer oltA.lock.Unlock() |
| 311 | |
| 312 | oltA.flows = map[uint64]*voltha.OfpFlowStats{} |
| 313 | } |