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