WIP: Fix ONOS-151 bug that raises exception when the same switch is designated

Now PointToPointIntentCompiler can handle the intent designates two
different ports belonging to the same switch.

Change-Id: Ie1ef4463fec0fc45a216bdb384792cc453a582c8
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 5c8eb94..efdc4bd 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
@@ -18,7 +18,9 @@
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
+import org.onlab.onos.net.ConnectPoint;
 import org.onlab.onos.net.DefaultEdgeLink;
+import org.onlab.onos.net.DefaultLink;
 import org.onlab.onos.net.DefaultPath;
 import org.onlab.onos.net.Link;
 import org.onlab.onos.net.Path;
@@ -31,6 +33,7 @@
 import java.util.List;
 
 import static java.util.Arrays.asList;
+import static org.onlab.onos.net.Link.Type.DIRECT;
 
 /**
  * An intent compiler for {@link org.onlab.onos.net.intent.PointToPointIntent}.
@@ -55,13 +58,21 @@
 
     @Override
     public List<Intent> compile(PointToPointIntent intent) {
-        Path path = getPath(intent, intent.ingressPoint().deviceId(),
-                            intent.egressPoint().deviceId());
+        ConnectPoint ingressPoint = intent.ingressPoint();
+        ConnectPoint egressPoint = intent.egressPoint();
+
+        if (ingressPoint.deviceId().equals(egressPoint.deviceId())) {
+            List<Link> links = asList(new DefaultLink(PID, ingressPoint, egressPoint, DIRECT));
+            return asList(createPathIntent(new DefaultPath(PID, links, 1), intent));
+        }
 
         List<Link> links = new ArrayList<>();
-        links.add(DefaultEdgeLink.createEdgeLink(intent.ingressPoint(), true));
+        Path path = getPath(intent, ingressPoint.deviceId(),
+                egressPoint.deviceId());
+
+        links.add(DefaultEdgeLink.createEdgeLink(ingressPoint, true));
         links.addAll(path.links());
-        links.add(DefaultEdgeLink.createEdgeLink(intent.egressPoint(), false));
+        links.add(DefaultEdgeLink.createEdgeLink(egressPoint, false));
 
         return asList(createPathIntent(new DefaultPath(PID, links, path.cost(),
                                                        path.annotations()), intent));
diff --git a/core/net/src/test/java/org/onlab/onos/net/intent/impl/TestPointToPointIntentCompiler.java b/core/net/src/test/java/org/onlab/onos/net/intent/impl/TestPointToPointIntentCompiler.java
index 20345aa..7f7e6d3 100644
--- a/core/net/src/test/java/org/onlab/onos/net/intent/impl/TestPointToPointIntentCompiler.java
+++ b/core/net/src/test/java/org/onlab/onos/net/intent/impl/TestPointToPointIntentCompiler.java
@@ -16,10 +16,12 @@
 package org.onlab.onos.net.intent.impl;
 
 import org.hamcrest.Matchers;
-//import org.junit.Test;
 import org.junit.Test;
-import org.onlab.onos.core.ApplicationId;
 import org.onlab.onos.TestApplicationId;
+import org.onlab.onos.core.ApplicationId;
+import org.onlab.onos.net.ConnectPoint;
+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.intent.Intent;
@@ -29,11 +31,15 @@
 
 import java.util.List;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.CoreMatchers.notNullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.hasSize;
 import static org.hamcrest.Matchers.is;
+import static org.onlab.onos.net.DeviceId.deviceId;
+import static org.onlab.onos.net.NetTestTools.APP_ID;
 import static org.onlab.onos.net.NetTestTools.connectPoint;
+import static org.onlab.onos.net.PortNumber.portNumber;
 import static org.onlab.onos.net.intent.LinksHaveEntryWithSourceDestinationPairMatcher.linksHasPath;
 
 /**
@@ -138,4 +144,28 @@
             assertThat(reversePathIntent.path().links(), linksHasPath("d8", "d7"));
         }
     }
+
+    /**
+     * Tests compilation of the intent which designates two different ports on the same switch.
+     */
+    @Test
+    public void testSameSwitchDifferentPortsIntentCompilation() {
+        ConnectPoint src = new ConnectPoint(deviceId("1"), portNumber(1));
+        ConnectPoint dst = new ConnectPoint(deviceId("1"), portNumber(2));
+        PointToPointIntent intent = new PointToPointIntent(APP_ID, selector, treatment, src, dst);
+
+        String[] hops = {"1"};
+        PointToPointIntentCompiler sut = makeCompiler(hops);
+
+        List<Intent> compiled = sut.compile(intent);
+
+        assertThat(compiled, hasSize(1));
+        assertThat(compiled.get(0), is(instanceOf(PathIntent.class)));
+        Path path = ((PathIntent) compiled.get(0)).path();
+
+        assertThat(path.links(), hasSize(1));
+        Link link = path.links().get(0);
+        assertThat(link.src(), is(src));
+        assertThat(link.dst(), is(dst));
+    }
 }