(一)基本要求

编写Python程序,调用OpenDaylight的北向接口实现以下功能

(1) 利用Mininet平台搭建下图所示网络拓扑,并连接OpenDaylight;

(2) 下发指令删除s1上的流表数据。

delete.py的代码

点击查看代码
#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
if __name__ == "__main__":
url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/'
headers = {'Content-Type': 'application/json'}
res = requests.delete(url, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)

(3) 下发硬超时流表,实现拓扑内主机h1和h3网络中断20s。

hardtime.py的代码

点击查看代码
import requests
from requests.auth import HTTPBasicAuth if __name__ == "__main__":
url = 'http://127.0.0.1:8181/restconf/config/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/flow/1'
with open("./hardtime.json") as f:
jstr = f.read()
headers = {'Content-Type': 'application/json'}
res = requests.put(url, jstr, headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)

hardtime.json的代码

点击查看代码
{
"flow": [
{
"id": "1",
"match": {
"in-port": "1",
"ethernet-match": {
"ethernet-type": {
"type": "0x0800"
}
},
"ipv4-destination": "10.0.0.3/32"
},
"instructions": {
"instruction": [
{
"order": "0",
"apply-actions": {
"action": [
{
"order": "0",
"drop-action": {}
}
]
}
}
]
},
"flow-name": "flow1",
"priority": "65535",
"hard-timeout": "20",
"cookie": "2",
"table_id": "0"
}
]
}

(4) 获取s1上活动的流表数。

getflow.py的代码

点击查看代码
#!/usr/bin/python
import requests
from requests.auth import HTTPBasicAuth
if __name__ == "__main__":
url = 'http://127.0.0.1:8181/restconf/operational/opendaylight-inventory:nodes/node/openflow:1/flow-node-inventory:table/0/opendaylight-flow-table-statistics:flow-table-statistics'
headers = {'Content-Type': 'application/json'}
res = requests.get(url,headers=headers, auth=HTTPBasicAuth('admin', 'admin'))
print (res.content)

编写Python程序,调用Ryu的北向接口实现以下功能

(1) 实现上述OpenDaylight实验拓扑上相同的硬超时流表下发。

Ryuhardtime.py的代码

点击查看代码
#!/usr/bin/python
import requests if __name__ == "__main__":
url = 'http://127.0.0.1:8080/stats/flowentry/add'
with open("./Ryuhardtime.json") as f:
jstr = f.read()
headers = {'Content-Type': 'application/json'}
res = requests.post(url, jstr, headers=headers)
print (res.content)

Ryuhardtime.json的代码

点击查看代码
{
"dpid": 1,
"cookie": 1,
"cookie_mask": 1,
"table_id": 0,
"hard_timeout": 20,
"priority": 65535,
"flags": 1,
"match":{
"in_port":1,
},
"actions":[ ]
}

启动Ryu控制器



(2) 参考Ryu REST API的文档,基于VLAN实验的网络拓扑,编程实现相同的VLAN配置。

topo.py代码

点击查看代码
from mininet.topo import Topo

class MyTopo(Topo):
def __init__(self):
# initilaize topology
Topo.__init__(self) self.addSwitch("s1")
self.addSwitch("s2") self.addHost("h1")
self.addHost("h2")
self.addHost("h3")
self.addHost("h4") self.addLink("s1", "h1")
self.addLink("s1", "h2")
self.addLink("s2", "h3")
self.addLink("s2", "h4")
self.addLink("s1", "s2") topos = {'mytopo': (lambda: MyTopo())}

Ryuvlan.py的代码

点击查看代码
import json

import requests

if __name__ == "__main__":
url = 'http://127.0.0.1:8080/stats/flowentry/add'
headers = {'Content-Type': 'application/json'}
flow1 = {
"dpid": 1,
"priority": 1,
"match":{
"in_port": 1
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type": "OUTPUT",
"port": 3
}
]
}
flow2 = {
"dpid": 1,
"priority": 1,
"match":{
"in_port": 2
},
"actions":[
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type": "OUTPUT",
"port": 3
}
]
}
flow3 = {
"dpid": 1,
"priority": 1,
"match":{
"vlan_vid": 0
},
"actions":[
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 1
}
]
}
flow4 = {
"dpid": 1,
"priority": 1,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 2
}
]
}
flow5 = {
"dpid": 2,
"priority": 1,
"match": {
"in_port": 1
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4096
},
{
"type": "OUTPUT",
"port": 3
}
]
}
flow6 = {
"dpid": 2,
"priority": 1,
"match": {
"in_port": 2
},
"actions": [
{
"type": "PUSH_VLAN",
"ethertype": 33024
},
{
"type": "SET_FIELD",
"field": "vlan_vid",
"value": 4097
},
{
"type": "OUTPUT",
"port": 3
}
]
}
flow7 = {
"dpid": 2,
"priority": 1,
"match": {
"vlan_vid": 0
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 1
}
]
}
flow8 = {
"dpid": 2,
"priority": 1,
"match": {
"vlan_vid": 1
},
"actions": [
{
"type": "POP_VLAN",
"ethertype": 33024
},
{
"type": "OUTPUT",
"port": 2
}
]
}
res1 = requests.post(url, json.dumps(flow1), headers=headers)
res2 = requests.post(url, json.dumps(flow2), headers=headers)
res3 = requests.post(url, json.dumps(flow3), headers=headers)
res4 = requests.post(url, json.dumps(flow4), headers=headers)
res5 = requests.post(url, json.dumps(flow5), headers=headers)
res6 = requests.post(url, json.dumps(flow6), headers=headers)
res7 = requests.post(url, json.dumps(flow7), headers=headers)
res8 = requests.post(url, json.dumps(flow8), headers=headers)



运行Ryuvlan.py之后pingall

(二)进阶要求

OpenDaylight或Ryu任选其一,编程实现查看前序VLAN实验拓扑中所有节点(含交换机、主机)的名称,以及显示每台交换机的所有流表项。

getnode.py的代码

点击查看代码
import requests
import time
import re class GetNodes:
def __init__(self, ip):
self.ip = ip def get_switch_id(self):
url = 'http://' + self.ip + '/stats/switches'
re_switch_id = requests.get(url=url).json()
switch_id_hex = []
for i in re_switch_id:
switch_id_hex.append(hex(i)) return switch_id_hex def getflow(self):
url = 'http://' + self.ip + '/stats/flow/%d'
switch_list = self.get_switch_id()
ret_flow = []
for switch in switch_list:
new_url = format(url % int(switch, 16))
re_switch_flow = requests.get(url=new_url).json()
ret_flow.append(re_switch_flow)
return ret_flow def show(self):
flow_list = self.getflow()
for flow in flow_list:
for dpid in flow.keys():
dp_id = dpid
switchnum= '{1}'.format(hex(int(dp_id)), int(dp_id))
print('s'+switchnum,end = " ")
switchnum = int(switchnum)
for list_table in flow.values():
for table in list_table:
string1 = str(table)
if re.search("'dl_vlan': '(.*?)'", string1) is not None:
num = re.search("'dl_vlan': '(.*?)'", string1).group(1);
if num == '0' and switchnum == 1:
print('h1',end = " ")
if num == '1' and switchnum == 1:
print('h2',end = " ")
if num == '0' and switchnum == 2:
print('h3',end = " ")
if num == '1' and switchnum == 2:
print('h4',end = " ")
print("")
flow_list = self.getflow()
for flow in flow_list:
for dpid in flow.keys():
dp_id = dpid
print('switch_name:s{1}'.format(hex(int(dp_id)), int(dp_id)))
for list_table in flow.values():
for table in list_table:
print(table)
s1 = GetNodes("127.0.0.1:8080")
s1.show()

个人总结

从配置环境开始就一直出问题的ODL,果不其然又出问题了,只能重新安装才能使用,后面的实验倒是一帆风顺,了解了相关OpenDaylight REST API和Ryu REST API的使用,学到了不少东西.

实验七:基于REST API的SDN北向应用实践的更多相关文章

  1. 实验7:基于REST API的SDN北向应用实践

    一.实验目的 1.能够编写程序调用OpenDaylight REST API实现特定网络功能: 2.能够编写程序调用Ryu REST API实现特定网络功能. 二.实验环境 下载虚拟机软件Oracle ...

  2. 实验:基于http的yum源

    实验:基于http的yum源 selinux,firewalld已经关闭',系统为CentOS7 repodata所在的目录就是yum源 下面介绍了如何把本地光盘通过httpd服务器变成yum源:多个 ...

  3. 《数据挖掘导论》实验课——实验七、数据挖掘之K-means聚类算法

    实验七.数据挖掘之K-means聚类算法 一.实验目的 1. 理解K-means聚类算法的基本原理 2. 学会用python实现K-means算法 二.实验工具 1. Anaconda 2. skle ...

  4. Angular SPA基于Ocelot API网关与IdentityServer4的身份认证与授权(二)

    上文已经介绍了Identity Service的实现过程.今天我们继续,实现一个简单的Weather API和一个基于Ocelot的API网关. 回顾 <Angular SPA基于Ocelot ...

  5. 实验6、Flask API使用示例和拓展

    实验介绍 1. 实验内容 Flask 提供了多种API拓展,本节我们主要学习基于RESTful的Flask应用程序设计 2. 实验要点 学习和掌握多种RESTful的设计模式 3.实验环境 Cento ...

  6. Atitit 游戏的通常流程 attilax 总结 基于cocos2d api

    Atitit 游戏的通常流程 attilax 总结 基于cocos2d api 加载音效1 加载页面1 添加精灵1 设置随机位置2 移动2 垃圾gc2 点击evt2 爆炸效果3 定时生成精灵3 加载音 ...

  7. Docker入门教程(七)Docker API

    Docker入门教程(七)Docker API [编者的话]DockerOne组织翻译了Flux7的Docker入门教程,本文是系列入门教程的第七篇,重点介绍了Docker Registry API和 ...

  8. 你也可以玩转Skype -- 基于Skype API开发外壳程序入门

    原文:你也可以玩转Skype -- 基于Skype API开发外壳程序入门 Skype是目前这个星球上最厉害的IM+VOIP软件,Skype现在已经改变了全球2.8亿人的生活方式.你,值得拥有! :) ...

  9. 基于 Aliexpress API 的小程序 : 批量 Copy 产品到不同的店铺

    第一个基于 Aliexpress API 的小程序 : 批量 Copy 产品到不同的店铺 还没来得及用 API 重写软件, 先写个小程序来缓解一下手工压力: 批量Copy 产品到不同的店铺. 开网店 ...

  10. 【C++ 流类库与输入输出 】实验七

    1. 基础练习 (1)教材习题 11-7 (2)教材习题 11-3 (3)教材习题 11-4 2. 应用练习 (1)已知有班级名单文件 list.txt(见实验 7 附件包).编写一个应用程序实现随机 ...

随机推荐

  1. php 动态实例化某个类

    $name = 'test'; $controller = 'test'; $a = '\\addons\\'.$name.'\\model\\'.$controller; $this->mod ...

  2. Codeforces Round #694 (Div. 1) - B. Strange Definition

    数论 Problem - B - Codeforces 题意 给定 \(n\;(1<=n<=3*10^5)\) 个数 \(a[i]\), \(1<=a[i]<=10^6\) 把 ...

  3. 请求/响应拦截器 给请求添加token认证

  4. 服务器重启后oracle监听无法打开

    我重启服务器后不知道为啥监听启动不了,试过各种办法都不行,然后把监听删了重新配置就可以了

  5. cookie和session、token的区别

    原作者:施杨(施杨's Think out)出处:http://shiyangxt.cnblogs.com ************** 本文版权归原作者和博客园共有,欢迎转载,转载请保留该申明 ** ...

  6. python菜鸟学习: 8. 集合基础知识

    # -*- coding: utf-8 -*-# 列表的特性:1,去重:2.关系测试list1 = [1, 4, 6, 8, 7, 8, 9]# 集合去重list2 = set(list1)print ...

  7. cpu主频对网络传输性能的影响

    数据包长度是:2KB iperf的测试结果:3.2GHz的cpu能上40Gb/s, 2.1GHz的cpu只能到28Gb/s.

  8. 字符流---->字符过滤流 缓冲流 : -----> printWrite用法:和BufferedReader用法

    printWrite用法:1.创建字符节点流FileWriter fw = new FileWriter("Files\\bufchar.txt");2创建字符过滤流 PrintW ...

  9. linux 串口查看信息

    查看串口是否可用,可以对串口发送数据比如对com1口,echo helloworld >/dev/ttyS0 [root@H3LINUX90 ~]# echo helloworld >/d ...

  10. StringIO 和 BytesIO

    StringIO 要把 str 字符串写入内存中,我们需要创建一个 StringIO 对象,然后像文件一样对读取内容.其中 StringIO 中多了一个 getvalue() 方法,目的是用于获取写入 ...