Adding REST API and CLI for managing UI user preferences.

Change-Id: If5a1b347bcf7443120c2dfbb096fca6695b8f0e2
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