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 { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 62 | logger.Fatalf("deviceUpdate-failed-%s", res) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 63 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 64 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 65 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 66 | d.OperStatus = voltha.OperStatus_DISCOVERED |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 67 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 68 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 69 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 70 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 71 | |
| 72 | uniPortNo := uint32(2) |
| 73 | if device.ProxyAddress != nil { |
| 74 | if device.ProxyAddress.ChannelId != 0 { |
| 75 | uniPortNo = device.ProxyAddress.ChannelId |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | uniPort := &voltha.Port{ |
| 80 | PortNo: uniPortNo, |
| 81 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 82 | Type: voltha.Port_ETHERNET_UNI, |
| 83 | OperStatus: voltha.OperStatus_ACTIVE, |
| 84 | } |
| 85 | var err error |
| 86 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 87 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | ponPortNo := uint32(1) |
| 91 | if device.ParentPortNo != 0 { |
| 92 | ponPortNo = device.ParentPortNo |
| 93 | } |
| 94 | |
| 95 | ponPort := &voltha.Port{ |
| 96 | PortNo: ponPortNo, |
| 97 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 98 | Type: voltha.Port_PON_ONU, |
| 99 | OperStatus: voltha.OperStatus_ACTIVE, |
| 100 | Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 101 | PortNo: device.ParentPortNo}}, // Peer port is parent's port number |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 102 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 103 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 104 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 108 | d.OperStatus = voltha.OperStatus_ACTIVE |
| 109 | |
| 110 | if err = onuA.coreProxy.DeviceStateUpdate(context.TODO(), d.Id, d.ConnectStatus, d.OperStatus); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 111 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 112 | } |
| 113 | //Get the latest device data from the Core |
| 114 | if d, err = onuA.coreProxy.GetDevice(context.TODO(), d.Id, d.Id); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 115 | logger.Fatalf("getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | if err = onuA.updateDevice(d); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 119 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 120 | } |
| 121 | }() |
| 122 | return nil |
| 123 | } |
| 124 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 125 | // Get_ofp_port_info returns ofp device info |
| 126 | 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] | 127 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 128 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 129 | } |
| 130 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 131 | return &ic.PortCapability{ |
| 132 | Port: &voltha.LogicalPort{ |
| 133 | OfpPort: &of.OfpPort{ |
| 134 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 135 | Config: 0, |
| 136 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 137 | Curr: capability, |
| 138 | Advertised: capability, |
| 139 | Peer: capability, |
| 140 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 141 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 142 | }, |
| 143 | DeviceId: device.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 144 | DevicePortNo: uint32(portNo), |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 145 | }, |
| 146 | }, nil |
| 147 | } |
| 148 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 149 | // Disable_device disables device |
| 150 | func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 151 | go func() { |
| 152 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 153 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 154 | } |
| 155 | cloned := proto.Clone(device).(*voltha.Device) |
| 156 | // Update the all ports state on that device to disable |
| 157 | if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); err != nil { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 158 | // Device may also have been deleted in the Core |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 159 | logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 160 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 161 | } |
| 162 | //Update the device state |
| 163 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 164 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 165 | |
| 166 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 167 | logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 168 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 169 | } |
| 170 | if err := onuA.updateDevice(cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 171 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 172 | } |
| 173 | }() |
| 174 | return nil |
| 175 | } |
| 176 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 177 | // Reenable_device reenables device |
| 178 | func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 179 | go func() { |
| 180 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 181 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | cloned := proto.Clone(device).(*voltha.Device) |
| 185 | // Update the all ports state on that device to enable |
| 186 | if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 187 | logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | //Update the device state |
| 191 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 192 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 193 | |
| 194 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 195 | logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 196 | } |
| 197 | if err := onuA.updateDevice(cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 198 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 199 | } |
| 200 | }() |
| 201 | return nil |
| 202 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 203 | |
| 204 | // Update_flows_incrementally mocks the incremental flow update |
| 205 | func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint |
| 206 | onuA.lock.Lock() |
| 207 | defer onuA.lock.Unlock() |
| 208 | |
| 209 | if flows.ToAdd != nil { |
| 210 | for _, f := range flows.ToAdd.Items { |
| 211 | onuA.flows[f.Id] = f |
| 212 | } |
| 213 | } |
| 214 | if flows.ToRemove != nil { |
| 215 | for _, f := range flows.ToRemove.Items { |
| 216 | delete(onuA.flows, f.Id) |
| 217 | } |
| 218 | } |
| 219 | return nil |
| 220 | } |
| 221 | |
| 222 | // GetFlowCount returns the total number of flows presently under this adapter |
| 223 | func (onuA *ONUAdapter) GetFlowCount() int { |
| 224 | onuA.lock.Lock() |
| 225 | defer onuA.lock.Unlock() |
| 226 | |
| 227 | return len(onuA.flows) |
| 228 | } |
| 229 | |
| 230 | // ClearFlows removes all flows in this adapter |
| 231 | func (onuA *ONUAdapter) ClearFlows() { |
| 232 | onuA.lock.Lock() |
| 233 | defer onuA.lock.Unlock() |
| 234 | |
| 235 | onuA.flows = map[uint64]*voltha.OfpFlowStats{} |
| 236 | } |