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