From e80760cd31b6b247f2841ba86a4828fa3be13e56 Mon Sep 17 00:00:00 2001 From: smbaker Date: Sun, 4 Dec 2011 22:45:23 -0800 Subject: [PATCH] fix bugs when adding and removing sliver attributes --- sfa/rspecs/elements/versions/sfav1Sliver.py | 2 +- sfa/rspecs/versions/sfav1.py | 33 +++++++++++---------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/sfa/rspecs/elements/versions/sfav1Sliver.py b/sfa/rspecs/elements/versions/sfav1Sliver.py index 8a113f34..f0375336 100644 --- a/sfa/rspecs/elements/versions/sfav1Sliver.py +++ b/sfa/rspecs/elements/versions/sfav1Sliver.py @@ -34,7 +34,7 @@ class SFAv1Sliver: for elem in xml.iterchildren(): if elem.tag not in Sliver.fields: xml_element = XmlElement(elem, xml.namespaces) - instance = Element(xml_element) + instance = Element(fields=xml_element, element=elem) instance['name'] = elem.tag instance['value'] = elem.text attribs.append(instance) diff --git a/sfa/rspecs/versions/sfav1.py b/sfa/rspecs/versions/sfav1.py index affab84f..8f172bd8 100644 --- a/sfa/rspecs/versions/sfav1.py +++ b/sfa/rspecs/versions/sfav1.py @@ -104,17 +104,20 @@ class SFAv1(BaseVersion): return attributes - def add_sliver_attribute(self, hostname, name, value, network=None): - nodes = self.get_nodes({'component_id': '*%s*' % hostname}) - if not nodes: + def add_sliver_attribute(self, component_id, name, value, network=None): + nodes = self.get_nodes({'component_id': '*%s*' % component_id}) + if nodes is not None and isinstance(nodes, list) and len(nodes) > 0: node = nodes[0] slivers = SFAv1Sliver.get_slivers(node) if slivers: sliver = slivers[0] - SFAv1Sliver.add_attribute(sliver, name, value) + SFAv1Sliver.add_sliver_attribute(sliver, name, value) + else: + # should this be an assert / raise an exception? + print "WARNING: failed to find component_id", component_id - def get_sliver_attributes(self, hostname, network=None): - nodes = self.get_nodes({'component_id': '*%s*' %hostname}) + def get_sliver_attributes(self, component_id, network=None): + nodes = self.get_nodes({'component_id': '*%s*' % component_id}) attribs = [] if nodes is not None and isinstance(nodes, list) and len(nodes) > 0: node = nodes[0] @@ -124,11 +127,13 @@ class SFAv1(BaseVersion): attribs = SFAv1Sliver.get_sliver_attributes(sliver.element) return attribs - def remove_sliver_attribute(self, hostname, name, value, network=None): - attribs = self.get_sliver_attributes(hostname) + def remove_sliver_attribute(self, component_id, name, value, network=None): + attribs = self.get_sliver_attributes(component_id) for attrib in attribs: if attrib['name'] == name and attrib['value'] == value: - attrib.element.delete() + #attrib.element.delete() + parent = attrib.element.getparent() + parent.remove(attrib.element) def add_default_sliver_attribute(self, name, value, network=None): if network: @@ -153,14 +158,12 @@ class SFAv1(BaseVersion): return SFAv1Sliver.get_sliver_attributes(defaults[0]) def remove_default_sliver_attribute(self, name, value, network=None): - if network: - defaults = self.xml.xpath("//network[@name='%s']/sliver_defaults" % network) - else: - defaults = self.xml.xpath("//sliver_defaults" % network) - attribs = SFAv1Sliver.get_sliver_attributes(defaults) + attribs = self.get_default_sliver_attributes(network) for attrib in attribs: if attrib['name'] == name and attrib['value'] == value: - attrib.element.delete() + #attrib.element.delete() + parent = attrib.element.getparent() + parent.remove(attrib.element) # Links -- 2.43.0