-
Notifications
You must be signed in to change notification settings - Fork 0
/
a.py
112 lines (103 loc) · 3.37 KB
/
a.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
N_HOSTS_HERE = 4
N_SWITCHES_HERE = 4
###############################################
from mininet.topo import Topo
import time
import argparse
from functools import partial # for using the RemoteController
from mininet.net import Mininet
from mininet.topo import Topo
from mininet.log import setLogLevel
from mininet.node import RemoteController
import logging
logger = logging.getLogger("topology")
logging.basicConfig(level = logging.DEBUG)
''' Class to create the topology object based on the parameters from the POST request.
@:parameter: mininet.topo: Topo
@:parameter: list: hosts
@:parameter: list: switches
@:parameter: list of objects: links
{
hosts: [<INTEGER>],
swiches: [<INTEGER>],
auto_set_macs: <BOOLEAN>,
ip: <STRING>,
links: <OBJECT> // e.g.: h1: [h1, h2], s1: [h1, h2]
}
'''
class topologyMininet(Topo):
def __init__(self, hosts, switches, links):
Topo.__init__(self)
listHosts = []
listSwitches = []
logger.info("Init Mininet topology")
#try:
for i in range(len(hosts)):
time.sleep(1)
listHosts.append(self.addHost('h' + str(hosts[i])))
for i in range(len(switches)):
time.sleep(1)
listSwitches.append(self.addSwitch('s' + str(switches[i])))
logger.info("Added Hosts: %s" % str(listHosts))
logger.info("Added Switches: %s" % str(listSwitches))
#except:
# logger.error("On creating devices for the topology")
# Link the hosts and the switches
for i in links:
for j in links[i]:
try:
time.sleep(1)
self.addLink(i, j)
logger.info("Added link of " + i + " to " + j)
except:
logger.error("On adding link of " + i + " to " + j)
DEFAULT_IP = "127.0.0.1"
logger = logging.getLogger("mininet")
logging.basicConfig(level = logging.INFO)
parser = argparse.ArgumentParser(description = "Mininet Script")
parser.add_argument("--ip", action = "store", dest = "ip", default = DEFAULT_IP, required = False, help = "Controller IP")
net = None
"""
Stop mininet.
"""
def closeNetwork():
logger.info("Closing network")
try:
import os
os.system("sudo mn -c")
except:
logger.error("On closing network")
return True
def startNetwork(auto_set_macs, hosts, ip, links, switches):
global net
net = Mininet(topo = topologyMininet(hosts, switches, links), build = False, autoSetMacs = auto_set_macs, controller = partial(RemoteController, ip = ip))
#net.addController('c0') # To let the controller in the local mode
closeNetwork()
logger.info("\nStarting network,\nNET:")
logger.info(net)
logger.info("- Hosts: %s" % str(hosts))
logger.info("- Switches: %s" % str(switches))
logger.info("- Links: %s" % str(links))
logger.info("- RemoteController: %s" % str(ip))
#try:
net.start() # Init the network
logger.info("Network started")
#logger.error("On starting network")
return True
n_hosts = N_HOSTS_HERE
n_switches = N_SWITCHES_HERE
hosts = range(1, n_hosts+1)
switches = range(1, n_switches+1)
auto_set_macs = False
ip = "127.0.0.1"
links = dict()
for s in switches:
links_s = ['s'+str(i) for i in switches if i != s] + ['h'+str(i) for i in hosts]
links.update({"s"+str(s): links_s})
for h in hosts:
links_h = ['s'+str(i) for i in switches] + ['h'+str(i) for i in hosts if i != h]
links.update({"h"+str(h): links_h})
startNetwork(auto_set_macs, hosts, ip, links, switches)
while True:
time.sleep(10)
logger.info("-> Up")