Added region-based filtering for the GUI to make sure only the relevant
events get sent to the session.

Change-Id: I649eb1b33fdf9ed4b82e29d7ba7eb3cfac5eadbb
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiEdgeLink.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiEdgeLink.java
index e2dc7fd..8175568 100644
--- a/core/api/src/main/java/org/onosproject/ui/model/topo/UiEdgeLink.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiEdgeLink.java
@@ -16,14 +16,18 @@
 
 package org.onosproject.ui.model.topo;
 
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.HostId;
+import org.onosproject.net.PortNumber;
+
 /**
  * Designates a link between a device and a host; that is, an edge link.
  */
 public class UiEdgeLink extends UiLink {
 
-    private final String hostId;
-    private final String edgeDevice;
-    private final String edgePort;
+    private final HostId hostId;
+    private final DeviceId deviceId;
+    private final PortNumber port;
 
     /**
      * Creates a UI link.
@@ -33,25 +37,53 @@
      */
     public UiEdgeLink(UiTopology topology, UiLinkId id) {
         super(topology, id);
-        hostId = id.idA();
-        edgeDevice = id.elementB().toString();
-        edgePort = id.portB().toString();
+        hostId = HostId.hostId(id.idA());
+        deviceId = (DeviceId) id.elementB();
+        port = id.portB();
     }
 
     @Override
     public String endPointA() {
-        return hostId;
+        return hostId.toString();
     }
 
     @Override
     public String endPointB() {
-        return edgeDevice;
+        return deviceId.toString();
     }
 
     // no port for end-point A
 
     @Override
     public String endPortB() {
-        return edgePort;
+        return port.toString();
     }
+
+    /**
+     * Returns the host identifier.
+     *
+     * @return host identifier
+     */
+    public HostId hostId() {
+        return hostId;
+    }
+
+    /**
+     * Returns the edge device identifier.
+     *
+     * @return device identifier
+     */
+    public DeviceId deviceId() {
+        return deviceId;
+    }
+
+    /**
+     * Returns the edge port number.
+     *
+     * @return edge port number
+     */
+    public PortNumber portNumber() {
+        return port;
+    }
+
 }
diff --git a/core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java
new file mode 100644
index 0000000..5448b88
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java
@@ -0,0 +1,85 @@
+/*
+ * 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.model.topo;
+
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import org.onosproject.event.AbstractEvent;
+
+/**
+ * UI Topology model events.
+ */
+public class UiModelEvent extends AbstractEvent<UiModelEvent.Type, UiElement> {
+
+    /**
+     * Enumeration of event types.
+     */
+    public enum Type {
+        CLUSTER_MEMBER_ADDED_OR_UPDATED,
+        CLUSTER_MEMBER_REMOVED,
+
+        REGION_ADDED_OR_UPDATED,
+        REGION_REMOVED,
+
+        DEVICE_ADDED_OR_UPDATED,
+        DEVICE_REMOVED,
+
+        LINK_ADDED_OR_UPDATED,
+        LINK_REMOVED,
+
+        HOST_ADDED_OR_UPDATED,
+        HOST_MOVED,
+        HOST_REMOVED
+    }
+
+    private final ObjectNode data;
+    private final String memo;
+
+    /**
+     * Creates a UI model event. Note that the memo field can be used to
+     * pass a hint to the listener about the event.
+     *
+     * @param type    event type
+     * @param subject subject of the event
+     * @param data    data containing details of the subject
+     * @param memo    a note about the event
+     */
+    public UiModelEvent(Type type, UiElement subject, ObjectNode data,
+                        String memo) {
+        super(type, subject);
+        this.data = data;
+        this.memo = memo;
+    }
+
+    /**
+     * Returns the data of the subject.
+     *
+     * @return the subject data
+     */
+    public ObjectNode data() {
+        return data;
+    }
+
+    /**
+     * Returns the memo.
+     *
+     * @return the memo
+     */
+    public String memo() {
+        return memo;
+    }
+
+}
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 7a565fe..ada4d15 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
@@ -30,6 +30,7 @@
 
 import static com.google.common.base.MoreObjects.toStringHelper;
 import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.onosproject.net.DeviceId.deviceId;
 import static org.onosproject.net.region.RegionId.regionId;
 
 /**
@@ -311,4 +312,70 @@
         String name = region.name();
         return isNullOrEmpty(name) ? NO_NAME : name;
     }
+
+    /**
+     * Determins whether the specified event is relevant to the view
+     * constrained to this region.
+     *
+     * @param event UI model event
+     * @return true if relevant
+     */
+    public boolean isRelevant(UiModelEvent event) {
+        switch (event.type()) {
+            case CLUSTER_MEMBER_ADDED_OR_UPDATED:
+            case CLUSTER_MEMBER_REMOVED:
+                return true;
+
+            case REGION_ADDED_OR_UPDATED:
+            case REGION_REMOVED:
+                return isRegionRelevant(((UiRegion) event.subject()).id());
+
+            case DEVICE_ADDED_OR_UPDATED:
+            case DEVICE_REMOVED:
+                return isDeviceRelevant(((UiDevice) event.subject()).id());
+
+            case LINK_ADDED_OR_UPDATED:
+            case LINK_REMOVED:
+                return isLinkRelevant((UiLink) event.subject());
+
+            case HOST_ADDED_OR_UPDATED:
+            case HOST_MOVED:
+            case HOST_REMOVED:
+                return isDeviceRelevant(((UiHost) event.subject()).locationDevice());
+
+            default:
+                return true;
+        }
+    }
+
+    private boolean isDeviceRelevant(DeviceId deviceId) {
+        return deviceIds.contains(deviceId);
+    }
+
+    private boolean isLinkRelevant(UiLink uiLink) {
+        if (uiLink instanceof UiDeviceLink) {
+            UiDeviceLink uiDeviceLink = (UiDeviceLink) uiLink;
+            return isDeviceRelevant(uiDeviceLink.deviceA()) ||
+                    isDeviceRelevant(uiDeviceLink.deviceB());
+
+        } else if (uiLink instanceof UiRegionLink) {
+            UiRegionLink uiRegionLink = (UiRegionLink) uiLink;
+            return isRegionRelevant(uiRegionLink.regionA()) ||
+                    isRegionRelevant(uiRegionLink.regionB());
+
+        } else if (uiLink instanceof UiRegionDeviceLink) {
+            UiRegionDeviceLink uiRegionDeviceLink = (UiRegionDeviceLink) uiLink;
+            return isRegionRelevant(uiRegionDeviceLink.region()) ||
+                    isDeviceRelevant(uiRegionDeviceLink.device());
+
+        } else if (uiLink instanceof UiEdgeLink) {
+            UiEdgeLink uiEdgeLink = (UiEdgeLink) uiLink;
+            return isDeviceRelevant(uiEdgeLink.deviceId());
+        }
+        return false;
+    }
+
+    private boolean isRegionRelevant(RegionId regionId) {
+        return kids.contains(regionId);
+    }
 }
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 58e1b13..8c8740b 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
@@ -271,6 +271,7 @@
      */
     public void remove(UiDevice uiDevice) {
         UiDevice d = deviceLookup.remove(uiDevice.id());
+        // TODO: Update the containing region
         if (d != null) {
             d.destroy();
         }