Adding server-side user preferences.

More work still needs to get done to allow client to process
server-pushed preferences updates.

Change-Id: I6e80e3f3677285cb19cfa3b6240c1b13aac56622
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/MainIndexResource.java b/web/gui/src/main/java/org/onosproject/ui/impl/MainIndexResource.java
index ac1a8bb..dd345b4 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/MainIndexResource.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/MainIndexResource.java
@@ -15,17 +15,22 @@
  */
 package org.onosproject.ui.impl;
 
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableList.Builder;
 import org.onlab.osgi.ServiceNotFoundException;
 import org.onosproject.rest.AbstractInjectionResource;
 import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiPreferencesService;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
+import javax.ws.rs.core.SecurityContext;
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.SequenceInputStream;
@@ -42,12 +47,21 @@
     private static final String INDEX = "index.html";
     private static final String NOT_READY = "not-ready.html";
 
+    private static final String INJECT_USER_START = "<!-- {INJECTED-USER-START} -->";
+    private static final String INJECT_USER_END = "<!-- {INJECTED-USER-END} -->";
+
     private static final String INJECT_CSS_START = "<!-- {INJECTED-STYLESHEETS-START} -->";
     private static final String INJECT_CSS_END = "<!-- {INJECTED-STYLESHEETS-END} -->";
 
     private static final String INJECT_JS_START = "<!-- {INJECTED-JAVASCRIPT-START} -->";
     private static final String INJECT_JS_END = "<!-- {INJECTED-JAVASCRIPT-END} -->";
 
+    private static final byte[] SCRIPT_START = "\n<script>\n".getBytes();
+    private static final byte[] SCRIPT_END = "\n</script>\n\n".getBytes();
+
+    @Context
+    private SecurityContext ctx;
+
     @GET
     @Produces(MediaType.TEXT_HTML)
     public Response getMainIndex() throws IOException {
@@ -62,14 +76,25 @@
         InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
         String index = new String(toByteArray(indexTemplate));
 
-        int p1s = split(index, 0, INJECT_JS_START) - INJECT_JS_START.length();
+        int p0s = split(index,   0, INJECT_USER_START) - INJECT_USER_START.length();
+        int p0e = split(index, p0s, INJECT_USER_END);
+        int p1s = split(index, p0e, INJECT_JS_START) - INJECT_JS_START.length();
         int p1e = split(index, p1s, INJECT_JS_END);
         int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
         int p2e = split(index, p2s, INJECT_CSS_END);
         int p3s = split(index, p2e, null);
 
+        // FIXME: use global opaque auth token to allow secure failover
+        String userName = ctx.getUserPrincipal().getName();
+        String auth = "var onosAuth='" + userName + "';\n";
+
         StreamEnumeration streams =
-                new StreamEnumeration(of(stream(index, 0, p1s),
+                new StreamEnumeration(of(stream(index, 0, p0s),
+                                         new ByteArrayInputStream(SCRIPT_START),
+                                         stream(auth, 0, auth.length()),
+                                         userPreferences(userName),
+                                         new ByteArrayInputStream(SCRIPT_END),
+                                         stream(index, p0e, p1s),
                                          includeJs(service),
                                          stream(index, p1e, p2s),
                                          includeCss(service),
@@ -78,6 +103,15 @@
         return Response.ok(new SequenceInputStream(streams)).build();
     }
 
+    // Produces an input stream including user preferences.
+    private InputStream userPreferences(String userName) {
+        UiPreferencesService service = get(UiPreferencesService.class);
+        ObjectNode prefs = mapper().createObjectNode();
+        service.getPreferences(userName).forEach(prefs::set);
+        String string = "var userPrefs = " + prefs.toString() + ";";
+        return new ByteArrayInputStream(string.getBytes());
+    }
+
     // Produces an input stream including JS injections from all extensions.
     private InputStream includeJs(UiExtensionService service) {
         Builder<InputStream> builder = ImmutableList.builder();
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 3bf6c27..55ff611 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
@@ -16,7 +16,19 @@
 package org.onosproject.ui.impl;
 
 import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.BooleanNode;
+import com.fasterxml.jackson.databind.node.DoubleNode;
+import com.fasterxml.jackson.databind.node.IntNode;
+import com.fasterxml.jackson.databind.node.JsonNodeFactory;
+import com.fasterxml.jackson.databind.node.LongNode;
+import com.fasterxml.jackson.databind.node.NullNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.fasterxml.jackson.databind.node.ShortNode;
+import com.fasterxml.jackson.databind.node.TextNode;
 import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Lists;
 import com.google.common.collect.Maps;
@@ -26,40 +38,49 @@
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
+import org.onlab.util.KryoNamespace;
 import org.onosproject.mastership.MastershipService;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.EventuallyConsistentMap;
+import org.onosproject.store.service.EventuallyConsistentMapEvent;
+import org.onosproject.store.service.EventuallyConsistentMapListener;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.WallClockTimestamp;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
 import org.onosproject.ui.UiMessageHandlerFactory;
+import org.onosproject.ui.UiPreferencesService;
 import org.onosproject.ui.UiTopoOverlayFactory;
 import org.onosproject.ui.UiView;
 import org.onosproject.ui.UiViewHidden;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.google.common.collect.ImmutableList.of;
 import static java.util.stream.Collectors.toSet;
-import static org.onosproject.ui.UiView.Category.NETWORK;
-import static org.onosproject.ui.UiView.Category.PLATFORM;
-
 import static org.onosproject.security.AppGuard.checkPermission;
 import static org.onosproject.security.AppPermission.Type.UI_READ;
 import static org.onosproject.security.AppPermission.Type.UI_WRITE;
+import static org.onosproject.ui.UiView.Category.NETWORK;
+import static org.onosproject.ui.UiView.Category.PLATFORM;
 
 /**
  * Manages the user interface extensions.
  */
 @Component(immediate = true)
 @Service
-public class UiExtensionManager implements UiExtensionService, SpriteService {
+public class UiExtensionManager implements UiExtensionService, UiPreferencesService, SpriteService {
 
     private static final ClassLoader CL = UiExtensionManager.class.getClassLoader();
     private static final String CORE = "core";
     private static final String GUI_ADDED = "guiAdded";
     private static final String GUI_REMOVED = "guiRemoved";
+    private static final String UPDATE_PREFS = "updatePrefs";
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -72,10 +93,19 @@
     // Core views & core extension
     private final UiExtension core = createCoreExtension();
 
-
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected MastershipService mastershipService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected StorageService storageService;
+
+    // User preferences
+    private EventuallyConsistentMap<String, ObjectNode> prefs;
+    private final EventuallyConsistentMapListener<String, ObjectNode> prefsListener =
+            new InternalPrefsListener();
+
+    private final ObjectMapper mapper = new ObjectMapper();
+
     // Creates core UI extension
     private UiExtension createCoreExtension() {
         List<UiView> coreViews = of(
@@ -98,6 +128,7 @@
 
         UiMessageHandlerFactory messageHandlerFactory =
                 () -> ImmutableList.of(
+                        new UserPreferencesMessageHandler(),
                         new TopologyViewMessageHandler(),
                         new DeviceViewMessageHandler(),
                         new LinkViewMessageHandler(),
@@ -128,12 +159,28 @@
 
     @Activate
     public void activate() {
+        KryoNamespace.Builder kryoBuilder = new KryoNamespace.Builder()
+                .register(KryoNamespaces.API)
+                .register(ObjectNode.class, ArrayNode.class,
+                          JsonNodeFactory.class, LinkedHashMap.class,
+                          TextNode.class, BooleanNode.class,
+                          LongNode.class, DoubleNode.class, ShortNode.class,
+                          IntNode.class, NullNode.class);
+
+        prefs = storageService.<String, ObjectNode>eventuallyConsistentMapBuilder()
+                .withName("onos-user-preferences")
+                .withSerializer(kryoBuilder)
+                .withTimestampProvider((k, v) -> new WallClockTimestamp())
+                .withPersistence()
+                .build();
+        prefs.addListener(prefsListener);
         register(core);
         log.info("Started");
     }
 
     @Deactivate
     public void deactivate() {
+        prefs.removeListener(prefsListener);
         UiWebSocketServlet.closeAll();
         unregister(core);
         log.info("Stopped");
@@ -171,6 +218,27 @@
         return views.get(viewId);
     }
 
+    @Override
+    public Set<String> getUserNames() {
+        ImmutableSet.Builder<String> builder = ImmutableSet.builder();
+        prefs.keySet().forEach(k -> builder.add(userName(k)));
+        return builder.build();
+    }
+
+    @Override
+    public Map<String, ObjectNode> getPreferences(String userName) {
+        ImmutableMap.Builder<String, ObjectNode> builder = ImmutableMap.builder();
+        prefs.entrySet().stream()
+                .filter(e -> e.getKey().startsWith(userName + "/"))
+                .forEach(e -> builder.put(keyName(e.getKey()), e.getValue()));
+        return builder.build();
+    }
+
+    @Override
+    public void setPreference(String userName, String preference, ObjectNode value) {
+        prefs.put(key(userName, preference), value);
+    }
+
     // =====================================================================
     // Provisional tracking of sprite definitions
 
@@ -192,4 +260,33 @@
         return sprites.get(name);
     }
 
+    private String key(String userName, String keyName) {
+        return userName + "/" + keyName;
+    }
+
+    private String userName(String key) {
+        return key.split("/")[0];
+    }
+
+    private String keyName(String key) {
+        return key.split("/")[1];
+    }
+
+    // Auxiliary listener to preference map events.
+    private class InternalPrefsListener
+            implements EventuallyConsistentMapListener<String, ObjectNode> {
+        @Override
+        public void event(EventuallyConsistentMapEvent<String, ObjectNode> event) {
+            String userName = userName(event.key());
+            if (event.type() == EventuallyConsistentMapEvent.Type.PUT) {
+                UiWebSocketServlet.sendToUser(userName, UPDATE_PREFS, jsonPrefs());
+            }
+        }
+
+        private ObjectNode jsonPrefs() {
+            ObjectNode json = mapper.createObjectNode();
+            prefs.entrySet().forEach(e -> json.set(keyName(e.getKey()), e.getValue()));
+            return json;
+        }
+    }
 }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
index d2d6705..efe30843 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocket.java
@@ -54,6 +54,7 @@
 
     private Connection connection;
     private FrameConnection control;
+    private String userName;
 
     private final ObjectMapper mapper = new ObjectMapper();
 
@@ -66,9 +67,16 @@
      * Creates a new web-socket for serving data to GUI.
      *
      * @param directory service directory
+     * @param userName user name of the logged-in user
      */
-    public UiWebSocket(ServiceDirectory directory) {
+    public UiWebSocket(ServiceDirectory directory, String userName) {
         this.directory = directory;
+        this.userName = userName;
+    }
+
+    @Override
+    public String userName() {
+        return userName;
     }
 
     /**
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
index 564b07f..a9f349f 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiWebSocketServlet.java
@@ -70,7 +70,10 @@
         if (isStopped) {
             return null;
         }
-        UiWebSocket socket = new UiWebSocket(directory);
+
+        // FIXME: Replace this with globally shared opaque token to allow secure failover
+        String userName = request.getUserPrincipal().getName();
+        UiWebSocket socket = new UiWebSocket(directory, userName);
         synchronized (sockets) {
             sockets.add(socket);
         }
@@ -89,6 +92,20 @@
         }
     }
 
+    /**
+     * Sends the specified message to all the GUI clients of the specified user.
+     *
+     * @param userName user name
+     * @param type     message type
+     * @param payload  message payload
+     */
+    static void sendToUser(String userName, String type, ObjectNode payload) {
+        if (instance != null) {
+            instance.sockets.stream().filter(ws -> userName.equals(ws.userName()))
+                    .forEach(ws -> ws.sendMessage(type, 0, payload));
+        }
+    }
+
     // Task for pruning web-sockets that are idle.
     private class Pruner extends TimerTask {
         @Override
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UserPreferencesMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/UserPreferencesMessageHandler.java
new file mode 100644
index 0000000..cc6fa59
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UserPreferencesMessageHandler.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2016 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.ui.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.UiPreferencesService;
+
+import java.util.Collection;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+
+/**
+ * Message handler for intercepting user preferences messages.
+ */
+class UserPreferencesMessageHandler extends UiMessageHandler {
+
+    private static final String UPDATE_PREFS_REQ = "updatePrefReq";
+    private static final String KEY = "key";
+    private static final String VALUE = "value";
+
+    @Override
+    protected Collection<RequestHandler> createRequestHandlers() {
+        return ImmutableSet.of(new UpdatePreferencesRequest());
+    }
+
+    private final class UpdatePreferencesRequest extends RequestHandler {
+        private UpdatePreferencesRequest() {
+            super(UPDATE_PREFS_REQ);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            if (!isNullOrEmpty(connection().userName())) {
+                UiPreferencesService service = get(UiPreferencesService.class);
+                service.setPreference(connection().userName(),
+                                      payload.get(KEY).asText(),
+                                      (ObjectNode) payload.get(VALUE));
+            }
+        }
+    }
+}