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 | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 17 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.collect.Sets; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 21 | import org.hamcrest.Matchers; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 22 | import org.junit.Rule; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 23 | import org.junit.Test; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 24 | import org.junit.rules.ExpectedException; |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 25 | import org.onlab.packet.IpPrefix; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 26 | import org.onlab.packet.VlanId; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 27 | import org.onlab.util.Bandwidth; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.TestApplicationId; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 32 | import org.onosproject.net.FilteredConnectPoint; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 33 | import org.onosproject.net.PortNumber; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficSelector; |
| 36 | import org.onosproject.net.flow.TrafficTreatment; |
| 37 | import org.onosproject.net.intent.AbstractIntentTest; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.Constraint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.Intent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.IntentException; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 41 | import org.onosproject.net.intent.IntentTestsMocks; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 42 | import org.onosproject.net.intent.Key; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 44 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.constraint.BandwidthConstraint; |
| 46 | import org.onosproject.net.intent.constraint.PartialFailureConstraint; |
| 47 | import org.onosproject.net.resource.ContinuousResource; |
| 48 | import org.onosproject.net.resource.MockResourceService; |
| 49 | import org.onosproject.net.resource.ResourceAllocation; |
| 50 | import org.onosproject.net.resource.ResourceService; |
| 51 | import org.onosproject.net.resource.Resources; |
| 52 | import org.onosproject.net.topology.PathService; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 53 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 54 | import java.util.Collections; |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 55 | import java.util.List; |
| 56 | import java.util.Set; |
| 57 | |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 58 | import static org.hamcrest.CoreMatchers.instanceOf; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 59 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 60 | import static org.hamcrest.MatcherAssert.assertThat; |
| 61 | import static org.hamcrest.Matchers.hasSize; |
| 62 | import static org.hamcrest.Matchers.is; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 63 | import static org.junit.Assert.assertEquals; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 64 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Unit tests for the MultiPointToSinglePoint intent compiler. |
| 68 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 69 | public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 70 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 71 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 72 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 73 | private static final String S1 = "s1"; |
| 74 | private static final String S2 = "s2"; |
| 75 | private static final String S3 = "s3"; |
| 76 | private static final String S4 = "s4"; |
| 77 | private static final String S5 = "s5"; |
| 78 | private static final String S6 = "s6"; |
| 79 | private static final String S7 = "s7"; |
| 80 | private static final String S8 = "s8"; |
| 81 | |
| 82 | private static final DeviceId DID_1 = DeviceId.deviceId("of:" + S1); |
| 83 | private static final DeviceId DID_2 = DeviceId.deviceId("of:" + S2); |
| 84 | private static final DeviceId DID_3 = DeviceId.deviceId("of:" + S3); |
| 85 | private static final DeviceId DID_4 = DeviceId.deviceId("of:" + S4); |
| 86 | private static final DeviceId DID_5 = DeviceId.deviceId("of:" + S5); |
| 87 | private static final DeviceId DID_8 = DeviceId.deviceId("of:" + S8); |
| 88 | |
| 89 | private static final PortNumber PORT_1 = PortNumber.portNumber(1); |
| 90 | private static final PortNumber PORT_2 = PortNumber.portNumber(2); |
| 91 | private static final PortNumber PORT_3 = PortNumber.portNumber(3); |
| 92 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 93 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 94 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 95 | |
| 96 | /** |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 97 | * Creates a MultiPointToSinglePoint intent for a group of ingress points |
| 98 | * and an egress point. |
| 99 | * |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 100 | * @param ingress the filtered ingress points |
| 101 | * @param egress the filtered egress point |
| 102 | * @return a MultiPointToSinglePoint intent |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 103 | */ |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 104 | private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress, |
| 105 | FilteredConnectPoint egress) { |
| 106 | return makeIntent(ingress, egress, Lists.newArrayList()); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /** |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 110 | * Generates a MultiPointToSinglePointIntent with filtered connection point. |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 111 | * |
| 112 | * @param ingress filtered ingress points |
| 113 | * @param egress filtered egress point |
| 114 | * @return |
| 115 | */ |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 116 | private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress, |
| 117 | FilteredConnectPoint egress, |
| 118 | TrafficSelector trafficSelector) { |
| 119 | return makeIntent(ingress, |
| 120 | egress, |
| 121 | trafficSelector, |
| 122 | Lists.newArrayList()); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Creates a MultiPointToSinglePoint intent for a group of ingress points, |
| 127 | * egress points and a list of constraints. |
| 128 | * |
| 129 | * @param ingress the filtered ingress point |
| 130 | * @param egress the filtered egress point |
| 131 | * @param constraints the list of intent constraints |
| 132 | * @return a MultiPointToSinglePoint intent |
| 133 | */ |
| 134 | private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress, |
| 135 | FilteredConnectPoint egress, |
| 136 | List<Constraint> constraints) { |
| 137 | return makeIntent(ingress, egress, selector, constraints); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Generates a MultiPointToSinglePointIntent with filtered connection point. |
| 142 | * |
| 143 | * @param ingress filtered ingress points |
| 144 | * @param egress filtered egress point |
| 145 | * @param constraints the list of intent constraints |
| 146 | * @return |
| 147 | */ |
| 148 | private MultiPointToSinglePointIntent makeIntent(Set<FilteredConnectPoint> ingress, |
| 149 | FilteredConnectPoint egress, |
| 150 | TrafficSelector trafficSelector, |
| 151 | List<Constraint> constraints) { |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 152 | return MultiPointToSinglePointIntent.builder() |
| 153 | .appId(APPID) |
| 154 | .treatment(treatment) |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 155 | .selector(trafficSelector) |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 156 | .filteredIngressPoints(ingress) |
| 157 | .filteredEgressPoint(egress) |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 158 | .constraints(constraints) |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 159 | .build(); |
| 160 | } |
| 161 | |
| 162 | /** |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 163 | * Creates a compiler for MultiPointToSinglePoint intents. |
| 164 | * |
| 165 | * @param hops hops to use while computing paths for this intent |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 166 | * @param pathService the path service |
| 167 | * @param resourceService the resource service |
| 168 | * @return MultiPointToSinglePoint intent |
| 169 | */ |
| 170 | private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops, |
| 171 | PathService pathService, |
| 172 | ResourceService resourceService) { |
| 173 | MultiPointToSinglePointIntentCompiler compiler = |
| 174 | new MultiPointToSinglePointIntentCompiler(); |
| 175 | |
| 176 | compiler.deviceService = new IntentTestsMocks.MockDeviceService(); |
| 177 | |
| 178 | if (pathService == null) { |
| 179 | compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops); |
| 180 | } else { |
| 181 | compiler.pathService = pathService; |
| 182 | } |
| 183 | |
| 184 | if (resourceService == null) { |
| 185 | compiler.resourceService = new MockResourceService(); |
| 186 | } else { |
| 187 | compiler.resourceService = resourceService; |
| 188 | } |
| 189 | |
| 190 | return compiler; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Creates a compiler for MultiPointToSinglePoint intents. |
| 195 | * |
| 196 | * @param hops hops to use while computing paths for this intent |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 197 | * @return MultiPointToSinglePoint intent |
| 198 | */ |
| 199 | private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 200 | return makeCompiler(hops, null, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Tests a single ingress point with 8 hops to its egress point. |
| 205 | */ |
| 206 | @Test |
| 207 | public void testSingleLongPathCompilation() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 208 | Set<FilteredConnectPoint> ingress = |
| 209 | Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1))); |
| 210 | FilteredConnectPoint egress = |
| 211 | new FilteredConnectPoint(new ConnectPoint(DID_8, PORT_1)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 212 | |
| 213 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 214 | assertThat(intent, is(notNullValue())); |
| 215 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 216 | String[] hops = {S2, S3, S4, S5, S6, S7}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 217 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 218 | assertThat(compiler, is(notNullValue())); |
| 219 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 220 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 221 | assertThat(result, is(Matchers.notNullValue())); |
| 222 | assertThat(result, hasSize(1)); |
| 223 | Intent resultIntent = result.get(0); |
| 224 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 225 | |
| 226 | if (resultIntent instanceof LinkCollectionIntent) { |
| 227 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 228 | assertThat(linkIntent.links(), hasSize(7)); |
| 229 | assertThat(linkIntent.links(), linksHasPath(S1, S2)); |
| 230 | assertThat(linkIntent.links(), linksHasPath(S2, S3)); |
| 231 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
| 232 | assertThat(linkIntent.links(), linksHasPath(S4, S5)); |
| 233 | assertThat(linkIntent.links(), linksHasPath(S5, S6)); |
| 234 | assertThat(linkIntent.links(), linksHasPath(S6, S7)); |
| 235 | assertThat(linkIntent.links(), linksHasPath(S7, S8)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 236 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 237 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Tests a simple topology where two ingress points share some path segments |
| 242 | * and some path segments are not shared. |
| 243 | */ |
| 244 | @Test |
| 245 | public void testTwoIngressCompilation() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 246 | Set<FilteredConnectPoint> ingress = |
| 247 | Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 248 | new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1))); |
| 249 | FilteredConnectPoint egress = |
| 250 | new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_1)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 251 | |
| 252 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 253 | assertThat(intent, is(notNullValue())); |
| 254 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 255 | final String[] hops = {S3}; |
| 256 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 257 | assertThat(compiler, is(notNullValue())); |
| 258 | |
| 259 | List<Intent> result = compiler.compile(intent, null); |
| 260 | assertThat(result, is(notNullValue())); |
| 261 | assertThat(result, hasSize(1)); |
| 262 | Intent resultIntent = result.get(0); |
| 263 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 264 | |
| 265 | if (resultIntent instanceof LinkCollectionIntent) { |
| 266 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 267 | assertThat(linkIntent.links(), hasSize(3)); |
| 268 | assertThat(linkIntent.links(), linksHasPath(S1, S3)); |
| 269 | assertThat(linkIntent.links(), linksHasPath(S2, S3)); |
| 270 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
| 271 | } |
| 272 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Tests a large number of ingress points that share a common path to the |
| 277 | * egress point. |
| 278 | */ |
| 279 | @Test |
| 280 | public void testMultiIngressCompilation() { |
| 281 | Set<FilteredConnectPoint> ingress = |
| 282 | Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 283 | new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1)), |
| 284 | new FilteredConnectPoint(new ConnectPoint(DID_3, PORT_1))); |
| 285 | FilteredConnectPoint egress = |
| 286 | new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1)); |
| 287 | |
| 288 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 289 | assertThat(intent, is(notNullValue())); |
| 290 | |
| 291 | final String[] hops = {S4}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 292 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 293 | assertThat(compiler, is(notNullValue())); |
| 294 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 295 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 296 | assertThat(result, is(notNullValue())); |
| 297 | assertThat(result, hasSize(1)); |
| 298 | Intent resultIntent = result.get(0); |
| 299 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 300 | |
| 301 | if (resultIntent instanceof LinkCollectionIntent) { |
| 302 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 303 | assertThat(linkIntent.links(), hasSize(4)); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 304 | assertThat(linkIntent.links(), linksHasPath(S1, S4)); |
| 305 | assertThat(linkIntent.links(), linksHasPath(S2, S4)); |
| 306 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
| 307 | assertThat(linkIntent.links(), linksHasPath(S4, S5)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 308 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 309 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 310 | } |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 311 | |
| 312 | /** |
| 313 | * Tests ingress and egress on the same device. |
| 314 | */ |
| 315 | @Test |
| 316 | public void testSameDeviceCompilation() { |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 317 | Set<FilteredConnectPoint> ingress = |
| 318 | Sets.newHashSet(new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 319 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_2))); |
| 320 | FilteredConnectPoint egress = |
| 321 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_3)); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 322 | |
| 323 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 324 | assertThat(intent, is(notNullValue())); |
| 325 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 326 | final String[] hops = {}; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 327 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 328 | assertThat(compiler, is(notNullValue())); |
| 329 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 330 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 331 | assertThat(result, is(notNullValue())); |
| 332 | assertThat(result, hasSize(1)); |
| 333 | Intent resultIntent = result.get(0); |
| 334 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 335 | |
| 336 | if (resultIntent instanceof LinkCollectionIntent) { |
| 337 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 338 | assertThat(linkIntent.links(), hasSize(0)); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 339 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 340 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 341 | } |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 342 | |
| 343 | /** |
| 344 | * Tests filtered ingress and egress. |
| 345 | */ |
| 346 | @Test |
| 347 | public void testFilteredConnectPointIntent() { |
| 348 | |
| 349 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 350 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1), |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 351 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()), |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 352 | new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1), |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 353 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build()) |
| 354 | ); |
| 355 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 356 | FilteredConnectPoint egress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)); |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 357 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 358 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress, selector); |
| 359 | String[] hops = {S3}; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 360 | |
| 361 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 362 | assertThat(compiler, is(notNullValue())); |
| 363 | |
| 364 | List<Intent> result = compiler.compile(intent, null); |
| 365 | assertThat(result, is(notNullValue())); |
| 366 | assertThat(result, hasSize(1)); |
| 367 | |
| 368 | Intent resultIntent = result.get(0); |
| 369 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 370 | |
| 371 | if (resultIntent instanceof LinkCollectionIntent) { |
| 372 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 373 | assertThat(linkIntent.links(), hasSize(3)); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 374 | assertThat(linkIntent.links(), linksHasPath(S1, S3)); |
| 375 | assertThat(linkIntent.links(), linksHasPath(S2, S3)); |
| 376 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 377 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 378 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 379 | |
| 380 | } |
| 381 | |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 382 | /** |
| 383 | * Tests selector, filtered ingress and egress. |
| 384 | */ |
| 385 | @Test |
| 386 | public void testNonTrivialSelectorsIntent() { |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 387 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 388 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1), |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 389 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()), |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 390 | new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1), |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 391 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build()) |
| 392 | ); |
| 393 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 394 | FilteredConnectPoint egress = |
| 395 | new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)); |
| 396 | |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 397 | TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder() |
| 398 | .matchIPDst(IpPrefix.valueOf("192.168.100.0/24")) |
| 399 | .build(); |
| 400 | |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 401 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 402 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress, ipPrefixSelector); |
| 403 | String[] hops = {S3}; |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 404 | |
| 405 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 406 | assertThat(compiler, is(notNullValue())); |
| 407 | |
| 408 | List<Intent> result = compiler.compile(intent, null); |
| 409 | assertThat(result, is(notNullValue())); |
| 410 | assertThat(result, hasSize(1)); |
| 411 | |
| 412 | Intent resultIntent = result.get(0); |
| 413 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 414 | |
| 415 | if (resultIntent instanceof LinkCollectionIntent) { |
| 416 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 417 | assertThat(linkIntent.links(), hasSize(3)); |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 418 | assertThat(linkIntent.links(), linksHasPath(S1, S3)); |
| 419 | assertThat(linkIntent.links(), linksHasPath(S2, S3)); |
| 420 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 421 | assertThat(linkIntent.selector(), is(ipPrefixSelector)); |
| 422 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 423 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
Pier Ventre | 973bb03 | 2016-10-11 08:57:39 -0700 | [diff] [blame] | 424 | } |
| 425 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 426 | /** |
| 427 | * Tests if bandwidth resources get allocated correctly. |
| 428 | */ |
| 429 | @Test |
| 430 | public void testBandwidthConstrainedIntentAllocation() { |
| 431 | final double bpsTotal = 1000.0; |
| 432 | final double bpsToReserve = 100.0; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame] | 433 | |
Luca Prete | de10c78 | 2017-01-05 17:23:08 -0800 | [diff] [blame] | 434 | ContinuousResource resourceSw1P1 = |
| 435 | Resources.continuous(DID_1, PORT_1, Bandwidth.class) |
| 436 | .resource(bpsToReserve); |
| 437 | ContinuousResource resourceSw1P2 = |
| 438 | Resources.continuous(DID_1, PORT_2, Bandwidth.class) |
| 439 | .resource(bpsToReserve); |
| 440 | ContinuousResource resourceSw2P1 = |
| 441 | Resources.continuous(DID_2, PORT_1, Bandwidth.class) |
| 442 | .resource(bpsToReserve); |
| 443 | ContinuousResource resourceSw2P2 = |
| 444 | Resources.continuous(DID_2, PORT_2, Bandwidth.class) |
| 445 | .resource(bpsToReserve); |
| 446 | ContinuousResource resourceSw3P1 = |
| 447 | Resources.continuous(DID_3, PORT_1, Bandwidth.class) |
| 448 | .resource(bpsToReserve); |
| 449 | ContinuousResource resourceSw3P2 = |
| 450 | Resources.continuous(DID_3, PORT_2, Bandwidth.class) |
| 451 | .resource(bpsToReserve); |
| 452 | ContinuousResource resourceSw3P3 = |
| 453 | Resources.continuous(DID_3, PORT_3, Bandwidth.class) |
| 454 | .resource(bpsToReserve); |
| 455 | ContinuousResource resourceSw4P1 = |
| 456 | Resources.continuous(DID_4, PORT_1, Bandwidth.class) |
| 457 | .resource(bpsToReserve); |
| 458 | ContinuousResource resourceSw4P2 = |
| 459 | Resources.continuous(DID_4, PORT_2, Bandwidth.class) |
| 460 | .resource(bpsToReserve); |
| 461 | |
| 462 | String[] hops = {DID_3.toString()}; |
| 463 | |
| 464 | final ResourceService resourceService = |
| 465 | MockResourceService.makeCustomBandwidthResourceService(bpsTotal); |
| 466 | final List<Constraint> constraints = |
| 467 | Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(bpsToReserve))); |
| 468 | |
| 469 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 470 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 471 | new FilteredConnectPoint(new ConnectPoint(DID_2, PORT_1))); |
| 472 | |
| 473 | TrafficSelector ipPrefixSelector = DefaultTrafficSelector.builder() |
| 474 | .matchIPDst(IpPrefix.valueOf("192.168.100.0/24")) |
| 475 | .build(); |
| 476 | |
| 477 | FilteredConnectPoint egress = new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)); |
| 478 | |
| 479 | MultiPointToSinglePointIntent intent = |
| 480 | makeIntent(ingress, egress, ipPrefixSelector, constraints); |
| 481 | |
| 482 | MultiPointToSinglePointIntentCompiler compiler = |
| 483 | makeCompiler(null, |
| 484 | new IntentTestsMocks.FixedMP2MPMockPathService(hops), |
| 485 | resourceService); |
| 486 | |
| 487 | compiler.compile(intent, null); |
| 488 | |
| 489 | Key intentKey = intent.key(); |
| 490 | |
| 491 | ResourceAllocation rA1 = new ResourceAllocation(resourceSw1P1, intentKey); |
| 492 | ResourceAllocation rA2 = new ResourceAllocation(resourceSw1P2, intentKey); |
| 493 | ResourceAllocation rA3 = new ResourceAllocation(resourceSw2P1, intentKey); |
| 494 | ResourceAllocation rA4 = new ResourceAllocation(resourceSw2P2, intentKey); |
| 495 | ResourceAllocation rA5 = new ResourceAllocation(resourceSw3P1, intentKey); |
| 496 | ResourceAllocation rA6 = new ResourceAllocation(resourceSw3P2, intentKey); |
| 497 | ResourceAllocation rA7 = new ResourceAllocation(resourceSw3P3, intentKey); |
| 498 | ResourceAllocation rA8 = new ResourceAllocation(resourceSw4P1, intentKey); |
| 499 | ResourceAllocation rA9 = new ResourceAllocation(resourceSw4P2, intentKey); |
| 500 | |
| 501 | Set<ResourceAllocation> expectedResourceAllocations = |
| 502 | ImmutableSet.of(rA1, rA2, rA3, rA4, rA5, rA6, rA7, rA8, rA9); |
| 503 | |
| 504 | Set<ResourceAllocation> resourceAllocations = |
| 505 | ImmutableSet.copyOf(resourceService.getResourceAllocations(intentKey)); |
| 506 | |
| 507 | assertThat(resourceAllocations, hasSize(9)); |
| 508 | assertEquals(expectedResourceAllocations, resourceAllocations); |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Tests if all expected links are present when a partial failure |
| 513 | * constraint is used and one ingress is not present. |
| 514 | */ |
| 515 | @Test |
| 516 | public void testPartialFailureConstraintSuccess() { |
| 517 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 518 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 519 | new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1))); |
| 520 | |
| 521 | FilteredConnectPoint egress = |
| 522 | new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)); |
| 523 | |
| 524 | final List<Constraint> constraints = |
| 525 | Collections.singletonList(new PartialFailureConstraint()); |
| 526 | |
| 527 | MultiPointToSinglePointIntent intent = |
| 528 | makeIntent(ingress, egress, constraints); |
| 529 | |
| 530 | String[] hops = {S3}; |
| 531 | |
| 532 | MultiPointToSinglePointIntentCompiler compiler = |
| 533 | makeCompiler(null, |
| 534 | new IntentTestsMocks.FixedMP2MPMockPathService(hops), |
| 535 | null); |
| 536 | assertThat(compiler, is(notNullValue())); |
| 537 | |
| 538 | List<Intent> result = compiler.compile(intent, null); |
| 539 | assertThat(result, is(notNullValue())); |
| 540 | assertThat(result, hasSize(1)); |
| 541 | |
| 542 | Intent resultIntent = result.get(0); |
| 543 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 544 | |
| 545 | if (resultIntent instanceof LinkCollectionIntent) { |
| 546 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 547 | assertThat(linkIntent.links(), hasSize(2)); |
| 548 | assertThat(linkIntent.links(), linksHasPath(S1, S3)); |
| 549 | assertThat(linkIntent.links(), linksHasPath(S3, S4)); |
| 550 | } |
| 551 | assertThat("key is inherited", resultIntent.key(), is(intent.key())); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Exception expected to be raised when an intent does not find all paths |
| 556 | * and a partiale failure constraint is not specified. |
| 557 | */ |
| 558 | @Rule |
| 559 | public ExpectedException intentException = ExpectedException.none(); |
| 560 | |
| 561 | /** |
| 562 | * Tests if compiling an intent without partial failure constraints set and |
| 563 | * with a missing ingress connect point generates an exception and no other |
| 564 | * results. |
| 565 | */ |
| 566 | @Test |
| 567 | public void testPartialFailureConstraintFailure() { |
| 568 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 569 | new FilteredConnectPoint(new ConnectPoint(DID_1, PORT_1)), |
| 570 | new FilteredConnectPoint(new ConnectPoint(DID_5, PORT_1))); |
| 571 | |
| 572 | FilteredConnectPoint egress = |
| 573 | new FilteredConnectPoint(new ConnectPoint(DID_4, PORT_2)); |
| 574 | |
| 575 | MultiPointToSinglePointIntent intent = |
| 576 | makeIntent(ingress, egress); |
| 577 | |
| 578 | String[] hops = {S3}; |
| 579 | |
| 580 | MultiPointToSinglePointIntentCompiler compiler = |
| 581 | makeCompiler(null, |
| 582 | new IntentTestsMocks.FixedMP2MPMockPathService(hops), |
| 583 | null); |
| 584 | assertThat(compiler, is(notNullValue())); |
| 585 | |
| 586 | intentException.expect(IntentException.class); |
| 587 | |
| 588 | List<Intent> result = compiler.compile(intent, null); |
| 589 | assertThat(result, null); |
| 590 | } |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 591 | } |