blob: 6270f5cf30bdf653d96fabcf78fa6a108bdc0ef9 [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;
22import org.onlab.packet.VlanId;
23import org.onosproject.TestApplicationId;
24import org.onosproject.core.ApplicationId;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.FilteredConnectPoint;
27import org.onosproject.net.flow.DefaultTrafficSelector;
28import 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.SinglePointToMultiPointIntent;
35
36import java.util.HashSet;
37import java.util.List;
38import java.util.Set;
39
40import 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,
94 Set<FilteredConnectPoint> egress) {
95 return SinglePointToMultiPointIntent.builder()
96 .appId(APPID)
97 .treatment(treatment)
98 .filteredIngressPoint(ingress)
99 .filteredEgressPoints(egress)
100 .build();
101 }
102
103 /**
104 * Creates a compiler for SinglePointToMultiPoint intents.
105 *
106 * @param hops hops to use while computing paths for this intent
107 * @return SinglePointToMultiPoint intent
108 */
109 private SinglePointToMultiPointIntentCompiler makeCompiler(String[] hops) {
110 SinglePointToMultiPointIntentCompiler compiler =
111 new SinglePointToMultiPointIntentCompiler();
112
113 compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops);
114 return compiler;
115 }
116
117 /**
118 * Tests a single ingress point with 8 hops to its egress point.
119 */
120 @Test
121 public void testSingleLongPathCompilation() {
122
123 String ingress = "ingress";
124 String[] egress = {"egress"};
125
126 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
127 assertThat(intent, is(notNullValue()));
128
129 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"};
130 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
131 assertThat(compiler, is(notNullValue()));
132
133 List<Intent> result = compiler.compile(intent, null);
134 assertThat(result, is(Matchers.notNullValue()));
135 assertThat(result, hasSize(1));
136 Intent resultIntent = result.get(0);
137 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
138
139 if (resultIntent instanceof LinkCollectionIntent) {
140 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
141 assertThat(linkIntent.links(), hasSize(9));
142 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
143 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
144 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
145 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
146 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
147 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
148 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
149 }
150 }
151
152 /**
153 * Tests a simple topology where two egress points share some path segments
154 * and some path segments are not shared.
155 */
156 @Test
157 public void testTwoIngressCompilation() {
158 String ingress = "ingress";
159 String[] egress = {"egress1", "egress2"};
160
161 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
162 assertThat(intent, is(notNullValue()));
163
164 final String[] hops = {"inner1", "inner2"};
165 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
166 assertThat(compiler, is(notNullValue()));
167
168 List<Intent> result = compiler.compile(intent, null);
169 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("ingress", "inner1"));
178 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
179 assertThat(linkIntent.links(), linksHasPath("inner2", "egress1"));
180 assertThat(linkIntent.links(), linksHasPath("inner2", "egress2"));
181 }
182 }
183
184 /**
185 * Tests a large number of ingress points that share a common path to the
186 * egress point.
187 */
188 @Test
189 public void testMultiIngressCompilation() {
190 String ingress = "i";
191 String[] egress = {"e1", "e2", "e3", "e4", "e5",
192 "e6", "e7", "e8", "e9", "e10"};
193
194 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
195 assertThat(intent, is(notNullValue()));
196
197 final String[] hops = {"n1"};
198 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
199 assertThat(compiler, is(notNullValue()));
200
201 List<Intent> result = compiler.compile(intent, null);
202 assertThat(result, is(notNullValue()));
203 assertThat(result, hasSize(1));
204 Intent resultIntent = result.get(0);
205 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
206
207 if (resultIntent instanceof LinkCollectionIntent) {
208 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
209 assertThat(linkIntent.links(), hasSize(egress.length + 1));
210 for (String egressToCheck : egress) {
211 assertThat(linkIntent.links(), linksHasPath("n1", egressToCheck));
212 }
213 assertThat(linkIntent.links(), linksHasPath(ingress, "n1"));
214 }
215 }
216
217 /**
218 * Tests ingress and egress on the same device.
219 */
220 @Test
221 public void testSameDeviceCompilation() {
222 String ingress = "i1";
223 String[] egress = {"i2", "i3"};
224
225 SinglePointToMultiPointIntent intent = makeIntent(ingress, egress);
226 assertThat(intent, is(notNullValue()));
227
228 final String[] hops = {};
229 SinglePointToMultiPointIntentCompiler compiler = makeCompiler(hops);
230 assertThat(compiler, is(notNullValue()));
231
232 List<Intent> result = compiler.compile(intent, null);
233 assertThat(result, is(notNullValue()));
234 assertThat(result, hasSize(1));
235 Intent resultIntent = result.get(0);
236 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
237
238 if (resultIntent instanceof LinkCollectionIntent) {
239 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
240 assertThat(linkIntent.links(), hasSize(egress.length));
241
242 assertThat(linkIntent.links(), linksHasPath("i1", "i2"));
243 assertThat(linkIntent.links(), linksHasPath("i1", "i3"));
244 }
245 }
246
247 /**
248 * Tests filtered ingress and egress.
249 */
250 @Test
251 public void testFilteredConnectPointIntent() {
252
253 FilteredConnectPoint ingress = new FilteredConnectPoint(connectPoint("of1", 1));
254
255 Set<FilteredConnectPoint> egress = ImmutableSet.of(
256 new FilteredConnectPoint(connectPoint("of3", 1),
257 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()),
258 new FilteredConnectPoint(connectPoint("of4", 1),
259 DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build())
260 );
261
262
263 SinglePointToMultiPointIntent intent = makeFilteredConnectPointIntent(ingress, egress);
264 String[] hops = {"of2"};
265
266 SinglePointToMultiPointIntentCompiler 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
280 assertThat(linkIntent.links(), linksHasPath("of1", "of2"));
281 assertThat(linkIntent.links(), linksHasPath("of2", "of3"));
282 assertThat(linkIntent.links(), linksHasPath("of2", "of4"));
283
284 Set<FilteredConnectPoint> ingressPoints = linkIntent.filteredIngressPoints();
285 assertThat("Link collection ingress points do not match base intent",
286 ingressPoints.size() == 1 && ingressPoints.contains(intent.filteredIngressPoint()));
287
288 assertThat("Link collection egress points do not match base intent",
289 linkIntent.filteredEgressPoints().equals(intent.filteredEgressPoints()));
290 }
291
292 }
293}