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; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 19 | import org.hamcrest.Matchers; |
| 20 | import org.junit.Test; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 21 | import org.onlab.packet.VlanId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 22 | import org.onosproject.TestApplicationId; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 23 | import org.onosproject.core.ApplicationId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.ConnectPoint; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.FilteredConnectPoint; |
| 26 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.flow.TrafficSelector; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.intent.AbstractIntentTest; |
| 30 | import org.onosproject.net.intent.Intent; |
| 31 | import org.onosproject.net.intent.IntentTestsMocks; |
| 32 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 33 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 34 | |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 35 | import java.util.HashSet; |
| 36 | import java.util.List; |
| 37 | import java.util.Set; |
| 38 | |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 39 | import static org.hamcrest.CoreMatchers.instanceOf; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 40 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 41 | import static org.hamcrest.MatcherAssert.assertThat; |
| 42 | import static org.hamcrest.Matchers.hasSize; |
| 43 | import static org.hamcrest.Matchers.is; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 44 | import static org.onosproject.net.NetTestTools.connectPoint; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Unit tests for the MultiPointToSinglePoint intent compiler. |
| 49 | */ |
Ray Milkey | 37f6a38 | 2014-11-25 14:54:42 -0800 | [diff] [blame] | 50 | public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest { |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 51 | |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 52 | private static final ApplicationId APPID = new TestApplicationId("foo"); |
| 53 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 54 | private TrafficSelector selector = new IntentTestsMocks.MockSelector(); |
| 55 | private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment(); |
| 56 | |
| 57 | /** |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 58 | * Creates a MultiPointToSinglePoint intent for a group of ingress points |
| 59 | * and an egress point. |
| 60 | * |
| 61 | * @param ingressIds array of ingress device ids |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 62 | * @param egressId device id of the egress point |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 63 | * @return MultiPointToSinglePoint intent |
| 64 | */ |
| 65 | private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) { |
| 66 | Set<ConnectPoint> ingressPoints = new HashSet<>(); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 67 | ConnectPoint egressPoint = connectPoint(egressId, 2); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 68 | |
| 69 | for (String ingressId : ingressIds) { |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 70 | ingressPoints.add(connectPoint(ingressId, 1)); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Ray Milkey | ebc5d22 | 2015-03-18 15:45:36 -0700 | [diff] [blame] | 73 | return MultiPointToSinglePointIntent.builder() |
| 74 | .appId(APPID) |
| 75 | .selector(selector) |
| 76 | .treatment(treatment) |
| 77 | .ingressPoints(ingressPoints) |
| 78 | .egressPoint(egressPoint) |
| 79 | .build(); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 83 | * Generate MultiPointToSinglePointIntent with filtered connection point. |
| 84 | * |
| 85 | * @param ingress filtered ingress points |
| 86 | * @param egress filtered egress point |
| 87 | * @return |
| 88 | */ |
| 89 | private MultiPointToSinglePointIntent makeFilteredConnectPointIntent(Set<FilteredConnectPoint> ingress, |
| 90 | FilteredConnectPoint egress) { |
| 91 | return MultiPointToSinglePointIntent.builder() |
| 92 | .appId(APPID) |
| 93 | .treatment(treatment) |
| 94 | .filteredIngressPoints(ingress) |
| 95 | .filteredEgressPoint(egress) |
| 96 | .build(); |
| 97 | } |
| 98 | |
| 99 | /** |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 100 | * Creates a compiler for MultiPointToSinglePoint intents. |
| 101 | * |
| 102 | * @param hops hops to use while computing paths for this intent |
| 103 | * @return MultiPointToSinglePoint intent |
| 104 | */ |
| 105 | private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) { |
| 106 | MultiPointToSinglePointIntentCompiler compiler = |
| 107 | new MultiPointToSinglePointIntentCompiler(); |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 108 | compiler.pathService = new IntentTestsMocks.Mp2MpMockPathService(hops); |
| 109 | compiler.deviceService = new IntentTestsMocks.MockDeviceService(); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 110 | return compiler; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Tests a single ingress point with 8 hops to its egress point. |
| 115 | */ |
| 116 | @Test |
| 117 | public void testSingleLongPathCompilation() { |
| 118 | |
| 119 | String[] ingress = {"ingress"}; |
| 120 | String egress = "egress"; |
| 121 | |
| 122 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 123 | assertThat(intent, is(notNullValue())); |
| 124 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 125 | String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8"}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 126 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 127 | assertThat(compiler, is(notNullValue())); |
| 128 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 129 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 130 | assertThat(result, is(Matchers.notNullValue())); |
| 131 | assertThat(result, hasSize(1)); |
| 132 | Intent resultIntent = result.get(0); |
| 133 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 134 | |
| 135 | if (resultIntent instanceof LinkCollectionIntent) { |
| 136 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 137 | assertThat(linkIntent.links(), hasSize(9)); |
| 138 | assertThat(linkIntent.links(), linksHasPath("ingress", "h1")); |
| 139 | assertThat(linkIntent.links(), linksHasPath("h1", "h2")); |
| 140 | assertThat(linkIntent.links(), linksHasPath("h2", "h3")); |
| 141 | assertThat(linkIntent.links(), linksHasPath("h4", "h5")); |
| 142 | assertThat(linkIntent.links(), linksHasPath("h5", "h6")); |
| 143 | assertThat(linkIntent.links(), linksHasPath("h7", "h8")); |
| 144 | assertThat(linkIntent.links(), linksHasPath("h8", "egress")); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Tests a simple topology where two ingress points share some path segments |
| 150 | * and some path segments are not shared. |
| 151 | */ |
| 152 | @Test |
| 153 | public void testTwoIngressCompilation() { |
| 154 | String[] ingress = {"ingress1", "ingress2"}; |
| 155 | String egress = "egress"; |
| 156 | |
| 157 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 158 | assertThat(intent, is(notNullValue())); |
| 159 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 160 | final String[] hops = {"inner1", "inner2"}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 161 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 162 | assertThat(compiler, is(notNullValue())); |
| 163 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 164 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 165 | assertThat(result, is(notNullValue())); |
| 166 | assertThat(result, hasSize(1)); |
| 167 | Intent resultIntent = result.get(0); |
| 168 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 169 | |
| 170 | if (resultIntent instanceof LinkCollectionIntent) { |
| 171 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 172 | assertThat(linkIntent.links(), hasSize(4)); |
| 173 | assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1")); |
| 174 | assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1")); |
| 175 | assertThat(linkIntent.links(), linksHasPath("inner1", "inner2")); |
| 176 | assertThat(linkIntent.links(), linksHasPath("inner2", "egress")); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Tests a large number of ingress points that share a common path to the |
| 182 | * egress point. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testMultiIngressCompilation() { |
| 186 | String[] ingress = {"i1", "i2", "i3", "i4", "i5", |
Thomas Vachuska | b97cf28 | 2014-10-20 23:31:12 -0700 | [diff] [blame] | 187 | "i6", "i7", "i8", "i9", "i10"}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 188 | String egress = "e"; |
| 189 | |
| 190 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 191 | assertThat(intent, is(notNullValue())); |
| 192 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 193 | final String[] hops = {"n1"}; |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 194 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 195 | assertThat(compiler, is(notNullValue())); |
| 196 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 197 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 198 | assertThat(result, is(notNullValue())); |
| 199 | assertThat(result, hasSize(1)); |
| 200 | Intent resultIntent = result.get(0); |
| 201 | assertThat(resultIntent instanceof LinkCollectionIntent, is(true)); |
| 202 | |
| 203 | if (resultIntent instanceof LinkCollectionIntent) { |
| 204 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 205 | assertThat(linkIntent.links(), hasSize(ingress.length + 1)); |
| 206 | for (String ingressToCheck : ingress) { |
| 207 | assertThat(linkIntent.links(), |
| 208 | linksHasPath(ingressToCheck, |
| 209 | "n1")); |
| 210 | } |
| 211 | assertThat(linkIntent.links(), linksHasPath("n1", egress)); |
| 212 | } |
| 213 | } |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Tests ingress and egress on the same device. |
| 217 | */ |
| 218 | @Test |
| 219 | public void testSameDeviceCompilation() { |
| 220 | String[] ingress = {"i1", "i2"}; |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 221 | String egress = "i3"; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 222 | |
| 223 | MultiPointToSinglePointIntent intent = makeIntent(ingress, egress); |
| 224 | assertThat(intent, is(notNullValue())); |
| 225 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 226 | final String[] hops = {}; |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 227 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 228 | assertThat(compiler, is(notNullValue())); |
| 229 | |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 230 | List<Intent> result = compiler.compile(intent, null); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 231 | assertThat(result, is(notNullValue())); |
| 232 | assertThat(result, hasSize(1)); |
| 233 | Intent resultIntent = result.get(0); |
| 234 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 235 | |
| 236 | if (resultIntent instanceof LinkCollectionIntent) { |
| 237 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
Jonathan Hart | 066244c | 2015-06-23 09:46:19 -0700 | [diff] [blame] | 238 | assertThat(linkIntent.links(), hasSize(ingress.length)); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 239 | |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 240 | assertThat(linkIntent.links(), linksHasPath("i1", "i3")); |
| 241 | assertThat(linkIntent.links(), linksHasPath("i2", "i3")); |
Ray Milkey | 6e0fb30 | 2015-04-16 14:44:12 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
Yi Tseng | 2a81c9d | 2016-09-14 10:14:24 -0700 | [diff] [blame^] | 244 | |
| 245 | /** |
| 246 | * Tests filtered ingress and egress. |
| 247 | */ |
| 248 | @Test |
| 249 | public void testFilteredConnectPointIntent() { |
| 250 | |
| 251 | Set<FilteredConnectPoint> ingress = ImmutableSet.of( |
| 252 | new FilteredConnectPoint(connectPoint("of1", 1), |
| 253 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("100")).build()), |
| 254 | new FilteredConnectPoint(connectPoint("of2", 1), |
| 255 | DefaultTrafficSelector.builder().matchVlanId(VlanId.vlanId("200")).build()) |
| 256 | ); |
| 257 | |
| 258 | FilteredConnectPoint egress = new FilteredConnectPoint(connectPoint("of4", 1)); |
| 259 | |
| 260 | MultiPointToSinglePointIntent intent = makeFilteredConnectPointIntent(ingress, egress); |
| 261 | String[] hops = {"of3"}; |
| 262 | |
| 263 | MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops); |
| 264 | assertThat(compiler, is(notNullValue())); |
| 265 | |
| 266 | List<Intent> result = compiler.compile(intent, null); |
| 267 | assertThat(result, is(notNullValue())); |
| 268 | assertThat(result, hasSize(1)); |
| 269 | |
| 270 | Intent resultIntent = result.get(0); |
| 271 | assertThat(resultIntent, instanceOf(LinkCollectionIntent.class)); |
| 272 | |
| 273 | if (resultIntent instanceof LinkCollectionIntent) { |
| 274 | LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent; |
| 275 | assertThat(linkIntent.links(), hasSize(3)); |
| 276 | assertThat(linkIntent.links(), linksHasPath("of1", "of3")); |
| 277 | assertThat(linkIntent.links(), linksHasPath("of2", "of3")); |
| 278 | assertThat(linkIntent.links(), linksHasPath("of3", "of4")); |
| 279 | } |
| 280 | |
| 281 | } |
| 282 | |
| 283 | |
Ray Milkey | e668408 | 2014-10-16 16:59:47 -0700 | [diff] [blame] | 284 | } |