blob: ea40a5c1a0d5f57adf84648b73733b9fd03d77e6 [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
kdarapu381c6902019-07-31 18:23:16 +053022 "net"
kdarapu891693b2019-09-16 12:33:49 +053023 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053024 "sync"
kdarapu381c6902019-07-31 18:23:16 +053025 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053026 "time"
27
khenaidoo106c61a2021-08-11 18:05:46 -040028 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
29 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
30
31 "github.com/opencord/voltha-lib-go/v7/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
Mahir Gunyel85f61c12021-10-06 11:53:45 -070033 plt "github.com/opencord/voltha-lib-go/v7/pkg/platform"
khenaidoo106c61a2021-08-11 18:05:46 -040034 "github.com/opencord/voltha-lib-go/v7/pkg/pmmetrics"
35 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
kesavand494c2082020-08-31 11:16:12 +053036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
39 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
khenaidoodc2116e2021-10-19 17:33:19 -040040 ia "github.com/opencord/voltha-protos/v5/go/inter_adapter"
khenaidoo106c61a2021-08-11 18:05:46 -040041 of "github.com/opencord/voltha-protos/v5/go/openflow_13"
42 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
43 oop "github.com/opencord/voltha-protos/v5/go/openolt"
44 "github.com/opencord/voltha-protos/v5/go/voltha"
kesavandb9f54fd2021-11-25 20:08:04 +053045 "github.com/stretchr/testify/assert"
kdarapu381c6902019-07-31 18:23:16 +053046)
47
Girish Gowdra9602eb42020-09-09 15:50:39 -070048const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070049 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070050 OnuIDStart = 1
51 OnuIDEnd = 32
52 AllocIDStart = 1
53 AllocIDEnd = 10
54 GemIDStart = 1
55 GemIDEnd = 10
56 FlowIDStart = 1
57 FlowIDEnd = 10
58)
59
khenaidoo106c61a2021-08-11 18:05:46 -040060func newMockOnuInterAdapterService() *mocks.MockOnuInterAdapterService {
61 return &mocks.MockOnuInterAdapterService{}
62}
63
64func newMockCoreService() *mocks.MockCoreService {
65 mcp := mocks.MockCoreService{
Kent Hagermanf1db18b2020-07-08 13:38:15 -040066 Devices: make(map[string]*voltha.Device),
67 DevicePorts: make(map[string][]*voltha.Port),
68 }
Naga Manjunath7615e552019-10-11 22:35:47 +053069 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053070 mcp.Devices["olt"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040071 Id: "olt",
72 Root: true,
73 ParentId: "logical_device",
74 ParentPortNo: 1,
75 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +053076 ProxyAddress: &voltha.Device_ProxyAddress{
77 DeviceId: "olt",
78 DeviceType: "onu",
79 ChannelId: 1,
80 ChannelGroupId: 1,
81 },
82 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053083 PmConfigs: &voltha.PmConfigs{
84 DefaultFreq: 10,
85 Id: "olt",
86 FreqOverride: false,
87 Grouped: false,
88 Metrics: pm,
89 },
kdarapu891693b2019-09-16 12:33:49 +053090 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040091 mcp.DevicePorts["olt"] = []*voltha.Port{
92 {PortNo: 1, Label: "pon"},
93 {PortNo: 2, Label: "nni"},
94 }
kdarapu891693b2019-09-16 12:33:49 +053095
Kent Hagermanf1db18b2020-07-08 13:38:15 -040096 mcp.Devices["onu1"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040097 Id: "1",
98 Root: false,
99 ParentId: "olt",
100 ParentPortNo: 1,
101 AdapterEndpoint: "mock-onu-endpoint",
102 OperStatus: 4,
kdarapu891693b2019-09-16 12:33:49 +0530103 ProxyAddress: &voltha.Device_ProxyAddress{
104 OnuId: 1,
105 ChannelId: 1,
106 ChannelGroupId: 1,
107 },
108 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530109 PmConfigs: &voltha.PmConfigs{
110 DefaultFreq: 10,
111 Id: "olt",
112 FreqOverride: false,
113 Grouped: false,
114 Metrics: pm,
115 },
kdarapu891693b2019-09-16 12:33:49 +0530116 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400117 mcp.DevicePorts["onu1"] = []*voltha.Port{
118 {PortNo: 1, Label: "pon"},
119 {PortNo: 2, Label: "uni"},
120 }
121
kdarapu891693b2019-09-16 12:33:49 +0530122 mcp.Devices["onu2"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400123 Id: "2",
124 Root: false,
125 ParentId: "olt",
126 OperStatus: 2,
127 AdapterEndpoint: "mock-onu-endpoint",
128 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +0530129
130 ProxyAddress: &voltha.Device_ProxyAddress{
131 OnuId: 2,
132 ChannelId: 1,
133 ChannelGroupId: 1,
134 },
135 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530136 PmConfigs: &voltha.PmConfigs{
137 DefaultFreq: 10,
138 Id: "olt",
139 FreqOverride: false,
140 Grouped: false,
141 Metrics: pm,
142 },
kdarapu891693b2019-09-16 12:33:49 +0530143 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400144 mcp.DevicePorts["onu2"] = []*voltha.Port{
145 {PortNo: 1, Label: "pon"},
146 {PortNo: 2, Label: "uni"},
147 }
kdarapu891693b2019-09-16 12:33:49 +0530148 return &mcp
149}
150func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530151 device := &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400152 Id: "olt",
153 Root: true,
154 ParentId: "logical_device",
155 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +0530156 ProxyAddress: &voltha.Device_ProxyAddress{
157 DeviceId: "olt",
158 DeviceType: "onu",
159 ChannelId: 1,
160 ChannelGroupId: 1,
161 },
162 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530163 }
khenaidoo106c61a2021-08-11 18:05:46 -0400164 mcs := newMockCoreService()
165 cc := mocks.NewMockCoreClient(mcs)
kdarapu891693b2019-09-16 12:33:49 +0530166 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800167 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700168 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
kesavand494c2082020-08-31 11:16:12 +0530169 cfg := &config.AdapterFlags{OmccEncryption: true}
khenaidoo106c61a2021-08-11 18:05:46 -0400170 openOLT := &OpenOLT{eventProxy: ep, config: cfg}
171 dh := NewDeviceHandler(cc, ep, device, openOLT, cm, cfg)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700172 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700173 IntfIds: []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
Girish Gowdra38d533d2020-03-30 20:38:51 -0700174 Technology: "xgs-pon",
175 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
176 }}
177
Girish Gowdra9602eb42020-09-09 15:50:39 -0700178 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700179 dh.deviceInfo = deviceInf
khenaidoo106c61a2021-08-11 18:05:46 -0400180 dh.device = device
yasin saplid0566272021-12-21 09:10:30 +0000181 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts+1)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700182 var i uint32
yasin saplid0566272021-12-21 09:10:30 +0000183 for i = 0; i < deviceInf.PonPorts+1; i++ {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700184 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
185 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700186 StoreType: "etcd",
187 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700188 }}
189 dh.resourceMgr[i].InitLocalCache()
190 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700191
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530192 ranges := make(map[string]interface{})
193 sharedIdxByType := make(map[string]string)
194 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
195 sharedIdxByType["ONU_ID"] = "ONU_ID"
196 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
197 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
198 ranges["ONU_ID"] = uint32(0)
199 ranges["GEMPORT_ID"] = uint32(0)
200 ranges["ALLOC_ID"] = uint32(0)
201 ranges["FLOW_ID"] = uint32(0)
202 ranges["onu_id_shared"] = uint32(0)
203 ranges["alloc_id_shared"] = uint32(0)
204 ranges["gemport_id_shared"] = uint32(0)
205 ranges["flow_id_shared"] = uint32(0)
206
Matteo Scandolo84585372021-03-18 14:21:22 -0700207 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700208 ponmgr.DeviceID = "onu-1"
209 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
210 ponmgr.KVStore = &db.Backend{
211 Client: &mocks.MockKVClient{},
212 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700213 ponmgr.KVStoreForConfig = &db.Backend{
214 Client: &mocks.MockKVClient{},
215 }
216 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700217 ponmgr.PonResourceRanges = ranges
218 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700219 ponmgr.Technology = "XGS-PON"
220 for i = 0; i < deviceInf.PonPorts; i++ {
221 dh.resourceMgr[i].PonRsrMgr = ponmgr
222 }
223
npujarec5762e2020-01-01 14:08:48 +0530224 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
225 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700226 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700227 dh.totalPonPorts = NumPonPorts
yasin saplid0566272021-12-21 09:10:30 +0000228 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts+1)
229 for i = 0; i < dh.totalPonPorts+1; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700230 dh.flowMgr[i] = &OpenOltFlowMgr{}
231 dh.flowMgr[i].deviceHandler = dh
232 dh.flowMgr[i].ponPortIdx = i
233 dh.flowMgr[i].grpMgr = dh.groupMgr
234 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700235 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700236 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700237
Girish Gowdra76a1b092021-07-28 10:07:04 -0700238 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
239
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700240 // Create a slice of buffered channels for handling concurrent flows per ONU.
241 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
Mahir Gunyel85f61c12021-10-06 11:53:45 -0700242 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, plt.MaxOnusPerPon+1)
243 dh.flowMgr[i].stopFlowHandlerRoutine = make([]chan bool, plt.MaxOnusPerPon+1)
244 dh.flowMgr[i].flowHandlerRoutineActive = make([]bool, plt.MaxOnusPerPon+1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700245 for j := range dh.flowMgr[i].incomingFlows {
246 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700247 dh.flowMgr[i].stopFlowHandlerRoutine[j] = make(chan bool, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700248 // Spin up a go routine to handling incoming flows (add/remove).
249 // There will be on go routine per ONU.
250 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700251 dh.flowMgr[i].flowHandlerRoutineActive[j] = true
252 go dh.flowMgr[i].perOnuFlowHandlerRoutine(j, dh.flowMgr[i].incomingFlows[j], dh.flowMgr[i].stopFlowHandlerRoutine[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700253 }
254 }
kdarapu891693b2019-09-16 12:33:49 +0530255 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500256 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530257 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530258 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000259
260 var pmNames = []string{
261 "rx_bytes",
262 "rx_packets",
263 "rx_mcast_packets",
264 "rx_bcast_packets",
265 "tx_bytes",
266 "tx_packets",
267 "tx_mcast_packets",
268 "tx_bcast_packets",
269 }
270
271 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
khenaidoo106c61a2021-08-11 18:05:46 -0400272
273 // Set the children endpoints
274 dh.childAdapterClients = map[string]*vgrpc.Client{
275 "mock-onu-endpoint": mocks.NewMockChildAdapterClient(newMockOnuInterAdapterService()),
276 }
kdarapu891693b2019-09-16 12:33:49 +0530277 return dh
kdarapu381c6902019-07-31 18:23:16 +0530278}
279
kdarapu891693b2019-09-16 12:33:49 +0530280func negativeDeviceHandler() *DeviceHandler {
281 dh := newMockDeviceHandler()
282 device := dh.device
283 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530284 return dh
285}
Girish Gowdra0fb24a32021-10-27 15:15:27 -0700286
287func negativeDeviceHandlerNilFlowMgr() *DeviceHandler {
288 dh := newMockDeviceHandler()
289 dh.flowMgr = nil
290 return dh
291}
292
kdarapu381c6902019-07-31 18:23:16 +0530293func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000294 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530295 type args struct {
296 host string
297 }
298 tests := []struct {
299 name string
300 args args
301 want string
302 wantErr bool
303 }{
kdarapu891693b2019-09-16 12:33:49 +0530304 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
305 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
306 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
307 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530308 }
309 for _, tt := range tests {
310 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000311 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530312 if (err != nil) != tt.wantErr {
313 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
314 return
315 }
316 if got != tt.want {
317 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
318 }
319 })
320 }
321}
322func Test_macifyIP(t *testing.T) {
323 type args struct {
324 ip net.IP
325 }
326 tests := []struct {
327 name string
328 args args
329 want string
330 }{{
kdarapu891693b2019-09-16 12:33:49 +0530331 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530332 args{ip: net.ParseIP("10.10.10.10")},
333 "00:00:0a:0a:0a:0a",
334 },
335 {
kdarapu891693b2019-09-16 12:33:49 +0530336 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530337 args{ip: net.ParseIP("127.0.0.1")},
338 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530339 },
340 {
341 "macifyIP-3",
342 args{ip: net.ParseIP("127.0.0.1/24")},
343 "",
344 },
345 }
kdarapu381c6902019-07-31 18:23:16 +0530346 for _, tt := range tests {
347 t.Run(tt.name, func(t *testing.T) {
348 if got := macifyIP(tt.args.ip); got != tt.want {
349 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
350 }
351 })
352 }
353}
354
David K. Bainbridge794735f2020-02-11 21:01:37 -0800355func sparseCompare(keys []string, spec, target interface{}) bool {
356 if spec == target {
357 return true
358 }
359 if spec == nil || target == nil {
360 return false
361 }
362 typeSpec := reflect.TypeOf(spec)
363 typeTarget := reflect.TypeOf(target)
364 if typeSpec != typeTarget {
365 return false
366 }
367
368 vSpec := reflect.ValueOf(spec)
369 vTarget := reflect.ValueOf(target)
370 if vSpec.Kind() == reflect.Ptr {
371 vSpec = vSpec.Elem()
372 vTarget = vTarget.Elem()
373 }
374
375 for _, key := range keys {
376 fSpec := vSpec.FieldByName(key)
377 fTarget := vTarget.FieldByName(key)
378 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
379 return false
380 }
381 }
382 return true
383}
384
kdarapu381c6902019-07-31 18:23:16 +0530385func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000386 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530387 dh1 := newMockDeviceHandler()
388 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530389 type args struct {
390 parentPort uint32
391 onuID uint32
392 }
393 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530394 name string
395 devicehandler *DeviceHandler
396 args args
397 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800398 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530399 }{
kdarapu891693b2019-09-16 12:33:49 +0530400 {"GetChildDevice-1", dh1,
401 args{parentPort: 1,
402 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800403 &voltha.Device{
404 Id: "1",
405 ParentId: "olt",
406 ParentPortNo: 1,
407 },
408 nil,
kdarapu891693b2019-09-16 12:33:49 +0530409 },
410 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530411 args{parentPort: 1,
412 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800413 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530414 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530415 },
416 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800417
418 /*
419 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
420 device_handler_test.go:309: GetportLabel() => want=(, <nil>) got=(id:"1" parent_id:"olt" parent_port_no:1 proxy_address:<channel_id:1 channel_group_id:1 onu_id:1 > oper_status:ACTIVE connect_status:UNREACHABLE ports:<port_no:1 label:"pon" > ports:<port_no:2 label:"uni" > pm_configs:<id:"olt" default_freq:10 > , <nil>)
421 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
422 */
kdarapu381c6902019-07-31 18:23:16 +0530423 for _, tt := range tests {
424 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000425 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800426 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
427 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
428 tt.want, tt.errType, got, reflect.TypeOf(err))
429 return
430 }
kdarapu381c6902019-07-31 18:23:16 +0530431 t.Log("onu device id", got)
432 })
433 }
434}
kdarapu891693b2019-09-16 12:33:49 +0530435
436func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530437 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530438 type args struct {
439 portNum uint32
440 portType voltha.Port_PortType
441 }
442 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800443 name string
444 args args
445 want string
446 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530447 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800448 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
449 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
450 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
451 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
452 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
453 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
454 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530455 }
456 for _, tt := range tests {
457 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800458 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
459 if reflect.TypeOf(err) != tt.errType || got != tt.want {
460 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
461 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530462 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800463
kdarapu891693b2019-09-16 12:33:49 +0530464 })
465 }
466}
467
khenaidoo106c61a2021-08-11 18:05:46 -0400468// func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
469// ctx := context.Background()
470// dh := newMockDeviceHandler()
471// proxyAddr := dh.device.ProxyAddress
khenaidoodc2116e2021-10-19 17:33:19 -0400472// body := &ca.InterAdapterOmciMessage{
khenaidoo106c61a2021-08-11 18:05:46 -0400473// Message: []byte("asdfasdfasdfasdfas"),
474// ProxyAddress: proxyAddr,
475// }
khenaidoodc2116e2021-10-19 17:33:19 -0400476// body2 := &ca.InterAdapterOmciMessage{
khenaidoo106c61a2021-08-11 18:05:46 -0400477// Message: []byte("asdfasdfasdfasdfas"),
478// //ProxyAddress: &voltha.Device_ProxyAddress{},
479// }
khenaidoodc2116e2021-10-19 17:33:19 -0400480// body3 := &ca.InterAdapterTechProfileDownloadMessage{}
khenaidoo106c61a2021-08-11 18:05:46 -0400481// var marshalledData *any.Any
482// var err error
kdarapu891693b2019-09-16 12:33:49 +0530483
khenaidoo106c61a2021-08-11 18:05:46 -0400484// if marshalledData, err = ptypes.MarshalAny(body); err != nil {
485// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
486// }
kdarapu891693b2019-09-16 12:33:49 +0530487
khenaidoo106c61a2021-08-11 18:05:46 -0400488// var marshalledData1 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530489
khenaidoo106c61a2021-08-11 18:05:46 -0400490// if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
491// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
492// }
493// var marshalledData2 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530494
khenaidoo106c61a2021-08-11 18:05:46 -0400495// if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
496// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
497// }
498// type args struct {
khenaidoodc2116e2021-10-19 17:33:19 -0400499// msg *ca.InterAdapterMessage
khenaidoo106c61a2021-08-11 18:05:46 -0400500// }
501// invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
502// tests := []struct {
503// name string
504// args args
505// wantErr reflect.Type
506// }{
khenaidoodc2116e2021-10-19 17:33:19 -0400507// {"ProcessInterAdapterMessage-1", args{msg: &ca.InterAdapterMessage{
508// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400509// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400510// Type: ca.InterAdapterMessageType_FLOW_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400511// },
512// Body: marshalledData,
513// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400514// {"ProcessInterAdapterMessage-2", args{msg: &ca.InterAdapterMessage{
515// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400516// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400517// Type: ca.InterAdapterMessageType_FLOW_RESPONSE,
khenaidoo106c61a2021-08-11 18:05:46 -0400518// },
519// Body: marshalledData1,
520// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400521// {"ProcessInterAdapterMessage-3", args{msg: &ca.InterAdapterMessage{
522// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400523// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400524// Type: ca.InterAdapterMessageType_OMCI_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400525// },
526// Body: marshalledData,
527// }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
khenaidoodc2116e2021-10-19 17:33:19 -0400528// {"ProcessInterAdapterMessage-4", args{msg: &ca.InterAdapterMessage{
529// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400530// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400531// Type: ca.InterAdapterMessageType_OMCI_RESPONSE,
khenaidoo106c61a2021-08-11 18:05:46 -0400532// }, Body: marshalledData,
533// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400534// {"ProcessInterAdapterMessage-5", args{msg: &ca.InterAdapterMessage{
535// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400536// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400537// Type: ca.InterAdapterMessageType_METRICS_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400538// }, Body: marshalledData1,
539// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400540// {"ProcessInterAdapterMessage-6", args{msg: &ca.InterAdapterMessage{
541// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400542// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400543// Type: ca.InterAdapterMessageType_METRICS_RESPONSE,
khenaidoo106c61a2021-08-11 18:05:46 -0400544// }, Body: marshalledData,
545// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400546// {"ProcessInterAdapterMessage-7", args{msg: &ca.InterAdapterMessage{
547// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400548// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400549// Type: ca.InterAdapterMessageType_ONU_IND_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400550// }, Body: marshalledData,
551// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400552// {"ProcessInterAdapterMessage-8", args{msg: &ca.InterAdapterMessage{
553// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400554// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400555// Type: ca.InterAdapterMessageType_ONU_IND_RESPONSE,
khenaidoo106c61a2021-08-11 18:05:46 -0400556// }, Body: marshalledData,
557// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400558// {"ProcessInterAdapterMessage-9", args{msg: &ca.InterAdapterMessage{
559// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400560// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400561// Type: ca.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400562// }, Body: marshalledData,
563// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400564// {"ProcessInterAdapterMessage-10", args{msg: &ca.InterAdapterMessage{
565// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400566// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400567// Type: ca.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400568// }, Body: marshalledData2,
569// }}, invalid},
khenaidoodc2116e2021-10-19 17:33:19 -0400570// {"ProcessInterAdapterMessage-11", args{msg: &ca.InterAdapterMessage{
571// Header: &ca.InterAdapterHeader{
khenaidoo106c61a2021-08-11 18:05:46 -0400572// Id: "012345",
khenaidoodc2116e2021-10-19 17:33:19 -0400573// Type: ca.InterAdapterMessageType_DELETE_TCONT_REQUEST,
khenaidoo106c61a2021-08-11 18:05:46 -0400574// }, Body: marshalledData2,
575// }}, invalid},
576// }
577// for _, tt := range tests {
578// t.Run(tt.name, func(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530579
khenaidoo106c61a2021-08-11 18:05:46 -0400580// if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
581// t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
582// }
583// })
584// }
585// }
kdarapu891693b2019-09-16 12:33:49 +0530586
khenaidoo106c61a2021-08-11 18:05:46 -0400587func TestDeviceHandler_ProxyOmciMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000588 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530589 dh1 := newMockDeviceHandler()
590 dh2 := negativeDeviceHandler()
591 device1 := &voltha.Device{
592 Id: "onu1",
593 Root: false,
594 ParentId: "logical_device",
595 ProxyAddress: &voltha.Device_ProxyAddress{
596 DeviceId: "onu1",
597 DeviceType: "onu",
598 ChannelId: 1,
599 ChannelGroupId: 1,
600 },
601 ConnectStatus: 1,
602 }
603 device2 := device1
604 device2.ConnectStatus = 2
khenaidoodc2116e2021-10-19 17:33:19 -0400605 iaomciMsg1 := &ia.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530606 ProxyAddress: &voltha.Device_ProxyAddress{
607 DeviceId: "onu2",
608 DeviceType: "onu",
609 ChannelId: 1,
610 ChannelGroupId: 1,
kdarapu891693b2019-09-16 12:33:49 +0530611 },
612 ConnectStatus: 1,
613 }
khenaidoodc2116e2021-10-19 17:33:19 -0400614 iaomciMsg2 := &ia.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530615 ProxyAddress: &voltha.Device_ProxyAddress{
616 DeviceId: "onu3",
617 DeviceType: "onu",
618 ChannelId: 1,
619 ChannelGroupId: 1,
620 },
621 ConnectStatus: 1,
622 }
623 type args struct {
624 onuDevice *voltha.Device
khenaidoodc2116e2021-10-19 17:33:19 -0400625 omciMsg *ia.OmciMessage
kdarapu891693b2019-09-16 12:33:49 +0530626 }
627 tests := []struct {
628 name string
629 devicehandler *DeviceHandler
630 args args
631 }{
khenaidoodc2116e2021-10-19 17:33:19 -0400632 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ia.OmciMessage{}}},
633 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ia.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530634 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
635 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
636 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
khenaidoodc2116e2021-10-19 17:33:19 -0400637 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ia.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530638 }
639 for _, tt := range tests {
640 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400641 _ = tt.devicehandler.ProxyOmciMessage(ctx, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400642 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530643 })
644 }
645}
646
kesavandb9f54fd2021-11-25 20:08:04 +0530647func TestDeviceHandler_ProxyOmciRequests(t *testing.T) {
648 ctx := context.Background()
649 dh1 := newMockDeviceHandler()
650 dh2 := negativeDeviceHandler()
651 device1 := &voltha.Device{
652 Id: "onu1",
653 Root: false,
654 ParentId: "logical_device",
655 ProxyAddress: &voltha.Device_ProxyAddress{
656 DeviceId: "onu1",
657 DeviceType: "onu",
658 ChannelId: 1,
659 ChannelGroupId: 1,
660 },
661 ConnectStatus: 1,
662 }
663 device2 := device1
664 device2.ConnectStatus = 2
665
666 iaomciMsg1 := &ia.OmciMessages{
667 ConnectStatus: 1,
668 ProxyAddress: &voltha.Device_ProxyAddress{
669 DeviceId: "onu2",
670 DeviceType: "onu",
671 ChannelId: 1,
672 ChannelGroupId: 1,
673 },
674 }
675
676 iaomciMsg2 := &ia.OmciMessages{
677 ConnectStatus: 1,
678 ProxyAddress: &voltha.Device_ProxyAddress{
679 DeviceId: "onu3",
680 DeviceType: "onu",
681 ChannelId: 1,
682 ChannelGroupId: 1,
683 },
684 }
685
686 type args struct {
687 onuDevice *voltha.Device
688 omciMsg *ia.OmciMessages
689 }
690 tests := []struct {
691 name string
692 devicehandler *DeviceHandler
693 args args
694 }{
695 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ia.OmciMessages{}}},
696 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ia.OmciMessages{}}},
697 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
698 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
699 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
700 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ia.OmciMessages{}}},
701 }
702 for _, tt := range tests {
703 t.Run(tt.name, func(t *testing.T) {
704 switch tt.name {
705 case "sendProxiedMessage-1":
706 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
707 assert.Contains(t, err.Error(), "no deviceID")
708 case "sendProxiedMessage-2":
709 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
710 assert.Contains(t, err.Error(), "no deviceID")
711 case "sendProxiedMessage-3":
712 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
713 assert.Contains(t, err.Error(), "failed-communication")
714 case "sendProxiedMessage-4":
715 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
716 assert.Contains(t, err.Error(), "failed-communication")
717 case "sendProxiedMessage-5":
718 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
719 assert.Contains(t, err.Error(), "failed-communication")
720 case "sendProxiedMessage-6":
721 err := tt.devicehandler.ProxyOmciRequests(ctx, tt.args.omciMsg)
722 assert.Contains(t, err.Error(), "no deviceID")
723
724 }
725
726 })
727 }
728}
729
kdarapu891693b2019-09-16 12:33:49 +0530730func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
731 dh1 := newMockDeviceHandler()
732 dh2 := negativeDeviceHandler()
733
734 type args struct {
735 logicalPort uint32
736 packetPayload []byte
737 }
738 tests := []struct {
739 name string
740 devicehandler *DeviceHandler
741 args args
742 }{
743 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
744 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
745 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
746 }
747 for _, tt := range tests {
748 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400749 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
750 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530751 })
752 }
753}
754
755func TestDeviceHandler_DisableDevice(t *testing.T) {
756 dh1 := newMockDeviceHandler()
757 dh2 := negativeDeviceHandler()
758 type args struct {
759 device *voltha.Device
760 }
761 tests := []struct {
762 name string
763 devicehandler *DeviceHandler
764 args args
765 wantErr bool
766 }{
767 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400768 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530769 }
770 for _, tt := range tests {
771 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000772 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530773 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
774 }
775 })
776 }
777}
778
779func TestDeviceHandler_ReenableDevice(t *testing.T) {
780 dh1 := newMockDeviceHandler()
781 dh2 := negativeDeviceHandler()
782 type args struct {
783 device *voltha.Device
784 }
785 tests := []struct {
786 name string
787 devicehandler *DeviceHandler
788 args args
789 wantErr bool
790 }{
791 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
792 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
793 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
794 }
795 for _, tt := range tests {
796 t.Run(tt.name, func(t *testing.T) {
797 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000798 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530799 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
800 }
801 })
802 }
803}
804
805func TestDeviceHandler_RebootDevice(t *testing.T) {
806 dh1 := newMockDeviceHandler()
807 dh2 := newMockDeviceHandler()
808 type args struct {
809 device *voltha.Device
810 }
811 tests := []struct {
812 name string
813 devicehandler *DeviceHandler
814 args args
815 wantErr bool
816 }{
817 // TODO: Add test cases.
818 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
819 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
820 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
821 }
822 for _, tt := range tests {
823 t.Run(tt.name, func(t *testing.T) {
824
Neha Sharma96b7bf22020-06-15 10:37:32 +0000825 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530826 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
827 }
828 })
829 }
830}
831
832func TestDeviceHandler_handleIndication(t *testing.T) {
833 dh1 := newMockDeviceHandler()
834 dh2 := negativeDeviceHandler()
835 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530836 dh3.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -0400837 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false, "mock_endpoint"))
838 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false, "mock_endpoint"))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530839
kdarapu891693b2019-09-16 12:33:49 +0530840 type args struct {
841 indication *oop.Indication
842 }
843 tests := []struct {
844 name string
845 deviceHandler *DeviceHandler
846 args args
847 }{
848 // TODO: Add test cases.
849 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
850 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
851 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
852 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
853 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
854 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
855 {"handleIndication-7", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}},
856 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
857 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
858 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
859 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
860 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
861 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
862 {"handleIndication-14", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}},
863 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
864 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
865 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
866 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
867
868 // Negative testcases
869 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
870 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
871 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
872 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
873 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
874 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
875 {"handleIndication-25", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuDiscInd{OnuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}}}},
876 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
877 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
878 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
879 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
880 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
881 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
882 {"handleIndication-32", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PortStats{PortStats: &oop.PortStatistics{IntfId: 1, RxBytes: 100, RxPackets: 100, RxUcastPackets: 100, RxMcastPackets: 100, RxBcastPackets: 100, RxErrorPackets: 100, TxBytes: 100, TxPackets: 100, TxUcastPackets: 100, TxMcastPackets: 100, TxBcastPackets: 100, TxErrorPackets: 100, RxCrcErrors: 100, BipErrors: 100, Timestamp: 1000}}}}},
883 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
884 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
885 //
886 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
887 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
888 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
889 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
890 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
891 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
892 }
893 for _, tt := range tests {
894 t.Run(tt.name, func(t *testing.T) {
895 dh := tt.deviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -0400896 time.Sleep(5 * time.Millisecond)
npujarec5762e2020-01-01 14:08:48 +0530897 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
898 defer cancel()
899 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530900 })
901 }
902}
903
904func TestDeviceHandler_addPort(t *testing.T) {
905 dh1 := newMockDeviceHandler()
906 dh2 := negativeDeviceHandler()
Elia Battiston596406d2022-02-02 12:19:00 +0100907
908 const defaultCapacity = uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)
kdarapu891693b2019-09-16 12:33:49 +0530909 type args struct {
Elia Battiston596406d2022-02-02 12:19:00 +0100910 intfID uint32
911 portType voltha.Port_PortType
912 state string
913 speedMbps uint32
kdarapu891693b2019-09-16 12:33:49 +0530914 }
915 tests := []struct {
Elia Battiston596406d2022-02-02 12:19:00 +0100916 name string
917 devicehandler *DeviceHandler
918 args args
919 expectedCapacity uint32
kdarapu891693b2019-09-16 12:33:49 +0530920 }{
921 // State up
Elia Battiston596406d2022-02-02 12:19:00 +0100922 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}, defaultCapacity},
923 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}, defaultCapacity},
924 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}, defaultCapacity},
925 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: defaultPortSpeedMbps}, defaultCapacity},
926 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}, defaultCapacity},
927 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}, defaultCapacity},
928 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}, defaultCapacity},
929 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}, defaultCapacity},
kdarapu891693b2019-09-16 12:33:49 +0530930 // state discovery
Elia Battiston596406d2022-02-02 12:19:00 +0100931 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}, defaultCapacity},
932 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}, defaultCapacity},
933 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}, defaultCapacity},
934 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down", speedMbps: defaultPortSpeedMbps}, defaultCapacity},
935 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}, defaultCapacity},
936 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}, defaultCapacity},
937 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}, defaultCapacity},
938 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}, defaultCapacity},
kdarapu891693b2019-09-16 12:33:49 +0530939
Elia Battiston596406d2022-02-02 12:19:00 +0100940 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: defaultPortSpeedMbps}, defaultCapacity},
941 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}, defaultCapacity},
942 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down", speedMbps: defaultPortSpeedMbps}, defaultCapacity},
943 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}, defaultCapacity},
944
945 // NNI speeds
946 {"addPort.21", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 0}, defaultCapacity},
947 {"addPort.22", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 10}, uint32(of.OfpPortFeatures_OFPPF_10MB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
948 {"addPort.23", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 100}, uint32(of.OfpPortFeatures_OFPPF_100MB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
949 {"addPort.24", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 1000}, uint32(of.OfpPortFeatures_OFPPF_1GB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
950 {"addPort.25", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 10000}, uint32(of.OfpPortFeatures_OFPPF_10GB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
951 {"addPort.26", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 40000}, uint32(of.OfpPortFeatures_OFPPF_40GB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
952 {"addPort.27", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 100000}, uint32(of.OfpPortFeatures_OFPPF_100GB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
953 {"addPort.28", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 1000000}, uint32(of.OfpPortFeatures_OFPPF_1TB_FD | of.OfpPortFeatures_OFPPF_FIBER)},
954 {"addPort.29", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up", speedMbps: 12345}, uint32(of.OfpPortFeatures_OFPPF_OTHER | of.OfpPortFeatures_OFPPF_FIBER)},
kdarapu891693b2019-09-16 12:33:49 +0530955 }
956 for _, tt := range tests {
957 t.Run(tt.name, func(t *testing.T) {
Elia Battiston596406d2022-02-02 12:19:00 +0100958 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state, tt.args.speedMbps)
959
960 //Check if the correct state is stored
961 storedState, ok := tt.devicehandler.activePorts.Load(tt.args.intfID)
962 expectedState := tt.args.state == "up"
963
964 if !ok {
965 t.Errorf("Port state not stored in test %v", tt.name)
966 } else if storedState != expectedState {
967 t.Errorf("Expected stored port state: %v, found: %v in test %v", expectedState, storedState, tt.name)
968 }
969
970 //Check if the reported speed values are correct
971 ofpPort := makeOfpPort(tt.devicehandler.device.MacAddress, tt.args.speedMbps)
972
973 if ofpPort.Curr != tt.expectedCapacity ||
974 ofpPort.Advertised != tt.expectedCapacity ||
975 ofpPort.Peer != tt.expectedCapacity {
976 t.Errorf("Expected port capacity: %v, found: (%v,%v,%v) in test %v", tt.expectedCapacity, ofpPort.Curr, ofpPort.Advertised, ofpPort.Peer, tt.name)
977 }
978
979 var expectedSpeedKbps uint32
980 if tt.args.speedMbps == 0 {
981 expectedSpeedKbps = defaultPortSpeedMbps * 1000
982 } else {
983 expectedSpeedKbps = tt.args.speedMbps * 1000
984 }
985
986 if ofpPort.CurrSpeed != expectedSpeedKbps ||
987 ofpPort.MaxSpeed != expectedSpeedKbps {
988 t.Errorf("Expected port speed: %v, found: (%v,%v) in test %v", expectedSpeedKbps, ofpPort.CurrSpeed, ofpPort.MaxSpeed, tt.name)
989 }
kdarapu891693b2019-09-16 12:33:49 +0530990 })
991 }
992}
993
994func Test_macAddressToUint32Array(t *testing.T) {
995 type args struct {
996 mac string
997 }
998 tests := []struct {
999 name string
1000 args args
1001 want []uint32
1002 }{
1003 // TODO: Add test cases.
1004 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
1005 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
1006 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
1007 }
1008 for _, tt := range tests {
1009 t.Run(tt.name, func(t *testing.T) {
1010 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
1011 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
1012 }
1013 })
1014 }
1015}
1016
1017func TestDeviceHandler_handleOltIndication(t *testing.T) {
1018
1019 type args struct {
1020 oltIndication *oop.OltIndication
1021 }
1022 tests := []struct {
1023 name string
1024 args args
1025 }{
1026 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
1027 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
1028 }
1029 for _, tt := range tests {
1030 t.Run(tt.name, func(t *testing.T) {
1031 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +05301032 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1033 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001034 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
1035 t.Error(err)
1036 }
kdarapu891693b2019-09-16 12:33:49 +05301037 })
1038 }
1039}
1040
1041func TestDeviceHandler_AdoptDevice(t *testing.T) {
1042 dh1 := newMockDeviceHandler()
1043 dh2 := negativeDeviceHandler()
1044 type args struct {
1045 device *voltha.Device
1046 }
1047 tests := []struct {
1048 name string
1049 devicehandler *DeviceHandler
1050 args args
1051 }{
1052 // TODO: Add test cases.
1053 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301054 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +05301055 }
1056 for _, tt := range tests {
1057 t.Run(tt.name, func(t *testing.T) {
1058 //dh.doStateInit()
1059 // context.
1060 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +05301061 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1062 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001063 if err := tt.devicehandler.postInit(ctx); err != nil {
1064 t.Error(err)
1065 }
kdarapu891693b2019-09-16 12:33:49 +05301066 })
1067 }
1068}
1069
1070func TestDeviceHandler_activateONU(t *testing.T) {
1071 dh := newMockDeviceHandler()
1072 dh1 := negativeDeviceHandler()
1073 type args struct {
1074 intfID uint32
1075 onuID int64
1076 serialNum *oop.SerialNumber
1077 serialNumber string
1078 }
1079 tests := []struct {
1080 name string
1081 devicehandler *DeviceHandler
1082 args args
1083 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -07001084 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
1085 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
1086 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
1087 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +05301088 }
1089 for _, tt := range tests {
1090 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301091 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1092 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001093 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
1094 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301095 })
1096 }
1097}
1098
1099func TestDeviceHandler_start(t *testing.T) {
1100 dh := newMockDeviceHandler()
1101 dh1 := negativeDeviceHandler()
1102 dh.start(context.Background())
1103 dh.stop(context.Background())
1104
1105 dh1.start(context.Background())
1106 dh1.stop(context.Background())
1107
1108}
1109
1110func TestDeviceHandler_PacketOut(t *testing.T) {
1111 dh1 := newMockDeviceHandler()
1112 dh2 := negativeDeviceHandler()
1113 acts := []*ofp.OfpAction{
1114 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
1115 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
1116 fu.Output(1),
1117 }
1118 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
1119 type args struct {
1120 egressPortNo int
1121 packet *of.OfpPacketOut
1122 }
1123 tests := []struct {
1124 name string
1125 devicehandler *DeviceHandler
1126 args args
1127 wantErr bool
1128 }{
1129 // TODO: Add test cases.
1130 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
1131 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
1132 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -07001133 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001134 {"PacketOut-4", dh1, args{egressPortNo: 16777217, packet: pktout}, false},
1135 {"PacketOut-5", dh2, args{egressPortNo: 16777216, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +05301136 }
1137 for _, tt := range tests {
1138 t.Run(tt.name, func(t *testing.T) {
1139 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +05301140 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1141 defer cancel()
khenaidoo106c61a2021-08-11 18:05:46 -04001142 if err := dh.PacketOut(ctx, uint32(tt.args.egressPortNo), tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301143 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
1144 }
1145 })
1146 }
1147}
1148
1149//
1150func TestDeviceHandler_doStateUp(t *testing.T) {
1151 dh1 := newMockDeviceHandler()
1152 dh2 := newMockDeviceHandler()
1153
Thomas Lee S985938d2020-05-04 11:40:41 +05301154 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301155 dh3 := negativeDeviceHandler()
1156
1157 tests := []struct {
1158 name string
1159 devicehandler *DeviceHandler
1160 wantErr bool
1161 }{
1162 {"dostateup-1", dh1, false},
1163 {"dostateup-2", dh2, false},
1164 {"dostateup-3", dh3, true},
1165 }
1166 for _, tt := range tests {
1167 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301168 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1169 defer cancel()
1170 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301171 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1172 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301173 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301174 })
1175 }
1176}
1177func TestDeviceHandler_doStateDown(t *testing.T) {
1178 dh1 := newMockDeviceHandler()
1179 dh2 := negativeDeviceHandler()
1180 dh3 := newMockDeviceHandler()
1181 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1182 tests := []struct {
1183 name string
1184 devicehandler *DeviceHandler
1185 wantErr bool
1186 }{
1187 {"dostatedown-1", dh1, false},
1188 {"dostatedown-2", dh2, true},
1189 {"dostatedown-2", dh3, true},
1190 }
1191 for _, tt := range tests {
1192 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301193 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1194 defer cancel()
1195 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301196 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001197 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301198 }
1199 })
1200 }
1201}
1202
1203func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1204 dh1 := newMockDeviceHandler()
1205 dh2 := negativeDeviceHandler()
1206 type args struct {
1207 device *voltha.Device
1208 }
1209 tests := []struct {
1210 name string
1211 devicehandler *DeviceHandler
1212 args args
1213 wantErr bool
1214 }{
1215 // TODO: Add test cases.
1216 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1217 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1218 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1219 }
1220 for _, tt := range tests {
1221 t.Run(tt.name, func(t *testing.T) {
1222 dh := tt.devicehandler
1223 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1224 if (err != nil) != tt.wantErr {
1225 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1226 return
1227 }
1228 })
1229 }
1230}
1231
kdarapu891693b2019-09-16 12:33:49 +05301232func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1233
1234 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301235 dh1.discOnus = sync.Map{}
1236 dh1.discOnus.Store("onu1", true)
1237 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301238 dh1.discOnus.Store("onu3", true)
1239 dh1.discOnus.Store("onu4", true)
1240 dh1.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -04001241 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true, "mock_endpoint"))
1242 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true, "mock_endpoint"))
kdarapu891693b2019-09-16 12:33:49 +05301243 dh2 := negativeDeviceHandler()
1244 type args struct {
1245 onuDiscInd *oop.OnuDiscIndication
1246 sn string
1247 }
1248 tests := []struct {
1249 name string
1250 devicehandler *DeviceHandler
1251 args args
1252 }{
1253 // TODO: Add test cases.
1254 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1255 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1256 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1257 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1258 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1259 {"onuDiscIndication-6", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu2"}},
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301260 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1261 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1262 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301263 }
1264 for _, tt := range tests {
1265 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301266 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1267 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001268 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001269 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301270 })
1271 }
1272}
1273
1274func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1275 dh1 := newMockDeviceHandler()
1276 dh2 := negativeDeviceHandler()
1277 tests := []struct {
1278 name string
1279 devicehandler *DeviceHandler
1280
1281 wantErr bool
1282 }{
1283 // TODO: Add test cases.
1284 {"populateDeviceInfo-1", dh1, false},
1285 {"populateDeviceInfo-2", dh1, true},
1286 {"populateDeviceInfo-3", dh1, true},
1287 {"populateDeviceInfo-4", dh1, true},
1288 {"populateDeviceInfo-5", dh2, true},
1289 }
1290 for _, tt := range tests {
1291 t.Run(tt.name, func(t *testing.T) {
1292
Neha Sharma96b7bf22020-06-15 10:37:32 +00001293 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301294 if (err != nil) != tt.wantErr {
1295 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1296 return
1297 }
1298
1299 })
1300 }
1301}
1302
1303func TestDeviceHandler_readIndications(t *testing.T) {
1304 dh1 := newMockDeviceHandler()
1305 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301306 dh3 := newMockDeviceHandler()
1307 dh3.device.AdminState = voltha.AdminState_DISABLED
1308 dh4 := negativeDeviceHandler()
1309 tests := []struct {
1310 name string
1311 devicehandler *DeviceHandler
1312 }{
1313 // TODO: Add test cases.
1314 {"readIndications-1", dh1},
1315 {"readIndications-2", dh2},
1316 {"readIndications-3", dh2},
1317 {"readIndications-4", dh2},
1318 {"readIndications-5", dh2},
1319 {"readIndications-6", dh3},
1320 {"readIndications-7", dh3},
1321 {"readIndications-8", dh4},
1322 }
1323 for _, tt := range tests {
1324 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301325 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1326 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001327 _ = tt.devicehandler.readIndications(ctx)
1328 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301329 })
1330 }
1331}
Naga Manjunath7615e552019-10-11 22:35:47 +05301332
1333func Test_startCollector(t *testing.T) {
1334 type args struct {
1335 dh *DeviceHandler
1336 }
1337 dh := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001338 mcs := newMockCoreService()
1339 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301340 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001341 {PortNo: 16777216, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1342 {PortNo: 16777218, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301343 }
khenaidoo106c61a2021-08-11 18:05:46 -04001344 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301345 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301346 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1347 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301348 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1349 dh.portStats.Device = dh
1350 for i := 0; i < 16; i++ {
1351 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1352 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301353 dh1 := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001354 mcs = newMockCoreService()
1355 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{}
1356 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301357 tests := []struct {
1358 name string
1359 args args
1360 }{
1361 // TODO: Add test cases.
1362 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301363 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301364 }
1365 for _, tt := range tests {
1366 t.Run(tt.name, func(t *testing.T) {
1367 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301368 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301369 tt.args.dh.stopCollector <- true
1370 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001371 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301372 })
1373 }
1374}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001375
1376func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1377
1378 // olt disconnect (not reboot)
1379 dh1 := newMockDeviceHandler()
1380 dh1.adapterPreviouslyConnected = false
1381 dh1.agentPreviouslyConnected = true
1382
1383 // adapter restart
1384 dh2 := newMockDeviceHandler()
1385 dh2.Client = &mocks.MockOpenoltClient{}
1386 dh2.adapterPreviouslyConnected = true
1387 dh2.agentPreviouslyConnected = true
1388
1389 // first connection or olt restart
1390 dh3 := newMockDeviceHandler()
1391 dh3.Client = &mocks.MockOpenoltClient{}
1392 dh3.adapterPreviouslyConnected = false
1393 dh3.agentPreviouslyConnected = false
1394
1395 // olt and adapter restart at the same time (first case)
1396 dh4 := newMockDeviceHandler()
1397 dh4.Client = &mocks.MockOpenoltClient{}
1398 dh4.adapterPreviouslyConnected = true
1399 dh4.agentPreviouslyConnected = false
1400
1401 // adapter restart and olt disconnect at the same time
1402 dh5 := newMockDeviceHandler()
1403 dh5.Client = &mocks.MockOpenoltClient{}
1404 dh5.adapterPreviouslyConnected = true
1405 dh5.agentPreviouslyConnected = true
1406
1407 tests := []struct {
1408 name string
1409 devicehandler *DeviceHandler
1410 expectedRestart bool
1411 wantErr bool
1412 }{
1413 {"dostateup-1", dh1, true, false},
1414 {"dostateup-2", dh2, false, false},
1415 {"dostateup-3", dh3, false, false},
1416 {"dostateup-4", dh4, true, false},
1417 {"dostateup-5", dh5, false, false},
1418 }
1419 for _, tt := range tests {
1420 t.Run(tt.name, func(t *testing.T) {
1421 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1422 defer cancel()
1423 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1424 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1425 }
1426 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1427 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1428 if tt.expectedRestart != isRestarted {
1429 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1430 }
1431 })
1432 }
1433}
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001434
1435func Test_UpdateFlowsIncrementallyNegativeTestCases(t *testing.T) {
1436 dh1 := negativeDeviceHandlerNilFlowMgr()
1437 tests := []struct {
1438 name string
1439 devicehandler *DeviceHandler
1440 wantErr bool
1441 }{
1442 {"update-flow-when-device-handler-is-nil", dh1, true},
1443 }
1444
khenaidoodc2116e2021-10-19 17:33:19 -04001445 flowMetadata0 := of.FlowMetadata{Meters: []*of.OfpMeterConfig{
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001446 {
1447 Flags: 5,
1448 MeterId: 1,
khenaidoodc2116e2021-10-19 17:33:19 -04001449 Bands: []*of.OfpMeterBandHeader{
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001450 {
khenaidoodc2116e2021-10-19 17:33:19 -04001451 Type: of.OfpMeterBandType_OFPMBT_DROP,
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001452 Rate: 16000,
1453 BurstSize: 0,
1454 },
1455 {
khenaidoodc2116e2021-10-19 17:33:19 -04001456 Type: of.OfpMeterBandType_OFPMBT_DROP,
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001457 Rate: 32000,
1458 BurstSize: 30,
1459 },
1460 {
khenaidoodc2116e2021-10-19 17:33:19 -04001461 Type: of.OfpMeterBandType_OFPMBT_DROP,
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001462 Rate: 64000,
1463 BurstSize: 30,
1464 },
1465 },
1466 },
1467 }}
1468
1469 kwTable0Meter1 := make(map[string]uint64)
1470 kwTable0Meter1["table_id"] = 0
1471 kwTable0Meter1["meter_id"] = 1
1472 kwTable0Meter1["write_metadata"] = 0x4000000000 // Tech-Profile-ID 64
1473
1474 // Upstream flow DHCP flow - ONU1 UNI0 PON0
1475 fa0 := &fu.FlowArgs{
1476 MatchFields: []*ofp.OfpOxmOfbField{
1477 fu.InPort(536870912),
1478 fu.Metadata_ofp(1),
1479 fu.IpProto(17), // dhcp
1480 fu.VlanPcp(0),
1481 fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT)),
1482 fu.TunnelId(256),
1483 },
1484 Actions: []*ofp.OfpAction{
1485 //fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA | 2))),
1486 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 257)),
1487 fu.Output(2147483645),
1488 fu.PushVlan(0x8100),
1489 },
1490 KV: kwTable0Meter1,
1491 }
1492
1493 flow0, _ := fu.MkFlowStat(fa0)
khenaidoodc2116e2021-10-19 17:33:19 -04001494 flowAdd := of.Flows{Items: make([]*of.OfpFlowStats, 0)}
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001495 flowAdd.Items = append(flowAdd.Items, flow0)
khenaidoodc2116e2021-10-19 17:33:19 -04001496 flowRemove := of.Flows{Items: make([]*of.OfpFlowStats, 0)}
Girish Gowdra0fb24a32021-10-27 15:15:27 -07001497 flowChanges := &ofp.FlowChanges{ToAdd: &flowAdd, ToRemove: &flowRemove}
1498
1499 for _, tt := range tests {
1500 t.Run(tt.name, func(t *testing.T) {
1501 err := tt.devicehandler.UpdateFlowsIncrementally(context.Background(), tt.devicehandler.device, flowChanges, nil, &flowMetadata0)
1502 if (err != nil) != tt.wantErr {
1503 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1504 return
1505 }
1506 })
1507 }
1508}