blob: 8aa0ae6e23bf05847d311b6cebc56f77b7c1053c [file] [log] [blame]
Matteo Scandolo10f965c2019-09-24 10:40:46 -07001/*
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
17package devices
18
19import (
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070020 "net"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070021 "testing"
Shrey Baid688b4242020-07-10 20:40:10 +053022
23 "github.com/opencord/voltha-protos/v2/go/openolt"
24 "gotest.tools/assert"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070025)
26
Shrey Baid688b4242020-07-10 20:40:10 +053027func createMockOlt(numPon int, numOnu int) *OltDevice {
28 olt := &OltDevice{
Matteo Scandolo10f965c2019-09-24 10:40:46 -070029 ID: 0,
30 }
31
32 for i := 0; i < numPon; i++ {
33 pon := PonPort{
34 ID: uint32(i),
35 }
36
37 for j := 0; j < numOnu; j++ {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070038 onuId := uint32(i + j)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070039 onu := Onu{
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070040 ID: onuId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070041 PonPort: &pon,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070042 PonPortID: pon.ID,
43 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(onuId)},
Matteo Scandolo10f965c2019-09-24 10:40:46 -070044 }
45 onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID)
Matteo Scandolo27428702019-10-11 16:21:16 -070046 pon.Onus = append(pon.Onus, &onu)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070047 }
Matteo Scandolo27428702019-10-11 16:21:16 -070048 olt.Pons = append(olt.Pons, &pon)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070049 }
50 return olt
51}
52
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070053func Test_Olt_FindOnuBySn_Success(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070054
55 numPon := 4
56 numOnu := 4
57
58 olt := createMockOlt(numPon, numOnu)
59
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070060 onu, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070061
62 assert.Equal(t, err, nil)
63 assert.Equal(t, onu.Sn(), "BBSM00000303")
64 assert.Equal(t, onu.ID, uint32(3))
65 assert.Equal(t, onu.PonPortID, uint32(3))
66}
67
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070068func Test_Olt_FindOnuBySn_Error(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070069
70 numPon := 1
71 numOnu := 4
72
73 olt := createMockOlt(numPon, numOnu)
74
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070075 _, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070076
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070077 assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303")
78}
79
80func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
81
82 numPon := 4
83 numOnu := 4
84
85 olt := createMockOlt(numPon, numOnu)
86
87 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
88
89 onu, err := olt.FindOnuByMacAddress(mac)
90
91 assert.Equal(t, err, nil)
92 assert.Equal(t, onu.Sn(), "BBSM00000303")
93 assert.Equal(t, onu.ID, uint32(3))
94 assert.Equal(t, onu.PonPortID, uint32(3))
95}
96
97func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) {
98
99 numPon := 1
100 numOnu := 4
101
102 olt := createMockOlt(numPon, numOnu)
103
104 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
105
106 _, err := olt.FindOnuByMacAddress(mac)
107
108 assert.Equal(t, err.Error(), "cannot-find-onu-by-mac-address-2e:60:70:13:03:03")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700109}
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700110
111func Test_Olt_GetOnuByFlowId(t *testing.T) {
112 numPon := 4
113 numOnu := 4
114
115 olt := createMockOlt(numPon, numOnu)
116
117 // Add the flows to onus (to be found)
118 onu1, _ := olt.FindOnuBySn("BBSM00000303")
119 flow1 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530120 FlowId: 64,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700121 Classifier: &openolt.Classifier{},
122 }
123 msg1 := OnuFlowUpdateMessage{
124 OnuID: onu1.ID,
125 PonPortID: onu1.PonPortID,
126 Flow: &flow1,
127 }
128 onu1.handleFlowAdd(msg1)
129
130 onu2, _ := olt.FindOnuBySn("BBSM00000103")
131 flow2 := openolt.Flow{
Shrey Baid688b4242020-07-10 20:40:10 +0530132 FlowId: 72,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700133 Classifier: &openolt.Classifier{},
134 }
135 msg2 := OnuFlowUpdateMessage{
136 OnuID: onu2.ID,
137 PonPortID: onu2.PonPortID,
138 Flow: &flow2,
139 }
140 onu2.handleFlowAdd(msg2)
141
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700142 found, err := olt.GetOnuByFlowId(flow1.FlowId)
143
144 assert.Equal(t, err, nil)
145 assert.Equal(t, found.Sn(), onu1.Sn())
Shrey Baid688b4242020-07-10 20:40:10 +0530146}