/
rest.py
67 lines (52 loc) · 2.23 KB
/
rest.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
#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Controller
from mininet.util import dumpNodeConnections
from mininet.node import OVSSwitch
from mininet.node import RemoteController
from mininet.cli import CLI
import requests
import json
class SingleSwitchTopo(Topo):
"Single switch connected to n hosts."
def __init__(self, n=2, **opts):
# Initialize topology and default options
Topo.__init__(self, **opts)
switch = self.addSwitch('s1')
# Python's range(N) generates 0..N-1
for h in range(n):
host = self.addHost('h%s' % (h + 1))
self.addLink(host, switch)
def setHosts(net):
h1 = net.get('h1')
h2 = net.get('h2')
h1.setIP('10.0.0.1',8)
h2.setIP('10.0.0.2',8)
def rest(tcp1,tcp2):
url = 'http://localhost:8080/stats/flowentry/add'
payload = { "dpid": 1, "priority":10, "match": { "in_port": 1}, "actions": [{ "type": "OUTPUT", "port": 2} ] }
r=requests.post(url, json.dumps(payload))
payload = { "dpid": 1, "priority":10, "match": { "in_port": 2}, "actions": [{ "type": "OUTPUT", "port": 1} ] }
r=requests.post(url, json.dumps(payload))
payload = { "dpid": 1, "priority":100, "match": { "ipv4_dst": "10.0.0.2", "ip_proto": 6, "eth_type": 2048, "tcp_dst": tcp1, "in_port": 1}, "actions": [ { "type": "SET_FIELD", "field": "tcp_dst", "value": tcp2, "port":1}, { "type": "OUTPUT", "port": 2} ] }
r=requests.post(url, json.dumps(payload))
payload = { "dpid": 1, "priority":100, "match": { "ipv4_src": "10.0.0.2", "ip_proto": 6, "eth_type": 2048, "tcp_src": tcp2, "in_port": 2}, "actions": [ { "type": "SET_FIELD", "field": "tcp_src", "value": tcp1, "port":1}, { "type": "OUTPUT", "port": 1} ] }
r=requests.post(url, json.dumps(payload))
def genericTest(topo):
c0=RemoteController( 'c0', ip='127.0.0.1', port=6633 )
net = Mininet(topo=topo, switch=OVSSwitch,
controller=c0)
net.start()
setHosts(net)
dumpNodeConnections(net.hosts)
x=input("Enter port1: ")
y=input("Enter port2: ")
rest(x,y)
CLI(net)
net.stop()
def main():
topo = SingleSwitchTopo()
genericTest(topo)
if __name__ == '__main__':
main()