blob: d69be1a93d7ab893f564917f2b4de2f523e3e165 [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"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070022 conf "github.com/opencord/voltha-lib-go/v5/pkg/config"
kdarapu381c6902019-07-31 18:23:16 +053023 "net"
kdarapu891693b2019-09-16 12:33:49 +053024 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053025 "sync"
kdarapu381c6902019-07-31 18:23:16 +053026 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053027 "time"
28
kdarapu891693b2019-09-16 12:33:49 +053029 "github.com/golang/protobuf/ptypes"
30 "github.com/golang/protobuf/ptypes/any"
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -070031 "github.com/opencord/voltha-lib-go/v5/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v5/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v5/pkg/log"
34 "github.com/opencord/voltha-lib-go/v5/pkg/pmmetrics"
35 ponrmgr "github.com/opencord/voltha-lib-go/v5/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"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070040 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
41 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
42 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
43 oop "github.com/opencord/voltha-protos/v4/go/openolt"
44 "github.com/opencord/voltha-protos/v4/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
kdarapu891693b2019-09-16 12:33:49 +053059func newMockCoreProxy() *mocks.MockCoreProxy {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040060 mcp := mocks.MockCoreProxy{
61 Devices: make(map[string]*voltha.Device),
62 DevicePorts: make(map[string][]*voltha.Port),
63 }
Naga Manjunath7615e552019-10-11 22:35:47 +053064 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053065 mcp.Devices["olt"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053066 Id: "olt",
67 Root: true,
68 ParentId: "logical_device",
69 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +053070 ProxyAddress: &voltha.Device_ProxyAddress{
71 DeviceId: "olt",
72 DeviceType: "onu",
73 ChannelId: 1,
74 ChannelGroupId: 1,
75 },
76 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053077 PmConfigs: &voltha.PmConfigs{
78 DefaultFreq: 10,
79 Id: "olt",
80 FreqOverride: false,
81 Grouped: false,
82 Metrics: pm,
83 },
kdarapu891693b2019-09-16 12:33:49 +053084 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040085 mcp.DevicePorts["olt"] = []*voltha.Port{
86 {PortNo: 1, Label: "pon"},
87 {PortNo: 2, Label: "nni"},
88 }
kdarapu891693b2019-09-16 12:33:49 +053089
Kent Hagermanf1db18b2020-07-08 13:38:15 -040090 mcp.Devices["onu1"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053091 Id: "1",
92 Root: false,
93 ParentId: "olt",
94 ParentPortNo: 1,
95
kdarapu891693b2019-09-16 12:33:49 +053096 OperStatus: 4,
97 ProxyAddress: &voltha.Device_ProxyAddress{
98 OnuId: 1,
99 ChannelId: 1,
100 ChannelGroupId: 1,
101 },
102 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530103 PmConfigs: &voltha.PmConfigs{
104 DefaultFreq: 10,
105 Id: "olt",
106 FreqOverride: false,
107 Grouped: false,
108 Metrics: pm,
109 },
kdarapu891693b2019-09-16 12:33:49 +0530110 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400111 mcp.DevicePorts["onu1"] = []*voltha.Port{
112 {PortNo: 1, Label: "pon"},
113 {PortNo: 2, Label: "uni"},
114 }
115
kdarapu891693b2019-09-16 12:33:49 +0530116 mcp.Devices["onu2"] = &voltha.Device{
117 Id: "2",
118 Root: false,
119 ParentId: "olt",
120 OperStatus: 2,
kdarapu891693b2019-09-16 12:33:49 +0530121
122 ParentPortNo: 1,
123
124 ProxyAddress: &voltha.Device_ProxyAddress{
125 OnuId: 2,
126 ChannelId: 1,
127 ChannelGroupId: 1,
128 },
129 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530130 PmConfigs: &voltha.PmConfigs{
131 DefaultFreq: 10,
132 Id: "olt",
133 FreqOverride: false,
134 Grouped: false,
135 Metrics: pm,
136 },
kdarapu891693b2019-09-16 12:33:49 +0530137 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400138 mcp.DevicePorts["onu2"] = []*voltha.Port{
139 {PortNo: 1, Label: "pon"},
140 {PortNo: 2, Label: "uni"},
141 }
kdarapu891693b2019-09-16 12:33:49 +0530142 return &mcp
143}
144func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530145 device := &voltha.Device{
146 Id: "olt",
147 Root: true,
148 ParentId: "logical_device",
kdarapu891693b2019-09-16 12:33:49 +0530149 ProxyAddress: &voltha.Device_ProxyAddress{
150 DeviceId: "olt",
151 DeviceType: "onu",
152 ChannelId: 1,
153 ChannelGroupId: 1,
154 },
155 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530156 }
kdarapu891693b2019-09-16 12:33:49 +0530157 cp := newMockCoreProxy()
158 ap := &mocks.MockAdapterProxy{}
159 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800160 cm := &conf.ConfigManager{}
kesavand494c2082020-08-31 11:16:12 +0530161 cfg := &config.AdapterFlags{OmccEncryption: true}
162 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep, config: cfg}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800163 dh := NewDeviceHandler(cp, ap, ep, device, openOLT, cm)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700164 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700165 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 -0700166 Technology: "xgs-pon",
167 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
168 }}
169
Girish Gowdra9602eb42020-09-09 15:50:39 -0700170 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700171 dh.deviceInfo = deviceInf
172 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
173 var i uint32
174 for i = 0; i < deviceInf.PonPorts; i++ {
175 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
176 KVStore: &db.Backend{
177 Client: &mocks.MockKVClient{},
178 }}
179 dh.resourceMgr[i].InitLocalCache()
180 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700181
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530182 ranges := make(map[string]interface{})
183 sharedIdxByType := make(map[string]string)
184 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
185 sharedIdxByType["ONU_ID"] = "ONU_ID"
186 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
187 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
188 ranges["ONU_ID"] = uint32(0)
189 ranges["GEMPORT_ID"] = uint32(0)
190 ranges["ALLOC_ID"] = uint32(0)
191 ranges["FLOW_ID"] = uint32(0)
192 ranges["onu_id_shared"] = uint32(0)
193 ranges["alloc_id_shared"] = uint32(0)
194 ranges["gemport_id_shared"] = uint32(0)
195 ranges["flow_id_shared"] = uint32(0)
196
Matteo Scandolo84585372021-03-18 14:21:22 -0700197 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700198 ponmgr.DeviceID = "onu-1"
199 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
200 ponmgr.KVStore = &db.Backend{
201 Client: &mocks.MockKVClient{},
202 }
203 ponmgr.PonResourceRanges = ranges
204 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700205 ponmgr.Technology = "XGS-PON"
206 for i = 0; i < deviceInf.PonPorts; i++ {
207 dh.resourceMgr[i].PonRsrMgr = ponmgr
208 }
209
210 /*
211 tpMgr, err := tp.NewTechProfile(ctx, ponmgr, "etcd", "127.0.0.1", "/")
212 if err != nil {
213 logger.Fatal(ctx, err.Error())
214 }
215 */
216 tpMgr := &mocks.MockTechProfile{TpID: 64}
Matteo Scandolo84585372021-03-18 14:21:22 -0700217 ponmgr.TechProfileMgr = tpMgr
218
npujarec5762e2020-01-01 14:08:48 +0530219 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
220 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700221 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700222 dh.totalPonPorts = NumPonPorts
223 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700224 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra9602eb42020-09-09 15:50:39 -0700225 // Instantiate flow manager
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700226 if dh.flowMgr[i] = NewFlowManager(ctx, dh, dh.resourceMgr[i], dh.groupMgr, uint32(i)); dh.flowMgr[i] == nil {
Girish Gowdra9602eb42020-09-09 15:50:39 -0700227 return nil
228 }
229 }
kdarapu891693b2019-09-16 12:33:49 +0530230 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500231 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530232 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530233 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000234
235 var pmNames = []string{
236 "rx_bytes",
237 "rx_packets",
238 "rx_mcast_packets",
239 "rx_bcast_packets",
240 "tx_bytes",
241 "tx_packets",
242 "tx_mcast_packets",
243 "tx_bcast_packets",
244 }
245
246 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530247 return dh
kdarapu381c6902019-07-31 18:23:16 +0530248}
249
kdarapu891693b2019-09-16 12:33:49 +0530250func negativeDeviceHandler() *DeviceHandler {
251 dh := newMockDeviceHandler()
252 device := dh.device
253 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530254 return dh
255}
kdarapu381c6902019-07-31 18:23:16 +0530256func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000257 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530258 type args struct {
259 host string
260 }
261 tests := []struct {
262 name string
263 args args
264 want string
265 wantErr bool
266 }{
kdarapu891693b2019-09-16 12:33:49 +0530267 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
268 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
269 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
270 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530271 }
272 for _, tt := range tests {
273 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000274 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530275 if (err != nil) != tt.wantErr {
276 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
277 return
278 }
279 if got != tt.want {
280 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
281 }
282 })
283 }
284}
285func Test_macifyIP(t *testing.T) {
286 type args struct {
287 ip net.IP
288 }
289 tests := []struct {
290 name string
291 args args
292 want string
293 }{{
kdarapu891693b2019-09-16 12:33:49 +0530294 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530295 args{ip: net.ParseIP("10.10.10.10")},
296 "00:00:0a:0a:0a:0a",
297 },
298 {
kdarapu891693b2019-09-16 12:33:49 +0530299 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530300 args{ip: net.ParseIP("127.0.0.1")},
301 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530302 },
303 {
304 "macifyIP-3",
305 args{ip: net.ParseIP("127.0.0.1/24")},
306 "",
307 },
308 }
kdarapu381c6902019-07-31 18:23:16 +0530309 for _, tt := range tests {
310 t.Run(tt.name, func(t *testing.T) {
311 if got := macifyIP(tt.args.ip); got != tt.want {
312 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
313 }
314 })
315 }
316}
317
David K. Bainbridge794735f2020-02-11 21:01:37 -0800318func sparseCompare(keys []string, spec, target interface{}) bool {
319 if spec == target {
320 return true
321 }
322 if spec == nil || target == nil {
323 return false
324 }
325 typeSpec := reflect.TypeOf(spec)
326 typeTarget := reflect.TypeOf(target)
327 if typeSpec != typeTarget {
328 return false
329 }
330
331 vSpec := reflect.ValueOf(spec)
332 vTarget := reflect.ValueOf(target)
333 if vSpec.Kind() == reflect.Ptr {
334 vSpec = vSpec.Elem()
335 vTarget = vTarget.Elem()
336 }
337
338 for _, key := range keys {
339 fSpec := vSpec.FieldByName(key)
340 fTarget := vTarget.FieldByName(key)
341 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
342 return false
343 }
344 }
345 return true
346}
347
kdarapu381c6902019-07-31 18:23:16 +0530348func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000349 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530350 dh1 := newMockDeviceHandler()
351 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530352 type args struct {
353 parentPort uint32
354 onuID uint32
355 }
356 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530357 name string
358 devicehandler *DeviceHandler
359 args args
360 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800361 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530362 }{
kdarapu891693b2019-09-16 12:33:49 +0530363 {"GetChildDevice-1", dh1,
364 args{parentPort: 1,
365 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800366 &voltha.Device{
367 Id: "1",
368 ParentId: "olt",
369 ParentPortNo: 1,
370 },
371 nil,
kdarapu891693b2019-09-16 12:33:49 +0530372 },
373 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530374 args{parentPort: 1,
375 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800376 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530377 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530378 },
379 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800380
381 /*
382 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
383 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>)
384 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
385 */
kdarapu381c6902019-07-31 18:23:16 +0530386 for _, tt := range tests {
387 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000388 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800389 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
390 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
391 tt.want, tt.errType, got, reflect.TypeOf(err))
392 return
393 }
kdarapu381c6902019-07-31 18:23:16 +0530394 t.Log("onu device id", got)
395 })
396 }
397}
kdarapu891693b2019-09-16 12:33:49 +0530398
399func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530400 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530401 type args struct {
402 portNum uint32
403 portType voltha.Port_PortType
404 }
405 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800406 name string
407 args args
408 want string
409 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530410 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800411 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
412 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
413 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
414 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
415 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
416 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
417 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530418 }
419 for _, tt := range tests {
420 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800421 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
422 if reflect.TypeOf(err) != tt.errType || got != tt.want {
423 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
424 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530425 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800426
kdarapu891693b2019-09-16 12:33:49 +0530427 })
428 }
429}
430
431func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000432 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530433 dh := newMockDeviceHandler()
434 proxyAddr := dh.device.ProxyAddress
435 body := &ic.InterAdapterOmciMessage{
436 Message: []byte("asdfasdfasdfasdfas"),
437 ProxyAddress: proxyAddr,
438 }
439 body2 := &ic.InterAdapterOmciMessage{
440 Message: []byte("asdfasdfasdfasdfas"),
441 //ProxyAddress: &voltha.Device_ProxyAddress{},
442 }
443 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
444 var marshalledData *any.Any
445 var err error
446
447 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700448 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530449 }
450
451 var marshalledData1 *any.Any
452
453 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700454 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530455 }
456 var marshalledData2 *any.Any
457
458 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700459 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530460 }
461 type args struct {
462 msg *ic.InterAdapterMessage
463 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530464 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530465 tests := []struct {
466 name string
467 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800468 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530469 }{
470 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
471 Header: &ic.InterAdapterHeader{
472 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800473 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530474 },
475 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800476 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530477 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
478 Header: &ic.InterAdapterHeader{
479 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800480 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530481 },
482 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800483 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530484 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
485 Header: &ic.InterAdapterHeader{
486 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800487 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530488 },
489 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530490 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530491 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
492 Header: &ic.InterAdapterHeader{
493 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800494 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530495 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800496 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530497 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
498 Header: &ic.InterAdapterHeader{
499 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800500 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530501 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800502 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530503 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
504 Header: &ic.InterAdapterHeader{
505 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800506 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530507 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800508 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530509 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
510 Header: &ic.InterAdapterHeader{
511 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800512 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530513 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800514 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530515 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
516 Header: &ic.InterAdapterHeader{
517 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800518 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530519 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800520 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530521 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
522 Header: &ic.InterAdapterHeader{
523 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800524 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530525 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800526 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530527 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
528 Header: &ic.InterAdapterHeader{
529 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800530 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530531 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800532 }}, invalid},
533 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
534 Header: &ic.InterAdapterHeader{
535 Id: "012345",
536 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
537 }, Body: marshalledData2,
538 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530539 }
540 for _, tt := range tests {
541 t.Run(tt.name, func(t *testing.T) {
542
Neha Sharma96b7bf22020-06-15 10:37:32 +0000543 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530544 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
545 }
546 })
547 }
548}
549
550func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000551 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530552 dh1 := newMockDeviceHandler()
553 dh2 := negativeDeviceHandler()
554 device1 := &voltha.Device{
555 Id: "onu1",
556 Root: false,
557 ParentId: "logical_device",
558 ProxyAddress: &voltha.Device_ProxyAddress{
559 DeviceId: "onu1",
560 DeviceType: "onu",
561 ChannelId: 1,
562 ChannelGroupId: 1,
563 },
564 ConnectStatus: 1,
565 }
566 device2 := device1
567 device2.ConnectStatus = 2
568 iaomciMsg1 := &ic.InterAdapterOmciMessage{
569 ProxyAddress: &voltha.Device_ProxyAddress{
570 DeviceId: "onu2",
571 DeviceType: "onu",
572 ChannelId: 1,
573 ChannelGroupId: 1,
574 //OnuId: 2,
575 },
576 ConnectStatus: 1,
577 }
578 iaomciMsg2 := &ic.InterAdapterOmciMessage{
579 ProxyAddress: &voltha.Device_ProxyAddress{
580 DeviceId: "onu3",
581 DeviceType: "onu",
582 ChannelId: 1,
583 ChannelGroupId: 1,
584 },
585 ConnectStatus: 1,
586 }
587 type args struct {
588 onuDevice *voltha.Device
589 omciMsg *ic.InterAdapterOmciMessage
590 }
591 tests := []struct {
592 name string
593 devicehandler *DeviceHandler
594 args args
595 }{
596 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
597 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
598 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
599 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
600 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
601 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
602 }
603 for _, tt := range tests {
604 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400605 _ = tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
606 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530607 })
608 }
609}
610
611func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
612 dh1 := newMockDeviceHandler()
613 dh2 := negativeDeviceHandler()
614
615 type args struct {
616 logicalPort uint32
617 packetPayload []byte
618 }
619 tests := []struct {
620 name string
621 devicehandler *DeviceHandler
622 args args
623 }{
624 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
625 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
626 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
627 }
628 for _, tt := range tests {
629 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400630 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
631 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530632 })
633 }
634}
635
636func TestDeviceHandler_DisableDevice(t *testing.T) {
637 dh1 := newMockDeviceHandler()
638 dh2 := negativeDeviceHandler()
639 type args struct {
640 device *voltha.Device
641 }
642 tests := []struct {
643 name string
644 devicehandler *DeviceHandler
645 args args
646 wantErr bool
647 }{
648 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400649 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530650 }
651 for _, tt := range tests {
652 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000653 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530654 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
655 }
656 })
657 }
658}
659
660func TestDeviceHandler_ReenableDevice(t *testing.T) {
661 dh1 := newMockDeviceHandler()
662 dh2 := negativeDeviceHandler()
663 type args struct {
664 device *voltha.Device
665 }
666 tests := []struct {
667 name string
668 devicehandler *DeviceHandler
669 args args
670 wantErr bool
671 }{
672 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
673 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
674 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
675 }
676 for _, tt := range tests {
677 t.Run(tt.name, func(t *testing.T) {
678 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000679 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530680 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
681 }
682 })
683 }
684}
685
686func TestDeviceHandler_RebootDevice(t *testing.T) {
687 dh1 := newMockDeviceHandler()
688 dh2 := newMockDeviceHandler()
689 type args struct {
690 device *voltha.Device
691 }
692 tests := []struct {
693 name string
694 devicehandler *DeviceHandler
695 args args
696 wantErr bool
697 }{
698 // TODO: Add test cases.
699 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
700 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
701 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
702 }
703 for _, tt := range tests {
704 t.Run(tt.name, func(t *testing.T) {
705
Neha Sharma96b7bf22020-06-15 10:37:32 +0000706 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530707 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
708 }
709 })
710 }
711}
712
713func TestDeviceHandler_handleIndication(t *testing.T) {
714 dh1 := newMockDeviceHandler()
715 dh2 := negativeDeviceHandler()
716 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530717 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530718 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
719 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530720
kdarapu891693b2019-09-16 12:33:49 +0530721 type args struct {
722 indication *oop.Indication
723 }
724 tests := []struct {
725 name string
726 deviceHandler *DeviceHandler
727 args args
728 }{
729 // TODO: Add test cases.
730 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
731 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
732 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
733 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
734 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
735 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
736 {"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")}}}}}},
737 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
738 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
739 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
740 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
741 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
742 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
743 {"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}}}}},
744 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
745 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
746 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
747 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
748
749 // Negative testcases
750 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
751 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
752 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
753 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
754 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
755 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
756 {"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")}}}}}},
757 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
758 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
759 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
760 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
761 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
762 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
763 {"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}}}}},
764 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
765 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
766 //
767 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
768 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
769 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
770 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
771 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
772 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
773 }
774 for _, tt := range tests {
775 t.Run(tt.name, func(t *testing.T) {
776 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530777 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
778 defer cancel()
779 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530780 })
781 }
782}
783
784func TestDeviceHandler_addPort(t *testing.T) {
785 dh1 := newMockDeviceHandler()
786 dh2 := negativeDeviceHandler()
787 type args struct {
788 intfID uint32
789 portType voltha.Port_PortType
790 state string
791 }
792 tests := []struct {
793 name string
794 devicehandler *DeviceHandler
795 args args
796 }{
797 // State up
798 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
799 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
800 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
801 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
802 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
803 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
804 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
805 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
806 // state discovery
807 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
808 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
809 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
810 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
811 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
812 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
813 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
814 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
815
816 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
817 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
818 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
819 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
820 }
821 for _, tt := range tests {
822 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400823 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
824 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530825 })
826 }
827}
828
829func Test_macAddressToUint32Array(t *testing.T) {
830 type args struct {
831 mac string
832 }
833 tests := []struct {
834 name string
835 args args
836 want []uint32
837 }{
838 // TODO: Add test cases.
839 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
840 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
841 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
842 }
843 for _, tt := range tests {
844 t.Run(tt.name, func(t *testing.T) {
845 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
846 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
847 }
848 })
849 }
850}
851
852func TestDeviceHandler_handleOltIndication(t *testing.T) {
853
854 type args struct {
855 oltIndication *oop.OltIndication
856 }
857 tests := []struct {
858 name string
859 args args
860 }{
861 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
862 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
863 }
864 for _, tt := range tests {
865 t.Run(tt.name, func(t *testing.T) {
866 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530867 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
868 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400869 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
870 t.Error(err)
871 }
kdarapu891693b2019-09-16 12:33:49 +0530872 })
873 }
874}
875
876func TestDeviceHandler_AdoptDevice(t *testing.T) {
877 dh1 := newMockDeviceHandler()
878 dh2 := negativeDeviceHandler()
879 type args struct {
880 device *voltha.Device
881 }
882 tests := []struct {
883 name string
884 devicehandler *DeviceHandler
885 args args
886 }{
887 // TODO: Add test cases.
888 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530889 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530890 }
891 for _, tt := range tests {
892 t.Run(tt.name, func(t *testing.T) {
893 //dh.doStateInit()
894 // context.
895 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530896 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
897 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400898 if err := tt.devicehandler.postInit(ctx); err != nil {
899 t.Error(err)
900 }
kdarapu891693b2019-09-16 12:33:49 +0530901 })
902 }
903}
904
905func TestDeviceHandler_activateONU(t *testing.T) {
906 dh := newMockDeviceHandler()
907 dh1 := negativeDeviceHandler()
908 type args struct {
909 intfID uint32
910 onuID int64
911 serialNum *oop.SerialNumber
912 serialNumber string
913 }
914 tests := []struct {
915 name string
916 devicehandler *DeviceHandler
917 args args
918 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700919 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
920 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
921 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
922 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530923 }
924 for _, tt := range tests {
925 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530926 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
927 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400928 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
929 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530930 })
931 }
932}
933
934func TestDeviceHandler_start(t *testing.T) {
935 dh := newMockDeviceHandler()
936 dh1 := negativeDeviceHandler()
937 dh.start(context.Background())
938 dh.stop(context.Background())
939
940 dh1.start(context.Background())
941 dh1.stop(context.Background())
942
943}
944
945func TestDeviceHandler_PacketOut(t *testing.T) {
946 dh1 := newMockDeviceHandler()
947 dh2 := negativeDeviceHandler()
948 acts := []*ofp.OfpAction{
949 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
950 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
951 fu.Output(1),
952 }
953 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
954 type args struct {
955 egressPortNo int
956 packet *of.OfpPacketOut
957 }
958 tests := []struct {
959 name string
960 devicehandler *DeviceHandler
961 args args
962 wantErr bool
963 }{
964 // TODO: Add test cases.
965 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
966 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
967 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700968 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
969 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
970 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530971 }
972 for _, tt := range tests {
973 t.Run(tt.name, func(t *testing.T) {
974 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530975 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
976 defer cancel()
977 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530978 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
979 }
980 })
981 }
982}
983
984//
985func TestDeviceHandler_doStateUp(t *testing.T) {
986 dh1 := newMockDeviceHandler()
987 dh2 := newMockDeviceHandler()
988
Thomas Lee S985938d2020-05-04 11:40:41 +0530989 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530990 dh3 := negativeDeviceHandler()
991
992 tests := []struct {
993 name string
994 devicehandler *DeviceHandler
995 wantErr bool
996 }{
997 {"dostateup-1", dh1, false},
998 {"dostateup-2", dh2, false},
999 {"dostateup-3", dh3, true},
1000 }
1001 for _, tt := range tests {
1002 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301003 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1004 defer cancel()
1005 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301006 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1007 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301008 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301009 })
1010 }
1011}
1012func TestDeviceHandler_doStateDown(t *testing.T) {
1013 dh1 := newMockDeviceHandler()
1014 dh2 := negativeDeviceHandler()
1015 dh3 := newMockDeviceHandler()
1016 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1017 tests := []struct {
1018 name string
1019 devicehandler *DeviceHandler
1020 wantErr bool
1021 }{
1022 {"dostatedown-1", dh1, false},
1023 {"dostatedown-2", dh2, true},
1024 {"dostatedown-2", dh3, true},
1025 }
1026 for _, tt := range tests {
1027 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301028 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1029 defer cancel()
1030 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301031 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001032 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301033 }
1034 })
1035 }
1036}
1037
1038func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1039 dh1 := newMockDeviceHandler()
1040 dh2 := negativeDeviceHandler()
1041 type args struct {
1042 device *voltha.Device
1043 }
1044 tests := []struct {
1045 name string
1046 devicehandler *DeviceHandler
1047 args args
1048 wantErr bool
1049 }{
1050 // TODO: Add test cases.
1051 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1052 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1053 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1054 }
1055 for _, tt := range tests {
1056 t.Run(tt.name, func(t *testing.T) {
1057 dh := tt.devicehandler
1058 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1059 if (err != nil) != tt.wantErr {
1060 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1061 return
1062 }
1063 })
1064 }
1065}
1066
kdarapu891693b2019-09-16 12:33:49 +05301067func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1068
1069 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301070 dh1.discOnus = sync.Map{}
1071 dh1.discOnus.Store("onu1", true)
1072 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301073 dh1.discOnus.Store("onu3", true)
1074 dh1.discOnus.Store("onu4", true)
1075 dh1.onus = sync.Map{}
1076 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1077 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301078 dh2 := negativeDeviceHandler()
1079 type args struct {
1080 onuDiscInd *oop.OnuDiscIndication
1081 sn string
1082 }
1083 tests := []struct {
1084 name string
1085 devicehandler *DeviceHandler
1086 args args
1087 }{
1088 // TODO: Add test cases.
1089 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1090 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1091 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1092 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1093 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1094 {"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 +05301095 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1096 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1097 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301098 }
1099 for _, tt := range tests {
1100 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301101 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1102 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001103 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001104 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301105 })
1106 }
1107}
1108
1109func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1110 dh1 := newMockDeviceHandler()
1111 dh2 := negativeDeviceHandler()
1112 tests := []struct {
1113 name string
1114 devicehandler *DeviceHandler
1115
1116 wantErr bool
1117 }{
1118 // TODO: Add test cases.
1119 {"populateDeviceInfo-1", dh1, false},
1120 {"populateDeviceInfo-2", dh1, true},
1121 {"populateDeviceInfo-3", dh1, true},
1122 {"populateDeviceInfo-4", dh1, true},
1123 {"populateDeviceInfo-5", dh2, true},
1124 }
1125 for _, tt := range tests {
1126 t.Run(tt.name, func(t *testing.T) {
1127
Neha Sharma96b7bf22020-06-15 10:37:32 +00001128 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301129 if (err != nil) != tt.wantErr {
1130 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1131 return
1132 }
1133
1134 })
1135 }
1136}
1137
1138func TestDeviceHandler_readIndications(t *testing.T) {
1139 dh1 := newMockDeviceHandler()
1140 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301141 dh3 := newMockDeviceHandler()
1142 dh3.device.AdminState = voltha.AdminState_DISABLED
1143 dh4 := negativeDeviceHandler()
1144 tests := []struct {
1145 name string
1146 devicehandler *DeviceHandler
1147 }{
1148 // TODO: Add test cases.
1149 {"readIndications-1", dh1},
1150 {"readIndications-2", dh2},
1151 {"readIndications-3", dh2},
1152 {"readIndications-4", dh2},
1153 {"readIndications-5", dh2},
1154 {"readIndications-6", dh3},
1155 {"readIndications-7", dh3},
1156 {"readIndications-8", dh4},
1157 }
1158 for _, tt := range tests {
1159 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301160 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1161 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001162 _ = tt.devicehandler.readIndications(ctx)
1163 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301164 })
1165 }
1166}
Naga Manjunath7615e552019-10-11 22:35:47 +05301167
1168func Test_startCollector(t *testing.T) {
1169 type args struct {
1170 dh *DeviceHandler
1171 }
1172 dh := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001173 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301174 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1175 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1176 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1177 }
Naga Manjunath7615e552019-10-11 22:35:47 +05301178 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301179 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1180 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301181 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1182 dh.portStats.Device = dh
1183 for i := 0; i < 16; i++ {
1184 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1185 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301186 dh1 := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001187 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301188 tests := []struct {
1189 name string
1190 args args
1191 }{
1192 // TODO: Add test cases.
1193 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301194 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301195 }
1196 for _, tt := range tests {
1197 t.Run(tt.name, func(t *testing.T) {
1198 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301199 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301200 tt.args.dh.stopCollector <- true
1201 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001202 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301203 })
1204 }
1205}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001206
1207func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1208
1209 // olt disconnect (not reboot)
1210 dh1 := newMockDeviceHandler()
1211 dh1.adapterPreviouslyConnected = false
1212 dh1.agentPreviouslyConnected = true
1213
1214 // adapter restart
1215 dh2 := newMockDeviceHandler()
1216 dh2.Client = &mocks.MockOpenoltClient{}
1217 dh2.adapterPreviouslyConnected = true
1218 dh2.agentPreviouslyConnected = true
1219
1220 // first connection or olt restart
1221 dh3 := newMockDeviceHandler()
1222 dh3.Client = &mocks.MockOpenoltClient{}
1223 dh3.adapterPreviouslyConnected = false
1224 dh3.agentPreviouslyConnected = false
1225
1226 // olt and adapter restart at the same time (first case)
1227 dh4 := newMockDeviceHandler()
1228 dh4.Client = &mocks.MockOpenoltClient{}
1229 dh4.adapterPreviouslyConnected = true
1230 dh4.agentPreviouslyConnected = false
1231
1232 // adapter restart and olt disconnect at the same time
1233 dh5 := newMockDeviceHandler()
1234 dh5.Client = &mocks.MockOpenoltClient{}
1235 dh5.adapterPreviouslyConnected = true
1236 dh5.agentPreviouslyConnected = true
1237
1238 tests := []struct {
1239 name string
1240 devicehandler *DeviceHandler
1241 expectedRestart bool
1242 wantErr bool
1243 }{
1244 {"dostateup-1", dh1, true, false},
1245 {"dostateup-2", dh2, false, false},
1246 {"dostateup-3", dh3, false, false},
1247 {"dostateup-4", dh4, true, false},
1248 {"dostateup-5", dh5, false, false},
1249 }
1250 for _, tt := range tests {
1251 t.Run(tt.name, func(t *testing.T) {
1252 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1253 defer cancel()
1254 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1255 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1256 }
1257 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1258 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1259 if tt.expectedRestart != isRestarted {
1260 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1261 }
1262 })
1263 }
1264}