參考場景一java
搭建的拓撲圖:
node
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; import net.sf.json.*; public class Main { public static JSONObject jsonObject = null; public static JSONObject[] jsonArray = new JSONObject[100]; static String url24= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0"; static String url14= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1"; static String url21= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0"; static String url12= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1"; public static JSONObject httpRequest(String requestUrl, String requestMethod,int index) { StringBuffer buffer = new StringBuffer(); try { URL url = new URL(requestUrl); // http協議傳輸 HttpURLConnection httpUrlConn = (HttpURLConnection) url.openConnection(); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); String userPassword = "admin" + ":" + "admin"; String encoding = Base64.getEncoder().encodeToString((userPassword).getBytes()); httpUrlConn.setRequestProperty("Authorization", "Basic " + encoding); httpUrlConn.setRequestProperty("Connection", "Keep-Alive"); // 設置維持長鏈接 httpUrlConn.setRequestProperty("Charset", "UTF-8");// 設置文件字符集: // 設置請求方式(GET/POST) httpUrlConn.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) { httpUrlConn.connect(); // 將返回的輸入流轉換成字符串 InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); // 釋放資源 inputStream.close(); inputStream = null; httpUrlConn.disconnect(); jsonObject = JSONObject.fromObject(buffer.toString()); // System.out.println(buffer.toString()); }else if("PUT".equalsIgnoreCase(requestMethod)){ byte[] data = (jsonArray[index].toString()).getBytes();//轉換爲字節數組 httpUrlConn.setRequestProperty("Content-Length", String.valueOf(data.length));// 設置文件長度 httpUrlConn.setRequestProperty("Content-Type", "application/json;charset=utf-8"); // 開始鏈接請求 httpUrlConn.connect(); OutputStream out = httpUrlConn.getOutputStream(); // 寫入請求的字符串 out.write((jsonArray[index].toString()).getBytes()); out.flush(); out.close(); if (httpUrlConn.getResponseCode() == 200) { System.out.println("發送成功"); } }else if("DELETE".equalsIgnoreCase(requestMethod)){ } } catch (Exception e) { e.printStackTrace(); } return jsonObject; } public static void init() throws IOException{ String s = null; int i = 0; try { BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("DATA.txt"),"UTF-8")); while((s = br.readLine())!=null){ jsonArray[i] = JSONObject.fromObject(s); i++; } String url31= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/0"; String url32= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/1"; String url11= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/0"; String url22= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/1"; String url13= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/2"; String url23= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/2"; String url33= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/2"; httpRequest(url31,"PUT",0); httpRequest(url32,"PUT",1); httpRequest(url21,"PUT",2); httpRequest(url11,"PUT",3); httpRequest(url12,"PUT",4); httpRequest(url22,"PUT",5); httpRequest(url13,"PUT",6); httpRequest(url23,"PUT",7); httpRequest(url33,"PUT",10); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static int getReceived(String url2){ jsonObject = httpRequest(url2,"GET",0); JSONArray j1 = (JSONArray) jsonObject.get("node-connector"); JSONObject j2 = (JSONObject) j1.get(0); JSONObject j3 = (JSONObject) j2.get("opendaylight-port-statistics:flow-capable-node-connector-statistics"); JSONObject j4 = (JSONObject)j3.get("bytes"); int received = (int) j4.get("received"); return received; } public static void main(String[] args) { System.out.println("-------------------------------------------------"); try { init(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String s = ""; String url = "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:3/flow-node-inventory:table/0/flow/1"; String url1= "http://172.17.172.244:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:2/flow-node-inventory:table/0/flow/0"; String url2 = "http://172.17.172.244:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:2/node-connector/openflow:2:2"; // jsonObject = httpRequest(url2,"GET",0); // System.out.println(jsonObject.toString()); int received ; int temp = 0; while(true){ received = getReceived(url2); try { Thread.sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(received-temp); //這裏咱們去判斷S2交換機經過包的增加數量來選擇路徑,實現負載均衡 if(received-temp<10000){ //下負載均衡流表 httpRequest(url24,"PUT",8); httpRequest(url14,"PUT",9); }else if(received-temp>10000 ){ httpRequest(url21,"PUT",2); httpRequest(url12,"PUT",4); } temp = received; } // jsonObject = httpRequest(url1,"PUT"); } }
https://pan.baidu.com/s/1htkKLPMpython
在最後的此次做業中主要負責查找相關的資料,以及輔助部分代碼的編寫json
這學期的SDN這門課對我來講,讓我對如今網絡前沿的發展方向有了一個大致的認識。在平時的上課與做業中學會了創建結構比較簡單的拓撲,以及用python模仿編寫的腳原本創建拓撲圖。另外,還學會了利用ODL下發流表,不得不說我從中收穫了不少。數組