diff options
author | vmaffione <v.maffione@gmail.com> | 2017-04-07 12:49:15 +0000 |
---|---|---|
committer | vmaffione <v.maffione@gmail.com> | 2017-04-07 12:49:15 +0000 |
commit | bf2dea90994a20ec0e0dcc4a460facb380d28943 (patch) | |
tree | 7f4c4a3e2327d6551b052b080a0f0578e675fcc8 | |
parent | d5089ab766d81af4c7f02e20bb34a5571bceea3c (diff) | |
parent | d3983291c00f537583446fe4e2168bb9d3e83063 (diff) | |
download | rumba-bf2dea90994a20ec0e0dcc4a460facb380d28943.tar.gz rumba-bf2dea90994a20ec0e0dcc4a460facb380d28943.zip |
Merge branch 'master-conf' into 'master'
Method parsing .conf files
See merge request !19
-rw-r--r-- | rumba/model.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/rumba/model.py b/rumba/model.py index c8c00ab..25a1356 100644 --- a/rumba/model.py +++ b/rumba/model.py @@ -20,6 +20,7 @@ # MA 02110-1301 USA import abc +import re # Represents generic testbed info # @@ -304,6 +305,105 @@ class Experiment: # Generate missing information self.generate() + @staticmethod + def from_config_file(testbed, filename='demo.conf'): + """ + :type testbed: Testbed + :rtype: Experiment + :param testbed: the testbed for the experiment + :param filename: name of the .conf file + :return: an Experiment object + """ + + shims = {} + nodes = {} + difs = {} + with open(filename, 'r') as conf: + + line_cnt = 0 + + while 1: + line = conf.readline() + if line == '': + break + line_cnt += 1 + + line = line.replace('\n', '').strip() + + if line.startswith('#') or line == "": + continue + + m = re.match(r'\s*eth\s+([\w-]+)\s+(\d+)([GMK])bps\s+(\w.*)$', line) + if m: + shim = m.group(1) + speed = int(m.group(2)) + speed_unit = m.group(3).lower() + vm_list = m.group(4).split() + + if shim in shims or shim in difs: + print('Error: Line %d: shim %s already defined' + % (line_cnt, shim)) + continue + + if speed_unit == 'K': + speed = speed // 1000 + if speed_unit == 'G': + speed = speed * 1000 + + shims[shim] = {'name': shim, 'speed': speed, 'type': 'eth'} + + for vm in vm_list: + nodes.setdefault(vm, {'name': vm, 'difs': [], 'dif_registrations': {}, 'registrations': {}}) + nodes[vm]['difs'].append(shim) + continue + + m = re.match(r'\s*dif\s+([\w-]+)\s+([\w-]+)\s+(\w.*)$', line) + if m: + dif = m.group(1) + vm = m.group(2) + dif_list = m.group(3).split() + + if dif in shims: + print('Error: Line %d: dif %s already defined as shim' + % (line_cnt, dif)) + continue + + difs.setdefault(dif, {'name': dif}) # Other dict contents might be policies. + + if vm in nodes and dif in nodes[vm]['dif_registrations']: + print('Error: Line %d: vm %s in dif %s already specified' + % (line_cnt, vm, dif)) + continue + + nodes.setdefault(vm, {'name': vm, 'difs': [], 'dif_registrations': {}, 'registrations': {}}) + nodes[vm]['difs'].append(dif) + nodes[vm]['dif_registrations'][dif] = dif_list # It is not defined yet, per check above. + + continue + + # No match, spit a warning + print('Warning: Line %d unrecognized and ignored' % line_cnt) + + # File parsed + + parsed_difs = {} + + for shim_name, shim in shims.items(): + parsed_difs[shim_name] = (ShimEthDIF(shim_name, link_speed=shim['speed'])) + + for dif_name, dif in difs.items(): + parsed_difs[dif_name] = (NormalDIF(dif_name)) + + parsed_nodes = [] + for node, node_data in nodes.items(): + name = node_data['name'] + difs = [parsed_difs[x] for x in node_data['difs']] + dif_registrations = {parsed_difs[x]: [parsed_difs[y] for y in l] + for x, l in node_data['dif_registrations'].items()} + parsed_nodes.append(Node(name, difs, dif_registrations)) + + return Experiment(testbed=testbed, nodes=parsed_nodes) + def __repr__(self): s = "" for n in self.nodes: |