added scripts and DELETE functionality
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/FpcManager.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/FpcManager.java
index 9b4b915..27e6b8b 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/FpcManager.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/FpcManager.java
@@ -16,15 +16,12 @@
 
 package org.onosproject.fpcagent;
 
-import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.felix.scr.annotations.*;
 import org.onosproject.config.DynamicConfigService;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.fpcagent.helpers.ConfigHelper;
-import org.onosproject.fpcagent.helpers.DpnApi;
 import org.onosproject.fpcagent.workers.ZMQSBPublisherManager;
 import org.onosproject.fpcagent.workers.ZMQSBSubscriberManager;
 import org.onosproject.net.config.*;
@@ -38,17 +35,13 @@
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.configuredpn.DefaultConfigureDpnOutput;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.CreateOrUpdate;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.DeleteOrQuery;
-import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.payload.Contexts;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.result.ResultEnum;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.resultbody.resulttype.DefaultErr;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.resultbodydpn.resulttype.DefaultEmptyCase;
-import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.mobilityinfo.mobprofileparameters.ThreegppTunnel;
 import org.onosproject.yang.model.*;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.Optional;
 
 import static org.onosproject.fpcagent.FpcUtil.*;
@@ -89,8 +82,6 @@
 
     /* Variables */
     private FpcConfig fpcConfig;
-    private HashMap<String, ArrayList<Contexts>> sessionContextsMap = Maps.newHashMap();
-    private HashMap<String, String> nodeNetworkMap = Maps.newHashMap();
 
     /* Config */
     private ConfigFactory<ApplicationId, FpcConfig> fpcConfigConfigFactory =
@@ -212,66 +203,24 @@
                     case QUERY:
                         break;
                     case DELETE:
-                        if (input.opBody() instanceof DeleteOrQuery) {
-                            DeleteOrQuery deleteOrQuery = (DeleteOrQuery) input.opBody();
-                            // TODO: move to tenant service
-                            deleteOrQuery.targets().forEach(
-                                    target -> {
-                                        log.info("target {}", target);
-                                        String targetStr = target.target().union().string();
-                                        sessionContextsMap.getOrDefault(targetStr, Lists.newArrayList()).parallelStream().forEach(
-                                                context -> {
-                                                    log.info("context {}", context);
-                                                    context.dpns().forEach(
-                                                            dpn -> {
-                                                                log.info("DPN {}", dpn);
-                                                                Long teid;
-                                                                if (context.ul().mobilityTunnelParameters().mobprofileParameters() instanceof ThreegppTunnel) {
-                                                                    teid = ((ThreegppTunnel) context.ul().mobilityTunnelParameters().mobprofileParameters()).tunnelIdentifier();
-                                                                } else {
-                                                                    return;
-                                                                }
-
-                                                                Short dpnTopic = DpnApi.getTopicFromNode(nodeNetworkMap.get(dpn.dpnId().fpcIdentity().union().string()));
-
-                                                                if (targetStr.endsWith("ul") || targetStr.endsWith("dl")) {
-                                                                    log.info("DELETE Bearer");
-                                                                    DpnApi.delete_bearer(
-                                                                            dpnTopic,
-                                                                            teid
-                                                                    );
-                                                                } else {
-                                                                    log.info("DELETE session");
-                                                                    DpnApi.delete_session(
-                                                                            dpnTopic,
-                                                                            context.lbi().uint8(),
-                                                                            teid,
-                                                                            input.clientId().fpcIdentity().union().int64(),
-                                                                            input.opId().uint64(),
-                                                                            context.contextId().fpcIdentity().union().int64()
-                                                                    );
-                                                                }
-                                                            }
-                                                    );
-                                                }
-                                        );
-                                    }
-                            );
-                        }
+                        configureOutput = tenantService.configureDelete(
+                                (DeleteOrQuery) input.opBody(),
+                                input.clientId(),
+                                input.opId()
+                        );
                         break;
                 }
                 configureOutput.opId(input.opId());
             }
+            // TODO fix DELETE to update the NODE correctly.
+            tenantService.getTenants().ifPresent(tenants -> tenantService.updateNode(tenants));
         } catch (Exception e) {
             DefaultErr defaultErr = new DefaultErr();
             defaultErr.errorInfo(ExceptionUtils.getFullStackTrace(e));
             defaultErr.errorTypeId(ErrorTypeId.of(0));
             configureOutput.resultType(defaultErr);
             configureOutput.result(Result.of(ResultEnum.ERR));
-
             log.error(ExceptionUtils.getFullStackTrace(e));
-        } finally {
-            tenantService.getTenants().ifPresent(tenants -> tenantService.updateNode(tenants));
         }
 
         ResourceData dataNode = modelConverter.createDataNode(
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantManager.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantManager.java
index 6acf2f8..3c1fb23 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantManager.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantManager.java
@@ -1,7 +1,7 @@
 package org.onosproject.fpcagent;
 
-import com.google.common.collect.Iterables;
 import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.felix.scr.annotations.*;
 import org.onlab.packet.Ip4Address;
@@ -18,9 +18,11 @@
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.configure.DefaultConfigureOutput;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.instructions.instructions.instrtype.Instr3GppMob;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.CreateOrUpdate;
+import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.DeleteOrQuery;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.payload.Contexts;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.result.ResultEnum;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.resultbody.resulttype.DefaultCommonSuccess;
+import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.resultbody.resulttype.DefaultDeleteSuccess;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.resultbody.resulttype.DefaultErr;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.tenants.DefaultTenant;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.tenants.Tenant;
@@ -30,6 +32,7 @@
 import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.FpcIdentity;
 import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.fpccontext.Dpns;
 import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.mobilityinfo.mobprofileparameters.ThreegppTunnel;
+import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.targetsvalue.Targets;
 import org.onosproject.yang.gen.v1.ietfdmmthreegpp.rev20160803.ietfdmmthreegpp.threegppinstr.Bits;
 import org.onosproject.yang.model.*;
 import org.slf4j.Logger;
@@ -181,9 +184,8 @@
     ) {
         DefaultConfigureOutput configureOutput = new DefaultConfigureOutput();
         Collection<Callable<Object>> tasks = new ArrayList<>();
-
+        DefaultCommonSuccess defaultCommonSuccess = new DefaultCommonSuccess();
         try {
-            DefaultCommonSuccess defaultCommonSuccess = new DefaultCommonSuccess();
             for (Contexts context : create.contexts()) {
                 defaultCommonSuccess.addToContexts(context);
 
@@ -247,39 +249,57 @@
 
 
                                 if (commands.contains("session")) {
-                                    tasks.add(Executors.callable(() -> DpnApi.create_session(
-                                            dpnTopic,
-                                            imsi,
-                                            Ip4Prefix.valueOf(context.delegatingIpPrefixes().get(0).toString()).address(),
-                                            ebi,
-                                            ulLocalAddress,
-                                            s1u_sgw_gtpu_teid,
-                                            cId,
-                                            opId,
-                                            contextId
-                                    )));
+                                    tasks.add(Executors.callable(() -> {
+                                        DpnApi.create_session(
+                                                dpnTopic,
+                                                imsi,
+                                                Ip4Prefix.valueOf(context.delegatingIpPrefixes().get(0).toString()).address(),
+                                                ebi,
+                                                ulLocalAddress,
+                                                s1u_sgw_gtpu_teid,
+                                                cId,
+                                                opId,
+                                                contextId
+                                        );
+
+                                        getDefaultTenant().ifPresent(
+                                                tenant -> tenant.fpcMobility().addToContexts(convertContext(context))
+                                        );
+                                    }));
 
                                     if (commands.contains("downlink")) {
-                                        tasks.add(Executors.callable(() -> DpnApi.modify_bearer_dl(
-                                                dpnTopic,
-                                                s1u_sgw_gtpu_teid,
-                                                dlRemoteAddress,
-                                                s1u_enb_gtpu_teid,
-                                                cId,
-                                                opId
-                                        )));
+                                        tasks.add(Executors.callable(() -> {
+                                            DpnApi.modify_bearer_dl(
+                                                    dpnTopic,
+                                                    s1u_sgw_gtpu_teid,
+                                                    dlRemoteAddress,
+                                                    s1u_enb_gtpu_teid,
+                                                    cId,
+                                                    opId
+                                            );
+
+                                            getDefaultTenant().ifPresent(
+                                                    tenant -> tenant.fpcMobility().addToContexts(convertContext(context))
+                                            );
+                                        }));
                                     }
                                 } else if (commands.contains("indirect-forward")) {
                                     // TODO - Modify API for Indirect Forwarding to/from another SGW
                                 } else if (commands.contains("uplink")) {
-                                    tasks.add(Executors.callable(() -> DpnApi.create_bearer_ul(
-                                            dpnTopic,
-                                            imsi,
-                                            lbi,
-                                            ebi,
-                                            ulLocalAddress,
-                                            s1u_sgw_gtpu_teid
-                                    )));
+                                    tasks.add(Executors.callable(() -> {
+                                        DpnApi.create_bearer_ul(
+                                                dpnTopic,
+                                                imsi,
+                                                lbi,
+                                                ebi,
+                                                ulLocalAddress,
+                                                s1u_sgw_gtpu_teid
+                                        );
+
+                                        getDefaultTenant().ifPresent(
+                                                tenant -> tenant.fpcMobility().addToContexts(convertContext(context))
+                                        );
+                                    }));
                                 }
                             } else {
                                 throw new IllegalArgumentException("Could not find Topic ID");
@@ -304,12 +324,6 @@
 
             configureOutput.resultType(defaultCommonSuccess);
             configureOutput.result(Result.of(ResultEnum.OK));
-
-            create.contexts().forEach(
-                    contexts -> getDefaultTenant().ifPresent(
-                            tenant -> tenant.fpcMobility().addToContexts(convertContext(contexts))
-                    )
-            );
         } catch (Exception e) {
             log.error(ExceptionUtils.getFullStackTrace(e));
             DefaultErr defaultErr = new DefaultErr();
@@ -473,6 +487,126 @@
         return configureOutput;
     }
 
+    public DefaultConfigureOutput configureDelete(
+            DeleteOrQuery delete,
+            ClientIdentifier clientId,
+            OpIdentifier operationId
+    ) {
+        DefaultConfigureOutput configureOutput = new DefaultConfigureOutput();
+        Collection<Callable<Object>> tasks = new ArrayList<>();
+        DefaultDeleteSuccess defaultDeleteSuccess = new DefaultDeleteSuccess();
+        try {
+            for (Targets target : delete.targets()) {
+                defaultDeleteSuccess.addToTargets(target);
+                String targetStr = target.target().toString(),
+                        s = StringUtils.substringBetween(targetStr, "contexts=", "/"),
+                        trgt = s != null ? s : StringUtils.substringAfter(targetStr, "contexts=");
+
+                getDefaultTenant().map(
+                        tenant -> {
+                            if (tenant.fpcMobility().contexts() != null) {
+                                return tenant.fpcMobility()
+                                        .contexts()
+                                        .stream()
+                                        .filter(contexts -> contexts.contextId().toString().equals(trgt))
+                                        .findFirst()
+                                        .orElseThrow(
+                                                () -> new IllegalArgumentException("Context doesn't exist.")
+                                        );
+                            }
+                            throw new IllegalArgumentException("Contexts are empty.");
+                        }
+                ).ifPresent(
+                        context -> context.dpns().forEach(
+                                dpn -> {
+                                    Optional<String> key = getDefaultTenant().flatMap(tenant ->
+                                            tenant.fpcTopology()
+                                                    .dpns()
+                                                    .stream()
+                                                    .filter(dpns -> dpns.dpnId().equals(dpn.dpnId()))
+                                                    .findFirst().map(node -> node.nodeId() + "/" + node.networkId())
+                                    );
+
+                                    if (key.isPresent()) {
+                                        Short dpnTopic = DpnApi.getTopicFromNode(key.get());
+
+                                        if (dpnTopic != null) {
+                                            Long teid;
+                                            if (context.ul().mobilityTunnelParameters().mobprofileParameters() instanceof ThreegppTunnel) {
+                                                teid = ((ThreegppTunnel) context.ul().mobilityTunnelParameters().mobprofileParameters()).tunnelIdentifier();
+                                            } else {
+                                                throw new IllegalArgumentException("mobprofileParameters are not instance of ThreegppTunnel");
+                                            }
+
+                                            long cId = clientId.fpcIdentity().union().int64();
+                                            BigInteger opId = operationId.uint64();
+
+                                            if (targetStr.endsWith("ul") || targetStr.endsWith("dl")) {
+                                                tasks.add(Executors.callable(() -> {
+                                                    DpnApi.delete_bearer(
+                                                            dpnTopic,
+                                                            teid
+                                                    );
+
+                                                    context.dl(null);
+                                                    context.ul(null);
+                                                }));
+                                            } else {
+                                                tasks.add(Executors.callable(() -> {
+                                                    DpnApi.delete_session(
+                                                            dpnTopic,
+                                                            context.lbi().uint8(),
+                                                            teid,
+                                                            cId,
+                                                            opId,
+                                                            context.contextId().fpcIdentity().union().int64()
+                                                    );
+
+                                                    getDefaultTenant().ifPresent(
+                                                            tenant -> tenant.fpcMobility()
+                                                                    .contexts()
+                                                                    .remove(context)
+                                                    );
+                                                }));
+                                            }
+
+                                        } else {
+                                            throw new IllegalArgumentException("Could not find Topic ID");
+                                        }
+                                    } else {
+                                        throw new IllegalArgumentException("DPN does not have node and network ID defined.");
+                                    }
+                                }
+                        )
+                );
+
+            }
+
+            ExecutorService executor = Executors.newWorkStealingPool();
+            executor.invokeAll(tasks).forEach(
+                    future -> {
+                        try {
+                            future.get();
+                        } catch (Exception e) {
+                            throw new IllegalStateException(e);
+                        }
+                    }
+            );
+
+            configureOutput.resultType(defaultDeleteSuccess);
+            configureOutput.result(Result.of(ResultEnum.OK));
+
+        } catch (Exception e) {
+            log.error(ExceptionUtils.getFullStackTrace(e));
+            DefaultErr defaultErr = new DefaultErr();
+            configureOutput.resultType(defaultErr);
+            defaultErr.errorInfo(ExceptionUtils.getFullStackTrace(e));
+            defaultErr.errorTypeId(ErrorTypeId.of(0));
+        }
+
+        return configureOutput;
+    }
+
     /**
      * Accumulates events to allow processing after a desired number of
      * events were accumulated.
@@ -488,25 +622,27 @@
 
         @Override
         public void processItems(List<DynamicConfigEvent> events) {
-            DynamicConfigEvent event = Iterables.getLast(events);
-            log.debug("processItems {}", event);
-            checkNotNull(event, EVENT_NULL);
-            switch (event.type()) {
-                case NODE_ADDED:
-                case NODE_DELETED:
-                    Filter filter = Filter.builder().build();
-                    DataNode node = dynamicConfigService.readNode(tenantsResourceId, filter);
-                    getModelObjects(node, null).forEach(
-                            modelObject -> fpcAgentData.tenants((DefaultTenants) modelObject)
-                    );
-                    break;
-                case NODE_UPDATED:
-                case NODE_REPLACED:
-                    break;
-                default:
-                    log.warn(UNKNOWN_EVENT, event.type());
-                    break;
-            }
+            events.forEach(
+                    event -> {
+                        checkNotNull(event, EVENT_NULL);
+                        switch (event.type()) {
+                            case NODE_ADDED:
+                            case NODE_DELETED:
+                                Filter filter = Filter.builder().build();
+                                DataNode node = dynamicConfigService.readNode(tenantsResourceId, filter);
+                                getModelObjects(node, null).forEach(
+                                        modelObject -> fpcAgentData.tenants((DefaultTenants) modelObject)
+                                );
+                                break;
+                            case NODE_UPDATED:
+                            case NODE_REPLACED:
+                                break;
+                            default:
+                                log.warn(UNKNOWN_EVENT, event.type());
+                                break;
+                        }
+                    }
+            );
         }
 
     }
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantService.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantService.java
index 60118ff..4b88510 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantService.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/TenantService.java
@@ -6,6 +6,7 @@
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.OpIdentifier;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.configure.DefaultConfigureOutput;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.CreateOrUpdate;
+import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.opinput.opbody.DeleteOrQuery;
 import org.onosproject.yang.gen.v1.ietfdmmfpcagent.rev20160803.ietfdmmfpcagent.tenants.Tenant;
 import org.onosproject.yang.gen.v1.ietfdmmfpcbase.rev20160803.ietfdmmfpcbase.FpcIdentity;
 import org.onosproject.yang.model.DataNode;
@@ -47,4 +48,10 @@
             ClientIdentifier clientId,
             OpIdentifier operationId
     );
+
+    DefaultConfigureOutput configureDelete(
+            DeleteOrQuery delete,
+            ClientIdentifier clientId,
+            OpIdentifier operationId
+    );
 }
diff --git a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/workers/ZMQSBSubscriberManager.java b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/workers/ZMQSBSubscriberManager.java
index 3b3e491..d2fbba3 100644
--- a/apps/fpcagent/src/main/java/org/onosproject/fpcagent/workers/ZMQSBSubscriberManager.java
+++ b/apps/fpcagent/src/main/java/org/onosproject/fpcagent/workers/ZMQSBSubscriberManager.java
@@ -290,8 +290,8 @@
         public void run() {
             try {
                 this.broadcastTopic();
-                log.info("Thread sleeping: " + Thread.currentThread().getName());
-                Thread.sleep(10000);
+                log.debug("Thread sleeping: " + Thread.currentThread().getName());
+                Thread.sleep(2000);
             } catch (InterruptedException e) {
                 if (conflictingTopic) {
                     conflictingTopic = false;
diff --git a/scripts/deleteContext.sh b/scripts/deleteContext.sh
new file mode 100755
index 0000000..0e9eda8
--- /dev/null
+++ b/scripts/deleteContext.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+curl -X DELETE -u onos:rocks 'http://localhost:8181/onos/restconf/data/ietf-dmm-fpcagent:tenants/tenant=default/fpc-mobility/contexts=202374887'
+./getTenants.sh
\ No newline at end of file
diff --git a/scripts/deleteContextDl.sh b/scripts/deleteContextDl.sh
new file mode 100755
index 0000000..64f6c2b
--- /dev/null
+++ b/scripts/deleteContextDl.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+curl -X DELETE -u onos:rocks 'http://localhost:8181/onos/restconf/data/ietf-dmm-fpcagent:tenants/tenant=default/fpc-mobility/contexts=202374887/dl'
+./getTenants.sh
\ No newline at end of file
diff --git a/scripts/deleteDPN.sh b/scripts/deleteDPN.sh
index 08b0dd4..f210b60 100755
--- a/scripts/deleteDPN.sh
+++ b/scripts/deleteDPN.sh
@@ -1,2 +1,3 @@
 #!/bin/bash
 curl -X DELETE -u onos:rocks 'http://localhost:8181/onos/restconf/data/ietf-dmm-fpcagent:tenants/tenant=default/fpc-topology/dpns=1'
+./getTenants.sh
diff --git a/scripts/deleteRPC.sh b/scripts/deleteRPC.sh
index d2f49ce..772b61b 100755
--- a/scripts/deleteRPC.sh
+++ b/scripts/deleteRPC.sh
@@ -4,7 +4,7 @@
         "op-id": "3",
         "targets": [
             {
-                "target": "/ietf-dmm-fpcagent:tenants/tenant/default/fpc-mobility/contexts/202374885"
+                "target": "/ietf-dmm-fpcagent:tenants/tenant=default/fpc-mobility/contexts=202374887"
             }
         ],
         "client-id": "1",
@@ -13,4 +13,4 @@
         "op-type": "delete",
         "op-ref-scope": "none"
     }
-}' 'http://localhost:8181/onos/restconf/operations/ietf-dmm-fpcagent:configure'
+}' 'http://localhost:8181/onos/restconf/operations/ietf-dmm-fpcagent:configure' | python -m json.tool