blob: d61e01dedf717d62ba651ee550aed1068656de2d [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 Milkeyc0fa4db2014-10-17 08:49:54 -070017
Pier Ventreffe88d62016-10-13 14:34:40 -070018import com.google.common.collect.ImmutableSet;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070019import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080020import org.junit.Test;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070021import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.TestApplicationId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.net.ConnectPoint;
Pier Ventreffe88d62016-10-13 14:34:40 -070025import org.onosproject.net.FilteredConnectPoint;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.Link;
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;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080030import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.net.intent.Intent;
32import org.onosproject.net.intent.IntentTestsMocks;
Pier Ventreffe88d62016-10-13 14:34:40 -070033import org.onosproject.net.intent.LinkCollectionIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080035import org.onosproject.net.intent.constraint.BandwidthConstraint;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080036import org.onosproject.net.intent.impl.PathNotFoundException;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080037import org.onosproject.net.resource.ResourceService;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070038
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070039import java.util.Collections;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070040import java.util.List;
Pier Ventreffe88d62016-10-13 14:34:40 -070041import java.util.Set;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070042
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080043import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070044import static org.hamcrest.MatcherAssert.assertThat;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080045import static org.hamcrest.Matchers.containsString;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070046import static org.hamcrest.Matchers.hasSize;
47import static org.hamcrest.Matchers.is;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080048import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import static org.onosproject.net.DeviceId.deviceId;
50import static org.onosproject.net.NetTestTools.APP_ID;
51import static org.onosproject.net.NetTestTools.connectPoint;
52import static org.onosproject.net.PortNumber.portNumber;
53import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070054
55/**
56 * Unit tests for the HostToHost intent compiler.
57 */
Ray Milkey37f6a382014-11-25 14:54:42 -080058public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070059
Thomas Vachuskab97cf282014-10-20 23:31:12 -070060 private static final ApplicationId APPID = new TestApplicationId("foo");
61
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070062 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
63 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
64
65 /**
66 * Creates a PointToPoint intent based on ingress and egress device Ids.
67 *
68 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070069 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070070 * @return PointToPointIntent for the two devices
71 */
72 private PointToPointIntent makeIntent(String ingressIdString,
73 String egressIdString) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070074 return PointToPointIntent.builder()
75 .appId(APPID)
76 .selector(selector)
77 .treatment(treatment)
Pier Ventreffe88d62016-10-13 14:34:40 -070078 .filteredIngressPoint(new FilteredConnectPoint(connectPoint(ingressIdString, 1)))
79 .filteredEgressPoint(new FilteredConnectPoint(connectPoint(egressIdString, 1)))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070080 .build();
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070081 }
82
83 /**
Sho SHIMIZUb7052172015-02-20 11:09:21 -080084 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
85 *
86 * @param ingressIdString string for id of ingress device
87 * @param egressIdString string for id of egress device
88 * @param constraints constraints
89 * @return PointToPointIntent for the two device with constraints
90 */
91 private PointToPointIntent makeIntent(String ingressIdString,
92 String egressIdString, List<Constraint> constraints) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070093 return PointToPointIntent.builder()
94 .appId(APPID)
95 .selector(selector)
96 .treatment(treatment)
Pier Ventreffe88d62016-10-13 14:34:40 -070097 .filteredIngressPoint(new FilteredConnectPoint(connectPoint(ingressIdString, 1)))
98 .filteredEgressPoint(new FilteredConnectPoint(connectPoint(egressIdString, 1)))
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070099 .constraints(constraints)
100 .build();
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800101 }
102
103 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700104 * Creates a compiler for HostToHost intents.
105 *
106 * @param hops string array describing the path hops to use when compiling
107 * @return HostToHost intent compiler
108 */
109 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800110 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -0800111 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700112 return compiler;
113 }
114
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800115 /**
116 * Creates a point to point intent compiler for a three switch linear
117 * topology.
118 *
119 * @param resourceService service to use for resource allocation requests
120 * @return point to point compiler
121 */
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800122 private PointToPointIntentCompiler makeCompiler(String[] hops, ResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800123 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
124 compiler.resourceService = resourceService;
125 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
126 return compiler;
127 }
128
129 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700130 * Tests a pair of devices in an 8 hop path, forward direction.
131 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800132 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700133 public void testForwardPathCompilation() {
134
135 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700136
137 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
138 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700139
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800140 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700141 assertThat(result, is(Matchers.notNullValue()));
142 assertThat(result, hasSize(1));
143 Intent forwardResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700144 assertThat(forwardResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700145
Pier Ventreffe88d62016-10-13 14:34:40 -0700146 if (forwardResultIntent instanceof LinkCollectionIntent) {
147 LinkCollectionIntent forwardIntent = (LinkCollectionIntent) forwardResultIntent;
148 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(connectPoint("d1", 1));
149 FilteredConnectPoint egressPoint = new FilteredConnectPoint(connectPoint("d8", 1));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700150 // 7 links for the hops, plus one default lnk on ingress and egress
Pier Ventreffe88d62016-10-13 14:34:40 -0700151 assertThat(forwardIntent.links(), hasSize(hops.length - 1));
152 assertThat(forwardIntent.links(), linksHasPath("d1", "d2"));
153 assertThat(forwardIntent.links(), linksHasPath("d2", "d3"));
154 assertThat(forwardIntent.links(), linksHasPath("d3", "d4"));
155 assertThat(forwardIntent.links(), linksHasPath("d4", "d5"));
156 assertThat(forwardIntent.links(), linksHasPath("d5", "d6"));
157 assertThat(forwardIntent.links(), linksHasPath("d6", "d7"));
158 assertThat(forwardIntent.links(), linksHasPath("d7", "d8"));
159 assertThat(forwardIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
160 assertThat(forwardIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700161 }
162 }
163
164 /**
165 * Tests a pair of devices in an 8 hop path, forward direction.
166 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800167 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700168 public void testReversePathCompilation() {
169
170 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700171
172 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
173 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700174
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800175 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700176 assertThat(result, is(Matchers.notNullValue()));
177 assertThat(result, hasSize(1));
178 Intent reverseResultIntent = result.get(0);
Pier Ventreffe88d62016-10-13 14:34:40 -0700179 assertThat(reverseResultIntent instanceof LinkCollectionIntent, is(true));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700180
Pier Ventreffe88d62016-10-13 14:34:40 -0700181 if (reverseResultIntent instanceof LinkCollectionIntent) {
182 LinkCollectionIntent reverseLinkCollectionIntent = (LinkCollectionIntent) reverseResultIntent;
183 FilteredConnectPoint egressPoint = new FilteredConnectPoint(connectPoint("d1", 1));
184 FilteredConnectPoint ingressPoint = new FilteredConnectPoint(connectPoint("d8", 1));
185 assertThat(reverseLinkCollectionIntent.links(), hasSize(hops.length - 1));
186 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d2", "d1"));
187 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d3", "d2"));
188 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d4", "d3"));
189 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d5", "d4"));
190 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d6", "d5"));
191 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d7", "d6"));
192 assertThat(reverseLinkCollectionIntent.links(), linksHasPath("d8", "d7"));
193 assertThat(reverseLinkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(ingressPoint)));
194 assertThat(reverseLinkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(egressPoint)));
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700195 }
196 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800197
198 /**
199 * Tests compilation of the intent which designates two different ports on the same switch.
200 */
201 @Test
202 public void testSameSwitchDifferentPortsIntentCompilation() {
Pier Ventreffe88d62016-10-13 14:34:40 -0700203 FilteredConnectPoint src = new FilteredConnectPoint(new ConnectPoint(deviceId("1"), portNumber(1)));
204 FilteredConnectPoint dst = new FilteredConnectPoint(new ConnectPoint(deviceId("1"), portNumber(2)));
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700205 PointToPointIntent intent = PointToPointIntent.builder()
206 .appId(APP_ID)
207 .selector(selector)
208 .treatment(treatment)
Pier Ventreffe88d62016-10-13 14:34:40 -0700209 .filteredIngressPoint(src)
210 .filteredEgressPoint(dst)
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700211 .build();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800212
213 String[] hops = {"1"};
214 PointToPointIntentCompiler sut = makeCompiler(hops);
215
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800216 List<Intent> compiled = sut.compile(intent, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800217
218 assertThat(compiled, hasSize(1));
Pier Ventreffe88d62016-10-13 14:34:40 -0700219 assertThat(compiled.get(0), is(instanceOf(LinkCollectionIntent.class)));
220 LinkCollectionIntent linkCollectionIntent = (LinkCollectionIntent) compiled.get(0);
221 Set<Link> links = linkCollectionIntent.links();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800222
Pier Ventreffe88d62016-10-13 14:34:40 -0700223 assertThat(links, hasSize(0));
224 assertThat(linkCollectionIntent.filteredIngressPoints(), is(ImmutableSet.of(src)));
225 assertThat(linkCollectionIntent.filteredEgressPoints(), is(ImmutableSet.of(dst)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800226 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800227
228 /**
229 * Tests that requests with sufficient available bandwidth succeed.
230 */
231 @Test
232 public void testBandwidthConstrainedIntentSuccess() {
233
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800234 final ResourceService resourceService =
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800235 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700236 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800237 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800238
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800239 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
240
241 String[] hops = {"s1", "s2", "s3"};
242 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
243
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800244 final List<Intent> compiledIntents = compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800245
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800246 assertThat(compiledIntents, Matchers.notNullValue());
247 assertThat(compiledIntents, hasSize(1));
248 }
249
250 /**
251 * Tests that requests with insufficient available bandwidth fail.
252 */
253 @Test
254 public void testBandwidthConstrainedIntentFailure() {
255
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800256 final ResourceService resourceService =
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800257 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700258 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800259 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800260
261 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800262 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
263
264 String[] hops = {"s1", "s2", "s3"};
265 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
266
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800267 compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800268
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800269 fail("Point to Point compilation with insufficient bandwidth does "
270 + "not throw exception.");
271 } catch (PathNotFoundException noPath) {
272 assertThat(noPath.getMessage(), containsString("No path"));
273 }
274 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700275}