blob: fdef627d765a8e9e72e32dddbf5c814f5033eb6e [file] [log] [blame]
Yi Tsengc927a062017-05-02 15:02:37 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yi Tsengc927a062017-05-02 15:02:37 -07003 *
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
17package org.onosproject.net.intent.impl.installer;
18
19import com.google.common.collect.ImmutableList;
20import com.google.common.collect.Lists;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010021import com.google.common.collect.Maps;
Yi Tsengc927a062017-05-02 15:02:37 -070022import com.google.common.collect.Sets;
23import org.junit.After;
24import org.junit.Before;
25import org.junit.Test;
26import org.onlab.packet.VlanId;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010027import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.net.ConnectPoint;
Yi Tsengc927a062017-05-02 15:02:37 -070029import org.onosproject.net.NetworkResource;
30import org.onosproject.net.flow.DefaultFlowRule;
31import org.onosproject.net.flow.DefaultTrafficSelector;
32import org.onosproject.net.flow.DefaultTrafficTreatment;
33import org.onosproject.net.flow.FlowRule;
34import org.onosproject.net.flow.FlowRuleOperations;
35import org.onosproject.net.flow.FlowRuleServiceAdapter;
36import org.onosproject.net.flow.TrafficSelector;
37import org.onosproject.net.flow.TrafficTreatment;
38import org.onosproject.net.intent.FlowRuleIntent;
39import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentData;
41import org.onosproject.net.intent.IntentInstallationContext;
42import org.onosproject.net.intent.IntentOperationContext;
43import org.onosproject.net.intent.IntentState;
44import org.onosproject.net.intent.PathIntent;
45import org.onosproject.store.service.WallClockTimestamp;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010046import org.onosproject.store.trivial.SimpleIntentStore;
Yi Tsengc927a062017-05-02 15:02:37 -070047
48import java.util.Collection;
49import java.util.List;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010050import java.util.Map;
Yi Tsengc927a062017-05-02 15:02:37 -070051import java.util.Set;
52import java.util.stream.Collectors;
53
Antonio Marsico4f68ec92017-03-09 11:16:32 +010054import static org.easymock.EasyMock.mock;
55import static org.junit.Assert.assertEquals;
56import static org.junit.Assert.assertTrue;
Yi Tsengc927a062017-05-02 15:02:37 -070057
58/**
59 * Tests for flow rule Intent installer.
60 */
61public class FlowRuleIntentInstallerTest extends AbstractIntentInstallerTest {
62
63 private TestFlowRuleService flowRuleService;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010064 private TestFlowRuleServiceNonDisruptive flowRuleServiceNonDisruptive;
Yi Tsengc927a062017-05-02 15:02:37 -070065 private FlowRuleIntentInstaller installer;
66
67 @Before
68 public void setup() {
69 super.setup();
70 flowRuleService = new TestFlowRuleService();
Antonio Marsico4f68ec92017-03-09 11:16:32 +010071 flowRuleServiceNonDisruptive = new TestFlowRuleServiceNonDisruptive();
Yi Tsengc927a062017-05-02 15:02:37 -070072 installer = new FlowRuleIntentInstaller();
73 installer.flowRuleService = flowRuleService;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010074 installer.store = new SimpleIntentStore();
Yi Tsengc927a062017-05-02 15:02:37 -070075 installer.intentExtensionService = intentExtensionService;
76 installer.intentInstallCoordinator = intentInstallCoordinator;
77 installer.trackerService = trackerService;
Antonio Marsico4f68ec92017-03-09 11:16:32 +010078 installer.configService = mock(ComponentConfigService.class);
Yi Tsengc927a062017-05-02 15:02:37 -070079
80 installer.activate();
81 }
82
83 @After
84 public void tearDown() {
85 super.tearDown();
86 installer.deactivated();
87 }
88
89 /**
90 * Installs Intents only, no Intents to be uninstall.
91 */
92 @Test
93 public void testInstallOnly() {
94 List<Intent> intentsToUninstall = Lists.newArrayList();
95 List<Intent> intentsToInstall = createFlowRuleIntents();
96
97 IntentData toUninstall = null;
98 IntentData toInstall = new IntentData(createP2PIntent(),
99 IntentState.INSTALLING,
100 new WallClockTimestamp());
101 toInstall = new IntentData(toInstall, intentsToInstall);
102
103
104 IntentOperationContext<FlowRuleIntent> operationContext;
105 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
106 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
107
108 installer.apply(operationContext);
109
110 IntentOperationContext successContext = intentInstallCoordinator.successContext;
111 assertEquals(successContext, operationContext);
112
113 Set<FlowRule> expectedFlowRules = intentsToInstall.stream()
114 .map(intent -> (FlowRuleIntent) intent)
115 .map(FlowRuleIntent::flowRules)
116 .flatMap(Collection::stream)
117 .collect(Collectors.toSet());
118
119 assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd);
120 }
121
122 /**
123 * Uninstalls Intents only, no Intents to be install.
124 */
125 @Test
126 public void testUninstallOnly() {
127 List<Intent> intentsToInstall = Lists.newArrayList();
128 List<Intent> intentsToUninstall = createFlowRuleIntents();
129
130 IntentData toInstall = null;
131 IntentData toUninstall = new IntentData(createP2PIntent(),
132 IntentState.WITHDRAWING,
133 new WallClockTimestamp());
134 toUninstall = new IntentData(toUninstall, intentsToUninstall);
135
136
137 IntentOperationContext<FlowRuleIntent> operationContext;
138 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
139 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
140
141 installer.apply(operationContext);
142
143 IntentOperationContext successContext = intentInstallCoordinator.successContext;
144 assertEquals(successContext, operationContext);
145
146 Set<FlowRule> expectedFlowRules = intentsToUninstall.stream()
147 .map(intent -> (FlowRuleIntent) intent)
148 .map(FlowRuleIntent::flowRules)
149 .flatMap(Collection::stream)
150 .collect(Collectors.toSet());
151
152 assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove);
153 }
154
155 /**
156 * Do both install and uninstall Intents with different flow rules.
157 */
158 @Test
159 public void testUninstallAndInstall() {
160 List<Intent> intentsToInstall = createAnotherFlowRuleIntents();
161 List<Intent> intentsToUninstall = createFlowRuleIntents();
162
163 IntentData toInstall = new IntentData(createP2PIntent(),
164 IntentState.INSTALLING,
165 new WallClockTimestamp());
166 toInstall = new IntentData(toInstall, intentsToInstall);
167 IntentData toUninstall = new IntentData(createP2PIntent(),
168 IntentState.INSTALLED,
169 new WallClockTimestamp());
170 toUninstall = new IntentData(toUninstall, intentsToUninstall);
171
172 IntentOperationContext<FlowRuleIntent> operationContext;
173 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
174 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
175
176 installer.apply(operationContext);
177
178 IntentOperationContext successContext = intentInstallCoordinator.successContext;
179 assertEquals(successContext, operationContext);
180
181 Set<FlowRule> expectedFlowRules = intentsToUninstall.stream()
182 .map(intent -> (FlowRuleIntent) intent)
183 .map(FlowRuleIntent::flowRules)
184 .flatMap(Collection::stream)
185 .collect(Collectors.toSet());
186
187 assertEquals(expectedFlowRules, flowRuleService.flowRulesRemove);
188
189 expectedFlowRules = intentsToInstall.stream()
190 .map(intent -> (FlowRuleIntent) intent)
191 .map(FlowRuleIntent::flowRules)
192 .flatMap(Collection::stream)
193 .collect(Collectors.toSet());
194
195 assertEquals(expectedFlowRules, flowRuleService.flowRulesAdd);
196 }
197
198 /**
199 * Do both install and uninstall Intents with same flow rules.
200 */
201 @Test
202 public void testUninstallAndInstallUnchanged() {
203 List<Intent> intentsToInstall = createFlowRuleIntents();
204 List<Intent> intentsToUninstall = createFlowRuleIntents();
205
206 IntentData toInstall = new IntentData(createP2PIntent(),
207 IntentState.INSTALLING,
208 new WallClockTimestamp());
209 toInstall = new IntentData(toInstall, intentsToInstall);
210 IntentData toUninstall = new IntentData(createP2PIntent(),
211 IntentState.INSTALLED,
212 new WallClockTimestamp());
213 toUninstall = new IntentData(toUninstall, intentsToUninstall);
214
215 IntentOperationContext<FlowRuleIntent> operationContext;
216 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
217 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
218
219 installer.apply(operationContext);
220
221 IntentOperationContext successContext = intentInstallCoordinator.successContext;
222 assertEquals(successContext, operationContext);
223
224 assertEquals(0, flowRuleService.flowRulesRemove.size());
225 assertEquals(0, flowRuleService.flowRulesAdd.size());
Yi Tsengc29d8822017-10-25 16:19:25 -0700226 assertEquals(0, flowRuleService.flowRulesModify.size());
Yi Tsengc927a062017-05-02 15:02:37 -0700227 }
228
229 /**
230 * Do both install and uninstall Intents with same flow rule Intent.
231 */
232 @Test
233 public void testUninstallAndInstallSame() {
234 List<Intent> intentsToInstall = createFlowRuleIntents();
235 List<Intent> intentsToUninstall = intentsToInstall;
236
237 IntentData toInstall = new IntentData(createP2PIntent(),
238 IntentState.INSTALLING,
239 new WallClockTimestamp());
240 toInstall = new IntentData(toInstall, intentsToInstall);
241 IntentData toUninstall = new IntentData(createP2PIntent(),
242 IntentState.INSTALLED,
243 new WallClockTimestamp());
244 toUninstall = new IntentData(toUninstall, intentsToUninstall);
245
246 IntentOperationContext<FlowRuleIntent> operationContext;
247 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
248 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
249
250 installer.apply(operationContext);
251
252 IntentOperationContext successContext = intentInstallCoordinator.successContext;
253 assertEquals(successContext, operationContext);
254
255 assertEquals(0, flowRuleService.flowRulesRemove.size());
256 assertEquals(0, flowRuleService.flowRulesAdd.size());
Yi Tsengc29d8822017-10-25 16:19:25 -0700257 assertEquals(0, flowRuleService.flowRulesModify.size());
Yi Tsengc927a062017-05-02 15:02:37 -0700258 }
259
260 /**
261 * Nothing to uninstall or install.
262 */
263 @Test
264 public void testNoAnyIntentToApply() {
265 IntentData toInstall = null;
266 IntentData toUninstall = null;
267 IntentOperationContext<FlowRuleIntent> operationContext;
268 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
269 operationContext = new IntentOperationContext<>(ImmutableList.of(), ImmutableList.of(), context);
270 installer.apply(operationContext);
271
272 IntentOperationContext successContext = intentInstallCoordinator.successContext;
273 assertEquals(successContext, operationContext);
274
275 assertEquals(0, flowRuleService.flowRulesRemove.size());
276 assertEquals(0, flowRuleService.flowRulesAdd.size());
Yi Tsengc29d8822017-10-25 16:19:25 -0700277 assertEquals(0, flowRuleService.flowRulesModify.size());
Yi Tsengc927a062017-05-02 15:02:37 -0700278 }
279
280 /**
281 * Test if the flow installation failed.
282 */
283 @Test
284 public void testFailed() {
285 installer.flowRuleService = new TestFailedFlowRuleService();
286 List<Intent> intentsToUninstall = Lists.newArrayList();
287 List<Intent> intentsToInstall = createFlowRuleIntents();
288
289 IntentData toUninstall = null;
290 IntentData toInstall = new IntentData(createP2PIntent(),
291 IntentState.INSTALLING,
292 new WallClockTimestamp());
293 toInstall = new IntentData(toInstall, intentsToInstall);
294
295
296 IntentOperationContext<FlowRuleIntent> operationContext;
297 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
298 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
299
300 installer.apply(operationContext);
301
302 IntentOperationContext failedContext = intentInstallCoordinator.failedContext;
303 assertEquals(failedContext, operationContext);
304 }
305
306 /**
Yi Tsengc29d8822017-10-25 16:19:25 -0700307 * Test intents with same match rules, should do modify instead of add.
308 */
309 @Test
310 public void testRuleModify() {
311 List<Intent> intentsToInstall = createFlowRuleIntents();
312 List<Intent> intentsToUninstall = createFlowRuleIntentsWithSameMatch();
313
314 IntentData toInstall = new IntentData(createP2PIntent(),
315 IntentState.INSTALLING,
316 new WallClockTimestamp());
317 toInstall = new IntentData(toInstall, intentsToInstall);
318 IntentData toUninstall = new IntentData(createP2PIntent(),
319 IntentState.INSTALLED,
320 new WallClockTimestamp());
321 toUninstall = new IntentData(toUninstall, intentsToUninstall);
322
323 IntentOperationContext<FlowRuleIntent> operationContext;
324 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
325 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
326
327 installer.apply(operationContext);
328
329 IntentOperationContext successContext = intentInstallCoordinator.successContext;
330 assertEquals(successContext, operationContext);
331
332 assertEquals(0, flowRuleService.flowRulesRemove.size());
333 assertEquals(0, flowRuleService.flowRulesAdd.size());
334 assertEquals(1, flowRuleService.flowRulesModify.size());
335
336 FlowRuleIntent installedIntent = (FlowRuleIntent) intentsToInstall.get(0);
337 assertEquals(flowRuleService.flowRulesModify.size(), installedIntent.flowRules().size());
338 assertTrue(flowRuleService.flowRulesModify.containsAll(installedIntent.flowRules()));
339 }
340
341 /**
Antonio Marsico4f68ec92017-03-09 11:16:32 +0100342 * Testing the non-disruptive reallocation.
343 */
344 @Test
345 public void testUninstallAndInstallNonDisruptive() throws InterruptedException {
346
347 installer.flowRuleService = flowRuleServiceNonDisruptive;
348
349 List<Intent> intentsToInstall = createAnotherFlowRuleIntentsNonDisruptive();
350 List<Intent> intentsToUninstall = createFlowRuleIntentsNonDisruptive();
351
352 IntentData toInstall = new IntentData(createP2PIntentNonDisruptive(),
353 IntentState.INSTALLING,
354 new WallClockTimestamp());
355 toInstall = new IntentData(toInstall, intentsToInstall);
356 IntentData toUninstall = new IntentData(createP2PIntentNonDisruptive(),
357 IntentState.INSTALLED,
358 new WallClockTimestamp());
359 toUninstall = new IntentData(toUninstall, intentsToUninstall);
360
361 IntentOperationContext<FlowRuleIntent> operationContext;
362 IntentInstallationContext context = new IntentInstallationContext(toUninstall, toInstall);
363 operationContext = new IntentOperationContext(intentsToUninstall, intentsToInstall, context);
364
365 installer.apply(operationContext);
366
367 //A single FlowRule is evaluated for every non-disruptive stage
368 TrafficSelector selector = DefaultTrafficSelector.builder()
369 .matchInPhyPort(CP1.port())
370 .build();
371 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
372 .setOutput(CP3.port())
373 .build();
374
375 FlowRule firstStageInstalledRule = DefaultFlowRule.builder()
376 .forDevice(CP1.deviceId())
377 .withSelector(selector)
378 .withTreatment(treatment)
379 .fromApp(APP_ID)
380 .withPriority(DEFAULT_PRIORITY - 1)
381 .makePermanent()
382 .build();
383
384 assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(firstStageInstalledRule));
385
386 selector = DefaultTrafficSelector.builder()
387 .matchInPhyPort(CP4_2.port())
388 .build();
389 treatment = DefaultTrafficTreatment.builder()
390 .setOutput(CP4_1.port())
391 .build();
392
393 FlowRule secondStageUninstalledRule = DefaultFlowRule.builder()
394 .forDevice(CP4_1.deviceId())
395 .withSelector(selector)
396 .withTreatment(treatment)
397 .fromApp(APP_ID)
398 .withPriority(DEFAULT_PRIORITY)
399 .makePermanent()
400 .build();
401
402 assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(secondStageUninstalledRule));
403
404 selector = DefaultTrafficSelector.builder()
405 .matchInPhyPort(CP4_3.port())
406 .build();
407 treatment = DefaultTrafficTreatment.builder()
408 .setOutput(CP4_1.port())
409 .build();
410
411 FlowRule thirdStageInstalledRule = DefaultFlowRule.builder()
412 .forDevice(CP4_1.deviceId())
413 .withSelector(selector)
414 .withTreatment(treatment)
415 .fromApp(APP_ID)
416 .withPriority(DEFAULT_PRIORITY)
417 .makePermanent()
418 .build();
419
420 assertTrue(flowRuleServiceNonDisruptive.flowRulesAdd.contains(thirdStageInstalledRule));
421
422 selector = DefaultTrafficSelector.builder()
423 .matchInPhyPort(CP2_1.port())
424 .build();
425 treatment = DefaultTrafficTreatment.builder()
426 .setOutput(CP2_2.port())
427 .build();
428
429 FlowRule lastStageUninstalledRule = DefaultFlowRule.builder()
430 .forDevice(CP2_1.deviceId())
431 .withSelector(selector)
432 .withTreatment(treatment)
433 .fromApp(APP_ID)
434 .withPriority(DEFAULT_PRIORITY)
435 .makePermanent()
436 .build();
437
438 assertTrue(flowRuleServiceNonDisruptive.flowRulesRemove.contains(lastStageUninstalledRule));
439
440 IntentOperationContext successContext = intentInstallCoordinator.successContext;
441 assertEquals(successContext, operationContext);
442 }
443
444 /**
Yi Tsengc927a062017-05-02 15:02:37 -0700445 * Generates FlowRuleIntents for test.
446 *
447 * @return the FlowRuleIntents for test
448 */
449 public List<Intent> createFlowRuleIntents() {
450 TrafficSelector selector = DefaultTrafficSelector.builder()
451 .matchInPhyPort(CP1.port())
452 .build();
453 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
454 .setOutput(CP2.port())
455 .build();
456
457 FlowRule flowRule = DefaultFlowRule.builder()
458 .forDevice(CP1.deviceId())
459 .withSelector(selector)
460 .withTreatment(treatment)
461 .fromApp(APP_ID)
462 .withPriority(DEFAULT_PRIORITY)
463 .makePermanent()
464 .build();
465
466 List<NetworkResource> resources = ImmutableList.of(CP1.deviceId());
467
468 FlowRuleIntent intent = new FlowRuleIntent(APP_ID,
469 KEY1,
470 ImmutableList.of(flowRule),
471 resources,
472 PathIntent.ProtectionType.PRIMARY,
473 RG1);
474
475 List<Intent> flowRuleIntents = Lists.newArrayList();
476 flowRuleIntents.add(intent);
477
478 return flowRuleIntents;
479 }
480
481 /**
Yi Tsengc29d8822017-10-25 16:19:25 -0700482 * Generates FlowRuleIntents for test. Flow rules in Intent should have same
483 * match as we created by createFlowRuleIntents method, but action will be
484 * different.
485 *
486 * @return the FlowRuleIntents for test
487 */
488 public List<Intent> createFlowRuleIntentsWithSameMatch() {
489 TrafficSelector selector = DefaultTrafficSelector.builder()
490 .matchInPhyPort(CP1.port())
491 .build();
492 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
493 .punt()
494 .build();
495
496 FlowRule flowRule = DefaultFlowRule.builder()
497 .forDevice(CP1.deviceId())
498 .withSelector(selector)
499 .withTreatment(treatment)
500 .fromApp(APP_ID)
501 .withPriority(DEFAULT_PRIORITY)
502 .makePermanent()
503 .build();
504
505 List<NetworkResource> resources = ImmutableList.of(CP1.deviceId());
506
507 FlowRuleIntent intent = new FlowRuleIntent(APP_ID,
508 KEY1,
509 ImmutableList.of(flowRule),
510 resources,
511 PathIntent.ProtectionType.PRIMARY,
512 RG1);
513
514 List<Intent> flowRuleIntents = Lists.newArrayList();
515 flowRuleIntents.add(intent);
516
517 return flowRuleIntents;
518 }
519
520 /**
Yi Tsengc927a062017-05-02 15:02:37 -0700521 * Generates another different FlowRuleIntents for test.
522 *
523 * @return the FlowRuleIntents for test
524 */
525 public List<Intent> createAnotherFlowRuleIntents() {
526 TrafficSelector selector = DefaultTrafficSelector.builder()
527 .matchVlanId(VlanId.vlanId("100"))
528 .matchInPhyPort(CP1.port())
529 .build();
530 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
531 .setOutput(CP2.port())
532 .build();
533
534 FlowRule flowRule = DefaultFlowRule.builder()
535 .forDevice(CP1.deviceId())
536 .withSelector(selector)
537 .withTreatment(treatment)
538 .fromApp(APP_ID)
539 .withPriority(DEFAULT_PRIORITY)
540 .makePermanent()
541 .build();
542
543 List<NetworkResource> resources = ImmutableList.of(CP1.deviceId());
544
545 FlowRuleIntent intent = new FlowRuleIntent(APP_ID,
546 KEY1,
547 ImmutableList.of(flowRule),
548 resources,
549 PathIntent.ProtectionType.PRIMARY,
550 RG1);
551
552 List<Intent> flowRuleIntents = Lists.newArrayList();
553 flowRuleIntents.add(intent);
554
555 return flowRuleIntents;
556 }
557
558 /**
Antonio Marsico4f68ec92017-03-09 11:16:32 +0100559 * Generates FlowRuleIntents for testing non-disruptive reallocation.
560 *
561 * @return the FlowRuleIntents for test
562 */
563 public List<Intent> createFlowRuleIntentsNonDisruptive() {
564
565 Map<ConnectPoint, ConnectPoint> portsAssociation = Maps.newHashMap();
566 portsAssociation.put(CP1, CP2);
567 portsAssociation.put(CP2_1, CP2_2);
568 portsAssociation.put(CP4_2, CP4_1);
569
570 List<FlowRule> flowRules = Lists.newArrayList();
571
572 for (ConnectPoint srcPoint : portsAssociation.keySet()) {
573
574 TrafficSelector selector = DefaultTrafficSelector.builder()
575 .matchInPhyPort(srcPoint.port())
576 .build();
577 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
578 .setOutput(portsAssociation.get(srcPoint).port())
579 .build();
580
581 FlowRule flowRule = DefaultFlowRule.builder()
582 .forDevice(srcPoint.deviceId())
583 .withSelector(selector)
584 .withTreatment(treatment)
585 .fromApp(APP_ID)
586 .withPriority(DEFAULT_PRIORITY)
587 .makePermanent()
588 .build();
589 flowRules.add(flowRule);
590 }
591
592
593
594 List<NetworkResource> resources = ImmutableList.of(S1_S2, S2_S4);
595
596 FlowRuleIntent intent = new FlowRuleIntent(APP_ID,
597 KEY1,
598 flowRules,
599 resources,
600 PathIntent.ProtectionType.PRIMARY,
601 RG1);
602
603 List<Intent> flowRuleIntents = Lists.newArrayList();
604 flowRuleIntents.add(intent);
605
606 return flowRuleIntents;
607 }
608
609 /**
610 * Generates another FlowRuleIntent, going through a different path, for testing non-disruptive reallocation.
611 *
612 * @return the FlowRuleIntents for test
613 */
614 public List<Intent> createAnotherFlowRuleIntentsNonDisruptive() {
615 Map<ConnectPoint, ConnectPoint> portsAssociation = Maps.newHashMap();
616 portsAssociation.put(CP1, CP3);
617 portsAssociation.put(CP3_1, CP3_2);
618 portsAssociation.put(CP4_3, CP4_1);
619
620 List<FlowRule> flowRules = Lists.newArrayList();
621
622 for (ConnectPoint srcPoint : portsAssociation.keySet()) {
623
624 TrafficSelector selector = DefaultTrafficSelector.builder()
625 .matchInPhyPort(srcPoint.port())
626 .build();
627 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
628 .setOutput(portsAssociation.get(srcPoint).port())
629 .build();
630
631 FlowRule flowRule = DefaultFlowRule.builder()
632 .forDevice(srcPoint.deviceId())
633 .withSelector(selector)
634 .withTreatment(treatment)
635 .fromApp(APP_ID)
636 .withPriority(DEFAULT_PRIORITY)
637 .makePermanent()
638 .build();
639 flowRules.add(flowRule);
640 }
641
642
643
644 List<NetworkResource> resources = ImmutableList.of(S1_S3, S3_S4);
645
646 FlowRuleIntent intent = new FlowRuleIntent(APP_ID,
647 KEY1,
648 flowRules,
649 resources,
650 PathIntent.ProtectionType.PRIMARY,
651 RG1);
652
653 List<Intent> flowRuleIntents = Lists.newArrayList();
654 flowRuleIntents.add(intent);
655
656 return flowRuleIntents;
657 }
658
659 /**
Yi Tsengc927a062017-05-02 15:02:37 -0700660 * The FlowRuleService for test; always success for any flow rule operations.
661 */
662 class TestFlowRuleService extends FlowRuleServiceAdapter {
663
664 Set<FlowRule> flowRulesAdd = Sets.newHashSet();
Yi Tsengc29d8822017-10-25 16:19:25 -0700665 Set<FlowRule> flowRulesModify = Sets.newHashSet();
Yi Tsengc927a062017-05-02 15:02:37 -0700666 Set<FlowRule> flowRulesRemove = Sets.newHashSet();
667
668 public void record(FlowRuleOperations ops) {
669 flowRulesAdd.clear();
670 flowRulesRemove.clear();
671 ops.stages().forEach(stage -> {
672 stage.forEach(op -> {
673 switch (op.type()) {
674 case ADD:
675 flowRulesAdd.add(op.rule());
676 break;
677 case REMOVE:
678 flowRulesRemove.add(op.rule());
679 break;
Yi Tsengc29d8822017-10-25 16:19:25 -0700680 case MODIFY:
681 flowRulesModify.add(op.rule());
Yi Tsengc927a062017-05-02 15:02:37 -0700682 default:
683 break;
684 }
685 });
686 });
687 }
688
689 @Override
690 public void apply(FlowRuleOperations ops) {
691 record(ops);
692 ops.callback().onSuccess(ops);
693 }
694 }
695
696 /**
697 * The FlowRuleService for test; always failed for any flow rule operations.
698 */
699 class TestFailedFlowRuleService extends TestFlowRuleService {
700 @Override
701 public void apply(FlowRuleOperations ops) {
702 record(ops);
703 ops.callback().onError(ops);
704 }
705 }
706
Antonio Marsico4f68ec92017-03-09 11:16:32 +0100707 /**
708 * The FlowRuleService for testing non-disruptive reallocation.
709 * It keeps all the FlowRules installed/uninstalled.
710 */
711 class TestFlowRuleServiceNonDisruptive extends FlowRuleServiceAdapter {
712
713 Set<FlowRule> flowRulesAdd = Sets.newHashSet();
714 Set<FlowRule> flowRulesRemove = Sets.newHashSet();
715
716 public void record(FlowRuleOperations ops) {
717 ops.stages().forEach(stage -> {
718 stage.forEach(op -> {
719 switch (op.type()) {
720 case ADD:
721 flowRulesAdd.add(op.rule());
722 break;
723 case REMOVE:
724 flowRulesRemove.add(op.rule());
725 break;
726 default:
727 break;
728 }
729 });
730 });
731 }
732
733 @Override
734 public void apply(FlowRuleOperations ops) {
735 record(ops);
736 ops.callback().onSuccess(ops);
737 }
738 }
739
Yi Tsengc927a062017-05-02 15:02:37 -0700740}