-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathryu-restapi.py
75 lines (63 loc) · 1.55 KB
/
ryu-restapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
import requests
import json
import time
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")
def getRyu(whoRyu):
#get DPID from switches
mapping = {}
a = requests.get('http://192.168.3.25:8080/stats/switches')
# print(a.json())
switches = a.json()
#get port description
for i in switches:
# print(i)
command = 'http://192.168.3.25:8080/stats/portdesc/' + str(i)
r = requests.get(command)
temp = r.json()[str(i)]
ports = []
for b in temp:
if b['port_no'] != 'LOCAL':
ports.append(b['port_no'])
# print("DPID:"+str(i)+";Port:"+str(b['port_no']))
mapping[i] = ports
print(mapping)
#mitigating - Flow Rule
for keys, values in mapping.items():
for a in values:
send = requests.post('http://192.168.3.25:8080/stats/flowentry/add', json={\
"dpid": keys,\
"cookie": 0,\
"table_id": 0,\
"hard_timeout": 60,\
"priority": 11111,\
"flags": 1,\
"match":{"in_port": a,"eth_type": 0x0800,"ip_proto": 1},\
"actions":[]\
})
print(send.status_code)
while True:
# Wait for next request from client
message = socket.recv()
# print("Received request: %s" % message)
if message == "Dattaa":
# getRyu()
print "I revceived a message"
socket.send(b"Done")
# curl -X POST -d '{
# "dpid": 1,
# "cookie": 0,
# "table_id": 0,
# "priority": 100,
# "flags": 1,
# "match":{
# "in_port": 1,
# "eth_type": 0x0800,
# "ip_proto": 1
# },
# "actions":[
# ]
# }' http://localhost:8080/stats/flowentry/add