ONOS-790 - REST APIs for paths

Change-Id: I5e08778e75c98c1156df7972af9897b52677d371
diff --git a/web/api/src/main/java/org/onosproject/codec/impl/CodecManager.java b/web/api/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 88fb8dd..4abb5e6 100644
--- a/web/api/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/web/api/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -33,6 +33,7 @@
 import org.onosproject.net.Host;
 import org.onosproject.net.HostLocation;
 import org.onosproject.net.Link;
+import org.onosproject.net.Path;
 import org.onosproject.net.Port;
 import org.onosproject.net.flow.FlowEntry;
 import org.onosproject.net.flow.TrafficSelector;
@@ -86,6 +87,7 @@
         registerCodec(Constraint.class, new ConstraintCodec());
         registerCodec(Topology.class, new TopologyCodec());
         registerCodec(TopologyCluster.class, new TopologyClusterCodec());
+        registerCodec(Path.class, new PathCodec());
         log.info("Started");
     }
 
diff --git a/web/api/src/main/java/org/onosproject/codec/impl/ConnectPointCodec.java b/web/api/src/main/java/org/onosproject/codec/impl/ConnectPointCodec.java
index 41a7629..74349db 100644
--- a/web/api/src/main/java/org/onosproject/codec/impl/ConnectPointCodec.java
+++ b/web/api/src/main/java/org/onosproject/codec/impl/ConnectPointCodec.java
@@ -19,6 +19,8 @@
 import org.onosproject.codec.CodecContext;
 import org.onosproject.codec.JsonCodec;
 import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
@@ -30,9 +32,16 @@
     @Override
     public ObjectNode encode(ConnectPoint point, CodecContext context) {
         checkNotNull(point, "Connect point cannot be null");
-        return context.mapper().createObjectNode()
-                .put("device", point.deviceId().toString())
+        ObjectNode root = context.mapper().createObjectNode()
                 .put("port", point.port().toString());
+
+        if (point.elementId() instanceof DeviceId) {
+            root.put("device", point.deviceId().toString());
+        } else if (point.elementId() instanceof HostId) {
+            root.put("host", point.hostId().toString());
+        }
+
+        return root;
     }
 
 }
diff --git a/web/api/src/main/java/org/onosproject/codec/impl/PathCodec.java b/web/api/src/main/java/org/onosproject/codec/impl/PathCodec.java
new file mode 100644
index 0000000..40530e4
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/codec/impl/PathCodec.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2015 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.onosproject.codec.impl;
+
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.Link;
+import org.onosproject.net.Path;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Path JSON codec.
+ */
+public class PathCodec extends AnnotatedCodec<Path> {
+    @Override
+    public ObjectNode encode(Path path, CodecContext context) {
+        checkNotNull(path, "Path cannot be null");
+        JsonCodec<Link> codec = context.codec(Link.class);
+        ObjectNode result = context.mapper()
+                .createObjectNode()
+                .put("cost", path.cost());
+        ArrayNode jsonLinks = result.putArray("links");
+
+        for (Link link : path.links()) {
+            jsonLinks.add(codec.encode(link, context));
+        }
+
+        return annotate(result, path, context);
+    }
+}
diff --git a/web/api/src/main/java/org/onosproject/rest/PathsWebResource.java b/web/api/src/main/java/org/onosproject/rest/PathsWebResource.java
new file mode 100644
index 0000000..063f6e5
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/PathsWebResource.java
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2015 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.onosproject.rest;
+
+import java.util.Set;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.ElementId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.topology.PathService;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+/**
+ * REST resource for interacting with path calculations.
+ */
+@Path("paths")
+public class PathsWebResource extends AbstractWebResource {
+
+    // Host id format is 00:00:00:00:00:01/-1
+    private static final int VLAN_SEPARATOR_OFFSET = 17;
+    private static final int FIRST_MAC_ADDRESS_SEPARATOR_OFFSET = 2;
+    private static final int SECOND_MAC_ADDRESS_SEPARATOR_OFFSET = 5;
+    private static final int THIRD_MAC_ADDRESS_SEPARATOR_OFFSET = 8;
+
+    /**
+     * Determines if the id appears to be the id of a host.
+     *
+     * @param id id string
+     * @return HostId if the id is valid, null otherwise
+     */
+    private HostId isHostId(String id) {
+
+        if (id.length() < VLAN_SEPARATOR_OFFSET + 1 ||
+            id.charAt(FIRST_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
+            id.charAt(SECOND_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
+            id.charAt(THIRD_MAC_ADDRESS_SEPARATOR_OFFSET) != ':' ||
+            id.charAt(VLAN_SEPARATOR_OFFSET) != '/') {
+            return null;
+        }
+
+        return HostId.hostId(id);
+    }
+
+    /**
+     * Gets the paths between two elements.
+     *
+     * @param src source
+     * @param dst destination
+     * @return path data
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{src}/{dst}")
+    public Response getPath(@PathParam("src") String src,
+                            @PathParam("dst") String dst) {
+        PathService pathService = get(PathService.class);
+
+        ElementId srcElement = isHostId(src);
+        ElementId dstElement = isHostId(dst);
+
+        if (srcElement == null) {
+            // Doesn't look like a host, assume it is a device
+            srcElement = DeviceId.deviceId(src);
+        }
+
+        if (dstElement == null) {
+            // Doesn't look like a host, assume it is a device
+            dstElement = DeviceId.deviceId(dst);
+        }
+
+        Set<org.onosproject.net.Path> paths =
+                pathService.getPaths(srcElement, dstElement);
+        ObjectNode root =
+                    encodeArray(org.onosproject.net.Path.class, "paths", paths);
+        return ok(root).build();
+    }
+
+}
diff --git a/web/api/src/main/webapp/WEB-INF/web.xml b/web/api/src/main/webapp/WEB-INF/web.xml
index 389ad94..d345643 100644
--- a/web/api/src/main/webapp/WEB-INF/web.xml
+++ b/web/api/src/main/webapp/WEB-INF/web.xml
@@ -43,7 +43,8 @@
                 org.onosproject.rest.IntentsWebResource,
                 org.onosproject.rest.FlowsWebResource,
                 org.onosproject.rest.TopologyWebResource,
-                org.onosproject.rest.ConfigResource
+                org.onosproject.rest.ConfigResource,
+                org.onosproject.rest.PathsWebResource
             </param-value>
         </init-param>
         <load-on-startup>1</load-on-startup>