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