Ray Milkey

Unit Tests for Bandwidth and Lambda allocations

These tests are for checking the valid path calculation logic
in the intent compilers and the PathIntent installer.

Change-Id: I2986729ae27202a2f42a71e64a53026383ddfb0b
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
15 */ 15 */
16 package org.onlab.onos.net; 16 package org.onlab.onos.net;
17 17
18 +import org.onlab.onos.TestApplicationId;
19 +import org.onlab.onos.core.ApplicationId;
18 import org.onlab.onos.net.provider.ProviderId; 20 import org.onlab.onos.net.provider.ProviderId;
19 import org.onlab.packet.ChassisId; 21 import org.onlab.packet.ChassisId;
20 import org.onlab.packet.IpAddress; 22 import org.onlab.packet.IpAddress;
...@@ -39,6 +41,7 @@ public final class NetTestTools { ...@@ -39,6 +41,7 @@ public final class NetTestTools {
39 } 41 }
40 42
41 public static final ProviderId PID = new ProviderId("of", "foo"); 43 public static final ProviderId PID = new ProviderId("of", "foo");
44 + public static final ApplicationId APP_ID = new TestApplicationId("foo");
42 45
43 // Short-hand for producing a device id from a string 46 // Short-hand for producing a device id from a string
44 public static DeviceId did(String id) { 47 public static DeviceId did(String id) {
......
...@@ -67,7 +67,7 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> { ...@@ -67,7 +67,7 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> {
67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 67 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
68 protected LinkResourceService resourceService; 68 protected LinkResourceService resourceService;
69 69
70 - private ApplicationId appId; 70 + protected ApplicationId appId;
71 71
72 @Activate 72 @Activate
73 public void activate() { 73 public void activate() {
......
...@@ -16,23 +16,39 @@ ...@@ -16,23 +16,39 @@
16 package org.onlab.onos.net.intent; 16 package org.onlab.onos.net.intent;
17 17
18 import static org.onlab.onos.net.NetTestTools.createPath; 18 import static org.onlab.onos.net.NetTestTools.createPath;
19 +import static org.onlab.onos.net.NetTestTools.link;
19 20
20 import java.util.ArrayList; 21 import java.util.ArrayList;
21 import java.util.Arrays; 22 import java.util.Arrays;
23 +import java.util.Collection;
22 import java.util.Collections; 24 import java.util.Collections;
23 import java.util.HashSet; 25 import java.util.HashSet;
26 +import java.util.LinkedList;
24 import java.util.List; 27 import java.util.List;
25 import java.util.Set; 28 import java.util.Set;
26 29
30 +import org.onlab.onos.net.DeviceId;
27 import org.onlab.onos.net.ElementId; 31 import org.onlab.onos.net.ElementId;
32 +import org.onlab.onos.net.Link;
28 import org.onlab.onos.net.Path; 33 import org.onlab.onos.net.Path;
29 import org.onlab.onos.net.flow.TrafficSelector; 34 import org.onlab.onos.net.flow.TrafficSelector;
30 import org.onlab.onos.net.flow.TrafficTreatment; 35 import org.onlab.onos.net.flow.TrafficTreatment;
31 import org.onlab.onos.net.flow.criteria.Criterion; 36 import org.onlab.onos.net.flow.criteria.Criterion;
32 import org.onlab.onos.net.flow.criteria.Criterion.Type; 37 import org.onlab.onos.net.flow.criteria.Criterion.Type;
33 import org.onlab.onos.net.flow.instructions.Instruction; 38 import org.onlab.onos.net.flow.instructions.Instruction;
39 +import org.onlab.onos.net.resource.BandwidthResourceRequest;
40 +import org.onlab.onos.net.resource.LambdaResourceRequest;
41 +import org.onlab.onos.net.resource.LinkResourceAllocations;
42 +import org.onlab.onos.net.resource.LinkResourceRequest;
43 +import org.onlab.onos.net.resource.LinkResourceService;
44 +import org.onlab.onos.net.resource.ResourceAllocation;
45 +import org.onlab.onos.net.resource.ResourceRequest;
46 +import org.onlab.onos.net.resource.ResourceType;
47 +import org.onlab.onos.net.topology.DefaultTopologyEdge;
48 +import org.onlab.onos.net.topology.DefaultTopologyVertex;
34 import org.onlab.onos.net.topology.LinkWeight; 49 import org.onlab.onos.net.topology.LinkWeight;
35 import org.onlab.onos.net.topology.PathService; 50 import org.onlab.onos.net.topology.PathService;
51 +import org.onlab.onos.net.topology.TopologyVertex;
36 52
37 /** 53 /**
38 * Common mocks used by the intent framework tests. 54 * Common mocks used by the intent framework tests.
...@@ -101,7 +117,158 @@ public class IntentTestsMocks { ...@@ -101,7 +117,158 @@ public class IntentTestsMocks {
101 117
102 @Override 118 @Override
103 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) { 119 public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
104 - return getPaths(src, dst); 120 + final Set<Path> paths = getPaths(src, dst);
121 +
122 + for (Path path : paths) {
123 + final DeviceId srcDevice = path.src().deviceId();
124 + final DeviceId dstDevice = path.dst().deviceId();
125 + final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
126 + final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
127 + final Link link = link(src.toString(), 1, dst.toString(), 1);
128 +
129 + final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
130 + if (weightValue < 0) {
131 + return new HashSet<>();
132 + }
133 + }
134 + return paths;
135 + }
136 + }
137 +
138 + public static class MockLinkResourceAllocations implements LinkResourceAllocations {
139 + @Override
140 + public Set<ResourceAllocation> getResourceAllocation(Link link) {
141 + return null;
142 + }
143 +
144 + @Override
145 + public IntentId intendId() {
146 + return null;
147 + }
148 +
149 + @Override
150 + public Collection<Link> links() {
151 + return null;
152 + }
153 +
154 + @Override
155 + public Set<ResourceRequest> resources() {
156 + return null;
157 + }
158 +
159 + @Override
160 + public ResourceType type() {
161 + return null;
162 + }
163 + }
164 +
165 + public static class MockedAllocationFailure extends RuntimeException { }
166 +
167 + public static class MockResourceService implements LinkResourceService {
168 +
169 + double availableBandwidth = -1.0;
170 + int availableLambda = -1;
171 +
172 + /**
173 + * Allocates a resource service that will allow bandwidth allocations
174 + * up to a limit.
175 + *
176 + * @param bandwidth available bandwidth limit
177 + * @return resource manager for bandwidth requests
178 + */
179 + public static MockResourceService makeBandwidthResourceService(double bandwidth) {
180 + final MockResourceService result = new MockResourceService();
181 + result.availableBandwidth = bandwidth;
182 + return result;
183 + }
184 +
185 + /**
186 + * Allocates a resource service that will allow lambda allocations.
187 + *
188 + * @param lambda Lambda to return for allocation requests. Currently unused
189 + * @return resource manager for lambda requests
190 + */
191 + public static MockResourceService makeLambdaResourceService(int lambda) {
192 + final MockResourceService result = new MockResourceService();
193 + result.availableLambda = lambda;
194 + return result;
195 + }
196 +
197 + public void setAvailableBandwidth(double availableBandwidth) {
198 + this.availableBandwidth = availableBandwidth;
199 + }
200 +
201 + public void setAvailableLambda(int availableLambda) {
202 + this.availableLambda = availableLambda;
203 + }
204 +
205 +
206 + @Override
207 + public LinkResourceAllocations requestResources(LinkResourceRequest req) {
208 + int lambda = -1;
209 + double bandwidth = -1.0;
210 +
211 + for (ResourceRequest resourceRequest : req.resources()) {
212 + if (resourceRequest.type() == ResourceType.BANDWIDTH) {
213 + final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
214 + bandwidth = brr.bandwidth().toDouble();
215 + } else if (resourceRequest.type() == ResourceType.LAMBDA) {
216 + lambda = 1;
217 + }
218 + }
219 +
220 + if (availableBandwidth < bandwidth) {
221 + throw new MockedAllocationFailure();
222 + }
223 + if (lambda > 0 && availableLambda == 0) {
224 + throw new MockedAllocationFailure();
225 + }
226 +
227 + return new IntentTestsMocks.MockLinkResourceAllocations();
228 + }
229 +
230 + @Override
231 + public void releaseResources(LinkResourceAllocations allocations) {
232 + // Mock
233 + }
234 +
235 + @Override
236 + public LinkResourceAllocations updateResources(LinkResourceRequest req,
237 + LinkResourceAllocations oldAllocations) {
238 + return null;
239 + }
240 +
241 + @Override
242 + public Iterable<LinkResourceAllocations> getAllocations() {
243 + return null;
244 + }
245 +
246 + @Override
247 + public Iterable<LinkResourceAllocations> getAllocations(Link link) {
248 + return null;
249 + }
250 +
251 + @Override
252 + public LinkResourceAllocations getAllocations(IntentId intentId) {
253 + return null;
254 + }
255 +
256 + @Override
257 + public Iterable<ResourceRequest> getAvailableResources(Link link) {
258 + final List<ResourceRequest> result = new LinkedList<>();
259 + if (availableBandwidth > 0.0) {
260 + result.add(new BandwidthResourceRequest(availableBandwidth));
261 + }
262 + if (availableLambda > 0) {
263 + result.add(new LambdaResourceRequest());
264 + }
265 + return result;
266 + }
267 +
268 + @Override
269 + public ResourceRequest getAvailableResources(Link link, LinkResourceAllocations allocations) {
270 + return null;
105 } 271 }
106 } 272 }
273 +
107 } 274 }
......