ONOS-2582 : fix for reselecting previously selected nodes in the topology view.
Note that this also fixes the race condition with showing selected intent.

Change-Id: Icf3cd168bca985136d3ca6c63d98aa193a476d00
diff --git a/web/gui/src/main/webapp/app/view/topo/topo.js b/web/gui/src/main/webapp/app/view/topo/topo.js
index fa3e1db..a3041a3 100644
--- a/web/gui/src/main/webapp/app/view/topo/topo.js
+++ b/web/gui/src/main/webapp/app/view/topo/topo.js
@@ -29,9 +29,9 @@
     ];
 
     // references to injected services
-    var $scope, $log, $cookies, $loc, fs, ks, zs, gs, ms, sus, flash, wss, ps, th,
-        tds, t3s, tes, tfs, tps, tis, tms, tss, tls, tts, tos, fltr, ttbs, tspr,
-        ttip, tov;
+    var $scope, $log, $loc, $timeout, $cookies,
+        fs, ks, zs, gs, ms, sus, flash, wss, ps, th, tds, t3s, tes, tfs, tps,
+        tis, tms, tss, tls, tts, tos, fltr, ttbs, tspr, ttip, tov;
 
     // DOM elements
     var ovtopo, svg, defs, zoomLayer, mapG, spriteG, forceG, noDevsLayer;
@@ -508,9 +508,14 @@
 
     function topoStartDone() {
         var d = $scope.intentData;
-        if (d) {
-            tts.selectIntent(d);
-        }
+        // give a small delay before attempting to reselect node(s) and stuff
+        // since they have to be re-added to the DOM first...
+        $timeout(function () {
+            tss.reselect();
+            if (d) {
+                tts.selectIntent(d);
+            }
+        }, 200);
     }
 
     // --- Controller Definition -----------------------------------------
@@ -527,7 +532,7 @@
             'TopoToolbarService', 'TopoMapService', 'TopoSpriteService',
             'TooltipService', 'TopoOverlayService',
 
-        function (_$scope_, _$log_, _$loc_, $timeout, _$cookies_, _fs_, mast, _ks_,
+        function (_$scope_, _$log_, _$loc_, _$timeout_, _$cookies_, _fs_, mast, _ks_,
                   _zs_, _gs_, _ms_, _sus_, _flash_, _wss_, _ps_, _th_,
                   _tds_, _t3s_, _tes_,
                   _tfs_, _tps_, _tis_, _tss_, _tls_, _tts_, _tos_, _fltr_,
@@ -548,6 +553,7 @@
             $scope = _$scope_;
             $log = _$log_;
             $loc = _$loc_;
+            $timeout = _$timeout_;
             $cookies = _$cookies_;
             fs = _fs_;
             ks = _ks_;
diff --git a/web/gui/src/main/webapp/app/view/topo/topoSelect.js b/web/gui/src/main/webapp/app/view/topo/topoSelect.js
index 8b554fb..77010df 100644
--- a/web/gui/src/main/webapp/app/view/topo/topoSelect.js
+++ b/web/gui/src/main/webapp/app/view/topo/topoSelect.js
@@ -23,7 +23,7 @@
     'use strict';
 
     // injected refs
-    var $log, fs, wss, tov, tps, tts, ns;
+    var $log, fs, wss, tov, tps, tts, ns, sus;
 
     // api to topoForce
     var api;
@@ -133,6 +133,14 @@
         updateDetail();
     }
 
+    function reselect() {
+        selectOrder.forEach(function (id) {
+            var sel = d3.select('g#' + sus.safeId(id));
+            sel.classed('selected', true);
+        });
+        updateDetail();
+    }
+
     function deselectObject(id) {
         var obj = selections[id];
         if (obj) {
@@ -280,8 +288,9 @@
     .factory('TopoSelectService',
         ['$log', 'FnService', 'WebSocketService', 'TopoOverlayService',
             'TopoPanelService', 'TopoTrafficService', 'NavService',
+            'SvgUtilService',
 
-        function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_) {
+        function (_$log_, _fs_, _wss_, _tov_, _tps_, _tts_, _ns_, _sus_) {
             $log = _$log_;
             fs = _fs_;
             wss = _wss_;
@@ -289,10 +298,13 @@
             tps = _tps_;
             tts = _tts_;
             ns = _ns_;
+            sus = _sus_;
 
             function initSelect(_api_) {
                 api = _api_;
-                setInitialState();
+                if (!selections) {
+                    setInitialState();
+                }
             }
 
             function destroySelect() { }
@@ -315,7 +327,8 @@
                 somethingSelected: somethingSelected,
 
                 clickConsumed: clickConsumed,
-                selectionContext: selectionContext
+                selectionContext: selectionContext,
+                reselect: reselect
             };
         }]);
 }());
diff --git a/web/gui/src/main/webapp/tests/app/view/topo/topoSelect-spec.js b/web/gui/src/main/webapp/tests/app/view/topo/topoSelect-spec.js
index f6a5157..48632a5 100644
--- a/web/gui/src/main/webapp/tests/app/view/topo/topoSelect-spec.js
+++ b/web/gui/src/main/webapp/tests/app/view/topo/topoSelect-spec.js
@@ -43,7 +43,7 @@
             'deselectAll', 'updateDetail',
             'hovered', 'selectOrder',
             'somethingSelected',
-            'clickConsumed', 'selectionContext'
+            'clickConsumed', 'selectionContext', 'reselect'
         ])).toBeTruthy();
     });