Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 19 | import org.hamcrest.Matchers; |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 20 | import org.junit.Test; |
Sho SHIMIZU | 6d01d3d | 2015-05-08 14:08:36 -0700 | [diff] [blame] | 21 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.TestApplicationId; |
| 23 | import org.onosproject.core.ApplicationId; |
| 24 | import org.onosproject.net.ConnectPoint; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 26 | import org.onosproject.net.FilteredConnectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.Link; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.ResourceGroup; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.TrafficSelector; |
| 31 | import org.onosproject.net.flow.TrafficTreatment; |
| 32 | import org.onosproject.net.intent.AbstractIntentTest; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 33 | import org.onosproject.net.intent.Constraint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.intent.Intent; |
| 35 | import org.onosproject.net.intent.IntentTestsMocks; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.Key; |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 37 | import org.onosproject.net.intent.LinkCollectionIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.PointToPointIntent; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.impl.PathNotFoundException; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 41 | import org.onosproject.net.resource.ContinuousResource; |
| 42 | import org.onosproject.net.resource.MockResourceService; |
| 43 | import org.onosproject.net.resource.ResourceAllocation; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.ResourceService; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 45 | import org.onosproject.net.resource.Resources; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 46 | |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 47 | import java.util.Collections; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 48 | import java.util.List; |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 49 | import java.util.Set; |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 50 | import java.util.stream.Collectors; |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 51 | |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 52 | import static org.hamcrest.CoreMatchers.instanceOf; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 53 | import static org.hamcrest.MatcherAssert.assertThat; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 54 | import static org.hamcrest.Matchers.*; |
| 55 | import static org.junit.Assert.assertEquals; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 56 | import static org.junit.Assert.fail; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * Unit tests for the HostToHost intent compiler. |
| 61 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 62 | public class PointToPointIntentCompilerTest extends AbstractIntentTest { |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 63 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 64 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 65 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 66 | private static final String S1 = "s1"; |
| 67 | private static final String S2 = "s2"; |
| 68 | private static final String S3 = "s3"; |
| 69 | private static final String S4 = "s4"; |
| 70 | private static final String S5 = "s5"; |
| 71 | private static final String S6 = "s6"; |
| 72 | private static final String S7 = "s7"; |
| 73 | private static final String S8 = "s8"; |
| 74 | |
| 75 | private static final DeviceId DID_1 = DeviceId.deviceId("of:" + S1); |
| 76 | private static final DeviceId DID_2 = DeviceId.deviceId("of:" + S2); |
| 77 | private static final DeviceId DID_3 = DeviceId.deviceId("of:" + S3); |
| 78 | private static final DeviceId DID_8 = DeviceId.deviceId("of:" + S8); |
| 79 | |
| 80 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 81 | private static final PortNumber PORT_2 = PortNumber.portNumber(2); |
| 82 | |
| 83 | private static final double BPS_TO_RESERVE = 100.0; |
| 84 | |
| 85 | private static final ContinuousResource RESOURCE_SW1_P1 = |
| 86 | Resources.continuous(DID_1, PORT_1, Bandwidth.class) |
| 87 | .resource(BPS_TO_RESERVE); |
| 88 | private static final ContinuousResource RESOURCE_SW1_P2 = |
| 89 | Resources.continuous(DID_1, PORT_2, Bandwidth.class) |
| 90 | .resource(BPS_TO_RESERVE); |
| 91 | private static final ContinuousResource RESOURCE_SW2_P1 = |
| 92 | Resources.continuous(DID_2, PORT_1, Bandwidth.class) |
| 93 | .resource(BPS_TO_RESERVE); |
| 94 | private static final ContinuousResource RESOURCE_SW2_P2 = |
| 95 | Resources.continuous(DID_2, PORT_2, Bandwidth.class) |
| 96 | .resource(BPS_TO_RESERVE); |
| 97 | private static final ContinuousResource RESOURCE_SW3_P1 = |
| 98 | Resources.continuous(DID_3, PORT_1, Bandwidth.class) |
| 99 | .resource(BPS_TO_RESERVE); |
| 100 | private static final ContinuousResource RESOURCE_SW3_P2 = |
| 101 | Resources.continuous(DID_3, PORT_2, Bandwidth.class) |
| 102 | .resource(BPS_TO_RESERVE); |
| 103 | |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 104 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 105 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 106 | |
| 107 | /** |
| 108 | * Creates a PointToPoint intent based on ingress and egress device Ids. |
| 109 | * |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 110 | * @param ingress the ingress connect point |
| 111 | * @param egress the egress connect point |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 112 | * @return PointToPointIntent for the two devices |
| 113 | */ |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 114 | private PointToPointIntent makeIntent(ConnectPoint ingress, ConnectPoint egress) { |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 115 | return PointToPointIntent.builder() |
| 116 | .appId(APPID) |
| 117 | .selector(selector) |
| 118 | .treatment(treatment) |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 119 | .filteredIngressPoint(new FilteredConnectPoint(ingress)) |
| 120 | .filteredEgressPoint(new FilteredConnectPoint(egress)) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 121 | .build(); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 125 | * Creates a PointToPoint intent based on ingress and egress deviceIds and |
| 126 | * constraints. |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 127 | * |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 128 | * @param ingress the ingress connect point |
| 129 | * @param egress the egress connect point |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 130 | * @param constraints constraints |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 131 | * @return the PointToPointIntent connecting the two connect points with |
| 132 | * constraints |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 133 | */ |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 134 | private PointToPointIntent makeIntent(ConnectPoint ingress, |
| 135 | ConnectPoint egress, |
| 136 | List<Constraint> constraints) { |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 137 | return PointToPointIntent.builder() |
| 138 | .appId(APPID) |
| 139 | .selector(selector) |
| 140 | .treatment(treatment) |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 141 | .filteredIngressPoint(new FilteredConnectPoint(ingress)) |
| 142 | .filteredEgressPoint(new FilteredConnectPoint(egress)) |
| 143 | .constraints(constraints) |
| 144 | .build(); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Creates a PointToPoint intent based on ingress and egress deviceIds, |
| 149 | * constraints and a resource group. |
| 150 | * |
| 151 | * @param ingress the ingress connect point |
| 152 | * @param egress the egress connect point |
| 153 | * @param constraints constraints |
| 154 | * @param resourceGroup the resource group |
| 155 | * @return the PointToPointIntent connecting the two connect points with |
| 156 | * constraints |
| 157 | */ |
| 158 | private PointToPointIntent makeIntent(ConnectPoint ingress, |
| 159 | ConnectPoint egress, |
| 160 | List<Constraint> constraints, |
| 161 | ResourceGroup resourceGroup) { |
| 162 | return PointToPointIntent.builder() |
| 163 | .appId(APPID) |
| 164 | .resourceGroup(resourceGroup) |
| 165 | .selector(selector) |
| 166 | .treatment(treatment) |
| 167 | .filteredIngressPoint(new FilteredConnectPoint(ingress)) |
| 168 | .filteredEgressPoint(new FilteredConnectPoint(egress)) |
| 169 | .constraints(constraints) |
| 170 | .build(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Creates a PointToPoint intent based on an intent key, ingress and egress |
| 175 | * deviceIds, constraints and a resource group. |
| 176 | * |
| 177 | * @param key the intent key |
| 178 | * @param ingress the ingress connect point |
| 179 | * @param egress the egress connect point |
| 180 | * @param constraints constraints |
| 181 | * @param resourceGroup the resource group |
| 182 | * @return the PointToPointIntent connecting the two connect points with |
| 183 | * constraints |
| 184 | */ |
| 185 | private PointToPointIntent makeIntent(Key key, |
| 186 | ConnectPoint ingress, |
| 187 | ConnectPoint egress, |
| 188 | List<Constraint> constraints, |
| 189 | ResourceGroup resourceGroup) { |
| 190 | return PointToPointIntent.builder() |
| 191 | .appId(APPID) |
| 192 | .key(key) |
| 193 | .resourceGroup(resourceGroup) |
| 194 | .selector(selector) |
| 195 | .treatment(treatment) |
| 196 | .filteredIngressPoint(new FilteredConnectPoint(ingress)) |
| 197 | .filteredEgressPoint(new FilteredConnectPoint(egress)) |
Ray Milkey | 3e3ec5f | 2015-03-17 17:00:38 -0700 | [diff] [blame] | 198 | .constraints(constraints) |
| 199 | .build(); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | /** |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 203 | * Creates a compiler for HostToHost intents. |
| 204 | * |
| 205 | * @param hops string array describing the path hops to use when compiling |
| 206 | * @return HostToHost intent compiler |
| 207 | */ |
| 208 | private PointToPointIntentCompiler makeCompiler(String[] hops) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 209 | return makeCompiler(hops, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 212 | /** |
| 213 | * Creates a point to point intent compiler for a three switch linear |
| 214 | * topology. |
| 215 | * |
| 216 | * @param resourceService service to use for resource allocation requests |
| 217 | * @return point to point compiler |
| 218 | */ |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 219 | private PointToPointIntentCompiler makeCompiler(String[] hops, |
| 220 | ResourceService resourceService) { |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 221 | final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler(); |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 222 | compiler.pathService = new IntentTestsMocks.MockPathService(hops); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 223 | |
| 224 | if (resourceService == null) { |
| 225 | compiler.resourceService = new MockResourceService(); |
| 226 | } else { |
| 227 | compiler.resourceService = resourceService; |
| 228 | } |
| 229 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 230 | return compiler; |
| 231 | } |
| 232 | |
| 233 | /** |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 234 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 235 | */ |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 236 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 237 | public void testForwardPathCompilation() { |
| 238 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 239 | PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 240 | new ConnectPoint(DID_8, PORT_1)); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 241 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 242 | String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8}; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 243 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 244 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 245 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 246 | assertThat(result, is(Matchers.notNullValue())); |
| 247 | assertThat(result, hasSize(1)); |
| 248 | Intent forwardResultIntent = result.get(0); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 249 | assertThat(forwardResultIntent instanceof LinkCollectionIntent, is(true)); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 250 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 251 | if (forwardResultIntent instanceof LinkCollectionIntent) { |
| 252 | LinkCollectionIntent forwardIntent = (LinkCollectionIntent) forwardResultIntent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 253 | FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)); |
| 254 | FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1)); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 255 | // 7 links for the hops, plus one default lnk on ingress and egress |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 256 | assertThat(forwardIntent.links(), hasSize(hops.length - 1)); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 257 | assertThat(forwardIntent.links(), linksHasPath(S1, S2)); |
| 258 | assertThat(forwardIntent.links(), linksHasPath(S2, S3)); |
| 259 | assertThat(forwardIntent.links(), linksHasPath(S3, S4)); |
| 260 | assertThat(forwardIntent.links(), linksHasPath(S4, S5)); |
| 261 | assertThat(forwardIntent.links(), linksHasPath(S5, S6)); |
| 262 | assertThat(forwardIntent.links(), linksHasPath(S6, S7)); |
| 263 | assertThat(forwardIntent.links(), linksHasPath(S7, S8)); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 264 | assertThat(forwardIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint))); |
| 265 | assertThat(forwardIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint))); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 266 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 267 | assertThat("key is inherited", forwardResultIntent.key(), is(intent.key())); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Tests a pair of devices in an 8 hop path, forward direction. |
| 272 | */ |
Ray Milkey | 40f50b9 | 2014-11-07 13:25:53 -0800 | [diff] [blame] | 273 | @Test |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 274 | public void testReversePathCompilation() { |
| 275 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 276 | PointToPointIntent intent = makeIntent(new ConnectPoint(DID_8, PORT_1), |
| 277 | new ConnectPoint(DID_1, PORT_1)); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 278 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 279 | String[] hops = {S1, S2, S3, S4, S5, S6, S7, S8}; |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 280 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 281 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 282 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 283 | assertThat(result, is(Matchers.notNullValue())); |
| 284 | assertThat(result, hasSize(1)); |
| 285 | Intent reverseResultIntent = result.get(0); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 286 | assertThat(reverseResultIntent instanceof LinkCollectionIntent, is(true)); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 287 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 288 | if (reverseResultIntent instanceof LinkCollectionIntent) { |
| 289 | LinkCollectionIntent reverseLinkCollectionIntent = (LinkCollectionIntent) reverseResultIntent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 290 | FilteredConnectPoint egressPoint = new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)); |
| 291 | FilteredConnectPoint ingressPoint = new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1)); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 292 | assertThat(reverseLinkCollectionIntent.links(), hasSize(hops.length - 1)); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 293 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S2, S1)); |
| 294 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S3, S2)); |
| 295 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S4, S3)); |
| 296 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S5, S4)); |
| 297 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S6, S5)); |
| 298 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S7, S6)); |
| 299 | assertThat(reverseLinkCollectionIntent.links(), linksHasPath(S8, S7)); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 300 | assertThat(reverseLinkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint))); |
| 301 | assertThat(reverseLinkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint))); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 302 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 303 | assertThat("key is inherited", reverseResultIntent.key(), is(intent.key())); |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 304 | } |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 305 | |
| 306 | /** |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 307 | * Tests the compilation of an intent which designates two different ports |
| 308 | * on the same switch. |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 309 | */ |
| 310 | @Test |
| 311 | public void testSameSwitchDifferentPortsIntentCompilation() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 312 | FilteredConnectPoint src = |
| 313 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)); |
| 314 | FilteredConnectPoint dst = |
| 315 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2)); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 316 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 317 | PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 318 | new ConnectPoint(DID_1, PORT_2)); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 319 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 320 | String[] hops = {S1}; |
| 321 | PointToPointIntentCompiler compiler = makeCompiler(hops); |
| 322 | |
| 323 | List<Intent> compiled = compiler.compile(intent, null); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 324 | |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 325 | assertThat("key is inherited", |
| 326 | compiled.stream().map(Intent::key).collect(Collectors.toList()), |
| 327 | everyItem(is(intent.key()))); |
| 328 | |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 329 | assertThat(compiled, hasSize(1)); |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 330 | assertThat(compiled.get(0), is(instanceOf(LinkCollectionIntent.class))); |
| 331 | LinkCollectionIntent linkCollectionIntent = (LinkCollectionIntent) compiled.get(0); |
| 332 | Set<Link> links = linkCollectionIntent.links(); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 333 | |
Pier Ventre | ffe88d6 | 2016-10-13 14:34:40 -0700 | [diff] [blame] | 334 | assertThat(links, hasSize(0)); |
| 335 | assertThat(linkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(src))); |
| 336 | assertThat(linkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(dst))); |
Sho SHIMIZU | de8e6b5 | 2014-11-13 11:23:17 -0800 | [diff] [blame] | 337 | } |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 338 | |
| 339 | /** |
| 340 | * Tests that requests with sufficient available bandwidth succeed. |
| 341 | */ |
| 342 | @Test |
| 343 | public void testBandwidthConstrainedIntentSuccess() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 344 | final double bpsTotal = 1000.0; |
| 345 | final double bpsToReserve = 100.0; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 346 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 347 | final ResourceService resourceService = |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 348 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 349 | final List<Constraint> constraints = |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 350 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve))); |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 351 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 352 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 353 | new ConnectPoint(DID_3, PORT_2), |
| 354 | constraints); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 355 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 356 | String[] hops = {S1, S2, S3}; |
| 357 | final PointToPointIntentCompiler compiler = makeCompiler(hops, |
| 358 | resourceService); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 359 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 360 | final List<Intent> compiledIntents = compiler.compile(intent, null); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 361 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 362 | assertThat(compiledIntents, Matchers.notNullValue()); |
| 363 | assertThat(compiledIntents, hasSize(1)); |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 364 | |
| 365 | assertThat("key is inherited", |
| 366 | compiledIntents.stream().map(Intent::key).collect(Collectors.toList()), |
| 367 | everyItem(is(intent.key()))); |
| 368 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Tests that requests with insufficient available bandwidth fail. |
| 373 | */ |
| 374 | @Test |
| 375 | public void testBandwidthConstrainedIntentFailure() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 376 | final double bpsTotal = 10.0; |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 377 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 378 | final ResourceService resourceService = |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 379 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 380 | final List<Constraint> constraints = |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 381 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE))); |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 382 | |
| 383 | try { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 384 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 385 | new ConnectPoint(DID_3, PORT_2), |
| 386 | constraints); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 387 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 388 | String[] hops = {S1, S2, S3}; |
| 389 | final PointToPointIntentCompiler compiler = makeCompiler(hops, |
| 390 | resourceService); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 391 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 392 | compiler.compile(intent, null); |
Sho SHIMIZU | b705217 | 2015-02-20 11:09:21 -0800 | [diff] [blame] | 393 | |
Sho SHIMIZU | 0e73880 | 2015-02-20 10:23:28 -0800 | [diff] [blame] | 394 | fail("Point to Point compilation with insufficient bandwidth does " |
| 395 | + "not throw exception."); |
| 396 | } catch (PathNotFoundException noPath) { |
| 397 | assertThat(noPath.getMessage(), containsString("No path")); |
| 398 | } |
| 399 | } |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 400 | |
| 401 | /** |
| 402 | * Tests if bandwidth resources get allocated correctly. An intent with a |
| 403 | * key only is submitted. |
| 404 | */ |
| 405 | @Test |
| 406 | public void testBandwidthConstrainedIntentAllocation() { |
| 407 | final double bpsTotal = 1000.0; |
| 408 | |
| 409 | String[] hops = {S1, S2, S3}; |
| 410 | |
| 411 | final ResourceService resourceService = |
| 412 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
| 413 | final List<Constraint> constraints = |
| 414 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE))); |
| 415 | |
| 416 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 417 | new ConnectPoint(DID_3, PORT_2), |
| 418 | constraints); |
| 419 | |
| 420 | PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 421 | |
| 422 | compiler.compile(intent, null); |
| 423 | |
| 424 | Key intentKey = intent.key(); |
| 425 | |
| 426 | ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey); |
| 427 | ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey); |
| 428 | ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey); |
| 429 | ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey); |
| 430 | ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey); |
| 431 | ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey); |
| 432 | |
| 433 | Set<ResourceAllocation> expectedresourceAllocations = |
| 434 | ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix); |
| 435 | |
| 436 | Set<ResourceAllocation> resourceAllocations = |
| 437 | ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey)); |
| 438 | |
| 439 | assertThat(resourceAllocations, hasSize(6)); |
| 440 | assertEquals(expectedresourceAllocations, resourceAllocations); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Tests if bandwidth resources get allocated correctly using the resource |
| 445 | * group. An intent with a resource group is submitted. |
| 446 | */ |
| 447 | @Test |
| 448 | public void testRGBandwidthConstrainedIntentAllocation() { |
| 449 | final double bpsTotal = 1000.0; |
| 450 | |
| 451 | ResourceGroup resourceGroup = ResourceGroup.of(100); |
| 452 | |
| 453 | String[] hops = {S1, S2, S3}; |
| 454 | |
| 455 | final ResourceService resourceService = |
| 456 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
| 457 | final List<Constraint> constraints = |
| 458 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE))); |
| 459 | |
| 460 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 461 | new ConnectPoint(DID_3, PORT_2), |
| 462 | constraints, |
| 463 | resourceGroup); |
| 464 | |
| 465 | PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 466 | |
| 467 | compiler.compile(intent, null); |
| 468 | |
| 469 | ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup); |
| 470 | ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup); |
| 471 | ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup); |
| 472 | ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup); |
| 473 | ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup); |
| 474 | ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup); |
| 475 | |
| 476 | Set<ResourceAllocation> expectedresourceAllocations = |
| 477 | ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix); |
| 478 | |
| 479 | Set<ResourceAllocation> resourceAllocations = |
| 480 | ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup)); |
| 481 | |
| 482 | assertThat(resourceAllocations, hasSize(6)); |
| 483 | assertEquals(expectedresourceAllocations, resourceAllocations); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * Tests that bandwidth resources don't get allocated twice if the intent |
| 488 | * is submitted twice. |
| 489 | */ |
| 490 | @Test |
| 491 | public void testTwoBandwidthConstrainedIntentAllocation() { |
| 492 | final double bpsTotal = 1000.0; |
| 493 | |
| 494 | String[] hops = {S1, S2, S3}; |
| 495 | |
| 496 | final ResourceService resourceService = |
| 497 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
| 498 | final List<Constraint> constraints = |
| 499 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE))); |
| 500 | |
| 501 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 502 | new ConnectPoint(DID_3, PORT_2), |
| 503 | constraints); |
| 504 | |
| 505 | PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 506 | |
| 507 | compiler.compile(intent, null); |
| 508 | |
| 509 | // Resubmit the same intent |
| 510 | compiler.compile(intent, null); |
| 511 | |
| 512 | Key intentKey = intent.key(); |
| 513 | |
| 514 | ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, intentKey); |
| 515 | ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, intentKey); |
| 516 | ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, intentKey); |
| 517 | ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, intentKey); |
| 518 | ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, intentKey); |
| 519 | ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, intentKey); |
| 520 | |
| 521 | Set<ResourceAllocation> expectedresourceAllocations = |
| 522 | ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix); |
| 523 | |
| 524 | Set<ResourceAllocation> resourceAllocations = |
| 525 | ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey)); |
| 526 | |
| 527 | assertThat(resourceAllocations, hasSize(6)); |
| 528 | assertEquals(expectedresourceAllocations, resourceAllocations); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Tests if bandwidth resources get allocated correctly using groups. |
| 533 | * An intent asks to allocate bandwidth using the intent key as a reference. |
| 534 | * Then, the intent is submitted with the same key and a group set. |
| 535 | * Previous allocations should be released and new resources should be |
| 536 | * allocated using the group. |
| 537 | */ |
| 538 | @Test |
| 539 | public void testKeyRGBandwidthConstrainedIntentAllocation() { |
| 540 | final double bpsTotal = 1000.0; |
| 541 | |
| 542 | String[] hops = {S1, S2, S3}; |
| 543 | |
| 544 | final ResourceService resourceService = |
| 545 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
| 546 | final List<Constraint> constraints = |
| 547 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(BPS_TO_RESERVE))); |
| 548 | |
| 549 | final PointToPointIntent intent = makeIntent(new ConnectPoint(DID_1, PORT_1), |
| 550 | new ConnectPoint(DID_3, PORT_2), |
| 551 | constraints); |
| 552 | |
| 553 | PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService); |
| 554 | |
| 555 | compiler.compile(intent, null); |
| 556 | |
| 557 | Key intentKey = intent.key(); |
| 558 | |
| 559 | ResourceGroup resourceGroup = ResourceGroup.of(100); |
| 560 | |
| 561 | final PointToPointIntent newIntent = makeIntent(intentKey, |
| 562 | new ConnectPoint(DID_1, PORT_1), |
| 563 | new ConnectPoint(DID_3, PORT_2), |
| 564 | constraints, |
| 565 | resourceGroup); |
| 566 | |
| 567 | compiler.compile(newIntent, null); |
| 568 | |
| 569 | ResourceAllocation rAOne = new ResourceAllocation(RESOURCE_SW1_P1, resourceGroup); |
| 570 | ResourceAllocation rATwo = new ResourceAllocation(RESOURCE_SW1_P2, resourceGroup); |
| 571 | ResourceAllocation rAThree = new ResourceAllocation(RESOURCE_SW2_P1, resourceGroup); |
| 572 | ResourceAllocation rAFour = new ResourceAllocation(RESOURCE_SW2_P2, resourceGroup); |
| 573 | ResourceAllocation rAFive = new ResourceAllocation(RESOURCE_SW3_P1, resourceGroup); |
| 574 | ResourceAllocation rASix = new ResourceAllocation(RESOURCE_SW3_P2, resourceGroup); |
| 575 | |
| 576 | Set<ResourceAllocation> expectedresourceAllocations = |
| 577 | ImmutableSet.of(rAOne, rATwo, rAThree, rAFour, rAFive, rASix); |
| 578 | |
| 579 | Set<ResourceAllocation> resourceAllocations = |
| 580 | ImmutableSet.copyOf(resourceService.getResourceAllocations(resourceGroup)); |
| 581 | |
| 582 | assertThat(resourceAllocations, hasSize(6)); |
| 583 | assertEquals(expectedresourceAllocations, resourceAllocations); |
| 584 | } |
Ray Milkey | c0fa4db | 2014-10-17 08:49:54 -0700 | [diff] [blame] | 585 | } |