diff options
author | Marco Capitani <m.capitani@nextworks.it> | 2017-06-21 17:09:16 +0200 |
---|---|---|
committer | Marco Capitani <m.capitani@nextworks.it> | 2017-06-21 17:09:16 +0200 |
commit | 44f5aa5af732fcfdbb6c0eb1655606469a64aee8 (patch) | |
tree | 39cd3d968454fbe1f41df716e2c7e94a5cfdb14d /rumba/model.py | |
parent | 26ed0d7231ce681e6f2041760ba69406ffb6ee86 (diff) | |
parent | 67288b3505dab7e9ea74fc87d139301e945b4a78 (diff) | |
download | rumba-44f5aa5af732fcfdbb6c0eb1655606469a64aee8.tar.gz rumba-44f5aa5af732fcfdbb6c0eb1655606469a64aee8.zip |
Merge branch 'master' into storyboard-impl
Diffstat (limited to 'rumba/model.py')
-rw-r--r-- | rumba/model.py | 133 |
1 files changed, 121 insertions, 12 deletions
diff --git a/rumba/model.py b/rumba/model.py index 5215065..46a2351 100644 --- a/rumba/model.py +++ b/rumba/model.py @@ -126,25 +126,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): + def __init__(self, name, members=None, policy=None): DIF.__init__(self, name, members) - if policies is None: - policies = dict() - self.policies = policies + if policy is None: + policy = Policy(self) + self.policy = policy - 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 @@ -161,10 +166,12 @@ 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 to apply for that dif in this node +# # class Node: def __init__(self, name, difs=None, dif_registrations=None, - client=False): + client=False, policies=None): self.name = name if difs is None: difs = list() @@ -176,6 +183,12 @@ class Node: self.dif_registrations = dif_registrations self.ssh_config = SSHConfig(name) self.ipcps = [] + self.policies = dict() + if policies is None: + policies = dict() + for dif in self.difs: + if hasattr(dif, 'policy'): # check if the dif supports policies + self.policies[dif] = policies.get(dif, Policy(dif, self)) self.client = client self._validate() @@ -216,6 +229,8 @@ class Node: s += ", ".join(rl) s += " ]\n" + s += " Policies: [ " + return s def __hash__(self): @@ -230,11 +245,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): @@ -245,6 +267,15 @@ class Node: self.dif_registrations[upper].remove(lower) 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) + + def get_policy(self, dif): + return self.policies[dif] + # Base class representing an IPC Process to be created in the experiment # @@ -293,6 +324,84 @@ class ShimUDPIPCP(IPCP): # TODO: add IP and port +# 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 + 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 + + # + # Fetches effective policy info + # + def get_policies(self, component_name=None, policy_name=None): + policy = self._superimpose() + 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] + + # + # 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(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 # # @name [string] Name of the experiment |