when deactivate sdn-ip, also delete all relative intents

Change-Id: I7a6bd64b5a9525dc49ca2b9353fcc45dc9a16288
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
index f2d5b5e..a3cd6875 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
@@ -15,6 +15,17 @@
  */
 package org.onosproject.sdnip;
 
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.IntentService;
@@ -24,16 +35,6 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutorService;
-
-import static java.util.concurrent.Executors.newSingleThreadExecutor;
-import static org.onlab.util.Tools.groupedThreads;
-
 /**
  * Synchronizes intents between the in-memory intent store and the
  * IntentService.
@@ -97,57 +98,32 @@
         synchronized (this) {
             // Stop the thread(s)
             intentsSynchronizerExecutor.shutdownNow();
+            log.info("Intents Synchronizer Executor shutdown completed");
 
-            //
-            // Withdraw all app related intents
-            //
-            if (!isElectedLeader) {
-                return;         // Nothing to do: not the leader anymore
-            }
-
-            //
-            // NOTE: We don't withdraw the intents during shutdown, because
-            // it creates flux in the data plane during switchover.
-            //
-
-            /*
-            //
-            // Build a batch operation to withdraw all intents from this
-            // application.
-            //
-            log.debug("Intent Synchronizer shutdown: " +
-                      "withdrawing all intents...");
-            IntentOperations.Builder builder = IntentOperations.builder(appId);
-            for (Intent intent : intentService.getIntents()) {
-                // Skip the intents from other applications
-                if (!intent.appId().equals(appId)) {
-                    continue;
-                }
-
-                // Skip the intents that are already withdrawn
-                IntentState intentState =
-                    intentService.getIntentState(intent.id());
-                if ((intentState == null) ||
-                    intentState.equals(IntentState.WITHDRAWING) ||
-                    intentState.equals(IntentState.WITHDRAWN)) {
-                    continue;
-                }
-
-                log.trace("Intent Synchronizer withdrawing intent: {}",
-                          intent);
-                builder.addWithdrawOperation(intent.id());
-            }
-            IntentOperations intentOperations = builder.build();
-            intentService.execute(intentOperations);
-            leaderChanged(false);
-
-            peerIntents.clear();
-            routeIntents.clear();
-            log.debug("Intent Synchronizer shutdown completed");
-            */
         }
     }
 
+    /**
+     * Withdraws all intents.
+     */
+    public void removeIntents() {
+        if (!isElectedLeader) {
+            // only leader will withdraw intents
+            return;
+        }
+
+        log.debug("Intent Synchronizer shutdown: withdrawing all intents...");
+
+        for (Entry<Key, Intent> entry : intents.entrySet()) {
+            intentService.withdraw(entry.getValue());
+            log.debug("Intent Synchronizer withdrawing intent: {}",
+                      entry.getValue());
+        }
+
+        intents.clear();
+        log.info("Tried to clean all intents");
+    }
+
     @Override
     public void submit(Intent intent) {
         synchronized (this) {
@@ -261,5 +237,4 @@
         }
         log.debug("Intent synchronization completed");
     }
-
 }
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
index 1b3eda9..ace888d 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
@@ -15,12 +15,17 @@
  */
 package org.onosproject.sdnip;
 
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.Objects;
+
 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.app.ApplicationService;
 import org.onosproject.cluster.ClusterService;
 import org.onosproject.cluster.ControllerNode;
 import org.onosproject.cluster.LeadershipEvent;
@@ -28,8 +33,8 @@
 import org.onosproject.cluster.LeadershipService;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
-import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.net.config.NetworkConfigService;
 import org.onosproject.net.host.HostService;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.routing.IntentSynchronizationService;
@@ -38,10 +43,6 @@
 import org.onosproject.routing.config.RoutingConfigurationService;
 import org.slf4j.Logger;
 
-import java.util.Objects;
-
-import static org.slf4j.LoggerFactory.getLogger;
-
 /**
  * Component for the SDN-IP peering application.
  */
@@ -59,6 +60,9 @@
     protected IntentService intentService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected ApplicationService applicationService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected HostService hostService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
@@ -84,7 +88,7 @@
     private SdnIpFib fib;
 
     private LeadershipEventListener leadershipEventListener =
-        new InnerLeadershipEventListener();
+            new InnerLeadershipEventListener();
     private ApplicationId appId;
     private ControllerNode localControllerNode;
 
@@ -113,6 +117,10 @@
 
         leadershipService.addListener(leadershipEventListener);
         leadershipService.runForLeadership(appId.name());
+
+        applicationService.registerDeactivateHook(appId,
+                intentSynchronizer::removeIntents);
+
     }
 
     @Deactivate