blob: 541b42d001e5b9414336dfc1746a0cc691192b71 [file] [log] [blame]
pingping-lina2cbfad2013-03-07 08:39:21 +08001package net.floodlightcontroller.restclient;
2
pingping-line2a09ca2013-03-23 09:33:58 +08003import java.io.BufferedReader;
pingping-lina2cbfad2013-03-07 08:39:21 +08004import java.io.IOException;
pingping-line2a09ca2013-03-23 09:33:58 +08005import java.io.InputStreamReader;
pingping-lina2cbfad2013-03-07 08:39:21 +08006import java.net.HttpURLConnection;
7import java.net.MalformedURLException;
8import java.net.URL;
9
pingping-line2a09ca2013-03-23 09:33:58 +080010import net.sf.json.JSONArray;
11import net.sf.json.JSONObject;
12import net.sf.json.JSONSerializer;
13
14
pingping-lina2cbfad2013-03-07 08:39:21 +080015public class RestClient {
16
17 public static void get (String str) {
18
19 if (str == null)
20 return;
21
22 try {
23
24 URL url = new URL(str);
25 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
pingping-line2a09ca2013-03-23 09:33:58 +080026 conn.setRequestMethod("GET");
pingping-lina2cbfad2013-03-07 08:39:21 +080027 conn.setRequestProperty("Accept", "application/json");
28
29 if (conn.getResponseCode() != 200) {
30 throw new RuntimeException("Failed : HTTP error code : "
31 + conn.getResponseCode());
32 }
33
pingping-line2a09ca2013-03-23 09:33:58 +080034 if (conn.getContentType().equals("application/json"))
35 { }else{
36 System.out.print("The content received is not json format!");
37 }
pingping-lina2cbfad2013-03-07 08:39:21 +080038
pingping-line2a09ca2013-03-23 09:33:58 +080039 BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
40 StringBuffer res = new StringBuffer();
41 String line;
42 while ((line = br.readLine()) != null) {
43 res.append(line);
44 }
45
46 String res2=res.toString().replaceAll("\"", "'");
47 JSONObject jsonObj = (JSONObject) JSONSerializer.toJSON(res2);
48 JSONArray rib_json_array = jsonObj.getJSONArray("rib");
49 String router_id = jsonObj.getString("router-id");
50
51 int size = rib_json_array.size();
52 System.out.print("size:"+size+"\n");
53 for (int j = 0; j < size; j++) {
54 JSONObject second_json_object = rib_json_array.getJSONObject(j);
55 String prefix = second_json_object.getString("prefix");
56 String nexthop = second_json_object.getString("nexthop");
57
58 //insert each rib entry into the local rib;
59 RestClient.post("http://127.0.0.1:8090/wm/bgp/"+router_id+"/"+prefix+"/"+nexthop);
60
61
62
63 }
64 br.close();
65 conn.disconnect();
66
67 } catch (MalformedURLException e) {
68
69 e.printStackTrace();
70
71 } catch (IOException e) {
72
73 e.printStackTrace();
74
75 }
76 }
77
78public static void post (String str) {
79
80 if (str == null)
81 return;
82
83 try {
84
85 URL url = new URL(str);
86 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
87 conn.setDoOutput(true);
88 conn.setRequestMethod("POST");
89 conn.setRequestProperty("Content-Type", "application/json");
90
91 if (conn.getResponseCode() != 200) {
92 throw new RuntimeException("Failed : HTTP error code : "
93 + conn.getResponseCode());
94 }
95
pingping-lina2cbfad2013-03-07 08:39:21 +080096 conn.disconnect();
97
98 } catch (MalformedURLException e) {
99
100 e.printStackTrace();
101
102 } catch (IOException e) {
103
104 e.printStackTrace();
105
106 }
107 }
pingping-line2a09ca2013-03-23 09:33:58 +0800108
109
110public static void delete (String str) {
111
112 if (str == null)
113 return;
114
115 try {
116
117 URL url = new URL(str);
118 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
119 conn.setRequestMethod("DELETE");
120 conn.setRequestProperty("Accept", "application/json");
121
122
123 if (conn.getResponseCode() != 200) {
124 throw new RuntimeException("Failed : HTTP error code : "
125 + conn.getResponseCode());
126 }
127
128 conn.disconnect();
129
130 } catch (MalformedURLException e) {
131
132 e.printStackTrace();
133
134 } catch (IOException e) {
135
136 e.printStackTrace();
137
138 }
139}
140
141
pingping-lina2cbfad2013-03-07 08:39:21 +0800142}