<s> from lxml import etree <EOL> from StringIO import StringIO <EOL> from openflow . optin_manager . sfa . util . xrn import Xrn , urn_to_hrn <EOL> from openflow . optin_manager . sfa . rspecs . rspec import RSpec <EOL> from openflow . optin_manager . sfa . rspecs . version_manager import VersionManager <EOL> xslt = '''<STR_LIT>''' <EOL> xslt_doc = etree . parse ( StringIO ( xslt ) ) <EOL> transform = etree . XSLT ( xslt_doc ) <EOL> class PGRSpecConverter : <EOL> @ staticmethod <EOL> def to_sfa_rspec ( rspec , content_type = None ) : <EOL> if not isinstance ( rspec , RSpec ) : <EOL> pg_rspec = RSpec ( rspec ) <EOL> else : <EOL> pg_rspec = rspec <EOL> version_manager = VersionManager ( ) <EOL> sfa_version = version_manager . _get_version ( '<STR_LIT>' , '<STR_LIT:1>' ) <EOL> sfa_rspec = RSpec ( version = sfa_version ) <EOL> networks = pg_rspec . version . get_networks ( ) <EOL> network_hrn = networks [ <NUM_LIT:0> ] [ "<STR_LIT:name>" ] <EOL> network_element = sfa_rspec . xml . add_element ( '<STR_LIT>' , name = network_hrn , id = network_hrn ) <EOL> pg_nodes_elements = pg_rspec . version . get_nodes ( ) <EOL> nodes_with_slivers = pg_rspec . version . get_nodes_with_slivers ( ) <EOL> i = <NUM_LIT:1> <EOL> for pg_node in pg_nodes_elements : <EOL> attribs = dict ( pg_node . items ( ) ) <EOL> attribs [ '<STR_LIT:id>' ] = '<STR_LIT:n>' + str ( i ) <EOL> node_element = network_element . add_element ( '<STR_LIT>' ) <EOL> for attrib in attribs : <EOL> if type ( attribs [ attrib ] ) == str : <EOL> node_element . set ( attrib , attribs [ attrib ] ) <EOL> urn = pg_node [ "<STR_LIT>" ] <EOL> if urn : <EOL> if type ( urn ) == list : <EOL> urn = urn [ <NUM_LIT:0> ] <EOL> hostname = Xrn . urn_split ( urn ) [ - <NUM_LIT:1> ] <EOL> hostname_element = node_element . add_element ( '<STR_LIT>' ) <EOL> hostname_element . set_text ( hostname ) <EOL> if hostname in nodes_with_slivers : <EOL> node_element . add_element ( '<STR_LIT>' ) <EOL> for hardware_type in pg_node [ "<STR_LIT>" ] : <EOL> if "<STR_LIT:name>" in hardware_type : <EOL> node_element . add_element ( "<STR_LIT>" , name = hardware_type [ "<STR_LIT:name>" ] ) <EOL> i = i + <NUM_LIT:1> <EOL> return sfa_rspec . toxml ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> print PGRSpecConverter . to_sfa_rspec ( sys . argv [ <NUM_LIT:1> ] ) </s>
