Restful API for bgpd
diff --git a/src/main/java/net/floodlightcontroller/restclient/RestClient.java b/src/main/java/net/floodlightcontroller/restclient/RestClient.java
index 07eab45..541b42d 100644
--- a/src/main/java/net/floodlightcontroller/restclient/RestClient.java
+++ b/src/main/java/net/floodlightcontroller/restclient/RestClient.java
@@ -1,10 +1,17 @@
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) {
@@ -16,7 +23,7 @@
URL url = new URL(str);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setRequestMethod("GET");
+ conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
@@ -24,18 +31,68 @@
+ conn.getResponseCode());
}
- /* Disable reading the output from the server for now
- *
- BufferedReader br = new BufferedReader(new InputStreamReader(
- (conn.getInputStream())));
-
- String output;
- System.out.println("Output from Server .... \n");
- while ((output = br.readLine()) != null) {
- System.out.println(output);
- }
- */
+ 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) {
@@ -48,4 +105,38 @@
}
}
+
+
+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();
+
+ }
+}
+
+
}