<s> from openflow . optin_manager . sfa . util . sfalogging import logger <EOL> from openflow . optin_manager . sfa . util . xml import XpathFilter <EOL> from openflow . optin_manager . sfa . util . xrn import Xrn <EOL> from openflow . optin_manager . sfa . rspecs . elements . element import Element <EOL> from openflow . optin_manager . sfa . rspecs . elements . node import Node <EOL> from openflow . optin_manager . sfa . rspecs . elements . sliver import Sliver <EOL> from openflow . optin_manager . sfa . rspecs . elements . location import Location <EOL> from openflow . optin_manager . sfa . rspecs . elements . position_3d import Position3D <EOL> from openflow . optin_manager . sfa . rspecs . elements . hardware_type import HardwareType <EOL> from openflow . optin_manager . sfa . rspecs . elements . disk_image import DiskImage <EOL> from openflow . optin_manager . sfa . rspecs . elements . interface import Interface <EOL> from openflow . optin_manager . sfa . rspecs . elements . bwlimit import BWlimit <EOL> from openflow . optin_manager . sfa . rspecs . elements . pltag import PLTag <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . nitosv1Sliver import NITOSv1Sliver <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . nitosv1PLTag import NITOSv1PLTag <EOL> from openflow . optin_manager . sfa . rspecs . elements . versions . pgv2Services import PGv2Services <EOL> class NITOSv1Node : <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> gateway_addr = '<STR_LIT>' <EOL> node_elems = [ ] <EOL> for node in nodes : <EOL> node_fields = [ '<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> network_hrn = None <EOL> if '<STR_LIT>' in node and node [ '<STR_LIT>' ] : <EOL> network_hrn = Xrn ( node [ '<STR_LIT>' ] ) . get_hrn ( ) <EOL> if '<STR_LIT>' in node and node [ '<STR_LIT>' ] : <EOL> component_name = Xrn ( xrn = node [ '<STR_LIT>' ] ) . get_leaf ( ) <EOL> node_elem . set ( '<STR_LIT>' , component_name ) <EOL> hostname_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> hostname_elem . set_text ( component_name ) <EOL> if '<STR_LIT>' in node and node [ '<STR_LIT>' ] : <EOL> node_elem . set ( '<STR_LIT>' , node [ '<STR_LIT>' ] ) <EOL> location = node . get ( '<STR_LIT:location>' ) <EOL> if location : <EOL> node_elem . add_instance ( '<STR_LIT:location>' , location , Location . fields ) <EOL> position_3d = node . get ( '<STR_LIT>' ) <EOL> if position_3d : <EOL> node_elem . add_instance ( '<STR_LIT>' , position_3d , Position3D . fields ) <EOL> exclusive_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> exclusive_elem . set_text ( '<STR_LIT>' ) <EOL> gateway_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> gateway_elem . set_text ( gateway_addr ) <EOL> granularity = node . get ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> if granularity : <EOL> granularity_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> granularity_elem . set_text ( str ( granularity ) ) <EOL> hardware_type_elem = node_elem . add_element ( '<STR_LIT>' ) <EOL> hardware_type_elem . set_text ( node . get ( '<STR_LIT>' ) ) <EOL> if isinstance ( node . get ( '<STR_LIT>' ) , list ) : <EOL> for interface in node . get ( '<STR_LIT>' , [ ] ) : <EOL> node_elem . add_instance ( '<STR_LIT>' , interface , [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> PGv2Services . add_services ( node_elem , node . get ( '<STR_LIT>' , [ ] ) ) <EOL> tags = node . get ( '<STR_LIT>' , [ ] ) <EOL> if tags : <EOL> for tag in tags : <EOL> tag_elem = node_elem . add_element ( tag [ '<STR_LIT>' ] ) <EOL> tag_elem . set_text ( tag [ '<STR_LIT:value>' ] ) <EOL> NITOSv1Sliver . add_slivers ( node_elem , node . get ( '<STR_LIT>' , [ ] ) ) <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 = NITOSv1Node . get_nodes ( xml , filter ) <EOL> if not nodes : <EOL> continue <EOL> node = nodes [ <NUM_LIT:0> ] <EOL> NITOSv1Sliver . add_slivers ( node , sliver ) <EOL> @ staticmethod <EOL> def remove_slivers ( xml , hostnames ) : <EOL> for hostname in hostnames : <EOL> nodes = NITOSv1Node . get_nodes ( xml , { '<STR_LIT>' : '<STR_LIT>' % hostname } ) <EOL> for node in nodes : <EOL> slivers = NITOSv1Sliver . get_slivers ( node . element ) <EOL> for sliver in slivers : <EOL> node . element . remove ( sliver . element ) <EOL> @ staticmethod <EOL> def get_nodes ( xml , filter = { } ) : <EOL> xpath = '<STR_LIT>' % ( XpathFilter . xpath ( filter ) , XpathFilter . xpath ( filter ) ) <EOL> node_elems = xml . xpath ( xpath ) <EOL> return NITOSv1Node . get_node_objs ( node_elems ) <EOL> @ staticmethod <EOL> def get_nodes_with_slivers ( xml ) : <EOL> xpath = '<STR_LIT>' <EOL> node_elems = xml . xpath ( xpath ) <EOL> return NITOSv1Node . 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> if '<STR_LIT>' in node_elem . attrib : <EOL> node [ '<STR_LIT>' ] = node_elem . attrib [ '<STR_LIT>' ] <EOL> location_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> locations = [ loc_elem . get_instance ( Location ) for loc_elem in location_elems ] <EOL> if len ( locations ) > <NUM_LIT:0> : <EOL> node [ '<STR_LIT:location>' ] = locations [ <NUM_LIT:0> ] <EOL> bwlimit_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> bwlimits = [ bwlimit_elem . get_instance ( BWlimit ) for bwlimit_elem in bwlimit_elems ] <EOL> if len ( bwlimits ) > <NUM_LIT:0> : <EOL> node [ '<STR_LIT>' ] = bwlimits [ <NUM_LIT:0> ] <EOL> iface_elems = node_elem . xpath ( '<STR_LIT>' ) <EOL> ifaces = [ iface_elem . get_instance ( Interface ) for iface_elem in iface_elems ] <EOL> node [ '<STR_LIT>' ] = ifaces <EOL> node [ '<STR_LIT>' ] = PGv2Services . get_services ( node_elem ) <EOL> node [ '<STR_LIT>' ] = NITOSv1Sliver . get_slivers ( node_elem ) <EOL> node [ '<STR_LIT>' ] = NITOSv1PLTag . get_pl_tags ( node_elem , ignore = Node . fields + [ "<STR_LIT>" ] ) <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> if not node [ '<STR_LIT>' ] : <EOL> hostname_elem = node_elem . find ( "<STR_LIT>" ) <EOL> if hostname_elem != None : <EOL> node [ '<STR_LIT>' ] = hostname_elem . text <EOL> nodes . append ( node ) <EOL> return nodes </s>
