[ONOS-6375] Add skeleton code for Web GUI of mapping management app

Change-Id: I95079e6ac422591e6d5afed59b97d2a248573770
diff --git a/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/MappingsWebApplication.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/MappingsWebApplication.java
new file mode 100644
index 0000000..8caa2b6
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/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.api;
+
+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/api/MappingsWebResource.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/MappingsWebResource.java
new file mode 100644
index 0000000..6a9acd1
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/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.api;
+
+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/java/org/onosproject/mapping/web/api/package-info.java b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/package-info.java
new file mode 100644
index 0000000..f2ee575
--- /dev/null
+++ b/apps/mappingmanagement/web/src/main/java/org/onosproject/mapping/web/api/package-info.java
@@ -0,0 +1,19 @@
+/*
+ * 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.
+ */
+/**
+ * REST APIs for the mapping management app.
+ */
+package org.onosproject.mapping.web.api;
\ No newline at end of file