blob: ce5f3b7021d4302717d4afe1dfdde4d1c10ed8b8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Yi Tseng2a81c9d2016-09-14 10:14:24 -070018import com.google.common.collect.ImmutableSet;
Ray Milkeye6684082014-10-16 16:59:47 -070019import org.hamcrest.Matchers;
20import org.junit.Test;
Pier Ventre973bb032016-10-11 08:57:39 -070021import org.onlab.packet.IpPrefix;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070022import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.TestApplicationId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070024import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.ConnectPoint;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070026import org.onosproject.net.FilteredConnectPoint;
27import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.TrafficTreatment;
30import org.onosproject.net.intent.AbstractIntentTest;
31import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentTestsMocks;
33import org.onosproject.net.intent.LinkCollectionIntent;
34import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkeye6684082014-10-16 16:59:47 -070035
Jonathan Hart066244c2015-06-23 09:46:19 -070036import java.util.HashSet;
37import java.util.List;
38import java.util.Set;
39
Ray Milkey6e0fb302015-04-16 14:44:12 -070040import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeye6684082014-10-16 16:59:47 -070041import static org.hamcrest.CoreMatchers.notNullValue;
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.Matchers.hasSize;
44import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.NetTestTools.connectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080046import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070047
48/**
49 * Unit tests for the MultiPointToSinglePoint intent compiler.
50 */
Ray Milkey37f6a382014-11-25 14:54:42 -080051public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070052
Thomas Vachuskab97cf282014-10-20 23:31:12 -070053 private static final ApplicationId APPID = new TestApplicationId("foo");
54
Ray Milkeye6684082014-10-16 16:59:47 -070055 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
56 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
57
58 /**
Ray Milkeye6684082014-10-16 16:59:47 -070059 * Creates a MultiPointToSinglePoint intent for a group of ingress points
60 * and an egress point.
61 *
62 * @param ingressIds array of ingress device ids
Thomas Vachuskab97cf282014-10-20 23:31:12 -070063 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -070064 * @return MultiPointToSinglePoint intent
65 */
66 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
67 Set<ConnectPoint> ingressPoints = new HashSet<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070068 ConnectPoint egressPoint = connectPoint(egressId, 2);
Ray Milkeye6684082014-10-16 16:59:47 -070069
70 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070071 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -070072 }
73
Ray Milkeyebc5d222015-03-18 15:45:36 -070074 return MultiPointToSinglePointIntent.builder()
75 .appId(APPID)
76 .selector(selector)
77 .treatment(treatment)
78 .ingressPoints(ingressPoints)
79 .egressPoint(egressPoint)
80 .build();
Ray Milkeye6684082014-10-16 16:59:47 -070081 }
82
83 /**
Yi Tseng2a81c9d2016-09-14 10:14:24 -070084 * Generate MultiPointToSinglePointIntent with filtered connection point.
85 *
86 * @param ingress filtered ingress points
87 * @param egress filtered egress point
88 * @return
89 */
90 private MultiPointToSinglePointIntent makeFilteredConnectPointIntent(Set<FilteredConnectPoint> ingress,
Pier Ventre973bb032016-10-11 08:57:39 -070091 FilteredConnectPoint egress,
92 TrafficSelector trafficSelector) {
Yi Tseng2a81c9d2016-09-14 10:14:24 -070093 return MultiPointToSinglePointIntent.builder()
94 .appId(APPID)
95 .treatment(treatment)
Pier Ventre973bb032016-10-11 08:57:39 -070096 .selector(trafficSelector)
Yi Tseng2a81c9d2016-09-14 10:14:24 -070097 .filteredIngressPoints(ingress)
98 .filteredEgressPoint(egress)
99 .build();
100 }
101
102 /**
Ray Milkeye6684082014-10-16 16:59:47 -0700103 * Creates a compiler for MultiPointToSinglePoint intents.
104 *
105 * @param hops hops to use while computing paths for this intent
106 * @return MultiPointToSinglePoint intent
107 */
108 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
109 MultiPointToSinglePointIntentCompiler compiler =
110 new MultiPointToSinglePointIntentCompiler();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700111 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
112 compiler.deviceService = new IntentTestsMocks.MockDeviceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700113 return compiler;
114 }
115
116 /**
117 * Tests a single ingress point with 8 hops to its egress point.
118 */
119 @Test
120 public void testSingleLongPathCompilation() {
121
122 String[] ingress = {"ingress"};
123 String egress = "egress";
124
125 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
126 assertThat(intent, is(notNullValue()));
127
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700128 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
Ray Milkeye6684082014-10-16 16:59:47 -0700129 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
130 assertThat(compiler, is(notNullValue()));
131
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800132 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700133 assertThat(result, is(Matchers.notNullValue()));
134 assertThat(result, hasSize(1));
135 Intent resultIntent = result.get(0);
136 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
137
138 if (resultIntent instanceof LinkCollectionIntent) {
139 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
140 assertThat(linkIntent.links(), hasSize(9));
141 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
142 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
143 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
144 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
145 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
146 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
147 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
148 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700149 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkeye6684082014-10-16 16:59:47 -0700150 }
151
152 /**
153 * Tests a simple topology where two ingress points share some path segments
154 * and some path segments are not shared.
155 */
156 @Test
157 public void testTwoIngressCompilation() {
158 String[] ingress = {"ingress1", "ingress2"};
159 String egress = "egress";
160
161 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
162 assertThat(intent, is(notNullValue()));
163
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700164 final String[] hops = {"inner1", "inner2"};
Ray Milkeye6684082014-10-16 16:59:47 -0700165 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
166 assertThat(compiler, is(notNullValue()));
167
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800168 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700169 assertThat(result, is(notNullValue()));
170 assertThat(result, hasSize(1));
171 Intent resultIntent = result.get(0);
172 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
173
174 if (resultIntent instanceof LinkCollectionIntent) {
175 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
176 assertThat(linkIntent.links(), hasSize(4));
177 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
178 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
179 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
180 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
181 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700182 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkeye6684082014-10-16 16:59:47 -0700183 }
184
185 /**
186 * Tests a large number of ingress points that share a common path to the
187 * egress point.
188 */
189 @Test
190 public void testMultiIngressCompilation() {
191 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700192 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700193 String egress = "e";
194
195 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
196 assertThat(intent, is(notNullValue()));
197
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700198 final String[] hops = {"n1"};
Ray Milkeye6684082014-10-16 16:59:47 -0700199 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
200 assertThat(compiler, is(notNullValue()));
201
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800202 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700203 assertThat(result, is(notNullValue()));
204 assertThat(result, hasSize(1));
205 Intent resultIntent = result.get(0);
206 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
207
208 if (resultIntent instanceof LinkCollectionIntent) {
209 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
210 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
211 for (String ingressToCheck : ingress) {
212 assertThat(linkIntent.links(),
213 linksHasPath(ingressToCheck,
214 "n1"));
215 }
216 assertThat(linkIntent.links(), linksHasPath("n1", egress));
217 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700218 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkeye6684082014-10-16 16:59:47 -0700219 }
Ray Milkey6e0fb302015-04-16 14:44:12 -0700220
221 /**
222 * Tests ingress and egress on the same device.
223 */
224 @Test
225 public void testSameDeviceCompilation() {
226 String[] ingress = {"i1", "i2"};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700227 String egress = "i3";
Ray Milkey6e0fb302015-04-16 14:44:12 -0700228
229 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
230 assertThat(intent, is(notNullValue()));
231
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700232 final String[] hops = {};
Ray Milkey6e0fb302015-04-16 14:44:12 -0700233 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
234 assertThat(compiler, is(notNullValue()));
235
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800236 List<Intent> result = compiler.compile(intent, null);
Ray Milkey6e0fb302015-04-16 14:44:12 -0700237 assertThat(result, is(notNullValue()));
238 assertThat(result, hasSize(1));
239 Intent resultIntent = result.get(0);
240 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
241
242 if (resultIntent instanceof LinkCollectionIntent) {
243 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Jonathan Hart066244c2015-06-23 09:46:19 -0700244 assertThat(linkIntent.links(), hasSize(ingress.length));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700245
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700246 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
247 assertThat(linkIntent.links(), linksHasPath("i2", "i3"));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700248 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700249 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700250 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700251
252 /**
253 * Tests filtered ingress and egress.
254 */
255 @Test
256 public void testFilteredConnectPointIntent() {
257
258 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
259 new FilteredConnectPoint(connectPoint("of1", 1),
260 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
261 new FilteredConnectPoint(connectPoint("of2", 1),
262 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
263 );
264
265 FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
266
Pier Ventre973bb032016-10-11 08:57:39 -0700267 MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress, selector);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700268 String[] hops = {"of3"};
269
270 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
271 assertThat(compiler, is(notNullValue()));
272
273 List<Intent> result = compiler.compile(intent, null);
274 assertThat(result, is(notNullValue()));
275 assertThat(result, hasSize(1));
276
277 Intent resultIntent = result.get(0);
278 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
279
280 if (resultIntent instanceof LinkCollectionIntent) {
281 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
282 assertThat(linkIntent.links(), hasSize(3));
283 assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
284 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
285 assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
286 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700287 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700288
289 }
290
Pier Ventre973bb032016-10-11 08:57:39 -0700291 /**
292 * Tests selector, filtered ingress and egress.
293 */
294 @Test
295 public void testNonTrivialSelectorsIntent() {
296
297 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
298 new FilteredConnectPoint(connectPoint("of1", 1),
299 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
300 new FilteredConnectPoint(connectPoint("of2", 1),
301 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
302 );
303
304 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
305 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
306 .build();
307
308 FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
309
310 MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress, ipPrefixSelector);
311 String[] hops = {"of3"};
312
313 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
314 assertThat(compiler, is(notNullValue()));
315
316 List<Intent> result = compiler.compile(intent, null);
317 assertThat(result, is(notNullValue()));
318 assertThat(result, hasSize(1));
319
320 Intent resultIntent = result.get(0);
321 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
322
323 if (resultIntent instanceof LinkCollectionIntent) {
324 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
325 assertThat(linkIntent.links(), hasSize(3));
326 assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
327 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
328 assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
329 assertThat(linkIntent.selector(), is(ipPrefixSelector));
330 }
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700331 assertThat("key is inherited", resultIntent.key(), is(intent.key()));
Pier Ventre973bb032016-10-11 08:57:39 -0700332
333 }
334
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700335
Ray Milkeye6684082014-10-16 16:59:47 -0700336}