blob: bcb6f77d9de86d6f1d763da421f107e9e4438b32 [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 (
20 "gotest.tools/assert"
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070021 "net"
Matteo Scandolo10f965c2019-09-24 10:40:46 -070022 "testing"
23)
24
25func createMockOlt(numPon int, numOnu int) OltDevice {
26 olt := OltDevice{
27 ID: 0,
28 }
29
30 for i := 0; i < numPon; i++ {
31 pon := PonPort{
32 ID: uint32(i),
33 }
34
35 for j := 0; j < numOnu; j++ {
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070036 onuId := uint32(i + j)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070037 onu := Onu{
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070038 ID: onuId,
Matteo Scandolo10f965c2019-09-24 10:40:46 -070039 PonPort: pon,
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070040 PonPortID: pon.ID,
41 HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(onuId)},
Matteo Scandolo10f965c2019-09-24 10:40:46 -070042 }
43 onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID)
Matteo Scandolo27428702019-10-11 16:21:16 -070044 pon.Onus = append(pon.Onus, &onu)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070045 }
Matteo Scandolo27428702019-10-11 16:21:16 -070046 olt.Pons = append(olt.Pons, &pon)
Matteo Scandolo10f965c2019-09-24 10:40:46 -070047 }
48 return olt
49}
50
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070051func Test_Olt_FindOnuBySn_Success(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070052
53 numPon := 4
54 numOnu := 4
55
56 olt := createMockOlt(numPon, numOnu)
57
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070058 onu, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070059
60 assert.Equal(t, err, nil)
61 assert.Equal(t, onu.Sn(), "BBSM00000303")
62 assert.Equal(t, onu.ID, uint32(3))
63 assert.Equal(t, onu.PonPortID, uint32(3))
64}
65
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070066func Test_Olt_FindOnuBySn_Error(t *testing.T) {
Matteo Scandolo10f965c2019-09-24 10:40:46 -070067
68 numPon := 1
69 numOnu := 4
70
71 olt := createMockOlt(numPon, numOnu)
72
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070073 _, err := olt.FindOnuBySn("BBSM00000303")
Matteo Scandolo10f965c2019-09-24 10:40:46 -070074
Matteo Scandolof6f3a7f2019-10-11 11:19:29 -070075 assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303")
76}
77
78func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) {
79
80 numPon := 4
81 numOnu := 4
82
83 olt := createMockOlt(numPon, numOnu)
84
85 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
86
87 onu, err := olt.FindOnuByMacAddress(mac)
88
89 assert.Equal(t, err, nil)
90 assert.Equal(t, onu.Sn(), "BBSM00000303")
91 assert.Equal(t, onu.ID, uint32(3))
92 assert.Equal(t, onu.PonPortID, uint32(3))
93}
94
95func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) {
96
97 numPon := 1
98 numOnu := 4
99
100 olt := createMockOlt(numPon, numOnu)
101
102 mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)}
103
104 _, err := olt.FindOnuByMacAddress(mac)
105
106 assert.Equal(t, err.Error(), "cannot-find-onu-by-mac-address-2e:60:70:13:03:03")
Matteo Scandolo10f965c2019-09-24 10:40:46 -0700107}