From 3081d070cda223afd548645143142e1104b07d83 Mon Sep 17 00:00:00 2001 From: Marco Capitani Date: Fri, 28 Apr 2017 12:10:00 +0200 Subject: model: added policy class + policy-oriented API fixes + Added policy class + Adapted NormalDIF class to use new policy class + NormalDIF constructor argument policy has changed format (parameters dict is now mandatory, added empty dicts where it was not passed) + Added Node.policies field (type: dict[DIF -> Policy]) --- rumba/model.py | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 76 insertions(+), 11 deletions(-) (limited to 'rumba/model.py') diff --git a/rumba/model.py b/rumba/model.py index 9187fcb..442933c 100644 --- a/rumba/model.py +++ b/rumba/model.py @@ -112,25 +112,30 @@ class ShimEthDIF(DIF): # Normal DIF # -# @policies [dict] Policies of the normal DIF +# @policies [dict] Policies of the normal DIF. Format: +# dict( componentName: str --> comp_policy: +# dict( policy_name: str --> parameters: +# dict( name: str --> value: str ))) # class NormalDIF(DIF): def __init__(self, name, members=None, policies=None): DIF.__init__(self, name, members) if policies is None: - policies = dict() - self.policies = policies + self.policy = Policy(self) + self.policy = Policy(self, policies=policies) - def add_policy(self, comp, pol): - self.policies[comp] = pol + def add_policy(self, comp, pol, **params): + self.policy.add_policy(comp, pol, **params) - def del_policy(self, comp): - del self.policies[comp] + def del_policy(self, comp=None, policy_name=None): + self.policy.del_policy(comp, policy_name) def show(self): s = DIF.__repr__(self) - for comp, pol in self.policies.items(): - s += "\n Component %s has policy %s" % (comp, pol) + for comp, pol_dict in self.policy.get_policies().items(): + for pol, params in pol_dict.items(): + s += "\n Component %s has policy %s with params %s" \ + % (comp, pol, params) return s @@ -149,10 +154,11 @@ class SSHConfig: # @dif_registrations: Which DIF is registered in which DIF # @registrations: Registrations of names in DIFs # @bindings: Binding of names on the processing system +# @policies: dict of dif -> policy dict to apply for that dif in this node # class Node: def __init__(self, name, difs=None, dif_registrations=None, - registrations=None, bindings=None): + registrations=None, bindings=None, policies=None): self.name = name if difs is None: difs = list() @@ -170,6 +176,13 @@ class Node: self.bindings = bindings self.ssh_config = SSHConfig(name) self.ipcps = [] + if policies is None: + policies = dict() + self.policies = dict() + for dif in self.difs: + if hasattr(dif, 'policy'): + self.policies[dif] = \ + Policy(dif, self, policies.get(dif.name, {})) self._validate() @@ -195,7 +208,7 @@ class Node: for dif in self.registrations[appl]: self._undeclared_dif(dif) - def __repr__(self): + def __repr__(self): # TODO add policies in repr? s = "Node " + self.name + ":\n" s += " DIFs: [ " @@ -240,11 +253,18 @@ class Node: def add_dif(self, dif): self.difs.append(dif) dif.add_member(self) + if hasattr(dif, 'policy'): + self.policies[dif] = Policy(dif, self) self._validate() def del_dif(self, dif): self.difs.remove(dif) dif.del_member(self) + try: + del self.policies[dif] + except KeyError: + # It was not in there, so nothing to do + pass self._validate() def add_dif_registration(self, upper, lower): @@ -271,6 +291,12 @@ class Node: del self.bindings[name] self._validate() + def add_policy(self, dif, component_name, policy_name, **parameters): + self.policies[dif].add_policy(component_name, policy_name, **parameters) + + def del_policy(self, dif, component_name=None, policy_name=None): + self.policies[dif].del_policy(component_name, policy_name) + # Base class representing an IPC Process to be created in the experiment # @@ -319,6 +345,45 @@ class ShimUDPIPCP(IPCP): # TODO: add IP and port +# Class representing DIF and Node policies +class Policy(object): + def __init__(self, dif, node=None, policies=None): + self.dif = dif # type: NormalDIF + self.node = node + if policies is None: + self._dict = dict() + else: + self._dict = policies + + def add_policy(self, component_name, policy_name, **parameters): + self._dict.setdefault(component_name, dict())[policy_name] = parameters + + def get_policies(self, component_name=None, policy_name=None): + if self.node is not None: + policy = self._superimpose(self.dif.policy) + else: + policy = self + if component_name is None: + return policy._dict + elif policy_name is None: + return policy._dict[component_name] + else: + return policy._dict[component_name][policy_name] + + def del_policy(self, component_name=None, policy_name=None): + if component_name is None: + self._dict = dict() + elif policy_name is None: + del self._dict[component_name] + else: + del self._dict[component_name][policy_name] + + def _superimpose(self, other): + base = dict(other._dict) + base.update(self._dict) + return Policy(base) + + # Base class for ARCFIRE experiments # # @name [string] Name of the experiment -- cgit v1.2.3 From e36189f8ba98d8a1b254a8dd300f59d5c12a9430 Mon Sep 17 00:00:00 2001 From: Marco Capitani Date: Tue, 13 Jun 2017 12:44:04 +0200 Subject: IRATI: added per-node policy support, and revamped policy data model. additional: + fixed small bug rlated to vhost checking in qemu. + example scripts now correctly use policies. --- examples/example.py | 6 ++- rumba/model.py | 78 ++++++++++++++++++++++++++++--------- rumba/prototypes/irati.py | 15 ++++--- rumba/prototypes/irati_templates.py | 4 +- rumba/testbeds/qemu.py | 11 +++--- 5 files changed, 81 insertions(+), 33 deletions(-) (limited to 'rumba/model.py') diff --git a/examples/example.py b/examples/example.py index 8a68aab..567852f 100755 --- a/examples/example.py +++ b/examples/example.py @@ -20,8 +20,10 @@ import rumba.log as log log.set_logging_level('DEBUG') -n1 = NormalDIF("n1", policies = {"rmt.pff": {"lfa": {}}, - "security-manager": {"passwd": {}}}) +n1 = NormalDIF("n1") + +n1.add_policy("rmt.pff", "lfa") +n1.add_policy("security-manager", "passwd") e1 = ShimEthDIF("e1") diff --git a/rumba/model.py b/rumba/model.py index e0f1dcc..affdcbf 100644 --- a/rumba/model.py +++ b/rumba/model.py @@ -20,9 +20,6 @@ # MA 02110-1301 USA import abc -import random - -import time import rumba.log as log @@ -128,11 +125,11 @@ class ShimEthDIF(DIF): # dict( name: str --> value: str ))) # class NormalDIF(DIF): - def __init__(self, name, members=None, policies=None): + def __init__(self, name, members=None, policy=None): DIF.__init__(self, name, members) - if policies is None: - self.policy = Policy(self) - self.policy = Policy(self, policies=policies) + if policy is None: + policy = Policy(self) + self.policy = policy def add_policy(self, comp, pol, **params): self.policy.add_policy(comp, pol, **params) @@ -162,7 +159,7 @@ class SSHConfig: # # @difs: DIFs the node will have an IPCP in # @dif_registrations: Which DIF is registered in which DIF -# @policies: dict of dif -> policy dict to apply for that dif in this node +# @policies: dict of dif -> policy to apply for that dif in this node # # class Node: @@ -179,13 +176,12 @@ class Node: self.dif_registrations = dif_registrations self.ssh_config = SSHConfig(name) self.ipcps = [] + self.policies = dict() if policies is None: policies = dict() - self.policies = dict() for dif in self.difs: - if hasattr(dif, 'policy'): - self.policies[dif] = \ - Policy(dif, self, policies.get(dif.name, {})) + if hasattr(dif, 'policy'): # check if the dif supports policies + self.policies[dif] = policies.get(dif, Policy(dif, self)) self.client = client self._validate() @@ -208,7 +204,7 @@ class Node: for lower in self.dif_registrations[upper]: self._undeclared_dif(lower) - def __repr__(self): # TODO add policies in repr + def __repr__(self): s = "Node " + self.name + ":\n" s += " DIFs: [ " @@ -226,6 +222,8 @@ class Node: s += ", ".join(rl) s += " ]\n" + s += " Policies: [ " + return s def __hash__(self): @@ -268,6 +266,9 @@ class Node: def del_policy(self, dif, component_name=None, policy_name=None): self.policies[dif].del_policy(component_name, policy_name) + def get_policy(self, dif): + return self.policies[dif] + # Base class representing an IPC Process to be created in the experiment # @@ -317,6 +318,10 @@ class ShimUDPIPCP(IPCP): # Class representing DIF and Node policies +# +# @dif: the dif this policy is applied to. +# @node: the node +# class Policy(object): def __init__(self, dif, node=None, policies=None): self.dif = dif # type: NormalDIF @@ -329,11 +334,11 @@ class Policy(object): def add_policy(self, component_name, policy_name, **parameters): self._dict.setdefault(component_name, dict())[policy_name] = parameters + # + # Fetches effective policy info + # def get_policies(self, component_name=None, policy_name=None): - if self.node is not None: - policy = self._superimpose(self.dif.policy) - else: - policy = self + policy = self._superimpose() if component_name is None: return policy._dict elif policy_name is None: @@ -349,10 +354,45 @@ class Policy(object): else: del self._dict[component_name][policy_name] - def _superimpose(self, other): + # + # Merges this policy into that of its dif, obtaining + # the effective policy acting on self.node. + # + def _superimpose(self): + if self.node is None: + return self + other = self.dif.policy base = dict(other._dict) base.update(self._dict) - return Policy(base) + return Policy(self.dif, self.node, base) + + def __eq__(self, other): + if not isinstance(other, Policy): + return False + else: + return other.dif == self.dif \ + and other.node == self.node \ + and other._dict == self._dict + + def __str__(self): + node_str = (" Node: " + self.node) if self.node is not None else "" + return "Policy[Dif: %(dif)s,%(node_str)s Dict: %(dict)s]" \ + % {"dif": self.dif, "node_str": node_str, "dict": self._dict} + + def __repr__(self): + node_str = (" Node: " + self.node) if self.node is not None else "" + s = "Policy[ Dif: %(dif)s,%(node_str)s" \ + % {"dif": self.dif, "node_str": node_str} + comps = [] + for component in self._dict: + for policy in self._dict[component]: + comps.append("\n Component %s has policy %s with params %s" + % (component, + policy, + self._dict[component][policy])) + s += ",".join(comps) + s += "\n]\n" + return s # Base class for ARCFIRE experiments diff --git a/rumba/prototypes/irati.py b/rumba/prototypes/irati.py index 42afe3b..c01e413 100644 --- a/rumba/prototypes/irati.py +++ b/rumba/prototypes/irati.py @@ -355,11 +355,16 @@ class Experiment(mod.Experiment): "apName": "%s.%s.IPCP" % (dif.name, node_name), "apInstance": "1", "address": 16 + node2id_map[node_name]}) - for path, ps in dif.policies.items(): - # if policy['nodes'] == [] or vmname in policy['nodes']: - # TODO: manage per-node-policies - irati_templates.translate_policy( - difconfs[dif.name][node_name], path, ps, parms=[]) + policy_dict = node.get_policy(dif).get_policies() + for component in policy_dict: + for policy_name in policy_dict[component]: + params = policy_dict[component][policy_name].items() + irati_templates.translate_policy( + difconfs[dif.name][node_name], + component, + policy_name, + params + ) # Dump the DIF Allocator map with open(self.conf_dir('da.map'), 'w') as da_map_file: diff --git a/rumba/prototypes/irati_templates.py b/rumba/prototypes/irati_templates.py index b8d9788..2a5c0f0 100644 --- a/rumba/prototypes/irati_templates.py +++ b/rumba/prototypes/irati_templates.py @@ -238,7 +238,7 @@ def ps_set(d, k, v, parms): if d[k]["name"] == v and "parameters" in d[k]: cur_names = [p["name"] for p in d[k]["parameters"]] for p in parms: - name, value = p.split('=') + name, value = p if name in cur_names: for i in range(len(d[k]["parameters"])): if d[k]["parameters"][i]["name"] == name: @@ -249,7 +249,7 @@ def ps_set(d, k, v, parms): elif len(parms) > 0: d[k]["parameters"] = [ - {'name': p.split('=')[0], 'value': p.split('=')[1]} + {'name': p[0], 'value': p[1]} for p in parms] d[k]["name"] = v diff --git a/rumba/testbeds/qemu.py b/rumba/testbeds/qemu.py index df02ab6..1d449dc 100644 --- a/rumba/testbeds/qemu.py +++ b/rumba/testbeds/qemu.py @@ -111,11 +111,12 @@ class Testbed(mod.Testbed): if os.geteuid() != 0: try: subprocess.check_call(["sudo", "-v"]) - if not os.access("/dev/vhost-net", os.R_OK) \ - or not os.access("/dev/vhost-net", os.W_OK) \ - or not os.access("/dev/kvm", os.R_OK) \ - or not os.access("/dev/kvm", os.W_OK): - raise Exception('Cannot open vhost device. Make sure it is' + if self.vhost and \ + (not os.access("/dev/vhost-net", os.R_OK) + or not os.access("/dev/vhost-net", os.W_OK) + or not os.access("/dev/kvm", os.R_OK) + or not os.access("/dev/kvm", os.W_OK)): + raise Exception('Cannot open vhost device. Make sure it is ' 'available and you have rw permissions ' 'on /dev/vhost-net') except subprocess.CalledProcessError: -- cgit v1.2.3