aboutsummaryrefslogtreecommitdiff
path: root/rumba/model.py
diff options
context:
space:
mode:
authorMarco Capitani <m.capitani@nextworks.it>2017-04-06 12:50:40 +0200
committerMarco Capitani <m.capitani@nextworks.it>2017-04-06 12:50:40 +0200
commit0e6ade3747dd363423f6cc4f0e5bd7d9f7259800 (patch)
tree682d0944a0f1b7714699af2ac8ed43a52a27252f /rumba/model.py
parentef723aedeba64f2b5e02acac43e34ac23cd8ddb7 (diff)
downloadrumba-0e6ade3747dd363423f6cc4f0e5bd7d9f7259800.tar.gz
rumba-0e6ade3747dd363423f6cc4f0e5bd7d9f7259800.zip
method for making an experiment from a .conf file with demo syntax
Diffstat (limited to 'rumba/model.py')
-rw-r--r--rumba/model.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/rumba/model.py b/rumba/model.py
index c8c00ab..c3fa39e 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 make_experiment(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: