blob: a3b452574ec98fe6e67225a80f4ed0b80e335c8d [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Ray Milkey6e0fb302015-04-16 14:44:12 -070018import java.util.HashSet;
19import java.util.List;
20import java.util.Set;
21
Ray Milkeye6684082014-10-16 16:59:47 -070022import org.hamcrest.Matchers;
23import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.TestApplicationId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070025import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.ConnectPoint;
27import org.onosproject.net.ElementId;
28import org.onosproject.net.Path;
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.MultiPointToSinglePointIntent;
36import org.onosproject.net.topology.LinkWeight;
37import org.onosproject.net.topology.PathService;
Ray Milkeye6684082014-10-16 16:59:47 -070038
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;
45import static org.onosproject.net.NetTestTools.createPath;
46import 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 /**
59 * Mock path service for creating paths within the test.
60 */
61 private static class MockPathService implements PathService {
62
63 final String[] pathHops;
64
65 /**
66 * Constructor that provides a set of hops to mock.
67 *
68 * @param pathHops path hops to mock
69 */
70 MockPathService(String[] pathHops) {
71 this.pathHops = pathHops;
72 }
73
74 @Override
75 public Set<Path> getPaths(ElementId src, ElementId dst) {
76 Set<Path> result = new HashSet<>();
77
78 String[] allHops = new String[pathHops.length + 1];
79 allHops[0] = src.toString();
Ray Milkey6e0fb302015-04-16 14:44:12 -070080 if (pathHops.length != 0) {
81 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
82 }
Ray Milkeye6684082014-10-16 16:59:47 -070083 result.add(createPath(allHops));
Ray Milkey6e0fb302015-04-16 14:44:12 -070084
Ray Milkeye6684082014-10-16 16:59:47 -070085 return result;
86 }
87
88 @Override
89 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
90 return null;
91 }
92 }
93
94 /**
95 * Creates a MultiPointToSinglePoint intent for a group of ingress points
96 * and an egress point.
97 *
98 * @param ingressIds array of ingress device ids
Thomas Vachuskab97cf282014-10-20 23:31:12 -070099 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -0700100 * @return MultiPointToSinglePoint intent
101 */
102 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
103 Set<ConnectPoint> ingressPoints = new HashSet<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -0700104 ConnectPoint egressPoint = connectPoint(egressId, 2);
Ray Milkeye6684082014-10-16 16:59:47 -0700105
106 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700107 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -0700108 }
109
Ray Milkeyebc5d222015-03-18 15:45:36 -0700110 return MultiPointToSinglePointIntent.builder()
111 .appId(APPID)
112 .selector(selector)
113 .treatment(treatment)
114 .ingressPoints(ingressPoints)
115 .egressPoint(egressPoint)
116 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700117 }
118
119 /**
120 * Creates a compiler for MultiPointToSinglePoint intents.
121 *
122 * @param hops hops to use while computing paths for this intent
123 * @return MultiPointToSinglePoint intent
124 */
125 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
126 MultiPointToSinglePointIntentCompiler compiler =
127 new MultiPointToSinglePointIntentCompiler();
128 compiler.pathService = new MockPathService(hops);
Ray Milkeye6684082014-10-16 16:59:47 -0700129 return compiler;
130 }
131
132 /**
133 * Tests a single ingress point with 8 hops to its egress point.
134 */
135 @Test
136 public void testSingleLongPathCompilation() {
137
138 String[] ingress = {"ingress"};
139 String egress = "egress";
140
141 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
142 assertThat(intent, is(notNullValue()));
143
144 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700145 egress};
Ray Milkeye6684082014-10-16 16:59:47 -0700146 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
147 assertThat(compiler, is(notNullValue()));
148
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700149 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700150 assertThat(result, is(Matchers.notNullValue()));
151 assertThat(result, hasSize(1));
152 Intent resultIntent = result.get(0);
153 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
154
155 if (resultIntent instanceof LinkCollectionIntent) {
156 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
157 assertThat(linkIntent.links(), hasSize(9));
158 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
159 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
160 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
161 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
162 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
163 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
164 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
165 }
166 }
167
168 /**
169 * Tests a simple topology where two ingress points share some path segments
170 * and some path segments are not shared.
171 */
172 @Test
173 public void testTwoIngressCompilation() {
174 String[] ingress = {"ingress1", "ingress2"};
175 String egress = "egress";
176
177 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
178 assertThat(intent, is(notNullValue()));
179
180 final String[] hops = {"inner1", "inner2", egress};
181 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
182 assertThat(compiler, is(notNullValue()));
183
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700184 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700185 assertThat(result, is(notNullValue()));
186 assertThat(result, hasSize(1));
187 Intent resultIntent = result.get(0);
188 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
189
190 if (resultIntent instanceof LinkCollectionIntent) {
191 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
192 assertThat(linkIntent.links(), hasSize(4));
193 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
194 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
195 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
196 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
197 }
198 }
199
200 /**
201 * Tests a large number of ingress points that share a common path to the
202 * egress point.
203 */
204 @Test
205 public void testMultiIngressCompilation() {
206 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700207 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700208 String egress = "e";
209
210 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
211 assertThat(intent, is(notNullValue()));
212
213 final String[] hops = {"n1", egress};
214 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
215 assertThat(compiler, is(notNullValue()));
216
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700217 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700218 assertThat(result, is(notNullValue()));
219 assertThat(result, hasSize(1));
220 Intent resultIntent = result.get(0);
221 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
222
223 if (resultIntent instanceof LinkCollectionIntent) {
224 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
225 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
226 for (String ingressToCheck : ingress) {
227 assertThat(linkIntent.links(),
228 linksHasPath(ingressToCheck,
229 "n1"));
230 }
231 assertThat(linkIntent.links(), linksHasPath("n1", egress));
232 }
233 }
Ray Milkey6e0fb302015-04-16 14:44:12 -0700234
235 /**
236 * Tests ingress and egress on the same device.
237 */
238 @Test
239 public void testSameDeviceCompilation() {
240 String[] ingress = {"i1", "i2"};
241 String egress = "i1";
242
243 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
244 assertThat(intent, is(notNullValue()));
245
246 final String[] hops = {"i1", "i2"};
247 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
248 assertThat(compiler, is(notNullValue()));
249
250 List<Intent> result = compiler.compile(intent, null, null);
251 assertThat(result, is(notNullValue()));
252 assertThat(result, hasSize(1));
253 Intent resultIntent = result.get(0);
254 assertThat(resultIntent, instanceOf(LinkCollectionIntent.class));
255
256 if (resultIntent instanceof LinkCollectionIntent) {
257 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
258 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
259
260 assertThat(linkIntent.links(), linksHasPath("i2", "i1"));
261
262 assertThat(linkIntent.links(), linksHasPath("i1", "i1"));
263 }
264 }
Ray Milkeye6684082014-10-16 16:59:47 -0700265}