blob: 8115da58715d73081eba88216b905f1edffddbb8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.TestApplicationId;
21import org.onosproject.core.ApplicationId;
22import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.Link;
24import org.onosproject.net.Path;
25import org.onosproject.net.flow.TrafficSelector;
26import org.onosproject.net.flow.TrafficTreatment;
27import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080028import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentTestsMocks;
31import org.onosproject.net.intent.PathIntent;
32import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080033import org.onosproject.net.intent.constraint.BandwidthConstraint;
34import org.onosproject.net.intent.constraint.LambdaConstraint;
35import org.onosproject.net.resource.Bandwidth;
36import org.onosproject.net.resource.Lambda;
37import org.onosproject.net.resource.LinkResourceService;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070038
Sho SHIMIZUb7052172015-02-20 11:09:21 -080039import java.util.Arrays;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.List;
41
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080042import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070043import static org.hamcrest.MatcherAssert.assertThat;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080044import static org.hamcrest.Matchers.containsString;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070045import static org.hamcrest.Matchers.hasSize;
46import static org.hamcrest.Matchers.is;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080047import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080048import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
49import static org.onosproject.net.DeviceId.deviceId;
50import static org.onosproject.net.NetTestTools.APP_ID;
51import static org.onosproject.net.NetTestTools.connectPoint;
52import static org.onosproject.net.PortNumber.portNumber;
53import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070054
55/**
56 * Unit tests for the HostToHost intent compiler.
57 */
Ray Milkey37f6a382014-11-25 14:54:42 -080058public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070059
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 private static final ApplicationId APPID = new TestApplicationId("foo");
61
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070062 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
63 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
64
65 /**
66 * Creates a PointToPoint intent based on ingress and egress device Ids.
67 *
68 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070069 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070070 * @return PointToPointIntent for the two devices
71 */
72 private PointToPointIntent makeIntent(String ingressIdString,
73 String egressIdString) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070074 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070075 connectPoint(ingressIdString, 1),
76 connectPoint(egressIdString, 1));
77 }
78
79 /**
Sho SHIMIZUb7052172015-02-20 11:09:21 -080080 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
81 *
82 * @param ingressIdString string for id of ingress device
83 * @param egressIdString string for id of egress device
84 * @param constraints constraints
85 * @return PointToPointIntent for the two device with constraints
86 */
87 private PointToPointIntent makeIntent(String ingressIdString,
88 String egressIdString, List<Constraint> constraints) {
89 return new PointToPointIntent(APPID, selector, treatment,
90 connectPoint(ingressIdString, 1),
91 connectPoint(egressIdString, 1),
92 constraints);
93 }
94
95 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070096 * Creates a compiler for HostToHost intents.
97 *
98 * @param hops string array describing the path hops to use when compiling
99 * @return HostToHost intent compiler
100 */
101 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800102 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -0800103 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700104 return compiler;
105 }
106
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800107 /**
108 * Creates a point to point intent compiler for a three switch linear
109 * topology.
110 *
111 * @param resourceService service to use for resource allocation requests
112 * @return point to point compiler
113 */
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800114 private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800115 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
116 compiler.resourceService = resourceService;
117 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
118 return compiler;
119 }
120
121 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700122 * Tests a pair of devices in an 8 hop path, forward direction.
123 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800124 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700125 public void testForwardPathCompilation() {
126
127 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700128
129 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
130 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700131
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700132 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700133 assertThat(result, is(Matchers.notNullValue()));
134 assertThat(result, hasSize(1));
135 Intent forwardResultIntent = result.get(0);
136 assertThat(forwardResultIntent instanceof PathIntent, is(true));
137
138 if (forwardResultIntent instanceof PathIntent) {
139 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
140 // 7 links for the hops, plus one default lnk on ingress and egress
141 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
142 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
143 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
144 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
145 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
146 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
147 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
148 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
149 }
150 }
151
152 /**
153 * Tests a pair of devices in an 8 hop path, forward direction.
154 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800155 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700156 public void testReversePathCompilation() {
157
158 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700159
160 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
161 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700162
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700163 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700164 assertThat(result, is(Matchers.notNullValue()));
165 assertThat(result, hasSize(1));
166 Intent reverseResultIntent = result.get(0);
167 assertThat(reverseResultIntent instanceof PathIntent, is(true));
168
169 if (reverseResultIntent instanceof PathIntent) {
170 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
171 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
172 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
173 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
174 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
175 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
176 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
177 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
178 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
179 }
180 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800181
182 /**
183 * Tests compilation of the intent which designates two different ports on the same switch.
184 */
185 @Test
186 public void testSameSwitchDifferentPortsIntentCompilation() {
187 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
188 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
189 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
190
191 String[] hops = {"1"};
192 PointToPointIntentCompiler sut = makeCompiler(hops);
193
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700194 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800195
196 assertThat(compiled, hasSize(1));
197 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
198 Path path = ((PathIntent) compiled.get(0)).path();
199
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800200 assertThat(path.links(), hasSize(2));
201 Link firstLink = path.links().get(0);
202 assertThat(firstLink, is(createEdgeLink(src, true)));
203 Link secondLink = path.links().get(1);
204 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800205 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800206
207 /**
208 * Tests that requests with sufficient available bandwidth succeed.
209 */
210 @Test
211 public void testBandwidthConstrainedIntentSuccess() {
212
213 final LinkResourceService resourceService =
214 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800215 final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800216
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800217 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
218
219 String[] hops = {"s1", "s2", "s3"};
220 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
221
222 final List<Intent> compiledIntents = compiler.compile(intent, null, null);
223
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800224 assertThat(compiledIntents, Matchers.notNullValue());
225 assertThat(compiledIntents, hasSize(1));
226 }
227
228 /**
229 * Tests that requests with insufficient available bandwidth fail.
230 */
231 @Test
232 public void testBandwidthConstrainedIntentFailure() {
233
234 final LinkResourceService resourceService =
235 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800236 final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800237
238 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800239 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
240
241 String[] hops = {"s1", "s2", "s3"};
242 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
243
244 compiler.compile(intent, null, null);
245
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800246 fail("Point to Point compilation with insufficient bandwidth does "
247 + "not throw exception.");
248 } catch (PathNotFoundException noPath) {
249 assertThat(noPath.getMessage(), containsString("No path"));
250 }
251 }
252
253 /**
254 * Tests that requests for available lambdas are successful.
255 */
256 @Test
257 public void testLambdaConstrainedIntentSuccess() {
258
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800259 final List<Constraint> constraints = Arrays.asList(new LambdaConstraint(Lambda.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800260 final LinkResourceService resourceService =
261 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
262
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800263 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
264
265 String[] hops = {"s1", "s2", "s3"};
266 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
267
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800268 final List<Intent> compiledIntents =
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800269 compiler.compile(intent, null, null);
270
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800271 assertThat(compiledIntents, Matchers.notNullValue());
272 assertThat(compiledIntents, hasSize(1));
273 }
274
275 /**
276 * Tests that requests for lambdas when there are no available lambdas
277 * fail.
278 */
279 @Test
280 public void testLambdaConstrainedIntentFailure() {
281
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800282 final List<Constraint> constraints = Arrays.asList(new LambdaConstraint(Lambda.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800283 final LinkResourceService resourceService =
284 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
285 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800286 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
287
288 String[] hops = {"s1", "s2", "s3"};
289 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
290
291 compiler.compile(intent, null, null);
292
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800293 fail("Point to Point compilation with no available lambda does "
294 + "not throw exception.");
295 } catch (PathNotFoundException noPath) {
296 assertThat(noPath.getMessage(), containsString("No path"));
297 }
298 }
299
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700300}