More plumbing of grid coordinates vs. geo coordinates.
- Added background reference parameter to layout command
- send correct location data to client for devices, hosts

Change-Id: Ic00bda76f4e4bc8d3e23e07a08f3bc5367ec85a9
diff --git a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
index b950b9f..8d2a73a 100644
--- a/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
+++ b/core/api/src/main/java/org/onosproject/net/AnnotationKeys.java
@@ -24,6 +24,8 @@
  */
 public final class AnnotationKeys {
 
+    private static final double DEFAULT_VALUE = 1.0;
+
     // Prohibit instantiation
     private AnnotationKeys() {
     }
@@ -47,16 +49,36 @@
     public static final String UI_TYPE = "uiType";
 
     /**
-     * Annotation key for latitude (e.g. latitude of device).
+     * Annotation key for UI location type of device/host
+     * (either 'geo' or 'grid').
+     */
+    public static final String LOC_TYPE = "locType";
+
+    /**
+     * Annotation key for latitude (e.g. latitude of device/host
+     * in a geo-layout).
      */
     public static final String LATITUDE = "latitude";
 
     /**
-     * Annotation key for longitude (e.g. longitude of device).
+     * Annotation key for longitude (e.g. longitude of device/host
+     * in a geo-layout).
      */
     public static final String LONGITUDE = "longitude";
 
     /**
+     * Annotation key for grid-Y (e.g. y-coordinate of device/host
+     * in a grid-layout).
+     */
+    public static final String GRID_Y = "gridY";
+
+    /**
+     * Annotation key for grid-X (e.g. x-coordinate of device/host
+     * in a grid-layout).
+     */
+    public static final String GRID_X = "gridX";
+
+    /**
      * Annotation key for southbound protocol.
      */
     public static final String PROTOCOL = "protocol";
@@ -168,7 +190,7 @@
     /**
      * Returns the value annotated object for the specified annotation key.
      * The annotated value is expected to be String that can be parsed as double.
-     * If parsing fails, the returned value will be 1.0.
+     * If parsing fails, the returned value will be {@value DEFAULT_VALUE}.
      *
      * @param annotated annotated object whose annotated value is obtained
      * @param key       key of annotation
@@ -179,7 +201,7 @@
         try {
             value = Double.parseDouble(annotated.annotations().value(key));
         } catch (NumberFormatException e) {
-            value = 1.0;
+            value = DEFAULT_VALUE;
         }
         return value;
     }
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
index ebcdd82..6fa169e 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicDeviceConfig.java
@@ -35,9 +35,10 @@
 
     @Override
     public boolean isValid() {
-        return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
-                RACK_ADDRESS, OWNER, TYPE, DRIVER, MANUFACTURER, HW_VERSION,
-                SW_VERSION, SERIAL, MANAGEMENT_ADDRESS, DEVICE_KEY_ID);
+        return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
+                GRID_Y, GRID_X, UI_TYPE, RACK_ADDRESS, OWNER, TYPE, DRIVER,
+                MANUFACTURER, HW_VERSION, SW_VERSION, SERIAL,
+                MANAGEMENT_ADDRESS, DEVICE_KEY_ID);
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
index 65a2783..1d18e80 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicElementConfig.java
@@ -48,15 +48,14 @@
     public static final String LONGITUDE = "longitude";
 
     /**
-     * Key for grid X coordinate.
-     */
-    public static final String GRID_X = "gridx";
-
-    /**
      * Key for grid Y coordinate.
      */
-    public static final String GRID_Y = "gridy";
+    public static final String GRID_Y = "gridY";
 
+    /**
+     * Key for grid X coordinate.
+     */
+    public static final String GRID_X = "gridX";
 
     /**
      * Key for rack address.
@@ -196,22 +195,17 @@
     }
 
     /**
-     * Returns element grid x-coordinate.
+     * Returns true if the grid coordinates (gridY and gridX) are set on
+     * this element; false otherwise.
+     * <p>
+     * It is assumed that elements will not be placed at {@code (0,0)}.
+     * If you really need to position the element there, consider setting the
+     * coordinates to something like {@code (0.000001, 0.000001)} instead.
      *
-     * @return element x-coordinate
+     * @return true if grid coordinates are set; false otherwise.
      */
-    public double gridX() {
-        return get(GRID_X, DEFAULT_COORD);
-    }
-
-    /**
-     * Sets the element grid x-coordinate.
-     *
-     * @param x new x-coordinate; null to clear
-     * @return self
-     */
-    public BasicElementConfig gridX(Double x) {
-        return (BasicElementConfig) setOrClear(GRID_X, x);
+    public boolean gridCoordsSet() {
+        return !doubleIsZero(gridY()) || !doubleIsZero(gridX());
     }
 
     /**
@@ -234,6 +228,25 @@
     }
 
     /**
+     * Returns element grid x-coordinate.
+     *
+     * @return element x-coordinate
+     */
+    public double gridX() {
+        return get(GRID_X, DEFAULT_COORD);
+    }
+
+    /**
+     * Sets the element grid x-coordinate.
+     *
+     * @param x new x-coordinate; null to clear
+     * @return self
+     */
+    public BasicElementConfig gridX(Double x) {
+        return (BasicElementConfig) setOrClear(GRID_X, x);
+    }
+
+    /**
      * Returns the element rack address.
      *
      * @return rack address; null if not set
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
index 3758c4e..cb99aaf 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicHostConfig.java
@@ -36,8 +36,8 @@
         // Location and IP addresses can be absent, but if present must be valid.
         this.location();
         this.ipAddresses();
-        return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
-                RACK_ADDRESS, OWNER, IPS, LOCATION);
+        return hasOnlyFields(ALLOWED, NAME, LOC_TYPE, LATITUDE, LONGITUDE,
+                GRID_Y, GRID_Y, UI_TYPE, RACK_ADDRESS, OWNER, IPS, LOCATION);
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopoLayout.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopoLayout.java
index b37f1df..45fdb80 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopoLayout.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopoLayout.java
@@ -202,7 +202,7 @@
 
     /**
      * Sets the name of the sprites definition for this layout. This is the
-     * symbolic name for a "json" file containing a definition of sprites,
+     * symbolic name for a definition of sprites,
      * which render as a symbolic background (e.g. a campus, or floor plan),
      * to be displayed in the topology view, for this layout.
      * <p>
diff --git a/core/api/src/main/java/org/onosproject/ui/topo/TopoConstants.java b/core/api/src/main/java/org/onosproject/ui/topo/TopoConstants.java
index bf1e829..dc5297a 100644
--- a/core/api/src/main/java/org/onosproject/ui/topo/TopoConstants.java
+++ b/core/api/src/main/java/org/onosproject/ui/topo/TopoConstants.java
@@ -47,6 +47,8 @@
         public static final String PROTOCOL = "Protocol";
         public static final String LATITUDE = "Latitude";
         public static final String LONGITUDE = "Longitude";
+        public static final String GRID_Y = "Grid Y";
+        public static final String GRID_X = "Grid X";
         public static final String PORTS = "Ports";
 
         // host details
diff --git a/core/api/src/test/java/org/onosproject/net/config/basics/BasicElementConfigTest.java b/core/api/src/test/java/org/onosproject/net/config/basics/BasicElementConfigTest.java
index f81f462..1c41843 100644
--- a/core/api/src/test/java/org/onosproject/net/config/basics/BasicElementConfigTest.java
+++ b/core/api/src/test/java/org/onosproject/net/config/basics/BasicElementConfigTest.java
@@ -110,6 +110,7 @@
     @Test
     public void defaultGridCoords() {
         print(cfg);
+        assertFalse("grid not origin?", cfg.gridCoordsSet());
         assertEquals("gridx", 0.0, cfg.gridX(), ZERO_THRESHOLD);
         assertEquals("gridy", 0.0, cfg.gridY(), ZERO_THRESHOLD);
     }
@@ -118,6 +119,7 @@
     public void someGridCoords() {
         cfg.gridX(35.0).gridY(49.7);
         print(cfg);
+        assertTrue("grid at origin?", cfg.gridCoordsSet());
         assertEquals("gridx", 35.0, cfg.gridX(), ZERO_THRESHOLD);
         assertEquals("gridy", 49.7, cfg.gridY(), ZERO_THRESHOLD);
     }