Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 14628cd | 2023-01-10 08:38:31 -0500 | [diff] [blame^] | 2 | * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 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 | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 20 | "context" |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 21 | "fmt" |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 22 | "net" |
| 23 | "sync" |
| 24 | "testing" |
| 25 | |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 26 | "github.com/looplab/fsm" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 27 | "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 28 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 29 | "github.com/opencord/bbsim/internal/common" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 30 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 31 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 32 | ) |
| 33 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 34 | func createMockOlt(numPon int, numOnu int, numUni int, services []ServiceIf) *OltDevice { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 35 | olt := &OltDevice{ |
Andrea Campanella | be1b7cf | 2021-04-30 09:53:40 +0200 | [diff] [blame] | 36 | ID: 0, |
| 37 | AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
| 38 | GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
| 39 | OmciResponseRate: 10, |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | for i := 0; i < numPon; i++ { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 43 | |
| 44 | // initialize the resource maps for every PON Ports |
| 45 | olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 46 | olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 47 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 48 | pon := PonPort{ |
| 49 | ID: uint32(i), |
| 50 | } |
| 51 | |
| 52 | for j := 0; j < numOnu; j++ { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 53 | |
| 54 | // initialize the resource maps for every ONU and the first UNI |
| 55 | olt.AllocIDs[uint32(i)][uint32(j)] = make(map[uint32]map[int32]map[uint64]bool) |
| 56 | olt.GemPortIDs[uint32(i)][uint32(j)] = make(map[uint32]map[int32]map[uint64]bool) |
| 57 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 58 | onuId := uint32(i + j) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 59 | onu := Onu{ |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 60 | ID: onuId, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 61 | PonPort: &pon, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 62 | PonPortID: pon.ID, |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 63 | InternalState: fsm.NewFSM( |
| 64 | OnuStateCreated, |
| 65 | // this is fake state machine, we don't care about transition in the OLT |
| 66 | // unit tests, we'll use SetState to emulate cases |
| 67 | fsm.Events{ |
| 68 | {Name: OnuTxEnable, Src: []string{}, Dst: OnuStateEnabled}, |
| 69 | {Name: OnuTxDisable, Src: []string{}, Dst: OnuStateDisabled}, |
| 70 | }, |
| 71 | fsm.Callbacks{}, |
| 72 | ), |
| 73 | Channel: make(chan bbsim.Message, 2048), |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 74 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 75 | |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 76 | for k := 0; k < numUni; k++ { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 77 | uni := UniPort{ |
| 78 | ID: uint32(k + 1), |
| 79 | Onu: &onu, |
| 80 | logger: uniLogger, |
| 81 | } |
| 82 | for l, s := range services { |
| 83 | service := s.(*Service) |
| 84 | service.HwAddress = net.HardwareAddr{0x2e, byte(olt.ID), byte(pon.ID), byte(onuId), byte(k), byte(l)} |
| 85 | service.UniPort = &uni |
| 86 | uni.Services = append(uni.Services, service) |
| 87 | } |
| 88 | onu.UniPorts = append(onu.UniPorts, &uni) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 91 | onu.SerialNumber = NewSN(olt.ID, pon.ID, onu.ID) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 92 | pon.Onus = append(pon.Onus, &onu) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 93 | } |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 94 | olt.Pons = append(olt.Pons, &pon) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 95 | } |
| 96 | return olt |
| 97 | } |
| 98 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 99 | // check the creation of an OLT with a single Service |
| 100 | func TestCreateOLT(t *testing.T) { |
| 101 | |
| 102 | common.Services = []common.ServiceYaml{ |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 103 | {Name: "hsia", CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDhcp: true, NeedsIgmp: true}, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | common.Config = &common.GlobalConfig{ |
| 107 | Olt: common.OltConfig{ |
| 108 | ID: 1, |
| 109 | PonPorts: 2, |
| 110 | OnusPonPort: 2, |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 111 | UniPorts: 4, |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 112 | }, |
| 113 | } |
| 114 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 115 | allocIdPerOnu := uint32(common.Config.Olt.UniPorts * uint32(len(common.Services))) |
| 116 | common.PonsConfig = &common.PonPortsConfig{ |
| 117 | Number: common.Config.Olt.PonPorts, |
| 118 | Ranges: []common.PonRangeConfig{ |
| 119 | { |
| 120 | PonRange: common.IdRange{StartId: 0, EndId: common.Config.Olt.PonPorts - 1}, |
| 121 | Technology: common.XGSPON.String(), |
| 122 | OnuRange: common.IdRange{StartId: 1, EndId: 1 + (common.Config.Olt.OnusPonPort - 1)}, |
| 123 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1024 + (common.Config.Olt.OnusPonPort * allocIdPerOnu)}, |
| 124 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + common.Config.Olt.OnusPonPort*allocIdPerOnu*8}, |
| 125 | }, |
| 126 | }, |
| 127 | } |
| 128 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 129 | olt := CreateOLT(*common.Config, common.Services, true) |
| 130 | |
| 131 | assert.Equal(t, len(olt.Pons), int(common.Config.Olt.PonPorts)) |
| 132 | |
| 133 | // count the ONUs |
| 134 | onus := 0 |
| 135 | for _, p := range olt.Pons { |
| 136 | onus = onus + len(p.Onus) |
| 137 | } |
| 138 | |
| 139 | assert.Equal(t, onus, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort)) |
| 140 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 141 | // counte the UNIs |
| 142 | unis := 0 |
| 143 | for _, p := range olt.Pons { |
| 144 | for _, o := range p.Onus { |
| 145 | unis = unis + len(o.UniPorts) |
| 146 | } |
| 147 | } |
| 148 | // NOTE when unis will be configurable this test will need to adapt |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 149 | assert.Equal(t, unis, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort*common.Config.Olt.UniPorts)) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 150 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 151 | // count the services |
| 152 | services := 0 |
| 153 | for _, p := range olt.Pons { |
| 154 | for _, o := range p.Onus { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 155 | for _, u := range o.UniPorts { |
| 156 | uni := u.(*UniPort) |
| 157 | services = services + len(uni.Services) |
| 158 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 161 | // NOTE when unis will be configurable this test will need to adapt |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 162 | assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*int(common.Config.Olt.UniPorts)*len(common.Services)) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 163 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 164 | s1 := olt.Pons[0].Onus[0].UniPorts[0].(*UniPort).Services[0].(*Service) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 165 | |
| 166 | assert.Equal(t, s1.Name, "hsia") |
| 167 | assert.Equal(t, s1.CTag, 900) |
| 168 | assert.Equal(t, s1.STag, 900) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 169 | assert.Equal(t, "2e:01:00:01:00:00", s1.HwAddress.String()) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 170 | assert.Equal(t, olt.Pons[0].Onus[0].ID, uint32(1)) |
| 171 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 172 | // each ONU has 4 UNIs, taking up the c-tags |
| 173 | s2 := olt.Pons[0].Onus[1].UniPorts[0].(*UniPort).Services[0].(*Service) |
| 174 | assert.Equal(t, s2.CTag, 904) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 175 | assert.Equal(t, s2.STag, 900) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 176 | assert.Equal(t, s2.HwAddress.String(), "2e:01:00:02:00:00") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 177 | assert.Equal(t, olt.Pons[0].Onus[1].ID, uint32(2)) |
| 178 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 179 | s3 := olt.Pons[1].Onus[0].UniPorts[0].(*UniPort).Services[0].(*Service) |
| 180 | assert.Equal(t, s3.CTag, 908) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 181 | assert.Equal(t, s3.STag, 900) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 182 | assert.Equal(t, s3.HwAddress.String(), "2e:01:01:01:00:00") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 183 | assert.Equal(t, olt.Pons[1].Onus[0].ID, uint32(1)) |
| 184 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 185 | s4 := olt.Pons[1].Onus[1].UniPorts[0].(*UniPort).Services[0].(*Service) |
| 186 | assert.Equal(t, s4.CTag, 912) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 187 | assert.Equal(t, s4.STag, 900) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 188 | assert.Equal(t, s4.HwAddress.String(), "2e:01:01:02:00:00") |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 189 | assert.Equal(t, olt.Pons[1].Onus[1].ID, uint32(2)) |
| 190 | } |
| 191 | |
Matteo Scandolo | 52d12e3 | 2022-02-02 15:30:20 -0800 | [diff] [blame] | 192 | func TestGetDeviceInfo(t *testing.T) { |
| 193 | |
| 194 | var onusPerPon uint32 = 4 |
| 195 | |
| 196 | common.Services = []common.ServiceYaml{ |
| 197 | {Name: "hsia", CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDhcp: true, NeedsIgmp: true}, |
| 198 | } |
| 199 | |
| 200 | common.Config = &common.GlobalConfig{ |
| 201 | Olt: common.OltConfig{ |
| 202 | ID: 1, |
| 203 | PonPorts: 2, |
| 204 | OnusPonPort: onusPerPon, |
| 205 | UniPorts: 4, |
| 206 | }, |
| 207 | } |
| 208 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 209 | common.PonsConfig = &common.PonPortsConfig{ |
| 210 | Number: 16, |
| 211 | Ranges: []common.PonRangeConfig{ |
| 212 | { |
| 213 | PonRange: common.IdRange{StartId: 0, EndId: 1}, |
| 214 | Technology: common.XGSPON.String(), |
| 215 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 216 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1024 + 4}, |
| 217 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 218 | }, |
| 219 | { |
| 220 | PonRange: common.IdRange{StartId: 2, EndId: 2}, |
| 221 | Technology: common.GPON.String(), |
| 222 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 223 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 224 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 225 | }, |
| 226 | { |
| 227 | PonRange: common.IdRange{StartId: 3, EndId: 3}, |
| 228 | Technology: common.XGSPON.String(), |
| 229 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 230 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 231 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 232 | }, |
| 233 | { |
| 234 | PonRange: common.IdRange{StartId: 4, EndId: 6}, |
| 235 | Technology: common.XGSPON.String(), |
| 236 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 237 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 238 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 239 | }, |
| 240 | { |
| 241 | PonRange: common.IdRange{StartId: 7, EndId: 8}, |
| 242 | Technology: common.XGSPON.String(), |
| 243 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 244 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 245 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 246 | }, |
| 247 | { |
| 248 | PonRange: common.IdRange{StartId: 9, EndId: 9}, |
| 249 | Technology: common.GPON.String(), |
| 250 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 251 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 252 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 253 | }, |
| 254 | { |
| 255 | PonRange: common.IdRange{StartId: 10, EndId: 10}, |
| 256 | Technology: common.GPON.String(), |
| 257 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 258 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 259 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 260 | }, |
| 261 | { |
| 262 | PonRange: common.IdRange{StartId: 11, EndId: 15}, |
| 263 | Technology: common.XGSPON.String(), |
| 264 | OnuRange: common.IdRange{StartId: 1, EndId: 4}, |
| 265 | AllocIdRange: common.IdRange{StartId: 1024, EndId: 1028 + 4}, |
| 266 | GemportRange: common.IdRange{StartId: 1024, EndId: 1024 + 32}, |
| 267 | }, |
| 268 | }, |
| 269 | } |
| 270 | |
Matteo Scandolo | 52d12e3 | 2022-02-02 15:30:20 -0800 | [diff] [blame] | 271 | olt := CreateOLT(*common.Config, common.Services, true) |
| 272 | |
| 273 | res, err := olt.GetDeviceInfo(context.Background(), &openolt.Empty{}) |
| 274 | |
| 275 | assert.NoError(t, err, "GetDeviceInfo returned error") |
| 276 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 277 | assert.Equal(t, len(res.Ranges), len(common.PonsConfig.Ranges)) |
Matteo Scandolo | 52d12e3 | 2022-02-02 15:30:20 -0800 | [diff] [blame] | 278 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 279 | for _, resRange := range res.Ranges { |
| 280 | for _, ponId := range resRange.IntfIds { |
| 281 | conf, err := common.GetPonConfigById(ponId) |
| 282 | assert.NoError(t, err, fmt.Sprintf("Cannot get pon configuration by id %d", ponId)) |
Matteo Scandolo | 52d12e3 | 2022-02-02 15:30:20 -0800 | [diff] [blame] | 283 | |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 284 | for _, pool := range resRange.Pools { |
| 285 | switch pool.Type { |
| 286 | case openolt.DeviceInfo_DeviceResourceRanges_Pool_ONU_ID: |
| 287 | assert.Equal(t, pool.Start, conf.OnuRange.StartId) |
| 288 | assert.Equal(t, pool.End, conf.OnuRange.EndId) |
| 289 | case openolt.DeviceInfo_DeviceResourceRanges_Pool_ALLOC_ID: |
| 290 | assert.Equal(t, pool.Start, conf.AllocIdRange.StartId) |
| 291 | assert.Equal(t, pool.End, conf.AllocIdRange.EndId) |
| 292 | case openolt.DeviceInfo_DeviceResourceRanges_Pool_GEMPORT_ID: |
| 293 | assert.Equal(t, pool.Start, conf.GemportRange.StartId) |
| 294 | assert.Equal(t, pool.End, conf.GemportRange.EndId) |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
Matteo Scandolo | 52d12e3 | 2022-02-02 15:30:20 -0800 | [diff] [blame] | 299 | } |
| 300 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 301 | func Test_Olt_FindOnuBySn_Success(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 302 | |
| 303 | numPon := 4 |
| 304 | numOnu := 4 |
| 305 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 306 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 307 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 308 | onu, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 309 | |
| 310 | assert.Equal(t, err, nil) |
| 311 | assert.Equal(t, onu.Sn(), "BBSM00000303") |
| 312 | assert.Equal(t, onu.ID, uint32(3)) |
| 313 | assert.Equal(t, onu.PonPortID, uint32(3)) |
| 314 | } |
| 315 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 316 | func Test_Olt_FindOnuBySn_Error(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 317 | |
| 318 | numPon := 1 |
| 319 | numOnu := 4 |
| 320 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 321 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 322 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 323 | _, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 324 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 325 | assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303") |
| 326 | } |
| 327 | |
| 328 | func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) { |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 329 | numPon := 4 |
| 330 | numOnu := 4 |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 331 | numUni := 4 |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 332 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 333 | services := []ServiceIf{ |
| 334 | &Service{Name: "hsia"}, |
| 335 | &Service{Name: "voip"}, |
| 336 | &Service{Name: "vod"}, |
| 337 | } |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 338 | |
Mahir Gunyel | a1753ae | 2021-06-23 00:24:56 -0700 | [diff] [blame] | 339 | olt := createMockOlt(numPon, numOnu, numUni, services) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 340 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 341 | mac := net.HardwareAddr{0x2e, byte(olt.ID), byte(3), byte(6), byte(3), byte(1)} |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 342 | s, err := olt.FindServiceByMacAddress(mac) |
| 343 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 344 | assert.NoError(t, err) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 345 | |
| 346 | service := s.(*Service) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 347 | |
| 348 | assert.Equal(t, err, nil) |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 349 | assert.Equal(t, service.UniPort.Onu.Sn(), "BBSM00000306") |
| 350 | assert.Equal(t, service.UniPort.ID, uint32(4)) |
| 351 | assert.Equal(t, service.UniPort.Onu.ID, uint32(6)) |
| 352 | assert.Equal(t, service.UniPort.Onu.PonPortID, uint32(3)) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 353 | |
| 354 | assert.Equal(t, service.Name, "voip") |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) { |
| 358 | |
| 359 | numPon := 1 |
| 360 | numOnu := 4 |
| 361 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 362 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 363 | |
| 364 | mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)} |
| 365 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 366 | _, err := olt.FindServiceByMacAddress(mac) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 367 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 368 | assert.Equal(t, err.Error(), "cannot-find-service-by-mac-address-2e:60:70:13:03:03") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 369 | } |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 370 | |
| 371 | func Test_Olt_GetOnuByFlowId(t *testing.T) { |
| 372 | numPon := 4 |
| 373 | numOnu := 4 |
| 374 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 375 | services := []ServiceIf{ |
| 376 | &Service{Name: "hsia"}, |
| 377 | } |
| 378 | |
| 379 | olt := createMockOlt(numPon, numOnu, 1, services) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 380 | |
| 381 | // Add the flows to onus (to be found) |
| 382 | onu1, _ := olt.FindOnuBySn("BBSM00000303") |
| 383 | flow1 := openolt.Flow{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 384 | FlowId: 64, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 385 | Classifier: &openolt.Classifier{}, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 386 | UniId: 1, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 387 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 388 | msg1 := types.OnuFlowUpdateMessage{ |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 389 | OnuID: onu1.ID, |
| 390 | PonPortID: onu1.PonPortID, |
| 391 | Flow: &flow1, |
| 392 | } |
| 393 | onu1.handleFlowAdd(msg1) |
| 394 | |
| 395 | onu2, _ := olt.FindOnuBySn("BBSM00000103") |
| 396 | flow2 := openolt.Flow{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 397 | FlowId: 72, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 398 | Classifier: &openolt.Classifier{}, |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 399 | UniId: 1, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 400 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 401 | msg2 := types.OnuFlowUpdateMessage{ |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 402 | OnuID: onu2.ID, |
| 403 | PonPortID: onu2.PonPortID, |
| 404 | Flow: &flow2, |
| 405 | } |
| 406 | onu2.handleFlowAdd(msg2) |
| 407 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 408 | found, err := olt.GetOnuByFlowId(flow1.FlowId) |
| 409 | |
| 410 | assert.Equal(t, err, nil) |
| 411 | assert.Equal(t, found.Sn(), onu1.Sn()) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 412 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 413 | |
| 414 | func Test_Olt_storeGemPortId(t *testing.T) { |
| 415 | |
| 416 | const ( |
| 417 | pon = 1 |
| 418 | onu = 1 |
| 419 | uni = 16 |
| 420 | gem1 = 1024 |
| 421 | gem2 = 1025 |
| 422 | ) |
| 423 | |
| 424 | numPon := 2 |
| 425 | numOnu := 2 |
| 426 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 427 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 428 | |
| 429 | // add a first flow on the ONU |
| 430 | flow1 := &openolt.Flow{ |
| 431 | AccessIntfId: pon, |
| 432 | OnuId: onu, |
| 433 | PortNo: uni, |
| 434 | FlowId: 1, |
| 435 | GemportId: gem1, |
| 436 | } |
| 437 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 438 | olt.storeGemPortIdByFlow(flow1) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 439 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1) // we have 1 gem port |
| 440 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1) // and one flow referencing it |
| 441 | |
| 442 | // add a second flow on the ONU (same gem) |
| 443 | flow2 := &openolt.Flow{ |
| 444 | AccessIntfId: pon, |
| 445 | OnuId: onu, |
| 446 | PortNo: uni, |
| 447 | FlowId: 2, |
| 448 | GemportId: gem1, |
| 449 | } |
| 450 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 451 | olt.storeGemPortIdByFlow(flow2) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 452 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1) // we have 1 gem port |
| 453 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // and two flows referencing it |
| 454 | |
| 455 | // add a third flow on the ONU (different gem) |
| 456 | flow3 := &openolt.Flow{ |
| 457 | AccessIntfId: pon, |
| 458 | OnuId: onu, |
| 459 | PortNo: uni, |
| 460 | FlowId: 2, |
| 461 | GemportId: 1025, |
| 462 | } |
| 463 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 464 | olt.storeGemPortIdByFlow(flow3) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 465 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2) // we have 2 gem ports |
| 466 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // two flows referencing the first one |
| 467 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem2]), 1) // and one flow referencing the second one |
| 468 | } |
| 469 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 470 | func Test_Olt_storeGemPortIdReplicatedFlow(t *testing.T) { |
| 471 | const ( |
| 472 | pon = 1 |
| 473 | onu = 1 |
| 474 | uni = 16 |
| 475 | gem1 = 1024 |
| 476 | gem2 = 1025 |
| 477 | ) |
| 478 | |
| 479 | numPon := 2 |
| 480 | numOnu := 2 |
| 481 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 482 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 483 | |
| 484 | // add a flow that needs replication |
| 485 | pbitToGemPortMap := make(map[uint32]uint32) |
| 486 | pbitToGemPortMap[0] = gem1 |
| 487 | pbitToGemPortMap[1] = gem2 |
| 488 | flow1 := &openolt.Flow{ |
| 489 | AccessIntfId: pon, |
| 490 | OnuId: onu, |
| 491 | PortNo: uni, |
| 492 | FlowId: 1, |
| 493 | GemportId: 0, |
| 494 | ReplicateFlow: true, |
| 495 | PbitToGemport: pbitToGemPortMap, |
| 496 | } |
| 497 | |
| 498 | olt.storeGemPortIdByFlow(flow1) |
| 499 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2) // we have 2 gem ports in the flow |
| 500 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1) // and one flow referencing them |
| 501 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem2]), 1) // and one flow referencing them |
| 502 | } |
| 503 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 504 | func Test_Olt_freeGemPortId(t *testing.T) { |
| 505 | const ( |
| 506 | pon = 1 |
| 507 | onu = 1 |
| 508 | uni = 16 |
| 509 | gem1 = 1024 |
| 510 | gem2 = 1025 |
| 511 | flow1 = 1 |
| 512 | flow2 = 2 |
| 513 | flow3 = 3 |
| 514 | ) |
| 515 | |
| 516 | numPon := 2 |
| 517 | numOnu := 2 |
| 518 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 519 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 520 | |
| 521 | olt.GemPortIDs[pon][onu][uni] = make(map[int32]map[uint64]bool) |
| 522 | olt.GemPortIDs[pon][onu][uni][gem1] = make(map[uint64]bool) |
| 523 | olt.GemPortIDs[pon][onu][uni][gem1][flow1] = true |
| 524 | olt.GemPortIDs[pon][onu][uni][gem1][flow2] = true |
| 525 | olt.GemPortIDs[pon][onu][uni][gem2] = make(map[uint64]bool) |
| 526 | olt.GemPortIDs[pon][onu][uni][gem2][flow3] = true |
| 527 | |
| 528 | // remove one flow on the first gem, check that the gem is still allocated as there is still a flow referencing it |
| 529 | // NOTE that the flow remove only carries the flow ID, no other information |
| 530 | flowGem1 := &openolt.Flow{ |
| 531 | FlowId: flow1, |
| 532 | } |
| 533 | |
| 534 | olt.freeGemPortId(flowGem1) |
| 535 | // we still have two unis in the map |
| 536 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2) |
| 537 | |
| 538 | // we should now have a single gem referenced on this UNI |
| 539 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1, "gemport-not-removed") |
| 540 | |
| 541 | // the gem should still reference flow 2 |
| 542 | assert.Equal(t, olt.GemPortIDs[pon][onu][uni][gem1][flow2], true) |
| 543 | // but should not reference flow1 |
| 544 | _, flow1Exists := olt.GemPortIDs[pon][onu][uni][gem1][flow1] |
| 545 | assert.Equal(t, flow1Exists, false) |
| 546 | |
| 547 | // this is the only flow remaining on this gem, the gem should be removed |
| 548 | flowGem2 := &openolt.Flow{ |
| 549 | FlowId: flow2, |
| 550 | } |
| 551 | olt.freeGemPortId(flowGem2) |
| 552 | |
| 553 | // we should now have a single gem referenced on this UNI |
| 554 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1, "gemport-not-removed") |
| 555 | |
| 556 | // and it should be gem2 |
| 557 | _, gem1exists := olt.GemPortIDs[pon][onu][uni][gem1] |
| 558 | assert.Equal(t, gem1exists, false) |
| 559 | _, gem2exists := olt.GemPortIDs[pon][onu][uni][gem2] |
| 560 | assert.Equal(t, gem2exists, true) |
| 561 | } |
| 562 | |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 563 | func Test_Olt_freeGemPortIdReplicatedflow(t *testing.T) { |
| 564 | const ( |
| 565 | pon = 1 |
| 566 | onu = 1 |
| 567 | uni = 16 |
| 568 | gem1 = 1024 |
| 569 | gem2 = 1025 |
| 570 | flow1 = 1 |
| 571 | ) |
| 572 | |
| 573 | numPon := 2 |
| 574 | numOnu := 2 |
| 575 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 576 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 577 | |
| 578 | olt.GemPortIDs[pon][onu][uni] = make(map[int32]map[uint64]bool) |
| 579 | olt.GemPortIDs[pon][onu][uni][gem1] = make(map[uint64]bool) |
| 580 | olt.GemPortIDs[pon][onu][uni][gem1][flow1] = true |
| 581 | olt.GemPortIDs[pon][onu][uni][gem2] = make(map[uint64]bool) |
| 582 | olt.GemPortIDs[pon][onu][uni][gem2][flow1] = true |
| 583 | |
| 584 | // this flow was a replicated flow, remove all the gems that are referenced by that flow |
| 585 | flowMultiGem := &openolt.Flow{ |
| 586 | FlowId: flow1, |
| 587 | } |
| 588 | |
| 589 | olt.freeGemPortId(flowMultiGem) |
| 590 | |
| 591 | // this flow removes all the gems, so no UNI should be left |
| 592 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 0) |
| 593 | } |
| 594 | |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 595 | // testing that we can validate flows while we are adding them |
| 596 | func Benchmark_validateAndAddFlows(b *testing.B) { |
| 597 | const ( |
| 598 | pon = 0 |
| 599 | start = 0 |
| 600 | end = 512 |
| 601 | ) |
| 602 | |
| 603 | for r := 0; r < b.N; r++ { |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 604 | olt := createMockOlt(1, 512, 4, []ServiceIf{}) |
Matteo Scandolo | 21195d6 | 2021-04-07 14:31:23 -0700 | [diff] [blame] | 605 | |
| 606 | wg := sync.WaitGroup{} |
| 607 | |
| 608 | // concurrently adding 1K gems |
| 609 | for i := start; i < end; i++ { |
| 610 | wg.Add(1) |
| 611 | flow := &openolt.Flow{ |
| 612 | AccessIntfId: pon, |
| 613 | OnuId: int32(i), |
| 614 | PortNo: uint32(i), |
| 615 | GemportId: int32(i), |
| 616 | FlowId: uint64(i), |
| 617 | } |
| 618 | go func(wg *sync.WaitGroup) { |
| 619 | olt.storeGemPortIdByFlow(flow) |
| 620 | olt.storeAllocId(flow) |
| 621 | wg.Done() |
| 622 | }(&wg) |
| 623 | } |
| 624 | |
| 625 | // at the same time validate flows |
| 626 | for i := start; i < end; i++ { |
| 627 | wg.Add(1) |
| 628 | flow := &openolt.Flow{ |
| 629 | AccessIntfId: pon, |
| 630 | OnuId: int32(i), |
| 631 | PortNo: uint32(i), |
| 632 | GemportId: 1, |
| 633 | FlowId: uint64(i), |
| 634 | } |
| 635 | go func(wg *sync.WaitGroup) { |
| 636 | _ = olt.validateFlow(flow) |
| 637 | wg.Done() |
| 638 | }(&wg) |
| 639 | } |
| 640 | |
| 641 | wg.Wait() |
| 642 | // NOTE this tests only fails if there is concurrent access to the map |
| 643 | } |
| 644 | } |
| 645 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 646 | func Test_Olt_validateFlow(t *testing.T) { |
| 647 | |
| 648 | const ( |
| 649 | pon0 = 0 |
| 650 | pon1 = 1 |
| 651 | onu0 = 0 |
| 652 | onu1 = 1 |
| 653 | uniPort = 0 |
| 654 | usedGemIdPon0 = 1024 |
| 655 | usedGemIdPon1 = 1025 |
| 656 | usedAllocIdPon0 = 1 |
| 657 | usedAllocIdPon1 = 2 |
| 658 | flowId = 1 |
| 659 | ) |
| 660 | |
| 661 | numPon := 2 |
| 662 | numOnu := 2 |
| 663 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 664 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 665 | |
| 666 | olt.GemPortIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 667 | olt.GemPortIDs[pon1][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 668 | |
| 669 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemIdPon0] = make(map[uint64]bool) |
| 670 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemIdPon0][flowId] = true |
| 671 | olt.GemPortIDs[pon1][onu0][uniPort][usedGemIdPon1] = make(map[uint64]bool) |
| 672 | olt.GemPortIDs[pon1][onu0][uniPort][usedGemIdPon1][flowId] = true |
| 673 | |
| 674 | olt.AllocIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 675 | olt.AllocIDs[pon1][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 676 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0] = make(map[uint64]bool) |
| 677 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0][flowId] = true |
| 678 | olt.AllocIDs[pon1][onu0][uniPort][usedAllocIdPon1] = make(map[uint64]bool) |
| 679 | olt.AllocIDs[pon1][onu0][uniPort][usedAllocIdPon1][flowId] = true |
| 680 | |
| 681 | // a GemPortID can be referenced across multiple flows on the same ONU |
| 682 | validGemFlow := &openolt.Flow{ |
| 683 | AccessIntfId: pon0, |
| 684 | OnuId: onu0, |
| 685 | GemportId: usedGemIdPon0, |
| 686 | } |
| 687 | |
| 688 | err := olt.validateFlow(validGemFlow) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 689 | assert.NoError(t, err) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 690 | |
| 691 | // a GemPortID can NOT be referenced across different ONUs on the same PON |
| 692 | invalidGemFlow := &openolt.Flow{ |
| 693 | AccessIntfId: pon0, |
| 694 | OnuId: onu1, |
| 695 | GemportId: usedGemIdPon0, |
| 696 | } |
| 697 | err = olt.validateFlow(invalidGemFlow) |
| 698 | assert.Error(t, err, "gem-1024-already-in-use-on-uni-0-onu-0") |
| 699 | |
| 700 | // if a flow reference the same GEM on a different PON it's a valid flow |
| 701 | invalidGemDifferentPonFlow := &openolt.Flow{ |
| 702 | AccessIntfId: pon1, |
| 703 | OnuId: onu1, |
| 704 | GemportId: usedGemIdPon0, |
| 705 | } |
| 706 | err = olt.validateFlow(invalidGemDifferentPonFlow) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 707 | assert.NoError(t, err) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 708 | |
| 709 | // an allocId can be referenced across multiple flows on the same ONU |
| 710 | validAllocFlow := &openolt.Flow{ |
| 711 | AccessIntfId: pon0, |
| 712 | OnuId: onu0, |
| 713 | AllocId: usedAllocIdPon0, |
| 714 | } |
| 715 | err = olt.validateFlow(validAllocFlow) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 716 | assert.NoError(t, err) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 717 | |
| 718 | // an allocId can NOT be referenced across different ONUs on the same PON |
| 719 | invalidAllocFlow := &openolt.Flow{ |
| 720 | AccessIntfId: pon0, |
| 721 | OnuId: onu1, |
| 722 | AllocId: usedAllocIdPon0, |
| 723 | } |
| 724 | err = olt.validateFlow(invalidAllocFlow) |
| 725 | assert.Error(t, err, "allocId-1-already-in-use-on-uni-0-onu-0") |
| 726 | |
| 727 | // if a flow reference the same AllocId on a different PON it's a valid flow |
| 728 | invalidAllocDifferentPonFlow := &openolt.Flow{ |
| 729 | AccessIntfId: pon1, |
| 730 | OnuId: onu1, |
| 731 | AllocId: usedAllocIdPon0, |
| 732 | } |
| 733 | err = olt.validateFlow(invalidAllocDifferentPonFlow) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 734 | assert.NoError(t, err) |
| 735 | } |
| 736 | |
| 737 | func Test_Olt_validateReplicatedFlow(t *testing.T) { |
| 738 | |
| 739 | const ( |
| 740 | pon0 = 0 |
| 741 | onu0 = 0 |
| 742 | onu1 = 1 |
| 743 | uniPort = 0 |
| 744 | usedGemId1 = 1024 |
| 745 | usedGemId2 = 1025 |
| 746 | usedAllocIdPon0 = 1 |
| 747 | flowId = 1 |
| 748 | ) |
| 749 | |
| 750 | numPon := 1 |
| 751 | numOnu := 1 |
| 752 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 753 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 754 | |
| 755 | // both the gemports referenced in this flow are already allocated |
| 756 | olt.GemPortIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 757 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemId1] = make(map[uint64]bool) |
| 758 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemId1][flowId] = true |
| 759 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemId2] = make(map[uint64]bool) |
| 760 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemId2][flowId] = true |
| 761 | |
| 762 | olt.AllocIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 763 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0] = make(map[uint64]bool) |
| 764 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0][flowId] = true |
| 765 | |
| 766 | pbitToGemPortMap := make(map[uint32]uint32) |
| 767 | pbitToGemPortMap[0] = usedGemId1 |
| 768 | pbitToGemPortMap[1] = usedGemId2 |
| 769 | |
| 770 | // this flow should fail vlidation as the gems are already allocated to Onu0 |
| 771 | invalidGemFlow := &openolt.Flow{ |
| 772 | AccessIntfId: pon0, |
| 773 | OnuId: onu1, |
| 774 | PortNo: uniPort, |
| 775 | GemportId: 0, |
| 776 | ReplicateFlow: true, |
| 777 | PbitToGemport: pbitToGemPortMap, |
| 778 | } |
| 779 | |
| 780 | err := olt.validateFlow(invalidGemFlow) |
| 781 | assert.NotNil(t, err) |
| 782 | |
| 783 | // PbitToGemport is a map, so any of the two gemPorts can fail first and determine the error message |
| 784 | foundError := false |
| 785 | switch err.Error() { |
| 786 | case fmt.Sprintf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", usedGemId2, uniPort, onu0, invalidGemFlow.FlowId): |
| 787 | foundError = true |
| 788 | case fmt.Sprintf("gem-%d-already-in-use-on-uni-%d-onu-%d-replicated-flow-%d", usedGemId1, uniPort, onu0, invalidGemFlow.FlowId): |
| 789 | foundError = true |
| 790 | |
| 791 | } |
| 792 | assert.True(t, foundError) |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 793 | } |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 794 | |
| 795 | func Test_Olt_OmciMsgOut(t *testing.T) { |
| 796 | numPon := 4 |
| 797 | numOnu := 4 |
| 798 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 799 | olt := createMockOlt(numPon, numOnu, 1, []ServiceIf{}) |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 800 | |
| 801 | // a malformed packet should return an error |
| 802 | msg := &openolt.OmciMsg{ |
| 803 | IntfId: 1, |
| 804 | OnuId: 1, |
| 805 | Pkt: []byte{}, |
| 806 | } |
| 807 | ctx := context.TODO() |
| 808 | _, err := olt.OmciMsgOut(ctx, msg) |
| 809 | assert.Error(t, err, "olt-received-malformed-omci-packet") |
| 810 | |
| 811 | // a correct packet for a non exiting ONU should throw an error |
| 812 | msg = &openolt.OmciMsg{ |
| 813 | IntfId: 10, |
| 814 | OnuId: 25, |
| 815 | Pkt: makeOmciSetRequest(t), |
| 816 | } |
| 817 | _, err = olt.OmciMsgOut(ctx, msg) |
| 818 | assert.Error(t, err, "Cannot find PonPort with id 10 in OLT 0") |
| 819 | |
| 820 | // a correct packet for a disabled ONU should be dropped |
| 821 | // note that an error is not returned, this is valid in BBsim |
| 822 | const ( |
| 823 | ponId = 1 |
| 824 | onuId = 1 |
| 825 | ) |
| 826 | pon, _ := olt.GetPonById(ponId) |
| 827 | onu, _ := pon.GetOnuById(onuId) |
| 828 | onu.InternalState.SetState(OnuStateDisabled) |
| 829 | msg = &openolt.OmciMsg{ |
| 830 | IntfId: ponId, |
| 831 | OnuId: onuId, |
| 832 | Pkt: makeOmciSetRequest(t), |
| 833 | } |
| 834 | _, err = olt.OmciMsgOut(ctx, msg) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 835 | assert.NoError(t, err) |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 836 | assert.Equal(t, len(onu.Channel), 0) // check that no messages have been sent |
| 837 | |
| 838 | // test that the ONU receives a valid packet |
| 839 | onu.InternalState.SetState(OnuStateEnabled) |
| 840 | _, err = olt.OmciMsgOut(ctx, msg) |
Matteo Scandolo | a8eca49 | 2021-03-23 09:45:16 -0700 | [diff] [blame] | 841 | assert.NoError(t, err) |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame] | 842 | assert.Equal(t, len(onu.Channel), 1) // check that one message have been sent |
| 843 | |
| 844 | } |