ONOS-4971: Synthetic Link Data -- WIP

- Enhancing UiRegion to capture the hierarchical (parent/child) relationships captured in the UiTopoLayouts.

Change-Id: I152e0d52d4580b14b679f3387402077f16f61e6a
diff --git a/core/api/src/main/java/org/onosproject/ui/UiTopoLayoutService.java b/core/api/src/main/java/org/onosproject/ui/UiTopoLayoutService.java
index 0036521..c6ae247 100644
--- a/core/api/src/main/java/org/onosproject/ui/UiTopoLayoutService.java
+++ b/core/api/src/main/java/org/onosproject/ui/UiTopoLayoutService.java
@@ -15,6 +15,7 @@
  */
 package org.onosproject.ui;
 
+import org.onosproject.net.region.RegionId;
 import org.onosproject.ui.model.topo.UiTopoLayout;
 import org.onosproject.ui.model.topo.UiTopoLayoutId;
 
@@ -57,6 +58,15 @@
     UiTopoLayout getLayout(UiTopoLayoutId layoutId);
 
     /**
+     * Returns the layout which has the backing region identified by
+     * the given region identifier.
+     *
+     * @param regionId region identifier
+     * @return corresponding layout
+     */
+    UiTopoLayout getLayout(RegionId regionId);
+
+    /**
      * Returns the set of peer layouts of the specified layout. That is,
      * those layouts that share the same parent.
      *
diff --git a/core/api/src/main/java/org/onosproject/ui/model/ServiceBundle.java b/core/api/src/main/java/org/onosproject/ui/model/ServiceBundle.java
index 61cf721..6986cf7 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/ServiceBundle.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/ServiceBundle.java
@@ -24,11 +24,20 @@
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.link.LinkService;
 import org.onosproject.net.region.RegionService;
+import org.onosproject.ui.UiTopoLayoutService;
 
 /**
  * A bundle of services to pass to elements that might need a reference to them.
  */
 public interface ServiceBundle {
+
+    /**
+     * Reference to a UI Topology Layout service implementation.
+     *
+     * @return layout service
+     */
+    UiTopoLayoutService layout();
+
     /**
      * Reference to a cluster service implementation.
      *
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
index c478424..b3185f1 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiRegion.java
@@ -61,6 +61,10 @@
 
     private final Region region;
 
+    // keep track of hierarchy (inferred from UiTopoLayoutService)
+    private RegionId parent;
+    private final Set<RegionId> kids = new HashSet<>();
+
     /**
      * Constructs a UI region, with a reference to the specified backing region.
      *
@@ -103,6 +107,52 @@
         return region == null ? NULL_ID : region.id();
     }
 
+    /**
+     * Returns the identity of the parent region.
+     *
+     * @return parent region ID
+     */
+    public RegionId parent() {
+        return parent;
+    }
+
+    /**
+     * Returns true if this is the root (default) region.
+     *
+     * @return true if root region
+     */
+    public boolean isRoot() {
+        return id().equals(parent);
+    }
+
+    /**
+     * Returns the identities of the child regions.
+     *
+     * @return child region IDs
+     */
+    public Set<RegionId> children() {
+        return ImmutableSet.copyOf(kids);
+    }
+
+    /**
+     * Sets the parent ID for this region.
+     *
+     * @param parentId parent ID
+     */
+    public void setParent(RegionId parentId) {
+        parent = parentId;
+    }
+
+    /**
+     * Sets the children IDs for this region.
+     *
+     * @param children children IDs
+     */
+    public void setChildren(Set<RegionId> children) {
+        kids.clear();
+        kids.addAll(children);
+    }
+
     @Override
     public String idAsString() {
         return id().toString();
@@ -138,6 +188,8 @@
         return toStringHelper(this)
                 .add("id", id())
                 .add("name", name())
+                .add("parent", parent)
+                .add("kids", kids)
                 .add("devices", deviceIds)
                 .add("#hosts", hostIds.size())
                 .add("#links", uiLinkIds.size())
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 12c9de6..ade86e1 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
@@ -68,13 +68,16 @@
     }
 
     /**
-     * Returns the identifier of the backing region. Will be null if the
-     * region is null.
+     * Returns the identifier of the backing region. If this is the default
+     * layout, the null-region ID will be returned, otherwise the ID of the
+     * backing region for this layout will be returned; null in the case that
+     * there is no backing region.
      *
      * @return backing region identifier
      */
     public RegionId regionId() {
-        return region == null ? null : region.id();
+        return isRoot() ? UiRegion.NULL_ID
+                : (region == null ? null : region.id());
     }
 
     /**
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
index 585e658..708fd18 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiTopology.java
@@ -151,7 +151,8 @@
 
 
     /**
-     * Returns all regions in the model.
+     * Returns all regions in the model (except the
+     * {@link #nullRegion() null region}).
      *
      * @return all regions
      */
@@ -177,7 +178,7 @@
      * @return corresponding UI region
      */
     public UiRegion findRegion(RegionId id) {
-        return regionLookup.get(id);
+        return UiRegion.NULL_ID.equals(id) ? nullRegion() : regionLookup.get(id);
     }
 
     /**
diff --git a/core/api/src/test/java/org/onosproject/ui/model/AbstractUiModelTest.java b/core/api/src/test/java/org/onosproject/ui/model/AbstractUiModelTest.java
index 5037832..15b3870 100644
--- a/core/api/src/test/java/org/onosproject/ui/model/AbstractUiModelTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/model/AbstractUiModelTest.java
@@ -28,6 +28,7 @@
 import org.onosproject.net.link.LinkService;
 import org.onosproject.net.region.RegionService;
 import org.onosproject.ui.AbstractUiTest;
+import org.onosproject.ui.UiTopoLayoutService;
 
 /**
  * Base class for UI model unit tests.
@@ -42,6 +43,11 @@
     protected static final ServiceBundle MOCK_SERVICES =
             new ServiceBundle() {
                 @Override
+                public UiTopoLayoutService layout() {
+                    return null;
+                }
+
+                @Override
                 public ClusterService cluster() {
                     return MOCK_CLUSTER;
                 }