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