Update and rename MantenerFIFO to MantenerFIFO.md
[vsorcdistro/.git] / scripts / precompiler.py
1 #!/usr/bin/python
2 """
3 Written by: Oscar J. Rodriguez and Felix G. Tejada
4
5 This code is for precompile the VSORC language to Mininet topology API.
6 VSORC language is a topology languaje, for example:
7
8 s1:h1
9 s2:h2
10 s1:s2
11
12 This create a simple two switch topology with two host each one.
13 """
14
15 import sys
16 import os
17 import time
18 from mininet.topo import Topo
19 from mininet.log import setLogLevel, info
20
21 links = []
22 devices = []
23 hosts = []
24 switches = []
25 #Lists
26
27 #cmd = './cleaner.sh '+sys.argv[1]
28 cmd2 = './cleaner.sh data'
29 os.system(cmd2)
30 time.sleep(.300)
31 #document = open(sys.argv[1] + "_clean" ,"r+")
32 document = open("data" + "_clean" ,"r+")
33 links = document.readlines()
34 document.close
35 #"data" is the file with the topo vsorc script
36
37 #clean the \n in the colected data
38 a = 0
39 for linkline in links:
40         links[a] = linkline.rstrip()
41         a+=1
42
43
44 # get a list of non repeating devices
45 for value in links:
46         value_split = value.split(':')
47         devices.append(value_split[0])
48         devices.append(value_split[1])
49 devices = list(dict.fromkeys(devices))
50
51
52 class TopoFromCompiler(Topo):
53 #This class is for create the custom topology from the data collected.
54 #Here we also process the data to make the topo
55         def build(self):
56                 for device in devices:
57                         if device.startswith("h"):
58                                 host = device
59                                 host = self.addHost(host) #Create a host with the data collected from the list
60                                 hosts.append(host)
61
62                         elif device.startswith("s"):
63                                 switch = device
64                                 switch = self.addSwitch(switch) #Create a switch
65                                 switches.append(switch)
66
67                 print ("Devices: " + str(devices) + "\n" + "Links: " + str(links) + "\n" + "Hosts: " + str(hosts) + "\n" + "Switches: " + str(switches) + "\n")
68                 sys.stdout.flush()
69                 
70                 #Create links
71                 for pair in links:
72                         split = pair.split(":")
73                         self.addLink(split[0],split[1])
74