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