Implemented initial loading of ModelCache.
Created UiLinkId to canonicalize identifiers for UI links, based on src and dst elements.
Added idAsString() and name() methods to UiElement.
Added toString() to UiDevice, UiLink, UiHost.
Created Mock services for testing.

Change-Id: I4d27110e5aca08f29bb719f17e9ec65d6786e2c8
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiCluster.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiCluster.java
deleted file mode 100644
index 40de28c..0000000
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiCluster.java
+++ /dev/null
@@ -1,92 +0,0 @@
-/*
- *  Copyright 2016-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.model.topo;
-
-import org.onosproject.cluster.NodeId;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Encapsulates the notion of the ONOS cluster.
- */
-class UiCluster extends UiElement {
-
-    private static final String DEFAULT_CLUSTER_ID = "CLUSTER-0";
-
-    private final List<UiClusterMember> members = new ArrayList<>();
-    private final Map<NodeId, UiClusterMember> lookup = new HashMap<>();
-
-    @Override
-    public String toString() {
-        return String.valueOf(size()) + "-member cluster";
-    }
-
-    /**
-     * Removes all cluster members.
-     */
-    void clear() {
-        members.clear();
-    }
-
-    /**
-     * Returns the cluster member with the given identifier, or null if no
-     * such member exists.
-     *
-     * @param id identifier of member to find
-     * @return corresponding member
-     */
-    public UiClusterMember find(NodeId id) {
-        return lookup.get(id);
-    }
-
-    /**
-     * Adds the given member to the cluster.
-     *
-     * @param member member to add
-     */
-    public void add(UiClusterMember member) {
-        members.add(member);
-        lookup.put(member.id(), member);
-    }
-
-    /**
-     * Removes the given member from the cluster.
-     *
-     * @param member member to remove
-     */
-    public void remove(UiClusterMember member) {
-        members.remove(member);
-        lookup.remove(member.id());
-    }
-
-    /**
-     * Returns the number of members in the cluster.
-     *
-     * @return number of members
-     */
-    public int size() {
-        return members.size();
-    }
-
-    @Override
-    public String idAsString() {
-        return DEFAULT_CLUSTER_ID;
-    }
-}
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
index 1cc9234..d8803ab 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiClusterMember.java
@@ -19,6 +19,11 @@
 import org.onlab.packet.IpAddress;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.NodeId;
+import org.onosproject.net.DeviceId;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import static org.onosproject.cluster.ControllerNode.State.INACTIVE;
 
@@ -27,18 +32,21 @@
  */
 public class UiClusterMember extends UiElement {
 
+    private final UiTopology topology;
     private final ControllerNode cnode;
 
-    private int deviceCount = 0;
     private ControllerNode.State state = INACTIVE;
+    private final Set<DeviceId> mastership = new HashSet<>();
 
     /**
-     * Constructs a cluster member, with a reference to the specified
-     * controller node instance.
+     * Constructs a UI cluster member, with a reference to the parent
+     * topology instance and the specified controller node instance.
      *
-     * @param cnode underlying controller node.
+     * @param topology parent topology containing this cluster member
+     * @param cnode    underlying controller node.
      */
-    public UiClusterMember(ControllerNode cnode) {
+    public UiClusterMember(UiTopology topology, ControllerNode cnode) {
+        this.topology = topology;
         this.cnode = cnode;
     }
 
@@ -47,10 +55,23 @@
         return "UiClusterMember{" + cnode +
                 ", online=" + isOnline() +
                 ", ready=" + isReady() +
-                ", #devices=" + deviceCount +
+                ", #devices=" + deviceCount() +
                 "}";
     }
 
+    @Override
+    public String idAsString() {
+        return id().toString();
+    }
+
+    /**
+     * Returns the controller node instance backing this UI cluster member.
+     *
+     * @return the backing controller node instance
+     */
+    public ControllerNode backingNode() {
+        return cnode;
+    }
 
     /**
      * Sets the state of this cluster member.
@@ -61,14 +82,15 @@
         this.state = state;
     }
 
-
     /**
-     * Sets the number of devices for which this cluster member is master.
+     * Sets the collection of identities of devices for which this
+     * controller node is master.
      *
-     * @param deviceCount number of devices
+     * @param mastership device IDs
      */
-    public void setDeviceCount(int deviceCount) {
-        this.deviceCount = deviceCount;
+    public void setMastership(Set<DeviceId> mastership) {
+        this.mastership.clear();
+        this.mastership.addAll(mastership);
     }
 
     /**
@@ -113,11 +135,26 @@
      * @return number of devices for which this member is master
      */
     public int deviceCount() {
-        return deviceCount;
+        return mastership.size();
     }
 
-    @Override
-    public String idAsString() {
-        return id().toString();
+    /**
+     * Returns the list of devices for which this cluster member is master.
+     *
+     * @return list of devices for which this member is master
+     */
+    public Set<DeviceId> masterOf() {
+        return Collections.unmodifiableSet(mastership);
+    }
+
+    /**
+     * Returns true if the specified device is one for which this cluster
+     * member is master.
+     *
+     * @param deviceId device ID
+     * @return true if this cluster member is master for the given device
+     */
+    public boolean masterOf(DeviceId deviceId) {
+        return mastership.contains(deviceId);
     }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
index 88d86d0..bb59752 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiDevice.java
@@ -16,21 +16,53 @@
 
 package org.onosproject.ui.model.topo;
 
+import com.google.common.base.MoreObjects;
 import org.onosproject.net.Device;
 import org.onosproject.net.DeviceId;
+import org.onosproject.net.region.RegionId;
 
 /**
  * Represents a device.
  */
 public class UiDevice extends UiNode {
 
-    private Device device;
+    private final UiTopology topology;
+    private final Device device;
+
+    private RegionId regionId;
+
+    /**
+     * Creates a new UI device.
+     *
+     * @param topology parent topology
+     * @param device   backing device
+     */
+    public UiDevice(UiTopology topology, Device device) {
+        this.topology = topology;
+        this.device = device;
+    }
+
+    /**
+     * Sets the ID of the region to which this device belongs.
+     *
+     * @param regionId region identifier
+     */
+    public void setRegionId(RegionId regionId) {
+        this.regionId = regionId;
+    }
 
     @Override
-    protected void destroy() {
-        device = null;
+    public String toString() {
+        return MoreObjects.toStringHelper(this)
+                .add("id", id())
+                .add("region", regionId)
+                .toString();
     }
 
+    //    @Override
+//    protected void destroy() {
+//    }
+
     /**
      * Returns the identity of the device.
      *
@@ -44,4 +76,22 @@
     public String idAsString() {
         return id().toString();
     }
+
+    /**
+     * Returns the device instance backing this UI device.
+     *
+     * @return the backing device instance
+     */
+    public Device backingDevice() {
+        return device;
+    }
+
+    /**
+     * Returns the UI region to which this device belongs.
+     *
+     * @return the UI region
+     */
+    public UiRegion uiRegion() {
+        return topology.findRegion(regionId);
+    }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiElement.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiElement.java
index f0c2684..558f206 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiElement.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiElement.java
@@ -35,4 +35,15 @@
      * @return the element unique identifier
      */
     public abstract String idAsString();
+
+    /**
+     * Returns a friendly name to be used for display purposes.
+     * This default implementation returns the result of calling
+     * {@link #idAsString()}.
+     *
+     * @return the friendly name
+     */
+    public String name() {
+        return idAsString();
+    }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
index 9a9e24a..e80836a 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiHost.java
@@ -16,19 +16,49 @@
 
 package org.onosproject.ui.model.topo;
 
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostId;
+import org.onosproject.net.PortNumber;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
 
 /**
  * Represents an end-station host.
  */
 public class UiHost extends UiNode {
 
-    private Host host;
+    private final UiTopology topology;
+    private final Host host;
+
+    // Host location
+    private DeviceId locDevice;
+    private PortNumber locPort;
+
+    private UiLinkId edgeLinkId;
+
+    /**
+     * Creates a new UI host.
+     *
+     * @param topology parent topology
+     * @param host     backing host
+     */
+    public UiHost(UiTopology topology, Host host) {
+        this.topology = topology;
+        this.host = host;
+    }
+
+//    @Override
+//    protected void destroy() {
+//    }
 
     @Override
-    protected void destroy() {
-        host = null;
+    public String toString() {
+        return toStringHelper(this)
+                .add("id", id())
+                .add("dev", locDevice)
+                .add("port", locPort)
+                .toString();
     }
 
     /**
@@ -44,4 +74,44 @@
     public String idAsString() {
         return id().toString();
     }
+
+    /**
+     * Sets the host's current location.
+     *
+     * @param deviceId ID of device
+     * @param port     port number
+     */
+    public void setLocation(DeviceId deviceId, PortNumber port) {
+        locDevice = deviceId;
+        locPort = port;
+    }
+
+    /**
+     * Sets the ID of the edge link between this host and the device to which
+     * it connects.
+     *
+     * @param id edge link identifier to set
+     */
+    public void setEdgeLinkId(UiLinkId id) {
+        this.edgeLinkId = id;
+    }
+
+    /**
+     * Returns the host instance backing this UI host.
+     *
+     * @return the backing host instance
+     */
+    public Host backingHost() {
+        return host;
+    }
+
+    /**
+     * Identifier for the edge link between this host and the device to which
+     * it is connected.
+     *
+     * @return edge link identifier
+     */
+    public UiLinkId edgeLinkId() {
+        return null;
+    }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiLayer.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLayer.java
new file mode 100644
index 0000000..30d16f6
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLayer.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2016-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.model.topo;
+
+/**
+ * Designates the logical layer of the network that an element belongs to.
+ */
+public enum UiLayer {
+    PACKET, OPTICAL;
+
+    /**
+     * Returns the default layer (for those elements that do not explicitly
+     * define which layer they belong to).
+     *
+     * @return default layer
+     */
+    public static UiLayer defaultLayer() {
+        return PACKET;
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiLink.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLink.java
index 99d6144..b386c5e 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiLink.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLink.java
@@ -16,26 +16,61 @@
 
 package org.onosproject.ui.model.topo;
 
-import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
 import org.onosproject.net.EdgeLink;
 import org.onosproject.net.Link;
 
 import java.util.Set;
 
+import static com.google.common.base.MoreObjects.toStringHelper;
+
 /**
- * Represents a bi-directional link backed by two uni-directional links.
+ * Represents a link (line between two elements). This may have one of
+ * several forms:
+ * <ul>
+ * <li>
+ * An infrastructure link:
+ * two backing unidirectional links between two devices.
+ * </li>
+ * <li>
+ * An edge link:
+ * representing the connection between a host and a device.
+ * </li>
+ * <li>
+ * An aggregation link:
+ * representing multiple underlying UI link instances.
+ * </li>
+ * </ul>
  */
 public class UiLink extends UiElement {
 
+    private static final String E_UNASSOC =
+            "backing link not associated with this UI link: ";
+
+    private final UiTopology topology;
+    private final UiLinkId id;
+
+    /**
+     * Creates a UI link.
+     *
+     * @param topology parent topology
+     * @param id       canonicalized link identifier
+     */
+    public UiLink(UiTopology topology, UiLinkId id) {
+        this.topology = topology;
+        this.id = id;
+    }
+
     // devices at either end of this link
-    private Device deviceA;
-    private Device deviceB;
+    private DeviceId deviceA;
+    private DeviceId deviceB;
 
     // two unidirectional links underlying this link...
     private Link linkAtoB;
     private Link linkBtoA;
 
     // ==OR== : private (synthetic) host link
+    private DeviceId edgeDevice;
     private EdgeLink edgeLink;
 
     // ==OR== : set of underlying UI links that this link aggregates
@@ -43,6 +78,13 @@
 
 
     @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("id", id)
+                .toString();
+    }
+
+    @Override
     protected void destroy() {
         deviceA = null;
         deviceB = null;
@@ -55,9 +97,84 @@
         }
     }
 
+    /**
+     * Returns the canonicalized link identifier for this link.
+     *
+     * @return the link identifier
+     */
+    public UiLinkId id() {
+        return id;
+    }
+
     @Override
     public String idAsString() {
-        // TODO
-        return null;
+        return id.toString();
+    }
+
+    /**
+     * Attaches the given backing link to this UI link. This method will
+     * throw an exception if this UI link is not representative of the
+     * supplied link.
+     *
+     * @param link backing link to attach
+     * @throws IllegalArgumentException if the link is not appropriate
+     */
+    public void attachBackingLink(Link link) {
+        UiLinkId.Direction d = id.directionOf(link);
+
+        if (d == UiLinkId.Direction.A_TO_B) {
+            linkAtoB = link;
+            deviceA = link.src().deviceId();
+            deviceB = link.dst().deviceId();
+
+        } else if (d == UiLinkId.Direction.B_TO_A) {
+            linkBtoA = link;
+            deviceB = link.src().deviceId();
+            deviceA = link.dst().deviceId();
+
+        } else {
+            throw new IllegalArgumentException(E_UNASSOC + link);
+        }
+    }
+
+    /**
+     * Detaches the given backing link from this UI link, returning true if the
+     * reverse link is still attached, or false otherwise.
+     *
+     * @param link the backing link to detach
+     * @return true if other link still attached, false otherwise
+     * @throws IllegalArgumentException if the link is not appropriate
+     */
+    public boolean detachBackingLink(Link link) {
+        UiLinkId.Direction d = id.directionOf(link);
+        if (d == UiLinkId.Direction.A_TO_B) {
+            linkAtoB = null;
+            return linkBtoA != null;
+        }
+        if (d == UiLinkId.Direction.B_TO_A) {
+            linkBtoA = null;
+            return linkAtoB != null;
+        }
+        throw new IllegalArgumentException(E_UNASSOC + link);
+    }
+
+    /**
+     * Attaches the given edge link to this UI link. This method will
+     * throw an exception if this UI link is not representative of the
+     * supplied link.
+     *
+     * @param elink edge link to attach
+     * @throws IllegalArgumentException if the link is not appropriate
+     */
+    public void attachEdgeLink(EdgeLink elink) {
+        UiLinkId.Direction d = id.directionOf(elink);
+        // Expected direction of edge links is A-to-B (Host to device)
+        // but checking not null is sufficient
+        if (d == null) {
+            throw new IllegalArgumentException(E_UNASSOC + elink);
+        }
+
+        edgeLink = elink;
+        edgeDevice = elink.hostLocation().deviceId();
     }
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiLinkId.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLinkId.java
new file mode 100644
index 0000000..070052d
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiLinkId.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2016-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.model.topo;
+
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.ElementId;
+import org.onosproject.net.Link;
+
+/**
+ * A canonical representation of an identifier for {@link UiLink}s.
+ */
+public final class UiLinkId {
+
+    /**
+     * Designates the directionality of an underlying (uni-directional) link.
+     */
+    public enum Direction {
+        A_TO_B,
+        B_TO_A
+    }
+
+    private static final String ID_DELIMITER = "~";
+
+    private final ElementId idA;
+    private final ElementId idB;
+    private final String idStr;
+
+    /**
+     * Creates a UI link identifier. It is expected that A comes before B when
+     * the two identifiers are naturally sorted, thus providing a representation
+     * which is invariant to whether A or B is source or destination of the
+     * underlying link.
+     *
+     * @param a first element ID
+     * @param b second element ID
+     */
+    private UiLinkId(ElementId a, ElementId b) {
+        idA = a;
+        idB = b;
+
+        idStr = a.toString() + ID_DELIMITER + b.toString();
+    }
+
+    @Override
+    public String toString() {
+        return idStr;
+    }
+
+    /**
+     * Returns the identifier of the first element.
+     *
+     * @return first element identity
+     */
+    public ElementId elementA() {
+        return idA;
+    }
+
+    /**
+     * Returns the identifier of the second element.
+     *
+     * @return second element identity
+     */
+    public ElementId elementB() {
+        return idB;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        UiLinkId uiLinkId = (UiLinkId) o;
+        return idStr.equals(uiLinkId.idStr);
+    }
+
+    @Override
+    public int hashCode() {
+        return idStr.hashCode();
+    }
+
+    /**
+     * Returns the direction of the given link, or null if this link ID does
+     * not correspond to the given link.
+     *
+     * @param link the link to examine
+     * @return corresponding direction
+     */
+    Direction directionOf(Link link) {
+        ConnectPoint src = link.src();
+        ElementId srcId = src.elementId();
+        return idA.equals(srcId) ? Direction.A_TO_B
+                : idB.equals(srcId) ? Direction.B_TO_A
+                : null;
+    }
+
+    /**
+     * Generates the canonical link identifier for the given link.
+     *
+     * @param link link for which the identifier is required
+     * @return link identifier
+     * @throws NullPointerException if any of the required fields are null
+     */
+    public static UiLinkId uiLinkId(Link link) {
+        ConnectPoint src = link.src();
+        ConnectPoint dst = link.dst();
+        if (src == null || dst == null) {
+            throw new NullPointerException("null src or dst connect point: " + link);
+        }
+
+        ElementId srcId = src.elementId();
+        ElementId dstId = dst.elementId();
+        if (srcId == null || dstId == null) {
+            throw new NullPointerException("null element ID in connect point: " + link);
+        }
+
+        // canonicalize
+        int comp = srcId.toString().compareTo(dstId.toString());
+        return comp <= 0 ? new UiLinkId(srcId, dstId)
+                : new UiLinkId(dstId, srcId);
+    }
+}
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 488c37b..0bcaa58 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
@@ -16,35 +16,46 @@
 
 package org.onosproject.ui.model.topo;
 
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
 import org.onosproject.net.region.Region;
 import org.onosproject.net.region.RegionId;
 
+import java.util.HashSet;
 import java.util.Set;
-import java.util.TreeSet;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
 
 /**
  * Represents a region.
  */
 public class UiRegion extends UiNode {
 
-    private final Set<UiDevice> uiDevices = new TreeSet<>();
-    private final Set<UiHost> uiHosts = new TreeSet<>();
-    private final Set<UiLink> uiLinks = new TreeSet<>();
+    // loose bindings to things in this region
+    private final Set<DeviceId> deviceIds = new HashSet<>();
+    private final Set<HostId> hostIds = new HashSet<>();
+    private final Set<UiLinkId> uiLinkIds = new HashSet<>();
 
-    private Region region;
+    private final UiTopology topology;
 
+    private final Region region;
+
+    /**
+     * Constructs a UI region, with a reference to the specified backing region.
+     *
+     * @param topology parent topology
+     * @param region   backing region
+     */
+    public UiRegion(UiTopology topology, Region region) {
+        this.topology = topology;
+        this.region = region;
+    }
 
     @Override
     protected void destroy() {
-        uiDevices.forEach(UiDevice::destroy);
-        uiHosts.forEach(UiHost::destroy);
-        uiLinks.forEach(UiLink::destroy);
-
-        uiDevices.clear();
-        uiHosts.clear();
-        uiLinks.clear();
-
-        region = null;
+        deviceIds.clear();
+        hostIds.clear();
+        uiLinkIds.clear();
     }
 
     /**
@@ -60,4 +71,75 @@
     public String idAsString() {
         return id().toString();
     }
+
+    @Override
+    public String name() {
+        return region.name();
+    }
+
+    /**
+     * Returns the region instance backing this UI region.
+     *
+     * @return the backing region instance
+     */
+    public Region backingRegion() {
+        return region;
+    }
+
+    /**
+     * Make sure we have only these devices in the region.
+     *
+     * @param devices devices in the region
+     */
+    public void reconcileDevices(Set<DeviceId> devices) {
+        deviceIds.clear();
+        deviceIds.addAll(devices);
+    }
+
+    @Override
+    public String toString() {
+        return toStringHelper(this)
+                .add("id", id())
+                .add("name", name())
+                .add("devices", deviceIds)
+                .add("#hosts", hostIds.size())
+                .add("#links", uiLinkIds.size())
+                .toString();
+    }
+
+    /**
+     * Returns the region's type.
+     *
+     * @return region type
+     */
+    public Region.Type type() {
+        return region.type();
+    }
+
+    /**
+     * Returns the devices in this region.
+     *
+     * @return the devices in this region
+     */
+    public Set<UiDevice> devices() {
+        return topology.deviceSet(deviceIds);
+    }
+
+    /**
+     * Returns the hosts in this region.
+     *
+     * @return the hosts in this region
+     */
+    public Set<UiHost> hosts() {
+        return topology.hostSet(hostIds);
+    }
+
+    /**
+     * Returns the links in this region.
+     *
+     * @return the links in this region
+     */
+    public Set<UiLink> links() {
+        return topology.linkSet(uiLinkIds);
+    }
 }
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 a8b5b06..3a11fcf 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
@@ -17,27 +17,54 @@
 package org.onosproject.ui.model.topo;
 
 import org.onosproject.cluster.NodeId;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.region.RegionId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
-import java.util.TreeSet;
+
+import static com.google.common.base.MoreObjects.toStringHelper;
 
 /**
  * Represents the overall network topology.
  */
 public class UiTopology extends UiElement {
 
+    private static final String E_UNMAPPED =
+            "Attempting to retrieve unmapped {}: {}";
+
     private static final String DEFAULT_TOPOLOGY_ID = "TOPOLOGY-0";
 
     private static final Logger log = LoggerFactory.getLogger(UiTopology.class);
 
-    private final UiCluster uiCluster = new UiCluster();
-    private final Set<UiRegion> uiRegions = new TreeSet<>();
+
+    // top level mappings of topology elements by ID
+    private final Map<NodeId, UiClusterMember> cnodeLookup = new HashMap<>();
+    private final Map<RegionId, UiRegion> regionLookup = new HashMap<>();
+    private final Map<DeviceId, UiDevice> deviceLookup = new HashMap<>();
+    private final Map<HostId, UiHost> hostLookup = new HashMap<>();
+    private final Map<UiLinkId, UiLink> linkLookup = new HashMap<>();
+
 
     @Override
     public String toString() {
-        return "Topology: " + uiCluster + ", " + uiRegions.size() + " regions";
+        return toStringHelper(this)
+                .add("#cnodes", clusterMemberCount())
+                .add("#regions", regionCount())
+                .add("#devices", deviceLookup.size())
+                .add("#hosts", hostLookup.size())
+                .add("#links", linkLookup.size())
+                .toString();
+    }
+
+    @Override
+    public String idAsString() {
+        return DEFAULT_TOPOLOGY_ID;
     }
 
     /**
@@ -46,19 +73,22 @@
      */
     public void clear() {
         log.debug("clearing topology model");
-        uiRegions.clear();
-        uiCluster.clear();
+        cnodeLookup.clear();
+        regionLookup.clear();
+        deviceLookup.clear();
+        hostLookup.clear();
+        linkLookup.clear();
     }
 
     /**
      * Returns the cluster member with the given identifier, or null if no
-     * such member.
+     * such member exists.
      *
      * @param id cluster node identifier
-     * @return the cluster member with that identifier
+     * @return corresponding UI cluster member
      */
     public UiClusterMember findClusterMember(NodeId id) {
-        return uiCluster.find(id);
+        return cnodeLookup.get(id);
     }
 
     /**
@@ -67,7 +97,7 @@
      * @param member cluster member to add
      */
     public void add(UiClusterMember member) {
-        uiCluster.add(member);
+        cnodeLookup.put(member.id(), member);
     }
 
     /**
@@ -76,7 +106,10 @@
      * @param member cluster member to remove
      */
     public void remove(UiClusterMember member) {
-        uiCluster.remove(member);
+        UiClusterMember m = cnodeLookup.remove(member.id());
+        if (m != null) {
+            m.destroy();
+        }
     }
 
     /**
@@ -85,7 +118,18 @@
      * @return number of cluster members
      */
     public int clusterMemberCount() {
-        return uiCluster.size();
+        return cnodeLookup.size();
+    }
+
+    /**
+     * Returns the region with the specified identifier, or null if
+     * no such region exists.
+     *
+     * @param id region identifier
+     * @return corresponding UI region
+     */
+    public UiRegion findRegion(RegionId id) {
+        return regionLookup.get(id);
     }
 
     /**
@@ -94,11 +138,182 @@
      * @return number of regions
      */
     public int regionCount() {
-        return uiRegions.size();
+        return regionLookup.size();
     }
 
-    @Override
-    public String idAsString() {
-        return DEFAULT_TOPOLOGY_ID;
+    /**
+     * Adds the given region to the topology model.
+     *
+     * @param uiRegion region to add
+     */
+    public void add(UiRegion uiRegion) {
+        regionLookup.put(uiRegion.id(), uiRegion);
     }
+
+    /**
+     * Removes the given region from the topology model.
+     *
+     * @param uiRegion region to remove
+     */
+    public void remove(UiRegion uiRegion) {
+        regionLookup.remove(uiRegion.id());
+    }
+
+    /**
+     * Returns the device with the specified identifier, or null if
+     * no such device exists.
+     *
+     * @param id device identifier
+     * @return corresponding UI device
+     */
+    public UiDevice findDevice(DeviceId id) {
+        return deviceLookup.get(id);
+    }
+
+    /**
+     * Adds the given device to the topology model.
+     *
+     * @param uiDevice device to add
+     */
+    public void add(UiDevice uiDevice) {
+        deviceLookup.put(uiDevice.id(), uiDevice);
+    }
+
+    /**
+     * Removes the given device from the topology model.
+     *
+     * @param uiDevice device to remove
+     */
+    public void remove(UiDevice uiDevice) {
+        UiDevice d = deviceLookup.remove(uiDevice.id());
+        if (d != null) {
+            d.destroy();
+        }
+    }
+
+    /**
+     * Returns the link with the specified identifier, or null if no such
+     * link exists.
+     *
+     * @param id the canonicalized link identifier
+     * @return corresponding UI link
+     */
+    public UiLink findLink(UiLinkId id) {
+        return linkLookup.get(id);
+    }
+
+    /**
+     * Adds the given UI link to the topology model.
+     *
+     * @param uiLink link to add
+     */
+    public void add(UiLink uiLink) {
+        linkLookup.put(uiLink.id(), uiLink);
+    }
+
+    /**
+     * Removes the given UI link from the model.
+     *
+     * @param uiLink link to remove
+     */
+    public void remove(UiLink uiLink) {
+        UiLink link = linkLookup.get(uiLink.id());
+        if (link != null) {
+            link.destroy();
+        }
+    }
+
+    /**
+     * Returns the host with the specified identifier, or null if no such
+     * host exists.
+     *
+     * @param id host identifier
+     * @return corresponding UI host
+     */
+    public UiHost findHost(HostId id) {
+        return hostLookup.get(id);
+    }
+
+    /**
+     * Adds the given host to the topology model.
+     *
+     * @param uiHost host to add
+     */
+    public void add(UiHost uiHost) {
+        hostLookup.put(uiHost.id(), uiHost);
+    }
+
+    /**
+     * Removes the given host from the topology model.
+     *
+     * @param uiHost host to remove
+     */
+    public void remove(UiHost uiHost) {
+        UiHost h = hostLookup.remove(uiHost.id());
+        if (h != null) {
+            h.destroy();
+        }
+    }
+
+    // ==
+    // package private methods for supporting linkage amongst topology entities
+    // ==
+
+    /**
+     * Returns the set of UI devices with the given identifiers.
+     *
+     * @param deviceIds device identifiers
+     * @return set of matching UI device instances
+     */
+    Set<UiDevice> deviceSet(Set<DeviceId> deviceIds) {
+        Set<UiDevice> uiDevices = new HashSet<>();
+        for (DeviceId id : deviceIds) {
+            UiDevice d = deviceLookup.get(id);
+            if (d != null) {
+                uiDevices.add(d);
+            } else {
+                log.warn(E_UNMAPPED, "device", id);
+            }
+        }
+        return uiDevices;
+    }
+
+    /**
+     * Returns the set of UI hosts with the given identifiers.
+     *
+     * @param hostIds host identifiers
+     * @return set of matching UI host instances
+     */
+    Set<UiHost> hostSet(Set<HostId> hostIds) {
+        Set<UiHost> uiHosts = new HashSet<>();
+        for (HostId id : hostIds) {
+            UiHost h = hostLookup.get(id);
+            if (h != null) {
+                uiHosts.add(h);
+            } else {
+                log.warn(E_UNMAPPED, "host", id);
+            }
+        }
+        return uiHosts;
+    }
+
+    /**
+     * Returns the set of UI links with the given identifiers.
+     *
+     * @param uiLinkIds link identifiers
+     * @return set of matching UI link instances
+     */
+    Set<UiLink> linkSet(Set<UiLinkId> uiLinkIds) {
+        Set<UiLink> uiLinks = new HashSet<>();
+        for (UiLinkId id : uiLinkIds) {
+            UiLink link = linkLookup.get(id);
+            if (link != null) {
+                uiLinks.add(link);
+            } else {
+                log.warn(E_UNMAPPED, "link", id);
+            }
+        }
+        return uiLinks;
+    }
+
 }