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