<s> from copy import deepcopy <EOL> from StringIO import StringIO <EOL> from foam . sfa . util . xrn import Xrn <EOL> from foam . sfa . rspecs . version import RSpecVersion <EOL> from foam . sfa . rspecs . elements . versions . ocfvtNode import OcfVtNode <EOL> class OcfVt ( RSpecVersion ) : <EOL> type = '<STR_LIT>' <EOL> content_type = '<STR_LIT>' <EOL> version = '<STR_LIT:1>' <EOL> schema = '<STR_LIT>' <EOL> namespace = None <EOL> content = '<STR_LIT:*>' <EOL> extensions = { } <EOL> namespaces = None <EOL> def get_networks ( self ) : <EOL> network_names = set ( ) <EOL> nodes = self . xml . xpath ( '<STR_LIT>' , namespaces = self . namespaces ) <EOL> for node in nodes : <EOL> if '<STR_LIT>' in node . attrib : <EOL> network_urn = node . get ( '<STR_LIT>' ) <EOL> if network_urn . startswith ( "<STR_LIT>" ) : <EOL> network_hrn = Xrn ( network_urn ) . get_hrn ( ) <EOL> else : <EOL> network_hrn = network_urn <EOL> network_names . add ( network_hrn ) <EOL> network_names = list ( network_names ) <EOL> networks = [ { "<STR_LIT:name>" : x } for x in network_names ] <EOL> return networks <EOL> def get_nodes ( self , filter = None ) : <EOL> return PGv2Node . get_nodes ( self . xml , filter ) <EOL> def get_nodes_with_slivers ( self ) : <EOL> return OcfVtNode . get_nodes_with_slivers ( self . xml ) <EOL> def add_nodes ( self , nodes , check_for_dupes = False ) : <EOL> return OcfVtNode . add_nodes ( self . xml , nodes ) <EOL> def merge_node ( self , source_node_tag ) : <EOL> self . xml . root . append ( deepcopy ( source_node_tag ) ) <EOL> def get_sliver_attributes ( self , node ) : <EOL> attribs = dict ( ) <EOL> attribs . update ( { '<STR_LIT>' : node [ '<STR_LIT>' ] , '<STR_LIT>' : node [ '<STR_LIT>' ] , '<STR_LIT>' : node [ '<STR_LIT>' ] } ) <EOL> return attribs <EOL> def get_slice_attributes ( self , network = None ) : <EOL> slice_attributes = list ( ) <EOL> nodes_with_slivers = self . get_nodes_with_slivers ( ) <EOL> for node in nodes_with_slivers : <EOL> sliver_attributes = self . get_sliver_attributes ( node ) <EOL> slice_attributes . append ( sliver_attributes ) <EOL> return slice_attributes <EOL> def attributes_list ( self , elem ) : <EOL> opts = [ ] <EOL> if elem is not None : <EOL> for e in elem : <EOL> opts . append ( ( e . tag , str ( e . text ) . strip ( ) , e . attrib ) ) <EOL> return opts <EOL> def get_default_sliver_attributes ( self , network = None ) : <EOL> return [ ] <EOL> def add_default_sliver_attribute ( self , name , value , network = None ) : <EOL> pass <EOL> def add_slivers ( self , hostnames , attributes = [ ] , sliver_urn = None , append = False ) : <EOL> for hostname in hostnames : <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> valid_sliver_types = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> requested_sliver_type = None <EOL> for sliver_type in node_elem . get ( '<STR_LIT>' , [ ] ) : <EOL> if sliver_type . get ( '<STR_LIT:type>' ) in valid_sliver_types : <EOL> requested_sliver_type = sliver_type [ '<STR_LIT:type>' ] <EOL> if not requested_sliver_type : <EOL> continue <EOL> sliver = { '<STR_LIT:type>' : requested_sliver_type , <EOL> '<STR_LIT>' : attributes } <EOL> for available_elem in node_elem . xpath ( '<STR_LIT>' ) : <EOL> node_elem . remove ( available_elem ) <EOL> for interface_elem in node_elem . xpath ( '<STR_LIT>' ) : <EOL> node_elem . remove ( interface_elem ) <EOL> for sliver_type in node_elem . get ( '<STR_LIT>' , [ ] ) : <EOL> node_elem . element . remove ( sliver_type . element ) <EOL> node_elem . element . set ( '<STR_LIT>' , hostname ) <EOL> if sliver_urn : <EOL> pass <EOL> PGv2SliverType . 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> PGv2Node . remove_slivers ( self . xml , slivers ) <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> PGv2Link . add_links ( self . xml . root , links ) <EOL> def add_link_requests ( self , link_tuples , append = False ) : <EOL> PGv2Link . add_link_requests ( self . xml . root , link_tuples , append ) <EOL> def get_leases ( self , filter = None ) : <EOL> pass <EOL> def add_leases ( self , leases , network = None , no_dupes = False ) : <EOL> pass <EOL> def merge ( self , in_rspec ) : <EOL> """<STR_LIT>""" <EOL> from foam . sfa . rspecs . rspec import RSpec <EOL> if isinstance ( in_rspec , basestring ) : <EOL> in_rspec = RSpec ( in_rspec ) <EOL> nodes = in_rspec . version . get_nodes ( ) <EOL> for node in nodes : <EOL> if not node . has_key ( '<STR_LIT>' ) or not node [ '<STR_LIT>' ] : <EOL> node [ '<STR_LIT>' ] = { '<STR_LIT:name>' : '<STR_LIT>' } <EOL> self . add_nodes ( nodes ) <EOL> self . add_links ( in_rspec . version . get_links ( ) ) <EOL> def cleanup ( self ) : <EOL> if self . type in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> self . xml . remove_element ( '<STR_LIT>' ) <EOL> class OcfVtAd ( OcfVt ) : <EOL> enabled = True <EOL> content_type = '<STR_LIT>' <EOL> schema = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> class OcfVtRequest ( OcfVt ) : <EOL> enabled = True <EOL> content_type = '<STR_LIT>' <EOL> schema = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> class OcfVtManifest ( OcfVt ) : <EOL> enabled = True <EOL> content_type = '<STR_LIT>' <EOL> schema = '<STR_LIT>' <EOL> template = '<STR_LIT>' <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from foam . sfa . rspecs . rspec import RSpec <EOL> from foam . sfa . rspecs . rspec_elements import * <EOL> r = RSpec ( '<STR_LIT>' ) <EOL> r . load_rspec_elements ( OcfVt . elements ) <EOL> print r . get ( RSpecElements . NODE ) </s>
