blob: 2eff4a413a0d70d61236d528798f2c05fb5ce8ec [file] [log] [blame]
khenaidoob64fc8a2019-11-27 15:08:19 -05001/*
Kent Hagerman45a13e42020-04-13 12:23:50 -04002 * 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.
khenaidoob64fc8a2019-11-27 15:08:19 -050015 */
Kent Hagerman45a13e42020-04-13 12:23:50 -040016
Kent Hagerman2b216042020-04-03 18:28:56 -040017package api
khenaidoob64fc8a2019-11-27 15:08:19 -050018
19import (
20 "context"
21 "errors"
22 "fmt"
khenaidoo67b22152020-03-02 16:01:25 -050023 "math/rand"
24 "os"
25 "runtime"
26 "runtime/pprof"
Neha Sharmad1387da2020-05-07 20:07:28 +000027 "strconv"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080028 "strings"
khenaidoo67b22152020-03-02 16:01:25 -050029 "sync"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080030 "testing"
31 "time"
32
khenaidoob64fc8a2019-11-27 15:08:19 -050033 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040034 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050035 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040036 "github.com/opencord/voltha-go/rw_core/core/adapter"
37 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050038 cm "github.com/opencord/voltha-go/rw_core/mocks"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070039 tst "github.com/opencord/voltha-go/rw_core/test"
Kent Hagerman2b216042020-04-03 18:28:56 -040040 "github.com/opencord/voltha-lib-go/v3/pkg/db"
41 "github.com/opencord/voltha-lib-go/v3/pkg/flows"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080042 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
Matteo Scandolod525ae32020-04-02 17:27:29 -070043 mock_etcd "github.com/opencord/voltha-lib-go/v3/pkg/mocks/etcd"
44 mock_kafka "github.com/opencord/voltha-lib-go/v3/pkg/mocks/kafka"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080045 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
46 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050047 "github.com/phayes/freeport"
48 "github.com/stretchr/testify/assert"
49 "google.golang.org/grpc/codes"
50 "google.golang.org/grpc/status"
khenaidoob64fc8a2019-11-27 15:08:19 -050051)
52
53type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070054 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040055 deviceMgr *device.Manager
56 logicalDeviceMgr *device.LogicalManager
57 adapterMgr *adapter.Manager
58 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050059 kClient kafka.Client
60 kvClientPort int
61 numONUPerOLT int
62 startingUNIPortNo int
63 oltAdapter *cm.OLTAdapter
64 onuAdapter *cm.ONUAdapter
65 oltAdapterName string
66 onuAdapterName string
67 coreInstanceID string
68 defaultTimeout time.Duration
69 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050070}
71
72func newNBTest() *NBTest {
73 test := &NBTest{}
74 // Start the embedded etcd server
75 var err error
Mahir Gunyel03de0d32020-06-03 01:36:59 -070076 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer("voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050077 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +000078 logger.Fatal(err)
khenaidoob64fc8a2019-11-27 15:08:19 -050079 }
80 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -070081 test.kClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -050082 test.oltAdapterName = "olt_adapter_mock"
83 test.onuAdapterName = "onu_adapter_mock"
84 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -050085 test.defaultTimeout = 10 * time.Second
86 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -050087 return test
88}
89
90func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +053091 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
92 defer cancel()
khenaidoob64fc8a2019-11-27 15:08:19 -050093 cfg := config.NewRWCoreFlags()
serkant.uluderya8ff291d2020-05-20 00:58:00 -070094 cfg.CoreTopic = "rw_core"
khenaidoo442e7c72020-03-10 16:13:48 -040095 cfg.DefaultRequestTimeout = nb.defaultTimeout
96 cfg.DefaultCoreTimeout = nb.defaultTimeout
khenaidoob64fc8a2019-11-27 15:08:19 -050097 cfg.InCompetingMode = inCompeteMode
Neha Sharmad1387da2020-05-07 20:07:28 +000098 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -050099 grpcPort, err := freeport.GetFreePort()
100 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000101 logger.Fatal("Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500102 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000103 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500104 setCoreCompeteMode(inCompeteMode)
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700105 client := tst.SetupKVClient(cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400106 backend := &db.Backend{
107 Client: client,
108 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000109 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400110 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700111 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400112 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000113 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400114 kafka.MsgClient(nb.kClient),
115 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}),
116 kafka.DeviceDiscoveryTopic(&kafka.Topic{Name: cfg.AffinityRouterTopic}))
117
118 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400119 proxy := model.NewDBPath(backend)
Kent Hagerman2b216042020-04-03 18:28:56 -0400120 nb.adapterMgr = adapter.NewAdapterManager(proxy, nb.coreInstanceID, nb.kClient)
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700121 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg.CoreTopic, nb.coreInstanceID, cfg.DefaultCoreTimeout)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400122 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400123
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400124 if err := nb.kmp.Start(); err != nil {
Kent Hagerman2b216042020-04-03 18:28:56 -0400125 logger.Fatalf("Cannot start InterContainerProxy: %s", err)
126 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400127 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Kent Hagerman2b216042020-04-03 18:28:56 -0400128 if err := nb.kmp.SubscribeWithRequestHandlerInterface(kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
129 logger.Fatalf("Cannot add request handler: %s", err)
130 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500131}
132
khenaidoob64fc8a2019-11-27 15:08:19 -0500133func (nb *NBTest) stopAll() {
134 if nb.kClient != nil {
135 nb.kClient.Stop()
136 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400137 if nb.kmp != nil {
138 nb.kmp.Stop()
khenaidoob64fc8a2019-11-27 15:08:19 -0500139 }
140 if nb.etcdServer != nil {
Mahir Gunyel03de0d32020-06-03 01:36:59 -0700141 tst.StopEmbeddedEtcdServer(nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500142 }
143}
144
Kent Hagerman2b216042020-04-03 18:28:56 -0400145func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500146 // Get the latest set of logical devices
147 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
148 assert.Nil(t, err)
149 assert.NotNil(t, logicalDevices)
150 assert.Equal(t, 1, len(logicalDevices.Items))
151
152 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400153 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
154 assert.Nil(t, err)
155
khenaidoob64fc8a2019-11-27 15:08:19 -0500156 assert.NotEqual(t, "", ld.Id)
157 assert.NotEqual(t, uint64(0), ld.DatapathId)
158 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
159 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
160 assert.NotEqual(t, "", ld.RootDeviceId)
161 assert.NotEqual(t, "", ld.Desc.SerialNum)
162 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
163 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
164 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400165 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500166 assert.Equal(t, oltDevice.ParentId, ld.Id)
167 //Expected port no
168 expectedPortNo := make(map[uint32]bool)
169 expectedPortNo[uint32(2)] = false
170 for i := 0; i < nb.numONUPerOLT; i++ {
171 expectedPortNo[uint32(i+100)] = false
172 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400173 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500174 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
175 assert.Equal(t, uint32(4), p.OfpPort.State)
176 expectedPortNo[p.OfpPort.PortNo] = true
177 if strings.HasPrefix(p.Id, "nni") {
178 assert.Equal(t, true, p.RootPort)
179 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
180 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
181 } else {
182 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
183 assert.Equal(t, false, p.RootPort)
184 }
185 }
186}
187
Kent Hagerman2b216042020-04-03 18:28:56 -0400188func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500189 // Get the latest set of devices
190 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
191 assert.Nil(t, err)
192 assert.NotNil(t, devices)
193
khenaidoo67b22152020-03-02 16:01:25 -0500194 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500195 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
196 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
197 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500198
khenaidoo67b22152020-03-02 16:01:25 -0500199 var wg sync.WaitGroup
200 for _, device := range devices.Items {
201 wg.Add(1)
202 go func(wg *sync.WaitGroup, device *voltha.Device) {
203 // Wait until the device is in the right state
204 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
205 assert.Nil(t, err)
206
207 // Now, verify the details of the device. First get the latest update
208 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
209 assert.Nil(t, err)
210 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
211 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
212 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
213 assert.Equal(t, d.Type, d.Adapter)
214 assert.NotEqual(t, "", d.MacAddress)
215 assert.NotEqual(t, "", d.SerialNumber)
216
217 if d.Type == "olt_adapter_mock" {
218 assert.Equal(t, true, d.Root)
219 assert.NotEqual(t, "", d.Id)
220 assert.NotEqual(t, "", d.ParentId)
221 assert.Nil(t, d.ProxyAddress)
222 } else if d.Type == "onu_adapter_mock" {
223 assert.Equal(t, false, d.Root)
224 assert.NotEqual(t, uint32(0), d.Vlan)
225 assert.NotEqual(t, "", d.Id)
226 assert.NotEqual(t, "", d.ParentId)
227 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
228 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500229 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500230 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500231 }
khenaidoo67b22152020-03-02 16:01:25 -0500232 assert.Equal(t, 2, len(d.Ports))
233 for _, p := range d.Ports {
234 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
235 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
236 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
237 assert.Equal(t, 0, len(p.Peers))
238 } else if p.Type == voltha.Port_PON_OLT {
239 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
240 assert.Equal(t, uint32(1), p.PortNo)
241 } else if p.Type == voltha.Port_PON_ONU {
242 assert.Equal(t, 1, len(p.Peers))
243 assert.Equal(t, uint32(1), p.PortNo)
244 } else {
245 assert.Error(t, errors.New("invalid-port"))
246 }
247 }
248 wg.Done()
249 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500250 }
khenaidoo67b22152020-03-02 16:01:25 -0500251 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500252}
253
Kent Hagerman2b216042020-04-03 18:28:56 -0400254func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500255 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
256 if err != nil {
257 return nil, err
258 }
259 for _, d := range devices.Items {
260 if d.Root == rootDevice {
261 return d, nil
262 }
263 }
264 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
265}
266
Kent Hagerman2b216042020-04-03 18:28:56 -0400267func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500268 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
269 assert.Nil(t, err)
270 assert.NotNil(t, lds)
271 assert.Equal(t, 0, len(lds.Items))
272 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
273 assert.Nil(t, err)
274 assert.NotNil(t, devices)
275 assert.Equal(t, 0, len(devices.Items))
276 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400277 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500278 assert.Nil(t, err)
279 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500280}
281
Kent Hagerman2b216042020-04-03 18:28:56 -0400282func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500283 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
284 assert.Nil(t, err)
285 assert.NotNil(t, adapters)
286 assert.Equal(t, 2, len(adapters.Items))
287 for _, a := range adapters.Items {
288 switch a.Id {
289 case nb.oltAdapterName:
290 assert.Equal(t, "Voltha-olt", a.Vendor)
291 case nb.onuAdapterName:
292 assert.Equal(t, "Voltha-onu", a.Vendor)
293 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000294 logger.Fatal("unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500295 }
296 }
297 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
298 assert.Nil(t, err)
299 assert.NotNil(t, deviceTypes)
300 assert.Equal(t, 2, len(deviceTypes.Items))
301 for _, dt := range deviceTypes.Items {
302 switch dt.Id {
303 case nb.oltAdapterName:
304 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
305 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
306 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
307 case nb.onuAdapterName:
308 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
309 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
310 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
311 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000312 logger.Fatal("invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500313 }
314 }
315}
316
Kent Hagerman2b216042020-04-03 18:28:56 -0400317func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500318 // Create a valid device
319 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
320 assert.Nil(t, err)
321 assert.NotNil(t, oltDevice)
322 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
323 assert.Nil(t, err)
324 assert.NotNil(t, device)
325 assert.Equal(t, oltDevice.String(), device.String())
326
327 // Try to create the same device
328 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
329 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400330 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500331
332 // Try to create a device with invalid data
333 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
334 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530335 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500336
337 // Ensure we only have 1 device in the Core
338 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
339 assert.Nil(t, err)
340 assert.NotNil(t, devices)
341 assert.Equal(t, 1, len(devices.Items))
342 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
343
344 //Remove the device
345 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
346 assert.Nil(t, err)
347
348 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
349 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
350 return devices != nil && len(devices.Items) == 0
351 }
khenaidoo442e7c72020-03-10 16:13:48 -0400352 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500353 assert.Nil(t, err)
354}
355
Kent Hagerman2b216042020-04-03 18:28:56 -0400356func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500357 // Create a device that has no adapter registered
358 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
359 assert.Nil(t, err)
360 assert.NotNil(t, oltDeviceNoAdapter)
361
362 // Try to enable the oltDevice and check the error message
363 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
364 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400365 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500366
367 //Remove the device
368 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
369 assert.Nil(t, err)
370
371 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
372 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
373 return devices != nil && len(devices.Items) == 0
374 }
khenaidoo442e7c72020-03-10 16:13:48 -0400375 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500376 assert.Nil(t, err)
377
khenaidoo67b22152020-03-02 16:01:25 -0500378 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
379 var wg sync.WaitGroup
380 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400381 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500382
khenaidoob64fc8a2019-11-27 15:08:19 -0500383 // Create the device with valid data
384 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
385 assert.Nil(t, err)
386 assert.NotNil(t, oltDevice)
387
388 // Verify oltDevice exist in the core
389 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
390 assert.Nil(t, err)
391 assert.Equal(t, 1, len(devices.Items))
392 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
393
394 // Enable the oltDevice
395 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
396 assert.Nil(t, err)
397
398 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400399 var vldFunction = func(ports []*voltha.LogicalPort) bool {
400 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500401 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400402 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500403 assert.Nil(t, err)
404
405 // Verify that the devices have been setup correctly
406 nb.verifyDevices(t, nbi)
407
408 // Get latest oltDevice data
409 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
410 assert.Nil(t, err)
411
412 // Verify that the logical device has been setup correctly
413 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500414
415 // Wait until all flows has been sent to the devices successfully
416 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500417}
418
Kent Hagerman2b216042020-04-03 18:28:56 -0400419func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500420 //Get an OLT device
421 oltDevice, err := nb.getADevice(true, nbi)
422 assert.Nil(t, err)
423 assert.NotNil(t, oltDevice)
424
425 // Disable the oltDevice
426 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
427 assert.Nil(t, err)
428
429 // Wait for the old device to be disabled
430 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
431 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
432 }
433 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
434 assert.Nil(t, err)
435
436 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400437 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500438 assert.Nil(t, err)
439 for _, onu := range onuDevices.Items {
440 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
441 assert.Nil(t, err)
442 }
443
444 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400445 var vlFunction = func(ports []*voltha.LogicalPort) bool {
446 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500447 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
448 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
449 return false
450 }
451 }
452 return true
453 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400454 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500455 assert.Nil(t, err)
456
457 // Reenable the oltDevice
458 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
459 assert.Nil(t, err)
460
461 // Wait for the old device to be enabled
462 vdFunction = func(device *voltha.Device) bool {
463 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
464 }
465 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
466 assert.Nil(t, err)
467
468 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400469 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500470 assert.Nil(t, err)
471 for _, onu := range onuDevices.Items {
472 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
473 assert.Nil(t, err)
474 }
475
476 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400477 vlFunction = func(ports []*voltha.LogicalPort) bool {
478 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500479 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
480 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
481 return false
482 }
483 }
484 return true
485 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400486 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500487 assert.Nil(t, err)
488}
489
Kent Hagerman2b216042020-04-03 18:28:56 -0400490func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500491 //Get an OLT device
492 oltDevice, err := nb.getADevice(true, nbi)
493 assert.Nil(t, err)
494 assert.NotNil(t, oltDevice)
495
496 // Disable the oltDevice
497 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
498 assert.Nil(t, err)
499
500 // Wait for the olt device to be disabled
501 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
502 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
503 }
504 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
505 assert.Nil(t, err)
506
507 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400508 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500509 assert.Nil(t, err)
510 for _, onu := range onuDevices.Items {
511 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
512 assert.Nil(t, err)
513 }
514
515 // Delete the oltDevice
516 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
517 assert.Nil(t, err)
518
519 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
520 return devices != nil && len(devices.Items) == 0
521 }
522 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
523 assert.Nil(t, err)
524
525 // Wait for absence of logical device
526 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
527 return lds != nil && len(lds.Items) == 0
528 }
529
530 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
531 assert.Nil(t, err)
532}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400533
534func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400535 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
536 if len(devices.Items) == 0 {
537 // Nothing to do
538 return
539 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400540 //Get an OLT device
541 oltDevice, err := nb.getADevice(true, nbi)
542 assert.Nil(t, err)
543 assert.NotNil(t, oltDevice)
544
545 // Delete the oltDevice
546 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
547 assert.Nil(t, err)
548
549 // Wait for all devices to be deleted
550 vFunction := func(devices *voltha.Devices) bool {
551 return devices != nil && len(devices.Items) == 0
552 }
553 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
554 assert.Nil(t, err)
555
556 // Wait for absence of logical device
557 vlFunction := func(lds *voltha.LogicalDevices) bool {
558 return lds != nil && len(lds.Items) == 0
559 }
560
561 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
562 assert.Nil(t, err)
563}
564
Kent Hagerman2b216042020-04-03 18:28:56 -0400565func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500566 //Create the device with valid data
567 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
568 assert.Nil(t, err)
569 assert.NotNil(t, oltDevice)
570
571 //Get an OLT device
572 oltDevice, err = nb.getADevice(true, nbi)
573 assert.Nil(t, err)
574 assert.NotNil(t, oltDevice)
575
576 // Enable the oltDevice
577 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
578 assert.Nil(t, err)
579
580 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400581 var vldFunction = func(ports []*voltha.LogicalPort) bool {
582 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500583 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400584 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500585 assert.Nil(t, err)
586
587 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400588 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500589 assert.Nil(t, err)
590
591 // Wait for the all onu devices to be enabled
592 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
593 return device.AdminState == voltha.AdminState_ENABLED
594 }
595 for _, onu := range onuDevices.Items {
596 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
597 assert.Nil(t, err)
598 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500599 // Wait for each onu device to get deleted
600 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
601 return device == nil
602 }
603
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500604 // Delete the onuDevice
605 for _, onu := range onuDevices.Items {
606 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
607 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500608 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
609 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500610 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500611
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500612 // Disable the oltDevice
613 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
614 assert.Nil(t, err)
615
616 // Wait for the olt device to be disabled
617 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
618 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
619 }
620 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
621 assert.Nil(t, err)
622
623 // Delete the oltDevice
624 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
625 assert.Nil(t, err)
626
627 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
628 return devices != nil && len(devices.Items) == 0
629 }
630 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
631 assert.Nil(t, err)
632}
Kent Hagerman2b216042020-04-03 18:28:56 -0400633func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500634 //Get an OLT device
635 var cp *voltha.Port
636 oltDevice, err := nb.getADevice(true, nbi)
637 assert.Nil(t, err)
638 assert.NotNil(t, oltDevice)
639
640 for _, cp = range oltDevice.Ports {
641 if cp.Type == voltha.Port_PON_OLT {
642 break
643 }
644
645 }
646 assert.NotNil(t, cp)
647 cp.DeviceId = oltDevice.Id
648
649 // Disable the NW Port of oltDevice
650 _, err = nbi.DisablePort(getContext(), cp)
651 assert.Nil(t, err)
652 // Wait for the olt device Port to be disabled
653 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
654 for _, port := range device.Ports {
655 if port.PortNo == cp.PortNo {
656 return port.AdminState == voltha.AdminState_DISABLED
657 }
658 }
659 return false
660 }
661 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
662 assert.Nil(t, err)
663 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400664 var vlFunction = func(ports []*voltha.LogicalPort) bool {
665 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500666 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
667 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
668 return false
669 }
670 }
671 return true
672 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400673 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500674 assert.Nil(t, err)
675
676 // Enable the NW Port of oltDevice
677 _, err = nbi.EnablePort(getContext(), cp)
678 assert.Nil(t, err)
679
680 // Wait for the olt device Port to be enabled
681 vdFunction = func(device *voltha.Device) bool {
682 for _, port := range device.Ports {
683 if port.PortNo == cp.PortNo {
684 return port.AdminState == voltha.AdminState_ENABLED
685 }
686 }
687 return false
688 }
689 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
690 assert.Nil(t, err)
691 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400692 vlFunction = func(ports []*voltha.LogicalPort) bool {
693 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500694 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
695 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
696 return false
697 }
698 }
699 return true
700 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400701 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500702 assert.Nil(t, err)
703
704 // Disable a non-PON port
705 for _, cp = range oltDevice.Ports {
706 if cp.Type != voltha.Port_PON_OLT {
707 break
708 }
709
710 }
711 assert.NotNil(t, cp)
712 cp.DeviceId = oltDevice.Id
713
714 // Disable the NW Port of oltDevice
715 _, err = nbi.DisablePort(getContext(), cp)
716 assert.NotNil(t, err)
717
718}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500719
Kent Hagerman2b216042020-04-03 18:28:56 -0400720func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700721 //Get an OLT device
722 oltDevice, err := nb.getADevice(true, nbi)
723 assert.Nil(t, err)
724 assert.NotNil(t, oltDevice)
725 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
726 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
727
728 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400729 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700730 assert.Nil(t, err)
731 assert.NotNil(t, onuDevices)
732 assert.Greater(t, len(onuDevices.Items), 0)
733
734 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
735 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
736 assert.Nil(t, err)
737
738 var vlFunction0 = func(d *voltha.Device) bool {
739 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
740 }
741
742 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
743 assert.Nil(t, err)
744
745 // Wait for the logical device to satisfy the expected condition
746 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
747 return ld == nil
748 }
749
750 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
751 assert.Nil(t, err)
752
753 // Wait for the device to satisfy the expected condition (device does not have flows)
754 var vlFunction2 = func(d *voltha.Device) bool {
755 var deviceFlows *ofp.Flows
756 var err error
757 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
758 return false
759 }
760 return len(deviceFlows.Items) == 0
761 }
762
763 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
764 assert.Nil(t, err)
765
766 // Wait for the device to satisfy the expected condition (there are no child devices)
767 var vlFunction3 = func(d *voltha.Device) bool {
768 var devices *voltha.Devices
769 var err error
770 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
771 return false
772 }
773 for _, device := range devices.Items {
774 if device.ParentId == d.Id {
775 // We have a child device still left
776 return false
777 }
778 }
779 return true
780 }
781
782 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
783 assert.Nil(t, err)
784
785 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
786 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -0400787 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -0700788 assert.Nil(t, err)
789
790 // Verify the device connection and operation states
791 oltDevice, err = nb.getADevice(true, nbi)
792 assert.Nil(t, err)
793 assert.NotNil(t, oltDevice)
794 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
795 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
796
797 // Wait for the logical device to satisfy the expected condition
798 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
799 return ld != nil
800 }
801 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
802 assert.Nil(t, err)
803
804 // Verify that logical device is created again
805 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
806 assert.Nil(t, err)
807 assert.NotNil(t, logicalDevices)
808 assert.Equal(t, 1, len(logicalDevices.Items))
809
810 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -0400811 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700812 assert.Nil(t, err)
813 assert.NotNil(t, onuDevices)
814 assert.Equal(t, 0, len(onuDevices.Items))
815}
816
Kent Hagerman2b216042020-04-03 18:28:56 -0400817func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -0700818 // -----------------------------------------------------------------------
819 // SubTest 1: Omci test action should fail due to nonexistent device id
820
821 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
822 _, err := nbi.StartOmciTestAction(getContext(), request)
823 assert.NotNil(t, err)
824 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
825
826 // -----------------------------------------------------------------------
827 // SubTest 2: Error should be returned for device with no adapter registered
828
829 // Create a device that has no adapter registered
830 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
831 assert.Nil(t, err)
832 assert.NotNil(t, deviceNoAdapter)
833
834 // Omci test action should fail due to nonexistent adapter
835 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
836 _, err = nbi.StartOmciTestAction(getContext(), request)
837 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400838 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -0700839
840 //Remove the device
841 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
842 assert.Nil(t, err)
843
844 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
845 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
846 return devices != nil && len(devices.Items) == 0
847 }
848 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
849 assert.Nil(t, err)
850
851 // -----------------------------------------------------------------------
852 // SubTest 3: Omci test action should succeed on valid ONU
853
854 // Create the device with valid data
855 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
856 assert.Nil(t, err)
857 assert.NotNil(t, oltDevice)
858
859 // Verify oltDevice exist in the core
860 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
861 assert.Nil(t, err)
862 assert.Equal(t, 1, len(devices.Items))
863 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
864
865 // Enable the oltDevice
866 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
867 assert.Nil(t, err)
868
869 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400870 var vldFunction = func(ports []*voltha.LogicalPort) bool {
871 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -0700872 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400873 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -0700874 assert.Nil(t, err)
875
876 // Wait for the olt device to be enabled
877 vdFunction := func(device *voltha.Device) bool {
878 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
879 }
880 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
881 assert.Nil(t, err)
882
Kent Hagerman2b216042020-04-03 18:28:56 -0400883 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -0700884 assert.Nil(t, err)
885 assert.Greater(t, len(onuDevices.Items), 0)
886
887 onuDevice := onuDevices.Items[0]
888
889 // Omci test action should succeed
890 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
891 resp, err := nbi.StartOmciTestAction(getContext(), request)
892 assert.Nil(t, err)
893 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
894
895 //Remove the device
896 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
897 assert.Nil(t, err)
898 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
899 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
900 assert.Nil(t, err)
901}
902
khenaidoo67b22152020-03-02 16:01:25 -0500903func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
904 matchFields := make([]*ofp.OfpOxmField, 0)
905 for _, val := range fa.MatchFields {
906 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
907 }
908 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
909}
910
911func createMetadata(cTag int, techProfile int, port int) uint64 {
912 md := 0
913 md = (md | (cTag & 0xFFFF)) << 16
914 md = (md | (techProfile & 0xFFFF)) << 32
915 return uint64(md | (port & 0xFFFFFFFF))
916}
917
khenaidoo8b4abbf2020-04-24 17:04:30 -0400918func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -0500919 expectedNumFlows := numNNIPorts*3 + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -0400920 if flowAddFail {
921 expectedNumFlows = 0
922 }
923 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -0500924 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700925 flows, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: lds.Items[0].Id})
926 return lds != nil && len(lds.Items) == 1 && len(flows.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -0500927 }
928 // No timeout implies a success
929 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
930 assert.Nil(t, err)
931}
932
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400933func (nb *NBTest) sendTrapFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, ports []*voltha.LogicalPort, meterID uint64, startingVlan int) (numNNIPorts, numUNIPorts int) {
khenaidoo67b22152020-03-02 16:01:25 -0500934 // Send flows for the parent device
935 var nniPorts []*voltha.LogicalPort
936 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400937 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -0500938 if p.RootPort {
939 nniPorts = append(nniPorts, p)
940 } else {
941 uniPorts = append(uniPorts, p)
942 }
943 }
944 assert.Equal(t, 1, len(nniPorts))
945 //assert.Greater(t, len(uniPorts), 1 )
946 nniPort := nniPorts[0].OfpPort.PortNo
947 maxInt32 := uint64(0xFFFFFFFF)
948 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
949 var fa *flows.FlowArgs
950 fa = &flows.FlowArgs{
951 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
952 MatchFields: []*ofp.OfpOxmOfbField{
953 flows.InPort(nniPort),
954 flows.EthType(35020),
955 },
956 Actions: []*ofp.OfpAction{
957 flows.Output(controllerPortMask),
958 },
959 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400960 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500961 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
962 assert.Nil(t, err)
963
964 fa = &flows.FlowArgs{
965 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
966 MatchFields: []*ofp.OfpOxmOfbField{
967 flows.InPort(nniPort),
968 flows.EthType(2048),
969 flows.IpProto(17),
970 flows.UdpSrc(67),
971 flows.UdpDst(68),
972 },
973 Actions: []*ofp.OfpAction{
974 flows.Output(controllerPortMask),
975 },
976 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400977 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500978 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
979 assert.Nil(t, err)
980
981 fa = &flows.FlowArgs{
982 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
983 MatchFields: []*ofp.OfpOxmOfbField{
984 flows.InPort(nniPort),
985 flows.EthType(34525),
986 flows.IpProto(17),
987 flows.UdpSrc(546),
988 flows.UdpDst(547),
989 },
990 Actions: []*ofp.OfpAction{
991 flows.Output(controllerPortMask),
992 },
993 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400994 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -0500995 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
996 assert.Nil(t, err)
997
998 return len(nniPorts), len(uniPorts)
999}
1000
Kent Hagerman2b216042020-04-03 18:28:56 -04001001func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001002 maxInt32 := uint64(0xFFFFFFFF)
1003 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1004 fa := &flows.FlowArgs{
1005 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},
1006 MatchFields: []*ofp.OfpOxmOfbField{
1007 flows.InPort(port.PortNo),
1008 flows.EthType(34958),
1009 flows.VlanVid(8187),
1010 },
1011 Actions: []*ofp.OfpAction{
1012 flows.Output(controllerPortMask),
1013 },
1014 }
1015 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001016 maxTries := 3
1017 var err error
1018 for {
1019 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1020 if maxTries < 3 {
1021 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1022 }
1023 break
1024 }
1025 t.Log("Sending EAPOL flows fail:", err)
1026 time.Sleep(50 * time.Millisecond)
1027 maxTries--
1028 if maxTries == 0 {
1029 break
1030 }
1031 }
khenaidoo67b22152020-03-02 16:01:25 -05001032 assert.Nil(t, err)
1033}
1034
khenaidoo0db4c812020-05-27 15:27:30 -04001035func (nb *NBTest) monitorLogicalDevice(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, wg *sync.WaitGroup, flowAddFail bool, flowDeleteFail bool) {
khenaidoo67b22152020-03-02 16:01:25 -05001036 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001037
1038 // Clear any existing flows on the adapters
1039 nb.oltAdapter.ClearFlows()
1040 nb.onuAdapter.ClearFlows()
1041
khenaidoo8b4abbf2020-04-24 17:04:30 -04001042 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001043 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1044 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001045
khenaidoo67b22152020-03-02 16:01:25 -05001046 // Wait until a logical device is ready
1047 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
1048 if lds == nil || len(lds.Items) != 1 {
1049 return false
1050 }
1051 // Ensure there are both NNI ports and at least one UNI port on the logical device
1052 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001053 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1054 if err != nil {
1055 return false
1056 }
khenaidoo67b22152020-03-02 16:01:25 -05001057 nniPort := false
1058 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001059 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001060 nniPort = nniPort || p.RootPort == true
1061 uniPort = uniPort || p.RootPort == false
1062 if nniPort && uniPort {
1063 return true
1064 }
1065 }
1066 return false
1067 }
1068 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1069 assert.Nil(t, err)
1070
1071 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1072 assert.Nil(t, err)
1073 assert.NotNil(t, logicalDevices)
1074 assert.Equal(t, 1, len(logicalDevices.Items))
1075
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001076 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001077 meterID := rand.Uint32()
1078
1079 // Add a meter to the logical device
1080 meterMod := &ofp.OfpMeterMod{
1081 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1082 Flags: rand.Uint32(),
1083 MeterId: meterID,
1084 Bands: []*ofp.OfpMeterBandHeader{
1085 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1086 Rate: rand.Uint32(),
1087 BurstSize: rand.Uint32(),
1088 Data: nil,
1089 },
1090 },
1091 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001092 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1093 assert.Nil(t, err)
1094
1095 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001096 assert.Nil(t, err)
1097
1098 // Send initial set of Trap flows
1099 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001100 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001101
1102 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001103 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001104 processedNniLogicalPorts := 0
1105 processedUniLogicalPorts := 0
1106
Kent Hagerman45a13e42020-04-13 12:23:50 -04001107 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001108 startingVlan++
1109 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1110 ps := portStatus.PortStatus
1111 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001112 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001113 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001114 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001115 } else {
1116 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001117 }
1118 }
1119 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001120
1121 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001122 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001123 break
1124 }
1125 }
1126 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001127 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001128
khenaidoo8b4abbf2020-04-24 17:04:30 -04001129 // Wait until all flows have been sent to the OLT adapters (or all failed)
1130 expectedFlowCount := (numNNIPorts * 3) + numNNIPorts*numUNIPorts
1131 if flowAddFail {
1132 expectedFlowCount = 0
1133 }
khenaidoo67b22152020-03-02 16:01:25 -05001134 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001135 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001136 }
1137 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1138 assert.Nil(t, err)
1139
khenaidoo8b4abbf2020-04-24 17:04:30 -04001140 // Wait until all flows have been sent to the ONU adapters (or all failed)
1141 expectedFlowCount = numUNIPorts
1142 if flowAddFail {
1143 expectedFlowCount = 0
1144 }
khenaidoo67b22152020-03-02 16:01:25 -05001145 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001146 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001147 }
1148 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1149 assert.Nil(t, err)
1150}
1151
khenaidoo8b4abbf2020-04-24 17:04:30 -04001152func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1153
1154 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1155 var wg sync.WaitGroup
1156 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001157 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001158
1159 // Create the device with valid data
1160 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1161 assert.Nil(t, err)
1162 assert.NotNil(t, oltDevice)
1163
1164 // Verify oltDevice exist in the core
1165 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1166 assert.Nil(t, err)
1167 assert.Equal(t, 1, len(devices.Items))
1168 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1169
1170 // Enable the oltDevice
1171 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1172 assert.Nil(t, err)
1173
1174 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001175 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1176 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001177 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001178 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001179 assert.Nil(t, err)
1180
1181 // Verify that the devices have been setup correctly
1182 nb.verifyDevices(t, nbi)
1183
1184 // Get latest oltDevice data
1185 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1186 assert.Nil(t, err)
1187
1188 // Verify that the logical device has been setup correctly
1189 nb.verifyLogicalDevices(t, oltDevice, nbi)
1190
1191 // Wait until all flows has been sent to the devices successfully
1192 wg.Wait()
1193}
1194
Matteo Scandolod525ae32020-04-02 17:27:29 -07001195func TestSuiteNbiApiHandler(t *testing.T) {
Kent Hagerman2b216042020-04-03 18:28:56 -04001196 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001197 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001198 logger.Fatalf("could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001199 }
1200 defer f.Close()
1201 runtime.SetBlockProfileRate(1)
1202 runtime.SetMutexProfileFraction(-1)
1203 if err := pprof.StartCPUProfile(f); err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +00001204 logger.Fatalf("could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001205 }
1206 defer pprof.StopCPUProfile()
1207
khenaidoo442e7c72020-03-10 16:13:48 -04001208 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1209
khenaidoob64fc8a2019-11-27 15:08:19 -05001210 nb := newNBTest()
1211 assert.NotNil(t, nb)
1212
1213 defer nb.stopAll()
1214
1215 // Start the Core
1216 nb.startCore(false)
1217
1218 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001219 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001220
1221 // 1. Basic test with no data in Core
1222 nb.testCoreWithoutData(t, nbi)
1223
1224 // Create/register the adapters
Mahir Gunyel03de0d32020-06-03 01:36:59 -07001225 nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr)
1226 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1227 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001228
1229 // 2. Test adapter registration
1230 nb.testAdapterRegistration(t, nbi)
1231
khenaidoo8b4abbf2020-04-24 17:04:30 -04001232 numberOfTestRuns := 2
1233 for i := 1; i <= numberOfTestRuns; i++ {
khenaidoo67b22152020-03-02 16:01:25 -05001234 //3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001235 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001236
khenaidoo93d5a3d2020-01-15 12:37:05 -05001237 // 4. Test Enable a device
1238 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001239
khenaidoo0db4c812020-05-27 15:27:30 -04001240 //// 5. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001241 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001242
kesavandbc2d1622020-01-21 00:42:01 -05001243 // 6. Test disable and Enable pon port of OLT device
1244 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001245
Girish Gowdra408cd962020-03-11 14:31:31 -07001246 // 7.Test Device unreachable when OLT is enabled
1247 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1248
1249 // 8. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001250 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001251
Girish Gowdra408cd962020-03-11 14:31:31 -07001252 // 9. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001253 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001254
1255 // 10. Test omci test
1256 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001257
khenaidoo0db4c812020-05-27 15:27:30 -04001258 // 11. Remove all devices from tests above
1259 nb.deleteAllDevices(t, nbi)
1260
khenaidoo8b4abbf2020-04-24 17:04:30 -04001261 // 11. Test flow add failure
1262 nb.testFlowAddFailure(t, nbi)
1263
1264 // 12. Clean up
1265 nb.deleteAllDevices(t, nbi)
1266 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001267}