Initial sketch of codecs and REST API approach.
FIxed typos and defects.
ONOS-81

Change-Id: I789444a181abea509c354966545c927e305710d1
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotatedCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotatedCodec.java
new file mode 100644
index 0000000..1a6c511
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotatedCodec.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.codec.JsonCodec;
+import org.onlab.onos.net.Annotated;
+import org.onlab.onos.net.Annotations;
+
+/**
+ * Base JSON codec for annotated entities.
+ */
+public abstract class AnnotatedCodec<T extends Annotated> extends JsonCodec<T> {
+
+    /**
+     * Adds JSON encoding of the given item annotations to the specified node.
+     *
+     * @param node    node to add annotations to
+     * @param entity  annotated entity
+     * @param context encode context
+     * @return the given node
+     */
+    protected ObjectNode annotate(ObjectNode node, T entity, CodecContext context) {
+        if (!entity.annotations().keys().isEmpty()) {
+            JsonCodec<Annotations> codec = context.codec(Annotations.class);
+            node.set("annotations", codec.encode(entity.annotations(), context));
+        }
+        return node;
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotationsCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotationsCodec.java
new file mode 100644
index 0000000..a8d2907
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/AnnotationsCodec.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.codec.JsonCodec;
+import org.onlab.onos.net.Annotations;
+
+/**
+ * Annotations JSON codec.
+ */
+public class AnnotationsCodec extends JsonCodec<Annotations> {
+
+    @Override
+    public ObjectNode encode(Annotations annotations, CodecContext context) {
+        ObjectNode result = context.mapper().createObjectNode();
+        for (String key : annotations.keys()) {
+            result.put(key, annotations.value(key));
+        }
+        return result;
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/CodecManager.java b/web/api/src/main/java/org/onlab/onos/codec/impl/CodecManager.java
new file mode 100644
index 0000000..30be730
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/CodecManager.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Service;
+import org.onlab.onos.codec.CodecService;
+import org.onlab.onos.codec.JsonCodec;
+import org.onlab.onos.net.Annotations;
+import org.onlab.onos.net.ConnectPoint;
+import org.onlab.onos.net.Device;
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.Port;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Implementation of the JSON codec brokering service.
+ */
+@Component(immediate = true)
+@Service
+public class CodecManager implements CodecService {
+
+    private static Logger log = LoggerFactory.getLogger(CodecManager.class);
+
+    private final Map<Class<?>, JsonCodec> codecs = new ConcurrentHashMap<>();
+
+    @Activate
+    public void activate() {
+        codecs.clear();
+        registerCodec(Annotations.class, new AnnotationsCodec());
+        registerCodec(Device.class, new DeviceCodec());
+        registerCodec(Port.class, new PortCodec());
+        registerCodec(ConnectPoint.class, new ConnectPointCodec());
+        registerCodec(Link.class, new LinkCodec());
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deativate() {
+        codecs.clear();
+        log.info("Stopped");
+    }
+
+    @Override
+    public Set<Class<?>> getCodecs() {
+        return ImmutableSet.copyOf(codecs.keySet());
+    }
+
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T> JsonCodec<T> getCodec(Class<T> entityClass) {
+        return codecs.get(entityClass);
+    }
+
+    @Override
+    public <T> void registerCodec(Class<T> entityClass, JsonCodec<T> codec) {
+        codecs.putIfAbsent(entityClass, codec);
+    }
+
+    @Override
+    public void unregisterCodec(Class<?> entityClass) {
+        codecs.remove(entityClass);
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/ConnectPointCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/ConnectPointCodec.java
new file mode 100644
index 0000000..3b5bf02
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/ConnectPointCodec.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.codec.JsonCodec;
+import org.onlab.onos.net.ConnectPoint;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Connection point JSON codec.
+ */
+public class ConnectPointCodec extends JsonCodec<ConnectPoint> {
+
+    @Override
+    public ObjectNode encode(ConnectPoint point, CodecContext context) {
+        checkNotNull(point, "Connect point cannot be null");
+        return context.mapper().createObjectNode()
+                .put("device", point.deviceId().toString())
+                .put("port", point.port().toString());
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/DeviceCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/DeviceCodec.java
new file mode 100644
index 0000000..5835382
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/DeviceCodec.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.net.Device;
+import org.onlab.onos.net.device.DeviceService;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Device JSON codec.
+ */
+public class DeviceCodec extends AnnotatedCodec<Device> {
+
+    @Override
+    public ObjectNode encode(Device device, CodecContext context) {
+        checkNotNull(device, "Device cannot be null");
+        DeviceService service = context.get(DeviceService.class);
+        ObjectNode result = context.mapper().createObjectNode()
+                .put("id", device.id().toString())
+                .put("available", service.isAvailable(device.id()))
+                .put("role", service.getRole(device.id()).toString())
+                .put("mfr", device.manufacturer())
+                .put("hw", device.hwVersion())
+                .put("sw", device.swVersion())
+                .put("serial", device.serialNumber());
+        return annotate(result, device, context);
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/LinkCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/LinkCodec.java
new file mode 100644
index 0000000..ad09663
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/LinkCodec.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.codec.JsonCodec;
+import org.onlab.onos.net.ConnectPoint;
+import org.onlab.onos.net.Link;
+import org.onlab.onos.net.device.DeviceService;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Link JSON codec.
+ */
+public class LinkCodec extends AnnotatedCodec<Link> {
+
+    @Override
+    public ObjectNode encode(Link link, CodecContext context) {
+        checkNotNull(link, "Link cannot be null");
+        DeviceService service = context.get(DeviceService.class);
+        JsonCodec<ConnectPoint> codec = context.codec(ConnectPoint.class);
+        ObjectNode result = context.mapper().createObjectNode();
+        result.set("src", codec.encode(link.src(), context));
+        result.set("dst", codec.encode(link.dst(), context));
+        return annotate(result, link, context);
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/PortCodec.java b/web/api/src/main/java/org/onlab/onos/codec/impl/PortCodec.java
new file mode 100644
index 0000000..4bfcc6a
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/PortCodec.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2014 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.onlab.onos.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.onos.codec.CodecContext;
+import org.onlab.onos.net.Port;
+import org.onlab.onos.net.PortNumber;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Device port JSON codec.
+ */
+public class PortCodec extends AnnotatedCodec<Port> {
+
+    @Override
+    public ObjectNode encode(Port port, CodecContext context) {
+        checkNotNull(port, "Port cannot be null");
+        ObjectNode result = context.mapper().createObjectNode()
+                .put("port", portName(port.number()))
+                .put("isEnabled", port.isEnabled())
+                .put("type", port.type().toString().toLowerCase())
+                .put("portSpeed", port.portSpeed());
+        return annotate(result, port, context);
+    }
+
+    private String portName(PortNumber port) {
+        return port.equals(PortNumber.LOCAL) ? "local" : port.toString();
+    }
+
+}
diff --git a/web/api/src/main/java/org/onlab/onos/codec/impl/package-info.java b/web/api/src/main/java/org/onlab/onos/codec/impl/package-info.java
new file mode 100644
index 0000000..31db517
--- /dev/null
+++ b/web/api/src/main/java/org/onlab/onos/codec/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2014 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.
+ */
+
+/**
+ * Implementations of the codec broker and built-in entity JSON codecs.
+ */
+package org.onlab.onos.codec.impl;
\ No newline at end of file