Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.impl.compiler; |
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; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 20 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.TestApplicationId; |
| 22 | import org.onosproject.core.ApplicationId; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.Path; |
| 26 | import org.onosproject.net.flow.TrafficSelector; |
| 27 | import org.onosproject.net.flow.TrafficTreatment; |
| 28 | import org.onosproject.net.intent.AbstractIntentTest; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 29 | import org.onosproject.net.intent.Constraint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentTestsMocks; |
| 32 | import org.onosproject.net.intent.PathIntent; |
| 33 | import org.onosproject.net.intent.PointToPointIntent; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.impl.PathNotFoundException; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 36 | import org.onosproject.net.resource.ResourceService; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 37 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 38 | import java.util.Collections; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 39 | import java.util.List; |
| 40 | |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 41 | import static org.hamcrest.CoreMatchers.instanceOf; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 42 | import static org.hamcrest.MatcherAssert.assertThat; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 43 | import static org.hamcrest.Matchers.containsString; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 44 | import static org.hamcrest.Matchers.hasSize; |
| 45 | import static org.hamcrest.Matchers.is; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 46 | import static org.junit.Assert.fail; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 47 | import static org.onosproject.net.DefaultEdgeLink.createEdgeLink; |
| 48 | import static org.onosproject.net.DeviceId.deviceId; |
| 49 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 50 | import static org.onosproject.net.NetTestTools.connectPoint; |
| 51 | import static org.onosproject.net.PortNumber.portNumber; |
| 52 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Unit tests for the HostToHost intent compiler. |
| 56 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 57 | public class PointToPointIntentCompilerTest extends AbstractIntentTest { |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 58 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 59 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 60 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 61 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 62 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 63 | |
| 64 | /** |
| 65 | * Creates a PointToPoint intent based on ingress and egress device Ids. |
| 66 | * |
| 67 | * @param ingressIdString string for id of ingress device |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 68 | * @param egressIdString string for id of egress device |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 69 | * @return PointToPointIntent for the two devices |
| 70 | */ |
| 71 | private PointToPointIntent makeIntent(String ingressIdString, |
| 72 | String egressIdString) { |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 73 | return PointToPointIntent.builder() |
| 74 | .appId(APPID) |
| 75 | .selector(selector) |
| 76 | .treatment(treatment) |
| 77 | .ingressPoint(connectPoint(ingressIdString, 1)) |
| 78 | .egressPoint(connectPoint(egressIdString, 1)) |
| 79 | .build(); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 83 | * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints. |
| 84 | * |
| 85 | * @param ingressIdString string for id of ingress device |
| 86 | * @param egressIdString string for id of egress device |
| 87 | * @param constraints constraints |
| 88 | * @return PointToPointIntent for the two device with constraints |
| 89 | */ |
| 90 | private PointToPointIntent makeIntent(String ingressIdString, |
| 91 | String egressIdString, List<Constraint> constraints) { |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 92 | return PointToPointIntent.builder() |
| 93 | .appId(APPID) |
| 94 | .selector(selector) |
| 95 | .treatment(treatment) |
| 96 | .ingressPoint(connectPoint(ingressIdString, 1)) |
| 97 | .egressPoint(connectPoint(egressIdString, 1)) |
| 98 | .constraints(constraints) |
| 99 | .build(); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 103 | * Creates a compiler for HostToHost intents. |
| 104 | * |
| 105 | * @param hops string array describing the path hops to use when compiling |
| 106 | * @return HostToHost intent compiler |
| 107 | */ |
| 108 | private PointToPointIntentCompiler makeCompiler(String[] hops) { |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 109 | PointToPointIntentCompiler compiler = new PointToPointIntentCompiler(); |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 110 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 111 | return compiler; |
| 112 | } |
| 113 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 114 | /** |
| 115 | * Creates a point to point intent compiler for a three switch linear |
| 116 | * topology. |
| 117 | * |
| 118 | * @param resourceService service to use for resource allocation requests |
| 119 | * @return point to point compiler |
| 120 | */ |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 121 | private PointToPointIntentCompiler makeCompiler(String[] hops, ResourceService resourceService) { |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 122 | final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler(); |
| 123 | compiler.resourceService = resourceService; |
| 124 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
| 125 | return compiler; |
| 126 | } |
| 127 | |
| 128 | /** |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 129 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 130 | */ |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 131 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 132 | public void testForwardPathCompilation() { |
| 133 | |
| 134 | PointToPointIntent intent = makeIntent("d1", "d8"); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 135 | |
| 136 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 137 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 138 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 139 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 140 | assertThat(result, is(Matchers.notNullValue())); |
| 141 | assertThat(result, hasSize(1)); |
| 142 | Intent forwardResultIntent = result.get(0); |
| 143 | assertThat(forwardResultIntent instanceof PathIntent, is(true)); |
| 144 | |
| 145 | if (forwardResultIntent instanceof PathIntent) { |
| 146 | PathIntent forwardPathIntent = (PathIntent) forwardResultIntent; |
| 147 | // 7 links for the hops, plus one default lnk on ingress and egress |
| 148 | assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1)); |
| 149 | assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2")); |
| 150 | assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3")); |
| 151 | assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4")); |
| 152 | assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5")); |
| 153 | assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6")); |
| 154 | assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7")); |
| 155 | assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8")); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 161 | */ |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 162 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 163 | public void testReversePathCompilation() { |
| 164 | |
| 165 | PointToPointIntent intent = makeIntent("d8", "d1"); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 166 | |
| 167 | String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"}; |
| 168 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 169 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 170 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 171 | assertThat(result, is(Matchers.notNullValue())); |
| 172 | assertThat(result, hasSize(1)); |
| 173 | Intent reverseResultIntent = result.get(0); |
| 174 | assertThat(reverseResultIntent instanceof PathIntent, is(true)); |
| 175 | |
| 176 | if (reverseResultIntent instanceof PathIntent) { |
| 177 | PathIntent reversePathIntent = (PathIntent) reverseResultIntent; |
| 178 | assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1)); |
| 179 | assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1")); |
| 180 | assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2")); |
| 181 | assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3")); |
| 182 | assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4")); |
| 183 | assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5")); |
| 184 | assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6")); |
| 185 | assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7")); |
| 186 | } |
| 187 | } |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Tests compilation of the intent which designates two different ports on the same switch. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testSameSwitchDifferentPortsIntentCompilation() { |
| 194 | ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1)); |
| 195 | ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2)); |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 196 | PointToPointIntent intent = PointToPointIntent.builder() |
| 197 | .appId(APP_ID) |
| 198 | .selector(selector) |
| 199 | .treatment(treatment) |
| 200 | .ingressPoint(src) |
| 201 | .egressPoint(dst) |
| 202 | .build(); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 203 | |
| 204 | String[] hops = {"1"}; |
| 205 | PointToPointIntentCompiler sut = makeCompiler(hops); |
| 206 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 207 | List<Intent> compiled = sut.compile(intent, 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 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 226 | final ResourceService resourceService = |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 227 | IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 228 | final List<Constraint> constraints = |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 229 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0))); |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 230 | |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 231 | final PointToPointIntent intent = makeIntent("s1", "s3", constraints); |
| 232 | |
| 233 | String[] hops = {"s1", "s2", "s3"}; |
| 234 | final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 235 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 236 | final List<Intent> compiledIntents = compiler.compile(intent, null); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 237 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 238 | assertThat(compiledIntents, Matchers.notNullValue()); |
| 239 | assertThat(compiledIntents, hasSize(1)); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Tests that requests with insufficient available bandwidth fail. |
| 244 | */ |
| 245 | @Test |
| 246 | public void testBandwidthConstrainedIntentFailure() { |
| 247 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 248 | final ResourceService resourceService = |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 249 | IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 250 | final List<Constraint> constraints = |
Sho SHIMIZU | a88db49 | 2015-11-23 13:21:04 -0800 | [diff] [blame] | 251 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0))); |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 252 | |
| 253 | try { |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 254 | final PointToPointIntent intent = makeIntent("s1", "s3", constraints); |
| 255 | |
| 256 | String[] hops = {"s1", "s2", "s3"}; |
| 257 | final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 258 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 259 | compiler.compile(intent, null); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 260 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 261 | fail("Point to Point compilation with insufficient bandwidth does " |
| 262 | + "not throw exception."); |
| 263 | } catch (PathNotFoundException noPath) { |
| 264 | assertThat(noPath.getMessage(), containsString("No path")); |
| 265 | } |
| 266 | } |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 267 | } |