pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 1 | package net.floodlightcontroller.restclient; |
| 2 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 3 | import java.io.BufferedReader; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 4 | import java.io.IOException; |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 5 | import java.io.InputStreamReader; |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 6 | import java.net.HttpURLConnection; |
| 7 | import java.net.MalformedURLException; |
| 8 | import java.net.URL; |
| 9 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 10 | import net.sf.json.JSONArray; |
| 11 | import net.sf.json.JSONObject; |
| 12 | import net.sf.json.JSONSerializer; |
| 13 | |
| 14 | |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 15 | public 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-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 26 | conn.setRequestMethod("GET"); |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 27 | 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-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 34 | if (conn.getContentType().equals("application/json")) |
| 35 | { }else{ |
| 36 | System.out.print("The content received is not json format!"); |
| 37 | } |
pingping-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 38 | |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 39 | 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 | |
| 78 | public 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-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 96 | conn.disconnect(); |
| 97 | |
| 98 | } catch (MalformedURLException e) { |
| 99 | |
| 100 | e.printStackTrace(); |
| 101 | |
| 102 | } catch (IOException e) { |
| 103 | |
| 104 | e.printStackTrace(); |
| 105 | |
| 106 | } |
| 107 | } |
pingping-lin | e2a09ca | 2013-03-23 09:33:58 +0800 | [diff] [blame] | 108 | |
| 109 | |
| 110 | public 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-lin | a2cbfad | 2013-03-07 08:39:21 +0800 | [diff] [blame] | 142 | } |