Add listner for down paths to path painter App and highlights remove when changing overlay

Change-Id: I1bf94468a61bd1a7211532a5b8e9e6cdfbecbda5
diff --git a/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovMessageHandler.java b/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovMessageHandler.java
index 8ead8be..cf395e4 100644
--- a/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovMessageHandler.java
+++ b/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovMessageHandler.java
@@ -30,6 +30,9 @@
 import org.onosproject.net.topology.GeoDistanceLinkWeight;
 import org.onosproject.net.topology.LinkWeight;
 import org.onosproject.net.topology.PathService;
+import org.onosproject.net.topology.TopologyEvent;
+import org.onosproject.net.topology.TopologyListener;
+import org.onosproject.net.topology.TopologyService;
 import org.onosproject.ui.RequestHandler;
 import org.onosproject.ui.UiConnection;
 import org.onosproject.ui.UiMessageHandler;
@@ -50,6 +53,7 @@
  */
 public class PathPainterTopovMessageHandler extends UiMessageHandler {
 
+    private static final String PAINTER_CLEAR = "ppTopovClear";
     private static final String PAINTER_SET_SRC = "ppTopovSetSrc";
     private static final String PAINTER_SET_DST = "ppTopovSetDst";
     private static final String PAINTER_SWAP_SRC_DST = "ppTopovSwapSrcDst";
@@ -67,7 +71,10 @@
     public static final String SRC = "Src";
     private static LinkWeight linkData;
 
+    private final TopologyListener topologyListener = new InternalTopologyListener();
+
     private Set<Link> allPathLinks;
+    private boolean listenersRemoved;
 
     private enum Mode {
         SHORTEST, DISJOINT, GEODATA, SRLG, INVALID
@@ -83,6 +90,8 @@
     private List<Path> paths;
     private int pathIndex;
 
+    protected TopologyService topologyService;
+
 
     // ===============-=-=-=-=-=-======================-=-=-=-=-=-=-================================
 
@@ -91,12 +100,22 @@
     public void init(UiConnection connection, ServiceDirectory directory) {
         super.init(connection, directory);
         pathService = directory.get(PathService.class);
+        topologyService = directory.get(TopologyService.class);
         linkData = new GeoDistanceLinkWeight(directory.get(DeviceService.class));
+        addListeners();
+    }
+
+
+    @Override
+    public void destroy() {
+        removeListeners();
+        super.destroy();
     }
 
     @Override
     protected Collection<RequestHandler> createRequestHandlers() {
         return ImmutableSet.of(
+                new ClearHandler(),
                 new SetSrcHandler(),
                 new SetDstHandler(),
                 new SwapSrcDstHandler(),
@@ -109,6 +128,20 @@
     // === -------------------------
     // === Handler classes
 
+    private final class ClearHandler extends RequestHandler {
+
+        public ClearHandler() {
+            super(PAINTER_CLEAR);
+        }
+
+        @Override
+        public void process(long sid, ObjectNode payload) {
+            src = null;
+            dst = null;
+            sendMessage(TopoJson.highlightsMessage(new Highlights()));
+        }
+    }
+
     private final class SetSrcHandler extends RequestHandler {
 
         public SetSrcHandler() {
@@ -333,4 +366,23 @@
         return NodeBadge.text(type);
     }
 
+    private synchronized void addListeners() {
+        listenersRemoved = false;
+        topologyService.addListener(topologyListener);
+    }
+    private synchronized void removeListeners() {
+        if (!listenersRemoved) {
+            listenersRemoved = true;
+            topologyService.removeListener(topologyListener);
+        }
+    }
+
+    // Link event listener.
+    private class InternalTopologyListener implements TopologyListener {
+        @Override
+        public void event(TopologyEvent event) {
+            findAndSendPaths(currentMode);
+        }
+    }
+
 }
\ No newline at end of file
diff --git a/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovOverlay.java b/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovOverlay.java
index 47ae0d3..78f5f90 100644
--- a/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovOverlay.java
+++ b/apps/pathpainter/src/main/java/org/onosproject/pathpainter/PathPainterTopovOverlay.java
@@ -37,6 +37,12 @@
     }
 
     @Override
+    public void deactivate() {
+        super.deactivate();
+        log.debug("PathPainterOverlay Deactivated");
+    }
+
+    @Override
     public void modifyDeviceDetails(PropertyPanel pp, DeviceId deviceId) {
         pp.addButton(SRC_BUTTON).addButton(DST_BUTTON);
     }
diff --git a/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopov.js b/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopov.js
index 213d9c5..baaaef9 100644
--- a/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopov.js
+++ b/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopov.js
@@ -31,6 +31,7 @@
         swapMessage = 'ppTopovSwapSrcDst',
         modeMessage = 'ppTopovSetMode',
         nextPathMessage = 'ppTopovNextPath',
+        clearMessage = 'ppTopovClear',
         prevPathMessage = 'ppTopovPrevPath';
 
     // internal state
@@ -44,6 +45,10 @@
     // === ---------------------------
     // === Main API functions
 
+    function clear() {
+        wss.sendEvent(clearMessage);
+        flash.flash('Source node: ' + node.id);
+    }
 
     function setSrc(node) {
         wss.sendEvent(srcMessage, {
@@ -107,7 +112,8 @@
                 setMode: setMode,
                 nextPath: nextPath,
                 prevPath: prevPath,
-                swapSrcDst: swapSrcDst
+                swapSrcDst: swapSrcDst,
+                clear: clear
             };
         }]);
 }());
diff --git a/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopovOverlay.js b/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopovOverlay.js
index a2219a0..f98ed2b 100644
--- a/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopovOverlay.js
+++ b/apps/pathpainter/src/main/resources/app/view/ppTopov/ppTopovOverlay.js
@@ -19,6 +19,13 @@
         glyphId: 'topo',
         tooltip: 'Path Painter Topo Overlay',
 
+        activate: function () {
+            $log.debug("Path painter topology overlay ACTIVATED");
+        },
+        deactivate: function () {
+            pps.clear();
+            $log.debug("Path painter topology overlay DEACTIVATED");
+        },
         // These glyphs get installed using the overlayId as a prefix.
         // e.g. 'src' is installed as 'pp-overlay-src'
         // They can be referenced (from this overlay) as '*src'
@@ -123,13 +130,6 @@
             }
         },
 
-        activate: function () {
-            $log.debug("Path painter topology overlay ACTIVATED");
-        },
-        deactivate: function () {
-            $log.debug("Path painter topology overlay DEACTIVATED");
-        },
-
         // detail panel button definitions
         buttons: {
             src: {