blob: 3a3648ab8c23d2fc9bb4393201e359f0b30d376e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070018import org.hamcrest.Matchers;
Ray Milkey40f50b92014-11-07 13:25:53 -080019import org.junit.Test;
Sho SHIMIZU6d01d3d2015-05-08 14:08:36 -070020import org.onlab.util.Bandwidth;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.TestApplicationId;
22import org.onosproject.core.ApplicationId;
23import org.onosproject.net.ConnectPoint;
24import org.onosproject.net.Link;
25import org.onosproject.net.Path;
26import org.onosproject.net.flow.TrafficSelector;
27import org.onosproject.net.flow.TrafficTreatment;
28import org.onosproject.net.intent.AbstractIntentTest;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080029import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentTestsMocks;
32import org.onosproject.net.intent.PathIntent;
33import org.onosproject.net.intent.PointToPointIntent;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080034import org.onosproject.net.intent.constraint.BandwidthConstraint;
Sho SHIMIZU6c28f832015-02-20 16:12:19 -080035import org.onosproject.net.intent.impl.PathNotFoundException;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080036import org.onosproject.net.newresource.ResourceService;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070037
Sho SHIMIZU98ffca82015-05-11 08:39:24 -070038import java.util.Collections;
Thomas Vachuskab97cf282014-10-20 23:31:12 -070039import java.util.List;
40
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -080041import static org.hamcrest.CoreMatchers.instanceOf;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070042import static org.hamcrest.MatcherAssert.assertThat;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080043import static org.hamcrest.Matchers.containsString;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070044import static org.hamcrest.Matchers.hasSize;
45import static org.hamcrest.Matchers.is;
Sho SHIMIZU0e738802015-02-20 10:23:28 -080046import static org.junit.Assert.fail;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
48import static org.onosproject.net.DeviceId.deviceId;
49import static org.onosproject.net.NetTestTools.APP_ID;
50import static org.onosproject.net.NetTestTools.connectPoint;
51import static org.onosproject.net.PortNumber.portNumber;
52import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070053
54/**
55 * Unit tests for the HostToHost intent compiler.
56 */
Ray Milkey37f6a382014-11-25 14:54:42 -080057public class PointToPointIntentCompilerTest extends AbstractIntentTest {
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070058
Thomas Vachuskab97cf282014-10-20 23:31:12 -070059 private static final ApplicationId APPID = new TestApplicationId("foo");
60
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070061 private TrafficSelector selector = new IntentTestsMocks.MockSelector();
62 private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
63
64 /**
65 * Creates a PointToPoint intent based on ingress and egress device Ids.
66 *
67 * @param ingressIdString string for id of ingress device
Thomas Vachuskab97cf282014-10-20 23:31:12 -070068 * @param egressIdString string for id of egress device
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070069 * @return PointToPointIntent for the two devices
70 */
71 private PointToPointIntent makeIntent(String ingressIdString,
72 String egressIdString) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070073 return PointToPointIntent.builder()
74 .appId(APPID)
75 .selector(selector)
76 .treatment(treatment)
77 .ingressPoint(connectPoint(ingressIdString, 1))
78 .egressPoint(connectPoint(egressIdString, 1))
79 .build();
Ray Milkeyc0fa4db2014-10-17 08:49:54 -070080 }
81
82 /**
Sho SHIMIZUb7052172015-02-20 11:09:21 -080083 * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
84 *
85 * @param ingressIdString string for id of ingress device
86 * @param egressIdString string for id of egress device
87 * @param constraints constraints
88 * @return PointToPointIntent for the two device with constraints
89 */
90 private PointToPointIntent makeIntent(String ingressIdString,
91 String egressIdString, List<Constraint> constraints) {
Ray Milkey3e3ec5f2015-03-17 17:00:38 -070092 return PointToPointIntent.builder()
93 .appId(APPID)
94 .selector(selector)
95 .treatment(treatment)
96 .ingressPoint(connectPoint(ingressIdString, 1))
97 .egressPoint(connectPoint(egressIdString, 1))
98 .constraints(constraints)
99 .build();
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800100 }
101
102 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700103 * Creates a compiler for HostToHost intents.
104 *
105 * @param hops string array describing the path hops to use when compiling
106 * @return HostToHost intent compiler
107 */
108 private PointToPointIntentCompiler makeCompiler(String[] hops) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800109 PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
Ray Milkey40f50b92014-11-07 13:25:53 -0800110 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700111 return compiler;
112 }
113
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800114 /**
115 * Creates a point to point intent compiler for a three switch linear
116 * topology.
117 *
118 * @param resourceService service to use for resource allocation requests
119 * @return point to point compiler
120 */
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800121 private PointToPointIntentCompiler makeCompiler(String[] hops, ResourceService resourceService) {
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800122 final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
123 compiler.resourceService = resourceService;
124 compiler.pathService = new IntentTestsMocks.MockPathService(hops);
125 return compiler;
126 }
127
128 /**
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700129 * Tests a pair of devices in an 8 hop path, forward direction.
130 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800131 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700132 public void testForwardPathCompilation() {
133
134 PointToPointIntent intent = makeIntent("d1", "d8");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700135
136 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
137 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700138
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800139 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700140 assertThat(result, is(Matchers.notNullValue()));
141 assertThat(result, hasSize(1));
142 Intent forwardResultIntent = result.get(0);
143 assertThat(forwardResultIntent instanceof PathIntent, is(true));
144
145 if (forwardResultIntent instanceof PathIntent) {
146 PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
147 // 7 links for the hops, plus one default lnk on ingress and egress
148 assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
149 assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
150 assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
151 assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
152 assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
153 assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
154 assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
155 assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
156 }
157 }
158
159 /**
160 * Tests a pair of devices in an 8 hop path, forward direction.
161 */
Ray Milkey40f50b92014-11-07 13:25:53 -0800162 @Test
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700163 public void testReversePathCompilation() {
164
165 PointToPointIntent intent = makeIntent("d8", "d1");
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700166
167 String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
168 PointToPointIntentCompiler compiler = makeCompiler(hops);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700169
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800170 List<Intent> result = compiler.compile(intent, null);
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700171 assertThat(result, is(Matchers.notNullValue()));
172 assertThat(result, hasSize(1));
173 Intent reverseResultIntent = result.get(0);
174 assertThat(reverseResultIntent instanceof PathIntent, is(true));
175
176 if (reverseResultIntent instanceof PathIntent) {
177 PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
178 assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
179 assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
180 assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
181 assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
182 assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
183 assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
184 assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
185 assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
186 }
187 }
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800188
189 /**
190 * Tests compilation of the intent which designates two different ports on the same switch.
191 */
192 @Test
193 public void testSameSwitchDifferentPortsIntentCompilation() {
194 ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
195 ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
Ray Milkey3e3ec5f2015-03-17 17:00:38 -0700196 PointToPointIntent intent = PointToPointIntent.builder()
197 .appId(APP_ID)
198 .selector(selector)
199 .treatment(treatment)
200 .ingressPoint(src)
201 .egressPoint(dst)
202 .build();
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800203
204 String[] hops = {"1"};
205 PointToPointIntentCompiler sut = makeCompiler(hops);
206
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800207 List<Intent> compiled = sut.compile(intent, null);
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800208
209 assertThat(compiled, hasSize(1));
210 assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
211 Path path = ((PathIntent) compiled.get(0)).path();
212
Sho SHIMIZU3908fde2014-11-19 16:30:22 -0800213 assertThat(path.links(), hasSize(2));
214 Link firstLink = path.links().get(0);
215 assertThat(firstLink, is(createEdgeLink(src, true)));
216 Link secondLink = path.links().get(1);
217 assertThat(secondLink, is(createEdgeLink(dst, false)));
Sho SHIMIZUde8e6b52014-11-13 11:23:17 -0800218 }
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800219
220 /**
221 * Tests that requests with sufficient available bandwidth succeed.
222 */
223 @Test
224 public void testBandwidthConstrainedIntentSuccess() {
225
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800226 final ResourceService resourceService =
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800227 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700228 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800229 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800230
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800231 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
232
233 String[] hops = {"s1", "s2", "s3"};
234 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
235
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800236 final List<Intent> compiledIntents = compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800237
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800238 assertThat(compiledIntents, Matchers.notNullValue());
239 assertThat(compiledIntents, hasSize(1));
240 }
241
242 /**
243 * Tests that requests with insufficient available bandwidth fail.
244 */
245 @Test
246 public void testBandwidthConstrainedIntentFailure() {
247
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -0800248 final ResourceService resourceService =
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800249 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
Sho SHIMIZU98ffca82015-05-11 08:39:24 -0700250 final List<Constraint> constraints =
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800251 Collections.singletonList(new BandwidthConstraint(Bandwidth.bps(100.0)));
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800252
253 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800254 final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
255
256 String[] hops = {"s1", "s2", "s3"};
257 final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
258
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800259 compiler.compile(intent, null);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800260
Sho SHIMIZU0e738802015-02-20 10:23:28 -0800261 fail("Point to Point compilation with insufficient bandwidth does "
262 + "not throw exception.");
263 } catch (PathNotFoundException noPath) {
264 assertThat(noPath.getMessage(), containsString("No path"));
265 }
266 }
Ray Milkeyc0fa4db2014-10-17 08:49:54 -0700267}