[ONOS-6958] Add glyph registration java code

Change-Id: I954c790062f8ff5498c34c334827c4f695278a9e
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 9b7472b..bb24c89 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
@@ -48,6 +48,8 @@
 import org.onosproject.store.service.StorageService;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiGlyph;
+import org.onosproject.ui.UiGlyphFactory;
 import org.onosproject.ui.UiMessageHandlerFactory;
 import org.onosproject.ui.UiPreferencesService;
 import org.onosproject.ui.UiSessionToken;
@@ -82,6 +84,8 @@
 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.security.AppPermission.Type.GLYPH_READ;
+import static org.onosproject.security.AppPermission.Type.GLYPH_WRITE;
 import static org.onosproject.ui.UiView.Category.NETWORK;
 import static org.onosproject.ui.UiView.Category.PLATFORM;
 import static org.onosproject.ui.impl.lion.BundleStitcher.generateBundles;
@@ -102,6 +106,8 @@
     private static final String CORE = "core";
     private static final String GUI_ADDED = "guiAdded";
     private static final String GUI_REMOVED = "guiRemoved";
+    private static final String GLYPH_ADDED = "glyphAdded";
+    private static final String GLYPH_REMOVED = "glyphRemoved";
     private static final String UPDATE_PREFS = "updatePrefs";
     private static final String SLASH = "/";
 
@@ -134,6 +140,8 @@
     // List of all extensions
     private final List<UiExtension> extensions = Lists.newArrayList();
 
+    private final List<UiGlyph> glyphs = Lists.newArrayList();
+
     // Map of views to extensions
     private final Map<String, UiExtension> views = Maps.newHashMap();
 
@@ -332,12 +340,46 @@
     }
 
     @Override
+    public synchronized void register(UiGlyphFactory glyphFactory) {
+        checkPermission(GLYPH_WRITE);
+        boolean glyphAdded = false;
+        for (UiGlyph glyph : glyphFactory.glyphs()) {
+            if (!glyphs.contains(glyph)) {
+                glyphs.add(glyph);
+                glyphAdded = true;
+            }
+        }
+        if (glyphAdded) {
+            UiWebSocketServlet.sendToAll(GLYPH_ADDED, null);
+        }
+    }
+
+    @Override
+    public synchronized void unregister(UiGlyphFactory glyphFactory) {
+        checkPermission(GLYPH_WRITE);
+        boolean glyphRemoved = false;
+        for (UiGlyph glyph : glyphFactory.glyphs()) {
+            glyphs.remove(glyph);
+            glyphRemoved = true;
+        }
+        if (glyphRemoved) {
+            UiWebSocketServlet.sendToAll(GLYPH_REMOVED, null);
+        }
+    }
+
+    @Override
     public synchronized List<UiExtension> getExtensions() {
         checkPermission(UI_READ);
         return ImmutableList.copyOf(extensions);
     }
 
     @Override
+    public synchronized List<UiGlyph> getGlyphs() {
+        checkPermission(GLYPH_READ);
+        return ImmutableList.copyOf(glyphs);
+    }
+
+    @Override
     public synchronized UiExtension getViewExtension(String viewId) {
         checkPermission(UI_READ);
         return views.get(viewId);
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 ea7e5ed..32f4981 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
@@ -27,6 +27,7 @@
 import org.onosproject.ui.UiConnection;
 import org.onosproject.ui.UiExtension;
 import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiGlyph;
 import org.onosproject.ui.UiMessageHandler;
 import org.onosproject.ui.UiMessageHandlerFactory;
 import org.onosproject.ui.UiSessionToken;
@@ -77,6 +78,8 @@
 
     private static final String TOPO = "topo";
 
+    private static final String GLYPHS = "glyphs";
+
     private static final long MAX_AGE_MS = 30_000;
 
     private static final byte PING = 0x9;
@@ -436,9 +439,21 @@
             instances.add(instance);
         }
 
+        ArrayNode glyphInstances = arrayNode();
+        UiExtensionService uiExtensionService = directory.get(UiExtensionService.class);
+        for (UiGlyph glyph : uiExtensionService.getGlyphs()) {
+            ObjectNode glyphInstance = objectNode()
+                    .put(GlyphConstants.ID, glyph.id())
+                    .put(GlyphConstants.VIEWBOX, glyph.viewbox())
+                    .put(GlyphConstants.PATH, glyph.path());
+            glyphInstances.add(glyphInstance);
+        }
+
         ObjectNode payload = objectNode();
         payload.set(CLUSTER_NODES, instances);
+        payload.set(GLYPHS, glyphInstances);
         payload.put(USER, userName);
+
         sendMessage(BOOTSTRAP, payload);
     }
 
diff --git a/web/gui/src/main/webapp/app/fw/remote/websocket.js b/web/gui/src/main/webapp/app/fw/remote/websocket.js
index ff6c063..bd3e9e1 100644
--- a/web/gui/src/main/webapp/app/fw/remote/websocket.js
+++ b/web/gui/src/main/webapp/app/fw/remote/websocket.js
@@ -21,7 +21,7 @@
     'use strict';
 
     // injected refs
-    var $log, $loc, fs, ufs, wsock, vs, ls;
+    var $log, $loc, fs, gs, ufs, wsock, vs, ls;
 
     // internal state
     var webSockOpts, // web socket options
@@ -33,6 +33,7 @@
         url, // web socket URL
         clusterNodes = [], // ONOS instances data for failover
         clusterIndex = -1, // the instance to which we are connected
+        glyphs = [],
         connectRetries = 0, // limit our attempts at reconnecting
         openListeners = {}, // registered listeners for websocket open()
         nextListenerId = 1, // internal ID for open listeners
@@ -52,6 +53,13 @@
                     // TODO: add connect info to masthead somewhere
                 }
             });
+            glyphs = data.glyphs;
+            glyphs.forEach(function (d, i) {
+                var gdata = {};
+                gdata['_' + d.id] = d.viewbox;
+                gdata[d.id] = d.path;
+                gs.registerGlyphs(gdata);
+            });
         },
 
         error: function (data) {
@@ -187,6 +195,7 @@
         handlers = {};
         clusterNodes = [];
         clusterIndex = -1;
+        glyphs = [];
         connectRetries = 0;
         openListeners = {};
         nextListenerId = 1;
@@ -324,12 +333,13 @@
     // ===== Definition of module
     angular.module('onosRemote')
     .factory('WebSocketService',
-        ['$log', '$location', 'FnService', 'UrlFnService', 'WSock',
+        ['$log', '$location', 'FnService', 'GlyphService', 'UrlFnService', 'WSock',
 
-        function (_$log_, _$loc_, _fs_, _ufs_, _wsock_) {
+        function (_$log_, _$loc_, _fs_, _gs_, _ufs_, _wsock_) {
             $log = _$log_;
             $loc = _$loc_;
             fs = _fs_;
+            gs = _gs_;
             ufs = _ufs_;
             wsock = _wsock_;
 
diff --git a/web/gui/src/main/webapp/tests/app/fw/util/prefs-spec.js b/web/gui/src/main/webapp/tests/app/fw/util/prefs-spec.js
index 3421efc..91cea6c 100644
--- a/web/gui/src/main/webapp/tests/app/fw/util/prefs-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/util/prefs-spec.js
@@ -20,7 +20,7 @@
 describe('factory: fw/util/prefs.js', function() {
     var $cookies, ps, fs;
 
-    beforeEach(module('onosUtil', 'onosRemote'));
+    beforeEach(module('onosUtil', 'onosSvg', 'onosRemote'));
 
     var mockCookies = {
         foo: 'bar'
diff --git a/web/gui/src/main/webapp/tests/app/fw/util/theme-spec.js b/web/gui/src/main/webapp/tests/app/fw/util/theme-spec.js
index 53df5f7..e6a7f9c 100644
--- a/web/gui/src/main/webapp/tests/app/fw/util/theme-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/util/theme-spec.js
@@ -20,7 +20,7 @@
 describe('factory: fw/util/theme.js', function() {
     var ts, $log, fs;
 
-    beforeEach(module('onosUtil', 'onosRemote'));
+    beforeEach(module('onosUtil', 'onosSvg', 'onosRemote'));
 
     beforeEach(inject(function (ThemeService, _$log_, FnService) {
         ts = ThemeService;