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