blob: eec242c044c7bbdc6e0d2d7e7fdb2164b2fea9d4 [file] [log] [blame]
Yi Tseng2a81c9d2016-09-14 10:14:24 -07001/*
2 * Copyright 2016-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
17package org.onosproject.net.intent.impl.compiler;
18
19import com.google.common.collect.ImmutableSet;
20import org.hamcrest.Matchers;
21import org.junit.Test;
Pier Ventre973bb032016-10-11 08:57:39 -070022import org.onlab.packet.IpPrefix;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070023import org.onlab.packet.VlanId;
24import org.onosproject.TestApplicationId;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.FilteredConnectPoint;
28import org.onosproject.net.flow.DefaultTrafficSelector;
29import org.onosproject.net.flow.TrafficSelector;
30import org.onosproject.net.flow.TrafficTreatment;
31import org.onosproject.net.intent.AbstractIntentTest;
32import org.onosproject.net.intent.Intent;
33import org.onosproject.net.intent.IntentTestsMocks;
34import org.onosproject.net.intent.LinkCollectionIntent;
35import org.onosproject.net.intent.SinglePointToMultiPointIntent;
36
37import java.util.HashSet;
38import java.util.List;
39import java.util.Set;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070040import static org.hamcrest.CoreMatchers.instanceOf;
41import static org.hamcrest.CoreMatchers.notNullValue;
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.Matchers.hasSize;
44import static org.hamcrest.Matchers.is;
45import static org.onosproject.net.NetTestTools.connectPoint;
46import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
47
48/**
49 * Unit tests for SinglePointToMultiPointIntentCompiler.
50 */
51public class SinglePointToMultiPointIntentCompilerTest extends AbstractIntentTest {
52
53 private static final ApplicationId APPID = new TestApplicationId("foo");
54
55 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
56 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
57
58
59
60 /**
61 * Creates a SinglePointToMultiPoint intent for an ingress point
62 * and a group of egress points.
63 *
64 * @param ingressId device id of the ingress point
65 * @param egressIds array of egress device ids
66 * @return MultiPointToSinglePoint intent
67 */
68 private SinglePointToMultiPointIntent makeIntent(String ingressId, String[] egressIds) {
69 ConnectPoint ingressPoint = connectPoint(ingressId, 2);
70 Set<ConnectPoint> egressPoints = new HashSet<>();
71
72
73 for (String egressId : egressIds) {
74 egressPoints.add(connectPoint(egressId, 1));
75 }
76
77 return SinglePointToMultiPointIntent.builder()
78 .appId(APPID)
79 .selector(selector)
80 .treatment(treatment)
81 .ingressPoint(ingressPoint)
82 .egressPoints(egressPoints)
83 .build();
84 }
85
86 /**
87 * Generate SinglePointToMultiPointIntent with filtered connection point.
88 *
89 * @param ingress filtered ingress point
90 * @param egress filtered egress point
91 * @return
92 */
93 private SinglePointToMultiPointIntent makeFilteredConnectPointIntent(FilteredConnectPoint ingress,
Pier Ventre973bb032016-10-11 08:57:39 -070094 Set<FilteredConnectPoint> egress,
95 TrafficSelector trafficSelector) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070096 return SinglePointToMultiPointIntent.builder()
97 .appId(APPID)
98 .treatment(treatment)
Pier Ventre973bb032016-10-11 08:57:39 -070099 .selector(trafficSelector)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700100 .filteredIngressPoint(ingress)
101 .filteredEgressPoints(egress)
102 .build();
103 }
104
105 /**
106 * Creates a compiler for SinglePointToMultiPoint intents.
107 *
108 * @param hops hops to use while computing paths for this intent
109 * @return SinglePointToMultiPoint intent
110 */
111 private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops) {
112 SinglePointToMultiPointIntentCompiler compiler =
113 new SinglePointToMultiPointIntentCompiler();
114
115 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
116 return compiler;
117 }
118
119 /**
120 * Tests a single ingress point with 8 hops to its egress point.
121 */
122 @Test
123 public void testSingleLongPathCompilation() {
124
125 String ingress = "ingress";
126 String[] egress = {"egress"};
127
128 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
129 assertThat(intent, is(notNullValue()));
130
131 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
132 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
133 assertThat(compiler, is(notNullValue()));
134
135 List<Intent> result = compiler.compile(intent, null);
136 assertThat(result, is(Matchers.notNullValue()));
137 assertThat(result, hasSize(1));
138 Intent resultIntent = result.get(0);
139 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
140
141 if (resultIntent instanceof LinkCollectionIntent) {
142 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
143 assertThat(linkIntent.links(), hasSize(9));
144 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
145 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
146 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
147 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
148 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
149 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
150 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
151 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700152 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700153 }
154
155 /**
156 * Tests a simple topology where two egress points share some path segments
157 * and some path segments are not shared.
158 */
159 @Test
160 public void testTwoIngressCompilation() {
161 String ingress = "ingress";
162 String[] egress = {"egress1", "egress2"};
163
164 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
165 assertThat(intent, is(notNullValue()));
166
167 final String[] hops = {"inner1", "inner2"};
168 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
169 assertThat(compiler, is(notNullValue()));
170
171 List<Intent> result = compiler.compile(intent, null);
172 assertThat(result, is(notNullValue()));
173 assertThat(result, hasSize(1));
174 Intent resultIntent = result.get(0);
175 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
176
177 if (resultIntent instanceof LinkCollectionIntent) {
178 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
179 assertThat(linkIntent.links(), hasSize(4));
180 assertThat(linkIntent.links(), linksHasPath("ingress", "inner1"));
181 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
182 assertThat(linkIntent.links(), linksHasPath("inner2", "egress1"));
183 assertThat(linkIntent.links(), linksHasPath("inner2", "egress2"));
184 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700185 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700186 }
187
188 /**
189 * Tests a large number of ingress points that share a common path to the
190 * egress point.
191 */
192 @Test
193 public void testMultiIngressCompilation() {
194 String ingress = "i";
195 String[] egress = {"e1", "e2", "e3", "e4", "e5",
196 "e6", "e7", "e8", "e9", "e10"};
197
198 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
199 assertThat(intent, is(notNullValue()));
200
201 final String[] hops = {"n1"};
202 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
203 assertThat(compiler, is(notNullValue()));
204
205 List<Intent> result = compiler.compile(intent, null);
206 assertThat(result, is(notNullValue()));
207 assertThat(result, hasSize(1));
208 Intent resultIntent = result.get(0);
209 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
210
211 if (resultIntent instanceof LinkCollectionIntent) {
212 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
213 assertThat(linkIntent.links(), hasSize(egress.length + 1));
214 for (String egressToCheck : egress) {
215 assertThat(linkIntent.links(), linksHasPath("n1", egressToCheck));
216 }
217 assertThat(linkIntent.links(), linksHasPath(ingress, "n1"));
218 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700219 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700220 }
221
222 /**
223 * Tests ingress and egress on the same device.
224 */
225 @Test
226 public void testSameDeviceCompilation() {
227 String ingress = "i1";
228 String[] egress = {"i2", "i3"};
229
230 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
231 assertThat(intent, is(notNullValue()));
232
233 final String[] hops = {};
234 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
235 assertThat(compiler, is(notNullValue()));
236
237 List<Intent> result = compiler.compile(intent, null);
238 assertThat(result, is(notNullValue()));
239 assertThat(result, hasSize(1));
240 Intent resultIntent = result.get(0);
241 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
242
243 if (resultIntent instanceof LinkCollectionIntent) {
244 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
245 assertThat(linkIntent.links(), hasSize(egress.length));
246
247 assertThat(linkIntent.links(), linksHasPath("i1", "i2"));
248 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
249 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700250 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700251 }
252
253 /**
254 * Tests filtered ingress and egress.
255 */
256 @Test
257 public void testFilteredConnectPointIntent() {
258
259 FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
260
261 Set<FilteredConnectPoint> egress = ImmutableSet.of(
262 new FilteredConnectPoint(connectPoint("of3", 1),
263 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
264 new FilteredConnectPoint(connectPoint("of4", 1),
265 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
266 );
267
268
Pier Ventre973bb032016-10-11 08:57:39 -0700269 SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress, selector);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700270 String[] hops = {"of2"};
271
272 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
273 assertThat(compiler, is(notNullValue()));
274
275 List<Intent> result = compiler.compile(intent, null);
276 assertThat(result, is(notNullValue()));
277 assertThat(result, hasSize(1));
278
279 Intent resultIntent = result.get(0);
280 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
281
282 if (resultIntent instanceof LinkCollectionIntent) {
283 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
284 assertThat(linkIntent.links(), hasSize(3));
285
286 assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
287 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
288 assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
289
290 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
291 assertThat("Link collection ingress points do not match base intent",
292 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
293
294 assertThat("Link collection egress points do not match base intent",
295 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
296 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700297 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700298
299 }
Pier Ventre973bb032016-10-11 08:57:39 -0700300
301 /**
302 * Tests selector, filtered ingress and egress.
303 */
304 @Test
305 public void testNonTrivialSelectorsIntent() {
306
307 FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
308
309 Set<FilteredConnectPoint> egress = ImmutableSet.of(
310 new FilteredConnectPoint(connectPoint("of3", 1),
311 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
312 new FilteredConnectPoint(connectPoint("of4", 1),
313 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
314 );
315
316 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
317 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
318 .build();
319
320 SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress, ipPrefixSelector);
321 String[] hops = {"of2"};
322
323 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
324 assertThat(compiler, is(notNullValue()));
325
326 List<Intent> result = compiler.compile(intent, null);
327 assertThat(result, is(notNullValue()));
328 assertThat(result, hasSize(1));
329
330 Intent resultIntent = result.get(0);
331 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
332
333 if (resultIntent instanceof LinkCollectionIntent) {
334 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
335 assertThat(linkIntent.links(), hasSize(3));
336
337 assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
338 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
339 assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
340
341 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
342 assertThat("Link collection ingress points do not match base intent",
343 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
344
345 assertThat("Link collection egress points do not match base intent",
346 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
347 assertThat(linkIntent.selector(), is(ipPrefixSelector));
348 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700349 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Pier Ventre973bb032016-10-11 08:57:39 -0700350
351 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700352}