Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package devices |
| 18 | |
| 19 | import ( |
| 20 | "gotest.tools/assert" |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 21 | "net" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func 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 Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 36 | onuId := uint32(i + j) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 37 | onu := Onu{ |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 38 | ID: onuId, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 39 | PonPort: pon, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 40 | PonPortID: pon.ID, |
| 41 | HwAddress: net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(pon.ID), byte(onuId)}, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 42 | } |
| 43 | onu.SerialNumber = onu.NewSN(olt.ID, pon.ID, onu.ID) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 44 | pon.Onus = append(pon.Onus, &onu) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 45 | } |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame^] | 46 | olt.Pons = append(olt.Pons, &pon) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 47 | } |
| 48 | return olt |
| 49 | } |
| 50 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 51 | func Test_Olt_FindOnuBySn_Success(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 52 | |
| 53 | numPon := 4 |
| 54 | numOnu := 4 |
| 55 | |
| 56 | olt := createMockOlt(numPon, numOnu) |
| 57 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 58 | onu, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 59 | |
| 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 Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 66 | func Test_Olt_FindOnuBySn_Error(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 67 | |
| 68 | numPon := 1 |
| 69 | numOnu := 4 |
| 70 | |
| 71 | olt := createMockOlt(numPon, numOnu) |
| 72 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 73 | _, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 74 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 75 | assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303") |
| 76 | } |
| 77 | |
| 78 | func 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 | |
| 95 | func 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 Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 107 | } |