blob: 642b4b18a08cab1ccdb55b65b5cb6d639098bdce [file] [log] [blame]
carlosmscabral29432252013-02-04 11:54:16 -02001import ConfigParser, re
carlosmscabralf40ecd12013-02-01 18:15:58 -02002
3class confCCNHost():
4
carlosmscabral29432252013-02-04 11:54:16 -02005 def __init__(self, name, app='', uri_tuples='', cpu=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
carlosmscabralf40ecd12013-02-01 18:15:58 -020010
11 def __repr__(self):
carlosmscabral29432252013-02-04 11:54:16 -020012 return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu)
carlosmscabralf40ecd12013-02-01 18:15:58 -020013
14class confCCNLink():
15
16 def __init__(self,h1,h2,linkDict=None):
17 self.h1 = h1
18 self.h2 = h2
19 self.linkDict = linkDict
20
21 def __repr__(self):
22 return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)
23
carlosmscabral29432252013-02-04 11:54:16 -020024def parse_hosts(conf_arq):
25 'Parse hosts section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020026 config = ConfigParser.RawConfigParser()
27 config.read(conf_arq)
28
29 hosts = []
30
31 items = config.items('hosts')
32
33 for item in items:
34
35 name = item[0]
36
37 rest = item[1].split()
38
39 app = rest.pop(0)
40
41 uris = rest
42 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020043 cpu = None
carlosmscabralf40ecd12013-02-01 18:15:58 -020044 for uri in uris:
carlosmscabral29432252013-02-04 11:54:16 -020045 if re.match("cpu",uri):
46 cpu = float(uri.split('=')[1])
47 else:
48 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
carlosmscabralf40ecd12013-02-01 18:15:58 -020049
carlosmscabral29432252013-02-04 11:54:16 -020050 hosts.append(confCCNHost(name , app, uri_list,cpu))
carlosmscabralf40ecd12013-02-01 18:15:58 -020051
52 return hosts
53
carlosmscabral29432252013-02-04 11:54:16 -020054def parse_routers(conf_arq):
55 'Parse routers section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020056 config = ConfigParser.RawConfigParser()
57 config.read(conf_arq)
58
59 routers = []
60
61 items = config.items('routers')
62
63 for item in items:
64 name = item[0]
65
66 rest = item[1].split()
67
68 uris = rest
69 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020070 cpu = None
carlosmscabralf40ecd12013-02-01 18:15:58 -020071
carlosmscabral29432252013-02-04 11:54:16 -020072 for uri in uris:
73 if re.match("cpu",uri):
74 cpu = float(uri.split('=')[1])
75 else:
76 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
77
78 routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu))
carlosmscabralf40ecd12013-02-01 18:15:58 -020079
80 return routers
81
carlosmscabral29432252013-02-04 11:54:16 -020082def parse_links(conf_arq):
83 'Parse links section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020084 arq = open(conf_arq,'r')
85
86 links = []
87
88 while True:
89 line = arq.readline()
90 if line == '[links]\n':
91 break
92
93 while True:
94 line = arq.readline()
95 if line == '':
96 break
97
98 args = line.split()
99 h1, h2 = args.pop(0).split(':')
100
101 link_dict = {}
102
103 for arg in args:
104 arg_name, arg_value = arg.split('=')
105 key = arg_name
106 value = arg_value
107 if key in ['loss','bw','jitter']:
108 value = int(value)
109
110 link_dict[key] = value
111
112 links.append(confCCNLink(h1,h2,link_dict))
113
114
115 return links
116
117
118