blob: 9c83572abc94af8859a49949ea32e9fd3dc16c4f [file] [log] [blame]
Ray Milkey8d3ce432014-11-07 16:21:10 -08001/*
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.impl;
Ray Milkey8d3ce432014-11-07 16:21:10 -080017
Sho SHIMIZUb7052172015-02-20 11:09:21 -080018import org.junit.Before;
Ray Milkey8d3ce432014-11-07 16:21:10 -080019import org.junit.Test;
Sho SHIMIZUb7052172015-02-20 11:09:21 -080020import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DefaultLink;
22import org.onosproject.net.DefaultPath;
23import org.onosproject.net.Link;
Ray Milkey71ade562015-02-18 15:08:07 -080024import org.onosproject.net.flow.FlowRuleOperation;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.intent.AbstractIntentTest;
26import org.onosproject.net.intent.Constraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.intent.IntentTestsMocks;
28import org.onosproject.net.intent.PathIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.intent.constraint.BandwidthConstraint;
30import org.onosproject.net.intent.constraint.LambdaConstraint;
31import org.onosproject.net.resource.Bandwidth;
32import org.onosproject.net.resource.Lambda;
Ray Milkey8d3ce432014-11-07 16:21:10 -080033
Sho SHIMIZUb7052172015-02-20 11:09:21 -080034import java.util.Arrays;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080035import java.util.Collection;
Brian O'Connor64a0369d2015-02-20 22:02:59 -080036import java.util.List;
37
Ray Milkey8d3ce432014-11-07 16:21:10 -080038import static org.hamcrest.MatcherAssert.assertThat;
Sho SHIMIZUb7052172015-02-20 11:09:21 -080039import static org.hamcrest.Matchers.hasSize;
40import static org.hamcrest.Matchers.instanceOf;
41import static org.hamcrest.Matchers.notNullValue;
Ray Milkey8d3ce432014-11-07 16:21:10 -080042import static org.junit.Assert.fail;
Sho SHIMIZUb7052172015-02-20 11:09:21 -080043import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
44import static org.onosproject.net.Link.Type.DIRECT;
Brian O'Connorabafb502014-12-02 22:26:20 -080045import static org.onosproject.net.NetTestTools.APP_ID;
Sho SHIMIZUb7052172015-02-20 11:09:21 -080046import static org.onosproject.net.NetTestTools.PID;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import static org.onosproject.net.NetTestTools.connectPoint;
Sho SHIMIZUb7052172015-02-20 11:09:21 -080048import static org.onosproject.net.intent.IntentTestsMocks.MockResourceService.makeBandwidthResourceService;
49import static org.onosproject.net.intent.IntentTestsMocks.MockResourceService.makeLambdaResourceService;
Ray Milkey8d3ce432014-11-07 16:21:10 -080050
51/**
52 * Unit tests for calculating paths for intents with constraints.
53 */
54
Brian O'Connor520c0522014-11-23 23:50:47 -080055public class PathConstraintCalculationTest extends AbstractIntentTest {
Ray Milkey8d3ce432014-11-07 16:21:10 -080056
Sho SHIMIZUb7052172015-02-20 11:09:21 -080057 private final IntentTestsMocks.MockSelector selector = new IntentTestsMocks.MockSelector();
58 private final IntentTestsMocks.MockTreatment treatment = new IntentTestsMocks.MockTreatment();
59 private final ConnectPoint d1p1 = connectPoint("s1", 0);
60 private final ConnectPoint d2p0 = connectPoint("s2", 0);
61 private final ConnectPoint d2p1 = connectPoint("s2", 1);
62 private final ConnectPoint d3p1 = connectPoint("s3", 1);
63 private final ConnectPoint d3p0 = connectPoint("s3", 10);
64 private final ConnectPoint d1p0 = connectPoint("s1", 10);
65
66 private PathIntentInstaller sut;
67
68 @Before
69 public void setUpIntentInstaller() {
70 sut = new PathIntentInstaller();
71 sut.appId = APP_ID;
Ray Milkey8d3ce432014-11-07 16:21:10 -080072 }
73
Sho SHIMIZUb7052172015-02-20 11:09:21 -080074 private PathIntent createPathIntent(List<Link> links, List<Constraint> constratins) {
75 int hops = links.size() - 1;
76 return new PathIntent(APP_ID, selector, treatment, new DefaultPath(PID, links, hops), constratins);
Ray Milkey8d3ce432014-11-07 16:21:10 -080077 }
78
79 /**
Ray Milkey8d3ce432014-11-07 16:21:10 -080080 * Tests that installation of bandwidth constrained path intents are
81 * successful.
82 */
83 @Test
84 public void testInstallBandwidthConstrainedIntentSuccess() {
85
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080086 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
Ray Milkey8d3ce432014-11-07 16:21:10 -080087
Sho SHIMIZUb7052172015-02-20 11:09:21 -080088 List<Link> links = Arrays.asList(
89 createEdgeLink(d1p0, true),
90 new DefaultLink(PID, d1p1, d2p0, DIRECT),
91 new DefaultLink(PID, d2p1, d3p1, DIRECT),
92 createEdgeLink(d3p0, false)
93 );
94 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -080095
Sho SHIMIZUb7052172015-02-20 11:09:21 -080096 sut.resourceService = makeBandwidthResourceService(1000.0);
97
98 final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -080099
100 assertThat(flowOperations, notNullValue());
101 assertThat(flowOperations, hasSize(1));
102 }
103
104 /**
105 * Tests that installation of bandwidth constrained path intents fail
106 * if there are no available resources.
107 */
108 @Test
109 public void testInstallBandwidthConstrainedIntentFailure() {
110
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800111 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800112
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800113 List<Link> links = Arrays.asList(
114 createEdgeLink(d1p0, true),
115 new DefaultLink(PID, d1p1, d2p0, DIRECT),
116 new DefaultLink(PID, d2p1, d3p1, DIRECT),
117 createEdgeLink(d3p0, false)
118 );
119 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800120
121 // Make it look like the available bandwidth was consumed
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800122 final IntentTestsMocks.MockResourceService resourceService = makeBandwidthResourceService(1000.0);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800123 resourceService.setAvailableBandwidth(1.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800124 sut.resourceService = resourceService;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800125
126 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800127 sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800128 fail("Bandwidth request with no available bandwidth did not fail.");
129 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
130 assertThat(failure,
131 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
132 }
133 }
134
135 /**
136 * Tests that installation of lambda constrained path intents are
137 * successful.
138 */
139 @Test
140 public void testInstallLambdaConstrainedIntentSuccess() {
141
Ray Milkey8d3ce432014-11-07 16:21:10 -0800142 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
143
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800144 List<Link> links = Arrays.asList(
145 createEdgeLink(d1p0, true),
146 new DefaultLink(PID, d1p1, d2p0, DIRECT),
147 new DefaultLink(PID, d2p1, d3p1, DIRECT),
148 createEdgeLink(d3p0, false)
149 );
150 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800151
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800152 sut.resourceService = makeLambdaResourceService(1);
153
154 final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800155
156 assertThat(flowOperations, notNullValue());
157 assertThat(flowOperations, hasSize(1));
158 }
159
160 /**
161 * Tests that installation of lambda constrained path intents fail
162 * if there are no available resources.
163 */
164 @Test
165 public void testInstallLambdaConstrainedIntentFailure() {
166
Ray Milkey8d3ce432014-11-07 16:21:10 -0800167 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
168
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800169 List<Link> links = Arrays.asList(
170 createEdgeLink(d1p0, true),
171 new DefaultLink(PID, d1p1, d2p0, DIRECT),
172 new DefaultLink(PID, d2p1, d3p1, DIRECT),
173 createEdgeLink(d3p0, false)
174 );
175 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800176
177 // Make it look like the available lambda was consumed
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800178 final IntentTestsMocks.MockResourceService resourceService = makeLambdaResourceService(1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800179 resourceService.setAvailableLambda(0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800180 sut.resourceService = resourceService;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800181
182 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800183 sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800184 fail("Lambda request with no available lambda did not fail.");
185 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
186 assertThat(failure,
187 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
188 }
189 }
190
191}