Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 1 | /* |
| 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'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 17 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 18 | import org.junit.Test; |
Ray Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 19 | import org.onosproject.net.flow.FlowRuleOperation; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.net.flow.TrafficSelector; |
| 21 | import org.onosproject.net.flow.TrafficTreatment; |
| 22 | import org.onosproject.net.intent.AbstractIntentTest; |
| 23 | import org.onosproject.net.intent.Constraint; |
| 24 | import org.onosproject.net.intent.Intent; |
| 25 | import org.onosproject.net.intent.IntentTestsMocks; |
| 26 | import org.onosproject.net.intent.PathIntent; |
| 27 | import org.onosproject.net.intent.PointToPointIntent; |
| 28 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 29 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 30 | import org.onosproject.net.resource.Bandwidth; |
| 31 | import org.onosproject.net.resource.Lambda; |
| 32 | import org.onosproject.net.resource.LinkResourceService; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 33 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 34 | import java.util.Collection; |
| 35 | import java.util.LinkedList; |
| 36 | import java.util.List; |
| 37 | |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 38 | import static org.hamcrest.MatcherAssert.assertThat; |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 39 | import static org.hamcrest.Matchers.*; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 40 | import static org.junit.Assert.fail; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 42 | import static org.onosproject.net.NetTestTools.connectPoint; |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 43 | |
| 44 | /** |
| 45 | * Unit tests for calculating paths for intents with constraints. |
| 46 | */ |
| 47 | |
Brian O'Connor | 520c052 | 2014-11-23 23:50:47 -0800 | [diff] [blame] | 48 | public class PathConstraintCalculationTest extends AbstractIntentTest { |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 49 | |
| 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 89 | return compiler.compile(intent, null, null); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 90 | } |
| 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 Milkey | 71ade56 | 2015-02-18 15:08:07 -0800 | [diff] [blame] | 97 | * @return fow rule entries |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 98 | */ |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 99 | private List<Collection<FlowRuleOperation>> installIntents(List<Intent> compiledIntents, |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 100 | 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 117 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 118 | |
| 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 132 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 133 | |
| 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 SHIMIZU | 877ec2c | 2015-02-09 12:50:36 -0800 | [diff] [blame] | 139 | assertThat(noPath.getMessage(), containsString("No path")); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 140 | } |
| 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 SHIMIZU | 877ec2c | 2015-02-09 12:50:36 -0800 | [diff] [blame] | 174 | assertThat(noPath.getMessage(), containsString("No path")); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 175 | } |
| 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 187 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 188 | |
| 189 | final List<Intent> compiledIntents = compileIntent(constraint, resourceService); |
| 190 | assertThat(compiledIntents, notNullValue()); |
| 191 | assertThat(compiledIntents, hasSize(1)); |
| 192 | |
Brian O'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 193 | final List<Collection<FlowRuleOperation>> flowOperations = |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 194 | 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 SHIMIZU | 0ce220a | 2015-01-23 15:54:47 -0800 | [diff] [blame] | 209 | final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0)); |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 210 | |
| 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'Connor | 64a0369d | 2015-02-20 22:02:59 -0800 | [diff] [blame^] | 242 | final List<Collection<FlowRuleOperation>> flowOperations = |
Ray Milkey | 8d3ce43 | 2014-11-07 16:21:10 -0800 | [diff] [blame] | 243 | 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 | } |