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/cli/src/main/java/org/onosproject/cli/net/LayoutAddCommand.java b/cli/src/main/java/org/onosproject/cli/net/LayoutAddCommand.java
index 8665a4e..942fe39 100644
--- a/cli/src/main/java/org/onosproject/cli/net/LayoutAddCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/LayoutAddCommand.java
@@ -28,24 +28,28 @@
 import static org.onosproject.ui.model.topo.UiTopoLayoutId.layoutId;
 
 /**
- * Creates a new UI layout.
+ * Add a new UI layout.
  */
 @Command(scope = "onos", name = "layout-add",
-        description = "Creates a new UI layout")
+        description = "Adds a new UI layout.")
 public class LayoutAddCommand extends AbstractShellCommand {
 
-    private static final String FMT = "id=%s, name=%s, type=%s";
-    private static final String FMT_MASTER = "  master=%s";
+    private static final char CODE_GEO = '@';
+    private static final char CODE_GRID = '+';
 
     @Argument(index = 0, name = "id", description = "Layout ID",
             required = true, multiValued = false)
     String id = null;
 
-    @Argument(index = 1, name = "id", description = "Region ID (optional)",
+    @Argument(index = 1, name = "bgref", description = "Background Ref",
+            required = true, multiValued = false)
+    String backgroundRef = null;
+
+    @Argument(index = 2, name = "rid", description = "Region ID (optional)",
             required = false, multiValued = false)
     String regionId = null;
 
-    @Argument(index = 2, name = "id", description = "Parent layout ID (optional)",
+    @Argument(index = 3, name = "plid", description = "Parent layout ID (optional)",
             required = false, multiValued = false)
     String parentId = null;
 
@@ -60,6 +64,28 @@
         UiTopoLayoutId pid = parentId == null ? UiTopoLayoutId.DEFAULT_ID : layoutId(parentId);
 
         UiTopoLayout layout = new UiTopoLayout(layoutId(id)).region(region).parent(pid);
+        setAppropriateBackground(layout, backgroundRef);
         service.addLayout(layout);
     }
+
+    private void setAppropriateBackground(UiTopoLayout layout, String bgRef) {
+        /*
+         * A note about the format of bgref.. it should be one of:
+         *    "."               - signifies no background
+         *    "@{map-id}"       - signifies geo background (map)
+         *    "+{sprite-id}"    - signifies grid background (sprite)
+         *
+         *    For example, "!", "@bayareaGEO", "+segmentRouting"
+         */
+        char type = bgRef.charAt(0);
+
+        if (type == CODE_GEO) {
+            // GEO (map) reference
+            layout.geomap(bgRef.substring(1));
+
+        } else if (type == CODE_GRID) {
+            // Grid (sprite) reference
+            layout.sprites(bgRef.substring(1));
+        }
+    }
 }
diff --git a/cli/src/main/java/org/onosproject/cli/net/LayoutListCommand.java b/cli/src/main/java/org/onosproject/cli/net/LayoutListCommand.java
index 8ba13f2..69588d3 100644
--- a/cli/src/main/java/org/onosproject/cli/net/LayoutListCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/LayoutListCommand.java
@@ -36,7 +36,7 @@
         description = "List layout details")
 public class LayoutListCommand extends AbstractShellCommand {
 
-    private static final String FMT = "id=%s, region=%s, parent=%s";
+    private static final String FMT = "id=%s, bgref=%s, region=%s, parent=%s";
 
     @Argument(index = 0, name = "id", description = "Layout ID",
             required = false, multiValued = false)
@@ -68,7 +68,17 @@
     }
 
     private void printLayout(UiTopoLayout layout) {
-        print(FMT, layout.id(), layout.regionId(),
-              layout.parent() != null ? layout.parent().id() : "none");
+        String map = layout.geomap();
+        String spr = layout.sprites();
+        String bgRef = ".";
+        if (map != null) {
+            bgRef = "@" + map;
+        } else if (spr != null) {
+            bgRef = "+" + spr;
+        }
+
+        String pid = layout.parent() != null ? layout.parent().id() : "(none)";
+
+        print(FMT, layout.id(), bgRef, layout.regionId(), pid);
     }
 }
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);
     }
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
index a9da3cc..ce3ee1a 100644
--- a/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/BasicDeviceOperator.java
@@ -19,25 +19,19 @@
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.Device;
 import org.onosproject.net.SparseAnnotations;
-import org.onosproject.net.config.ConfigOperator;
 import org.onosproject.net.config.basics.BasicDeviceConfig;
 import org.onosproject.net.device.DefaultDeviceDescription;
 import org.onosproject.net.device.DeviceDescription;
-import org.slf4j.Logger;
 
 import java.util.Objects;
 
 import static com.google.common.base.Preconditions.checkNotNull;
-import static org.slf4j.LoggerFactory.getLogger;
 
 /**
  * Implementations of merge policies for various sources of device configuration
  * information. This includes applications, providers, and network configurations.
  */
-public final class BasicDeviceOperator implements ConfigOperator {
-
-    protected static final double DEFAULT_COORD = -1.0;
-    private static final Logger log = getLogger(BasicDeviceOperator.class);
+public final class BasicDeviceOperator extends BasicElementOperator {
 
     private BasicDeviceOperator() {
     }
@@ -46,83 +40,76 @@
      * Generates a DeviceDescription containing fields from a DeviceDescription and
      * a DeviceConfig.
      *
-     * @param bdc   the device config entity from network config
+     * @param cfg   the device config entity from network config
      * @param descr a DeviceDescription
      * @return DeviceDescription based on both sources
      */
-    public static DeviceDescription combine(BasicDeviceConfig bdc, DeviceDescription descr) {
-        if (bdc == null || descr == null) {
+    public static DeviceDescription combine(BasicDeviceConfig cfg, DeviceDescription descr) {
+        if (cfg == null || descr == null) {
             return descr;
         }
 
         Device.Type type = descr.type();
-        if (bdc.type() != null && bdc.type() != type) {
-            type = bdc.type();
+        if (cfg.type() != null && cfg.type() != type) {
+            type = cfg.type();
         }
         String manufacturer = descr.manufacturer();
-        if (bdc.manufacturer() != null && !bdc.manufacturer().equals(manufacturer)) {
-            manufacturer = bdc.manufacturer();
+        if (cfg.manufacturer() != null && !cfg.manufacturer().equals(manufacturer)) {
+            manufacturer = cfg.manufacturer();
         }
         String hwVersion = descr.hwVersion();
-        if (bdc.hwVersion() != null && !bdc.hwVersion().equals(hwVersion)) {
-            hwVersion = bdc.hwVersion();
+        if (cfg.hwVersion() != null && !cfg.hwVersion().equals(hwVersion)) {
+            hwVersion = cfg.hwVersion();
         }
         String swVersion = descr.swVersion();
-        if (bdc.swVersion() != null && !bdc.swVersion().equals(swVersion)) {
-            swVersion = bdc.swVersion();
+        if (cfg.swVersion() != null && !cfg.swVersion().equals(swVersion)) {
+            swVersion = cfg.swVersion();
         }
         String serial = descr.serialNumber();
-        if (bdc.serial() != null && !bdc.serial().equals(serial)) {
-            serial = bdc.serial();
+        if (cfg.serial() != null && !cfg.serial().equals(serial)) {
+            serial = cfg.serial();
         }
 
-        SparseAnnotations sa = combine(bdc, descr.annotations());
+        SparseAnnotations sa = combine(cfg, descr.annotations());
         return new DefaultDeviceDescription(descr.deviceUri(), type, manufacturer,
-                                            hwVersion, swVersion,
-                                            serial, descr.chassisId(),
-                                            descr.isDefaultAvailable(), sa);
+                hwVersion, swVersion,
+                serial, descr.chassisId(),
+                descr.isDefaultAvailable(), sa);
     }
 
     /**
      * Generates an annotation from an existing annotation and DeviceConfig.
      *
-     * @param bdc the device config entity from network config
+     * @param cfg the device config entity from network config
      * @param an  the annotation
      * @return annotation combining both sources
      */
-    public static SparseAnnotations combine(BasicDeviceConfig bdc, SparseAnnotations an) {
-        DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
-        if (!Objects.equals(bdc.driver(), an.value(AnnotationKeys.DRIVER))) {
-            newBuilder.set(AnnotationKeys.DRIVER, bdc.driver());
+    public static SparseAnnotations combine(BasicDeviceConfig cfg, SparseAnnotations an) {
+        DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
+        if (!Objects.equals(cfg.driver(), an.value(AnnotationKeys.DRIVER))) {
+            builder.set(AnnotationKeys.DRIVER, cfg.driver());
         }
-        if (bdc.name() != null) {
-            newBuilder.set(AnnotationKeys.NAME, bdc.name());
+
+        combineElementAnnotations(cfg, builder);
+
+        if (cfg.managementAddress() != null) {
+            builder.set(AnnotationKeys.MANAGEMENT_ADDRESS, cfg.managementAddress());
         }
-        if (bdc.uiType() != null) {
-            newBuilder.set(AnnotationKeys.UI_TYPE, bdc.uiType());
-        }
-        if (bdc.geoCoordsSet()) {
-            newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(bdc.latitude()));
-            newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(bdc.longitude()));
-        }
-        if (bdc.rackAddress() != null) {
-            newBuilder.set(AnnotationKeys.RACK_ADDRESS, bdc.rackAddress());
-        }
-        if (bdc.owner() != null) {
-            newBuilder.set(AnnotationKeys.OWNER, bdc.owner());
-        }
-        if (bdc.managementAddress() != null) {
-            newBuilder.set(AnnotationKeys.MANAGEMENT_ADDRESS, bdc.managementAddress());
-        }
-        DefaultAnnotations newAnnotations = newBuilder.build();
-        return DefaultAnnotations.union(an, newAnnotations);
+
+        return DefaultAnnotations.union(an, builder.build());
     }
 
+    /**
+     * Returns a description of the given device.
+     *
+     * @param device the device
+     * @return a description of the device
+     */
     public static DeviceDescription descriptionOf(Device device) {
         checkNotNull(device, "Must supply non-null Device");
         return new DefaultDeviceDescription(device.id().uri(), device.type(),
-                                            device.manufacturer(), device.hwVersion(),
-                                            device.swVersion(), device.serialNumber(),
-                                            device.chassisId(), (SparseAnnotations) device.annotations());
+                device.manufacturer(), device.hwVersion(),
+                device.swVersion(), device.serialNumber(),
+                device.chassisId(), (SparseAnnotations) device.annotations());
     }
 }
diff --git a/core/net/src/main/java/org/onosproject/net/device/impl/BasicElementOperator.java b/core/net/src/main/java/org/onosproject/net/device/impl/BasicElementOperator.java
new file mode 100644
index 0000000..9177f51
--- /dev/null
+++ b/core/net/src/main/java/org/onosproject/net/device/impl/BasicElementOperator.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright 2017-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.net.device.impl;
+
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.DefaultAnnotations;
+import org.onosproject.net.config.ConfigOperator;
+import org.onosproject.net.config.basics.BasicElementConfig;
+
+/**
+ * Abstract base implementation for element operators.
+ */
+public abstract class BasicElementOperator implements ConfigOperator {
+
+    /**
+     * Sets all defined values from the element config on the supplied
+     * annotations builder.
+     *
+     * @param cfg     element configuration
+     * @param builder annotations builder
+     */
+    protected static void combineElementAnnotations(BasicElementConfig cfg,
+                                                    DefaultAnnotations.Builder builder) {
+
+        if (cfg.name() != null) {
+            builder.set(AnnotationKeys.NAME, cfg.name());
+        }
+        if (cfg.uiType() != null) {
+            builder.set(AnnotationKeys.UI_TYPE, cfg.uiType());
+        }
+
+        if (cfg.locType() != null) {
+            builder.set(AnnotationKeys.LOC_TYPE, cfg.locType());
+        }
+        if (cfg.geoCoordsSet()) {
+            builder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
+            builder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
+        } else if (cfg.gridCoordsSet()) {
+            builder.set(AnnotationKeys.GRID_Y, Double.toString(cfg.gridY()));
+            builder.set(AnnotationKeys.GRID_X, Double.toString(cfg.gridX()));
+        }
+
+        if (cfg.rackAddress() != null) {
+            builder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
+        }
+        if (cfg.owner() != null) {
+            builder.set(AnnotationKeys.OWNER, cfg.owner());
+        }
+    }
+}
diff --git a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
index a35eecf..97b87f0 100644
--- a/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
+++ b/core/net/src/main/java/org/onosproject/net/host/impl/BasicHostOperator.java
@@ -16,13 +16,12 @@
 package org.onosproject.net.host.impl;
 
 import org.onlab.packet.IpAddress;
-import org.onosproject.net.AnnotationKeys;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DefaultAnnotations;
 import org.onosproject.net.HostLocation;
 import org.onosproject.net.SparseAnnotations;
-import org.onosproject.net.config.ConfigOperator;
 import org.onosproject.net.config.basics.BasicHostConfig;
+import org.onosproject.net.device.impl.BasicElementOperator;
 import org.onosproject.net.host.DefaultHostDescription;
 import org.onosproject.net.host.HostDescription;
 
@@ -32,7 +31,7 @@
  * Implementations of merge policies for various sources of host configuration
  * information. This includes applications, providers, and network configurations.
  */
-public final class BasicHostOperator implements ConfigOperator {
+public final class BasicHostOperator extends BasicElementOperator {
 
     private BasicHostOperator() {
     }
@@ -65,36 +64,22 @@
 
         SparseAnnotations sa = combine(cfg, descr.annotations());
         return new DefaultHostDescription(descr.hwAddress(), descr.vlan(),
-                                          location, ipAddresses,
-                                          descr.configured(), sa);
+                location, ipAddresses,
+                descr.configured(), sa);
     }
 
     /**
      * Generates an annotation from an existing annotation and HostConfig.
      *
-     * @param cfg the device config entity from network config
+     * @param cfg the host config entity from network config
      * @param an  the annotation
      * @return annotation combining both sources
      */
-    public static SparseAnnotations combine(BasicHostConfig cfg,
-                                            SparseAnnotations an) {
-        DefaultAnnotations.Builder newBuilder = DefaultAnnotations.builder();
-        if (cfg.name() != null) {
-            newBuilder.set(AnnotationKeys.NAME, cfg.name());
-        }
-        if (cfg.uiType() != null) {
-            newBuilder.set(AnnotationKeys.UI_TYPE, cfg.uiType());
-        }
-        if (cfg.geoCoordsSet()) {
-            newBuilder.set(AnnotationKeys.LATITUDE, Double.toString(cfg.latitude()));
-            newBuilder.set(AnnotationKeys.LONGITUDE, Double.toString(cfg.longitude()));
-        }
-        if (cfg.rackAddress() != null) {
-            newBuilder.set(AnnotationKeys.RACK_ADDRESS, cfg.rackAddress());
-        }
-        if (cfg.owner() != null) {
-            newBuilder.set(AnnotationKeys.OWNER, cfg.owner());
-        }
-        return DefaultAnnotations.union(an, newBuilder.build());
+    public static SparseAnnotations combine(BasicHostConfig cfg, SparseAnnotations an) {
+        DefaultAnnotations.Builder builder = DefaultAnnotations.builder();
+
+        combineElementAnnotations(cfg, builder);
+
+        return DefaultAnnotations.union(an, builder.build());
     }
 }
diff --git a/tools/test/topos/regions-bayarea-grid.sh b/tools/test/topos/regions-bayarea-grid.sh
index 2c700c4..923b7d5 100755
--- a/tools/test/topos/regions-bayarea-grid.sh
+++ b/tools/test/topos/regions-bayarea-grid.sh
@@ -262,14 +262,14 @@
 
 ### Add layouts, associating backing regions, and optional parent.
 #
-# layout-add <layout-id> <region-id(opt)> <parent-layout-id(opt)>
+# layout-add <layout-id> <bg-ref> <region-id(opt)> <parent-layout-id(opt)>
 
 onos ${host} <<-EOF
 
-layout-add lC01 c01
-layout-add lC02 c02
-layout-add lC03 c03
-layout-add lC04 c04
+layout-add lC01 +segmentRouting c01
+layout-add lC02 @bayareaGEO c02
+layout-add lC03 . c03
+layout-add lC04 . c04
 
 layouts
 
diff --git a/tools/test/topos/regions-bayarea.sh b/tools/test/topos/regions-bayarea.sh
index c73cd3e..ae0ef10 100755
--- a/tools/test/topos/regions-bayarea.sh
+++ b/tools/test/topos/regions-bayarea.sh
@@ -266,10 +266,10 @@
 
 onos ${host} <<-EOF
 
-layout-add lC01 c01
-layout-add lC02 c02
-layout-add lC03 c03
-layout-add lC04 c04
+layout-add lC01 . c01
+layout-add lC02 . c02
+layout-add lC03 . c03
+layout-add lC04 . c04
 
 layouts
 
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 fa2613b..2f813d0 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
@@ -40,10 +40,23 @@
 @Path("topology")
 public class TopologyResource extends BaseResource {
 
+    private static final String ID = "id";
+    private static final String URI = "uri";
+
+    // length of a MAC defined as a string ... "xx:xx:xx:xx:xx:xx"
+    private static final int MAC_LEN = 17;
+    private static final char SLASH_CHAR = '/';
+
     private static final Logger log = getLogger(TopologyResource.class);
 
     private final ObjectMapper mapper = new ObjectMapper();
 
+    /**
+     * Returns the location data associated with devices and hosts, that is
+     * currently cached in the Meta-UI store.
+     *
+     * @return cached location data for devices and hosts
+     */
     @Path("geoloc")
     @GET
     @Produces("application/json")
@@ -55,10 +68,10 @@
         Map<String, ObjectNode> metaUi = TopologyViewMessageHandler.getMetaUi();
         for (String id : metaUi.keySet()) {
             ObjectNode memento = metaUi.get(id);
-            if (id.length() > 17 && id.charAt(17) == '/') {
-                addGeoData(hosts, "id", id, memento);
+            if (isHostId(id)) {
+                addGeoData(hosts, ID, id, memento);
             } else {
-                addGeoData(devices, "uri", id, memento);
+                addGeoData(devices, URI, id, memento);
             }
         }
 
@@ -67,11 +80,18 @@
         return Response.ok(rootNode.toString()).build();
     }
 
+    private boolean isHostId(String id) {
+        return id.length() > MAC_LEN && id.charAt(MAC_LEN) == SLASH_CHAR;
+    }
+
     private void addGeoData(ArrayNode array, String idField, String id,
                             ObjectNode memento) {
         ObjectNode node = mapper.createObjectNode().put(idField, id);
         ObjectNode annot = mapper.createObjectNode();
         node.set("annotations", annot);
+
+        // 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());
@@ -81,14 +101,22 @@
         }
     }
 
+    /**
+     * Stores sprite data for retrieval by the UI Topology View.
+     *
+     * @param stream input data stream (typically from an uploaded file).
+     * @return REST response
+     * @throws IOException if there is an issue reading from the stream
+     * @deprecated since Junco (1.9), in favor of client-side defined sprite layers
+     */
     @Path("sprites")
     @POST
     @Consumes("application/json")
+    @Deprecated
     public Response setSprites(InputStream stream) throws IOException {
         JsonNode root = mapper.readTree(stream);
         String name = root.path("defn_name").asText("sprites");
         get(SpriteService.class).put(name, root);
         return Response.ok().build();
     }
-
 }
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 0ad0585..bd2574b 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
@@ -365,7 +365,7 @@
                 double lng = Double.parseDouble(slng);
                 double lat = Double.parseDouble(slat);
                 ObjectNode loc = objectNode()
-                        .put("type", "lnglat")
+                        .put("type", "geo")
                         .put("lng", lng)
                         .put("lat", lat);
                 payload.set("location", loc);
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 8db72aa..fe59dce 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
@@ -63,6 +63,8 @@
 import java.util.concurrent.ConcurrentHashMap;
 
 import static com.google.common.base.Preconditions.checkNotNull;
+import static org.onosproject.net.AnnotationKeys.GRID_X;
+import static org.onosproject.net.AnnotationKeys.GRID_Y;
 import static org.onosproject.net.AnnotationKeys.LATITUDE;
 import static org.onosproject.net.AnnotationKeys.LONGITUDE;
 import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
@@ -196,9 +198,20 @@
                 .put("region", nullIsEmpty(layout.regionId()))
                 .put("regionName", UiRegion.safeName(layout.region()));
         addCrumbs(result, crumbs);
+        addBgRef(result, layout);
         return result;
     }
 
+    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);
+        }
+    }
+
     private void addCrumbs(ObjectNode result, List<UiTopoLayout> crumbs) {
         ArrayNode trail = arrayNode();
         crumbs.forEach(c -> {
@@ -342,7 +355,7 @@
         Device d = device.backingDevice();
 
         addProps(node, d);
-        addGeoLocation(node, d);
+        addGeoGridLocation(node, d);
         addMetaUi(node, device.idAsString());
 
         return node;
@@ -364,25 +377,31 @@
         }
     }
 
-    // FIXME: need to handle geo vs. grid location...
-    private void addGeoLocation(ObjectNode node, Annotated a) {
+    private void addGeoGridLocation(ObjectNode node, Annotated a) {
         List<String> lngLat = getAnnotValues(a, LONGITUDE, LATITUDE);
-        if (lngLat != null) {
-            try {
-                double lng = Double.parseDouble(lngLat.get(0));
-                double lat = Double.parseDouble(lngLat.get(1));
-                ObjectNode loc = objectNode()
-                        .put("type", "lnglat")
-                        .put("lng", lng)
-                        .put("lat", lat);
-                node.set("location", loc);
+        List<String> gridYX = getAnnotValues(a, GRID_Y, GRID_X);
 
-            } catch (NumberFormatException e) {
-                log.warn("Invalid geo data: longitude={}, latitude={}",
-                        lngLat.get(0), lngLat.get(1));
-            }
-        } else {
-            log.debug("No geo lng/lat for {}", a);
+        if (lngLat != null) {
+            attachLocation(node, "geo", "lng", "lat", lngLat);
+        } else if (gridYX != null) {
+            attachLocation(node, "grid", "gridY", "gridX", gridYX);
+        }
+    }
+
+    private void attachLocation(ObjectNode node, String locType,
+                                String keyA, String keyB, List<String> values) {
+        try {
+            double valA = Double.parseDouble(values.get(0));
+            double valB = Double.parseDouble(values.get(1));
+            ObjectNode loc = objectNode()
+                    .put("type", locType)
+                    .put(keyA, valA)
+                    .put(keyB, valB);
+            node.set("location", loc);
+
+        } catch (NumberFormatException e) {
+            log.warn("Invalid {} data: long/Y={}, lat/X={}",
+                    locType, values.get(0), values.get(1));
         }
     }
 
@@ -429,7 +448,8 @@
         Host h = host.backingHost();
 
         addIps(node, h);
-        addGeoLocation(node, h);
+        addProps(node, h);
+        addGeoGridLocation(node, h);
         addMetaUi(node, host.idAsString());
 
         return node;
@@ -466,7 +486,7 @@
                 .put("nHosts", region.hostCount());
 
         Region r = region.backingRegion();
-        addGeoLocation(node, r);
+        addGeoGridLocation(node, r);
         addProps(node, r);
 
         addMetaUi(node, region.idAsString());
diff --git a/web/gui/src/main/webapp/app/view/topo/topoModel.js b/web/gui/src/main/webapp/app/view/topo/topoModel.js
index 0b4fd2c..887635d 100644
--- a/web/gui/src/main/webapp/app/view/topo/topoModel.js
+++ b/web/gui/src/main/webapp/app/view/topo/topoModel.js
@@ -115,7 +115,7 @@
         var loc = node.location,
             coord;
 
-        if (loc && loc.type === 'lnglat') {
+        if (loc && loc.type === 'geo') {
             coord = coordFromLngLat(loc);
             node.fixed = true;
             node.px = node.x = coord[0];
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 6dedcd9..694af14 100644
--- a/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js
+++ b/web/gui/src/main/webapp/app/view/topo2/topo2NodePosition.js
@@ -93,7 +93,7 @@
         var loc = el.get('location'),
             coord;
 
-        if (loc && loc.type === 'lnglat') {
+        if (loc && loc.type === 'geo') {
 
             if (loc.lat === 0 && loc.lng === 0) {
                 return false;
@@ -106,6 +106,9 @@
 
             return true;
         }
+
+        // TODO: handle case where loc.type === 'grid'
+        //  implying loc.gridX and loc.gridY hold values
     }
 
     function coordFromLngLat(loc) {
diff --git a/web/gui/src/main/webapp/tests/app/view/topo/topoModel-spec.js b/web/gui/src/main/webapp/tests/app/view/topo/topoModel-spec.js
index 0d1f8a5..e9f63f6 100644
--- a/web/gui/src/main/webapp/tests/app/view/topo/topoModel-spec.js
+++ b/web/gui/src/main/webapp/tests/app/view/topo/topoModel-spec.js
@@ -232,7 +232,7 @@
     it('should position a node by translating lng/lat', function () {
         var node = {
             location: {
-                type: 'lnglat',
+                type: 'geo',
                 lng: 2008,
                 lat: 3009
             }
@@ -319,7 +319,7 @@
             type: 'yowser',
             online: true,
             location: {
-                type: 'lnglat',
+                type: 'geo',
                 lng: 2048,
                 lat: 3096
             }