[ONOS-6352] Add REST API for querying mapping information

Change-Id: I8dc67ec2bf5bdaeeaa24a430bcfab66da018a854
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingWebResource.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingWebResource.java
deleted file mode 100644
index f1dfb4e..0000000
--- a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingWebResource.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright 2017-present 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.mapping.web;
-
-import org.onosproject.rest.AbstractWebResource;
-
-import javax.ws.rs.GET;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.Response;
-
-/**
- * LISP mapping REST API implementation.
- */
-@Path("mappings")
-public class MappingWebResource extends AbstractWebResource {
-
-    // FIXME: dummy method for generating swagger.json file
-    @GET
-    @Produces(MediaType.APPLICATION_JSON)
-    public Response getMappings() {
-        return null;
-    }
-}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebApplication.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebApplication.java
new file mode 100644
index 0000000..ec1c61a
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebApplication.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2017-present 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.mapping.web;
+
+import org.onlab.rest.AbstractWebApplication;
+
+import java.util.Set;
+
+/**
+ * Mapping management REST APIs web application.
+ */
+public class MappingsWebApplication extends AbstractWebApplication {
+    @Override
+    public Set<Class<?>> getClasses() {
+        return getClasses(MappingsWebResource.class);
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebResource.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebResource.java
new file mode 100644
index 0000000..5c38390
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/MappingsWebResource.java
@@ -0,0 +1,135 @@
+/*
+ * Copyright 2017-present 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.mapping.web;
+
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.util.ItemNotFoundException;
+import org.onosproject.mapping.MappingEntry;
+import org.onosproject.mapping.MappingService;
+import org.onosproject.mapping.MappingStore.Type;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.rest.AbstractWebResource;
+
+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;
+
+/**
+ * ONOS mapping management REST API implementation.
+ */
+@Path("mappings")
+public class MappingsWebResource extends AbstractWebResource {
+
+    private static final String DEVICE_NOT_FOUND = "Device is not found";
+    private static final String TYPE_NOT_NULL = "Mapping store type should not be null";
+    private static final String TYPE_ILLEGAL = "Mapping store type is not correct";
+    private static final String MAPPINGS = "mappings";
+
+    private static final String DB = "db";
+    private static final String CACHE = "cache";
+
+    private final MappingService mappingService = get(MappingService.class);
+    private final DeviceService deviceService = get(DeviceService.class);
+    private final ObjectNode root = mapper().createObjectNode();
+    private final ArrayNode mappingsNode = root.putArray(MAPPINGS);
+
+    /**
+     * Gets all mapping entries. Returns array of all mappings in the system.
+     *
+     * @param type mapping store type
+     * @return 200 OK with a collection of mappings
+     *
+     * @onos.rsModel MappingEntries
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{type}")
+    public Response getMappings(@PathParam("type") String type) {
+
+        final Iterable<MappingEntry> mappingEntries =
+                mappingService.getAllMappingEntries(getTypeEnum(type));
+
+        if (mappingEntries == null || !mappingEntries.iterator().hasNext()) {
+            return ok(root).build();
+        }
+
+        for (final MappingEntry entry : mappingEntries) {
+            mappingsNode.add(codec(MappingEntry.class).encode(entry, this));
+        }
+        return ok(root).build();
+    }
+
+    /**
+     * Gets mapping entries of a device. Returns array of all mappings for the
+     * specified device.
+     *
+     * @param deviceId device identifier
+     * @param type mapping store type
+     * @return 200 OK with a collection of mappings of given device
+     * @onos.rsModel MappingEntries
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    @Path("{deviceId}/{type}")
+    public Response getMappingsByDeviceId(@PathParam("deviceId") String deviceId,
+                                          @PathParam("type") String type) {
+        Device device = deviceService.getDevice(DeviceId.deviceId(deviceId));
+
+        if (device == null) {
+            throw new ItemNotFoundException(DEVICE_NOT_FOUND);
+        }
+
+        final Iterable<MappingEntry> mappingEntries =
+                mappingService.getMappingEntries(getTypeEnum(type), device.id());
+        if (mappingEntries == null || !mappingEntries.iterator().hasNext()) {
+            return ok(root).build();
+        }
+
+        for (final MappingEntry entry : mappingEntries) {
+            mappingsNode.add(codec(MappingEntry.class).encode(entry, this));
+        }
+        return ok(root).build();
+    }
+
+    /**
+     * Returns corresponding type enumeration based on the given
+     * string formatted type.
+     *
+     * @param type string formatted type
+     * @return type enumeration
+     */
+    private Type getTypeEnum(String type) {
+
+        if (type == null) {
+            throw new IllegalArgumentException(TYPE_NOT_NULL);
+        }
+
+        switch (type) {
+            case DB:
+                return Type.MAP_DATABASE;
+            case CACHE:
+                return Type.MAP_CACHE;
+            default:
+                throw new IllegalArgumentException(TYPE_ILLEGAL);
+        }
+    }
+}
diff --git a/apps/mappingmanagement/web/src/main/resources/definitions/MappingEntries.json b/apps/mappingmanagement/web/src/main/resources/definitions/MappingEntries.json
new file mode 100644
index 0000000..3395876
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/resources/definitions/MappingEntries.json
@@ -0,0 +1,143 @@
+{
+  "type": "object",
+  "title": "mappings",
+  "required": [
+    "mappings"
+  ],
+  "properties": {
+    "mappings": {
+      "type": "array",
+      "xml": {
+        "name": "mappings",
+        "wrapped": true
+      },
+      "items": {
+        "type": "object",
+        "title": "mapping",
+        "required": [
+          "id",
+          "state",
+          "deviceId",
+          "key",
+          "value"
+        ],
+        "properties": {
+          "id": {
+            "type": "string",
+            "example": "12103425214920339"
+          },
+          "state": {
+            "type": "string",
+            "example": "ADDED"
+          },
+          "deviceId": {
+            "type": "string",
+            "example": "lisp:1.2.3.4"
+          },
+          "key": {
+            "type": "object",
+            "title": "key",
+            "required": [
+              "address"
+            ],
+            "properties": {
+              "address": {
+                "type": "object",
+                "title": "address",
+                "required": [
+                  "addrType",
+                  "typeValue"
+                ],
+                "properties": {
+                  "addrType": {
+                    "type": "string",
+                    "example": "IPV4"
+                  },
+                  "typeValue": {
+                    "type": "string",
+                    "example": "1.2.3.4"
+                  }
+                }
+              }
+            }
+          },
+          "value": {
+            "type": "object",
+            "title": "value",
+            "required": [
+              "action",
+              "treatments"
+            ],
+            "properties": {
+              "action": {
+                "type": "string",
+                "example": "FORWARD"
+              },
+              "treatments": {
+                "type": "array",
+                "xml": {
+                  "name": "treatments",
+                  "wrapped": true
+                },
+                "items": {
+                  "type": "object",
+                  "title": "treatment",
+                  "required": [
+                    "address",
+                    "instructions"
+                  ],
+                  "properties": {
+                    "address": {
+                      "type": "object",
+                      "title": "address",
+                      "required": [
+                        "addrType",
+                        "typeValue"
+                      ],
+                      "properties": {
+                        "addrType": {
+                          "type": "string",
+                          "example": "IPV4"
+                        },
+                        "typeValue": {
+                          "type": "string",
+                          "example": "1.2.3.4"
+                        }
+                      }
+                    },
+                    "instructions": {
+                      "type": "array",
+                      "xml": {
+                        "name": "instructions",
+                        "wrapped": true
+                      },
+                      "items": {
+                        "type": "object",
+                        "title": "instruction",
+                        "required": [
+                          "instType",
+                          "value"
+                        ],
+                        "properties": {
+                          "instType": {
+                            "type": "string",
+                            "example": "WEIGHT"
+                          },
+                          "value": {
+                            "type": "int32",
+                            "format": "int32",
+                            "example": 1
+                          }
+                        }
+                      }
+                    }
+                  }
+                }
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/apps/mappingmanagement/web/src/main/webapp/WEB-INF/web.xml b/apps/mappingmanagement/web/src/main/webapp/WEB-INF/web.xml
index 680c725..6b3a65c 100644
--- a/apps/mappingmanagement/web/src/main/webapp/WEB-INF/web.xml
+++ b/apps/mappingmanagement/web/src/main/webapp/WEB-INF/web.xml
@@ -18,21 +18,40 @@
          xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
          id="ONOS" version="2.5">
-    <display-name>LISP Mapping REST API v1.0</display-name>
+    <display-name>Mapping Management REST API v1.0</display-name>
+
+    <security-constraint>
+        <web-resource-collection>
+            <web-resource-name>Secured</web-resource-name>
+            <url-pattern>/*</url-pattern>
+        </web-resource-collection>
+        <auth-constraint>
+            <role-name>admin</role-name>
+        </auth-constraint>
+    </security-constraint>
+
+    <security-role>
+        <role-name>admin</role-name>
+    </security-role>
+
+    <login-config>
+        <auth-method>BASIC</auth-method>
+        <realm-name>karaf</realm-name>
+    </login-config>
 
     <servlet>
         <servlet-name>JAX-RS Service</servlet-name>
         <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
         <init-param>
-            <param-name>jersey.config.server.provider.classnames</param-name>
-            <param-value>
-                org.onosproject.mapping.web.MappingWebResource</param-value>
+            <param-name>javax.ws.rs.Application</param-name>
+            <param-value>org.onosproject.mapping.web.MappingsWebApplication</param-value>
         </init-param>
-        <load-on-startup>10</load-on-startup>
+        <load-on-startup>1</load-on-startup>
     </servlet>
 
     <servlet-mapping>
         <servlet-name>JAX-RS Service</servlet-name>
         <url-pattern>/*</url-pattern>
     </servlet-mapping>
+
 </web-app>