<s> from copy import deepcopy <EOL> from lxml import etree <EOL> from openflow . optin_manager . sfa . util . sfalogging import logger <EOL> from openflow . optin_manager . sfa . util . xrn import hrn_to_urn , urn_to_hrn <EOL> from openflow . optin_manager . sfa . rspecs . version import RSpecVersion <EOL> from openflow . optin_manager . sfa . rspecs . elements . element import Element <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . pgv2Link import PGv2Link <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . sfav1Node import SFAv1Node <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . sfav1Sliver import SFAv1Sliver <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . sfav1Lease import SFAv1Lease <EOL> class SFAv1 ( RSpecVersion ) : <EOL> enabled = True <EOL> type = '<STR_LIT>' <EOL> content_type = '<STR_LIT:*>' <EOL> version = '<STR_LIT:1>' <EOL> schema = None <EOL> namespace = None <EOL> extensions = { } <EOL> namespaces = None <EOL> template = '<STR_LIT>' % type <EOL> def get_networks ( self ) : <EOL> network_elems = self . xml . xpath ( '<STR_LIT>' ) <EOL> networks = [ network_elem . get_instance ( fields = [ '<STR_LIT:name>' , '<STR_LIT>' ] ) for network_elem in network_elems ] <EOL> return networks <EOL> def add_network ( self , network ) : <EOL> network_tags = self . xml . xpath ( '<STR_LIT>' % network ) <EOL> if not network_tags : <EOL> network_tag = self . xml . add_element ( '<STR_LIT>' , name = network ) <EOL> else : <EOL> network_tag = network_tags [ <NUM_LIT:0> ] <EOL> return network_tag <EOL> def get_nodes ( self , filter = None ) : <EOL> return SFAv1Node . get_nodes ( self . xml , filter ) <EOL> def get_nodes_with_slivers ( self ) : <EOL> return SFAv1Node . get_nodes_with_slivers ( self . xml ) <EOL> def add_nodes ( self , nodes , network = None , no_dupes = False ) : <EOL> SFAv1Node . add_nodes ( self . xml , nodes ) <EOL> def merge_node ( self , source_node_tag , network , no_dupes = False ) : <EOL> if no_dupes and self . get_node_element ( node [ '<STR_LIT>' ] ) : <EOL> return <EOL> network_tag = self . add_network ( network ) <EOL> network_tag . append ( deepcopy ( source_node_tag ) ) <EOL> def add_slivers ( self , hostnames , attributes = [ ] , sliver_urn = None , append = False ) : <EOL> network_tags = self . xml . xpath ( '<STR_LIT>' ) <EOL> if network_tags : <EOL> network_tag = network_tags [ <NUM_LIT:0> ] <EOL> network_tag . set ( '<STR_LIT>' , urn_to_hrn ( sliver_urn ) [ <NUM_LIT:0> ] ) <EOL> sliver = { '<STR_LIT:name>' : sliver_urn , <EOL> '<STR_LIT>' : attributes } <EOL> for hostname in hostnames : <EOL> if sliver_urn : <EOL> sliver [ '<STR_LIT:name>' ] = sliver_urn <EOL> node_elems = self . get_nodes ( { '<STR_LIT>' : '<STR_LIT>' % hostname } ) <EOL> if not node_elems : <EOL> continue <EOL> node_elem = node_elems [ <NUM_LIT:0> ] <EOL> SFAv1Sliver . add_slivers ( node_elem . element , sliver ) <EOL> if not append : <EOL> for node_elem in self . get_nodes ( ) : <EOL> if not node_elem [ '<STR_LIT>' ] : <EOL> parent = node_elem . element . getparent ( ) <EOL> parent . remove ( node_elem . element ) <EOL> def remove_slivers ( self , slivers , network = None , no_dupes = False ) : <EOL> SFAv1Node . remove_slivers ( self . xml , slivers ) <EOL> def get_slice_attributes ( self , network = None ) : <EOL> attributes = [ ] <EOL> nodes_with_slivers = self . get_nodes_with_slivers ( ) <EOL> for default_attribute in self . get_default_sliver_attributes ( network ) : <EOL> attribute = default_attribute . copy ( ) <EOL> attribute [ '<STR_LIT>' ] = None <EOL> attributes . append ( attribute ) <EOL> for node in nodes_with_slivers : <EOL> nodename = node [ '<STR_LIT>' ] <EOL> sliver_attributes = self . get_sliver_attributes ( nodename , network ) <EOL> for sliver_attribute in sliver_attributes : <EOL> sliver_attribute [ '<STR_LIT>' ] = nodename <EOL> attributes . append ( sliver_attribute ) <EOL> return attributes <EOL> def add_sliver_attribute ( self , component_id , name , value , network = None ) : <EOL> nodes = self . get_nodes ( { '<STR_LIT>' : '<STR_LIT>' % component_id } ) <EOL> if nodes is not None and isinstance ( nodes , list ) and len ( nodes ) > <NUM_LIT:0> : <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> slivers = SFAv1Sliver . get_slivers ( node ) <EOL> if slivers : <EOL> sliver = slivers [ <NUM_LIT:0> ] <EOL> SFAv1Sliver . add_sliver_attribute ( sliver , name , value ) <EOL> else : <EOL> logger . error ( "<STR_LIT>" % component_id ) <EOL> def get_sliver_attributes ( self , component_id , network = None ) : <EOL> nodes = self . get_nodes ( { '<STR_LIT>' : '<STR_LIT>' % component_id } ) <EOL> attribs = [ ] <EOL> if nodes is not None and isinstance ( nodes , list ) and len ( nodes ) > <NUM_LIT:0> : <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> slivers = SFAv1Sliver . get_slivers ( node . element ) <EOL> if slivers is not None and isinstance ( slivers , list ) and len ( slivers ) > <NUM_LIT:0> : <EOL> sliver = slivers [ <NUM_LIT:0> ] <EOL> attribs = SFAv1Sliver . get_sliver_attributes ( sliver . element ) <EOL> return attribs <EOL> def remove_sliver_attribute ( self , component_id , name , value , network = None ) : <EOL> attribs = self . get_sliver_attributes ( component_id ) <EOL> for attrib in attribs : <EOL> if attrib [ '<STR_LIT:name>' ] == name and attrib [ '<STR_LIT:value>' ] == value : <EOL> parent = attrib . element . getparent ( ) <EOL> parent . remove ( attrib . element ) <EOL> def add_default_sliver_attribute ( self , name , value , network = None ) : <EOL> if network : <EOL> defaults = self . xml . xpath ( "<STR_LIT>" % network ) <EOL> else : <EOL> defaults = self . xml . xpath ( "<STR_LIT>" ) <EOL> if not defaults : <EOL> if network : <EOL> network_tag = self . xml . xpath ( "<STR_LIT>" % network ) <EOL> else : <EOL> network_tag = self . xml . xpath ( "<STR_LIT>" ) <EOL> if isinstance ( network_tag , list ) : <EOL> network_tag = network_tag [ <NUM_LIT:0> ] <EOL> defaults = network_tag . add_element ( '<STR_LIT>' ) <EOL> elif isinstance ( defaults , list ) : <EOL> defaults = defaults [ <NUM_LIT:0> ] <EOL> SFAv1Sliver . add_sliver_attribute ( defaults , name , value ) <EOL> def get_default_sliver_attributes ( self , network = None ) : <EOL> if network : <EOL> defaults = self . xml . xpath ( "<STR_LIT>" % network ) <EOL> else : <EOL> defaults = self . xml . xpath ( "<STR_LIT>" ) <EOL> if not defaults : return [ ] <EOL> return SFAv1Sliver . get_sliver_attributes ( defaults [ <NUM_LIT:0> ] ) <EOL> def remove_default_sliver_attribute ( self , name , value , network = None ) : <EOL> attribs = self . get_default_sliver_attributes ( network ) <EOL> for attrib in attribs : <EOL> if attrib [ '<STR_LIT:name>' ] == name and attrib [ '<STR_LIT:value>' ] == value : <EOL> parent = attrib . element . getparent ( ) <EOL> parent . remove ( attrib . element ) <EOL> def get_links ( self , network = None ) : <EOL> return PGv2Link . get_links ( self . xml ) <EOL> def get_link_requests ( self ) : <EOL> return PGv2Link . get_link_requests ( self . xml ) <EOL> def add_links ( self , links ) : <EOL> networks = self . get_networks ( ) <EOL> if len ( networks ) > <NUM_LIT:0> : <EOL> xml = networks [ <NUM_LIT:0> ] . element <EOL> else : <EOL> xml = self . xml <EOL> PGv2Link . add_links ( xml , links ) <EOL> def add_link_requests ( self , links ) : <EOL> PGv2Link . add_link_requests ( self . xml , links ) <EOL> def merge ( self , in_rspec ) : <EOL> """<STR_LIT>""" <EOL> if not in_rspec : <EOL> return <EOL> from openflow . optin_manager . sfa . rspecs . rspec import RSpec <EOL> if isinstance ( in_rspec , RSpec ) : <EOL> rspec = in_rspec <EOL> else : <EOL> rspec = RSpec ( in_rspec ) <EOL> if rspec . version . type . lower ( ) == '<STR_LIT>' : <EOL> from openflow . optin_manager . sfa . rspecs . rspec_converter import RSpecConverter <EOL> in_rspec = RSpecConverter . to_sfa_rspec ( rspec . toxml ( ) ) <EOL> rspec = RSpec ( in_rspec ) <EOL> current_networks = self . get_networks ( ) <EOL> networks = rspec . version . get_networks ( ) <EOL> for network in networks : <EOL> current_network = network . get ( '<STR_LIT:name>' ) <EOL> if current_network and current_network not in current_networks : <EOL> self . xml . append ( network . element ) <EOL> current_networks . append ( current_network ) <EOL> def get_leases ( self , filter = None ) : <EOL> return SFAv1Lease . get_leases ( self . xml , filter ) <EOL> def add_leases ( self , leases , network = None , no_dupes = False ) : <EOL> SFAv1Lease . add_leases ( self . xml , leases ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from openflow . optin_manager . sfa . rspecs . rspec import RSpec <EOL> from openflow . optin_manager . sfa . rspecs . rspec_elements import * <EOL> r = RSpec ( '<STR_LIT>' ) <EOL> r . load_rspec_elements ( SFAv1 . elements ) <EOL> print r . get ( RSpecElements . NODE ) </s>
