khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package core |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "errors" |
| 21 | "fmt" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/flows" |
| 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" |
| 33 | "github.com/opencord/voltha-go/rw_core/config" |
| 34 | cm "github.com/opencord/voltha-go/rw_core/mocks" |
serkant.uluderya | 2ae470f | 2020-01-21 11:13:09 -0800 | [diff] [blame] | 35 | "github.com/opencord/voltha-lib-go/v3/pkg/kafka" |
| 36 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 37 | lm "github.com/opencord/voltha-lib-go/v3/pkg/mocks" |
| 38 | "github.com/opencord/voltha-lib-go/v3/pkg/version" |
| 39 | ofp "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 40 | "github.com/opencord/voltha-protos/v3/go/voltha" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 41 | "github.com/phayes/freeport" |
| 42 | "github.com/stretchr/testify/assert" |
| 43 | "google.golang.org/grpc/codes" |
| 44 | "google.golang.org/grpc/status" |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 45 | ) |
| 46 | |
| 47 | type NBTest struct { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 48 | etcdServer *lm.EtcdServer |
| 49 | core *Core |
| 50 | kClient kafka.Client |
| 51 | kvClientPort int |
| 52 | numONUPerOLT int |
| 53 | startingUNIPortNo int |
| 54 | oltAdapter *cm.OLTAdapter |
| 55 | onuAdapter *cm.ONUAdapter |
| 56 | oltAdapterName string |
| 57 | onuAdapterName string |
| 58 | coreInstanceID string |
| 59 | defaultTimeout time.Duration |
| 60 | maxTimeout time.Duration |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | func newNBTest() *NBTest { |
| 64 | test := &NBTest{} |
| 65 | // Start the embedded etcd server |
| 66 | var err error |
| 67 | test.etcdServer, test.kvClientPort, err = startEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error") |
| 68 | if err != nil { |
| 69 | log.Fatal(err) |
| 70 | } |
| 71 | // Create the kafka client |
| 72 | test.kClient = lm.NewKafkaClient() |
| 73 | test.oltAdapterName = "olt_adapter_mock" |
| 74 | test.onuAdapterName = "onu_adapter_mock" |
| 75 | test.coreInstanceID = "rw-nbi-test" |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 76 | test.defaultTimeout = 50 * time.Second |
| 77 | test.maxTimeout = 300 * time.Second |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 78 | return test |
| 79 | } |
| 80 | |
| 81 | func (nb *NBTest) startCore(inCompeteMode bool) { |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 82 | ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) |
| 83 | defer cancel() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 84 | cfg := config.NewRWCoreFlags() |
| 85 | cfg.CorePairTopic = "rw_core" |
| 86 | cfg.DefaultRequestTimeout = nb.defaultTimeout.Nanoseconds() / 1000000 //TODO: change when Core changes to Duration |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 87 | cfg.DefaultCoreTimeout = nb.defaultTimeout.Nanoseconds() / 1000000 |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 88 | cfg.KVStorePort = nb.kvClientPort |
| 89 | cfg.InCompetingMode = inCompeteMode |
| 90 | grpcPort, err := freeport.GetFreePort() |
| 91 | if err != nil { |
| 92 | log.Fatal("Cannot get a freeport for grpc") |
| 93 | } |
| 94 | cfg.GrpcPort = grpcPort |
| 95 | cfg.GrpcHost = "127.0.0.1" |
| 96 | setCoreCompeteMode(inCompeteMode) |
| 97 | client := setupKVClient(cfg, nb.coreInstanceID) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 98 | nb.core = NewCore(ctx, nb.coreInstanceID, cfg, client, nb.kClient) |
| 99 | err = nb.core.Start(context.Background()) |
| 100 | if err != nil { |
| 101 | log.Fatal("Cannot start core") |
| 102 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 105 | func (nb *NBTest) createAndregisterAdapters(t *testing.T) { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 106 | // Setup the mock OLT adapter |
| 107 | oltAdapter, err := createMockAdapter(OltAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.oltAdapterName) |
| 108 | if err != nil { |
| 109 | log.Fatalw("setting-mock-olt-adapter-failed", log.Fields{"error": err}) |
| 110 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 111 | nb.oltAdapter = (oltAdapter).(*cm.OLTAdapter) |
| 112 | nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT() |
| 113 | nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo() |
| 114 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 115 | // Register the adapter |
| 116 | registrationData := &voltha.Adapter{ |
| 117 | Id: nb.oltAdapterName, |
| 118 | Vendor: "Voltha-olt", |
| 119 | Version: version.VersionInfo.Version, |
| 120 | } |
| 121 | types := []*voltha.DeviceType{{Id: nb.oltAdapterName, Adapter: nb.oltAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 122 | deviceTypes := &voltha.DeviceTypes{Items: types} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 123 | if _, err := nb.core.adapterMgr.registerAdapter(registrationData, deviceTypes); err != nil { |
| 124 | log.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 125 | assert.NotNil(t, err) |
| 126 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 127 | |
| 128 | // Setup the mock ONU adapter |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 129 | onuAdapter, err := createMockAdapter(OnuAdapter, nb.kClient, nb.coreInstanceID, coreName, nb.onuAdapterName) |
| 130 | if err != nil { |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 131 | log.Fatalw("setting-mock-onu-adapter-failed", log.Fields{"error": err}) |
| 132 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 133 | nb.onuAdapter = (onuAdapter).(*cm.ONUAdapter) |
| 134 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 135 | // Register the adapter |
| 136 | registrationData = &voltha.Adapter{ |
| 137 | Id: nb.onuAdapterName, |
| 138 | Vendor: "Voltha-onu", |
| 139 | Version: version.VersionInfo.Version, |
| 140 | } |
| 141 | types = []*voltha.DeviceType{{Id: nb.onuAdapterName, Adapter: nb.onuAdapterName, AcceptsAddRemoveFlowUpdates: true}} |
| 142 | deviceTypes = &voltha.DeviceTypes{Items: types} |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 143 | if _, err := nb.core.adapterMgr.registerAdapter(registrationData, deviceTypes); err != nil { |
| 144 | log.Errorw("failed-to-register-adapter", log.Fields{"error": err}) |
| 145 | assert.NotNil(t, err) |
| 146 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func (nb *NBTest) stopAll() { |
| 150 | if nb.kClient != nil { |
| 151 | nb.kClient.Stop() |
| 152 | } |
| 153 | if nb.core != nil { |
| 154 | nb.core.Stop(context.Background()) |
| 155 | } |
| 156 | if nb.etcdServer != nil { |
| 157 | stopEmbeddedEtcdServer(nb.etcdServer) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *APIHandler) { |
| 162 | // Get the latest set of logical devices |
| 163 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 164 | assert.Nil(t, err) |
| 165 | assert.NotNil(t, logicalDevices) |
| 166 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 167 | |
| 168 | ld := logicalDevices.Items[0] |
| 169 | assert.NotEqual(t, "", ld.Id) |
| 170 | assert.NotEqual(t, uint64(0), ld.DatapathId) |
| 171 | assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc) |
| 172 | assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc) |
| 173 | assert.NotEqual(t, "", ld.RootDeviceId) |
| 174 | assert.NotEqual(t, "", ld.Desc.SerialNum) |
| 175 | assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers) |
| 176 | assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables) |
| 177 | assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities) |
| 178 | assert.Equal(t, 1+nb.numONUPerOLT, len(ld.Ports)) |
| 179 | assert.Equal(t, oltDevice.ParentId, ld.Id) |
| 180 | //Expected port no |
| 181 | expectedPortNo := make(map[uint32]bool) |
| 182 | expectedPortNo[uint32(2)] = false |
| 183 | for i := 0; i < nb.numONUPerOLT; i++ { |
| 184 | expectedPortNo[uint32(i+100)] = false |
| 185 | } |
| 186 | for _, p := range ld.Ports { |
| 187 | assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo) |
| 188 | assert.Equal(t, uint32(4), p.OfpPort.State) |
| 189 | expectedPortNo[p.OfpPort.PortNo] = true |
| 190 | if strings.HasPrefix(p.Id, "nni") { |
| 191 | assert.Equal(t, true, p.RootPort) |
| 192 | //assert.Equal(t, uint32(2), p.OfpPort.PortNo) |
| 193 | assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo)) |
| 194 | } else { |
| 195 | assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo)) |
| 196 | assert.Equal(t, false, p.RootPort) |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | func (nb *NBTest) verifyDevices(t *testing.T, nbi *APIHandler) { |
| 202 | // Get the latest set of devices |
| 203 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 204 | assert.Nil(t, err) |
| 205 | assert.NotNil(t, devices) |
| 206 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 207 | // 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] | 208 | var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 209 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 210 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 211 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 212 | var wg sync.WaitGroup |
| 213 | for _, device := range devices.Items { |
| 214 | wg.Add(1) |
| 215 | go func(wg *sync.WaitGroup, device *voltha.Device) { |
| 216 | // Wait until the device is in the right state |
| 217 | err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi) |
| 218 | assert.Nil(t, err) |
| 219 | |
| 220 | // Now, verify the details of the device. First get the latest update |
| 221 | d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id}) |
| 222 | assert.Nil(t, err) |
| 223 | assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState) |
| 224 | assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus) |
| 225 | assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus) |
| 226 | assert.Equal(t, d.Type, d.Adapter) |
| 227 | assert.NotEqual(t, "", d.MacAddress) |
| 228 | assert.NotEqual(t, "", d.SerialNumber) |
| 229 | |
| 230 | if d.Type == "olt_adapter_mock" { |
| 231 | assert.Equal(t, true, d.Root) |
| 232 | assert.NotEqual(t, "", d.Id) |
| 233 | assert.NotEqual(t, "", d.ParentId) |
| 234 | assert.Nil(t, d.ProxyAddress) |
| 235 | } else if d.Type == "onu_adapter_mock" { |
| 236 | assert.Equal(t, false, d.Root) |
| 237 | assert.NotEqual(t, uint32(0), d.Vlan) |
| 238 | assert.NotEqual(t, "", d.Id) |
| 239 | assert.NotEqual(t, "", d.ParentId) |
| 240 | assert.NotEqual(t, "", d.ProxyAddress.DeviceId) |
| 241 | assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 242 | } else { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 243 | assert.Error(t, errors.New("invalid-device-type")) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 244 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 245 | assert.Equal(t, 2, len(d.Ports)) |
| 246 | for _, p := range d.Ports { |
| 247 | assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState) |
| 248 | assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus) |
| 249 | if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI { |
| 250 | assert.Equal(t, 0, len(p.Peers)) |
| 251 | } else if p.Type == voltha.Port_PON_OLT { |
| 252 | assert.Equal(t, nb.numONUPerOLT, len(p.Peers)) |
| 253 | assert.Equal(t, uint32(1), p.PortNo) |
| 254 | } else if p.Type == voltha.Port_PON_ONU { |
| 255 | assert.Equal(t, 1, len(p.Peers)) |
| 256 | assert.Equal(t, uint32(1), p.PortNo) |
| 257 | } else { |
| 258 | assert.Error(t, errors.New("invalid-port")) |
| 259 | } |
| 260 | } |
| 261 | wg.Done() |
| 262 | }(&wg, device) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 263 | } |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 264 | wg.Wait() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | func (nb *NBTest) getADevice(rootDevice bool, nbi *APIHandler) (*voltha.Device, error) { |
| 268 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 269 | if err != nil { |
| 270 | return nil, err |
| 271 | } |
| 272 | for _, d := range devices.Items { |
| 273 | if d.Root == rootDevice { |
| 274 | return d, nil |
| 275 | } |
| 276 | } |
| 277 | return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice) |
| 278 | } |
| 279 | |
| 280 | func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *APIHandler) { |
| 281 | lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 282 | assert.Nil(t, err) |
| 283 | assert.NotNil(t, lds) |
| 284 | assert.Equal(t, 0, len(lds.Items)) |
| 285 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 286 | assert.Nil(t, err) |
| 287 | assert.NotNil(t, devices) |
| 288 | assert.Equal(t, 0, len(devices.Items)) |
| 289 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
| 290 | assert.Nil(t, err) |
| 291 | assert.NotNil(t, adapters) |
| 292 | assert.Equal(t, 0, len(adapters.Items)) |
| 293 | } |
| 294 | |
| 295 | func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *APIHandler) { |
| 296 | adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{}) |
| 297 | assert.Nil(t, err) |
| 298 | assert.NotNil(t, adapters) |
| 299 | assert.Equal(t, 2, len(adapters.Items)) |
| 300 | for _, a := range adapters.Items { |
| 301 | switch a.Id { |
| 302 | case nb.oltAdapterName: |
| 303 | assert.Equal(t, "Voltha-olt", a.Vendor) |
| 304 | case nb.onuAdapterName: |
| 305 | assert.Equal(t, "Voltha-onu", a.Vendor) |
| 306 | default: |
| 307 | log.Fatal("unregistered-adapter", a.Id) |
| 308 | } |
| 309 | } |
| 310 | deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{}) |
| 311 | assert.Nil(t, err) |
| 312 | assert.NotNil(t, deviceTypes) |
| 313 | assert.Equal(t, 2, len(deviceTypes.Items)) |
| 314 | for _, dt := range deviceTypes.Items { |
| 315 | switch dt.Id { |
| 316 | case nb.oltAdapterName: |
| 317 | assert.Equal(t, nb.oltAdapterName, dt.Adapter) |
| 318 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 319 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 320 | case nb.onuAdapterName: |
| 321 | assert.Equal(t, nb.onuAdapterName, dt.Adapter) |
| 322 | assert.Equal(t, false, dt.AcceptsBulkFlowUpdate) |
| 323 | assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates) |
| 324 | default: |
| 325 | log.Fatal("invalid-device-type", dt.Id) |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | func (nb *NBTest) testCreateDevice(t *testing.T, nbi *APIHandler) { |
| 331 | // Create a valid device |
| 332 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 333 | assert.Nil(t, err) |
| 334 | assert.NotNil(t, oltDevice) |
| 335 | device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 336 | assert.Nil(t, err) |
| 337 | assert.NotNil(t, device) |
| 338 | assert.Equal(t, oltDevice.String(), device.String()) |
| 339 | |
| 340 | // Try to create the same device |
| 341 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 342 | assert.NotNil(t, err) |
| 343 | assert.Equal(t, "Device is already pre-provisioned", err.Error()) |
| 344 | |
| 345 | // Try to create a device with invalid data |
| 346 | _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName}) |
| 347 | assert.NotNil(t, err) |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 348 | assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error()) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 349 | |
| 350 | // Ensure we only have 1 device in the Core |
| 351 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 352 | assert.Nil(t, err) |
| 353 | assert.NotNil(t, devices) |
| 354 | assert.Equal(t, 1, len(devices.Items)) |
| 355 | assert.Equal(t, oltDevice.String(), devices.Items[0].String()) |
| 356 | |
| 357 | //Remove the device |
| 358 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 359 | assert.Nil(t, err) |
| 360 | |
| 361 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 362 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 363 | return devices != nil && len(devices.Items) == 0 |
| 364 | } |
| 365 | err = waitUntilConditionForDevices(5*time.Second, nbi, vFunction) |
| 366 | assert.Nil(t, err) |
| 367 | } |
| 368 | |
| 369 | func (nb *NBTest) testEnableDevice(t *testing.T, nbi *APIHandler) { |
| 370 | // Create a device that has no adapter registered |
| 371 | oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"}) |
| 372 | assert.Nil(t, err) |
| 373 | assert.NotNil(t, oltDeviceNoAdapter) |
| 374 | |
| 375 | // Try to enable the oltDevice and check the error message |
| 376 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 377 | assert.NotNil(t, err) |
| 378 | assert.Equal(t, "Adapter-not-registered-for-device-type noAdapterRegistered", err.Error()) |
| 379 | |
| 380 | //Remove the device |
| 381 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id}) |
| 382 | assert.Nil(t, err) |
| 383 | |
| 384 | //Ensure there are no devices in the Core now - wait until condition satisfied or timeout |
| 385 | var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 386 | return devices != nil && len(devices.Items) == 0 |
| 387 | } |
| 388 | err = waitUntilConditionForDevices(5*time.Second, nbi, vdFunction) |
| 389 | assert.Nil(t, err) |
| 390 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 391 | // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables |
| 392 | var wg sync.WaitGroup |
| 393 | wg.Add(1) |
| 394 | go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg) |
| 395 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 396 | // Create the device with valid data |
| 397 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 398 | assert.Nil(t, err) |
| 399 | assert.NotNil(t, oltDevice) |
| 400 | |
| 401 | // Verify oltDevice exist in the core |
| 402 | devices, err := nbi.ListDevices(getContext(), &empty.Empty{}) |
| 403 | assert.Nil(t, err) |
| 404 | assert.Equal(t, 1, len(devices.Items)) |
| 405 | assert.Equal(t, oltDevice.Id, devices.Items[0].Id) |
| 406 | |
| 407 | // Enable the oltDevice |
| 408 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 409 | assert.Nil(t, err) |
| 410 | |
| 411 | // Wait for the logical device to be in the ready state |
| 412 | var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool { |
| 413 | return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1 |
| 414 | } |
| 415 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
| 416 | assert.Nil(t, err) |
| 417 | |
| 418 | // Verify that the devices have been setup correctly |
| 419 | nb.verifyDevices(t, nbi) |
| 420 | |
| 421 | // Get latest oltDevice data |
| 422 | oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 423 | assert.Nil(t, err) |
| 424 | |
| 425 | // Verify that the logical device has been setup correctly |
| 426 | nb.verifyLogicalDevices(t, oltDevice, nbi) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 427 | |
| 428 | // Wait until all flows has been sent to the devices successfully |
| 429 | wg.Wait() |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *APIHandler) { |
| 433 | //Get an OLT device |
| 434 | oltDevice, err := nb.getADevice(true, nbi) |
| 435 | assert.Nil(t, err) |
| 436 | assert.NotNil(t, oltDevice) |
| 437 | |
| 438 | // Disable the oltDevice |
| 439 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 440 | assert.Nil(t, err) |
| 441 | |
| 442 | // Wait for the old device to be disabled |
| 443 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 444 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 445 | } |
| 446 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 447 | assert.Nil(t, err) |
| 448 | |
| 449 | // Verify that all onu devices are disabled as well |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 450 | onuDevices, err := nb.core.deviceMgr.getAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 451 | assert.Nil(t, err) |
| 452 | for _, onu := range onuDevices.Items { |
| 453 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 454 | assert.Nil(t, err) |
| 455 | } |
| 456 | |
| 457 | // Wait for the logical device to satisfy the expected condition |
| 458 | var vlFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 459 | if ld == nil { |
| 460 | return false |
| 461 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 462 | for _, lp := range ld.Ports { |
| 463 | if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 464 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) { |
| 465 | return false |
| 466 | } |
| 467 | } |
| 468 | return true |
| 469 | } |
| 470 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 471 | assert.Nil(t, err) |
| 472 | |
| 473 | // Reenable the oltDevice |
| 474 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 475 | assert.Nil(t, err) |
| 476 | |
| 477 | // Wait for the old device to be enabled |
| 478 | vdFunction = func(device *voltha.Device) bool { |
| 479 | return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE |
| 480 | } |
| 481 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 482 | assert.Nil(t, err) |
| 483 | |
| 484 | // Verify that all onu devices are enabled as well |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 485 | onuDevices, err = nb.core.deviceMgr.getAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 486 | assert.Nil(t, err) |
| 487 | for _, onu := range onuDevices.Items { |
| 488 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 489 | assert.Nil(t, err) |
| 490 | } |
| 491 | |
| 492 | // Wait for the logical device to satisfy the expected condition |
| 493 | vlFunction = func(ld *voltha.LogicalDevice) bool { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 494 | if ld == nil { |
| 495 | return false |
| 496 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 497 | for _, lp := range ld.Ports { |
| 498 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 499 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 500 | return false |
| 501 | } |
| 502 | } |
| 503 | return true |
| 504 | } |
| 505 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 506 | assert.Nil(t, err) |
| 507 | } |
| 508 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 509 | func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *APIHandler) { |
| 510 | //Get an OLT device |
| 511 | oltDevice, err := nb.getADevice(true, nbi) |
| 512 | assert.Nil(t, err) |
| 513 | assert.NotNil(t, oltDevice) |
| 514 | |
| 515 | // Disable the oltDevice |
| 516 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 517 | assert.Nil(t, err) |
| 518 | |
| 519 | // Wait for the olt device to be disabled |
| 520 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 521 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 522 | } |
| 523 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 524 | assert.Nil(t, err) |
| 525 | |
| 526 | // Verify that all onu devices are disabled as well |
npujar | 467fe75 | 2020-01-16 20:17:45 +0530 | [diff] [blame] | 527 | onuDevices, err := nb.core.deviceMgr.getAllChildDevices(getContext(), oltDevice.Id) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 528 | assert.Nil(t, err) |
| 529 | for _, onu := range onuDevices.Items { |
| 530 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 531 | assert.Nil(t, err) |
| 532 | } |
| 533 | |
| 534 | // Delete the oltDevice |
| 535 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 536 | assert.Nil(t, err) |
| 537 | |
| 538 | var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 539 | return devices != nil && len(devices.Items) == 0 |
| 540 | } |
| 541 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction) |
| 542 | assert.Nil(t, err) |
| 543 | |
| 544 | // Wait for absence of logical device |
| 545 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 546 | return lds != nil && len(lds.Items) == 0 |
| 547 | } |
| 548 | |
| 549 | err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 550 | assert.Nil(t, err) |
| 551 | } |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 552 | func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *APIHandler) { |
| 553 | //Create the device with valid data |
| 554 | oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"}) |
| 555 | assert.Nil(t, err) |
| 556 | assert.NotNil(t, oltDevice) |
| 557 | |
| 558 | //Get an OLT device |
| 559 | oltDevice, err = nb.getADevice(true, nbi) |
| 560 | assert.Nil(t, err) |
| 561 | assert.NotNil(t, oltDevice) |
| 562 | |
| 563 | // Enable the oltDevice |
| 564 | _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 565 | assert.Nil(t, err) |
| 566 | |
| 567 | // Wait for the logical device to be in the ready state |
| 568 | var vldFunction isLogicalDeviceConditionSatisfied = func(ld *voltha.LogicalDevice) bool { |
| 569 | return ld != nil && len(ld.Ports) == nb.numONUPerOLT+1 |
| 570 | } |
| 571 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction) |
| 572 | assert.Nil(t, err) |
| 573 | |
| 574 | //Get all child devices |
| 575 | onuDevices, err := nb.core.deviceMgr.getAllChildDevices(getContext(), oltDevice.Id) |
| 576 | assert.Nil(t, err) |
| 577 | |
| 578 | // Wait for the all onu devices to be enabled |
| 579 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 580 | return device.AdminState == voltha.AdminState_ENABLED |
| 581 | } |
| 582 | for _, onu := range onuDevices.Items { |
| 583 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi) |
| 584 | assert.Nil(t, err) |
| 585 | } |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 586 | // Wait for each onu device to get deleted |
| 587 | var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 588 | return device == nil |
| 589 | } |
| 590 | |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 591 | // Delete the onuDevice |
| 592 | for _, onu := range onuDevices.Items { |
| 593 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id}) |
| 594 | assert.Nil(t, err) |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 595 | err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi) |
| 596 | assert.Nil(t, err) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 597 | } |
Chaitrashree G S | e8ad020 | 2020-02-27 18:48:00 -0500 | [diff] [blame] | 598 | |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 599 | // Disable the oltDevice |
| 600 | _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 601 | assert.Nil(t, err) |
| 602 | |
| 603 | // Wait for the olt device to be disabled |
| 604 | var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 605 | return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN |
| 606 | } |
| 607 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi) |
| 608 | assert.Nil(t, err) |
| 609 | |
| 610 | // Delete the oltDevice |
| 611 | _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id}) |
| 612 | assert.Nil(t, err) |
| 613 | |
| 614 | var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool { |
| 615 | return devices != nil && len(devices.Items) == 0 |
| 616 | } |
| 617 | err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc) |
| 618 | assert.Nil(t, err) |
| 619 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 620 | func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *APIHandler) { |
| 621 | //Get an OLT device |
| 622 | var cp *voltha.Port |
| 623 | oltDevice, err := nb.getADevice(true, nbi) |
| 624 | assert.Nil(t, err) |
| 625 | assert.NotNil(t, oltDevice) |
| 626 | |
| 627 | for _, cp = range oltDevice.Ports { |
| 628 | if cp.Type == voltha.Port_PON_OLT { |
| 629 | break |
| 630 | } |
| 631 | |
| 632 | } |
| 633 | assert.NotNil(t, cp) |
| 634 | cp.DeviceId = oltDevice.Id |
| 635 | |
| 636 | // Disable the NW Port of oltDevice |
| 637 | _, err = nbi.DisablePort(getContext(), cp) |
| 638 | assert.Nil(t, err) |
| 639 | // Wait for the olt device Port to be disabled |
| 640 | var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool { |
| 641 | for _, port := range device.Ports { |
| 642 | if port.PortNo == cp.PortNo { |
| 643 | return port.AdminState == voltha.AdminState_DISABLED |
| 644 | } |
| 645 | } |
| 646 | return false |
| 647 | } |
| 648 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 649 | assert.Nil(t, err) |
| 650 | // Wait for the logical device to satisfy the expected condition |
| 651 | var vlFunction = func(ld *voltha.LogicalDevice) bool { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 652 | if ld == nil { |
| 653 | return false |
| 654 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 655 | for _, lp := range ld.Ports { |
| 656 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 657 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 658 | return false |
| 659 | } |
| 660 | } |
| 661 | return true |
| 662 | } |
| 663 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 664 | assert.Nil(t, err) |
| 665 | |
| 666 | // Enable the NW Port of oltDevice |
| 667 | _, err = nbi.EnablePort(getContext(), cp) |
| 668 | assert.Nil(t, err) |
| 669 | |
| 670 | // Wait for the olt device Port to be enabled |
| 671 | vdFunction = func(device *voltha.Device) bool { |
| 672 | for _, port := range device.Ports { |
| 673 | if port.PortNo == cp.PortNo { |
| 674 | return port.AdminState == voltha.AdminState_ENABLED |
| 675 | } |
| 676 | } |
| 677 | return false |
| 678 | } |
| 679 | err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi) |
| 680 | assert.Nil(t, err) |
| 681 | // Wait for the logical device to satisfy the expected condition |
| 682 | vlFunction = func(ld *voltha.LogicalDevice) bool { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 683 | if ld == nil { |
| 684 | return false |
| 685 | } |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 686 | for _, lp := range ld.Ports { |
| 687 | if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) || |
| 688 | lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) { |
| 689 | return false |
| 690 | } |
| 691 | } |
| 692 | return true |
| 693 | } |
| 694 | err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction) |
| 695 | assert.Nil(t, err) |
| 696 | |
| 697 | // Disable a non-PON port |
| 698 | for _, cp = range oltDevice.Ports { |
| 699 | if cp.Type != voltha.Port_PON_OLT { |
| 700 | break |
| 701 | } |
| 702 | |
| 703 | } |
| 704 | assert.NotNil(t, cp) |
| 705 | cp.DeviceId = oltDevice.Id |
| 706 | |
| 707 | // Disable the NW Port of oltDevice |
| 708 | _, err = nbi.DisablePort(getContext(), cp) |
| 709 | assert.NotNil(t, err) |
| 710 | |
| 711 | } |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 712 | |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 713 | func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod { |
| 714 | matchFields := make([]*ofp.OfpOxmField, 0) |
| 715 | for _, val := range fa.MatchFields { |
| 716 | matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}}) |
| 717 | } |
| 718 | return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV) |
| 719 | } |
| 720 | |
| 721 | func createMetadata(cTag int, techProfile int, port int) uint64 { |
| 722 | md := 0 |
| 723 | md = (md | (cTag & 0xFFFF)) << 16 |
| 724 | md = (md | (techProfile & 0xFFFF)) << 32 |
| 725 | return uint64(md | (port & 0xFFFFFFFF)) |
| 726 | } |
| 727 | |
| 728 | func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *APIHandler, numNNIPorts int, numUNIPorts int) { |
| 729 | expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts |
| 730 | // Wait for logical device to have all the flows |
| 731 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 732 | return lds != nil && len(lds.Items) == 1 && len(lds.Items[0].Flows.Items) == expectedNumFlows |
| 733 | } |
| 734 | // No timeout implies a success |
| 735 | err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 736 | assert.Nil(t, err) |
| 737 | } |
| 738 | |
| 739 | func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *APIHandler, logicalDevice *voltha.LogicalDevice, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) { |
| 740 | // Send flows for the parent device |
| 741 | var nniPorts []*voltha.LogicalPort |
| 742 | var uniPorts []*voltha.LogicalPort |
| 743 | for _, p := range logicalDevice.Ports { |
| 744 | if p.RootPort { |
| 745 | nniPorts = append(nniPorts, p) |
| 746 | } else { |
| 747 | uniPorts = append(uniPorts, p) |
| 748 | } |
| 749 | } |
| 750 | assert.Equal(t, 1, len(nniPorts)) |
| 751 | //assert.Greater(t, len(uniPorts), 1 ) |
| 752 | nniPort := nniPorts[0].OfpPort.PortNo |
| 753 | maxInt32 := uint64(0xFFFFFFFF) |
| 754 | controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port |
| 755 | var fa *flows.FlowArgs |
| 756 | fa = &flows.FlowArgs{ |
| 757 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 758 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 759 | flows.InPort(nniPort), |
| 760 | flows.EthType(35020), |
| 761 | }, |
| 762 | Actions: []*ofp.OfpAction{ |
| 763 | flows.Output(controllerPortMask), |
| 764 | }, |
| 765 | } |
| 766 | flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id} |
| 767 | _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP) |
| 768 | assert.Nil(t, err) |
| 769 | |
| 770 | fa = &flows.FlowArgs{ |
| 771 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 772 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 773 | flows.InPort(nniPort), |
| 774 | flows.EthType(2048), |
| 775 | flows.IpProto(17), |
| 776 | flows.UdpSrc(67), |
| 777 | flows.UdpDst(68), |
| 778 | }, |
| 779 | Actions: []*ofp.OfpAction{ |
| 780 | flows.Output(controllerPortMask), |
| 781 | }, |
| 782 | } |
| 783 | flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id} |
| 784 | _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4) |
| 785 | assert.Nil(t, err) |
| 786 | |
| 787 | fa = &flows.FlowArgs{ |
| 788 | KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1}, |
| 789 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 790 | flows.InPort(nniPort), |
| 791 | flows.EthType(34525), |
| 792 | flows.IpProto(17), |
| 793 | flows.UdpSrc(546), |
| 794 | flows.UdpDst(547), |
| 795 | }, |
| 796 | Actions: []*ofp.OfpAction{ |
| 797 | flows.Output(controllerPortMask), |
| 798 | }, |
| 799 | } |
| 800 | flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDevice.Id} |
| 801 | _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6) |
| 802 | assert.Nil(t, err) |
| 803 | |
| 804 | return len(nniPorts), len(uniPorts) |
| 805 | } |
| 806 | |
| 807 | func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *APIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) { |
| 808 | maxInt32 := uint64(0xFFFFFFFF) |
| 809 | controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port |
| 810 | fa := &flows.FlowArgs{ |
| 811 | 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}, |
| 812 | MatchFields: []*ofp.OfpOxmOfbField{ |
| 813 | flows.InPort(port.PortNo), |
| 814 | flows.EthType(34958), |
| 815 | flows.VlanVid(8187), |
| 816 | }, |
| 817 | Actions: []*ofp.OfpAction{ |
| 818 | flows.Output(controllerPortMask), |
| 819 | }, |
| 820 | } |
| 821 | flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID} |
| 822 | _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP) |
| 823 | assert.Nil(t, err) |
| 824 | } |
| 825 | |
| 826 | func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *APIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup) { |
| 827 | defer wg.Done() |
| 828 | if nb.core.logicalDeviceMgr.grpcNbiHdlr != nbi { |
| 829 | nb.core.logicalDeviceMgr.setGrpcNbiHandler(nbi) |
| 830 | } |
| 831 | |
| 832 | // Clear any existing flows on the adapters |
| 833 | nb.oltAdapter.ClearFlows() |
| 834 | nb.onuAdapter.ClearFlows() |
| 835 | |
| 836 | // Wait until a logical device is ready |
| 837 | var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool { |
| 838 | if lds == nil || len(lds.Items) != 1 { |
| 839 | return false |
| 840 | } |
| 841 | // Ensure there are both NNI ports and at least one UNI port on the logical device |
| 842 | ld := lds.Items[0] |
| 843 | nniPort := false |
| 844 | uniPort := false |
| 845 | for _, p := range ld.Ports { |
| 846 | nniPort = nniPort || p.RootPort == true |
| 847 | uniPort = uniPort || p.RootPort == false |
| 848 | if nniPort && uniPort { |
| 849 | return true |
| 850 | } |
| 851 | } |
| 852 | return false |
| 853 | } |
| 854 | err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction) |
| 855 | assert.Nil(t, err) |
| 856 | |
| 857 | logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{}) |
| 858 | assert.Nil(t, err) |
| 859 | assert.NotNil(t, logicalDevices) |
| 860 | assert.Equal(t, 1, len(logicalDevices.Items)) |
| 861 | |
| 862 | logicalDevice := logicalDevices.Items[0] |
| 863 | meterID := rand.Uint32() |
| 864 | |
| 865 | // Add a meter to the logical device |
| 866 | meterMod := &ofp.OfpMeterMod{ |
| 867 | Command: ofp.OfpMeterModCommand_OFPMC_ADD, |
| 868 | Flags: rand.Uint32(), |
| 869 | MeterId: meterID, |
| 870 | Bands: []*ofp.OfpMeterBandHeader{ |
| 871 | {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER, |
| 872 | Rate: rand.Uint32(), |
| 873 | BurstSize: rand.Uint32(), |
| 874 | Data: nil, |
| 875 | }, |
| 876 | }, |
| 877 | } |
| 878 | _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDevice.Id, MeterMod: meterMod}) |
| 879 | assert.Nil(t, err) |
| 880 | |
| 881 | // Send initial set of Trap flows |
| 882 | startingVlan := 4091 |
| 883 | nb.sendTrapFlows(t, nbi, logicalDevice, uint64(meterID), startingVlan) |
| 884 | |
| 885 | // Listen for port events |
| 886 | processedLogicalPorts := 0 |
| 887 | for event := range nbi.changeEventQueue { |
| 888 | startingVlan++ |
| 889 | if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok { |
| 890 | ps := portStatus.PortStatus |
| 891 | if ps.Reason == ofp.OfpPortReason_OFPPR_ADD { |
| 892 | processedLogicalPorts++ |
| 893 | if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) { |
| 894 | nb.sendEAPFlows(t, nbi, logicalDevice.Id, ps.Desc, startingVlan, uint64(meterID)) |
| 895 | } |
| 896 | } |
| 897 | } |
| 898 | if processedLogicalPorts >= numNNIPorts+numUNIPorts { |
| 899 | break |
| 900 | } |
| 901 | } |
| 902 | //Verify the flow count on the logical device |
| 903 | nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts) |
| 904 | |
| 905 | // Wait until all flows have been sent to the OLT adapters |
| 906 | var oltVFunc isConditionSatisfied = func() bool { |
| 907 | return nb.oltAdapter.GetFlowCount() >= (numNNIPorts*3)+numNNIPorts*numUNIPorts |
| 908 | } |
| 909 | err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc) |
| 910 | assert.Nil(t, err) |
| 911 | |
| 912 | // Wait until all flows have been sent to the ONU adapters |
| 913 | var onuVFunc isConditionSatisfied = func() bool { |
| 914 | return nb.onuAdapter.GetFlowCount() == numUNIPorts |
| 915 | } |
| 916 | err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc) |
| 917 | assert.Nil(t, err) |
| 918 | } |
| 919 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 920 | func TestSuite1(t *testing.T) { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 921 | f, err := os.Create("profile.cpu") |
| 922 | if err != nil { |
| 923 | log.Fatalf("could not create CPU profile: %v\n ", err) |
| 924 | } |
| 925 | defer f.Close() |
| 926 | runtime.SetBlockProfileRate(1) |
| 927 | runtime.SetMutexProfileFraction(-1) |
| 928 | if err := pprof.StartCPUProfile(f); err != nil { |
| 929 | log.Fatalf("could not start CPU profile: %v\n", err) |
| 930 | } |
| 931 | defer pprof.StopCPUProfile() |
| 932 | |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 933 | nb := newNBTest() |
| 934 | assert.NotNil(t, nb) |
| 935 | |
| 936 | defer nb.stopAll() |
| 937 | |
| 938 | // Start the Core |
| 939 | nb.startCore(false) |
| 940 | |
| 941 | // Set the grpc API interface - no grpc server is running in unit test |
| 942 | nbi := NewAPIHandler(nb.core) |
| 943 | |
| 944 | // 1. Basic test with no data in Core |
| 945 | nb.testCoreWithoutData(t, nbi) |
| 946 | |
| 947 | // Create/register the adapters |
Thomas Lee S | e5a4401 | 2019-11-07 20:32:24 +0530 | [diff] [blame] | 948 | nb.createAndregisterAdapters(t) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 949 | |
| 950 | // 2. Test adapter registration |
| 951 | nb.testAdapterRegistration(t, nbi) |
| 952 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 953 | numberOfDeviceTestRuns := 2 |
| 954 | for i := 1; i <= numberOfDeviceTestRuns; i++ { |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 955 | //3. Test create device |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 956 | nb.testCreateDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 957 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 958 | // 4. Test Enable a device |
| 959 | nb.testEnableDevice(t, nbi) |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 960 | |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 961 | // 5. Test disable and ReEnable a root device |
| 962 | nb.testDisableAndReEnableRootDevice(t, nbi) |
khenaidoo | 67b2215 | 2020-03-02 16:01:25 -0500 | [diff] [blame] | 963 | |
kesavand | bc2d162 | 2020-01-21 00:42:01 -0500 | [diff] [blame] | 964 | // 6. Test disable and Enable pon port of OLT device |
| 965 | nb.testDisableAndEnablePort(t, nbi) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 966 | |
| 967 | // 6. Test disable and delete all devices |
| 968 | nb.testDisableAndDeleteAllDevice(t, nbi) |
Chaitrashree G S | 543df3e | 2020-02-24 22:36:54 -0500 | [diff] [blame] | 969 | |
| 970 | //7. Test enable and delete all devices |
| 971 | nb.testEnableAndDeleteAllDevice(t, nbi) |
khenaidoo | 93d5a3d | 2020-01-15 12:37:05 -0500 | [diff] [blame] | 972 | } |
khenaidoo | b64fc8a | 2019-11-27 15:08:19 -0500 | [diff] [blame] | 973 | |
| 974 | //x. TODO - More tests to come |
| 975 | } |