Gamze Abaka | 1b7816e | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-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 | package org.opencord.olt.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableMap; |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import com.google.common.collect.ListMultimap; |
| 21 | import org.apache.commons.lang3.tuple.Pair; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.EthType; |
| 25 | import org.onlab.packet.VlanId; |
| 26 | import org.onosproject.cluster.NodeId; |
| 27 | import org.onosproject.cluster.RoleInfo; |
| 28 | import org.onosproject.mastership.MastershipInfo; |
| 29 | import org.onosproject.mastership.MastershipListener; |
| 30 | import org.onosproject.net.DeviceId; |
| 31 | import org.onosproject.net.MastershipRole; |
| 32 | import org.onosproject.net.PortNumber; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.TrafficTreatment; |
| 35 | import org.onosproject.net.flow.criteria.Criterion; |
| 36 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 37 | import org.onosproject.net.flow.criteria.PortCriterion; |
| 38 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 39 | import org.onosproject.net.flow.instructions.Instruction; |
| 40 | import org.onosproject.net.flow.instructions.Instructions; |
| 41 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 42 | import org.onosproject.net.flowobjective.FilteringObjQueueKey; |
| 43 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 44 | import org.onosproject.net.flowobjective.ForwardingObjQueueKey; |
| 45 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 46 | import org.onosproject.net.flowobjective.NextObjQueueKey; |
| 47 | import org.onosproject.net.flowobjective.NextObjective; |
| 48 | import org.onosproject.net.flowobjective.Objective; |
| 49 | import org.onosproject.net.meter.MeterId; |
| 50 | import org.onosproject.net.meter.MeterKey; |
| 51 | import org.opencord.sadis.BandwidthProfileInformation; |
| 52 | import org.opencord.sadis.UniTagInformation; |
| 53 | |
| 54 | import java.util.Collection; |
| 55 | import java.util.List; |
| 56 | import java.util.Map; |
| 57 | import java.util.Optional; |
| 58 | import java.util.Set; |
| 59 | import java.util.concurrent.CompletableFuture; |
| 60 | |
| 61 | public class OltFlowTest extends TestBase { |
| 62 | private OltFlowService oltFlowService; |
| 63 | |
| 64 | PortNumber uniPortNumber = PortNumber.portNumber(1); |
| 65 | PortNumber nniPortNumber = PortNumber.portNumber(65535); |
| 66 | |
| 67 | UniTagInformation.Builder tagInfoBuilder = new UniTagInformation.Builder(); |
| 68 | UniTagInformation uniTagInfo = tagInfoBuilder.setUniTagMatch(VlanId.vlanId((short) 35)) |
| 69 | .setPonCTag(VlanId.vlanId((short) 33)) |
| 70 | .setPonSTag(VlanId.vlanId((short) 7)) |
| 71 | .setDsPonCTagPriority(0) |
| 72 | .setUsPonSTagPriority(0) |
| 73 | .setTechnologyProfileId(64) |
| 74 | .setDownstreamBandwidthProfile(dsBpId) |
| 75 | .setUpstreamBandwidthProfile(usBpId) |
| 76 | .build(); |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() { |
| 80 | oltFlowService = new OltFlowService(); |
| 81 | oltFlowService.oltMeterService = new MockOltMeterService(); |
| 82 | oltFlowService.flowObjectiveService = new MockOltFlowObjectiveService(); |
| 83 | oltFlowService.mastershipService = new MockMastershipService(); |
| 84 | oltFlowService.sadisService = new MockSadisService(); |
| 85 | oltFlowService.bpService = oltFlowService.sadisService.getBandwidthProfileService(); |
| 86 | oltFlowService.appId = appId; |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testDhcpFiltering() { |
| 91 | oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo, |
| 92 | true, true); |
| 93 | oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo, |
| 94 | false, true); |
| 95 | oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, dsMeterId, uniTagInfo, |
| 96 | true, false); |
| 97 | oltFlowService.processDhcpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo, |
| 98 | false, false); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testIgmpFiltering() { |
| 103 | oltFlowService.processIgmpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo, |
| 104 | true, true); |
| 105 | oltFlowService.processIgmpFilteringObjectives(DEVICE_ID_1, uniPortNumber, usMeterId, uniTagInfo, |
| 106 | false, true); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testEapolFiltering() { |
| 111 | addBandwidthProfile(uniTagInfo.getUpstreamBandwidthProfile()); |
| 112 | oltFlowService.enableEapol = true; |
| 113 | |
| 114 | //will install |
| 115 | oltFlowService.processEapolFilteringObjectives(DEVICE_ID_1, uniPortNumber, |
| 116 | uniTagInfo.getUpstreamBandwidthProfile(), new CompletableFuture<>(), |
| 117 | uniTagInfo.getUniTagMatch(), true); |
| 118 | |
| 119 | //bp profile doesn't exist |
| 120 | oltFlowService.processEapolFilteringObjectives(DEVICE_ID_1, uniPortNumber, |
| 121 | uniTagInfo.getDownstreamBandwidthProfile(), new CompletableFuture<>(), |
| 122 | uniTagInfo.getUniTagMatch(), true); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void testLldpFiltering() { |
| 127 | oltFlowService.processLldpFilteringObjective(DEVICE_ID_1, nniPortNumber, true); |
| 128 | oltFlowService.processLldpFilteringObjective(DEVICE_ID_1, nniPortNumber, false); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testNniFiltering() { |
| 133 | oltFlowService.enableDhcpOnProvisioning = true; |
| 134 | oltFlowService.enableIgmpOnProvisioning = true; |
| 135 | oltFlowService.processNniFilteringObjectives(DEVICE_ID_1, nniPortNumber, true); |
| 136 | oltFlowService.processNniFilteringObjectives(DEVICE_ID_1, nniPortNumber, false); |
| 137 | } |
| 138 | |
| 139 | @Test |
| 140 | public void testUpBuilder() { |
| 141 | ForwardingObjective objective = |
| 142 | oltFlowService.createUpBuilder(nniPortNumber, uniPortNumber, usMeterId, uniTagInfo).add(); |
| 143 | checkObjective(objective, true); |
| 144 | } |
| 145 | |
| 146 | @Test |
| 147 | public void testDownBuilder() { |
| 148 | ForwardingObjective objective = |
| 149 | oltFlowService.createDownBuilder(nniPortNumber, uniPortNumber, dsMeterId, uniTagInfo).remove(); |
| 150 | checkObjective(objective, false); |
| 151 | } |
| 152 | |
| 153 | private void checkObjective(ForwardingObjective fwd, boolean upstream) { |
| 154 | TrafficTreatment treatment = fwd.treatment(); |
| 155 | |
| 156 | //check instructions |
| 157 | Set<Instructions.MeterInstruction> meters = treatment.meters(); |
| 158 | assert !meters.isEmpty(); |
| 159 | |
| 160 | Instructions.MetadataInstruction writeMetadata = treatment.writeMetadata(); |
| 161 | assert writeMetadata != null; |
| 162 | |
| 163 | List<Instruction> immediateInstructions = treatment.immediate(); |
| 164 | Optional<Instruction> vlanInstruction = immediateInstructions.stream() |
| 165 | .filter(i -> i.type() == Instruction.Type.L2MODIFICATION) |
| 166 | .filter(i -> ((L2ModificationInstruction) i).subtype() == |
| 167 | L2ModificationInstruction.L2SubType.VLAN_PUSH || |
| 168 | ((L2ModificationInstruction) i).subtype() == |
| 169 | L2ModificationInstruction.L2SubType.VLAN_POP) |
| 170 | .findAny(); |
| 171 | |
| 172 | assert vlanInstruction.isPresent(); |
| 173 | |
| 174 | //check match criteria |
| 175 | TrafficSelector selector = fwd.selector(); |
| 176 | assert selector.getCriterion(Criterion.Type.IN_PORT) != null; |
| 177 | assert selector.getCriterion(Criterion.Type.VLAN_VID) != null; |
| 178 | |
| 179 | if (!upstream) { |
| 180 | assert selector.getCriterion(Criterion.Type.METADATA) != null; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private class MockOltMeterService implements org.opencord.olt.internalapi.AccessDeviceMeterService { |
| 185 | @Override |
Andrea Campanella | d936c8e | 2020-01-30 11:04:34 +0100 | [diff] [blame^] | 186 | public ImmutableMap<String, Collection<MeterKey>> getBpMeterMappings() { |
Gamze Abaka | 1b7816e | 2019-11-25 06:38:41 +0000 | [diff] [blame] | 187 | return null; |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void addMeterIdToBpMapping(DeviceId deviceId, MeterId meterId, String bandwidthProfile) { |
| 192 | |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public MeterId getMeterIdFromBpMapping(DeviceId deviceId, String bandwidthProfile) { |
| 197 | return null; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | @Override |
| 202 | public ImmutableSet<MeterKey> getProgMeters() { |
| 203 | return null; |
| 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public MeterId createMeter(DeviceId deviceId, BandwidthProfileInformation bpInfo, |
| 208 | CompletableFuture<Object> meterFuture) { |
| 209 | return usMeterId; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | private class MockOltFlowObjectiveService implements org.onosproject.net.flowobjective.FlowObjectiveService { |
| 214 | @Override |
| 215 | public void filter(DeviceId deviceId, FilteringObjective filteringObjective) { |
| 216 | |
| 217 | EthTypeCriterion ethType = (EthTypeCriterion) |
| 218 | filterForCriterion(filteringObjective.conditions(), Criterion.Type.ETH_TYPE); |
| 219 | |
| 220 | Instructions.MeterInstruction meter = filteringObjective.meta().metered(); |
| 221 | Instruction writeMetadata = filteringObjective.meta().writeMetadata(); |
| 222 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) |
| 223 | filterForCriterion(filteringObjective.conditions(), Criterion.Type.VLAN_VID); |
| 224 | PortCriterion portCriterion = (PortCriterion) filteringObjective.key(); |
| 225 | |
| 226 | |
| 227 | if (ethType.ethType().equals(EthType.EtherType.LLDP.ethType()) || |
| 228 | portCriterion.port().equals(nniPortNumber)) { |
| 229 | assert meter == null; |
| 230 | assert writeMetadata == null; |
| 231 | assert vlanIdCriterion == null; |
| 232 | } else { |
| 233 | assert meter.meterId().equals(usMeterId) || meter.meterId().equals(dsMeterId); |
| 234 | assert writeMetadata != null; |
| 235 | assert vlanIdCriterion.vlanId() == uniTagInfo.getPonCTag(); |
| 236 | } |
| 237 | |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | public void forward(DeviceId deviceId, ForwardingObjective forwardingObjective) { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public void next(DeviceId deviceId, NextObjective nextObjective) { |
| 247 | |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public int allocateNextId() { |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | @Override |
| 256 | public void initPolicy(String s) { |
| 257 | |
| 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void apply(DeviceId deviceId, Objective objective) { |
| 262 | |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public Map<Pair<Integer, DeviceId>, List<String>> getNextMappingsChain() { |
| 267 | return null; |
| 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public List<String> getNextMappings() { |
| 272 | return null; |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public List<String> getPendingFlowObjectives() { |
| 277 | return null; |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public ListMultimap<FilteringObjQueueKey, Objective> getFilteringObjQueue() { |
| 282 | return null; |
| 283 | } |
| 284 | |
| 285 | @Override |
| 286 | public ListMultimap<ForwardingObjQueueKey, Objective> getForwardingObjQueue() { |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public ListMultimap<NextObjQueueKey, Objective> getNextObjQueue() { |
| 292 | return null; |
| 293 | } |
| 294 | |
| 295 | @Override |
| 296 | public Map<FilteringObjQueueKey, Objective> getFilteringObjQueueHead() { |
| 297 | return null; |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public Map<ForwardingObjQueueKey, Objective> getForwardingObjQueueHead() { |
| 302 | return null; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public Map<NextObjQueueKey, Objective> getNextObjQueueHead() { |
| 307 | return null; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public void clearQueue() { |
| 312 | |
| 313 | } |
| 314 | |
| 315 | private Criterion filterForCriterion(Collection<Criterion> criteria, Criterion.Type type) { |
| 316 | return criteria.stream() |
| 317 | .filter(c -> c.type().equals(type)) |
| 318 | .limit(1) |
| 319 | .findFirst().orElse(null); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private class MockMastershipService implements org.onosproject.mastership.MastershipService { |
| 324 | @Override |
| 325 | public MastershipRole getLocalRole(DeviceId deviceId) { |
| 326 | return null; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public boolean isLocalMaster(DeviceId deviceId) { |
| 331 | return true; |
| 332 | } |
| 333 | |
| 334 | @Override |
| 335 | public CompletableFuture<MastershipRole> requestRoleFor(DeviceId deviceId) { |
| 336 | return null; |
| 337 | } |
| 338 | |
| 339 | @Override |
| 340 | public CompletableFuture<Void> relinquishMastership(DeviceId deviceId) { |
| 341 | return null; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public NodeId getMasterFor(DeviceId deviceId) { |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public RoleInfo getNodesFor(DeviceId deviceId) { |
| 351 | return null; |
| 352 | } |
| 353 | |
| 354 | @Override |
| 355 | public MastershipInfo getMastershipFor(DeviceId deviceId) { |
| 356 | return null; |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public Set<DeviceId> getDevicesOf(NodeId nodeId) { |
| 361 | return null; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public void addListener(MastershipListener mastershipListener) { |
| 366 | |
| 367 | } |
| 368 | |
| 369 | @Override |
| 370 | public void removeListener(MastershipListener mastershipListener) { |
| 371 | |
| 372 | } |
| 373 | } |
| 374 | } |