blob: a9705d789a4c3a21acd6941d56ddadba807a703a [file] [log] [blame]
Ray Milkey8d3ce432014-11-07 16:21:10 -08001/*
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 */
16package org.onlab.onos.net.intent.impl;
17
18import java.util.LinkedList;
19import java.util.List;
20
21import org.junit.Test;
22import org.onlab.onos.net.flow.FlowRuleBatchOperation;
23import org.onlab.onos.net.flow.TrafficSelector;
24import org.onlab.onos.net.flow.TrafficTreatment;
Brian O'Connor520c0522014-11-23 23:50:47 -080025import org.onlab.onos.net.intent.AbstractIntentTest;
Ray Milkey8d3ce432014-11-07 16:21:10 -080026import org.onlab.onos.net.intent.Constraint;
27import org.onlab.onos.net.intent.Intent;
28import org.onlab.onos.net.intent.IntentTestsMocks;
29import org.onlab.onos.net.intent.PathIntent;
30import org.onlab.onos.net.intent.PointToPointIntent;
31import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
32import org.onlab.onos.net.intent.constraint.LambdaConstraint;
33import org.onlab.onos.net.resource.Bandwidth;
34import org.onlab.onos.net.resource.Lambda;
35import org.onlab.onos.net.resource.LinkResourceService;
36
37import static org.hamcrest.MatcherAssert.assertThat;
38import static org.hamcrest.Matchers.containsString;
39import static org.hamcrest.Matchers.hasSize;
40import static org.hamcrest.Matchers.instanceOf;
41import static org.hamcrest.Matchers.notNullValue;
42import static org.junit.Assert.fail;
43import static org.onlab.onos.net.NetTestTools.APP_ID;
44import static org.onlab.onos.net.NetTestTools.connectPoint;
45
46/**
47 * Unit tests for calculating paths for intents with constraints.
48 */
49
Brian O'Connor520c0522014-11-23 23:50:47 -080050public class PathConstraintCalculationTest extends AbstractIntentTest {
Ray Milkey8d3ce432014-11-07 16:21:10 -080051
52 /**
53 * Creates a point to point intent compiler for a three switch linear
54 * topology.
55 *
56 * @param resourceService service to use for resource allocation requests
57 * @return point to point compiler
58 */
59 private PointToPointIntentCompiler makeCompiler(LinkResourceService resourceService) {
60 final String[] hops = {"s1", "s2", "s3"};
61 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
62 compiler.resourceService = resourceService;
63 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
64 return compiler;
65 }
66
67 /**
68 * Creates an intent with a given constraint and compiles it. The compiler
69 * will throw PathNotFoundException if the allocations cannot be satisfied.
70 *
71 * @param constraint constraint to apply to the created intent
72 * @param resourceService service to use for resource allocation requests
73 * @return List of compiled intents
74 */
75 private List<Intent> compileIntent(Constraint constraint,
76 LinkResourceService resourceService) {
77 final List<Constraint> constraints = new LinkedList<>();
78 constraints.add(constraint);
79 final TrafficSelector selector = new IntentTestsMocks.MockSelector();
80 final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
81
82 final PointToPointIntent intent =
83 new PointToPointIntent(APP_ID,
84 selector,
85 treatment,
86 connectPoint("s1", 1),
87 connectPoint("s3", 1),
88 constraints);
89 final PointToPointIntentCompiler compiler = makeCompiler(resourceService);
90
Brian O'Connorfa81eae2014-10-30 13:20:05 -070091 return compiler.compile(intent, null, null);
Ray Milkey8d3ce432014-11-07 16:21:10 -080092 }
93
94 /**
95 * Installs a compiled path intent and returns the flow rules it generates.
96 *
97 * @param compiledIntents list of compiled intents
98 * @param resourceService service to use for resource allocation requests
99 * @return
100 */
101 private List<FlowRuleBatchOperation> installIntents(List<Intent> compiledIntents,
102 LinkResourceService resourceService) {
103 final PathIntent path = (PathIntent) compiledIntents.get(0);
104
105 final PathIntentInstaller installer = new PathIntentInstaller();
106 installer.resourceService = resourceService;
107 installer.appId = APP_ID;
108 return installer.install(path);
109 }
110
111 /**
112 * Tests that requests with sufficient available bandwidth succeed.
113 */
114 @Test
115 public void testBandwidthConstrainedIntentSuccess() {
116
117 final LinkResourceService resourceService =
118 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
119 final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
120
121 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
122 assertThat(compiledIntents, notNullValue());
123 assertThat(compiledIntents, hasSize(1));
124 }
125
126 /**
127 * Tests that requests with insufficient available bandwidth fail.
128 */
129 @Test
130 public void testBandwidthConstrainedIntentFailure() {
131
132 final LinkResourceService resourceService =
133 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
134 final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
135
136 try {
137 compileIntent(constraint, resourceService);
138 fail("Point to Point compilation with insufficient bandwidth does "
139 + "not throw exception.");
140 } catch (PathNotFoundException noPath) {
141 assertThat(noPath.getMessage(), containsString("No packet path"));
142 }
143 }
144
145 /**
146 * Tests that requests for available lambdas are successful.
147 */
148 @Test
149 public void testLambdaConstrainedIntentSuccess() {
150
151 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
152 final LinkResourceService resourceService =
153 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
154
155 final List<Intent> compiledIntents =
156 compileIntent(constraint, resourceService);
157 assertThat(compiledIntents, notNullValue());
158 assertThat(compiledIntents, hasSize(1));
159 }
160
161 /**
162 * Tests that requests for lambdas when there are no available lambdas
163 * fail.
164 */
165 @Test
166 public void testLambdaConstrainedIntentFailure() {
167
168 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
169 final LinkResourceService resourceService =
170 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
171 try {
172 compileIntent(constraint, resourceService);
173 fail("Point to Point compilation with no available lambda does "
174 + "not throw exception.");
175 } catch (PathNotFoundException noPath) {
176 assertThat(noPath.getMessage(), containsString("No packet path"));
177 }
178 }
179
180 /**
181 * Tests that installation of bandwidth constrained path intents are
182 * successful.
183 */
184 @Test
185 public void testInstallBandwidthConstrainedIntentSuccess() {
186
187 final IntentTestsMocks.MockResourceService resourceService =
188 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
189 final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
190
191 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
192 assertThat(compiledIntents, notNullValue());
193 assertThat(compiledIntents, hasSize(1));
194
195 final List<FlowRuleBatchOperation> flowOperations =
196 installIntents(compiledIntents, resourceService);
197
198 assertThat(flowOperations, notNullValue());
199 assertThat(flowOperations, hasSize(1));
200 }
201
202 /**
203 * Tests that installation of bandwidth constrained path intents fail
204 * if there are no available resources.
205 */
206 @Test
207 public void testInstallBandwidthConstrainedIntentFailure() {
208
209 final IntentTestsMocks.MockResourceService resourceService =
210 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
211 final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
212
213 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
214 assertThat(compiledIntents, notNullValue());
215 assertThat(compiledIntents, hasSize(1));
216
217 // Make it look like the available bandwidth was consumed
218 resourceService.setAvailableBandwidth(1.0);
219
220 try {
221 installIntents(compiledIntents, resourceService);
222 fail("Bandwidth request with no available bandwidth did not fail.");
223 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
224 assertThat(failure,
225 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
226 }
227 }
228
229 /**
230 * Tests that installation of lambda constrained path intents are
231 * successful.
232 */
233 @Test
234 public void testInstallLambdaConstrainedIntentSuccess() {
235
236 final IntentTestsMocks.MockResourceService resourceService =
237 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
238 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
239
240 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
241 assertThat(compiledIntents, notNullValue());
242 assertThat(compiledIntents, hasSize(1));
243
244 final List<FlowRuleBatchOperation> flowOperations =
245 installIntents(compiledIntents, resourceService);
246
247 assertThat(flowOperations, notNullValue());
248 assertThat(flowOperations, hasSize(1));
249 }
250
251 /**
252 * Tests that installation of lambda constrained path intents fail
253 * if there are no available resources.
254 */
255 @Test
256 public void testInstallLambdaConstrainedIntentFailure() {
257
258 final IntentTestsMocks.MockResourceService resourceService =
259 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
260 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
261
262 final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
263 assertThat(compiledIntents, notNullValue());
264 assertThat(compiledIntents, hasSize(1));
265
266 // Make it look like the available lambda was consumed
267 resourceService.setAvailableLambda(0);
268
269 try {
270 installIntents(compiledIntents, resourceService);
271 fail("Lambda request with no available lambda did not fail.");
272 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
273 assertThat(failure,
274 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
275 }
276 }
277
278}