Sonar: Exception handler should preserve exception 46/31146/1
authorLorand Jakab <lojakab@cisco.com>
Thu, 10 Dec 2015 13:54:26 +0000 (15:54 +0200)
committerLorand Jakab <lojakab@cisco.com>
Thu, 10 Dec 2015 13:54:26 +0000 (15:54 +0200)
Fix 29 instances of Sonar issue "Exception handlers should preserve the
original exception" labeled as Major. The rule description says: "When
handling a caught exception, two mandatory informations should be
logged: 1) some context to ease the reproduction of the issue; 2) the
original's exception, for its message and stack trace."

Change-Id: I172b52d86d98ad735c940738dc5c35abc1cf675e
Signed-off-by: Lorand Jakab <lojakab@cisco.com>
14 files changed:
commons/unittest_tools/src/main/java/org/opendaylight/lispflowmapping/tools/junit/BaseExpectations.java
commons/unittest_tools/src/main/java/org/opendaylight/lispflowmapping/tools/junit/BaseTestCase.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/authentication/LispMACAuthentication.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/mdsal/DataStoreBackEnd.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/util/LispNotificationHelper.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/serializer/MapRequestSerializer.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/serializer/address/Ipv4PrefixSerializer.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/serializer/address/Ipv4Serializer.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/serializer/address/Ipv6PrefixSerializer.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/serializer/address/Ipv6Serializer.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/LispAddressUtil.java
mappingservice/lisp-proto/src/main/java/org/opendaylight/lispflowmapping/lisp/util/MapRequestUtil.java
mappingservice/southbound/src/main/java/org/opendaylight/lispflowmapping/southbound/LispSouthboundPlugin.java

index c1dd23d02f2ee1b16886f7d04dff54f76767c9e4..b34ecc631b75f00d849b2337cfb3570d1c164040 100644 (file)
@@ -144,7 +144,9 @@ public abstract class BaseExpectations extends Expectations {
             try {
                 return clazz.getMethod(methodName, Invocation.class);
             } catch (SecurityException e) {
+                LOG.debug("Catched SecurityException", e);
             } catch (NoSuchMethodException e) {
+                LOG.debug("Catched NoSuchMethodException", e);
             }
 
             return findMethod(clazz.getSuperclass());
@@ -387,6 +389,7 @@ public abstract class BaseExpectations extends Expectations {
         try {
             return currentBuilder();
         } catch (IllegalStateException e) {
+            LOG.debug("Catched IllegalStateException", e);
             return null;
         }
     }
index e14463d3dd09fa758c3570d8707b167aef6440b0..c1c832b66aab0f1b3ce7f871c9f0987e38bc12a8 100644 (file)
@@ -20,12 +20,17 @@ import org.hamcrest.Description;
 import org.jmock.api.Action;
 import org.jmock.api.Invocation;
 import org.junit.Assert;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public abstract class BaseTestCase extends BaseExpectations {
+    protected static final Logger LOG = LoggerFactory.getLogger(BaseTestCase.class);
+
     protected InetAddress asInet(int intValue) {
         try {
             return InetAddress.getByAddress(ByteBuffer.allocate(4).putInt(intValue).array());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ByteBuffer.allocate(4).putInt(intValue).array(), e);
             fail("");
         }
         return null;
@@ -109,6 +114,7 @@ public abstract class BaseTestCase extends BaseExpectations {
                 field.set(testedObject, fieldData.value);
                 return fieldData.value;
             } catch (Exception e) {
+                LOG.debug("Caught generic exception", e);
                 fail(e.getMessage());
             }
         }
index ef05b58f68e0046cac98bca235e511d435f3bf66..ed92ec9aa792dd0f18971b295fff971cd8b022cd 100644 (file)
@@ -37,7 +37,7 @@ public class LispMACAuthentication implements ILispAuthentication {
             authenticationLength = Mac.getInstance(algorithm).getMacLength();
             tempAuthenticationData = new byte[authenticationLength];
         } catch (NoSuchAlgorithmException e) {
-            LOG.warn("No such MAC algorithm" + algorithm);
+            LOG.warn("No such MAC algorithm {}", algorithm, e);
         }
     }
 
@@ -66,9 +66,9 @@ public class LispMACAuthentication implements ILispAuthentication {
 
             return mac.doFinal(data);
         } catch (InvalidKeyException e) {
-            LOG.warn("Invalid password" + key);
+            LOG.warn("Invalid password {}", key, e);
         } catch (NoSuchAlgorithmException e) {
-            LOG.warn("No such MAC algorithm" + algorithm);
+            LOG.warn("No such MAC algorithm {}", algorithm, e);
         }
         return null;
     }
index 204fade5120be937f8956618af61bdd683506022..7b53d3ab1b402be595c62cef88e87c20eb06aa80 100644 (file)
@@ -214,6 +214,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
                 }
             }
         } catch (SocketException se) {
+            LOG.debug("Caught socket exceptio", se);
         }
         return null;
     }
index 5435a4edc684a59728be95b9e3a9e95353e5987f..2a50f39a69f2aed9fbc9b2ad66ceee2586530ce5 100644 (file)
@@ -157,7 +157,7 @@ public class DataStoreBackEnd {
             submitFuture.checkedGet();
             ret = true;
         } catch (TransactionCommitFailedException e) {
-            LOG.error("{} : {}", errMsg, e.getMessage());
+            LOG.error(errMsg, e);
             ret = false;
         }
         return ret;
@@ -193,7 +193,7 @@ public class DataStoreBackEnd {
             submitFuture.checkedGet();
             ret = true;
         } catch (TransactionCommitFailedException e) {
-            LOG.error("{} : {}", errMsg, e.getMessage());
+            LOG.error(errMsg, e);
             ret = false;
         }
         return ret;
index a02fd235037a9ec7148d6eb8f03e4a6b131dbd72..5df1ab1c34b8e388b08bd6a46a80aeec1a10635b 100644 (file)
@@ -34,8 +34,12 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.addres
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv6;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.KeyValueAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class LispNotificationHelper {
+    protected static final Logger LOG = LoggerFactory.getLogger(LispNotificationHelper.class);
+
     // Utility class, should not be instantiated
     private LispNotificationHelper() {
     }
@@ -79,6 +83,7 @@ public final class LispNotificationHelper {
             InetAddress address = InetAddress.getByName(IPAddress);
             return address;
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", IPAddress, e);
             return null;
         }
     }
index ae35b57b1a2fac0f4c102e1b317d949aa9bb6873..7babedd388edd61083711a30e3d1af847506f62f 100644 (file)
@@ -157,7 +157,7 @@ public final class MapRequestSerializer {
                     builder.setMapReply(new org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.MapReplyBuilder().setMappingRecord(
                             MappingRecordSerializer.getInstance().deserialize(requestBuffer)).build()).build();
                 } catch (RuntimeException re) {
-                    LOG.warn("couldn't deserialize map reply encapsulated in map request. {}", re.getMessage());
+                    LOG.warn("Couldn't deserialize Map-Reply encapsulated in Map-Request", re);
                 }
             }
             return builder.build();
index bfc606c2e909c30c59941d3001c4ed3a2b9ab677..5be18f23b9d897fced449c4950c9871c2b813b0a 100644 (file)
@@ -25,6 +25,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.addres
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Lorand Jakab
@@ -32,6 +34,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl
  */
 public final class Ipv4PrefixSerializer extends LispAddressSerializer {
 
+    protected static final Logger LOG = LoggerFactory.getLogger(Ipv4PrefixSerializer.class);
+
     private static final Ipv4PrefixSerializer INSTANCE = new Ipv4PrefixSerializer();
 
     // Private constructor prevents instantiation from other classes
@@ -59,10 +63,12 @@ public final class Ipv4PrefixSerializer extends LispAddressSerializer {
 
     @Override
     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
-        Ipv4Prefix address = (Ipv4Prefix) lispAddress.getAddress();
+        Ipv4Prefix prefix = (Ipv4Prefix) lispAddress.getAddress();
+        String address = MaskUtil.getAddressStringForIpv4Prefix(prefix);
         try {
-            buffer.put(Inet4Address.getByName(MaskUtil.getAddressStringForIpv4Prefix(address)).getAddress());
+            buffer.put(Inet4Address.getByName(address).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address, e);
         }
     }
 
@@ -101,6 +107,7 @@ public final class Ipv4PrefixSerializer extends LispAddressSerializer {
         try {
             address = InetAddress.getByAddress(ipBuffer);
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ipBuffer, e);
         }
         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix(
                 address.getHostAddress() + "/" + ctx.getMaskLen());
index 5073a9eb2472da7bf9339a8a9f102fa76186d7f5..be39be4087b1a2a0764f44314f9bc0fe15fcb3a4 100644 (file)
@@ -24,6 +24,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ei
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Lorand Jakab
@@ -31,6 +33,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl
  */
 public final class Ipv4Serializer extends LispAddressSerializer {
 
+    protected static final Logger LOG = LoggerFactory.getLogger(Ipv4Serializer.class);
+
     private static final Ipv4Serializer INSTANCE = new Ipv4Serializer();
 
     // Private constructor prevents instantiation from other classes
@@ -62,6 +66,7 @@ public final class Ipv4Serializer extends LispAddressSerializer {
         try {
             buffer.put(Inet4Address.getByName(address.getIpv4().getValue()).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address.getIpv4().getValue(), e);
         }
     }
 
@@ -70,6 +75,7 @@ public final class Ipv4Serializer extends LispAddressSerializer {
         try {
             buffer.put(Inet4Address.getByName(address.getIpAddress().getIpv4Address().getValue()).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address.getIpAddress().getIpv4Address().getValue(), e);
         }
     }
 
@@ -103,6 +109,7 @@ public final class Ipv4Serializer extends LispAddressSerializer {
         try {
             address = InetAddress.getByAddress(ipBuffer);
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ipBuffer, e);
         }
         return new Ipv4Address(address.getHostAddress());
     }
index 8cee656374fe6468fc73f73abf43a843783ab39e..27aa2cc52648f7e9fb3ee9e75947f96dca7e1bff 100644 (file)
@@ -25,6 +25,8 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.addres
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Lorand Jakab
@@ -32,6 +34,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl
  */
 public final class Ipv6PrefixSerializer extends LispAddressSerializer {
 
+    protected static final Logger LOG = LoggerFactory.getLogger(Ipv6PrefixSerializer.class);
+
     private static final Ipv6PrefixSerializer INSTANCE = new Ipv6PrefixSerializer();
 
     // Private constructor prevents instantiation from other classes
@@ -59,10 +63,12 @@ public final class Ipv6PrefixSerializer extends LispAddressSerializer {
 
     @Override
     protected void serializeData(ByteBuffer buffer, LispAddress lispAddress) {
-        Ipv6Prefix address = (Ipv6Prefix) lispAddress.getAddress();
+        Ipv6Prefix prefix = (Ipv6Prefix) lispAddress.getAddress();
+        String address = MaskUtil.getAddressStringForIpv6Prefix(prefix);
         try {
-            buffer.put(Inet6Address.getByName(MaskUtil.getAddressStringForIpv6Prefix(address)).getAddress());
+            buffer.put(Inet6Address.getByName(address).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address, e);
         }
     }
 
@@ -71,6 +77,7 @@ public final class Ipv6PrefixSerializer extends LispAddressSerializer {
         try {
             buffer.put(Inet6Address.getByName(prefix.getIpv6Prefix().getValue()).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", prefix.getIpv6Prefix().getValue(), e);
         }
     }
 
@@ -101,6 +108,7 @@ public final class Ipv6PrefixSerializer extends LispAddressSerializer {
         try {
             address = InetAddress.getByAddress(ipBuffer);
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ipBuffer, e);
         }
         return new org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix(
                 address.getHostAddress() + "/" + ctx.getMaskLen());
index 52f4f3e7598eb80f4bfef996c5b407770ffcbb94..485d271cf0caf15cd9a5aac541b74d1b58f4163d 100644 (file)
@@ -24,6 +24,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ei
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.RlocBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * @author Lorand Jakab
@@ -31,6 +33,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rl
  */
 public final class Ipv6Serializer extends LispAddressSerializer {
 
+    protected static final Logger LOG = LoggerFactory.getLogger(Ipv6Serializer.class);
+
     private static final Ipv6Serializer INSTANCE = new Ipv6Serializer();
 
     // Private constructor prevents instantiation from other classes
@@ -62,6 +66,7 @@ public final class Ipv6Serializer extends LispAddressSerializer {
         try {
             buffer.put(Inet6Address.getByName(address.getIpv6().getValue()).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address.getIpv6().getValue(), e);
         }
     }
 
@@ -70,6 +75,7 @@ public final class Ipv6Serializer extends LispAddressSerializer {
         try {
             buffer.put(Inet6Address.getByName(address.getIpAddress().getIpv6Address().getValue()).getAddress());
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", address.getIpAddress().getIpv6Address().getValue(), e);
         }
     }
 
@@ -103,6 +109,7 @@ public final class Ipv6Serializer extends LispAddressSerializer {
         try {
             address = InetAddress.getByAddress(ipBuffer);
         } catch (UnknownHostException e) {
+            LOG.debug("Unknown host {}", ipBuffer, e);
         }
         return new Ipv6Address(address.getHostAddress());
     }
index 29fc5b0c911fc70316d4c9e6a62c80bf4e1ad63b..94be33022082b13c2685b48f4073eeec3b16154f 100644 (file)
@@ -61,8 +61,12 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.addres
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop.LrsBits;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.HopBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class LispAddressUtil {
+    protected static final Logger LOG = LoggerFactory.getLogger(LispAddressUtil.class);
+
     private static Pattern IP4_PATTERN = null;
     private static Pattern IP6_PATTERN = null;
     private static final String ip4Pattern = "(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])";
@@ -72,6 +76,7 @@ public final class LispAddressUtil {
           IP4_PATTERN = Pattern.compile(ip4Pattern, Pattern.CASE_INSENSITIVE);
           IP6_PATTERN = Pattern.compile(ip6Pattern, Pattern.CASE_INSENSITIVE);
         } catch (PatternSyntaxException e) {
+            LOG.debug("Caught pattern syntax exception", e);
         }
       }
 
index 4a668786478daf941aee31ac20c31d0241a112d3..e0a7068048eb570d62723ec2fed132c02e2328db 100644 (file)
@@ -22,8 +22,12 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ma
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.SourceEidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequestnotification.MapRequestBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public final class MapRequestUtil {
+    protected static final Logger LOG = LoggerFactory.getLogger(MapRequestUtil.class);
+
     // Utility class, should not be instantiated
     private MapRequestUtil() {
     }
@@ -39,6 +43,7 @@ public final class MapRequestUtil {
                 try {
                     selectedItrRloc = InetAddress.getByName(((Ipv4) addr).getIpv4().getValue());
                 } catch (UnknownHostException e) {
+                    LOG.debug("Unknown host {}", ((Ipv4) addr).getIpv4().getValue(), e);
                 }
                 break;
             }
@@ -46,6 +51,7 @@ public final class MapRequestUtil {
                 try {
                     selectedItrRloc = InetAddress.getByName(((Ipv6) addr).getIpv6().getValue());
                 } catch (UnknownHostException e) {
+                    LOG.debug("Unknown host {}", ((Ipv6) addr).getIpv6().getValue(), e);
                 }
                 break;
             }
index aab88c66c943fb0808aca92031e2ec0ad0989025..699542a2796bde09b29f25f8ce4a4515de7c284e 100644 (file)
@@ -138,6 +138,7 @@ public class LispSouthboundPlugin implements IConfigLispSouthboundPlugin, AutoCl
                     threadSocket.receive(packet);
                     LOG.trace("Received a packet!");
                 } catch (SocketTimeoutException ste) {
+                    LOG.trace("Timed out waiting on socket", ste);
                     continue;
                 } catch (IOException e) {
                     LOG.warn("IO Exception while trying to recieve packet", e);