2 * Copyright 2014-2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.intent.impl.compiler;
18 import org.hamcrest.Matchers;
19 import org.junit.Test;
20 import org.onlab.util.Bandwidth;
21 import org.onosproject.TestApplicationId;
22 import org.onosproject.core.ApplicationId;
23 import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.Link;
25 import org.onosproject.net.Path;
26 import org.onosproject.net.flow.TrafficSelector;
27 import org.onosproject.net.flow.TrafficTreatment;
28 import org.onosproject.net.intent.AbstractIntentTest;
29 import org.onosproject.net.intent.Constraint;
30 import org.onosproject.net.intent.Intent;
31 import org.onosproject.net.intent.IntentTestsMocks;
32 import org.onosproject.net.intent.PathIntent;
33 import org.onosproject.net.intent.PointToPointIntent;
34 import org.onosproject.net.intent.constraint.BandwidthConstraint;
35 import org.onosproject.net.intent.constraint.LambdaConstraint;
36 import org.onosproject.net.intent.impl.PathNotFoundException;
37 import org.onosproject.net.resource.link.BandwidthResource;
38 import org.onosproject.net.resource.link.LambdaResource;
39 import org.onosproject.net.resource.link.LinkResourceService;
41 import java.util.Collections;
42 import java.util.List;
44 import static org.hamcrest.CoreMatchers.instanceOf;
45 import static org.hamcrest.MatcherAssert.assertThat;
46 import static org.hamcrest.Matchers.containsString;
47 import static org.hamcrest.Matchers.hasSize;
48 import static org.hamcrest.Matchers.is;
49 import static org.junit.Assert.fail;
50 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
51 import static org.onosproject.net.DeviceId.deviceId;
52 import static org.onosproject.net.NetTestTools.APP_ID;
53 import static org.onosproject.net.NetTestTools.connectPoint;
54 import static org.onosproject.net.PortNumber.portNumber;
55 import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
58 * Unit tests for the HostToHost intent compiler.
60 public class PointToPointIntentCompilerTest extends AbstractIntentTest {
62 private static final ApplicationId APPID = new TestApplicationId("foo");
64 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
65 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
68 * Creates a PointToPoint intent based on ingress and egress device Ids.
70 * @param ingressIdString string for id of ingress device
71 * @param egressIdString string for id of egress device
72 * @return PointToPointIntent for the two devices
74 private PointToPointIntent makeIntent(String ingressIdString,
75 String egressIdString) {
76 return PointToPointIntent.builder()
80 .ingressPoint(connectPoint(ingressIdString, 1))
81 .egressPoint(connectPoint(egressIdString, 1))
86 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
88 * @param ingressIdString string for id of ingress device
89 * @param egressIdString string for id of egress device
90 * @param constraints constraints
91 * @return PointToPointIntent for the two device with constraints
93 private PointToPointIntent makeIntent(String ingressIdString,
94 String egressIdString, List<Constraint> constraints) {
95 return PointToPointIntent.builder()
99 .ingressPoint(connectPoint(ingressIdString, 1))
100 .egressPoint(connectPoint(egressIdString, 1))
101 .constraints(constraints)
106 * Creates a compiler for HostToHost intents.
108 * @param hops string array describing the path hops to use when compiling
109 * @return HostToHost intent compiler
111 private PointToPointIntentCompiler makeCompiler(String[] hops) {
112 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
113 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
118 * Creates a point to point intent compiler for a three switch linear
121 * @param resourceService service to use for resource allocation requests
122 * @return point to point compiler
124 private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) {
125 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
126 compiler.resourceService = resourceService;
127 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
132 * Tests a pair of devices in an 8 hop path, forward direction.
135 public void testForwardPathCompilation() {
137 PointToPointIntent intent = makeIntent("d1", "d8");
139 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
140 PointToPointIntentCompiler compiler = makeCompiler(hops);
142 List<Intent> result = compiler.compile(intent, null, null);
143 assertThat(result, is(Matchers.notNullValue()));
144 assertThat(result, hasSize(1));
145 Intent forwardResultIntent = result.get(0);
146 assertThat(forwardResultIntent instanceof PathIntent, is(true));
148 if (forwardResultIntent instanceof PathIntent) {
149 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
150 // 7 links for the hops, plus one default lnk on ingress and egress
151 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
152 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
153 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
154 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
155 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
156 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
157 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
158 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
163 * Tests a pair of devices in an 8 hop path, forward direction.
166 public void testReversePathCompilation() {
168 PointToPointIntent intent = makeIntent("d8", "d1");
170 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
171 PointToPointIntentCompiler compiler = makeCompiler(hops);
173 List<Intent> result = compiler.compile(intent, null, null);
174 assertThat(result, is(Matchers.notNullValue()));
175 assertThat(result, hasSize(1));
176 Intent reverseResultIntent = result.get(0);
177 assertThat(reverseResultIntent instanceof PathIntent, is(true));
179 if (reverseResultIntent instanceof PathIntent) {
180 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
181 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
182 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
183 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
184 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
185 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
186 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
187 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
188 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
193 * Tests compilation of the intent which designates two different ports on the same switch.
196 public void testSameSwitchDifferentPortsIntentCompilation() {
197 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
198 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
199 PointToPointIntent intent = PointToPointIntent.builder()
202 .treatment(treatment)
207 String[] hops = {"1"};
208 PointToPointIntentCompiler sut = makeCompiler(hops);
210 List<Intent> compiled = sut.compile(intent, null, null);
212 assertThat(compiled, hasSize(1));
213 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
214 Path path = ((PathIntent) compiled.get(0)).path();
216 assertThat(path.links(), hasSize(2));
217 Link firstLink = path.links().get(0);
218 assertThat(firstLink, is(createEdgeLink(src, true)));
219 Link secondLink = path.links().get(1);
220 assertThat(secondLink, is(createEdgeLink(dst, false)));
224 * Tests that requests with sufficient available bandwidth succeed.
227 public void testBandwidthConstrainedIntentSuccess() {
229 final LinkResourceService resourceService =
230 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
231 final List<Constraint> constraints =
232 Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
234 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
236 String[] hops = {"s1", "s2", "s3"};
237 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
239 final List<Intent> compiledIntents = compiler.compile(intent, null, null);
241 assertThat(compiledIntents, Matchers.notNullValue());
242 assertThat(compiledIntents, hasSize(1));
246 * Tests that requests with insufficient available bandwidth fail.
249 public void testBandwidthConstrainedIntentFailure() {
251 final LinkResourceService resourceService =
252 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
253 final List<Constraint> constraints =
254 Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
257 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
259 String[] hops = {"s1", "s2", "s3"};
260 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
262 compiler.compile(intent, null, null);
264 fail("Point to Point compilation with insufficient bandwidth does "
265 + "not throw exception.");
266 } catch (PathNotFoundException noPath) {
267 assertThat(noPath.getMessage(), containsString("No path"));
272 * Tests that requests for available lambdas are successful.
275 public void testLambdaConstrainedIntentSuccess() {
277 final List<Constraint> constraints =
278 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
279 final LinkResourceService resourceService =
280 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
282 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
284 String[] hops = {"s1", "s2", "s3"};
285 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
287 final List<Intent> compiledIntents =
288 compiler.compile(intent, null, null);
290 assertThat(compiledIntents, Matchers.notNullValue());
291 assertThat(compiledIntents, hasSize(1));
295 * Tests that requests for lambdas when there are no available lambdas
299 public void testLambdaConstrainedIntentFailure() {
301 final List<Constraint> constraints =
302 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
303 final LinkResourceService resourceService =
304 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
306 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
308 String[] hops = {"s1", "s2", "s3"};
309 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
311 compiler.compile(intent, null, null);
313 fail("Point to Point compilation with no available lambda does "
314 + "not throw exception.");
315 } catch (PathNotFoundException noPath) {
316 assertThat(noPath.getMessage(), containsString("No path"));