fix obstacle constraint to support edge links

support paths including edge links (e.g., compilation result of
hostToHostIntent) for the obstacle constraint

Change-Id: I0f82e3cb8e4b88ffd30a9275e5e7dcaf5fbae122
diff --git a/core/api/src/main/java/org/onosproject/net/intent/constraint/ObstacleConstraint.java b/core/api/src/main/java/org/onosproject/net/intent/constraint/ObstacleConstraint.java
index 7908b58..0b0c414 100644
--- a/core/api/src/main/java/org/onosproject/net/intent/constraint/ObstacleConstraint.java
+++ b/core/api/src/main/java/org/onosproject/net/intent/constraint/ObstacleConstraint.java
@@ -64,10 +64,29 @@
     }
 
     private boolean isValid(Link link) {
-        DeviceId src = link.src().deviceId();
-        DeviceId dst = link.dst().deviceId();
+        if (link.type() != Link.Type.EDGE) {
+            DeviceId src = link.src().deviceId();
+            DeviceId dst = link.dst().deviceId();
 
-        return !(obstacles.contains(src) || obstacles.contains(dst));
+            return !(obstacles.contains(src) || obstacles.contains(dst));
+
+        } else {
+
+            boolean isSrc = true;
+            if (link.src().elementId() instanceof DeviceId) {
+
+                DeviceId src = link.src().deviceId();
+                isSrc = !(obstacles.contains(src));
+            }
+
+            boolean isDst = true;
+            if (link.dst().elementId() instanceof DeviceId) {
+                DeviceId dst = link.dst().deviceId();
+                isDst = !(obstacles.contains(dst));
+            }
+
+            return isSrc || isDst;
+        }
     }
 
     @Override
diff --git a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
index dd438c5..3dbb132 100644
--- a/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
+++ b/core/api/src/test/java/org/onosproject/net/intent/constraint/ObstacleConstraintTest.java
@@ -18,12 +18,19 @@
 /**
  * Test for constraint of intermediate nodes not passed.
  */
+import com.google.common.collect.ImmutableSet;
 import com.google.common.testing.EqualsTester;
 import org.junit.Before;
 import org.junit.Test;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.DefaultHost;
 import org.onosproject.net.DefaultLink;
 import org.onosproject.net.DefaultPath;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
 import org.onosproject.net.Path;
 import org.onosproject.net.PortNumber;
 import org.onosproject.net.intent.ResourceContext;
@@ -34,6 +41,7 @@
 import static org.easymock.EasyMock.createMock;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.*;
+import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
 import static org.onosproject.net.DefaultLinkTest.cp;
 import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.Link.Type.DIRECT;
@@ -44,6 +52,8 @@
     private static final DeviceId DID2 = deviceId("of:2");
     private static final DeviceId DID3 = deviceId("of:3");
     private static final DeviceId DID4 = deviceId("of:4");
+    private static final DeviceId DID5 = deviceId("of:5");
+    private static final DeviceId DID6 = deviceId("of:6");
     private static final PortNumber PN1 = PortNumber.portNumber(1);
     private static final PortNumber PN2 = PortNumber.portNumber(2);
     private static final PortNumber PN3 = PortNumber.portNumber(3);
@@ -53,8 +63,13 @@
     private ResourceContext resourceContext;
 
     private Path path;
-    private DefaultLink link2;
+    private Path pathWithEdgeLink;
     private DefaultLink link1;
+    private DefaultLink link2;
+    private DefaultLink edgelink1;
+    private DefaultLink edgelink2;
+    private DefaultHost host1;
+    private DefaultHost host2;
 
     private ObstacleConstraint sut;
 
@@ -74,7 +89,20 @@
                 .dst(cp(DID3, PN4))
                 .type(DIRECT)
                 .build();
+        host1 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:01/None"),
+                MacAddress.valueOf(0), VlanId.vlanId(),
+                new HostLocation(DID5, PN1, 1),
+                ImmutableSet.of(), DefaultAnnotations.EMPTY);
+        host2 = new DefaultHost(PROVIDER_ID, HostId.hostId("00:00:00:00:00:02/None"),
+                MacAddress.valueOf(0), VlanId.vlanId(),
+                new HostLocation(DID6, PN1, 1),
+                ImmutableSet.of(), DefaultAnnotations.EMPTY);
+        edgelink1 = createEdgeLink(host1, true);
+        edgelink2 = createEdgeLink(host2, false);
+
         path = new DefaultPath(PROVIDER_ID, Arrays.asList(link1, link2), 10);
+        pathWithEdgeLink = new DefaultPath(PROVIDER_ID,
+                Arrays.asList(edgelink1, link1, link2, edgelink2), 10);
     }
 
     @Test
@@ -109,4 +137,14 @@
 
         assertThat(sut.validate(path, resourceContext), is(false));
     }
+
+    /**
+     * Test the specified path does not avoid the specified obstacle including edge links.
+     */
+    @Test
+    public void testPathThroughObstacleWithEdgeLink() {
+        sut = new ObstacleConstraint(DID1);
+
+        assertThat(sut.validate(pathWithEdgeLink, resourceContext), is(false));
+    }
 }