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 | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame^] | 20 | "context" |
| 21 | "github.com/looplab/fsm" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame^] | 23 | bbsim "github.com/opencord/bbsim/internal/bbsim/types" |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 24 | "github.com/opencord/bbsim/internal/common" |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 25 | "net" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 26 | "testing" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 27 | |
Matteo Scandolo | 4f4ac79 | 2020-10-01 16:33:21 -0700 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v4/go/openolt" |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 29 | "gotest.tools/assert" |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 32 | func createMockOlt(numPon int, numOnu int, services []ServiceIf) *OltDevice { |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 33 | olt := &OltDevice{ |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 34 | ID: 0, |
| 35 | AllocIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
| 36 | GemPortIDs: make(map[uint32]map[uint32]map[uint32]map[int32]map[uint64]bool), |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | for i := 0; i < numPon; i++ { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 40 | |
| 41 | // initialize the resource maps for every PON Ports |
| 42 | olt.AllocIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 43 | olt.GemPortIDs[uint32(i)] = make(map[uint32]map[uint32]map[int32]map[uint64]bool) |
| 44 | |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 45 | pon := PonPort{ |
| 46 | ID: uint32(i), |
| 47 | } |
| 48 | |
| 49 | for j := 0; j < numOnu; j++ { |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 50 | |
| 51 | // initialize the resource maps for every ONU and the first UNI |
| 52 | olt.AllocIDs[uint32(i)][uint32(j)] = make(map[uint32]map[int32]map[uint64]bool) |
| 53 | olt.GemPortIDs[uint32(i)][uint32(j)] = make(map[uint32]map[int32]map[uint64]bool) |
| 54 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 55 | onuId := uint32(i + j) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 56 | onu := Onu{ |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 57 | ID: onuId, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 58 | PonPort: &pon, |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 59 | PonPortID: pon.ID, |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame^] | 60 | InternalState: fsm.NewFSM( |
| 61 | OnuStateCreated, |
| 62 | // this is fake state machine, we don't care about transition in the OLT |
| 63 | // unit tests, we'll use SetState to emulate cases |
| 64 | fsm.Events{ |
| 65 | {Name: OnuTxEnable, Src: []string{}, Dst: OnuStateEnabled}, |
| 66 | {Name: OnuTxDisable, Src: []string{}, Dst: OnuStateDisabled}, |
| 67 | }, |
| 68 | fsm.Callbacks{}, |
| 69 | ), |
| 70 | Channel: make(chan bbsim.Message, 2048), |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 71 | } |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 72 | |
| 73 | for k, s := range services { |
| 74 | service := s.(*Service) |
| 75 | service.HwAddress = net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(pon.ID), byte(onuId), byte(k)} |
| 76 | service.Onu = &onu |
| 77 | onu.Services = append(onu.Services, service) |
| 78 | } |
| 79 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 80 | onu.SerialNumber = NewSN(olt.ID, pon.ID, onu.ID) |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 81 | pon.Onus = append(pon.Onus, &onu) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 82 | } |
Matteo Scandolo | 2742870 | 2019-10-11 16:21:16 -0700 | [diff] [blame] | 83 | olt.Pons = append(olt.Pons, &pon) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 84 | } |
| 85 | return olt |
| 86 | } |
| 87 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 88 | // check the creation of an OLT with a single Service |
| 89 | func TestCreateOLT(t *testing.T) { |
| 90 | |
| 91 | common.Services = []common.ServiceYaml{ |
| 92 | {Name: "hsia", CTag: 900, CTagAllocation: common.TagAllocationUnique.String(), STag: 900, STagAllocation: common.TagAllocationShared.String(), NeedsEapol: true, NeedsDchp: true, NeedsIgmp: true}, |
| 93 | } |
| 94 | |
| 95 | common.Config = &common.GlobalConfig{ |
| 96 | Olt: common.OltConfig{ |
| 97 | ID: 1, |
| 98 | PonPorts: 2, |
| 99 | OnusPonPort: 2, |
| 100 | }, |
| 101 | } |
| 102 | |
| 103 | olt := CreateOLT(*common.Config, common.Services, true) |
| 104 | |
| 105 | assert.Equal(t, len(olt.Pons), int(common.Config.Olt.PonPorts)) |
| 106 | |
| 107 | // count the ONUs |
| 108 | onus := 0 |
| 109 | for _, p := range olt.Pons { |
| 110 | onus = onus + len(p.Onus) |
| 111 | } |
| 112 | |
| 113 | assert.Equal(t, onus, int(common.Config.Olt.PonPorts*common.Config.Olt.OnusPonPort)) |
| 114 | |
| 115 | // count the services |
| 116 | services := 0 |
| 117 | for _, p := range olt.Pons { |
| 118 | for _, o := range p.Onus { |
| 119 | services = services + len(o.Services) |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | assert.Equal(t, services, int(common.Config.Olt.PonPorts)*int(common.Config.Olt.OnusPonPort)*len(common.Services)) |
| 124 | |
| 125 | s1 := olt.Pons[0].Onus[0].Services[0].(*Service) |
| 126 | |
| 127 | assert.Equal(t, s1.Name, "hsia") |
| 128 | assert.Equal(t, s1.CTag, 900) |
| 129 | assert.Equal(t, s1.STag, 900) |
| 130 | assert.Equal(t, s1.HwAddress.String(), "2e:60:01:00:01:00") |
| 131 | assert.Equal(t, olt.Pons[0].Onus[0].ID, uint32(1)) |
| 132 | |
| 133 | s2 := olt.Pons[0].Onus[1].Services[0].(*Service) |
| 134 | assert.Equal(t, s2.CTag, 901) |
| 135 | assert.Equal(t, s2.STag, 900) |
| 136 | assert.Equal(t, s2.HwAddress.String(), "2e:60:01:00:02:00") |
| 137 | assert.Equal(t, olt.Pons[0].Onus[1].ID, uint32(2)) |
| 138 | |
| 139 | s3 := olt.Pons[1].Onus[0].Services[0].(*Service) |
| 140 | assert.Equal(t, s3.CTag, 902) |
| 141 | assert.Equal(t, s3.STag, 900) |
| 142 | assert.Equal(t, s3.HwAddress.String(), "2e:60:01:01:01:00") |
| 143 | assert.Equal(t, olt.Pons[1].Onus[0].ID, uint32(1)) |
| 144 | |
| 145 | s4 := olt.Pons[1].Onus[1].Services[0].(*Service) |
| 146 | assert.Equal(t, s4.CTag, 903) |
| 147 | assert.Equal(t, s4.STag, 900) |
| 148 | assert.Equal(t, s4.HwAddress.String(), "2e:60:01:01:02:00") |
| 149 | assert.Equal(t, olt.Pons[1].Onus[1].ID, uint32(2)) |
| 150 | } |
| 151 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 152 | func Test_Olt_FindOnuBySn_Success(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 153 | |
| 154 | numPon := 4 |
| 155 | numOnu := 4 |
| 156 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 157 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 158 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 159 | onu, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 160 | |
| 161 | assert.Equal(t, err, nil) |
| 162 | assert.Equal(t, onu.Sn(), "BBSM00000303") |
| 163 | assert.Equal(t, onu.ID, uint32(3)) |
| 164 | assert.Equal(t, onu.PonPortID, uint32(3)) |
| 165 | } |
| 166 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 167 | func Test_Olt_FindOnuBySn_Error(t *testing.T) { |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 168 | |
| 169 | numPon := 1 |
| 170 | numOnu := 4 |
| 171 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 172 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 173 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 174 | _, err := olt.FindOnuBySn("BBSM00000303") |
Matteo Scandolo | 10f965c | 2019-09-24 10:40:46 -0700 | [diff] [blame] | 175 | |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 176 | assert.Equal(t, err.Error(), "cannot-find-onu-by-serial-number-BBSM00000303") |
| 177 | } |
| 178 | |
| 179 | func Test_Olt_FindOnuByMacAddress_Success(t *testing.T) { |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 180 | numPon := 4 |
| 181 | numOnu := 4 |
| 182 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 183 | services := []ServiceIf{ |
| 184 | &Service{Name: "hsia"}, |
| 185 | &Service{Name: "voip"}, |
| 186 | &Service{Name: "vod"}, |
| 187 | } |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 188 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 189 | olt := createMockOlt(numPon, numOnu, services) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 190 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 191 | mac := net.HardwareAddr{0x2e, 0x60, byte(olt.ID), byte(3), byte(6), byte(1)} |
| 192 | s, err := olt.FindServiceByMacAddress(mac) |
| 193 | |
| 194 | assert.NilError(t, err) |
| 195 | |
| 196 | service := s.(*Service) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 197 | |
| 198 | assert.Equal(t, err, nil) |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 199 | assert.Equal(t, service.Onu.Sn(), "BBSM00000306") |
| 200 | assert.Equal(t, service.Onu.ID, uint32(6)) |
| 201 | assert.Equal(t, service.Onu.PonPortID, uint32(3)) |
| 202 | |
| 203 | assert.Equal(t, service.Name, "voip") |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | func Test_Olt_FindOnuByMacAddress_Error(t *testing.T) { |
| 207 | |
| 208 | numPon := 1 |
| 209 | numOnu := 4 |
| 210 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 211 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 212 | |
| 213 | mac := net.HardwareAddr{0x2e, 0x60, 0x70, 0x13, byte(3), byte(3)} |
| 214 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 215 | _, err := olt.FindServiceByMacAddress(mac) |
Matteo Scandolo | f6f3a7f | 2019-10-11 11:19:29 -0700 | [diff] [blame] | 216 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 217 | 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] | 218 | } |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 219 | |
| 220 | func Test_Olt_GetOnuByFlowId(t *testing.T) { |
| 221 | numPon := 4 |
| 222 | numOnu := 4 |
| 223 | |
Matteo Scandolo | 4a03626 | 2020-08-17 15:56:13 -0700 | [diff] [blame] | 224 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 225 | |
| 226 | // Add the flows to onus (to be found) |
| 227 | onu1, _ := olt.FindOnuBySn("BBSM00000303") |
| 228 | flow1 := openolt.Flow{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 229 | FlowId: 64, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 230 | Classifier: &openolt.Classifier{}, |
| 231 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 232 | msg1 := types.OnuFlowUpdateMessage{ |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 233 | OnuID: onu1.ID, |
| 234 | PonPortID: onu1.PonPortID, |
| 235 | Flow: &flow1, |
| 236 | } |
| 237 | onu1.handleFlowAdd(msg1) |
| 238 | |
| 239 | onu2, _ := olt.FindOnuBySn("BBSM00000103") |
| 240 | flow2 := openolt.Flow{ |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 241 | FlowId: 72, |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 242 | Classifier: &openolt.Classifier{}, |
| 243 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 244 | msg2 := types.OnuFlowUpdateMessage{ |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 245 | OnuID: onu2.ID, |
| 246 | PonPortID: onu2.PonPortID, |
| 247 | Flow: &flow2, |
| 248 | } |
| 249 | onu2.handleFlowAdd(msg2) |
| 250 | |
Matteo Scandolo | eb6b5af | 2020-06-24 16:23:58 -0700 | [diff] [blame] | 251 | found, err := olt.GetOnuByFlowId(flow1.FlowId) |
| 252 | |
| 253 | assert.Equal(t, err, nil) |
| 254 | assert.Equal(t, found.Sn(), onu1.Sn()) |
Shrey Baid | 688b424 | 2020-07-10 20:40:10 +0530 | [diff] [blame] | 255 | } |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 256 | |
| 257 | func Test_Olt_storeGemPortId(t *testing.T) { |
| 258 | |
| 259 | const ( |
| 260 | pon = 1 |
| 261 | onu = 1 |
| 262 | uni = 16 |
| 263 | gem1 = 1024 |
| 264 | gem2 = 1025 |
| 265 | ) |
| 266 | |
| 267 | numPon := 2 |
| 268 | numOnu := 2 |
| 269 | |
| 270 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
| 271 | |
| 272 | // add a first flow on the ONU |
| 273 | flow1 := &openolt.Flow{ |
| 274 | AccessIntfId: pon, |
| 275 | OnuId: onu, |
| 276 | PortNo: uni, |
| 277 | FlowId: 1, |
| 278 | GemportId: gem1, |
| 279 | } |
| 280 | |
| 281 | olt.storeGemPortId(flow1) |
| 282 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1) // we have 1 gem port |
| 283 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1) // and one flow referencing it |
| 284 | |
| 285 | // add a second flow on the ONU (same gem) |
| 286 | flow2 := &openolt.Flow{ |
| 287 | AccessIntfId: pon, |
| 288 | OnuId: onu, |
| 289 | PortNo: uni, |
| 290 | FlowId: 2, |
| 291 | GemportId: gem1, |
| 292 | } |
| 293 | |
| 294 | olt.storeGemPortId(flow2) |
| 295 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1) // we have 1 gem port |
| 296 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // and two flows referencing it |
| 297 | |
| 298 | // add a third flow on the ONU (different gem) |
| 299 | flow3 := &openolt.Flow{ |
| 300 | AccessIntfId: pon, |
| 301 | OnuId: onu, |
| 302 | PortNo: uni, |
| 303 | FlowId: 2, |
| 304 | GemportId: 1025, |
| 305 | } |
| 306 | |
| 307 | olt.storeGemPortId(flow3) |
| 308 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2) // we have 2 gem ports |
| 309 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 2) // two flows referencing the first one |
| 310 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem2]), 1) // and one flow referencing the second one |
| 311 | } |
| 312 | |
| 313 | func Test_Olt_freeGemPortId(t *testing.T) { |
| 314 | const ( |
| 315 | pon = 1 |
| 316 | onu = 1 |
| 317 | uni = 16 |
| 318 | gem1 = 1024 |
| 319 | gem2 = 1025 |
| 320 | flow1 = 1 |
| 321 | flow2 = 2 |
| 322 | flow3 = 3 |
| 323 | ) |
| 324 | |
| 325 | numPon := 2 |
| 326 | numOnu := 2 |
| 327 | |
| 328 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
| 329 | |
| 330 | olt.GemPortIDs[pon][onu][uni] = make(map[int32]map[uint64]bool) |
| 331 | olt.GemPortIDs[pon][onu][uni][gem1] = make(map[uint64]bool) |
| 332 | olt.GemPortIDs[pon][onu][uni][gem1][flow1] = true |
| 333 | olt.GemPortIDs[pon][onu][uni][gem1][flow2] = true |
| 334 | olt.GemPortIDs[pon][onu][uni][gem2] = make(map[uint64]bool) |
| 335 | olt.GemPortIDs[pon][onu][uni][gem2][flow3] = true |
| 336 | |
| 337 | // remove one flow on the first gem, check that the gem is still allocated as there is still a flow referencing it |
| 338 | // NOTE that the flow remove only carries the flow ID, no other information |
| 339 | flowGem1 := &openolt.Flow{ |
| 340 | FlowId: flow1, |
| 341 | } |
| 342 | |
| 343 | olt.freeGemPortId(flowGem1) |
| 344 | // we still have two unis in the map |
| 345 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 2) |
| 346 | |
| 347 | // we should now have a single gem referenced on this UNI |
| 348 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni][gem1]), 1, "gemport-not-removed") |
| 349 | |
| 350 | // the gem should still reference flow 2 |
| 351 | assert.Equal(t, olt.GemPortIDs[pon][onu][uni][gem1][flow2], true) |
| 352 | // but should not reference flow1 |
| 353 | _, flow1Exists := olt.GemPortIDs[pon][onu][uni][gem1][flow1] |
| 354 | assert.Equal(t, flow1Exists, false) |
| 355 | |
| 356 | // this is the only flow remaining on this gem, the gem should be removed |
| 357 | flowGem2 := &openolt.Flow{ |
| 358 | FlowId: flow2, |
| 359 | } |
| 360 | olt.freeGemPortId(flowGem2) |
| 361 | |
| 362 | // we should now have a single gem referenced on this UNI |
| 363 | assert.Equal(t, len(olt.GemPortIDs[pon][onu][uni]), 1, "gemport-not-removed") |
| 364 | |
| 365 | // and it should be gem2 |
| 366 | _, gem1exists := olt.GemPortIDs[pon][onu][uni][gem1] |
| 367 | assert.Equal(t, gem1exists, false) |
| 368 | _, gem2exists := olt.GemPortIDs[pon][onu][uni][gem2] |
| 369 | assert.Equal(t, gem2exists, true) |
| 370 | } |
| 371 | |
| 372 | func Test_Olt_validateFlow(t *testing.T) { |
| 373 | |
| 374 | const ( |
| 375 | pon0 = 0 |
| 376 | pon1 = 1 |
| 377 | onu0 = 0 |
| 378 | onu1 = 1 |
| 379 | uniPort = 0 |
| 380 | usedGemIdPon0 = 1024 |
| 381 | usedGemIdPon1 = 1025 |
| 382 | usedAllocIdPon0 = 1 |
| 383 | usedAllocIdPon1 = 2 |
| 384 | flowId = 1 |
| 385 | ) |
| 386 | |
| 387 | numPon := 2 |
| 388 | numOnu := 2 |
| 389 | |
| 390 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
| 391 | |
| 392 | olt.GemPortIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 393 | olt.GemPortIDs[pon1][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 394 | |
| 395 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemIdPon0] = make(map[uint64]bool) |
| 396 | olt.GemPortIDs[pon0][onu0][uniPort][usedGemIdPon0][flowId] = true |
| 397 | olt.GemPortIDs[pon1][onu0][uniPort][usedGemIdPon1] = make(map[uint64]bool) |
| 398 | olt.GemPortIDs[pon1][onu0][uniPort][usedGemIdPon1][flowId] = true |
| 399 | |
| 400 | olt.AllocIDs[pon0][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 401 | olt.AllocIDs[pon1][onu0][uniPort] = make(map[int32]map[uint64]bool) |
| 402 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0] = make(map[uint64]bool) |
| 403 | olt.AllocIDs[pon0][onu0][uniPort][usedAllocIdPon0][flowId] = true |
| 404 | olt.AllocIDs[pon1][onu0][uniPort][usedAllocIdPon1] = make(map[uint64]bool) |
| 405 | olt.AllocIDs[pon1][onu0][uniPort][usedAllocIdPon1][flowId] = true |
| 406 | |
| 407 | // a GemPortID can be referenced across multiple flows on the same ONU |
| 408 | validGemFlow := &openolt.Flow{ |
| 409 | AccessIntfId: pon0, |
| 410 | OnuId: onu0, |
| 411 | GemportId: usedGemIdPon0, |
| 412 | } |
| 413 | |
| 414 | err := olt.validateFlow(validGemFlow) |
| 415 | assert.NilError(t, err) |
| 416 | |
| 417 | // a GemPortID can NOT be referenced across different ONUs on the same PON |
| 418 | invalidGemFlow := &openolt.Flow{ |
| 419 | AccessIntfId: pon0, |
| 420 | OnuId: onu1, |
| 421 | GemportId: usedGemIdPon0, |
| 422 | } |
| 423 | err = olt.validateFlow(invalidGemFlow) |
| 424 | assert.Error(t, err, "gem-1024-already-in-use-on-uni-0-onu-0") |
| 425 | |
| 426 | // if a flow reference the same GEM on a different PON it's a valid flow |
| 427 | invalidGemDifferentPonFlow := &openolt.Flow{ |
| 428 | AccessIntfId: pon1, |
| 429 | OnuId: onu1, |
| 430 | GemportId: usedGemIdPon0, |
| 431 | } |
| 432 | err = olt.validateFlow(invalidGemDifferentPonFlow) |
| 433 | assert.NilError(t, err) |
| 434 | |
| 435 | // an allocId can be referenced across multiple flows on the same ONU |
| 436 | validAllocFlow := &openolt.Flow{ |
| 437 | AccessIntfId: pon0, |
| 438 | OnuId: onu0, |
| 439 | AllocId: usedAllocIdPon0, |
| 440 | } |
| 441 | err = olt.validateFlow(validAllocFlow) |
| 442 | assert.NilError(t, err) |
| 443 | |
| 444 | // an allocId can NOT be referenced across different ONUs on the same PON |
| 445 | invalidAllocFlow := &openolt.Flow{ |
| 446 | AccessIntfId: pon0, |
| 447 | OnuId: onu1, |
| 448 | AllocId: usedAllocIdPon0, |
| 449 | } |
| 450 | err = olt.validateFlow(invalidAllocFlow) |
| 451 | assert.Error(t, err, "allocId-1-already-in-use-on-uni-0-onu-0") |
| 452 | |
| 453 | // if a flow reference the same AllocId on a different PON it's a valid flow |
| 454 | invalidAllocDifferentPonFlow := &openolt.Flow{ |
| 455 | AccessIntfId: pon1, |
| 456 | OnuId: onu1, |
| 457 | AllocId: usedAllocIdPon0, |
| 458 | } |
| 459 | err = olt.validateFlow(invalidAllocDifferentPonFlow) |
| 460 | assert.NilError(t, err) |
| 461 | } |
Matteo Scandolo | b591314 | 2021-03-19 16:10:18 -0700 | [diff] [blame^] | 462 | |
| 463 | func Test_Olt_OmciMsgOut(t *testing.T) { |
| 464 | numPon := 4 |
| 465 | numOnu := 4 |
| 466 | |
| 467 | olt := createMockOlt(numPon, numOnu, []ServiceIf{}) |
| 468 | |
| 469 | // a malformed packet should return an error |
| 470 | msg := &openolt.OmciMsg{ |
| 471 | IntfId: 1, |
| 472 | OnuId: 1, |
| 473 | Pkt: []byte{}, |
| 474 | } |
| 475 | ctx := context.TODO() |
| 476 | _, err := olt.OmciMsgOut(ctx, msg) |
| 477 | assert.Error(t, err, "olt-received-malformed-omci-packet") |
| 478 | |
| 479 | // a correct packet for a non exiting ONU should throw an error |
| 480 | msg = &openolt.OmciMsg{ |
| 481 | IntfId: 10, |
| 482 | OnuId: 25, |
| 483 | Pkt: makeOmciSetRequest(t), |
| 484 | } |
| 485 | _, err = olt.OmciMsgOut(ctx, msg) |
| 486 | assert.Error(t, err, "Cannot find PonPort with id 10 in OLT 0") |
| 487 | |
| 488 | // a correct packet for a disabled ONU should be dropped |
| 489 | // note that an error is not returned, this is valid in BBsim |
| 490 | const ( |
| 491 | ponId = 1 |
| 492 | onuId = 1 |
| 493 | ) |
| 494 | pon, _ := olt.GetPonById(ponId) |
| 495 | onu, _ := pon.GetOnuById(onuId) |
| 496 | onu.InternalState.SetState(OnuStateDisabled) |
| 497 | msg = &openolt.OmciMsg{ |
| 498 | IntfId: ponId, |
| 499 | OnuId: onuId, |
| 500 | Pkt: makeOmciSetRequest(t), |
| 501 | } |
| 502 | _, err = olt.OmciMsgOut(ctx, msg) |
| 503 | assert.NilError(t, err) |
| 504 | assert.Equal(t, len(onu.Channel), 0) // check that no messages have been sent |
| 505 | |
| 506 | // test that the ONU receives a valid packet |
| 507 | onu.InternalState.SetState(OnuStateEnabled) |
| 508 | _, err = olt.OmciMsgOut(ctx, msg) |
| 509 | assert.NilError(t, err) |
| 510 | assert.Equal(t, len(onu.Channel), 1) // check that one message have been sent |
| 511 | |
| 512 | } |