blob: 60da1185120c93e76b520747d6d3b890bc90461b [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 }
149 }
150
151 /**
152 * Tests a simple topology where two ingress points share some path segments
153 * and some path segments are not shared.
154 */
155 @Test
156 public void testTwoIngressCompilation() {
157 String[] ingress = {"ingress1", "ingress2"};
158 String egress = "egress";
159
160 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
161 assertThat(intent, is(notNullValue()));
162
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700163 final String[] hops = {"inner1", "inner2"};
Ray Milkeye6684082014-10-16 16:59:47 -0700164 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
165 assertThat(compiler, is(notNullValue()));
166
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800167 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700168 assertThat(result, is(notNullValue()));
169 assertThat(result, hasSize(1));
170 Intent resultIntent = result.get(0);
171 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
172
173 if (resultIntent instanceof LinkCollectionIntent) {
174 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
175 assertThat(linkIntent.links(), hasSize(4));
176 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
177 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
178 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
179 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
180 }
181 }
182
183 /**
184 * Tests a large number of ingress points that share a common path to the
185 * egress point.
186 */
187 @Test
188 public void testMultiIngressCompilation() {
189 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700190 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700191 String egress = "e";
192
193 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
194 assertThat(intent, is(notNullValue()));
195
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700196 final String[] hops = {"n1"};
Ray Milkeye6684082014-10-16 16:59:47 -0700197 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
198 assertThat(compiler, is(notNullValue()));
199
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800200 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700201 assertThat(result, is(notNullValue()));
202 assertThat(result, hasSize(1));
203 Intent resultIntent = result.get(0);
204 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
205
206 if (resultIntent instanceof LinkCollectionIntent) {
207 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
208 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
209 for (String ingressToCheck : ingress) {
210 assertThat(linkIntent.links(),
211 linksHasPath(ingressToCheck,
212 "n1"));
213 }
214 assertThat(linkIntent.links(), linksHasPath("n1", egress));
215 }
216 }
Ray Milkey6e0fb302015-04-16 14:44:12 -0700217
218 /**
219 * Tests ingress and egress on the same device.
220 */
221 @Test
222 public void testSameDeviceCompilation() {
223 String[] ingress = {"i1", "i2"};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700224 String egress = "i3";
Ray Milkey6e0fb302015-04-16 14:44:12 -0700225
226 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
227 assertThat(intent, is(notNullValue()));
228
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700229 final String[] hops = {};
Ray Milkey6e0fb302015-04-16 14:44:12 -0700230 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
231 assertThat(compiler, is(notNullValue()));
232
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800233 List<Intent> result = compiler.compile(intent, null);
Ray Milkey6e0fb302015-04-16 14:44:12 -0700234 assertThat(result, is(notNullValue()));
235 assertThat(result, hasSize(1));
236 Intent resultIntent = result.get(0);
237 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
238
239 if (resultIntent instanceof LinkCollectionIntent) {
240 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Jonathan Hart066244c2015-06-23 09:46:19 -0700241 assertThat(linkIntent.links(), hasSize(ingress.length));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700242
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700243 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
244 assertThat(linkIntent.links(), linksHasPath("i2", "i3"));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700245 }
246 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700247
248 /**
249 * Tests filtered ingress and egress.
250 */
251 @Test
252 public void testFilteredConnectPointIntent() {
253
254 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
255 new FilteredConnectPoint(connectPoint("of1", 1),
256 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
257 new FilteredConnectPoint(connectPoint("of2", 1),
258 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
259 );
260
261 FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
262
Pier Ventre973bb032016-10-11 08:57:39 -0700263 MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress, selector);
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700264 String[] hops = {"of3"};
265
266 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
267 assertThat(compiler, is(notNullValue()));
268
269 List<Intent> result = compiler.compile(intent, null);
270 assertThat(result, is(notNullValue()));
271 assertThat(result, hasSize(1));
272
273 Intent resultIntent = result.get(0);
274 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
275
276 if (resultIntent instanceof LinkCollectionIntent) {
277 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
278 assertThat(linkIntent.links(), hasSize(3));
279 assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
280 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
281 assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
282 }
283
284 }
285
Pier Ventre973bb032016-10-11 08:57:39 -0700286 /**
287 * Tests selector, filtered ingress and egress.
288 */
289 @Test
290 public void testNonTrivialSelectorsIntent() {
291
292 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
293 new FilteredConnectPoint(connectPoint("of1", 1),
294 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
295 new FilteredConnectPoint(connectPoint("of2", 1),
296 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
297 );
298
299 TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder()
300 .matchIPDst(IpPrefix.valueOf("192.168.100.0/24"))
301 .build();
302
303 FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
304
305 MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress, ipPrefixSelector);
306 String[] hops = {"of3"};
307
308 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
309 assertThat(compiler, is(notNullValue()));
310
311 List<Intent> result = compiler.compile(intent, null);
312 assertThat(result, is(notNullValue()));
313 assertThat(result, hasSize(1));
314
315 Intent resultIntent = result.get(0);
316 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
317
318 if (resultIntent instanceof LinkCollectionIntent) {
319 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
320 assertThat(linkIntent.links(), hasSize(3));
321 assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
322 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
323 assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
324 assertThat(linkIntent.selector(), is(ipPrefixSelector));
325 }
326
327 }
328
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700329
Ray Milkeye6684082014-10-16 16:59:47 -0700330}