blob: 2e8014c699e359b2b5aef2f898bb21c3cb6cd595 [file] [log] [blame]
carlosmscabral29432252013-02-04 11:54:16 -02001import ConfigParser, re
carlosmscabralf40ecd12013-02-01 18:15:58 -02002
3class confCCNHost():
carlosmscabral6d3dd602013-03-23 11:12:34 -03004
carlosmscabrale121a7b2013-02-18 18:14:53 -03005 def __init__(self, name, app='', uri_tuples='', cpu=None, cores=None):
carlosmscabralf40ecd12013-02-01 18:15:58 -02006 self.name = name
7 self.app = app
8 self.uri_tuples = uri_tuples
carlosmscabral29432252013-02-04 11:54:16 -02009 self.cpu = cpu
carlosmscabrale121a7b2013-02-18 18:14:53 -030010 self.cores = cores
carlosmscabral6d3dd602013-03-23 11:12:34 -030011
carlosmscabralf40ecd12013-02-01 18:15:58 -020012 def __repr__(self):
carlosmscabrale121a7b2013-02-18 18:14:53 -030013 return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) + ' Cores:' +str(self.cores)
carlosmscabralf40ecd12013-02-01 18:15:58 -020014
15class confCCNLink():
carlosmscabral6d3dd602013-03-23 11:12:34 -030016
carlosmscabralf40ecd12013-02-01 18:15:58 -020017 def __init__(self,h1,h2,linkDict=None):
18 self.h1 = h1
19 self.h2 = h2
20 self.linkDict = linkDict
carlosmscabral6d3dd602013-03-23 11:12:34 -030021
carlosmscabralf40ecd12013-02-01 18:15:58 -020022 def __repr__(self):
23 return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030024
carlosmscabral29432252013-02-04 11:54:16 -020025def parse_hosts(conf_arq):
26 'Parse hosts section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020027 config = ConfigParser.RawConfigParser()
28 config.read(conf_arq)
carlosmscabral6d3dd602013-03-23 11:12:34 -030029
carlosmscabralf40ecd12013-02-01 18:15:58 -020030 hosts = []
carlosmscabral6d3dd602013-03-23 11:12:34 -030031
carlosmscabralf40ecd12013-02-01 18:15:58 -020032 items = config.items('hosts')
carlosmscabral6d3dd602013-03-23 11:12:34 -030033
carlosmscabralf40ecd12013-02-01 18:15:58 -020034 for item in items:
35
36 name = item[0]
carlosmscabral6d3dd602013-03-23 11:12:34 -030037
carlosmscabralf40ecd12013-02-01 18:15:58 -020038 rest = item[1].split()
39
40 app = rest.pop(0)
carlosmscabral6d3dd602013-03-23 11:12:34 -030041
carlosmscabralf40ecd12013-02-01 18:15:58 -020042 uris = rest
43 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020044 cpu = None
carlosmscabrale121a7b2013-02-18 18:14:53 -030045 cores = None
carlosmscabral6d3dd602013-03-23 11:12:34 -030046
carlosmscabralf40ecd12013-02-01 18:15:58 -020047 for uri in uris:
carlosmscabral29432252013-02-04 11:54:16 -020048 if re.match("cpu",uri):
49 cpu = float(uri.split('=')[1])
carlosmscabrale121a7b2013-02-18 18:14:53 -030050 elif re.match("cores",uri):
51 cores = uri.split('=')[1]
carlosmscabral29432252013-02-04 11:54:16 -020052 else:
53 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
carlosmscabral6d3dd602013-03-23 11:12:34 -030054
carlosmscabrale121a7b2013-02-18 18:14:53 -030055 hosts.append(confCCNHost(name , app, uri_list,cpu,cores))
carlosmscabral6d3dd602013-03-23 11:12:34 -030056
carlosmscabralf40ecd12013-02-01 18:15:58 -020057 return hosts
58
carlosmscabral29432252013-02-04 11:54:16 -020059def parse_routers(conf_arq):
60 'Parse routers section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020061 config = ConfigParser.RawConfigParser()
62 config.read(conf_arq)
63
64 routers = []
carlosmscabral6d3dd602013-03-23 11:12:34 -030065
carlosmscabralf40ecd12013-02-01 18:15:58 -020066 items = config.items('routers')
carlosmscabral6d3dd602013-03-23 11:12:34 -030067
carlosmscabralf40ecd12013-02-01 18:15:58 -020068 for item in items:
69 name = item[0]
carlosmscabral6d3dd602013-03-23 11:12:34 -030070
carlosmscabralf40ecd12013-02-01 18:15:58 -020071 rest = item[1].split()
72
73 uris = rest
74 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020075 cpu = None
carlosmscabrale121a7b2013-02-18 18:14:53 -030076 cores = None
carlosmscabral6d3dd602013-03-23 11:12:34 -030077
carlosmscabral29432252013-02-04 11:54:16 -020078 for uri in uris:
79 if re.match("cpu",uri):
80 cpu = float(uri.split('=')[1])
carlosmscabrale121a7b2013-02-18 18:14:53 -030081 elif re.match("cores",uri):
carlosmscabral6d3dd602013-03-23 11:12:34 -030082 cores = uri.split('=')[1]
carlosmscabral29432252013-02-04 11:54:16 -020083 else:
84 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
carlosmscabral6d3dd602013-03-23 11:12:34 -030085
carlosmscabrale121a7b2013-02-18 18:14:53 -030086 routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu, cores=cores))
carlosmscabral6d3dd602013-03-23 11:12:34 -030087
carlosmscabralf40ecd12013-02-01 18:15:58 -020088 return routers
89
carlosmscabral29432252013-02-04 11:54:16 -020090def parse_links(conf_arq):
91 'Parse links section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020092 arq = open(conf_arq,'r')
carlosmscabral6d3dd602013-03-23 11:12:34 -030093
carlosmscabralf40ecd12013-02-01 18:15:58 -020094 links = []
carlosmscabral6d3dd602013-03-23 11:12:34 -030095
carlosmscabralf40ecd12013-02-01 18:15:58 -020096 while True:
97 line = arq.readline()
98 if line == '[links]\n':
99 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300100
carlosmscabralf40ecd12013-02-01 18:15:58 -0200101 while True:
102 line = arq.readline()
103 if line == '':
104 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300105
carlosmscabralf40ecd12013-02-01 18:15:58 -0200106 args = line.split()
107 h1, h2 = args.pop(0).split(':')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300108
carlosmscabralf40ecd12013-02-01 18:15:58 -0200109 link_dict = {}
carlosmscabral6d3dd602013-03-23 11:12:34 -0300110
carlosmscabralf40ecd12013-02-01 18:15:58 -0200111 for arg in args:
112 arg_name, arg_value = arg.split('=')
113 key = arg_name
114 value = arg_value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300115 if key in ['bw','jitter','max_queue_size']:
carlosmscabralf40ecd12013-02-01 18:15:58 -0200116 value = int(value)
carlosmscabral6d3dd602013-03-23 11:12:34 -0300117 if key in ['loss']:
118 value = float(value)
carlosmscabralf40ecd12013-02-01 18:15:58 -0200119 link_dict[key] = value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300120
carlosmscabralf40ecd12013-02-01 18:15:58 -0200121 links.append(confCCNLink(h1,h2,link_dict))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300122
123
carlosmscabralf40ecd12013-02-01 18:15:58 -0200124 return links
carlosmscabral6d3dd602013-03-23 11:12:34 -0300125
126
127