Remove code for genius
[integration/test.git] / 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(
28         "{0}  <switch_count> <host_per_switch> <base_mac: Eg:00:4b:00:00:00:00 > \
29           <base_ip: Eg:75.75.0.0>".format(
30             sys.argv[0].split("/")[-1]
31         )
32     )
33     print(
34         "Dpid of switches is derived from base mac and \
35            host ip address is derived from base ip"
36     )
37     sys.exit(1)
38
39 switch_count = int(sys.argv[1])
40 host_per_switch = int(sys.argv[2])
41 base_mac = sys.argv[3]
42 base_host_ip = sys.argv[4]
43
44 base_host_mac = base_mac.split(":")
45 base_host_mac[0] = "10"
46 base_host_mac = (":").join(base_host_mac)
47 dpid_mac = base_mac.split(":")
48 dpid_mac = ("").join(dpid_mac)
49
50
51 def new_mac(mac, offset):
52     """
53     Description: This function increments an existing mac address by offset
54     and returns the new mac
55     :param mac: Mac address with each hex separated by a colon
56                 (Eg: 00:01:02:03:04:05
57     :param offset: Increment the mac to this offset value
58     :return: new mac in same format as input mac.
59     """
60     mac = netaddr.EUI(mac).value
61     mac = mac + offset
62     mac = str(netaddr.EUI(mac)).replace("-", ":")
63     return mac
64
65
66 def new_ip(ip, offset):
67     """
68     Description: This function increments an existing ip address by offset
69     and returns the new ip
70     :param ip: Ipv4 address string
71     :param offset: increment value of IP
72     :rtype : new Ipv4 address string after incrementing by offset
73     """
74     ip = netaddr.IPAddress(ip)
75     return ip.__add__(offset)
76
77
78 def new_dpid(mac, offset):
79     """
80     Description: This function is for returns a new dpid by
81     incrementing a mac address by offset value.
82     :param mac: mac address separated by colon
83     :param offset: increment value
84     :return: New dpid
85     """
86     mac = netaddr.EUI(mac).value
87     mac = mac + offset
88     mac = str(netaddr.EUI(mac)).replace("-", ":")
89     dpid_mac = mac.split(":")
90     dpid_mac = ("").join(dpid_mac)
91     DPID = "0000" + dpid_mac
92     return DPID
93
94
95 if __name__ == "__main__":
96     DPID = new_dpid(base_mac, 1)
97     HMAC = new_mac(base_host_mac, 1)
98     HIP = new_ip(base_host_ip, 1)
99     prefix = 8
100     configfile = open("switch.py", "w")
101     configfile.write(
102         '"""@author: sandeep gangadharan\n             \
103     This topology has {0:d} switches {1:d} hosts                       \
104     \nThis topology is made out of {2:s} script                        \
105     \nThis is a fully mesh topology. Not available in mininet by default.\
106     \nHence generating this python file dynamically"""     \
107     \nfrom mininet.topo import Topo\nclass DemoTopo(Topo):          \
108     \n'.format(
109             switch_count, switch_count * host_per_switch, sys.argv[0]
110         )
111     )
112     print(
113         "This topology has %d switches %d hosts"
114         % (switch_count, switch_count * host_per_switch)
115     )
116     configfile.write("    def __init__(self):\n ")
117     configfile.write("        #  Initialize topology\n")
118     configfile.write("        Topo.__init__(self)\n")
119     configfile.write("        #  Add Switches\n")
120     # Add switches
121     for i in range(1, switch_count + 1):
122         configfile.write(
123             "        s{0:d} = self.addSwitch('s{1:d}',dpid='{2:s}')\
124             \n".format(
125                 i, i, DPID
126             )
127         )
128         DPID = new_dpid(base_mac, i + 1)
129
130     # Add hosts
131     configfile.write("        #  Add Hosts\n")
132     for i in range(1, switch_count + 1):
133         for j in range(1, host_per_switch + 1):
134             configfile.write(
135                 "        self.addLink(s{0:d}, \
136                 self.addHost('s{1:d}h{2:d}',\
137                 ip='{3:s}',mac='{4:s}',prefixLen='{5:d}'))\n".format(
138                     i, i, j, HIP, HMAC, prefix
139                 )
140             )
141             HMAC = new_mac(HMAC, 1)
142             HIP = new_ip(HIP, 1)
143
144     #  Add Links
145     configfile.write("  #  Add Links\n")
146     count = 0
147     for i in range(1, switch_count + 1):
148         if i == 1:
149             continue
150         for j in range(1, i + 1):
151             if i != j:
152                 configfile.write(
153                     "        self.addLink(s{0:d}, s{1:d})\
154                 \n".format(
155                         i, j
156                     )
157                 )
158     configfile.write("topos = { 'demotopo': ( lambda: DemoTopo() ) }")
159     configfile.close()