blob: 57103c4db8706d78df688a1257e2e58b54420ac1 [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
Girish Gowdra4c3d4602021-07-22 16:33:37 -070022 conf "github.com/opencord/voltha-lib-go/v6/pkg/config"
kdarapu381c6902019-07-31 18:23:16 +053023 "net"
kdarapu891693b2019-09-16 12:33:49 +053024 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053025 "sync"
kdarapu381c6902019-07-31 18:23:16 +053026 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053027 "time"
28
kdarapu891693b2019-09-16 12:33:49 +053029 "github.com/golang/protobuf/ptypes"
30 "github.com/golang/protobuf/ptypes/any"
Girish Gowdra4c3d4602021-07-22 16:33:37 -070031 "github.com/opencord/voltha-lib-go/v6/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v6/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v6/pkg/log"
34 "github.com/opencord/voltha-lib-go/v6/pkg/pmmetrics"
35 ponrmgr "github.com/opencord/voltha-lib-go/v6/pkg/ponresourcemanager"
kesavand494c2082020-08-31 11:16:12 +053036 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
Thomas Lee S94109f12020-03-03 16:39:29 +053037 "github.com/opencord/voltha-openolt-adapter/internal/pkg/olterrors"
Scott Bakerdbd960e2020-02-28 08:57:51 -080038 "github.com/opencord/voltha-openolt-adapter/internal/pkg/resourcemanager"
39 "github.com/opencord/voltha-openolt-adapter/pkg/mocks"
Girish Gowdraa09aeab2020-09-14 16:30:52 -070040 ic "github.com/opencord/voltha-protos/v4/go/inter_container"
41 of "github.com/opencord/voltha-protos/v4/go/openflow_13"
42 ofp "github.com/opencord/voltha-protos/v4/go/openflow_13"
43 oop "github.com/opencord/voltha-protos/v4/go/openolt"
44 "github.com/opencord/voltha-protos/v4/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053045)
46
Girish Gowdra9602eb42020-09-09 15:50:39 -070047const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070048 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070049 OnuIDStart = 1
50 OnuIDEnd = 32
51 AllocIDStart = 1
52 AllocIDEnd = 10
53 GemIDStart = 1
54 GemIDEnd = 10
55 FlowIDStart = 1
56 FlowIDEnd = 10
57)
58
kdarapu891693b2019-09-16 12:33:49 +053059func newMockCoreProxy() *mocks.MockCoreProxy {
Kent Hagermanf1db18b2020-07-08 13:38:15 -040060 mcp := mocks.MockCoreProxy{
61 Devices: make(map[string]*voltha.Device),
62 DevicePorts: make(map[string][]*voltha.Port),
63 }
Naga Manjunath7615e552019-10-11 22:35:47 +053064 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053065 mcp.Devices["olt"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053066 Id: "olt",
67 Root: true,
68 ParentId: "logical_device",
69 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +053070 ProxyAddress: &voltha.Device_ProxyAddress{
71 DeviceId: "olt",
72 DeviceType: "onu",
73 ChannelId: 1,
74 ChannelGroupId: 1,
75 },
76 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053077 PmConfigs: &voltha.PmConfigs{
78 DefaultFreq: 10,
79 Id: "olt",
80 FreqOverride: false,
81 Grouped: false,
82 Metrics: pm,
83 },
kdarapu891693b2019-09-16 12:33:49 +053084 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040085 mcp.DevicePorts["olt"] = []*voltha.Port{
86 {PortNo: 1, Label: "pon"},
87 {PortNo: 2, Label: "nni"},
88 }
kdarapu891693b2019-09-16 12:33:49 +053089
Kent Hagermanf1db18b2020-07-08 13:38:15 -040090 mcp.Devices["onu1"] = &voltha.Device{
kdarapu891693b2019-09-16 12:33:49 +053091 Id: "1",
92 Root: false,
93 ParentId: "olt",
94 ParentPortNo: 1,
95
kdarapu891693b2019-09-16 12:33:49 +053096 OperStatus: 4,
97 ProxyAddress: &voltha.Device_ProxyAddress{
98 OnuId: 1,
99 ChannelId: 1,
100 ChannelGroupId: 1,
101 },
102 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530103 PmConfigs: &voltha.PmConfigs{
104 DefaultFreq: 10,
105 Id: "olt",
106 FreqOverride: false,
107 Grouped: false,
108 Metrics: pm,
109 },
kdarapu891693b2019-09-16 12:33:49 +0530110 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400111 mcp.DevicePorts["onu1"] = []*voltha.Port{
112 {PortNo: 1, Label: "pon"},
113 {PortNo: 2, Label: "uni"},
114 }
115
kdarapu891693b2019-09-16 12:33:49 +0530116 mcp.Devices["onu2"] = &voltha.Device{
117 Id: "2",
118 Root: false,
119 ParentId: "olt",
120 OperStatus: 2,
kdarapu891693b2019-09-16 12:33:49 +0530121
122 ParentPortNo: 1,
123
124 ProxyAddress: &voltha.Device_ProxyAddress{
125 OnuId: 2,
126 ChannelId: 1,
127 ChannelGroupId: 1,
128 },
129 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530130 PmConfigs: &voltha.PmConfigs{
131 DefaultFreq: 10,
132 Id: "olt",
133 FreqOverride: false,
134 Grouped: false,
135 Metrics: pm,
136 },
kdarapu891693b2019-09-16 12:33:49 +0530137 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400138 mcp.DevicePorts["onu2"] = []*voltha.Port{
139 {PortNo: 1, Label: "pon"},
140 {PortNo: 2, Label: "uni"},
141 }
kdarapu891693b2019-09-16 12:33:49 +0530142 return &mcp
143}
144func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530145 device := &voltha.Device{
146 Id: "olt",
147 Root: true,
148 ParentId: "logical_device",
kdarapu891693b2019-09-16 12:33:49 +0530149 ProxyAddress: &voltha.Device_ProxyAddress{
150 DeviceId: "olt",
151 DeviceType: "onu",
152 ChannelId: 1,
153 ChannelGroupId: 1,
154 },
155 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530156 }
kdarapu891693b2019-09-16 12:33:49 +0530157 cp := newMockCoreProxy()
158 ap := &mocks.MockAdapterProxy{}
159 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800160 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700161 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
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}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700172 dh.deviceInfo = deviceInf
173 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
174 var i uint32
175 for i = 0; i < deviceInf.PonPorts; i++ {
176 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
177 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700178 StoreType: "etcd",
179 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700180 }}
181 dh.resourceMgr[i].InitLocalCache()
182 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700183
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530184 ranges := make(map[string]interface{})
185 sharedIdxByType := make(map[string]string)
186 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
187 sharedIdxByType["ONU_ID"] = "ONU_ID"
188 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
189 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
190 ranges["ONU_ID"] = uint32(0)
191 ranges["GEMPORT_ID"] = uint32(0)
192 ranges["ALLOC_ID"] = uint32(0)
193 ranges["FLOW_ID"] = uint32(0)
194 ranges["onu_id_shared"] = uint32(0)
195 ranges["alloc_id_shared"] = uint32(0)
196 ranges["gemport_id_shared"] = uint32(0)
197 ranges["flow_id_shared"] = uint32(0)
198
Matteo Scandolo84585372021-03-18 14:21:22 -0700199 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700200 ponmgr.DeviceID = "onu-1"
201 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
202 ponmgr.KVStore = &db.Backend{
203 Client: &mocks.MockKVClient{},
204 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700205 ponmgr.KVStoreForConfig = &db.Backend{
206 Client: &mocks.MockKVClient{},
207 }
208 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700209 ponmgr.PonResourceRanges = ranges
210 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700211 ponmgr.Technology = "XGS-PON"
212 for i = 0; i < deviceInf.PonPorts; i++ {
213 dh.resourceMgr[i].PonRsrMgr = ponmgr
214 }
215
npujarec5762e2020-01-01 14:08:48 +0530216 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
217 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700218 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700219 dh.totalPonPorts = NumPonPorts
220 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700221 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700222 dh.flowMgr[i] = &OpenOltFlowMgr{}
223 dh.flowMgr[i].deviceHandler = dh
224 dh.flowMgr[i].ponPortIdx = i
225 dh.flowMgr[i].grpMgr = dh.groupMgr
226 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700227 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
228 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
229 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
230 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
231
Girish Gowdra76a1b092021-07-28 10:07:04 -0700232 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
233
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700234 // Create a slice of buffered channels for handling concurrent flows per ONU.
235 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
236 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, MaxOnusPerPon+1)
237 for j := range dh.flowMgr[i].incomingFlows {
238 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
239 // Spin up a go routine to handling incoming flows (add/remove).
240 // There will be on go routine per ONU.
241 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
242 go dh.flowMgr[i].perOnuFlowHandlerRoutine(dh.flowMgr[i].incomingFlows[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700243 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700244 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700245 }
kdarapu891693b2019-09-16 12:33:49 +0530246 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500247 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530248 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530249 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000250
251 var pmNames = []string{
252 "rx_bytes",
253 "rx_packets",
254 "rx_mcast_packets",
255 "rx_bcast_packets",
256 "tx_bytes",
257 "tx_packets",
258 "tx_mcast_packets",
259 "tx_bcast_packets",
260 }
261
262 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
kdarapu891693b2019-09-16 12:33:49 +0530263 return dh
kdarapu381c6902019-07-31 18:23:16 +0530264}
265
kdarapu891693b2019-09-16 12:33:49 +0530266func negativeDeviceHandler() *DeviceHandler {
267 dh := newMockDeviceHandler()
268 device := dh.device
269 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530270 return dh
271}
kdarapu381c6902019-07-31 18:23:16 +0530272func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000273 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530274 type args struct {
275 host string
276 }
277 tests := []struct {
278 name string
279 args args
280 want string
281 wantErr bool
282 }{
kdarapu891693b2019-09-16 12:33:49 +0530283 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
284 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
285 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
286 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530287 }
288 for _, tt := range tests {
289 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000290 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530291 if (err != nil) != tt.wantErr {
292 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
293 return
294 }
295 if got != tt.want {
296 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
297 }
298 })
299 }
300}
301func Test_macifyIP(t *testing.T) {
302 type args struct {
303 ip net.IP
304 }
305 tests := []struct {
306 name string
307 args args
308 want string
309 }{{
kdarapu891693b2019-09-16 12:33:49 +0530310 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530311 args{ip: net.ParseIP("10.10.10.10")},
312 "00:00:0a:0a:0a:0a",
313 },
314 {
kdarapu891693b2019-09-16 12:33:49 +0530315 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530316 args{ip: net.ParseIP("127.0.0.1")},
317 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530318 },
319 {
320 "macifyIP-3",
321 args{ip: net.ParseIP("127.0.0.1/24")},
322 "",
323 },
324 }
kdarapu381c6902019-07-31 18:23:16 +0530325 for _, tt := range tests {
326 t.Run(tt.name, func(t *testing.T) {
327 if got := macifyIP(tt.args.ip); got != tt.want {
328 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
329 }
330 })
331 }
332}
333
David K. Bainbridge794735f2020-02-11 21:01:37 -0800334func sparseCompare(keys []string, spec, target interface{}) bool {
335 if spec == target {
336 return true
337 }
338 if spec == nil || target == nil {
339 return false
340 }
341 typeSpec := reflect.TypeOf(spec)
342 typeTarget := reflect.TypeOf(target)
343 if typeSpec != typeTarget {
344 return false
345 }
346
347 vSpec := reflect.ValueOf(spec)
348 vTarget := reflect.ValueOf(target)
349 if vSpec.Kind() == reflect.Ptr {
350 vSpec = vSpec.Elem()
351 vTarget = vTarget.Elem()
352 }
353
354 for _, key := range keys {
355 fSpec := vSpec.FieldByName(key)
356 fTarget := vTarget.FieldByName(key)
357 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
358 return false
359 }
360 }
361 return true
362}
363
kdarapu381c6902019-07-31 18:23:16 +0530364func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000365 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530366 dh1 := newMockDeviceHandler()
367 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530368 type args struct {
369 parentPort uint32
370 onuID uint32
371 }
372 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530373 name string
374 devicehandler *DeviceHandler
375 args args
376 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800377 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530378 }{
kdarapu891693b2019-09-16 12:33:49 +0530379 {"GetChildDevice-1", dh1,
380 args{parentPort: 1,
381 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800382 &voltha.Device{
383 Id: "1",
384 ParentId: "olt",
385 ParentPortNo: 1,
386 },
387 nil,
kdarapu891693b2019-09-16 12:33:49 +0530388 },
389 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530390 args{parentPort: 1,
391 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800392 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530393 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530394 },
395 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800396
397 /*
398 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
399 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>)
400 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
401 */
kdarapu381c6902019-07-31 18:23:16 +0530402 for _, tt := range tests {
403 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000404 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800405 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
406 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
407 tt.want, tt.errType, got, reflect.TypeOf(err))
408 return
409 }
kdarapu381c6902019-07-31 18:23:16 +0530410 t.Log("onu device id", got)
411 })
412 }
413}
kdarapu891693b2019-09-16 12:33:49 +0530414
415func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530416 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530417 type args struct {
418 portNum uint32
419 portType voltha.Port_PortType
420 }
421 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800422 name string
423 args args
424 want string
425 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530426 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800427 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
428 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
429 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
430 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
431 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
432 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
433 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530434 }
435 for _, tt := range tests {
436 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800437 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
438 if reflect.TypeOf(err) != tt.errType || got != tt.want {
439 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
440 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530441 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800442
kdarapu891693b2019-09-16 12:33:49 +0530443 })
444 }
445}
446
447func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000448 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530449 dh := newMockDeviceHandler()
450 proxyAddr := dh.device.ProxyAddress
451 body := &ic.InterAdapterOmciMessage{
452 Message: []byte("asdfasdfasdfasdfas"),
453 ProxyAddress: proxyAddr,
454 }
455 body2 := &ic.InterAdapterOmciMessage{
456 Message: []byte("asdfasdfasdfasdfas"),
457 //ProxyAddress: &voltha.Device_ProxyAddress{},
458 }
459 body3 := &ic.InterAdapterTechProfileDownloadMessage{}
460 var marshalledData *any.Any
461 var err error
462
463 if marshalledData, err = ptypes.MarshalAny(body); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700464 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530465 }
466
467 var marshalledData1 *any.Any
468
469 if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700470 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530471 }
472 var marshalledData2 *any.Any
473
474 if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700475 logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
kdarapu891693b2019-09-16 12:33:49 +0530476 }
477 type args struct {
478 msg *ic.InterAdapterMessage
479 }
Thomas Lee S94109f12020-03-03 16:39:29 +0530480 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530481 tests := []struct {
482 name string
483 args args
David K. Bainbridge794735f2020-02-11 21:01:37 -0800484 wantErr reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530485 }{
486 {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
487 Header: &ic.InterAdapterHeader{
488 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800489 Type: ic.InterAdapterMessageType_FLOW_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530490 },
491 Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800492 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530493 {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
494 Header: &ic.InterAdapterHeader{
495 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800496 Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530497 },
498 Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800499 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530500 {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
501 Header: &ic.InterAdapterHeader{
502 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800503 Type: ic.InterAdapterMessageType_OMCI_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530504 },
505 Body: marshalledData,
Thomas Lee S94109f12020-03-03 16:39:29 +0530506 }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
kdarapu891693b2019-09-16 12:33:49 +0530507 {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
508 Header: &ic.InterAdapterHeader{
509 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800510 Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
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-5", args{msg: &ic.InterAdapterMessage{
514 Header: &ic.InterAdapterHeader{
515 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800516 Type: ic.InterAdapterMessageType_METRICS_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530517 }, Body: marshalledData1,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800518 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530519 {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
520 Header: &ic.InterAdapterHeader{
521 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800522 Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
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-7", args{msg: &ic.InterAdapterMessage{
526 Header: &ic.InterAdapterHeader{
527 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800528 Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530529 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800530 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530531 {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
532 Header: &ic.InterAdapterHeader{
533 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800534 Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
kdarapu891693b2019-09-16 12:33:49 +0530535 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800536 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530537 {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
538 Header: &ic.InterAdapterHeader{
539 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800540 Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530541 }, Body: marshalledData,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800542 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530543 {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
544 Header: &ic.InterAdapterHeader{
545 Id: "012345",
David K. Bainbridge794735f2020-02-11 21:01:37 -0800546 Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
kdarapu891693b2019-09-16 12:33:49 +0530547 }, Body: marshalledData2,
David K. Bainbridge794735f2020-02-11 21:01:37 -0800548 }}, invalid},
549 {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
550 Header: &ic.InterAdapterHeader{
551 Id: "012345",
552 Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
553 }, Body: marshalledData2,
554 }}, invalid},
kdarapu891693b2019-09-16 12:33:49 +0530555 }
556 for _, tt := range tests {
557 t.Run(tt.name, func(t *testing.T) {
558
Neha Sharma96b7bf22020-06-15 10:37:32 +0000559 if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530560 t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
561 }
562 })
563 }
564}
565
566func TestDeviceHandler_sendProxiedMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000567 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530568 dh1 := newMockDeviceHandler()
569 dh2 := negativeDeviceHandler()
570 device1 := &voltha.Device{
571 Id: "onu1",
572 Root: false,
573 ParentId: "logical_device",
574 ProxyAddress: &voltha.Device_ProxyAddress{
575 DeviceId: "onu1",
576 DeviceType: "onu",
577 ChannelId: 1,
578 ChannelGroupId: 1,
579 },
580 ConnectStatus: 1,
581 }
582 device2 := device1
583 device2.ConnectStatus = 2
584 iaomciMsg1 := &ic.InterAdapterOmciMessage{
585 ProxyAddress: &voltha.Device_ProxyAddress{
586 DeviceId: "onu2",
587 DeviceType: "onu",
588 ChannelId: 1,
589 ChannelGroupId: 1,
590 //OnuId: 2,
591 },
592 ConnectStatus: 1,
593 }
594 iaomciMsg2 := &ic.InterAdapterOmciMessage{
595 ProxyAddress: &voltha.Device_ProxyAddress{
596 DeviceId: "onu3",
597 DeviceType: "onu",
598 ChannelId: 1,
599 ChannelGroupId: 1,
600 },
601 ConnectStatus: 1,
602 }
603 type args struct {
604 onuDevice *voltha.Device
605 omciMsg *ic.InterAdapterOmciMessage
606 }
607 tests := []struct {
608 name string
609 devicehandler *DeviceHandler
610 args args
611 }{
612 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
613 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.InterAdapterOmciMessage{}}},
614 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
615 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
616 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
617 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.InterAdapterOmciMessage{}}},
618 }
619 for _, tt := range tests {
620 t.Run(tt.name, func(t *testing.T) {
Girish Gowdra7de58ac2021-07-20 15:09:25 -0700621 _ = tt.devicehandler.sendProxiedOmciMessage(ctx, tt.args.onuDevice, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400622 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530623 })
624 }
625}
626
627func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
628 dh1 := newMockDeviceHandler()
629 dh2 := negativeDeviceHandler()
630
631 type args struct {
632 logicalPort uint32
633 packetPayload []byte
634 }
635 tests := []struct {
636 name string
637 devicehandler *DeviceHandler
638 args args
639 }{
640 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
641 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
642 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
643 }
644 for _, tt := range tests {
645 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400646 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
647 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530648 })
649 }
650}
651
652func TestDeviceHandler_DisableDevice(t *testing.T) {
653 dh1 := newMockDeviceHandler()
654 dh2 := negativeDeviceHandler()
655 type args struct {
656 device *voltha.Device
657 }
658 tests := []struct {
659 name string
660 devicehandler *DeviceHandler
661 args args
662 wantErr bool
663 }{
664 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400665 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530666 }
667 for _, tt := range tests {
668 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000669 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530670 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
671 }
672 })
673 }
674}
675
676func TestDeviceHandler_ReenableDevice(t *testing.T) {
677 dh1 := newMockDeviceHandler()
678 dh2 := negativeDeviceHandler()
679 type args struct {
680 device *voltha.Device
681 }
682 tests := []struct {
683 name string
684 devicehandler *DeviceHandler
685 args args
686 wantErr bool
687 }{
688 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
689 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
690 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
691 }
692 for _, tt := range tests {
693 t.Run(tt.name, func(t *testing.T) {
694 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000695 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530696 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
697 }
698 })
699 }
700}
701
702func TestDeviceHandler_RebootDevice(t *testing.T) {
703 dh1 := newMockDeviceHandler()
704 dh2 := newMockDeviceHandler()
705 type args struct {
706 device *voltha.Device
707 }
708 tests := []struct {
709 name string
710 devicehandler *DeviceHandler
711 args args
712 wantErr bool
713 }{
714 // TODO: Add test cases.
715 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
716 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
717 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
718 }
719 for _, tt := range tests {
720 t.Run(tt.name, func(t *testing.T) {
721
Neha Sharma96b7bf22020-06-15 10:37:32 +0000722 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530723 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
724 }
725 })
726 }
727}
728
729func TestDeviceHandler_handleIndication(t *testing.T) {
730 dh1 := newMockDeviceHandler()
731 dh2 := negativeDeviceHandler()
732 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530733 dh3.onus = sync.Map{}
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +0530734 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false))
735 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530736
kdarapu891693b2019-09-16 12:33:49 +0530737 type args struct {
738 indication *oop.Indication
739 }
740 tests := []struct {
741 name string
742 deviceHandler *DeviceHandler
743 args args
744 }{
745 // TODO: Add test cases.
746 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
747 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
748 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
749 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
750 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
751 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
752 {"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")}}}}}},
753 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
754 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
755 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
756 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
757 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
758 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
759 {"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}}}}},
760 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
761 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
762 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
763 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
764
765 // Negative testcases
766 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
767 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
768 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
769 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
770 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
771 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
772 {"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")}}}}}},
773 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
774 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
775 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
776 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
777 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
778 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
779 {"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}}}}},
780 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
781 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
782 //
783 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
784 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
785 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
786 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
787 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
788 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
789 }
790 for _, tt := range tests {
791 t.Run(tt.name, func(t *testing.T) {
792 dh := tt.deviceHandler
npujarec5762e2020-01-01 14:08:48 +0530793 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
794 defer cancel()
795 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530796 })
797 }
798}
799
800func TestDeviceHandler_addPort(t *testing.T) {
801 dh1 := newMockDeviceHandler()
802 dh2 := negativeDeviceHandler()
803 type args struct {
804 intfID uint32
805 portType voltha.Port_PortType
806 state string
807 }
808 tests := []struct {
809 name string
810 devicehandler *DeviceHandler
811 args args
812 }{
813 // State up
814 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
815 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
816 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
817 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
818 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
819 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
820 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
821 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
822 // state discovery
823 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
824 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
825 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
826 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
827 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
828 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
829 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
830 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
831
832 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
833 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
834 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
835 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
836 }
837 for _, tt := range tests {
838 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400839 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
840 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530841 })
842 }
843}
844
845func Test_macAddressToUint32Array(t *testing.T) {
846 type args struct {
847 mac string
848 }
849 tests := []struct {
850 name string
851 args args
852 want []uint32
853 }{
854 // TODO: Add test cases.
855 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
856 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
857 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
858 }
859 for _, tt := range tests {
860 t.Run(tt.name, func(t *testing.T) {
861 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
862 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
863 }
864 })
865 }
866}
867
868func TestDeviceHandler_handleOltIndication(t *testing.T) {
869
870 type args struct {
871 oltIndication *oop.OltIndication
872 }
873 tests := []struct {
874 name string
875 args args
876 }{
877 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
878 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
879 }
880 for _, tt := range tests {
881 t.Run(tt.name, func(t *testing.T) {
882 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530883 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
884 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400885 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
886 t.Error(err)
887 }
kdarapu891693b2019-09-16 12:33:49 +0530888 })
889 }
890}
891
892func TestDeviceHandler_AdoptDevice(t *testing.T) {
893 dh1 := newMockDeviceHandler()
894 dh2 := negativeDeviceHandler()
895 type args struct {
896 device *voltha.Device
897 }
898 tests := []struct {
899 name string
900 devicehandler *DeviceHandler
901 args args
902 }{
903 // TODO: Add test cases.
904 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530905 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530906 }
907 for _, tt := range tests {
908 t.Run(tt.name, func(t *testing.T) {
909 //dh.doStateInit()
910 // context.
911 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530912 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
913 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400914 if err := tt.devicehandler.postInit(ctx); err != nil {
915 t.Error(err)
916 }
kdarapu891693b2019-09-16 12:33:49 +0530917 })
918 }
919}
920
921func TestDeviceHandler_activateONU(t *testing.T) {
922 dh := newMockDeviceHandler()
923 dh1 := negativeDeviceHandler()
924 type args struct {
925 intfID uint32
926 onuID int64
927 serialNum *oop.SerialNumber
928 serialNumber string
929 }
930 tests := []struct {
931 name string
932 devicehandler *DeviceHandler
933 args args
934 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700935 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
936 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
937 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
938 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530939 }
940 for _, tt := range tests {
941 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530942 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
943 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400944 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
945 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530946 })
947 }
948}
949
950func TestDeviceHandler_start(t *testing.T) {
951 dh := newMockDeviceHandler()
952 dh1 := negativeDeviceHandler()
953 dh.start(context.Background())
954 dh.stop(context.Background())
955
956 dh1.start(context.Background())
957 dh1.stop(context.Background())
958
959}
960
961func TestDeviceHandler_PacketOut(t *testing.T) {
962 dh1 := newMockDeviceHandler()
963 dh2 := negativeDeviceHandler()
964 acts := []*ofp.OfpAction{
965 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
966 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
967 fu.Output(1),
968 }
969 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
970 type args struct {
971 egressPortNo int
972 packet *of.OfpPacketOut
973 }
974 tests := []struct {
975 name string
976 devicehandler *DeviceHandler
977 args args
978 wantErr bool
979 }{
980 // TODO: Add test cases.
981 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
982 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
983 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700984 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
985 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
986 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +0530987 }
988 for _, tt := range tests {
989 t.Run(tt.name, func(t *testing.T) {
990 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +0530991 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
992 defer cancel()
993 if err := dh.PacketOut(ctx, tt.args.egressPortNo, tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530994 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
995 }
996 })
997 }
998}
999
1000//
1001func TestDeviceHandler_doStateUp(t *testing.T) {
1002 dh1 := newMockDeviceHandler()
1003 dh2 := newMockDeviceHandler()
1004
Thomas Lee S985938d2020-05-04 11:40:41 +05301005 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301006 dh3 := negativeDeviceHandler()
1007
1008 tests := []struct {
1009 name string
1010 devicehandler *DeviceHandler
1011 wantErr bool
1012 }{
1013 {"dostateup-1", dh1, false},
1014 {"dostateup-2", dh2, false},
1015 {"dostateup-3", dh3, true},
1016 }
1017 for _, tt := range tests {
1018 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301019 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1020 defer cancel()
1021 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301022 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1023 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301024 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301025 })
1026 }
1027}
1028func TestDeviceHandler_doStateDown(t *testing.T) {
1029 dh1 := newMockDeviceHandler()
1030 dh2 := negativeDeviceHandler()
1031 dh3 := newMockDeviceHandler()
1032 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1033 tests := []struct {
1034 name string
1035 devicehandler *DeviceHandler
1036 wantErr bool
1037 }{
1038 {"dostatedown-1", dh1, false},
1039 {"dostatedown-2", dh2, true},
1040 {"dostatedown-2", dh3, true},
1041 }
1042 for _, tt := range tests {
1043 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301044 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1045 defer cancel()
1046 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301047 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001048 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301049 }
1050 })
1051 }
1052}
1053
1054func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1055 dh1 := newMockDeviceHandler()
1056 dh2 := negativeDeviceHandler()
1057 type args struct {
1058 device *voltha.Device
1059 }
1060 tests := []struct {
1061 name string
1062 devicehandler *DeviceHandler
1063 args args
1064 wantErr bool
1065 }{
1066 // TODO: Add test cases.
1067 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1068 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1069 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1070 }
1071 for _, tt := range tests {
1072 t.Run(tt.name, func(t *testing.T) {
1073 dh := tt.devicehandler
1074 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1075 if (err != nil) != tt.wantErr {
1076 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1077 return
1078 }
1079 })
1080 }
1081}
1082
kdarapu891693b2019-09-16 12:33:49 +05301083func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1084
1085 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301086 dh1.discOnus = sync.Map{}
1087 dh1.discOnus.Store("onu1", true)
1088 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301089 dh1.discOnus.Store("onu3", true)
1090 dh1.discOnus.Store("onu4", true)
1091 dh1.onus = sync.Map{}
1092 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true))
1093 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true))
kdarapu891693b2019-09-16 12:33:49 +05301094 dh2 := negativeDeviceHandler()
1095 type args struct {
1096 onuDiscInd *oop.OnuDiscIndication
1097 sn string
1098 }
1099 tests := []struct {
1100 name string
1101 devicehandler *DeviceHandler
1102 args args
1103 }{
1104 // TODO: Add test cases.
1105 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1106 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1107 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1108 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1109 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1110 {"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 +05301111 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1112 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1113 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301114 }
1115 for _, tt := range tests {
1116 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301117 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1118 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001119 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001120 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301121 })
1122 }
1123}
1124
1125func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1126 dh1 := newMockDeviceHandler()
1127 dh2 := negativeDeviceHandler()
1128 tests := []struct {
1129 name string
1130 devicehandler *DeviceHandler
1131
1132 wantErr bool
1133 }{
1134 // TODO: Add test cases.
1135 {"populateDeviceInfo-1", dh1, false},
1136 {"populateDeviceInfo-2", dh1, true},
1137 {"populateDeviceInfo-3", dh1, true},
1138 {"populateDeviceInfo-4", dh1, true},
1139 {"populateDeviceInfo-5", dh2, true},
1140 }
1141 for _, tt := range tests {
1142 t.Run(tt.name, func(t *testing.T) {
1143
Neha Sharma96b7bf22020-06-15 10:37:32 +00001144 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301145 if (err != nil) != tt.wantErr {
1146 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1147 return
1148 }
1149
1150 })
1151 }
1152}
1153
1154func TestDeviceHandler_readIndications(t *testing.T) {
1155 dh1 := newMockDeviceHandler()
1156 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301157 dh3 := newMockDeviceHandler()
1158 dh3.device.AdminState = voltha.AdminState_DISABLED
1159 dh4 := negativeDeviceHandler()
1160 tests := []struct {
1161 name string
1162 devicehandler *DeviceHandler
1163 }{
1164 // TODO: Add test cases.
1165 {"readIndications-1", dh1},
1166 {"readIndications-2", dh2},
1167 {"readIndications-3", dh2},
1168 {"readIndications-4", dh2},
1169 {"readIndications-5", dh2},
1170 {"readIndications-6", dh3},
1171 {"readIndications-7", dh3},
1172 {"readIndications-8", dh4},
1173 }
1174 for _, tt := range tests {
1175 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301176 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1177 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001178 _ = tt.devicehandler.readIndications(ctx)
1179 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301180 })
1181 }
1182}
Naga Manjunath7615e552019-10-11 22:35:47 +05301183
1184func Test_startCollector(t *testing.T) {
1185 type args struct {
1186 dh *DeviceHandler
1187 }
1188 dh := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001189 dh.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301190 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1191 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1192 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1193 }
Naga Manjunath7615e552019-10-11 22:35:47 +05301194 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301195 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1196 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301197 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1198 dh.portStats.Device = dh
1199 for i := 0; i < 16; i++ {
1200 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1201 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301202 dh1 := newMockDeviceHandler()
Kent Hagermanf1db18b2020-07-08 13:38:15 -04001203 dh1.coreProxy.(*mocks.MockCoreProxy).DevicePorts[dh.device.Id] = []*voltha.Port{}
Naga Manjunath7615e552019-10-11 22:35:47 +05301204 tests := []struct {
1205 name string
1206 args args
1207 }{
1208 // TODO: Add test cases.
1209 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301210 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301211 }
1212 for _, tt := range tests {
1213 t.Run(tt.name, func(t *testing.T) {
1214 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301215 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301216 tt.args.dh.stopCollector <- true
1217 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001218 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301219 })
1220 }
1221}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001222
1223func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1224
1225 // olt disconnect (not reboot)
1226 dh1 := newMockDeviceHandler()
1227 dh1.adapterPreviouslyConnected = false
1228 dh1.agentPreviouslyConnected = true
1229
1230 // adapter restart
1231 dh2 := newMockDeviceHandler()
1232 dh2.Client = &mocks.MockOpenoltClient{}
1233 dh2.adapterPreviouslyConnected = true
1234 dh2.agentPreviouslyConnected = true
1235
1236 // first connection or olt restart
1237 dh3 := newMockDeviceHandler()
1238 dh3.Client = &mocks.MockOpenoltClient{}
1239 dh3.adapterPreviouslyConnected = false
1240 dh3.agentPreviouslyConnected = false
1241
1242 // olt and adapter restart at the same time (first case)
1243 dh4 := newMockDeviceHandler()
1244 dh4.Client = &mocks.MockOpenoltClient{}
1245 dh4.adapterPreviouslyConnected = true
1246 dh4.agentPreviouslyConnected = false
1247
1248 // adapter restart and olt disconnect at the same time
1249 dh5 := newMockDeviceHandler()
1250 dh5.Client = &mocks.MockOpenoltClient{}
1251 dh5.adapterPreviouslyConnected = true
1252 dh5.agentPreviouslyConnected = true
1253
1254 tests := []struct {
1255 name string
1256 devicehandler *DeviceHandler
1257 expectedRestart bool
1258 wantErr bool
1259 }{
1260 {"dostateup-1", dh1, true, false},
1261 {"dostateup-2", dh2, false, false},
1262 {"dostateup-3", dh3, false, false},
1263 {"dostateup-4", dh4, true, false},
1264 {"dostateup-5", dh5, false, false},
1265 }
1266 for _, tt := range tests {
1267 t.Run(tt.name, func(t *testing.T) {
1268 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1269 defer cancel()
1270 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1271 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1272 }
1273 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1274 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1275 if tt.expectedRestart != isRestarted {
1276 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1277 }
1278 })
1279 }
1280}