blob: e85d09025666790952d8670525874a362c360409 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
Ray Milkeye6684082014-10-16 16:59:47 -070016package org.onlab.onos.net.intent.impl;
17
Ray Milkeye6684082014-10-16 16:59:47 -070018import org.hamcrest.Matchers;
19import org.junit.Test;
Thomas Vachuskae0f804a2014-10-27 23:40:48 -070020import org.onlab.onos.core.ApplicationId;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070021import org.onlab.onos.TestApplicationId;
Ray Milkeye6684082014-10-16 16:59:47 -070022import org.onlab.onos.net.ConnectPoint;
23import org.onlab.onos.net.ElementId;
24import org.onlab.onos.net.Path;
25import org.onlab.onos.net.flow.TrafficSelector;
26import org.onlab.onos.net.flow.TrafficTreatment;
27import org.onlab.onos.net.intent.Intent;
Ray Milkeye6684082014-10-16 16:59:47 -070028import org.onlab.onos.net.intent.IntentTestsMocks;
29import org.onlab.onos.net.intent.LinkCollectionIntent;
30import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
31import org.onlab.onos.net.topology.LinkWeight;
32import org.onlab.onos.net.topology.PathService;
33
Thomas Vachuskab97cf282014-10-20 23:31:12 -070034import java.util.HashSet;
35import java.util.List;
36import java.util.Set;
37
Ray Milkeye6684082014-10-16 16:59:47 -070038import static org.hamcrest.CoreMatchers.notNullValue;
39import static org.hamcrest.MatcherAssert.assertThat;
40import static org.hamcrest.Matchers.hasSize;
41import static org.hamcrest.Matchers.is;
42import static org.onlab.onos.net.NetTestTools.connectPoint;
43import static org.onlab.onos.net.NetTestTools.createPath;
44import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
45
46/**
47 * Unit tests for the MultiPointToSinglePoint intent compiler.
48 */
49public class TestMultiPointToSinglePointIntentCompiler {
50
Thomas Vachuskab97cf282014-10-20 23:31:12 -070051 private static final ApplicationId APPID = new TestApplicationId("foo");
52
Ray Milkeye6684082014-10-16 16:59:47 -070053 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
54 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
55
56 /**
57 * Mock path service for creating paths within the test.
58 */
59 private static class MockPathService implements PathService {
60
61 final String[] pathHops;
62
63 /**
64 * Constructor that provides a set of hops to mock.
65 *
66 * @param pathHops path hops to mock
67 */
68 MockPathService(String[] pathHops) {
69 this.pathHops = pathHops;
70 }
71
72 @Override
73 public Set<Path> getPaths(ElementId src, ElementId dst) {
74 Set<Path> result = new HashSet<>();
75
76 String[] allHops = new String[pathHops.length + 1];
77 allHops[0] = src.toString();
78 System.arraycopy(pathHops, 0, allHops, 1, pathHops.length);
79
80 result.add(createPath(allHops));
81 return result;
82 }
83
84 @Override
85 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
86 return null;
87 }
88 }
89
90 /**
91 * Creates a MultiPointToSinglePoint intent for a group of ingress points
92 * and an egress point.
93 *
94 * @param ingressIds array of ingress device ids
Thomas Vachuskab97cf282014-10-20 23:31:12 -070095 * @param egressId device id of the egress point
Ray Milkeye6684082014-10-16 16:59:47 -070096 * @return MultiPointToSinglePoint intent
97 */
98 private MultiPointToSinglePointIntent makeIntent(String[] ingressIds, String egressId) {
99 Set<ConnectPoint> ingressPoints = new HashSet<>();
100 ConnectPoint egressPoint = connectPoint(egressId, 1);
101
102 for (String ingressId : ingressIds) {
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700103 ingressPoints.add(connectPoint(ingressId, 1));
Ray Milkeye6684082014-10-16 16:59:47 -0700104 }
105
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700106 return new MultiPointToSinglePointIntent(APPID, selector, treatment,
107 ingressPoints, egressPoint);
Ray Milkeye6684082014-10-16 16:59:47 -0700108 }
109
110 /**
111 * Creates a compiler for MultiPointToSinglePoint intents.
112 *
113 * @param hops hops to use while computing paths for this intent
114 * @return MultiPointToSinglePoint intent
115 */
116 private MultiPointToSinglePointIntentCompiler makeCompiler(String[] hops) {
117 MultiPointToSinglePointIntentCompiler compiler =
118 new MultiPointToSinglePointIntentCompiler();
119 compiler.pathService = new MockPathService(hops);
Ray Milkeye6684082014-10-16 16:59:47 -0700120 return compiler;
121 }
122
123 /**
124 * Tests a single ingress point with 8 hops to its egress point.
125 */
126 @Test
127 public void testSingleLongPathCompilation() {
128
129 String[] ingress = {"ingress"};
130 String egress = "egress";
131
132 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
133 assertThat(intent, is(notNullValue()));
134
135 String[] hops = {"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700136 egress};
Ray Milkeye6684082014-10-16 16:59:47 -0700137 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
138 assertThat(compiler, is(notNullValue()));
139
140 List<Intent> result = compiler.compile(intent);
141 assertThat(result, is(Matchers.notNullValue()));
142 assertThat(result, hasSize(1));
143 Intent resultIntent = result.get(0);
144 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
145
146 if (resultIntent instanceof LinkCollectionIntent) {
147 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
148 assertThat(linkIntent.links(), hasSize(9));
149 assertThat(linkIntent.links(), linksHasPath("ingress", "h1"));
150 assertThat(linkIntent.links(), linksHasPath("h1", "h2"));
151 assertThat(linkIntent.links(), linksHasPath("h2", "h3"));
152 assertThat(linkIntent.links(), linksHasPath("h4", "h5"));
153 assertThat(linkIntent.links(), linksHasPath("h5", "h6"));
154 assertThat(linkIntent.links(), linksHasPath("h7", "h8"));
155 assertThat(linkIntent.links(), linksHasPath("h8", "egress"));
156 }
157 }
158
159 /**
160 * Tests a simple topology where two ingress points share some path segments
161 * and some path segments are not shared.
162 */
163 @Test
164 public void testTwoIngressCompilation() {
165 String[] ingress = {"ingress1", "ingress2"};
166 String egress = "egress";
167
168 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
169 assertThat(intent, is(notNullValue()));
170
171 final String[] hops = {"inner1", "inner2", egress};
172 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
173 assertThat(compiler, is(notNullValue()));
174
175 List<Intent> result = compiler.compile(intent);
176 assertThat(result, is(notNullValue()));
177 assertThat(result, hasSize(1));
178 Intent resultIntent = result.get(0);
179 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
180
181 if (resultIntent instanceof LinkCollectionIntent) {
182 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
183 assertThat(linkIntent.links(), hasSize(4));
184 assertThat(linkIntent.links(), linksHasPath("ingress1", "inner1"));
185 assertThat(linkIntent.links(), linksHasPath("ingress2", "inner1"));
186 assertThat(linkIntent.links(), linksHasPath("inner1", "inner2"));
187 assertThat(linkIntent.links(), linksHasPath("inner2", "egress"));
188 }
189 }
190
191 /**
192 * Tests a large number of ingress points that share a common path to the
193 * egress point.
194 */
195 @Test
196 public void testMultiIngressCompilation() {
197 String[] ingress = {"i1", "i2", "i3", "i4", "i5",
Thomas Vachuskab97cf282014-10-20 23:31:12 -0700198 "i6", "i7", "i8", "i9", "i10"};
Ray Milkeye6684082014-10-16 16:59:47 -0700199 String egress = "e";
200
201 MultiPointToSinglePointIntent intent = makeIntent(ingress, egress);
202 assertThat(intent, is(notNullValue()));
203
204 final String[] hops = {"n1", egress};
205 MultiPointToSinglePointIntentCompiler compiler = makeCompiler(hops);
206 assertThat(compiler, is(notNullValue()));
207
208 List<Intent> result = compiler.compile(intent);
209 assertThat(result, is(notNullValue()));
210 assertThat(result, hasSize(1));
211 Intent resultIntent = result.get(0);
212 assertThat(resultIntent instanceof LinkCollectionIntent, is(true));
213
214 if (resultIntent instanceof LinkCollectionIntent) {
215 LinkCollectionIntent linkIntent = (LinkCollectionIntent) resultIntent;
216 assertThat(linkIntent.links(), hasSize(ingress.length + 1));
217 for (String ingressToCheck : ingress) {
218 assertThat(linkIntent.links(),
219 linksHasPath(ingressToCheck,
220 "n1"));
221 }
222 assertThat(linkIntent.links(), linksHasPath("n1", egress));
223 }
224 }
225}