Fix: Make the mapping storage type identical in CLI and REST API

Change-Id: If9cd9bea747c165204428775473e75f05ac27917
diff --git a/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingStoreTypeCompleter.java b/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingStoreTypeCompleter.java
index 2a7c04b..768029e 100755
--- a/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingStoreTypeCompleter.java
+++ b/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingStoreTypeCompleter.java
@@ -16,6 +16,7 @@
 package org.onosproject.mapping.cli;
 
 import com.google.common.collect.ImmutableList;
+import org.apache.commons.lang3.StringUtils;
 import org.onosproject.cli.AbstractChoicesCompleter;
 import org.onosproject.mapping.MappingStore.Type;
 
@@ -29,10 +30,16 @@
 
     private static final List<Type> STORE_TYPES =
             ImmutableList.of(Type.MAP_CACHE, Type.MAP_DATABASE);
+    private static final String MAP_PREFIX = "map_";
 
     @Override
     protected List<String> choices() {
         return STORE_TYPES.stream().map(type ->
-                type.toString().toLowerCase()).collect(Collectors.toList());
+                removeMapPrefix(type.toString().toLowerCase()))
+                .collect(Collectors.toList());
+    }
+
+    private String removeMapPrefix(String type) {
+        return StringUtils.replaceAll(type, MAP_PREFIX, "");
     }
 }
\ No newline at end of file
diff --git a/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingsListCommand.java b/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingsListCommand.java
index de5c0c9..3323bea 100755
--- a/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingsListCommand.java
+++ b/apps/mappingmanagement/cli/src/main/java/org/onosproject/mapping/cli/MappingsListCommand.java
@@ -44,8 +44,8 @@
         description = "Lists mappings")
 public class MappingsListCommand extends AbstractShellCommand {
 
-    private static final String DB = "map_database";
-    private static final String CACHE = "map_cache";
+    private static final String DB = "database";
+    private static final String CACHE = "cache";
 
     private static final String SUMMARY_FORMAT = "deviceId=%s, mappingCount=%d";
     private static final String MAPPING_ID_FORMAT = "  id=%s";
@@ -59,6 +59,9 @@
     private static final String MAPPING_TREATMENT_SHORT_FORMAT = "      %s";
     private static final String JSON_FORMAT = "%s";
 
+    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";
+
     @Argument(index = 0, name = "type",
             description = "Shows mappings with specified type",
             required = true, multiValued = false)
@@ -80,13 +83,7 @@
     @Override
     protected void execute() {
 
-        MappingStore.Type typeEnum = null;
-
-        if (type.equals(DB)) {
-            typeEnum = MappingStore.Type.MAP_DATABASE;
-        } else if (type.equals(CACHE)) {
-            typeEnum = MappingStore.Type.MAP_CACHE;
-        }
+        MappingStore.Type typeEnum = getTypeEnum(type);
 
         DeviceService deviceService = get(DeviceService.class);
         Iterable<Device> devices = deviceService.getDevices();
@@ -184,6 +181,29 @@
     }
 
     /**
+     * Returns corresponding type enumeration based on the given
+     * string formatted type.
+     *
+     * @param type string formatted type
+     * @return type enumeration
+     */
+    private MappingStore.Type getTypeEnum(String type) {
+
+        if (type == null) {
+            throw new IllegalArgumentException(TYPE_NOT_NULL);
+        }
+
+        switch (type) {
+            case DB:
+                return MappingStore.Type.MAP_DATABASE;
+            case CACHE:
+                return MappingStore.Type.MAP_CACHE;
+            default:
+                throw new IllegalArgumentException(TYPE_ILLEGAL);
+        }
+    }
+
+    /**
      * Generates JSON object with the mappings of the given device.
      *
      * @param mapper   object mapper
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
index 6a9acd1..9235f9a 100644
--- 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
@@ -44,7 +44,7 @@
     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 DB = "database";
     private static final String CACHE = "cache";
 
     private final MappingService mappingService = get(MappingService.class);
@@ -83,8 +83,9 @@
      * specified device.
      *
      * @param deviceId device identifier
-     * @param type mapping store type
+     * @param type     mapping store type
      * @return 200 OK with a collection of mappings of given device
+     *
      * @onos.rsModel MappingEntries
      */
     @GET
diff --git a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/MappingsWebResourceTest.java b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/MappingsWebResourceTest.java
index ffbd434..7d8c898 100644
--- a/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/MappingsWebResourceTest.java
+++ b/apps/mappingmanagement/web/src/test/java/org/onosproject/mapping/web/MappingsWebResourceTest.java
@@ -101,6 +101,7 @@
     private static final int DIFF_VALUE = 99;
 
     private static final String ID = "id";
+    private static final String DATABASE = "database";
 
     private static final String PREFIX = "mappings";
 
@@ -412,7 +413,7 @@
                 .andReturn(null).anyTimes();
         replay(mockMappingService);
         final WebTarget wt = target();
-        final String response = wt.path(PREFIX + "/db").request().get(String.class);
+        final String response = wt.path(PREFIX + "/" + DATABASE).request().get(String.class);
         assertThat(response, is("{\"mappings\":[]}"));
     }
 
@@ -426,7 +427,7 @@
                 .andReturn(mappingEntries).once();
         replay(mockMappingService);
         final WebTarget wt = target();
-        final String response = wt.path(PREFIX + "/db").request().get(String.class);
+        final String response = wt.path(PREFIX + "/" + DATABASE).request().get(String.class);
         final JsonObject result = Json.parse(response).asObject();
         assertThat(result, notNullValue());
 
@@ -453,7 +454,7 @@
         replay(mockMappingService);
 
         final WebTarget wt = target();
-        final String response = wt.path(PREFIX + "/" + deviceId1 + "/db")
+        final String response = wt.path(PREFIX + "/" + deviceId1 + "/" + DATABASE)
                 .request().get(String.class);
         assertThat(response, is("{\"mappings\":[]}"));
     }
@@ -472,7 +473,7 @@
         replay(mockMappingService);
 
         final WebTarget wt = target();
-        final String response = wt.path(PREFIX + "/" + deviceId2 + "/db")
+        final String response = wt.path(PREFIX + "/" + deviceId2 + "/" + DATABASE)
                 .request().get(String.class);
 
         final JsonObject result = Json.parse(response).asObject();