carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 1 | import ConfigParser, re |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 2 | |
| 3 | class confCCNHost(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 4 | |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 5 | def __init__(self, name, app='', uri_tuples='', cpu=None, cores=None, cache=None): |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 6 | self.name = name |
| 7 | self.app = app |
| 8 | self.uri_tuples = uri_tuples |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 9 | self.cpu = cpu |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 10 | self.cores = cores |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 11 | self.cache = cache |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 12 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 13 | def __repr__(self): |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 14 | return 'Name: ' + self.name + ' App: ' + self.app + ' URIS: ' + str(self.uri_tuples) + ' CPU:' + str(self.cpu) + ' Cores:' +str(self.cores) + ' Cache: ' + str(self.cache) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 15 | |
| 16 | class confCCNLink(): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 17 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 18 | def __init__(self,h1,h2,linkDict=None): |
| 19 | self.h1 = h1 |
| 20 | self.h2 = h2 |
| 21 | self.linkDict = linkDict |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 22 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 23 | def __repr__(self): |
| 24 | return 'h1: ' + self.h1 + ' h2: ' + self.h2 + ' params: ' + str(self.linkDict) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 25 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 26 | def parse_hosts(conf_arq): |
| 27 | 'Parse hosts section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 28 | config = ConfigParser.RawConfigParser() |
| 29 | config.read(conf_arq) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 30 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 31 | hosts = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 32 | |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 33 | items = config.items('nodes') |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 34 | |
| 35 | #makes a first-pass read to nodes section to find empty nodes sections |
Caio Elias | b5e55fa | 2014-09-19 14:27:54 -0300 | [diff] [blame] | 36 | for item in items: |
| 37 | name = item[0] |
| 38 | rest = item[1].split() |
| 39 | if len(rest) == 0: |
| 40 | config.set('nodes', name, '_') |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 41 | #updates 'items' list |
Caio Elias | b5e55fa | 2014-09-19 14:27:54 -0300 | [diff] [blame] | 42 | items = config.items('nodes') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 43 | |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 44 | #makes a second-pass read to nodes section to properly add hosts |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 45 | for item in items: |
| 46 | |
| 47 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 48 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 49 | rest = item[1].split() |
| 50 | |
| 51 | app = rest.pop(0) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 52 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 53 | uris = rest |
| 54 | uri_list=[] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 55 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 56 | cores = None |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 57 | cache = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 58 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 59 | for uri in uris: |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 60 | if re.match("cpu",uri): |
| 61 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 62 | elif re.match("cores",uri): |
| 63 | cores = uri.split('=')[1] |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 64 | elif re.match("cache",uri): |
| 65 | cache = uri.split('=')[1] |
carlosmscabral | 3d060fc | 2013-07-18 13:18:09 -0300 | [diff] [blame] | 66 | elif re.match("mem",uri): |
carlosmscabral | 7736b82 | 2013-07-21 14:40:31 -0300 | [diff] [blame] | 67 | mem = uri.split('=')[1] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 68 | else: |
| 69 | uri_list.append((uri.split(',')[0],uri.split(',')[1])) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 70 | |
carlosmscabral | b9d85b7 | 2013-07-15 15:24:33 -0300 | [diff] [blame] | 71 | hosts.append(confCCNHost(name , app, uri_list,cpu,cores,cache)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 72 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 73 | return hosts |
| 74 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 75 | def parse_routers(conf_arq): |
| 76 | 'Parse routers section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 77 | config = ConfigParser.RawConfigParser() |
| 78 | config.read(conf_arq) |
| 79 | |
| 80 | routers = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 81 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 82 | items = config.items('routers') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 83 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 84 | for item in items: |
| 85 | name = item[0] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 86 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 87 | rest = item[1].split() |
| 88 | |
| 89 | uris = rest |
| 90 | uri_list=[] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 91 | cpu = None |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 92 | cores = None |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 93 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 94 | for uri in uris: |
| 95 | if re.match("cpu",uri): |
| 96 | cpu = float(uri.split('=')[1]) |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 97 | elif re.match("cores",uri): |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 98 | cores = uri.split('=')[1] |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 99 | else: |
| 100 | uri_list.append((uri.split(',')[0],uri.split(',')[1])) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 101 | |
carlosmscabral | e121a7b | 2013-02-18 18:14:53 -0300 | [diff] [blame] | 102 | routers.append(confCCNHost(name=name , uri_tuples=uri_list, cpu=cpu, cores=cores)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 103 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 104 | return routers |
| 105 | |
carlosmscabral | 2943225 | 2013-02-04 11:54:16 -0200 | [diff] [blame] | 106 | def parse_links(conf_arq): |
| 107 | 'Parse links section from the conf file.' |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 108 | arq = open(conf_arq,'r') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 109 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 110 | links = [] |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 111 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 112 | while True: |
| 113 | line = arq.readline() |
| 114 | if line == '[links]\n': |
| 115 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 116 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 117 | while True: |
| 118 | line = arq.readline() |
| 119 | if line == '': |
| 120 | break |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 121 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 122 | args = line.split() |
Caio Elias | c6b5603 | 2014-09-19 14:12:48 -0300 | [diff] [blame] | 123 | |
| 124 | #checks for non-empty line |
| 125 | if len(args) == 0: |
| 126 | continue |
| 127 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 128 | h1, h2 = args.pop(0).split(':') |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 129 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 130 | link_dict = {} |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 131 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 132 | for arg in args: |
| 133 | arg_name, arg_value = arg.split('=') |
| 134 | key = arg_name |
| 135 | value = arg_value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 136 | if key in ['bw','jitter','max_queue_size']: |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 137 | value = int(value) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 138 | if key in ['loss']: |
| 139 | value = float(value) |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 140 | link_dict[key] = value |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 141 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 142 | links.append(confCCNLink(h1,h2,link_dict)) |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 143 | |
| 144 | |
carlosmscabral | f40ecd1 | 2013-02-01 18:15:58 -0200 | [diff] [blame] | 145 | return links |
carlosmscabral | 6d3dd60 | 2013-03-23 11:12:34 -0300 | [diff] [blame] | 146 | |
| 147 | |
| 148 | |