blob: ae514f37a51107272ab43466ed6a3c105dfcd795 [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
carlosmscabralb9d85b72013-07-15 15:24:33 -03005 def __init__(self, name, app='', uri_tuples='', cpu=None, cores=None, cache=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
carlosmscabralb9d85b72013-07-15 15:24:33 -030011 self.cache = cache
carlosmscabral6d3dd602013-03-23 11:12:34 -030012
carlosmscabralf40ecd12013-02-01 18:15:58 -020013 def __repr__(self):
carlosmscabralb9d85b72013-07-15 15:24:33 -030014 return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) + ' Cores:' +str(self.cores) + ' Cache: ' + str(self.cache)
carlosmscabralf40ecd12013-02-01 18:15:58 -020015
16class confCCNLink():
carlosmscabral6d3dd602013-03-23 11:12:34 -030017
carlosmscabralf40ecd12013-02-01 18:15:58 -020018 def __init__(self,h1,h2,linkDict=None):
19 self.h1 = h1
20 self.h2 = h2
21 self.linkDict = linkDict
carlosmscabral6d3dd602013-03-23 11:12:34 -030022
carlosmscabralf40ecd12013-02-01 18:15:58 -020023 def __repr__(self):
24 return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict)
carlosmscabral6d3dd602013-03-23 11:12:34 -030025
carlosmscabral29432252013-02-04 11:54:16 -020026def parse_hosts(conf_arq):
27 'Parse hosts section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020028 config = ConfigParser.RawConfigParser()
29 config.read(conf_arq)
carlosmscabral6d3dd602013-03-23 11:12:34 -030030
carlosmscabralf40ecd12013-02-01 18:15:58 -020031 hosts = []
carlosmscabral6d3dd602013-03-23 11:12:34 -030032
Caio188d2f32015-01-22 23:35:08 -020033 items = config.items('hosts')
Caio Eliasc6b56032014-09-19 14:12:48 -030034
Caio188d2f32015-01-22 23:35:08 -020035 #makes a first-pass read to hosts section to find empty host sections
Caio Eliasb5e55fa2014-09-19 14:27:54 -030036 for item in items:
37 name = item[0]
38 rest = item[1].split()
39 if len(rest) == 0:
Caio188d2f32015-01-22 23:35:08 -020040 config.set('hosts', name, '_')
Caio Eliasc6b56032014-09-19 14:12:48 -030041 #updates 'items' list
Caio188d2f32015-01-22 23:35:08 -020042 items = config.items('hosts')
carlosmscabral6d3dd602013-03-23 11:12:34 -030043
Caio188d2f32015-01-22 23:35:08 -020044 #makes a second-pass read to hosts section to properly add hosts
carlosmscabralf40ecd12013-02-01 18:15:58 -020045 for item in items:
46
47 name = item[0]
carlosmscabral6d3dd602013-03-23 11:12:34 -030048
carlosmscabralf40ecd12013-02-01 18:15:58 -020049 rest = item[1].split()
50
51 app = rest.pop(0)
carlosmscabral6d3dd602013-03-23 11:12:34 -030052
carlosmscabralf40ecd12013-02-01 18:15:58 -020053 uris = rest
54 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020055 cpu = None
carlosmscabrale121a7b2013-02-18 18:14:53 -030056 cores = None
carlosmscabralb9d85b72013-07-15 15:24:33 -030057 cache = None
carlosmscabral6d3dd602013-03-23 11:12:34 -030058
carlosmscabralf40ecd12013-02-01 18:15:58 -020059 for uri in uris:
carlosmscabral29432252013-02-04 11:54:16 -020060 if re.match("cpu",uri):
61 cpu = float(uri.split('=')[1])
carlosmscabrale121a7b2013-02-18 18:14:53 -030062 elif re.match("cores",uri):
63 cores = uri.split('=')[1]
carlosmscabralb9d85b72013-07-15 15:24:33 -030064 elif re.match("cache",uri):
65 cache = uri.split('=')[1]
carlosmscabral3d060fc2013-07-18 13:18:09 -030066 elif re.match("mem",uri):
carlosmscabral7736b822013-07-21 14:40:31 -030067 mem = uri.split('=')[1]
carlosmscabral29432252013-02-04 11:54:16 -020068 else:
69 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
carlosmscabral6d3dd602013-03-23 11:12:34 -030070
carlosmscabralb9d85b72013-07-15 15:24:33 -030071 hosts.append(confCCNHost(name , app, uri_list,cpu,cores,cache))
carlosmscabral6d3dd602013-03-23 11:12:34 -030072
carlosmscabralf40ecd12013-02-01 18:15:58 -020073 return hosts
74
carlosmscabral29432252013-02-04 11:54:16 -020075def parse_routers(conf_arq):
76 'Parse routers section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -020077 config = ConfigParser.RawConfigParser()
78 config.read(conf_arq)
79
80 routers = []
carlosmscabral6d3dd602013-03-23 11:12:34 -030081
carlosmscabralf40ecd12013-02-01 18:15:58 -020082 items = config.items('routers')
carlosmscabral6d3dd602013-03-23 11:12:34 -030083
carlosmscabralf40ecd12013-02-01 18:15:58 -020084 for item in items:
85 name = item[0]
carlosmscabral6d3dd602013-03-23 11:12:34 -030086
carlosmscabralf40ecd12013-02-01 18:15:58 -020087 rest = item[1].split()
88
89 uris = rest
90 uri_list=[]
carlosmscabral29432252013-02-04 11:54:16 -020091 cpu = None
carlosmscabrale121a7b2013-02-18 18:14:53 -030092 cores = None
Caio188d2f32015-01-22 23:35:08 -020093 cache = None
carlosmscabral6d3dd602013-03-23 11:12:34 -030094
Caio188d2f32015-01-22 23:35:08 -020095 if '_' in uris:
96 pass
97 else:
98 for uri in uris:
99 if re.match("cpu",uri):
100 cpu = float(uri.split('=')[1])
101 elif re.match("cores",uri):
102 cores = uri.split('=')[1]
103 elif re.match("cache",uri):
104 cache = uri.split('=')[1]
105 elif re.match("mem",uri):
106 mem = uri.split('=')[1]
107 else:
108 uri_list.append((uri.split(',')[0],uri.split(',')[1]))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300109
carlosmscabrale121a7b2013-02-18 18:14:53 -0300110 routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu, cores=cores))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300111
carlosmscabralf40ecd12013-02-01 18:15:58 -0200112 return routers
113
carlosmscabral29432252013-02-04 11:54:16 -0200114def parse_links(conf_arq):
115 'Parse links section from the conf file.'
carlosmscabralf40ecd12013-02-01 18:15:58 -0200116 arq = open(conf_arq,'r')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300117
carlosmscabralf40ecd12013-02-01 18:15:58 -0200118 links = []
carlosmscabral6d3dd602013-03-23 11:12:34 -0300119
carlosmscabralf40ecd12013-02-01 18:15:58 -0200120 while True:
121 line = arq.readline()
122 if line == '[links]\n':
123 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300124
carlosmscabralf40ecd12013-02-01 18:15:58 -0200125 while True:
126 line = arq.readline()
127 if line == '':
128 break
carlosmscabral6d3dd602013-03-23 11:12:34 -0300129
carlosmscabralf40ecd12013-02-01 18:15:58 -0200130 args = line.split()
Caio Eliasc6b56032014-09-19 14:12:48 -0300131
132 #checks for non-empty line
133 if len(args) == 0:
134 continue
135
carlosmscabralf40ecd12013-02-01 18:15:58 -0200136 h1, h2 = args.pop(0).split(':')
carlosmscabral6d3dd602013-03-23 11:12:34 -0300137
carlosmscabralf40ecd12013-02-01 18:15:58 -0200138 link_dict = {}
carlosmscabral6d3dd602013-03-23 11:12:34 -0300139
carlosmscabralf40ecd12013-02-01 18:15:58 -0200140 for arg in args:
141 arg_name, arg_value = arg.split('=')
142 key = arg_name
143 value = arg_value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300144 if key in ['bw','jitter','max_queue_size']:
carlosmscabralf40ecd12013-02-01 18:15:58 -0200145 value = int(value)
carlosmscabral6d3dd602013-03-23 11:12:34 -0300146 if key in ['loss']:
147 value = float(value)
carlosmscabralf40ecd12013-02-01 18:15:58 -0200148 link_dict[key] = value
carlosmscabral6d3dd602013-03-23 11:12:34 -0300149
carlosmscabralf40ecd12013-02-01 18:15:58 -0200150 links.append(confCCNLink(h1,h2,link_dict))
carlosmscabral6d3dd602013-03-23 11:12:34 -0300151
152
carlosmscabralf40ecd12013-02-01 18:15:58 -0200153 return links