blob: 747ddf89a331fcc3c9a7def006a20a29a26e280e [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 Scandoloeb6b5af2020-06-24 16:23:58 -070020 "github.com/opencord/voltha-protos/v2/go/openolt"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070021 "gotest.tools/assert"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070022 "net"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070023 "testing"
24)
25
26func createMockOlt(numPon int, numOnu int) OltDevice {
27 olt := OltDevice{
28 ID: 0,
29 }
30
31 for i := 0; i < numPon; i++ {
32 pon := PonPort{
33 ID: uint32(i),
34 }
35
36 for j := 0; j < numOnu; j++ {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070037 onuId := uint32(i + j)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070038 onu := Onu{
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070039 ID: onuId,
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -070040 PonPort: &pon,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070041 PonPortID: pon.ID,
42 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(onuId)},
Matteo Scandolo10f965c2019-09-24 10:40:46 -070043 }
44 onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID)
Matteo Scandolo27428702019-10-11 16:21:16 -070045 pon.Onus = append(pon.Onus, &onu)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070046 }
Matteo Scandolo27428702019-10-11 16:21:16 -070047 olt.Pons = append(olt.Pons, &pon)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070048 }
49 return olt
50}
51
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070052func Test_Olt_FindOnuBySn_Success(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070053
54 numPon := 4
55 numOnu := 4
56
57 olt := createMockOlt(numPon, numOnu)
58
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070059 onu, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070060
61 assert.Equal(t, err, nil)
62 assert.Equal(t, onu.Sn(), "BBSM00000303")
63 assert.Equal(t, onu.ID, uint32(3))
64 assert.Equal(t, onu.PonPortID, uint32(3))
65}
66
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070067func Test_Olt_FindOnuBySn_Error(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070068
69 numPon := 1
70 numOnu := 4
71
72 olt := createMockOlt(numPon, numOnu)
73
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070074 _, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070075
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070076 assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303")
77}
78
79func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
80
81 numPon := 4
82 numOnu := 4
83
84 olt := createMockOlt(numPon, numOnu)
85
86 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
87
88 onu, err := olt.FindOnuByMacAddress(mac)
89
90 assert.Equal(t, err, nil)
91 assert.Equal(t, onu.Sn(), "BBSM00000303")
92 assert.Equal(t, onu.ID, uint32(3))
93 assert.Equal(t, onu.PonPortID, uint32(3))
94}
95
96func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) {
97
98 numPon := 1
99 numOnu := 4
100
101 olt := createMockOlt(numPon, numOnu)
102
103 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
104
105 _, err := olt.FindOnuByMacAddress(mac)
106
107 assert.Equal(t, err.Error(), "cannot-find-onu-by-mac-address-2e:60:70:13:03:03")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700108}
Matteo Scandoloeb6b5af2020-06-24 16:23:58 -0700109
110func Test_Olt_GetOnuByFlowId(t *testing.T) {
111 numPon := 4
112 numOnu := 4
113
114 olt := createMockOlt(numPon, numOnu)
115
116 // Add the flows to onus (to be found)
117 onu1, _ := olt.FindOnuBySn("BBSM00000303")
118 flow1 := openolt.Flow{
119 FlowId: 64,
120 Classifier: &openolt.Classifier{},
121 }
122 msg1 := OnuFlowUpdateMessage{
123 OnuID: onu1.ID,
124 PonPortID: onu1.PonPortID,
125 Flow: &flow1,
126 }
127 onu1.handleFlowAdd(msg1)
128
129 onu2, _ := olt.FindOnuBySn("BBSM00000103")
130 flow2 := openolt.Flow{
131 FlowId: 72,
132 Classifier: &openolt.Classifier{},
133 }
134 msg2 := OnuFlowUpdateMessage{
135 OnuID: onu2.ID,
136 PonPortID: onu2.PonPortID,
137 Flow: &flow2,
138 }
139 onu2.handleFlowAdd(msg2)
140
141
142
143 found, err := olt.GetOnuByFlowId(flow1.FlowId)
144
145 assert.Equal(t, err, nil)
146 assert.Equal(t, found.Sn(), onu1.Sn())
147}