Ryu example of a REST API call for basic operations

Source: Internet
Author: User

Import Urllib2import jsondef get_all_switches (): url = "http://127.0.0.1:8080/v1.0/topology/switches" req = urllib2. Request (URL) res_data = Urllib2.urlopen (req) res = Res_data.read () res = json.loads (res) return resdef Get_all _links (): url = "http://127.0.0.1:8080/v1.0/topology/links" req = urllib2. Request (URL) res_data = Urllib2.urlopen (req) res = Res_data.read () res = json.loads (res) return resdef GET_SWI TCH (dpid): url = "http://127.0.0.1:8080/v1.0/topology/switches/" + dpid req = urllib2. Request (URL) res_data = Urllib2.urlopen (req) res = Res_data.read () res = json.loads (res) return resdef Get_flo W_entries (dpid): url = "http://127.0.0.1:8080/stats/flow/" + dpid req = urllib2. Request (URL) res_data = Urllib2.urlopen (req) res = Res_data.read () res = json.loads (res) return resdef Add_flo W_entry (dpid,match,priority,actions): url = "Http://127.0.0.1:8080/stats/flowentry/add" Post_data = "{' Dpid ':%s, ' Mat Ch ':%s, ' PRIority ':%s, ' Actions ':%s} '% (Dpid,str (match), PRIORITY,STR (actions)) req = Urllib2. Request (url,post_data) res = Urllib2.urlopen (req) return Res.getcode () def delete_flow_entry (Dpid, Match=none, Priori Ty=none, actions=none): url = "Http://127.0.0.1:8080/stats/flowentry/delete" Post_data = "{' Dpid ':%s"% dpid if M Atch is not none:post_data + = ", ' Match ':%s"% str (match) If priority is not none:post_data + = ", ' Priori Ty ':%s '% priority if actions are not none:post_data + = ", ' Actions ':%s '% str (actions) Post_data + ="} "re Q = urllib2. Request (url,post_data) res = Urllib2.urlopen (req) return Res.getcode ()

Function Description:

#get_all_switches ()
Parameters: None
Return Result: A list containing all the switch information
Examples of results:
[{"Ports": [{"Hw_addr": "12:ad:47:17:6d:1d", "name": "S1-eth1", "Port_no": "00000001", "Dpid": "0000000000000001"}, {" Hw_addr ":" 62:bf:89:79:68:67 "," name ":" S1-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000001 "}]," Dpid ":" 0000000000000001 "}, {" Ports ": [{" Hw_addr ":" da:d7:cb:f8:a4:7f "," name ":" S2-eth1 "," Port_no ":" 00000001 "," Dpid ":" 0000000000000002 "}, {" Hw_addr ":" ce:31:74:a1:c1:2d "," name ":" S2-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000002 "}]," Dpid ":" 0000000000000002 "}, {" Ports ": [{" Hw_addr ":" Ea:c5:e8:ee:72:f7 "," name ":" S3-eth1 "," Port _no ":" 00000001 "," Dpid ":" 0000000000000003 "}, {" Hw_addr ":" da:57:80:b2:74:67 "," name ":" S3-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000003 "}]," Dpid ":" 0000000000000003 "}]
#get_all_links ()
Parameters: None
Return Result: A list containing all the link information
Examples of results:
[{"src": {"hw_addr": "12:ad:47:17:6d:1d", "name": "S1-eth1", "Port_no": "00000001", "Dpid": "0000000000000001"}, "DST": {"Hw_addr": "da:d7:cb:f8:a4:7f", "name": "S2-eth1", "Port_no": "00000001", "Dpid": "0000000000000002"}}, {"src": {"Hw_ Addr ":" Ea:c5:e8:ee:72:f7 "," name ":" S3-eth1 "," Port_no ":" 00000001 "," Dpid ":" 0000000000000003 "}," DST ": {" hw_addr ":" Ce:31:74:a1:c1:2d "," name ":" S2-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000002 "}}, {" src ": {" hw_addr ":" Da:d7: cb:f8:a4:7f "," name ":" S2-eth1 "," Port_no ":" 00000001 "," Dpid ":" 0000000000000002 "}," DST ": {" hw_addr ":" 12:ad:47:17:6d : 1d "," name ":" S1-eth1 "," Port_no ":" 00000001 "," Dpid ":" 0000000000000001 "}}, {" src ": {" hw_addr ":" ce:31:74:a1:c1:2d "," Name ":" S2-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000002 "}," DST ": {" hw_addr ":" Ea:c5:e8:ee:72:f7 "," Name ":" S3-eth1 "," Port_no ":" 00000001 "," Dpid ":" 0000000000000003 "}}]
#get_switch (Dpid)
Parameter: Dpid is a string, such as "0000000000000001"
Return result: A list of information that contains the dpid corresponding switch
Examples of results:
[{"Ports": [{"Hw_addr": "12:ad:47:17:6d:1d", "name": "S1-eth1", "Port_no": "00000001", "Dpid": "0000000000000001"}, {" Hw_addr ":" 62:bf:89:79:68:67 "," name ":" S1-eth2 "," Port_no ":" 00000002 "," Dpid ":" 0000000000000001 "}]," Dpid ":" 0000000000000001 "}]
#get_flow_entries (Dpid)
Parameter: Dpid is a string, such as "0000000000000001"
Return result: A dictionary containing flow table entries
Examples of results:
{"1": [{"Actions": ["output:65533"], "Idle_timeout": 0, "cookie": 0, "Packet_count": 2252, "Hard_timeout": 0, "Byte_count ": 114852," duration_nsec ": 370000000," priority ": 65535," duration_sec ": 2026," table_id ": 0," match ": {" Dl_type ": 35020, "NW_DST": "0.0.0.0", "DL_VLAN_PCP": 0, "dl_src": "00:00:00:00:00:00", "Nw_tos": 0, "TP_SRC": 0, "Dl_vlan": 0, "nw_src": " 0.0.0.0 "," Nw_proto ": 0," TP_DST ": 0," DL_DST ":" 01:80:c2:00:00:0e "," In_port ": 0}}, {" Actions ": [" Output:2 "]," idle_time Out ": 0," cookies ": 0," Packet_count ": 0," Hard_timeout ": 0," Byte_count ": 0," duration_nsec ": 864000000," priority ": 1111, "Duration_sec": 104, "table_id": 0, "match": {"Dl_type": 0, "NW_DST": "0.0.0.0", "DL_VLAN_PCP": 0, "dl_src": "00:00:00:00 : 00:00 "," Nw_tos ": 0," TP_SRC ": 0," Dl_vlan ": 0," nw_src ":" 0.0.0.0 "," Nw_proto ": 0," TP_DST ": 0," DL_DST ":" 00:00:00:00:0 0:00 "," In_port ": 1}}]}
Where the beginning of "1" represents the Dpid number
#add_flow_entry (dpid,match,priority,actions)
Parameters:
Dpid is a string, such as "0000000000000001"
Match is a dictionary, such as match = {"In_port": 00000001}, the name of the other key is shown in "Ryu documentation" P161 Flow Match Structure
Priority is a string, such as "1111"
Actions are lists, such as actions = [{"Type": "OUTPUT", "Port": 00000002}], other element names see "Ryu Documentation" P282 actions
Return result: integer, HTTP status code, 200 indicates success of adding flow table entry
Results Example: 200,403,404
#delete_flow_entry (dpid,match,priority,actions)
Parameters:
Dpid is a string, such as "0000000000000001"
Match is a dictionary, such as match = {"In_port": 00000001}, the name of the other key is shown in "Ryu documentation" P161 Flow Match Structure
Priority is a string, such as "1111"
Actions are lists, such as actions = [{"Type": "OUTPUT", "Port": 00000002}], other element names see "Ryu Documentation" P282 actions
Return result: integer, HTTP status code, 200 for Delete stream table entry succeeded
Results Example: 200,403,404

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Ryu example of a REST API call for basic operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.