adding constaints to intent API
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/ConnectivityIntent.java b/core/api/src/main/java/org/onlab/onos/net/intent/ConnectivityIntent.java
index 65d48d5..6f1f71e 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/ConnectivityIntent.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/ConnectivityIntent.java
@@ -23,6 +23,7 @@
import org.onlab.onos.net.flow.TrafficTreatment;
import java.util.Collection;
+import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -40,10 +41,14 @@
private final TrafficSelector selector;
private final TrafficTreatment treatment;
+ private final List<Constraint> constraints;
/**
* Creates a connectivity intent that matches on the specified selector
* and applies the specified treatment.
+ * <p>
+ * Path will be chosen without any constraints.
+ * </p>
*
* @param id intent identifier
* @param appId application identifier
@@ -56,9 +61,33 @@
Collection<NetworkResource> resources,
TrafficSelector selector,
TrafficTreatment treatment) {
+ this(id, appId, resources, selector, treatment, null);
+ }
+
+ /**
+ * Creates a connectivity intent that matches on the specified selector
+ * and applies the specified treatment.
+ * <p>
+ * Path will be optimized based on the first constraint if one is given.
+ * </p>
+ *
+ * @param id intent identifier
+ * @param appId application identifier
+ * @param resources required network resources (optional)
+ * @param selector traffic selector
+ * @param treatment treatment
+ * @param constraints optional prioritized list of constraints
+ * @throws NullPointerException if the selector or treatement is null
+ */
+ protected ConnectivityIntent(IntentId id, ApplicationId appId,
+ Collection<NetworkResource> resources,
+ TrafficSelector selector,
+ TrafficTreatment treatment,
+ List<Constraint> constraints) {
super(id, appId, resources);
this.selector = checkNotNull(selector);
this.treatment = checkNotNull(treatment);
+ this.constraints = constraints;
}
/**
@@ -68,6 +97,7 @@
super();
this.selector = null;
this.treatment = null;
+ this.constraints = null;
}
/**
@@ -89,13 +119,22 @@
}
/**
+ * Returns the set of connectivity constraints.
+ *
+ * @return list of intent constraints
+ */
+ public List<Constraint> constraints() {
+ return constraints;
+ }
+
+ /**
* Produces a collection of network resources from the given links.
*
* @param links collection of links
* @return collection of link resources
*/
protected static Collection<NetworkResource> resources(Collection<Link> links) {
- return ImmutableSet.<NetworkResource>copyOf(links);
+ return ImmutableSet.<NetworkResource>copyOf((Iterable<? extends NetworkResource>) links);
}
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/Constraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/Constraint.java
new file mode 100644
index 0000000..9004aa6
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/Constraint.java
@@ -0,0 +1,51 @@
+/*
+ * 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;
+
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.Path;
+import org.onlab.onos.net.resource.LinkResourceService;
+
+/**
+ * Representation of a connectivity constraint capable of evaluating a link
+ * and determining the cost of traversing that link in the context of this
+ * constraint.
+ */
+public interface Constraint {
+
+ // TODO: Consider separating cost vs viability.
+
+ /**
+ * Evaluates the specified link and provides the cost for its traversal.
+ *
+ * @param link link to be evaluated
+ * @param resourceService resource service for validating availability of
+ * link resources
+ * @return cost of link traversal
+ */
+ double cost(Link link, LinkResourceService resourceService);
+
+ /**
+ * Validates that the specified path satisfies the constraint.
+ *
+ * @param path path to be validated
+ * @param resourceService resource service for validating availability of
+ * link resources
+ * @return cost of link traversal
+ */
+ boolean validate(Path path, LinkResourceService resourceService);
+
+}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/HostToHostIntent.java b/core/api/src/main/java/org/onlab/onos/net/intent/HostToHostIntent.java
index 376a122..3fad93d 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/HostToHostIntent.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/HostToHostIntent.java
@@ -21,6 +21,8 @@
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
+import java.util.List;
+
import static com.google.common.base.Preconditions.checkNotNull;
/**
@@ -44,11 +46,30 @@
public HostToHostIntent(ApplicationId appId, HostId one, HostId two,
TrafficSelector selector,
TrafficTreatment treatment) {
+ this(appId, one, two, selector, treatment, null);
+ }
+
+ /**
+ * Creates a new host-to-host intent with the supplied host pair.
+ *
+ * @param appId application identifier
+ * @param one first host
+ * @param two second host
+ * @param selector action
+ * @param treatment ingress port
+ * @param constraints optional prioritized list of path selection constraints
+ * @throws NullPointerException if {@code one} or {@code two} is null.
+ */
+ public HostToHostIntent(ApplicationId appId, HostId one, HostId two,
+ TrafficSelector selector,
+ TrafficTreatment treatment,
+ List<Constraint> constraints) {
super(id(HostToHostIntent.class, min(one, two), max(one, two),
- selector, treatment),
- appId, null, selector, treatment);
+ selector, treatment, constraints),
+ appId, null, selector, treatment, constraints);
this.one = checkNotNull(one);
this.two = checkNotNull(two);
+
}
private static HostId min(HostId one, HostId two) {
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/IntentId.java b/core/api/src/main/java/org/onlab/onos/net/intent/IntentId.java
index 8c75397..c4c2752 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/IntentId.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/IntentId.java
@@ -51,6 +51,15 @@
this.fingerprint = fingerprint;
}
+ /**
+ * Returns the backing fingerprint.
+ *
+ * @return the fingerprint
+ */
+ public long fingerprint() {
+ return fingerprint;
+ }
+
@Override
public int hashCode() {
return (int) (fingerprint ^ (fingerprint >>> 32));
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/PathIntent.java b/core/api/src/main/java/org/onlab/onos/net/intent/PathIntent.java
index 0e9fa6d..9189bae 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/PathIntent.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/PathIntent.java
@@ -15,16 +15,11 @@
*/
package org.onlab.onos.net.intent;
-import java.util.List;
-
import com.google.common.base.MoreObjects;
-import com.google.common.collect.ImmutableList;
-
import org.onlab.onos.core.ApplicationId;
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.resource.LinkResourceRequest;
/**
* Abstraction of explicitly path specified connectivity intent.
@@ -32,7 +27,6 @@
public class PathIntent extends ConnectivityIntent {
private final Path path;
- private final List<LinkResourceRequest> resourceRequests;
/**
* Creates a new point-to-point intent with the supplied ingress/egress
@@ -42,15 +36,13 @@
* @param selector traffic selector
* @param treatment treatment
* @param path traversed links
- * @param resourceRequests link resource request
* @throws NullPointerException {@code path} is null
*/
public PathIntent(ApplicationId appId, TrafficSelector selector,
- TrafficTreatment treatment, Path path, LinkResourceRequest[] resourceRequests) {
+ TrafficTreatment treatment, Path path) {
super(id(PathIntent.class, selector, treatment, path), appId,
resources(path.links()), selector, treatment);
this.path = path;
- this.resourceRequests = ImmutableList.copyOf(resourceRequests);
}
/**
@@ -59,7 +51,6 @@
protected PathIntent() {
super();
this.path = null;
- this.resourceRequests = ImmutableList.of();
}
/**
@@ -76,10 +67,6 @@
return true;
}
- // TODO: consider changing return type
- public LinkResourceRequest[] resourceRequests() {
- return resourceRequests.toArray(new LinkResourceRequest[resourceRequests.size()]);
- }
@Override
public String toString() {
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntent.java b/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntent.java
index 61a1a56..e480ee2 100644
--- a/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntent.java
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntent.java
@@ -16,10 +16,15 @@
package org.onlab.onos.net.intent;
import com.google.common.base.MoreObjects;
+import com.google.common.collect.ImmutableList;
import org.onlab.onos.core.ApplicationId;
import org.onlab.onos.net.ConnectPoint;
+import org.onlab.onos.net.Link;
import org.onlab.onos.net.flow.TrafficSelector;
import org.onlab.onos.net.flow.TrafficTreatment;
+import org.onlab.onos.net.intent.constraint.LinkTypeConstraint;
+
+import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull;
@@ -33,7 +38,7 @@
/**
* Creates a new point-to-point intent with the supplied ingress/egress
- * ports.
+ * ports and with built-in link type constraint to avoid optical links.
*
* @param appId application identifier
* @param selector traffic selector
@@ -46,8 +51,30 @@
TrafficTreatment treatment,
ConnectPoint ingressPoint,
ConnectPoint egressPoint) {
- super(id(PointToPointIntent.class, selector, treatment, ingressPoint, egressPoint),
- appId, null, selector, treatment);
+ this(appId, selector, treatment, ingressPoint, egressPoint,
+ ImmutableList.of(new LinkTypeConstraint(false, Link.Type.OPTICAL)));
+ }
+
+ /**
+ * Creates a new point-to-point intent with the supplied ingress/egress
+ * ports and constraints.
+ *
+ * @param appId application identifier
+ * @param selector traffic selector
+ * @param treatment treatment
+ * @param ingressPoint ingress port
+ * @param egressPoint egress port
+ * @param constraints optional list of constraints
+ * @throws NullPointerException if {@code ingressPoint} or {@code egressPoints} is null.
+ */
+ public PointToPointIntent(ApplicationId appId, TrafficSelector selector,
+ TrafficTreatment treatment,
+ ConnectPoint ingressPoint,
+ ConnectPoint egressPoint,
+ List<Constraint> constraints) {
+ super(id(PointToPointIntent.class, selector, treatment,
+ ingressPoint, egressPoint, constraints),
+ appId, null, selector, treatment, constraints);
this.ingressPoint = checkNotNull(ingressPoint);
this.egressPoint = checkNotNull(egressPoint);
}
@@ -89,6 +116,7 @@
.add("treatment", treatment())
.add("ingress", ingressPoint)
.add("egress", egressPoint)
+ .add("constraints", constraints())
.toString();
}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntentWithBandwidthConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntentWithBandwidthConstraint.java
deleted file mode 100644
index a9b614c..0000000
--- a/core/api/src/main/java/org/onlab/onos/net/intent/PointToPointIntentWithBandwidthConstraint.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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;
-
-import org.onlab.onos.core.ApplicationId;
-import org.onlab.onos.net.ConnectPoint;
-import org.onlab.onos.net.flow.TrafficSelector;
-import org.onlab.onos.net.flow.TrafficTreatment;
-import org.onlab.onos.net.resource.BandwidthResourceRequest;
-
-import com.google.common.base.MoreObjects;
-
-import static com.google.common.base.Preconditions.checkNotNull;
-
-/**
- * Abstraction of point-to-point connectivity.
- */
-public class PointToPointIntentWithBandwidthConstraint extends ConnectivityIntent {
-
- private final ConnectPoint ingressPoint;
- private final ConnectPoint egressPoint;
- private final BandwidthResourceRequest bandwidthResourceRequest;
-
- /**
- * Creates a new point-to-point intent with the supplied ingress/egress
- * ports.
- *
- * @param appId application identifier
- * @param selector traffic selector
- * @param treatment treatment
- * @param ingressPoint ingress port
- * @param egressPoint egress port
- * @param bandwidthResourceRequest bandwidth resource request
- * @throws NullPointerException if {@code ingressPoint} or {@code egressPoints} is null.
- */
- public PointToPointIntentWithBandwidthConstraint(ApplicationId appId, TrafficSelector selector,
- TrafficTreatment treatment,
- ConnectPoint ingressPoint,
- ConnectPoint egressPoint,
- BandwidthResourceRequest bandwidthResourceRequest) {
- super(id(PointToPointIntentWithBandwidthConstraint.class, selector,
- treatment, ingressPoint, egressPoint, bandwidthResourceRequest.bandwidth()),
- appId, null, selector, treatment);
- this.ingressPoint = checkNotNull(ingressPoint);
- this.egressPoint = checkNotNull(egressPoint);
- this.bandwidthResourceRequest = bandwidthResourceRequest;
- }
-
- /**
- * Constructor for serializer.
- */
- protected PointToPointIntentWithBandwidthConstraint() {
- super();
- this.ingressPoint = null;
- this.egressPoint = null;
- bandwidthResourceRequest = new BandwidthResourceRequest(0.0);
- }
-
- /**
- * Returns the port on which the ingress traffic should be connected to
- * the egress.
- *
- * @return ingress port
- */
- public ConnectPoint ingressPoint() {
- return ingressPoint;
- }
-
- /**
- * Returns the port on which the traffic should egress.
- *
- * @return egress port
- */
- public ConnectPoint egressPoint() {
- return egressPoint;
- }
-
- public BandwidthResourceRequest bandwidthRequest() {
- return this.bandwidthResourceRequest;
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(getClass())
- .add("id", id())
- .add("appId", appId())
- .add("selector", selector())
- .add("treatment", treatment())
- .add("ingress", ingressPoint)
- .add("egress", egressPoint)
- .add("bandwidth", bandwidthResourceRequest.bandwidth().toString())
- .toString();
- }
-
-}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BandwidthConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BandwidthConstraint.java
new file mode 100644
index 0000000..44d6875
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BandwidthConstraint.java
@@ -0,0 +1,89 @@
+/*
+ * 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.constraint;
+
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.resource.Bandwidth;
+import org.onlab.onos.net.resource.BandwidthResourceRequest;
+import org.onlab.onos.net.resource.LinkResourceService;
+import org.onlab.onos.net.resource.ResourceRequest;
+import org.onlab.onos.net.resource.ResourceType;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Constraint that evaluates links based on available bandwidths.
+ */
+public class BandwidthConstraint extends BooleanConstraint {
+
+ private final Bandwidth bandwidth;
+
+ /**
+ * Creates a new bandwidth constraint.
+ *
+ * @param bandwidth required bandwidth
+ */
+ public BandwidthConstraint(Bandwidth bandwidth) {
+ this.bandwidth = checkNotNull(bandwidth, "Bandwidth cannot be null");
+ }
+
+ @Override
+ public boolean isValid(Link link, LinkResourceService resourceService) {
+ for (ResourceRequest request : resourceService.getAvailableResources(link)) {
+ if (request.type() == ResourceType.BANDWIDTH) {
+ BandwidthResourceRequest brr = (BandwidthResourceRequest) request;
+ if (brr.bandwidth().toDouble() >= bandwidth.toDouble()) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns the bandwidth required by this constraint.
+ *
+ * @return required bandwidth
+ */
+ public Bandwidth bandwidth() {
+ return bandwidth;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(bandwidth);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final BandwidthConstraint other = (BandwidthConstraint) obj;
+ return Objects.equals(this.bandwidth, other.bandwidth);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).add("bandwidth", bandwidth).toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BooleanConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BooleanConstraint.java
new file mode 100644
index 0000000..9ad4ac7
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/BooleanConstraint.java
@@ -0,0 +1,53 @@
+/*
+ * 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.constraint;
+
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.Path;
+import org.onlab.onos.net.intent.Constraint;
+import org.onlab.onos.net.resource.LinkResourceService;
+
+/**
+ * Abstract base class for various constraints that evaluate link viability
+ * in a yes/no fashion.
+ */
+public abstract class BooleanConstraint implements Constraint {
+
+ /**
+ * Returns true if the specified link satisfies the constraint.
+ *
+ * @param link link to be validated
+ * @param resourceService resource service for checking available link resources
+ * @return true if link is viable
+ */
+ public abstract boolean isValid(Link link, LinkResourceService resourceService);
+
+ @Override
+ public double cost(Link link, LinkResourceService resourceService) {
+ return isValid(link, resourceService) ? +1 : -1;
+ }
+
+ @Override
+ public boolean validate(Path path, LinkResourceService resourceService) {
+ for (Link link : path.links()) {
+ if (isValid(link, resourceService)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LambdaConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LambdaConstraint.java
new file mode 100644
index 0000000..d1d33d6
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LambdaConstraint.java
@@ -0,0 +1,84 @@
+/*
+ * 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.constraint;
+
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.resource.Lambda;
+import org.onlab.onos.net.resource.LinkResourceService;
+import org.onlab.onos.net.resource.ResourceRequest;
+import org.onlab.onos.net.resource.ResourceType;
+
+import java.util.Objects;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+
+/**
+ * Constraint that evaluates links based on available lambda.
+ */
+public class LambdaConstraint extends BooleanConstraint {
+
+ private final Lambda lambda;
+
+ /**
+ * Creates a new optical lambda constraint.
+ *
+ * @param lambda optional lambda to indicate a specific lambda
+ */
+ public LambdaConstraint(Lambda lambda) {
+ this.lambda = lambda;
+ }
+
+ @Override
+ public boolean isValid(Link link, LinkResourceService resourceService) {
+ for (ResourceRequest request : resourceService.getAvailableResources(link)) {
+ if (request.type() == ResourceType.LAMBDA) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Returns the lambda required by this constraint.
+ *
+ * @return required lambda
+ */
+ public Lambda lambda() {
+ return lambda;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(lambda);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final LambdaConstraint other = (LambdaConstraint) obj;
+ return Objects.equals(this.lambda, other.lambda);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this).add("lambda", lambda).toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LinkTypeConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LinkTypeConstraint.java
new file mode 100644
index 0000000..5640591
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/LinkTypeConstraint.java
@@ -0,0 +1,100 @@
+/*
+ * 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.constraint;
+
+import com.google.common.collect.ImmutableSet;
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.resource.LinkResourceService;
+
+import java.util.Objects;
+import java.util.Set;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Constraint that evaluates links based on their type.
+ */
+public class LinkTypeConstraint extends BooleanConstraint {
+
+ private final Set<Link.Type> types;
+ private final boolean isInclusive;
+
+ /**
+ * Creates a new constraint for requesting connectivity using or avoiding
+ * the specified link types.
+ *
+ * @param inclusive indicates whether the given link types are to be
+ * permitted or avoided
+ * @param types link types
+ */
+ public LinkTypeConstraint(boolean inclusive, Link.Type... types) {
+ checkNotNull(types, "Link types cannot be null");
+ checkArgument(types.length > 0, "There must be more than one type");
+ this.types = ImmutableSet.copyOf(types);
+ this.isInclusive = inclusive;
+ }
+
+ @Override
+ public boolean isValid(Link link, LinkResourceService resourceService) {
+ boolean contains = types.contains(link.type());
+ return isInclusive ? contains : !contains;
+ }
+
+ /**
+ * Returns the set of link types.
+ *
+ * @return set of link types
+ */
+ public Set<Link.Type> types() {
+ return types;
+ }
+
+ /**
+ * Indicates if the constraint is inclusive or exclusive.
+ *
+ * @return true if inclusive
+ */
+ public boolean isInclusive() {
+ return isInclusive;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(types, isInclusive);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass()) {
+ return false;
+ }
+ final LinkTypeConstraint other = (LinkTypeConstraint) obj;
+ return Objects.equals(this.types, other.types) && Objects.equals(this.isInclusive, other.isInclusive);
+ }
+
+ @Override
+ public String toString() {
+ return toStringHelper(this)
+ .add("inclusive", isInclusive)
+ .add("types", types)
+ .toString();
+ }
+}
diff --git a/core/api/src/main/java/org/onlab/onos/net/resource/DefaultLinkResourceRequest.java b/core/api/src/main/java/org/onlab/onos/net/resource/DefaultLinkResourceRequest.java
index 73b48df6..c3f5c28 100644
--- a/core/api/src/main/java/org/onlab/onos/net/resource/DefaultLinkResourceRequest.java
+++ b/core/api/src/main/java/org/onlab/onos/net/resource/DefaultLinkResourceRequest.java
@@ -20,9 +20,12 @@
import java.util.Set;
import org.onlab.onos.net.Link;
+import org.onlab.onos.net.intent.Constraint;
import org.onlab.onos.net.intent.IntentId;
import com.google.common.collect.ImmutableSet;
+import org.onlab.onos.net.intent.constraint.BandwidthConstraint;
+import org.onlab.onos.net.intent.constraint.LambdaConstraint;
/**
* Implementation of {@link LinkResourceRequest}.
@@ -125,6 +128,18 @@
return this;
}
+ @Override
+ public LinkResourceRequest.Builder addConstraint(Constraint constraint) {
+ if (constraint instanceof LambdaConstraint) {
+ return addLambdaRequest();
+ } else if (constraint instanceof BandwidthConstraint) {
+ BandwidthConstraint bw = (BandwidthConstraint) constraint;
+ return addBandwidthRequest(bw.bandwidth().toDouble());
+ }
+ return this;
+ }
+
+
/**
* Returns link resource request.
*
diff --git a/core/api/src/main/java/org/onlab/onos/net/resource/LinkResourceRequest.java b/core/api/src/main/java/org/onlab/onos/net/resource/LinkResourceRequest.java
index a511778..23a0c30 100644
--- a/core/api/src/main/java/org/onlab/onos/net/resource/LinkResourceRequest.java
+++ b/core/api/src/main/java/org/onlab/onos/net/resource/LinkResourceRequest.java
@@ -19,6 +19,7 @@
import java.util.Set;
import org.onlab.onos.net.Link;
+import org.onlab.onos.net.intent.Constraint;
import org.onlab.onos.net.intent.IntentId;
/**
@@ -67,6 +68,14 @@
public Builder addBandwidthRequest(double bandwidth);
/**
+ * Adds the resources required for a constraint.
+ *
+ * @param constraint the constraint
+ * @return self
+ */
+ public Builder addConstraint(Constraint constraint);
+
+ /**
* Returns link resource request.
*
* @return link resource request