Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Laboratory |
| 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.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.MacAddress; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 26 | import org.onosproject.core.CoreService; |
| 27 | import org.onosproject.net.DefaultLink; |
| 28 | import org.onosproject.net.FilteredConnectPoint; |
| 29 | import org.onosproject.net.Link; |
| 30 | import org.onosproject.net.PortNumber; |
| 31 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 32 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 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.PortCriterion; |
| 37 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 38 | import org.onosproject.net.flowobjective.FlowObjectiveServiceAdapter; |
| 39 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 40 | import org.onosproject.net.flowobjective.NextObjective; |
| 41 | import org.onosproject.net.flowobjective.Objective; |
| 42 | import org.onosproject.net.intent.FlowObjectiveIntent; |
| 43 | import org.onosproject.net.intent.Intent; |
| 44 | import org.onosproject.net.intent.IntentExtensionService; |
| 45 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 46 | import org.onosproject.net.resource.MockResourceService; |
| 47 | import org.onosproject.net.resource.ResourceService; |
| 48 | |
| 49 | import java.util.Collections; |
| 50 | import java.util.List; |
| 51 | import java.util.Set; |
| 52 | |
| 53 | import static org.easymock.EasyMock.*; |
| 54 | import static org.hamcrest.CoreMatchers.hasItem; |
| 55 | import static org.hamcrest.CoreMatchers.instanceOf; |
| 56 | import static org.hamcrest.MatcherAssert.assertThat; |
| 57 | import static org.hamcrest.Matchers.hasSize; |
| 58 | import static org.hamcrest.Matchers.is; |
| 59 | import static org.hamcrest.Matchers.nullValue; |
| 60 | import static org.onosproject.net.Link.Type.DIRECT; |
| 61 | import static org.onosproject.net.NetTestTools.PID; |
| 62 | |
| 63 | public class LinkCollectionIntentObjectiveCompilerTest extends AbstractLinkCollectionTest { |
| 64 | |
| 65 | private LinkCollectionIntentObjectiveCompiler compiler; |
| 66 | private FlowObjectiveServiceAdapter flowObjectiveService; |
| 67 | |
| 68 | private NextObjective nextObjective; |
| 69 | private ForwardingObjective forwardingObjective; |
| 70 | private FilteringObjective filteringObjective; |
| 71 | |
| 72 | private ResourceService resourceService; |
| 73 | |
| 74 | @Before |
| 75 | public void setUp() { |
| 76 | compiler = new LinkCollectionIntentObjectiveCompiler(); |
| 77 | coreService = createMock(CoreService.class); |
| 78 | expect(coreService.registerApplication("org.onosproject.net.intent")) |
| 79 | .andReturn(appId); |
| 80 | flowObjectiveService = new FlowObjectiveServiceAdapter(); |
| 81 | resourceService = new MockResourceService(); |
| 82 | compiler.coreService = coreService; |
| 83 | compiler.flowObjectiveService = flowObjectiveService; |
| 84 | |
| 85 | Intent.bindIdGenerator(idGenerator); |
| 86 | |
| 87 | intentExtensionService = createMock(IntentExtensionService.class); |
| 88 | intentExtensionService.registerCompiler(LinkCollectionIntent.class, compiler); |
| 89 | intentExtensionService.unregisterCompiler(LinkCollectionIntent.class); |
| 90 | |
| 91 | registrator = new IntentConfigurableRegistrator(); |
| 92 | registrator.extensionService = intentExtensionService; |
| 93 | registrator.cfgService = new ComponentConfigAdapter(); |
| 94 | registrator.activate(); |
| 95 | |
| 96 | compiler.registrator = registrator; |
| 97 | compiler.resourceService = resourceService; |
| 98 | |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame^] | 99 | LinkCollectionCompiler.optimizeInstructions = false; |
Yi Tseng | a64f0c8 | 2017-02-03 11:17:15 -0800 | [diff] [blame] | 100 | LinkCollectionCompiler.copyTtl = false; |
| 101 | |
| 102 | replay(coreService, intentExtensionService); |
| 103 | |
| 104 | } |
| 105 | |
| 106 | @After |
| 107 | public void tearDown() { |
| 108 | Intent.unbindIdGenerator(idGenerator); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * We test the proper compilation of a simple link collection intent |
| 113 | * with connect points, empty trivial treatment and empty trivial selector. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testCompile() { |
| 117 | compiler.activate(); |
| 118 | |
| 119 | LinkCollectionIntent intent = LinkCollectionIntent.builder() |
| 120 | .appId(appId) |
| 121 | .selector(selector) |
| 122 | .treatment(treatment) |
| 123 | .links(links) |
| 124 | .filteredIngressPoints(ImmutableSet.of(new FilteredConnectPoint(d1p1))) |
| 125 | .filteredEgressPoints(ImmutableSet.of(new FilteredConnectPoint(d3p1))) |
| 126 | .build(); |
| 127 | |
| 128 | List<Intent> result = compiler.compile(intent, Collections.emptyList()); |
| 129 | assertThat(result, hasSize(1)); |
| 130 | assertThat(result.get(0), instanceOf(FlowObjectiveIntent.class)); |
| 131 | |
| 132 | FlowObjectiveIntent foIntent = (FlowObjectiveIntent) result.get(0); |
| 133 | List<Objective> objectives = foIntent.objectives(); |
| 134 | assertThat(objectives, hasSize(9)); |
| 135 | |
| 136 | /* |
| 137 | * First set of objective |
| 138 | */ |
| 139 | filteringObjective = (FilteringObjective) objectives.get(0); |
| 140 | forwardingObjective = (ForwardingObjective) objectives.get(1); |
| 141 | nextObjective = (NextObjective) objectives.get(2); |
| 142 | |
| 143 | // expect selector and treatment |
| 144 | TrafficSelector expectSelector = DefaultTrafficSelector.builder() |
| 145 | .matchInPort(PortNumber.portNumber(1)) |
| 146 | .build(); |
| 147 | TrafficTreatment expectTreatment = DefaultTrafficTreatment.builder() |
| 148 | .setOutput(PortNumber.portNumber(1)) |
| 149 | .build(); |
| 150 | PortCriterion inPortCriterion = |
| 151 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 152 | |
| 153 | // test case for first filtering objective |
| 154 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 155 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 156 | assertThat(filteringObjective.meta(), nullValue()); |
| 157 | assertThat(filteringObjective.appId(), is(appId)); |
| 158 | assertThat(filteringObjective.permanent(), is(true)); |
| 159 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 160 | |
| 161 | // test case for first next objective |
| 162 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 163 | assertThat(nextObjective.next(), hasSize(1)); |
| 164 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 165 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 166 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 167 | |
| 168 | // test case for first forwarding objective |
| 169 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 170 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 171 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 172 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 173 | |
| 174 | /* |
| 175 | * Second set of objective |
| 176 | */ |
| 177 | filteringObjective = (FilteringObjective) objectives.get(3); |
| 178 | forwardingObjective = (ForwardingObjective) objectives.get(4); |
| 179 | nextObjective = (NextObjective) objectives.get(5); |
| 180 | |
| 181 | |
| 182 | expectSelector = DefaultTrafficSelector.builder() |
| 183 | .matchInPort(PortNumber.portNumber(0)) |
| 184 | .build(); |
| 185 | |
| 186 | inPortCriterion = |
| 187 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 188 | |
| 189 | // test case for first filtering objective |
| 190 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 191 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 192 | assertThat(filteringObjective.meta(), nullValue()); |
| 193 | assertThat(filteringObjective.appId(), is(appId)); |
| 194 | assertThat(filteringObjective.permanent(), is(true)); |
| 195 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 196 | |
| 197 | // test case for second next objective |
| 198 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 199 | assertThat(nextObjective.next(), hasSize(1)); |
| 200 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 201 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 202 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 203 | |
| 204 | // test case for second forwarding objective |
| 205 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 206 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 207 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 208 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 209 | |
| 210 | /* |
| 211 | * 3rd set of objective |
| 212 | */ |
| 213 | filteringObjective = (FilteringObjective) objectives.get(6); |
| 214 | forwardingObjective = (ForwardingObjective) objectives.get(7); |
| 215 | nextObjective = (NextObjective) objectives.get(8); |
| 216 | |
| 217 | expectSelector = DefaultTrafficSelector.builder() |
| 218 | .matchInPort(PortNumber.portNumber(1)) |
| 219 | .build(); |
| 220 | |
| 221 | inPortCriterion = |
| 222 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 223 | |
| 224 | // test case for first filtering objective |
| 225 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 226 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 227 | assertThat(filteringObjective.meta(), nullValue()); |
| 228 | assertThat(filteringObjective.appId(), is(appId)); |
| 229 | assertThat(filteringObjective.permanent(), is(true)); |
| 230 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 231 | |
| 232 | // test case for 3rd next objective |
| 233 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 234 | assertThat(nextObjective.next(), hasSize(1)); |
| 235 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 236 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 237 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 238 | |
| 239 | // test case for 3rd forwarding objective |
| 240 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 241 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 242 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 243 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 244 | |
| 245 | compiler.deactivate(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Single point to multi point case. Scenario is the follow: |
| 250 | * |
| 251 | * -1 of1 2-1 of2 2--1 of3 2- |
| 252 | * 3 |
| 253 | * `-1 of4 2- |
| 254 | * |
| 255 | * We test the proper compilation of sp2mp with trivial selector, |
| 256 | * empty treatment and points. |
| 257 | * |
| 258 | */ |
| 259 | @Test |
| 260 | public void testFilteredConnectPointForSp() { |
| 261 | compiler.activate(); |
| 262 | Set<Link> testLinks = ImmutableSet.of( |
| 263 | DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(), |
| 264 | DefaultLink.builder().providerId(PID).src(of2p2).dst(of3p1).type(DIRECT).build(), |
| 265 | DefaultLink.builder().providerId(PID).src(of2p3).dst(of4p1).type(DIRECT).build() |
| 266 | ); |
| 267 | |
| 268 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 269 | new FilteredConnectPoint(of1p1, vlan100Selector) |
| 270 | ); |
| 271 | |
| 272 | Set<FilteredConnectPoint> egress = ImmutableSet.of( |
| 273 | new FilteredConnectPoint(of3p2, vlan100Selector), |
| 274 | new FilteredConnectPoint(of4p2, vlan100Selector) |
| 275 | ); |
| 276 | |
| 277 | TrafficSelector broadcastSelector = DefaultTrafficSelector.builder() |
| 278 | .matchEthDst(MacAddress.BROADCAST) |
| 279 | .build(); |
| 280 | |
| 281 | LinkCollectionIntent intent = LinkCollectionIntent.builder() |
| 282 | .appId(appId) |
| 283 | .selector(broadcastSelector) |
| 284 | .treatment(treatment) |
| 285 | .links(testLinks) |
| 286 | .filteredIngressPoints(ingress) |
| 287 | .filteredEgressPoints(egress) |
| 288 | .applyTreatmentOnEgress(true) |
| 289 | .resourceGroup(resourceGroup1) |
| 290 | .build(); |
| 291 | |
| 292 | List<Intent> result = compiler.compile(intent, Collections.emptyList()); |
| 293 | assertThat(result, hasSize(1)); |
| 294 | assertThat(result.get(0), instanceOf(FlowObjectiveIntent.class)); |
| 295 | |
| 296 | FlowObjectiveIntent foIntent = (FlowObjectiveIntent) result.get(0); |
| 297 | List<Objective> objectives = foIntent.objectives(); |
| 298 | assertThat(objectives, hasSize(12)); |
| 299 | |
| 300 | /* |
| 301 | * First set of objective |
| 302 | */ |
| 303 | filteringObjective = (FilteringObjective) objectives.get(0); |
| 304 | forwardingObjective = (ForwardingObjective) objectives.get(1); |
| 305 | nextObjective = (NextObjective) objectives.get(2); |
| 306 | |
| 307 | // expect selector and treatment |
| 308 | TrafficSelector expectSelector = DefaultTrafficSelector.builder() |
| 309 | .matchInPort(PortNumber.portNumber(1)) |
| 310 | .matchVlanId(VlanId.vlanId("100")) |
| 311 | .matchEthDst(MacAddress.BROADCAST) |
| 312 | .build(); |
| 313 | TrafficTreatment expectTreatment = DefaultTrafficTreatment.builder() |
| 314 | .setOutput(PortNumber.portNumber(2)) |
| 315 | .build(); |
| 316 | |
| 317 | PortCriterion inPortCriterion = |
| 318 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 319 | |
| 320 | // test case for first filtering objective |
| 321 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 322 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 323 | assertThat(filteringObjective.meta(), nullValue()); |
| 324 | assertThat(filteringObjective.appId(), is(appId)); |
| 325 | assertThat(filteringObjective.permanent(), is(true)); |
| 326 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 327 | |
| 328 | // test case for first next objective |
| 329 | assertThat(nextObjective.type(), is(NextObjective.Type.BROADCAST)); |
| 330 | assertThat(nextObjective.next(), hasSize(1)); |
| 331 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 332 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 333 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 334 | |
| 335 | // test case for first forwarding objective |
| 336 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 337 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 338 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 339 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 340 | |
| 341 | /* |
| 342 | * Second set of objective |
| 343 | */ |
| 344 | filteringObjective = (FilteringObjective) objectives.get(3); |
| 345 | forwardingObjective = (ForwardingObjective) objectives.get(4); |
| 346 | nextObjective = (NextObjective) objectives.get(5); |
| 347 | |
| 348 | inPortCriterion = |
| 349 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 350 | |
| 351 | // test case for first filtering objective |
| 352 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 353 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 354 | assertThat(filteringObjective.meta(), nullValue()); |
| 355 | assertThat(filteringObjective.appId(), is(appId)); |
| 356 | assertThat(filteringObjective.permanent(), is(true)); |
| 357 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 358 | |
| 359 | // test case for second next objective |
| 360 | assertThat(nextObjective.type(), is(NextObjective.Type.BROADCAST)); |
| 361 | assertThat(nextObjective.next(), hasSize(1)); |
| 362 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 363 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 364 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 365 | |
| 366 | // test case for second forwarding objective |
| 367 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 368 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 369 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 370 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 371 | |
| 372 | /* |
| 373 | * 3rd set of objective |
| 374 | */ |
| 375 | filteringObjective = (FilteringObjective) objectives.get(6); |
| 376 | forwardingObjective = (ForwardingObjective) objectives.get(7); |
| 377 | nextObjective = (NextObjective) objectives.get(8); |
| 378 | |
| 379 | inPortCriterion = |
| 380 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 381 | |
| 382 | // test case for first filtering objective |
| 383 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 384 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 385 | assertThat(filteringObjective.meta(), nullValue()); |
| 386 | assertThat(filteringObjective.appId(), is(appId)); |
| 387 | assertThat(filteringObjective.permanent(), is(true)); |
| 388 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 389 | |
| 390 | // test case for 3rd next objective |
| 391 | assertThat(nextObjective.type(), is(NextObjective.Type.BROADCAST)); |
| 392 | assertThat(nextObjective.next(), hasSize(1)); |
| 393 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 394 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 395 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 396 | |
| 397 | // test case for 3rd forwarding objective |
| 398 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 399 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 400 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 401 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 402 | |
| 403 | /* |
| 404 | * 4th set of objective |
| 405 | */ |
| 406 | filteringObjective = (FilteringObjective) objectives.get(9); |
| 407 | forwardingObjective = (ForwardingObjective) objectives.get(10); |
| 408 | nextObjective = (NextObjective) objectives.get(11); |
| 409 | |
| 410 | inPortCriterion = |
| 411 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 412 | |
| 413 | // test case for first filtering objective |
| 414 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 415 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 416 | assertThat(filteringObjective.meta(), nullValue()); |
| 417 | assertThat(filteringObjective.appId(), is(appId)); |
| 418 | assertThat(filteringObjective.permanent(), is(true)); |
| 419 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 420 | |
| 421 | // test case for 3rd next objective |
| 422 | assertThat(nextObjective.type(), is(NextObjective.Type.BROADCAST)); |
| 423 | |
| 424 | // have 2 treatments in this objective |
| 425 | assertThat(nextObjective.next(), hasSize(2)); |
| 426 | expectTreatment = DefaultTrafficTreatment.builder() |
| 427 | .setOutput(PortNumber.portNumber(2)) |
| 428 | .build(); |
| 429 | assertThat(nextObjective.next(), hasItem(expectTreatment)); |
| 430 | |
| 431 | expectTreatment = DefaultTrafficTreatment.builder() |
| 432 | .setOutput(PortNumber.portNumber(3)) |
| 433 | .build(); |
| 434 | assertThat(nextObjective.next(), hasItem(expectTreatment)); |
| 435 | |
| 436 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 437 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 438 | |
| 439 | // test case for 3rd forwarding objective |
| 440 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 441 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 442 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 443 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 444 | |
| 445 | compiler.deactivate(); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Multi point to single point intent with filtered connect point. |
| 450 | * Scenario is the follow: |
| 451 | * |
| 452 | * -1 of1 2-1 of2 2-1 of4 2- |
| 453 | * 3 |
| 454 | * -1 of3 2---/ |
| 455 | * |
| 456 | */ |
| 457 | @Test |
| 458 | public void testFilteredConnectPointForMp() { |
| 459 | compiler.activate(); |
| 460 | Set<Link> testLinks = ImmutableSet.of( |
| 461 | DefaultLink.builder().providerId(PID).src(of1p2).dst(of2p1).type(DIRECT).build(), |
| 462 | DefaultLink.builder().providerId(PID).src(of3p2).dst(of2p3).type(DIRECT).build(), |
| 463 | DefaultLink.builder().providerId(PID).src(of2p2).dst(of4p1).type(DIRECT).build() |
| 464 | ); |
| 465 | |
| 466 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 467 | new FilteredConnectPoint(of3p1, vlan100Selector), |
| 468 | new FilteredConnectPoint(of1p1, vlan100Selector) |
| 469 | ); |
| 470 | |
| 471 | Set<FilteredConnectPoint> egress = ImmutableSet.of( |
| 472 | new FilteredConnectPoint(of4p2, vlan100Selector) |
| 473 | ); |
| 474 | |
| 475 | |
| 476 | |
| 477 | LinkCollectionIntent intent = LinkCollectionIntent.builder() |
| 478 | .appId(appId) |
| 479 | .selector(ethDstSelector) |
| 480 | .treatment(treatment) |
| 481 | .links(testLinks) |
| 482 | .filteredIngressPoints(ingress) |
| 483 | .filteredEgressPoints(egress) |
| 484 | .build(); |
| 485 | |
| 486 | List<Intent> result = compiler.compile(intent, Collections.emptyList()); |
| 487 | assertThat(result, hasSize(1)); |
| 488 | assertThat(result.get(0), instanceOf(FlowObjectiveIntent.class)); |
| 489 | |
| 490 | FlowObjectiveIntent foIntent = (FlowObjectiveIntent) result.get(0); |
| 491 | List<Objective> objectives = foIntent.objectives(); |
| 492 | assertThat(objectives, hasSize(15)); |
| 493 | |
| 494 | TrafficSelector expectSelector = DefaultTrafficSelector |
| 495 | .builder(ethDstSelector) |
| 496 | .matchInPort(PortNumber.portNumber(1)) |
| 497 | .matchVlanId(VlanId.vlanId("100")) |
| 498 | .build(); |
| 499 | |
| 500 | TrafficTreatment expectTreatment = DefaultTrafficTreatment.builder() |
| 501 | .setOutput(PortNumber.portNumber(2)) |
| 502 | .build(); |
| 503 | |
| 504 | /* |
| 505 | * First set of objective |
| 506 | */ |
| 507 | filteringObjective = (FilteringObjective) objectives.get(0); |
| 508 | forwardingObjective = (ForwardingObjective) objectives.get(1); |
| 509 | nextObjective = (NextObjective) objectives.get(2); |
| 510 | |
| 511 | PortCriterion inPortCriterion = |
| 512 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 513 | |
| 514 | // test case for first filtering objective |
| 515 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 516 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 517 | assertThat(filteringObjective.meta(), nullValue()); |
| 518 | assertThat(filteringObjective.appId(), is(appId)); |
| 519 | assertThat(filteringObjective.permanent(), is(true)); |
| 520 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 521 | |
| 522 | // test case for first next objective |
| 523 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 524 | assertThat(nextObjective.next(), hasSize(1)); |
| 525 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 526 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 527 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 528 | |
| 529 | // test case for first forwarding objective |
| 530 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 531 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 532 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 533 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 534 | |
| 535 | /* |
| 536 | * Second set of objective |
| 537 | */ |
| 538 | filteringObjective = (FilteringObjective) objectives.get(3); |
| 539 | forwardingObjective = (ForwardingObjective) objectives.get(4); |
| 540 | nextObjective = (NextObjective) objectives.get(5); |
| 541 | |
| 542 | // test case for first filtering objective |
| 543 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 544 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 545 | assertThat(filteringObjective.meta(), nullValue()); |
| 546 | assertThat(filteringObjective.appId(), is(appId)); |
| 547 | assertThat(filteringObjective.permanent(), is(true)); |
| 548 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 549 | |
| 550 | // test case for first next objective |
| 551 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 552 | assertThat(nextObjective.next(), hasSize(1)); |
| 553 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 554 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 555 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 556 | |
| 557 | // test case for first forwarding objective |
| 558 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 559 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 560 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 561 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 562 | |
| 563 | /* |
| 564 | * 3rd set of objective |
| 565 | */ |
| 566 | filteringObjective = (FilteringObjective) objectives.get(6); |
| 567 | forwardingObjective = (ForwardingObjective) objectives.get(7); |
| 568 | nextObjective = (NextObjective) objectives.get(8); |
| 569 | |
| 570 | // test case for first filtering objective |
| 571 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 572 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 573 | assertThat(filteringObjective.meta(), nullValue()); |
| 574 | assertThat(filteringObjective.appId(), is(appId)); |
| 575 | assertThat(filteringObjective.permanent(), is(true)); |
| 576 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 577 | |
| 578 | // test case for first next objective |
| 579 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 580 | assertThat(nextObjective.next(), hasSize(1)); |
| 581 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 582 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 583 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 584 | |
| 585 | // test case for first forwarding objective |
| 586 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 587 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 588 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 589 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 590 | |
| 591 | /* |
| 592 | * 4th set of objective |
| 593 | */ |
| 594 | filteringObjective = (FilteringObjective) objectives.get(9); |
| 595 | forwardingObjective = (ForwardingObjective) objectives.get(10); |
| 596 | nextObjective = (NextObjective) objectives.get(11); |
| 597 | |
| 598 | // test case for first filtering objective |
| 599 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 600 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 601 | assertThat(filteringObjective.meta(), nullValue()); |
| 602 | assertThat(filteringObjective.appId(), is(appId)); |
| 603 | assertThat(filteringObjective.permanent(), is(true)); |
| 604 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 605 | |
| 606 | // test case for first next objective |
| 607 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 608 | assertThat(nextObjective.next(), hasSize(1)); |
| 609 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 610 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 611 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 612 | |
| 613 | // test case for first forwarding objective |
| 614 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 615 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 616 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 617 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 618 | |
| 619 | /* |
| 620 | * 5th set of objective |
| 621 | */ |
| 622 | filteringObjective = (FilteringObjective) objectives.get(12); |
| 623 | forwardingObjective = (ForwardingObjective) objectives.get(13); |
| 624 | nextObjective = (NextObjective) objectives.get(14); |
| 625 | |
| 626 | expectSelector = DefaultTrafficSelector.builder(ethDstSelector) |
| 627 | .matchVlanId(VlanId.vlanId("100")) |
| 628 | .matchInPort(PortNumber.portNumber(3)) |
| 629 | .build(); |
| 630 | |
| 631 | inPortCriterion = |
| 632 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 633 | |
| 634 | // test case for first filtering objective |
| 635 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 636 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 637 | assertThat(filteringObjective.meta(), nullValue()); |
| 638 | assertThat(filteringObjective.appId(), is(appId)); |
| 639 | assertThat(filteringObjective.permanent(), is(true)); |
| 640 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 641 | |
| 642 | // test case for first next objective |
| 643 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 644 | assertThat(nextObjective.next(), hasSize(1)); |
| 645 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 646 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 647 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 648 | |
| 649 | // test case for first forwarding objective |
| 650 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 651 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 652 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 653 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 654 | compiler.deactivate(); |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * Multiple point to single point intent with only one switch. |
| 659 | * We test the proper compilation of mp2sp with |
| 660 | * trivial selector, trivial treatment and 1 hop. |
| 661 | */ |
| 662 | @Test |
| 663 | public void singleHopTestForMp() { |
| 664 | compiler.activate(); |
| 665 | Set<Link> testLinks = ImmutableSet.of(); |
| 666 | |
| 667 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 668 | new FilteredConnectPoint(of1p1, vlan100Selector), |
| 669 | new FilteredConnectPoint(of1p2, vlan100Selector) |
| 670 | ); |
| 671 | |
| 672 | Set<FilteredConnectPoint> egress = ImmutableSet.of( |
| 673 | new FilteredConnectPoint(of1p3, vlan100Selector) |
| 674 | ); |
| 675 | |
| 676 | |
| 677 | LinkCollectionIntent intent = LinkCollectionIntent.builder() |
| 678 | .appId(appId) |
| 679 | .selector(ethDstSelector) |
| 680 | .treatment(treatment) |
| 681 | .links(testLinks) |
| 682 | .filteredIngressPoints(ingress) |
| 683 | .filteredEgressPoints(egress) |
| 684 | .build(); |
| 685 | |
| 686 | List<Intent> result = compiler.compile(intent, Collections.emptyList()); |
| 687 | assertThat(result, hasSize(1)); |
| 688 | assertThat(result.get(0), instanceOf(FlowObjectiveIntent.class)); |
| 689 | |
| 690 | FlowObjectiveIntent foIntent = (FlowObjectiveIntent) result.get(0); |
| 691 | List<Objective> objectives = foIntent.objectives(); |
| 692 | assertThat(objectives, hasSize(6)); |
| 693 | |
| 694 | TrafficSelector expectSelector = DefaultTrafficSelector |
| 695 | .builder(ethDstSelector) |
| 696 | .matchInPort(PortNumber.portNumber(1)) |
| 697 | .matchVlanId(VlanId.vlanId("100")) |
| 698 | .build(); |
| 699 | |
| 700 | TrafficTreatment expectTreatment = DefaultTrafficTreatment.builder() |
| 701 | .setOutput(PortNumber.portNumber(3)) |
| 702 | .build(); |
| 703 | |
| 704 | /* |
| 705 | * First set of objective |
| 706 | */ |
| 707 | filteringObjective = (FilteringObjective) objectives.get(0); |
| 708 | forwardingObjective = (ForwardingObjective) objectives.get(1); |
| 709 | nextObjective = (NextObjective) objectives.get(2); |
| 710 | |
| 711 | PortCriterion inPortCriterion = |
| 712 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 713 | |
| 714 | // test case for first filtering objective |
| 715 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 716 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 717 | assertThat(filteringObjective.meta(), nullValue()); |
| 718 | assertThat(filteringObjective.appId(), is(appId)); |
| 719 | assertThat(filteringObjective.permanent(), is(true)); |
| 720 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 721 | |
| 722 | // test case for first next objective |
| 723 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 724 | assertThat(nextObjective.next(), hasSize(1)); |
| 725 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 726 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 727 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 728 | |
| 729 | // test case for first forwarding objective |
| 730 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 731 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 732 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 733 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 734 | |
| 735 | /* |
| 736 | * Second set of objective |
| 737 | */ |
| 738 | filteringObjective = (FilteringObjective) objectives.get(3); |
| 739 | forwardingObjective = (ForwardingObjective) objectives.get(4); |
| 740 | nextObjective = (NextObjective) objectives.get(5); |
| 741 | |
| 742 | expectSelector = DefaultTrafficSelector.builder(ethDstSelector) |
| 743 | .matchInPort(PortNumber.portNumber(2)) |
| 744 | .matchVlanId(VlanId.vlanId("100")) |
| 745 | .build(); |
| 746 | |
| 747 | inPortCriterion = |
| 748 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 749 | |
| 750 | // test case for first filtering objective |
| 751 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 752 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 753 | assertThat(filteringObjective.meta(), nullValue()); |
| 754 | assertThat(filteringObjective.appId(), is(appId)); |
| 755 | assertThat(filteringObjective.permanent(), is(true)); |
| 756 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 757 | |
| 758 | // test case for first next objective |
| 759 | assertThat(nextObjective.type(), is(NextObjective.Type.SIMPLE)); |
| 760 | assertThat(nextObjective.next(), hasSize(1)); |
| 761 | assertThat(nextObjective.next().iterator().next(), is(expectTreatment)); |
| 762 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 763 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 764 | |
| 765 | // test case for first forwarding objective |
| 766 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 767 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 768 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 769 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 770 | compiler.deactivate(); |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Single point to multiple point intent with only one switch. |
| 775 | * We test the proper compilation of sp2mp with |
| 776 | * trivial selector, trivial treatment and 1 hop. |
| 777 | */ |
| 778 | @Test |
| 779 | public void singleHopTestForSp() { |
| 780 | compiler.activate(); |
| 781 | Set<Link> testLinks = ImmutableSet.of(); |
| 782 | |
| 783 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 784 | new FilteredConnectPoint(of1p1, vlan100Selector) |
| 785 | ); |
| 786 | |
| 787 | Set<FilteredConnectPoint> egress = ImmutableSet.of( |
| 788 | new FilteredConnectPoint(of1p2, vlan100Selector), |
| 789 | new FilteredConnectPoint(of1p3, vlan100Selector) |
| 790 | ); |
| 791 | |
| 792 | |
| 793 | LinkCollectionIntent intent = LinkCollectionIntent.builder() |
| 794 | .appId(appId) |
| 795 | .selector(ethDstSelector) |
| 796 | .treatment(treatment) |
| 797 | .links(testLinks) |
| 798 | .filteredIngressPoints(ingress) |
| 799 | .filteredEgressPoints(egress) |
| 800 | .applyTreatmentOnEgress(true) |
| 801 | .resourceGroup(resourceGroup2) |
| 802 | .build(); |
| 803 | |
| 804 | List<Intent> result = compiler.compile(intent, Collections.emptyList()); |
| 805 | assertThat(result, hasSize(1)); |
| 806 | assertThat(result.get(0), instanceOf(FlowObjectiveIntent.class)); |
| 807 | |
| 808 | FlowObjectiveIntent foIntent = (FlowObjectiveIntent) result.get(0); |
| 809 | List<Objective> objectives = foIntent.objectives(); |
| 810 | assertThat(objectives, hasSize(3)); |
| 811 | |
| 812 | TrafficSelector expectSelector = DefaultTrafficSelector |
| 813 | .builder(ethDstSelector) |
| 814 | .matchInPort(PortNumber.portNumber(1)) |
| 815 | .matchVlanId(VlanId.vlanId("100")) |
| 816 | .build(); |
| 817 | |
| 818 | TrafficTreatment expectTreatment = DefaultTrafficTreatment.builder() |
| 819 | .setOutput(PortNumber.portNumber(3)) |
| 820 | .build(); |
| 821 | |
| 822 | /* |
| 823 | * First set of objective |
| 824 | */ |
| 825 | filteringObjective = (FilteringObjective) objectives.get(0); |
| 826 | forwardingObjective = (ForwardingObjective) objectives.get(1); |
| 827 | nextObjective = (NextObjective) objectives.get(2); |
| 828 | |
| 829 | PortCriterion inPortCriterion = |
| 830 | (PortCriterion) expectSelector.getCriterion(Criterion.Type.IN_PORT); |
| 831 | |
| 832 | // test case for first filtering objective |
| 833 | assertThat(filteringObjective.key(), is(inPortCriterion)); |
| 834 | assertThat(filteringObjective.priority(), is(intent.priority())); |
| 835 | assertThat(filteringObjective.meta(), nullValue()); |
| 836 | assertThat(filteringObjective.appId(), is(appId)); |
| 837 | assertThat(filteringObjective.permanent(), is(true)); |
| 838 | assertThat(filteringObjective.conditions(), is(Lists.newArrayList(expectSelector.criteria()))); |
| 839 | |
| 840 | // test case for first next objective |
| 841 | assertThat(nextObjective.type(), is(NextObjective.Type.BROADCAST)); |
| 842 | assertThat(nextObjective.next(), hasSize(2)); |
| 843 | assertThat(nextObjective.next(), hasItem(expectTreatment)); |
| 844 | expectTreatment = DefaultTrafficTreatment.builder() |
| 845 | .setOutput(PortNumber.portNumber(3)) |
| 846 | .build(); |
| 847 | assertThat(nextObjective.next(), hasItem(expectTreatment)); |
| 848 | assertThat(nextObjective.meta(), is(expectSelector)); |
| 849 | assertThat(nextObjective.op(), is(Objective.Operation.ADD)); |
| 850 | |
| 851 | // test case for first forwarding objective |
| 852 | assertThat(forwardingObjective.op(), is(Objective.Operation.ADD)); |
| 853 | assertThat(forwardingObjective.selector(), is(expectSelector)); |
| 854 | assertThat(forwardingObjective.nextId(), is(nextObjective.id())); |
| 855 | assertThat(forwardingObjective.flag(), is(ForwardingObjective.Flag.SPECIFIC)); |
| 856 | |
| 857 | compiler.deactivate(); |
| 858 | |
| 859 | } |
| 860 | |
| 861 | |
| 862 | } |