3 from StringIO import StringIO
4 from sfa.util.xrn import *
5 from sfa.rspecs.pg_rspec import PGRSpec
6 from sfa.rspecs.sfa_rspec import SfaRSpec
8 xslt='''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
9 <xsl:output method="xml" indent="no"/>
11 <xsl:template match="/|comment()|processing-instruction()">
13 <xsl:apply-templates/>
17 <xsl:template match="*">
18 <xsl:element name="{local-name()}">
19 <xsl:apply-templates select="@*|node()"/>
23 <xsl:template match="@*">
24 <xsl:attribute name="{local-name()}">
25 <xsl:value-of select="."/>
31 xslt_doc=etree.parse(StringIO(xslt))
32 transform=etree.XSLT(xslt_doc)
34 class PGRSpecConverter:
37 def to_sfa_rspec(rspec):
38 if isinstance(rspec, PGRSpec):
41 pg_rspec = PGRSpec(rspec=rspec)
42 sfa_rspec = SfaRSpec()
45 network_urn = pg_rspec.get_network()
46 network, _ = urn_to_hrn(network_urn)
47 network_element = sfa_rspec.add_element('network', {'name': network, 'id': network})
50 pg_nodes_elements = pg_rspec.get_node_elements()
51 nodes_with_slivers = pg_rspec.get_nodes_with_slivers()
53 for pg_node_element in pg_nodes_elements:
54 attribs = dict(pg_node_element.attrib.items())
55 attribs['id'] = 'n'+str(i)
57 node_element = sfa_rspec.add_element('node', attribs, parent=network_element)
58 urn = pg_node_element.xpath('@component_id', namespaces=pg_rspec.namespaces)
61 hostname = Xrn.urn_split(urn)[-1]
62 hostname_element = sfa_rspec.add_element('hostname', parent=node_element, text=hostname)
63 if hostname in nodes_with_slivers:
64 sfa_rspec.add_element('sliver', parent=node_element)
66 urn_element = sfa_rspec.add_element('urn', parent=node_element, text=urn)
69 # just copy over remaining child elements
70 for child in pg_node_element.getchildren():
71 node_element.append(transform(child).getroot())
74 return sfa_rspec.toxml()
76 if __name__ == '__main__':
79 print PGRSpecConverter.to_sfa_rspec(sys.argv[1])