Refactor to remove empty catch exception. 49/2449/4
authorRyan Moats <rmoats@us.ibm.com>
Wed, 6 Nov 2013 16:45:07 +0000 (10:45 -0600)
committerGiovanni Meo <gmeo@cisco.com>
Thu, 7 Nov 2013 18:30:15 +0000 (19:30 +0100)
Initializing a subnet object included an empty catch exception.
Refactor to return a boolean from this method so that exception
is reflected to calling methods, which can then take appropriate
action.

Change-Id: Ia6f3188064677de9035508994d864373915db0f1
Signed-off-by: Ryan Moats <rmoats@us.ibm.com>
opendaylight/networkconfiguration/neutron/src/main/java/org/opendaylight/controller/networkconfig/neutron/NeutronSubnet.java
opendaylight/northbound/networkconfiguration/neutron/src/main/java/org/opendaylight/controller/networkconfig/neutron/northbound/NeutronSubnetsNorthbound.java

index c6ea942..8b1a8d6 100644 (file)
@@ -145,15 +145,16 @@ public class NeutronSubnet {
     }\r
 \r
     public boolean isEnableDHCP() {\r
-        if (enableDHCP == null)\r
+        if (enableDHCP == null) {\r
             return true;\r
+        }\r
         return enableDHCP;\r
     }\r
 \r
     public Boolean getEnableDHCP() { return enableDHCP; }\r
 \r
     public void setEnableDHCP(Boolean newValue) {\r
-            this.enableDHCP = newValue;\r
+            enableDHCP = newValue;\r
     }\r
 \r
     public String getTenantID() {\r
@@ -179,18 +180,24 @@ public class NeutronSubnet {
         Iterator<String> i = fields.iterator();\r
         while (i.hasNext()) {\r
             String s = i.next();\r
-            if (s.equals("id"))\r
+            if (s.equals("id")) {\r
                 ans.setSubnetUUID(this.getSubnetUUID());\r
-            if (s.equals("network_id"))\r
+            }\r
+            if (s.equals("network_id")) {\r
                 ans.setNetworkUUID(this.getNetworkUUID());\r
-            if (s.equals("name"))\r
+            }\r
+            if (s.equals("name")) {\r
                 ans.setName(this.getName());\r
-            if (s.equals("ip_version"))\r
+            }\r
+            if (s.equals("ip_version")) {\r
                 ans.setIpVersion(this.getIpVersion());\r
-            if (s.equals("cidr"))\r
+            }\r
+            if (s.equals("cidr")) {\r
                 ans.setCidr(this.getCidr());\r
-            if (s.equals("gateway_ip"))\r
+            }\r
+            if (s.equals("gateway_ip")) {\r
                 ans.setGatewayIP(this.getGatewayIP());\r
+            }\r
             if (s.equals("dns_nameservers")) {\r
                 List<String> nsList = new ArrayList<String>();\r
                 nsList.addAll(this.getDnsNameservers());\r
@@ -206,10 +213,12 @@ public class NeutronSubnet {
                 hRoutes.addAll(this.getHostRoutes());\r
                 ans.setHostRoutes(hRoutes);\r
             }\r
-            if (s.equals("enable_dhcp"))\r
+            if (s.equals("enable_dhcp")) {\r
                 ans.setEnableDHCP(this.getEnableDHCP());\r
-            if (s.equals("tenant_id"))\r
+            }\r
+            if (s.equals("tenant_id")) {\r
                 ans.setTenantID(this.getTenantID());\r
+            }\r
         }\r
         return ans;\r
     }\r
@@ -222,8 +231,9 @@ public class NeutronSubnet {
         try {\r
             SubnetUtils util = new SubnetUtils(cidr);\r
             SubnetInfo info = util.getInfo();\r
-            if (!info.getNetworkAddress().equals(info.getAddress()))\r
+            if (!info.getNetworkAddress().equals(info.getAddress())) {\r
                 return false;\r
+            }\r
         } catch (Exception e) {\r
             return false;\r
         }\r
@@ -238,17 +248,20 @@ public class NeutronSubnet {
         Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();\r
         while (i.hasNext()) {\r
             NeutronSubnet_IPAllocationPool pool = i.next();\r
-            if (pool.contains(gatewayIP))\r
+            if (pool.contains(gatewayIP)) {\r
                 return true;\r
+            }\r
         }\r
         return false;\r
     }\r
 \r
-    public void initDefaults() {\r
-        if (enableDHCP == null)\r
+    public boolean initDefaults() {\r
+        if (enableDHCP == null) {\r
             enableDHCP = true;\r
-        if (ipVersion == null)\r
+        }\r
+        if (ipVersion == null) {\r
             ipVersion = 4;\r
+        }\r
         gatewayIPAssigned = false;\r
         dnsNameservers = new ArrayList<String>();\r
         allocationPools = new ArrayList<NeutronSubnet_IPAllocationPool>();\r
@@ -256,8 +269,9 @@ public class NeutronSubnet {
         try {\r
             SubnetUtils util = new SubnetUtils(cidr);\r
             SubnetInfo info = util.getInfo();\r
-            if (gatewayIP == null)\r
+            if (gatewayIP == null) {\r
                 gatewayIP = info.getLowAddress();\r
+            }\r
             if (allocationPools.size() < 1) {\r
                 NeutronSubnet_IPAllocationPool source =\r
                     new NeutronSubnet_IPAllocationPool(info.getLowAddress(),\r
@@ -265,7 +279,9 @@ public class NeutronSubnet {
                 allocationPools = source.splitPool(gatewayIP);\r
             }\r
         } catch (Exception e) {\r
+            return false;\r
         }\r
+        return true;\r
     }\r
 \r
     public List<NeutronPort> getPortsInSubnet() {\r
@@ -297,13 +313,15 @@ public class NeutronSubnet {
      * available allocation pools or not\r
      */\r
     public boolean isIPInUse(String ipAddress) {\r
-        if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned )\r
+        if (ipAddress.equals(gatewayIP) && !gatewayIPAssigned ) {\r
             return false;\r
+        }\r
         Iterator<NeutronSubnet_IPAllocationPool> i = allocationPools.iterator();\r
         while (i.hasNext()) {\r
             NeutronSubnet_IPAllocationPool pool = i.next();\r
-            if (pool.contains(ipAddress))\r
+            if (pool.contains(ipAddress)) {\r
                 return false;\r
+            }\r
         }\r
         return true;\r
     }\r
@@ -322,8 +340,9 @@ public class NeutronSubnet {
             }\r
             else\r
                 if (NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart()) <\r
-                        NeutronSubnet_IPAllocationPool.convert(ans))\r
+                        NeutronSubnet_IPAllocationPool.convert(ans)) {\r
                     ans = pool.getPoolStart();\r
+                }\r
         }\r
         return ans;\r
     }\r
@@ -349,8 +368,9 @@ public class NeutronSubnet {
                 if (pool.contains(ipAddress)) {\r
                     List<NeutronSubnet_IPAllocationPool> pools = pool.splitPool(ipAddress);\r
                     newList.addAll(pools);\r
-                } else\r
+                } else {\r
                     newList.add(pool);\r
+                }\r
             }\r
         }\r
         allocationPools = newList;\r
@@ -372,20 +392,25 @@ public class NeutronSubnet {
             NeutronSubnet_IPAllocationPool pool = i.next();\r
             long lIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolStart());\r
             long hIP = NeutronSubnet_IPAllocationPool.convert(pool.getPoolEnd());\r
-            if (sIP+1 == lIP)\r
+            if (sIP+1 == lIP) {\r
                 hPool = pool;\r
-            if (sIP-1 == hIP)\r
+            }\r
+            if (sIP-1 == hIP) {\r
                 lPool = pool;\r
+            }\r
         }\r
         //if (lPool == NULL and hPool == NULL) create new pool where low = ip = high\r
-        if (lPool == null && hPool == null)\r
+        if (lPool == null && hPool == null) {\r
             allocationPools.add(new NeutronSubnet_IPAllocationPool(ipAddress,ipAddress));\r
+        }\r
         //if (lPool == NULL and hPool != NULL) change low address of hPool to ipAddr\r
-        if (lPool == null && hPool != null)\r
+        if (lPool == null && hPool != null) {\r
             hPool.setPoolStart(ipAddress);\r
+        }\r
         //if (lPool != NULL and hPool == NULL) change high address of lPool to ipAddr\r
-        if (lPool != null && hPool == null)\r
+        if (lPool != null && hPool == null) {\r
             lPool.setPoolEnd(ipAddress);\r
+        }\r
         //if (lPool != NULL and hPool != NULL) remove lPool and hPool and create new pool\r
         //        where low address = lPool.low address and high address = hPool.high Address\r
         if (lPool != null && hPool != null) {\r
index d2d7a5a..699aee9 100644 (file)
@@ -33,6 +33,7 @@ import org.opendaylight.controller.networkconfig.neutron.INeutronSubnetCRUD;
 import org.opendaylight.controller.networkconfig.neutron.NeutronCRUDInterfaces;\r
 import org.opendaylight.controller.networkconfig.neutron.NeutronSubnet;\r
 import org.opendaylight.controller.northbound.commons.RestMessages;\r
+import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;\r
 import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;\r
 import org.opendaylight.controller.sal.utils.ServiceHelper;\r
 \r
@@ -107,10 +108,11 @@ public class NeutronSubnetsNorthbound {
                     (queryGatewayIP == null || queryGatewayIP.equals(oSS.getGatewayIP())) &&\r
                     (queryEnableDHCP == null || queryEnableDHCP.equals(oSS.getEnableDHCP())) &&\r
                     (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {\r
-                if (fields.size() > 0)\r
+                if (fields.size() > 0) {\r
                     ans.add(extractFields(oSS,fields));\r
-                else\r
+                } else {\r
                     ans.add(oSS);\r
+                }\r
             }\r
         }\r
         //TODO: apply pagination to results\r
@@ -139,15 +141,17 @@ public class NeutronSubnetsNorthbound {
             throw new ServiceUnavailableException("Subnet CRUD Interface "\r
                     + RestMessages.SERVICEUNAVAILABLE.toString());\r
         }\r
-        if (!subnetInterface.subnetExists(subnetUUID))\r
+        if (!subnetInterface.subnetExists(subnetUUID)) {\r
             return Response.status(404).build();\r
+        }\r
         if (fields.size() > 0) {\r
             NeutronSubnet ans = subnetInterface.getSubnet(subnetUUID);\r
             return Response.status(200).entity(\r
                     new NeutronSubnetRequest(extractFields(ans, fields))).build();\r
-        } else\r
+        } else {\r
             return Response.status(200).entity(\r
                     new NeutronSubnetRequest(subnetInterface.getSubnet(subnetUUID))).build();\r
+        }\r
     }\r
 \r
     /**\r
@@ -185,22 +189,29 @@ public class NeutronSubnetsNorthbound {
              *  and that the gateway IP doesn't overlap with the allocation pools\r
              *  *then* add the subnet to the cache\r
              */\r
-            if (subnetInterface.subnetExists(singleton.getID()))\r
+            if (subnetInterface.subnetExists(singleton.getID())) {\r
                 return Response.status(400).build();\r
-            if (!networkInterface.networkExists(singleton.getNetworkUUID()))\r
+            }\r
+            if (!networkInterface.networkExists(singleton.getNetworkUUID())) {\r
                 return Response.status(404).build();\r
-            if (!singleton.isValidCIDR())\r
+            }\r
+            if (!singleton.isValidCIDR()) {\r
                 return Response.status(400).build();\r
-            singleton.initDefaults();\r
-            if (singleton.gatewayIP_Pool_overlap())\r
+            }\r
+            if (!singleton.initDefaults()) {\r
+                throw new InternalServerErrorException("subnet object could not be initialized properly");\r
+            }\r
+            if (singleton.gatewayIP_Pool_overlap()) {\r
                 return Response.status(409).build();\r
+            }\r
             Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
             if (instances != null) {\r
                 for (Object instance : instances) {\r
                     INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
                     int status = service.canCreateSubnet(singleton);\r
-                    if (status < 200 || status > 299)\r
+                    if (status < 200 || status > 299) {\r
                         return Response.status(status).build();\r
+                    }\r
                 }\r
             }\r
             subnetInterface.addSubnet(singleton);\r
@@ -225,24 +236,32 @@ public class NeutronSubnetsNorthbound {
                  *  and that the bulk request doesn't already contain a subnet with this id\r
                  */\r
 \r
-                test.initDefaults();\r
-                if (subnetInterface.subnetExists(test.getID()))\r
+                if (!test.initDefaults()) {\r
+                    throw new InternalServerErrorException("subnet object could not be initialized properly");\r
+                }\r
+                if (subnetInterface.subnetExists(test.getID())) {\r
                     return Response.status(400).build();\r
-                if (testMap.containsKey(test.getID()))\r
+                }\r
+                if (testMap.containsKey(test.getID())) {\r
                     return Response.status(400).build();\r
+                }\r
                 testMap.put(test.getID(), test);\r
-                if (!networkInterface.networkExists(test.getNetworkUUID()))\r
+                if (!networkInterface.networkExists(test.getNetworkUUID())) {\r
                     return Response.status(404).build();\r
-                if (!test.isValidCIDR())\r
+                }\r
+                if (!test.isValidCIDR()) {\r
                     return Response.status(400).build();\r
-                if (test.gatewayIP_Pool_overlap())\r
+                }\r
+                if (test.gatewayIP_Pool_overlap()) {\r
                     return Response.status(409).build();\r
+                }\r
                 if (instances != null) {\r
                     for (Object instance : instances) {\r
                         INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
                         int status = service.canCreateSubnet(test);\r
-                        if (status < 200 || status > 299)\r
+                        if (status < 200 || status > 299) {\r
                             return Response.status(status).build();\r
+                        }\r
                     }\r
                 }\r
             }\r
@@ -292,10 +311,12 @@ public class NeutronSubnetsNorthbound {
         /*\r
          * verify the subnet exists and there is only one delta provided\r
          */\r
-        if (!subnetInterface.subnetExists(subnetUUID))\r
+        if (!subnetInterface.subnetExists(subnetUUID)) {\r
             return Response.status(404).build();\r
-        if (!input.isSingleton())\r
+        }\r
+        if (!input.isSingleton()) {\r
             return Response.status(400).build();\r
+        }\r
         NeutronSubnet delta = input.getSingleton();\r
         NeutronSubnet original = subnetInterface.getSubnet(subnetUUID);\r
 \r
@@ -304,16 +325,18 @@ public class NeutronSubnetsNorthbound {
          */\r
         if (delta.getID() != null || delta.getTenantID() != null ||\r
                 delta.getIpVersion() != null || delta.getCidr() != null ||\r
-                delta.getAllocationPools() != null)\r
+                delta.getAllocationPools() != null) {\r
             return Response.status(400).build();\r
+        }\r
 \r
         Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
         if (instances != null) {\r
             for (Object instance : instances) {\r
                 INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
                 int status = service.canUpdateSubnet(delta, original);\r
-                if (status < 200 || status > 299)\r
+                if (status < 200 || status > 299) {\r
                     return Response.status(status).build();\r
+                }\r
             }\r
         }\r
 \r
@@ -354,18 +377,21 @@ public class NeutronSubnetsNorthbound {
         /*\r
          * verify the subnet exists and it isn't currently in use\r
          */\r
-        if (!subnetInterface.subnetExists(subnetUUID))\r
+        if (!subnetInterface.subnetExists(subnetUUID)) {\r
             return Response.status(404).build();\r
-        if (subnetInterface.subnetInUse(subnetUUID))\r
+        }\r
+        if (subnetInterface.subnetInUse(subnetUUID)) {\r
             return Response.status(409).build();\r
+        }\r
         NeutronSubnet singleton = subnetInterface.getSubnet(subnetUUID);\r
         Object[] instances = ServiceHelper.getGlobalInstances(INeutronSubnetAware.class, this, null);\r
         if (instances != null) {\r
             for (Object instance : instances) {\r
                 INeutronSubnetAware service = (INeutronSubnetAware) instance;\r
                 int status = service.canDeleteSubnet(singleton);\r
-                if (status < 200 || status > 299)\r
+                if (status < 200 || status > 299) {\r
                     return Response.status(status).build();\r
+                }\r
             }\r
         }\r
 \r