khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1 | /* |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 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. |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 15 | */ |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 16 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 17 | package api |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "errors" |
| 22 | "fmt" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 23 | "math/rand" |
| 24 | "os" |
| 25 | "runtime" |
| 26 | "runtime/pprof" |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 27 | "strconv" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 28 | "strings" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 29 | "sync" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 30 | "testing" |
| 31 | "time" |
| 32 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 33 | "github.com/golang/protobuf/ptypes/empty" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 34 | "github.com/opencord/voltha-go/db/model" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 35 | "github.com/opencord/voltha-go/rw_core/config" |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 36 | "github.com/opencord/voltha-go/rw_core/core/adapter" |
| 37 | "github.com/opencord/voltha-go/rw_core/core/device" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 38 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 39 | tst "github.com/opencord/voltha-go/rw_core/test" |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame] | 40 | "github.com/opencord/voltha-lib-go/v4/pkg/db" |
| 41 | "github.com/opencord/voltha-lib-go/v4/pkg/flows" |
| 42 | "github.com/opencord/voltha-lib-go/v4/pkg/kafka" |
| 43 | mock_etcd "github.com/opencord/voltha-lib-go/v4/pkg/mocks/etcd" |
| 44 | mock_kafka "github.com/opencord/voltha-lib-go/v4/pkg/mocks/kafka" |
| 45 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
| 46 | "github.com/opencord/voltha-protos/v4/go/voltha" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 47 | "github.com/phayes/freeport" |
| 48 | "github.com/stretchr/testify/assert" |
| 49 | "google.golang.org/grpc/codes" |
| 50 | "google.golang.org/grpc/status" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 51 | ) |
| 52 | |
Marcos Aurelio Carrero (Furukawa) | a61a72c | 2021-01-28 13:48:20 -0300 | [diff] [blame^] | 53 | const numTrapOnNNIFlows = 4 |
| 54 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 55 | type NBTest struct { |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 56 | etcdServer *mock_etcd.EtcdServer |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 57 | deviceMgr *device.Manager |
| 58 | logicalDeviceMgr *device.LogicalManager |
| 59 | adapterMgr *adapter.Manager |
| 60 | kmp kafka.InterContainerProxy |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 61 | kClient kafka.Client |
| 62 | kvClientPort int |
| 63 | numONUPerOLT int |
| 64 | startingUNIPortNo int |
| 65 | oltAdapter *cm.OLTAdapter |
| 66 | onuAdapter *cm.ONUAdapter |
| 67 | oltAdapterName string |
| 68 | onuAdapterName string |
| 69 | coreInstanceID string |
| 70 | defaultTimeout time.Duration |
| 71 | maxTimeout time.Duration |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 74 | func newNBTest(ctx context.Context) *NBTest { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 75 | test := &NBTest{} |
| 76 | // Start the embedded etcd server |
| 77 | var err error |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 78 | test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error") |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 79 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 80 | logger.Fatal(ctx, err) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 81 | } |
| 82 | // Create the kafka client |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 83 | test.kClient = mock_kafka.NewKafkaClient() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 84 | test.oltAdapterName = "olt_adapter_mock" |
| 85 | test.onuAdapterName = "onu_adapter_mock" |
| 86 | test.coreInstanceID = "rw-nbi-test" |
khenaidoo | 3283673 | 2020-03-05 16:10:44 -0500 | [diff] [blame] | 87 | test.defaultTimeout = 10 * time.Second |
| 88 | test.maxTimeout = 20 * time.Second |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 89 | return test |
| 90 | } |
| 91 | |
| 92 | func (nb *NBTest) startCore(inCompeteMode bool) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 93 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 94 | defer cancel() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 95 | cfg := config.NewRWCoreFlags() |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 96 | cfg.CoreTopic = "rw_core" |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 97 | cfg.DefaultRequestTimeout = nb.defaultTimeout |
| 98 | cfg.DefaultCoreTimeout = nb.defaultTimeout |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 99 | cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 100 | grpcPort, err := freeport.GetFreePort() |
| 101 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 102 | logger.Fatal(ctx, "Cannot get a freeport for grpc") |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 103 | } |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 104 | cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 105 | setCoreCompeteMode(inCompeteMode) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 106 | client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 107 | backend := &db.Backend{ |
| 108 | Client: client, |
| 109 | StoreType: cfg.KVStoreType, |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 110 | Address: cfg.KVStoreAddress, |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 111 | Timeout: cfg.KVStoreTimeout, |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 112 | LivenessChannelInterval: cfg.LiveProbeInterval / 2} |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 113 | nb.kmp = kafka.NewInterContainerProxy( |
Neha Sharma | d1387da | 2020-05-07 20:07:28 +0000 | [diff] [blame] | 114 | kafka.InterContainerAddress(cfg.KafkaAdapterAddress), |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 115 | kafka.MsgClient(nb.kClient), |
David Bainbridge | 9ae1313 | 2020-06-22 17:28:01 -0700 | [diff] [blame] | 116 | kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic})) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 117 | |
| 118 | endpointMgr := kafka.NewEndpointManager(backend) |
Kent Hagerman | f5a6735 | 2020-04-30 15:15:26 -0400 | [diff] [blame] | 119 | proxy := model.NewDBPath(backend) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 120 | nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient) |
serkant.uluderya | 8ff291d | 2020-05-20 00:58:00 -0700 | [diff] [blame] | 121 | nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout) |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 122 | nb.adapterMgr.Start(ctx) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 123 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 124 | if err := nb.kmp.Start(ctx); err != nil { |
| 125 | logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 126 | } |
Kent Hagerman | 2f0d055 | 2020-04-23 17:28:52 -0400 | [diff] [blame] | 127 | requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 128 | if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil { |
| 129 | logger.Fatalf(ctx, "Cannot add request handler: %s", err) |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 130 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 131 | } |
| 132 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 133 | func (nb *NBTest) stopAll(ctx context.Context) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 134 | if nb.kClient != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 135 | nb.kClient.Stop(ctx) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 136 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 137 | if nb.kmp != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 138 | nb.kmp.Stop(ctx) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 139 | } |
| 140 | if nb.etcdServer != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 141 | tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 145 | func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 146 | // Get the latest set of logical devices |
| 147 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 148 | assert.Nil(t, err) |
| 149 | assert.NotNil(t, logicalDevices) |
| 150 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 151 | |
| 152 | ld := logicalDevices.Items[0] |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 153 | ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id}) |
| 154 | assert.Nil(t, err) |
| 155 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 156 | assert.NotEqual(t, "", ld.Id) |
| 157 | assert.NotEqual(t, uint64(0), ld.DatapathId) |
| 158 | assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc) |
| 159 | assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc) |
| 160 | assert.NotEqual(t, "", ld.RootDeviceId) |
| 161 | assert.NotEqual(t, "", ld.Desc.SerialNum) |
| 162 | assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers) |
| 163 | assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables) |
| 164 | assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities) |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 165 | assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items)) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 166 | assert.Equal(t, oltDevice.ParentId, ld.Id) |
| 167 | //Expected port no |
| 168 | expectedPortNo := make(map[uint32]bool) |
| 169 | expectedPortNo[uint32(2)] = false |
| 170 | for i := 0; i < nb.numONUPerOLT; i++ { |
| 171 | expectedPortNo[uint32(i+100)] = false |
| 172 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 173 | for _, p := range ports.Items { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 174 | assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo) |
| 175 | assert.Equal(t, uint32(4), p.OfpPort.State) |
| 176 | expectedPortNo[p.OfpPort.PortNo] = true |
| 177 | if strings.HasPrefix(p.Id, "nni") { |
| 178 | assert.Equal(t, true, p.RootPort) |
| 179 | //assert.Equal(t, uint32(2), p.OfpPort.PortNo) |
| 180 | assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo)) |
| 181 | } else { |
| 182 | assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo)) |
| 183 | assert.Equal(t, false, p.RootPort) |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 188 | func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 189 | // Get the latest set of devices |
| 190 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 191 | assert.Nil(t, err) |
| 192 | assert.NotNil(t, devices) |
| 193 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 194 | // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 195 | var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 196 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 197 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 198 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 199 | var wg sync.WaitGroup |
| 200 | for _, device := range devices.Items { |
| 201 | wg.Add(1) |
| 202 | go func(wg *sync.WaitGroup, device *voltha.Device) { |
| 203 | // Wait until the device is in the right state |
| 204 | err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi) |
| 205 | assert.Nil(t, err) |
| 206 | |
| 207 | // Now, verify the details of the device. First get the latest update |
| 208 | d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id}) |
| 209 | assert.Nil(t, err) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 210 | dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id}) |
| 211 | assert.Nil(t, err) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 212 | assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState) |
| 213 | assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus) |
| 214 | assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus) |
| 215 | assert.Equal(t, d.Type, d.Adapter) |
| 216 | assert.NotEqual(t, "", d.MacAddress) |
| 217 | assert.NotEqual(t, "", d.SerialNumber) |
| 218 | |
| 219 | if d.Type == "olt_adapter_mock" { |
| 220 | assert.Equal(t, true, d.Root) |
| 221 | assert.NotEqual(t, "", d.Id) |
| 222 | assert.NotEqual(t, "", d.ParentId) |
| 223 | assert.Nil(t, d.ProxyAddress) |
| 224 | } else if d.Type == "onu_adapter_mock" { |
| 225 | assert.Equal(t, false, d.Root) |
| 226 | assert.NotEqual(t, uint32(0), d.Vlan) |
| 227 | assert.NotEqual(t, "", d.Id) |
| 228 | assert.NotEqual(t, "", d.ParentId) |
| 229 | assert.NotEqual(t, "", d.ProxyAddress.DeviceId) |
| 230 | assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 231 | } else { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 232 | assert.Error(t, errors.New("invalid-device-type")) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 233 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 234 | assert.Equal(t, 2, len(dPorts.Items)) |
| 235 | for _, p := range dPorts.Items { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 236 | assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState) |
| 237 | assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus) |
| 238 | if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI { |
| 239 | assert.Equal(t, 0, len(p.Peers)) |
| 240 | } else if p.Type == voltha.Port_PON_OLT { |
| 241 | assert.Equal(t, nb.numONUPerOLT, len(p.Peers)) |
| 242 | assert.Equal(t, uint32(1), p.PortNo) |
| 243 | } else if p.Type == voltha.Port_PON_ONU { |
| 244 | assert.Equal(t, 1, len(p.Peers)) |
| 245 | assert.Equal(t, uint32(1), p.PortNo) |
| 246 | } else { |
| 247 | assert.Error(t, errors.New("invalid-port")) |
| 248 | } |
| 249 | } |
| 250 | wg.Done() |
| 251 | }(&wg, device) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 252 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 253 | wg.Wait() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 254 | } |
| 255 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 256 | func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 257 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 258 | if err != nil { |
| 259 | return nil, err |
| 260 | } |
| 261 | for _, d := range devices.Items { |
| 262 | if d.Root == rootDevice { |
| 263 | return d, nil |
| 264 | } |
| 265 | } |
| 266 | return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice) |
| 267 | } |
| 268 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 269 | func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 270 | lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 271 | assert.Nil(t, err) |
| 272 | assert.NotNil(t, lds) |
| 273 | assert.Equal(t, 0, len(lds.Items)) |
| 274 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 275 | assert.Nil(t, err) |
| 276 | assert.NotNil(t, devices) |
| 277 | assert.Equal(t, 0, len(devices.Items)) |
| 278 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 279 | assert.Equal(t, 0, len(adapters.Items)) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 280 | assert.Nil(t, err) |
| 281 | assert.NotNil(t, adapters) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 282 | } |
| 283 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 284 | func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 285 | ctx := context.Background() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 286 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
| 287 | assert.Nil(t, err) |
| 288 | assert.NotNil(t, adapters) |
| 289 | assert.Equal(t, 2, len(adapters.Items)) |
| 290 | for _, a := range adapters.Items { |
| 291 | switch a.Id { |
| 292 | case nb.oltAdapterName: |
| 293 | assert.Equal(t, "Voltha-olt", a.Vendor) |
| 294 | case nb.onuAdapterName: |
| 295 | assert.Equal(t, "Voltha-onu", a.Vendor) |
| 296 | default: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 297 | logger.Fatal(ctx, "unregistered-adapter", a.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{}) |
| 301 | assert.Nil(t, err) |
| 302 | assert.NotNil(t, deviceTypes) |
| 303 | assert.Equal(t, 2, len(deviceTypes.Items)) |
| 304 | for _, dt := range deviceTypes.Items { |
| 305 | switch dt.Id { |
| 306 | case nb.oltAdapterName: |
| 307 | assert.Equal(t, nb.oltAdapterName, dt.Adapter) |
| 308 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 309 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 310 | case nb.onuAdapterName: |
| 311 | assert.Equal(t, nb.onuAdapterName, dt.Adapter) |
| 312 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 313 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 314 | default: |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 315 | logger.Fatal(ctx, "invalid-device-type", dt.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 320 | func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 321 | // Create a valid device |
| 322 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 323 | assert.Nil(t, err) |
| 324 | assert.NotNil(t, oltDevice) |
| 325 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 326 | assert.Nil(t, err) |
| 327 | assert.NotNil(t, device) |
| 328 | assert.Equal(t, oltDevice.String(), device.String()) |
| 329 | |
| 330 | // Try to create the same device |
| 331 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 332 | assert.NotNil(t, err) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 333 | assert.Equal(t, "device is already pre-provisioned", err.Error()) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 334 | |
| 335 | // Try to create a device with invalid data |
| 336 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName}) |
| 337 | assert.NotNil(t, err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 338 | assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error()) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 339 | |
| 340 | // Ensure we only have 1 device in the Core |
| 341 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 342 | assert.Nil(t, err) |
| 343 | assert.NotNil(t, devices) |
| 344 | assert.Equal(t, 1, len(devices.Items)) |
| 345 | assert.Equal(t, oltDevice.String(), devices.Items[0].String()) |
| 346 | |
| 347 | //Remove the device |
| 348 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 349 | assert.Nil(t, err) |
| 350 | |
| 351 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 352 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 353 | return devices != nil && len(devices.Items) == 0 |
| 354 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 355 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 356 | assert.Nil(t, err) |
| 357 | } |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 358 | func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) { |
| 359 | // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables |
| 360 | var wg sync.WaitGroup |
| 361 | wg.Add(1) |
| 362 | go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false) |
| 363 | |
| 364 | // Enable the oltDevice |
| 365 | _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 366 | assert.Nil(t, err) |
| 367 | |
| 368 | // Wait for the logical device to be in the ready state |
| 369 | var vldFunction = func(ports []*voltha.LogicalPort) bool { |
| 370 | return len(ports) == nb.numONUPerOLT+1 |
| 371 | } |
| 372 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
| 373 | assert.Nil(t, err) |
| 374 | |
| 375 | // Verify that the devices have been setup correctly |
| 376 | nb.verifyDevices(t, nbi) |
| 377 | |
| 378 | // Get latest oltDevice data |
| 379 | oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 380 | assert.Nil(t, err) |
| 381 | |
| 382 | // Verify that the logical device has been setup correctly |
| 383 | nb.verifyLogicalDevices(t, oltDevice, nbi) |
| 384 | |
| 385 | // Wait until all flows has been sent to the devices successfully |
| 386 | wg.Wait() |
| 387 | |
| 388 | } |
| 389 | func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) { |
| 390 | // Create a valid device |
| 391 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 392 | assert.Nil(t, err) |
| 393 | assert.NotNil(t, oltDevice) |
| 394 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 395 | assert.Nil(t, err) |
| 396 | assert.NotNil(t, device) |
| 397 | assert.Equal(t, oltDevice.String(), device.String()) |
| 398 | |
| 399 | // Ensure we only have 1 device in the Core |
| 400 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 401 | assert.Nil(t, err) |
| 402 | assert.NotNil(t, devices) |
| 403 | assert.Equal(t, 1, len(devices.Items)) |
| 404 | assert.Equal(t, oltDevice.String(), devices.Items[0].String()) |
| 405 | |
| 406 | //Remove the device forcefully |
| 407 | _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 408 | assert.Nil(t, err) |
| 409 | |
| 410 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 411 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 412 | return devices != nil && len(devices.Items) == 0 |
| 413 | } |
| 414 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 415 | assert.Nil(t, err) |
| 416 | } |
| 417 | |
| 418 | func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) { |
| 419 | // Create a valid device |
| 420 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 421 | assert.Nil(t, err) |
| 422 | assert.NotNil(t, oltDevice) |
| 423 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 424 | assert.Nil(t, err) |
| 425 | assert.NotNil(t, device) |
| 426 | assert.Equal(t, oltDevice.String(), device.String()) |
| 427 | |
| 428 | nb.enableDevice(t, nbi, oltDevice) |
| 429 | |
| 430 | //Remove the device forcefully |
| 431 | _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 432 | assert.Nil(t, err) |
| 433 | |
| 434 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 435 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 436 | return devices != nil && len(devices.Items) == 0 |
| 437 | } |
| 438 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 439 | assert.Nil(t, err) |
| 440 | } |
| 441 | |
| 442 | func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) { |
| 443 | // Create a valid device |
| 444 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 445 | assert.Nil(t, err) |
| 446 | assert.NotNil(t, oltDevice) |
| 447 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 448 | assert.Nil(t, err) |
| 449 | assert.NotNil(t, device) |
| 450 | assert.Equal(t, oltDevice.String(), device.String()) |
| 451 | |
| 452 | // Ensure we only have 1 device in the Core |
| 453 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 454 | assert.Nil(t, err) |
| 455 | assert.NotNil(t, devices) |
| 456 | assert.Equal(t, 1, len(devices.Items)) |
| 457 | assert.Equal(t, oltDevice.String(), devices.Items[0].String()) |
| 458 | |
| 459 | //Remove the device forcefully |
| 460 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 461 | assert.Nil(t, err) |
| 462 | |
| 463 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 464 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 465 | return devices != nil && len(devices.Items) == 0 |
| 466 | } |
| 467 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 468 | assert.Nil(t, err) |
| 469 | } |
| 470 | |
| 471 | func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) { |
| 472 | // Create a valid device |
| 473 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 474 | assert.Nil(t, err) |
| 475 | assert.NotNil(t, oltDevice) |
| 476 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 477 | assert.Nil(t, err) |
| 478 | assert.NotNil(t, device) |
| 479 | assert.Equal(t, oltDevice.String(), device.String()) |
| 480 | |
| 481 | nb.enableDevice(t, nbi, oltDevice) |
| 482 | |
| 483 | //Remove the device |
| 484 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 485 | assert.Nil(t, err) |
| 486 | |
| 487 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 488 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 489 | return devices != nil && len(devices.Items) == 0 |
| 490 | } |
| 491 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 492 | assert.Nil(t, err) |
| 493 | } |
| 494 | |
| 495 | func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) { |
| 496 | // Create a valid device |
| 497 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 498 | assert.Nil(t, err) |
| 499 | assert.NotNil(t, oltDevice) |
| 500 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 501 | assert.Nil(t, err) |
| 502 | assert.NotNil(t, device) |
| 503 | assert.Equal(t, oltDevice.String(), device.String()) |
| 504 | |
| 505 | nb.enableDevice(t, nbi, oltDevice) |
| 506 | nb.oltAdapter.SetDeleteAction(true) |
| 507 | //Remove the device |
| 508 | _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 509 | assert.Nil(t, err) |
| 510 | |
| 511 | //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout |
| 512 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 513 | return devices != nil && len(devices.Items) == 0 |
| 514 | } |
| 515 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 516 | assert.Nil(t, err) |
| 517 | |
| 518 | } |
| 519 | |
| 520 | func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) { |
| 521 | // Create a valid device |
| 522 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 523 | assert.Nil(t, err) |
| 524 | assert.NotNil(t, oltDevice) |
| 525 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 526 | assert.Nil(t, err) |
| 527 | assert.NotNil(t, device) |
| 528 | assert.Equal(t, oltDevice.String(), device.String()) |
| 529 | |
| 530 | nb.enableDevice(t, nbi, oltDevice) |
| 531 | |
| 532 | nb.oltAdapter.SetDeleteAction(true) |
| 533 | //Remove the device |
| 534 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 535 | assert.Nil(t, err) |
| 536 | |
| 537 | //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout |
| 538 | var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 539 | return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1) |
| 540 | } |
| 541 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1) |
| 542 | assert.Nil(t, err) |
| 543 | |
| 544 | nb.oltAdapter.SetDeleteAction(false) |
| 545 | |
| 546 | // Now Force Delete this device |
| 547 | _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 548 | assert.Nil(t, err) |
| 549 | |
| 550 | //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout |
| 551 | var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 552 | return devices != nil && len(devices.Items) == 0 |
| 553 | } |
| 554 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2) |
| 555 | assert.Nil(t, err) |
| 556 | |
| 557 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 558 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 559 | func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 560 | // Create a device that has no adapter registered |
| 561 | oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"}) |
| 562 | assert.Nil(t, err) |
| 563 | assert.NotNil(t, oltDeviceNoAdapter) |
| 564 | |
| 565 | // Try to enable the oltDevice and check the error message |
| 566 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 567 | assert.NotNil(t, err) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 568 | assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error()) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 569 | |
| 570 | //Remove the device |
| 571 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 572 | assert.Nil(t, err) |
| 573 | |
| 574 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 575 | var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 576 | return devices != nil && len(devices.Items) == 0 |
| 577 | } |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 578 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 579 | assert.Nil(t, err) |
| 580 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 581 | // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables |
| 582 | var wg sync.WaitGroup |
| 583 | wg.Add(1) |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 584 | go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 585 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 586 | // Create the device with valid data |
| 587 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 588 | assert.Nil(t, err) |
| 589 | assert.NotNil(t, oltDevice) |
| 590 | |
| 591 | // Verify oltDevice exist in the core |
| 592 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 593 | assert.Nil(t, err) |
| 594 | assert.Equal(t, 1, len(devices.Items)) |
| 595 | assert.Equal(t, oltDevice.Id, devices.Items[0].Id) |
| 596 | |
| 597 | // Enable the oltDevice |
| 598 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 599 | assert.Nil(t, err) |
| 600 | |
| 601 | // Wait for the logical device to be in the ready state |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 602 | var vldFunction = func(ports []*voltha.LogicalPort) bool { |
| 603 | return len(ports) == nb.numONUPerOLT+1 |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 604 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 605 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 606 | assert.Nil(t, err) |
| 607 | |
| 608 | // Verify that the devices have been setup correctly |
| 609 | nb.verifyDevices(t, nbi) |
| 610 | |
| 611 | // Get latest oltDevice data |
| 612 | oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 613 | assert.Nil(t, err) |
| 614 | |
| 615 | // Verify that the logical device has been setup correctly |
| 616 | nb.verifyLogicalDevices(t, oltDevice, nbi) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 617 | |
| 618 | // Wait until all flows has been sent to the devices successfully |
| 619 | wg.Wait() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 620 | } |
| 621 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 622 | func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 623 | //Get an OLT device |
| 624 | oltDevice, err := nb.getADevice(true, nbi) |
| 625 | assert.Nil(t, err) |
| 626 | assert.NotNil(t, oltDevice) |
| 627 | |
| 628 | // Disable the oltDevice |
| 629 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 630 | assert.Nil(t, err) |
| 631 | |
| 632 | // Wait for the old device to be disabled |
| 633 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 634 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 635 | } |
| 636 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 637 | assert.Nil(t, err) |
| 638 | |
| 639 | // Verify that all onu devices are disabled as well |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 640 | onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 641 | assert.Nil(t, err) |
| 642 | for _, onu := range onuDevices.Items { |
| 643 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 644 | assert.Nil(t, err) |
| 645 | } |
| 646 | |
| 647 | // Wait for the logical device to satisfy the expected condition |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 648 | var vlFunction = func(ports []*voltha.LogicalPort) bool { |
| 649 | for _, lp := range ports { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 650 | if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 651 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) { |
| 652 | return false |
| 653 | } |
| 654 | } |
| 655 | return true |
| 656 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 657 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 658 | assert.Nil(t, err) |
| 659 | |
| 660 | // Reenable the oltDevice |
| 661 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 662 | assert.Nil(t, err) |
| 663 | |
| 664 | // Wait for the old device to be enabled |
| 665 | vdFunction = func(device *voltha.Device) bool { |
| 666 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 667 | } |
| 668 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 669 | assert.Nil(t, err) |
| 670 | |
| 671 | // Verify that all onu devices are enabled as well |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 672 | onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 673 | assert.Nil(t, err) |
| 674 | for _, onu := range onuDevices.Items { |
| 675 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 676 | assert.Nil(t, err) |
| 677 | } |
| 678 | |
| 679 | // Wait for the logical device to satisfy the expected condition |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 680 | vlFunction = func(ports []*voltha.LogicalPort) bool { |
| 681 | for _, lp := range ports { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 682 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 683 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 684 | return false |
| 685 | } |
| 686 | } |
| 687 | return true |
| 688 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 689 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 690 | assert.Nil(t, err) |
| 691 | } |
| 692 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 693 | func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) { |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 694 | //Get an OLT device |
| 695 | oltDevice, err := nb.getADevice(true, nbi) |
| 696 | assert.Nil(t, err) |
| 697 | assert.NotNil(t, oltDevice) |
| 698 | |
| 699 | // Disable the oltDevice |
| 700 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 701 | assert.Nil(t, err) |
| 702 | |
| 703 | // Wait for the olt device to be disabled |
| 704 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 705 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 706 | } |
| 707 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 708 | assert.Nil(t, err) |
| 709 | |
| 710 | // Verify that all onu devices are disabled as well |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 711 | onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 712 | assert.Nil(t, err) |
| 713 | for _, onu := range onuDevices.Items { |
| 714 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 715 | assert.Nil(t, err) |
| 716 | } |
| 717 | |
| 718 | // Delete the oltDevice |
| 719 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 720 | assert.Nil(t, err) |
| 721 | |
| 722 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 723 | return devices != nil && len(devices.Items) == 0 |
| 724 | } |
| 725 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 726 | assert.Nil(t, err) |
| 727 | |
| 728 | // Wait for absence of logical device |
| 729 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 730 | return lds != nil && len(lds.Items) == 0 |
| 731 | } |
| 732 | |
| 733 | err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 734 | assert.Nil(t, err) |
| 735 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 736 | |
| 737 | func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) { |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 738 | devices, _ := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 739 | if len(devices.Items) == 0 { |
| 740 | // Nothing to do |
| 741 | return |
| 742 | } |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 743 | //Get an OLT device |
| 744 | oltDevice, err := nb.getADevice(true, nbi) |
| 745 | assert.Nil(t, err) |
| 746 | assert.NotNil(t, oltDevice) |
| 747 | |
| 748 | // Delete the oltDevice |
| 749 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 750 | assert.Nil(t, err) |
| 751 | |
| 752 | // Wait for all devices to be deleted |
| 753 | vFunction := func(devices *voltha.Devices) bool { |
| 754 | return devices != nil && len(devices.Items) == 0 |
| 755 | } |
| 756 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 757 | assert.Nil(t, err) |
| 758 | |
| 759 | // Wait for absence of logical device |
| 760 | vlFunction := func(lds *voltha.LogicalDevices) bool { |
| 761 | return lds != nil && len(lds.Items) == 0 |
| 762 | } |
| 763 | |
| 764 | err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 765 | assert.Nil(t, err) |
| 766 | } |
| 767 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 768 | func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) { |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 769 | //Create the device with valid data |
| 770 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 771 | assert.Nil(t, err) |
| 772 | assert.NotNil(t, oltDevice) |
| 773 | |
| 774 | //Get an OLT device |
| 775 | oltDevice, err = nb.getADevice(true, nbi) |
| 776 | assert.Nil(t, err) |
| 777 | assert.NotNil(t, oltDevice) |
| 778 | |
| 779 | // Enable the oltDevice |
| 780 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 781 | assert.Nil(t, err) |
| 782 | |
| 783 | // Wait for the logical device to be in the ready state |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 784 | var vldFunction = func(ports []*voltha.LogicalPort) bool { |
| 785 | return len(ports) == nb.numONUPerOLT+1 |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 786 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 787 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 788 | assert.Nil(t, err) |
| 789 | |
| 790 | //Get all child devices |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 791 | onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 792 | assert.Nil(t, err) |
| 793 | |
| 794 | // Wait for the all onu devices to be enabled |
| 795 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 796 | return device.AdminState == voltha.AdminState_ENABLED |
| 797 | } |
| 798 | for _, onu := range onuDevices.Items { |
| 799 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 800 | assert.Nil(t, err) |
| 801 | } |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 802 | // Wait for each onu device to get deleted |
| 803 | var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 804 | return device == nil |
| 805 | } |
| 806 | |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 807 | // Delete the onuDevice |
| 808 | for _, onu := range onuDevices.Items { |
| 809 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id}) |
| 810 | assert.Nil(t, err) |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 811 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi) |
| 812 | assert.Nil(t, err) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 813 | } |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 814 | |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 815 | // Disable the oltDevice |
| 816 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 817 | assert.Nil(t, err) |
| 818 | |
| 819 | // Wait for the olt device to be disabled |
| 820 | var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 821 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 822 | } |
| 823 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi) |
| 824 | assert.Nil(t, err) |
| 825 | |
| 826 | // Delete the oltDevice |
| 827 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 828 | assert.Nil(t, err) |
| 829 | |
| 830 | var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 831 | return devices != nil && len(devices.Items) == 0 |
| 832 | } |
| 833 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc) |
| 834 | assert.Nil(t, err) |
| 835 | } |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 836 | func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 837 | //Get an OLT device |
| 838 | var cp *voltha.Port |
| 839 | oltDevice, err := nb.getADevice(true, nbi) |
| 840 | assert.Nil(t, err) |
| 841 | assert.NotNil(t, oltDevice) |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 842 | oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 843 | assert.Nil(t, err) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 844 | |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 845 | for _, cp = range oltPorts.Items { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 846 | if cp.Type == voltha.Port_PON_OLT { |
| 847 | break |
| 848 | } |
| 849 | |
| 850 | } |
| 851 | assert.NotNil(t, cp) |
| 852 | cp.DeviceId = oltDevice.Id |
| 853 | |
| 854 | // Disable the NW Port of oltDevice |
| 855 | _, err = nbi.DisablePort(getContext(), cp) |
| 856 | assert.Nil(t, err) |
| 857 | // Wait for the olt device Port to be disabled |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 858 | var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool { |
| 859 | for _, port := range ports.Items { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 860 | if port.PortNo == cp.PortNo { |
| 861 | return port.AdminState == voltha.AdminState_DISABLED |
| 862 | } |
| 863 | } |
| 864 | return false |
| 865 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 866 | err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 867 | assert.Nil(t, err) |
| 868 | // Wait for the logical device to satisfy the expected condition |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 869 | var vlFunction = func(ports []*voltha.LogicalPort) bool { |
| 870 | for _, lp := range ports { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 871 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 872 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 873 | return false |
| 874 | } |
| 875 | } |
| 876 | return true |
| 877 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 878 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 879 | assert.Nil(t, err) |
| 880 | |
| 881 | // Enable the NW Port of oltDevice |
| 882 | _, err = nbi.EnablePort(getContext(), cp) |
| 883 | assert.Nil(t, err) |
| 884 | |
| 885 | // Wait for the olt device Port to be enabled |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 886 | vdFunction = func(ports *voltha.Ports) bool { |
| 887 | for _, port := range ports.Items { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 888 | if port.PortNo == cp.PortNo { |
| 889 | return port.AdminState == voltha.AdminState_ENABLED |
| 890 | } |
| 891 | } |
| 892 | return false |
| 893 | } |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 894 | err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 895 | assert.Nil(t, err) |
| 896 | // Wait for the logical device to satisfy the expected condition |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 897 | vlFunction = func(ports []*voltha.LogicalPort) bool { |
| 898 | for _, lp := range ports { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 899 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 900 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 901 | return false |
| 902 | } |
| 903 | } |
| 904 | return true |
| 905 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 906 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 907 | assert.Nil(t, err) |
| 908 | |
| 909 | // Disable a non-PON port |
Kent Hagerman | 2a07b86 | 2020-06-19 15:23:07 -0400 | [diff] [blame] | 910 | for _, cp = range oltPorts.Items { |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 911 | if cp.Type != voltha.Port_PON_OLT { |
| 912 | break |
| 913 | } |
| 914 | |
| 915 | } |
| 916 | assert.NotNil(t, cp) |
| 917 | cp.DeviceId = oltDevice.Id |
| 918 | |
| 919 | // Disable the NW Port of oltDevice |
| 920 | _, err = nbi.DisablePort(getContext(), cp) |
| 921 | assert.NotNil(t, err) |
| 922 | |
| 923 | } |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 924 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 925 | func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) { |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 926 | //Get an OLT device |
| 927 | oltDevice, err := nb.getADevice(true, nbi) |
| 928 | assert.Nil(t, err) |
| 929 | assert.NotNil(t, oltDevice) |
| 930 | assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE) |
| 931 | assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED) |
| 932 | |
| 933 | // Verify that we have one or more ONUs to start with |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 934 | onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 935 | assert.Nil(t, err) |
| 936 | assert.NotNil(t, onuDevices) |
| 937 | assert.Greater(t, len(onuDevices.Items), 0) |
| 938 | |
| 939 | // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN |
| 940 | _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 941 | assert.Nil(t, err) |
| 942 | |
| 943 | var vlFunction0 = func(d *voltha.Device) bool { |
| 944 | return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN |
| 945 | } |
| 946 | |
| 947 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi) |
| 948 | assert.Nil(t, err) |
| 949 | |
| 950 | // Wait for the logical device to satisfy the expected condition |
| 951 | var vlFunction1 = func(ld *voltha.LogicalDevice) bool { |
| 952 | return ld == nil |
| 953 | } |
| 954 | |
| 955 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1) |
| 956 | assert.Nil(t, err) |
| 957 | |
| 958 | // Wait for the device to satisfy the expected condition (device does not have flows) |
| 959 | var vlFunction2 = func(d *voltha.Device) bool { |
| 960 | var deviceFlows *ofp.Flows |
| 961 | var err error |
| 962 | if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil { |
| 963 | return false |
| 964 | } |
| 965 | return len(deviceFlows.Items) == 0 |
| 966 | } |
| 967 | |
| 968 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi) |
| 969 | assert.Nil(t, err) |
| 970 | |
| 971 | // Wait for the device to satisfy the expected condition (there are no child devices) |
| 972 | var vlFunction3 = func(d *voltha.Device) bool { |
| 973 | var devices *voltha.Devices |
| 974 | var err error |
| 975 | if devices, err = nbi.ListDevices(getContext(), nil); err != nil { |
| 976 | return false |
| 977 | } |
| 978 | for _, device := range devices.Items { |
| 979 | if device.ParentId == d.Id { |
| 980 | // We have a child device still left |
| 981 | return false |
| 982 | } |
| 983 | } |
| 984 | return true |
| 985 | } |
| 986 | |
| 987 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi) |
| 988 | assert.Nil(t, err) |
| 989 | |
| 990 | // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE |
| 991 | // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 992 | err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 993 | assert.Nil(t, err) |
| 994 | |
| 995 | // Verify the device connection and operation states |
| 996 | oltDevice, err = nb.getADevice(true, nbi) |
| 997 | assert.Nil(t, err) |
| 998 | assert.NotNil(t, oltDevice) |
| 999 | assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE) |
| 1000 | assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED) |
| 1001 | |
| 1002 | // Wait for the logical device to satisfy the expected condition |
| 1003 | var vlFunction4 = func(ld *voltha.LogicalDevice) bool { |
| 1004 | return ld != nil |
| 1005 | } |
| 1006 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4) |
| 1007 | assert.Nil(t, err) |
| 1008 | |
| 1009 | // Verify that logical device is created again |
| 1010 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 1011 | assert.Nil(t, err) |
| 1012 | assert.NotNil(t, logicalDevices) |
| 1013 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 1014 | |
| 1015 | // Verify that we have no ONUs left |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1016 | onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1017 | assert.Nil(t, err) |
| 1018 | assert.NotNil(t, onuDevices) |
| 1019 | assert.Equal(t, 0, len(onuDevices.Items)) |
| 1020 | } |
| 1021 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1022 | func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) { |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1023 | // ----------------------------------------------------------------------- |
| 1024 | // SubTest 1: Omci test action should fail due to nonexistent device id |
| 1025 | |
| 1026 | request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"} |
| 1027 | _, err := nbi.StartOmciTestAction(getContext(), request) |
| 1028 | assert.NotNil(t, err) |
| 1029 | assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error()) |
| 1030 | |
| 1031 | // ----------------------------------------------------------------------- |
| 1032 | // SubTest 2: Error should be returned for device with no adapter registered |
| 1033 | |
| 1034 | // Create a device that has no adapter registered |
| 1035 | deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"}) |
| 1036 | assert.Nil(t, err) |
| 1037 | assert.NotNil(t, deviceNoAdapter) |
| 1038 | |
| 1039 | // Omci test action should fail due to nonexistent adapter |
| 1040 | request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"} |
| 1041 | _, err = nbi.StartOmciTestAction(getContext(), request) |
| 1042 | assert.NotNil(t, err) |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1043 | assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error()) |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1044 | |
| 1045 | //Remove the device |
| 1046 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id}) |
| 1047 | assert.Nil(t, err) |
| 1048 | |
| 1049 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 1050 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 1051 | return devices != nil && len(devices.Items) == 0 |
| 1052 | } |
| 1053 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 1054 | assert.Nil(t, err) |
| 1055 | |
| 1056 | // ----------------------------------------------------------------------- |
| 1057 | // SubTest 3: Omci test action should succeed on valid ONU |
| 1058 | |
| 1059 | // Create the device with valid data |
| 1060 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 1061 | assert.Nil(t, err) |
| 1062 | assert.NotNil(t, oltDevice) |
| 1063 | |
| 1064 | // Verify oltDevice exist in the core |
| 1065 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 1066 | assert.Nil(t, err) |
| 1067 | assert.Equal(t, 1, len(devices.Items)) |
| 1068 | assert.Equal(t, oltDevice.Id, devices.Items[0].Id) |
| 1069 | |
| 1070 | // Enable the oltDevice |
| 1071 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 1072 | assert.Nil(t, err) |
| 1073 | |
| 1074 | // Wait for the logical device to be in the ready state |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1075 | var vldFunction = func(ports []*voltha.LogicalPort) bool { |
| 1076 | return len(ports) == nb.numONUPerOLT+1 |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1077 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1078 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1079 | assert.Nil(t, err) |
| 1080 | |
| 1081 | // Wait for the olt device to be enabled |
| 1082 | vdFunction := func(device *voltha.Device) bool { |
| 1083 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 1084 | } |
| 1085 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 1086 | assert.Nil(t, err) |
| 1087 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1088 | onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id) |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1089 | assert.Nil(t, err) |
| 1090 | assert.Greater(t, len(onuDevices.Items), 0) |
| 1091 | |
| 1092 | onuDevice := onuDevices.Items[0] |
| 1093 | |
| 1094 | // Omci test action should succeed |
| 1095 | request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"} |
| 1096 | resp, err := nbi.StartOmciTestAction(getContext(), request) |
| 1097 | assert.Nil(t, err) |
| 1098 | assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS) |
| 1099 | |
| 1100 | //Remove the device |
| 1101 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 1102 | assert.Nil(t, err) |
| 1103 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 1104 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 1105 | assert.Nil(t, err) |
| 1106 | } |
| 1107 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1108 | func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod { |
| 1109 | matchFields := make([]*ofp.OfpOxmField, 0) |
| 1110 | for _, val := range fa.MatchFields { |
| 1111 | matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}}) |
| 1112 | } |
| 1113 | return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV) |
| 1114 | } |
| 1115 | |
| 1116 | func createMetadata(cTag int, techProfile int, port int) uint64 { |
| 1117 | md := 0 |
| 1118 | md = (md | (cTag & 0xFFFF)) << 16 |
| 1119 | md = (md | (techProfile & 0xFFFF)) << 32 |
| 1120 | return uint64(md | (port & 0xFFFFFFFF)) |
| 1121 | } |
| 1122 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1123 | func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) { |
Marcos Aurelio Carrero (Furukawa) | a61a72c | 2021-01-28 13:48:20 -0300 | [diff] [blame^] | 1124 | expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1125 | if flowAddFail { |
| 1126 | expectedNumFlows = 0 |
| 1127 | } |
| 1128 | // Wait for logical device to have the flows (or none |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1129 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
Mahir Gunyel | addb66a | 2020-04-29 18:08:50 -0700 | [diff] [blame] | 1130 | flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id}) |
| 1131 | return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1132 | } |
| 1133 | // No timeout implies a success |
| 1134 | err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 1135 | assert.Nil(t, err) |
| 1136 | } |
| 1137 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1138 | func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, ports []*voltha.LogicalPort, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1139 | // Send flows for the parent device |
| 1140 | var nniPorts []*voltha.LogicalPort |
| 1141 | var uniPorts []*voltha.LogicalPort |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1142 | for _, p := range ports { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1143 | if p.RootPort { |
| 1144 | nniPorts = append(nniPorts, p) |
| 1145 | } else { |
| 1146 | uniPorts = append(uniPorts, p) |
| 1147 | } |
| 1148 | } |
| 1149 | assert.Equal(t, 1, len(nniPorts)) |
| 1150 | //assert.Greater(t, len(uniPorts), 1 ) |
| 1151 | nniPort := nniPorts[0].OfpPort.PortNo |
| 1152 | maxInt32 := uint64(0xFFFFFFFF) |
| 1153 | controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port |
| 1154 | var fa *flows.FlowArgs |
| 1155 | fa = &flows.FlowArgs{ |
| 1156 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 1157 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 1158 | flows.InPort(nniPort), |
| 1159 | flows.EthType(35020), |
| 1160 | }, |
| 1161 | Actions: []*ofp.OfpAction{ |
| 1162 | flows.Output(controllerPortMask), |
| 1163 | }, |
| 1164 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1165 | flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1166 | _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP) |
| 1167 | assert.Nil(t, err) |
| 1168 | |
| 1169 | fa = &flows.FlowArgs{ |
| 1170 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 1171 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 1172 | flows.InPort(nniPort), |
| 1173 | flows.EthType(2048), |
| 1174 | flows.IpProto(17), |
| 1175 | flows.UdpSrc(67), |
| 1176 | flows.UdpDst(68), |
| 1177 | }, |
| 1178 | Actions: []*ofp.OfpAction{ |
| 1179 | flows.Output(controllerPortMask), |
| 1180 | }, |
| 1181 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1182 | flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1183 | _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4) |
| 1184 | assert.Nil(t, err) |
| 1185 | |
| 1186 | fa = &flows.FlowArgs{ |
| 1187 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 1188 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 1189 | flows.InPort(nniPort), |
| 1190 | flows.EthType(34525), |
| 1191 | flows.IpProto(17), |
| 1192 | flows.UdpSrc(546), |
| 1193 | flows.UdpDst(547), |
| 1194 | }, |
| 1195 | Actions: []*ofp.OfpAction{ |
| 1196 | flows.Output(controllerPortMask), |
| 1197 | }, |
| 1198 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1199 | flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1200 | _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6) |
| 1201 | assert.Nil(t, err) |
| 1202 | |
Marcos Aurelio Carrero (Furukawa) | a61a72c | 2021-01-28 13:48:20 -0300 | [diff] [blame^] | 1203 | fa = &flows.FlowArgs{ |
| 1204 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 1205 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 1206 | flows.InPort(nniPort), |
| 1207 | flows.EthType(34915), |
| 1208 | }, |
| 1209 | Actions: []*ofp.OfpAction{ |
| 1210 | flows.Output(controllerPortMask), |
| 1211 | }, |
| 1212 | } |
| 1213 | flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
| 1214 | _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP) |
| 1215 | assert.Nil(t, err) |
| 1216 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1217 | return len(nniPorts), len(uniPorts) |
| 1218 | } |
| 1219 | |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1220 | func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1221 | maxInt32 := uint64(0xFFFFFFFF) |
| 1222 | controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port |
| 1223 | fa := &flows.FlowArgs{ |
| 1224 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1, "write_metadata": createMetadata(vlan, 64, 0), "meter_id": meterID}, |
| 1225 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 1226 | flows.InPort(port.PortNo), |
| 1227 | flows.EthType(34958), |
| 1228 | flows.VlanVid(8187), |
| 1229 | }, |
| 1230 | Actions: []*ofp.OfpAction{ |
| 1231 | flows.Output(controllerPortMask), |
| 1232 | }, |
| 1233 | } |
| 1234 | flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 1235 | maxTries := 3 |
| 1236 | var err error |
| 1237 | for { |
| 1238 | if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil { |
| 1239 | if maxTries < 3 { |
| 1240 | t.Log("Re-sending EAPOL flow succeeded for port:", port) |
| 1241 | } |
| 1242 | break |
| 1243 | } |
| 1244 | t.Log("Sending EAPOL flows fail:", err) |
| 1245 | time.Sleep(50 * time.Millisecond) |
| 1246 | maxTries-- |
| 1247 | if maxTries == 0 { |
| 1248 | break |
| 1249 | } |
| 1250 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1251 | assert.Nil(t, err) |
| 1252 | } |
| 1253 | |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 1254 | func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup, flowAddFail bool, flowDeleteFail bool) { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1255 | defer wg.Done() |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1256 | |
| 1257 | // Clear any existing flows on the adapters |
| 1258 | nb.oltAdapter.ClearFlows() |
| 1259 | nb.onuAdapter.ClearFlows() |
| 1260 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1261 | // Set the adapter actions on flow addition/deletion |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 1262 | nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail) |
| 1263 | nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail) |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1264 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1265 | // Wait until a logical device is ready |
| 1266 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 1267 | if lds == nil || len(lds.Items) != 1 { |
| 1268 | return false |
| 1269 | } |
| 1270 | // Ensure there are both NNI ports and at least one UNI port on the logical device |
| 1271 | ld := lds.Items[0] |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1272 | ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id}) |
| 1273 | if err != nil { |
| 1274 | return false |
| 1275 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1276 | nniPort := false |
| 1277 | uniPort := false |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1278 | for _, p := range ports.Items { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1279 | nniPort = nniPort || p.RootPort == true |
| 1280 | uniPort = uniPort || p.RootPort == false |
| 1281 | if nniPort && uniPort { |
| 1282 | return true |
| 1283 | } |
| 1284 | } |
| 1285 | return false |
| 1286 | } |
| 1287 | err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 1288 | assert.Nil(t, err) |
| 1289 | |
| 1290 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 1291 | assert.Nil(t, err) |
| 1292 | assert.NotNil(t, logicalDevices) |
| 1293 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 1294 | |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1295 | logicalDeviceID := logicalDevices.Items[0].Id |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1296 | meterID := rand.Uint32() |
| 1297 | |
| 1298 | // Add a meter to the logical device |
| 1299 | meterMod := &ofp.OfpMeterMod{ |
| 1300 | Command: ofp.OfpMeterModCommand_OFPMC_ADD, |
| 1301 | Flags: rand.Uint32(), |
| 1302 | MeterId: meterID, |
| 1303 | Bands: []*ofp.OfpMeterBandHeader{ |
| 1304 | {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER, |
| 1305 | Rate: rand.Uint32(), |
| 1306 | BurstSize: rand.Uint32(), |
| 1307 | Data: nil, |
| 1308 | }, |
| 1309 | }, |
| 1310 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1311 | _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod}) |
| 1312 | assert.Nil(t, err) |
| 1313 | |
| 1314 | ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID}) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1315 | assert.Nil(t, err) |
| 1316 | |
| 1317 | // Send initial set of Trap flows |
| 1318 | startingVlan := 4091 |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1319 | nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1320 | |
| 1321 | // Listen for port events |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1322 | start := time.Now() |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1323 | processedNniLogicalPorts := 0 |
| 1324 | processedUniLogicalPorts := 0 |
| 1325 | |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1326 | for event := range nbi.GetChangeEventsQueueForTest() { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1327 | startingVlan++ |
| 1328 | if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok { |
| 1329 | ps := portStatus.PortStatus |
| 1330 | if ps.Reason == ofp.OfpPortReason_OFPPR_ADD { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1331 | if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) { |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1332 | processedUniLogicalPorts++ |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1333 | nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID)) |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1334 | } else { |
| 1335 | processedNniLogicalPorts++ |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1336 | } |
| 1337 | } |
| 1338 | } |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1339 | |
| 1340 | if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts { |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1341 | fmt.Println("Total time to send all flows:", time.Since(start)) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1342 | break |
| 1343 | } |
| 1344 | } |
| 1345 | //Verify the flow count on the logical device |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1346 | nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1347 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1348 | // Wait until all flows have been sent to the OLT adapters (or all failed) |
Marcos Aurelio Carrero (Furukawa) | a61a72c | 2021-01-28 13:48:20 -0300 | [diff] [blame^] | 1349 | expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1350 | if flowAddFail { |
| 1351 | expectedFlowCount = 0 |
| 1352 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1353 | var oltVFunc isConditionSatisfied = func() bool { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1354 | return nb.oltAdapter.GetFlowCount() >= expectedFlowCount |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1355 | } |
| 1356 | err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc) |
| 1357 | assert.Nil(t, err) |
| 1358 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1359 | // Wait until all flows have been sent to the ONU adapters (or all failed) |
| 1360 | expectedFlowCount = numUNIPorts |
| 1361 | if flowAddFail { |
| 1362 | expectedFlowCount = 0 |
| 1363 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1364 | var onuVFunc isConditionSatisfied = func() bool { |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1365 | return nb.onuAdapter.GetFlowCount() == expectedFlowCount |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1366 | } |
| 1367 | err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc) |
| 1368 | assert.Nil(t, err) |
| 1369 | } |
| 1370 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1371 | func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) { |
| 1372 | |
| 1373 | // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables |
| 1374 | var wg sync.WaitGroup |
| 1375 | wg.Add(1) |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 1376 | go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false) |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1377 | |
| 1378 | // Create the device with valid data |
| 1379 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 1380 | assert.Nil(t, err) |
| 1381 | assert.NotNil(t, oltDevice) |
| 1382 | |
| 1383 | // Verify oltDevice exist in the core |
| 1384 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 1385 | assert.Nil(t, err) |
| 1386 | assert.Equal(t, 1, len(devices.Items)) |
| 1387 | assert.Equal(t, oltDevice.Id, devices.Items[0].Id) |
| 1388 | |
| 1389 | // Enable the oltDevice |
| 1390 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 1391 | assert.Nil(t, err) |
| 1392 | |
| 1393 | // Wait for the logical device to be in the ready state |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1394 | var vldFunction = func(ports []*voltha.LogicalPort) bool { |
| 1395 | return len(ports) == nb.numONUPerOLT+1 |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1396 | } |
Kent Hagerman | fa9d6d4 | 2020-05-25 11:49:40 -0400 | [diff] [blame] | 1397 | err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1398 | assert.Nil(t, err) |
| 1399 | |
| 1400 | // Verify that the devices have been setup correctly |
| 1401 | nb.verifyDevices(t, nbi) |
| 1402 | |
| 1403 | // Get latest oltDevice data |
| 1404 | oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 1405 | assert.Nil(t, err) |
| 1406 | |
| 1407 | // Verify that the logical device has been setup correctly |
| 1408 | nb.verifyLogicalDevices(t, oltDevice, nbi) |
| 1409 | |
| 1410 | // Wait until all flows has been sent to the devices successfully |
| 1411 | wg.Wait() |
| 1412 | } |
| 1413 | |
Matteo Scandolo | d525ae3 | 2020-04-02 17:27:29 -0700 | [diff] [blame] | 1414 | func TestSuiteNbiApiHandler(t *testing.T) { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1415 | ctx := context.Background() |
Kent Hagerman | 2b21604 | 2020-04-03 18:28:56 -0400 | [diff] [blame] | 1416 | f, err := os.Create("../../../tests/results/profile.cpu") |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1417 | if err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1418 | logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1419 | } |
| 1420 | defer f.Close() |
| 1421 | runtime.SetBlockProfileRate(1) |
| 1422 | runtime.SetMutexProfileFraction(-1) |
| 1423 | if err := pprof.StartCPUProfile(f); err != nil { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1424 | logger.Fatalf(ctx, "could not start CPU profile: %v\n", err) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1425 | } |
| 1426 | defer pprof.StopCPUProfile() |
| 1427 | |
khenaidoo | 442e7c7 | 2020-03-10 16:13:48 -0400 | [diff] [blame] | 1428 | //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel) |
| 1429 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1430 | nb := newNBTest(ctx) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1431 | assert.NotNil(t, nb) |
| 1432 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1433 | defer nb.stopAll(ctx) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1434 | |
| 1435 | // Start the Core |
| 1436 | nb.startCore(false) |
| 1437 | |
| 1438 | // Set the grpc API interface - no grpc server is running in unit test |
Kent Hagerman | 45a13e4 | 2020-04-13 12:23:50 -0400 | [diff] [blame] | 1439 | nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1440 | |
| 1441 | // 1. Basic test with no data in Core |
| 1442 | nb.testCoreWithoutData(t, nbi) |
| 1443 | |
| 1444 | // Create/register the adapters |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 1445 | nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(ctx, t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr) |
Mahir Gunyel | 03de0d3 | 2020-06-03 01:36:59 -0700 | [diff] [blame] | 1446 | nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT() |
| 1447 | nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1448 | |
| 1449 | // 2. Test adapter registration |
| 1450 | nb.testAdapterRegistration(t, nbi) |
| 1451 | |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1452 | numberOfTestRuns := 2 |
| 1453 | for i := 1; i <= numberOfTestRuns; i++ { |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1454 | |
| 1455 | // 3. Test create device |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 1456 | nb.testCreateDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1457 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1458 | // 4. Test Delete Device Scenarios |
| 1459 | nb.testForceDeletePreProvDevice(t, nbi) |
| 1460 | nb.testDeletePreProvDevice(t, nbi) |
| 1461 | nb.testForceDeleteEnabledDevice(t, nbi) |
| 1462 | nb.testDeleteEnabledDevice(t, nbi) |
| 1463 | nb.testForceDeleteDeviceFailure(t, nbi) |
| 1464 | nb.testDeleteDeviceFailure(t, nbi) |
| 1465 | |
| 1466 | // 5. Test Enable a device |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 1467 | nb.testEnableDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1468 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1469 | // 6. Test disable and ReEnable a root device |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 1470 | nb.testDisableAndReEnableRootDevice(t, nbi) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 1471 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1472 | // 7. Test disable and Enable pon port of OLT device |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 1473 | nb.testDisableAndEnablePort(t, nbi) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 1474 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1475 | // 8.Test Device unreachable when OLT is enabled |
Girish Gowdra | 408cd96 | 2020-03-11 14:31:31 -0700 | [diff] [blame] | 1476 | nb.testDeviceRebootWhenOltIsEnabled(t, nbi) |
| 1477 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1478 | // 9. Test disable and delete all devices |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 1479 | nb.testDisableAndDeleteAllDevice(t, nbi) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1480 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1481 | // 10. Test enable and delete all devices |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 1482 | nb.testEnableAndDeleteAllDevice(t, nbi) |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1483 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1484 | // 11. Test omci test |
Scott Baker | 432f9be | 2020-03-26 11:56:30 -0700 | [diff] [blame] | 1485 | nb.testStartOmciTestAction(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1486 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1487 | // 12. Remove all devices from tests above |
khenaidoo | 0db4c81 | 2020-05-27 15:27:30 -0400 | [diff] [blame] | 1488 | nb.deleteAllDevices(t, nbi) |
| 1489 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1490 | // 13. Test flow add failure |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1491 | nb.testFlowAddFailure(t, nbi) |
| 1492 | |
Himani Chawla | 2ba1c9c | 2020-10-07 13:19:03 +0530 | [diff] [blame] | 1493 | // 14. Clean up |
khenaidoo | 8b4abbf | 2020-04-24 17:04:30 -0400 | [diff] [blame] | 1494 | nb.deleteAllDevices(t, nbi) |
| 1495 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1496 | } |