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" |
| 23 | "github.com/gogo/protobuf/proto" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 24 | "github.com/opencord/voltha-lib-go/v3/pkg/adapters/adapterif" |
| 25 | com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common" |
| 26 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 27 | ic "github.com/opencord/voltha-protos/v3/go/inter_container" |
| 28 | of "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 29 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 30 | "strings" |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 31 | ) |
| 32 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 33 | // ONUAdapter represent ONU adapter attributes |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 34 | type ONUAdapter struct { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 35 | *Adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 36 | } |
| 37 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 38 | // NewONUAdapter creates ONU adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 39 | func NewONUAdapter(cp adapterif.CoreProxy) *ONUAdapter { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 40 | return &ONUAdapter{ |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 41 | Adapter: NewAdapter(cp), |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 42 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 43 | } |
| 44 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 45 | // Adopt_device creates new handler for added device |
| 46 | func (onuA *ONUAdapter) Adopt_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 47 | go func() { |
| 48 | d := proto.Clone(device).(*voltha.Device) |
| 49 | d.Root = false |
| 50 | d.Vendor = "onu_adapter_mock" |
| 51 | d.Model = "go-mock" |
| 52 | d.SerialNumber = com.GetRandomSerialNumber() |
| 53 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 54 | onuA.storeDevice(d) |
| 55 | if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 56 | logger.Fatalf("deviceUpdate-failed-%s", res) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 57 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 58 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 59 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 60 | d.OperStatus = voltha.OperStatus_DISCOVERED |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 61 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 62 | 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] | 63 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 64 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 65 | |
| 66 | uniPortNo := uint32(2) |
| 67 | if device.ProxyAddress != nil { |
| 68 | if device.ProxyAddress.ChannelId != 0 { |
| 69 | uniPortNo = device.ProxyAddress.ChannelId |
| 70 | } |
| 71 | } |
| 72 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 73 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 74 | uniPort := &voltha.Port{ |
| 75 | PortNo: uniPortNo, |
| 76 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 77 | Type: voltha.Port_ETHERNET_UNI, |
| 78 | OperStatus: voltha.OperStatus_ACTIVE, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 79 | OfpPort: &of.OfpPort{ |
| 80 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 81 | Config: 0, |
| 82 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 83 | Curr: capability, |
| 84 | Advertised: capability, |
| 85 | Peer: capability, |
| 86 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 87 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 88 | }, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 89 | } |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 90 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 91 | var err error |
| 92 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 93 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | ponPortNo := uint32(1) |
| 97 | if device.ParentPortNo != 0 { |
| 98 | ponPortNo = device.ParentPortNo |
| 99 | } |
| 100 | |
| 101 | ponPort := &voltha.Port{ |
| 102 | PortNo: ponPortNo, |
| 103 | Label: fmt.Sprintf("pon-%d", ponPortNo), |
| 104 | Type: voltha.Port_PON_ONU, |
| 105 | OperStatus: voltha.OperStatus_ACTIVE, |
| 106 | Peers: []*voltha.Port_PeerPort{{DeviceId: d.ParentId, // Peer device is OLT |
khenaidoo | 6e55d9e | 2019-12-12 18:26:26 -0500 | [diff] [blame] | 107 | PortNo: device.ParentPortNo}}, // Peer port is parent's port number |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 108 | } |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 109 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, ponPort); err != nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 110 | logger.Fatalf("PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 114 | d.OperStatus = voltha.OperStatus_ACTIVE |
| 115 | |
| 116 | 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] | 117 | logger.Fatalf("device-state-update-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 118 | } |
| 119 | //Get the latest device data from the Core |
| 120 | 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] | 121 | logger.Fatalf("getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 122 | } |
| 123 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 124 | onuA.updateDevice(d) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 125 | }() |
| 126 | return nil |
| 127 | } |
| 128 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 129 | // Disable_device disables device |
| 130 | func (onuA *ONUAdapter) Disable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 131 | go func() { |
| 132 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 133 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 134 | } |
| 135 | cloned := proto.Clone(device).(*voltha.Device) |
| 136 | // Update the all ports state on that device to disable |
| 137 | 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] | 138 | // Device may also have been deleted in the Core |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 139 | logger.Warnw("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 140 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 141 | } |
| 142 | //Update the device state |
| 143 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 144 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 145 | |
| 146 | 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] | 147 | logger.Warnw("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 148 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 149 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 150 | onuA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 151 | }() |
| 152 | return nil |
| 153 | } |
| 154 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 155 | // Reenable_device reenables device |
| 156 | func (onuA *ONUAdapter) Reenable_device(device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 157 | go func() { |
| 158 | if d := onuA.getDevice(device.Id); d == nil { |
Girish Kumar | f56a468 | 2020-03-20 20:07:46 +0000 | [diff] [blame] | 159 | logger.Fatalf("device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | cloned := proto.Clone(device).(*voltha.Device) |
| 163 | // Update the all ports state on that device to enable |
| 164 | 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] | 165 | logger.Fatalf("updating-ports-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | //Update the device state |
| 169 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 170 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 171 | |
| 172 | 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] | 173 | logger.Fatalf("device-state-update-failed", log.Fields{"deviceId": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 174 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 175 | |
| 176 | onuA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 177 | }() |
| 178 | return nil |
| 179 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 180 | |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 181 | // Start_omci_test begins an omci self-test |
| 182 | func (onuA *ONUAdapter) Start_omci_test(device *voltha.Device, request *voltha.OmciTestRequest) (*ic.TestResponse, error) { // nolint |
| 183 | _ = device |
| 184 | return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil |
| 185 | } |
| 186 | |
Dinesh Belwalkar | c1129f1 | 2020-02-27 10:41:33 -0800 | [diff] [blame] | 187 | func (onuA *ONUAdapter) Get_ext_value(deviceId string, device *voltha.Device, valueflag voltha.ValueType_Type) (*voltha.ReturnValues, error) { // nolint |
| 188 | _ = deviceId |
| 189 | _ = device |
| 190 | _ = valueflag |
| 191 | return nil, errors.New("get-ext-value-not-implemented") |
| 192 | } |