SDN课程作业总结
SDN 期末作业总结
设计场景
我们采用参考场景一,实现负载均衡,拓扑图及端口示意如下:

演示视频
关键代码
package loadBalance;
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);
if(received-temp<10000){
//下负载均衡流表
// System.out.println(received-temp);
httpRequest(url24,"PUT",8);
httpRequest(url14,"PUT",9);
}else if(received-temp>10000 ){
// System.out.println(received-temp);
httpRequest(url21,"PUT",2);
httpRequest(url12,"PUT",4);
}
temp = received;
}
// jsonObject = httpRequest(url1,"PUT");
}
}
期末分工
主要负责代码编写,参与小组讨论。
课程总结
上了这门课程之后,使得我对sdn新构型网络有了更进一步的理解,通过控制面板和转发面的分离,更好的是的程序能够很好的控制网络各项功能,这是在传统网络上的一个大的飞跃。并且学会了简单的odl控制器下发流标,实现负载均衡的思路。相信对以后会很有帮助。
SDN课程作业总结的更多相关文章
- 2017秋 FZU SDN 课程作业汇总
课程: SDN课程上机作业:SDN上机作业 参考作业: deepYY SDN作业: SDN作业 faberry的博客:faberry peiqiaoWang的博客:peiqiaoWang 相关博客汇总 ...
- 17秋 SDN课程 第五次上机作业
17秋 SDN课程 第五次上机作业 Project:https://github.com/Wasdns/new_balance Slide is available at https://github ...
- SDN课程阅读作业(2)
SDN课程阅读作业(2) 1.过去20年中可编程网络的发展可以分为几个阶段?每个阶段的贡献是什么? 答:可编程网络的发展分为三个阶段. (1)主动网络阶段 贡献: 1.对主动网络的研究开创了可编程网络 ...
- 17秋 SDN课程 第一次作业
SDN第一次作业 你会选择作 网络编程 方向的程序员吗?为什么? 有可能.原因如下: 1.我的研究与网络密切相关: 2.SDN侧重软件实现,自然涉及socket等网络编程知识,属于基本功: 3.市场. ...
- SDN 期末作业验收
前言 SDN 期末作业验收我们是采用的参考场景一,我们在此场景的基础上来做负载均衡,下面是我们搭建的拓扑图 演示视频 https://pan.baidu.com/s/1htkKLPM 负载均衡程序 相 ...
- Web编程基础--HTML、CSS、JavaScript 学习之课程作业“仿360极速浏览器新标签页”
Web编程基础--HTML.CSS.JavaScript 学习之课程作业"仿360极速浏览器新标签页" 背景: 作为一个中专网站建设出身,之前总是做静态的HTML+CSS+DIV没 ...
- 课程作业01:模仿JavaAppArguments.java示例,编写一个程序,此程序从命令行接收多个数字,求和之后输出结果。
1.设计思想: 首先是从JavaAppArguments.java示例开始,此示例已打印参数,定义数字 之和和作为存储单位的整型,然后将输入参数的字符串转化为整型,之后求和即可. 2.程序流程图: 3 ...
- coursea机器学习课程作业
coursea机器学习课程作业 一 关于此课程 课程地址 图片来自网络 1.官网课程地址传送 2.如果访问官网速度较慢可以上 B站课程地址 机器学习是一门研究在非特定编程条件下让计算机采取行动的学科. ...
- THE LAST ONE!! 2017《面向对象程序设计》课程作业八
THE LAST ONE!! 2017<面向对象程序设计>课程作业八 031602230 卢恺翔 GitHub传送门 题目描述 1.时间匆匆,本学期的博客作业就要结束了,是否有点不舍,是否 ...
随机推荐
- Autofac和nopcommerce中的Autofac, 还有反射
随笔分类 - Ioc Ioc容器Autofac系列(3)-- 三种注册组件的方式 摘要: 简单来说,所谓注册组件,就是注册类并映射为接口,然后根据接口获取对应类,Autofac将被注册的类称为组件. ...
- Thinkphp 图片上传
案例:广告的增删改查 步骤: 1引用 js 2 填写 input type=" file" 的id 3 填写 url 4后台保存地址 5前台成功后的处理 广告添加页 <div ...
- Java“毒丸”使用示例,实现取消任务
一.简介 在Java并发编程中,“毒丸”指的是将一个对象放在队列当中,当得到这个对象的时候立即停止执行 下面是一个使用“毒丸”来取消任务的一个示例 如图所示,我们假设一个任务从开始到结束需要经历4个步 ...
- 自己写一个java的mvc框架吧(五)
自己写一个mvc框架吧(五) 给框架添加注解的支持 一段废话 上一章本来是说这一章要写视图处理的部分,但是由于我在测试代码的时候需要频繁的修改配置文件,太麻烦了.所以这一章先把支持注解的功能加上,这样 ...
- Java基础——iO(三)
一.管道流 演示:PipedInputStream , PipedOutputStream 注意:管道流本身就不建议在一个线程中使用,这是因为向输出流中写的数据,都会存到输入流内部的一个1024字节 ...
- 小希的迷宫(hdu1272)并查集(有点坑)
纪念我死去的脑细胞们 ..... 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- Android-加载图片避免OOM
http://blog.csdn.net/guolin_blog/article/details/9316683 高效加载大图片 我们在编写Android程序的时候经常要用到许多图片,不同图片总是会有 ...
- 【 js 基础 】【读书笔记】Javascript “继承”
是时候写一写 “继承”了,为什么加引号,因为当你阅读完这篇文章,你会知道,说是 继承 其实是不准确的. 一.类1.传统的面向类的语言中的类:类/继承 描述了一种代码的组织结构形式.举个例子:“汽车”可 ...
- 关于python操作带有中文文件名报错的解决办法
python代码的编码格式 #coding:utf-8 在操作文件时,如果文件名带有中文,则需要将文件路径以Unicode的编码格式进行操作 具体的方式如下 path = "你的 ...
- LOJ2522:[FJOI2018]邮递员问题(乱搞)
传送门 乱搞. 可以发现如果起点在左边界,终点在右边界的时候上下走的点一定是连续的(可能吧) 那么可以设 \(f_{i,j,0/1}\) 表示当前上面到 \(i\),下面到 \(j\),当前在上面/下 ...