blob: d970193bad27d1de2132c98cd2930a06ec019e23 [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 SHIMIZU0e738802015-02-20 10:23:28 -080039import java.util.LinkedList;
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 /**
80 * Creates a compiler for HostToHost intents.
81 *
82 * @param hops string array describing the path hops to use when compiling
83 * @return HostToHost intent compiler
84 */
85 private PointToPointIntentCompiler makeCompiler(String[] hops) {
86 PointToPointIntentCompiler compiler =
87 new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -080088 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070089 return compiler;
90 }
91
Sho SHIMIZU0e738802015-02-20 10:23:28 -080092 /**
93 * Creates a point to point intent compiler for a three switch linear
94 * topology.
95 *
96 * @param resourceService service to use for resource allocation requests
97 * @return point to point compiler
98 */
99 private PointToPointIntentCompiler makeCompiler(LinkResourceService resourceService) {
100 final String[] hops = {"s1", "s2", "s3"};
101 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
102 compiler.resourceService = resourceService;
103 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
104 return compiler;
105 }
106
107 /**
108 * Creates an intent with a given constraint and compiles it. The compiler
109 * will throw PathNotFoundException if the allocations cannot be satisfied.
110 *
111 * @param constraint constraint to apply to the created intent
112 * @param resourceService service to use for resource allocation requests
113 * @return List of compiled intents
114 */
115 private List<Intent> compileIntent(Constraint constraint,
116 LinkResourceService resourceService) {
117 final List<Constraint> constraints = new LinkedList<>();
118 constraints.add(constraint);
119 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
120 final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
121
122 final PointToPointIntent intent =
123 new PointToPointIntent(APP_ID,
124 selector,
125 treatment,
126 connectPoint("s1", 1),
127 connectPoint("s3", 1),
128 constraints);
129 final PointToPointIntentCompiler compiler = makeCompiler(resourceService);
130
131 return compiler.compile(intent, null, null);
132 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700133
134 /**
135 * Tests a pair of devices in an 8 hop path, forward direction.
136 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800137 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700138 public void testForwardPathCompilation() {
139
140 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700141
142 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
143 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700144
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700145 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700146 assertThat(result, is(Matchers.notNullValue()));
147 assertThat(result, hasSize(1));
148 Intent forwardResultIntent = result.get(0);
149 assertThat(forwardResultIntent instanceof PathIntent, is(true));
150
151 if (forwardResultIntent instanceof PathIntent) {
152 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
153 // 7 links for the hops, plus one default lnk on ingress and egress
154 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
155 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
156 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
157 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
158 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
159 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
160 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
161 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
162 }
163 }
164
165 /**
166 * Tests a pair of devices in an 8 hop path, forward direction.
167 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800168 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700169 public void testReversePathCompilation() {
170
171 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700172
173 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
174 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700175
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700176 List<Intent> result = compiler.compile(intent, null, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700177 assertThat(result, is(Matchers.notNullValue()));
178 assertThat(result, hasSize(1));
179 Intent reverseResultIntent = result.get(0);
180 assertThat(reverseResultIntent instanceof PathIntent, is(true));
181
182 if (reverseResultIntent instanceof PathIntent) {
183 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
184 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
185 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
186 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
187 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
188 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
189 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
190 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
191 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
192 }
193 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800194
195 /**
196 * Tests compilation of the intent which designates two different ports on the same switch.
197 */
198 @Test
199 public void testSameSwitchDifferentPortsIntentCompilation() {
200 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
201 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
202 PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
203
204 String[] hops = {"1"};
205 PointToPointIntentCompiler sut = makeCompiler(hops);
206
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700207 List<Intent> compiled = sut.compile(intent, null, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800208
209 assertThat(compiled, hasSize(1));
210 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
211 Path path = ((PathIntent) compiled.get(0)).path();
212
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800213 assertThat(path.links(), hasSize(2));
214 Link firstLink = path.links().get(0);
215 assertThat(firstLink, is(createEdgeLink(src, true)));
216 Link secondLink = path.links().get(1);
217 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800218 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800219
220 /**
221 * Tests that requests with sufficient available bandwidth succeed.
222 */
223 @Test
224 public void testBandwidthConstrainedIntentSuccess() {
225
226 final LinkResourceService resourceService =
227 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
228 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
229
230 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
231 assertThat(compiledIntents, Matchers.notNullValue());
232 assertThat(compiledIntents, hasSize(1));
233 }
234
235 /**
236 * Tests that requests with insufficient available bandwidth fail.
237 */
238 @Test
239 public void testBandwidthConstrainedIntentFailure() {
240
241 final LinkResourceService resourceService =
242 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
243 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
244
245 try {
246 compileIntent(constraint, resourceService);
247 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
260 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
261 final LinkResourceService resourceService =
262 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
263
264 final List<Intent> compiledIntents =
265 compileIntent(constraint, resourceService);
266 assertThat(compiledIntents, Matchers.notNullValue());
267 assertThat(compiledIntents, hasSize(1));
268 }
269
270 /**
271 * Tests that requests for lambdas when there are no available lambdas
272 * fail.
273 */
274 @Test
275 public void testLambdaConstrainedIntentFailure() {
276
277 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
278 final LinkResourceService resourceService =
279 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
280 try {
281 compileIntent(constraint, resourceService);
282 fail("Point to Point compilation with no available lambda does "
283 + "not throw exception.");
284 } catch (PathNotFoundException noPath) {
285 assertThat(noPath.getMessage(), containsString("No path"));
286 }
287 }
288
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700289}