fix npe when calculating IpMatchHash 34/20134/1
authorMartin Bobak <mbobak@cisco.com>
Tue, 12 May 2015 14:03:38 +0000 (16:03 +0200)
committerMartin Bobak <mbobak@cisco.com>
Tue, 12 May 2015 14:03:38 +0000 (16:03 +0200)
Change-Id: I32a0af1b840cb1716be40f8a7596e1fa235f51b8
Signed-off-by: Martin Bobak <mbobak@cisco.com>
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/util/HashUtil.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/util/HashUtilTest.java

index 6ea6fa68c2c0b2fc96108577f7f7cbc91f4f1190..3c906fcb4cbdf1d4999ff5165134fdb62f8ff9e3 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.openflowplugin.impl.util;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.math.BigInteger;
 import java.util.StringTokenizer;
 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
@@ -518,7 +519,8 @@ public final class HashUtil {
         return hash;
     }
 
-    private static long calculateIpMatchHash(final IpMatch ipMatch) {
+    @VisibleForTesting
+    public static long calculateIpMatchHash(final IpMatch ipMatch) {
         long hash = 0;
         Short ipEcn = ipMatch.getIpEcn();
         if (null != ipEcn) {
@@ -529,15 +531,20 @@ public final class HashUtil {
             hash += ipProtocol;
         }
 
-        Short ipDscp = ipMatch.getIpDscp().getValue();
-        if (null != ipDscp) {
-            hash += ipDscp;
+        if (null != ipMatch.getIpDscp()) {
+            Short ipDscp = ipMatch.getIpDscp().getValue();
+            if (null != ipDscp) {
+                hash += ipDscp;
+            }
         }
 
         IpVersion ipVersion = ipMatch.getIpProto();
-        if (null != ipVersion) {
+        if (null != ipVersion)
+
+        {
             hash += ipVersion.getIntValue();
         }
+
         return hash;
     }
 
index f7451a475c290a4dbb846b86daa4162017e96552..11c822096db3b756a9a2b8e572d50284e48b6a91 100644 (file)
@@ -13,6 +13,7 @@ import org.junit.Test;
 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.match.IpMatchBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -119,4 +120,11 @@ public class HashUtilTest {
         }
     }
 
+    @Test
+    public void calculateIpMatchHash() {
+        IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
+        ipMatchBuilder.setIpEcn((short) 42);
+        HashUtil.calculateIpMatchHash(ipMatchBuilder.build());
+    }
+
 }
\ No newline at end of file