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