backing up
[vsorcdistro/.git] / precompiler.py
1 #!/usr/bin/python
2 import sys
3 import os
4 import time
5 from mininet.topo import Topo
6 from mininet.log import setLogLevel, info
7
8 """
9 Este precompilador transforma el lenguaje VSORC a la API de topologias de 
10 Mininet
11 """
12 links = []
13 devices = []
14 hosts = []
15 switches = []
16 #Lists
17
18 #cmd = './cleaner.sh '+sys.argv[1]
19 cmd2 = './cleaner.sh data'
20 os.system(cmd2)
21 time.sleep(.300)
22 #document = open(sys.argv[1] + "_clean" ,"r+")
23 document = open("data" + "_clean" ,"r+")
24 links = document.readlines()
25 document.close
26 #"data" is the file with the topo vsorc script
27
28 #clean the \n in the colected data
29 a = 0
30 for linkline in links:
31         links[a] = linkline.rstrip()
32         a+=1
33
34
35 # get a list of non repeating devices
36 for value in links:
37         value_split = value.split(':')
38         devices.append(value_split[0])
39         devices.append(value_split[1])
40 devices = list(dict.fromkeys(devices))
41
42
43 class TopoFromCompiler(Topo):
44 #This class is for create the custom topology from the data collected.
45 #Here we also process the data to make the topo
46         def build(self):
47                 for device in devices:
48                         if device.startswith("h"):
49                                 host = device
50                                 host = self.addHost(host) #Create a host with the data collected from the list
51                                 hosts.append(host)
52
53                         elif device.startswith("s"):
54                                 switch = device
55                                 switch = self.addSwitch(switch) #Create a switch
56                                 switches.append(switch)
57
58                 print ("Devices: " + str(devices) + "\n" + "Links: " + str(links) + "\n" + "Hosts: " + str(hosts) + "\n" + "Switches: " + str(switches) + "\n")
59                 
60                 #Create links
61                 for pair in links:
62                         split = pair.split(":")
63                         self.addLink(split[0],split[1])
64