Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [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 | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 17 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 18 | import org.hamcrest.Matchers; |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 19 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 20 | import org.onosproject.TestApplicationId; |
| 21 | import org.onosproject.core.ApplicationId; |
| 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.Link; |
| 24 | import org.onosproject.net.Path; |
| 25 | import org.onosproject.net.flow.TrafficSelector; |
| 26 | import org.onosproject.net.flow.TrafficTreatment; |
| 27 | import org.onosproject.net.intent.AbstractIntentTest; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 28 | import org.onosproject.net.intent.Constraint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.Intent; |
| 30 | import org.onosproject.net.intent.IntentTestsMocks; |
| 31 | import org.onosproject.net.intent.PathIntent; |
| 32 | import org.onosproject.net.intent.PointToPointIntent; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 33 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 34 | import org.onosproject.net.intent.constraint.LambdaConstraint; |
| 35 | import org.onosproject.net.resource.Bandwidth; |
| 36 | import org.onosproject.net.resource.Lambda; |
| 37 | import org.onosproject.net.resource.LinkResourceService; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 38 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 39 | import java.util.LinkedList; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 42 | import static org.hamcrest.CoreMatchers.instanceOf; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 43 | import static org.hamcrest.MatcherAssert.assertThat; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 44 | import static org.hamcrest.Matchers.containsString; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 45 | import static org.hamcrest.Matchers.hasSize; |
| 46 | import static org.hamcrest.Matchers.is; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 47 | import static org.junit.Assert.fail; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 48 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 49 | import static org.onosproject.net.DeviceId.deviceId; |
| 50 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 51 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 52 | import static org.onosproject.net.PortNumber.portNumber; |
| 53 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Unit tests for the HostToHost intent compiler. |
| 57 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 58 | public class PointToPointIntentCompilerTest extends AbstractIntentTest { |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 59 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 60 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 61 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 62 | 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 Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 69 | * @param egressIdString string for id of egress device |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 70 | * @return PointToPointIntent for the two devices |
| 71 | */ |
| 72 | private PointToPointIntent makeIntent(String ingressIdString, |
| 73 | String egressIdString) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 74 | return new PointToPointIntent(APPID, selector, treatment, |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 75 | 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 Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 88 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 89 | return compiler; |
| 90 | } |
| 91 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 92 | /** |
| 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 Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 136 | */ |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 137 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 138 | public void testForwardPathCompilation() { |
| 139 | |
| 140 | PointToPointIntent intent = makeIntent("d1", "d8"); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 141 | |
| 142 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 143 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 144 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 145 | List<Intent> result = compiler.compile(intent, null, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 146 | 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 Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 168 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 169 | public void testReversePathCompilation() { |
| 170 | |
| 171 | PointToPointIntent intent = makeIntent("d8", "d1"); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 172 | |
| 173 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 174 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 175 | |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 176 | List<Intent> result = compiler.compile(intent, null, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 177 | 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 SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 194 | |
| 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'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 207 | List<Intent> compiled = sut.compile(intent, null, null); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 208 | |
| 209 | assertThat(compiled, hasSize(1)); |
| 210 | assertThat(compiled.get(0), is(instanceOf(PathIntent.class))); |
| 211 | Path path = ((PathIntent) compiled.get(0)).path(); |
| 212 | |
Sho SHIMIZU | 3908fde | 2014-11-19 16:30:22 -0800 | [diff] [blame] | 213 | 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 SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 218 | } |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame^] | 219 | |
| 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 Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 289 | } |