Merge "Bug-4957: Make async operational DS Read"
authormichal rehak <mirehak@cisco.com>
Wed, 2 Mar 2016 16:27:38 +0000 (16:27 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 2 Mar 2016 16:27:38 +0000 (16:27 +0000)
applications/statistics-manager/src/main/java/org/opendaylight/openflowplugin/applications/statistics/manager/impl/helper/MatchComparatorHelper.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java

index 4688d6bd91886950c0a1e1dca0334628d28cabb8..c6e97f1120b9a9d3e64abb240cdeb4050be37464 100644 (file)
@@ -12,11 +12,13 @@ import java.net.Inet4Address;
 
 import com.google.common.annotations.VisibleForTesting;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.MacAddressFilter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
 
 /**
  * @author joe
@@ -106,7 +108,16 @@ public class MatchComparatorHelper {
                 verdict = MatchComparatorHelper.compareIpv4PrefixNullSafe(statsIpv4Match.getIpv4Source(),
                         storedIpv4Match.getIpv4Source());
             }
-        } else {
+        } else if(statsLayer3Match instanceof Ipv6Match && storedLayer3Match instanceof Ipv6Match) {
+            final Ipv6Match statsIpv6Match = (Ipv6Match) statsLayer3Match;
+            final Ipv6Match storedIpv6Match = (Ipv6Match) storedLayer3Match;
+            verdict = MatchComparatorHelper.compareIpv6PrefixNullSafe(storedIpv6Match.getIpv6Destination(),
+                    statsIpv6Match.getIpv6Destination());
+            if (verdict) {
+                verdict = MatchComparatorHelper.compareIpv6PrefixNullSafe(statsIpv6Match.getIpv6Source(),
+                        storedIpv6Match.getIpv6Source());
+            }
+        }else {
             final Boolean nullCheckOut = checkNullValues(storedLayer3Match, statsLayer3Match);
             if (nullCheckOut != null) {
                 verdict = nullCheckOut;
@@ -118,6 +129,7 @@ public class MatchComparatorHelper {
         return verdict;
     }
 
+
     /**
      * TODO: why don't we use the default Ipv4Prefix.equals()?
      *
@@ -148,6 +160,20 @@ public class MatchComparatorHelper {
         return (statsIpAddressInt.getIp() == storedIpAddressInt.getIp());
     }
 
+
+    private static boolean IpAddressEquals(Ipv6Prefix statsIpv6, Ipv6Prefix storedIpv6) {
+        final String[] statsIpMask = statsIpv6.getValue().split("/");
+        final String[] storedIpMask = storedIpv6.getValue().split("/");
+        if(! (statsIpMask.length > 1 && storedIpMask.length > 1 &&  statsIpMask[1].equals(storedIpMask[1]))){
+            return false;
+        }
+        if(InetAddresses.forString(statsIpMask[0]).equals(InetAddresses.forString(storedIpMask[0]))){
+            return true;
+        }
+        return false;
+    }
+
+
     static Boolean checkNullValues(final Object v1, final Object v2) {
         Boolean verdict = null;
         if (v1 == null && v2 != null) {
@@ -173,6 +199,18 @@ public class MatchComparatorHelper {
         return verdict;
     }
 
+    private static boolean compareIpv6PrefixNullSafe(Ipv6Prefix statsIpv6, Ipv6Prefix storedIpv6) {
+        boolean verdict = true;
+        final Boolean checkDestNullValuesOut = checkNullValues(statsIpv6, storedIpv6);
+        if (checkDestNullValuesOut != null) {
+            verdict = checkDestNullValuesOut;
+        } else if (!IpAddressEquals(statsIpv6, storedIpv6)) {
+            verdict = false;
+        }
+
+        return verdict;
+    }
+
     /**
      * Method return integer version of ip address. Converted int will be mask if mask specified
      */
index e41912152091658c49aa8a0752ee35f0ad5025a5..59f1e3b0d16b3f4d091d72ea97cab492246f66d9 100644 (file)
@@ -9,7 +9,6 @@
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
 
 import static org.opendaylight.openflowjava.util.ByteBufUtils.macAddressToString;
-
 import com.google.common.base.Optional;
 import java.math.BigInteger;
 import java.nio.ByteBuffer;
@@ -275,6 +274,19 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     private static final short PROTO_ICMPV4 = 1;
     private static final String NO_IP = "0.0.0.0/0";
 
+    // Pre-calculated masks for the 33 possible values. Do not give them out, but clone() them as they may
+    // end up being leaked and vulnerable.
+    private static final byte[][] IPV4_MASKS;
+    static {
+        final byte[][] tmp = new byte[33][];
+        for (int i = 0; i <= 32; ++i) {
+            final int mask = 0xffffffff << (32 - i);
+            tmp[i] =  new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8), (byte) mask};
+        }
+
+        IPV4_MASKS = tmp;
+    }
+
     @Override
     public List<MatchEntry> convert(
             final org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match match, final BigInteger datapathid) {
@@ -351,8 +363,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void protocolMatchFields(List<MatchEntry> matchEntryList,
-            ProtocolMatchFields protocolMatchFields) {
+    private static void protocolMatchFields(final List<MatchEntry> matchEntryList,
+            final ProtocolMatchFields protocolMatchFields) {
         if (protocolMatchFields != null) {
             if (protocolMatchFields.getMplsLabel() != null) {
                 matchEntryList.add(toOfMplsLabel(protocolMatchFields.getMplsLabel()));
@@ -373,8 +385,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void layer3Match(List<MatchEntry> matchEntryList,
-            Layer3Match layer3Match) {
+    private static void layer3Match(final List<MatchEntry> matchEntryList, final Layer3Match layer3Match) {
         if (layer3Match != null) {
             if (layer3Match instanceof Ipv4Match) {
                 Ipv4Match ipv4Match = (Ipv4Match) layer3Match;
@@ -657,8 +668,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void icmpv6Match(List<MatchEntry> matchEntryList,
-            Icmpv6Match icmpv6Match) {
+    private static void icmpv6Match(final List<MatchEntry> matchEntryList, final Icmpv6Match icmpv6Match) {
         if (icmpv6Match != null) {
             if (icmpv6Match.getIcmpv6Type() != null) {
                 matchEntryList.add(toOfIcmpv6Type(icmpv6Match.getIcmpv6Type()));
@@ -671,8 +681,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void icmpv4Match(List<MatchEntry> matchEntryList,
-            Icmpv4Match icmpv4Match) {
+    private static void icmpv4Match(final List<MatchEntry> matchEntryList, final Icmpv4Match icmpv4Match) {
         if (icmpv4Match != null) {
             if (icmpv4Match.getIcmpv4Type() != null) {
                 matchEntryList.add(toOfIcmpv4Type(icmpv4Match.getIcmpv4Type()));
@@ -685,8 +694,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void layer4Match(List<MatchEntry> matchEntryList,
-            Layer4Match layer4Match) {
+    private static void layer4Match(final List<MatchEntry> matchEntryList, final Layer4Match layer4Match) {
         if (layer4Match != null) {
             if (layer4Match instanceof TcpMatch) {
                 TcpMatch tcpMatch = (TcpMatch) layer4Match;
@@ -782,7 +790,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void ipMatch(List<MatchEntry> matchEntryList, IpMatch ipMatch) {
+    private static void ipMatch(final List<MatchEntry> matchEntryList, final IpMatch ipMatch) {
         if (ipMatch != null) {
             if (ipMatch.getIpDscp() != null) {
                 matchEntryList.add(toOfIpDscp(ipMatch.getIpDscp()));
@@ -795,13 +803,11 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
             if (ipMatch.getIpProtocol() != null) {
                 matchEntryList.add(toOfIpProto(ipMatch.getIpProtocol()));
             }
-
         }
     }
 
 
-    private void vlanMatch(List<MatchEntry> matchEntryList,
-            VlanMatch vlanMatch) {
+    private static void vlanMatch(final List<MatchEntry> matchEntryList, final VlanMatch vlanMatch) {
         if (vlanMatch != null) {
             if (vlanMatch.getVlanId() != null) {
                 VlanId vlanId = vlanMatch.getVlanId();
@@ -840,8 +846,7 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
     }
 
 
-    private void ethernetMatch(List<MatchEntry> matchEntryList,
-            EthernetMatch ethernetMatch) {
+    private static void ethernetMatch(final List<MatchEntry> matchEntryList, final EthernetMatch ethernetMatch) {
         if (ethernetMatch != null) {
             EthernetDestination ethernetDestination = ethernetMatch.getEthernetDestination();
             if (ethernetDestination != null) {
@@ -899,10 +904,8 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntry>> {
         }
 
         if (prefix != 0) {
-            int mask = 0xffffffff << (32 - prefix);
-            byte[] maskBytes = new byte[]{(byte) (mask >>> 24), (byte) (mask >>> 16), (byte) (mask >>> 8),
-                    (byte) mask};
-            return maskBytes;
+            // clone() is necessary to protect our constants
+            return IPV4_MASKS[prefix].clone();
         }
         return null;
     }