Added the possibility for users to define a set of information fields
[sfa.git] / sfa / rspecs / sfa_rspec.py
index 50d42bd..2c8a0c9 100755 (executable)
@@ -4,14 +4,21 @@ from StringIO import StringIO
 from sfa.rspecs.rspec import RSpec 
 from sfa.util.xrn import *
 from sfa.util.plxrn import hostname_to_urn
-from sfa.util.config import Config  
+from sfa.util.config import Config
+from sfa.rspecs.rspec_version import RSpecVersion  
 
 
+_version = { 'type': 'sfa', 
+             'version': '1' 
+}
+
+sfa_rspec_version = RSpecVersion(_version)
+
 class SfaRSpec(RSpec):
     xml = None
     header = '<?xml version="1.0"?>\n'
-    namespaces = {}
-    format = 'sfa'
+    version = sfa_rspec_version
+
     ###################
     # Parser
     ###################
@@ -31,8 +38,11 @@ class SfaRSpec(RSpec):
                 return name.getparent()
         return None
  
-    def get_node_elements(self):
-        return self.xml.xpath('//node')
+    def get_node_elements(self, network=None):
+        if network:
+            return self.xml.xpath('//network[@name="%s"]//node' % network)
+        else:
+            return self.xml.xpath('//node')
 
     def get_nodes(self, network=None):
         if network == None:
@@ -189,12 +199,27 @@ class SfaRSpec(RSpec):
             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').tet = str(interface['bwlimit']/1000)
+                        bwlimit = etree.SubElement(node_tag, 'bw_limit', units='kbps').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'])           
+                    #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'])
+
+                   #panos expose tags only if requested by the user using the -i option
+                   if self.user_options:
+                       #print "[sfa rspec] found tag ",tag," and have info ",self.user_options.get('info')
+                       if self.user_options.get('info') and tag['tagname'] in self.user_options.get('info'):
+                            tag_element = etree.SubElement(node_tag, tag['tagname'], value=tag['value'])
+
+                    if 'sfa' in tag['category'].split('/'):
+                        tag_element = etree.SubElement(node_tag, tag['tagname'], value=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)                
 
     def add_interfaces(self, interfaces):
         pass     
@@ -214,7 +239,7 @@ class SfaRSpec(RSpec):
             sliver_elem = etree.SubElement(node_elem, 'sliver')
             if 'tags' in sliver:
                 for tag in sliver['tags']:
-                    etree.SubElement(sliver_elem, tag['name'], value=tag['value'])
+                    etree.SubElement(sliver_elem, tag['tagname'], value=tag['value'])
 
     def remove_slivers(self, slivers, network=None, no_dupes=False):
         if not isinstance(slivers, list):