Step 1: Move vm scripts to the right place
[integration/test.git] / test / csit / libraries / MininetTopo / create_fullymesh.py
1 #!/usr/bin/env python
2
3 import sys
4 import netaddr
5
6 __author__ = "Sandeep Gangadharan"
7 __copyright__ = "(c) Copyright [2015] Hewlett-Packard \
8                 Development Company, L.P."
9 __license__ = "Eclipse Public License "
10 __email__ = "sandeep.gangadharan@hp.com"
11 __created__ = "19 March 2014"
12
13 """
14     create_fullymesh.py:
15     Description : Creates Fully mesh mininet topology.
16     Input       : switch_count, host count per switch, base mac address,
17                   base ip address
18     Output      : switch.py (A python topology file)
19     Note       :  This is a fully mesh network. Not available in
20                   mininet by default. Hence generating a python file
21                   dynamically which represents the topology.
22
23 """
24
25 if len(sys.argv) < 5:
26     print("Please povide correct inputs. Exiting!!!")
27     print "{0}  <switch_count> <host_per_switch> <base_mac: Eg:00:4b:00:00:00:00 > \
28           <base_ip: Eg:75.75.0.0>".format(sys.argv[0].split('/')[-1])
29     print "Dpid of switches is derived from base mac and \
30            host ip address is derived from base ip"
31     sys.exit(1)
32
33 switch_count = int(sys.argv[1])
34 host_per_switch = int(sys.argv[2])
35 base_mac = sys.argv[3]
36 base_host_ip = sys.argv[4]
37
38 base_host_mac = base_mac.split(':')
39 base_host_mac[0] = '10'
40 base_host_mac = (':').join(base_host_mac)
41 dpid_mac = base_mac.split(':')
42 dpid_mac = ('').join(dpid_mac)
43
44
45 def new_mac(mac, offset):
46     """
47     Description: This function increments an existing mac address by offset
48     and returns the new mac
49     :param mac: Mac address with each hex separated by a colon
50                 (Eg: 00:01:02:03:04:05
51     :param offset: Increment the mac to this offset value
52     :return: new mac in same format as input mac.
53     """
54     mac = netaddr.EUI(mac).value
55     mac = mac + offset
56     mac = str(netaddr.EUI(mac)).replace('-', ':')
57     return mac
58
59
60 def new_ip(ip, offset):
61     """
62     Description: This function increments an existing ip address by offset
63     and returns the new ip
64     :param ip: Ipv4 address string
65     :param offset: increment value of IP
66     :rtype : new Ipv4 address string after incrementing by offset
67     """
68     ip = netaddr.IPAddress(ip)
69     return ip.__add__(offset)
70
71
72 def new_dpid(mac, offset):
73     """
74     Description: This function is for returns a new dpid by
75     incrementing a mac address by offset value.
76     :param mac: mac address separated by colon
77     :param offset: increment value
78     :return: New dpid
79     """
80     mac = netaddr.EUI(mac).value
81     mac = mac + offset
82     mac = str(netaddr.EUI(mac)).replace('-', ':')
83     dpid_mac = mac.split(':')
84     dpid_mac = ('').join(dpid_mac)
85     DPID = "0000" + dpid_mac
86     return DPID
87
88
89 if __name__ == "__main__":
90     DPID = new_dpid(base_mac, 1)
91     HMAC = new_mac(base_host_mac, 1)
92     HIP = new_ip(base_host_ip, 1)
93     prefix = 8
94     configfile = open("switch.py", 'w')
95     configfile.write('\"\"\"@author: sandeep gangadharan\n             \
96     This topology has {0:d} switches {1:d} hosts                       \
97     \nThis topology is made out of {2:s} script                        \
98     \nThis is a fully mesh topology. Not available in mininet by default.\
99     \nHence generating this python file dynamically\"\"\"     \
100     \nfrom mininet.topo import Topo\nclass DemoTopo(Topo):          \
101     \n'.format(switch_count, switch_count * host_per_switch, sys.argv[0]))
102     print "This topology has %d switches %d hosts" \
103           % (switch_count, switch_count * host_per_switch)
104     configfile.write("    def __init__(self):\n ")
105     configfile.write("        #  Initialize topology\n")
106     configfile.write("        Topo.__init__(self)\n")
107     configfile.write("        #  Add Switches\n")
108     # Add switches
109     for i in range(1, switch_count + 1):
110         configfile.write("        s{0:d} = self.addSwitch(\'s{1:d}\',dpid=\'{2:s}\')\
111             \n".format(i, i, DPID))
112         DPID = new_dpid(base_mac, i + 1)
113
114     # Add hosts
115     configfile.write("        #  Add Hosts\n")
116     for i in range(1, switch_count + 1):
117         for j in range(1, host_per_switch + 1):
118             configfile.write("        self.addLink(s{0:d}, \
119                 self.addHost('s{1:d}h{2:d}',\
120                 ip='{3:s}',mac='{4:s}',prefixLen='{5:d}'))\n"
121                              .format(i, i, j, HIP, HMAC, prefix))
122             HMAC = new_mac(HMAC, 1)
123             HIP = new_ip(HIP, 1)
124
125     #  Add Links
126     configfile.write("  #  Add Links\n")
127     count = 0
128     for i in range(1, switch_count + 1):
129         if i == 1:
130             continue
131         for j in range(1, i + 1):
132             if i != j:
133                 configfile.write("        self.addLink(s{0:d}, s{1:d})\
134                 \n".format(i, j))
135     configfile.write("topos = { 'demotopo': ( lambda: DemoTopo() ) }")
136     configfile.close()