(一)基本要求

编写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. redhat7

    systemctl systemctl enable sshd 开机启动某服务 systemctl disable sshd 开机不启动某服务 systemctl is-enabled sshd查看某 ...

  2. vue 收藏

    html: //收藏 <el-table-column prop="isOpen" label="">                 <te ...

  3. 在Unity3D中开发的坦克履带模拟器Tank Track Simulator

    为了在Unity游戏中比较真实地模拟坦克履带的运动,本人便开发了这款Tank Track Simulator插件 特点 比较真实地模拟了坦克履带的运动. 本插件中包含了一辆M1A2坦克模型,已经将这个 ...

  4. K Smallest In Unsorted Array

    Find the K smallest numbers in an unsorted integer array A. The returned numbers should be in ascend ...

  5. Ubuntu ROOT默认密码设置

    Ubuntu默认密码设置 Ubuntu默认是不设置ROOT密码的,如果需要安装一些包则需要从普通用户调转至ROOT用户,这个时候就需要设置一下密码了. 下面这种情况 chen@chen:~/Deskt ...

  6. kubectl工具安装指南

    kubectl是一个用于连接Service Mesh控制平面的工具,可以安装在办公电脑的Windows系统上,也可以安装在虚拟机的Linux系统上,只要网络能与控制平面的公网地址互通即可.下面分别介绍 ...

  7. python与c++区别

    1 都是面向对象编程,但是python是脚本语言,无需main函数 2 python不需要引用库,前不要定义类型,后不要: 列表可以用-x,表示倒数第几个,不能用a++ a = [1, 2, 3, 4 ...

  8. Mysql 查询最近一年的数量sql

    按月查询分月数量 , 某个月份没有的显示为0 SELECT@s := @s + 1 AS `index`,DATE_FORMAT( DATE_SUB( CURDATE(), INTERVAL @s M ...

  9. C# Async / Await State Machine

    The async/await keywords in C# are very much syntactical sugar that the compiler will use to generat ...

  10. Python在字典中通过键名查找键值

    def find(target, dict_data): """ :param target: 需要查找的键名 :param dict_data: 需要查找的列表 :re ...