diff options
-rwxr-xr-x | examples/example.py | 5 | ||||
-rw-r--r-- | rumba/model.py | 88 |
2 files changed, 80 insertions, 13 deletions
diff --git a/examples/example.py b/examples/example.py index 1acc883..8a68aab 100755 --- a/examples/example.py +++ b/examples/example.py @@ -19,8 +19,9 @@ import rumba.log as log log.set_logging_level('DEBUG') -n1 = NormalDIF("n1", policies = {"rmt.pff": "lfa", - "security-manager": "passwd"}) + +n1 = NormalDIF("n1", policies = {"rmt.pff": {"lfa": {}}, + "security-manager": {"passwd": {}}}) e1 = ShimEthDIF("e1") diff --git a/rumba/model.py b/rumba/model.py index 9818ac7..e0f1dcc 100644 --- a/rumba/model.py +++ b/rumba/model.py @@ -122,25 +122,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 @@ -157,10 +162,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 dict 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() @@ -172,6 +179,13 @@ class Node: self.dif_registrations = dif_registrations 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.client = client self._validate() @@ -194,7 +208,7 @@ class Node: for lower in self.dif_registrations[upper]: self._undeclared_dif(lower) - def __repr__(self): + def __repr__(self): # TODO add policies in repr s = "Node " + self.name + ":\n" s += " DIFs: [ " @@ -226,11 +240,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): @@ -241,6 +262,12 @@ 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) + # Base class representing an IPC Process to be created in the experiment # @@ -289,6 +316,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 |