ONOS-6372: Topo2 - Refactor "location" data to be consistent
- consistently labeled fields "locType", "latOrY", "longOrX"
- simplified code in topo2NodePosition.js

Change-Id: I73e8daadcc3e6ca3ff45f7f60e7b372ccfd8b045
diff --git a/core/api/src/main/java/org/onosproject/ui/topo/LayoutLocation.java b/core/api/src/main/java/org/onosproject/ui/topo/LayoutLocation.java
index 69be6aa..316d095 100644
--- a/core/api/src/main/java/org/onosproject/ui/topo/LayoutLocation.java
+++ b/core/api/src/main/java/org/onosproject/ui/topo/LayoutLocation.java
@@ -44,7 +44,12 @@
      * Designates the type of location; either geographic or logical grid.
      */
     public enum Type {
-        GEO, GRID
+        GEO, GRID;
+
+        @Override
+        public String toString() {
+            return name().toLowerCase();
+        }
     }
 
     private final String id;
diff --git a/core/api/src/test/java/org/onosproject/ui/topo/LayoutLocationTest.java b/core/api/src/test/java/org/onosproject/ui/topo/LayoutLocationTest.java
index cca430c..fad0f0b 100644
--- a/core/api/src/test/java/org/onosproject/ui/topo/LayoutLocationTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/topo/LayoutLocationTest.java
@@ -38,8 +38,8 @@
     private static final double PI = 3.142;
     private static final double ZERO = 0.0;
 
-    private static final String COMPACT_LL_1 = "foo,GEO,3.142,1.414";
-    private static final String COMPACT_LL_2 = "bar,GRID,1.414,3.142";
+    private static final String COMPACT_LL_1 = "foo,geo,3.142,1.414";
+    private static final String COMPACT_LL_2 = "bar,grid,1.414,3.142";
 
     private static final String COMPACT_LIST = COMPACT_LL_1 + "~" + COMPACT_LL_2;
 
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyResource.java b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyResource.java
index 2f813d0..82fbfa9 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyResource.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyResource.java
@@ -93,8 +93,8 @@
         // TODO: add handling of gridY/gridX if locType is "grid" (not "geo")
 
         try {
-            annot.put("latitude", memento.get("lat").asDouble())
-                    .put("longitude", memento.get("lng").asDouble());
+            annot.put("latitude", memento.get("latOrY").asDouble())
+                 .put("longitude", memento.get("longOrX").asDouble());
             array.add(node);
         } catch (Exception e) {
             log.debug("Skipping geo entry");
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
index 24b1773..e3b4f76 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/TopologyViewMessageHandlerBase.java
@@ -319,21 +319,21 @@
             return;
         }
 
-        String slng = annotations.value(AnnotationKeys.LONGITUDE);
         String slat = annotations.value(AnnotationKeys.LATITUDE);
-        boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
+        String slng = annotations.value(AnnotationKeys.LONGITUDE);
         boolean validLat = slat != null && !slat.equals(NO_GEO_VALUE);
+        boolean validLng = slng != null && !slng.equals(NO_GEO_VALUE);
         if (validLat && validLng) {
             try {
-                double lng = Double.parseDouble(slng);
                 double lat = Double.parseDouble(slat);
+                double lng = Double.parseDouble(slng);
                 ObjectNode loc = objectNode()
-                        .put("type", "geo")
-                        .put("lng", lng)
-                        .put("lat", lat);
+                        .put("locType", "geo")
+                        .put("latOrY", lat)
+                        .put("longOrX", lng);
                 payload.set("location", loc);
             } catch (NumberFormatException e) {
-                log.warn("Invalid geo data: longitude={}, latitude={}", slng, slat);
+                log.warn("Invalid geo data: latitude={}, longitude={}", slat, slng);
             }
         }
     }
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 5c9b608..c7a223f 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
@@ -102,6 +102,10 @@
     private static final String GEO = "geo";
     private static final String GRID = "grid";
     private static final String PEER_LOCATIONS = "peerLocations";
+    private static final String LOCATION = "location";
+    private static final String LOC_TYPE = "locType";
+    private static final String LAT_OR_Y = "latOrY";
+    private static final String LONG_OR_X = "longOrX";
 
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -478,29 +482,29 @@
     }
 
     private void addGeoGridLocation(ObjectNode node, Annotated a) {
-        List<String> lngLat = getAnnotValues(a, LONGITUDE, LATITUDE);
-        List<String> gridYX = getAnnotValues(a, GRID_Y, GRID_X);
+        List<String> latLongData = getAnnotValues(a, LATITUDE, LONGITUDE);
+        List<String> gridYXdata = getAnnotValues(a, GRID_Y, GRID_X);
 
-        if (lngLat != null) {
-            attachLocation(node, "geo", "lng", "lat", lngLat);
-        } else if (gridYX != null) {
-            attachLocation(node, "grid", "gridY", "gridX", gridYX);
+        if (latLongData != null) {
+            attachLocation(node, GEO, latLongData);
+        } else if (gridYXdata != null) {
+            attachLocation(node, GRID, gridYXdata);
         }
     }
 
     private void attachLocation(ObjectNode node, String locType,
-                                String keyA, String keyB, List<String> values) {
+                                List<String> values) {
         try {
-            double valA = Double.parseDouble(values.get(0));
-            double valB = Double.parseDouble(values.get(1));
+            double latOrY = Double.parseDouble(values.get(0));
+            double longOrX = Double.parseDouble(values.get(1));
             ObjectNode loc = objectNode()
-                    .put("type", locType)
-                    .put(keyA, valA)
-                    .put(keyB, valB);
-            node.set("location", loc);
+                    .put(LOC_TYPE, locType)
+                    .put(LAT_OR_Y, latOrY)
+                    .put(LONG_OR_X, longOrX);
+            node.set(LOCATION, loc);
 
         } catch (NumberFormatException e) {
-            log.warn("Invalid {} data: long/Y={}, lat/X={}",
+            log.warn("Invalid {} data: lat/Y={}, long/X={}",
                     locType, values.get(0), values.get(1));
         }
     }
@@ -513,9 +517,9 @@
             ObjectNode o = objectNode();
             for (LayoutLocation ll : locs) {
                 ObjectNode lnode = objectNode()
-                    .put("locType", ll.locType().toString())
-                    .put("latOrY", ll.latOrY())
-                    .put("longOrX", ll.longOrX());
+                    .put(LOC_TYPE, ll.locType().toString())
+                    .put(LAT_OR_Y, ll.latOrY())
+                    .put(LONG_OR_X, ll.longOrX());
                 o.set(ll.id(), lnode);
             }
 
diff --git a/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js b/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js
index 1d71212..7c96cad 100644
--- a/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js
+++ b/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js
@@ -28,6 +28,13 @@
     // Internal state;
     var nearDist = 15;
 
+    function setElCoord(el, coord) {
+        el.fix(true);
+        el.x = el.px = coord[0];
+        el.y = el.py = coord[1];
+        return true;
+    }
+
     function positionNode(node, forUpdate) {
 
         var meta = node.get('metaUi'),
@@ -40,7 +47,7 @@
         // If the node has metaUI data attached, it indicates that the user
         //  has dragged the node to a new position on the view; so we should
         //  respect that above any script-configured position...
-        // (NOTE: This is a slightly different to the original topology code)
+        // (NOTE: This is slightly different to the "classic" topology code)
 
         if (hasMeta) {
             node.fix(true);
@@ -53,33 +60,18 @@
         // LONG/LAT (or GRID) locations for regions/devices/hosts
 
         if (node.nodeType === 'peer-region') {
-            var coord = [0, 0],
-                loc = {};
-
             if (t2bgs.getBackgroundType() === 'geo') {
-
-                var loc = node.get('location'),
-                    type = loc.locType || loc.type;
-
-                node.set({ location: {
-                    type: type.toLowerCase(),
-                    lat: loc.latOrY || loc.lat,
-                    lng: loc.longOrX || loc.lng
-                }});
-
                 setLongLat(node);
-
                 return true;
-            } else {
-                loc.gridX = -20;
-                loc.gridY = 10 * node.index();
-                coord = coordFromXY(loc);
             }
 
-            node.px = node.x = coord[0];
-            node.py = node.y = coord[1];
+            // assumed to be grid
+            var loc = {
+                longOrX: -20,
+                latOrY: 10 * node.index()
+            };
 
-            node.fix(true);
+            setElCoord(node, coordFromXY(loc));
             return;
         }
 
@@ -130,45 +122,29 @@
     }
 
     function setLongLat(el) {
-        var loc = el.get('location'),
-            coord;
+        var loc = el.get('location');
 
-        if (loc && loc.type === 'geo') {
-
-            if (loc.lat === 0 && loc.lng === 0) {
-                return false;
-            }
-
-            coord = coordFromLngLat(loc);
-            el.fix(true);
-            el.x = el.px = coord[0];
-            el.y = el.py = coord[1];
-
-            return true;
+        // bail if no location set
+        if (!loc || (loc.latOrY === 0 && loc.longOrX === 0)) {
+            return false;
         }
 
-        if (loc && loc.type === 'grid') {
-
-            if (loc.gridX === 0 && loc.gridY === 0) {
-                return false;
-            }
-
-            coord = coordFromXY(loc);
-            el.fix(true);
-            el.x = el.px = coord[0];
-            el.y = el.py = coord[1];
-
-            return true;
+        if (loc.locType === 'geo') {
+            return setElCoord(el, coordFromLngLat(loc));
         }
+        if (loc.locType === 'grid') {
+            return setElCoord(el, coordFromXY(loc));
+        }
+
+        return false;
     }
 
     function coordFromLngLat(loc) {
         var p = t2mcs.projection();
-        return p ? p([loc.lng, loc.lat]) : [0, 0];
+        return p ? p([loc.longOrX, loc.latOrY]) : [0, 0];
     }
 
     function coordFromXY(loc) {
-
         var bgWidth = t2sls.getWidth() || 100,
             bgHeight = t2sls.getHeight() || 100;
 
@@ -176,8 +152,8 @@
             yOffset = (1000 - (bgHeight * scale)) / 2;
 
         // 1000 is a hardcoded HTML value of the SVG element (topo2.html)
-        var x = scale * loc.gridX,
-            y = (scale * loc.gridY) + yOffset;
+        var x = scale * loc.longOrX,
+            y = (scale * loc.latOrY) + yOffset;
 
         return [x, y];
     }