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