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" |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame^] | 21 | "errors" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 22 | "fmt" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 23 | "strings" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 24 | "sync" |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 25 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 26 | "github.com/gogo/protobuf/proto" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 28 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 29 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 30 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 31 | of "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 32 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 33 | ) |
| 34 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 35 | // ONUAdapter represent ONU adapter attributes |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 36 | type ONUAdapter struct { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 37 | flows map[uint64]*voltha.OfpFlowStats |
| 38 | lock sync.Mutex |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 39 | Adapter |
| 40 | } |
| 41 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 42 | // NewONUAdapter creates ONU adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 43 | func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 44 | return &ONUAdapter{ |
| 45 | flows: map[uint64]*voltha.OfpFlowStats{}, |
| 46 | Adapter: Adapter{ |
| 47 | coreProxy: cp, |
| 48 | }, |
| 49 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 50 | } |
| 51 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 52 | // Adopt_device creates new handler for added device |
| 53 | func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 54 | go func() { |
| 55 | d := proto.Clone(device).(*voltha.Device) |
| 56 | d.Root = false |
| 57 | d.Vendor = "onu_adapter_mock" |
| 58 | d.Model = "go-mock" |
| 59 | d.SerialNumber = com.GetRandomSerialNumber() |
| 60 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 61 | onuA.storeDevice(d) |
| 62 | if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 63 | logger.Fatalf("deviceUpdate-failed-%s", res) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 64 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 65 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 66 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 67 | d.OperStatus = voltha.OperStatus_DISCOVERED |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 68 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 69 | 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] | 70 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 71 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 72 | |
| 73 | uniPortNo := uint32(2) |
| 74 | if device.ProxyAddress != nil { |
| 75 | if device.ProxyAddress.ChannelId != 0 { |
| 76 | uniPortNo = device.ProxyAddress.ChannelId |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | uniPort := &voltha.Port{ |
| 81 | PortNo: uniPortNo, |
| 82 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 83 | Type: voltha.Port_ETHERNET_UNI, |
| 84 | OperStatus: voltha.OperStatus_ACTIVE, |
| 85 | } |
| 86 | var err error |
| 87 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 88 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | ponPortNo := uint32(1) |
| 92 | if device.ParentPortNo != 0 { |
| 93 | ponPortNo = device.ParentPortNo |
| 94 | } |
| 95 | |
| 96 | ponPort := &voltha.Port{ |
| 97 | PortNo: ponPortNo, |
| 98 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 99 | Type: voltha.Port_PON_ONU, |
| 100 | OperStatus: voltha.OperStatus_ACTIVE, |
| 101 | Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 102 | PortNo: device.ParentPortNo}}, // Peer port is parent's port number |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 103 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 104 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 105 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 109 | d.OperStatus = voltha.OperStatus_ACTIVE |
| 110 | |
| 111 | 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] | 112 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 113 | } |
| 114 | //Get the latest device data from the Core |
| 115 | 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] | 116 | logger.Fatalf("getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if err = onuA.updateDevice(d); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 120 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 121 | } |
| 122 | }() |
| 123 | return nil |
| 124 | } |
| 125 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 126 | // Get_ofp_port_info returns ofp device info |
| 127 | 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] | 128 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 129 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 130 | } |
| 131 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
| 132 | return &ic.PortCapability{ |
| 133 | Port: &voltha.LogicalPort{ |
| 134 | OfpPort: &of.OfpPort{ |
| 135 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 136 | Config: 0, |
| 137 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 138 | Curr: capability, |
| 139 | Advertised: capability, |
| 140 | Peer: capability, |
| 141 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 142 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 143 | }, |
| 144 | DeviceId: device.Id, |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 145 | DevicePortNo: uint32(portNo), |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 146 | }, |
| 147 | }, nil |
| 148 | } |
| 149 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 150 | // Disable_device disables device |
| 151 | func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 152 | go func() { |
| 153 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 154 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 155 | } |
| 156 | cloned := proto.Clone(device).(*voltha.Device) |
| 157 | // Update the all ports state on that device to disable |
| 158 | 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] | 159 | // Device may also have been deleted in the Core |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 160 | logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 161 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 162 | } |
| 163 | //Update the device state |
| 164 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 165 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 166 | |
| 167 | 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] | 168 | logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 169 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 170 | } |
| 171 | if err := onuA.updateDevice(cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 172 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 173 | } |
| 174 | }() |
| 175 | return nil |
| 176 | } |
| 177 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 178 | // Reenable_device reenables device |
| 179 | func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 180 | go func() { |
| 181 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 182 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | cloned := proto.Clone(device).(*voltha.Device) |
| 186 | // Update the all ports state on that device to enable |
| 187 | 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] | 188 | logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | //Update the device state |
| 192 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 193 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 194 | |
| 195 | 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] | 196 | logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 197 | } |
| 198 | if err := onuA.updateDevice(cloned); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 199 | logger.Fatalf("saving-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 200 | } |
| 201 | }() |
| 202 | return nil |
| 203 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 204 | |
| 205 | // Update_flows_incrementally mocks the incremental flow update |
| 206 | func (onuA *ONUAdapter) Update_flows_incrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error { // nolint |
| 207 | onuA.lock.Lock() |
| 208 | defer onuA.lock.Unlock() |
| 209 | |
| 210 | if flows.ToAdd != nil { |
| 211 | for _, f := range flows.ToAdd.Items { |
| 212 | onuA.flows[f.Id] = f |
| 213 | } |
| 214 | } |
| 215 | if flows.ToRemove != nil { |
| 216 | for _, f := range flows.ToRemove.Items { |
| 217 | delete(onuA.flows, f.Id) |
| 218 | } |
| 219 | } |
| 220 | return nil |
| 221 | } |
| 222 | |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 223 | // Start_omci_test begins an omci self-test |
| 224 | func (onuA *ONUAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint |
| 225 | _ = device |
| 226 | return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil |
| 227 | } |
| 228 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 229 | // GetFlowCount returns the total number of flows presently under this adapter |
| 230 | func (onuA *ONUAdapter) GetFlowCount() int { |
| 231 | onuA.lock.Lock() |
| 232 | defer onuA.lock.Unlock() |
| 233 | |
| 234 | return len(onuA.flows) |
| 235 | } |
| 236 | |
| 237 | // ClearFlows removes all flows in this adapter |
| 238 | func (onuA *ONUAdapter) ClearFlows() { |
| 239 | onuA.lock.Lock() |
| 240 | defer onuA.lock.Unlock() |
| 241 | |
| 242 | onuA.flows = map[uint64]*voltha.OfpFlowStats{} |
| 243 | } |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame^] | 244 | |
| 245 | func (onuA *ONUAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint |
| 246 | _ = deviceId |
| 247 | _ = device |
| 248 | _ = valueflag |
| 249 | return nil, errors.New("get-ext-value-not-implemented") |
| 250 | } |