blob: 60ed30b5f4398c879ea7de0eb3cb37bef2746975 [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"
khenaidoo106c61a2021-08-11 18:05:46 -040040 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
41 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"
kdarapu381c6902019-07-31 18:23:16 +053045)
46
Girish Gowdra9602eb42020-09-09 15:50:39 -070047const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070048 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070049 OnuIDStart = 1
50 OnuIDEnd = 32
51 AllocIDStart = 1
52 AllocIDEnd = 10
53 GemIDStart = 1
54 GemIDEnd = 10
55 FlowIDStart = 1
56 FlowIDEnd = 10
57)
58
khenaidoo106c61a2021-08-11 18:05:46 -040059func newMockOnuInterAdapterService() *mocks.MockOnuInterAdapterService {
60 return &mocks.MockOnuInterAdapterService{}
61}
62
63func newMockCoreService() *mocks.MockCoreService {
64 mcp := mocks.MockCoreService{
Kent Hagermanf1db18b2020-07-08 13:38:15 -040065 Devices: make(map[string]*voltha.Device),
66 DevicePorts: make(map[string][]*voltha.Port),
67 }
Naga Manjunath7615e552019-10-11 22:35:47 +053068 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053069 mcp.Devices["olt"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040070 Id: "olt",
71 Root: true,
72 ParentId: "logical_device",
73 ParentPortNo: 1,
74 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +053075 ProxyAddress: &voltha.Device_ProxyAddress{
76 DeviceId: "olt",
77 DeviceType: "onu",
78 ChannelId: 1,
79 ChannelGroupId: 1,
80 },
81 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053082 PmConfigs: &voltha.PmConfigs{
83 DefaultFreq: 10,
84 Id: "olt",
85 FreqOverride: false,
86 Grouped: false,
87 Metrics: pm,
88 },
kdarapu891693b2019-09-16 12:33:49 +053089 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040090 mcp.DevicePorts["olt"] = []*voltha.Port{
91 {PortNo: 1, Label: "pon"},
92 {PortNo: 2, Label: "nni"},
93 }
kdarapu891693b2019-09-16 12:33:49 +053094
Kent Hagermanf1db18b2020-07-08 13:38:15 -040095 mcp.Devices["onu1"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040096 Id: "1",
97 Root: false,
98 ParentId: "olt",
99 ParentPortNo: 1,
100 AdapterEndpoint: "mock-onu-endpoint",
101 OperStatus: 4,
kdarapu891693b2019-09-16 12:33:49 +0530102 ProxyAddress: &voltha.Device_ProxyAddress{
103 OnuId: 1,
104 ChannelId: 1,
105 ChannelGroupId: 1,
106 },
107 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530108 PmConfigs: &voltha.PmConfigs{
109 DefaultFreq: 10,
110 Id: "olt",
111 FreqOverride: false,
112 Grouped: false,
113 Metrics: pm,
114 },
kdarapu891693b2019-09-16 12:33:49 +0530115 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400116 mcp.DevicePorts["onu1"] = []*voltha.Port{
117 {PortNo: 1, Label: "pon"},
118 {PortNo: 2, Label: "uni"},
119 }
120
kdarapu891693b2019-09-16 12:33:49 +0530121 mcp.Devices["onu2"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400122 Id: "2",
123 Root: false,
124 ParentId: "olt",
125 OperStatus: 2,
126 AdapterEndpoint: "mock-onu-endpoint",
127 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +0530128
129 ProxyAddress: &voltha.Device_ProxyAddress{
130 OnuId: 2,
131 ChannelId: 1,
132 ChannelGroupId: 1,
133 },
134 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530135 PmConfigs: &voltha.PmConfigs{
136 DefaultFreq: 10,
137 Id: "olt",
138 FreqOverride: false,
139 Grouped: false,
140 Metrics: pm,
141 },
kdarapu891693b2019-09-16 12:33:49 +0530142 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400143 mcp.DevicePorts["onu2"] = []*voltha.Port{
144 {PortNo: 1, Label: "pon"},
145 {PortNo: 2, Label: "uni"},
146 }
kdarapu891693b2019-09-16 12:33:49 +0530147 return &mcp
148}
149func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530150 device := &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400151 Id: "olt",
152 Root: true,
153 ParentId: "logical_device",
154 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +0530155 ProxyAddress: &voltha.Device_ProxyAddress{
156 DeviceId: "olt",
157 DeviceType: "onu",
158 ChannelId: 1,
159 ChannelGroupId: 1,
160 },
161 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530162 }
khenaidoo106c61a2021-08-11 18:05:46 -0400163 mcs := newMockCoreService()
164 cc := mocks.NewMockCoreClient(mcs)
kdarapu891693b2019-09-16 12:33:49 +0530165 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800166 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700167 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
kesavand494c2082020-08-31 11:16:12 +0530168 cfg := &config.AdapterFlags{OmccEncryption: true}
khenaidoo106c61a2021-08-11 18:05:46 -0400169 openOLT := &OpenOLT{eventProxy: ep, config: cfg}
170 dh := NewDeviceHandler(cc, ep, device, openOLT, cm, cfg)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700171 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700172 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 -0700173 Technology: "xgs-pon",
174 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
175 }}
176
Girish Gowdra9602eb42020-09-09 15:50:39 -0700177 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700178 dh.deviceInfo = deviceInf
khenaidoo106c61a2021-08-11 18:05:46 -0400179 dh.device = device
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700180 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
181 var i uint32
182 for i = 0; i < deviceInf.PonPorts; i++ {
183 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
184 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700185 StoreType: "etcd",
186 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700187 }}
188 dh.resourceMgr[i].InitLocalCache()
189 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700190
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530191 ranges := make(map[string]interface{})
192 sharedIdxByType := make(map[string]string)
193 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
194 sharedIdxByType["ONU_ID"] = "ONU_ID"
195 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
196 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
197 ranges["ONU_ID"] = uint32(0)
198 ranges["GEMPORT_ID"] = uint32(0)
199 ranges["ALLOC_ID"] = uint32(0)
200 ranges["FLOW_ID"] = uint32(0)
201 ranges["onu_id_shared"] = uint32(0)
202 ranges["alloc_id_shared"] = uint32(0)
203 ranges["gemport_id_shared"] = uint32(0)
204 ranges["flow_id_shared"] = uint32(0)
205
Matteo Scandolo84585372021-03-18 14:21:22 -0700206 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700207 ponmgr.DeviceID = "onu-1"
208 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
209 ponmgr.KVStore = &db.Backend{
210 Client: &mocks.MockKVClient{},
211 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700212 ponmgr.KVStoreForConfig = &db.Backend{
213 Client: &mocks.MockKVClient{},
214 }
215 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700216 ponmgr.PonResourceRanges = ranges
217 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700218 ponmgr.Technology = "XGS-PON"
219 for i = 0; i < deviceInf.PonPorts; i++ {
220 dh.resourceMgr[i].PonRsrMgr = ponmgr
221 }
222
npujarec5762e2020-01-01 14:08:48 +0530223 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
224 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700225 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700226 dh.totalPonPorts = NumPonPorts
227 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700228 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700229 dh.flowMgr[i] = &OpenOltFlowMgr{}
230 dh.flowMgr[i].deviceHandler = dh
231 dh.flowMgr[i].ponPortIdx = i
232 dh.flowMgr[i].grpMgr = dh.groupMgr
233 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700234 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
235 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
236 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
237 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
238
Girish Gowdra76a1b092021-07-28 10:07:04 -0700239 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
240
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700241 // Create a slice of buffered channels for handling concurrent flows per ONU.
242 // 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 -0700243 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, plt.MaxOnusPerPon+1)
244 dh.flowMgr[i].stopFlowHandlerRoutine = make([]chan bool, plt.MaxOnusPerPon+1)
245 dh.flowMgr[i].flowHandlerRoutineActive = make([]bool, plt.MaxOnusPerPon+1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700246 for j := range dh.flowMgr[i].incomingFlows {
247 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700248 dh.flowMgr[i].stopFlowHandlerRoutine[j] = make(chan bool, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700249 // Spin up a go routine to handling incoming flows (add/remove).
250 // There will be on go routine per ONU.
251 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700252 dh.flowMgr[i].flowHandlerRoutineActive[j] = true
253 go dh.flowMgr[i].perOnuFlowHandlerRoutine(j, dh.flowMgr[i].incomingFlows[j], dh.flowMgr[i].stopFlowHandlerRoutine[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700254 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700255 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700256 }
kdarapu891693b2019-09-16 12:33:49 +0530257 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500258 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530259 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530260 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000261
262 var pmNames = []string{
263 "rx_bytes",
264 "rx_packets",
265 "rx_mcast_packets",
266 "rx_bcast_packets",
267 "tx_bytes",
268 "tx_packets",
269 "tx_mcast_packets",
270 "tx_bcast_packets",
271 }
272
273 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
khenaidoo106c61a2021-08-11 18:05:46 -0400274
275 // Set the children endpoints
276 dh.childAdapterClients = map[string]*vgrpc.Client{
277 "mock-onu-endpoint": mocks.NewMockChildAdapterClient(newMockOnuInterAdapterService()),
278 }
kdarapu891693b2019-09-16 12:33:49 +0530279 return dh
kdarapu381c6902019-07-31 18:23:16 +0530280}
281
kdarapu891693b2019-09-16 12:33:49 +0530282func negativeDeviceHandler() *DeviceHandler {
283 dh := newMockDeviceHandler()
284 device := dh.device
285 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530286 return dh
287}
kdarapu381c6902019-07-31 18:23:16 +0530288func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000289 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530290 type args struct {
291 host string
292 }
293 tests := []struct {
294 name string
295 args args
296 want string
297 wantErr bool
298 }{
kdarapu891693b2019-09-16 12:33:49 +0530299 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
300 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
301 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
302 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530303 }
304 for _, tt := range tests {
305 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000306 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530307 if (err != nil) != tt.wantErr {
308 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
309 return
310 }
311 if got != tt.want {
312 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
313 }
314 })
315 }
316}
317func Test_macifyIP(t *testing.T) {
318 type args struct {
319 ip net.IP
320 }
321 tests := []struct {
322 name string
323 args args
324 want string
325 }{{
kdarapu891693b2019-09-16 12:33:49 +0530326 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530327 args{ip: net.ParseIP("10.10.10.10")},
328 "00:00:0a:0a:0a:0a",
329 },
330 {
kdarapu891693b2019-09-16 12:33:49 +0530331 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530332 args{ip: net.ParseIP("127.0.0.1")},
333 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530334 },
335 {
336 "macifyIP-3",
337 args{ip: net.ParseIP("127.0.0.1/24")},
338 "",
339 },
340 }
kdarapu381c6902019-07-31 18:23:16 +0530341 for _, tt := range tests {
342 t.Run(tt.name, func(t *testing.T) {
343 if got := macifyIP(tt.args.ip); got != tt.want {
344 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
345 }
346 })
347 }
348}
349
David K. Bainbridge794735f2020-02-11 21:01:37 -0800350func sparseCompare(keys []string, spec, target interface{}) bool {
351 if spec == target {
352 return true
353 }
354 if spec == nil || target == nil {
355 return false
356 }
357 typeSpec := reflect.TypeOf(spec)
358 typeTarget := reflect.TypeOf(target)
359 if typeSpec != typeTarget {
360 return false
361 }
362
363 vSpec := reflect.ValueOf(spec)
364 vTarget := reflect.ValueOf(target)
365 if vSpec.Kind() == reflect.Ptr {
366 vSpec = vSpec.Elem()
367 vTarget = vTarget.Elem()
368 }
369
370 for _, key := range keys {
371 fSpec := vSpec.FieldByName(key)
372 fTarget := vTarget.FieldByName(key)
373 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
374 return false
375 }
376 }
377 return true
378}
379
kdarapu381c6902019-07-31 18:23:16 +0530380func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000381 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530382 dh1 := newMockDeviceHandler()
383 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530384 type args struct {
385 parentPort uint32
386 onuID uint32
387 }
388 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530389 name string
390 devicehandler *DeviceHandler
391 args args
392 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800393 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530394 }{
kdarapu891693b2019-09-16 12:33:49 +0530395 {"GetChildDevice-1", dh1,
396 args{parentPort: 1,
397 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800398 &voltha.Device{
399 Id: "1",
400 ParentId: "olt",
401 ParentPortNo: 1,
402 },
403 nil,
kdarapu891693b2019-09-16 12:33:49 +0530404 },
405 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530406 args{parentPort: 1,
407 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800408 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530409 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530410 },
411 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800412
413 /*
414 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
415 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>)
416 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
417 */
kdarapu381c6902019-07-31 18:23:16 +0530418 for _, tt := range tests {
419 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000420 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800421 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
422 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
423 tt.want, tt.errType, got, reflect.TypeOf(err))
424 return
425 }
kdarapu381c6902019-07-31 18:23:16 +0530426 t.Log("onu device id", got)
427 })
428 }
429}
kdarapu891693b2019-09-16 12:33:49 +0530430
431func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530432 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530433 type args struct {
434 portNum uint32
435 portType voltha.Port_PortType
436 }
437 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800438 name string
439 args args
440 want string
441 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530442 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800443 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
444 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
445 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
446 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
447 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
448 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
449 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530450 }
451 for _, tt := range tests {
452 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800453 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
454 if reflect.TypeOf(err) != tt.errType || got != tt.want {
455 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
456 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530457 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800458
kdarapu891693b2019-09-16 12:33:49 +0530459 })
460 }
461}
462
khenaidoo106c61a2021-08-11 18:05:46 -0400463// func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
464// ctx := context.Background()
465// dh := newMockDeviceHandler()
466// proxyAddr := dh.device.ProxyAddress
467// body := &ic.InterAdapterOmciMessage{
468// Message: []byte("asdfasdfasdfasdfas"),
469// ProxyAddress: proxyAddr,
470// }
471// body2 := &ic.InterAdapterOmciMessage{
472// Message: []byte("asdfasdfasdfasdfas"),
473// //ProxyAddress: &voltha.Device_ProxyAddress{},
474// }
475// body3 := &ic.InterAdapterTechProfileDownloadMessage{}
476// var marshalledData *any.Any
477// var err error
kdarapu891693b2019-09-16 12:33:49 +0530478
khenaidoo106c61a2021-08-11 18:05:46 -0400479// if marshalledData, err = ptypes.MarshalAny(body); err != nil {
480// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
481// }
kdarapu891693b2019-09-16 12:33:49 +0530482
khenaidoo106c61a2021-08-11 18:05:46 -0400483// var marshalledData1 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530484
khenaidoo106c61a2021-08-11 18:05:46 -0400485// if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
486// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
487// }
488// var marshalledData2 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530489
khenaidoo106c61a2021-08-11 18:05:46 -0400490// if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
491// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
492// }
493// type args struct {
494// msg *ic.InterAdapterMessage
495// }
496// invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
497// tests := []struct {
498// name string
499// args args
500// wantErr reflect.Type
501// }{
502// {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
503// Header: &ic.InterAdapterHeader{
504// Id: "012345",
505// Type: ic.InterAdapterMessageType_FLOW_REQUEST,
506// },
507// Body: marshalledData,
508// }}, invalid},
509// {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
510// Header: &ic.InterAdapterHeader{
511// Id: "012345",
512// Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
513// },
514// Body: marshalledData1,
515// }}, invalid},
516// {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
517// Header: &ic.InterAdapterHeader{
518// Id: "012345",
519// Type: ic.InterAdapterMessageType_OMCI_REQUEST,
520// },
521// Body: marshalledData,
522// }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
523// {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
524// Header: &ic.InterAdapterHeader{
525// Id: "012345",
526// Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
527// }, Body: marshalledData,
528// }}, invalid},
529// {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
530// Header: &ic.InterAdapterHeader{
531// Id: "012345",
532// Type: ic.InterAdapterMessageType_METRICS_REQUEST,
533// }, Body: marshalledData1,
534// }}, invalid},
535// {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
536// Header: &ic.InterAdapterHeader{
537// Id: "012345",
538// Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
539// }, Body: marshalledData,
540// }}, invalid},
541// {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
542// Header: &ic.InterAdapterHeader{
543// Id: "012345",
544// Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
545// }, Body: marshalledData,
546// }}, invalid},
547// {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
548// Header: &ic.InterAdapterHeader{
549// Id: "012345",
550// Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
551// }, Body: marshalledData,
552// }}, invalid},
553// {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
554// Header: &ic.InterAdapterHeader{
555// Id: "012345",
556// Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
557// }, Body: marshalledData,
558// }}, invalid},
559// {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
560// Header: &ic.InterAdapterHeader{
561// Id: "012345",
562// Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
563// }, Body: marshalledData2,
564// }}, invalid},
565// {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
566// Header: &ic.InterAdapterHeader{
567// Id: "012345",
568// Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
569// }, Body: marshalledData2,
570// }}, invalid},
571// }
572// for _, tt := range tests {
573// t.Run(tt.name, func(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530574
khenaidoo106c61a2021-08-11 18:05:46 -0400575// if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
576// t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
577// }
578// })
579// }
580// }
kdarapu891693b2019-09-16 12:33:49 +0530581
khenaidoo106c61a2021-08-11 18:05:46 -0400582func TestDeviceHandler_ProxyOmciMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000583 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530584 dh1 := newMockDeviceHandler()
585 dh2 := negativeDeviceHandler()
586 device1 := &voltha.Device{
587 Id: "onu1",
588 Root: false,
589 ParentId: "logical_device",
590 ProxyAddress: &voltha.Device_ProxyAddress{
591 DeviceId: "onu1",
592 DeviceType: "onu",
593 ChannelId: 1,
594 ChannelGroupId: 1,
595 },
596 ConnectStatus: 1,
597 }
598 device2 := device1
599 device2.ConnectStatus = 2
khenaidoo106c61a2021-08-11 18:05:46 -0400600 iaomciMsg1 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530601 ProxyAddress: &voltha.Device_ProxyAddress{
602 DeviceId: "onu2",
603 DeviceType: "onu",
604 ChannelId: 1,
605 ChannelGroupId: 1,
kdarapu891693b2019-09-16 12:33:49 +0530606 },
607 ConnectStatus: 1,
608 }
khenaidoo106c61a2021-08-11 18:05:46 -0400609 iaomciMsg2 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530610 ProxyAddress: &voltha.Device_ProxyAddress{
611 DeviceId: "onu3",
612 DeviceType: "onu",
613 ChannelId: 1,
614 ChannelGroupId: 1,
615 },
616 ConnectStatus: 1,
617 }
618 type args struct {
619 onuDevice *voltha.Device
khenaidoo106c61a2021-08-11 18:05:46 -0400620 omciMsg *ic.OmciMessage
kdarapu891693b2019-09-16 12:33:49 +0530621 }
622 tests := []struct {
623 name string
624 devicehandler *DeviceHandler
625 args args
626 }{
khenaidoo106c61a2021-08-11 18:05:46 -0400627 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
628 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530629 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
630 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
631 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
khenaidoo106c61a2021-08-11 18:05:46 -0400632 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530633 }
634 for _, tt := range tests {
635 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400636 _ = tt.devicehandler.ProxyOmciMessage(ctx, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400637 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530638 })
639 }
640}
641
642func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
643 dh1 := newMockDeviceHandler()
644 dh2 := negativeDeviceHandler()
645
646 type args struct {
647 logicalPort uint32
648 packetPayload []byte
649 }
650 tests := []struct {
651 name string
652 devicehandler *DeviceHandler
653 args args
654 }{
655 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
656 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
657 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
658 }
659 for _, tt := range tests {
660 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400661 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
662 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530663 })
664 }
665}
666
667func TestDeviceHandler_DisableDevice(t *testing.T) {
668 dh1 := newMockDeviceHandler()
669 dh2 := negativeDeviceHandler()
670 type args struct {
671 device *voltha.Device
672 }
673 tests := []struct {
674 name string
675 devicehandler *DeviceHandler
676 args args
677 wantErr bool
678 }{
679 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400680 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530681 }
682 for _, tt := range tests {
683 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000684 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530685 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
686 }
687 })
688 }
689}
690
691func TestDeviceHandler_ReenableDevice(t *testing.T) {
692 dh1 := newMockDeviceHandler()
693 dh2 := negativeDeviceHandler()
694 type args struct {
695 device *voltha.Device
696 }
697 tests := []struct {
698 name string
699 devicehandler *DeviceHandler
700 args args
701 wantErr bool
702 }{
703 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
704 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
705 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
706 }
707 for _, tt := range tests {
708 t.Run(tt.name, func(t *testing.T) {
709 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000710 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530711 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
712 }
713 })
714 }
715}
716
717func TestDeviceHandler_RebootDevice(t *testing.T) {
718 dh1 := newMockDeviceHandler()
719 dh2 := newMockDeviceHandler()
720 type args struct {
721 device *voltha.Device
722 }
723 tests := []struct {
724 name string
725 devicehandler *DeviceHandler
726 args args
727 wantErr bool
728 }{
729 // TODO: Add test cases.
730 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
731 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
732 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
733 }
734 for _, tt := range tests {
735 t.Run(tt.name, func(t *testing.T) {
736
Neha Sharma96b7bf22020-06-15 10:37:32 +0000737 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530738 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
739 }
740 })
741 }
742}
743
744func TestDeviceHandler_handleIndication(t *testing.T) {
745 dh1 := newMockDeviceHandler()
746 dh2 := negativeDeviceHandler()
747 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530748 dh3.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -0400749 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false, "mock_endpoint"))
750 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false, "mock_endpoint"))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530751
kdarapu891693b2019-09-16 12:33:49 +0530752 type args struct {
753 indication *oop.Indication
754 }
755 tests := []struct {
756 name string
757 deviceHandler *DeviceHandler
758 args args
759 }{
760 // TODO: Add test cases.
761 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
762 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
763 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
764 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
765 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
766 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
767 {"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")}}}}}},
768 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
769 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
770 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
771 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
772 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
773 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
774 {"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}}}}},
775 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
776 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
777 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
778 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
779
780 // Negative testcases
781 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
782 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
783 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
784 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
785 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
786 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
787 {"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")}}}}}},
788 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
789 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
790 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
791 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
792 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
793 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
794 {"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}}}}},
795 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
796 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
797 //
798 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
799 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
800 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
801 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
802 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
803 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
804 }
805 for _, tt := range tests {
806 t.Run(tt.name, func(t *testing.T) {
807 dh := tt.deviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -0400808 time.Sleep(5 * time.Millisecond)
npujarec5762e2020-01-01 14:08:48 +0530809 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
810 defer cancel()
811 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530812 })
813 }
814}
815
816func TestDeviceHandler_addPort(t *testing.T) {
817 dh1 := newMockDeviceHandler()
818 dh2 := negativeDeviceHandler()
819 type args struct {
820 intfID uint32
821 portType voltha.Port_PortType
822 state string
823 }
824 tests := []struct {
825 name string
826 devicehandler *DeviceHandler
827 args args
828 }{
829 // State up
830 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
831 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
832 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
833 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
834 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
835 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
836 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
837 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
838 // state discovery
839 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
840 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
841 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
842 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
843 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
844 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
845 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
846 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
847
848 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
849 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
850 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
851 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
852 }
853 for _, tt := range tests {
854 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400855 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
856 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530857 })
858 }
859}
860
861func Test_macAddressToUint32Array(t *testing.T) {
862 type args struct {
863 mac string
864 }
865 tests := []struct {
866 name string
867 args args
868 want []uint32
869 }{
870 // TODO: Add test cases.
871 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
872 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
873 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
874 }
875 for _, tt := range tests {
876 t.Run(tt.name, func(t *testing.T) {
877 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
878 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
879 }
880 })
881 }
882}
883
884func TestDeviceHandler_handleOltIndication(t *testing.T) {
885
886 type args struct {
887 oltIndication *oop.OltIndication
888 }
889 tests := []struct {
890 name string
891 args args
892 }{
893 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
894 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
895 }
896 for _, tt := range tests {
897 t.Run(tt.name, func(t *testing.T) {
898 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530899 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
900 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400901 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
902 t.Error(err)
903 }
kdarapu891693b2019-09-16 12:33:49 +0530904 })
905 }
906}
907
908func TestDeviceHandler_AdoptDevice(t *testing.T) {
909 dh1 := newMockDeviceHandler()
910 dh2 := negativeDeviceHandler()
911 type args struct {
912 device *voltha.Device
913 }
914 tests := []struct {
915 name string
916 devicehandler *DeviceHandler
917 args args
918 }{
919 // TODO: Add test cases.
920 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530921 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530922 }
923 for _, tt := range tests {
924 t.Run(tt.name, func(t *testing.T) {
925 //dh.doStateInit()
926 // context.
927 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530928 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
929 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400930 if err := tt.devicehandler.postInit(ctx); err != nil {
931 t.Error(err)
932 }
kdarapu891693b2019-09-16 12:33:49 +0530933 })
934 }
935}
936
937func TestDeviceHandler_activateONU(t *testing.T) {
938 dh := newMockDeviceHandler()
939 dh1 := negativeDeviceHandler()
940 type args struct {
941 intfID uint32
942 onuID int64
943 serialNum *oop.SerialNumber
944 serialNumber string
945 }
946 tests := []struct {
947 name string
948 devicehandler *DeviceHandler
949 args args
950 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700951 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
952 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
953 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
954 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530955 }
956 for _, tt := range tests {
957 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530958 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
959 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400960 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
961 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530962 })
963 }
964}
965
966func TestDeviceHandler_start(t *testing.T) {
967 dh := newMockDeviceHandler()
968 dh1 := negativeDeviceHandler()
969 dh.start(context.Background())
970 dh.stop(context.Background())
971
972 dh1.start(context.Background())
973 dh1.stop(context.Background())
974
975}
976
977func TestDeviceHandler_PacketOut(t *testing.T) {
978 dh1 := newMockDeviceHandler()
979 dh2 := negativeDeviceHandler()
980 acts := []*ofp.OfpAction{
981 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
982 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
983 fu.Output(1),
984 }
985 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
986 type args struct {
987 egressPortNo int
988 packet *of.OfpPacketOut
989 }
990 tests := []struct {
991 name string
992 devicehandler *DeviceHandler
993 args args
994 wantErr bool
995 }{
996 // TODO: Add test cases.
997 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
998 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
999 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -07001000 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001001 {"PacketOut-4", dh1, args{egressPortNo: 16777217, packet: pktout}, false},
1002 {"PacketOut-5", dh2, args{egressPortNo: 16777216, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +05301003 }
1004 for _, tt := range tests {
1005 t.Run(tt.name, func(t *testing.T) {
1006 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +05301007 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1008 defer cancel()
khenaidoo106c61a2021-08-11 18:05:46 -04001009 if err := dh.PacketOut(ctx, uint32(tt.args.egressPortNo), tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301010 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
1011 }
1012 })
1013 }
1014}
1015
1016//
1017func TestDeviceHandler_doStateUp(t *testing.T) {
1018 dh1 := newMockDeviceHandler()
1019 dh2 := newMockDeviceHandler()
1020
Thomas Lee S985938d2020-05-04 11:40:41 +05301021 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301022 dh3 := negativeDeviceHandler()
1023
1024 tests := []struct {
1025 name string
1026 devicehandler *DeviceHandler
1027 wantErr bool
1028 }{
1029 {"dostateup-1", dh1, false},
1030 {"dostateup-2", dh2, false},
1031 {"dostateup-3", dh3, true},
1032 }
1033 for _, tt := range tests {
1034 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301035 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1036 defer cancel()
1037 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301038 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1039 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301040 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301041 })
1042 }
1043}
1044func TestDeviceHandler_doStateDown(t *testing.T) {
1045 dh1 := newMockDeviceHandler()
1046 dh2 := negativeDeviceHandler()
1047 dh3 := newMockDeviceHandler()
1048 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1049 tests := []struct {
1050 name string
1051 devicehandler *DeviceHandler
1052 wantErr bool
1053 }{
1054 {"dostatedown-1", dh1, false},
1055 {"dostatedown-2", dh2, true},
1056 {"dostatedown-2", dh3, true},
1057 }
1058 for _, tt := range tests {
1059 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301060 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1061 defer cancel()
1062 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301063 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001064 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301065 }
1066 })
1067 }
1068}
1069
1070func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1071 dh1 := newMockDeviceHandler()
1072 dh2 := negativeDeviceHandler()
1073 type args struct {
1074 device *voltha.Device
1075 }
1076 tests := []struct {
1077 name string
1078 devicehandler *DeviceHandler
1079 args args
1080 wantErr bool
1081 }{
1082 // TODO: Add test cases.
1083 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1084 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1085 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1086 }
1087 for _, tt := range tests {
1088 t.Run(tt.name, func(t *testing.T) {
1089 dh := tt.devicehandler
1090 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1091 if (err != nil) != tt.wantErr {
1092 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1093 return
1094 }
1095 })
1096 }
1097}
1098
kdarapu891693b2019-09-16 12:33:49 +05301099func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1100
1101 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301102 dh1.discOnus = sync.Map{}
1103 dh1.discOnus.Store("onu1", true)
1104 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301105 dh1.discOnus.Store("onu3", true)
1106 dh1.discOnus.Store("onu4", true)
1107 dh1.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -04001108 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true, "mock_endpoint"))
1109 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true, "mock_endpoint"))
kdarapu891693b2019-09-16 12:33:49 +05301110 dh2 := negativeDeviceHandler()
1111 type args struct {
1112 onuDiscInd *oop.OnuDiscIndication
1113 sn string
1114 }
1115 tests := []struct {
1116 name string
1117 devicehandler *DeviceHandler
1118 args args
1119 }{
1120 // TODO: Add test cases.
1121 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1122 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1123 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1124 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1125 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1126 {"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 +05301127 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1128 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1129 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301130 }
1131 for _, tt := range tests {
1132 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301133 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1134 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001135 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001136 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301137 })
1138 }
1139}
1140
1141func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1142 dh1 := newMockDeviceHandler()
1143 dh2 := negativeDeviceHandler()
1144 tests := []struct {
1145 name string
1146 devicehandler *DeviceHandler
1147
1148 wantErr bool
1149 }{
1150 // TODO: Add test cases.
1151 {"populateDeviceInfo-1", dh1, false},
1152 {"populateDeviceInfo-2", dh1, true},
1153 {"populateDeviceInfo-3", dh1, true},
1154 {"populateDeviceInfo-4", dh1, true},
1155 {"populateDeviceInfo-5", dh2, true},
1156 }
1157 for _, tt := range tests {
1158 t.Run(tt.name, func(t *testing.T) {
1159
Neha Sharma96b7bf22020-06-15 10:37:32 +00001160 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301161 if (err != nil) != tt.wantErr {
1162 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1163 return
1164 }
1165
1166 })
1167 }
1168}
1169
1170func TestDeviceHandler_readIndications(t *testing.T) {
1171 dh1 := newMockDeviceHandler()
1172 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301173 dh3 := newMockDeviceHandler()
1174 dh3.device.AdminState = voltha.AdminState_DISABLED
1175 dh4 := negativeDeviceHandler()
1176 tests := []struct {
1177 name string
1178 devicehandler *DeviceHandler
1179 }{
1180 // TODO: Add test cases.
1181 {"readIndications-1", dh1},
1182 {"readIndications-2", dh2},
1183 {"readIndications-3", dh2},
1184 {"readIndications-4", dh2},
1185 {"readIndications-5", dh2},
1186 {"readIndications-6", dh3},
1187 {"readIndications-7", dh3},
1188 {"readIndications-8", dh4},
1189 }
1190 for _, tt := range tests {
1191 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301192 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1193 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001194 _ = tt.devicehandler.readIndications(ctx)
1195 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301196 })
1197 }
1198}
Naga Manjunath7615e552019-10-11 22:35:47 +05301199
1200func Test_startCollector(t *testing.T) {
1201 type args struct {
1202 dh *DeviceHandler
1203 }
1204 dh := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001205 mcs := newMockCoreService()
1206 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301207 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
Mahir Gunyel0c009fc2021-10-22 17:47:16 -07001208 {PortNo: 16777216, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1209 {PortNo: 16777218, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301210 }
khenaidoo106c61a2021-08-11 18:05:46 -04001211 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301212 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301213 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1214 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301215 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1216 dh.portStats.Device = dh
1217 for i := 0; i < 16; i++ {
1218 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1219 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301220 dh1 := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001221 mcs = newMockCoreService()
1222 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{}
1223 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301224 tests := []struct {
1225 name string
1226 args args
1227 }{
1228 // TODO: Add test cases.
1229 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301230 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301231 }
1232 for _, tt := range tests {
1233 t.Run(tt.name, func(t *testing.T) {
1234 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301235 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301236 tt.args.dh.stopCollector <- true
1237 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001238 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301239 })
1240 }
1241}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001242
1243func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1244
1245 // olt disconnect (not reboot)
1246 dh1 := newMockDeviceHandler()
1247 dh1.adapterPreviouslyConnected = false
1248 dh1.agentPreviouslyConnected = true
1249
1250 // adapter restart
1251 dh2 := newMockDeviceHandler()
1252 dh2.Client = &mocks.MockOpenoltClient{}
1253 dh2.adapterPreviouslyConnected = true
1254 dh2.agentPreviouslyConnected = true
1255
1256 // first connection or olt restart
1257 dh3 := newMockDeviceHandler()
1258 dh3.Client = &mocks.MockOpenoltClient{}
1259 dh3.adapterPreviouslyConnected = false
1260 dh3.agentPreviouslyConnected = false
1261
1262 // olt and adapter restart at the same time (first case)
1263 dh4 := newMockDeviceHandler()
1264 dh4.Client = &mocks.MockOpenoltClient{}
1265 dh4.adapterPreviouslyConnected = true
1266 dh4.agentPreviouslyConnected = false
1267
1268 // adapter restart and olt disconnect at the same time
1269 dh5 := newMockDeviceHandler()
1270 dh5.Client = &mocks.MockOpenoltClient{}
1271 dh5.adapterPreviouslyConnected = true
1272 dh5.agentPreviouslyConnected = true
1273
1274 tests := []struct {
1275 name string
1276 devicehandler *DeviceHandler
1277 expectedRestart bool
1278 wantErr bool
1279 }{
1280 {"dostateup-1", dh1, true, false},
1281 {"dostateup-2", dh2, false, false},
1282 {"dostateup-3", dh3, false, false},
1283 {"dostateup-4", dh4, true, false},
1284 {"dostateup-5", dh5, false, false},
1285 }
1286 for _, tt := range tests {
1287 t.Run(tt.name, func(t *testing.T) {
1288 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1289 defer cancel()
1290 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1291 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1292 }
1293 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1294 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1295 if tt.expectedRestart != isRestarted {
1296 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1297 }
1298 })
1299 }
1300}