Bug 1181 : of-flow: Flow match criteria (ipv6-source,ipv6-destination) is different... 57/7957/2
authorAnilkumar Vishnoi <avishnoi@in.ibm.com>
Thu, 12 Jun 2014 19:06:35 +0000 (00:36 +0530)
committerMichal Rehak <mirehak@cisco.com>
Fri, 13 Jun 2014 08:48:25 +0000 (10:48 +0200)
- added unit test

Change-Id: I186d9bdd67a62fa8f98606a8f3ad76d99ef56fec
Signed-off-by: Anilkumar Vishnoi <avishnoi@in.ibm.com>
Signed-off-by: Michal Rehak <mirehak@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorImpl.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorUtil.java
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/convertor/match/MatchConvertorUtilTest.java

index d9f48235d2a936975c0bac275b91cbdfc695046b..11a5127435d00a9f8a73af0aaef247f52264e231 100644 (file)
@@ -802,9 +802,10 @@ public class MatchConvertorImpl implements MatchConvertor<List<MatchEntries>> {
                 if (ipv6AddressMatchEntry != null) {
                     String ipv6PrefixStr = ipv6AddressMatchEntry.getIpv6Address().getValue();
                     MaskMatchEntry maskMatchEntry = ofMatch.getAugmentation(MaskMatchEntry.class);
-                    if (maskMatchEntry != null) {
-                        ipv6PrefixStr += PREFIX_SEPARATOR + ByteBuffer.wrap(maskMatchEntry.getMask()).getInt();
-                    }
+
+                    ipv6PrefixStr += PREFIX_SEPARATOR
+                            + MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskMatchEntry.getMask());
+                        
                     if (ofMatch.getOxmMatchField().equals(Ipv6Src.class)) {
                         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix(ipv6PrefixStr));
                     }
index 56d7fe4a2385da50cc5efe53773f446570a5c6b6..6302a5365a8d17a6b617477ee3433a35913e9df9 100644 (file)
@@ -7,6 +7,10 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
 
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
 
 /**
@@ -31,5 +35,45 @@ public abstract class MatchConvertorUtil {
         bitmap |= pField.isUnseq() ?  (1 << 8) : 0;
         return bitmap;
     }
+    
+    public static int ipv6NetmaskArrayToCIDRValue(byte[] rawMask){
+
+        /*
+         * Openflow Spec : 1.3.2+
+         * An all-one-bits oxm_mask is equivalent to specifying 0 for oxm_hasmask and omitting oxm_mask.
+         * So when user specify 128 as a mask, switch omit that mask and we get null as a mask in flow
+         * statistics response.
+         */
+
+        int maskValue = 128;
+
+        if (rawMask != null) {
+            maskValue = 0;
+            for(int subArrayCounter=0;subArrayCounter<4;subArrayCounter++){
+                int copyFrom = subArrayCounter * 4;
+
+                byte[] subArray = Arrays.copyOfRange(rawMask, copyFrom, copyFrom+4);  
+
+                int receivedMask = ByteBuffer.wrap(subArray).getInt();
+
+                int shiftCount=0;
+
+                if (receivedMask == 0) {
+                    break;
+                }
+
+                while(receivedMask != 0xffffffff){
+                    receivedMask = receivedMask >> 1;
+                    shiftCount++;
+                }
+
+                maskValue = maskValue+(32-shiftCount);
+                if(shiftCount != 0) {
+                    break;
+                }
+            }
+        }
+        return maskValue;
+    }
 
 }
index 76cc45ab1badab09a10f10387ce450245943606d..b5ffbbb4017c4cb13e0e1e940edd8cb6c5cbe041 100644 (file)
@@ -8,9 +8,11 @@
 package org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match;
 
 import java.lang.reflect.Constructor;
+import java.math.BigInteger;
 
 import org.junit.Assert;
 import org.junit.Test;
+import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Ipv6ExthdrFlags;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -63,4 +65,25 @@ public class MatchConvertorUtilTest {
         return flags;
     }
 
+    /**
+     * Test method for {@link org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.match.MatchConvertorUtil#ipv6NetmaskArrayToCIDRValue(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry)}.
+     * @throws Exception
+     */
+    @Test
+    public void testIpv6NetmaskArrayToCIDRValue() throws Exception {
+        BigInteger maskSeed = new BigInteger("1ffffffffffffffffffffffffffffffff", 16);
+        byte[] maskArray = new byte[16];
+        LOG.debug("maskSeed= {}", ByteBufUtils.bytesToHexString(maskSeed.toByteArray()));
+
+        for (int i = 0; i <= 128; i++) {
+            System.arraycopy(maskSeed.toByteArray(), 1, maskArray, 0, 16);
+            LOG.debug("maskHex[{}] = {}", i, ByteBufUtils.bytesToHexString(maskArray));
+            int cidr = MatchConvertorUtil.ipv6NetmaskArrayToCIDRValue(maskArray);
+            LOG.debug("cidr = {}", cidr);
+            Assert.assertEquals(128-i, cidr);
+
+            maskSeed = maskSeed.clearBit(i);
+        }
+    }
+
 }