blob: 38affa07cea3cfbedb6305b08a7278b383122f3e [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
kdarapu381c6902019-07-31 18:23:16 +053022 "net"
kdarapu891693b2019-09-16 12:33:49 +053023 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053024 "sync"
kdarapu381c6902019-07-31 18:23:16 +053025 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053026 "time"
27
Esin Karamanccb714b2019-11-29 15:02:06 +000028 "github.com/opencord/voltha-lib-go/v3/pkg/pmmetrics"
kdarapu381c6902019-07-31 18:23:16 +053029
kdarapu891693b2019-09-16 12:33:49 +053030 "github.com/golang/protobuf/ptypes"
31 "github.com/golang/protobuf/ptypes/any"
Esin Karamanccb714b2019-11-29 15:02:06 +000032 "github.com/opencord/voltha-lib-go/v3/pkg/db"
33 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
34 "github.com/opencord/voltha-lib-go/v3/pkg/log"
35 ponrmgr "github.com/opencord/voltha-lib-go/v3/pkg/ponresourcemanager"
Thomas Lee S94109f12020-03-03 16:39:29 +053036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
38 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Esin Karamanccb714b2019-11-29 15:02:06 +000039 ic "github.com/opencord/voltha-protos/v3/go/inter_container"
40 of "github.com/opencord/voltha-protos/v3/go/openflow_13"
41 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
42 oop "github.com/opencord/voltha-protos/v3/go/openolt"
43 "github.com/opencord/voltha-protos/v3/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053044)
45
kdarapu891693b2019-09-16 12:33:49 +053046func newMockCoreProxy() *mocks.MockCoreProxy {
47 mcp := mocks.MockCoreProxy{}
48 mcp.Devices = make(map[string]*voltha.Device)
Naga Manjunath7615e552019-10-11 22:35:47 +053049 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053050 mcp.Devices["olt"] = &voltha.Device{
51
52 Id: "olt",
53 Root: true,
54 ParentId: "logical_device",
55 ParentPortNo: 1,
56
57 Ports: []*voltha.Port{
58 {PortNo: 1, Label: "pon"},
59 {PortNo: 2, Label: "nni"},
60 },
61 ProxyAddress: &voltha.Device_ProxyAddress{
62 DeviceId: "olt",
63 DeviceType: "onu",
64 ChannelId: 1,
65 ChannelGroupId: 1,
66 },
67 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053068 PmConfigs: &voltha.PmConfigs{
69 DefaultFreq: 10,
70 Id: "olt",
71 FreqOverride: false,
72 Grouped: false,
73 Metrics: pm,
74 },
kdarapu891693b2019-09-16 12:33:49 +053075 }
76 mcp.Devices["onu1"] = &voltha.Device{
77
78 Id: "1",
79 Root: false,
80 ParentId: "olt",
81 ParentPortNo: 1,
82
83 Ports: []*voltha.Port{
84 {PortNo: 1, Label: "pon"},
85 {PortNo: 2, Label: "uni"},
86 },
87 OperStatus: 4,
88 ProxyAddress: &voltha.Device_ProxyAddress{
89 OnuId: 1,
90 ChannelId: 1,
91 ChannelGroupId: 1,
92 },
93 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053094 PmConfigs: &voltha.PmConfigs{
95 DefaultFreq: 10,
96 Id: "olt",
97 FreqOverride: false,
98 Grouped: false,
99 Metrics: pm,
100 },
kdarapu891693b2019-09-16 12:33:49 +0530101 }
102 mcp.Devices["onu2"] = &voltha.Device{
103 Id: "2",
104 Root: false,
105 ParentId: "olt",
106 OperStatus: 2,
107 Ports: []*voltha.Port{
108 {PortNo: 1, Label: "pon"},
109 {PortNo: 2, Label: "uni"},
110 },
111
112 ParentPortNo: 1,
113
114 ProxyAddress: &voltha.Device_ProxyAddress{
115 OnuId: 2,
116 ChannelId: 1,
117 ChannelGroupId: 1,
118 },
119 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530120 PmConfigs: &voltha.PmConfigs{
121 DefaultFreq: 10,
122 Id: "olt",
123 FreqOverride: false,
124 Grouped: false,
125 Metrics: pm,
126 },
kdarapu891693b2019-09-16 12:33:49 +0530127 }
128 return &mcp
129}
130func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530131 device := &voltha.Device{
132 Id: "olt",
133 Root: true,
134 ParentId: "logical_device",
135 Ports: []*voltha.Port{
kdarapu1afeceb2020-02-12 01:38:09 -0500136 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
137 {PortNo: 2, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
kdarapu381c6902019-07-31 18:23:16 +0530138 },
kdarapu891693b2019-09-16 12:33:49 +0530139 ProxyAddress: &voltha.Device_ProxyAddress{
140 DeviceId: "olt",
141 DeviceType: "onu",
142 ChannelId: 1,
143 ChannelGroupId: 1,
144 },
145 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530146 }
kdarapu891693b2019-09-16 12:33:49 +0530147 cp := newMockCoreProxy()
148 ap := &mocks.MockAdapterProxy{}
149 ep := &mocks.MockEventProxy{}
150 openOLT := &OpenOLT{coreProxy: cp, adapterProxy: ap, eventProxy: ep}
151 dh := NewDeviceHandler(cp, ap, ep, device, openOLT)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700152 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
153 IntfIds: []uint32{0, 1},
154 Technology: "xgs-pon",
155 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
156 }}
157
Thomas Lee S985938d2020-05-04 11:40:41 +0530158 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: 2}
159 rsrMgr := resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530160 KVStore: &db.Backend{
161 Client: &mocks.MockKVClient{},
162 }}
Girish Gowdra38d533d2020-03-30 20:38:51 -0700163 rsrMgr.AllocIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
164 rsrMgr.GemPortIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
165 rsrMgr.OnuIDMgmtLock = make([]sync.RWMutex, deviceInf.PonPorts)
166
167 dh.resourceMgr = &rsrMgr
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530168 dh.resourceMgr.ResourceMgrs = make(map[uint32]*ponrmgr.PONResourceManager)
169 ranges := make(map[string]interface{})
170 sharedIdxByType := make(map[string]string)
171 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
172 sharedIdxByType["ONU_ID"] = "ONU_ID"
173 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
174 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
175 ranges["ONU_ID"] = uint32(0)
176 ranges["GEMPORT_ID"] = uint32(0)
177 ranges["ALLOC_ID"] = uint32(0)
178 ranges["FLOW_ID"] = uint32(0)
179 ranges["onu_id_shared"] = uint32(0)
180 ranges["alloc_id_shared"] = uint32(0)
181 ranges["gemport_id_shared"] = uint32(0)
182 ranges["flow_id_shared"] = uint32(0)
183
184 ponmgr := &ponrmgr.PONResourceManager{
185 DeviceID: "onu-1",
Girish Gowdra38d533d2020-03-30 20:38:51 -0700186 IntfIDs: []uint32{0, 1},
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530187 KVStore: &db.Backend{
188 Client: &mocks.MockKVClient{},
189 },
190 PonResourceRanges: ranges,
191 SharedIdxByType: sharedIdxByType,
192 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700193 dh.resourceMgr.ResourceMgrs[0] = ponmgr
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530194 dh.resourceMgr.ResourceMgrs[1] = ponmgr
npujarec5762e2020-01-01 14:08:48 +0530195 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
196 defer cancel()
197 dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr)
kdarapu891693b2019-09-16 12:33:49 +0530198 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500199 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530200 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530201 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000202
203 var pmNames = []string{
204 "rx_bytes",
205 "rx_packets",
206 "rx_mcast_packets",
207 "rx_bcast_packets",
208 "tx_bytes",
209 "tx_packets",
210 "tx_mcast_packets",
211 "tx_bcast_packets",
212 }
213
214 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530215 return dh
kdarapu381c6902019-07-31 18:23:16 +0530216}
217
kdarapu891693b2019-09-16 12:33:49 +0530218func negativeDeviceHandler() *DeviceHandler {
219 dh := newMockDeviceHandler()
220 device := dh.device
221 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530222 return dh
223}
kdarapu381c6902019-07-31 18:23:16 +0530224func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000225 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530226 type args struct {
227 host string
228 }
229 tests := []struct {
230 name string
231 args args
232 want string
233 wantErr bool
234 }{
kdarapu891693b2019-09-16 12:33:49 +0530235 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
236 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
237 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
238 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530239 }
240 for _, tt := range tests {
241 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000242 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530243 if (err != nil) != tt.wantErr {
244 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
245 return
246 }
247 if got != tt.want {
248 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
249 }
250 })
251 }
252}
253func Test_macifyIP(t *testing.T) {
254 type args struct {
255 ip net.IP
256 }
257 tests := []struct {
258 name string
259 args args
260 want string
261 }{{
kdarapu891693b2019-09-16 12:33:49 +0530262 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530263 args{ip: net.ParseIP("10.10.10.10")},
264 "00:00:0a:0a:0a:0a",
265 },
266 {
kdarapu891693b2019-09-16 12:33:49 +0530267 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530268 args{ip: net.ParseIP("127.0.0.1")},
269 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530270 },
271 {
272 "macifyIP-3",
273 args{ip: net.ParseIP("127.0.0.1/24")},
274 "",
275 },
276 }
kdarapu381c6902019-07-31 18:23:16 +0530277 for _, tt := range tests {
278 t.Run(tt.name, func(t *testing.T) {
279 if got := macifyIP(tt.args.ip); got != tt.want {
280 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
281 }
282 })
283 }
284}
285
David K. Bainbridge794735f2020-02-11 21:01:37 -0800286func sparseCompare(keys []string, spec, target interface{}) bool {
287 if spec == target {
288 return true
289 }
290 if spec == nil || target == nil {
291 return false
292 }
293 typeSpec := reflect.TypeOf(spec)
294 typeTarget := reflect.TypeOf(target)
295 if typeSpec != typeTarget {
296 return false
297 }
298
299 vSpec := reflect.ValueOf(spec)
300 vTarget := reflect.ValueOf(target)
301 if vSpec.Kind() == reflect.Ptr {
302 vSpec = vSpec.Elem()
303 vTarget = vTarget.Elem()
304 }
305
306 for _, key := range keys {
307 fSpec := vSpec.FieldByName(key)
308 fTarget := vTarget.FieldByName(key)
309 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
310 return false
311 }
312 }
313 return true
314}
315
kdarapu381c6902019-07-31 18:23:16 +0530316func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000317 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530318 dh1 := newMockDeviceHandler()
319 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530320 type args struct {
321 parentPort uint32
322 onuID uint32
323 }
324 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530325 name string
326 devicehandler *DeviceHandler
327 args args
328 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800329 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530330 }{
kdarapu891693b2019-09-16 12:33:49 +0530331 {"GetChildDevice-1", dh1,
332 args{parentPort: 1,
333 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800334 &voltha.Device{
335 Id: "1",
336 ParentId: "olt",
337 ParentPortNo: 1,
338 },
339 nil,
kdarapu891693b2019-09-16 12:33:49 +0530340 },
341 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530342 args{parentPort: 1,
343 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800344 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530345 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530346 },
347 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800348
349 /*
350 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
351 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>)
352 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
353 */
kdarapu381c6902019-07-31 18:23:16 +0530354 for _, tt := range tests {
355 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000356 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800357 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
358 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
359 tt.want, tt.errType, got, reflect.TypeOf(err))
360 return
361 }
kdarapu381c6902019-07-31 18:23:16 +0530362 t.Log("onu device id", got)
363 })
364 }
365}
kdarapu891693b2019-09-16 12:33:49 +0530366
367func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530368 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530369 type args struct {
370 portNum uint32
371 portType voltha.Port_PortType
372 }
373 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800374 name string
375 args args
376 want string
377 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530378 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800379 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
380 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
381 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
382 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
383 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
384 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
385 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530386 }
387 for _, tt := range tests {
388 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800389 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
390 if reflect.TypeOf(err) != tt.errType || got != tt.want {
391 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
392 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530393 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800394
kdarapu891693b2019-09-16 12:33:49 +0530395 })
396 }
397}
398
399func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000400 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530401 dh := newMockDeviceHandler()
402 proxyAddr := dh.device.ProxyAddress
403 body := &ic.InterAdapterOmciMessage{
404 Message: []byte("asdfasdfasdfasdfas"),
405 ProxyAddress: proxyAddr,
406 }
407 body2 := &ic.InterAdapterOmciMessage{
408 Message: []byte("asdfasdfasdfasdfas"),
409 //ProxyAddress: &voltha.Device_ProxyAddress{},
410 }
411 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
412 var marshalledData *any.Any
413 var err error
414
415 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000416 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530417 }
418
419 var marshalledData1 *any.Any
420
421 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000422 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530423 }
424 var marshalledData2 *any.Any
425
426 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000427 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"error": err})
kdarapu891693b2019-09-16 12:33:49 +0530428 }
429 type args struct {
430 msg *ic.InterAdapterMessage
431 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530432 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530433 tests := []struct {
434 name string
435 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800436 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530437 }{
438 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
439 Header: &ic.InterAdapterHeader{
440 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800441 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530442 },
443 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800444 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530445 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
446 Header: &ic.InterAdapterHeader{
447 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800448 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530449 },
450 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800451 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530452 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
453 Header: &ic.InterAdapterHeader{
454 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800455 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530456 },
457 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530458 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530459 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
460 Header: &ic.InterAdapterHeader{
461 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800462 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530463 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800464 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530465 {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
466 Header: &ic.InterAdapterHeader{
467 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800468 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530469 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800470 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530471 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
472 Header: &ic.InterAdapterHeader{
473 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800474 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530475 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800476 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530477 {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
478 Header: &ic.InterAdapterHeader{
479 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800480 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530481 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800482 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530483 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
484 Header: &ic.InterAdapterHeader{
485 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800486 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530487 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800488 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530489 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
490 Header: &ic.InterAdapterHeader{
491 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800492 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
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-10", args{msg: &ic.InterAdapterMessage{
496 Header: &ic.InterAdapterHeader{
497 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800498 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530499 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800500 }}, invalid},
501 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
502 Header: &ic.InterAdapterHeader{
503 Id: "012345",
504 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
505 }, Body: marshalledData2,
506 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530507 }
508 for _, tt := range tests {
509 t.Run(tt.name, func(t *testing.T) {
510
Neha Sharma96b7bf22020-06-15 10:37:32 +0000511 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530512 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
513 }
514 })
515 }
516}
517
518func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000519 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530520 dh1 := newMockDeviceHandler()
521 dh2 := negativeDeviceHandler()
522 device1 := &voltha.Device{
523 Id: "onu1",
524 Root: false,
525 ParentId: "logical_device",
526 ProxyAddress: &voltha.Device_ProxyAddress{
527 DeviceId: "onu1",
528 DeviceType: "onu",
529 ChannelId: 1,
530 ChannelGroupId: 1,
531 },
532 ConnectStatus: 1,
533 }
534 device2 := device1
535 device2.ConnectStatus = 2
536 iaomciMsg1 := &ic.InterAdapterOmciMessage{
537 ProxyAddress: &voltha.Device_ProxyAddress{
538 DeviceId: "onu2",
539 DeviceType: "onu",
540 ChannelId: 1,
541 ChannelGroupId: 1,
542 //OnuId: 2,
543 },
544 ConnectStatus: 1,
545 }
546 iaomciMsg2 := &ic.InterAdapterOmciMessage{
547 ProxyAddress: &voltha.Device_ProxyAddress{
548 DeviceId: "onu3",
549 DeviceType: "onu",
550 ChannelId: 1,
551 ChannelGroupId: 1,
552 },
553 ConnectStatus: 1,
554 }
555 type args struct {
556 onuDevice *voltha.Device
557 omciMsg *ic.InterAdapterOmciMessage
558 }
559 tests := []struct {
560 name string
561 devicehandler *DeviceHandler
562 args args
563 }{
564 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
565 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
566 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
567 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
568 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
569 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
570 }
571 for _, tt := range tests {
572 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000573 tt.devicehandler.sendProxiedMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
kdarapu891693b2019-09-16 12:33:49 +0530574 })
575 }
576}
577
578func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
579 dh1 := newMockDeviceHandler()
580 dh2 := negativeDeviceHandler()
581
582 type args struct {
583 logicalPort uint32
584 packetPayload []byte
585 }
586 tests := []struct {
587 name string
588 devicehandler *DeviceHandler
589 args args
590 }{
591 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
592 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
593 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
594 }
595 for _, tt := range tests {
596 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000597 tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
kdarapu891693b2019-09-16 12:33:49 +0530598 })
599 }
600}
601
602func TestDeviceHandler_DisableDevice(t *testing.T) {
603 dh1 := newMockDeviceHandler()
604 dh2 := negativeDeviceHandler()
605 type args struct {
606 device *voltha.Device
607 }
608 tests := []struct {
609 name string
610 devicehandler *DeviceHandler
611 args args
612 wantErr bool
613 }{
614 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400615 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530616 }
617 for _, tt := range tests {
618 t.Run(tt.name, func(t *testing.T) {
619
Neha Sharma96b7bf22020-06-15 10:37:32 +0000620 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530621 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
622 }
623 })
624 }
625}
626
627func TestDeviceHandler_ReenableDevice(t *testing.T) {
628 dh1 := newMockDeviceHandler()
629 dh2 := negativeDeviceHandler()
630 type args struct {
631 device *voltha.Device
632 }
633 tests := []struct {
634 name string
635 devicehandler *DeviceHandler
636 args args
637 wantErr bool
638 }{
639 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
640 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
641 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
642 }
643 for _, tt := range tests {
644 t.Run(tt.name, func(t *testing.T) {
645 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000646 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530647 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
648 }
649 })
650 }
651}
652
653func TestDeviceHandler_RebootDevice(t *testing.T) {
654 dh1 := newMockDeviceHandler()
655 dh2 := newMockDeviceHandler()
656 type args struct {
657 device *voltha.Device
658 }
659 tests := []struct {
660 name string
661 devicehandler *DeviceHandler
662 args args
663 wantErr bool
664 }{
665 // TODO: Add test cases.
666 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
667 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
668 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
669 }
670 for _, tt := range tests {
671 t.Run(tt.name, func(t *testing.T) {
672
Neha Sharma96b7bf22020-06-15 10:37:32 +0000673 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530674 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
675 }
676 })
677 }
678}
679
680func TestDeviceHandler_handleIndication(t *testing.T) {
681 dh1 := newMockDeviceHandler()
682 dh2 := negativeDeviceHandler()
683 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530684 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530685 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
686 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530687
kdarapu891693b2019-09-16 12:33:49 +0530688 type args struct {
689 indication *oop.Indication
690 }
691 tests := []struct {
692 name string
693 deviceHandler *DeviceHandler
694 args args
695 }{
696 // TODO: Add test cases.
697 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
698 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
699 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
700 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
701 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
702 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
703 {"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")}}}}}},
704 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
705 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
706 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
707 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
708 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
709 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
710 {"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}}}}},
711 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
712 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
713 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
714 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
715
716 // Negative testcases
717 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
718 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
719 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
720 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
721 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
722 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
723 {"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")}}}}}},
724 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
725 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
726 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
727 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
728 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
729 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
730 {"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}}}}},
731 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
732 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
733 //
734 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
735 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
736 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
737 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
738 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
739 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
740 }
741 for _, tt := range tests {
742 t.Run(tt.name, func(t *testing.T) {
743 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530744 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
745 defer cancel()
746 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530747 })
748 }
749}
750
751func TestDeviceHandler_addPort(t *testing.T) {
752 dh1 := newMockDeviceHandler()
753 dh2 := negativeDeviceHandler()
754 type args struct {
755 intfID uint32
756 portType voltha.Port_PortType
757 state string
758 }
759 tests := []struct {
760 name string
761 devicehandler *DeviceHandler
762 args args
763 }{
764 // State up
765 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
766 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
767 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
768 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
769 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
770 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
771 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
772 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
773 // state discovery
774 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
775 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
776 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
777 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
778 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
779 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
780 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
781 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
782
783 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
784 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
785 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
786 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
787 }
788 for _, tt := range tests {
789 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000790 tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
kdarapu891693b2019-09-16 12:33:49 +0530791 })
792 }
793}
794
795func Test_macAddressToUint32Array(t *testing.T) {
796 type args struct {
797 mac string
798 }
799 tests := []struct {
800 name string
801 args args
802 want []uint32
803 }{
804 // TODO: Add test cases.
805 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
806 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
807 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
808 }
809 for _, tt := range tests {
810 t.Run(tt.name, func(t *testing.T) {
811 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
812 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
813 }
814 })
815 }
816}
817
818func TestDeviceHandler_handleOltIndication(t *testing.T) {
819
820 type args struct {
821 oltIndication *oop.OltIndication
822 }
823 tests := []struct {
824 name string
825 args args
826 }{
827 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
828 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
829 }
830 for _, tt := range tests {
831 t.Run(tt.name, func(t *testing.T) {
832 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530833 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
834 defer cancel()
835 dh.handleOltIndication(ctx, tt.args.oltIndication)
kdarapu891693b2019-09-16 12:33:49 +0530836 })
837 }
838}
839
840func TestDeviceHandler_AdoptDevice(t *testing.T) {
841 dh1 := newMockDeviceHandler()
842 dh2 := negativeDeviceHandler()
843 type args struct {
844 device *voltha.Device
845 }
846 tests := []struct {
847 name string
848 devicehandler *DeviceHandler
849 args args
850 }{
851 // TODO: Add test cases.
852 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530853 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530854 }
855 for _, tt := range tests {
856 t.Run(tt.name, func(t *testing.T) {
857 //dh.doStateInit()
858 // context.
859 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530860 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
861 defer cancel()
862 tt.devicehandler.postInit(ctx)
kdarapu891693b2019-09-16 12:33:49 +0530863 })
864 }
865}
866
867func TestDeviceHandler_activateONU(t *testing.T) {
868 dh := newMockDeviceHandler()
869 dh1 := negativeDeviceHandler()
870 type args struct {
871 intfID uint32
872 onuID int64
873 serialNum *oop.SerialNumber
874 serialNumber string
875 }
876 tests := []struct {
877 name string
878 devicehandler *DeviceHandler
879 args args
880 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700881 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
882 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
883 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
884 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530885 }
886 for _, tt := range tests {
887 t.Run(tt.name, func(t *testing.T) {
888
npujarec5762e2020-01-01 14:08:48 +0530889 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
890 defer cancel()
891 tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID,
kdarapu891693b2019-09-16 12:33:49 +0530892 tt.args.serialNum, tt.args.serialNumber)
893 })
894 }
895}
896
897func TestDeviceHandler_start(t *testing.T) {
898 dh := newMockDeviceHandler()
899 dh1 := negativeDeviceHandler()
900 dh.start(context.Background())
901 dh.stop(context.Background())
902
903 dh1.start(context.Background())
904 dh1.stop(context.Background())
905
906}
907
908func TestDeviceHandler_PacketOut(t *testing.T) {
909 dh1 := newMockDeviceHandler()
910 dh2 := negativeDeviceHandler()
911 acts := []*ofp.OfpAction{
912 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
913 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
914 fu.Output(1),
915 }
916 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
917 type args struct {
918 egressPortNo int
919 packet *of.OfpPacketOut
920 }
921 tests := []struct {
922 name string
923 devicehandler *DeviceHandler
924 args args
925 wantErr bool
926 }{
927 // TODO: Add test cases.
928 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
929 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
930 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700931 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
932 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
933 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530934 }
935 for _, tt := range tests {
936 t.Run(tt.name, func(t *testing.T) {
937 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530938 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
939 defer cancel()
940 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530941 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
942 }
943 })
944 }
945}
946
947//
948func TestDeviceHandler_doStateUp(t *testing.T) {
949 dh1 := newMockDeviceHandler()
950 dh2 := newMockDeviceHandler()
951
Thomas Lee S985938d2020-05-04 11:40:41 +0530952 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530953 dh3 := negativeDeviceHandler()
954
955 tests := []struct {
956 name string
957 devicehandler *DeviceHandler
958 wantErr bool
959 }{
960 {"dostateup-1", dh1, false},
961 {"dostateup-2", dh2, false},
962 {"dostateup-3", dh3, true},
963 }
964 for _, tt := range tests {
965 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530966 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
967 defer cancel()
968 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530969 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
970 }
Thomas Lee S85f37312020-04-03 17:06:12 +0530971 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +0530972 })
973 }
974}
975func TestDeviceHandler_doStateDown(t *testing.T) {
976 dh1 := newMockDeviceHandler()
977 dh2 := negativeDeviceHandler()
978 dh3 := newMockDeviceHandler()
979 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
980 tests := []struct {
981 name string
982 devicehandler *DeviceHandler
983 wantErr bool
984 }{
985 {"dostatedown-1", dh1, false},
986 {"dostatedown-2", dh2, true},
987 {"dostatedown-2", dh3, true},
988 }
989 for _, tt := range tests {
990 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530991 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
992 defer cancel()
993 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530994 t.Logf("DeviceHandler.doStateDown() error = %v", err)
995 }
996 })
997 }
998}
999
1000func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1001 dh1 := newMockDeviceHandler()
1002 dh2 := negativeDeviceHandler()
1003 type args struct {
1004 device *voltha.Device
1005 }
1006 tests := []struct {
1007 name string
1008 devicehandler *DeviceHandler
1009 args args
1010 wantErr bool
1011 }{
1012 // TODO: Add test cases.
1013 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1014 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1015 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1016 }
1017 for _, tt := range tests {
1018 t.Run(tt.name, func(t *testing.T) {
1019 dh := tt.devicehandler
1020 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1021 if (err != nil) != tt.wantErr {
1022 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1023 return
1024 }
1025 })
1026 }
1027}
1028
kdarapu891693b2019-09-16 12:33:49 +05301029func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1030
1031 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301032 dh1.discOnus = sync.Map{}
1033 dh1.discOnus.Store("onu1", true)
1034 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301035 dh1.discOnus.Store("onu3", true)
1036 dh1.discOnus.Store("onu4", true)
1037 dh1.onus = sync.Map{}
1038 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1039 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301040 dh2 := negativeDeviceHandler()
1041 type args struct {
1042 onuDiscInd *oop.OnuDiscIndication
1043 sn string
1044 }
1045 tests := []struct {
1046 name string
1047 devicehandler *DeviceHandler
1048 args args
1049 }{
1050 // TODO: Add test cases.
1051 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1052 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1053 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1054 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1055 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1056 {"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 +05301057 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1058 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1059 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301060 }
1061 for _, tt := range tests {
1062 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301063 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1064 defer cancel()
1065 tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd, tt.args.sn)
kdarapu891693b2019-09-16 12:33:49 +05301066 })
1067 }
1068}
1069
1070func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1071 dh1 := newMockDeviceHandler()
1072 dh2 := negativeDeviceHandler()
1073 tests := []struct {
1074 name string
1075 devicehandler *DeviceHandler
1076
1077 wantErr bool
1078 }{
1079 // TODO: Add test cases.
1080 {"populateDeviceInfo-1", dh1, false},
1081 {"populateDeviceInfo-2", dh1, true},
1082 {"populateDeviceInfo-3", dh1, true},
1083 {"populateDeviceInfo-4", dh1, true},
1084 {"populateDeviceInfo-5", dh2, true},
1085 }
1086 for _, tt := range tests {
1087 t.Run(tt.name, func(t *testing.T) {
1088
Neha Sharma96b7bf22020-06-15 10:37:32 +00001089 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301090 if (err != nil) != tt.wantErr {
1091 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1092 return
1093 }
1094
1095 })
1096 }
1097}
1098
1099func TestDeviceHandler_readIndications(t *testing.T) {
1100 dh1 := newMockDeviceHandler()
1101 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301102 dh3 := newMockDeviceHandler()
1103 dh3.device.AdminState = voltha.AdminState_DISABLED
1104 dh4 := negativeDeviceHandler()
1105 tests := []struct {
1106 name string
1107 devicehandler *DeviceHandler
1108 }{
1109 // TODO: Add test cases.
1110 {"readIndications-1", dh1},
1111 {"readIndications-2", dh2},
1112 {"readIndications-3", dh2},
1113 {"readIndications-4", dh2},
1114 {"readIndications-5", dh2},
1115 {"readIndications-6", dh3},
1116 {"readIndications-7", dh3},
1117 {"readIndications-8", dh4},
1118 }
1119 for _, tt := range tests {
1120 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301121 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1122 defer cancel()
1123 tt.devicehandler.readIndications(ctx)
kdarapu891693b2019-09-16 12:33:49 +05301124 })
1125 }
1126}
Naga Manjunath7615e552019-10-11 22:35:47 +05301127
1128func Test_startCollector(t *testing.T) {
1129 type args struct {
1130 dh *DeviceHandler
1131 }
1132 dh := newMockDeviceHandler()
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301133 ports := []*voltha.Port{
1134 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1135 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1136 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1137 }
1138 dh.device.Ports = ports
Naga Manjunath7615e552019-10-11 22:35:47 +05301139 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301140 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1141 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301142 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1143 dh.portStats.Device = dh
1144 for i := 0; i < 16; i++ {
1145 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1146 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301147 dh1 := newMockDeviceHandler()
1148 dh1.device.Ports = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301149 tests := []struct {
1150 name string
1151 args args
1152 }{
1153 // TODO: Add test cases.
1154 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301155 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301156 }
1157 for _, tt := range tests {
1158 t.Run(tt.name, func(t *testing.T) {
1159 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301160 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301161 tt.args.dh.stopCollector <- true
1162 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001163 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301164 })
1165 }
1166}