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