blob: b651bebe23f8b47bdb908432b72a5cf873fb74b1 [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 */
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;
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;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080035import org.onosproject.net.intent.impl.PathNotFoundException;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080036import org.onosproject.net.resource.Bandwidth;
37import org.onosproject.net.resource.Lambda;
38import org.onosproject.net.resource.LinkResourceService;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070039
Sho SHIMIZUb7052172015-02-20 11:09:21 -080040import java.util.Arrays;
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) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -070075 return new PointToPointIntent(APPID, selector, treatment,
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070076 connectPoint(ingressIdString, 1),
77 connectPoint(egressIdString, 1));
78 }
79
80 /**
Sho SHIMIZUb7052172015-02-20 11:09:21 -080081 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
82 *
83 * @param ingressIdString string for id of ingress device
84 * @param egressIdString string for id of egress device
85 * @param constraints constraints
86 * @return PointToPointIntent for the two device with constraints
87 */
88 private PointToPointIntent makeIntent(String ingressIdString,
89 String egressIdString, List<Constraint> constraints) {
90 return new PointToPointIntent(APPID, selector, treatment,
91 connectPoint(ingressIdString, 1),
92 connectPoint(egressIdString, 1),
93 constraints);
94 }
95
96 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070097 * Creates a compiler for HostToHost intents.
98 *
99 * @param hops string array describing the path hops to use when compiling
100 * @return HostToHost intent compiler
101 */
102 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800103 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -0800104 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700105 return compiler;
106 }
107
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800108 /**
109 * Creates a point to point intent compiler for a three switch linear
110 * topology.
111 *
112 * @param resourceService service to use for resource allocation requests
113 * @return point to point compiler
114 */
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800115 private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800116 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
117 compiler.resourceService = resourceService;
118 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
119 return compiler;
120 }
121
122 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700123 * Tests a pair of devices in an 8 hop path, forward direction.
124 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800125 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700126 public void testForwardPathCompilation() {
127
128 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700129
130 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
131 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700132
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700133 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700134 assertThat(result, is(Matchers.notNullValue()));
135 assertThat(result, hasSize(1));
136 Intent forwardResultIntent = result.get(0);
137 assertThat(forwardResultIntent instanceof PathIntent, is(true));
138
139 if (forwardResultIntent instanceof PathIntent) {
140 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
141 // 7 links for the hops, plus one default lnk on ingress and egress
142 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
143 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
144 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
145 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
146 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
147 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
148 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
149 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
150 }
151 }
152
153 /**
154 * Tests a pair of devices in an 8 hop path, forward direction.
155 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800156 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700157 public void testReversePathCompilation() {
158
159 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700160
161 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
162 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700163
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700164 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700165 assertThat(result, is(Matchers.notNullValue()));
166 assertThat(result, hasSize(1));
167 Intent reverseResultIntent = result.get(0);
168 assertThat(reverseResultIntent instanceof PathIntent, is(true));
169
170 if (reverseResultIntent instanceof PathIntent) {
171 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
172 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
173 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
174 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
175 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
176 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
177 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
178 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
179 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
180 }
181 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800182
183 /**
184 * Tests compilation of the intent which designates two different ports on the same switch.
185 */
186 @Test
187 public void testSameSwitchDifferentPortsIntentCompilation() {
188 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
189 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
190 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
191
192 String[] hops = {"1"};
193 PointToPointIntentCompiler sut = makeCompiler(hops);
194
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700195 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800196
197 assertThat(compiled, hasSize(1));
198 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
199 Path path = ((PathIntent) compiled.get(0)).path();
200
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800201 assertThat(path.links(), hasSize(2));
202 Link firstLink = path.links().get(0);
203 assertThat(firstLink, is(createEdgeLink(src, true)));
204 Link secondLink = path.links().get(1);
205 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800206 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800207
208 /**
209 * Tests that requests with sufficient available bandwidth succeed.
210 */
211 @Test
212 public void testBandwidthConstrainedIntentSuccess() {
213
214 final LinkResourceService resourceService =
215 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800216 final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800217
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800218 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
219
220 String[] hops = {"s1", "s2", "s3"};
221 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
222
223 final List<Intent> compiledIntents = compiler.compile(intent, null, null);
224
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800225 assertThat(compiledIntents, Matchers.notNullValue());
226 assertThat(compiledIntents, hasSize(1));
227 }
228
229 /**
230 * Tests that requests with insufficient available bandwidth fail.
231 */
232 @Test
233 public void testBandwidthConstrainedIntentFailure() {
234
235 final LinkResourceService resourceService =
236 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800237 final List<Constraint> constraints = Arrays.asList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800238
239 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800240 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
241
242 String[] hops = {"s1", "s2", "s3"};
243 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
244
245 compiler.compile(intent, null, null);
246
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800247 fail("Point to Point compilation with insufficient bandwidth does "
248 + "not throw exception.");
249 } catch (PathNotFoundException noPath) {
250 assertThat(noPath.getMessage(), containsString("No path"));
251 }
252 }
253
254 /**
255 * Tests that requests for available lambdas are successful.
256 */
257 @Test
258 public void testLambdaConstrainedIntentSuccess() {
259
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800260 final List<Constraint> constraints = Arrays.asList(new LambdaConstraint(Lambda.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800261 final LinkResourceService resourceService =
262 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
263
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800264 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
265
266 String[] hops = {"s1", "s2", "s3"};
267 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
268
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800269 final List<Intent> compiledIntents =
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800270 compiler.compile(intent, null, null);
271
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800272 assertThat(compiledIntents, Matchers.notNullValue());
273 assertThat(compiledIntents, hasSize(1));
274 }
275
276 /**
277 * Tests that requests for lambdas when there are no available lambdas
278 * fail.
279 */
280 @Test
281 public void testLambdaConstrainedIntentFailure() {
282
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800283 final List<Constraint> constraints = Arrays.asList(new LambdaConstraint(Lambda.valueOf(1)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800284 final LinkResourceService resourceService =
285 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
286 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800287 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
288
289 String[] hops = {"s1", "s2", "s3"};
290 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
291
292 compiler.compile(intent, null, null);
293
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800294 fail("Point to Point compilation with no available lambda does "
295 + "not throw exception.");
296 } catch (PathNotFoundException noPath) {
297 assertThat(noPath.getMessage(), containsString("No path"));
298 }
299 }
300
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700301}