Fixed not deleting old rules from maps on delete session
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 f795769..11c2c06 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
@@ -94,6 +94,8 @@
             Maps.newConcurrentMap();
     private static final ConcurrentMap<Long, Set<FlowRule>> SESS_ID_TO_FLOWS =
             Maps.newConcurrentMap();
+    private static final ConcurrentMap<Long, Set<Route>> SESS_ID_TO_ROUTES =
+            Maps.newConcurrentMap();
 
     // FIXME: should use a cache with timeout
     private static final Map<Long, Lock> SESS_LOCKS = Maps.newConcurrentMap();
@@ -157,8 +159,8 @@
         log.info("createSession(topic_id={}, imsi={}, default_ebi={}, " +
                          "ue_ipv4={}, s1u_sgw_teid={}, s1u_sgw_ipv4={}, " +
                          "session_id={}, client_id={}, op_id={})",
-                topicId, imsi, defaultEbi, ueIpv4, s1USgwTeid,
-                s1USgwIpv4, sessionId, clientId, opId);
+                 topicId, imsi, defaultEbi, ueIpv4, s1USgwTeid,
+                 s1USgwIpv4, sessionId, clientId, opId);
 
         if (isNotInit()) {
             return;
@@ -169,8 +171,8 @@
         try {
             if (SESS_ID_TO_FLOWS.containsKey(sessionId)
                     && !SESS_ID_TO_FLOWS.get(sessionId).isEmpty()) {
-                log.warn("Creating session {}, but {} rules already exists for such session.",
-                        sessionId, SESS_ID_TO_FLOWS.get(sessionId).size());
+                log.error("Creating session {}, but {} rules already exists for such session.",
+                          sessionId, SESS_ID_TO_FLOWS.get(sessionId).size());
                 SESS_ID_TO_FLOWS.get(sessionId).forEach(f -> log.debug("{}", f));
             }
 
@@ -199,8 +201,8 @@
         log.info("modifyBearer(topic_id={}, s1u_sgw_ipv4={}, " +
                          "s1u_enodeb_teid={}, s1u_enodeb_ipv4={}, " +
                          "session_id={}, client_id={}, op_id={})",
-                topicId, s1USgwIpv4, s1UEnodebTeid, s1UEnodebIpv4,
-                sessionId, clientId, opId);
+                 topicId, s1USgwIpv4, s1UEnodebTeid, s1UEnodebIpv4,
+                 sessionId, clientId, opId);
 
         if (isNotInit()) {
             return;
@@ -211,19 +213,27 @@
         try {
             if (!SESS_ID_TO_UE_ADDR.containsKey(sessionId)) {
                 log.error("Missing sess ID in SESS_ID_TO_UE_ADDR map: {}",
-                        sessionId);
+                          sessionId);
                 return;
             }
             final Ip4Address ueAddr = SESS_ID_TO_UE_ADDR.get(sessionId);
 
+            if (SESS_ID_TO_ROUTES.containsKey(sessionId)
+                    && !SESS_ID_TO_ROUTES.get(sessionId).isEmpty()) {
+                log.error("Modifying session {}, but {} DL routes already exists for such session.",
+                          sessionId, SESS_ID_TO_ROUTES.get(sessionId).size());
+                SESS_ID_TO_ROUTES.get(sessionId).forEach(r -> log.debug("{}", r));
+            }
+            SESS_ID_TO_ROUTES.putIfAbsent(sessionId, Sets.newHashSet());
+
             SESS_ID_TO_FLOWS.get(sessionId).add(s1uFilterRule(s1USgwIpv4));
             SESS_ID_TO_FLOWS.get(sessionId)
                     .add(dlSessLookupRule(ueAddr, s1UEnodebTeid,
-                            s1UEnodebIpv4, s1USgwIpv4));
+                                          s1UEnodebIpv4, s1USgwIpv4));
 
             // Segment routing configuration
-            Route route = new Route(Route.Source.STATIC, ueAddr.toIpPrefix(), s1UEnodebIpv4);
-            routeStore.updateRoute(route);
+            SESS_ID_TO_ROUTES.get(sessionId).add(
+                    new Route(Route.Source.STATIC, ueAddr.toIpPrefix(), s1UEnodebIpv4));
 
 //            List<InterfaceIpAddress> ifaceIpAddrList = Lists.newArrayList();
 //            ifaceIpAddrList.add(InterfaceIpAddress.valueOf(s1USgwIpv4.toIpPrefix().toString()));
@@ -251,7 +261,7 @@
 
         log.info("deleteSession(topic_id={}, session_id={}, client_id={}, " +
                          "op_id={})",
-                topicId, sessionId, clientId, opId);
+                 topicId, sessionId, clientId, opId);
 
         if (isNotInit()) {
             return;
@@ -260,20 +270,17 @@
         SESS_LOCKS.putIfAbsent(sessionId, new ReentrantLock());
         SESS_LOCKS.get(sessionId).lock();
         try {
+            removeSessionRules(sessionId);
+
             SESS_ID_TO_UE_ADDR.remove(sessionId);
+            SESS_ID_TO_FLOWS.remove(sessionId);
+            SESS_ID_TO_ROUTES.remove(sessionId);
 
-            if (!SESS_ID_TO_FLOWS.containsKey(sessionId)
-                    || SESS_ID_TO_FLOWS.get(sessionId).isEmpty()) {
-                log.warn("Deleting session {}, but no rules exist for this session",
-                        sessionId);
-            } else {
-                removeSessionRules(sessionId);
+            sendNotification(
+                    clientId.toString(),
+                    createConfigResultNotification(OpIdentifier.of(opId), (short) 16)
+            );
 
-                sendNotification(
-                        clientId.toString(),
-                        createConfigResultNotification(OpIdentifier.of(opId), (short) 16)
-                );
-            }
         } finally {
             SESS_LOCKS.get(sessionId).unlock();
         }
@@ -287,7 +294,7 @@
         log.info("sendAdcRules(topic={}, domain_name={}, ip={}, drop={}, " +
                          "rating_group={}, service_ID={}, sponsor_ID={})",
                  topic, domainName, ip, drop, ratingGroup, serviceId,
-                sponsorId);
+                 sponsorId);
 
         if (isNotInit()) {
             return;
@@ -375,12 +382,18 @@
         if (SESS_ID_TO_FLOWS.containsKey(sessionId)) {
             batchApply(SESS_ID_TO_FLOWS.get(sessionId));
         }
+        if (SESS_ID_TO_ROUTES.containsKey(sessionId)) {
+            SESS_ID_TO_ROUTES.get(sessionId).forEach(routeStore::updateRoute);
+        }
     }
 
     private void removeSessionRules(long sessionId) {
         if (SESS_ID_TO_FLOWS.containsKey(sessionId)) {
             batchRemove(SESS_ID_TO_FLOWS.get(sessionId));
         }
+        if (SESS_ID_TO_ROUTES.containsKey(sessionId)) {
+            SESS_ID_TO_ROUTES.get(sessionId).forEach(routeStore::removeRoute);
+        }
     }
 
     private void batchApply(Collection<FlowRule> rules) {