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