blob: 5241a951709dc7c8bb40bd57faa31790ada55ab2 [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
ssiddiqui21e54c32021-07-27 11:30:46 +053033 "github.com/opencord/voltha-lib-go/v5/pkg/log"
34
35 "github.com/golang/protobuf/jsonpb"
36
khenaidoob64fc8a2019-11-27 15:08:19 -050037 "github.com/golang/protobuf/ptypes/empty"
Kent Hagerman2b216042020-04-03 18:28:56 -040038 "github.com/opencord/voltha-go/db/model"
khenaidoob64fc8a2019-11-27 15:08:19 -050039 "github.com/opencord/voltha-go/rw_core/config"
Kent Hagerman2b216042020-04-03 18:28:56 -040040 "github.com/opencord/voltha-go/rw_core/core/adapter"
41 "github.com/opencord/voltha-go/rw_core/core/device"
khenaidoob64fc8a2019-11-27 15:08:19 -050042 cm "github.com/opencord/voltha-go/rw_core/mocks"
Mahir Gunyel03de0d32020-06-03 01:36:59 -070043 tst "github.com/opencord/voltha-go/rw_core/test"
yasin sapli5458a1c2021-06-14 22:24:38 +000044 "github.com/opencord/voltha-lib-go/v5/pkg/db"
45 "github.com/opencord/voltha-lib-go/v5/pkg/events"
46 "github.com/opencord/voltha-lib-go/v5/pkg/flows"
47 "github.com/opencord/voltha-lib-go/v5/pkg/kafka"
48 mock_etcd "github.com/opencord/voltha-lib-go/v5/pkg/mocks/etcd"
49 mock_kafka "github.com/opencord/voltha-lib-go/v5/pkg/mocks/kafka"
Maninderdfadc982020-10-28 14:04:33 +053050 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
51 "github.com/opencord/voltha-protos/v4/go/voltha"
khenaidoob64fc8a2019-11-27 15:08:19 -050052 "github.com/phayes/freeport"
53 "github.com/stretchr/testify/assert"
54 "google.golang.org/grpc/codes"
55 "google.golang.org/grpc/status"
khenaidoob64fc8a2019-11-27 15:08:19 -050056)
57
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030058const numTrapOnNNIFlows = 4
59
khenaidoob64fc8a2019-11-27 15:08:19 -050060type NBTest struct {
Matteo Scandolod525ae32020-04-02 17:27:29 -070061 etcdServer *mock_etcd.EtcdServer
Kent Hagerman2b216042020-04-03 18:28:56 -040062 deviceMgr *device.Manager
63 logicalDeviceMgr *device.LogicalManager
64 adapterMgr *adapter.Manager
65 kmp kafka.InterContainerProxy
khenaidoo67b22152020-03-02 16:01:25 -050066 kClient kafka.Client
Himani Chawlab4c25912020-11-12 17:16:38 +053067 kEventClient kafka.Client
khenaidoo67b22152020-03-02 16:01:25 -050068 kvClientPort int
69 numONUPerOLT int
70 startingUNIPortNo int
71 oltAdapter *cm.OLTAdapter
72 onuAdapter *cm.ONUAdapter
73 oltAdapterName string
74 onuAdapterName string
75 coreInstanceID string
76 defaultTimeout time.Duration
77 maxTimeout time.Duration
khenaidoob64fc8a2019-11-27 15:08:19 -050078}
79
ssiddiqui21e54c32021-07-27 11:30:46 +053080var testLogger log.CLogger
81
82func init() {
83 var err error
84 testLogger, err = log.RegisterPackage(log.JSON, log.InfoLevel, log.Fields{"nbi-handler-test": true})
85 if err != nil {
86 panic(err)
87 }
88
89 if err = log.SetLogLevel(log.InfoLevel); err != nil {
90 panic(err)
91 }
92}
93
Rohan Agrawal31f21802020-06-12 05:38:46 +000094func newNBTest(ctx context.Context) *NBTest {
khenaidoob64fc8a2019-11-27 15:08:19 -050095 test := &NBTest{}
96 // Start the embedded etcd server
97 var err error
Rohan Agrawal31f21802020-06-12 05:38:46 +000098 test.etcdServer, test.kvClientPort, err = tst.StartEmbeddedEtcdServer(ctx, "voltha.rwcore.nb.test", "voltha.rwcore.nb.etcd", "error")
khenaidoob64fc8a2019-11-27 15:08:19 -050099 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000100 logger.Fatal(ctx, err)
khenaidoob64fc8a2019-11-27 15:08:19 -0500101 }
102 // Create the kafka client
Matteo Scandolod525ae32020-04-02 17:27:29 -0700103 test.kClient = mock_kafka.NewKafkaClient()
Himani Chawlab4c25912020-11-12 17:16:38 +0530104 test.kEventClient = mock_kafka.NewKafkaClient()
khenaidoob64fc8a2019-11-27 15:08:19 -0500105 test.oltAdapterName = "olt_adapter_mock"
106 test.onuAdapterName = "onu_adapter_mock"
107 test.coreInstanceID = "rw-nbi-test"
khenaidoo32836732020-03-05 16:10:44 -0500108 test.defaultTimeout = 10 * time.Second
109 test.maxTimeout = 20 * time.Second
khenaidoob64fc8a2019-11-27 15:08:19 -0500110 return test
111}
112
113func (nb *NBTest) startCore(inCompeteMode bool) {
Thomas Lee Se5a44012019-11-07 20:32:24 +0530114 ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
115 defer cancel()
David K. Bainbridge6080c172021-07-24 00:22:28 +0000116 cfg := &config.RWCoreFlags{}
117 cfg.ParseCommandArguments([]string{}) // sets defaults
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700118 cfg.CoreTopic = "rw_core"
Himani Chawlab4c25912020-11-12 17:16:38 +0530119 cfg.EventTopic = "voltha.events"
khenaidoo442e7c72020-03-10 16:13:48 -0400120 cfg.DefaultRequestTimeout = nb.defaultTimeout
121 cfg.DefaultCoreTimeout = nb.defaultTimeout
Neha Sharmad1387da2020-05-07 20:07:28 +0000122 cfg.KVStoreAddress = "127.0.0.1" + ":" + strconv.Itoa(nb.kvClientPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500123 grpcPort, err := freeport.GetFreePort()
124 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000125 logger.Fatal(ctx, "Cannot get a freeport for grpc")
khenaidoob64fc8a2019-11-27 15:08:19 -0500126 }
Neha Sharmad1387da2020-05-07 20:07:28 +0000127 cfg.GrpcAddress = "127.0.0.1" + ":" + strconv.Itoa(grpcPort)
khenaidoob64fc8a2019-11-27 15:08:19 -0500128 setCoreCompeteMode(inCompeteMode)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000129 client := tst.SetupKVClient(ctx, cfg, nb.coreInstanceID)
Kent Hagerman2b216042020-04-03 18:28:56 -0400130 backend := &db.Backend{
131 Client: client,
132 StoreType: cfg.KVStoreType,
Neha Sharmad1387da2020-05-07 20:07:28 +0000133 Address: cfg.KVStoreAddress,
Kent Hagerman2b216042020-04-03 18:28:56 -0400134 Timeout: cfg.KVStoreTimeout,
serkant.uluderya8ff291d2020-05-20 00:58:00 -0700135 LivenessChannelInterval: cfg.LiveProbeInterval / 2}
Kent Hagerman2b216042020-04-03 18:28:56 -0400136 nb.kmp = kafka.NewInterContainerProxy(
Neha Sharmad1387da2020-05-07 20:07:28 +0000137 kafka.InterContainerAddress(cfg.KafkaAdapterAddress),
Kent Hagerman2b216042020-04-03 18:28:56 -0400138 kafka.MsgClient(nb.kClient),
David Bainbridge9ae13132020-06-22 17:28:01 -0700139 kafka.DefaultTopic(&kafka.Topic{Name: cfg.CoreTopic}))
Kent Hagerman2b216042020-04-03 18:28:56 -0400140
141 endpointMgr := kafka.NewEndpointManager(backend)
Kent Hagermanf5a67352020-04-30 15:15:26 -0400142 proxy := model.NewDBPath(backend)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000143 nb.adapterMgr = adapter.NewAdapterManager(ctx, proxy, nb.coreInstanceID, nb.kClient)
Himani Chawlab4c25912020-11-12 17:16:38 +0530144 eventProxy := events.NewEventProxy(events.MsgClient(nb.kEventClient), events.MsgTopic(kafka.Topic{Name: cfg.EventTopic}))
Maninder0aabf0c2021-03-17 14:55:14 +0530145 nb.deviceMgr, nb.logicalDeviceMgr = device.NewManagers(proxy, nb.adapterMgr, nb.kmp, endpointMgr, cfg, nb.coreInstanceID, eventProxy)
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400146 nb.adapterMgr.Start(ctx)
Kent Hagerman2b216042020-04-03 18:28:56 -0400147
Rohan Agrawal31f21802020-06-12 05:38:46 +0000148 if err := nb.kmp.Start(ctx); err != nil {
149 logger.Fatalf(ctx, "Cannot start InterContainerProxy: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400150 }
Kent Hagerman2f0d0552020-04-23 17:28:52 -0400151 requestProxy := NewAdapterRequestHandlerProxy(nb.deviceMgr, nb.adapterMgr)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000152 if err := nb.kmp.SubscribeWithRequestHandlerInterface(ctx, kafka.Topic{Name: cfg.CoreTopic}, requestProxy); err != nil {
153 logger.Fatalf(ctx, "Cannot add request handler: %s", err)
Kent Hagerman2b216042020-04-03 18:28:56 -0400154 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500155}
156
Rohan Agrawal31f21802020-06-12 05:38:46 +0000157func (nb *NBTest) stopAll(ctx context.Context) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500158 if nb.kClient != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000159 nb.kClient.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500160 }
Kent Hagerman2b216042020-04-03 18:28:56 -0400161 if nb.kmp != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000162 nb.kmp.Stop(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -0500163 }
164 if nb.etcdServer != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000165 tst.StopEmbeddedEtcdServer(ctx, nb.etcdServer)
khenaidoob64fc8a2019-11-27 15:08:19 -0500166 }
Himani Chawlab4c25912020-11-12 17:16:38 +0530167 if nb.kEventClient != nil {
168 nb.kEventClient.Stop(ctx)
169 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500170}
171
Kent Hagerman2b216042020-04-03 18:28:56 -0400172func (nb *NBTest) verifyLogicalDevices(t *testing.T, oltDevice *voltha.Device, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500173 // Get the latest set of logical devices
174 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
175 assert.Nil(t, err)
176 assert.NotNil(t, logicalDevices)
177 assert.Equal(t, 1, len(logicalDevices.Items))
178
179 ld := logicalDevices.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400180 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
181 assert.Nil(t, err)
182
khenaidoob64fc8a2019-11-27 15:08:19 -0500183 assert.NotEqual(t, "", ld.Id)
184 assert.NotEqual(t, uint64(0), ld.DatapathId)
185 assert.Equal(t, "olt_adapter_mock", ld.Desc.HwDesc)
186 assert.Equal(t, "olt_adapter_mock", ld.Desc.SwDesc)
187 assert.NotEqual(t, "", ld.RootDeviceId)
188 assert.NotEqual(t, "", ld.Desc.SerialNum)
189 assert.Equal(t, uint32(256), ld.SwitchFeatures.NBuffers)
190 assert.Equal(t, uint32(2), ld.SwitchFeatures.NTables)
191 assert.Equal(t, uint32(15), ld.SwitchFeatures.Capabilities)
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400192 assert.Equal(t, 1+nb.numONUPerOLT, len(ports.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500193 assert.Equal(t, oltDevice.ParentId, ld.Id)
194 //Expected port no
195 expectedPortNo := make(map[uint32]bool)
196 expectedPortNo[uint32(2)] = false
197 for i := 0; i < nb.numONUPerOLT; i++ {
198 expectedPortNo[uint32(i+100)] = false
199 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400200 for _, p := range ports.Items {
khenaidoob64fc8a2019-11-27 15:08:19 -0500201 assert.Equal(t, p.OfpPort.PortNo, p.DevicePortNo)
202 assert.Equal(t, uint32(4), p.OfpPort.State)
203 expectedPortNo[p.OfpPort.PortNo] = true
204 if strings.HasPrefix(p.Id, "nni") {
205 assert.Equal(t, true, p.RootPort)
206 //assert.Equal(t, uint32(2), p.OfpPort.PortNo)
207 assert.Equal(t, p.Id, fmt.Sprintf("nni-%d", p.DevicePortNo))
208 } else {
209 assert.Equal(t, p.Id, fmt.Sprintf("uni-%d", p.DevicePortNo))
210 assert.Equal(t, false, p.RootPort)
211 }
212 }
213}
214
Kent Hagerman2b216042020-04-03 18:28:56 -0400215func (nb *NBTest) verifyDevices(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500216 // Get the latest set of devices
217 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
218 assert.Nil(t, err)
219 assert.NotNil(t, devices)
220
khenaidoo67b22152020-03-02 16:01:25 -0500221 // A device is ready to be examined when its ADMIN state is ENABLED and OPERATIONAL state is ACTIVE
khenaidoob64fc8a2019-11-27 15:08:19 -0500222 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
223 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
224 }
khenaidoob64fc8a2019-11-27 15:08:19 -0500225
khenaidoo67b22152020-03-02 16:01:25 -0500226 var wg sync.WaitGroup
227 for _, device := range devices.Items {
228 wg.Add(1)
229 go func(wg *sync.WaitGroup, device *voltha.Device) {
230 // Wait until the device is in the right state
231 err := waitUntilDeviceReadiness(device.Id, nb.maxTimeout, vFunction, nbi)
232 assert.Nil(t, err)
233
234 // Now, verify the details of the device. First get the latest update
235 d, err := nbi.GetDevice(getContext(), &voltha.ID{Id: device.Id})
236 assert.Nil(t, err)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400237 dPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: device.Id})
238 assert.Nil(t, err)
khenaidoo67b22152020-03-02 16:01:25 -0500239 assert.Equal(t, voltha.AdminState_ENABLED, d.AdminState)
240 assert.Equal(t, voltha.ConnectStatus_REACHABLE, d.ConnectStatus)
241 assert.Equal(t, voltha.OperStatus_ACTIVE, d.OperStatus)
242 assert.Equal(t, d.Type, d.Adapter)
243 assert.NotEqual(t, "", d.MacAddress)
244 assert.NotEqual(t, "", d.SerialNumber)
245
246 if d.Type == "olt_adapter_mock" {
247 assert.Equal(t, true, d.Root)
248 assert.NotEqual(t, "", d.Id)
249 assert.NotEqual(t, "", d.ParentId)
250 assert.Nil(t, d.ProxyAddress)
251 } else if d.Type == "onu_adapter_mock" {
252 assert.Equal(t, false, d.Root)
253 assert.NotEqual(t, uint32(0), d.Vlan)
254 assert.NotEqual(t, "", d.Id)
255 assert.NotEqual(t, "", d.ParentId)
256 assert.NotEqual(t, "", d.ProxyAddress.DeviceId)
257 assert.Equal(t, "olt_adapter_mock", d.ProxyAddress.DeviceType)
khenaidoob64fc8a2019-11-27 15:08:19 -0500258 } else {
khenaidoo67b22152020-03-02 16:01:25 -0500259 assert.Error(t, errors.New("invalid-device-type"))
khenaidoob64fc8a2019-11-27 15:08:19 -0500260 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400261 assert.Equal(t, 2, len(dPorts.Items))
262 for _, p := range dPorts.Items {
khenaidoo67b22152020-03-02 16:01:25 -0500263 assert.Equal(t, voltha.AdminState_ENABLED, p.AdminState)
264 assert.Equal(t, voltha.OperStatus_ACTIVE, p.OperStatus)
265 if p.Type == voltha.Port_ETHERNET_NNI || p.Type == voltha.Port_ETHERNET_UNI {
266 assert.Equal(t, 0, len(p.Peers))
267 } else if p.Type == voltha.Port_PON_OLT {
268 assert.Equal(t, nb.numONUPerOLT, len(p.Peers))
269 assert.Equal(t, uint32(1), p.PortNo)
270 } else if p.Type == voltha.Port_PON_ONU {
271 assert.Equal(t, 1, len(p.Peers))
272 assert.Equal(t, uint32(1), p.PortNo)
273 } else {
274 assert.Error(t, errors.New("invalid-port"))
275 }
276 }
277 wg.Done()
278 }(&wg, device)
khenaidoob64fc8a2019-11-27 15:08:19 -0500279 }
khenaidoo67b22152020-03-02 16:01:25 -0500280 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500281}
282
Kent Hagerman2b216042020-04-03 18:28:56 -0400283func (nb *NBTest) getADevice(rootDevice bool, nbi *NBIHandler) (*voltha.Device, error) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500284 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
285 if err != nil {
286 return nil, err
287 }
288 for _, d := range devices.Items {
289 if d.Root == rootDevice {
290 return d, nil
291 }
292 }
293 return nil, status.Errorf(codes.NotFound, "%v device not found", rootDevice)
294}
295
Kent Hagerman2b216042020-04-03 18:28:56 -0400296func (nb *NBTest) testCoreWithoutData(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500297 lds, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
298 assert.Nil(t, err)
299 assert.NotNil(t, lds)
300 assert.Equal(t, 0, len(lds.Items))
301 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
302 assert.Nil(t, err)
303 assert.NotNil(t, devices)
304 assert.Equal(t, 0, len(devices.Items))
305 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
khenaidoo442e7c72020-03-10 16:13:48 -0400306 assert.Equal(t, 0, len(adapters.Items))
khenaidoob64fc8a2019-11-27 15:08:19 -0500307 assert.Nil(t, err)
308 assert.NotNil(t, adapters)
khenaidoob64fc8a2019-11-27 15:08:19 -0500309}
310
Kent Hagerman2b216042020-04-03 18:28:56 -0400311func (nb *NBTest) testAdapterRegistration(t *testing.T, nbi *NBIHandler) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000312 ctx := context.Background()
khenaidoob64fc8a2019-11-27 15:08:19 -0500313 adapters, err := nbi.ListAdapters(getContext(), &empty.Empty{})
314 assert.Nil(t, err)
315 assert.NotNil(t, adapters)
316 assert.Equal(t, 2, len(adapters.Items))
317 for _, a := range adapters.Items {
318 switch a.Id {
319 case nb.oltAdapterName:
320 assert.Equal(t, "Voltha-olt", a.Vendor)
321 case nb.onuAdapterName:
322 assert.Equal(t, "Voltha-onu", a.Vendor)
323 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000324 logger.Fatal(ctx, "unregistered-adapter", a.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500325 }
326 }
327 deviceTypes, err := nbi.ListDeviceTypes(getContext(), &empty.Empty{})
328 assert.Nil(t, err)
329 assert.NotNil(t, deviceTypes)
330 assert.Equal(t, 2, len(deviceTypes.Items))
331 for _, dt := range deviceTypes.Items {
332 switch dt.Id {
333 case nb.oltAdapterName:
334 assert.Equal(t, nb.oltAdapterName, dt.Adapter)
335 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
336 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
337 case nb.onuAdapterName:
338 assert.Equal(t, nb.onuAdapterName, dt.Adapter)
339 assert.Equal(t, false, dt.AcceptsBulkFlowUpdate)
340 assert.Equal(t, true, dt.AcceptsAddRemoveFlowUpdates)
341 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000342 logger.Fatal(ctx, "invalid-device-type", dt.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500343 }
344 }
345}
346
Kent Hagerman2b216042020-04-03 18:28:56 -0400347func (nb *NBTest) testCreateDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500348 // Create a valid device
349 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
350 assert.Nil(t, err)
351 assert.NotNil(t, oltDevice)
352 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
353 assert.Nil(t, err)
354 assert.NotNil(t, device)
355 assert.Equal(t, oltDevice.String(), device.String())
356
357 // Try to create the same device
358 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
359 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400360 assert.Equal(t, "device is already pre-provisioned", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500361
362 // Try to create a device with invalid data
363 _, err = nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName})
364 assert.NotNil(t, err)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530365 assert.Equal(t, "no-device-info-present; MAC or HOSTIP&PORT", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500366
367 // Ensure we only have 1 device in the Core
368 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
369 assert.Nil(t, err)
370 assert.NotNil(t, devices)
371 assert.Equal(t, 1, len(devices.Items))
372 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
373
374 //Remove the device
375 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
376 assert.Nil(t, err)
377
378 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
379 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
380 return devices != nil && len(devices.Items) == 0
381 }
khenaidoo442e7c72020-03-10 16:13:48 -0400382 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500383 assert.Nil(t, err)
384}
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530385func (nb *NBTest) enableDevice(t *testing.T, nbi *NBIHandler, oltDevice *voltha.Device) {
386 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
387 var wg sync.WaitGroup
388 wg.Add(1)
389 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
390
391 // Enable the oltDevice
392 _, err := nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
393 assert.Nil(t, err)
394
395 // Wait for the logical device to be in the ready state
396 var vldFunction = func(ports []*voltha.LogicalPort) bool {
397 return len(ports) == nb.numONUPerOLT+1
398 }
399 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
400 assert.Nil(t, err)
401
402 // Verify that the devices have been setup correctly
403 nb.verifyDevices(t, nbi)
404
405 // Get latest oltDevice data
406 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
407 assert.Nil(t, err)
408
409 // Verify that the logical device has been setup correctly
410 nb.verifyLogicalDevices(t, oltDevice, nbi)
411
412 // Wait until all flows has been sent to the devices successfully
413 wg.Wait()
414
415}
416func (nb *NBTest) testForceDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
417 // Create a valid device
418 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
419 assert.Nil(t, err)
420 assert.NotNil(t, oltDevice)
421 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
422 assert.Nil(t, err)
423 assert.NotNil(t, device)
424 assert.Equal(t, oltDevice.String(), device.String())
425
426 // Ensure we only have 1 device in the Core
427 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
428 assert.Nil(t, err)
429 assert.NotNil(t, devices)
430 assert.Equal(t, 1, len(devices.Items))
431 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
432
433 //Remove the device forcefully
434 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
435 assert.Nil(t, err)
436
437 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
438 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
439 return devices != nil && len(devices.Items) == 0
440 }
441 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
442 assert.Nil(t, err)
443}
444
445func (nb *NBTest) testForceDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
446 // Create a valid device
447 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
448 assert.Nil(t, err)
449 assert.NotNil(t, oltDevice)
450 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
451 assert.Nil(t, err)
452 assert.NotNil(t, device)
453 assert.Equal(t, oltDevice.String(), device.String())
454
455 nb.enableDevice(t, nbi, oltDevice)
456
457 //Remove the device forcefully
458 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
459 assert.Nil(t, err)
460
461 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
462 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
463 return devices != nil && len(devices.Items) == 0
464 }
465 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
466 assert.Nil(t, err)
467}
468
469func (nb *NBTest) testDeletePreProvDevice(t *testing.T, nbi *NBIHandler) {
470 // Create a valid device
471 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
472 assert.Nil(t, err)
473 assert.NotNil(t, oltDevice)
474 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
475 assert.Nil(t, err)
476 assert.NotNil(t, device)
477 assert.Equal(t, oltDevice.String(), device.String())
478
479 // Ensure we only have 1 device in the Core
480 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
481 assert.Nil(t, err)
482 assert.NotNil(t, devices)
483 assert.Equal(t, 1, len(devices.Items))
484 assert.Equal(t, oltDevice.String(), devices.Items[0].String())
485
486 //Remove the device forcefully
487 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
488 assert.Nil(t, err)
489
490 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
491 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
492 return devices != nil && len(devices.Items) == 0
493 }
494 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
495 assert.Nil(t, err)
496}
497
498func (nb *NBTest) testDeleteEnabledDevice(t *testing.T, nbi *NBIHandler) {
499 // Create a valid device
500 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
501 assert.Nil(t, err)
502 assert.NotNil(t, oltDevice)
503 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
504 assert.Nil(t, err)
505 assert.NotNil(t, device)
506 assert.Equal(t, oltDevice.String(), device.String())
507
508 nb.enableDevice(t, nbi, oltDevice)
509
510 //Remove the device
511 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
512 assert.Nil(t, err)
513
514 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
515 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
516 return devices != nil && len(devices.Items) == 0
517 }
518 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
519 assert.Nil(t, err)
520}
521
522func (nb *NBTest) testForceDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
523 // Create a valid device
524 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
525 assert.Nil(t, err)
526 assert.NotNil(t, oltDevice)
527 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
528 assert.Nil(t, err)
529 assert.NotNil(t, device)
530 assert.Equal(t, oltDevice.String(), device.String())
531
532 nb.enableDevice(t, nbi, oltDevice)
533 nb.oltAdapter.SetDeleteAction(true)
534 //Remove the device
535 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
536 assert.Nil(t, err)
537
538 //Ensure there are no devices in the Core although delete was failed - wait until condition satisfied or timeout
539 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
540 return devices != nil && len(devices.Items) == 0
541 }
542 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
543 assert.Nil(t, err)
544
545}
546
547func (nb *NBTest) testDeleteDeviceFailure(t *testing.T, nbi *NBIHandler) {
548 // Create a valid device
549 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
550 assert.Nil(t, err)
551 assert.NotNil(t, oltDevice)
552 device, err := nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
553 assert.Nil(t, err)
554 assert.NotNil(t, device)
555 assert.Equal(t, oltDevice.String(), device.String())
556
557 nb.enableDevice(t, nbi, oltDevice)
558
559 nb.oltAdapter.SetDeleteAction(true)
560 //Remove the device
561 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
562 assert.Nil(t, err)
563
564 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
565 var vFunction1 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
Manindera496f852021-02-22 09:57:56 +0530566 state, err := nbi.GetTransientState(getContext(), oltDevice.Id)
567 if err != nil {
568 return false
569 }
570 return devices != nil && len(devices.Items) == (nb.numONUPerOLT+1) &&
571 state == voltha.DeviceTransientState_DELETE_FAILED
Himani Chawla2ba1c9c2020-10-07 13:19:03 +0530572 }
573 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction1)
574 assert.Nil(t, err)
575
576 nb.oltAdapter.SetDeleteAction(false)
577
578 // Now Force Delete this device
579 _, err = nbi.ForceDeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
580 assert.Nil(t, err)
581
582 //Ensure there are devices in the Core as delete was failed - wait until condition satisfied or timeout
583 var vFunction2 isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
584 return devices != nil && len(devices.Items) == 0
585 }
586 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction2)
587 assert.Nil(t, err)
588
589}
khenaidoob64fc8a2019-11-27 15:08:19 -0500590
Kent Hagerman2b216042020-04-03 18:28:56 -0400591func (nb *NBTest) testEnableDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500592 // Create a device that has no adapter registered
593 oltDeviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegistered", MacAddress: "aa:bb:cc:cc:ee:ff"})
594 assert.Nil(t, err)
595 assert.NotNil(t, oltDeviceNoAdapter)
596
597 // Try to enable the oltDevice and check the error message
598 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
599 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -0400600 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegistered", err.Error())
khenaidoob64fc8a2019-11-27 15:08:19 -0500601
602 //Remove the device
603 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDeviceNoAdapter.Id})
604 assert.Nil(t, err)
605
606 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
607 var vdFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
608 return devices != nil && len(devices.Items) == 0
609 }
khenaidoo442e7c72020-03-10 16:13:48 -0400610 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vdFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500611 assert.Nil(t, err)
612
khenaidoo67b22152020-03-02 16:01:25 -0500613 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
614 var wg sync.WaitGroup
615 wg.Add(1)
khenaidoo8b4abbf2020-04-24 17:04:30 -0400616 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, false, false)
khenaidoo67b22152020-03-02 16:01:25 -0500617
khenaidoob64fc8a2019-11-27 15:08:19 -0500618 // Create the device with valid data
619 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
620 assert.Nil(t, err)
621 assert.NotNil(t, oltDevice)
622
623 // Verify oltDevice exist in the core
624 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
625 assert.Nil(t, err)
626 assert.Equal(t, 1, len(devices.Items))
627 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
628
629 // Enable the oltDevice
630 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
631 assert.Nil(t, err)
632
633 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400634 var vldFunction = func(ports []*voltha.LogicalPort) bool {
635 return len(ports) == nb.numONUPerOLT+1
khenaidoob64fc8a2019-11-27 15:08:19 -0500636 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400637 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500638 assert.Nil(t, err)
639
640 // Verify that the devices have been setup correctly
641 nb.verifyDevices(t, nbi)
642
643 // Get latest oltDevice data
644 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
645 assert.Nil(t, err)
646
647 // Verify that the logical device has been setup correctly
648 nb.verifyLogicalDevices(t, oltDevice, nbi)
khenaidoo67b22152020-03-02 16:01:25 -0500649
650 // Wait until all flows has been sent to the devices successfully
651 wg.Wait()
khenaidoob64fc8a2019-11-27 15:08:19 -0500652}
653
Kent Hagerman2b216042020-04-03 18:28:56 -0400654func (nb *NBTest) testDisableAndReEnableRootDevice(t *testing.T, nbi *NBIHandler) {
khenaidoob64fc8a2019-11-27 15:08:19 -0500655 //Get an OLT device
656 oltDevice, err := nb.getADevice(true, nbi)
657 assert.Nil(t, err)
658 assert.NotNil(t, oltDevice)
659
660 // Disable the oltDevice
661 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
662 assert.Nil(t, err)
663
664 // Wait for the old device to be disabled
665 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
666 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
667 }
668 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
669 assert.Nil(t, err)
670
671 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400672 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500673 assert.Nil(t, err)
674 for _, onu := range onuDevices.Items {
675 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
676 assert.Nil(t, err)
677 }
678
679 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400680 var vlFunction = func(ports []*voltha.LogicalPort) bool {
681 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500682 if (lp.OfpPort.Config&uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
683 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LINK_DOWN) {
684 return false
685 }
686 }
687 return true
688 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400689 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500690 assert.Nil(t, err)
691
692 // Reenable the oltDevice
693 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
694 assert.Nil(t, err)
695
696 // Wait for the old device to be enabled
697 vdFunction = func(device *voltha.Device) bool {
698 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
699 }
700 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
701 assert.Nil(t, err)
702
703 // Verify that all onu devices are enabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400704 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoob64fc8a2019-11-27 15:08:19 -0500705 assert.Nil(t, err)
706 for _, onu := range onuDevices.Items {
707 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
708 assert.Nil(t, err)
709 }
710
711 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400712 vlFunction = func(ports []*voltha.LogicalPort) bool {
713 for _, lp := range ports {
khenaidoob64fc8a2019-11-27 15:08:19 -0500714 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
715 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
716 return false
717 }
718 }
719 return true
720 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400721 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
khenaidoob64fc8a2019-11-27 15:08:19 -0500722 assert.Nil(t, err)
723}
724
Kent Hagerman2b216042020-04-03 18:28:56 -0400725func (nb *NBTest) testDisableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
khenaidoo93d5a3d2020-01-15 12:37:05 -0500726 //Get an OLT device
727 oltDevice, err := nb.getADevice(true, nbi)
728 assert.Nil(t, err)
729 assert.NotNil(t, oltDevice)
730
731 // Disable the oltDevice
732 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
733 assert.Nil(t, err)
734
735 // Wait for the olt device to be disabled
736 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
737 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
738 }
739 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
740 assert.Nil(t, err)
741
742 // Verify that all onu devices are disabled as well
Kent Hagerman2b216042020-04-03 18:28:56 -0400743 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
khenaidoo93d5a3d2020-01-15 12:37:05 -0500744 assert.Nil(t, err)
745 for _, onu := range onuDevices.Items {
746 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
747 assert.Nil(t, err)
748 }
749
750 // Delete the oltDevice
751 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
752 assert.Nil(t, err)
753
754 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
755 return devices != nil && len(devices.Items) == 0
756 }
757 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
758 assert.Nil(t, err)
759
760 // Wait for absence of logical device
761 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
762 return lds != nil && len(lds.Items) == 0
763 }
764
765 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
766 assert.Nil(t, err)
767}
khenaidoo8b4abbf2020-04-24 17:04:30 -0400768
769func (nb *NBTest) deleteAllDevices(t *testing.T, nbi *NBIHandler) {
khenaidoo0db4c812020-05-27 15:27:30 -0400770 devices, _ := nbi.ListDevices(getContext(), &empty.Empty{})
771 if len(devices.Items) == 0 {
772 // Nothing to do
773 return
774 }
khenaidoo8b4abbf2020-04-24 17:04:30 -0400775 //Get an OLT device
776 oltDevice, err := nb.getADevice(true, nbi)
777 assert.Nil(t, err)
778 assert.NotNil(t, oltDevice)
779
780 // Delete the oltDevice
781 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
782 assert.Nil(t, err)
783
784 // Wait for all devices to be deleted
785 vFunction := func(devices *voltha.Devices) bool {
786 return devices != nil && len(devices.Items) == 0
787 }
788 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
789 assert.Nil(t, err)
790
791 // Wait for absence of logical device
792 vlFunction := func(lds *voltha.LogicalDevices) bool {
793 return lds != nil && len(lds.Items) == 0
794 }
795
796 err = waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
797 assert.Nil(t, err)
798}
799
Kent Hagerman2b216042020-04-03 18:28:56 -0400800func (nb *NBTest) testEnableAndDeleteAllDevice(t *testing.T, nbi *NBIHandler) {
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500801 //Create the device with valid data
802 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
803 assert.Nil(t, err)
804 assert.NotNil(t, oltDevice)
805
806 //Get an OLT device
807 oltDevice, err = nb.getADevice(true, nbi)
808 assert.Nil(t, err)
809 assert.NotNil(t, oltDevice)
810
811 // Enable the oltDevice
812 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
813 assert.Nil(t, err)
814
815 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400816 var vldFunction = func(ports []*voltha.LogicalPort) bool {
817 return len(ports) == nb.numONUPerOLT+1
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500818 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400819 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500820 assert.Nil(t, err)
821
822 //Get all child devices
Kent Hagerman2b216042020-04-03 18:28:56 -0400823 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500824 assert.Nil(t, err)
825
826 // Wait for the all onu devices to be enabled
827 var vdFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
828 return device.AdminState == voltha.AdminState_ENABLED
829 }
830 for _, onu := range onuDevices.Items {
831 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunction, nbi)
832 assert.Nil(t, err)
833 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500834 // Wait for each onu device to get deleted
835 var vdFunc isDeviceConditionSatisfied = func(device *voltha.Device) bool {
836 return device == nil
837 }
838
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500839 // Delete the onuDevice
840 for _, onu := range onuDevices.Items {
841 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: onu.Id})
842 assert.Nil(t, err)
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500843 err = waitUntilDeviceReadiness(onu.Id, nb.maxTimeout, vdFunc, nbi)
844 assert.Nil(t, err)
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500845 }
Chaitrashree G Se8ad0202020-02-27 18:48:00 -0500846
Chaitrashree G S543df3e2020-02-24 22:36:54 -0500847 // Disable the oltDevice
848 _, err = nbi.DisableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
849 assert.Nil(t, err)
850
851 // Wait for the olt device to be disabled
852 var vFunction isDeviceConditionSatisfied = func(device *voltha.Device) bool {
853 return device.AdminState == voltha.AdminState_DISABLED && device.OperStatus == voltha.OperStatus_UNKNOWN
854 }
855 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vFunction, nbi)
856 assert.Nil(t, err)
857
858 // Delete the oltDevice
859 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
860 assert.Nil(t, err)
861
862 var vFunc isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
863 return devices != nil && len(devices.Items) == 0
864 }
865 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunc)
866 assert.Nil(t, err)
867}
Kent Hagerman2b216042020-04-03 18:28:56 -0400868func (nb *NBTest) testDisableAndEnablePort(t *testing.T, nbi *NBIHandler) {
kesavandbc2d1622020-01-21 00:42:01 -0500869 //Get an OLT device
870 var cp *voltha.Port
871 oltDevice, err := nb.getADevice(true, nbi)
872 assert.Nil(t, err)
873 assert.NotNil(t, oltDevice)
Kent Hagerman2a07b862020-06-19 15:23:07 -0400874 oltPorts, err := nbi.ListDevicePorts(getContext(), &voltha.ID{Id: oltDevice.Id})
875 assert.Nil(t, err)
kesavandbc2d1622020-01-21 00:42:01 -0500876
Kent Hagerman2a07b862020-06-19 15:23:07 -0400877 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500878 if cp.Type == voltha.Port_PON_OLT {
879 break
880 }
881
882 }
883 assert.NotNil(t, cp)
884 cp.DeviceId = oltDevice.Id
885
886 // Disable the NW Port of oltDevice
887 _, err = nbi.DisablePort(getContext(), cp)
888 assert.Nil(t, err)
889 // Wait for the olt device Port to be disabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400890 var vdFunction isDevicePortsConditionSatisfied = func(ports *voltha.Ports) bool {
891 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500892 if port.PortNo == cp.PortNo {
893 return port.AdminState == voltha.AdminState_DISABLED
894 }
895 }
896 return false
897 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400898 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500899 assert.Nil(t, err)
900 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400901 var vlFunction = func(ports []*voltha.LogicalPort) bool {
902 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500903 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
904 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
905 return false
906 }
907 }
908 return true
909 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400910 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500911 assert.Nil(t, err)
912
913 // Enable the NW Port of oltDevice
914 _, err = nbi.EnablePort(getContext(), cp)
915 assert.Nil(t, err)
916
917 // Wait for the olt device Port to be enabled
Kent Hagerman2a07b862020-06-19 15:23:07 -0400918 vdFunction = func(ports *voltha.Ports) bool {
919 for _, port := range ports.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500920 if port.PortNo == cp.PortNo {
921 return port.AdminState == voltha.AdminState_ENABLED
922 }
923 }
924 return false
925 }
Kent Hagerman2a07b862020-06-19 15:23:07 -0400926 err = waitUntilDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
kesavandbc2d1622020-01-21 00:42:01 -0500927 assert.Nil(t, err)
928 // Wait for the logical device to satisfy the expected condition
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400929 vlFunction = func(ports []*voltha.LogicalPort) bool {
930 for _, lp := range ports {
kesavandbc2d1622020-01-21 00:42:01 -0500931 if (lp.OfpPort.Config&^uint32(ofp.OfpPortConfig_OFPPC_PORT_DOWN) != lp.OfpPort.Config) ||
932 lp.OfpPort.State != uint32(ofp.OfpPortState_OFPPS_LIVE) {
933 return false
934 }
935 }
936 return true
937 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400938 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction)
kesavandbc2d1622020-01-21 00:42:01 -0500939 assert.Nil(t, err)
940
941 // Disable a non-PON port
Kent Hagerman2a07b862020-06-19 15:23:07 -0400942 for _, cp = range oltPorts.Items {
kesavandbc2d1622020-01-21 00:42:01 -0500943 if cp.Type != voltha.Port_PON_OLT {
944 break
945 }
946
947 }
948 assert.NotNil(t, cp)
949 cp.DeviceId = oltDevice.Id
950
951 // Disable the NW Port of oltDevice
952 _, err = nbi.DisablePort(getContext(), cp)
953 assert.NotNil(t, err)
954
955}
khenaidoo93d5a3d2020-01-15 12:37:05 -0500956
Kent Hagerman2b216042020-04-03 18:28:56 -0400957func (nb *NBTest) testDeviceRebootWhenOltIsEnabled(t *testing.T, nbi *NBIHandler) {
Girish Gowdra408cd962020-03-11 14:31:31 -0700958 //Get an OLT device
959 oltDevice, err := nb.getADevice(true, nbi)
960 assert.Nil(t, err)
961 assert.NotNil(t, oltDevice)
962 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
963 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
964
965 // Verify that we have one or more ONUs to start with
Kent Hagerman2b216042020-04-03 18:28:56 -0400966 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -0700967 assert.Nil(t, err)
968 assert.NotNil(t, onuDevices)
969 assert.Greater(t, len(onuDevices.Items), 0)
970
971 // Reboot the OLT and very that Connection Status goes to UNREACHABLE and operation status to UNKNOWN
972 _, err = nbi.RebootDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
973 assert.Nil(t, err)
974
975 var vlFunction0 = func(d *voltha.Device) bool {
976 return d.ConnectStatus == voltha.ConnectStatus_UNREACHABLE && d.OperStatus == voltha.OperStatus_UNKNOWN
977 }
978
979 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction0, nbi)
980 assert.Nil(t, err)
981
982 // Wait for the logical device to satisfy the expected condition
983 var vlFunction1 = func(ld *voltha.LogicalDevice) bool {
984 return ld == nil
985 }
986
987 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction1)
988 assert.Nil(t, err)
989
990 // Wait for the device to satisfy the expected condition (device does not have flows)
991 var vlFunction2 = func(d *voltha.Device) bool {
992 var deviceFlows *ofp.Flows
993 var err error
994 if deviceFlows, err = nbi.ListDeviceFlows(getContext(), &voltha.ID{Id: d.Id}); err != nil {
995 return false
996 }
997 return len(deviceFlows.Items) == 0
998 }
999
1000 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction2, nbi)
1001 assert.Nil(t, err)
1002
1003 // Wait for the device to satisfy the expected condition (there are no child devices)
1004 var vlFunction3 = func(d *voltha.Device) bool {
1005 var devices *voltha.Devices
1006 var err error
1007 if devices, err = nbi.ListDevices(getContext(), nil); err != nil {
1008 return false
1009 }
1010 for _, device := range devices.Items {
1011 if device.ParentId == d.Id {
1012 // We have a child device still left
1013 return false
1014 }
1015 }
1016 return true
1017 }
1018
1019 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vlFunction3, nbi)
1020 assert.Nil(t, err)
1021
1022 // Update the OLT Connection Status to REACHABLE and operation status to ACTIVE
1023 // Normally, in a real adapter this happens after connection regain via a heartbeat mechanism with real hardware
Kent Hagerman45a13e42020-04-13 12:23:50 -04001024 err = nbi.UpdateDeviceStatus(getContext(), oltDevice.Id, voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
Girish Gowdra408cd962020-03-11 14:31:31 -07001025 assert.Nil(t, err)
1026
1027 // Verify the device connection and operation states
1028 oltDevice, err = nb.getADevice(true, nbi)
1029 assert.Nil(t, err)
1030 assert.NotNil(t, oltDevice)
1031 assert.Equal(t, oltDevice.ConnectStatus, voltha.ConnectStatus_REACHABLE)
1032 assert.Equal(t, oltDevice.AdminState, voltha.AdminState_ENABLED)
1033
1034 // Wait for the logical device to satisfy the expected condition
1035 var vlFunction4 = func(ld *voltha.LogicalDevice) bool {
1036 return ld != nil
1037 }
1038 err = waitUntilLogicalDeviceReadiness(oltDevice.Id, nb.maxTimeout, nbi, vlFunction4)
1039 assert.Nil(t, err)
1040
1041 // Verify that logical device is created again
1042 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1043 assert.Nil(t, err)
1044 assert.NotNil(t, logicalDevices)
1045 assert.Equal(t, 1, len(logicalDevices.Items))
1046
1047 // Verify that we have no ONUs left
Kent Hagerman2b216042020-04-03 18:28:56 -04001048 onuDevices, err = nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Girish Gowdra408cd962020-03-11 14:31:31 -07001049 assert.Nil(t, err)
1050 assert.NotNil(t, onuDevices)
1051 assert.Equal(t, 0, len(onuDevices.Items))
1052}
1053
Kent Hagerman2b216042020-04-03 18:28:56 -04001054func (nb *NBTest) testStartOmciTestAction(t *testing.T, nbi *NBIHandler) {
Scott Baker432f9be2020-03-26 11:56:30 -07001055 // -----------------------------------------------------------------------
1056 // SubTest 1: Omci test action should fail due to nonexistent device id
1057
1058 request := &voltha.OmciTestRequest{Id: "123", Uuid: "456"}
1059 _, err := nbi.StartOmciTestAction(getContext(), request)
1060 assert.NotNil(t, err)
1061 assert.Equal(t, "rpc error: code = NotFound desc = 123", err.Error())
1062
1063 // -----------------------------------------------------------------------
1064 // SubTest 2: Error should be returned for device with no adapter registered
1065
1066 // Create a device that has no adapter registered
1067 deviceNoAdapter, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: "noAdapterRegisteredOmciTest", MacAddress: "aa:bb:cc:cc:ee:01"})
1068 assert.Nil(t, err)
1069 assert.NotNil(t, deviceNoAdapter)
1070
1071 // Omci test action should fail due to nonexistent adapter
1072 request = &voltha.OmciTestRequest{Id: deviceNoAdapter.Id, Uuid: "456"}
1073 _, err = nbi.StartOmciTestAction(getContext(), request)
1074 assert.NotNil(t, err)
Kent Hagerman45a13e42020-04-13 12:23:50 -04001075 assert.Equal(t, "adapter-not-registered-for-device-type noAdapterRegisteredOmciTest", err.Error())
Scott Baker432f9be2020-03-26 11:56:30 -07001076
1077 //Remove the device
1078 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: deviceNoAdapter.Id})
1079 assert.Nil(t, err)
1080
1081 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1082 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1083 return devices != nil && len(devices.Items) == 0
1084 }
1085 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1086 assert.Nil(t, err)
1087
1088 // -----------------------------------------------------------------------
1089 // SubTest 3: Omci test action should succeed on valid ONU
1090
1091 // Create the device with valid data
1092 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1093 assert.Nil(t, err)
1094 assert.NotNil(t, oltDevice)
1095
1096 // Verify oltDevice exist in the core
1097 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1098 assert.Nil(t, err)
1099 assert.Equal(t, 1, len(devices.Items))
1100 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1101
1102 // Enable the oltDevice
1103 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1104 assert.Nil(t, err)
1105
1106 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001107 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1108 return len(ports) == nb.numONUPerOLT+1
Scott Baker432f9be2020-03-26 11:56:30 -07001109 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001110 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
Scott Baker432f9be2020-03-26 11:56:30 -07001111 assert.Nil(t, err)
1112
1113 // Wait for the olt device to be enabled
1114 vdFunction := func(device *voltha.Device) bool {
1115 return device.AdminState == voltha.AdminState_ENABLED && device.OperStatus == voltha.OperStatus_ACTIVE
1116 }
1117 err = waitUntilDeviceReadiness(oltDevice.Id, nb.maxTimeout, vdFunction, nbi)
1118 assert.Nil(t, err)
1119
Kent Hagerman2b216042020-04-03 18:28:56 -04001120 onuDevices, err := nb.deviceMgr.GetAllChildDevices(getContext(), oltDevice.Id)
Scott Baker432f9be2020-03-26 11:56:30 -07001121 assert.Nil(t, err)
1122 assert.Greater(t, len(onuDevices.Items), 0)
1123
1124 onuDevice := onuDevices.Items[0]
1125
1126 // Omci test action should succeed
1127 request = &voltha.OmciTestRequest{Id: onuDevice.Id, Uuid: "456"}
1128 resp, err := nbi.StartOmciTestAction(getContext(), request)
1129 assert.Nil(t, err)
1130 assert.Equal(t, resp.Result, voltha.TestResponse_SUCCESS)
1131
1132 //Remove the device
1133 _, err = nbi.DeleteDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1134 assert.Nil(t, err)
1135 //Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1136 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1137 assert.Nil(t, err)
1138}
1139
khenaidoo67b22152020-03-02 16:01:25 -05001140func makeSimpleFlowMod(fa *flows.FlowArgs) *ofp.OfpFlowMod {
1141 matchFields := make([]*ofp.OfpOxmField, 0)
1142 for _, val := range fa.MatchFields {
1143 matchFields = append(matchFields, &ofp.OfpOxmField{Field: &ofp.OfpOxmField_OfbField{OfbField: val}})
1144 }
1145 return flows.MkSimpleFlowMod(matchFields, fa.Actions, fa.Command, fa.KV)
1146}
1147
1148func createMetadata(cTag int, techProfile int, port int) uint64 {
1149 md := 0
1150 md = (md | (cTag & 0xFFFF)) << 16
1151 md = (md | (techProfile & 0xFFFF)) << 32
1152 return uint64(md | (port & 0xFFFFFFFF))
1153}
1154
khenaidoo8b4abbf2020-04-24 17:04:30 -04001155func (nb *NBTest) verifyLogicalDeviceFlowCount(t *testing.T, nbi *NBIHandler, numNNIPorts int, numUNIPorts int, flowAddFail bool) {
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001156 expectedNumFlows := numNNIPorts*numTrapOnNNIFlows + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001157 if flowAddFail {
1158 expectedNumFlows = 0
1159 }
1160 // Wait for logical device to have the flows (or none
khenaidoo67b22152020-03-02 16:01:25 -05001161 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +03001162 id := ""
1163 if lds != nil {
1164 id = lds.Items[0].Id
1165 }
1166 flws, _ := nbi.ListLogicalDeviceFlows(getContext(), &voltha.ID{Id: id})
1167 return lds != nil && len(lds.Items) == 1 && len(flws.Items) == expectedNumFlows
khenaidoo67b22152020-03-02 16:01:25 -05001168 }
1169 // No timeout implies a success
1170 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1171 assert.Nil(t, err)
1172}
1173
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001174func (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 -05001175 // Send flows for the parent device
1176 var nniPorts []*voltha.LogicalPort
1177 var uniPorts []*voltha.LogicalPort
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001178 for _, p := range ports {
khenaidoo67b22152020-03-02 16:01:25 -05001179 if p.RootPort {
1180 nniPorts = append(nniPorts, p)
1181 } else {
1182 uniPorts = append(uniPorts, p)
1183 }
1184 }
1185 assert.Equal(t, 1, len(nniPorts))
1186 //assert.Greater(t, len(uniPorts), 1 )
1187 nniPort := nniPorts[0].OfpPort.PortNo
1188 maxInt32 := uint64(0xFFFFFFFF)
1189 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1190 var fa *flows.FlowArgs
1191 fa = &flows.FlowArgs{
1192 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1193 MatchFields: []*ofp.OfpOxmOfbField{
1194 flows.InPort(nniPort),
1195 flows.EthType(35020),
1196 },
1197 Actions: []*ofp.OfpAction{
1198 flows.Output(controllerPortMask),
1199 },
1200 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001201 flowLLDP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001202 _, err := nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowLLDP)
1203 assert.Nil(t, err)
1204
1205 fa = &flows.FlowArgs{
1206 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1207 MatchFields: []*ofp.OfpOxmOfbField{
1208 flows.InPort(nniPort),
1209 flows.EthType(2048),
1210 flows.IpProto(17),
1211 flows.UdpSrc(67),
1212 flows.UdpDst(68),
1213 },
1214 Actions: []*ofp.OfpAction{
1215 flows.Output(controllerPortMask),
1216 },
1217 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001218 flowIPV4 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001219 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV4)
1220 assert.Nil(t, err)
1221
1222 fa = &flows.FlowArgs{
1223 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1224 MatchFields: []*ofp.OfpOxmOfbField{
1225 flows.InPort(nniPort),
1226 flows.EthType(34525),
1227 flows.IpProto(17),
1228 flows.UdpSrc(546),
1229 flows.UdpDst(547),
1230 },
1231 Actions: []*ofp.OfpAction{
1232 flows.Output(controllerPortMask),
1233 },
1234 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001235 flowIPV6 := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo67b22152020-03-02 16:01:25 -05001236 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowIPV6)
1237 assert.Nil(t, err)
1238
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001239 fa = &flows.FlowArgs{
1240 KV: flows.OfpFlowModArgs{"priority": 10000, "buffer_id": maxInt32, "out_port": maxInt32, "out_group": maxInt32, "flags": 1},
1241 MatchFields: []*ofp.OfpOxmOfbField{
1242 flows.InPort(nniPort),
1243 flows.EthType(34915),
1244 },
1245 Actions: []*ofp.OfpAction{
1246 flows.Output(controllerPortMask),
1247 },
1248 }
1249 flowPPPoEP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
1250 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowPPPoEP)
1251 assert.Nil(t, err)
1252
khenaidoo67b22152020-03-02 16:01:25 -05001253 return len(nniPorts), len(uniPorts)
1254}
1255
Kent Hagerman2b216042020-04-03 18:28:56 -04001256func (nb *NBTest) sendEAPFlows(t *testing.T, nbi *NBIHandler, logicalDeviceID string, port *ofp.OfpPort, vlan int, meterID uint64) {
khenaidoo67b22152020-03-02 16:01:25 -05001257 maxInt32 := uint64(0xFFFFFFFF)
1258 controllerPortMask := uint32(4294967293) // will result in 4294967293&0x7fffffff => 2147483645 which is the actual controller port
1259 fa := &flows.FlowArgs{
1260 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},
1261 MatchFields: []*ofp.OfpOxmOfbField{
1262 flows.InPort(port.PortNo),
1263 flows.EthType(34958),
1264 flows.VlanVid(8187),
1265 },
1266 Actions: []*ofp.OfpAction{
1267 flows.Output(controllerPortMask),
1268 },
1269 }
1270 flowEAP := ofp.FlowTableUpdate{FlowMod: makeSimpleFlowMod(fa), Id: logicalDeviceID}
khenaidoo0db4c812020-05-27 15:27:30 -04001271 maxTries := 3
1272 var err error
1273 for {
1274 if _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &flowEAP); err == nil {
1275 if maxTries < 3 {
1276 t.Log("Re-sending EAPOL flow succeeded for port:", port)
1277 }
1278 break
1279 }
1280 t.Log("Sending EAPOL flows fail:", err)
1281 time.Sleep(50 * time.Millisecond)
1282 maxTries--
1283 if maxTries == 0 {
1284 break
1285 }
1286 }
khenaidoo67b22152020-03-02 16:01:25 -05001287 assert.Nil(t, err)
1288}
1289
khenaidoo0db4c812020-05-27 15:27:30 -04001290func (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 -05001291 defer wg.Done()
khenaidoo67b22152020-03-02 16:01:25 -05001292
1293 // Clear any existing flows on the adapters
1294 nb.oltAdapter.ClearFlows()
1295 nb.onuAdapter.ClearFlows()
1296
khenaidoo8b4abbf2020-04-24 17:04:30 -04001297 // Set the adapter actions on flow addition/deletion
khenaidoo0db4c812020-05-27 15:27:30 -04001298 nb.oltAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
1299 nb.onuAdapter.SetFlowAction(flowAddFail, flowDeleteFail)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001300
khenaidoo67b22152020-03-02 16:01:25 -05001301 // Wait until a logical device is ready
1302 var vlFunction isLogicalDevicesConditionSatisfied = func(lds *voltha.LogicalDevices) bool {
Matteo Scandoload5e14f2021-03-23 10:32:28 -07001303 if lds == nil || len(lds.Items) != 1 || lds.Items[0] == nil {
khenaidoo67b22152020-03-02 16:01:25 -05001304 return false
1305 }
1306 // Ensure there are both NNI ports and at least one UNI port on the logical device
1307 ld := lds.Items[0]
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001308 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: ld.Id})
1309 if err != nil {
1310 return false
1311 }
khenaidoo67b22152020-03-02 16:01:25 -05001312 nniPort := false
1313 uniPort := false
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001314 for _, p := range ports.Items {
khenaidoo67b22152020-03-02 16:01:25 -05001315 nniPort = nniPort || p.RootPort == true
1316 uniPort = uniPort || p.RootPort == false
1317 if nniPort && uniPort {
1318 return true
1319 }
1320 }
1321 return false
1322 }
1323 err := waitUntilConditionForLogicalDevices(nb.maxTimeout, nbi, vlFunction)
1324 assert.Nil(t, err)
1325
1326 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1327 assert.Nil(t, err)
1328 assert.NotNil(t, logicalDevices)
1329 assert.Equal(t, 1, len(logicalDevices.Items))
1330
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001331 logicalDeviceID := logicalDevices.Items[0].Id
khenaidoo67b22152020-03-02 16:01:25 -05001332 meterID := rand.Uint32()
1333
1334 // Add a meter to the logical device
1335 meterMod := &ofp.OfpMeterMod{
1336 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1337 Flags: rand.Uint32(),
1338 MeterId: meterID,
1339 Bands: []*ofp.OfpMeterBandHeader{
1340 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1341 Rate: rand.Uint32(),
1342 BurstSize: rand.Uint32(),
1343 Data: nil,
1344 },
1345 },
1346 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001347 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{Id: logicalDeviceID, MeterMod: meterMod})
1348 assert.Nil(t, err)
1349
1350 ports, err := nbi.ListLogicalDevicePorts(getContext(), &voltha.ID{Id: logicalDeviceID})
khenaidoo67b22152020-03-02 16:01:25 -05001351 assert.Nil(t, err)
1352
1353 // Send initial set of Trap flows
1354 startingVlan := 4091
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001355 nb.sendTrapFlows(t, nbi, logicalDeviceID, ports.Items, uint64(meterID), startingVlan)
khenaidoo67b22152020-03-02 16:01:25 -05001356
1357 // Listen for port events
khenaidoo442e7c72020-03-10 16:13:48 -04001358 start := time.Now()
Girish Gowdra408cd962020-03-11 14:31:31 -07001359 processedNniLogicalPorts := 0
1360 processedUniLogicalPorts := 0
1361
Kent Hagerman45a13e42020-04-13 12:23:50 -04001362 for event := range nbi.GetChangeEventsQueueForTest() {
khenaidoo67b22152020-03-02 16:01:25 -05001363 startingVlan++
1364 if portStatus, ok := (event.Event).(*ofp.ChangeEvent_PortStatus); ok {
1365 ps := portStatus.PortStatus
1366 if ps.Reason == ofp.OfpPortReason_OFPPR_ADD {
khenaidoo67b22152020-03-02 16:01:25 -05001367 if ps.Desc.PortNo >= uint32(nb.startingUNIPortNo) {
Girish Gowdra408cd962020-03-11 14:31:31 -07001368 processedUniLogicalPorts++
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001369 nb.sendEAPFlows(t, nbi, logicalDeviceID, ps.Desc, startingVlan, uint64(meterID))
Girish Gowdra408cd962020-03-11 14:31:31 -07001370 } else {
1371 processedNniLogicalPorts++
khenaidoo67b22152020-03-02 16:01:25 -05001372 }
1373 }
1374 }
Girish Gowdra408cd962020-03-11 14:31:31 -07001375
1376 if processedNniLogicalPorts >= numNNIPorts && processedUniLogicalPorts >= numUNIPorts {
khenaidoo442e7c72020-03-10 16:13:48 -04001377 fmt.Println("Total time to send all flows:", time.Since(start))
khenaidoo67b22152020-03-02 16:01:25 -05001378 break
1379 }
1380 }
1381 //Verify the flow count on the logical device
khenaidoo8b4abbf2020-04-24 17:04:30 -04001382 nb.verifyLogicalDeviceFlowCount(t, nbi, numNNIPorts, numUNIPorts, flowAddFail)
khenaidoo67b22152020-03-02 16:01:25 -05001383
khenaidoo8b4abbf2020-04-24 17:04:30 -04001384 // Wait until all flows have been sent to the OLT adapters (or all failed)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -03001385 expectedFlowCount := (numNNIPorts * numTrapOnNNIFlows) + numNNIPorts*numUNIPorts
khenaidoo8b4abbf2020-04-24 17:04:30 -04001386 if flowAddFail {
1387 expectedFlowCount = 0
1388 }
khenaidoo67b22152020-03-02 16:01:25 -05001389 var oltVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001390 return nb.oltAdapter.GetFlowCount() >= expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001391 }
1392 err = waitUntilCondition(nb.maxTimeout, nbi, oltVFunc)
1393 assert.Nil(t, err)
1394
khenaidoo8b4abbf2020-04-24 17:04:30 -04001395 // Wait until all flows have been sent to the ONU adapters (or all failed)
1396 expectedFlowCount = numUNIPorts
1397 if flowAddFail {
1398 expectedFlowCount = 0
1399 }
khenaidoo67b22152020-03-02 16:01:25 -05001400 var onuVFunc isConditionSatisfied = func() bool {
khenaidoo8b4abbf2020-04-24 17:04:30 -04001401 return nb.onuAdapter.GetFlowCount() == expectedFlowCount
khenaidoo67b22152020-03-02 16:01:25 -05001402 }
1403 err = waitUntilCondition(nb.maxTimeout, nbi, onuVFunc)
1404 assert.Nil(t, err)
1405}
1406
khenaidoo8b4abbf2020-04-24 17:04:30 -04001407func (nb *NBTest) testFlowAddFailure(t *testing.T, nbi *NBIHandler) {
1408
1409 // Create a logical device monitor will automatically send trap and eapol flows to the devices being enables
1410 var wg sync.WaitGroup
1411 wg.Add(1)
khenaidoo0db4c812020-05-27 15:27:30 -04001412 go nb.monitorLogicalDevice(t, nbi, 1, nb.numONUPerOLT, &wg, true, false)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001413
1414 // Create the device with valid data
1415 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ee"})
1416 assert.Nil(t, err)
1417 assert.NotNil(t, oltDevice)
1418
1419 // Verify oltDevice exist in the core
1420 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1421 assert.Nil(t, err)
1422 assert.Equal(t, 1, len(devices.Items))
1423 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1424
1425 // Enable the oltDevice
1426 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1427 assert.Nil(t, err)
1428
1429 // Wait for the logical device to be in the ready state
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001430 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1431 return len(ports) == nb.numONUPerOLT+1
khenaidoo8b4abbf2020-04-24 17:04:30 -04001432 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -04001433 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
khenaidoo8b4abbf2020-04-24 17:04:30 -04001434 assert.Nil(t, err)
1435
1436 // Verify that the devices have been setup correctly
1437 nb.verifyDevices(t, nbi)
1438
1439 // Get latest oltDevice data
1440 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1441 assert.Nil(t, err)
1442
1443 // Verify that the logical device has been setup correctly
1444 nb.verifyLogicalDevices(t, oltDevice, nbi)
1445
1446 // Wait until all flows has been sent to the devices successfully
1447 wg.Wait()
1448}
1449
ssiddiqui21e54c32021-07-27 11:30:46 +05301450func (nb *NBTest) testMPLSFlowsAddition(t *testing.T, nbi *NBIHandler) {
1451 // Check whether Device already exist
1452 devices, err := nbi.ListDevices(getContext(), &empty.Empty{})
1453 assert.NoError(t, err)
1454 testLogger.Infow(getContext(), "device-list", log.Fields{"devices": devices})
1455 for _, dev := range devices.GetItems() {
1456 // Delete the found device for fresh start
1457 testLogger.Warnf(getContext(), "deleting-existing-device", dev.GetId())
1458 _, err := nbi.DeleteDevice(context.Background(), &voltha.ID{
1459 Id: dev.GetId(),
1460 })
1461 assert.NoError(t, err)
1462 }
1463
1464 // Ensure there are no devices in the Core now - wait until condition satisfied or timeout
1465 var vFunction isDevicesConditionSatisfied = func(devices *voltha.Devices) bool {
1466 return devices != nil && len(devices.Items) == 0
1467 }
1468 err = waitUntilConditionForDevices(nb.maxTimeout, nbi, vFunction)
1469 assert.NoError(t, err)
1470
1471 // Get list of devices, to make sure the above operation deleted all the devices
1472 devices, err = nbi.ListDevices(getContext(), &empty.Empty{})
1473 assert.NoError(t, err)
1474 assert.Equal(t, 0, len(devices.Items))
1475
1476 // Create device
1477 oltDevice, err := nbi.CreateDevice(getContext(), &voltha.Device{Type: nb.oltAdapterName, MacAddress: "aa:bb:cc:cc:ee:ff"})
1478 assert.Nil(t, err)
1479 assert.NotNil(t, oltDevice)
1480
1481 // Verify oltDevice exist in the core
1482 devices, err = nbi.ListDevices(getContext(), &empty.Empty{})
1483 assert.Nil(t, err)
1484 assert.Equal(t, 1, len(devices.Items))
1485 assert.Equal(t, oltDevice.Id, devices.Items[0].Id)
1486
1487 // Enable the oltDevice
1488 _, err = nbi.EnableDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1489 assert.Nil(t, err)
1490
1491 // Wait for the logical device to be in the ready state
1492 var vldFunction = func(ports []*voltha.LogicalPort) bool {
1493 return len(ports) == nb.numONUPerOLT+1
1494 }
1495 err = waitUntilLogicalDevicePortsReadiness(oltDevice.Id, nb.maxTimeout, nbi, vldFunction)
1496 assert.Nil(t, err)
1497
1498 // Verify that the devices have been setup correctly
1499 nb.verifyDevices(t, nbi)
1500
1501 // Get latest oltDevice data
1502 oltDevice, err = nbi.GetDevice(getContext(), &voltha.ID{Id: oltDevice.Id})
1503 assert.Nil(t, err)
1504 assert.NotNil(t, oltDevice)
1505 testLogger.Infow(getContext(), "olt-device-created-and-verified", log.Fields{"device-id": oltDevice.GetId()})
1506
1507 // Verify that the logical device has been setup correctly
1508 nb.verifyLogicalDevices(t, oltDevice, nbi)
1509
1510 logicalDevices, err := nbi.ListLogicalDevices(getContext(), &empty.Empty{})
1511 assert.NoError(t, err)
1512
1513 testLogger.Infow(getContext(), "list-logical-devices", log.Fields{"logical-device": logicalDevices.GetItems()[0]})
1514 // Add a meter to the logical device, which the flow can refer to
1515 meterMod := &ofp.OfpMeterMod{
1516 Command: ofp.OfpMeterModCommand_OFPMC_ADD,
1517 Flags: rand.Uint32(),
1518 MeterId: 1,
1519 Bands: []*ofp.OfpMeterBandHeader{
1520 {Type: ofp.OfpMeterBandType_OFPMBT_EXPERIMENTER,
1521 Rate: rand.Uint32(),
1522 BurstSize: rand.Uint32(),
1523 Data: nil,
1524 },
1525 },
1526 }
1527 _, err = nbi.UpdateLogicalDeviceMeterTable(getContext(), &ofp.MeterModUpdate{
1528 Id: logicalDevices.GetItems()[0].GetId(),
1529 MeterMod: meterMod,
1530 })
1531 assert.NoError(t, err)
1532
1533 meters, err := nbi.ListLogicalDeviceMeters(getContext(), &voltha.ID{Id: logicalDevices.GetItems()[0].GetId()})
1534 assert.NoError(t, err)
1535
1536 for _, item := range meters.GetItems() {
1537 testLogger.Infow(getContext(), "list-logical-device-meters", log.Fields{"meter-config": item.GetConfig()})
1538 }
1539
1540 logicalPorts, err := nbi.ListLogicalDevicePorts(context.Background(), &voltha.ID{Id: logicalDevices.GetItems()[0].GetId()})
1541 assert.NoError(t, err)
1542 m := jsonpb.Marshaler{}
1543 logicalPortsJson, err := m.MarshalToString(logicalPorts)
1544 assert.NoError(t, err)
1545
1546 testLogger.Infow(getContext(), "list-logical-ports", log.Fields{"ports": logicalPortsJson})
1547
1548 callables := []func() *ofp.OfpFlowMod{getOnuUpstreamRules, getOltUpstreamRules, getOLTDownstreamMplsSingleTagRules,
1549 getOLTDownstreamMplsDoubleTagRules, getOLTDownstreamRules, getOnuDownstreamRules}
1550
1551 for _, callable := range callables {
1552 _, err = nbi.UpdateLogicalDeviceFlowTable(getContext(), &ofp.FlowTableUpdate{Id: logicalDevices.GetItems()[0].GetId(), FlowMod: callable()})
1553 assert.NoError(t, err)
1554 }
1555}
1556
1557func getOnuUpstreamRules() (flowMod *ofp.OfpFlowMod) {
1558 fa := &flows.FlowArgs{
1559 KV: flows.OfpFlowModArgs{"priority": 1000, "table_id": 1, "meter_id": 1, "write_metadata": 4100100000},
1560 MatchFields: []*ofp.OfpOxmOfbField{
1561 flows.InPort(103),
1562 flows.VlanVid(4096),
1563 },
1564 Actions: []*ofp.OfpAction{},
1565 }
1566
1567 flowMod = makeSimpleFlowMod(fa)
1568 flowMod.TableId = 0
1569 m := jsonpb.Marshaler{}
1570 flowModJson, _ := m.MarshalToString(flowMod)
1571 testLogger.Infow(getContext(), "onu-upstream-flow", log.Fields{"flow-mod": flowModJson})
1572 return
1573}
1574
1575func getOltUpstreamRules() (flowMod *ofp.OfpFlowMod) {
1576 fa := &flows.FlowArgs{
1577 KV: flows.OfpFlowModArgs{"priority": 1000, "table_id": 1, "meter_id": 1, "write_metadata": 4100000000},
1578 MatchFields: []*ofp.OfpOxmOfbField{
1579 flows.InPort(103),
1580 flows.VlanVid(4096),
1581 },
1582 Actions: []*ofp.OfpAction{
1583 flows.PushVlan(0x8100),
1584 flows.SetField(flows.VlanVid(2)),
1585 flows.SetField(flows.EthSrc(1111)),
1586 flows.SetField(flows.EthDst(2222)),
1587 flows.PushVlan(0x8847),
1588 flows.SetField(flows.MplsLabel(100)),
1589 flows.SetField(flows.MplsBos(1)),
1590 flows.PushVlan(0x8847),
1591 flows.SetField(flows.MplsLabel(200)),
1592 flows.MplsTtl(64),
1593 flows.Output(2),
1594 },
1595 }
1596 flowMod = makeSimpleFlowMod(fa)
1597 flowMod.TableId = 1
1598 m := jsonpb.Marshaler{}
1599 flowModJson, _ := m.MarshalToString(flowMod)
1600 testLogger.Infow(getContext(), "olt-upstream-flow", log.Fields{"flow-mod": flowModJson})
1601 return
1602}
1603
1604func getOLTDownstreamMplsSingleTagRules() (flowMod *ofp.OfpFlowMod) {
1605 fa := &flows.FlowArgs{
1606 KV: flows.OfpFlowModArgs{"priority": 1000, "table_id": 1},
1607 MatchFields: []*ofp.OfpOxmOfbField{
1608 flows.InPort(2),
1609 flows.Metadata_ofp((1000 << 32) | 1),
1610 flows.EthType(0x8847),
1611 flows.MplsBos(1),
1612 flows.EthSrc(2222),
1613 },
1614 Actions: []*ofp.OfpAction{
1615 {Type: ofp.OfpActionType_OFPAT_DEC_MPLS_TTL, Action: &ofp.OfpAction_MplsTtl{MplsTtl: &ofp.OfpActionMplsTtl{MplsTtl: 62}}},
1616 flows.PopMpls(0x8847),
1617 },
1618 }
1619 flowMod = makeSimpleFlowMod(fa)
1620 flowMod.TableId = 0
1621 m := jsonpb.Marshaler{}
1622 flowModJson, _ := m.MarshalToString(flowMod)
1623 testLogger.Infow(getContext(), "olt-mpls-downstream-single-tag-flow", log.Fields{"flow-mod": flowModJson})
1624 return
1625}
1626
1627func getOLTDownstreamMplsDoubleTagRules() (flowMod *ofp.OfpFlowMod) {
1628 fa := &flows.FlowArgs{
1629 KV: flows.OfpFlowModArgs{"priority": 1000, "table_id": 1},
1630 MatchFields: []*ofp.OfpOxmOfbField{
1631 flows.InPort(2),
1632 flows.EthType(0x8847),
1633 flows.EthSrc(2222),
1634 },
1635 Actions: []*ofp.OfpAction{
1636 {Type: ofp.OfpActionType_OFPAT_DEC_MPLS_TTL, Action: &ofp.OfpAction_MplsTtl{MplsTtl: &ofp.OfpActionMplsTtl{MplsTtl: 62}}},
1637 flows.PopMpls(0x8847),
1638 flows.PopMpls(0x8847),
1639 },
1640 }
1641 flowMod = makeSimpleFlowMod(fa)
1642 flowMod.TableId = 0
1643 m := jsonpb.Marshaler{}
1644 flowModJson, _ := m.MarshalToString(flowMod)
1645 testLogger.Infow(getContext(), "olt-mpls-downstream-double-tagged-flow", log.Fields{"flow-mod": flowModJson})
1646 return
1647}
1648
1649func getOLTDownstreamRules() (flowMod *ofp.OfpFlowMod) {
1650 fa := &flows.FlowArgs{
1651 KV: flows.OfpFlowModArgs{"priority": 1000, "table_id": 2, "meter_id": 1},
1652 MatchFields: []*ofp.OfpOxmOfbField{
1653 flows.InPort(2),
1654 flows.VlanVid(2),
1655 },
1656 Actions: []*ofp.OfpAction{
1657 flows.PopVlan(),
1658 },
1659 }
1660 flowMod = makeSimpleFlowMod(fa)
1661 flowMod.TableId = 1
1662 m := jsonpb.Marshaler{}
1663 flowModJson, _ := m.MarshalToString(flowMod)
1664 testLogger.Infow(getContext(), "olt-downstream-flow", log.Fields{"flow-mod": flowModJson})
1665 return
1666}
1667
1668func getOnuDownstreamRules() (flowMod *ofp.OfpFlowMod) {
1669 fa := &flows.FlowArgs{
1670 KV: flows.OfpFlowModArgs{"priority": 1000, "meter_id": 1},
1671 MatchFields: []*ofp.OfpOxmOfbField{
1672 flows.InPort(2),
1673 flows.Metadata_ofp((1000 << 32) | 1),
1674 flows.VlanVid(4096),
1675 },
1676 Actions: []*ofp.OfpAction{
1677 flows.Output(103),
1678 },
1679 }
1680 flowMod = makeSimpleFlowMod(fa)
1681 flowMod.TableId = 2
1682 m := jsonpb.Marshaler{}
1683 flowModJson, _ := m.MarshalToString(flowMod)
1684 testLogger.Infow(getContext(), "onu-downstream-flow", log.Fields{"flow-mod": flowModJson})
1685 return
1686}
1687
Matteo Scandolod525ae32020-04-02 17:27:29 -07001688func TestSuiteNbiApiHandler(t *testing.T) {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001689 ctx := context.Background()
Kent Hagerman2b216042020-04-03 18:28:56 -04001690 f, err := os.Create("../../../tests/results/profile.cpu")
khenaidoo67b22152020-03-02 16:01:25 -05001691 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001692 logger.Fatalf(ctx, "could not create CPU profile: %v\n ", err)
khenaidoo67b22152020-03-02 16:01:25 -05001693 }
Andrey Pozolotin34dd63f2021-05-31 21:26:40 +03001694 defer func() {
1695 err = f.Close()
1696 if err != nil {
1697 logger.Errorf(ctx, "failed to close file: %v\n", err)
1698 }
1699 }()
khenaidoo67b22152020-03-02 16:01:25 -05001700 runtime.SetBlockProfileRate(1)
1701 runtime.SetMutexProfileFraction(-1)
1702 if err := pprof.StartCPUProfile(f); err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +00001703 logger.Fatalf(ctx, "could not start CPU profile: %v\n", err)
khenaidoo67b22152020-03-02 16:01:25 -05001704 }
1705 defer pprof.StopCPUProfile()
1706
khenaidoo442e7c72020-03-10 16:13:48 -04001707 //log.SetPackageLogLevel("github.com/opencord/voltha-go/rw_core/core", log.DebugLevel)
1708
Rohan Agrawal31f21802020-06-12 05:38:46 +00001709 nb := newNBTest(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001710 assert.NotNil(t, nb)
1711
Rohan Agrawal31f21802020-06-12 05:38:46 +00001712 defer nb.stopAll(ctx)
khenaidoob64fc8a2019-11-27 15:08:19 -05001713
1714 // Start the Core
1715 nb.startCore(false)
1716
1717 // Set the grpc API interface - no grpc server is running in unit test
Kent Hagerman45a13e42020-04-13 12:23:50 -04001718 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
khenaidoob64fc8a2019-11-27 15:08:19 -05001719
1720 // 1. Basic test with no data in Core
1721 nb.testCoreWithoutData(t, nbi)
1722
1723 // Create/register the adapters
Rohan Agrawal31f21802020-06-12 05:38:46 +00001724 nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(ctx, t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr)
Mahir Gunyel03de0d32020-06-03 01:36:59 -07001725 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1726 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
khenaidoob64fc8a2019-11-27 15:08:19 -05001727
1728 // 2. Test adapter registration
1729 nb.testAdapterRegistration(t, nbi)
1730
khenaidoo8b4abbf2020-04-24 17:04:30 -04001731 numberOfTestRuns := 2
1732 for i := 1; i <= numberOfTestRuns; i++ {
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301733
1734 // 3. Test create device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001735 nb.testCreateDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001736
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301737 // 4. Test Delete Device Scenarios
1738 nb.testForceDeletePreProvDevice(t, nbi)
1739 nb.testDeletePreProvDevice(t, nbi)
1740 nb.testForceDeleteEnabledDevice(t, nbi)
1741 nb.testDeleteEnabledDevice(t, nbi)
1742 nb.testForceDeleteDeviceFailure(t, nbi)
1743 nb.testDeleteDeviceFailure(t, nbi)
1744
1745 // 5. Test Enable a device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001746 nb.testEnableDevice(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001747
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301748 // 6. Test disable and ReEnable a root device
khenaidoo93d5a3d2020-01-15 12:37:05 -05001749 nb.testDisableAndReEnableRootDevice(t, nbi)
khenaidoo67b22152020-03-02 16:01:25 -05001750
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301751 // 7. Test disable and Enable pon port of OLT device
kesavandbc2d1622020-01-21 00:42:01 -05001752 nb.testDisableAndEnablePort(t, nbi)
khenaidoo93d5a3d2020-01-15 12:37:05 -05001753
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301754 // 8.Test Device unreachable when OLT is enabled
Girish Gowdra408cd962020-03-11 14:31:31 -07001755 nb.testDeviceRebootWhenOltIsEnabled(t, nbi)
1756
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301757 // 9. Test disable and delete all devices
khenaidoo93d5a3d2020-01-15 12:37:05 -05001758 nb.testDisableAndDeleteAllDevice(t, nbi)
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001759
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301760 // 10. Test enable and delete all devices
Chaitrashree G S543df3e2020-02-24 22:36:54 -05001761 nb.testEnableAndDeleteAllDevice(t, nbi)
Scott Baker432f9be2020-03-26 11:56:30 -07001762
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301763 // 11. Test omci test
Scott Baker432f9be2020-03-26 11:56:30 -07001764 nb.testStartOmciTestAction(t, nbi)
khenaidoob64fc8a2019-11-27 15:08:19 -05001765
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301766 // 12. Remove all devices from tests above
khenaidoo0db4c812020-05-27 15:27:30 -04001767 nb.deleteAllDevices(t, nbi)
1768
Himani Chawla2ba1c9c2020-10-07 13:19:03 +05301769 // 13. Test flow add failure
khenaidoo8b4abbf2020-04-24 17:04:30 -04001770 nb.testFlowAddFailure(t, nbi)
1771
ssiddiqui21e54c32021-07-27 11:30:46 +05301772 // 14. Clean up
khenaidoo8b4abbf2020-04-24 17:04:30 -04001773 nb.deleteAllDevices(t, nbi)
1774 }
khenaidoob64fc8a2019-11-27 15:08:19 -05001775}
ssiddiqui21e54c32021-07-27 11:30:46 +05301776
1777func TestFlowAddition(t *testing.T) {
1778 ctx := context.Background()
1779 nb := newNBTest(ctx)
1780 assert.NotNil(t, nb)
1781
1782 defer nb.stopAll(ctx)
1783
1784 // Start the Core
1785 nb.startCore(false)
1786
1787 // Set the grpc API interface - no grpc server is running in unit test
1788 nbi := NewNBIHandler(nb.deviceMgr, nb.logicalDeviceMgr, nb.adapterMgr)
1789
1790 // Create/register the adapters
1791 nb.oltAdapter, nb.onuAdapter = tst.CreateAndregisterAdapters(ctx, t, nb.kClient, nb.coreInstanceID, nb.oltAdapterName, nb.onuAdapterName, nb.adapterMgr)
1792 nb.numONUPerOLT = nb.oltAdapter.GetNumONUPerOLT()
1793 nb.startingUNIPortNo = nb.oltAdapter.GetStartingUNIPortNo()
1794
1795 // 2. Test adapter registration
1796 nb.testAdapterRegistration(t, nbi)
1797
1798 // 3. Test MPLS flows addition where:
1799 /*
1800 Upstream
1801 ONU
1802 ADDED, bytes=0, packets=0, table=0, priority=1000, selector=[IN_PORT:32, VLAN_VID:ANY], treatment=[immediate=[],
1803 transition=TABLE:1, meter=METER:1, metadata=METADATA:4100010000/0]
1804 OLT
1805 ADDED, bytes=0, packets=0, table=1, priority=1000, selector=[IN_PORT:32, VLAN_VID:ANY], treatment=[immediate=[VLAN_PUSH:vlan,
1806 VLAN_ID:2, MPLS_PUSH:mpls_unicast, MPLS_LABEL:YYY,MPLS_BOS:true, MPLS_PUSH:mpls_unicast ,MPLS_LABEL:XXX, MPLS_BOS:false,
1807 EXTENSION:of:0000000000000227/VolthaPushL2Header{​​​​​​​}​​​​​​​, ETH_SRC:OLT_MAC, ETH_DST:LEAF_MAC, TTL:64, OUTPUT:65536],
1808 meter=METER:1, metadata=METADATA:4100000000/0]
1809
1810 Downstream
1811 OLT
1812 //Below flow rule to pop L2 Ethernet headers from packets which have a single MPLS label
1813 ADDED, bytes=0, packets=0, table=0, priority=1000, selector=[IN_PORT:65536, ETH_TYPE:mpls_unicast, MPLS_BOS:true, ETH_SRC:LEAF_MAC],
1814 treatment=[DefaultTrafficTreatment{immediate=[DEC_MPLS_TTL, TTL_IN, MPLS_POP:mpls_unicast, EXTENSION:of:0000000000000227/VolthaPopL2Header{},
1815 transition=TABLE:1]
1816
1817 //Below flow rule to pop L2 Ethernet headers from packets which have two MPLS label
1818 ADDED, bytes=0, packets=0, table=0, priority=1000, selector=[IN_PORT:65536, ETH_TYPE:mpls_unicast, MPLS_BOS:false, ETH_SRC:LEAF_MAC],
1819 treatment=[DefaultTrafficTreatment{immediate=[DEC_MPLS_TTL, TTL_IN, MPLS_POP:mpls_unicast, MPLS_POP:mpls_unicast ,
1820 EXTENSION:of:0000000000000227/VolthaPopL2Header{}, transition=TABLE:1]
1821
1822 //Below flow rules are unchanged from the current implementations except for the table numbers
1823 ADDED, bytes=0, packets=0, table=1, priority=1000, selector=[IN_PORT:65536, VLAN_VID:2], treatment=[immediate=[VLAN_POP], transition=TABLE:2,
1824 meter=METER:2, metadata=METADATA:1000004100000020/0]
1825 ONU
1826 ADDED, bytes=0, packets=0, table=2, priority=1000, selector=[IN_PORT:65536, METADATA:20 VLAN_VID:ANY], treatment=[immediate=[OUTPUT:32],
1827 meter=METER:2, metadata=METADATA:4100000000/0]
1828 */
1829 nb.testMPLSFlowsAddition(t, nbi)
1830}