blob: 8f12a1c7970a2df78a12f51a303c09bf56b605b5 [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 Milkeyc0fa4db2014-10-17 08:49:54 -070017
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070018import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080019import org.junit.Test;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070020import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Link;
25import org.onosproject.net.Path;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080029import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.PathIntent;
33import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080034import org.onosproject.net.intent.constraint.BandwidthConstraint;
35import org.onosproject.net.intent.constraint.LambdaConstraint;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080036import org.onosproject.net.intent.impl.PathNotFoundException;
Brian O'Connor6de2e202015-05-21 14:30:41 -070037import org.onosproject.net.resource.link.LambdaResource;
38import org.onosproject.net.resource.link.LinkResourceService;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070039
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070040import java.util.Collections;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070041import java.util.List;
42
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080043import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070044import static org.hamcrest.MatcherAssert.assertThat;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080045import static org.hamcrest.Matchers.containsString;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070046import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080048import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
50import static org.onosproject.net.DeviceId.deviceId;
51import static org.onosproject.net.NetTestTools.APP_ID;
52import static org.onosproject.net.NetTestTools.connectPoint;
53import static org.onosproject.net.PortNumber.portNumber;
54import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070055
56/**
57 * Unit tests for the HostToHost intent compiler.
58 */
Ray Milkey37f6a382014-11-25 14:54:42 -080059public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070060
Thomas Vachuskab97cf282014-10-20 23:31:12 -070061 private static final ApplicationId APPID = new TestApplicationId("foo");
62
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070063 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
64 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
65
66 /**
67 * Creates a PointToPoint intent based on ingress and egress device Ids.
68 *
69 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070070 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070071 * @return PointToPointIntent for the two devices
72 */
73 private PointToPointIntent makeIntent(String ingressIdString,
74 String egressIdString) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070075 return PointToPointIntent.builder()
76 .appId(APPID)
77 .selector(selector)
78 .treatment(treatment)
79 .ingressPoint(connectPoint(ingressIdString, 1))
80 .egressPoint(connectPoint(egressIdString, 1))
81 .build();
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070082 }
83
84 /**
Sho SHIMIZUb7052172015-02-20 11:09:21 -080085 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
86 *
87 * @param ingressIdString string for id of ingress device
88 * @param egressIdString string for id of egress device
89 * @param constraints constraints
90 * @return PointToPointIntent for the two device with constraints
91 */
92 private PointToPointIntent makeIntent(String ingressIdString,
93 String egressIdString, List<Constraint> constraints) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070094 return PointToPointIntent.builder()
95 .appId(APPID)
96 .selector(selector)
97 .treatment(treatment)
98 .ingressPoint(connectPoint(ingressIdString, 1))
99 .egressPoint(connectPoint(egressIdString, 1))
100 .constraints(constraints)
101 .build();
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800102 }
103
104 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700105 * Creates a compiler for HostToHost intents.
106 *
107 * @param hops string array describing the path hops to use when compiling
108 * @return HostToHost intent compiler
109 */
110 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800111 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -0800112 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700113 return compiler;
114 }
115
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800116 /**
117 * Creates a point to point intent compiler for a three switch linear
118 * topology.
119 *
120 * @param resourceService service to use for resource allocation requests
121 * @return point to point compiler
122 */
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800123 private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800124 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
125 compiler.resourceService = resourceService;
126 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
127 return compiler;
128 }
129
130 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700131 * Tests a pair of devices in an 8 hop path, forward direction.
132 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800133 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700134 public void testForwardPathCompilation() {
135
136 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700137
138 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
139 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700140
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700141 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700142 assertThat(result, is(Matchers.notNullValue()));
143 assertThat(result, hasSize(1));
144 Intent forwardResultIntent = result.get(0);
145 assertThat(forwardResultIntent instanceof PathIntent, is(true));
146
147 if (forwardResultIntent instanceof PathIntent) {
148 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
149 // 7 links for the hops, plus one default lnk on ingress and egress
150 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
151 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
152 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
153 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
154 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
155 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
156 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
157 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
158 }
159 }
160
161 /**
162 * Tests a pair of devices in an 8 hop path, forward direction.
163 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800164 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700165 public void testReversePathCompilation() {
166
167 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700168
169 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
170 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700171
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700172 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700173 assertThat(result, is(Matchers.notNullValue()));
174 assertThat(result, hasSize(1));
175 Intent reverseResultIntent = result.get(0);
176 assertThat(reverseResultIntent instanceof PathIntent, is(true));
177
178 if (reverseResultIntent instanceof PathIntent) {
179 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
180 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
181 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
182 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
183 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
184 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
185 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
186 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
187 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
188 }
189 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800190
191 /**
192 * Tests compilation of the intent which designates two different ports on the same switch.
193 */
194 @Test
195 public void testSameSwitchDifferentPortsIntentCompilation() {
196 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
197 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700198 PointToPointIntent intent = PointToPointIntent.builder()
199 .appId(APP_ID)
200 .selector(selector)
201 .treatment(treatment)
202 .ingressPoint(src)
203 .egressPoint(dst)
204 .build();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800205
206 String[] hops = {"1"};
207 PointToPointIntentCompiler sut = makeCompiler(hops);
208
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700209 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800210
211 assertThat(compiled, hasSize(1));
212 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
213 Path path = ((PathIntent) compiled.get(0)).path();
214
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800215 assertThat(path.links(), hasSize(2));
216 Link firstLink = path.links().get(0);
217 assertThat(firstLink, is(createEdgeLink(src, true)));
218 Link secondLink = path.links().get(1);
219 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800220 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800221
222 /**
223 * Tests that requests with sufficient available bandwidth succeed.
224 */
225 @Test
226 public void testBandwidthConstrainedIntentSuccess() {
227
228 final LinkResourceService resourceService =
229 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700230 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800231 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800232
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800233 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
234
235 String[] hops = {"s1", "s2", "s3"};
236 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
237
238 final List<Intent> compiledIntents = compiler.compile(intent, null, null);
239
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800240 assertThat(compiledIntents, Matchers.notNullValue());
241 assertThat(compiledIntents, hasSize(1));
242 }
243
244 /**
245 * Tests that requests with insufficient available bandwidth fail.
246 */
247 @Test
248 public void testBandwidthConstrainedIntentFailure() {
249
250 final LinkResourceService resourceService =
251 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700252 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800253 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800254
255 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800256 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
257
258 String[] hops = {"s1", "s2", "s3"};
259 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
260
261 compiler.compile(intent, null, null);
262
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800263 fail("Point to Point compilation with insufficient bandwidth does "
264 + "not throw exception.");
265 } catch (PathNotFoundException noPath) {
266 assertThat(noPath.getMessage(), containsString("No path"));
267 }
268 }
269
270 /**
271 * Tests that requests for available lambdas are successful.
272 */
273 @Test
274 public void testLambdaConstrainedIntentSuccess() {
275
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700276 final List<Constraint> constraints =
277 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800278 final LinkResourceService resourceService =
279 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
280
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800281 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
282
283 String[] hops = {"s1", "s2", "s3"};
284 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
285
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800286 final List<Intent> compiledIntents =
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800287 compiler.compile(intent, null, null);
288
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800289 assertThat(compiledIntents, Matchers.notNullValue());
290 assertThat(compiledIntents, hasSize(1));
291 }
292
293 /**
294 * Tests that requests for lambdas when there are no available lambdas
295 * fail.
296 */
297 @Test
298 public void testLambdaConstrainedIntentFailure() {
299
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700300 final List<Constraint> constraints =
301 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800302 final LinkResourceService resourceService =
303 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
304 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800305 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
306
307 String[] hops = {"s1", "s2", "s3"};
308 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
309
310 compiler.compile(intent, null, null);
311
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800312 fail("Point to Point compilation with no available lambda does "
313 + "not throw exception.");
314 } catch (PathNotFoundException noPath) {
315 assertThat(noPath.getMessage(), containsString("No path"));
316 }
317 }
318
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700319}