Thomas Vachuska
Committed by Gerrit Code Review

Merge "Unit Tests for Bandwidth and Lambda allocations"

......@@ -15,6 +15,8 @@
*/
package org.onlab.onos.net;
import org.onlab.onos.TestApplicationId;
import org.onlab.onos.core.ApplicationId;
import org.onlab.onos.net.provider.ProviderId;
import org.onlab.packet.ChassisId;
import org.onlab.packet.IpAddress;
......@@ -39,6 +41,7 @@ public final class NetTestTools {
}
public static final ProviderId PID = new ProviderId("of", "foo");
public static final ApplicationId APP_ID = new TestApplicationId("foo");
// Short-hand for producing a device id from a string
public static DeviceId did(String id) {
......
......@@ -67,7 +67,7 @@ public class PathIntentInstaller implements IntentInstaller<PathIntent> {
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected LinkResourceService resourceService;
private ApplicationId appId;
protected ApplicationId appId;
@Activate
public void activate() {
......
......@@ -16,23 +16,39 @@
package org.onlab.onos.net.intent;
import static org.onlab.onos.net.NetTestTools.createPath;
import static org.onlab.onos.net.NetTestTools.link;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.onlab.onos.net.DeviceId;
import org.onlab.onos.net.ElementId;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.Path;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
import org.onlab.onos.net.flow.criteria.Criterion;
import org.onlab.onos.net.flow.criteria.Criterion.Type;
import org.onlab.onos.net.flow.instructions.Instruction;
import org.onlab.onos.net.resource.BandwidthResourceRequest;
import org.onlab.onos.net.resource.LambdaResourceRequest;
import org.onlab.onos.net.resource.LinkResourceAllocations;
import org.onlab.onos.net.resource.LinkResourceRequest;
import org.onlab.onos.net.resource.LinkResourceService;
import org.onlab.onos.net.resource.ResourceAllocation;
import org.onlab.onos.net.resource.ResourceRequest;
import org.onlab.onos.net.resource.ResourceType;
import org.onlab.onos.net.topology.DefaultTopologyEdge;
import org.onlab.onos.net.topology.DefaultTopologyVertex;
import org.onlab.onos.net.topology.LinkWeight;
import org.onlab.onos.net.topology.PathService;
import org.onlab.onos.net.topology.TopologyVertex;
/**
* Common mocks used by the intent framework tests.
......@@ -101,7 +117,158 @@ public class IntentTestsMocks {
@Override
public Set<Path> getPaths(ElementId src, ElementId dst, LinkWeight weight) {
return getPaths(src, dst);
final Set<Path> paths = getPaths(src, dst);
for (Path path : paths) {
final DeviceId srcDevice = path.src().deviceId();
final DeviceId dstDevice = path.dst().deviceId();
final TopologyVertex srcVertex = new DefaultTopologyVertex(srcDevice);
final TopologyVertex dstVertex = new DefaultTopologyVertex(dstDevice);
final Link link = link(src.toString(), 1, dst.toString(), 1);
final double weightValue = weight.weight(new DefaultTopologyEdge(srcVertex, dstVertex, link));
if (weightValue < 0) {
return new HashSet<>();
}
}
return paths;
}
}
public static class MockLinkResourceAllocations implements LinkResourceAllocations {
@Override
public Set<ResourceAllocation> getResourceAllocation(Link link) {
return null;
}
@Override
public IntentId intendId() {
return null;
}
@Override
public Collection<Link> links() {
return null;
}
@Override
public Set<ResourceRequest> resources() {
return null;
}
@Override
public ResourceType type() {
return null;
}
}
public static class MockedAllocationFailure extends RuntimeException { }
public static class MockResourceService implements LinkResourceService {
double availableBandwidth = -1.0;
int availableLambda = -1;
/**
* Allocates a resource service that will allow bandwidth allocations
* up to a limit.
*
* @param bandwidth available bandwidth limit
* @return resource manager for bandwidth requests
*/
public static MockResourceService makeBandwidthResourceService(double bandwidth) {
final MockResourceService result = new MockResourceService();
result.availableBandwidth = bandwidth;
return result;
}
/**
* Allocates a resource service that will allow lambda allocations.
*
* @param lambda Lambda to return for allocation requests. Currently unused
* @return resource manager for lambda requests
*/
public static MockResourceService makeLambdaResourceService(int lambda) {
final MockResourceService result = new MockResourceService();
result.availableLambda = lambda;
return result;
}
public void setAvailableBandwidth(double availableBandwidth) {
this.availableBandwidth = availableBandwidth;
}
public void setAvailableLambda(int availableLambda) {
this.availableLambda = availableLambda;
}
@Override
public LinkResourceAllocations requestResources(LinkResourceRequest req) {
int lambda = -1;
double bandwidth = -1.0;
for (ResourceRequest resourceRequest : req.resources()) {
if (resourceRequest.type() == ResourceType.BANDWIDTH) {
final BandwidthResourceRequest brr = (BandwidthResourceRequest) resourceRequest;
bandwidth = brr.bandwidth().toDouble();
} else if (resourceRequest.type() == ResourceType.LAMBDA) {
lambda = 1;
}
}
if (availableBandwidth < bandwidth) {
throw new MockedAllocationFailure();
}
if (lambda > 0 && availableLambda == 0) {
throw new MockedAllocationFailure();
}
return new IntentTestsMocks.MockLinkResourceAllocations();
}
@Override
public void releaseResources(LinkResourceAllocations allocations) {
// Mock
}
@Override
public LinkResourceAllocations updateResources(LinkResourceRequest req,
LinkResourceAllocations oldAllocations) {
return null;
}
@Override
public Iterable<LinkResourceAllocations> getAllocations() {
return null;
}
@Override
public Iterable<LinkResourceAllocations> getAllocations(Link link) {
return null;
}
@Override
public LinkResourceAllocations getAllocations(IntentId intentId) {
return null;
}
@Override
public Iterable<ResourceRequest> getAvailableResources(Link link) {
final List<ResourceRequest> result = new LinkedList<>();
if (availableBandwidth > 0.0) {
result.add(new BandwidthResourceRequest(availableBandwidth));
}
if (availableLambda > 0) {
result.add(new LambdaResourceRequest());
}
return result;
}
@Override
public ResourceRequest getAvailableResources(Link link, LinkResourceAllocations allocations) {
return null;
}
}
}
......
/*
* Copyright 2014 Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onlab.onos.net.intent.impl;
import java.util.LinkedList;
import java.util.List;
import org.junit.Test;
import org.onlab.onos.net.flow.FlowRuleBatchOperation;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
import org.onlab.onos.net.intent.Constraint;
import org.onlab.onos.net.intent.Intent;
import org.onlab.onos.net.intent.IntentTestsMocks;
import org.onlab.onos.net.intent.PathIntent;
import org.onlab.onos.net.intent.PointToPointIntent;
import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
import org.onlab.onos.net.intent.constraint.LambdaConstraint;
import org.onlab.onos.net.resource.Bandwidth;
import org.onlab.onos.net.resource.Lambda;
import org.onlab.onos.net.resource.LinkResourceService;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.fail;
import static org.onlab.onos.net.NetTestTools.APP_ID;
import static org.onlab.onos.net.NetTestTools.connectPoint;
/**
* Unit tests for calculating paths for intents with constraints.
*/
public class PathConstraintCalculationTest {
/**
* Creates a point to point intent compiler for a three switch linear
* topology.
*
* @param resourceService service to use for resource allocation requests
* @return point to point compiler
*/
private PointToPointIntentCompiler makeCompiler(LinkResourceService resourceService) {
final String[] hops = {"s1", "s2", "s3"};
final PointToPointIntentCompiler compiler = new PointToPointIntentCompiler();
compiler.resourceService = resourceService;
compiler.pathService = new IntentTestsMocks.MockPathService(hops);
return compiler;
}
/**
* Creates an intent with a given constraint and compiles it. The compiler
* will throw PathNotFoundException if the allocations cannot be satisfied.
*
* @param constraint constraint to apply to the created intent
* @param resourceService service to use for resource allocation requests
* @return List of compiled intents
*/
private List<Intent> compileIntent(Constraint constraint,
LinkResourceService resourceService) {
final List<Constraint> constraints = new LinkedList<>();
constraints.add(constraint);
final TrafficSelector selector = new IntentTestsMocks.MockSelector();
final TrafficTreatment treatment = new IntentTestsMocks.MockTreatment();
final PointToPointIntent intent =
new PointToPointIntent(APP_ID,
selector,
treatment,
connectPoint("s1", 1),
connectPoint("s3", 1),
constraints);
final PointToPointIntentCompiler compiler = makeCompiler(resourceService);
return compiler.compile(intent);
}
/**
* Installs a compiled path intent and returns the flow rules it generates.
*
* @param compiledIntents list of compiled intents
* @param resourceService service to use for resource allocation requests
* @return
*/
private List<FlowRuleBatchOperation> installIntents(List<Intent> compiledIntents,
LinkResourceService resourceService) {
final PathIntent path = (PathIntent) compiledIntents.get(0);
final PathIntentInstaller installer = new PathIntentInstaller();
installer.resourceService = resourceService;
installer.appId = APP_ID;
return installer.install(path);
}
/**
* Tests that requests with sufficient available bandwidth succeed.
*/
@Test
public void testBandwidthConstrainedIntentSuccess() {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
}
/**
* Tests that requests with insufficient available bandwidth fail.
*/
@Test
public void testBandwidthConstrainedIntentFailure() {
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
try {
compileIntent(constraint, resourceService);
fail("Point to Point compilation with insufficient bandwidth does "
+ "not throw exception.");
} catch (PathNotFoundException noPath) {
assertThat(noPath.getMessage(), containsString("No packet path"));
}
}
/**
* Tests that requests for available lambdas are successful.
*/
@Test
public void testLambdaConstrainedIntentSuccess() {
final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
final List<Intent> compiledIntents =
compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
}
/**
* Tests that requests for lambdas when there are no available lambdas
* fail.
*/
@Test
public void testLambdaConstrainedIntentFailure() {
final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
final LinkResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(10.0);
try {
compileIntent(constraint, resourceService);
fail("Point to Point compilation with no available lambda does "
+ "not throw exception.");
} catch (PathNotFoundException noPath) {
assertThat(noPath.getMessage(), containsString("No packet path"));
}
}
/**
* Tests that installation of bandwidth constrained path intents are
* successful.
*/
@Test
public void testInstallBandwidthConstrainedIntentSuccess() {
final IntentTestsMocks.MockResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
final List<FlowRuleBatchOperation> flowOperations =
installIntents(compiledIntents, resourceService);
assertThat(flowOperations, notNullValue());
assertThat(flowOperations, hasSize(1));
}
/**
* Tests that installation of bandwidth constrained path intents fail
* if there are no available resources.
*/
@Test
public void testInstallBandwidthConstrainedIntentFailure() {
final IntentTestsMocks.MockResourceService resourceService =
IntentTestsMocks.MockResourceService.makeBandwidthResourceService(1000.0);
final Constraint constraint = new BandwidthConstraint(Bandwidth.valueOf(100.0));
final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
// Make it look like the available bandwidth was consumed
resourceService.setAvailableBandwidth(1.0);
try {
installIntents(compiledIntents, resourceService);
fail("Bandwidth request with no available bandwidth did not fail.");
} catch (IntentTestsMocks.MockedAllocationFailure failure) {
assertThat(failure,
instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
}
}
/**
* Tests that installation of lambda constrained path intents are
* successful.
*/
@Test
public void testInstallLambdaConstrainedIntentSuccess() {
final IntentTestsMocks.MockResourceService resourceService =
IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
final List<FlowRuleBatchOperation> flowOperations =
installIntents(compiledIntents, resourceService);
assertThat(flowOperations, notNullValue());
assertThat(flowOperations, hasSize(1));
}
/**
* Tests that installation of lambda constrained path intents fail
* if there are no available resources.
*/
@Test
public void testInstallLambdaConstrainedIntentFailure() {
final IntentTestsMocks.MockResourceService resourceService =
IntentTestsMocks.MockResourceService.makeLambdaResourceService(1);
final Constraint constraint = new LambdaConstraint(Lambda.valueOf(1));
final List<Intent> compiledIntents = compileIntent(constraint, resourceService);
assertThat(compiledIntents, notNullValue());
assertThat(compiledIntents, hasSize(1));
// Make it look like the available lambda was consumed
resourceService.setAvailableLambda(0);
try {
installIntents(compiledIntents, resourceService);
fail("Lambda request with no available lambda did not fail.");
} catch (IntentTestsMocks.MockedAllocationFailure failure) {
assertThat(failure,
instanceOf(IntentTestsMocks.MockedAllocationFailure.class));
}
}
}