blob: 1cae8b6ba28a3600ad74a0096725ced9dc0bab26 [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"
Matteo Scandolodfa7a972020-11-06 13:03:40 -080022 conf "github.com/opencord/voltha-lib-go/v4/pkg/config"
Matteo Scandolo84585372021-03-18 14:21:22 -070023 tp "github.com/opencord/voltha-lib-go/v4/pkg/techprofile"
kdarapu381c6902019-07-31 18:23:16 +053024 "net"
kdarapu891693b2019-09-16 12:33:49 +053025 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053026 "sync"
kdarapu381c6902019-07-31 18:23:16 +053027 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053028 "time"
29
kdarapu891693b2019-09-16 12:33:49 +053030 "github.com/golang/protobuf/ptypes"
31 "github.com/golang/protobuf/ptypes/any"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070032 "github.com/opencord/voltha-lib-go/v4/pkg/db"
33 fu "github.com/opencord/voltha-lib-go/v4/pkg/flows"
34 "github.com/opencord/voltha-lib-go/v4/pkg/log"
35 "github.com/opencord/voltha-lib-go/v4/pkg/pmmetrics"
36 ponrmgr "github.com/opencord/voltha-lib-go/v4/pkg/ponresourcemanager"
kesavand494c2082020-08-31 11:16:12 +053037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080039 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
40 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070041 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
42 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
43 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
44 oop "github.com/opencord/voltha-protos/v4/go/openolt"
45 "github.com/opencord/voltha-protos/v4/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053046)
47
Girish Gowdra9602eb42020-09-09 15:50:39 -070048const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070049 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070050 OnuIDStart = 1
51 OnuIDEnd = 32
52 AllocIDStart = 1
53 AllocIDEnd = 10
54 GemIDStart = 1
55 GemIDEnd = 10
56 FlowIDStart = 1
57 FlowIDEnd = 10
58)
59
kdarapu891693b2019-09-16 12:33:49 +053060func newMockCoreProxy() *mocks.MockCoreProxy {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040061 mcp := mocks.MockCoreProxy{
62 Devices: make(map[string]*voltha.Device),
63 DevicePorts: make(map[string][]*voltha.Port),
64 }
Naga Manjunath7615e552019-10-11 22:35:47 +053065 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053066 mcp.Devices["olt"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053067 Id: "olt",
68 Root: true,
69 ParentId: "logical_device",
70 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +053071 ProxyAddress: &voltha.Device_ProxyAddress{
72 DeviceId: "olt",
73 DeviceType: "onu",
74 ChannelId: 1,
75 ChannelGroupId: 1,
76 },
77 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053078 PmConfigs: &voltha.PmConfigs{
79 DefaultFreq: 10,
80 Id: "olt",
81 FreqOverride: false,
82 Grouped: false,
83 Metrics: pm,
84 },
kdarapu891693b2019-09-16 12:33:49 +053085 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040086 mcp.DevicePorts["olt"] = []*voltha.Port{
87 {PortNo: 1, Label: "pon"},
88 {PortNo: 2, Label: "nni"},
89 }
kdarapu891693b2019-09-16 12:33:49 +053090
Kent Hagermanf1db18b2020-07-08 13:38:15 -040091 mcp.Devices["onu1"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053092 Id: "1",
93 Root: false,
94 ParentId: "olt",
95 ParentPortNo: 1,
96
kdarapu891693b2019-09-16 12:33:49 +053097 OperStatus: 4,
98 ProxyAddress: &voltha.Device_ProxyAddress{
99 OnuId: 1,
100 ChannelId: 1,
101 ChannelGroupId: 1,
102 },
103 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530104 PmConfigs: &voltha.PmConfigs{
105 DefaultFreq: 10,
106 Id: "olt",
107 FreqOverride: false,
108 Grouped: false,
109 Metrics: pm,
110 },
kdarapu891693b2019-09-16 12:33:49 +0530111 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400112 mcp.DevicePorts["onu1"] = []*voltha.Port{
113 {PortNo: 1, Label: "pon"},
114 {PortNo: 2, Label: "uni"},
115 }
116
kdarapu891693b2019-09-16 12:33:49 +0530117 mcp.Devices["onu2"] = &voltha.Device{
118 Id: "2",
119 Root: false,
120 ParentId: "olt",
121 OperStatus: 2,
kdarapu891693b2019-09-16 12:33:49 +0530122
123 ParentPortNo: 1,
124
125 ProxyAddress: &voltha.Device_ProxyAddress{
126 OnuId: 2,
127 ChannelId: 1,
128 ChannelGroupId: 1,
129 },
130 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530131 PmConfigs: &voltha.PmConfigs{
132 DefaultFreq: 10,
133 Id: "olt",
134 FreqOverride: false,
135 Grouped: false,
136 Metrics: pm,
137 },
kdarapu891693b2019-09-16 12:33:49 +0530138 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400139 mcp.DevicePorts["onu2"] = []*voltha.Port{
140 {PortNo: 1, Label: "pon"},
141 {PortNo: 2, Label: "uni"},
142 }
kdarapu891693b2019-09-16 12:33:49 +0530143 return &mcp
144}
145func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530146 device := &voltha.Device{
147 Id: "olt",
148 Root: true,
149 ParentId: "logical_device",
kdarapu891693b2019-09-16 12:33:49 +0530150 ProxyAddress: &voltha.Device_ProxyAddress{
151 DeviceId: "olt",
152 DeviceType: "onu",
153 ChannelId: 1,
154 ChannelGroupId: 1,
155 },
156 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530157 }
kdarapu891693b2019-09-16 12:33:49 +0530158 cp := newMockCoreProxy()
159 ap := &mocks.MockAdapterProxy{}
160 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800161 cm := &conf.ConfigManager{}
kesavand494c2082020-08-31 11:16:12 +0530162 cfg := &config.AdapterFlags{OmccEncryption: true}
163 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep, config: cfg}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800164 dh := NewDeviceHandler(cp, ap, ep, device, openOLT, cm)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700165 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700166 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 -0700167 Technology: "xgs-pon",
168 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
169 }}
170
Girish Gowdra9602eb42020-09-09 15:50:39 -0700171 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Thomas Lee S985938d2020-05-04 11:40:41 +0530172 rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530173 KVStore: &db.Backend{
174 Client: &mocks.MockKVClient{},
175 }}
Girish Gowdra38d533d2020-03-30 20:38:51 -0700176 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
177 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
178 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
179
180 dh.resourceMgr = &rsrMgr
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530181 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
182 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{}
198
199 ctx := context.TODO()
200 tpMgr, err := tp.NewTechProfile(ctx, ponmgr, "etcd", "127.0.0.1", "/")
201 if err != nil {
202 logger.Fatal(ctx, err.Error())
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530203 }
Matteo Scandolo84585372021-03-18 14:21:22 -0700204
205 ponmgr.DeviceID = "onu-1"
206 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
207 ponmgr.KVStore = &db.Backend{
208 Client: &mocks.MockKVClient{},
209 }
210 ponmgr.PonResourceRanges = ranges
211 ponmgr.SharedIdxByType = sharedIdxByType
212 ponmgr.TechProfileMgr = tpMgr
213
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700214 for i := 0; i < NumPonPorts; i++ {
215 dh.resourceMgr.ResourceMgrs[uint32(i)] = ponmgr
216 }
npujarec5762e2020-01-01 14:08:48 +0530217 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
218 defer cancel()
Girish Gowdra9602eb42020-09-09 15:50:39 -0700219 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr)
220 dh.totalPonPorts = NumPonPorts
221 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
222 for i := 0; i < int(dh.totalPonPorts); i++ {
223 // Instantiate flow manager
Girish Gowdraa09aeab2020-09-14 16:30:52 -0700224 if dh.flowMgr[i] = NewFlowManager(ctx, dh, dh.resourceMgr, dh.groupMgr, uint32(i)); dh.flowMgr[i] == nil {
Girish Gowdra9602eb42020-09-09 15:50:39 -0700225 return nil
226 }
227 }
kdarapu891693b2019-09-16 12:33:49 +0530228 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500229 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530230 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530231 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000232
233 var pmNames = []string{
234 "rx_bytes",
235 "rx_packets",
236 "rx_mcast_packets",
237 "rx_bcast_packets",
238 "tx_bytes",
239 "tx_packets",
240 "tx_mcast_packets",
241 "tx_bcast_packets",
242 }
243
244 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530245 return dh
kdarapu381c6902019-07-31 18:23:16 +0530246}
247
kdarapu891693b2019-09-16 12:33:49 +0530248func negativeDeviceHandler() *DeviceHandler {
249 dh := newMockDeviceHandler()
250 device := dh.device
251 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530252 return dh
253}
kdarapu381c6902019-07-31 18:23:16 +0530254func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000255 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530256 type args struct {
257 host string
258 }
259 tests := []struct {
260 name string
261 args args
262 want string
263 wantErr bool
264 }{
kdarapu891693b2019-09-16 12:33:49 +0530265 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
266 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
267 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
268 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530269 }
270 for _, tt := range tests {
271 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000272 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530273 if (err != nil) != tt.wantErr {
274 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
275 return
276 }
277 if got != tt.want {
278 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
279 }
280 })
281 }
282}
283func Test_macifyIP(t *testing.T) {
284 type args struct {
285 ip net.IP
286 }
287 tests := []struct {
288 name string
289 args args
290 want string
291 }{{
kdarapu891693b2019-09-16 12:33:49 +0530292 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530293 args{ip: net.ParseIP("10.10.10.10")},
294 "00:00:0a:0a:0a:0a",
295 },
296 {
kdarapu891693b2019-09-16 12:33:49 +0530297 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530298 args{ip: net.ParseIP("127.0.0.1")},
299 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530300 },
301 {
302 "macifyIP-3",
303 args{ip: net.ParseIP("127.0.0.1/24")},
304 "",
305 },
306 }
kdarapu381c6902019-07-31 18:23:16 +0530307 for _, tt := range tests {
308 t.Run(tt.name, func(t *testing.T) {
309 if got := macifyIP(tt.args.ip); got != tt.want {
310 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
311 }
312 })
313 }
314}
315
David K. Bainbridge794735f2020-02-11 21:01:37 -0800316func sparseCompare(keys []string, spec, target interface{}) bool {
317 if spec == target {
318 return true
319 }
320 if spec == nil || target == nil {
321 return false
322 }
323 typeSpec := reflect.TypeOf(spec)
324 typeTarget := reflect.TypeOf(target)
325 if typeSpec != typeTarget {
326 return false
327 }
328
329 vSpec := reflect.ValueOf(spec)
330 vTarget := reflect.ValueOf(target)
331 if vSpec.Kind() == reflect.Ptr {
332 vSpec = vSpec.Elem()
333 vTarget = vTarget.Elem()
334 }
335
336 for _, key := range keys {
337 fSpec := vSpec.FieldByName(key)
338 fTarget := vTarget.FieldByName(key)
339 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
340 return false
341 }
342 }
343 return true
344}
345
kdarapu381c6902019-07-31 18:23:16 +0530346func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000347 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530348 dh1 := newMockDeviceHandler()
349 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530350 type args struct {
351 parentPort uint32
352 onuID uint32
353 }
354 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530355 name string
356 devicehandler *DeviceHandler
357 args args
358 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800359 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530360 }{
kdarapu891693b2019-09-16 12:33:49 +0530361 {"GetChildDevice-1", dh1,
362 args{parentPort: 1,
363 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800364 &voltha.Device{
365 Id: "1",
366 ParentId: "olt",
367 ParentPortNo: 1,
368 },
369 nil,
kdarapu891693b2019-09-16 12:33:49 +0530370 },
371 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530372 args{parentPort: 1,
373 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800374 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530375 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530376 },
377 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800378
379 /*
380 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
381 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>)
382 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
383 */
kdarapu381c6902019-07-31 18:23:16 +0530384 for _, tt := range tests {
385 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000386 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800387 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
388 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
389 tt.want, tt.errType, got, reflect.TypeOf(err))
390 return
391 }
kdarapu381c6902019-07-31 18:23:16 +0530392 t.Log("onu device id", got)
393 })
394 }
395}
kdarapu891693b2019-09-16 12:33:49 +0530396
397func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530398 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530399 type args struct {
400 portNum uint32
401 portType voltha.Port_PortType
402 }
403 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800404 name string
405 args args
406 want string
407 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530408 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800409 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
410 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
411 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
412 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
413 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
414 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
415 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530416 }
417 for _, tt := range tests {
418 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800419 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
420 if reflect.TypeOf(err) != tt.errType || got != tt.want {
421 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
422 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530423 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800424
kdarapu891693b2019-09-16 12:33:49 +0530425 })
426 }
427}
428
429func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000430 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530431 dh := newMockDeviceHandler()
432 proxyAddr := dh.device.ProxyAddress
433 body := &ic.InterAdapterOmciMessage{
434 Message: []byte("asdfasdfasdfasdfas"),
435 ProxyAddress: proxyAddr,
436 }
437 body2 := &ic.InterAdapterOmciMessage{
438 Message: []byte("asdfasdfasdfasdfas"),
439 //ProxyAddress: &voltha.Device_ProxyAddress{},
440 }
441 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
442 var marshalledData *any.Any
443 var err error
444
445 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000446 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530447 }
448
449 var marshalledData1 *any.Any
450
451 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000452 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530453 }
454 var marshalledData2 *any.Any
455
456 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000457 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530458 }
459 type args struct {
460 msg *ic.InterAdapterMessage
461 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530462 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530463 tests := []struct {
464 name string
465 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800466 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530467 }{
468 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
469 Header: &ic.InterAdapterHeader{
470 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800471 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530472 },
473 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800474 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530475 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
476 Header: &ic.InterAdapterHeader{
477 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800478 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530479 },
480 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800481 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530482 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
483 Header: &ic.InterAdapterHeader{
484 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800485 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530486 },
487 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530488 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530489 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
490 Header: &ic.InterAdapterHeader{
491 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800492 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530493 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800494 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530495 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
496 Header: &ic.InterAdapterHeader{
497 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800498 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530499 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800500 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530501 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
502 Header: &ic.InterAdapterHeader{
503 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800504 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530505 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800506 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530507 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
508 Header: &ic.InterAdapterHeader{
509 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800510 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530511 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800512 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530513 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
514 Header: &ic.InterAdapterHeader{
515 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800516 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530517 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800518 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530519 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
520 Header: &ic.InterAdapterHeader{
521 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800522 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530523 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800524 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530525 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
526 Header: &ic.InterAdapterHeader{
527 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800528 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530529 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800530 }}, invalid},
531 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
532 Header: &ic.InterAdapterHeader{
533 Id: "012345",
534 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
535 }, Body: marshalledData2,
536 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530537 }
538 for _, tt := range tests {
539 t.Run(tt.name, func(t *testing.T) {
540
Neha Sharma96b7bf22020-06-15 10:37:32 +0000541 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530542 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
543 }
544 })
545 }
546}
547
548func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000549 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530550 dh1 := newMockDeviceHandler()
551 dh2 := negativeDeviceHandler()
552 device1 := &voltha.Device{
553 Id: "onu1",
554 Root: false,
555 ParentId: "logical_device",
556 ProxyAddress: &voltha.Device_ProxyAddress{
557 DeviceId: "onu1",
558 DeviceType: "onu",
559 ChannelId: 1,
560 ChannelGroupId: 1,
561 },
562 ConnectStatus: 1,
563 }
564 device2 := device1
565 device2.ConnectStatus = 2
566 iaomciMsg1 := &ic.InterAdapterOmciMessage{
567 ProxyAddress: &voltha.Device_ProxyAddress{
568 DeviceId: "onu2",
569 DeviceType: "onu",
570 ChannelId: 1,
571 ChannelGroupId: 1,
572 //OnuId: 2,
573 },
574 ConnectStatus: 1,
575 }
576 iaomciMsg2 := &ic.InterAdapterOmciMessage{
577 ProxyAddress: &voltha.Device_ProxyAddress{
578 DeviceId: "onu3",
579 DeviceType: "onu",
580 ChannelId: 1,
581 ChannelGroupId: 1,
582 },
583 ConnectStatus: 1,
584 }
585 type args struct {
586 onuDevice *voltha.Device
587 omciMsg *ic.InterAdapterOmciMessage
588 }
589 tests := []struct {
590 name string
591 devicehandler *DeviceHandler
592 args args
593 }{
594 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
595 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
596 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
597 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
598 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
599 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
600 }
601 for _, tt := range tests {
602 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400603 _ = tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
604 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530605 })
606 }
607}
608
609func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
610 dh1 := newMockDeviceHandler()
611 dh2 := negativeDeviceHandler()
612
613 type args struct {
614 logicalPort uint32
615 packetPayload []byte
616 }
617 tests := []struct {
618 name string
619 devicehandler *DeviceHandler
620 args args
621 }{
622 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
623 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
624 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
625 }
626 for _, tt := range tests {
627 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400628 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
629 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530630 })
631 }
632}
633
634func TestDeviceHandler_DisableDevice(t *testing.T) {
635 dh1 := newMockDeviceHandler()
636 dh2 := negativeDeviceHandler()
637 type args struct {
638 device *voltha.Device
639 }
640 tests := []struct {
641 name string
642 devicehandler *DeviceHandler
643 args args
644 wantErr bool
645 }{
646 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400647 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530648 }
649 for _, tt := range tests {
650 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000651 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530652 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
653 }
654 })
655 }
656}
657
658func TestDeviceHandler_ReenableDevice(t *testing.T) {
659 dh1 := newMockDeviceHandler()
660 dh2 := negativeDeviceHandler()
661 type args struct {
662 device *voltha.Device
663 }
664 tests := []struct {
665 name string
666 devicehandler *DeviceHandler
667 args args
668 wantErr bool
669 }{
670 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
671 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
672 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
673 }
674 for _, tt := range tests {
675 t.Run(tt.name, func(t *testing.T) {
676 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000677 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530678 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
679 }
680 })
681 }
682}
683
684func TestDeviceHandler_RebootDevice(t *testing.T) {
685 dh1 := newMockDeviceHandler()
686 dh2 := newMockDeviceHandler()
687 type args struct {
688 device *voltha.Device
689 }
690 tests := []struct {
691 name string
692 devicehandler *DeviceHandler
693 args args
694 wantErr bool
695 }{
696 // TODO: Add test cases.
697 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
698 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
699 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
700 }
701 for _, tt := range tests {
702 t.Run(tt.name, func(t *testing.T) {
703
Neha Sharma96b7bf22020-06-15 10:37:32 +0000704 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530705 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
706 }
707 })
708 }
709}
710
711func TestDeviceHandler_handleIndication(t *testing.T) {
712 dh1 := newMockDeviceHandler()
713 dh2 := negativeDeviceHandler()
714 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530715 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530716 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
717 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530718
kdarapu891693b2019-09-16 12:33:49 +0530719 type args struct {
720 indication *oop.Indication
721 }
722 tests := []struct {
723 name string
724 deviceHandler *DeviceHandler
725 args args
726 }{
727 // TODO: Add test cases.
728 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
729 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
730 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
731 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
732 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
733 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
734 {"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")}}}}}},
735 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
736 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
737 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
738 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
739 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
740 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
741 {"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}}}}},
742 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
743 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
744 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
745 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
746
747 // Negative testcases
748 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
749 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
750 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
751 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
752 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
753 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
754 {"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")}}}}}},
755 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
756 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
757 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
758 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
759 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
760 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
761 {"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}}}}},
762 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
763 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
764 //
765 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
766 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
767 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
768 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
769 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
770 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
771 }
772 for _, tt := range tests {
773 t.Run(tt.name, func(t *testing.T) {
774 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530775 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
776 defer cancel()
777 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530778 })
779 }
780}
781
782func TestDeviceHandler_addPort(t *testing.T) {
783 dh1 := newMockDeviceHandler()
784 dh2 := negativeDeviceHandler()
785 type args struct {
786 intfID uint32
787 portType voltha.Port_PortType
788 state string
789 }
790 tests := []struct {
791 name string
792 devicehandler *DeviceHandler
793 args args
794 }{
795 // State up
796 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
797 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
798 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
799 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
800 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
801 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
802 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
803 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
804 // state discovery
805 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
806 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
807 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
808 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
809 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
810 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
811 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
812 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
813
814 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
815 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
816 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
817 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
818 }
819 for _, tt := range tests {
820 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400821 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
822 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530823 })
824 }
825}
826
827func Test_macAddressToUint32Array(t *testing.T) {
828 type args struct {
829 mac string
830 }
831 tests := []struct {
832 name string
833 args args
834 want []uint32
835 }{
836 // TODO: Add test cases.
837 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
838 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
839 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
840 }
841 for _, tt := range tests {
842 t.Run(tt.name, func(t *testing.T) {
843 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
844 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
845 }
846 })
847 }
848}
849
850func TestDeviceHandler_handleOltIndication(t *testing.T) {
851
852 type args struct {
853 oltIndication *oop.OltIndication
854 }
855 tests := []struct {
856 name string
857 args args
858 }{
859 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
860 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
861 }
862 for _, tt := range tests {
863 t.Run(tt.name, func(t *testing.T) {
864 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530865 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
866 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400867 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
868 t.Error(err)
869 }
kdarapu891693b2019-09-16 12:33:49 +0530870 })
871 }
872}
873
874func TestDeviceHandler_AdoptDevice(t *testing.T) {
875 dh1 := newMockDeviceHandler()
876 dh2 := negativeDeviceHandler()
877 type args struct {
878 device *voltha.Device
879 }
880 tests := []struct {
881 name string
882 devicehandler *DeviceHandler
883 args args
884 }{
885 // TODO: Add test cases.
886 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530887 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530888 }
889 for _, tt := range tests {
890 t.Run(tt.name, func(t *testing.T) {
891 //dh.doStateInit()
892 // context.
893 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530894 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
895 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400896 if err := tt.devicehandler.postInit(ctx); err != nil {
897 t.Error(err)
898 }
kdarapu891693b2019-09-16 12:33:49 +0530899 })
900 }
901}
902
903func TestDeviceHandler_activateONU(t *testing.T) {
904 dh := newMockDeviceHandler()
905 dh1 := negativeDeviceHandler()
906 type args struct {
907 intfID uint32
908 onuID int64
909 serialNum *oop.SerialNumber
910 serialNumber string
911 }
912 tests := []struct {
913 name string
914 devicehandler *DeviceHandler
915 args args
916 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700917 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
918 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
919 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
920 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530921 }
922 for _, tt := range tests {
923 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530924 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
925 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400926 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
927 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530928 })
929 }
930}
931
932func TestDeviceHandler_start(t *testing.T) {
933 dh := newMockDeviceHandler()
934 dh1 := negativeDeviceHandler()
935 dh.start(context.Background())
936 dh.stop(context.Background())
937
938 dh1.start(context.Background())
939 dh1.stop(context.Background())
940
941}
942
943func TestDeviceHandler_PacketOut(t *testing.T) {
944 dh1 := newMockDeviceHandler()
945 dh2 := negativeDeviceHandler()
946 acts := []*ofp.OfpAction{
947 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
948 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
949 fu.Output(1),
950 }
951 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
952 type args struct {
953 egressPortNo int
954 packet *of.OfpPacketOut
955 }
956 tests := []struct {
957 name string
958 devicehandler *DeviceHandler
959 args args
960 wantErr bool
961 }{
962 // TODO: Add test cases.
963 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
964 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
965 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700966 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
967 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
968 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530969 }
970 for _, tt := range tests {
971 t.Run(tt.name, func(t *testing.T) {
972 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530973 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
974 defer cancel()
975 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530976 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
977 }
978 })
979 }
980}
981
982//
983func TestDeviceHandler_doStateUp(t *testing.T) {
984 dh1 := newMockDeviceHandler()
985 dh2 := newMockDeviceHandler()
986
Thomas Lee S985938d2020-05-04 11:40:41 +0530987 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530988 dh3 := negativeDeviceHandler()
989
990 tests := []struct {
991 name string
992 devicehandler *DeviceHandler
993 wantErr bool
994 }{
995 {"dostateup-1", dh1, false},
996 {"dostateup-2", dh2, false},
997 {"dostateup-3", dh3, true},
998 }
999 for _, tt := range tests {
1000 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301001 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1002 defer cancel()
1003 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301004 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1005 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301006 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301007 })
1008 }
1009}
1010func TestDeviceHandler_doStateDown(t *testing.T) {
1011 dh1 := newMockDeviceHandler()
1012 dh2 := negativeDeviceHandler()
1013 dh3 := newMockDeviceHandler()
1014 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1015 tests := []struct {
1016 name string
1017 devicehandler *DeviceHandler
1018 wantErr bool
1019 }{
1020 {"dostatedown-1", dh1, false},
1021 {"dostatedown-2", dh2, true},
1022 {"dostatedown-2", dh3, true},
1023 }
1024 for _, tt := range tests {
1025 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301026 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1027 defer cancel()
1028 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301029 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001030 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301031 }
1032 })
1033 }
1034}
1035
1036func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1037 dh1 := newMockDeviceHandler()
1038 dh2 := negativeDeviceHandler()
1039 type args struct {
1040 device *voltha.Device
1041 }
1042 tests := []struct {
1043 name string
1044 devicehandler *DeviceHandler
1045 args args
1046 wantErr bool
1047 }{
1048 // TODO: Add test cases.
1049 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1050 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1051 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1052 }
1053 for _, tt := range tests {
1054 t.Run(tt.name, func(t *testing.T) {
1055 dh := tt.devicehandler
1056 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1057 if (err != nil) != tt.wantErr {
1058 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1059 return
1060 }
1061 })
1062 }
1063}
1064
kdarapu891693b2019-09-16 12:33:49 +05301065func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1066
1067 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301068 dh1.discOnus = sync.Map{}
1069 dh1.discOnus.Store("onu1", true)
1070 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301071 dh1.discOnus.Store("onu3", true)
1072 dh1.discOnus.Store("onu4", true)
1073 dh1.onus = sync.Map{}
1074 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1075 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301076 dh2 := negativeDeviceHandler()
1077 type args struct {
1078 onuDiscInd *oop.OnuDiscIndication
1079 sn string
1080 }
1081 tests := []struct {
1082 name string
1083 devicehandler *DeviceHandler
1084 args args
1085 }{
1086 // TODO: Add test cases.
1087 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1088 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1089 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1090 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1091 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1092 {"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 +05301093 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1094 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1095 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301096 }
1097 for _, tt := range tests {
1098 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301099 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1100 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001101 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001102 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301103 })
1104 }
1105}
1106
1107func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1108 dh1 := newMockDeviceHandler()
1109 dh2 := negativeDeviceHandler()
1110 tests := []struct {
1111 name string
1112 devicehandler *DeviceHandler
1113
1114 wantErr bool
1115 }{
1116 // TODO: Add test cases.
1117 {"populateDeviceInfo-1", dh1, false},
1118 {"populateDeviceInfo-2", dh1, true},
1119 {"populateDeviceInfo-3", dh1, true},
1120 {"populateDeviceInfo-4", dh1, true},
1121 {"populateDeviceInfo-5", dh2, true},
1122 }
1123 for _, tt := range tests {
1124 t.Run(tt.name, func(t *testing.T) {
1125
Neha Sharma96b7bf22020-06-15 10:37:32 +00001126 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301127 if (err != nil) != tt.wantErr {
1128 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1129 return
1130 }
1131
1132 })
1133 }
1134}
1135
1136func TestDeviceHandler_readIndications(t *testing.T) {
1137 dh1 := newMockDeviceHandler()
1138 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301139 dh3 := newMockDeviceHandler()
1140 dh3.device.AdminState = voltha.AdminState_DISABLED
1141 dh4 := negativeDeviceHandler()
1142 tests := []struct {
1143 name string
1144 devicehandler *DeviceHandler
1145 }{
1146 // TODO: Add test cases.
1147 {"readIndications-1", dh1},
1148 {"readIndications-2", dh2},
1149 {"readIndications-3", dh2},
1150 {"readIndications-4", dh2},
1151 {"readIndications-5", dh2},
1152 {"readIndications-6", dh3},
1153 {"readIndications-7", dh3},
1154 {"readIndications-8", dh4},
1155 }
1156 for _, tt := range tests {
1157 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301158 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1159 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001160 _ = tt.devicehandler.readIndications(ctx)
1161 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301162 })
1163 }
1164}
Naga Manjunath7615e552019-10-11 22:35:47 +05301165
1166func Test_startCollector(t *testing.T) {
1167 type args struct {
1168 dh *DeviceHandler
1169 }
1170 dh := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001171 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301172 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1173 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1174 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1175 }
Naga Manjunath7615e552019-10-11 22:35:47 +05301176 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301177 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1178 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301179 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1180 dh.portStats.Device = dh
1181 for i := 0; i < 16; i++ {
1182 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1183 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301184 dh1 := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001185 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301186 tests := []struct {
1187 name string
1188 args args
1189 }{
1190 // TODO: Add test cases.
1191 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301192 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301193 }
1194 for _, tt := range tests {
1195 t.Run(tt.name, func(t *testing.T) {
1196 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301197 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301198 tt.args.dh.stopCollector <- true
1199 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001200 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301201 })
1202 }
1203}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001204
1205func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1206
1207 // olt disconnect (not reboot)
1208 dh1 := newMockDeviceHandler()
1209 dh1.adapterPreviouslyConnected = false
1210 dh1.agentPreviouslyConnected = true
1211
1212 // adapter restart
1213 dh2 := newMockDeviceHandler()
1214 dh2.Client = &mocks.MockOpenoltClient{}
1215 dh2.adapterPreviouslyConnected = true
1216 dh2.agentPreviouslyConnected = true
1217
1218 // first connection or olt restart
1219 dh3 := newMockDeviceHandler()
1220 dh3.Client = &mocks.MockOpenoltClient{}
1221 dh3.adapterPreviouslyConnected = false
1222 dh3.agentPreviouslyConnected = false
1223
1224 // olt and adapter restart at the same time (first case)
1225 dh4 := newMockDeviceHandler()
1226 dh4.Client = &mocks.MockOpenoltClient{}
1227 dh4.adapterPreviouslyConnected = true
1228 dh4.agentPreviouslyConnected = false
1229
1230 // adapter restart and olt disconnect at the same time
1231 dh5 := newMockDeviceHandler()
1232 dh5.Client = &mocks.MockOpenoltClient{}
1233 dh5.adapterPreviouslyConnected = true
1234 dh5.agentPreviouslyConnected = true
1235
1236 tests := []struct {
1237 name string
1238 devicehandler *DeviceHandler
1239 expectedRestart bool
1240 wantErr bool
1241 }{
1242 {"dostateup-1", dh1, true, false},
1243 {"dostateup-2", dh2, false, false},
1244 {"dostateup-3", dh3, false, false},
1245 {"dostateup-4", dh4, true, false},
1246 {"dostateup-5", dh5, false, false},
1247 }
1248 for _, tt := range tests {
1249 t.Run(tt.name, func(t *testing.T) {
1250 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1251 defer cancel()
1252 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1253 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1254 }
1255 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1256 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1257 if tt.expectedRestart != isRestarted {
1258 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1259 }
1260 })
1261 }
1262}