blob: 8d49138311e7ea975510543f3877e877a8050bff [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;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070021import org.onlab.packet.VlanId;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.TestApplicationId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070023import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ConnectPoint;
Yi Tseng2a81c9d2016-09-14 10:14:24 -070025import org.onosproject.net.FilteredConnectPoint;
26import org.onosproject.net.flow.DefaultTrafficSelector;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.intent.AbstractIntentTest;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Ray Milkeye6684082014-10-16 16:59:47 -070034
Jonathan Hart066244c2015-06-23 09:46:19 -070035import java.util.HashSet;
36import java.util.List;
37import java.util.Set;
38
Ray Milkey6e0fb302015-04-16 14:44:12 -070039import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeye6684082014-10-16 16:59:47 -070040import static org.hamcrest.CoreMatchers.notNullValue;
41import static org.hamcrest.MatcherAssert.assertThat;
42import static org.hamcrest.Matchers.hasSize;
43import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080044import static org.onosproject.net.NetTestTools.connectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070046
47/**
48 * Unit tests for the MultiPointToSinglePoint intent compiler.
49 */
Ray Milkey37f6a382014-11-25 14:54:42 -080050public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070051
Thomas Vachuskab97cf282014-10-20 23:31:12 -070052 private static final ApplicationId APPID = new TestApplicationId("foo");
53
Ray Milkeye6684082014-10-16 16:59:47 -070054 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
55 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
56
57 /**
Ray Milkeye6684082014-10-16 16:59:47 -070058 * Creates a MultiPointToSinglePoint intent for a group of ingress points
59 * and an egress point.
60 *
61 * @param ingressIds array of ingress device ids
Thomas Vachuskab97cf282014-10-20 23:31:12 -070062 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -070063 * @return MultiPointToSinglePoint intent
64 */
65 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
66 Set<ConnectPoint> ingressPoints = new HashSet<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -070067 ConnectPoint egressPoint = connectPoint(egressId, 2);
Ray Milkeye6684082014-10-16 16:59:47 -070068
69 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070070 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -070071 }
72
Ray Milkeyebc5d222015-03-18 15:45:36 -070073 return MultiPointToSinglePointIntent.builder()
74 .appId(APPID)
75 .selector(selector)
76 .treatment(treatment)
77 .ingressPoints(ingressPoints)
78 .egressPoint(egressPoint)
79 .build();
Ray Milkeye6684082014-10-16 16:59:47 -070080 }
81
82 /**
Yi Tseng2a81c9d2016-09-14 10:14:24 -070083 * Generate MultiPointToSinglePointIntent with filtered connection point.
84 *
85 * @param ingress filtered ingress points
86 * @param egress filtered egress point
87 * @return
88 */
89 private MultiPointToSinglePointIntent makeFilteredConnectPointIntent(Set<FilteredConnectPoint> ingress,
90 FilteredConnectPoint egress) {
91 return MultiPointToSinglePointIntent.builder()
92 .appId(APPID)
93 .treatment(treatment)
94 .filteredIngressPoints(ingress)
95 .filteredEgressPoint(egress)
96 .build();
97 }
98
99 /**
Ray Milkeye6684082014-10-16 16:59:47 -0700100 * Creates a compiler for MultiPointToSinglePoint intents.
101 *
102 * @param hops hops to use while computing paths for this intent
103 * @return MultiPointToSinglePoint intent
104 */
105 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
106 MultiPointToSinglePointIntentCompiler compiler =
107 new MultiPointToSinglePointIntentCompiler();
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700108 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
109 compiler.deviceService = new IntentTestsMocks.MockDeviceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700110 return compiler;
111 }
112
113 /**
114 * Tests a single ingress point with 8 hops to its egress point.
115 */
116 @Test
117 public void testSingleLongPathCompilation() {
118
119 String[] ingress = {"ingress"};
120 String egress = "egress";
121
122 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
123 assertThat(intent, is(notNullValue()));
124
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700125 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
Ray Milkeye6684082014-10-16 16:59:47 -0700126 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
127 assertThat(compiler, is(notNullValue()));
128
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800129 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700130 assertThat(result, is(Matchers.notNullValue()));
131 assertThat(result, hasSize(1));
132 Intent resultIntent = result.get(0);
133 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
134
135 if (resultIntent instanceof LinkCollectionIntent) {
136 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
137 assertThat(linkIntent.links(), hasSize(9));
138 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
139 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
140 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
141 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
142 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
143 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
144 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
145 }
146 }
147
148 /**
149 * Tests a simple topology where two ingress points share some path segments
150 * and some path segments are not shared.
151 */
152 @Test
153 public void testTwoIngressCompilation() {
154 String[] ingress = {"ingress1", "ingress2"};
155 String egress = "egress";
156
157 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
158 assertThat(intent, is(notNullValue()));
159
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700160 final String[] hops = {"inner1", "inner2"};
Ray Milkeye6684082014-10-16 16:59:47 -0700161 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
162 assertThat(compiler, is(notNullValue()));
163
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800164 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700165 assertThat(result, is(notNullValue()));
166 assertThat(result, hasSize(1));
167 Intent resultIntent = result.get(0);
168 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
169
170 if (resultIntent instanceof LinkCollectionIntent) {
171 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
172 assertThat(linkIntent.links(), hasSize(4));
173 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
174 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
175 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
176 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
177 }
178 }
179
180 /**
181 * Tests a large number of ingress points that share a common path to the
182 * egress point.
183 */
184 @Test
185 public void testMultiIngressCompilation() {
186 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700187 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700188 String egress = "e";
189
190 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
191 assertThat(intent, is(notNullValue()));
192
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700193 final String[] hops = {"n1"};
Ray Milkeye6684082014-10-16 16:59:47 -0700194 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
195 assertThat(compiler, is(notNullValue()));
196
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800197 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700198 assertThat(result, is(notNullValue()));
199 assertThat(result, hasSize(1));
200 Intent resultIntent = result.get(0);
201 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
202
203 if (resultIntent instanceof LinkCollectionIntent) {
204 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
205 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
206 for (String ingressToCheck : ingress) {
207 assertThat(linkIntent.links(),
208 linksHasPath(ingressToCheck,
209 "n1"));
210 }
211 assertThat(linkIntent.links(), linksHasPath("n1", egress));
212 }
213 }
Ray Milkey6e0fb302015-04-16 14:44:12 -0700214
215 /**
216 * Tests ingress and egress on the same device.
217 */
218 @Test
219 public void testSameDeviceCompilation() {
220 String[] ingress = {"i1", "i2"};
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700221 String egress = "i3";
Ray Milkey6e0fb302015-04-16 14:44:12 -0700222
223 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
224 assertThat(intent, is(notNullValue()));
225
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700226 final String[] hops = {};
Ray Milkey6e0fb302015-04-16 14:44:12 -0700227 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
228 assertThat(compiler, is(notNullValue()));
229
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800230 List<Intent> result = compiler.compile(intent, null);
Ray Milkey6e0fb302015-04-16 14:44:12 -0700231 assertThat(result, is(notNullValue()));
232 assertThat(result, hasSize(1));
233 Intent resultIntent = result.get(0);
234 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
235
236 if (resultIntent instanceof LinkCollectionIntent) {
237 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
Jonathan Hart066244c2015-06-23 09:46:19 -0700238 assertThat(linkIntent.links(), hasSize(ingress.length));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700239
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700240 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
241 assertThat(linkIntent.links(), linksHasPath("i2", "i3"));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700242 }
243 }
Yi Tseng2a81c9d2016-09-14 10:14:24 -0700244
245 /**
246 * Tests filtered ingress and egress.
247 */
248 @Test
249 public void testFilteredConnectPointIntent() {
250
251 Set<FilteredConnectPoint> ingress = ImmutableSet.of(
252 new FilteredConnectPoint(connectPoint("of1", 1),
253 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
254 new FilteredConnectPoint(connectPoint("of2", 1),
255 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
256 );
257
258 FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1));
259
260 MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress);
261 String[] hops = {"of3"};
262
263 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
264 assertThat(compiler, is(notNullValue()));
265
266 List<Intent> result = compiler.compile(intent, null);
267 assertThat(result, is(notNullValue()));
268 assertThat(result, hasSize(1));
269
270 Intent resultIntent = result.get(0);
271 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
272
273 if (resultIntent instanceof LinkCollectionIntent) {
274 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
275 assertThat(linkIntent.links(), hasSize(3));
276 assertThat(linkIntent.links(), linksHasPath("of1", "of3"));
277 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
278 assertThat(linkIntent.links(), linksHasPath("of3", "of4"));
279 }
280
281 }
282
283
Ray Milkeye6684082014-10-16 16:59:47 -0700284}