Cleaned up BgpRoute module code
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java
index 47f3d1a..d4ef11d 100644
--- a/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRoute.java
@@ -1,31 +1,21 @@
package net.floodlightcontroller.bgproute;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.Collection;
-import java.util.Map;
+import java.net.UnknownHostException;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.Map;
+import net.floodlightcontroller.core.IFloodlightProviderService;
import net.floodlightcontroller.core.module.FloodlightModuleContext;
import net.floodlightcontroller.core.module.FloodlightModuleException;
import net.floodlightcontroller.core.module.IFloodlightModule;
import net.floodlightcontroller.core.module.IFloodlightService;
-import net.floodlightcontroller.core.IFloodlightProviderService;
-
+import net.floodlightcontroller.linkdiscovery.ILinkDiscovery;
import net.floodlightcontroller.linkdiscovery.ILinkDiscovery.LDUpdate;
import net.floodlightcontroller.restserver.IRestApiService;
import net.floodlightcontroller.topology.ITopologyListener;
import net.floodlightcontroller.topology.ITopologyService;
-import net.floodlightcontroller.restclient.RestClient;
-
-import net.floodlightcontroller.linkdiscovery.ILinkDiscovery;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
@@ -38,33 +28,34 @@
protected static Logger log = LoggerFactory.getLogger(BgpRoute.class);
protected IFloodlightProviderService floodlightProvider;
+ protected IRestApiService restApi;
protected ITopologyService topology;
protected static Ptree ptree;
- protected static String BGPdRestIp;
- protected static String RouterId;
-
+ protected String bgpdRestIp;
+ protected String routerId;
@Override
public Collection<Class<? extends IFloodlightService>> getModuleServices() {
- Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
+ Collection<Class<? extends IFloodlightService>> l
+ = new ArrayList<Class<? extends IFloodlightService>>();
l.add(IBgpRouteService.class);
return l;
}
@Override
public Map<Class<? extends IFloodlightService>, IFloodlightService> getServiceImpls() {
- Map<Class<? extends IFloodlightService>, IFloodlightService> m = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
+ Map<Class<? extends IFloodlightService>, IFloodlightService> m
+ = new HashMap<Class<? extends IFloodlightService>, IFloodlightService>();
m.put(IBgpRouteService.class, this);
return m;
}
- protected IRestApiService restApi;
-
@Override
public Collection<Class<? extends IFloodlightService>> getModuleDependencies() {
- Collection<Class<? extends IFloodlightService>> l = new ArrayList<Class<? extends IFloodlightService>>();
+ Collection<Class<? extends IFloodlightService>> l
+ = new ArrayList<Class<? extends IFloodlightService>>();
l.add(IFloodlightProviderService.class);
l.add(ITopologyService.class);
l.add(IBgpRouteService.class);
@@ -82,24 +73,43 @@
restApi = context.getServiceImpl(IRestApiService.class);
topology = context.getServiceImpl(ITopologyService.class);
+ //Read in config values
+ bgpdRestIp = context.getConfigParams(this).get("BgpdRestIp");
+ if (bgpdRestIp == null){
+ log.error("BgpdRestIp property not found in config file");
+ System.exit(1);
+ }
+ else {
+ log.info("BgpdRestIp set to {}", bgpdRestIp);
+ }
+
+ routerId = context.getConfigParams(this).get("RouterId");
+ if (routerId == null){
+ log.error("RouterId property not found in config file");
+ System.exit(1);
+ }
+ else {
+ log.info("RouterId set to {}", routerId);
+ }
// Test.
//test();
-
}
public Ptree getPtree() {
return ptree;
}
- public void clearPtree() {
- ptree = null;
- ptree = new Ptree(32);
-
+
+ public void clearPtree() {
+ //ptree = null;
+ ptree = new Ptree(32);
}
+
public String getBGPdRestIp() {
- return BGPdRestIp;
+ return bgpdRestIp;
}
+
public String getRouterId() {
- return RouterId;
+ return routerId;
}
// Return nexthop address as byte array.
@@ -114,17 +124,20 @@
log.debug("lookupRib: ptree node null");
return null;
}
+
if (node.rib == null) {
log.debug("lookupRib: ptree rib null");
return null;
}
+
ptree.delReference(node);
return node.rib;
}
+ //TODO looks like this should be a unit test
@SuppressWarnings("unused")
- private void test() {
+ private void test() throws UnknownHostException {
System.out.println("Here it is");
Prefix p = new Prefix("128.0.0.0", 8);
Prefix q = new Prefix("8.0.0.0", 8);
@@ -179,115 +192,63 @@
}
+ private void retrieveRib(){
+ String url = "http://" + bgpdRestIp + "/wm/bgp/" + routerId;
+ String response = RestClient.get(url);
+
+ if (response.equals("")){
+ return;
+ }
+
+ response = response.replaceAll("\"", "'");
+ JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(response);
+ JSONArray rib_json_array = jsonObj.getJSONArray("rib");
+ String router_id = jsonObj.getString("router-id");
+
+ int size = rib_json_array.size();
+
+ log.info("Retrived RIB of {} entries from BGPd", size);
+
+ for (int j = 0; j < size; j++) {
+ JSONObject second_json_object = rib_json_array.getJSONObject(j);
+ String prefix = second_json_object.getString("prefix");
+ String nexthop = second_json_object.getString("nexthop");
+
+ //insert each rib entry into the local rib;
+ String[] substring = prefix.split("/");
+ String prefix1 = substring[0];
+ String mask1 = substring[1];
+
+ Prefix p;
+ try {
+ p = new Prefix(prefix1, Integer.valueOf(mask1));
+ } catch (NumberFormatException e) {
+ log.warn("Wrong mask format in RIB JSON: {}", mask1);
+ continue;
+ } catch (UnknownHostException e1) {
+ log.warn("Wrong prefix format in RIB JSON: {}", prefix1);
+ continue;
+ }
+
+ PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
+ Rib rib = new Rib(router_id, nexthop, p.masklen);
+
+ if (node.rib != null) {
+ node.rib = null;
+ ptree.delReference(node);
+ }
+
+ node.rib = rib;
+ }
+ }
+
@Override
public void startUp(FloodlightModuleContext context) {
restApi.addRestletRoutable(new BgpRouteWebRoutable());
- topology.addListener((ITopologyListener) this);
+ topology.addListener(this);
- // get the BGPdRestIp and RouterId from transit-route-pusher.py
- File file = new File("/home/ubuntu/sdn/transit-route-pusher.py");
-
-
- try{
- BufferedReader input = new BufferedReader (new FileReader(file));
- String text;
- int is_BGPdRestIp=0;
- int is_RouterId=0;
-
- while((text = input.readLine()) != null && (is_BGPdRestIp == 0) || (is_RouterId == 0) ){
-
- if(is_BGPdRestIp == 1 && is_RouterId ==1)
- {break;}
-
- if(is_BGPdRestIp == 0 && text.contains("BGPdRestIp") ){
- String[] temp = text.split("\"");
- BGPdRestIp = temp[1];
- is_BGPdRestIp = 1;
-
-
- }else if (is_RouterId == 0 && text.contains("RouterId") ){
-
- String[] temp = text.split("\"");
- RouterId = temp[1];
- is_RouterId = 1;
-
-
- }
-
- }
-
-
- } catch(Exception e){
- e.printStackTrace();
- }
-
-
- // automatically get the rib from bgpd at the ONOS initiation process.
- String dest=RouterId;
- String str="http://"+BGPdRestIp+"/wm/bgp/"+dest;
-
-
- try {
-
- URL url = new URL(str);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setRequestProperty("Accept", "application/json");
-
- if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + conn.getResponseCode());
- }
-
- BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
- StringBuffer res = new StringBuffer();
- String line;
- while ((line = br.readLine()) != null) {
- res.append(line);
- }
-
- String res2=res.toString().replaceAll("\"", "'");
- JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(res2);
- JSONArray rib_json_array = jsonObj.getJSONArray("rib");
- String router_id = jsonObj.getString("router-id");
-
- int size = rib_json_array.size();
- System.out.print("size:"+size+"\n");
- for (int j = 0; j < size; j++) {
- JSONObject second_json_object = rib_json_array.getJSONObject(j);
- String prefix = second_json_object.getString("prefix");
- String nexthop = second_json_object.getString("nexthop");
-
- //insert each rib entry into the local rib;
- String[] substring= prefix.split("/");
- String prefix1=substring[0];
- String mask1=substring[1];
-
- Prefix p = new Prefix(prefix1, Integer.valueOf(mask1));
- PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
- Rib rib = new Rib(router_id, nexthop, p.masklen);
-
- if (node.rib != null) {
- node.rib = null;
- ptree.delReference(node);
- }
- node.rib = rib;
-
- }
- br.close();
- conn.disconnect();
-
- } catch (MalformedURLException e) {
-
- e.printStackTrace();
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- }
-
-
+ //Retrieve the RIB from BGPd during startup
+ retrieveRib();
}
@Override
@@ -307,7 +268,7 @@
log.info ("received topo change" + changelog);
if (change) {
- RestClient.get ("http://localhost:5000/topo_change");
+ //RestClient.get ("http://localhost:5000/topo_change");
}
}
}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java
index 28d9621..fa9d577 100644
--- a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResource.java
@@ -1,193 +1,210 @@
package net.floodlightcontroller.bgproute;
+import java.net.UnknownHostException;
+
+import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
-import org.restlet.resource.Delete;
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import net.floodlightcontroller.restclient.RestClient;
-import java.io.UnsupportedEncodingException;
-import java.nio.ByteBuffer;
public class BgpRouteResource extends ServerResource {
-
- protected static Logger log = LoggerFactory
- .getLogger(BgpRouteResource.class);
-
+
+ protected static Logger log = LoggerFactory.getLogger(BgpRouteResource.class);
+
private String addrToString(byte [] addr) {
- String str = "";
-
+ String str = "";
+
for (int i = 0; i < 4; i++) {
int val = (addr[i] & 0xff);
str += val;
if (i != 3)
str += ".";
}
-
+
return str;
}
-
- @SuppressWarnings("unused")
+
+ //@SuppressWarnings("unused")
@Get
- public String get(String fmJson) {
- String linpp=fmJson;
- String dest = (String) getRequestAttributes().get("dest");
- String output = "";
- IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
- get(IBgpRouteService.class.getCanonicalName());
+ public String get(String fmJson) {
+ //String linpp=fmJson;
+ String dest = (String) getRequestAttributes().get("dest");
+ String output = "";
+ IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
+ get(IBgpRouteService.class.getCanonicalName());
+
+ if (dest != null) {
+ //TODO this code path looks like its never used (except maybe for debugging)
+ //Needs to be changed to use the new RestClient.get().
- if (dest != null) {
- Prefix p = new Prefix(dest, 32);
- if (p == null) {
- return "[GET]: dest address format is wrong";
- }
-
- // 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;
-
-
-
- RestClient.get(url);
- output="Get rib from bgpd finished!\n";
- return output;
- } else {
- Ptree ptree = bgpRoute.getPtree();
- output += "{\n \"rib\": [\n";
- boolean printed = false;
- for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
- if (node.rib == null) {
- continue;
- }
- if (printed == true) {
- output += ",\n";
- }
- output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
- output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
- printed = true;
- }
- //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
- output += "\n ]\n}\n";
-
- }
+ //Prefix p;
+ //try {
+ // p = new Prefix(dest, 32);
+ //} catch (UnknownHostException e) {
+ //if (p == null) {
+ // return "[GET]: dest address format is wrong";
+ //}
+
+ // 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;
+
+ RestClient.get(url);
+
+ output="Get rib from bgpd finished!\n";
return output;
+ }
+ else {
+ Ptree ptree = bgpRoute.getPtree();
+ output += "{\n \"rib\": [\n";
+ boolean printed = false;
+
+ for (PtreeNode node = ptree.begin(); node != null; node = ptree.next(node)) {
+ if (node.rib == null) {
+ continue;
+ }
+ if (printed == true) {
+ output += ",\n";
+ }
+ output += " {\"prefix\": \"" + addrToString(node.key) + "/" + node.keyBits +"\", ";
+ output += "\"nexthop\": \"" + addrToString(node.rib.nextHop.getAddress()) +"\"}";
+ printed = true;
+ }
+ //output += "{\"router_id\": \"" + addrToString(node.rib.routerId.getAddress()) +"\"}\n";
+ output += "\n ]\n}\n";
}
+
+ return output;
+ }
- public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException
- {
- return ByteBuffer.wrap(value.getBytes("UTF-8"));
- }
+ //unused?
+ /*
+ public static ByteBuffer toByteBuffer(String value) throws UnsupportedEncodingException {
+ return ByteBuffer.wrap(value.getBytes("UTF-8"));
+ }
+ */
-public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException
- {
- byte[] bytes = new byte[buffer.remaining()];
- buffer.get(bytes);
- return new String(bytes, "UTF-8");
-
- }
+ //unused?
+ /*
+ public static String toString(ByteBuffer buffer) throws UnsupportedEncodingException {
+ byte[] bytes = new byte[buffer.remaining()];
+ buffer.get(bytes);
+ return new String(bytes, "UTF-8");
+ }
+ */
-
@Post
public String store(String fmJson) {
- IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
- get(IBgpRouteService.class.getCanonicalName());
-
- Ptree ptree = bgpRoute.getPtree();
-
- String router_id = (String) getRequestAttributes().get("routerid");
+ IBgpRouteService bgpRoute = (IBgpRouteService) getContext().getAttributes().
+ get(IBgpRouteService.class.getCanonicalName());
+
+ Ptree ptree = bgpRoute.getPtree();
+
+ String routerId = (String) getRequestAttributes().get("routerid");
String prefix = (String) getRequestAttributes().get("prefix");
String mask = (String) getRequestAttributes().get("mask");
String nexthop = (String) getRequestAttributes().get("nexthop");
String capability = (String) getRequestAttributes().get("capability");
-
-
+
String reply = "";
-
+
if (capability == null) {
-
// this is a prefix add
- Prefix p = new Prefix(prefix, Integer.valueOf(mask));
+ Prefix p;
+ try {
+ p = new Prefix(prefix, Integer.valueOf(mask));
+ } catch (NumberFormatException e) {
+ reply = "[POST: mask format is wrong]";
+ log.info(reply);
+ return reply + "\n";
+ } catch (UnknownHostException e1) {
+ reply = "[POST: prefix format is wrong]";
+ log.info(reply);
+ return reply + "\n";
+ }
+
PtreeNode node = ptree.acquire(p.getAddress(), p.masklen);
- Rib rib = new Rib(router_id, nexthop, p.masklen);
+ Rib rib = new Rib(routerId, nexthop, p.masklen);
if (node.rib != null) {
node.rib = null;
ptree.delReference(node);
}
node.rib = rib;
-
+
reply = "[POST: " + prefix + "/" + mask + ":" + nexthop + "]";
log.info(reply);
-
-
- }else if(capability.equals("1")){
- reply = "[POST-capability: " + capability + "]\n";
- log.info(reply);
- // to store the number in the top node of the Ptree
-
- }else{
- reply = "[POST-capability: " + capability + "]\n";
- log.info(reply);
- // to store the number in the top node of the Ptree
-
}
-
-
+ else if(capability.equals("1")) {
+ reply = "[POST-capability: " + capability + "]\n";
+ log.info(reply);
+ // to store the number in the top node of the Ptree
+ }
+ else {
+ reply = "[POST-capability: " + capability + "]\n";
+ log.info(reply);
+ // to store the number in the top node of the Ptree
+ }
+
return reply + "\n";
-
-
}
-
+
@Delete
public String delete(String fmJson) {
IBgpRouteService bgpRoute = (IBgpRouteService)getContext().getAttributes().
- get(IBgpRouteService.class.getCanonicalName());
+ get(IBgpRouteService.class.getCanonicalName());
- Ptree ptree = bgpRoute.getPtree();
-
+ Ptree ptree = bgpRoute.getPtree();
+
String routerId = (String) getRequestAttributes().get("routerid");
String prefix = (String) getRequestAttributes().get("prefix");
String mask = (String) getRequestAttributes().get("mask");
String nextHop = (String) getRequestAttributes().get("nexthop");
String capability = (String) getRequestAttributes().get("capability");
-
+
String reply = "";
-
+
if (capability == null) {
- // this is a prefix delete
- Prefix p = new Prefix(prefix, Integer.valueOf(mask));
-
- PtreeNode node = ptree.lookup(p.getAddress(), p.masklen);
-
- Rib r = new Rib(routerId, nextHop, p.masklen);
-
- if (node != null && node.rib != null) {
-
- if (r.equals(node.rib)) {
-
- node.rib = null;
- ptree.delReference(node);
- }
- }
-
-
- reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
-
- }else {
-
+ // this is a prefix delete
+ Prefix p;
+ try {
+ p = new Prefix(prefix, Integer.valueOf(mask));
+ } catch (NumberFormatException e) {
+ reply = "[DELE: mask format is wrong]";
+ log.info(reply);
+ return reply + "\n";
+ } catch (UnknownHostException e1) {
+ reply = "[DELE: prefix format is wrong]";
+ log.info(reply);
+ return reply + "\n";
+ }
+
+ PtreeNode node = ptree.lookup(p.getAddress(), p.masklen);
+
+ Rib r = new Rib(routerId, nextHop, p.masklen);
+
+ if (node != null && node.rib != null) {
+ if (r.equals(node.rib)) {
+ node.rib = null;
+ ptree.delReference(node);
+ }
+ }
+
+ reply =reply + "[DELE: " + prefix + "/" + mask + ":" + nextHop + "]";
+ }
+ else {
// clear the local rib: Ptree
bgpRoute.clearPtree();
reply = "[DELE-capability: " + capability + "; The local Rib is cleared!]\n";
-
-
+
// to store the number in the top node of the Ptree
-
- }
+ }
+
log.info(reply);
-
return reply + "\n";
}
}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResourceSynch.java b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResourceSynch.java
index d0c337a..ff7aea6 100644
--- a/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResourceSynch.java
+++ b/src/main/java/net/floodlightcontroller/bgproute/BgpRouteResourceSynch.java
@@ -6,7 +6,6 @@
import org.restlet.resource.ServerResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import net.floodlightcontroller.restclient.RestClient;
public class BgpRouteResourceSynch extends ServerResource {
@@ -29,7 +28,7 @@
String BGPdRestIp = bgpRoute.getBGPdRestIp();
- //BGPdRestIp includes port number, such as 1.1.1.1:8080
+ //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();}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java b/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java
index a6025ef..35bc3d4 100644
--- a/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java
+++ b/src/main/java/net/floodlightcontroller/bgproute/IBgpRouteService.java
@@ -4,15 +4,13 @@
public interface IBgpRouteService extends IFloodlightService {
- public Rib lookupRib(byte[] dest);
-
- public Ptree getPtree();
-
- public String getBGPdRestIp();
-
- public String getRouterId();
-
- public void clearPtree() ;
-
-
+ public Rib lookupRib(byte[] dest);
+
+ public Ptree getPtree();
+
+ public String getBGPdRestIp();
+
+ public String getRouterId();
+
+ public void clearPtree() ;
}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/Prefix.java b/src/main/java/net/floodlightcontroller/bgproute/Prefix.java
index 7ba014b..980646f 100644
--- a/src/main/java/net/floodlightcontroller/bgproute/Prefix.java
+++ b/src/main/java/net/floodlightcontroller/bgproute/Prefix.java
@@ -7,28 +7,27 @@
public int masklen;
protected InetAddress address;
- Prefix(byte[] addr, int masklen) {
- try {
- address = InetAddress.getByAddress(addr);
- } catch (UnknownHostException e) {
- System.out.println("InetAddress exception");
- return;
- }
+ public Prefix(byte[] addr, int masklen) throws UnknownHostException {
+ //try {
+ address = InetAddress.getByAddress(addr);
+ //} catch (UnknownHostException e) {
+ // System.out.println("InetAddress exception");
+ // return;
+ //}
this.masklen = masklen;
- System.out.println(address.toString() + "/" + masklen);
+ //System.out.println(address.toString() + "/" + masklen);
}
-
- Prefix(String str, int masklen) {
- try {
- address = InetAddress.getByName(str);
- //System.out.println(address.toString());
- } catch (UnknownHostException e) {
- System.out.println("InetAddress exception");
- return;
- }
+
+ public Prefix(String str, int masklen) throws UnknownHostException {
+ //try {
+ address = InetAddress.getByName(str);
+ //} catch (UnknownHostException e) {
+ // System.out.println("InetAddress exception");
+ // return;
+ //}
this.masklen = masklen;
}
-
+
public byte [] getAddress() {
return address.getAddress();
}
diff --git a/src/main/java/net/floodlightcontroller/bgproute/RestClient.java b/src/main/java/net/floodlightcontroller/bgproute/RestClient.java
new file mode 100644
index 0000000..faf7d6e
--- /dev/null
+++ b/src/main/java/net/floodlightcontroller/bgproute/RestClient.java
@@ -0,0 +1,100 @@
+package net.floodlightcontroller.bgproute;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class RestClient {
+ protected static Logger log = LoggerFactory.getLogger(RestClient.class);
+
+ public static String get(String str) {
+ StringBuilder response = new StringBuilder();
+
+ try {
+
+ URL url = new URL(str);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("GET");
+ conn.setRequestProperty("Accept", "application/json");
+
+ if (conn.getResponseCode() != 200) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + conn.getResponseCode());
+ }
+
+ if (!conn.getContentType().equals("application/json")){
+ log.warn("The content received from {} is not json", str);
+ }
+
+ BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
+ String line;
+ while ((line = br.readLine()) != null) {
+ response.append(line);
+ }
+
+ br.close();
+ conn.disconnect();
+
+ } catch (MalformedURLException e) {
+ log.error("Malformed URL for GET request", e);
+ } catch (IOException e) {
+ log.warn("Couldn't connect remote REST server");
+ }
+
+ return response.toString();
+ }
+
+ public static void post (String str) {
+
+ try {
+ URL url = new URL(str);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setDoOutput(true);
+ conn.setRequestMethod("POST");
+ conn.setRequestProperty("Content-Type", "application/json");
+
+ if (conn.getResponseCode() != 200) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + conn.getResponseCode());
+ }
+
+ conn.disconnect();
+
+ } catch (MalformedURLException e) {
+ log.error("Malformed URL for GET request", e);
+ } catch (IOException e) {
+ log.warn("Couldn't connect remote REST server");
+ }
+ }
+
+
+ public static void delete (String str) {
+
+ try {
+ URL url = new URL(str);
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ conn.setRequestMethod("DELETE");
+ conn.setRequestProperty("Accept", "application/json");
+
+
+ if (conn.getResponseCode() != 200) {
+ throw new RuntimeException("Failed : HTTP error code : "
+ + conn.getResponseCode());
+ }
+
+ conn.disconnect();
+
+ } catch (MalformedURLException e) {
+ log.error("Malformed URL for GET request", e);
+ } catch (IOException e) {
+ log.warn("Couldn't connect remote REST server");
+ }
+ }
+}
diff --git a/src/main/java/net/floodlightcontroller/restclient/RestClient.java b/src/main/java/net/floodlightcontroller/restclient/RestClient.java
deleted file mode 100644
index 541b42d..0000000
--- a/src/main/java/net/floodlightcontroller/restclient/RestClient.java
+++ /dev/null
@@ -1,142 +0,0 @@
-package net.floodlightcontroller.restclient;
-
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
-import net.sf.json.JSONSerializer;
-
-
-public class RestClient {
-
- public static void get (String str) {
-
- if (str == null)
- return;
-
- try {
-
- URL url = new URL(str);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
- conn.setRequestProperty("Accept", "application/json");
-
- if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + conn.getResponseCode());
- }
-
- if (conn.getContentType().equals("application/json"))
- { }else{
- System.out.print("The content received is not json format!");
- }
-
- BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
- StringBuffer res = new StringBuffer();
- String line;
- while ((line = br.readLine()) != null) {
- res.append(line);
- }
-
- String res2=res.toString().replaceAll("\"", "'");
- JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(res2);
- JSONArray rib_json_array = jsonObj.getJSONArray("rib");
- String router_id = jsonObj.getString("router-id");
-
- int size = rib_json_array.size();
- System.out.print("size:"+size+"\n");
- for (int j = 0; j < size; j++) {
- JSONObject second_json_object = rib_json_array.getJSONObject(j);
- String prefix = second_json_object.getString("prefix");
- String nexthop = second_json_object.getString("nexthop");
-
- //insert each rib entry into the local rib;
- RestClient.post("http://127.0.0.1:8090/wm/bgp/"+router_id+"/"+prefix+"/"+nexthop);
-
-
-
- }
- br.close();
- conn.disconnect();
-
- } catch (MalformedURLException e) {
-
- e.printStackTrace();
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- }
- }
-
-public static void post (String str) {
-
- if (str == null)
- return;
-
- try {
-
- URL url = new URL(str);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setDoOutput(true);
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Content-Type", "application/json");
-
- if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + conn.getResponseCode());
- }
-
- conn.disconnect();
-
- } catch (MalformedURLException e) {
-
- e.printStackTrace();
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- }
- }
-
-
-public static void delete (String str) {
-
- if (str == null)
- return;
-
- try {
-
- URL url = new URL(str);
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("DELETE");
- conn.setRequestProperty("Accept", "application/json");
-
-
- if (conn.getResponseCode() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + conn.getResponseCode());
- }
-
- conn.disconnect();
-
- } catch (MalformedURLException e) {
-
- e.printStackTrace();
-
- } catch (IOException e) {
-
- e.printStackTrace();
-
- }
-}
-
-
-}