Implemented REST API for multicast RIB.

Change-Id: I5de6fe0aae412083597113e355ba0e8c984be5f1
diff --git a/core/api/src/main/java/org/onosproject/net/mcast/McastStore.java b/core/api/src/main/java/org/onosproject/net/mcast/McastStore.java
index 96b21f6..067f517 100644
--- a/core/api/src/main/java/org/onosproject/net/mcast/McastStore.java
+++ b/core/api/src/main/java/org/onosproject/net/mcast/McastStore.java
@@ -51,7 +51,7 @@
     void storeSink(McastRoute route, ConnectPoint sink, Type operation);
 
     /**
-     * Obtain the source for a multicast route.
+     * Obtains the source for a multicast route.
      *
      * @param route a multicast route
      * @return a connect point
@@ -59,10 +59,17 @@
     ConnectPoint sourceFor(McastRoute route);
 
     /**
-     * Obtain the sinks for a multicast route.
+     * Obtains the sinks for a multicast route.
      *
      * @param route a multicast route
      * @return a set of sinks
      */
     Set<ConnectPoint> sinksFor(McastRoute route);
+
+    /**
+     * Gets the set of all known multicast routes.
+     *
+     * @return set of multicast routes
+     */
+    Set<McastRoute> getRoutes();
 }
diff --git a/core/api/src/main/java/org/onosproject/net/mcast/MulticastRouteService.java b/core/api/src/main/java/org/onosproject/net/mcast/MulticastRouteService.java
index 05bd6cb..64aa842 100644
--- a/core/api/src/main/java/org/onosproject/net/mcast/MulticastRouteService.java
+++ b/core/api/src/main/java/org/onosproject/net/mcast/MulticastRouteService.java
@@ -43,6 +43,13 @@
     void remove(McastRoute route);
 
     /**
+     * Gets all multicast routes in the system.
+     *
+     * @return set of multicast routes
+     */
+    Set<McastRoute> getRoutes();
+
+    /**
      * Adds a source connection to the route from where the
      * data stream is originating.
      *
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
index 99030ca..cbed1c2 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/CodecManager.java
@@ -53,6 +53,7 @@
 import org.onosproject.net.intent.HostToHostIntent;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.net.mcast.McastRoute;
 import org.onosproject.net.meter.Band;
 import org.onosproject.net.meter.Meter;
 import org.onosproject.net.meter.MeterRequest;
@@ -117,6 +118,7 @@
         registerCodec(FilteringObjective.class, new FilteringObjectiveCodec());
         registerCodec(ForwardingObjective.class, new ForwardingObjectiveCodec());
         registerCodec(NextObjective.class, new NextObjectiveCodec());
+        registerCodec(McastRoute.class, new McastRouteCodec());
         log.info("Started");
     }
 
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/McastRouteCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/McastRouteCodec.java
new file mode 100644
index 0000000..58bd95c
--- /dev/null
+++ b/core/common/src/main/java/org/onosproject/codec/impl/McastRouteCodec.java
@@ -0,0 +1,44 @@
+package org.onosproject.codec.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.codec.CodecContext;
+import org.onosproject.codec.JsonCodec;
+import org.onosproject.net.mcast.McastRoute;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Codec to encode and decode a multicast route to and from JSON.
+ */
+public class McastRouteCodec extends JsonCodec<McastRoute> {
+
+    private static final String SOURCE = "source";
+    private static final String GROUP = "group";
+    private static final String TYPE = "type";
+
+    @Override
+    public ObjectNode encode(McastRoute route, CodecContext context) {
+        checkNotNull(route);
+        ObjectNode root = context.mapper().createObjectNode()
+                .put(TYPE, route.type().toString())
+                .put(SOURCE, route.source().toString())
+                .put(GROUP, route.group().toString());
+
+        return root;
+    }
+
+    @Override
+    public McastRoute decode(ObjectNode json, CodecContext context) {
+        if (json == null || !json.isObject()) {
+            return null;
+        }
+
+        IpAddress source = IpAddress.valueOf(json.path(SOURCE).asText());
+        IpAddress group = IpAddress.valueOf(json.path(GROUP).asText());
+
+        McastRoute route = new McastRoute(source, group, McastRoute.Type.STATIC);
+
+        return route;
+    }
+}
diff --git a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
index 13092fe..d5a73c0 100644
--- a/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
+++ b/core/store/serializers/src/main/java/org/onosproject/store/serializers/KryoNamespaces.java
@@ -20,6 +20,7 @@
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Maps;
 
+import com.google.common.collect.Sets;
 import org.onlab.packet.ChassisId;
 import org.onlab.packet.EthType;
 import org.onlab.packet.Ip4Address;
@@ -219,6 +220,7 @@
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.AtomicReference;
 
 public final class KryoNamespaces {
 
@@ -228,6 +230,7 @@
             .register(AtomicBoolean.class)
             .register(AtomicInteger.class)
             .register(AtomicLong.class)
+            .register(AtomicReference.class)
             .register(new ImmutableListSerializer(),
                       ImmutableList.class,
                       ImmutableList.of(1).getClass(),
@@ -245,6 +248,7 @@
             .register(HashMap.class)
             .register(ConcurrentHashMap.class)
             .register(CopyOnWriteArraySet.class)
+            .register(Sets.newConcurrentHashSet().getClass())
             .register(ArrayList.class,
                       LinkedList.class,
                       HashSet.class,
diff --git a/incubator/net/src/main/java/org/onosproject/incubator/net/mcast/impl/MulticastRouteManager.java b/incubator/net/src/main/java/org/onosproject/incubator/net/mcast/impl/MulticastRouteManager.java
index fe23505..e7ba259 100644
--- a/incubator/net/src/main/java/org/onosproject/incubator/net/mcast/impl/MulticastRouteManager.java
+++ b/incubator/net/src/main/java/org/onosproject/incubator/net/mcast/impl/MulticastRouteManager.java
@@ -55,7 +55,6 @@
 
     @Activate
     public void activate() {
-
         eventDispatcher.addSink(McastEvent.class, listenerRegistry);
         store.setDelegate(delegate);
 
@@ -80,6 +79,11 @@
     }
 
     @Override
+    public Set<McastRoute> getRoutes() {
+        return store.getRoutes();
+    }
+
+    @Override
     public void addSource(McastRoute route, ConnectPoint connectPoint) {
         checkNotNull(route, "Route cannot be null");
         checkNotNull(connectPoint, "Source cannot be null");
@@ -94,7 +98,6 @@
 
     }
 
-
     @Override
     public void removeSink(McastRoute route, ConnectPoint connectPoint) {
 
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java
index 3ec0abb..60510fc 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/DistributedMcastStore.java
@@ -6,7 +6,6 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
-import org.onlab.packet.IpAddress;
 import org.onlab.util.KryoNamespace;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.mcast.McastEvent;
@@ -15,12 +14,12 @@
 import org.onosproject.net.mcast.McastStore;
 import org.onosproject.net.mcast.McastStoreDelegate;
 import org.onosproject.store.AbstractStore;
+import org.onosproject.store.serializers.KryoNamespaces;
 import org.onosproject.store.service.ConsistentMap;
 import org.onosproject.store.service.Serializer;
 import org.onosproject.store.service.StorageService;
 import org.slf4j.Logger;
 
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -43,7 +42,7 @@
     private Logger log = getLogger(getClass());
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    private StorageService storageService;
+    protected StorageService storageService;
 
     protected ConsistentMap<McastRoute, MulticastData> mcastRib;
     protected Map<McastRoute, MulticastData> mcastRoutes;
@@ -54,12 +53,13 @@
 
         mcastRib = storageService.<McastRoute, MulticastData>consistentMapBuilder()
                 .withName(MCASTRIB)
-                .withSerializer(Serializer.using(KryoNamespace.newBuilder().register(
+                .withSerializer(Serializer.using(KryoNamespace.newBuilder()
+                        .register(KryoNamespaces.BASIC)
+                        .register(KryoNamespaces.MISC)
+                        .register(
                         MulticastData.class,
                         McastRoute.class,
                         McastRoute.Type.class,
-                        IpAddress.class,
-                        List.class,
                         ConnectPoint.class
                 ).build()))
                 .withRelaxedReadConsistency()
@@ -174,4 +174,9 @@
         return mcastRoutes.getOrDefault(route, MulticastData.empty()).sinks();
     }
 
+    @Override
+    public Set<McastRoute> getRoutes() {
+        return mcastRoutes.keySet();
+    }
+
 }
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java
index 412d6ef..75b5206 100644
--- a/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/mcast/impl/MulticastData.java
@@ -61,10 +61,12 @@
     }
 
     public void appendSink(ConnectPoint sink) {
+        checkNotNull(sink);
         sinks.add(sink);
     }
 
     public boolean removeSink(ConnectPoint sink) {
+        checkNotNull(sink);
         return sinks.remove(sink);
     }
 
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
index 8d02f54..f5312f8 100644
--- a/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
+++ b/web/api/src/main/java/org/onosproject/rest/resources/CoreWebApplication.java
@@ -44,7 +44,8 @@
                 PathsWebResource.class,
                 StatisticsWebResource.class,
                 MetricsWebResource.class,
-                FlowObjectiveWebResource.class
+                FlowObjectiveWebResource.class,
+                MulticastRouteWebResource.class
         );
     }
-}
\ No newline at end of file
+}
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/MulticastRouteWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/MulticastRouteWebResource.java
new file mode 100644
index 0000000..ed26824
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/MulticastRouteWebResource.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright 2016 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.resources;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.net.mcast.McastRoute;
+import org.onosproject.net.mcast.MulticastRouteService;
+import org.onosproject.rest.AbstractWebResource;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URI;
+import java.util.Set;
+
+/**
+ * Manage the multicast routing information.
+ */
+@Path("mcast")
+public class MulticastRouteWebResource extends AbstractWebResource {
+
+    /**
+     * Get all multicast routes.
+     * Returns array of all known multicast routes.
+     *
+     * @return 200 OK
+     * @onos.rsModel McastRoutesGet
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response getRoutes() {
+        Set<McastRoute> routes = get(MulticastRouteService.class).getRoutes();
+        ObjectNode root = encodeArray(McastRoute.class, "routes", routes);
+        return ok(root).build();
+    }
+
+    /**
+     * Create new multicast route.
+     * Creates a new route in the multicast RIB.
+     *
+     * @onos.rsModel McastRoutePost
+     * @param stream multicast route JSON
+     * @return status of the request - CREATED if the JSON is correct,
+     * BAD_REQUEST if the JSON is invalid
+     */
+    @POST
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response createRoute(InputStream stream) {
+        MulticastRouteService service = get(MulticastRouteService.class);
+        try {
+            ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+            McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
+            service.add(route);
+        } catch (IOException ex) {
+            throw new IllegalArgumentException(ex);
+        }
+
+        return Response
+                .created(URI.create(""))
+                .build();
+    }
+
+    /**
+     * Remove a multicast route.
+     * Removes a route from the multicast RIB.
+     *
+     * @param stream multicast route JSON
+     * @onos.rsModel McastRoutePost
+     */
+    @DELETE
+    @Consumes(MediaType.APPLICATION_JSON)
+    @Produces(MediaType.APPLICATION_JSON)
+    public void deleteRoute(InputStream stream) {
+        MulticastRouteService service = get(MulticastRouteService.class);
+        try {
+            ObjectNode jsonTree = (ObjectNode) mapper().readTree(stream);
+            McastRoute route = codec(McastRoute.class).decode(jsonTree, this);
+            service.remove(route);
+        } catch (IOException ex) {
+            throw new IllegalArgumentException(ex);
+        }
+    }
+}
diff --git a/web/api/src/main/resources/definitions/McastRoutePost.json b/web/api/src/main/resources/definitions/McastRoutePost.json
new file mode 100644
index 0000000..3a73adb
--- /dev/null
+++ b/web/api/src/main/resources/definitions/McastRoutePost.json
@@ -0,0 +1,20 @@
+{
+  "type": "object",
+  "title": "route",
+  "required": [
+    "source",
+    "group"
+  ],
+  "properties": {
+    "source": {
+      "type": "string",
+      "example": "10.1.1.0",
+      "description": "Multicast source IP address"
+    },
+    "group": {
+      "type": "string",
+      "example": "10.1.1.0",
+      "description": "Multicast group IP address"
+    }
+  }
+}
diff --git a/web/api/src/main/resources/definitions/McastRoutesGet.json b/web/api/src/main/resources/definitions/McastRoutesGet.json
new file mode 100644
index 0000000..652c2ee
--- /dev/null
+++ b/web/api/src/main/resources/definitions/McastRoutesGet.json
@@ -0,0 +1,42 @@
+{
+  "type": "object",
+  "title": "routes",
+  "required": [
+    "routes"
+  ],
+  "properties": {
+    "routes": {
+      "type": "array",
+      "xml": {
+        "name": "routes",
+        "wrapped": true
+      },
+      "items": {
+        "type": "object",
+        "title": "route",
+        "required": [
+          "source",
+          "group",
+          "type"
+        ],
+        "properties": {
+          "source": {
+            "type": "string",
+            "example": "10.1.1.0",
+            "description": "Multicast source IP address"
+          },
+          "group": {
+            "type": "string",
+            "example": "10.1.1.0",
+            "description": "Multicast group IP address"
+          },
+          "type": {
+            "type": "string",
+            "example": "STATIC",
+            "description": "Type of the multicast route"
+          }
+        }
+      }
+    }
+  }
+}