Adding REST API and CLI for managing UI user preferences.

Change-Id: If5a1b347bcf7443120c2dfbb096fca6695b8f0e2
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 4677844..f1ca75f 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
@@ -53,7 +53,8 @@
                 MastershipWebResource.class,
                 InvalidConfigExceptionMapper.class,
                 DpisWebResource.class,
-                DiagnosticsWebResource.class
+                DiagnosticsWebResource.class,
+                UiPreferencesWebResource.class
         );
     }
 }
diff --git a/web/api/src/main/java/org/onosproject/rest/resources/UiPreferencesWebResource.java b/web/api/src/main/java/org/onosproject/rest/resources/UiPreferencesWebResource.java
new file mode 100644
index 0000000..e64ec24
--- /dev/null
+++ b/web/api/src/main/java/org/onosproject/rest/resources/UiPreferencesWebResource.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.rest.AbstractWebResource;
+import org.onosproject.ui.UiPreferencesService;
+
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.PUT;
+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;
+import java.io.IOException;
+
+import static org.onlab.util.Tools.nullIsNotFound;
+
+/**
+ * Manage user preferences.
+ */
+@Path("ui/preferences")
+public class UiPreferencesWebResource extends AbstractWebResource {
+
+    /**
+     * Gets all user preferences.
+     *
+     * @return 200 OK with user preferences JSON
+     */
+    @GET
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response download() {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode root = mapper.createObjectNode();
+
+        service.getUserNames().forEach(user -> {
+            ObjectNode prefs = mapper.createObjectNode();
+            root.set(user, prefs);
+            service.getPreferences(user).forEach(prefs::set);
+        });
+        return ok(root).build();
+    }
+
+    /**
+     * Gets user preferences for the given user.
+     *
+     * @param user user name
+     * @return 200 OK with user preferences JSON
+     */
+    @GET
+    @Path("{user}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response download(@PathParam("user") String user) {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode prefs = mapper.createObjectNode();
+        service.getPreferences(user).forEach(prefs::set);
+        return ok(prefs).build();
+    }
+
+    /**
+     * Gets the specified user preferences for the given user.
+     *
+     * @param user user name
+     * @param pref preferences name
+     * @return 200 OK with user preferences JSON
+     */
+    @GET
+    @Path("{user}/{pref}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response download(@PathParam("user") String user,
+                             @PathParam("pref") String pref) {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        return ok(nullIsNotFound(service.getPreference(user, pref), "No such preference")).build();
+    }
+
+    /**
+     * Gets the specified user preferences for the given user.
+     *
+     * @param user    user name
+     * @param pref    preferences name
+     * @param request preferences JSON
+     * @return 200 OK
+     * @throws IOException if given JSON is invalid
+     */
+    @PUT
+    @Path("{user}/{pref}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response upload(@PathParam("user") String user,
+                           @PathParam("pref") String pref,
+                           String request) throws IOException {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        ObjectNode json = (ObjectNode) mapper().readTree(request);
+        service.setPreference(user, pref, json);
+        return Response.ok().build();
+    }
+
+    /**
+     * Removes the specified user preferences for the given user.
+     *
+     * @param user user name
+     * @param pref preferences name
+     * @return 204 no content
+     */
+    @DELETE
+    @Path("{user}/{pref}")
+    @Produces(MediaType.APPLICATION_JSON)
+    public Response remove(@PathParam("user") String user,
+                           @PathParam("pref") String pref) {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        service.setPreference(user, pref, null);
+        return Response.noContent().build();
+    }
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
index f3afa1a..a50fd1b 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
@@ -366,7 +366,11 @@
 
     @Override
     public void setPreference(String username, String key, ObjectNode value) {
-        prefs.put(key(username, key), value);
+        if (value != null) {
+            prefs.put(key(username, key), value);
+        } else {
+            prefs.remove(key(username, key));
+        }
     }
 
     // =====================================================================
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiPreferencesListCommand.java b/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiPreferencesListCommand.java
new file mode 100644
index 0000000..b79a116
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiPreferencesListCommand.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.ui.impl.cli;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.ui.UiPreferencesService;
+
+/**
+ * Lists all UI user preferences.
+ */
+@Command(scope = "onos", name = "ui-prefs",
+        description = "Lists all UI user preferences")
+public class UiPreferencesListCommand extends AbstractShellCommand {
+
+    @Override
+    protected void execute() {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        ObjectMapper mapper = new ObjectMapper();
+        ObjectNode root = mapper.createObjectNode();
+
+        service.getUserNames().forEach(user -> {
+            ObjectNode prefs = mapper.createObjectNode();
+            root.set(user, prefs);
+            service.getPreferences(user).forEach(prefs::set);
+        });
+
+        print("%s", root);
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiViewListCommand.java b/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiViewListCommand.java
new file mode 100644
index 0000000..cea67a0
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/cli/UiViewListCommand.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2015-present Open Networking Foundation
+ *
+ * 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.ui.impl.cli;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.ui.UiExtension;
+import org.onosproject.ui.UiExtensionService;
+
+import java.util.List;
+
+/**
+ * Lists all registered UI views.
+ */
+@Command(scope = "onos", name = "ui-views",
+        description = "Lists all registered UI views")
+public class UiViewListCommand extends AbstractShellCommand {
+
+    private static final String FMT = "id=%s, category=%s, label=%s, icon=%s";
+
+    @Override
+    protected void execute() {
+        UiExtensionService service = get(UiExtensionService.class);
+        if (outputJson()) {
+            print("%s", json(service.getExtensions()));
+        } else {
+            service.getExtensions().forEach(ext -> ext.views()
+                    .forEach(v -> print(FMT, v.id(), v.category(),
+                                        v.label(), v.iconId())));
+        }
+    }
+
+    private JsonNode json(List<UiExtension> extensions) {
+        ObjectMapper mapper = new ObjectMapper();
+        ArrayNode node = mapper.createArrayNode();
+        extensions.forEach(ext -> ext.views()
+                .forEach(v -> node.add(mapper.createObjectNode()
+                                               .put("id", v.id())
+                                               .put("category", v.category().toString())
+                                               .put("label", v.label())
+                                               .put("icon", v.iconId()))));
+        return node;
+    }
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/cli/package-info.java b/web/gui/src/main/java/org/onosproject/ui/impl/cli/package-info.java
new file mode 100644
index 0000000..9b0d604
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/cli/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * Set of CLI commands for interacting with the UI services.
+ */
+package org.onosproject.ui.impl.cli;
\ No newline at end of file
diff --git a/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index 0a82738..4bc2b55 100644
--- a/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/web/gui/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -18,6 +18,14 @@
 
     <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
         <command>
+            <action class="org.onosproject.ui.impl.cli.UiViewListCommand"/>
+        </command>
+
+        <command>
+            <action class="org.onosproject.ui.impl.cli.UiPreferencesListCommand"/>
+        </command>
+
+        <command>
             <action class="org.onosproject.ui.impl.topo.cli.UiCacheMembersCommand"/>
         </command>
         <command>