blob: 58daade69d876efe37bd1de24557afc3f45ac288 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 SHIMIZU6c28f832015-02-20 16:12:19 -080016package org.onosproject.net.intent.impl.compiler;
Ray Milkeye6684082014-10-16 16:59:47 -070017
Ray Milkeye6684082014-10-16 16:59:47 -070018import org.hamcrest.Matchers;
19import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080020import org.onosproject.TestApplicationId;
Ray Milkey6e0fb302015-04-16 14:44:12 -070021import org.onosproject.core.ApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.net.ConnectPoint;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070023import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.net.ElementId;
25import org.onosproject.net.Path;
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070026import org.onosproject.net.device.DeviceServiceAdapter;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.flow.TrafficSelector;
28import org.onosproject.net.flow.TrafficTreatment;
29import org.onosproject.net.intent.AbstractIntentTest;
30import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Thomas Vachuska48e64e42015-09-22 15:32:55 -070034import org.onosproject.net.topology.PathServiceAdapter;
Ray Milkeye6684082014-10-16 16:59:47 -070035
Jonathan Hart066244c2015-06-23 09:46:19 -070036import java.util.HashSet;
37import java.util.List;
38import java.util.Set;
39
Ray Milkey6e0fb302015-04-16 14:44:12 -070040import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeye6684082014-10-16 16:59:47 -070041import static org.hamcrest.CoreMatchers.notNullValue;
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.Matchers.hasSize;
44import static org.hamcrest.Matchers.is;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.NetTestTools.connectPoint;
46import static org.onosproject.net.NetTestTools.createPath;
47import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeye6684082014-10-16 16:59:47 -070048
49/**
50 * Unit tests for the MultiPointToSinglePoint intent compiler.
51 */
Ray Milkey37f6a382014-11-25 14:54:42 -080052public class MultiPointToSinglePointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeye6684082014-10-16 16:59:47 -070053
Thomas Vachuskab97cf282014-10-20 23:31:12 -070054 private static final ApplicationId APPID = new TestApplicationId("foo");
55
Ray Milkeye6684082014-10-16 16:59:47 -070056 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 Vachuska48e64e42015-09-22 15:32:55 -070062 private static class MockPathService extends PathServiceAdapter {
Ray Milkeye6684082014-10-16 16:59:47 -070063
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 Milkey6e0fb302015-04-16 14:44:12 -070081 if (pathHops.length != 0) {
82 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
83 }
Ray Milkeye6684082014-10-16 16:59:47 -070084 result.add(createPath(allHops));
Ray Milkey6e0fb302015-04-16 14:44:12 -070085
Ray Milkeye6684082014-10-16 16:59:47 -070086 return result;
87 }
Ray Milkeye6684082014-10-16 16:59:47 -070088 }
89
90 /**
Jonathan Hart96c5a4a2015-07-31 14:23:33 -070091 * 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 Milkeye6684082014-10-16 16:59:47 -0700101 * 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 Vachuskab97cf282014-10-20 23:31:12 -0700105 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -0700106 * @return MultiPointToSinglePoint intent
107 */
108 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
109 Set<ConnectPoint> ingressPoints = new HashSet<>();
Ray Milkey6e0fb302015-04-16 14:44:12 -0700110 ConnectPoint egressPoint = connectPoint(egressId, 2);
Ray Milkeye6684082014-10-16 16:59:47 -0700111
112 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700113 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -0700114 }
115
Ray Milkeyebc5d222015-03-18 15:45:36 -0700116 return MultiPointToSinglePointIntent.builder()
117 .appId(APPID)
118 .selector(selector)
119 .treatment(treatment)
120 .ingressPoints(ingressPoints)
121 .egressPoint(egressPoint)
122 .build();
Ray Milkeye6684082014-10-16 16:59:47 -0700123 }
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 Hart96c5a4a2015-07-31 14:23:33 -0700135 compiler.deviceService = new MockDeviceService();
Ray Milkeye6684082014-10-16 16:59:47 -0700136 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 Vachuskab97cf282014-10-20 23:31:12 -0700152 egress};
Ray Milkeye6684082014-10-16 16:59:47 -0700153 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
154 assertThat(compiler, is(notNullValue()));
155
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800156 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700157 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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800191 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700192 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 Vachuskab97cf282014-10-20 23:31:12 -0700214 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700215 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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800224 List<Intent> result = compiler.compile(intent, null);
Ray Milkeye6684082014-10-16 16:59:47 -0700225 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 Milkey6e0fb302015-04-16 14:44:12 -0700241
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
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800257 List<Intent> result = compiler.compile(intent, null);
Ray Milkey6e0fb302015-04-16 14:44:12 -0700258 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 Hart066244c2015-06-23 09:46:19 -0700265 assertThat(linkIntent.links(), hasSize(ingress.length));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700266
267 assertThat(linkIntent.links(), linksHasPath("i2", "i1"));
Ray Milkey6e0fb302015-04-16 14:44:12 -0700268 }
269 }
Ray Milkeye6684082014-10-16 16:59:47 -0700270}