blob: cb13c68cbebce823e7db3c4dfee2b28a06155366 [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;
40
41import static org.hamcrest.CoreMatchers.instanceOf;
42import static org.hamcrest.CoreMatchers.notNullValue;
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.Matchers.hasSize;
45import static org.hamcrest.Matchers.is;
46import static org.onosproject.net.NetTestTools.connectPoint;
47import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
48
49/**
50 * Unit tests for SinglePointToMultiPointIntentCompiler.
51 */
52public class SinglePointToMultiPointIntentCompilerTest extends AbstractIntentTest {
53
54 private static final ApplicationId APPID = new TestApplicationId("foo");
55
56 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
57 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
58
59
60
61 /**
62 * Creates a SinglePointToMultiPoint intent for an ingress point
63 * and a group of egress points.
64 *
65 * @param ingressId device id of the ingress point
66 * @param egressIds array of egress device ids
67 * @return MultiPointToSinglePoint intent
68 */
69 private SinglePointToMultiPointIntent makeIntent(String ingressId, String[] egressIds) {
70 ConnectPoint ingressPoint = connectPoint(ingressId, 2);
71 Set<ConnectPoint> egressPoints = new HashSet<>();
72
73
74 for (String egressId : egressIds) {
75 egressPoints.add(connectPoint(egressId, 1));
76 }
77
78 return SinglePointToMultiPointIntent.builder()
79 .appId(APPID)
80 .selector(selector)
81 .treatment(treatment)
82 .ingressPoint(ingressPoint)
83 .egressPoints(egressPoints)
84 .build();
85 }
86
87 /**
88 * Generate SinglePointToMultiPointIntent with filtered connection point.
89 *
90 * @param ingress filtered ingress point
91 * @param egress filtered egress point
92 * @return
93 */
94 private SinglePointToMultiPointIntent makeFilteredConnectPointIntent(FilteredConnectPoint ingress,
Pier Ventre973bb032016-10-11 08:57:39 -070095 Set<FilteredConnectPoint> egress,
96 TrafficSelector trafficSelector) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070097 return SinglePointToMultiPointIntent.builder()
98 .appId(APPID)
99 .treatment(treatment)
Pier Ventre973bb032016-10-11 08:57:39 -0700100 .selector(trafficSelector)
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700101 .filteredIngressPoint(ingress)
102 .filteredEgressPoints(egress)
103 .build();
104 }
105
106 /**
107 * Creates a compiler for SinglePointToMultiPoint intents.
108 *
109 * @param hops hops to use while computing paths for this intent
110 * @return SinglePointToMultiPoint intent
111 */
112 private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops) {
113 SinglePointToMultiPointIntentCompiler compiler =
114 new SinglePointToMultiPointIntentCompiler();
115
116 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
117 return compiler;
118 }
119
120 /**
121 * Tests a single ingress point with 8 hops to its egress point.
122 */
123 @Test
124 public void testSingleLongPathCompilation() {
125
126 String ingress = "ingress";
127 String[] egress = {"egress"};
128
129 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
130 assertThat(intent, is(notNullValue()));
131
132 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
133 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
134 assertThat(compiler, is(notNullValue()));
135
136 List<Intent> result = compiler.compile(intent, null);
137 assertThat(result, is(Matchers.notNullValue()));
138 assertThat(result, hasSize(1));
139 Intent resultIntent = result.get(0);
140 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
141
142 if (resultIntent instanceof LinkCollectionIntent) {
143 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
144 assertThat(linkIntent.links(), hasSize(9));
145 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
146 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
147 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
148 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
149 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
150 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
151 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
152 }
153 }
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 }
185 }
186
187 /**
188 * Tests a large number of ingress points that share a common path to the
189 * egress point.
190 */
191 @Test
192 public void testMultiIngressCompilation() {
193 String ingress = "i";
194 String[] egress = {"e1", "e2", "e3", "e4", "e5",
195 "e6", "e7", "e8", "e9", "e10"};
196
197 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
198 assertThat(intent, is(notNullValue()));
199
200 final String[] hops = {"n1"};
201 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
202 assertThat(compiler, is(notNullValue()));
203
204 List<Intent> result = compiler.compile(intent, null);
205 assertThat(result, is(notNullValue()));
206 assertThat(result, hasSize(1));
207 Intent resultIntent = result.get(0);
208 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
209
210 if (resultIntent instanceof LinkCollectionIntent) {
211 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
212 assertThat(linkIntent.links(), hasSize(egress.length + 1));
213 for (String egressToCheck : egress) {
214 assertThat(linkIntent.links(), linksHasPath("n1", egressToCheck));
215 }
216 assertThat(linkIntent.links(), linksHasPath(ingress, "n1"));
217 }
218 }
219
220 /**
221 * Tests ingress and egress on the same device.
222 */
223 @Test
224 public void testSameDeviceCompilation() {
225 String ingress = "i1";
226 String[] egress = {"i2", "i3"};
227
228 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
229 assertThat(intent, is(notNullValue()));
230
231 final String[] hops = {};
232 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
233 assertThat(compiler, is(notNullValue()));
234
235 List<Intent> result = compiler.compile(intent, null);
236 assertThat(result, is(notNullValue()));
237 assertThat(result, hasSize(1));
238 Intent resultIntent = result.get(0);
239 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
240
241 if (resultIntent instanceof LinkCollectionIntent) {
242 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
243 assertThat(linkIntent.links(), hasSize(egress.length));
244
245 assertThat(linkIntent.links(), linksHasPath("i1", "i2"));
246 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
247 }
248 }
249
250 /**
251 * Tests filtered ingress and egress.
252 */
253 @Test
254 public void testFilteredConnectPointIntent() {
255
256 FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
257
258 Set<FilteredConnectPoint> egress = ImmutableSet.of(
259 new FilteredConnectPoint(connectPoint("of3", 1),
260 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
261 new FilteredConnectPoint(connectPoint("of4", 1),
262 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
263 );
264
265
Pier Ventre973bb032016-10-11 08:57:39 -0700266 SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress, selector);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700267 String[] hops = {"of2"};
268
269 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
270 assertThat(compiler, is(notNullValue()));
271
272 List<Intent> result = compiler.compile(intent, null);
273 assertThat(result, is(notNullValue()));
274 assertThat(result, hasSize(1));
275
276 Intent resultIntent = result.get(0);
277 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
278
279 if (resultIntent instanceof LinkCollectionIntent) {
280 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
281 assertThat(linkIntent.links(), hasSize(3));
282
283 assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
284 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
285 assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
286
287 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
288 assertThat("Link collection ingress points do not match base intent",
289 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
290
291 assertThat("Link collection egress points do not match base intent",
292 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
293 }
294
295 }
Pier Ventre973bb032016-10-11 08:57:39 -0700296
297 /**
298 * Tests selector, filtered ingress and egress.
299 */
300 @Test
301 public void testNonTrivialSelectorsIntent() {
302
303 FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
304
305 Set<FilteredConnectPoint> egress = ImmutableSet.of(
306 new FilteredConnectPoint(connectPoint("of3", 1),
307 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
308 new FilteredConnectPoint(connectPoint("of4", 1),
309 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
310 );
311
312 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
313 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
314 .build();
315
316 SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress, ipPrefixSelector);
317 String[] hops = {"of2"};
318
319 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
320 assertThat(compiler, is(notNullValue()));
321
322 List<Intent> result = compiler.compile(intent, null);
323 assertThat(result, is(notNullValue()));
324 assertThat(result, hasSize(1));
325
326 Intent resultIntent = result.get(0);
327 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
328
329 if (resultIntent instanceof LinkCollectionIntent) {
330 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
331 assertThat(linkIntent.links(), hasSize(3));
332
333 assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
334 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
335 assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
336
337 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
338 assertThat("Link collection ingress points do not match base intent",
339 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
340
341 assertThat("Link collection egress points do not match base intent",
342 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
343 assertThat(linkIntent.selector(), is(ipPrefixSelector));
344 }
345
346 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700347}