blob: aa534d009b15e1b887b7b590ba3ebcf66fc8221a [file] [log] [blame]
kdarapu381c6902019-07-31 18:23:16 +05301/*
2 * Copyright 2018-present Open Networking Foundation
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Scott Bakerdbd960e2020-02-28 08:57:51 -080017//Package core provides the utility for olt devices, flows and statistics
18package core
kdarapu381c6902019-07-31 18:23:16 +053019
20import (
kdarapu891693b2019-09-16 12:33:49 +053021 "context"
kdarapu381c6902019-07-31 18:23:16 +053022 "net"
kdarapu891693b2019-09-16 12:33:49 +053023 "reflect"
Naga Manjunatha8dc9372019-10-31 23:01:18 +053024 "sync"
kdarapu381c6902019-07-31 18:23:16 +053025 "testing"
Naga Manjunath7615e552019-10-11 22:35:47 +053026 "time"
27
khenaidoo106c61a2021-08-11 18:05:46 -040028 conf "github.com/opencord/voltha-lib-go/v7/pkg/config"
29 vgrpc "github.com/opencord/voltha-lib-go/v7/pkg/grpc"
30
31 "github.com/opencord/voltha-lib-go/v7/pkg/db"
32 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
33 "github.com/opencord/voltha-lib-go/v7/pkg/pmmetrics"
34 ponrmgr "github.com/opencord/voltha-lib-go/v7/pkg/ponresourcemanager"
kesavand494c2082020-08-31 11:16:12 +053035 "github.com/opencord/voltha-openolt-adapter/internal/pkg/config"
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"
khenaidoo106c61a2021-08-11 18:05:46 -040039 ic "github.com/opencord/voltha-protos/v5/go/inter_container"
40 of "github.com/opencord/voltha-protos/v5/go/openflow_13"
41 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
42 oop "github.com/opencord/voltha-protos/v5/go/openolt"
43 "github.com/opencord/voltha-protos/v5/go/voltha"
kdarapu381c6902019-07-31 18:23:16 +053044)
45
Girish Gowdra9602eb42020-09-09 15:50:39 -070046const (
Girish Gowdrafb3d6102020-10-16 16:32:36 -070047 NumPonPorts = 16
Girish Gowdra9602eb42020-09-09 15:50:39 -070048 OnuIDStart = 1
49 OnuIDEnd = 32
50 AllocIDStart = 1
51 AllocIDEnd = 10
52 GemIDStart = 1
53 GemIDEnd = 10
54 FlowIDStart = 1
55 FlowIDEnd = 10
56)
57
khenaidoo106c61a2021-08-11 18:05:46 -040058func newMockOnuInterAdapterService() *mocks.MockOnuInterAdapterService {
59 return &mocks.MockOnuInterAdapterService{}
60}
61
62func newMockCoreService() *mocks.MockCoreService {
63 mcp := mocks.MockCoreService{
Kent Hagermanf1db18b2020-07-08 13:38:15 -040064 Devices: make(map[string]*voltha.Device),
65 DevicePorts: make(map[string][]*voltha.Port),
66 }
Naga Manjunath7615e552019-10-11 22:35:47 +053067 var pm []*voltha.PmConfig
kdarapu891693b2019-09-16 12:33:49 +053068 mcp.Devices["olt"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040069 Id: "olt",
70 Root: true,
71 ParentId: "logical_device",
72 ParentPortNo: 1,
73 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +053074 ProxyAddress: &voltha.Device_ProxyAddress{
75 DeviceId: "olt",
76 DeviceType: "onu",
77 ChannelId: 1,
78 ChannelGroupId: 1,
79 },
80 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +053081 PmConfigs: &voltha.PmConfigs{
82 DefaultFreq: 10,
83 Id: "olt",
84 FreqOverride: false,
85 Grouped: false,
86 Metrics: pm,
87 },
kdarapu891693b2019-09-16 12:33:49 +053088 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -040089 mcp.DevicePorts["olt"] = []*voltha.Port{
90 {PortNo: 1, Label: "pon"},
91 {PortNo: 2, Label: "nni"},
92 }
kdarapu891693b2019-09-16 12:33:49 +053093
Kent Hagermanf1db18b2020-07-08 13:38:15 -040094 mcp.Devices["onu1"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -040095 Id: "1",
96 Root: false,
97 ParentId: "olt",
98 ParentPortNo: 1,
99 AdapterEndpoint: "mock-onu-endpoint",
100 OperStatus: 4,
kdarapu891693b2019-09-16 12:33:49 +0530101 ProxyAddress: &voltha.Device_ProxyAddress{
102 OnuId: 1,
103 ChannelId: 1,
104 ChannelGroupId: 1,
105 },
106 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530107 PmConfigs: &voltha.PmConfigs{
108 DefaultFreq: 10,
109 Id: "olt",
110 FreqOverride: false,
111 Grouped: false,
112 Metrics: pm,
113 },
kdarapu891693b2019-09-16 12:33:49 +0530114 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400115 mcp.DevicePorts["onu1"] = []*voltha.Port{
116 {PortNo: 1, Label: "pon"},
117 {PortNo: 2, Label: "uni"},
118 }
119
kdarapu891693b2019-09-16 12:33:49 +0530120 mcp.Devices["onu2"] = &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400121 Id: "2",
122 Root: false,
123 ParentId: "olt",
124 OperStatus: 2,
125 AdapterEndpoint: "mock-onu-endpoint",
126 ParentPortNo: 1,
kdarapu891693b2019-09-16 12:33:49 +0530127
128 ProxyAddress: &voltha.Device_ProxyAddress{
129 OnuId: 2,
130 ChannelId: 1,
131 ChannelGroupId: 1,
132 },
133 ConnectStatus: 1,
Naga Manjunath7615e552019-10-11 22:35:47 +0530134 PmConfigs: &voltha.PmConfigs{
135 DefaultFreq: 10,
136 Id: "olt",
137 FreqOverride: false,
138 Grouped: false,
139 Metrics: pm,
140 },
kdarapu891693b2019-09-16 12:33:49 +0530141 }
Kent Hagermanf1db18b2020-07-08 13:38:15 -0400142 mcp.DevicePorts["onu2"] = []*voltha.Port{
143 {PortNo: 1, Label: "pon"},
144 {PortNo: 2, Label: "uni"},
145 }
kdarapu891693b2019-09-16 12:33:49 +0530146 return &mcp
147}
148func newMockDeviceHandler() *DeviceHandler {
kdarapu381c6902019-07-31 18:23:16 +0530149 device := &voltha.Device{
khenaidoo106c61a2021-08-11 18:05:46 -0400150 Id: "olt",
151 Root: true,
152 ParentId: "logical_device",
153 AdapterEndpoint: "mock-olt-endpoint",
kdarapu891693b2019-09-16 12:33:49 +0530154 ProxyAddress: &voltha.Device_ProxyAddress{
155 DeviceId: "olt",
156 DeviceType: "onu",
157 ChannelId: 1,
158 ChannelGroupId: 1,
159 },
160 ConnectStatus: 1,
kdarapu381c6902019-07-31 18:23:16 +0530161 }
khenaidoo106c61a2021-08-11 18:05:46 -0400162 mcs := newMockCoreService()
163 cc := mocks.NewMockCoreClient(mcs)
kdarapu891693b2019-09-16 12:33:49 +0530164 ep := &mocks.MockEventProxy{}
Matteo Scandolodfa7a972020-11-06 13:03:40 -0800165 cm := &conf.ConfigManager{}
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700166 cm.Backend = &db.Backend{StoreType: "etcd", Client: &mocks.MockKVClient{}}
kesavand494c2082020-08-31 11:16:12 +0530167 cfg := &config.AdapterFlags{OmccEncryption: true}
khenaidoo106c61a2021-08-11 18:05:46 -0400168 openOLT := &OpenOLT{eventProxy: ep, config: cfg}
169 dh := NewDeviceHandler(cc, ep, device, openOLT, cm, cfg)
Girish Gowdra38d533d2020-03-30 20:38:51 -0700170 oopRanges := []*oop.DeviceInfo_DeviceResourceRanges{{
Girish Gowdrafb3d6102020-10-16 16:32:36 -0700171 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 -0700172 Technology: "xgs-pon",
173 Pools: []*oop.DeviceInfo_DeviceResourceRanges_Pool{{}},
174 }}
175
Girish Gowdra9602eb42020-09-09 15:50:39 -0700176 deviceInf := &oop.DeviceInfo{Vendor: "openolt", Ranges: oopRanges, Model: "openolt", DeviceId: dh.device.Id, PonPorts: NumPonPorts}
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700177 dh.deviceInfo = deviceInf
khenaidoo106c61a2021-08-11 18:05:46 -0400178 dh.device = device
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700179 dh.resourceMgr = make([]*resourcemanager.OpenOltResourceMgr, deviceInf.PonPorts)
180 var i uint32
181 for i = 0; i < deviceInf.PonPorts; i++ {
182 dh.resourceMgr[i] = &resourcemanager.OpenOltResourceMgr{DeviceID: dh.device.Id, DeviceType: dh.device.Type, DevInfo: deviceInf,
183 KVStore: &db.Backend{
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700184 StoreType: "etcd",
185 Client: &mocks.MockKVClient{},
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700186 }}
187 dh.resourceMgr[i].InitLocalCache()
188 }
Girish Gowdra38d533d2020-03-30 20:38:51 -0700189
Abhilash Laxmeshwarab0bd522019-10-21 15:05:15 +0530190 ranges := make(map[string]interface{})
191 sharedIdxByType := make(map[string]string)
192 sharedIdxByType["ALLOC_ID"] = "ALLOC_ID"
193 sharedIdxByType["ONU_ID"] = "ONU_ID"
194 sharedIdxByType["GEMPORT_ID"] = "GEMPORT_ID"
195 sharedIdxByType["FLOW_ID"] = "FLOW_ID"
196 ranges["ONU_ID"] = uint32(0)
197 ranges["GEMPORT_ID"] = uint32(0)
198 ranges["ALLOC_ID"] = uint32(0)
199 ranges["FLOW_ID"] = uint32(0)
200 ranges["onu_id_shared"] = uint32(0)
201 ranges["alloc_id_shared"] = uint32(0)
202 ranges["gemport_id_shared"] = uint32(0)
203 ranges["flow_id_shared"] = uint32(0)
204
Matteo Scandolo84585372021-03-18 14:21:22 -0700205 ponmgr := &ponrmgr.PONResourceManager{}
Matteo Scandolo84585372021-03-18 14:21:22 -0700206 ponmgr.DeviceID = "onu-1"
207 ponmgr.IntfIDs = []uint32{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
208 ponmgr.KVStore = &db.Backend{
209 Client: &mocks.MockKVClient{},
210 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700211 ponmgr.KVStoreForConfig = &db.Backend{
212 Client: &mocks.MockKVClient{},
213 }
214 ponmgr.Backend = "etcd"
Matteo Scandolo84585372021-03-18 14:21:22 -0700215 ponmgr.PonResourceRanges = ranges
216 ponmgr.SharedIdxByType = sharedIdxByType
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700217 ponmgr.Technology = "XGS-PON"
218 for i = 0; i < deviceInf.PonPorts; i++ {
219 dh.resourceMgr[i].PonRsrMgr = ponmgr
220 }
221
npujarec5762e2020-01-01 14:08:48 +0530222 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
223 defer cancel()
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700224 dh.groupMgr = NewGroupManager(ctx, dh, dh.resourceMgr[0])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700225 dh.totalPonPorts = NumPonPorts
226 dh.flowMgr = make([]*OpenOltFlowMgr, dh.totalPonPorts)
Girish Gowdra8a0bdcd2021-05-13 12:31:04 -0700227 for i = 0; i < dh.totalPonPorts; i++ {
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700228 dh.flowMgr[i] = &OpenOltFlowMgr{}
229 dh.flowMgr[i].deviceHandler = dh
230 dh.flowMgr[i].ponPortIdx = i
231 dh.flowMgr[i].grpMgr = dh.groupMgr
232 dh.flowMgr[i].resourceMgr = dh.resourceMgr[i]
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700233 dh.flowMgr[i].techprofile = mocks.MockTechProfile{}
234 dh.flowMgr[i].gemToFlowIDs = make(map[uint32][]uint64)
235 dh.flowMgr[i].packetInGemPort = make(map[resourcemanager.PacketInInfoKey]uint32)
236 dh.flowMgr[i].flowIDToGems = make(map[uint64][]uint32)
237
Girish Gowdra76a1b092021-07-28 10:07:04 -0700238 dh.resourceMgr[i].TechprofileRef = dh.flowMgr[i].techprofile
239
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700240 // Create a slice of buffered channels for handling concurrent flows per ONU.
241 // The additional entry (+1) is to handle the NNI trap flows on a separate channel from individual ONUs channel
242 dh.flowMgr[i].incomingFlows = make([]chan flowControlBlock, MaxOnusPerPon+1)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700243 dh.flowMgr[i].stopFlowHandlerRoutine = make([]chan bool, MaxOnusPerPon+1)
244 dh.flowMgr[i].flowHandlerRoutineActive = make([]bool, MaxOnusPerPon+1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700245 for j := range dh.flowMgr[i].incomingFlows {
246 dh.flowMgr[i].incomingFlows[j] = make(chan flowControlBlock, maxConcurrentFlowsPerOnu)
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700247 dh.flowMgr[i].stopFlowHandlerRoutine[j] = make(chan bool, 1)
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700248 // Spin up a go routine to handling incoming flows (add/remove).
249 // There will be on go routine per ONU.
250 // This routine will be blocked on the flowMgr.incomingFlows[onu-id] channel for incoming flows.
Girish Gowdra4736e5c2021-08-25 15:19:10 -0700251 dh.flowMgr[i].flowHandlerRoutineActive[j] = true
252 go dh.flowMgr[i].perOnuFlowHandlerRoutine(j, dh.flowMgr[i].incomingFlows[j], dh.flowMgr[i].stopFlowHandlerRoutine[j])
Girish Gowdra9602eb42020-09-09 15:50:39 -0700253 }
Girish Gowdra4c3d4602021-07-22 16:33:37 -0700254 dh.flowMgr[i].onuGemInfoMap = make(map[uint32]*resourcemanager.OnuGemInfo)
Girish Gowdra9602eb42020-09-09 15:50:39 -0700255 }
kdarapu891693b2019-09-16 12:33:49 +0530256 dh.Client = &mocks.MockOpenoltClient{}
kesavand39e0aa32020-01-28 20:58:50 -0500257 dh.eventMgr = &OpenOltEventMgr{eventProxy: &mocks.MockEventProxy{}, handler: dh}
kdarapu891693b2019-09-16 12:33:49 +0530258 dh.transitionMap = &TransitionMap{}
Naga Manjunath7615e552019-10-11 22:35:47 +0530259 dh.portStats = &OpenOltStatisticsMgr{}
Amit Ghoshd4cbe482019-11-21 12:07:14 +0000260
261 var pmNames = []string{
262 "rx_bytes",
263 "rx_packets",
264 "rx_mcast_packets",
265 "rx_bcast_packets",
266 "tx_bytes",
267 "tx_packets",
268 "tx_mcast_packets",
269 "tx_bcast_packets",
270 }
271
272 dh.metrics = pmmetrics.NewPmMetrics(device.Id, pmmetrics.Frequency(2), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
khenaidoo106c61a2021-08-11 18:05:46 -0400273
274 // Set the children endpoints
275 dh.childAdapterClients = map[string]*vgrpc.Client{
276 "mock-onu-endpoint": mocks.NewMockChildAdapterClient(newMockOnuInterAdapterService()),
277 }
kdarapu891693b2019-09-16 12:33:49 +0530278 return dh
kdarapu381c6902019-07-31 18:23:16 +0530279}
280
kdarapu891693b2019-09-16 12:33:49 +0530281func negativeDeviceHandler() *DeviceHandler {
282 dh := newMockDeviceHandler()
283 device := dh.device
284 device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +0530285 return dh
286}
kdarapu381c6902019-07-31 18:23:16 +0530287func Test_generateMacFromHost(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000288 ctx := context.Background()
kdarapu381c6902019-07-31 18:23:16 +0530289 type args struct {
290 host string
291 }
292 tests := []struct {
293 name string
294 args args
295 want string
296 wantErr bool
297 }{
kdarapu891693b2019-09-16 12:33:49 +0530298 {"generateMacFromHost-1", args{host: "localhost"}, "00:00:7f:00:00:01", false},
299 {"generateMacFromHost-2", args{host: "10.10.10.10"}, "00:00:0a:0a:0a:0a", false},
300 //{"generateMacFromHost-3", args{host: "google.com"}, "00:00:d8:3a:c8:8e", false},
301 {"generateMacFromHost-4", args{host: "testing3"}, "", true},
kdarapu381c6902019-07-31 18:23:16 +0530302 }
303 for _, tt := range tests {
304 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000305 got, err := generateMacFromHost(ctx, tt.args.host)
kdarapu381c6902019-07-31 18:23:16 +0530306 if (err != nil) != tt.wantErr {
307 t.Errorf("generateMacFromHost() error = %v, wantErr %v", err, tt.wantErr)
308 return
309 }
310 if got != tt.want {
311 t.Errorf("generateMacFromHost() = %v, want %v", got, tt.want)
312 }
313 })
314 }
315}
316func Test_macifyIP(t *testing.T) {
317 type args struct {
318 ip net.IP
319 }
320 tests := []struct {
321 name string
322 args args
323 want string
324 }{{
kdarapu891693b2019-09-16 12:33:49 +0530325 "macifyIP-1",
kdarapu381c6902019-07-31 18:23:16 +0530326 args{ip: net.ParseIP("10.10.10.10")},
327 "00:00:0a:0a:0a:0a",
328 },
329 {
kdarapu891693b2019-09-16 12:33:49 +0530330 "macifyIP-2",
kdarapu381c6902019-07-31 18:23:16 +0530331 args{ip: net.ParseIP("127.0.0.1")},
332 "00:00:7f:00:00:01",
kdarapu891693b2019-09-16 12:33:49 +0530333 },
334 {
335 "macifyIP-3",
336 args{ip: net.ParseIP("127.0.0.1/24")},
337 "",
338 },
339 }
kdarapu381c6902019-07-31 18:23:16 +0530340 for _, tt := range tests {
341 t.Run(tt.name, func(t *testing.T) {
342 if got := macifyIP(tt.args.ip); got != tt.want {
343 t.Errorf("macifyIP() = %v, want %v", got, tt.want)
344 }
345 })
346 }
347}
348
David K. Bainbridge794735f2020-02-11 21:01:37 -0800349func sparseCompare(keys []string, spec, target interface{}) bool {
350 if spec == target {
351 return true
352 }
353 if spec == nil || target == nil {
354 return false
355 }
356 typeSpec := reflect.TypeOf(spec)
357 typeTarget := reflect.TypeOf(target)
358 if typeSpec != typeTarget {
359 return false
360 }
361
362 vSpec := reflect.ValueOf(spec)
363 vTarget := reflect.ValueOf(target)
364 if vSpec.Kind() == reflect.Ptr {
365 vSpec = vSpec.Elem()
366 vTarget = vTarget.Elem()
367 }
368
369 for _, key := range keys {
370 fSpec := vSpec.FieldByName(key)
371 fTarget := vTarget.FieldByName(key)
372 if !reflect.DeepEqual(fSpec.Interface(), fTarget.Interface()) {
373 return false
374 }
375 }
376 return true
377}
378
kdarapu381c6902019-07-31 18:23:16 +0530379func TestDeviceHandler_GetChildDevice(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000380 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530381 dh1 := newMockDeviceHandler()
382 dh2 := negativeDeviceHandler()
kdarapu381c6902019-07-31 18:23:16 +0530383 type args struct {
384 parentPort uint32
385 onuID uint32
386 }
387 tests := []struct {
kdarapu891693b2019-09-16 12:33:49 +0530388 name string
389 devicehandler *DeviceHandler
390 args args
391 want *voltha.Device
David K. Bainbridge794735f2020-02-11 21:01:37 -0800392 errType reflect.Type
kdarapu381c6902019-07-31 18:23:16 +0530393 }{
kdarapu891693b2019-09-16 12:33:49 +0530394 {"GetChildDevice-1", dh1,
395 args{parentPort: 1,
396 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800397 &voltha.Device{
398 Id: "1",
399 ParentId: "olt",
400 ParentPortNo: 1,
401 },
402 nil,
kdarapu891693b2019-09-16 12:33:49 +0530403 },
404 {"GetChildDevice-2", dh2,
kdarapu381c6902019-07-31 18:23:16 +0530405 args{parentPort: 1,
406 onuID: 1},
David K. Bainbridge794735f2020-02-11 21:01:37 -0800407 nil,
Thomas Lee S94109f12020-03-03 16:39:29 +0530408 reflect.TypeOf(&olterrors.ErrNotFound{}),
kdarapu381c6902019-07-31 18:23:16 +0530409 },
410 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800411
412 /*
413 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-1 (0.00s)
414 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>)
415 --- FAIL: TestDeviceHandler_GetChildDevice/GetChildDevice-2 (0.00s)
416 */
kdarapu381c6902019-07-31 18:23:16 +0530417 for _, tt := range tests {
418 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000419 got, err := tt.devicehandler.GetChildDevice(ctx, tt.args.parentPort, tt.args.onuID)
David K. Bainbridge794735f2020-02-11 21:01:37 -0800420 if reflect.TypeOf(err) != tt.errType || !sparseCompare([]string{"Id", "ParentId", "ParentPortNo"}, tt.want, got) {
421 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
422 tt.want, tt.errType, got, reflect.TypeOf(err))
423 return
424 }
kdarapu381c6902019-07-31 18:23:16 +0530425 t.Log("onu device id", got)
426 })
427 }
428}
kdarapu891693b2019-09-16 12:33:49 +0530429
430func TestGetportLabel(t *testing.T) {
Thomas Lee S94109f12020-03-03 16:39:29 +0530431 invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
kdarapu891693b2019-09-16 12:33:49 +0530432 type args struct {
433 portNum uint32
434 portType voltha.Port_PortType
435 }
436 tests := []struct {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800437 name string
438 args args
439 want string
440 errType reflect.Type
kdarapu891693b2019-09-16 12:33:49 +0530441 }{
David K. Bainbridge794735f2020-02-11 21:01:37 -0800442 {"GetportLabel-1", args{portNum: 0, portType: 0}, "", invalid},
443 {"GetportLabel-2", args{portNum: 1, portType: 1}, "nni-1", nil},
444 {"GetportLabel-3", args{portNum: 2, portType: 2}, "", invalid},
445 {"GetportLabel-4", args{portNum: 3, portType: 3}, "pon-3", nil},
446 {"GetportLabel-5", args{portNum: 4, portType: 4}, "", invalid},
447 {"GetportLabel-6", args{portNum: 5, portType: 5}, "", invalid},
448 {"GetportLabel-7", args{portNum: 6, portType: 6}, "", invalid},
kdarapu891693b2019-09-16 12:33:49 +0530449 }
450 for _, tt := range tests {
451 t.Run(tt.name, func(t *testing.T) {
David K. Bainbridge794735f2020-02-11 21:01:37 -0800452 got, err := GetportLabel(tt.args.portNum, tt.args.portType)
453 if reflect.TypeOf(err) != tt.errType || got != tt.want {
454 t.Errorf("GetportLabel() => want=(%v, %v) got=(%v, %v)",
455 tt.want, tt.errType, got, reflect.TypeOf(err))
kdarapu891693b2019-09-16 12:33:49 +0530456 }
David K. Bainbridge794735f2020-02-11 21:01:37 -0800457
kdarapu891693b2019-09-16 12:33:49 +0530458 })
459 }
460}
461
khenaidoo106c61a2021-08-11 18:05:46 -0400462// func TestDeviceHandler_ProcessInterAdapterMessage(t *testing.T) {
463// ctx := context.Background()
464// dh := newMockDeviceHandler()
465// proxyAddr := dh.device.ProxyAddress
466// body := &ic.InterAdapterOmciMessage{
467// Message: []byte("asdfasdfasdfasdfas"),
468// ProxyAddress: proxyAddr,
469// }
470// body2 := &ic.InterAdapterOmciMessage{
471// Message: []byte("asdfasdfasdfasdfas"),
472// //ProxyAddress: &voltha.Device_ProxyAddress{},
473// }
474// body3 := &ic.InterAdapterTechProfileDownloadMessage{}
475// var marshalledData *any.Any
476// var err error
kdarapu891693b2019-09-16 12:33:49 +0530477
khenaidoo106c61a2021-08-11 18:05:46 -0400478// if marshalledData, err = ptypes.MarshalAny(body); err != nil {
479// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
480// }
kdarapu891693b2019-09-16 12:33:49 +0530481
khenaidoo106c61a2021-08-11 18:05:46 -0400482// var marshalledData1 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530483
khenaidoo106c61a2021-08-11 18:05:46 -0400484// if marshalledData1, err = ptypes.MarshalAny(body2); err != nil {
485// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
486// }
487// var marshalledData2 *any.Any
kdarapu891693b2019-09-16 12:33:49 +0530488
khenaidoo106c61a2021-08-11 18:05:46 -0400489// if marshalledData2, err = ptypes.MarshalAny(body3); err != nil {
490// logger.Errorw(ctx, "cannot-marshal-request", log.Fields{"err": err})
491// }
492// type args struct {
493// msg *ic.InterAdapterMessage
494// }
495// invalid := reflect.TypeOf(&olterrors.ErrInvalidValue{})
496// tests := []struct {
497// name string
498// args args
499// wantErr reflect.Type
500// }{
501// {"ProcessInterAdapterMessage-1", args{msg: &ic.InterAdapterMessage{
502// Header: &ic.InterAdapterHeader{
503// Id: "012345",
504// Type: ic.InterAdapterMessageType_FLOW_REQUEST,
505// },
506// Body: marshalledData,
507// }}, invalid},
508// {"ProcessInterAdapterMessage-2", args{msg: &ic.InterAdapterMessage{
509// Header: &ic.InterAdapterHeader{
510// Id: "012345",
511// Type: ic.InterAdapterMessageType_FLOW_RESPONSE,
512// },
513// Body: marshalledData1,
514// }}, invalid},
515// {"ProcessInterAdapterMessage-3", args{msg: &ic.InterAdapterMessage{
516// Header: &ic.InterAdapterHeader{
517// Id: "012345",
518// Type: ic.InterAdapterMessageType_OMCI_REQUEST,
519// },
520// Body: marshalledData,
521// }}, reflect.TypeOf(&olterrors.ErrCommunication{})},
522// {"ProcessInterAdapterMessage-4", args{msg: &ic.InterAdapterMessage{
523// Header: &ic.InterAdapterHeader{
524// Id: "012345",
525// Type: ic.InterAdapterMessageType_OMCI_RESPONSE,
526// }, Body: marshalledData,
527// }}, invalid},
528// {"ProcessInterAdapterMessage-5", args{msg: &ic.InterAdapterMessage{
529// Header: &ic.InterAdapterHeader{
530// Id: "012345",
531// Type: ic.InterAdapterMessageType_METRICS_REQUEST,
532// }, Body: marshalledData1,
533// }}, invalid},
534// {"ProcessInterAdapterMessage-6", args{msg: &ic.InterAdapterMessage{
535// Header: &ic.InterAdapterHeader{
536// Id: "012345",
537// Type: ic.InterAdapterMessageType_METRICS_RESPONSE,
538// }, Body: marshalledData,
539// }}, invalid},
540// {"ProcessInterAdapterMessage-7", args{msg: &ic.InterAdapterMessage{
541// Header: &ic.InterAdapterHeader{
542// Id: "012345",
543// Type: ic.InterAdapterMessageType_ONU_IND_REQUEST,
544// }, Body: marshalledData,
545// }}, invalid},
546// {"ProcessInterAdapterMessage-8", args{msg: &ic.InterAdapterMessage{
547// Header: &ic.InterAdapterHeader{
548// Id: "012345",
549// Type: ic.InterAdapterMessageType_ONU_IND_RESPONSE,
550// }, Body: marshalledData,
551// }}, invalid},
552// {"ProcessInterAdapterMessage-9", args{msg: &ic.InterAdapterMessage{
553// Header: &ic.InterAdapterHeader{
554// Id: "012345",
555// Type: ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
556// }, Body: marshalledData,
557// }}, invalid},
558// {"ProcessInterAdapterMessage-10", args{msg: &ic.InterAdapterMessage{
559// Header: &ic.InterAdapterHeader{
560// Id: "012345",
561// Type: ic.InterAdapterMessageType_DELETE_GEM_PORT_REQUEST,
562// }, Body: marshalledData2,
563// }}, invalid},
564// {"ProcessInterAdapterMessage-11", args{msg: &ic.InterAdapterMessage{
565// Header: &ic.InterAdapterHeader{
566// Id: "012345",
567// Type: ic.InterAdapterMessageType_DELETE_TCONT_REQUEST,
568// }, Body: marshalledData2,
569// }}, invalid},
570// }
571// for _, tt := range tests {
572// t.Run(tt.name, func(t *testing.T) {
kdarapu891693b2019-09-16 12:33:49 +0530573
khenaidoo106c61a2021-08-11 18:05:46 -0400574// if err := dh.ProcessInterAdapterMessage(ctx, tt.args.msg); reflect.TypeOf(err) != tt.wantErr {
575// t.Errorf("DeviceHandler.ProcessInterAdapterMessage() error = %v, wantErr %v", err, tt.wantErr)
576// }
577// })
578// }
579// }
kdarapu891693b2019-09-16 12:33:49 +0530580
khenaidoo106c61a2021-08-11 18:05:46 -0400581func TestDeviceHandler_ProxyOmciMessage(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000582 ctx := context.Background()
kdarapu891693b2019-09-16 12:33:49 +0530583 dh1 := newMockDeviceHandler()
584 dh2 := negativeDeviceHandler()
585 device1 := &voltha.Device{
586 Id: "onu1",
587 Root: false,
588 ParentId: "logical_device",
589 ProxyAddress: &voltha.Device_ProxyAddress{
590 DeviceId: "onu1",
591 DeviceType: "onu",
592 ChannelId: 1,
593 ChannelGroupId: 1,
594 },
595 ConnectStatus: 1,
596 }
597 device2 := device1
598 device2.ConnectStatus = 2
khenaidoo106c61a2021-08-11 18:05:46 -0400599 iaomciMsg1 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530600 ProxyAddress: &voltha.Device_ProxyAddress{
601 DeviceId: "onu2",
602 DeviceType: "onu",
603 ChannelId: 1,
604 ChannelGroupId: 1,
kdarapu891693b2019-09-16 12:33:49 +0530605 },
606 ConnectStatus: 1,
607 }
khenaidoo106c61a2021-08-11 18:05:46 -0400608 iaomciMsg2 := &ic.OmciMessage{
kdarapu891693b2019-09-16 12:33:49 +0530609 ProxyAddress: &voltha.Device_ProxyAddress{
610 DeviceId: "onu3",
611 DeviceType: "onu",
612 ChannelId: 1,
613 ChannelGroupId: 1,
614 },
615 ConnectStatus: 1,
616 }
617 type args struct {
618 onuDevice *voltha.Device
khenaidoo106c61a2021-08-11 18:05:46 -0400619 omciMsg *ic.OmciMessage
kdarapu891693b2019-09-16 12:33:49 +0530620 }
621 tests := []struct {
622 name string
623 devicehandler *DeviceHandler
624 args args
625 }{
khenaidoo106c61a2021-08-11 18:05:46 -0400626 {"sendProxiedMessage-1", dh1, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
627 {"sendProxiedMessage-2", dh1, args{onuDevice: device2, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530628 {"sendProxiedMessage-3", dh1, args{onuDevice: nil, omciMsg: iaomciMsg1}},
629 {"sendProxiedMessage-4", dh1, args{onuDevice: nil, omciMsg: iaomciMsg2}},
630 {"sendProxiedMessage-5", dh2, args{onuDevice: nil, omciMsg: iaomciMsg2}},
khenaidoo106c61a2021-08-11 18:05:46 -0400631 {"sendProxiedMessage-6", dh2, args{onuDevice: device1, omciMsg: &ic.OmciMessage{}}},
kdarapu891693b2019-09-16 12:33:49 +0530632 }
633 for _, tt := range tests {
634 t.Run(tt.name, func(t *testing.T) {
khenaidoo106c61a2021-08-11 18:05:46 -0400635 _ = tt.devicehandler.ProxyOmciMessage(ctx, tt.args.omciMsg)
Kent Hagermane6ff1012020-07-14 15:07:53 -0400636 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530637 })
638 }
639}
640
641func TestDeviceHandler_SendPacketInToCore(t *testing.T) {
642 dh1 := newMockDeviceHandler()
643 dh2 := negativeDeviceHandler()
644
645 type args struct {
646 logicalPort uint32
647 packetPayload []byte
648 }
649 tests := []struct {
650 name string
651 devicehandler *DeviceHandler
652 args args
653 }{
654 {"SendPacketInToCore-1", dh1, args{logicalPort: 1, packetPayload: []byte("test1")}},
655 {"SendPacketInToCore-2", dh1, args{logicalPort: 1, packetPayload: []byte("")}},
656 {"SendPacketInToCore-3", dh2, args{logicalPort: 1, packetPayload: []byte("test1")}},
657 }
658 for _, tt := range tests {
659 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400660 _ = tt.devicehandler.SendPacketInToCore(context.Background(), tt.args.logicalPort, tt.args.packetPayload)
661 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530662 })
663 }
664}
665
666func TestDeviceHandler_DisableDevice(t *testing.T) {
667 dh1 := newMockDeviceHandler()
668 dh2 := negativeDeviceHandler()
669 type args struct {
670 device *voltha.Device
671 }
672 tests := []struct {
673 name string
674 devicehandler *DeviceHandler
675 args args
676 wantErr bool
677 }{
678 {"DisableDevice-1", dh1, args{device: dh1.device}, false},
Chaitrashree G S3b4c0352019-09-09 20:59:29 -0400679 {"DisableDevice-2", dh1, args{device: dh2.device}, true},
kdarapu891693b2019-09-16 12:33:49 +0530680 }
681 for _, tt := range tests {
682 t.Run(tt.name, func(t *testing.T) {
Neha Sharma96b7bf22020-06-15 10:37:32 +0000683 if err := tt.devicehandler.DisableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530684 t.Errorf("DeviceHandler.DisableDevice() error = %v, wantErr %v", err, tt.wantErr)
685 }
686 })
687 }
688}
689
690func TestDeviceHandler_ReenableDevice(t *testing.T) {
691 dh1 := newMockDeviceHandler()
692 dh2 := negativeDeviceHandler()
693 type args struct {
694 device *voltha.Device
695 }
696 tests := []struct {
697 name string
698 devicehandler *DeviceHandler
699 args args
700 wantErr bool
701 }{
702 {"ReenableDevice-1", dh1, args{device: dh1.device}, false},
703 {"ReenableDevice-2", dh1, args{device: &voltha.Device{}}, true},
704 {"ReenableDevice-3", dh2, args{device: dh1.device}, false},
705 }
706 for _, tt := range tests {
707 t.Run(tt.name, func(t *testing.T) {
708 dh := tt.devicehandler
Neha Sharma96b7bf22020-06-15 10:37:32 +0000709 if err := dh.ReenableDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530710 t.Errorf("DeviceHandler.ReenableDevice() error = %v, wantErr %v", err, tt.wantErr)
711 }
712 })
713 }
714}
715
716func TestDeviceHandler_RebootDevice(t *testing.T) {
717 dh1 := newMockDeviceHandler()
718 dh2 := newMockDeviceHandler()
719 type args struct {
720 device *voltha.Device
721 }
722 tests := []struct {
723 name string
724 devicehandler *DeviceHandler
725 args args
726 wantErr bool
727 }{
728 // TODO: Add test cases.
729 {"RebootDevice-1", dh1, args{device: dh1.device}, false},
730 {"RebootDevice-2", dh1, args{device: dh2.device}, true},
731 {"RebootDevice-3", dh2, args{device: dh2.device}, false},
732 }
733 for _, tt := range tests {
734 t.Run(tt.name, func(t *testing.T) {
735
Neha Sharma96b7bf22020-06-15 10:37:32 +0000736 if err := tt.devicehandler.RebootDevice(context.Background(), tt.args.device); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +0530737 t.Errorf("DeviceHandler.RebootDevice() error = %v, wantErr %v", err, tt.wantErr)
738 }
739 })
740 }
741}
742
743func TestDeviceHandler_handleIndication(t *testing.T) {
744 dh1 := newMockDeviceHandler()
745 dh2 := negativeDeviceHandler()
746 dh3 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530747 dh3.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -0400748 dh3.onus.Store("onu1", NewOnuDevice("onu1", "onu1", "onu1", 1, 1, "onu1", false, "mock_endpoint"))
749 dh3.onus.Store("onu2", NewOnuDevice("onu2", "onu2", "onu2", 2, 2, "onu2", false, "mock_endpoint"))
Naga Manjunatha8dc9372019-10-31 23:01:18 +0530750
kdarapu891693b2019-09-16 12:33:49 +0530751 type args struct {
752 indication *oop.Indication
753 }
754 tests := []struct {
755 name string
756 deviceHandler *DeviceHandler
757 args args
758 }{
759 // TODO: Add test cases.
760 {"handleIndication-1", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
761 {"handleIndication-2", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
762 {"handleIndication-3", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
763 {"handleIndication-4", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
764 {"handleIndication-5", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
765 {"handleIndication-6", dh1, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
766 {"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")}}}}}},
767 {"handleIndication-8", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
768 {"handleIndication-9", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
769 {"handleIndication-10", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
770 {"handleIndication-11", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
771 {"handleIndication-12", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
772 {"handleIndication-13", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
773 {"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}}}}},
774 {"handleIndication-15", dh1, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
775 {"handleIndication-16", dh1, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
776 {"handleIndication-17", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", FlowId: 1234, PortNo: 1}}}}},
777 {"handleIndication-18", dh1, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{}}}}},
778
779 // Negative testcases
780 {"handleIndication-19", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "up"}}}}},
781 {"handleIndication-20", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OltInd{OltInd: &oop.OltIndication{OperState: "down"}}}}},
782 {"handleIndication-21", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "up"}}}}},
783 {"handleIndication-22", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfInd{IntfInd: &oop.IntfIndication{IntfId: 1, OperState: "down"}}}}},
784 {"handleIndication-23", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "nni", IntfId: 1, OperState: "up"}}}}},
785 {"handleIndication-24", dh2, args{indication: &oop.Indication{Data: &oop.Indication_IntfOperInd{IntfOperInd: &oop.IntfOperIndication{Type: "pon", IntfId: 1, OperState: "up"}}}}},
786 {"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")}}}}}},
787 {"handleIndication-26", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
788 {"handleIndication-27", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
789 {"handleIndication-28", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
790 {"handleIndication-29", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
791 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 1, Pkt: []byte("onu123-random value")}}}}},
792 {"handleIndication-31", dh2, args{indication: &oop.Indication{Data: &oop.Indication_PktInd{PktInd: &oop.PacketIndication{IntfType: "nni", IntfId: 1, GemportId: 1, FlowId: 1234, PortNo: 1}}}}},
793 {"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}}}}},
794 {"handleIndication-33", dh2, args{indication: &oop.Indication{Data: &oop.Indication_FlowStats{FlowStats: &oop.FlowStatistics{RxBytes: 100, RxPackets: 100, TxBytes: 100, TxPackets: 100, Timestamp: 1000}}}}},
795 {"handleIndication-34", dh2, args{indication: &oop.Indication{Data: &oop.Indication_AlarmInd{AlarmInd: &oop.AlarmIndication{}}}}},
796 //
797 {"handleIndication-35", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "up"}}}}},
798 {"handleIndication-36", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "up"}}}}},
799 {"handleIndication-37", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "up", AdminState: "down"}}}}},
800 {"handleIndication-38", dh3, args{indication: &oop.Indication{Data: &oop.Indication_OnuInd{OnuInd: &oop.OnuIndication{IntfId: 1, OnuId: 1, OperState: "down", AdminState: "down"}}}}},
801 {"handleIndication-30", dh1, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
802 {"handleIndication-30", dh2, args{indication: &oop.Indication{Data: &oop.Indication_OmciInd{OmciInd: &oop.OmciIndication{IntfId: 1, OnuId: 4, Pkt: []byte("onu123-random value")}}}}},
803 }
804 for _, tt := range tests {
805 t.Run(tt.name, func(t *testing.T) {
806 dh := tt.deviceHandler
khenaidoo106c61a2021-08-11 18:05:46 -0400807 time.Sleep(5 * time.Millisecond)
npujarec5762e2020-01-01 14:08:48 +0530808 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
809 defer cancel()
810 dh.handleIndication(ctx, tt.args.indication)
kdarapu891693b2019-09-16 12:33:49 +0530811 })
812 }
813}
814
815func TestDeviceHandler_addPort(t *testing.T) {
816 dh1 := newMockDeviceHandler()
817 dh2 := negativeDeviceHandler()
818 type args struct {
819 intfID uint32
820 portType voltha.Port_PortType
821 state string
822 }
823 tests := []struct {
824 name string
825 devicehandler *DeviceHandler
826 args args
827 }{
828 // State up
829 {"addPort.1", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "up"}},
830 {"addPort.2", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "up"}},
831 {"addPort.3", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "up"}},
832 {"addPort.4", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
833 {"addPort.5", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
834 {"addPort.6", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "up"}},
835 {"addPort.7", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "up"}},
836 {"addPort.8", dh1, args{intfID: 1, portType: 8, state: "up"}},
837 // state discovery
838 {"addPort.9", dh1, args{intfID: 1, portType: voltha.Port_UNKNOWN, state: "down"}},
839 {"addPort.10", dh1, args{intfID: 1, portType: voltha.Port_VENET_OLT, state: "down"}},
840 {"addPort.11", dh1, args{intfID: 1, portType: voltha.Port_VENET_ONU, state: "down"}},
841 {"addPort.12", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
842 {"addPort.13", dh1, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
843 {"addPort.14", dh1, args{intfID: 1, portType: voltha.Port_PON_OLT, state: "down"}},
844 {"addPort.15", dh1, args{intfID: 1, portType: voltha.Port_PON_ONU, state: "down"}},
845 {"addPort.16", dh1, args{intfID: 1, portType: 8, state: "down"}},
846
847 {"addPort.17", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "up"}},
848 {"addPort.18", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "up"}},
849 {"addPort.19", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_NNI, state: "down"}},
850 {"addPort.20", dh2, args{intfID: 1, portType: voltha.Port_ETHERNET_UNI, state: "down"}},
851 }
852 for _, tt := range tests {
853 t.Run(tt.name, func(t *testing.T) {
Kent Hagermane6ff1012020-07-14 15:07:53 -0400854 _ = tt.devicehandler.addPort(context.Background(), tt.args.intfID, tt.args.portType, tt.args.state)
855 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530856 })
857 }
858}
859
860func Test_macAddressToUint32Array(t *testing.T) {
861 type args struct {
862 mac string
863 }
864 tests := []struct {
865 name string
866 args args
867 want []uint32
868 }{
869 // TODO: Add test cases.
870 {"macAddressToUint32Array-1", args{mac: "00:00:00:00:00:01"}, []uint32{0, 0, 0, 0, 0, 1}},
871 {"macAddressToUint32Array-2", args{mac: "0abcdef"}, []uint32{11259375}},
872 {"macAddressToUint32Array-3", args{mac: "testing"}, []uint32{1, 2, 3, 4, 5, 6}},
873 }
874 for _, tt := range tests {
875 t.Run(tt.name, func(t *testing.T) {
876 if got := macAddressToUint32Array(tt.args.mac); !reflect.DeepEqual(got, tt.want) {
877 t.Errorf("macAddressToUint32Array() = %v, want %v", got, tt.want)
878 }
879 })
880 }
881}
882
883func TestDeviceHandler_handleOltIndication(t *testing.T) {
884
885 type args struct {
886 oltIndication *oop.OltIndication
887 }
888 tests := []struct {
889 name string
890 args args
891 }{
892 {"handleOltIndication-1", args{oltIndication: &oop.OltIndication{OperState: "up"}}},
893 {"handleOltIndication-2", args{oltIndication: &oop.OltIndication{OperState: "down"}}},
894 }
895 for _, tt := range tests {
896 t.Run(tt.name, func(t *testing.T) {
897 dh := newMockDeviceHandler()
npujarec5762e2020-01-01 14:08:48 +0530898 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
899 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400900 if err := dh.handleOltIndication(ctx, tt.args.oltIndication); err != nil {
901 t.Error(err)
902 }
kdarapu891693b2019-09-16 12:33:49 +0530903 })
904 }
905}
906
907func TestDeviceHandler_AdoptDevice(t *testing.T) {
908 dh1 := newMockDeviceHandler()
909 dh2 := negativeDeviceHandler()
910 type args struct {
911 device *voltha.Device
912 }
913 tests := []struct {
914 name string
915 devicehandler *DeviceHandler
916 args args
917 }{
918 // TODO: Add test cases.
919 {"AdoptDevice-1", dh1, args{device: dh1.device}},
Naga Manjunath7615e552019-10-11 22:35:47 +0530920 {"AdoptDevice-2", dh2, args{device: dh2.device}},
kdarapu891693b2019-09-16 12:33:49 +0530921 }
922 for _, tt := range tests {
923 t.Run(tt.name, func(t *testing.T) {
924 //dh.doStateInit()
925 // context.
926 //dh.AdoptDevice(tt.args.device)
npujarec5762e2020-01-01 14:08:48 +0530927 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
928 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400929 if err := tt.devicehandler.postInit(ctx); err != nil {
930 t.Error(err)
931 }
kdarapu891693b2019-09-16 12:33:49 +0530932 })
933 }
934}
935
936func TestDeviceHandler_activateONU(t *testing.T) {
937 dh := newMockDeviceHandler()
938 dh1 := negativeDeviceHandler()
939 type args struct {
940 intfID uint32
941 onuID int64
942 serialNum *oop.SerialNumber
943 serialNumber string
944 }
945 tests := []struct {
946 name string
947 devicehandler *DeviceHandler
948 args args
949 }{
Girish Gowdra38d533d2020-03-30 20:38:51 -0700950 {"activateONU-1", dh, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
951 {"activateONU-2", dh, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
952 {"activateONU-3", dh1, args{intfID: 0, onuID: 1, serialNum: &oop.SerialNumber{VendorId: []byte("onu1")}}},
953 {"activateONU-4", dh1, args{intfID: 1, onuID: 2, serialNum: &oop.SerialNumber{VendorId: []byte("onu2")}}},
kdarapu891693b2019-09-16 12:33:49 +0530954 }
955 for _, tt := range tests {
956 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +0530957 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
958 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -0400959 _ = tt.devicehandler.activateONU(ctx, tt.args.intfID, tt.args.onuID, tt.args.serialNum, tt.args.serialNumber)
960 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +0530961 })
962 }
963}
964
965func TestDeviceHandler_start(t *testing.T) {
966 dh := newMockDeviceHandler()
967 dh1 := negativeDeviceHandler()
968 dh.start(context.Background())
969 dh.stop(context.Background())
970
971 dh1.start(context.Background())
972 dh1.stop(context.Background())
973
974}
975
976func TestDeviceHandler_PacketOut(t *testing.T) {
977 dh1 := newMockDeviceHandler()
978 dh2 := negativeDeviceHandler()
979 acts := []*ofp.OfpAction{
980 fu.SetField(fu.Metadata_ofp(uint64(ofp.OfpInstructionType_OFPIT_WRITE_METADATA))),
981 fu.SetField(fu.VlanVid(uint32(ofp.OfpVlanId_OFPVID_PRESENT) | 101)),
982 fu.Output(1),
983 }
984 pktout := &ofp.OfpPacketOut{BufferId: 0, InPort: 1, Actions: acts, Data: []byte("AYDCAAAOAODsSE5TiMwCBwQA4OxITlIEBQUwLzUxBgIAFAgEMC81MQoJbG9jYWxob3N0EBwFAawbqqACAAAAoRAxLjMuNi4xLjQuMS40NDEz/gYAgMILAgD+GQCAwgkDAAAAAGQAAAAAAAAAAgICAgICAgL+GQCAwgoDAAAAAGQAAAAAAAAAAgICAgICAgIAAA==")}
985 type args struct {
986 egressPortNo int
987 packet *of.OfpPacketOut
988 }
989 tests := []struct {
990 name string
991 devicehandler *DeviceHandler
992 args args
993 wantErr bool
994 }{
995 // TODO: Add test cases.
996 //{"test1", args{egressPortNo: 0, packet: &ofp.OfpPacketOut{}}, true},
997 {"PacketOut-1", dh1, args{egressPortNo: 0, packet: pktout}, false},
998 {"PacketOut-2", dh2, args{egressPortNo: 1, packet: pktout}, false},
Matteo Scandolo2c0d2742020-06-10 11:28:42 -0700999 {"PacketOut-3", dh2, args{egressPortNo: 4112, packet: pktout}, false},
1000 {"PacketOut-4", dh1, args{egressPortNo: 1048577, packet: pktout}, false},
1001 {"PacketOut-5", dh2, args{egressPortNo: 1048576, packet: pktout}, false},
kdarapu891693b2019-09-16 12:33:49 +05301002 }
1003 for _, tt := range tests {
1004 t.Run(tt.name, func(t *testing.T) {
1005 dh := tt.devicehandler
npujarec5762e2020-01-01 14:08:48 +05301006 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1007 defer cancel()
khenaidoo106c61a2021-08-11 18:05:46 -04001008 if err := dh.PacketOut(ctx, uint32(tt.args.egressPortNo), tt.args.packet); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301009 t.Errorf("DeviceHandler.PacketOut() error = %v, wantErr %v", err, tt.wantErr)
1010 }
1011 })
1012 }
1013}
1014
1015//
1016func TestDeviceHandler_doStateUp(t *testing.T) {
1017 dh1 := newMockDeviceHandler()
1018 dh2 := newMockDeviceHandler()
1019
Thomas Lee S985938d2020-05-04 11:40:41 +05301020 dh2.device.Id = ""
kdarapu891693b2019-09-16 12:33:49 +05301021 dh3 := negativeDeviceHandler()
1022
1023 tests := []struct {
1024 name string
1025 devicehandler *DeviceHandler
1026 wantErr bool
1027 }{
1028 {"dostateup-1", dh1, false},
1029 {"dostateup-2", dh2, false},
1030 {"dostateup-3", dh3, true},
1031 }
1032 for _, tt := range tests {
1033 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301034 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1035 defer cancel()
1036 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301037 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1038 }
Thomas Lee S85f37312020-04-03 17:06:12 +05301039 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
kdarapu891693b2019-09-16 12:33:49 +05301040 })
1041 }
1042}
1043func TestDeviceHandler_doStateDown(t *testing.T) {
1044 dh1 := newMockDeviceHandler()
1045 dh2 := negativeDeviceHandler()
1046 dh3 := newMockDeviceHandler()
1047 dh3.device.OperStatus = voltha.OperStatus_UNKNOWN
1048 tests := []struct {
1049 name string
1050 devicehandler *DeviceHandler
1051 wantErr bool
1052 }{
1053 {"dostatedown-1", dh1, false},
1054 {"dostatedown-2", dh2, true},
1055 {"dostatedown-2", dh3, true},
1056 }
1057 for _, tt := range tests {
1058 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301059 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1060 defer cancel()
1061 if err := tt.devicehandler.doStateDown(ctx); (err != nil) != tt.wantErr {
kdarapu891693b2019-09-16 12:33:49 +05301062 t.Logf("DeviceHandler.doStateDown() error = %v", err)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001063 //TODO: should fail this test case (Errorf) if result is not as expected
kdarapu891693b2019-09-16 12:33:49 +05301064 }
1065 })
1066 }
1067}
1068
1069func TestDeviceHandler_GetOfpDeviceInfo(t *testing.T) {
1070 dh1 := newMockDeviceHandler()
1071 dh2 := negativeDeviceHandler()
1072 type args struct {
1073 device *voltha.Device
1074 }
1075 tests := []struct {
1076 name string
1077 devicehandler *DeviceHandler
1078 args args
1079 wantErr bool
1080 }{
1081 // TODO: Add test cases.
1082 {"GetOfpDeviceInfo-1", dh1, args{dh1.device}, false},
1083 {"GetOfpDeviceInfo-2", dh1, args{&voltha.Device{}}, false},
1084 {"GetOfpDeviceInfo-3", dh2, args{dh1.device}, false},
1085 }
1086 for _, tt := range tests {
1087 t.Run(tt.name, func(t *testing.T) {
1088 dh := tt.devicehandler
1089 _, err := dh.GetOfpDeviceInfo(tt.args.device)
1090 if (err != nil) != tt.wantErr {
1091 t.Errorf("DeviceHandler.GetOfpDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1092 return
1093 }
1094 })
1095 }
1096}
1097
kdarapu891693b2019-09-16 12:33:49 +05301098func TestDeviceHandler_onuDiscIndication(t *testing.T) {
1099
1100 dh1 := newMockDeviceHandler()
Naga Manjunatha8dc9372019-10-31 23:01:18 +05301101 dh1.discOnus = sync.Map{}
1102 dh1.discOnus.Store("onu1", true)
1103 dh1.discOnus.Store("onu2", false)
Thiyagarajan Subramani34a00282020-03-10 20:19:31 +05301104 dh1.discOnus.Store("onu3", true)
1105 dh1.discOnus.Store("onu4", true)
1106 dh1.onus = sync.Map{}
khenaidoo106c61a2021-08-11 18:05:46 -04001107 dh1.onus.Store("onu3", NewOnuDevice("onu3", "onu3", "onu3", 3, 3, "onu3", true, "mock_endpoint"))
1108 dh1.onus.Store("onu4", NewOnuDevice("onu4", "onu4", "onu4", 4, 4, "onu4", true, "mock_endpoint"))
kdarapu891693b2019-09-16 12:33:49 +05301109 dh2 := negativeDeviceHandler()
1110 type args struct {
1111 onuDiscInd *oop.OnuDiscIndication
1112 sn string
1113 }
1114 tests := []struct {
1115 name string
1116 devicehandler *DeviceHandler
1117 args args
1118 }{
1119 // TODO: Add test cases.
1120 {"onuDiscIndication-1", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
1121 {"onuDiscIndication-2", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{}}}},
1122 {"onuDiscIndication-3", dh1, args{onuDiscInd: &oop.OnuDiscIndication{SerialNumber: &oop.SerialNumber{}}}},
1123 {"onuDiscIndication-4", dh1, args{onuDiscInd: &oop.OnuDiscIndication{}}},
1124 {"onuDiscIndication-5", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu1"}},
1125 {"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 +05301126 {"onuDiscIndication-7", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu3"}},
1127 {"onuDiscIndication-8", dh1, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 3, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}, sn: "onu4"}},
1128 {"onuDiscIndication-9", dh2, args{onuDiscInd: &oop.OnuDiscIndication{IntfId: 1, SerialNumber: &oop.SerialNumber{VendorId: []byte("TWSH"), VendorSpecific: []byte("1234")}}}},
kdarapu891693b2019-09-16 12:33:49 +05301129 }
1130 for _, tt := range tests {
1131 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301132 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1133 defer cancel()
Mahir Gunyelb0046752021-02-26 13:51:05 -08001134 _ = tt.devicehandler.onuDiscIndication(ctx, tt.args.onuDiscInd)
Kent Hagermane6ff1012020-07-14 15:07:53 -04001135 //TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301136 })
1137 }
1138}
1139
1140func TestDeviceHandler_populateDeviceInfo(t *testing.T) {
1141 dh1 := newMockDeviceHandler()
1142 dh2 := negativeDeviceHandler()
1143 tests := []struct {
1144 name string
1145 devicehandler *DeviceHandler
1146
1147 wantErr bool
1148 }{
1149 // TODO: Add test cases.
1150 {"populateDeviceInfo-1", dh1, false},
1151 {"populateDeviceInfo-2", dh1, true},
1152 {"populateDeviceInfo-3", dh1, true},
1153 {"populateDeviceInfo-4", dh1, true},
1154 {"populateDeviceInfo-5", dh2, true},
1155 }
1156 for _, tt := range tests {
1157 t.Run(tt.name, func(t *testing.T) {
1158
Neha Sharma96b7bf22020-06-15 10:37:32 +00001159 _, err := tt.devicehandler.populateDeviceInfo(context.Background())
kdarapu891693b2019-09-16 12:33:49 +05301160 if (err != nil) != tt.wantErr {
1161 t.Errorf("DeviceHandler.populateDeviceInfo() error = %v, wantErr %v", err, tt.wantErr)
1162 return
1163 }
1164
1165 })
1166 }
1167}
1168
1169func TestDeviceHandler_readIndications(t *testing.T) {
1170 dh1 := newMockDeviceHandler()
1171 dh2 := newMockDeviceHandler()
kdarapu891693b2019-09-16 12:33:49 +05301172 dh3 := newMockDeviceHandler()
1173 dh3.device.AdminState = voltha.AdminState_DISABLED
1174 dh4 := negativeDeviceHandler()
1175 tests := []struct {
1176 name string
1177 devicehandler *DeviceHandler
1178 }{
1179 // TODO: Add test cases.
1180 {"readIndications-1", dh1},
1181 {"readIndications-2", dh2},
1182 {"readIndications-3", dh2},
1183 {"readIndications-4", dh2},
1184 {"readIndications-5", dh2},
1185 {"readIndications-6", dh3},
1186 {"readIndications-7", dh3},
1187 {"readIndications-8", dh4},
1188 }
1189 for _, tt := range tests {
1190 t.Run(tt.name, func(t *testing.T) {
npujarec5762e2020-01-01 14:08:48 +05301191 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1192 defer cancel()
Kent Hagermane6ff1012020-07-14 15:07:53 -04001193 _ = tt.devicehandler.readIndications(ctx)
1194 // TODO: actually verify test cases
kdarapu891693b2019-09-16 12:33:49 +05301195 })
1196 }
1197}
Naga Manjunath7615e552019-10-11 22:35:47 +05301198
1199func Test_startCollector(t *testing.T) {
1200 type args struct {
1201 dh *DeviceHandler
1202 }
1203 dh := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001204 mcs := newMockCoreService()
1205 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301206 {PortNo: 1, Label: "pon", Type: voltha.Port_PON_OLT},
1207 {PortNo: 1048577, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1208 {PortNo: 1048578, Label: "nni", Type: voltha.Port_ETHERNET_NNI},
1209 }
khenaidoo106c61a2021-08-11 18:05:46 -04001210 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301211 dh.portStats.NorthBoundPort = make(map[uint32]*NniPort)
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301212 dh.portStats.NorthBoundPort[1] = &NniPort{Name: "OLT-1"}
1213 dh.portStats.NorthBoundPort[2] = &NniPort{Name: "OLT-1"}
Naga Manjunath7615e552019-10-11 22:35:47 +05301214 dh.portStats.SouthBoundPort = make(map[uint32]*PonPort)
1215 dh.portStats.Device = dh
1216 for i := 0; i < 16; i++ {
1217 dh.portStats.SouthBoundPort[uint32(i)] = &PonPort{DeviceID: "OLT-1"}
1218 }
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301219 dh1 := newMockDeviceHandler()
khenaidoo106c61a2021-08-11 18:05:46 -04001220 mcs = newMockCoreService()
1221 mcs.DevicePorts[dh.device.Id] = []*voltha.Port{}
1222 dh.coreClient.SetService(mcs)
Naga Manjunath7615e552019-10-11 22:35:47 +05301223 tests := []struct {
1224 name string
1225 args args
1226 }{
1227 // TODO: Add test cases.
1228 {"StartCollector-1", args{dh}},
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301229 {"StartCollector-2", args{dh1}},
Naga Manjunath7615e552019-10-11 22:35:47 +05301230 }
1231 for _, tt := range tests {
1232 t.Run(tt.name, func(t *testing.T) {
1233 go func() {
Kishore Darapuaaf9c102020-05-04 13:06:57 +05301234 time.Sleep(1 * time.Second) // simulated wait time to stop startCollector
Naga Manjunath7615e552019-10-11 22:35:47 +05301235 tt.args.dh.stopCollector <- true
1236 }()
Neha Sharma96b7bf22020-06-15 10:37:32 +00001237 startCollector(context.Background(), tt.args.dh)
Naga Manjunath7615e552019-10-11 22:35:47 +05301238 })
1239 }
1240}
Gamze Abakac2c32a62021-03-11 11:44:18 +00001241
1242func TestDeviceHandler_TestReconcileStatus(t *testing.T) {
1243
1244 // olt disconnect (not reboot)
1245 dh1 := newMockDeviceHandler()
1246 dh1.adapterPreviouslyConnected = false
1247 dh1.agentPreviouslyConnected = true
1248
1249 // adapter restart
1250 dh2 := newMockDeviceHandler()
1251 dh2.Client = &mocks.MockOpenoltClient{}
1252 dh2.adapterPreviouslyConnected = true
1253 dh2.agentPreviouslyConnected = true
1254
1255 // first connection or olt restart
1256 dh3 := newMockDeviceHandler()
1257 dh3.Client = &mocks.MockOpenoltClient{}
1258 dh3.adapterPreviouslyConnected = false
1259 dh3.agentPreviouslyConnected = false
1260
1261 // olt and adapter restart at the same time (first case)
1262 dh4 := newMockDeviceHandler()
1263 dh4.Client = &mocks.MockOpenoltClient{}
1264 dh4.adapterPreviouslyConnected = true
1265 dh4.agentPreviouslyConnected = false
1266
1267 // adapter restart and olt disconnect at the same time
1268 dh5 := newMockDeviceHandler()
1269 dh5.Client = &mocks.MockOpenoltClient{}
1270 dh5.adapterPreviouslyConnected = true
1271 dh5.agentPreviouslyConnected = true
1272
1273 tests := []struct {
1274 name string
1275 devicehandler *DeviceHandler
1276 expectedRestart bool
1277 wantErr bool
1278 }{
1279 {"dostateup-1", dh1, true, false},
1280 {"dostateup-2", dh2, false, false},
1281 {"dostateup-3", dh3, false, false},
1282 {"dostateup-4", dh4, true, false},
1283 {"dostateup-5", dh5, false, false},
1284 }
1285 for _, tt := range tests {
1286 t.Run(tt.name, func(t *testing.T) {
1287 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
1288 defer cancel()
1289 if err := tt.devicehandler.doStateUp(ctx); (err != nil) != tt.wantErr {
1290 t.Logf("DeviceHandler.doStateUp() error = %v, wantErr %v", err, tt.wantErr)
1291 }
1292 tt.devicehandler.stopCollector <- true //stop the stat collector invoked from doStateUp
1293 isRestarted := tt.devicehandler.Client.(*mocks.MockOpenoltClient).IsRestarted
1294 if tt.expectedRestart != isRestarted {
1295 t.Errorf("olt-reboot-failed expected= %v, got= %v", tt.expectedRestart, isRestarted)
1296 }
1297 })
1298 }
1299}