# ryurest
A Python module to interact with the REST API of the Ryu SDN controller.
# ABOUT
These are two Python modules that individually provide either a functional or object-orientated approach to using the Ryu REST API.
The modules make use of the Requests framework to interact with the RYU REST API.
# MODULES
Both the modules contain identical functions/methods. Which one you should use depends entirely on how comfortable you are with Python (although it is generally seen as better practice in the community to use OO approaches where possible)
### ryuswitch
Object-orientated approach.
Provides the `RyuSwitch` class to instantiate the physical switches connected to the controller as objects.
### ryufunc
Functional approach
Allows you to call the RyuSwitch methods directly (although a switch Datapath ID (DPID) must be passed as an argument in most cases).
# REQUIREMENTS
Run the Ryu controller with REST API enabled.
`$ sudo ryu-manager ryu.app.simple_switch_13 ryu.app.ofctl_rest`
# INSTALLATION
## PyPi
`$ pip install ryurest`
You may wish to use `sudo` with this command.
## From source
Alternatively you can either download or clone this repository, place the required `ryufunc.py` and/or `ryuswitch.py` modules into your project directory, and import them as per normal.
`$ git clone https://github.com/nathancatania/ryurest`
```python
# imports the functional module
import ryufunc
# imports the object-orientated module
from ryuswitch import RySwitch
```
You may also need to install the [requests][requests] library if it is not already on your machine:
`$ pip install requests`
# DEMO
A demo for each module has been created to assist in usage.
These can be found in the `demos` folder of this repository.
# USAGE
## ryuswitch.py (Object-Orientated module)
**1. Import the `ryurest` module and the `RyuSwitch` class into your script**
```python
from ryurest import RyuSwitch
```
**2. Create one or more RyuSwitch objects**
```python
switch1 = RyuSwitch( DPID )
```
If you do not know any of the DPIDs of the connected switches, you can initialize with no arguments and call the .get_switches() method to return an array of DPIDs. Be sure to assign any object created in this way a DPID manually:
```python
# Create a switch
switch0 = RyuSwitch()
# Get an array of Datapath IDs (DPIDs) for all connected switches
DPID_list = switch0.get_switches()
# Assign a DPID manually to the switch created
switch0.DPID = DPID_list[0]
```
**3. [OPTIONAL] Change the REST API URI**
* The default location for the Ryu REST API is: `http://localhost:8080`
* If Ryu is running on the same PC as the module (localhost), then there is no need to change anything.
* If the Ryu controller is running on a different machine and/or port, you MUST set the API path within each `RyuSwitch` object created.
* For example:
```python
switch1 = RyuSwitch( DPID_list[0] )
switch1.API = "http://192.168.1.30:8080"
switch2 = RyuSwitch( DPID_list[1] )
switch2.API = "http://192.168.1.30:8080"
```
* **Warning!** If altering the API path, DO NOT add a trailing '/' at the end or the API call will fail!
**4. Execute the class methods as required**
```python
# Gets all flows in flowtable
flows = switch1.get_flows()
```
* Some methods have optional filters as well.
* Consult the `ryuswitch.py` module or the [Ryu REST API documentation][ryu_rest_docs] for more info.
## ryufunc.py (functional module)
**1. From the `ryurest` module, import `ryufunc` into your script**
```python
from ryurest import ryufunc
```
**2. [OPTIONAL] Change the REST API URI**
* The default location for the Ryu REST API is: `http://localhost:8080`
* If Ryu is running on the same PC as the module (localhost), then there is no need to change anything.
* If the Ryu controller is running on a different machine and/or port, you MUST set the API path. This is global for the entire ryufunc namespace.
```python
print ryufunc.API
# prints: http://localhost:8080
# Change the default IP and Port
ryufunc.API = "http://192.168.0.30:8080"
```
* **Warning!** If altering the API path, DO NOT add a trailing '/' at the end or the API call will fail!
**3. [OPTIONAL] Obtain a list of Datapath IDs (DPIDs)**
* If you know the DPID(s) of the switch(es) you wish to interact with, you can skip this step.
* To return an array containing all of the DPIDs (switches) connected to the Ryu controller, use the get_switches() function:
```python
DPID_list = ryufunc.get_switches()
# Prints a list of all DPIDs
for DPID in DPID_list:
print DPID
# Access how you would any other array
switch1_dpid = DPID_list[0]
switch2_dpid = DPID_list[1]
# etc...
```
**4. Execute the functions as required**
* Once you know the DPID(s) of the connected switch(es), you can start to execute function calls.
```python
# Gets all flows in flowtable
flows = ryufunc.get_flows( switch1_dpid ) # returns JSON
```
* Some methods have optional filters.
* Consult the `ryufunc.py` module or the [Ryu REST API documentation][ryu_rest_docs] for more info.
# RETURN FORMATS
* If API call was **successful**...
* All the .get_x() methods will return **JSON formatted data**; EXCEPT .get_switches() which will return an array of DPIDs.
* All the .set_x(), .delete_x(), .modify_X() methods will return boolean **True**.
* If the API call **fails**...
* ALL methods/functions will return boolean **False**.
This means that you can use `if` statements to check for and handle errors accordingly.
[requests]: http://docs.python-requests.org/en/master/
[ryu_rest_docs]: http://ryu.readthedocs.io/en/latest/app/ofctl_rest.html
没有合适的资源?快使用搜索试试~ 我知道了~
ryurest:与Ryu SDN控制器的REST API交互的Python库
共7个文件
py:4个
gitignore:1个
md:1个
需积分: 50 4 下载量 174 浏览量
2021-02-05
21:23:23
上传
评论
收藏 22KB ZIP 举报
温馨提示
黑麦草 一个与Ryu SDN控制器的REST API交互的Python模块。 关于 这是两个Python模块,分别提供使用Ryu REST API的功能或面向对象的方法。 这些模块利用Requests框架与RYU REST API进行交互。 模组 两个模块都包含相同的功能/方法。 您应该使用哪一种完全取决于您对Python的适应程度(尽管在社区中通常被视为更好的实践,在可能的情况下使用OO方法) ryuswitch 面向对象的方法。 提供RyuSwitch类以将连接到控制器的物理交换机实例化为对象。 龙飞 功能方法允许您直接调用RyuSwitch方法(尽管在大多数情况下必须将开关的数据路径
资源详情
资源评论
资源推荐
收起资源包目录
ryurest-master.zip (7个子文件)
ryurest-master
demos
OO_demo.py 3KB
functional_demo.py 2KB
LICENSE 10KB
README.md 6KB
ryurest
ryuswitch.py 55KB
ryufunc.py 48KB
.gitignore 612B
共 7 条
- 1
想知道不知道但想知道
- 粉丝: 43
- 资源: 4729
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- HandTrackingModule.py
- Python基于卷积神经网络的鸟类识别项目源代码,ipynb文件
- 批量将py编译为pyd文件.atbx
- Python项目-学生管理系统
- 图像处理基于matlab图像RGB三色合成分离【含Matlab源码第1发】
- verilog HDL硬件语法设计包括算术运算三人表决器Verilog的阻塞和非阻塞赋值源码例程quartus13.1工程合集
- 【文章话题分类论文】OpenAlex Topic Classification Whitepaper
- linux学习常用命令
- 功率拓扑快速参考指南-ti,TI官方出品
- 开源2023电赛国赛运动目标控制(E题)视觉部分
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0