adding constaints to intent API
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/ConnectivityIntentCompiler.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/ConnectivityIntentCompiler.java
new file mode 100644
index 0000000..cecff78
--- /dev/null
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/ConnectivityIntentCompiler.java
@@ -0,0 +1,136 @@
+/*
+ * 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 org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.onos.net.ElementId;
+import org.onlab.onos.net.Path;
+import org.onlab.onos.net.intent.ConnectivityIntent;
+import org.onlab.onos.net.intent.Constraint;
+import org.onlab.onos.net.intent.IntentCompiler;
+import org.onlab.onos.net.intent.IntentExtensionService;
+import org.onlab.onos.net.provider.ProviderId;
+import org.onlab.onos.net.resource.LinkResourceService;
+import org.onlab.onos.net.topology.LinkWeight;
+import org.onlab.onos.net.topology.PathService;
+import org.onlab.onos.net.topology.TopologyEdge;
+
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Base class for compilers of various
+ * {@link org.onlab.onos.net.intent.ConnectivityIntent connectivity intents}.
+ */
+@Component(immediate = true)
+public abstract class ConnectivityIntentCompiler<T extends ConnectivityIntent>
+ implements IntentCompiler<T> {
+
+ private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected IntentExtensionService intentManager;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected PathService pathService;
+
+ @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+ protected LinkResourceService resourceService;
+
+ /**
+ * Returns an edge-weight capable of evaluating links on the basis of the
+ * specified constraints.
+ *
+ * @param constraints path constraints
+ * @return edge-weight function
+ */
+ protected LinkWeight weight(List<Constraint> constraints) {
+ return new ConstraintBasedLinkWeight(constraints);
+ }
+
+ /**
+ * Validates the specified path against the given constraints.
+ *
+ * @param path path to be checked
+ * @return true if the path passes all constraints
+ */
+ protected boolean checkPath(Path path, List<Constraint> constraints) {
+ for (Constraint constraint : constraints) {
+ if (!constraint.validate(path, resourceService)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ /**
+ * Computes a path between two ConnectPoints.
+ *
+ * @param one start of the path
+ * @param two end of the path
+ * @return Path between the two
+ * @throws PathNotFoundException if a path cannot be found
+ */
+ protected Path getPath(ConnectivityIntent intent,
+ ElementId one, ElementId two) {
+ Set<Path> paths = pathService.getPaths(one, two, weight(intent.constraints()));
+ if (paths.isEmpty()) {
+ throw new PathNotFoundException("No packet path from " + one + " to " + two);
+ }
+ // TODO: let's be more intelligent about this eventually
+ return paths.iterator().next();
+ }
+
+ /**
+ * Edge-weight capable of evaluating link cost using a set of constraints.
+ */
+ protected class ConstraintBasedLinkWeight implements LinkWeight {
+
+ private final List<Constraint> constraints;
+
+ /**
+ * Creates a new edge-weight function capable of evaluating links
+ * on the basis of the specified constraints.
+ *
+ * @param constraints path constraints
+ */
+ ConstraintBasedLinkWeight(List<Constraint> constraints) {
+ this.constraints = constraints;
+ }
+
+ @Override
+ public double weight(TopologyEdge edge) {
+ if (constraints == null) {
+ return 1.0;
+ }
+
+ // iterate over all constraints in order and return the weight of
+ // the first one with fast fail over the first failure
+ Iterator<Constraint> it = constraints.iterator();
+ double cost = it.next().cost(edge.link(), resourceService);
+ while (it.hasNext() && cost > 0) {
+ if (it.next().cost(edge.link(), resourceService) < 0) {
+ return -1;
+ }
+ }
+ return cost;
+ }
+ }
+
+}
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/HostToHostIntentCompiler.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/HostToHostIntentCompiler.java
index 37cf84b..605d3c7 100644
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/HostToHostIntentCompiler.java
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/HostToHostIntentCompiler.java
@@ -15,30 +15,21 @@
*/
package org.onlab.onos.net.intent.impl;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.onlab.onos.net.Host;
-import org.onlab.onos.net.HostId;
-import org.onlab.onos.net.Link;
import org.onlab.onos.net.Path;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.host.HostService;
import org.onlab.onos.net.intent.HostToHostIntent;
import org.onlab.onos.net.intent.Intent;
-import org.onlab.onos.net.intent.IntentCompiler;
-import org.onlab.onos.net.intent.IntentExtensionService;
import org.onlab.onos.net.intent.PathIntent;
-import org.onlab.onos.net.topology.LinkWeight;
-import org.onlab.onos.net.resource.LinkResourceRequest;
-import org.onlab.onos.net.topology.PathService;
-import org.onlab.onos.net.topology.TopologyEdge;
+
+import java.util.Arrays;
+import java.util.List;
import static org.onlab.onos.net.flow.DefaultTrafficSelector.builder;
@@ -47,13 +38,7 @@
*/
@Component(immediate = true)
public class HostToHostIntentCompiler
- implements IntentCompiler<HostToHostIntent> {
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected IntentExtensionService intentManager;
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected PathService pathService;
+ extends ConnectivityIntentCompiler<HostToHostIntent> {
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected HostService hostService;
@@ -70,8 +55,8 @@
@Override
public List<Intent> compile(HostToHostIntent intent) {
- Path pathOne = getPath(intent.one(), intent.two());
- Path pathTwo = getPath(intent.two(), intent.one());
+ Path pathOne = getPath(intent, intent.one(), intent.two());
+ Path pathTwo = getPath(intent, intent.two(), intent.one());
Host one = hostService.getHost(intent.one());
Host two = hostService.getHost(intent.two());
@@ -85,22 +70,7 @@
HostToHostIntent intent) {
TrafficSelector selector = builder(intent.selector())
.matchEthSrc(src.mac()).matchEthDst(dst.mac()).build();
- return new PathIntent(intent.appId(), selector, intent.treatment(),
- path, new LinkResourceRequest[0]);
+ return new PathIntent(intent.appId(), selector, intent.treatment(), path);
}
- private Path getPath(HostId one, HostId two) {
- Set<Path> paths = pathService.getPaths(one, two, new LinkWeight() {
- @Override
- public double weight(TopologyEdge edge) {
- return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
- }
- });
-
- if (paths.isEmpty()) {
- throw new PathNotFoundException("No path from host " + one + " to " + two);
- }
- // TODO: let's be more intelligent about this eventually
- return paths.iterator().next();
- }
}
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PathIntentInstaller.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/PathIntentInstaller.java
index c3b27bf..461f670 100644
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PathIntentInstaller.java
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/PathIntentInstaller.java
@@ -35,10 +35,13 @@
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.IntentExtensionService;
import org.onlab.onos.net.intent.IntentInstaller;
import org.onlab.onos.net.intent.PathIntent;
+import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
import org.onlab.onos.net.resource.LinkResourceAllocations;
+import org.onlab.onos.net.resource.LinkResourceRequest;
import org.onlab.onos.net.resource.LinkResourceService;
import org.slf4j.Logger;
@@ -79,13 +82,7 @@
@Override
public List<FlowRuleBatchOperation> install(PathIntent intent) {
- if (intent.resourceRequests().length > 0) {
- LinkResourceAllocations allocations = allocateBandwidth(intent);
- if (allocations == null) {
- log.debug("Insufficient bandwidth available to install path intent {}", intent);
- return null;
- }
- }
+ LinkResourceAllocations allocations = allocateResources(intent);
TrafficSelector.Builder builder =
DefaultTrafficSelector.builder(intent.selector());
@@ -110,6 +107,10 @@
@Override
public List<FlowRuleBatchOperation> uninstall(PathIntent intent) {
+ LinkResourceAllocations allocatedResources = resourceService.getAllocations(intent.id());
+ if (allocatedResources != null) {
+ resourceService.releaseResources(allocatedResources);
+ }
TrafficSelector.Builder builder =
DefaultTrafficSelector.builder(intent.selector());
Iterator<Link> links = intent.path().links().iterator();
@@ -130,8 +131,23 @@
return Lists.newArrayList(new FlowRuleBatchOperation(rules));
}
- private LinkResourceAllocations allocateBandwidth(PathIntent intent) {
- return resourceService.requestResources(intent.resourceRequests()[0]);
+ /**
+ * Allocate resources required for an intent.
+ *
+ * @param intent intent to allocate resource for
+ * @return allocated resources if any are required, null otherwise
+ */
+ private LinkResourceAllocations allocateResources(PathIntent intent) {
+ if (intent.constraints() == null) {
+ return null;
+ }
+ LinkResourceRequest.Builder builder =
+ DefaultLinkResourceRequest.builder(intent.id(), intent.path().links());
+ for (Constraint constraint : intent.constraints()) {
+ builder.addConstraint(constraint);
+ }
+ LinkResourceRequest request = builder.build();
+ return request.resources().isEmpty() ? null : resourceService.requestResources(request);
}
// TODO refactor below this line... ----------------------------
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentCompiler.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentCompiler.java
index 893b7ea..c32c8ee 100644
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentCompiler.java
+++ b/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentCompiler.java
@@ -15,31 +15,20 @@
*/
package org.onlab.onos.net.intent.impl;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Set;
-
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.onlab.onos.net.ConnectPoint;
import org.onlab.onos.net.DefaultEdgeLink;
import org.onlab.onos.net.DefaultPath;
import org.onlab.onos.net.Link;
import org.onlab.onos.net.Path;
import org.onlab.onos.net.intent.Intent;
-import org.onlab.onos.net.intent.IntentCompiler;
-import org.onlab.onos.net.intent.IntentExtensionService;
import org.onlab.onos.net.intent.PathIntent;
import org.onlab.onos.net.intent.PointToPointIntent;
import org.onlab.onos.net.provider.ProviderId;
-import org.onlab.onos.net.resource.LinkResourceRequest;
-import org.onlab.onos.net.topology.LinkWeight;
-import org.onlab.onos.net.topology.Topology;
-import org.onlab.onos.net.topology.TopologyEdge;
-import org.onlab.onos.net.topology.TopologyService;
+
+import java.util.ArrayList;
+import java.util.List;
import static java.util.Arrays.asList;
@@ -48,14 +37,11 @@
*/
@Component(immediate = true)
public class PointToPointIntentCompiler
- implements IntentCompiler<PointToPointIntent> {
+ extends ConnectivityIntentCompiler<PointToPointIntent> {
- private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected IntentExtensionService intentManager;
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected TopologyService topologyService;
+ // TODO: use off-the-shell core provider ID
+ private static final ProviderId PID =
+ new ProviderId("core", "org.onlab.onos.core", true);
@Activate
public void activate() {
@@ -69,15 +55,16 @@
@Override
public List<Intent> compile(PointToPointIntent intent) {
- Path path = getPath(intent.ingressPoint(), intent.egressPoint());
+ Path path = getPath(intent, intent.ingressPoint().deviceId(),
+ intent.egressPoint().deviceId());
List<Link> links = new ArrayList<>();
links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
links.addAll(path.links());
links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
- return asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
- path.annotations()), intent));
+ return asList(createPathIntent(new DefaultPath(PID, links, path.cost(),
+ path.annotations()), intent));
}
/**
@@ -90,34 +77,7 @@
private Intent createPathIntent(Path path,
PointToPointIntent intent) {
return new PathIntent(intent.appId(),
- intent.selector(), intent.treatment(), path,
- new LinkResourceRequest[0]);
+ intent.selector(), intent.treatment(), path);
}
- /**
- * Computes a path between two ConnectPoints.
- *
- * @param one start of the path
- * @param two end of the path
- * @return Path between the two
- * @throws PathNotFoundException if a path cannot be found
- */
- private Path getPath(ConnectPoint one, ConnectPoint two) {
- Topology topology = topologyService.currentTopology();
- LinkWeight weight = new LinkWeight() {
- @Override
- public double weight(TopologyEdge edge) {
- return edge.link().type() == Link.Type.OPTICAL ? -1 : +1;
- }
- };
-
- Set<Path> paths = topologyService.getPaths(topology, one.deviceId(),
- two.deviceId(), weight);
- if (paths.isEmpty()) {
- throw new PathNotFoundException("No packet path from " + one + " to " + two);
- }
-
- // TODO: let's be more intelligent about this eventually
- return paths.iterator().next();
- }
}
diff --git a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentWithBandwidthConstraintCompiler.java b/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentWithBandwidthConstraintCompiler.java
deleted file mode 100644
index a192a9f..0000000
--- a/core/net/src/main/java/org/onlab/onos/net/intent/impl/PointToPointIntentWithBandwidthConstraintCompiler.java
+++ /dev/null
@@ -1,146 +0,0 @@
-package org.onlab.onos.net.intent.impl;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.onlab.onos.net.ConnectPoint;
-import org.onlab.onos.net.DefaultEdgeLink;
-import org.onlab.onos.net.DefaultPath;
-import org.onlab.onos.net.Link;
-import org.onlab.onos.net.Path;
-import org.onlab.onos.net.intent.Intent;
-import org.onlab.onos.net.intent.IntentCompiler;
-import org.onlab.onos.net.intent.IntentExtensionService;
-import org.onlab.onos.net.intent.PathIntent;
-import org.onlab.onos.net.intent.PointToPointIntent;
-import org.onlab.onos.net.intent.PointToPointIntentWithBandwidthConstraint;
-import org.onlab.onos.net.provider.ProviderId;
-import org.onlab.onos.net.resource.BandwidthResourceRequest;
-import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
-import org.onlab.onos.net.resource.LinkResourceRequest;
-import org.onlab.onos.net.resource.LinkResourceService;
-import org.onlab.onos.net.resource.ResourceRequest;
-import org.onlab.onos.net.resource.ResourceType;
-import org.onlab.onos.net.topology.LinkWeight;
-import org.onlab.onos.net.topology.Topology;
-import org.onlab.onos.net.topology.TopologyEdge;
-import org.onlab.onos.net.topology.TopologyService;
-
-/**
- * A intent compiler for {@link org.onlab.onos.net.intent.HostToHostIntent}.
- */
-@Component(immediate = true)
-public class PointToPointIntentWithBandwidthConstraintCompiler
- implements IntentCompiler<PointToPointIntentWithBandwidthConstraint> {
-
- private static final ProviderId PID = new ProviderId("core", "org.onlab.onos.core", true);
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected IntentExtensionService intentManager;
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected TopologyService topologyService;
-
- @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
- protected LinkResourceService resourceService;
-
- @Activate
- public void activate() {
- intentManager.registerCompiler(PointToPointIntentWithBandwidthConstraint.class, this);
- }
-
- @Deactivate
- public void deactivate() {
- intentManager.unregisterCompiler(PointToPointIntent.class);
- }
-
- @Override
- public List<Intent> compile(PointToPointIntentWithBandwidthConstraint intent) {
- Path path = getPath(intent.ingressPoint(), intent.egressPoint(), intent.bandwidthRequest());
-
- List<Link> links = new ArrayList<>();
- links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
- links.addAll(path.links());
- links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
-
- return Arrays.asList(createPathIntent(new DefaultPath(PID, links, path.cost() + 2,
- path.annotations()),
- intent));
- }
-
- /**
- * Creates a path intent from the specified path and original
- * connectivity intent.
- *
- * @param path path to create an intent for
- * @param intent original intent
- */
- private Intent createPathIntent(Path path,
- PointToPointIntentWithBandwidthConstraint intent) {
- LinkResourceRequest.Builder request = DefaultLinkResourceRequest.builder(intent.id(),
- path.links())
- // TODO - this seems awkward, maybe allow directly attaching a BandwidthRequest
- .addBandwidthRequest(intent.bandwidthRequest().bandwidth().toDouble());
- LinkResourceRequest bandwidthRequest = request.build();
- LinkResourceRequest[] bandwidthRequests = {bandwidthRequest};
- return new PathIntent(intent.appId(),
- intent.selector(), intent.treatment(), path,
- bandwidthRequests);
- }
-
- /**
- * Computes a path between two ConnectPoints.
- *
- * @param one start of the path
- * @param two end of the path
- * @return Path between the two
- * @throws org.onlab.onos.net.intent.impl.PathNotFoundException if a path cannot be found
- */
- private Path getPath(ConnectPoint one, ConnectPoint two, final BandwidthResourceRequest bandwidthRequest) {
- Topology topology = topologyService.currentTopology();
- LinkWeight weight = new LinkWeight() {
- @Override
- public double weight(TopologyEdge edge) {
- if (bandwidthRequest != null) {
- double allocatedBandwidth = 0.0;
- Iterable<ResourceRequest> availableResources = resourceService.getAvailableResources(edge.link());
- for (ResourceRequest availableResource : availableResources) {
- if (availableResource.type() == ResourceType.BANDWIDTH) {
- BandwidthResourceRequest bandwidthRequest = (BandwidthResourceRequest) availableResource;
- allocatedBandwidth += bandwidthRequest.bandwidth().toDouble();
- }
- }
-
- // TODO this needs to be discovered from switch/ports somehow
- double maxBandwidth = 1000;
-
- double availableBandwidth = maxBandwidth - allocatedBandwidth;
- if (availableBandwidth >= bandwidthRequest.bandwidth().toDouble()) {
- return 1;
- } else {
- return -1;
- }
- } else {
- return 1;
- }
- }
- };
-
- Set<Path> paths = topologyService.getPaths(topology,
- one.deviceId(),
- two.deviceId(),
- weight);
-
- if (paths.isEmpty()) {
- throw new PathNotFoundException("No packet path from " + one + " to " + two);
- }
- // TODO: let's be more intelligent about this eventually
- return paths.iterator().next();
- }
-}