GUI -- implemeted GlyphService.register().

Change-Id: I769294b428d9eb5414b45d1099baee1ab3814528
diff --git a/web/gui/src/main/webapp/app/fw/svg/glyph.js b/web/gui/src/main/webapp/app/fw/svg/glyph.js
index bcb42b5..2afc102 100644
--- a/web/gui/src/main/webapp/app/fw/svg/glyph.js
+++ b/web/gui/src/main/webapp/app/fw/svg/glyph.js
@@ -23,7 +23,8 @@
     'use strict';
 
     var $log,
-        glyphs = d3.map();
+        glyphs = d3.map(),
+        msgGS = 'GlyphService.';
 
     // ----------------------------------------------------------------------
     // Base set of Glyphs...
@@ -119,13 +120,6 @@
 
     // ----------------------------------------------------------------------
 
-    function reg(vbox, data) {
-        d3.map(data).keys().forEach(function (key) {
-            glyphs.set(key, {id: key, vb: vbox, d: data[key]});
-        });
-    }
-
-
     angular.module('onosSvg')
         .factory('GlyphService', ['$log', function (_$log_) {
             $log = _$log_;
@@ -133,14 +127,30 @@
             function init() {
                 // start with a fresh map
                 glyphs = d3.map();
-                reg(birdViewBox, birdData);
-                reg(glyphViewBox, glyphData);
-                reg(badgeViewBox, badgeData);
+                register(birdViewBox, birdData);
+                register(glyphViewBox, glyphData);
+                register(badgeViewBox, badgeData);
             }
 
             function register(viewBox, data, overwrite) {
-                // TODO: register specified glyph definitions
+                var dmap = d3.map(data),
+                    dups = [],
+                    ok, msg;
 
+                dmap.forEach(function (key, value) {
+                    if (!overwrite && glyphs.get(key)) {
+                        dups.push(key);
+                    } else {
+                        glyphs.set(key, {id: key, vb: viewBox, d: value});
+                    }
+                });
+                ok = (dups.length == 0);
+                if (!ok) {
+                    dups.forEach(function (id) {
+                        $log.warn(msgGS + 'register(): ID collision: "'+id+'"');
+                    });
+                }
+                return ok;
             }
 
             function ids() {
diff --git a/web/gui/src/main/webapp/tests/app/fw/svg/glyph-spec.js b/web/gui/src/main/webapp/tests/app/fw/svg/glyph-spec.js
index f6c4036..b6bd404 100644
--- a/web/gui/src/main/webapp/tests/app/fw/svg/glyph-spec.js
+++ b/web/gui/src/main/webapp/tests/app/fw/svg/glyph-spec.js
@@ -22,7 +22,8 @@
 describe('factory: fw/svg/glyph.js', function() {
     var $log, fs, gs;
 
-    var vbBird = '352 224 113 112',
+    var numBaseGlyphs = 11,
+        vbBird = '352 224 113 112',
         vbGlyph = '0 0 110 110',
         vbBadge = '0 0 10 10';
 
@@ -50,7 +51,7 @@
 
     it('should load the base set of glyphs', function () {
         gs.init();
-        expect(gs.ids().length).toEqual(11);
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
     });
 
     function verifyGlyphLoaded(id, vbox, prefix) {
@@ -107,4 +108,64 @@
         gs.init();
         verifyGlyphLoaded('uiAttached', vbBadge, 'M2,2.5a.5,.5');
     });
+
+    // define some glyphs that we want to install
+
+    var testVbox = '0 0 1 1',
+        dTriangle = 'M.5,.2l.3,.6,h-.6z',
+        dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
+        newGlyphs = {
+            triangle: dTriangle,
+            diamond: dDiamond
+        },
+        dupGlyphs = {
+            router: dTriangle,
+            switch: dDiamond
+        },
+        idCollision = 'GlyphService.register(): ID collision: ';
+
+    it('should install new glyphs', function () {
+        gs.init();
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
+        spyOn($log, 'warn');
+
+        var ok = gs.register(testVbox, newGlyphs);
+        expect(ok).toBeTruthy();
+        expect($log.warn).not.toHaveBeenCalled();
+
+        expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
+        verifyGlyphLoaded('triangle', testVbox, 'M.5,.2');
+        verifyGlyphLoaded('diamond', testVbox, 'M.2,.5');
+    });
+
+    it('should not overwrite glyphs with dup IDs', function () {
+        gs.init();
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
+        spyOn($log, 'warn');
+
+        var ok = gs.register(testVbox, dupGlyphs);
+        expect(ok).toBeFalsy();
+        expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
+        expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
+
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
+        // verify original glyphs still exist...
+        verifyGlyphLoaded('router', vbGlyph, 'M10,55A45,45');
+        verifyGlyphLoaded('switch', vbGlyph, 'M10,20a10');
+    });
+
+    it('should replace glyphs if asked nicely', function () {
+        gs.init();
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
+        spyOn($log, 'warn');
+
+        var ok = gs.register(testVbox, dupGlyphs, true);
+        expect(ok).toBeTruthy();
+        expect($log.warn).not.toHaveBeenCalled();
+
+        expect(gs.ids().length).toEqual(numBaseGlyphs);
+        // verify glyphs have been overwritten...
+        verifyGlyphLoaded('router', testVbox, 'M.5,.2');
+        verifyGlyphLoaded('switch', testVbox, 'M.2,.5');
+    });
 });