bug fixing
[integration/test.git] / test / tools / OF_Test / robot_suites / 500__OF_Cluster_Sanity_OF / mininetwork.py
1 #!/usr/bin/python
2
3 from mininet.net import Mininet
4 from mininet.node import OVSKernelSwitch, RemoteController
5 from mininet.cli import CLI
6 import multiprocessing
7 import time
8 import argparse
9
10
11 d = 11
12 c = 0
13 b = 0
14 a = 1
15
16
17 def get_next_ip():
18     global a, b, c, d
19     rip = "{0}.{1}.{2}.{3}".format(d, c, b, a)
20     a += 1
21     if a == 255:
22         a = 1
23         b += 1
24     if b == 255:
25         b = 0
26         c += 1
27     if c == 255:
28         c = 0
29         d += 1
30     return rip
31
32 # switchid and hostid
33 hid = 0
34 sid = 0
35
36
37 def get_next_hid():
38     global hid
39     hid += 1
40     return hid
41
42
43 def get_next_sid():
44     global sid
45     sid += 1
46     return sid
47
48
49 def get_switch(net, hosts):
50     sname = 's{0}'.format(get_next_sid())
51     s = net.addSwitch(sname)
52     for i in range(hosts):
53         hname = 'h{0}'.format(get_next_hid())
54         hip = get_next_ip()
55         h = net.addHost(hname, ip=hip)
56         s.linkTo(h)
57         print "switch {0}: host {1}-{2} added".format(sname, hname, hip)
58     return s
59
60
61 def get_net(switches, hostpswtich, controllers=['10.25.2.9']):
62
63     net = Mininet(controller=RemoteController, switch=OVSKernelSwitch)
64
65     cs = []
66     for i, cip in enumerate(controllers):
67         c = net.addController('c{0}'.format(i), controller=RemoteController, ip=cip, port=6633)
68         cs.append(c)
69         print "contrller {0} created".format(c)
70
71     ss = []
72     for i in range(switches):
73         s = get_switch(net, hostpswtich)
74
75     net.build()
76     for c in cs:
77         c.start()
78
79     for s in ss:
80         s.start(cs)
81
82     return net
83
84
85 class MininetworkProcess(multiprocessing.Process):
86     """Base class.
87     Do NOT use this class directly.
88     """
89     def __init__(self, swithes, hps, controllers=['10.25.2.9']):
90         super(MininetworkProcess, self).__init__()
91         self._event = multiprocessing.Event()
92         self._net = get_net(swithes, hps, controllers=controllers)
93
94     def run(self):
95         self._net.start()
96         self._net.staticArp()
97         while self._event.is_set() is False:
98             time.sleep(1)
99         self._net.stop()
100
101
102 if __name__ == '__main__':
103     # setLogLevel( 'info' )
104     parser = argparse.ArgumentParser(description='Starts switches with specified controllers')
105     parser.add_argument('--controllers', default='10.25.2.9',
106                         help='Comma separated list of cluster members (default ="10.25.2.9,10.25.2.10,10.25.2.11")')
107     args = parser.parse_args()
108
109     net = get_net(3, 1, controllers=args.controllers.split(','))
110
111     net.start()
112     net.staticArp()
113     CLI(net)
114     net.stop()
115