Subnet creation issue fix 75/49975/1
authorKonsta Pozdeev <konsta.pozdeev@hpe.com>
Sun, 18 Dec 2016 15:46:23 +0000 (17:46 +0200)
committerDavid Goldberg <gdavid@hpe.com>
Tue, 3 Jan 2017 15:33:30 +0000 (17:33 +0200)
Change-Id: Iccba33b9196240bd42477c064f7e5b8288733afb
Signed-off-by: Konsta Pozdeev <konsta.pozdeev@hpe.com>
netvirt/src/main/java/org/opendaylight/unimgr/mef/netvirt/GwMacListener.java
netvirt/src/main/java/org/opendaylight/unimgr/mef/netvirt/IGwMacListener.java
netvirt/src/main/java/org/opendaylight/unimgr/mef/netvirt/SubnetListener.java

index f2c3d9b00fb37b2a483630c7a2e38c1709ff25a0..a85371c8caca486cece6418bfc4f92db943bc608 100644 (file)
@@ -8,6 +8,10 @@
 
 package org.opendaylight.unimgr.mef.netvirt;
 
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -85,7 +89,7 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
         }
     }
 
-    private void updateMac(VpnPortipToPort portIpToPort) {
+    private synchronized void updateMac(VpnPortipToPort portIpToPort) {
         String portName = portIpToPort.getPortName();
         String macAddress = portIpToPort.getMacAddress();
         String vpnName = portIpToPort.getVpnName();
@@ -101,40 +105,73 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
             Log.trace("Updating vpn {} port {} with IP {} MAC {}", vpnName, portName, ipAddress, macAddress);
         }
 
-        if (gwMacResolver.get(gwMacKey).getGwMac() == null || !gwMacResolver.get(gwMacKey).getGwMac().equals(macAddress)) {
-            Log.info("Creating GW for vpn {} port {} ip {} MAC {}", vpnName, portName, ipAddress, macAddress);
-            NetvirtVpnUtils.createUpdateVpnInterface(dataBroker, vpnName, portName, gwMacResolver.get(gwMacKey).getSubnet(), macAddress,
-                    false, gwMacResolver.get(gwMacKey).getPortIp(), null);
+        if (gwMacResolver.get(gwMacKey).getGwMac() == null
+                || !gwMacResolver.get(gwMacKey).getGwMac().equals(macAddress)) {
+            String portIp = gwMacResolver.get(gwMacKey).getPortIp();
+            for (String subnet : gwMacResolver.get(gwMacKey).getSubnets()) {
+                Log.info("Creating GW for vpn {} port {} ip {} subnet {} MAC {}", vpnName, portName, ipAddress, subnet,
+                        macAddress);
+
+                NetvirtVpnUtils.createUpdateVpnInterface(dataBroker, vpnName, portName, subnet, macAddress, false,
+                        portIp, null);
+            }
 
             gwMacResolver.get(gwMacKey).setGwMac(macAddress);
         }
     }
 
+    private void forceUpdateSubnet(GwMacKey gwMacKey, String subnet) {
+        if (!gwMacResolver.containsKey(gwMacKey)) {
+            return;
+        }
+        String portIp = gwMacResolver.get(gwMacKey).getPortIp();
+        String macAddress = gwMacResolver.get(gwMacKey).getGwMac();
+        if (macAddress == null) {
+            return;
+        }
+        Log.info("Creating GW for vpn {} port {} ip {} subnet {} MAC {}", gwMacKey.vpnId, gwMacKey.portId,
+                gwMacKey.gwIp, subnet, macAddress);
+        NetvirtVpnUtils.createUpdateVpnInterface(dataBroker, gwMacKey.vpnId, gwMacKey.portId, subnet, macAddress, false,
+                portIp, null);
+
+    }
+
     @Override
-    public void resolveGwMac(String vpnName, String portName, IpAddress srcIpAddress, IpAddress dstIpAddress, String subnet) {
+    public synchronized void resolveGwMac(String vpnName, String portName, IpAddress srcIpAddress,
+            IpAddress dstIpAddress, String subnet) {
         String dstIpAddressStr = NetvirtVpnUtils.ipAddressToString(dstIpAddress);
         GwMacKey gwMacKey = new GwMacKey(vpnName, portName, dstIpAddressStr);
 
         if (!gwMacResolver.containsKey(gwMacKey)) {
-            // check if IP was resolved already
-            gwMacResolver.putIfAbsent(gwMacKey, new GwMacValue(NetvirtVpnUtils.ipAddressToString(srcIpAddress), subnet));
-            VpnPortipToPort portIpToPort = NetvirtVpnUtils.getVpnPortFixedIp(dataBroker, vpnName, dstIpAddressStr);
-            if (portIpToPort != null) {
-                updateMac(portIpToPort);
-            }
-
+            gwMacResolver.putIfAbsent(gwMacKey,
+                    new GwMacValue(NetvirtVpnUtils.ipAddressToString(srcIpAddress), subnet));
             Log.info("Starting GW mac resolution for vpn {} port {} GW ip {}", vpnName, portName, dstIpAddress);
             NetvirtVpnUtils.sendArpRequest(arpUtilService, srcIpAddress, dstIpAddress, portName);
+        } else {
+            forceUpdateSubnet(gwMacKey, subnet);
+            gwMacResolver.get(gwMacKey).getSubnets().add(subnet);
         }
+
+        VpnPortipToPort portIpToPort = NetvirtVpnUtils.getVpnPortFixedIp(dataBroker, vpnName, dstIpAddressStr);
+        if (portIpToPort != null) {
+            updateMac(portIpToPort);
+        }
+
     }
 
     @Override
-    public void unResolveGwMac(String vpnName, String portName, IpAddress srcIpAddress, IpAddress dstIpAddress) {
+    public synchronized void unResolveGwMac(String vpnName, String portName, IpAddress srcIpAddress,
+            IpAddress dstIpAddress, String subnet) {
         String dstIpAddressStr = NetvirtVpnUtils.ipAddressToString(dstIpAddress);
         GwMacKey gwMacKey = new GwMacKey(vpnName, portName, dstIpAddressStr);
         if (gwMacResolver.containsKey(gwMacKey)) {
-            Log.info("Stopping GW mac resolution for vpn {} port {} GW ip {}", vpnName, portName, dstIpAddress);
-            gwMacResolver.remove(gwMacKey);
+            Log.info("Stopping GW mac resolution for vpn {} port {} GW ip {} Subnet {}", vpnName, portName,
+                    dstIpAddress, subnet);
+            gwMacResolver.get(gwMacKey).getSubnets().remove(subnet);
+
+            if (gwMacResolver.get(gwMacKey).getSubnets().isEmpty()) {
+                gwMacResolver.remove(gwMacKey);
+            }
         }
     }
 
@@ -163,13 +200,13 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
         Log.debug("Subnet GW Arp Retries");
     }
 
-    public static class GwMacKey {
-        private final String vpId;
+    private static class GwMacKey {
+        private final String vpnId;
         private final String portId;
         private final String gwIp;
 
         public GwMacKey(String vpId, String portId, String gwIp) {
-            this.vpId = vpId;
+            this.vpnId = vpId;
             this.portId = portId;
             this.gwIp = gwIp;
         }
@@ -178,21 +215,13 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
             return gwIp;
         }
 
-        public String getVpId() {
-            return vpId;
-        }
-
-        public String getPortId() {
-            return portId;
-        }
-
         @Override
         public int hashCode() {
             final int prime = 31;
             int result = 1;
             result = prime * result + (gwIp == null ? 0 : gwIp.hashCode());
             result = prime * result + (portId == null ? 0 : portId.hashCode());
-            result = prime * result + (vpId == null ? 0 : vpId.hashCode());
+            result = prime * result + (vpnId == null ? 0 : vpnId.hashCode());
             return result;
         }
 
@@ -222,27 +251,27 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
             } else if (!portId.equals(other.portId)) {
                 return false;
             }
-            if (vpId == null) {
-                if (other.vpId != null) {
+            if (vpnId == null) {
+                if (other.vpnId != null) {
                     return false;
                 }
-            } else if (!vpId.equals(other.vpId)) {
+            } else if (!vpnId.equals(other.vpnId)) {
                 return false;
             }
             return true;
         }
 
-
     }
 
-    public static class GwMacValue {
-        String portIp;
-        String subnet;
-        String gwMac;
+    private static class GwMacValue {
+        private String portIp;
+        private Set<String> subnets;
+        private String gwMac;
 
         public GwMacValue(String portIp, String subnet) {
             this.portIp = portIp;
-            this.subnet = subnet;
+            this.subnets = new HashSet<String>();
+            this.subnets.add(subnet);
         }
 
         public String getGwMac() {
@@ -257,8 +286,8 @@ public class GwMacListener extends UnimgrDataTreeChangeListener<VpnPortipToPort>
             return portIp;
         }
 
-        public String getSubnet() {
-            return subnet;
+        public Set<String> getSubnets() {
+            return subnets;
         }
 
     }
index 4a083f9b8d75feb8345df8bf0365e1d944b048ec..3e934372500cd540ea937b8d9ff686e911e792ea 100644 (file)
@@ -11,9 +11,6 @@ package org.opendaylight.unimgr.mef.netvirt;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
 
 public interface IGwMacListener {
-    // IpPrefix srcIpPrefix, IpAddress dstIpAddress, String interf
     public void resolveGwMac (String vpnName, String portName, IpAddress srcIpAddress, IpAddress dstIpAddress, String subnet);
-
-    public void unResolveGwMac (String vpnName, String portName, IpAddress srcIpAddress, IpAddress dstIpAddress);
-
+    public void unResolveGwMac(String vpnName, String portName, IpAddress srcIpAddress, IpAddress dstIpAddress, String subnet);
 }
index c563746a7521e1294a32b98d1ede600c50e5a9a6..147d572400fada995a048cead0d3c415a294390a 100644 (file)
@@ -283,7 +283,8 @@ public class SubnetListener extends UnimgrDataTreeChangeListener<Subnet> impleme
 
         String srcTpAddressStr = NetvirtVpnUtils.getIpAddressFromPrefix(NetvirtVpnUtils.ipPrefixToString(ipUni.getIpAddress()));
         IpAddress srcIpAddress = new IpAddress(srcTpAddressStr.toCharArray());
-        gwMacListener.unResolveGwMac(ipvcVpn.getVpnId(), vpnElan.getElanPort(), srcIpAddress, deletedSubnet.getGateway() );
+        String subnet = NetvirtVpnUtils.ipPrefixToString(deletedSubnet.getSubnet());
+        gwMacListener.unResolveGwMac(ipvcVpn.getVpnId(), vpnElan.getElanPort(), srcIpAddress, deletedSubnet.getGateway(), subnet);
 
         NetvirtVpnUtils.removeVpnInterfaceAdjacency(dataBroker, vpnElan.getElanPort(), deletedSubnet.getSubnet());
         NetvirtVpnUtils.removeVpnInterfaceAdjacency(dataBroker, vpnElan.getElanPort(), deletedSubnet.getGateway());