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