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/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelEvent.java b/core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java
similarity index 62%
rename from web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelEvent.java
rename to core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java
index 4e2a15c..5448b88 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelEvent.java
+++ b/core/api/src/main/java/org/onosproject/ui/model/topo/UiModelEvent.java
@@ -1,24 +1,23 @@
 /*
- *  Copyright 2016-present Open Networking Laboratory
+ * 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
+ * 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
+ *     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.
+ * 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.model;
+package org.onosproject.ui.model.topo;
 
 import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onosproject.event.AbstractEvent;
-import org.onosproject.ui.model.topo.UiElement;
 
 /**
  * UI Topology model events.
@@ -28,7 +27,7 @@
     /**
      * Enumeration of event types.
      */
-    enum Type {
+    public enum Type {
         CLUSTER_MEMBER_ADDED_OR_UPDATED,
         CLUSTER_MEMBER_REMOVED,
 
@@ -58,8 +57,8 @@
      * @param data    data containing details of the subject
      * @param memo    a note about the event
      */
-    protected UiModelEvent(Type type, UiElement subject, ObjectNode data,
-                           String memo) {
+    public UiModelEvent(Type type, UiElement subject, ObjectNode data,
+                        String memo) {
         super(type, subject);
         this.data = data;
         this.memo = 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();
         }
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
index b407171..a2c13c5 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/Topo2Jsonifier.java
@@ -45,7 +45,7 @@
 import org.onosproject.ui.UiPreferencesService;
 import org.onosproject.ui.UiTopoMap;
 import org.onosproject.ui.UiTopoMapFactory;
-import org.onosproject.ui.impl.topo.model.UiModelEvent;
+import org.onosproject.ui.model.topo.UiModelEvent;
 import org.onosproject.ui.model.topo.UiClusterMember;
 import org.onosproject.ui.model.topo.UiDevice;
 import org.onosproject.ui.model.topo.UiElement;
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/UiTopoSession.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/UiTopoSession.java
index 288a343..3f7f4dc 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/UiTopoSession.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/UiTopoSession.java
@@ -16,11 +16,10 @@
 
 package org.onosproject.ui.impl.topo;
 
-import com.fasterxml.jackson.databind.node.ObjectNode;
 import org.onosproject.net.region.RegionId;
 import org.onosproject.ui.UiTopoLayoutService;
 import org.onosproject.ui.impl.UiWebSocket;
-import org.onosproject.ui.impl.topo.model.UiModelEvent;
+import org.onosproject.ui.model.topo.UiModelEvent;
 import org.onosproject.ui.impl.topo.model.UiModelListener;
 import org.onosproject.ui.impl.topo.model.UiSharedTopologyModel;
 import org.onosproject.ui.model.topo.UiClusterMember;
@@ -129,23 +128,17 @@
     }
 
     @Override
-    public void event(UiModelEvent event) {
-        String msg = messagesEnabled
-                ? "Event received: {}"
-                : "Event received: {}, but not transmitted";
-        log.debug(msg, event);
-
-        if (messagesEnabled) {
-            ObjectNode payload = t2json.jsonEvent(event);
-
-            // TODO: add filtering for relevant objects only...
-            // TO Decide: Since the session holds the state of what is being
-            //   displayed on the client, we should filter out any model events
-            //   that are not relevant, and only send up events for objects that
-            //   are currently being viewed by the user.
-
-            webSocket.sendMessage(TOPO2_UI_MODEL_EVENT, payload);
+    public boolean isRelevant(UiModelEvent event) {
+        if (!messagesEnabled) {
+            return false;
         }
+        UiRegion uiRegion = sharedModel.getRegion(currentLayout.regionId());
+        return uiRegion.isRelevant(event);
+    }
+
+    @Override
+    public void event(UiModelEvent event) {
+        webSocket.sendMessage(TOPO2_UI_MODEL_EVENT, t2json.jsonEvent(event));
     }
 
     /**
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
index 12d75f3..ec07673 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/ModelCache.java
@@ -40,6 +40,7 @@
 import org.onosproject.ui.model.topo.UiElement;
 import org.onosproject.ui.model.topo.UiHost;
 import org.onosproject.ui.model.topo.UiLinkId;
+import org.onosproject.ui.model.topo.UiModelEvent;
 import org.onosproject.ui.model.topo.UiRegion;
 import org.onosproject.ui.model.topo.UiSynthLink;
 import org.onosproject.ui.model.topo.UiTopoLayout;
@@ -53,17 +54,17 @@
 import java.util.Set;
 
 import static org.onosproject.net.DefaultEdgeLink.createEdgeLink;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.DEVICE_REMOVED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_MOVED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.HOST_REMOVED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.LINK_REMOVED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
-import static org.onosproject.ui.impl.topo.model.UiModelEvent.Type.REGION_REMOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_ADDED_OR_UPDATED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.CLUSTER_MEMBER_REMOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_ADDED_OR_UPDATED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.DEVICE_REMOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_ADDED_OR_UPDATED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_MOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.HOST_REMOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_ADDED_OR_UPDATED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.LINK_REMOVED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_ADDED_OR_UPDATED;
+import static org.onosproject.ui.model.topo.UiModelEvent.Type.REGION_REMOVED;
 import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
 
 /**
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelListener.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelListener.java
index bdbc70a..1342414 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelListener.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelListener.java
@@ -17,6 +17,7 @@
 package org.onosproject.ui.impl.topo.model;
 
 import org.onosproject.event.EventListener;
+import org.onosproject.ui.model.topo.UiModelEvent;
 
 /**
  * Can receive {@link UiModelEvent}s.
diff --git a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
index 80d134a..6500ae3 100644
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
+++ b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiSharedTopologyModel.java
@@ -67,6 +67,7 @@
 import org.onosproject.ui.model.topo.UiDevice;
 import org.onosproject.ui.model.topo.UiDeviceLink;
 import org.onosproject.ui.model.topo.UiHost;
+import org.onosproject.ui.model.topo.UiModelEvent;
 import org.onosproject.ui.model.topo.UiRegion;
 import org.onosproject.ui.model.topo.UiSynthLink;
 import org.slf4j.Logger;
diff --git a/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java
index e8c2d3b..606756b 100644
--- a/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java
+++ b/web/gui/src/test/java/org/onosproject/ui/impl/topo/model/ModelCacheTest.java
@@ -26,7 +26,7 @@
 import org.onosproject.net.HostId;
 import org.onosproject.net.Link;
 import org.onosproject.net.region.Region;
-import org.onosproject.ui.impl.topo.model.UiModelEvent.Type;
+import org.onosproject.ui.model.topo.UiModelEvent.Type;
 import org.onosproject.ui.model.topo.UiClusterMember;
 import org.onosproject.ui.model.topo.UiDevice;
 import org.onosproject.ui.model.topo.UiDeviceLink;