blob: e282347462920e25a2e704768fbdd17906a470bd [file] [log] [blame]
Ray Milkeyc0fa4db2014-10-17 08:49:54 -07001package org.onlab.onos.net.intent.impl;
2
3import java.util.List;
4
5import org.hamcrest.Matchers;
6import org.junit.Test;
7import org.onlab.onos.net.flow.TrafficSelector;
8import org.onlab.onos.net.flow.TrafficTreatment;
9import org.onlab.onos.net.intent.Intent;
10import org.onlab.onos.net.intent.IntentId;
11import org.onlab.onos.net.intent.IntentTestsMocks;
12import org.onlab.onos.net.intent.PathIntent;
13import org.onlab.onos.net.intent.PointToPointIntent;
14
15import static org.hamcrest.CoreMatchers.notNullValue;
16import static org.hamcrest.MatcherAssert.assertThat;
17import static org.hamcrest.Matchers.hasSize;
18import static org.hamcrest.Matchers.is;
19import static org.onlab.onos.net.NetTestTools.connectPoint;
20import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
21
22/**
23 * Unit tests for the HostToHost intent compiler.
24 */
25public class TestPointToPointIntentCompiler {
26
27 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
28 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
29
30 /**
31 * Creates a PointToPoint intent based on ingress and egress device Ids.
32 *
33 * @param ingressIdString string for id of ingress device
34 * @param egressIdString string for id of egress device
35 * @return PointToPointIntent for the two devices
36 */
37 private PointToPointIntent makeIntent(String ingressIdString,
38 String egressIdString) {
39 return new PointToPointIntent(new IntentId(12),
40 selector,
41 treatment,
42 connectPoint(ingressIdString, 1),
43 connectPoint(egressIdString, 1));
44 }
45
46 /**
47 * Creates a compiler for HostToHost intents.
48 *
49 * @param hops string array describing the path hops to use when compiling
50 * @return HostToHost intent compiler
51 */
52 private PointToPointIntentCompiler makeCompiler(String[] hops) {
53 PointToPointIntentCompiler compiler =
54 new PointToPointIntentCompiler();
55 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
56 IdBlockAllocator idBlockAllocator = new DummyIdBlockAllocator();
57 compiler.intentIdGenerator =
58 new IdBlockAllocatorBasedIntentIdGenerator(idBlockAllocator);
59 return compiler;
60 }
61
62
63 /**
64 * Tests a pair of devices in an 8 hop path, forward direction.
65 */
66 @Test
67 public void testForwardPathCompilation() {
68
69 PointToPointIntent intent = makeIntent("d1", "d8");
70 assertThat(intent, is(notNullValue()));
71
72 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
73 PointToPointIntentCompiler compiler = makeCompiler(hops);
74 assertThat(compiler, is(notNullValue()));
75
76 List<Intent> result = compiler.compile(intent);
77 assertThat(result, is(Matchers.notNullValue()));
78 assertThat(result, hasSize(1));
79 Intent forwardResultIntent = result.get(0);
80 assertThat(forwardResultIntent instanceof PathIntent, is(true));
81
82 if (forwardResultIntent instanceof PathIntent) {
83 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
84 // 7 links for the hops, plus one default lnk on ingress and egress
85 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
86 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
87 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
88 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
89 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
90 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
91 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
92 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
93 }
94 }
95
96 /**
97 * Tests a pair of devices in an 8 hop path, forward direction.
98 */
99 @Test
100 public void testReversePathCompilation() {
101
102 PointToPointIntent intent = makeIntent("d8", "d1");
103 assertThat(intent, is(notNullValue()));
104
105 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
106 PointToPointIntentCompiler compiler = makeCompiler(hops);
107 assertThat(compiler, is(notNullValue()));
108
109 List<Intent> result = compiler.compile(intent);
110 assertThat(result, is(Matchers.notNullValue()));
111 assertThat(result, hasSize(1));
112 Intent reverseResultIntent = result.get(0);
113 assertThat(reverseResultIntent instanceof PathIntent, is(true));
114
115 if (reverseResultIntent instanceof PathIntent) {
116 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
117 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
118 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
119 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
120 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
121 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
122 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
123 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
124 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
125 }
126 }
127}