initial checkin of rspec elements
Tony Mack [Mon, 27 Jun 2011 20:44:27 +0000 (16:44 -0400)]
12 files changed:
setup.py
sfa/managers/aggregate_manager_pl.py
sfa/rspecs/elements/.element.py.swp [new file with mode: 0644]
sfa/rspecs/elements/PGv1Network.py [new file with mode: 0644]
sfa/rspecs/elements/SFAv1Network.py [new file with mode: 0755]
sfa/rspecs/elements/SFAv1Node.py [new file with mode: 0755]
sfa/rspecs/elements/__init__.py [new file with mode: 0644]
sfa/rspecs/elements/element.py [new file with mode: 0644]
sfa/rspecs/elements/link.py [new file with mode: 0644]
sfa/rspecs/elements/network.py [new file with mode: 0644]
sfa/rspecs/elements/node.py [new file with mode: 0644]
sfa/rspecs/elements/sliver.py [new file with mode: 0644]

index 2800555..e91c5dc 100755 (executable)
--- a/setup.py
+++ b/setup.py
@@ -44,6 +44,7 @@ package_dirs = [
     'sfa/util', 
     'sfa/managers',
     'sfa/rspecs',
+    'sfa/rspecs/elements',
     'sfatables',
     'sfatables/commands',
     'sfatables/processors',
index 3b81ba6..a2449b2 100644 (file)
@@ -196,7 +196,7 @@ def CreateSliver(api, slice_xrn, creds, rspec_string, users, call_id):
     added_nodes = list(set(requested_slivers).difference(current_slivers))
 
     # get sliver attributes
-    slice_attributes = rspec.get_slice_attributes()
+    slice_attributes = rspec.get_sliver_attributes()
 
     try:
         if peer:
diff --git a/sfa/rspecs/elements/.element.py.swp b/sfa/rspecs/elements/.element.py.swp
new file mode 100644 (file)
index 0000000..65a105c
Binary files /dev/null and b/sfa/rspecs/elements/.element.py.swp differ
diff --git a/sfa/rspecs/elements/PGv1Network.py b/sfa/rspecs/elements/PGv1Network.py
new file mode 100644 (file)
index 0000000..c4f3c87
--- /dev/null
@@ -0,0 +1,7 @@
+from sfa.rspecs.elements.networks import Network
+
+class PGv2Network(Network):
+
+    def get_networks_names(self):
+        networks = self.xml.xpath('//rspecv2:node[@component_manager_id]/@component_manager_id', namespaces=self.namespaces)
+        return list(set(networks))
diff --git a/sfa/rspecs/elements/SFAv1Network.py b/sfa/rspecs/elements/SFAv1Network.py
new file mode 100755 (executable)
index 0000000..4bfa26d
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+from sfa.rspecs.elements.network import Network
+
+class SFAv1Network(Network):
+
+    def get_network_elements(self):
+        return self.root_node.xpath('//network')        
+
+    def get_networks(self):
+        network_elems = self.get_network_elements()
+        networks = [self.get_attributes(network_elem) \
+                    for network_elem in network_elems]
+        return networks
+
+    def add_networks(self, networks):
+        if not isinstance(networks, list):
+            networks = [networks]
+        return self.add_element('network', {'id': network}, self.root_node)
+
+
+
+if __name__ == '__main__':
+    import sys
+    from lxml import etree
+    args = sys.argv[1:]
+    filename = args[0]
+
+    root_node = etree.parse(filename)
+    network = SFAv1Network(root_node)
+    print network.get_networks()
+
+    
diff --git a/sfa/rspecs/elements/SFAv1Node.py b/sfa/rspecs/elements/SFAv1Node.py
new file mode 100755 (executable)
index 0000000..06dcfa7
--- /dev/null
@@ -0,0 +1,83 @@
+#!/usr/bin/python
+
+from sfa.rspecs.elements.node import Node
+
+class SFAv1Node(Node):
+
+    def get_node_elements(self, network=None):
+        if network:
+            nodes = self.root_node.xpath('//network[@name="%s"]//node' % network)
+        else:
+            nodes = self.root_node.xpath('//node')
+        return nodes
+
+    def get_nodes(self, network=None):
+        node_elems = self.get_node_elements(network)
+        nodes = [self.get_attributes(node_elem, recursive=True) \
+                 for node_elem in node_elems]
+        return nodes
+
+    def add_nodes(self, nodes, network=None, no_dupes=False):
+        if not isinstance(nodes, list):
+            nodes = [nodes]
+        for node in nodes:
+            if no_dupes and \
+              self.get_node_element(node['hostname']):
+                # node already exists
+                continue
+
+            network_tag = self.root_node
+            if 'network' in node:
+                network = node['network']
+                network_tags = self.root_node.xpath('//network[@name="%s"]' % network)
+                if not network_tags:
+                    #network_tag = etree.SubElement(self.root_node, 'network', name=network)
+                    network_tag = self.add_element('network', {'name': network}, self.root_node)
+                else:
+                    network_tag = network_tags[0]
+
+            #node_tag = etree.SubElement(network_tag, 'node')
+            node_tag = self.add_element('node', parent=network_tag)
+            if 'network' in node:
+                node_tag.set('component_manager_id', network)
+            if 'urn' in node:
+                node_tag.set('component_id', node['urn'])
+            if 'site_urn' in node:
+                node_tag.set('site_id', node['site_urn'])
+            if 'node_id' in node:
+                node_tag.set('node_id', 'n'+str(node['node_id']))
+            if 'hostname' in node:
+                #hostname_tag = etree.SubElement(node_tag, 'hostname').text = node['hostname']
+                hostname_tag = self.add_element('hostname', parent=node_tag)
+                hostname_tag.text = node['hostname']
+            if 'interfaces' in node:
+                for interface in node['interfaces']:
+                    if 'bwlimit' in interface and interface['bwlimit']:
+                        #bwlimit = etree.SubElement(node_tag, 'bw_limit', units='kbps').text = str(interface['bwlimit']/1000)
+                        bwlimit_tag = self.add_element('bw_limit', {'units': 'kbps'}, parent=node_tag)
+                        bwlimit_tag.text = str(interface['bwlimit']/1000)
+            if 'tags' in node:
+                for tag in node['tags']:
+                   # expose this hard wired list of tags, plus the ones that are marked 'sfa' in their category
+                   if tag['tagname'] in ['fcdistro', 'arch'] or 'sfa' in tag['category'].split('/'):
+                        #tag_element = etree.SubElement(node_tag, tag['tagname'], value=tag['value'])
+                        tag_element = self.add_element(tag['tagname'], parent=node_tag)
+                        tag_element.text = tag['value']
+
+            if 'site' in node:
+                longitude = str(node['site']['longitude'])
+                latitude = str(node['site']['latitude'])
+                #location = etree.SubElement(node_tag, 'location', country='unknown', \
+                #                            longitude=longitude, latitude=latitude) 
+                location_attrs = {'country': 'unknown', 'longitude': longitude, 'latitude': latitude} 
+                self.add_element('location', location_attrs, node_tag) 
+
+if __name__ == '__main__':
+    import sys
+    from lxml import etree
+    args = sys.argv[1:]
+    filename = args[0]
+
+    root_node = etree.parse(filename)
+    network = SFAv1Node(root_node)
+    print network.get_nodes()
diff --git a/sfa/rspecs/elements/__init__.py b/sfa/rspecs/elements/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/sfa/rspecs/elements/element.py b/sfa/rspecs/elements/element.py
new file mode 100644 (file)
index 0000000..495ff3e
--- /dev/null
@@ -0,0 +1,82 @@
+from lxml import etree
+
+class Element:
+    def __init__(self, root_node, namespaces = None):
+        self.root_node = root_node
+        self.namespaces = namespaces
+
+    def xpath(self, xpath):
+        return this.root_node.xpath(xpath, namespaces=self.namespaces) 
+
+    def add_element(self, name, attrs={}, parent=None, text=""):
+        """
+        Generic wrapper around etree.SubElement(). Adds an element to
+        specified parent node. Adds element to root node is parent is
+        not specified.
+        """
+        if parent == None:
+            parent = self.root_node
+        element = etree.SubElement(parent, name)
+        if text:
+            element.text = text
+        if isinstance(attrs, dict):
+            for attr in attrs:
+                element.set(attr, attrs[attr])
+        return element
+
+    def remove_element(self, element_name, root_node = None):
+        """
+        Removes all occurences of an element from the tree. Start at
+        specified root_node if specified, otherwise start at tree's root.
+        """
+        if not root_node:
+            root_node = self.root_node
+
+        if not element_name.startswith('//'):
+            element_name = '//' + element_name
+
+        elements = root_node.xpath('%s ' % element_name, namespaces=self.namespaces)
+        for element in elements:
+            parent = element.getparent()
+            parent.remove(element)
+
+    
+    def add_attribute(self, elem, name, value):
+        """
+        Add attribute to specified etree element
+        """
+        opt = etree.SubElement(elem, name)
+        opt.text = value
+
+    def remove_attribute(self, elem, name, value):
+        """
+        Removes an attribute from an element
+        """
+        if not elem == None:
+            opts = elem.iterfind(name)
+            if opts is not None:
+                for opt in opts:
+                    if opt.text == value:
+                        elem.remove(opt)
+
+    def get_attributes(self, elem=None, recursive=False):
+        if elem == None:
+            elem = self.root_node
+        attrs = dict(elem.attrib)
+        attrs['text'] = str(elem.text).strip()
+        if recursive:
+            for child_elem in list(elem):
+                attrs[str(child_elem.tag)] = self.get_attributes(child_elem, recursive)
+             
+        return attrs
+    
+    def attributes_list(self, elem):
+        # convert a list of attribute tags into list of tuples
+        # (tagnme, text_value)
+        opts = []
+        if not elem == None:
+            for e in elem:
+                opts.append((e.tag, e.text))
+        return opts
+
+    
diff --git a/sfa/rspecs/elements/link.py b/sfa/rspecs/elements/link.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/sfa/rspecs/elements/network.py b/sfa/rspecs/elements/network.py
new file mode 100644 (file)
index 0000000..6a358a4
--- /dev/null
@@ -0,0 +1,11 @@
+from sfa.rspecs.elements.element import Element
+from sfa.util.sfalogging import logger
+
+class Network(Element):
+
+    def get_networks(*args, **kwds):
+        logger.info("sfa.rspecs.networks: get_networks not implemented")
+
+    def add_networks(*args, **kwds):
+        logger.info("sfa.rspecs.networks: add_network not implemented")
+        
diff --git a/sfa/rspecs/elements/node.py b/sfa/rspecs/elements/node.py
new file mode 100644 (file)
index 0000000..db6e119
--- /dev/null
@@ -0,0 +1,13 @@
+from sfa.rspecs.elements.element import Element
+from sfa.util.faults import SfaNotImplemented 
+from sfa.util.sfalogging import logger
+class Node(Element):
+
+    def get_nodes(*args):
+        logger.info("sfa.rspecs.nodes: get_nodes not implemented") 
+    
+    def add_nodes(*args):
+        logger.info("sfa.rspecs.nodes: add_nodes not implemented") 
+                
+      
diff --git a/sfa/rspecs/elements/sliver.py b/sfa/rspecs/elements/sliver.py
new file mode 100644 (file)
index 0000000..a264f01
--- /dev/null
@@ -0,0 +1,17 @@
+from sfa.rspecs.elements.element import Element
+from sfa.util.sfalogging import logger
+
+class Slivers(Element):
+
+    def get_slivers(*args, **kwds):
+        logger.info("sfa.rspecs.slivers: get_slivers not implemented")
+
+    def add_slivers(*args, **kwds):
+        logger.info("sfa.rspecs.slivers: add_slivers not implemented")
+
+    def get_sliver_defaults(*args, **kwds):    
+        logger.info("sfa.rspecs.slivers: get_sliver_defaults not implemented")
+
+    
+    def add_sliver_defaults(*args, **kwds):
+        logger.info("sfa.rspecs.slivers: add_sliver_defaults not implemented")