GUI -- Major re-write of server-side Topology View code. WIP.

Change-Id: I332e8e59dc2271d3897c3a5c2fafa775324ef72d
diff --git a/core/api/src/main/java/org/onosproject/ui/JsonUtils.java b/core/api/src/main/java/org/onosproject/ui/JsonUtils.java
index db753e2..b427e33c 100644
--- a/core/api/src/main/java/org/onosproject/ui/JsonUtils.java
+++ b/core/api/src/main/java/org/onosproject/ui/JsonUtils.java
@@ -39,6 +39,7 @@
      * @param payload event payload
      * @return the object node representation
      */
+    @Deprecated
     public static ObjectNode envelope(String type, long sid, ObjectNode payload) {
         ObjectNode event = MAPPER.createObjectNode();
         event.put("event", type);
@@ -50,6 +51,20 @@
     }
 
     /**
+     * Composes a message structure for the given message type and payload.
+     *
+     * @param type    message type
+     * @param payload message payload
+     * @return the object node representation
+     */
+    public static ObjectNode envelope(String type, ObjectNode payload) {
+        ObjectNode event = MAPPER.createObjectNode();
+        event.put("event", type);
+        event.set("payload", payload);
+        return event;
+    }
+
+    /**
      * Returns the event type from the specified event.
      * If the node does not have an "event" property, "unknown" is returned.
      *
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/AltTopoViewMessageHandler.java b/web/gui/src/main/java/org/onosproject/ui/impl/AltTopoViewMessageHandler.java
new file mode 100644
index 0000000..068bfe9
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/AltTopoViewMessageHandler.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2015 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.impl;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.google.common.collect.ImmutableSet;
+import org.onlab.osgi.ServiceDirectory;
+import org.onosproject.core.CoreService;
+import org.onosproject.ui.RequestHandler;
+import org.onosproject.ui.UiConnection;
+import org.onosproject.ui.UiMessageHandler;
+import org.onosproject.ui.impl.topo.TopoUiEvent;
+import org.onosproject.ui.impl.topo.TopoUiListener;
+import org.onosproject.ui.impl.topo.TopoUiModelService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Facility for handling inbound messages from the topology view, and
+ * generating outbound messages for the same.
+ */
+public class AltTopoViewMessageHandler extends UiMessageHandler {
+
+    private static final String TOPO_START = "topoStart";
+    private static final String TOPO_STOP = "topoStop";
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    protected ServiceDirectory directory;
+    protected TopoUiModelService modelService;
+
+    private TopoUiListener modelListener;
+    private String version;
+
+    private boolean topoActive = false;
+
+    @Override
+    public void init(UiConnection connection, ServiceDirectory directory) {
+        super.init(connection, directory);
+        this.directory = checkNotNull(directory, "Directory cannot be null");
+        modelService = directory.get(TopoUiModelService.class);
+
+        modelListener = new ModelListener();
+        version = getVersion();
+    }
+
+
+    private String getVersion() {
+        String ver = directory.get(CoreService.class).version().toString();
+        return ver.replace(".SNAPSHOT", "*").replaceFirst("~.*$", "");
+    }
+
+
+    @Override
+    protected Collection<RequestHandler> getHandlers() {
+        return ImmutableSet.of(
+                new TopoStart(),
+                new TopoStop()
+        );
+    }
+
+    // =====================================================================
+    // Request Handlers for (topo view) events from the UI...
+
+    private final class TopoStart extends RequestHandler {
+        private TopoStart() {
+            super(TOPO_START);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            topoActive = true;
+            modelService.addListener(modelListener);
+            sendMessages(modelService.getInitialState());
+            log.debug(TOPO_START);
+        }
+    }
+
+    private final class TopoStop extends RequestHandler {
+        private TopoStop() {
+            super(TOPO_STOP);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            topoActive = false;
+            modelService.removeListener(modelListener);
+            log.debug(TOPO_STOP);
+        }
+    }
+
+    // =====================================================================
+    // Private Helper Methods...
+
+    private void sendMessages(Collection<ObjectNode> messages) {
+        if (topoActive) {
+            UiConnection connection = connection();
+            if (connection != null) {
+                messages.forEach(connection::sendMessage);
+            }
+        }
+    }
+
+    // =====================================================================
+    // Our listener for model events so we can push changes out to the UI...
+
+    private class ModelListener implements TopoUiListener {
+        @Override
+        public void event(TopoUiEvent event) {
+            log.debug("Handle Event: {}", event);
+            // TODO: handle event
+        }
+    }
+}
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 6f72dcb..9f24f39 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
@@ -101,6 +101,7 @@
 /**
  * Facility for creating messages bound for the topology viewer.
  */
+@Deprecated
 public abstract class TopologyViewMessageHandlerBase extends UiMessageHandler {
 
     protected static final Logger log =
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
index a09ae58..b4a5999 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/UiExtensionManager.java
@@ -78,6 +78,7 @@
         UiMessageHandlerFactory messageHandlerFactory =
                 () -> ImmutableList.of(
                         new TopologyViewMessageHandler(),
+//                        new AltTopoViewMessageHandler(),
                         new DeviceViewMessageHandler(),
                         new LinkViewMessageHandler(),
                         new HostViewMessageHandler(),
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/MetaDb.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/MetaDb.java
new file mode 100644
index 0000000..f890137
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/MetaDb.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * A database of meta information stored for topology objects.
+ */
+// package private
+class MetaDb {
+
+    private static Map<String, ObjectNode> metaCache = new ConcurrentHashMap<>();
+
+    /**
+     * Adds meta UI information about the specified object to the given payload.
+     *
+     * @param id object identifier
+     * @param payload payload to which the info should be added
+     */
+    public void addMetaUi(String id, ObjectNode payload) {
+        ObjectNode meta = metaCache.get(id);
+        if (meta != null) {
+            payload.set("metaUi", meta);
+        }
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoMessageFactory.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoMessageFactory.java
new file mode 100644
index 0000000..59af263
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoMessageFactory.java
@@ -0,0 +1,337 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onlab.packet.IpAddress;
+import org.onosproject.cluster.ClusterEvent;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.cluster.NodeId;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.Annotated;
+import org.onosproject.net.AnnotationKeys;
+import org.onosproject.net.Annotations;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DefaultEdgeLink;
+import org.onosproject.net.Device;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.EdgeLink;
+import org.onosproject.net.Host;
+import org.onosproject.net.HostId;
+import org.onosproject.net.HostLocation;
+import org.onosproject.net.Link;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.host.HostEvent;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.link.LinkEvent;
+import org.onosproject.net.link.LinkService;
+import org.onosproject.net.provider.ProviderId;
+import org.onosproject.ui.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onosproject.cluster.ControllerNode.State.ACTIVE;
+import static org.onosproject.net.PortNumber.portNumber;
+
+/**
+ * Facility for generating messages in {@link ObjectNode} form from
+ * ONOS model events.
+ */
+// package private
+class TopoMessageFactory {
+
+    private static final ProviderId PROVIDER_ID =
+            new ProviderId("core", "org.onosproject.core", true);
+    private static final String COMPACT = "%s/%s-%s/%s";
+    private static final PortNumber PORT_ZERO = portNumber(0);
+
+    private static final Map<Enum<?>, String> LOOKUP = new HashMap<>();
+
+    static {
+        LOOKUP.put(ClusterEvent.Type.INSTANCE_ADDED, "addInstance");
+        LOOKUP.put(ClusterEvent.Type.INSTANCE_REMOVED, "removeInstance");
+        LOOKUP.put(DeviceEvent.Type.DEVICE_ADDED, "addDevice");
+        LOOKUP.put(DeviceEvent.Type.DEVICE_UPDATED, "updateDevice");
+        LOOKUP.put(DeviceEvent.Type.DEVICE_REMOVED, "removeDevice");
+        LOOKUP.put(LinkEvent.Type.LINK_ADDED, "addLink");
+        LOOKUP.put(LinkEvent.Type.LINK_UPDATED, "updateLink");
+        LOOKUP.put(LinkEvent.Type.LINK_REMOVED, "removeLink");
+        LOOKUP.put(HostEvent.Type.HOST_ADDED, "addHost");
+        LOOKUP.put(HostEvent.Type.HOST_UPDATED, "updateHost");
+        LOOKUP.put(HostEvent.Type.HOST_REMOVED, "removeHost");
+    }
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private MetaDb metaDb;
+
+    private ClusterService clusterService;
+    private DeviceService deviceService;
+    private LinkService linkService;
+    private HostService hostService;
+    private MastershipService mastershipService;
+
+
+    // ===================================================================
+    // Private helper methods
+
+    private ObjectNode objectNode() {
+        return MAPPER.createObjectNode();
+    }
+
+    private ArrayNode arrayNode() {
+        return MAPPER.createArrayNode();
+    }
+
+    private String toLc(Object o) {
+        return o.toString().toLowerCase();
+    }
+
+    // Event type to message type lookup (with fallback).
+    private String messageTypeLookup(Enum<?> type, String fallback) {
+        String msgType = LOOKUP.get(type);
+        return msgType == null ? fallback : msgType;
+    }
+
+    // Returns the name of the master node for the specified device ID.
+    private String master(DeviceId deviceId) {
+        NodeId master = mastershipService.getMasterFor(deviceId);
+        return master != null ? master.toString() : "";
+    }
+
+    // Produces JSON structure from annotations.
+    private ObjectNode props(Annotations annotations) {
+        ObjectNode props = objectNode();
+        if (annotations != null) {
+            for (String key : annotations.keys()) {
+                props.put(key, annotations.value(key));
+            }
+        }
+        return props;
+    }
+
+    // Adds a geo location JSON to the specified payload object.
+    private void addGeoLocation(Annotated annotated, ObjectNode payload) {
+        Annotations annot = annotated.annotations();
+        if (annot == null) {
+            return;
+        }
+
+        String slat = annot.value(AnnotationKeys.LATITUDE);
+        String slng = annot.value(AnnotationKeys.LONGITUDE);
+        try {
+            if (!isNullOrEmpty(slat) && !isNullOrEmpty(slng)) {
+                double lat = Double.parseDouble(slat);
+                double lng = Double.parseDouble(slng);
+                ObjectNode loc = objectNode()
+                        .put("type", "latlng")
+                        .put("lat", lat)
+                        .put("lng", lng);
+                payload.set("location", loc);
+            }
+        } catch (NumberFormatException e) {
+            log.warn("Invalid geo data latitude={}; longitude={}", slat, slng);
+        }
+    }
+
+    // Produces compact string representation of a link.
+    private String compactLinkString(Link link) {
+        return String.format(COMPACT, link.src().elementId(), link.src().port(),
+                             link.dst().elementId(), link.dst().port());
+    }
+
+    // Generates an edge link from the specified host location.
+    private EdgeLink edgeLink(Host host, boolean isIngress) {
+        ConnectPoint cp = new ConnectPoint(host.id(), PORT_ZERO);
+        return new DefaultEdgeLink(PROVIDER_ID, cp, host.location(), isIngress);
+    }
+
+    // Encodes the specified host location into a JSON object.
+    private ObjectNode hostConnect(HostLocation loc) {
+        return objectNode()
+                .put("device", loc.deviceId().toString())
+                .put("port", loc.port().toLong());
+    }
+
+    // Returns the first IP address from the specified set.
+    private String firstIp(Set<IpAddress> addresses) {
+        Iterator<IpAddress> it = addresses.iterator();
+        return it.hasNext() ? it.next().toString() : "unknown";
+    }
+
+    // Returns a JSON array of the specified strings.
+    private ArrayNode labels(String... labels) {
+        ArrayNode array = arrayNode();
+        for (String label : labels) {
+            array.add(label);
+        }
+        return array;
+    }
+
+    // ===================================================================
+    // API for generating messages
+
+    /**
+     * Injects service references so that the message compilation methods
+     * can do required lookups when needed.
+     *
+     * @param meta meta DB
+     * @param cs cluster service
+     * @param ds device service
+     * @param ls link service
+     * @param hs host service
+     * @param ms mastership service
+     */
+    public void injectServices(MetaDb meta, ClusterService cs, DeviceService ds,
+                               LinkService ls, HostService hs,
+                               MastershipService ms) {
+        metaDb = meta;
+        clusterService = cs;
+        deviceService = ds;
+        linkService = ls;
+        hostService = hs;
+        mastershipService = ms;
+    }
+
+    /**
+     * Transforms a cluster event into an object-node-based message.
+     *
+     * @param ev cluster event
+     * @return marshaled event message
+     */
+    public ObjectNode instanceMessage(ClusterEvent ev) {
+        ControllerNode node = ev.subject();
+        NodeId nid = node.id();
+        String id = nid.toString();
+        String ip = node.ip().toString();
+        int switchCount = mastershipService.getDevicesOf(nid).size();
+
+        ObjectNode payload = objectNode()
+                .put("id", id)
+                .put("ip", ip)
+                .put("online", clusterService.getState(nid) == ACTIVE)
+                .put("uiAttached", node.equals(clusterService.getLocalNode()))
+                .put("switches", switchCount);
+
+        ArrayNode labels = arrayNode().add(id).add(ip);
+
+        payload.set("labels", labels);
+        metaDb.addMetaUi(id, payload);
+
+        String msgType = messageTypeLookup(ev.type(), "addInstance");
+        return JsonUtils.envelope(msgType, payload);
+    }
+
+    /**
+     * Transforms a device event into an object-node-based message.
+     *
+     * @param ev device event
+     * @return marshaled event message
+     */
+    public ObjectNode deviceMessage(DeviceEvent ev) {
+        Device device = ev.subject();
+        DeviceId did = device.id();
+        String id = did.toString();
+
+        ObjectNode payload = objectNode()
+                .put("id", id)
+                .put("type", toLc(device.type()))
+                .put("online", deviceService.isAvailable(did))
+                .put("master", master(did));
+
+        Annotations annot = device.annotations();
+        String name = annot.value(AnnotationKeys.NAME);
+        String friendly = isNullOrEmpty(name) ? id : name;
+        payload.set("labels", labels("", friendly, id));
+        payload.set("props", props(annot));
+
+        addGeoLocation(device, payload);
+        metaDb.addMetaUi(id, payload);
+
+        String msgType = messageTypeLookup(ev.type(), "updateDevice");
+        return JsonUtils.envelope(msgType, payload);
+    }
+
+    /**
+     * Transforms a link event into an object-node-based message.
+     *
+     * @param ev link event
+     * @return marshaled event message
+     */
+    public ObjectNode linkMessage(LinkEvent ev) {
+        Link link = ev.subject();
+        ObjectNode payload = objectNode()
+                .put("id", compactLinkString(link))
+                .put("type", toLc(link.type()))
+                .put("online", link.state() == Link.State.ACTIVE)
+                .put("linkWidth", 1.2)
+                .put("src", link.src().deviceId().toString())
+                .put("srcPort", link.src().port().toString())
+                .put("dst", link.dst().deviceId().toString())
+                .put("dstPort", link.dst().port().toString());
+
+        String msgType = messageTypeLookup(ev.type(), "updateLink");
+        return JsonUtils.envelope(msgType, payload);
+    }
+
+    /**
+     * Transforms a host event into an object-node-based message.
+     *
+     * @param ev host event
+     * @return marshaled event message
+     */
+    public ObjectNode hostMessage(HostEvent ev) {
+        Host host = ev.subject();
+        HostId hid = host.id();
+        String id = hid.toString();
+        Annotations annot = host.annotations();
+
+        String hostType = annot.value(AnnotationKeys.TYPE);
+
+        ObjectNode payload = objectNode()
+                .put("id", id)
+                .put("type", isNullOrEmpty(hostType) ? "endstation" : hostType)
+                .put("ingress", compactLinkString(edgeLink(host, true)))
+                .put("egress", compactLinkString(edgeLink(host, false)));
+
+        // TODO: make cp an array of connect point objects (multi-homed)
+        payload.set("cp", hostConnect(host.location()));
+        String ipStr = firstIp(host.ipAddresses());
+        String macStr = host.mac().toString();
+        payload.set("labels", labels(ipStr, macStr));
+        payload.set("props", props(annot));
+        addGeoLocation(host, payload);
+        metaDb.addMetaUi(id, payload);
+
+        String mstType = messageTypeLookup(ev.type(), "updateHost");
+        return JsonUtils.envelope(mstType, payload);
+    }
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java
new file mode 100644
index 0000000..32ff62b
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiEvent.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * Describes Topology UI Model events.
+ */
+public class TopoUiEvent extends AbstractEvent<TopoUiEvent.Type, ObjectNode> {
+
+    /**
+     * Type of Topology UI Model events.
+     */
+    public enum Type {
+        INSTANCE_ADDED,
+        INSTANCE_REMOVED,
+        DEVICE_ADDED,
+        DEVICE_UPDATED,
+        DEVICE_REMOVED,
+        LINK_ADDED,
+        LINK_UPDATED,
+        LINK_REMOVED,
+        HOST_ADDED,
+        HOST_UPDATED,
+        HOST_REMOVED
+    }
+
+
+    protected TopoUiEvent(Type type, ObjectNode subject) {
+        super(type, subject);
+    }
+
+    protected TopoUiEvent(Type type, ObjectNode subject, long time) {
+        super(type, subject, time);
+    }
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiListener.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiListener.java
new file mode 100644
index 0000000..df49954
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiListener.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import org.onosproject.event.EventListener;
+
+/**
+ * Defines a listener of Topology UI Model events.
+ */
+public interface TopoUiListener extends EventListener<TopoUiEvent> {
+
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java
new file mode 100644
index 0000000..95e8430
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelManager.java
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.Service;
+import org.onosproject.cluster.ClusterEvent;
+import org.onosproject.cluster.ClusterService;
+import org.onosproject.cluster.ControllerNode;
+import org.onosproject.event.AbstractListenerRegistry;
+import org.onosproject.event.EventDeliveryService;
+import org.onosproject.mastership.MastershipService;
+import org.onosproject.net.Device;
+import org.onosproject.net.Host;
+import org.onosproject.net.Link;
+import org.onosproject.net.device.DeviceEvent;
+import org.onosproject.net.device.DeviceService;
+import org.onosproject.net.host.HostEvent;
+import org.onosproject.net.host.HostService;
+import org.onosproject.net.link.LinkEvent;
+import org.onosproject.net.link.LinkService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+import static org.onosproject.cluster.ClusterEvent.Type.INSTANCE_ADDED;
+import static org.onosproject.net.device.DeviceEvent.Type.DEVICE_ADDED;
+import static org.onosproject.net.host.HostEvent.Type.HOST_ADDED;
+import static org.onosproject.net.link.LinkEvent.Type.LINK_ADDED;
+
+
+/**
+ * Maintains a UI-centric model of the topology, as inferred from interactions
+ * with the different (device, host, link, ...) services. Will serve up this
+ * model to anyone who cares to {@link TopoUiListener listen}.
+ */
+@Component(immediate = true)
+@Service
+public class TopoUiModelManager implements TopoUiModelService {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ClusterService clusterService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected LinkService linkService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostService hostService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected MastershipService mastershipService;
+
+
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EventDeliveryService eventDispatcher;
+
+
+    private AbstractListenerRegistry<TopoUiEvent, TopoUiListener>
+            listenerRegistry = new AbstractListenerRegistry<>();
+
+
+    private final TopoMessageFactory messageFactory = new TopoMessageFactory();
+    private final MetaDb metaDb = new MetaDb();
+
+
+    @Activate
+    public void activate() {
+        eventDispatcher.addSink(TopoUiEvent.class, listenerRegistry);
+        messageFactory.injectServices(
+                metaDb,
+                clusterService,
+                deviceService,
+                linkService,
+                hostService,
+                mastershipService
+        );
+        log.info("Started");
+    }
+
+    @Deactivate
+    public void deactivate() {
+        eventDispatcher.removeSink(TopoUiEvent.class);
+        log.info("Stopped");
+    }
+
+    @Override
+    public void addListener(TopoUiListener listener) {
+        listenerRegistry.addListener(listener);
+    }
+
+    @Override
+    public void removeListener(TopoUiListener listener) {
+        listenerRegistry.removeListener(listener);
+    }
+
+    @Override
+    public List<ObjectNode> getInitialState() {
+        List<ObjectNode> results = new ArrayList<>();
+        addInstances(results);
+        addDevices(results);
+        addLinks(results);
+        addHosts(results);
+        return results;
+    }
+
+    // =====================================================================
+
+    private static final Comparator<? super ControllerNode> NODE_COMPARATOR =
+            (o1, o2) -> o1.id().toString().compareTo(o2.id().toString());
+
+    // =====================================================================
+
+    private void addInstances(List<ObjectNode> results) {
+        List<ControllerNode> nodes = new ArrayList<>(clusterService.getNodes());
+        Collections.sort(nodes, NODE_COMPARATOR);
+        for (ControllerNode node : nodes) {
+            ClusterEvent ev = new ClusterEvent(INSTANCE_ADDED, node);
+            results.add(messageFactory.instanceMessage(ev));
+        }
+    }
+
+    private void addDevices(List<ObjectNode> results) {
+        // Send optical first, others later -- for layered rendering
+        List<DeviceEvent> deferred = new ArrayList<>();
+
+        for (Device device : deviceService.getDevices()) {
+            DeviceEvent ev = new DeviceEvent(DEVICE_ADDED, device);
+            if (device.type() == Device.Type.ROADM) {
+                results.add(messageFactory.deviceMessage(ev));
+            } else {
+                deferred.add(ev);
+            }
+        }
+
+        for (DeviceEvent ev : deferred) {
+            results.add(messageFactory.deviceMessage(ev));
+        }
+    }
+
+    private void addLinks(List<ObjectNode> results) {
+        // Send optical first, others later -- for layered rendering
+        List<LinkEvent> deferred = new ArrayList<>();
+
+        for (Link link : linkService.getLinks()) {
+            LinkEvent ev = new LinkEvent(LINK_ADDED, link);
+            if (link.type() == Link.Type.OPTICAL) {
+                results.add(messageFactory.linkMessage(ev));
+            } else {
+                deferred.add(ev);
+            }
+        }
+
+        for (LinkEvent ev : deferred) {
+            results.add(messageFactory.linkMessage(ev));
+        }
+    }
+
+    private void addHosts(List<ObjectNode> results) {
+        for (Host host : hostService.getHosts()) {
+            HostEvent ev = new HostEvent(HOST_ADDED, host);
+            results.add(messageFactory.hostMessage(ev));
+        }
+    }
+
+    // =====================================================================
+
+    private void post(TopoUiEvent event) {
+        if (event != null) {
+            eventDispatcher.post(event);
+        }
+    }
+
+    // NOTE: session-independent state only
+    // private inner classes to listen to device/host/link events
+    // TODO..
+}
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java
new file mode 100644
index 0000000..7bbbbe8
--- /dev/null
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/TopoUiModelService.java
@@ -0,0 +1,54 @@
+/*
+ * Copyright 2015 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.impl.topo;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+
+import java.util.List;
+
+/**t
+ * Defines the API for the Topology UI Model.
+ */
+public interface TopoUiModelService {
+
+    /**
+     * Registers the specified listener for Topology UI Model events.
+     *
+     * @param listener the listener
+     */
+    void addListener(TopoUiListener listener);
+
+    /**
+     * Unregister the specified listener.
+     *
+     * @param listener the listener
+     */
+    void removeListener(TopoUiListener listener);
+
+
+    /**
+     * Returns events describing the current state of the model.
+     * <p>
+     * These will be in the form of "addInstance", "addDevice", "addLink",
+     * and "addHost" events, as appropriate.
+     *
+     * @return initial state events
+     */
+    List<ObjectNode> getInitialState();
+
+}