Added deactivate method to fpcagent P4 communicator
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/protocols/DpnP4Communicator.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/protocols/DpnP4Communicator.java
index b2e6062..15f3eb3 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/protocols/DpnP4Communicator.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/protocols/DpnP4Communicator.java
@@ -51,6 +51,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
@@ -100,14 +101,16 @@
     // FIXME: should use a cache with timeout
     private static final Map<Long, Lock> SESS_LOCKS = Maps.newConcurrentMap();
 
+    private static final AtomicBoolean DEACTIVATED = new AtomicBoolean(true);
+
     private static DpnP4Communicator _instance;
 
     private final ApplicationId appId;
     private DeviceId deviceId;
     private ClientIdentifier clientId;
-    private final FlowRuleService flowRuleService;
-    private final RouteStore routeStore;
-    private final InterfaceAdminService interfaceService;
+    private FlowRuleService flowRuleService;
+    private RouteStore routeStore;
+    private InterfaceAdminService interfaceService;
 
     private DpnP4Communicator(ApplicationId appId,
                               FlowRuleService flowRuleService,
@@ -123,10 +126,13 @@
     public static DpnP4Communicator createInstance(
             ApplicationId appId, FlowRuleService flowRuleService,
             RouteStore routeStore, InterfaceAdminService interfaceService) {
-        if (_instance == null) {
-            _instance = new DpnP4Communicator(appId, flowRuleService, routeStore, interfaceService);
+        synchronized (DEACTIVATED) {
+            if (_instance == null) {
+                _instance = new DpnP4Communicator(appId, flowRuleService, routeStore, interfaceService);
+            }
+            DEACTIVATED.set(false);
+            return _instance;
         }
-        return _instance;
     }
 
     public static DpnP4Communicator getInstance() {
@@ -141,12 +147,25 @@
         this.clientId = clientId;
     }
 
+    public void deactivate() {
+        synchronized (DEACTIVATED) {
+            flowRuleService.removeFlowRulesById(appId);
+            DEACTIVATED.set(true);
+        }
+    }
+
     private boolean isNotInit() {
-        if (deviceId == null) {
-            log.error("Not initialized. Cannot perform operations.");
-            return true;
-        } else {
-            return false;
+        synchronized (DEACTIVATED) {
+            if (DEACTIVATED.get()) {
+                log.error("DEACTIVATED. Cannot perform operations.");
+                return false;
+            }
+            if (deviceId == null) {
+                log.error("Not initialized. Cannot perform operations.");
+                return true;
+            } else {
+                return false;
+            }
         }
     }
 
@@ -369,24 +388,36 @@
     }
 
     private void applySession(long sessionId) {
-        if (FLOWS.containsKey(sessionId)) {
-            batchApplyFlowRules(FLOWS.get(sessionId));
-        }
-        if (ROUTES.containsKey(sessionId)) {
-            ROUTES.get(sessionId).forEach(routeStore::updateRoute);
+        synchronized (DEACTIVATED) {
+            if (DEACTIVATED.get()) {
+                log.warn("DEACTIVATED. Cannot perform applySession()");
+                return;
+            }
+            if (FLOWS.containsKey(sessionId)) {
+                batchApplyFlowRules(FLOWS.get(sessionId));
+            }
+            if (ROUTES.containsKey(sessionId)) {
+                ROUTES.get(sessionId).forEach(routeStore::updateRoute);
+            }
         }
     }
 
     private void cleanUpSession(long sessionId) {
-        if (FLOWS.containsKey(sessionId)) {
-            batchRemoveFlowRules(FLOWS.get(sessionId));
-            FLOWS.get(sessionId).clear();
+        synchronized (DEACTIVATED) {
+            if (DEACTIVATED.get()) {
+                log.warn("DEACTIVATED. Cannot perform cleanUpSession()");
+                return;
+            }
+            if (FLOWS.containsKey(sessionId)) {
+                batchRemoveFlowRules(FLOWS.get(sessionId));
+                FLOWS.get(sessionId).clear();
+            }
+            if (ROUTES.containsKey(sessionId)) {
+                ROUTES.get(sessionId).forEach(routeStore::removeRoute);
+                ROUTES.get(sessionId).clear();
+            }
+            UE_ADDRESSES.remove(sessionId);
         }
-        if (ROUTES.containsKey(sessionId)) {
-            ROUTES.get(sessionId).forEach(routeStore::removeRoute);
-            ROUTES.get(sessionId).clear();
-        }
-        UE_ADDRESSES.remove(sessionId);
     }
 
     private void logSessionState(long sessionId) {