Fix checkstyle issues related to exception handling 01/46101/3
authorRitu Sood <ritu.sood@intel.com>
Thu, 22 Sep 2016 02:20:01 +0000 (19:20 -0700)
committerRitu Sood <ritu.sood@intel.com>
Mon, 10 Oct 2016 03:17:31 +0000 (20:17 -0700)
This is based on the Guidelines given here
https://wiki.opendaylight.org/view/BestPractices/Coding_Guidelines#IllegalCatch

Change-Id: I70f9841f706c8d5c38b0834dba04b79ad8fbfa71
Signed-off-by: Ritu Sood <ritu.sood@intel.com>
integration/test/src/test/java/org/opendaylight/neutron/e2etest/ITNeutronE2E.java
integration/test/src/test/java/org/opendaylight/neutron/e2etest/NeutronNetworkTests.java
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronCRUDInterfaces.java
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronSubnet.java
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronSubnetIPAllocationPool.java
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/AbstractNeutronNorthbound.java
parent/checkstyle-suppressions.xml
transcriber/src/main/java/org/opendaylight/neutron/transcriber/AbstractNeutronInterface.java

index 222537766e92f299113d56855f56ec9a8d4b36c8..9ab4cc3bf81a2f112cf08c66f86df7c0d274025d 100644 (file)
@@ -22,8 +22,10 @@ import com.google.gson.JsonElement;
 import com.google.gson.JsonObject;
 import java.io.BufferedReader;
 import java.io.File;
+import java.io.IOException;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
+import java.io.UncheckedIOException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.util.Map;
@@ -83,7 +85,7 @@ public class ITNeutronE2E {
     final String base = "http://127.0.0.1:8080/controller/nb/v2/neutron";
 
     @Test
-    public void test() {
+    public void test() throws IOException, InterruptedException {
         NeutronNetworkTests.runTests(base);
         NeutronSubnetTests.runTests(base);
         NeutronPortTests.runTests(base);
@@ -120,7 +122,7 @@ public class ITNeutronE2E {
         Neutron_Bug4027_Tests.runTests(base);
     }
 
-    static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws Exception {
+    static HttpURLConnection httpURLConnectionFactoryGet(URL url) throws IOException {
         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
         httpConn.setRequestMethod("GET");
         httpConn.setRequestProperty("Content-Type", "application/json");
@@ -128,7 +130,7 @@ public class ITNeutronE2E {
         return httpConn;
     }
 
-    static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws Exception {
+    static HttpURLConnection httpURLConnectionFactoryDelete(URL url) throws IOException {
         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
         httpConn.setRequestMethod("DELETE");
         httpConn.setRequestProperty("Content-Type", "application/json");
@@ -136,7 +138,7 @@ public class ITNeutronE2E {
         return httpConn;
     }
 
-    static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws Exception {
+    static HttpURLConnection httpURLConnectionFactoryPost(URL url, String content) throws IOException {
         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
         httpConn.setRequestMethod("POST");
         httpConn.setRequestProperty("Content-Type", "application/json");
@@ -148,7 +150,7 @@ public class ITNeutronE2E {
         return httpConn;
     }
 
-    static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws Exception {
+    static HttpURLConnection httpURLConnectionFactoryPut(URL url, String content) throws IOException {
         HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
         httpConn.setRequestMethod("PUT");
         httpConn.setRequestProperty("Content-Type", "application/json");
@@ -165,9 +167,8 @@ public class ITNeutronE2E {
             URL url = new URL(url_s);
             HttpURLConnection httpConn = httpURLConnectionFactoryPost(url, content);
             Assert.assertEquals(context, 201, httpConn.getResponseCode());
-        } catch (Exception e) {
-            e.printStackTrace(); // temporary, remove me
-            Assert.assertFalse("E2E Tests Failed", true);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
@@ -176,9 +177,8 @@ public class ITNeutronE2E {
             URL url = new URL(url_s);
             HttpURLConnection httpConn = httpURLConnectionFactoryPut(url, content);
             Assert.assertEquals(context, 200, httpConn.getResponseCode());
-        } catch (Exception e) {
-            e.printStackTrace(); // temporary, remove me
-            Assert.assertFalse("E2E Tests Failed", true);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
@@ -187,9 +187,8 @@ public class ITNeutronE2E {
             URL url = new URL(url_s);
             HttpURLConnection httpConn = httpURLConnectionFactoryGet(url);
             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
-        } catch (Exception e) {
-            e.printStackTrace(); // temporary, remove me
-            Assert.assertFalse("E2E Tests Failed", true);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
@@ -207,9 +206,8 @@ public class ITNeutronE2E {
             URL url = new URL(url_s);
             HttpURLConnection httpConn = httpURLConnectionFactoryDelete(url);
             Assert.assertEquals(context, responseCode, httpConn.getResponseCode());
-        } catch (Exception e) {
-            e.printStackTrace(); // temporary, remove me
-            Assert.assertFalse("E2E Tests Failed", true);
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
     }
 
@@ -234,10 +232,8 @@ public class ITNeutronE2E {
                 response.append(inputLine);
             }
             in.close();
-        } catch (Exception e) {
-            e.printStackTrace(); // temporary, remove me
-            Assert.assertFalse("E2E Tests Failed", true);
-
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
         }
         return response.toString();
     }
index 6ccefa0bdc67caa287b57de9fe4e9edcc8ab0fac..9e4c223d7aef090dc4c0ba54278598a21c734160 100644 (file)
@@ -8,6 +8,7 @@
 
 package org.opendaylight.neutron.e2etest;
 
+import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import org.junit.Assert;
@@ -26,27 +27,23 @@ public class NeutronNetworkTests {
         this.base = base;
     }
 
-    public void network_collection_get_test_with_wait() {
+    public void network_collection_get_test_with_wait() throws IOException, InterruptedException {
         String url_s = base + "/networks";
-        try {
-            int i = 0;
-            while (i < TIMEOUT) {
-                URL url = new URL(url_s);
-                HttpURLConnection httpConn = ITNeutronE2E.httpURLConnectionFactoryGet(url);
-                if (httpConn.getResponseCode() != 200) {
-                    LOGGER.info("trial " + Integer.toString(i) + ": failed with: "
-                            + Integer.toString(httpConn.getResponseCode()));
-                    Thread.sleep(1000);
-                    i += 1;
-                } else {
-                    Assert.assertEquals("Network Collection GET failed", 200, httpConn.getResponseCode());
-                    return;
-                }
+        int i = 0;
+        while (i < TIMEOUT) {
+            URL url = new URL(url_s);
+            HttpURLConnection httpConn = ITNeutronE2E.httpURLConnectionFactoryGet(url);
+            if (httpConn.getResponseCode() != 200) {
+                LOGGER.info("trial " + Integer.toString(i) + ": failed with: "
+                        + Integer.toString(httpConn.getResponseCode()));
+                Thread.sleep(1000);
+                i += 1;
+            } else {
+                Assert.assertEquals("Network Collection GET failed", 200, httpConn.getResponseCode());
+                return;
             }
-            Assert.assertFalse("Network Collection GET with wait failed", true);
-        } catch (Exception e) {
-            Assert.assertFalse("E2E Tests Failed", true);
         }
+        Assert.assertFalse("Network Collection GET with wait failed", true);
     }
 
     //TODO handle SB check
@@ -137,7 +134,7 @@ public class NeutronNetworkTests {
         ITNeutronE2E.test_delete(url, "Network Element Delete Failed");
     }
 
-    public static void runTests(String base) {
+    public static void runTests(String base) throws IOException, InterruptedException {
         NeutronNetworkTests network_tester = new NeutronNetworkTests(base);
         network_tester.network_collection_get_test_with_wait();
         String createJsonString = network_tester.singleton_network_create_test();
index e3a4a796f87c61206e070f96f5bcc7c979f59b65..e8eab9490f0835ea907b0e2af9465e9dd5abc15e 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.neutron.spi;
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -35,7 +36,7 @@ public final class NeutronCRUDInterfaces {
             if (services != null) {
                 return bCtx.getService(services[0]);
             }
-        } catch (Exception e) {
+        } catch (InvalidSyntaxException e) {
             LOGGER.error("Error in getInstances", e);
         }
         return null;
index 46a4fe9a291e328eaa98e7e20b234476270e0df2..1ce5961a270feded2d28e89ccd25d26e780a1c05 100644 (file)
@@ -317,24 +317,20 @@ public final class NeutronSubnet extends NeutronBaseAttributes<NeutronSubnet> im
                 if (parts.length != 2) {
                     return;
                 }
-                try {
-                    int length = Integer.parseInt(parts[1]);
-                    BigInteger lowAddress_bi = NeutronSubnetIPAllocationPool.convertV6(parts[0]);
-                    String lowAddress = NeutronSubnetIPAllocationPool.bigIntegerToIP(lowAddress_bi.add(BigInteger.ONE));
-                    BigInteger mask = BigInteger.ONE.shiftLeft(length).subtract(BigInteger.ONE);
-                    String highAddress = NeutronSubnetIPAllocationPool
-                            .bigIntegerToIP(lowAddress_bi.add(mask).subtract(BigInteger.ONE));
-                    if (gatewayIP == null || ("").equals(gatewayIP)) {
-                        gatewayIP = lowAddress;
-                    }
-                    if (allocationPools.size() < 1) {
-                        NeutronSubnetIPAllocationPool source = new NeutronSubnetIPAllocationPool(lowAddress,
-                                highAddress);
-                        allocationPools = source.splitPoolV6(gatewayIP);
-                    }
-                } catch (Exception e) {
-                    LOGGER.warn("Failure in initDefault()", e);
-                    return;
+
+                int length = Integer.parseInt(parts[1]);
+                BigInteger lowAddress_bi = NeutronSubnetIPAllocationPool.convertV6(parts[0]);
+                String lowAddress = NeutronSubnetIPAllocationPool.bigIntegerToIP(lowAddress_bi.add(BigInteger.ONE));
+                BigInteger mask = BigInteger.ONE.shiftLeft(length).subtract(BigInteger.ONE);
+                String highAddress = NeutronSubnetIPAllocationPool
+                        .bigIntegerToIP(lowAddress_bi.add(mask).subtract(BigInteger.ONE));
+                if (gatewayIP == null || ("").equals(gatewayIP)) {
+                    gatewayIP = lowAddress;
+                }
+                if (allocationPools.size() < 1) {
+                    NeutronSubnetIPAllocationPool source = new NeutronSubnetIPAllocationPool(lowAddress,
+                            highAddress);
+                    allocationPools = source.splitPoolV6(gatewayIP);
                 }
             }
         }
index 05aca86ec3417bf55372e3e8149957aac52723a7..375a62383187f4c73131efff6f199719f98e35d3 100644 (file)
@@ -12,6 +12,7 @@ import java.io.Serializable;
 import java.math.BigInteger;
 import java.net.Inet6Address;
 import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.List;
 import javax.xml.bind.annotation.XmlAccessType;
@@ -136,7 +137,7 @@ public final class NeutronSubnetIPAllocationPool implements Serializable {
         }
         try {
             return new BigInteger(((Inet6Address) InetAddress.getByName(inputString)).getAddress());
-        } catch (Exception e) {
+        } catch (UnknownHostException e) {
             LOGGER.error("convertV6 error", e);
             return BigInteger.ZERO;
         }
@@ -172,7 +173,7 @@ public final class NeutronSubnetIPAllocationPool implements Serializable {
     static String bigIntegerToIP(BigInteger b) {
         try {
             return Inet6Address.getByAddress(b.toByteArray()).getHostAddress();
-        } catch (Exception e) {
+        } catch (UnknownHostException e) {
             LOGGER.error("bigIntegerToIP", e);
             return "ERROR";
         }
index 20788c62984da2bb99de0be0bddf8c3bb6c2d0c4..9ff8078dd7a3b0da43c9ed758f654c93293439d9 100644 (file)
@@ -137,15 +137,7 @@ public abstract class AbstractNeutronNorthbound<T extends INeutronObject<T>, Neu
         /*
          * remove it and return 204 status
          */
-        boolean exist = false;
-        try {
-            exist = neutronCRUD.remove(uuid);
-        } catch (Exception e) {
-            final String resourceName = getResourceName();
-            LOGGER.debug("exception during remove {} {} {}", resourceName, uuid, e);
-            throw new InternalServerErrorException("Could not delete " + resourceName);
-        }
-        if (!exist) {
+        if (!neutronCRUD.remove(uuid)) {
             throw new ResourceNotFoundException(uuidNoExist());
         }
 
index 97c1ffe05fd8e19e18d1ae5f1a012232dc53cd4c..5291703b0467ead954c89e87a1e3c6fb4df7fc9c 100644 (file)
   <suppress checks="TypeName"
             files="."
              />
-  <suppress checks="IllegalCatch"
-            files="."
-             />
-  <suppress checks="RegexpSinglelineJava"
-            files="."
-             />
 </suppressions>
 
index 7ec82cbb370a94b27c7956dbabf6eac844c3a1db..4e329d34d53e923157bce59377d12cb738d1dc7f 100644 (file)
@@ -317,32 +317,6 @@ public abstract class AbstractNeutronInterface<T extends DataObject & Identifiab
         return result;
     }
 
-    // this method uses reflection to update an object from it's delta.
-
-    protected boolean overwrite(Object target, Object delta) {
-        final Method[] methods = target.getClass().getMethods();
-
-        for (final Method toMethod : methods) {
-            if (toMethod.getDeclaringClass().equals(target.getClass()) && toMethod.getName().startsWith("set")) {
-
-                final String toName = toMethod.getName();
-                final String fromName = toName.replace("set", "get");
-
-                try {
-                    final Method fromMethod = delta.getClass().getMethod(fromName);
-                    final Object value = fromMethod.invoke(delta, (Object[]) null);
-                    if (value != null) {
-                        toMethod.invoke(target, value);
-                    }
-                } catch (final Exception e) {
-                    LOGGER.error("Error in overwrite", e);
-                    return false;
-                }
-            }
-        }
-        return true;
-    }
-
     @Override
     public void close() throws Exception {
         // TODO Auto-generated method stub