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" |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 23 | "github.com/opencord/voltha-protos/v4/go/extension" |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 24 | "strings" |
| 25 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 26 | "github.com/gogo/protobuf/proto" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 27 | "github.com/opencord/voltha-lib-go/v4/pkg/adapters/adapterif" |
| 28 | com "github.com/opencord/voltha-lib-go/v4/pkg/adapters/common" |
| 29 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 30 | ic "github.com/opencord/voltha-protos/v4/go/inter_container" |
| 31 | of "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 32 | "github.com/opencord/voltha-protos/v4/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 | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 37 | *Adapter |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 38 | } |
| 39 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 40 | // NewONUAdapter creates ONU adapter |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 41 | func NewONUAdapter(ctx context.Context, cp adapterif.CoreProxy) *ONUAdapter { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 42 | return &ONUAdapter{ |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 43 | Adapter: NewAdapter(cp), |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 44 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 45 | } |
| 46 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 47 | // Adopt_device creates new handler for added device |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 48 | func (onuA *ONUAdapter) Adopt_device(ctx context.Context, device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 49 | go func() { |
| 50 | d := proto.Clone(device).(*voltha.Device) |
| 51 | d.Root = false |
| 52 | d.Vendor = "onu_adapter_mock" |
| 53 | d.Model = "go-mock" |
| 54 | d.SerialNumber = com.GetRandomSerialNumber() |
| 55 | d.MacAddress = strings.ToUpper(com.GetRandomMacAddress()) |
| 56 | onuA.storeDevice(d) |
| 57 | if res := onuA.coreProxy.DeviceUpdate(context.TODO(), d); res != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 58 | logger.Fatalf(ctx, "deviceUpdate-failed-%s", res) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 59 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 60 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 61 | d.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 62 | d.OperStatus = voltha.OperStatus_DISCOVERED |
khenaidoo | 9318152 | 2020-01-23 12:43:21 -0500 | [diff] [blame] | 63 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 64 | 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] | 65 | logger.Fatalf(ctx, "device-state-update-failed-%s", err) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 66 | } |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 67 | |
| 68 | uniPortNo := uint32(2) |
| 69 | if device.ProxyAddress != nil { |
| 70 | if device.ProxyAddress.ChannelId != 0 { |
| 71 | uniPortNo = device.ProxyAddress.ChannelId |
| 72 | } |
| 73 | } |
| 74 | |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 75 | capability := uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 76 | uniPort := &voltha.Port{ |
| 77 | PortNo: uniPortNo, |
| 78 | Label: fmt.Sprintf("uni-%d", uniPortNo), |
| 79 | Type: voltha.Port_ETHERNET_UNI, |
| 80 | OperStatus: voltha.OperStatus_ACTIVE, |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 81 | OfpPort: &of.OfpPort{ |
| 82 | HwAddr: macAddressToUint32Array("12:12:12:12:12:12"), |
| 83 | Config: 0, |
| 84 | State: uint32(of.OfpPortState_OFPPS_LIVE), |
| 85 | Curr: capability, |
| 86 | Advertised: capability, |
| 87 | Peer: capability, |
| 88 | CurrSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 89 | MaxSpeed: uint32(of.OfpPortFeatures_OFPPF_1GB_FD), |
| 90 | }, |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 91 | } |
khenaidoo | c6c7bda | 2020-06-17 17:20:18 -0400 | [diff] [blame] | 92 | |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 93 | var err error |
| 94 | if err = onuA.coreProxy.PortCreated(context.TODO(), d.Id, uniPort); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 95 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 112 | logger.Fatalf(ctx, "PortCreated-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 119 | logger.Fatalf(ctx, "device-state-update-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 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 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 123 | logger.Fatalf(ctx, "getting-device-failed-%s", err) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 124 | } |
| 125 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 126 | onuA.updateDevice(d) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 127 | }() |
| 128 | return nil |
| 129 | } |
| 130 | |
Andrea Campanella | 025667e | 2021-01-14 11:50:07 +0100 | [diff] [blame^] | 131 | // Single_get_value_request retrieves a single value. |
| 132 | func (onuA *ONUAdapter) Single_get_value_request(ctx context.Context, // nolint |
| 133 | request extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) { |
| 134 | logger.Fatalf(ctx, "Single_get_value_request unimplemented") |
| 135 | return nil, nil |
| 136 | } |
| 137 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 138 | // Disable_device disables device |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 139 | func (onuA *ONUAdapter) Disable_device(ctx context.Context, device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 140 | go func() { |
| 141 | if d := onuA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 142 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 143 | } |
| 144 | cloned := proto.Clone(device).(*voltha.Device) |
| 145 | // Update the all ports state on that device to disable |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 146 | 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] | 147 | // Device may also have been deleted in the Core |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 148 | logger.Warnw(ctx, "updating-ports-failed", log.Fields{"device-id": 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 | } |
| 151 | //Update the device state |
| 152 | cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE |
| 153 | cloned.OperStatus = voltha.OperStatus_UNKNOWN |
| 154 | |
| 155 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 156 | logger.Warnw(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 157 | return |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 158 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 159 | onuA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 160 | }() |
| 161 | return nil |
| 162 | } |
| 163 | |
npujar | 1d86a52 | 2019-11-14 17:11:16 +0530 | [diff] [blame] | 164 | // Reenable_device reenables device |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 165 | func (onuA *ONUAdapter) Reenable_device(ctx context.Context, device *voltha.Device) error { // nolint |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 166 | go func() { |
| 167 | if d := onuA.getDevice(device.Id); d == nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 168 | logger.Fatalf(ctx, "device-not-found-%s", device.Id) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | cloned := proto.Clone(device).(*voltha.Device) |
| 172 | // Update the all ports state on that device to enable |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 173 | if err := onuA.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, 0, voltha.OperStatus_ACTIVE); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 174 | logger.Fatalf(ctx, "updating-ports-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | //Update the device state |
| 178 | cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE |
| 179 | cloned.OperStatus = voltha.OperStatus_ACTIVE |
| 180 | |
| 181 | if err := onuA.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 182 | logger.Fatalf(ctx, "device-state-update-failed", log.Fields{"device-id": device.Id, "error": err}) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 183 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 184 | |
| 185 | onuA.updateDevice(cloned) |
khenaidoo | ab1f7bd | 2019-11-14 14:00:27 -0500 | [diff] [blame] | 186 | }() |
| 187 | return nil |
| 188 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 189 | |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 190 | // Start_omci_test begins an omci self-test |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 191 | 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] | 192 | _ = device |
| 193 | return &ic.TestResponse{Result: ic.TestResponse_SUCCESS}, nil |
| 194 | } |
| 195 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 196 | 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] | 197 | _ = deviceId |
| 198 | _ = device |
| 199 | _ = valueflag |
| 200 | return nil, errors.New("get-ext-value-not-implemented") |
| 201 | } |