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/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/UiModelEvent.java b/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelEvent.java
deleted file mode 100644
index 4e2a15c..0000000
--- a/web/gui/src/main/java/org/onosproject/ui/impl/topo/model/UiModelEvent.java
+++ /dev/null
@@ -1,86 +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.impl.topo.model;
-
-import com.fasterxml.jackson.databind.node.ObjectNode;
-import org.onosproject.event.AbstractEvent;
-import org.onosproject.ui.model.topo.UiElement;
-
-/**
- * UI Topology model events.
- */
-public class UiModelEvent extends AbstractEvent<UiModelEvent.Type, UiElement> {
-
-    /**
-     * Enumeration of event types.
-     */
-    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
-     */
-    protected 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/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;