e57d9dbeeb694e635e47eceb25b71bb20563f4cf
[onosfw.git] /
1 /*
2  * Copyright 2014-2015 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  */
16 package org.onosproject.net.intent.impl.compiler;
17
18 import org.hamcrest.Matchers;
19 import org.junit.Test;
20 import org.onlab.util.Bandwidth;
21 import org.onosproject.TestApplicationId;
22 import org.onosproject.core.ApplicationId;
23 import org.onosproject.net.ConnectPoint;
24 import org.onosproject.net.Link;
25 import org.onosproject.net.Path;
26 import org.onosproject.net.flow.TrafficSelector;
27 import org.onosproject.net.flow.TrafficTreatment;
28 import org.onosproject.net.intent.AbstractIntentTest;
29 import org.onosproject.net.intent.Constraint;
30 import org.onosproject.net.intent.Intent;
31 import org.onosproject.net.intent.IntentTestsMocks;
32 import org.onosproject.net.intent.PathIntent;
33 import org.onosproject.net.intent.PointToPointIntent;
34 import org.onosproject.net.intent.constraint.BandwidthConstraint;
35 import org.onosproject.net.intent.constraint.LambdaConstraint;
36 import org.onosproject.net.intent.impl.PathNotFoundException;
37 import org.onosproject.net.resource.link.BandwidthResource;
38 import org.onosproject.net.resource.link.LambdaResource;
39 import org.onosproject.net.resource.link.LinkResourceService;
40
41 import java.util.Collections;
42 import java.util.List;
43
44 import static org.hamcrest.CoreMatchers.instanceOf;
45 import static org.hamcrest.MatcherAssert.assertThat;
46 import static org.hamcrest.Matchers.containsString;
47 import static org.hamcrest.Matchers.hasSize;
48 import static org.hamcrest.Matchers.is;
49 import static org.junit.Assert.fail;
50 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
51 import static org.onosproject.net.DeviceId.deviceId;
52 import static org.onosproject.net.NetTestTools.APP_ID;
53 import static org.onosproject.net.NetTestTools.connectPoint;
54 import static org.onosproject.net.PortNumber.portNumber;
55 import static org.onosproject.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
56
57 /**
58  * Unit tests for the HostToHost intent compiler.
59  */
60 public class PointToPointIntentCompilerTest extends AbstractIntentTest {
61
62     private static final ApplicationId APPID = new TestApplicationId("foo");
63
64     private TrafficSelector selector = new IntentTestsMocks.MockSelector();
65     private TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
66
67     /**
68      * Creates a PointToPoint intent based on ingress and egress device Ids.
69      *
70      * @param ingressIdString string for id of ingress device
71      * @param egressIdString  string for id of egress device
72      * @return PointToPointIntent for the two devices
73      */
74     private PointToPointIntent makeIntent(String ingressIdString,
75                                           String egressIdString) {
76         return PointToPointIntent.builder()
77                 .appId(APPID)
78                 .selector(selector)
79                 .treatment(treatment)
80                 .ingressPoint(connectPoint(ingressIdString, 1))
81                 .egressPoint(connectPoint(egressIdString, 1))
82                 .build();
83     }
84
85     /**
86      * Creates a PointToPoint intent based on ingress and egress deviceIds and constraints.
87      *
88      * @param ingressIdString string for id of ingress device
89      * @param egressIdString  string for id of egress device
90      * @param constraints     constraints
91      * @return PointToPointIntent for the two device with constraints
92      */
93     private PointToPointIntent makeIntent(String ingressIdString,
94                                           String egressIdString, List<Constraint> constraints) {
95         return PointToPointIntent.builder()
96                 .appId(APPID)
97                 .selector(selector)
98                 .treatment(treatment)
99                 .ingressPoint(connectPoint(ingressIdString, 1))
100                 .egressPoint(connectPoint(egressIdString, 1))
101                 .constraints(constraints)
102                 .build();
103     }
104
105     /**
106      * Creates a compiler for HostToHost intents.
107      *
108      * @param hops string array describing the path hops to use when compiling
109      * @return HostToHost intent compiler
110      */
111     private PointToPointIntentCompiler makeCompiler(String[] hops) {
112         PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
113         compiler.pathService = new IntentTestsMocks.MockPathService(hops);
114         return compiler;
115     }
116
117     /**
118      * Creates a point to point intent compiler for a three switch linear
119      * topology.
120      *
121      * @param resourceService service to use for resource allocation requests
122      * @return point to point compiler
123      */
124     private PointToPointIntentCompiler makeCompiler(String[] hops, LinkResourceService resourceService) {
125         final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
126         compiler.resourceService = resourceService;
127         compiler.pathService = new IntentTestsMocks.MockPathService(hops);
128         return compiler;
129     }
130
131     /**
132      * Tests a pair of devices in an 8 hop path, forward direction.
133      */
134     @Test
135     public void testForwardPathCompilation() {
136
137         PointToPointIntent intent = makeIntent("d1", "d8");
138
139         String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
140         PointToPointIntentCompiler compiler = makeCompiler(hops);
141
142         List<Intent> result = compiler.compile(intent, null, null);
143         assertThat(result, is(Matchers.notNullValue()));
144         assertThat(result, hasSize(1));
145         Intent forwardResultIntent = result.get(0);
146         assertThat(forwardResultIntent instanceof PathIntent, is(true));
147
148         if (forwardResultIntent instanceof PathIntent) {
149             PathIntent forwardPathIntent = (PathIntent) forwardResultIntent;
150             // 7 links for the hops, plus one default lnk on ingress and egress
151             assertThat(forwardPathIntent.path().links(), hasSize(hops.length + 1));
152             assertThat(forwardPathIntent.path().links(), linksHasPath("d1", "d2"));
153             assertThat(forwardPathIntent.path().links(), linksHasPath("d2", "d3"));
154             assertThat(forwardPathIntent.path().links(), linksHasPath("d3", "d4"));
155             assertThat(forwardPathIntent.path().links(), linksHasPath("d4", "d5"));
156             assertThat(forwardPathIntent.path().links(), linksHasPath("d5", "d6"));
157             assertThat(forwardPathIntent.path().links(), linksHasPath("d6", "d7"));
158             assertThat(forwardPathIntent.path().links(), linksHasPath("d7", "d8"));
159         }
160     }
161
162     /**
163      * Tests a pair of devices in an 8 hop path, forward direction.
164      */
165     @Test
166     public void testReversePathCompilation() {
167
168         PointToPointIntent intent = makeIntent("d8", "d1");
169
170         String[] hops = {"d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8"};
171         PointToPointIntentCompiler compiler = makeCompiler(hops);
172
173         List<Intent> result = compiler.compile(intent, null, null);
174         assertThat(result, is(Matchers.notNullValue()));
175         assertThat(result, hasSize(1));
176         Intent reverseResultIntent = result.get(0);
177         assertThat(reverseResultIntent instanceof PathIntent, is(true));
178
179         if (reverseResultIntent instanceof PathIntent) {
180             PathIntent reversePathIntent = (PathIntent) reverseResultIntent;
181             assertThat(reversePathIntent.path().links(), hasSize(hops.length + 1));
182             assertThat(reversePathIntent.path().links(), linksHasPath("d2", "d1"));
183             assertThat(reversePathIntent.path().links(), linksHasPath("d3", "d2"));
184             assertThat(reversePathIntent.path().links(), linksHasPath("d4", "d3"));
185             assertThat(reversePathIntent.path().links(), linksHasPath("d5", "d4"));
186             assertThat(reversePathIntent.path().links(), linksHasPath("d6", "d5"));
187             assertThat(reversePathIntent.path().links(), linksHasPath("d7", "d6"));
188             assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
189         }
190     }
191
192     /**
193      * Tests compilation of the intent which designates two different ports on the same switch.
194      */
195     @Test
196     public void testSameSwitchDifferentPortsIntentCompilation() {
197         ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
198         ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
199         PointToPointIntent intent = PointToPointIntent.builder()
200                 .appId(APP_ID)
201                 .selector(selector)
202                 .treatment(treatment)
203                 .ingressPoint(src)
204                 .egressPoint(dst)
205                 .build();
206
207         String[] hops = {"1"};
208         PointToPointIntentCompiler sut = makeCompiler(hops);
209
210         List<Intent> compiled = sut.compile(intent, null, null);
211
212         assertThat(compiled, hasSize(1));
213         assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
214         Path path = ((PathIntent) compiled.get(0)).path();
215
216         assertThat(path.links(), hasSize(2));
217         Link firstLink = path.links().get(0);
218         assertThat(firstLink, is(createEdgeLink(src, true)));
219         Link secondLink = path.links().get(1);
220         assertThat(secondLink, is(createEdgeLink(dst, false)));
221     }
222
223     /**
224      * Tests that requests with sufficient available bandwidth succeed.
225      */
226     @Test
227     public void testBandwidthConstrainedIntentSuccess() {
228
229         final LinkResourceService resourceService =
230                 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
231         final List<Constraint> constraints =
232                 Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
233
234         final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
235
236         String[] hops = {"s1", "s2", "s3"};
237         final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
238
239         final List<Intent> compiledIntents = compiler.compile(intent, null, null);
240
241         assertThat(compiledIntents, Matchers.notNullValue());
242         assertThat(compiledIntents, hasSize(1));
243     }
244
245     /**
246      * Tests that requests with insufficient available bandwidth fail.
247      */
248     @Test
249     public void testBandwidthConstrainedIntentFailure() {
250
251         final LinkResourceService resourceService =
252                 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
253         final List<Constraint> constraints =
254                 Collections.singletonList(new BandwidthConstraint(new BandwidthResource(Bandwidth.bps(100.0))));
255
256         try {
257             final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
258
259             String[] hops = {"s1", "s2", "s3"};
260             final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
261
262             compiler.compile(intent, null, null);
263
264             fail("Point to Point compilation with insufficient bandwidth does "
265                     + "not throw exception.");
266         } catch (PathNotFoundException noPath) {
267             assertThat(noPath.getMessage(), containsString("No path"));
268         }
269     }
270
271     /**
272      * Tests that requests for available lambdas are successful.
273      */
274     @Test
275     public void testLambdaConstrainedIntentSuccess() {
276
277         final List<Constraint> constraints =
278                 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
279         final LinkResourceService resourceService =
280                 IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
281
282         final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
283
284         String[] hops = {"s1", "s2", "s3"};
285         final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
286
287         final List<Intent> compiledIntents =
288                 compiler.compile(intent, null, null);
289
290         assertThat(compiledIntents, Matchers.notNullValue());
291         assertThat(compiledIntents, hasSize(1));
292     }
293
294     /**
295      * Tests that requests for lambdas when there are no available lambdas
296      * fail.
297      */
298     @Test
299     public void testLambdaConstrainedIntentFailure() {
300
301         final List<Constraint> constraints =
302                 Collections.singletonList(new LambdaConstraint(LambdaResource.valueOf(1)));
303         final LinkResourceService resourceService =
304                 IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
305         try {
306             final PointToPointIntent intent = makeIntent("s1", "s3", constraints);
307
308             String[] hops = {"s1", "s2", "s3"};
309             final PointToPointIntentCompiler compiler = makeCompiler(hops, resourceService);
310
311             compiler.compile(intent, null, null);
312
313             fail("Point to Point compilation with no available lambda does "
314                     + "not throw exception.");
315         } catch (PathNotFoundException noPath) {
316             assertThat(noPath.getMessage(), containsString("No path"));
317         }
318     }
319
320 }