Fixed some PMD issues in the SDN-IP module
Change-Id: Iaf002e70c1a00f0008edef5e77b91579acf146aa
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResource.java b/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResource.java
index 2e14757..2ac3414 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResource.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResource.java
@@ -12,32 +12,19 @@
import org.slf4j.LoggerFactory;
public class BgpRouteResource extends ServerResource {
-
- protected final static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
+ private final static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
@Get
public String get(String fmJson) {
String dest = (String) getRequestAttributes().get("dest");
- String output = "";
- IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
+ StringBuilder output = new StringBuilder(80);
+ IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
+ .getAttributes().
get(IBgpRouteService.class.getCanonicalName());
- if (dest != null) {
- //TODO Needs to be changed to use the new RestClient.get().
-
- // the dest here refers to router-id
- //bgpdRestIp includes port number, such as 1.1.1.1:8080
- String BGPdRestIp = bgpRoute.getBGPdRestIp();
- String url = "http://" + BGPdRestIp + "/wm/bgp/" + dest;
-
- //Doesn't actually do anything with the response
- RestClient.get(url);
-
- output = "Get rib from bgpd finished!\n";
- return output;
- } else {
+ if (dest == null) {
IPatriciaTrie<RibEntry> ptree = bgpRoute.getPtree();
- output += "{\n \"rib\": [\n";
+ output.append("{\n \"rib\": [\n");
boolean printed = false;
synchronized (ptree) {
@@ -45,26 +32,42 @@
while (it.hasNext()) {
IPatriciaTrie.Entry<RibEntry> entry = it.next();
- if (printed == true) {
- output += ",\n";
+ if (printed) {
+ output.append(",\n");
}
- output += " {\"prefix\": \"" + entry.getPrefix() + "\", ";
- output += "\"nexthop\": \"" + entry.getValue().getNextHop().getHostAddress() + "\"}";
+ output.append(" {\"prefix\": \"");
+ output.append(entry.getPrefix());
+ output.append("\", \"nexthop\": \"");
+ output.append(entry.getValue().getNextHop().getHostAddress());
+ output.append("\"}");
printed = true;
}
}
- output += "\n ]\n}\n";
+ output.append("\n ]\n}\n");
+ } else {
+ // TODO Needs to be changed to use the new RestClient.get().
+
+ // the dest here refers to router-id
+ // bgpdRestIp includes port number, such as 1.1.1.1:8080
+ String bgpdRestIp = bgpRoute.getBGPdRestIp();
+ String url = "http://" + bgpdRestIp + "/wm/bgp/" + dest;
+
+ // Doesn't actually do anything with the response
+ RestClient.get(url);
+
+ output.append("Get rib from bgpd finished!\n");
}
- return output;
+ return output.toString();
}
@Post
public String store(String fmJson) {
- IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
+ IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
+ .getAttributes().
get(IBgpRouteService.class.getCanonicalName());
String strSysuptime = (String) getRequestAttributes().get("sysuptime");
@@ -98,13 +101,14 @@
return reply + "\n";
}
- RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime, sequenceNum);
+ RibEntry rib = new RibEntry(routerId, nexthop, sysUpTime,
+ sequenceNum);
bgpRoute.newRibUpdate(new RibUpdate(Operation.UPDATE, p, rib));
reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
log.info(reply);
- } else if (capability.equals("1")) {
+ } else if ("1".equals(capability)) {
reply = "[POST-capability: " + capability + "]\n";
log.info(reply);
// to store the number in the top node of the Ptree
@@ -119,7 +123,8 @@
@Delete
public String delete(String fmJson) {
- IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
+ IBgpRouteService bgpRoute = (IBgpRouteService) getContext()
+ .getAttributes().
get(IBgpRouteService.class.getCanonicalName());
String strSysuptime = (String) getRequestAttributes().get("sysuptime");
@@ -157,11 +162,13 @@
bgpRoute.newRibUpdate(new RibUpdate(Operation.DELETE, p, r));
- reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
+ reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop
+ + "]";
} else {
// clear the local rib: Ptree
bgpRoute.clearPtree();
- reply = "[DELE-capability: " + capability + "; The local RibEntry is cleared!]\n";
+ reply = "[DELE-capability: " + capability
+ + "; The local RibEntry is cleared!]\n";
// to store the number in the top node of the Ptree
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResourceSynch.java b/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResourceSynch.java
index 20fc71b..b38a400 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResourceSynch.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/BgpRouteResourceSynch.java
@@ -1,17 +1,13 @@
package net.onrc.onos.apps.bgproute;
-
import org.restlet.resource.Delete;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
public class BgpRouteResourceSynch extends ServerResource {
-
- protected final static Logger log = LoggerFactory
- .getLogger(BgpRouteResource.class);
+ private final static Logger log = LoggerFactory.getLogger(BgpRouteResourceSynch.class);
@Post
public String store(String fmJson) {
@@ -19,20 +15,16 @@
IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
get(IBgpRouteService.class.getCanonicalName());
- String router_id = (String) getRequestAttributes().get("routerid");
+ String routerId = (String) getRequestAttributes().get("routerid");
String prefix = (String) getRequestAttributes().get("prefix");
String mask = (String) getRequestAttributes().get("mask");
String nexthop = (String) getRequestAttributes().get("nexthop");
- try {
+ String bgpdRestIp = bgpRoute.getBGPdRestIp();
- String BGPdRestIp = bgpRoute.getBGPdRestIp();
-
- //bgpdRestIp includes port number, such as 1.1.1.1:8080
- RestClient.post("http://" + BGPdRestIp + "/wm/bgp/" + router_id + "/" + prefix + "/" + mask + "/" + nexthop);
- } catch (Exception e) {
- e.printStackTrace();
- }
+ // bgpdRestIp includes port number, e.g. 1.1.1.1:8080
+ RestClient.post("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
+ + mask + "/" + nexthop);
String reply = "";
reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "/synch]";
@@ -40,7 +32,6 @@
return reply + "\n";
-
}
@Delete
@@ -54,20 +45,16 @@
String nextHop = (String) getRequestAttributes().get("nexthop");
String reply = "";
- try {
- String BGPdRestIp = bgpRoute.getBGPdRestIp();
- RestClient.delete("http://" + BGPdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/" + mask + "/" + nextHop);
+ String bgpdRestIp = bgpRoute.getBGPdRestIp();
- } catch (Exception e) {
- e.printStackTrace();
- }
+ RestClient.delete("http://" + bgpdRestIp + "/wm/bgp/" + routerId + "/" + prefix + "/"
+ + mask + "/" + nextHop);
reply = reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "/synch]";
log.info(reply);
-
return reply + "\n";
}
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/Interface.java b/src/main/java/net/onrc/onos/apps/bgproute/Interface.java
index 52e9a32..0339401 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/Interface.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/Interface.java
@@ -60,7 +60,7 @@
@Override
public boolean equals(Object other) {
- if (other == null || !(other instanceof Interface)) {
+ if (!(other instanceof Interface)) {
return false;
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/Path.java b/src/main/java/net/onrc/onos/apps/bgproute/Path.java
index 3ecf7c7..f96e0b0 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/Path.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/Path.java
@@ -11,12 +11,12 @@
public class Path {
- private Interface dstInterface;
- private InetAddress dstIpAddress;
- private int numUsers = 0;
+ private final Interface dstInterface;
+ private final InetAddress dstIpAddress;
+ private int numUsers; // initialized to 0
- private List<PushedFlowMod> flowMods = null;
- private boolean permanent = false;
+ private List<PushedFlowMod> flowMods; // initialized to null
+ private boolean permanent; // initialized to false
public Path(Interface dstInterface, InetAddress dstIpAddress) {
this.dstInterface = dstInterface;
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/Prefix.java b/src/main/java/net/onrc/onos/apps/bgproute/Prefix.java
index 5e5ba91..74b6ad9 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/Prefix.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/Prefix.java
@@ -7,7 +7,7 @@
import com.google.common.net.InetAddresses;
public class Prefix {
- private final int MAX_BYTES = 4;
+ private static final int MAX_BYTES = 4;
private final int prefixLength;
private final byte[] address;
@@ -27,7 +27,7 @@
try {
inetAddress = InetAddress.getByAddress(address);
} catch (UnknownHostException e) {
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("Couldn't parse IP address", e);
}
}
@@ -46,7 +46,7 @@
try {
inetAddress = InetAddress.getByAddress(address);
} catch (UnknownHostException e) {
- throw new IllegalArgumentException();
+ throw new IllegalArgumentException("Couldn't parse IP address", e);
}
}
@@ -94,7 +94,7 @@
@Override
public boolean equals(Object other) {
- if (other == null || !(other instanceof Prefix)) {
+ if (!(other instanceof Prefix)) {
return false;
}
@@ -118,17 +118,17 @@
}
public String printAsBits() {
- String result = "";
+ StringBuilder result = new StringBuilder();
for (int i = 0; i < address.length; i++) {
byte b = address[i];
for (int j = 0; j < Byte.SIZE; j++) {
byte mask = (byte) (0x80 >>> j);
- result += ((b & mask) == 0) ? "0" : "1";
+ result.append(((b & mask) == 0) ? "0" : "1");
if (i * Byte.SIZE + j == prefixLength - 1) {
- return result;
+ return result.toString();
}
}
- result += " ";
+ result.append(" ");
}
return result.substring(0, result.length() - 1);
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/PushedFlowMod.java b/src/main/java/net/onrc/onos/apps/bgproute/PushedFlowMod.java
index 612a704..fc13cd3 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/PushedFlowMod.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/PushedFlowMod.java
@@ -9,7 +9,7 @@
* TODO This functionality should be handled by ONOS's flow layer in future.
*/
public class PushedFlowMod {
- private long dpid;
+ private final long dpid;
private OFFlowMod flowMod;
public PushedFlowMod(long dpid, OFFlowMod flowMod) {
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/RestClient.java b/src/main/java/net/onrc/onos/apps/bgproute/RestClient.java
index 5ea4699..3164945 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/RestClient.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/RestClient.java
@@ -11,23 +11,26 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+public final class RestClient {
+ private final static Logger log = LoggerFactory.getLogger(RestClient.class);
-public class RestClient {
- protected final static Logger log = LoggerFactory.getLogger(RestClient.class);
-
+ private RestClient() {
+ // Private constructor to prevent instantiation
+ }
+
public static String get(String str) {
StringBuilder response = new StringBuilder();
try {
-
URL url = new URL(str);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(2 * 1000); //2 seconds
+ conn.setConnectTimeout(2 * 1000); // 2 seconds
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
+ // XXX bad. RestClient API needs to be redesigned
+ throw new IOException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
@@ -47,9 +50,9 @@
} catch (MalformedURLException e) {
log.error("Malformed URL for GET request", e);
} catch (ConnectTimeoutException e) {
- log.warn("Couldn't connect remote REST server");
+ log.warn("Couldn't connect to the remote REST server", e);
} catch (IOException e) {
- log.warn("Couldn't connect remote REST server");
+ log.warn("Couldn't connect to the remote REST server", e);
}
return response.toString();
@@ -65,7 +68,8 @@
conn.setRequestProperty("Content-Type", "application/json");
if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
+ // XXX bad. RestClient API needs to be redesigned
+ throw new IOException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
@@ -74,11 +78,10 @@
} catch (MalformedURLException e) {
log.error("Malformed URL for GET request", e);
} catch (IOException e) {
- log.warn("Couldn't connect remote REST server");
+ log.warn("Couldn't connect to the remote REST server", e);
}
}
-
public static void delete(String str) {
try {
@@ -87,9 +90,9 @@
conn.setRequestMethod("DELETE");
conn.setRequestProperty("Accept", "application/json");
-
if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
+ // XXX bad. RestClient API needs to be redesigned
+ throw new IOException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
@@ -98,7 +101,7 @@
} catch (MalformedURLException e) {
log.error("Malformed URL for GET request", e);
} catch (IOException e) {
- log.warn("Couldn't connect remote REST server");
+ log.warn("Couldn't connect to the remote REST server", e);
}
}
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/RibEntry.java b/src/main/java/net/onrc/onos/apps/bgproute/RibEntry.java
index 4dccd5f..8e4e12e 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/RibEntry.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/RibEntry.java
@@ -60,7 +60,7 @@
@Override
public boolean equals(Object other) {
- if (other == null || !(other instanceof RibEntry)) {
+ if (!(other instanceof RibEntry)) {
return false;
}
diff --git a/src/main/java/net/onrc/onos/apps/bgproute/RibUpdate.java b/src/main/java/net/onrc/onos/apps/bgproute/RibUpdate.java
index 0faa94f..c9d48e7 100644
--- a/src/main/java/net/onrc/onos/apps/bgproute/RibUpdate.java
+++ b/src/main/java/net/onrc/onos/apps/bgproute/RibUpdate.java
@@ -1,15 +1,15 @@
package net.onrc.onos.apps.bgproute;
public class RibUpdate {
+ private final Operation operation;
+ private final Prefix prefix;
+ private final RibEntry ribEntry;
+
public enum Operation {
UPDATE,
DELETE
}
- private final Operation operation;
- private final Prefix prefix;
- private final RibEntry ribEntry;
-
public RibUpdate(Operation operation, Prefix prefix, RibEntry ribEntry) {
this.operation = operation;
this.prefix = prefix;