<s> from foam . sfa . util . xrn import Xrn <EOL> from foam . sfa . util . xml import XpathFilter <EOL> from foam . sfa . rspecs . elements . node import Node <EOL> from foam . sfa . rspecs . elements . sliver import Sliver <EOL> from foam . sfa . rspecs . elements . location import Location <EOL> from foam . sfa . rspecs . elements . ocf_vt_server import OcfVtServer <EOL> from foam . sfa . rspecs . elements . hardware_type import HardwareType <EOL> from foam . sfa . rspecs . elements . disk_image import DiskImage <EOL> from foam . sfa . rspecs . elements . interface import Interface <EOL> from foam . sfa . rspecs . elements . bwlimit import BWlimit <EOL> from foam . sfa . rspecs . elements . pltag import PLTag <EOL> from foam . sfa . rspecs . elements . versions . pgv2Services import PGv2Services <EOL> from foam . sfa . rspecs . elements . versions . pgv2SliverType import PGv2SliverType <EOL> from foam . sfa . rspecs . elements . versions . pgv2Interface import PGv2Interface <EOL> from foam . sfa . rspecs . elements . range import Range <EOL> from foam . sfa . rspecs . elements . network_interface import NetworkInterface <EOL> from foam . sfa . rspecs . elements . versions . ocfvtSlivers import OcfVtSlivers <EOL> def xrn_to_hostname ( a = None , b = None ) : <EOL> return a <EOL> class OcfVtNode : <EOL> @ staticmethod <EOL> def add_nodes ( xml , nodes ) : <EOL> network_elems = xml . xpath ( '<STR_LIT>' ) <EOL> if len ( network_elems ) > <NUM_LIT:0> : <EOL> network_elem = network_elems [ <NUM_LIT:0> ] <EOL> elif len ( nodes ) > <NUM_LIT:0> and nodes [ <NUM_LIT:0> ] . get ( '<STR_LIT>' ) : <EOL> network_urn = nodes [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> network_elem = xml . add_element ( '<STR_LIT>' , name = Xrn ( network_urn ) . get_hrn ( ) ) <EOL> else : <EOL> network_elem = xml <EOL> node_elems = [ ] <EOL> for node in nodes : <EOL> node_fields = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> node_elem = network_elem . add_instance ( '<STR_LIT>' , node , node_fields ) <EOL> node_elems . append ( node_elem ) <EOL> if node . get ( '<STR_LIT>' ) : <EOL> component_name = xrn_to_hostname ( node [ '<STR_LIT>' ] ) <EOL> node_elem . set ( '<STR_LIT>' , component_name ) <EOL> if node . get ( '<STR_LIT>' ) : <EOL> simple_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> simple_elem . set_text ( node [ '<STR_LIT>' ] ) <EOL> if node . get ( '<STR_LIT>' ) : <EOL> for hardware_type in node . get ( '<STR_LIT>' , [ ] ) : <EOL> for field in OcfVtServer . fields : <EOL> simple_elem = node_elem . add_element ( field ) <EOL> simple_elem . set_text ( hardware_type [ field ] ) <EOL> if node . get ( '<STR_LIT:location>' ) : <EOL> node_elem . add_instance ( '<STR_LIT:location>' , node [ '<STR_LIT:location>' ] , Location . fields ) <EOL> PGv2Interface . add_interfaces ( node_elem , node . get ( '<STR_LIT>' ) ) <EOL> if node . get ( '<STR_LIT>' ) : <EOL> if node . get ( '<STR_LIT>' ) . lower ( ) == '<STR_LIT>' : <EOL> available_elem = node_elem . add_element ( '<STR_LIT>' , now = '<STR_LIT:true>' ) <EOL> else : <EOL> available_elem = node_elem . add_element ( '<STR_LIT>' , now = '<STR_LIT:false>' ) <EOL> if node . get ( '<STR_LIT>' ) : <EOL> for service in node . get ( '<STR_LIT>' , [ ] ) : <EOL> fields = service . fields <EOL> s = node_elem . add_element ( '<STR_LIT>' , type = str ( service . __class__ . __name__ ) ) <EOL> for field in fields : <EOL> if service [ field ] : <EOL> simple_elem = s . add_element ( field ) <EOL> simple_elem . set_text ( service [ field ] ) <EOL> slivers = node . get ( '<STR_LIT>' , [ ] ) <EOL> if slivers : <EOL> for sliver in slivers : <EOL> fields = sliver . fields <EOL> s = node_elem . add_element ( '<STR_LIT>' , type = str ( sliver . __class__ . __name__ ) ) <EOL> for field in fields : <EOL> if sliver [ field ] : <EOL> simple_elem = s . add_element ( field ) <EOL> simple_elem . set_text ( sliver [ field ] ) <EOL> return node_elems <EOL> @ staticmethod <EOL> def get_nodes ( xml , filter = { } ) : <EOL> xpath = '<STR_LIT>' <EOL> node_elems = xml . xpath ( xpath ) <EOL> return OcfVtNode . get_node_objs ( node_elems ) <EOL> @ staticmethod <EOL> def get_nodes_with_slivers ( xml , filter = { } ) : <EOL> xpath = '<STR_LIT>' <EOL> node_elems = xml . xpath ( xpath ) <EOL> if not node_elems : <EOL> node_elems = xml . xpath ( '<STR_LIT>' ) <EOL> return OcfVtNode . get_node_objs ( node_elems ) <EOL> @ staticmethod <EOL> def get_node_objs ( node_elems ) : <EOL> nodes = [ ] <EOL> for node_elem in node_elems : <EOL> node = Node ( node_elem . attrib , node_elem ) <EOL> nodes . append ( node ) <EOL> if '<STR_LIT>' in node_elem . attrib : <EOL> node [ '<STR_LIT>' ] = Xrn ( node_elem . attrib [ '<STR_LIT>' ] ) . get_authority_urn ( ) <EOL> hardware_type_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> node [ '<STR_LIT>' ] = [ hw_type . get_instance ( HardwareType ) for hw_type in hardware_type_elems ] <EOL> location_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> locations = [ location_elem . get_instance ( Location ) for location_elem in location_elems ] <EOL> if len ( locations ) > <NUM_LIT:0> : <EOL> node [ '<STR_LIT:location>' ] = locations [ <NUM_LIT:0> ] <EOL> iface_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> node [ '<STR_LIT>' ] = [ iface_elem . get_instance ( Interface ) for iface_elem in iface_elems ] <EOL> service_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> if service_elems : <EOL> node [ '<STR_LIT>' ] = PGv2Services . get_services ( node_elem ) <EOL> sliver_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> if sliver_elems : <EOL> node [ '<STR_LIT>' ] = OcfVtSlivers . get_slivers ( sliver_elems ) <EOL> available_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> if len ( available_elems ) > <NUM_LIT:0> and '<STR_LIT:name>' in available_elems [ <NUM_LIT:0> ] . attrib : <EOL> if available_elems [ <NUM_LIT:0> ] . attrib . get ( '<STR_LIT>' , '<STR_LIT>' ) . lower ( ) == '<STR_LIT:true>' : <EOL> node [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> node [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return nodes <EOL> @ staticmethod <EOL> def add_slivers ( xml , slivers ) : <EOL> component_ids = [ ] <EOL> for sliver in slivers : <EOL> filter = { } <EOL> if isinstance ( sliver , str ) : <EOL> filter [ '<STR_LIT>' ] = '<STR_LIT>' % sliver <EOL> sliver = { } <EOL> elif '<STR_LIT>' in sliver and sliver [ '<STR_LIT>' ] : <EOL> filter [ '<STR_LIT>' ] = '<STR_LIT>' % sliver [ '<STR_LIT>' ] <EOL> if not filter : <EOL> continue <EOL> nodes = OcfVtNode . get_nodes ( xml , filter ) <EOL> if not nodes : <EOL> continue <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> PGv2SliverType . add_slivers ( node , sliver ) <EOL> @ staticmethod <EOL> def remove_slivers ( xml , hostnames ) : <EOL> for hostname in hostnames : <EOL> nodes = OcfVtNode . get_nodes ( xml , { '<STR_LIT>' : '<STR_LIT>' % hostname } ) <EOL> for node in nodes : <EOL> slivers = OcfVtSliverType . get_slivers ( node . element ) <EOL> for sliver in slivers : <EOL> node . element . remove ( sliver . element ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> from foam . sfa . rspecs . rspec import RSpec <EOL> r = RSpec ( '<STR_LIT>' ) <EOL> r2 = RSpec ( version = '<STR_LIT>' ) <EOL> nodes = OcfVtNode . get_nodes ( r . xml ) <EOL> OcfVtNode . add_nodes ( r2 . xml . root , nodes ) </s>
