Europe Region Demo data script written.
- Added LayoutLocation class
- Added RegionAddPeerLocCommand class
Note: still need to plumb through peer locations to UI JSON.

Change-Id: Ic3513a3880f50b440fe318dce6896b66d7e79704
diff --git a/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java b/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java
index e27157e..7a0e8d6 100644
--- a/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java
+++ b/core/api/src/main/java/org/onosproject/net/config/basics/BasicRegionConfig.java
@@ -16,14 +16,22 @@
 
 package org.onosproject.net.config.basics;
 
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
 import com.google.common.base.MoreObjects;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionId;
+import org.onosproject.ui.topo.LayoutLocation;
 
+import java.util.ArrayList;
+import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
+import static org.onosproject.ui.topo.LayoutLocation.layoutLocation;
+
 /**
  * Basic configuration for network regions.
  */
@@ -31,11 +39,17 @@
 
     private static final String TYPE = "type";
     private static final String DEVICES = "devices";
+    private static final String LOC_IN_PEERS = "locInPeers";
+
+    private static final String LOC_TYPE = "locType";
+    private static final String LAT_OR_Y = "latOrY";
+    private static final String LONG_OR_X = "LongOrX";
+
 
     @Override
     public boolean isValid() {
         return hasOnlyFields(ALLOWED, NAME, LATITUDE, LONGITUDE, UI_TYPE,
-                RACK_ADDRESS, OWNER, TYPE, DEVICES);
+                             RACK_ADDRESS, OWNER, TYPE, DEVICES, LOC_IN_PEERS);
     }
 
     @Override
@@ -97,4 +111,65 @@
     public BasicRegionConfig devices(Set<DeviceId> devices) {
         return (BasicRegionConfig) setOrClear(DEVICES, devices);
     }
+
+
+    // Requires some custom json-node handling for maintaining a map
+    // of peer location data...
+
+    /**
+     * Adds a peer location mapping to this region.
+     *
+     * @param peerId  the region ID of the peer
+     * @param locType the type of location (geo/grid)
+     * @param latOrY  geo latitude / grid y-coord
+     * @param longOrX geo longitude / grid x-coord
+     * @return self
+     */
+    public BasicRegionConfig addPeerLocMapping(String peerId, String locType,
+                                               Double latOrY, Double longOrX) {
+        ObjectNode map = getLocMap();
+        map.set(peerId, makeLocation(locType, latOrY, longOrX));
+        return this;
+    }
+
+    private JsonNode makeLocation(String locType, Double latOrY, Double longOrX) {
+        return mapper.createObjectNode()
+                .put(LOC_TYPE, locType)
+                .put(LAT_OR_Y, latOrY)
+                .put(LONG_OR_X, longOrX);
+    }
+
+    private ObjectNode getLocMap() {
+        ObjectNode locMap = (ObjectNode) object.get(LOC_IN_PEERS);
+        if (locMap == null) {
+            locMap = mapper.createObjectNode();
+            object.set(LOC_IN_PEERS, locMap);
+        }
+        return locMap;
+    }
+
+    /**
+     * Returns the list of layout location mappings for where peer region nodes
+     * should be placed on the layout when viewing this region.
+     *
+     * @return list of peer node locations
+     */
+    public List<LayoutLocation> getMappings() {
+        List<LayoutLocation> mappings = new ArrayList<>();
+        ObjectNode map = (ObjectNode) object.get(LOC_IN_PEERS);
+        if (map != null) {
+            for (Iterator<Map.Entry<String, JsonNode>> it = map.fields(); it.hasNext();) {
+                Map.Entry<String, JsonNode> entry = it.next();
+                String peerId = entry.getKey();
+                ObjectNode data = (ObjectNode) entry.getValue();
+
+                String lt = data.get(LOC_TYPE).asText();
+                double latY = data.get(LAT_OR_Y).asDouble();
+                double longX = data.get(LONG_OR_X).asDouble();
+
+                mappings.add(layoutLocation(peerId, lt, latY, longX));
+            }
+        }
+        return mappings;
+    }
 }
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
new file mode 100644
index 0000000..9e006b5
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/topo/LayoutLocation.java
@@ -0,0 +1,141 @@
+/*
+ * 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.ui.topo;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Represents a "node location" on a UI layout.
+ */
+public final class LayoutLocation {
+    private static final double ZERO_THRESHOLD = Double.MIN_VALUE * 2.0;
+
+    /**
+     * Designates the type of location; either geographic or logical grid.
+     */
+    public enum Type {
+        GEO, GRID
+    }
+
+    private final String id;
+    private final double latOrY;
+    private final double longOrX;
+    private final Type locType;
+
+    private LayoutLocation(String id, Type locType, double latOrY, double longOrX) {
+        this.id = id;
+        this.latOrY = latOrY;
+        this.longOrX = longOrX;
+        this.locType = locType;
+    }
+
+
+    private boolean doubleIsZero(double value) {
+        return value >= -ZERO_THRESHOLD && value <= ZERO_THRESHOLD;
+    }
+
+    /**
+     * Returns true if the coordinates indicate the origin (0, 0) of the
+     * coordinate system; false otherwise.
+     *
+     * @return true if geo-coordinates are set; false otherwise
+     */
+    public boolean isOrigin() {
+        return doubleIsZero(latOrY) && doubleIsZero(longOrX);
+    }
+
+    /**
+     * Returns the identifier associated with this location.
+     *
+     * @return the identifier
+     */
+    public String id() {
+        return id;
+    }
+
+    /**
+     * Returns the latitude (geo) or y-coord (grid) data value.
+     *
+     * @return geo latitude or grid y-coord
+     */
+    public double latOrY() {
+        return latOrY;
+    }
+
+    /**
+     * Returns the longitude (geo) or x-coord (grid) data value.
+     *
+     * @return geo longitude or grid x-coord
+     */
+    public double longOrX() {
+        return longOrX;
+    }
+
+    /**
+     * Returns the location type (geo or grid), which indicates how the data
+     * is to be interpreted.
+     *
+     * @return location type
+     */
+    public Type locType() {
+        return locType;
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("id", id)
+                .add("lat/Y", latOrY)
+                .add("long/X", longOrX)
+                .add("loc-type", locType)
+                .toString();
+    }
+
+    /**
+     * Creates an instance of a layout location.
+     *
+     * @param id      an identifier for the item at this location
+     * @param locType the location type
+     * @param latOrY  geo latitude / grid y-coord
+     * @param longOrX geo longitude / grid x-coord
+     * @return layout location instance
+     */
+    public static LayoutLocation layoutLocation(String id, Type locType,
+                                                double latOrY, double longOrX) {
+        checkNotNull(id, "must supply an identifier");
+        checkNotNull(locType, "must declare location type");
+        return new LayoutLocation(id, locType, latOrY, longOrX);
+    }
+
+    /**
+     * Creates an instance of a layout location.
+     *
+     * @param id      an identifier for the item at this location
+     * @param locType the location type ("geo" or "grid")
+     * @param latOrY  geo latitude / grid y-coord
+     * @param longOrX geo longitude / grid x-coord
+     * @return layout location instance
+     * @throws IllegalArgumentException if the type is not "geo" or "grid"
+     */
+    public static LayoutLocation layoutLocation(String id, String locType,
+                                                double latOrY, double longOrX) {
+        Type t = Type.valueOf(locType.toUpperCase());
+        return new LayoutLocation(id, t, latOrY, longOrX);
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/ui/AbstractUiTest.java b/core/api/src/test/java/org/onosproject/ui/AbstractUiTest.java
index 077d132..9242863 100644
--- a/core/api/src/test/java/org/onosproject/ui/AbstractUiTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/AbstractUiTest.java
@@ -27,6 +27,11 @@
     protected static final String EOL = String.format("%n");
 
     /**
+     * Tolerance for Double equality assertions.
+     */
+    protected static final double TOLERANCE = Double.MIN_VALUE * 2;
+
+    /**
      * Prints the given string to stdout.
      *
      * @param s string to print
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
new file mode 100644
index 0000000..64908fd
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/topo/LayoutLocationTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.ui.topo;
+
+import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+
+import static org.junit.Assert.*;
+import static org.onosproject.ui.topo.LayoutLocation.Type;
+import static org.onosproject.ui.topo.LayoutLocation.layoutLocation;
+
+/**
+ * Unit tests for {@link LayoutLocation}.
+ */
+public class LayoutLocationTest extends AbstractUiTest {
+
+    private static final String SOME_ID = "foo";
+    private static final double SQRT2 = 1.414;
+    private static final double PI = 3.142;
+    private static final double ZERO = 0.0;
+
+    private LayoutLocation ll;
+
+    @Test
+    public void basic() {
+        ll = layoutLocation(SOME_ID, Type.GRID, SQRT2, PI);
+        print(ll);
+        assertEquals("bad id", SOME_ID, ll.id());
+        assertEquals("bad type", Type.GRID, ll.locType());
+        assertEquals("bad Y", SQRT2, ll.latOrY(), TOLERANCE);
+        assertEquals("bad X", PI, ll.longOrX(), TOLERANCE);
+        assertFalse("bad origin check", ll.isOrigin());
+    }
+
+    @Test
+    public void createGeoLocFromStringType() {
+        ll = layoutLocation(SOME_ID, "geo", SQRT2, PI);
+        assertEquals("bad type - not geo", Type.GEO, ll.locType());
+    }
+
+    @Test
+    public void createGridLocFromStringType() {
+        ll = layoutLocation(SOME_ID, "grid", SQRT2, PI);
+        assertEquals("bad type - not grid", Type.GRID, ll.locType());
+    }
+
+    @Test
+    public void zeroLatitude() {
+        ll = layoutLocation(SOME_ID, Type.GEO, ZERO, PI);
+        assertFalse("shouldn't be origin for zero latitude", ll.isOrigin());
+    }
+
+    @Test
+    public void zeroLongitude() {
+        ll = layoutLocation(SOME_ID, Type.GEO, PI, ZERO);
+        assertFalse("shouldn't be origin for zero longitude", ll.isOrigin());
+    }
+
+    @Test
+    public void origin() {
+        ll = layoutLocation(SOME_ID, Type.GRID, ZERO, ZERO);
+        assertTrue("should be origin", ll.isOrigin());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void badType() {
+        layoutLocation(SOME_ID, "foo", ZERO, PI);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void nullId() {
+        layoutLocation(null, Type.GRID, PI, PI);
+    }
+}