Topo2: Change the map background according to the current layout payload

Change-Id: I4170857ee22faf61094ddb1021bbfd9a4b606cdf
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 14b3db9..fa75f14 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
@@ -135,7 +135,7 @@
 
                 // FIXME: leave commented out for now, while still under development
                 // (remember to also comment out inclusions in index.html)
-//                new UiView(NETWORK, "topo2", "New-Topo"),
+                new UiView(NETWORK, "topo2", "New-Topo"),
 
                 new UiView(NETWORK, "device", "Devices", "nav_devs"),
                 new UiViewHidden("flow"),
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 e1d403d..5d6c7d4 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
@@ -102,7 +102,7 @@
 
         // FIXME: this is temporary to prevent unhandled events being set to GUI...
         //         while Topo2 is still under development
-        topoSession.enableEvent(false);
+//        topoSession.enableEvent(false);
     }
 
     @Override
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
index fe59dce..4b12e68 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
@@ -41,6 +41,9 @@
 import org.onosproject.net.statistic.StatisticService;
 import org.onosproject.net.topology.TopologyService;
 import org.onosproject.ui.JsonUtils;
+import org.onosproject.ui.UiExtensionService;
+import org.onosproject.ui.UiTopoMap;
+import org.onosproject.ui.UiTopoMapFactory;
 import org.onosproject.ui.impl.topo.model.UiModelEvent;
 import org.onosproject.ui.model.topo.UiClusterMember;
 import org.onosproject.ui.model.topo.UiDevice;
@@ -104,6 +107,7 @@
     private PortStatisticsService portStatsService;
     private TopologyService topologyService;
     private TunnelService tunnelService;
+    private UiExtensionService uiextService;
 
 
     // NOTE: we'll stick this here for now, but maybe there is a better home?
@@ -132,6 +136,7 @@
         portStatsService = directory.get(PortStatisticsService.class);
         topologyService = directory.get(TopologyService.class);
         tunnelService = directory.get(TunnelService.class);
+        uiextService = directory.get(UiExtensionService.class);
     }
 
     // for unit testing
@@ -203,12 +208,53 @@
     }
 
     private void addBgRef(ObjectNode result, UiTopoLayout layout) {
-        String map = layout.geomap();
-        String spr = layout.sprites();
-        if (map != null) {
-            result.put("bgType", "geo").put("bgId", map);
-        } else if (spr != null) {
-            result.put("bgType", "grid").put("bgId", spr);
+        String mapId = layout.geomap();
+        String sprId = layout.sprites();
+
+        if (mapId != null) {
+            result.put("bgType", "geo").put("bgId", mapId);
+            addMapParameters(result, mapId);
+        } else if (sprId != null) {
+            result.put("bgType", "grid").put("bgId", sprId);
+        }
+    }
+
+    private void addMapParameters(ObjectNode result, String mapId) {
+
+        // TODO: This ought to be written more efficiently.
+
+        // ALSO: Should retrieving a UiTopoMap by ID be something that
+        //       the UiExtensionService provides, along with other
+        //       useful lookups?
+        //
+        //       Or should it remain very basic / general?
+        //
+        //       return uiextService.getTopoMap(String mapId);
+
+        final UiTopoMap[] map = {null};
+
+        uiextService.getExtensions().forEach(ext -> {
+            UiTopoMapFactory factory = ext.topoMapFactory();
+
+            // TODO: use .stream().filter(...) here
+            if (map[0] == null && factory != null) {
+                List<UiTopoMap> topoMaps = factory.geoMaps();
+
+                topoMaps.forEach(m -> {
+                    if (map[0] == null && m.id().equals(mapId)) {
+                        map[0] = m;
+                    }
+                });
+            }
+        });
+
+        UiTopoMap m = map[0];
+        if (m != null) {
+            result.put("bgDesc", m.description())
+                    .put("bgFilePath", m.filePath())
+                    .put("bgDefaultScale", m.scale());
+        } else {
+            result.put("bgWarn", "no map registered with id: " + mapId);
         }
     }
 
diff --git a/web/gui/src/main/webapp/app/view/topo2/topo2Map.js b/web/gui/src/main/webapp/app/view/topo2/topo2Map.js
index 0ab744b..ff595e7 100644
--- a/web/gui/src/main/webapp/app/view/topo2/topo2Map.js
+++ b/web/gui/src/main/webapp/app/view/topo2/topo2Map.js
@@ -41,7 +41,7 @@
         var prefs = currentMap(),
             mapId = prefs.mapid,
             mapFilePath = prefs.mapfilepath,
-            mapScale = prefs.mapscale,
+            mapScale = prefs.mapscale || 1,
             loadMap = ms.loadMapInto,
             promise, cfilter;
 
@@ -94,8 +94,8 @@
 
     function setMap(map) {
         ps.setPrefs('topo_mapid', map);
-        setUpMap();
         opacifyMap(true);
+        return setUpMap();
     }
 
     // TODO: -- START -- Move to dedicated module
@@ -120,6 +120,14 @@
     }
     // TODO: -- END -- Move to dedicated module
 
+    function show() {
+        sus.visible(mapG, true);
+    }
+
+    function hide() {
+        sus.visible(mapG, false);
+    }
+
     function toggle(x) {
         return _togSvgLayer(x, mapG, 'bg', 'background map');
     }
@@ -156,7 +164,11 @@
 
                 return {
                     init: init,
+                    setMap: setMap,
                     openMapSelection: openMapSelection,
+
+                    show: show,
+                    hide: hide,
                     toggle: toggle,
 
                     resetZoom: resetZoom
diff --git a/web/gui/src/main/webapp/app/view/topo2/topo2Region.js b/web/gui/src/main/webapp/app/view/topo2/topo2Region.js
index ba1a8c4..bb8e0db 100644
--- a/web/gui/src/main/webapp/app/view/topo2/topo2Region.js
+++ b/web/gui/src/main/webapp/app/view/topo2/topo2Region.js
@@ -35,8 +35,9 @@
     .factory('Topo2RegionService', [
         '$log', 'Topo2Model', 'Topo2SubRegionService', 'Topo2DeviceService',
         'Topo2HostService', 'Topo2LinkService', 'Topo2ZoomService', 'Topo2DetailsPanelService',
-        'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService',
-        function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls) {
+        'Topo2BreadcrumbService', 'Topo2ViewController', 'Topo2SpriteLayerService', 'Topo2MapService',
+        'Topo2MapConfigService',
+        function ($log, _Model_, t2sr, t2ds, t2hs, t2ls, t2zs, t2dps, t2bcs, ViewController, t2sls, t2ms, t2mcs) {
 
             Model = _Model_;
 
@@ -47,7 +48,23 @@
                 },
                 addLayout: function (data) {
 
-                    // TODO: Dynamically change the Geo Map from the layout data
+                    var _this = this;
+
+                    if (data.bgType === 'geo') {
+                        t2ms.setMap({
+                            mapid: data.bgId,
+
+                            // TODO: The following value will be specified in the Topo2CurrentLayout Payload
+                            mapfilepath: "*bayarea"
+                        }).then(function () {
+                            _.each(_this.regionNodes(), function (node) {
+                                node.resetPosition();
+                            });
+                        });
+                        t2ms.show();
+                    } else {
+                        t2ms.hide();
+                    }
 
                     if (data.bgType === 'grid') {
                         t2sls.loadLayout(data.bgId);
diff --git a/web/gui/src/main/webapp/index.html b/web/gui/src/main/webapp/index.html
index aa751ea..fb0e67e 100644
--- a/web/gui/src/main/webapp/index.html
+++ b/web/gui/src/main/webapp/index.html
@@ -132,7 +132,7 @@
     <link rel="stylesheet" href="app/fw/widget/table-theme.css">
 
     <!-- Under development for Region support. -->
-    <!-- <script src="app/view/topo2/topo2.js"></script>
+    <script src="app/view/topo2/topo2.js"></script>
     <script src="app/view/topo2/topo2Breadcrumb.js"></script>
     <script src="app/view/topo2/topo2Collection.js"></script>
     <script src="app/view/topo2/topo2D3.js"></script>
@@ -170,7 +170,7 @@
     <script src="app/view/topo2/topo2Zoom.js"></script>
     <script src="app/view/topo2/uiView.js"></script>
     <link rel="stylesheet" href="app/view/topo2/topo2.css">
-    <link rel="stylesheet" href="app/view/topo2/topo2-theme.css"> -->
+    <link rel="stylesheet" href="app/view/topo2/topo2-theme.css">
 
 
     <!-- Builtin views javascript. -->