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));