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 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 34 | // ONUAdapter represent ONU adapter attributes |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 35 | type ONUAdapter struct { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame^] | 36 | flows map[uint64]*voltha.OfpFlowStats |
| 37 | lock sync.Mutex |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 38 | Adapter |
| 39 | } |
| 40 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 41 | // NewONUAdapter creates ONU adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 42 | func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame^] | 43 | return &ONUAdapter{ |
| 44 | flows: map[uint64]*voltha.OfpFlowStats{}, |
| 45 | Adapter: Adapter{ |
| 46 | coreProxy: cp, |
| 47 | }, |
| 48 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 49 | } |
| 50 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 51 | // Adopt_device creates new handler for added device |
| 52 | func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 53 | go func() { |
| 54 | d := proto.Clone(device).(*voltha.Device) |
| 55 | d.Root = false |
| 56 | d.Vendor = "onu_adapter_mock" |
| 57 | d.Model = "go-mock" |
| 58 | d.SerialNumber = com.GetRandomSerialNumber() |
| 59 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 60 | onuA.storeDevice(d) |
| 61 | if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil { |
| 62 | log.Fatalf("deviceUpdate-failed-%s", res) |
| 63 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 64 | |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 65 | // Updating the device states twice, once with oper status to discovered and followed by active may cause |
| 66 | // a failure for unit tests when these requests reaches the Core within a millisecond of each other (with real |
| 67 | // hardware will not happen as the time between these requests is much higher than 1 millisecond). For |
| 68 | // some reasons this issue is seen on Jenkins but not when running the tests locally. The issue |
| 69 | // in the core is triggered when these requests are processed out of order (an issue in the Core that is |
| 70 | // being handled by https://jira.opencord.org/browse/VOL-2164). |
| 71 | // TODO: Once the above change is completed then this code can be uncommented. |
| 72 | |
| 73 | //d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 74 | //d.OperStatus = voltha.OperStatus_DISCOVERED |
| 75 | |
| 76 | //if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil { |
| 77 | // log.Fatalf("device-state-update-failed-%s", err) |
| 78 | //} |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 79 | |
| 80 | uniPortNo := uint32(2) |
| 81 | if device.ProxyAddress != nil { |
| 82 | if device.ProxyAddress.ChannelId != 0 { |
| 83 | uniPortNo = device.ProxyAddress.ChannelId |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | uniPort := &voltha.Port{ |
| 88 | PortNo: uniPortNo, |
| 89 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 90 | Type: voltha.Port_ETHERNET_UNI, |
| 91 | OperStatus: voltha.OperStatus_ACTIVE, |
| 92 | } |
| 93 | var err error |
| 94 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil { |
| 95 | log.Fatalf("PortCreated-failed-%s", err) |
| 96 | } |
| 97 | |
| 98 | ponPortNo := uint32(1) |
| 99 | if device.ParentPortNo != 0 { |
| 100 | ponPortNo = device.ParentPortNo |
| 101 | } |
| 102 | |
| 103 | ponPort := &voltha.Port{ |
| 104 | PortNo: ponPortNo, |
| 105 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 106 | Type: voltha.Port_PON_ONU, |
| 107 | OperStatus: voltha.OperStatus_ACTIVE, |
| 108 | Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 109 | PortNo: device.ParentPortNo}}, // Peer port is parent's port number |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 110 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 111 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil { |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 112 | log.Fatalf("PortCreated-failed-%s", err) |
| 113 | } |
| 114 | |
| 115 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 116 | d.OperStatus = voltha.OperStatus_ACTIVE |
| 117 | |
| 118 | if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil { |
| 119 | log.Fatalf("device-state-update-failed-%s", err) |
| 120 | } |
| 121 | //Get the latest device data from the Core |
| 122 | if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil { |
| 123 | log.Fatalf("getting-device-failed-%s", err) |
| 124 | } |
| 125 | |
| 126 | if err = onuA.updateDevice(d); err != nil { |
| 127 | log.Fatalf("saving-device-failed-%s", err) |
| 128 | } |
| 129 | }() |
| 130 | return nil |
| 131 | } |
| 132 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 133 | // Get_ofp_port_info returns ofp device info |
| 134 | func (onuA *ONUAdapter) Get_ofp_port_info(device *voltha.Device, portNo int64) (*ic.PortCapability, error) { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 135 | if d := onuA.getDevice(device.Id); d == nil { |
| 136 | log.Fatalf("device-not-found-%s", device.Id) |
| 137 | } |
| 138 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 139 | return &ic.PortCapability{ |
| 140 | Port: &voltha.LogicalPort{ |
| 141 | OfpPort: &of.OfpPort{ |
| 142 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 143 | Config: 0, |
| 144 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 145 | Curr: capability, |
| 146 | Advertised: capability, |
| 147 | Peer: capability, |
| 148 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 149 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 150 | }, |
| 151 | DeviceId: device.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 152 | DevicePortNo: uint32(portNo), |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 153 | }, |
| 154 | }, nil |
| 155 | } |
| 156 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 157 | // Disable_device disables device |
| 158 | func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 159 | go func() { |
| 160 | if d := onuA.getDevice(device.Id); d == nil { |
| 161 | log.Fatalf("device-not-found-%s", device.Id) |
| 162 | } |
| 163 | cloned := proto.Clone(device).(*voltha.Device) |
| 164 | // Update the all ports state on that device to disable |
| 165 | if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
| 166 | log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 167 | } |
| 168 | //Update the device state |
| 169 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 170 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 171 | |
| 172 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 173 | log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 174 | } |
| 175 | if err := onuA.updateDevice(cloned); err != nil { |
| 176 | log.Fatalf("saving-device-failed-%s", err) |
| 177 | } |
| 178 | }() |
| 179 | return nil |
| 180 | } |
| 181 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 182 | // Reenable_device reenables device |
| 183 | func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 184 | go func() { |
| 185 | if d := onuA.getDevice(device.Id); d == nil { |
| 186 | log.Fatalf("device-not-found-%s", device.Id) |
| 187 | } |
| 188 | |
| 189 | cloned := proto.Clone(device).(*voltha.Device) |
| 190 | // Update the all ports state on that device to enable |
| 191 | if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
| 192 | log.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 193 | } |
| 194 | |
| 195 | //Update the device state |
| 196 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 197 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 198 | |
| 199 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
| 200 | log.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
| 201 | } |
| 202 | if err := onuA.updateDevice(cloned); err != nil { |
| 203 | log.Fatalf("saving-device-failed-%s", err) |
| 204 | } |
| 205 | }() |
| 206 | return nil |
| 207 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame^] | 208 | |
| 209 | // Update_flows_incrementally mocks the incremental flow update |
| 210 | func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint |
| 211 | onuA.lock.Lock() |
| 212 | defer onuA.lock.Unlock() |
| 213 | |
| 214 | if flows.ToAdd != nil { |
| 215 | for _, f := range flows.ToAdd.Items { |
| 216 | onuA.flows[f.Id] = f |
| 217 | } |
| 218 | } |
| 219 | if flows.ToRemove != nil { |
| 220 | for _, f := range flows.ToRemove.Items { |
| 221 | delete(onuA.flows, f.Id) |
| 222 | } |
| 223 | } |
| 224 | return nil |
| 225 | } |
| 226 | |
| 227 | // GetFlowCount returns the total number of flows presently under this adapter |
| 228 | func (onuA *ONUAdapter) GetFlowCount() int { |
| 229 | onuA.lock.Lock() |
| 230 | defer onuA.lock.Unlock() |
| 231 | |
| 232 | return len(onuA.flows) |
| 233 | } |
| 234 | |
| 235 | // ClearFlows removes all flows in this adapter |
| 236 | func (onuA *ONUAdapter) ClearFlows() { |
| 237 | onuA.lock.Lock() |
| 238 | defer onuA.lock.Unlock() |
| 239 | |
| 240 | onuA.flows = map[uint64]*voltha.OfpFlowStats{} |
| 241 | } |