Getter REST APIs for Intents, FlowManager, and MatchAction.

Resource and Routable classes are implemented but will need to be registered
with the REST server when the module implementations are in place.

Right now they use default JSON serializers for everything apart from the ID
classes, which couldn't be default serialized. We can write custom serializers
for the objects if the need arises.

ONOS-1878
ONOS-1876
ONOS-1877

Change-Id: I48df3532afcf1e8bec104a52d58e23a6af4cffbe
diff --git a/src/main/java/net/floodlightcontroller/restserver/CustomSerializerHelper.java b/src/main/java/net/floodlightcontroller/restserver/CustomSerializerHelper.java
new file mode 100644
index 0000000..9810563
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/restserver/CustomSerializerHelper.java
@@ -0,0 +1,66 @@
+package net.floodlightcontroller.restserver;
+
+import org.codehaus.jackson.Version;
+import org.codehaus.jackson.map.JsonSerializer;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.map.module.SimpleModule;
+import org.codehaus.jackson.map.ser.CustomSerializerFactory;
+import org.restlet.ext.jackson.JacksonRepresentation;
+import org.restlet.representation.Representation;
+
+/**
+ * Helper class used to dynamically override the default serializers used for
+ * JSON serialization.
+ * <p/>
+ * Serializers can be added to the helper class at runtime using
+ * {@link #addSerializer(Class, JsonSerializer)}. The serializers contained by
+ * the helper class can then be added to a JacksonRepresentation prior to
+ * serializing it using {@link #applySerializers(JacksonRepresentation)}.
+ * <p/>
+ * This class enables the use of custom serializers for Java objects without
+ * having to hardcode the mapping of class to serializer using
+ * annotations on the class. Any serialization annotations on the class will be
+ * overridden by the serializers used here, so different serializers can be
+ * used on the class in different contexts.
+ */
+public class CustomSerializerHelper {
+    private final ObjectMapper mapper;
+    private final SimpleModule customSerializerModule;
+    private CustomSerializerFactory sf;
+
+    /**
+     * Constructor.
+     */
+    public CustomSerializerHelper() {
+        mapper = new ObjectMapper();
+        customSerializerModule = new SimpleModule("custom-serializers", new Version(1, 0, 0, null));
+        mapper.registerModule(customSerializerModule);
+        sf =  new CustomSerializerFactory();
+    }
+
+    /**
+     * Adds a serializer to the set of serializers that will be used for JSON
+     * serialization.
+     *
+     * @param serializer the serializer to add
+     */
+    public <T> void addSerializer(Class<T> clazz, JsonSerializer<T> serializer) {
+        customSerializerModule.addSerializer(serializer);
+        sf.addGenericMapping(clazz, serializer);
+    }
+
+    /**
+     * Applies the list of serializers to the JacksonRepresentation so they
+     * will be used when the object in the representation is serialized
+     *
+     * @param jacksonRepresentation the representation to apply the serializers
+     * to
+     * @return a representation with the custom serializers applied
+     */
+    public Representation applySerializers(JacksonRepresentation<?> jacksonRepresentation) {
+        mapper.registerModule(customSerializerModule);
+        jacksonRepresentation.setObjectMapper(mapper);
+        mapper.setSerializerFactory(sf);
+        return jacksonRepresentation;
+    }
+}