blob: 1c78e26b065995874e5e4f44ea9bbaae48b9ba0f [file] [log] [blame]
Ray Milkeye6684082014-10-16 16:59:47 -07001package org.onlab.onos.net.intent.impl;
2
Ray Milkeye6684082014-10-16 16:59:47 -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 Milkeye6684082014-10-16 16:59:47 -07007import org.onlab.onos.net.ConnectPoint;
8import org.onlab.onos.net.ElementId;
9import org.onlab.onos.net.Path;
10import org.onlab.onos.net.flow.TrafficSelector;
11import org.onlab.onos.net.flow.TrafficTreatment;
12import org.onlab.onos.net.intent.Intent;
Ray Milkeye6684082014-10-16 16:59:47 -070013import org.onlab.onos.net.intent.IntentTestsMocks;
14import org.onlab.onos.net.intent.LinkCollectionIntent;
15import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
16import org.onlab.onos.net.topology.LinkWeight;
17import org.onlab.onos.net.topology.PathService;
18
Thomas Vachuskab97cf282014-10-20 23:31:12 -070019import java.util.HashSet;
20import java.util.List;
21import java.util.Set;
22
Ray Milkeye6684082014-10-16 16:59:47 -070023import static org.hamcrest.CoreMatchers.notNullValue;
24import static org.hamcrest.MatcherAssert.assertThat;
25import static org.hamcrest.Matchers.hasSize;
26import static org.hamcrest.Matchers.is;
27import static org.onlab.onos.net.NetTestTools.connectPoint;
28import static org.onlab.onos.net.NetTestTools.createPath;
29import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
30
31/**
32 * Unit tests for the MultiPointToSinglePoint intent compiler.
33 */
34public class TestMultiPointToSinglePointIntentCompiler {
35
Thomas Vachuskab97cf282014-10-20 23:31:12 -070036 private static final ApplicationId APPID = new TestApplicationId("foo");
37
Ray Milkeye6684082014-10-16 16:59:47 -070038 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
39 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
40
41 /**
42 * Mock path service for creating paths within the test.
43 */
44 private static class MockPathService implements PathService {
45
46 final String[] pathHops;
47
48 /**
49 * Constructor that provides a set of hops to mock.
50 *
51 * @param pathHops path hops to mock
52 */
53 MockPathService(String[] pathHops) {
54 this.pathHops = pathHops;
55 }
56
57 @Override
58 public Set<Path> getPaths(ElementId src, ElementId dst) {
59 Set<Path> result = new HashSet<>();
60
61 String[] allHops = new String[pathHops.length + 1];
62 allHops[0] = src.toString();
63 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
64
65 result.add(createPath(allHops));
66 return result;
67 }
68
69 @Override
70 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
71 return null;
72 }
73 }
74
75 /**
76 * Creates a MultiPointToSinglePoint intent for a group of ingress points
77 * and an egress point.
78 *
79 * @param ingressIds array of ingress device ids
Thomas Vachuskab97cf282014-10-20 23:31:12 -070080 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -070081 * @return MultiPointToSinglePoint intent
82 */
83 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
84 Set<ConnectPoint> ingressPoints = new HashSet<>();
85 ConnectPoint egressPoint = connectPoint(egressId, 1);
86
87 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070088 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -070089 }
90
Thomas Vachuskab97cf282014-10-20 23:31:12 -070091 return new MultiPointToSinglePointIntent(APPID, selector, treatment,
92 ingressPoints, egressPoint);
Ray Milkeye6684082014-10-16 16:59:47 -070093 }
94
95 /**
96 * Creates a compiler for MultiPointToSinglePoint intents.
97 *
98 * @param hops hops to use while computing paths for this intent
99 * @return MultiPointToSinglePoint intent
100 */
101 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
102 MultiPointToSinglePointIntentCompiler compiler =
103 new MultiPointToSinglePointIntentCompiler();
104 compiler.pathService = new MockPathService(hops);
Ray Milkeye6684082014-10-16 16:59:47 -0700105 return compiler;
106 }
107
108 /**
109 * Tests a single ingress point with 8 hops to its egress point.
110 */
111 @Test
112 public void testSingleLongPathCompilation() {
113
114 String[] ingress = {"ingress"};
115 String egress = "egress";
116
117 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
118 assertThat(intent, is(notNullValue()));
119
120 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700121 egress};
Ray Milkeye6684082014-10-16 16:59:47 -0700122 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
123 assertThat(compiler, is(notNullValue()));
124
125 List<Intent> result = compiler.compile(intent);
126 assertThat(result, is(Matchers.notNullValue()));
127 assertThat(result, hasSize(1));
128 Intent resultIntent = result.get(0);
129 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
130
131 if (resultIntent instanceof LinkCollectionIntent) {
132 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
133 assertThat(linkIntent.links(), hasSize(9));
134 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
135 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
136 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
137 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
138 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
139 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
140 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
141 }
142 }
143
144 /**
145 * Tests a simple topology where two ingress points share some path segments
146 * and some path segments are not shared.
147 */
148 @Test
149 public void testTwoIngressCompilation() {
150 String[] ingress = {"ingress1", "ingress2"};
151 String egress = "egress";
152
153 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
154 assertThat(intent, is(notNullValue()));
155
156 final String[] hops = {"inner1", "inner2", egress};
157 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
158 assertThat(compiler, is(notNullValue()));
159
160 List<Intent> result = compiler.compile(intent);
161 assertThat(result, is(notNullValue()));
162 assertThat(result, hasSize(1));
163 Intent resultIntent = result.get(0);
164 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
165
166 if (resultIntent instanceof LinkCollectionIntent) {
167 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
168 assertThat(linkIntent.links(), hasSize(4));
169 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
170 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
171 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
172 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
173 }
174 }
175
176 /**
177 * Tests a large number of ingress points that share a common path to the
178 * egress point.
179 */
180 @Test
181 public void testMultiIngressCompilation() {
182 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700183 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700184 String egress = "e";
185
186 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
187 assertThat(intent, is(notNullValue()));
188
189 final String[] hops = {"n1", egress};
190 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
191 assertThat(compiler, is(notNullValue()));
192
193 List<Intent> result = compiler.compile(intent);
194 assertThat(result, is(notNullValue()));
195 assertThat(result, hasSize(1));
196 Intent resultIntent = result.get(0);
197 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
198
199 if (resultIntent instanceof LinkCollectionIntent) {
200 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
201 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
202 for (String ingressToCheck : ingress) {
203 assertThat(linkIntent.links(),
204 linksHasPath(ingressToCheck,
205 "n1"));
206 }
207 assertThat(linkIntent.links(), linksHasPath("n1", egress));
208 }
209 }
210}