blob: 0ae59037ebc659706e4e96a5dbbb3c91b571f7fd [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 */
Sho SHIMIZU3a7c98e2015-02-25 17:27:04 -080016package org.onosproject.net.intent.impl.installer;
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
Ray Milkey67c221f2015-02-25 08:34:55 -080074 private PathIntent createPathIntent(List<Link> links, List<Constraint> constraints) {
Sho SHIMIZUb7052172015-02-20 11:09:21 -080075 int hops = links.size() - 1;
Ray Milkey67c221f2015-02-25 08:34:55 -080076 return new PathIntent(APP_ID, selector, treatment,
77 new DefaultPath(PID, links, hops), constraints);
Ray Milkey8d3ce432014-11-07 16:21:10 -080078 }
79
80 /**
Ray Milkey8d3ce432014-11-07 16:21:10 -080081 * Tests that installation of bandwidth constrained path intents are
82 * successful.
83 */
84 @Test
85 public void testInstallBandwidthConstrainedIntentSuccess() {
86
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -080087 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
Ray Milkey8d3ce432014-11-07 16:21:10 -080088
Sho SHIMIZUb7052172015-02-20 11:09:21 -080089 List<Link> links = Arrays.asList(
90 createEdgeLink(d1p0, true),
91 new DefaultLink(PID, d1p1, d2p0, DIRECT),
92 new DefaultLink(PID, d2p1, d3p1, DIRECT),
93 createEdgeLink(d3p0, false)
94 );
95 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -080096
Sho SHIMIZUb7052172015-02-20 11:09:21 -080097 sut.resourceService = makeBandwidthResourceService(1000.0);
98
99 final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800100
101 assertThat(flowOperations, notNullValue());
102 assertThat(flowOperations, hasSize(1));
103 }
104
105 /**
106 * Tests that installation of bandwidth constrained path intents fail
107 * if there are no available resources.
108 */
109 @Test
110 public void testInstallBandwidthConstrainedIntentFailure() {
111
Sho SHIMIZU0ce220a2015-01-23 15:54:47 -0800112 final Constraint constraint = new BandwidthConstraint(Bandwidth.bps(100.0));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800113
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800114 List<Link> links = Arrays.asList(
115 createEdgeLink(d1p0, true),
116 new DefaultLink(PID, d1p1, d2p0, DIRECT),
117 new DefaultLink(PID, d2p1, d3p1, DIRECT),
118 createEdgeLink(d3p0, false)
119 );
120 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800121
122 // Make it look like the available bandwidth was consumed
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800123 final IntentTestsMocks.MockResourceService resourceService = makeBandwidthResourceService(1000.0);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800124 resourceService.setAvailableBandwidth(1.0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800125 sut.resourceService = resourceService;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800126
127 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800128 sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800129 fail("Bandwidth request with no available bandwidth did not fail.");
130 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
131 assertThat(failure,
132 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
133 }
134 }
135
136 /**
137 * Tests that installation of lambda constrained path intents are
138 * successful.
139 */
140 @Test
141 public void testInstallLambdaConstrainedIntentSuccess() {
142
Ray Milkey8d3ce432014-11-07 16:21:10 -0800143 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
144
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800145 List<Link> links = Arrays.asList(
146 createEdgeLink(d1p0, true),
147 new DefaultLink(PID, d1p1, d2p0, DIRECT),
148 new DefaultLink(PID, d2p1, d3p1, DIRECT),
149 createEdgeLink(d3p0, false)
150 );
151 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800152
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800153 sut.resourceService = makeLambdaResourceService(1);
154
155 final List<Collection<FlowRuleOperation>> flowOperations = sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800156
157 assertThat(flowOperations, notNullValue());
158 assertThat(flowOperations, hasSize(1));
159 }
160
161 /**
162 * Tests that installation of lambda constrained path intents fail
163 * if there are no available resources.
164 */
165 @Test
166 public void testInstallLambdaConstrainedIntentFailure() {
167
Ray Milkey8d3ce432014-11-07 16:21:10 -0800168 final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
169
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800170 List<Link> links = Arrays.asList(
171 createEdgeLink(d1p0, true),
172 new DefaultLink(PID, d1p1, d2p0, DIRECT),
173 new DefaultLink(PID, d2p1, d3p1, DIRECT),
174 createEdgeLink(d3p0, false)
175 );
176 PathIntent installable = createPathIntent(links, Arrays.asList(constraint));
Ray Milkey8d3ce432014-11-07 16:21:10 -0800177
178 // Make it look like the available lambda was consumed
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800179 final IntentTestsMocks.MockResourceService resourceService = makeLambdaResourceService(1);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800180 resourceService.setAvailableLambda(0);
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800181 sut.resourceService = resourceService;
Ray Milkey8d3ce432014-11-07 16:21:10 -0800182
183 try {
Sho SHIMIZUb7052172015-02-20 11:09:21 -0800184 sut.install(installable);
Ray Milkey8d3ce432014-11-07 16:21:10 -0800185 fail("Lambda request with no available lambda did not fail.");
186 } catch (IntentTestsMocks.MockedAllocationFailure failure) {
187 assertThat(failure,
188 instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
189 }
190 }
191
192}