Merge "Natservice module bug fixes"
[vpnservice.git] / natservice / natservice-impl / src / main / java / org / opendaylight / vpnservice / natservice / internal / DpnInVpnListener.java
index d033b80c95c0b6766fff02133a208d96cb22aacd..12eece89c5249db7c1d38dd7db7e294d3bb0a3c7 100644 (file)
@@ -62,6 +62,7 @@ public class DpnInVpnListener implements OdlL3vpnListener {
     }
 
     public void onAddDpnEvent(AddDpnEvent notification) {
+/*
         AddEventData eventData =  notification.getAddEventData();
         BigInteger dpnId = eventData.getDpnId();
         String vpnName = eventData.getVpnName();
@@ -92,12 +93,14 @@ public class DpnInVpnListener implements OdlL3vpnListener {
                 }
             }
         }
+*/
     }
 
     void handleSNATForDPN(BigInteger dpnId, String routerName) {
         //Check if primary and secondary switch are selected, If not select the role
         //Install select group to NAPT switch
         //Install default miss entry to NAPT switch
+/*
         BigInteger naptSwitch;
         try {
             Long routerId = NatUtil.getVpnId(dataBroker, routerName);
@@ -106,7 +109,7 @@ public class DpnInVpnListener implements OdlL3vpnListener {
                 return;
             }
             BigInteger naptId = NatUtil.getPrimaryNaptfromRouterId(dataBroker, routerId);
-            if (naptId == null) {
+            if (naptId == null || naptId.equals(BigInteger.ZERO)) {
                 LOG.debug("No Naptswitch is selected for router {}", routerName);
 
                 naptSwitch = dpnId;
@@ -115,40 +118,44 @@ public class DpnInVpnListener implements OdlL3vpnListener {
                     LOG.error("Failed to update newNaptSwitch {} for routername {}",naptSwitch,routerName);
                     return;
                 }
-                LOG.debug("Switch {} is elected as NaptSwitch for router {}", dpnId, routerName);
+                LOG.debug("Switch {} is elected as NaptSwitch for router {}",dpnId,routerName);
 
                 //installing group
                 List<BucketInfo> bucketInfo = naptSwitchHA.handleGroupInPrimarySwitch();
                 naptSwitchHA.installSnatGroupEntry(naptSwitch,bucketInfo,routerName);
 
+                naptSwitchHA.installSnatFlows(routerName,routerId,naptSwitch);
+
             }  else {
-                LOG.debug("Napt switch is already elected for router {}"
-                        , naptId, routerName);
+                LOG.debug("Napt switch with Id {} is already elected for router {}",naptId, routerName);
                 naptSwitch = naptId;
 
                 //installing group
                 List<BucketInfo> bucketInfo = naptSwitchHA.handleGroupInNeighborSwitches(dpnId, routerName, naptSwitch);
                 if (bucketInfo == null) {
+                    LOG.debug("Failed to populate bucketInfo for dpnId {} routername {} naptSwitch {}",dpnId,routerName,
+                            naptSwitch);
                     return;
                 }
                 naptSwitchHA.installSnatGroupEntry(dpnId, bucketInfo, routerName);
-
             }
-            // Install miss entry pointing to group
+            // Install miss entry (table 26) pointing to group
             long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
             FlowEntity flowEntity = naptSwitchHA.buildSnatFlowEntity(dpnId, routerName, groupId,NatConstants.ADD_FLOW);
             if (flowEntity == null) {
-                LOG.debug("Failed to populate flowentity for router {} with dpnId {} groupIs {}",routerName,dpnId,groupId);
+                LOG.debug("Failed to populate flowentity for router {} with dpnId {} groupId {}",routerName,dpnId,groupId);
                 return;
             }
-            LOG.debug("Sucessfully installed flow for dpnId {} router {} group {}",dpnId,routerName,groupId);
+            LOG.debug("Successfully installed flow for dpnId {} router {} group {}",dpnId,routerName,groupId);
             mdsalManager.installFlow(flowEntity);
         } catch (Exception ex) {
             LOG.error("Exception in handleSNATForDPN method : {}",ex);
         }
+*/
     }
 
     public void onRemoveDpnEvent(RemoveDpnEvent notification) {
+/*
         RemoveEventData eventData = notification.getRemoveEventData();
         BigInteger dpnId = eventData.getDpnId();
         String vpnName = eventData.getVpnName();
@@ -179,11 +186,36 @@ public class DpnInVpnListener implements OdlL3vpnListener {
                 }
             }
         }
+*/
     }
 
-    void removeSNATFromDPN(BigInteger dpnId, String routerName) {
+    /*void removeSNATFromDPN(BigInteger dpnId, String routerName) {
         //irrespective of naptswitch or non-naptswitch, SNAT default miss entry need to be removed
         //remove miss entry to NAPT switch
+        //if naptswitch elect new switch and install Snat flows and remove those flows in oldnaptswitch
+
+        Long routerId = NatUtil.getVpnId(dataBroker, routerName);
+        if (routerId == NatConstants.INVALID_ID) {
+            LOG.error("Invalid routerId returned for routerName {}",routerName);
+            return;
+        }
+        BigInteger naptSwitch = NatUtil.getPrimaryNaptfromRouterId(dataBroker, routerId);
+        if (naptSwitch == null || naptSwitch.equals(BigInteger.ZERO)) {
+            LOG.debug("No naptSwitch is selected for router {}", routerName);
+            return;
+        }
+        try {
+            boolean naptStatus = naptSwitchHA.isNaptSwitchDown(routerName,dpnId,naptSwitch);
+            if (!naptStatus) {
+                LOG.debug("NaptSwitchDown: Switch with DpnId {} is not naptSwitch for router {}",
+                        dpnId, routerName);
+            } else {
+                naptSwitchHA.removeSnatFlowsInOldNaptSwitch(routerName,naptSwitch);
+            }
+        } catch (Exception ex) {
+            LOG.debug("Exception while handling naptSwitch down for router {} : {}",routerName,ex);
+        }
+
         long groupId = NatUtil.createGroupId(NatUtil.getGroupIdKey(routerName), idManager);
         FlowEntity flowEntity = null;
         try {
@@ -202,15 +234,16 @@ public class DpnInVpnListener implements OdlL3vpnListener {
         LOG.debug("NAT Service : Removed default SNAT miss entry flow for dpnID {} with routername {}", dpnId, routerName);
 
         //remove group
+        GroupEntity groupEntity = null;
         try {
-            GroupEntity groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName,
+            groupEntity = MDSALUtil.buildGroupEntity(dpnId, groupId, routerName,
                     GroupTypes.GroupAll, null);
             LOG.info("NAT Service : Removing NAPT GroupEntity:{}", groupEntity);
             mdsalManager.removeGroup(groupEntity);
         } catch (Exception ex) {
-            LOG.debug("NAT Service : Failed to remove group entity {} : {}",flowEntity,ex);
+            LOG.debug("NAT Service : Failed to remove group entity {} : {}",groupEntity,ex);
             return;
         }
-        LOG.debug("NAT Service : Removed default SNAT miss entry flow for dpnID {} with routername {}", dpnId, routerName);
-    }
+        LOG.debug("NAT Service : Removed default SNAT miss entry flow for dpnID {} with routerName {}", dpnId, routerName);
+    }*/
 }
\ No newline at end of file