/
topo.py
148 lines (121 loc) · 5.24 KB
/
topo.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from multiprocessing import Process
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.log import lg
from mininet.node import RemoteController
from bottle import request, Bottle
import socket
IP_DATA_FILE = 'ip_data.txt'
SERVER_PORT = 8181
class App:
def __init__(self):
self.app = Bottle()
self.net = Mininet(controller=RemoteController)
h1 = self.net.addHost('h1')
h2 = self.net.addHost('h2')
h3 = self.net.addHost('h3')
s1 = self.net.addSwitch('s1')
s2 = self.net.addSwitch('s2')
s3 = self.net.addSwitch('s3')
c0 = self.net.addController('c0', controller=RemoteController, ip='127.0.0.1', port=8888)
self.net.addLink(h1, s1)
self.net.addLink(s1, s2)
self.net.addLink(s2, h2)
self.net.addLink(s1, s3)
self.net.addLink(s3, s2)
self.net.addLink(s3, h3)
pass
def run(self):
bp = self.start_bottle_process()
self.run_mininet()
bp.terminate()
def start_bottle_process(self):
p = Process(target=self.run_bottle)
p.start()
return p
def run_mininet(self):
self.net.start()
CLI(self.net)
self.net.stop()
def run_bottle(self):
self.app.route('/check_connection/', callback=self.check_connection)
self.app.post('/change_setting/', callback=self.change_setting)
self.app.route('/stop', callback=self.stop)
self.app.run(host=self.read_ip(), port=SERVER_PORT)
def read_ip(self):
f = open(IP_DATA_FILE, "r")
my_ip = f.readline().rstrip('\n')
if self.is_valid_ipv4_address(my_ip):
print('Bottle server IP set as = ' + my_ip)
print('Set server IP in your RyuPilot app as \'' + my_ip + '\'.')
return my_ip
else:
print('Bottle server IP = ' + my_ip + ' invalid, changed to = localhost.')
return 'localhost'
def is_valid_ipv4_address(self, address):
try:
socket.inet_pton(socket.AF_INET, address)
except AttributeError:
try:
socket.inet_aton(address)
except socket.error:
return False
return address.count('.') == 3
except socket.error:
return False
return True
def check_connection(self):
print("Connection correct.")
return "<p>Connection correct.</p>"
def change_setting(self):
postdata = request.body.read()
json = request.json
setting_id = json["setting_id"]
print("Requested setting_id = " + str(setting_id))
#for safety remember to always update all links
#everything off
if setting_id == 1:
print('Setting network up.')
print('Setting h3 to be connected through s3 & disconnecting s1 from s2')
self.net.configLinkStatus(src='h1', dst='s1', status='up')
self.net.configLinkStatus(src='s2', dst='h2', status='up')
self.net.configLinkStatus(src='s1', dst='s2', status='down')
self.net.configLinkStatus(src='s1', dst='s3', status='up')
self.net.configLinkStatus(src='s3', dst='h3', status='up')
self.net.configLinkStatus(src='s3', dst='s2', status='up')
print(str(setting_id) + " set successfully.")
if setting_id == 2:
print('Disconnecting h2.')
print('Setting links h1-s1 and s1-h2 to status=\'up\'.')
self.net.configLinkStatus(src='h1', dst='s1', status='up')
self.net.configLinkStatus(src='s1', dst='s2', status='up')
self.net.configLinkStatus(src='s2', dst='h2', status='up')
self.net.configLinkStatus(src='s1', dst='s3', status='down')
self.net.configLinkStatus(src='s3', dst='s2', status='down')
self.net.configLinkStatus(src='s3', dst='h3', status='down')
print(str(setting_id) + " set successfully.")
if setting_id == 3:
print('Disconnecting h1.')
self.net.configLinkStatus(src='h1', dst='s1', status='down')
self.net.configLinkStatus(src='s2', dst='h2', status='up')
self.net.configLinkStatus(src='s1', dst='s2', status='down')
self.net.configLinkStatus(src='s1', dst='s3', status='down')
self.net.configLinkStatus(src='s3', dst='h3', status='up')
self.net.configLinkStatus(src='s3', dst='s2', status='up')
print(str(setting_id) + " set successfully.")
if setting_id == 4:
print('Setting network down.')
print('Setting links h1-s1 and s1-h2 to status=\'down\'.')
self.net.configLinkStatus(src='h1', dst='s1', status='down')
self.net.configLinkStatus(src='s2', dst='h2', status='down')
self.net.configLinkStatus(src='s1', dst='s3', status='down')
self.net.configLinkStatus(src='s3', dst='s2', status='down')
self.net.configLinkStatus(src='s3', dst='h3', status='down')
self.net.configLinkStatus(src='s1', dst='s2', status='down')
print(str(setting_id) + " set successfully.")
def stop(self):
self.net.stop()
print("Requested stop.")
if __name__ == '__main__':
lg.setLogLevel( 'info')
App().run()