Add constraint to avoid specified devices
Change-Id: I9b29ebc0f38a3840740cc21c43cc448befc95015
diff --git a/core/api/src/main/java/org/onlab/onos/net/intent/constraint/ObstacleConstraint.java b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/ObstacleConstraint.java
new file mode 100644
index 0000000..3ed3fae
--- /dev/null
+++ b/core/api/src/main/java/org/onlab/onos/net/intent/constraint/ObstacleConstraint.java
@@ -0,0 +1,87 @@
+/*
+ * 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.base.MoreObjects;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.onos.net.DeviceId;
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.Path;
+import org.onlab.onos.net.resource.LinkResourceService;
+
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Constraint that evaluates elements not passed through.
+ */
+public class ObstacleConstraint extends BooleanConstraint {
+
+ private final Set<DeviceId> obstacles;
+
+ /**
+ * Creates a new constraint that the specified device are not passed through.
+ * @param obstacles devices not to be passed
+ */
+ public ObstacleConstraint(DeviceId... obstacles) {
+ this.obstacles = ImmutableSet.copyOf(obstacles);
+ }
+
+ @Override
+ public boolean isValid(Link link, LinkResourceService resourceService) {
+ DeviceId src = link.src().deviceId();
+ DeviceId dst = link.dst().deviceId();
+
+ return !(obstacles.contains(src) || obstacles.contains(dst));
+ }
+
+ @Override
+ public boolean validate(Path path, LinkResourceService resourceService) {
+ for (Link link : path.links()) {
+ if (!isValid(link, resourceService)) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(obstacles);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+
+ if (!(obj instanceof ObstacleConstraint)) {
+ return false;
+ }
+
+ final ObstacleConstraint that = (ObstacleConstraint) obj;
+ return Objects.equals(this.obstacles, that.obstacles);
+ }
+
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this)
+ .add("obstacles", obstacles)
+ .toString();
+ }
+}