def add_link_requests(xml, link_tuples, append=False): if not isinstance(link_tuples, set): link_tuples = set(link_tuples) available_links = PGv2Link.get_links(xml) recently_added = [] for link in available_links: if_name1 = Xrn(link['interface1']['component_id']).get_leaf() if_name2 = Xrn(link['interface2']['component_id']).get_leaf() requested_link = None l_tup_1 = (if_name1, if_name2) l_tup_2 = (if_name2, if_name1) if link_tuples.issuperset([(if_name1, if_name2)]): requested_link = (if_name1, if_name2) elif link_tuples.issuperset([(if_name2, if_name2)]): requested_link = (if_name2, if_name1) if requested_link: # add client id to link ane interface elements link.element.set('client_id', link['component_name']) link['interface1'].element.set( 'client_id', Xrn(link['interface1']['component_id']).get_leaf()) link['interface2'].element.set( 'client_id', Xrn(link['interface2']['component_id']).get_leaf()) recently_added.append(link['component_name']) if not append: # remove all links that don't have a client id for link in PGv2Link.get_links(xml): if not link['client_id'] or link[ 'component_name'] not in recently_added: parent = link.element.getparent() parent.remove(link.element)
def call(self, xrn, creds, type): xrn=Xrn(xrn,type=type) # validate the cred valid_creds = self.api.auth.checkCredentials(creds, "remove") self.api.auth.verify_object_permission(xrn.get_hrn()) #log the call origin_hrn = Credential(string=valid_creds[0]).get_gid_caller().get_hrn() self.api.logger.info("interface: %s\tmethod-name: %s\tcaller-hrn: %s\ttarget-urn: %s"%( self.api.interface, self.name, origin_hrn, xrn.get_urn())) return self.api.manager.Remove(self.api, xrn)
def call(self, xrn, creds, type): xrn = Xrn(xrn, type=type) # validate the cred valid_creds = self.api.auth.checkCredentials(creds, "remove") self.api.auth.verify_object_permission(xrn.get_hrn()) #log the call origin_hrn = Credential( string=valid_creds[0]).get_gid_caller().get_hrn() self.api.logger.info( "interface: %s\tmethod-name: %s\tcaller-hrn: %s\ttarget-urn: %s" % (self.api.interface, self.name, origin_hrn, xrn.get_urn())) return self.api.manager.Remove(self.api, xrn)
def to_sfa_rspec(rspec, content_type=None): if not isinstance(rspec, RSpec): pg_rspec = RSpec(rspec) else: pg_rspec = rspec version_manager = VersionManager() sfa_version = version_manager._get_version('sfa', '1') sfa_rspec = RSpec(version=sfa_version) #nodes = pg_rspec.version.get_nodes() #sfa_rspec.version.add_nodes(nodes()) #sfa_rspec.version.add_links(pg_rspec.version.get_links()) #return sfa_rspec.toxml() # get network networks = pg_rspec.version.get_networks() network_hrn = networks[0]["name"] network_element = sfa_rspec.xml.add_element('network', name=network_hrn, id=network_hrn) # get nodes pg_nodes_elements = pg_rspec.version.get_nodes() nodes_with_slivers = pg_rspec.version.get_nodes_with_slivers() i = 1 for pg_node in pg_nodes_elements: attribs = dict(pg_node.items()) attribs['id'] = 'n' + str(i) node_element = network_element.add_element('node') for attrib in attribs: if type(attribs[attrib]) == str: node_element.set(attrib, attribs[attrib]) urn = pg_node["component_id"] if urn: if type(urn) == list: # legacy code, not sure if urn is ever a list... urn = urn[0] hostname = Xrn.urn_split(urn)[-1] hostname_element = node_element.add_element('hostname') hostname_element.set_text(hostname) if hostname in nodes_with_slivers: node_element.add_element('sliver') for hardware_type in pg_node["hardware_types"]: if "name" in hardware_type: node_element.add_element("hardware_type", name=hardware_type["name"]) # just copy over remaining child elements #for child in pg_node_element.getchildren(): # node_element.append(transform(child).getroot()) i = i + 1 return sfa_rspec.toxml()
def get_node_objs(node_elems): nodes = [] for node_elem in node_elems: node = Node(node_elem.attrib, node_elem) nodes.append(node) if 'component_id' in node_elem.attrib: node['authority_id'] = Xrn( node_elem.attrib['component_id']).get_authority_urn() # get hardware types hardware_type_elems = node_elem.xpath( './hardware_type | ./default:hardware_type') node['hardware_types'] = [ hw_type.get_instance(HardwareType) for hw_type in hardware_type_elems ] # get location location_elems = node_elem.xpath('./location | ./default:location') locations = [ location_elem.get_instance(Location) for location_elem in location_elems ] if len(locations) > 0: node['location'] = locations[0] # get interfaces iface_elems = node_elem.xpath('./interface | ./default:interface') node['interfaces'] = [ iface_elem.get_instance(Interface) for iface_elem in iface_elems ] # get services service_elems = node_elem.xpath('./services | ./default:service') if service_elems: node['services'] = PGv2Services.get_services(node_elem) # get slivers sliver_elems = node_elem.xpath('./sliver | ./default:slivers') if sliver_elems: node['slivers'] = OcfVtSlivers.get_slivers(sliver_elems) available_elems = node_elem.xpath( './available | ./default:available') if len(available_elems ) > 0 and 'name' in available_elems[0].attrib: if available_elems[0].attrib.get('now', '').lower() == 'true': node['boot_state'] = 'boot' else: node['boot_state'] = 'disabled' return nodes
def add_slivers(xml, of_xml): network_elems = xml.xpath('//network') if len(network_elems) > 0: network_elem = network_elems[0] else: network_urn = 'ocf.ofam' network_elem = xml.add_element('network', name = Xrn(network_urn).get_hrn()) slivers = of_xml.xpath('//rspec/*') for sliver in slivers: network_elem.append(sliver) return slivers
def to_sfa_rspec(rspec, content_type = None): if not isinstance(rspec, RSpec): pg_rspec = RSpec(rspec) else: pg_rspec = rspec version_manager = VersionManager() sfa_version = version_manager._get_version('sfa', '1') sfa_rspec = RSpec(version=sfa_version) #nodes = pg_rspec.version.get_nodes() #sfa_rspec.version.add_nodes(nodes()) #sfa_rspec.version.add_links(pg_rspec.version.get_links()) #return sfa_rspec.toxml() # get network networks = pg_rspec.version.get_networks() network_hrn = networks[0]["name"] network_element = sfa_rspec.xml.add_element('network', name=network_hrn, id=network_hrn) # get nodes pg_nodes_elements = pg_rspec.version.get_nodes() nodes_with_slivers = pg_rspec.version.get_nodes_with_slivers() i = 1 for pg_node in pg_nodes_elements: attribs = dict(pg_node.items()) attribs['id'] = 'n'+str(i) node_element = network_element.add_element('node') for attrib in attribs: if type(attribs[attrib]) == str: node_element.set(attrib, attribs[attrib]) urn = pg_node["component_id"] if urn: if type(urn)==list: # legacy code, not sure if urn is ever a list... urn = urn[0] hostname = Xrn.urn_split(urn)[-1] hostname_element = node_element.add_element('hostname') hostname_element.set_text(hostname) if hostname in nodes_with_slivers: node_element.add_element('sliver') for hardware_type in pg_node["hardware_types"]: if "name" in hardware_type: node_element.add_element("hardware_type", name=hardware_type["name"]) # just copy over remaining child elements #for child in pg_node_element.getchildren(): # node_element.append(transform(child).getroot()) i = i+1 return sfa_rspec.toxml()
def add_leases(xml, leases, channels): network_elems = xml.xpath('//network') if len(network_elems) > 0: network_elem = network_elems[0] elif len(leases) > 0: network_urn = Xrn( leases[0]['component_id']).get_authority_urn().split(':')[0] network_elem = xml.add_element('network', name=network_urn) else: network_elem = xml # group the leases by slice and timeslots grouped_leases = [] while leases: slice_id = leases[0]['slice_id'] start_time = leases[0]['start_time'] duration = leases[0]['duration'] group = [] for lease in leases: if slice_id == lease['slice_id'] and start_time == lease[ 'start_time'] and duration == lease['duration']: group.append(lease) grouped_leases.append(group) for lease1 in group: leases.remove(lease1) lease_elems = [] for lease in grouped_leases: #lease_fields = ['lease_id', 'component_id', 'slice_id', 'start_time', 'duration'] lease_fields = ['slice_id', 'start_time', 'duration'] lease_elem = network_elem.add_instance('lease', lease[0], lease_fields) lease_elems.append(lease_elem) # add nodes of this lease for node in lease: lease_elem.add_instance('node', node, ['component_id']) # add reserved channels of this lease #channels = [{'channel_id': 1}, {'channel_id': 2}] for channel in channels: if channel['slice_id'] == lease[0]['slice_id'] and channel[ 'start_time'] == lease[0]['start_time'] and channel[ 'duration'] == lease[0]['duration']: lease_elem.add_instance('channel', channel, ['channel_num'])
def get_networks(self): network_names = set() nodes = self.xml.xpath( '//default:node[@component_manager_id] | //node[@component_manager_id]', namespaces=self.namespaces) for node in nodes: if 'component_manager_id' in node.attrib: network_urn = node.get('component_manager_id') if network_urn.startswith("urn:"): network_hrn = Xrn(network_urn).get_hrn() else: # some component_manager_ids are hrns instead of urns?? network_hrn = network_urn network_names.add(network_hrn) network_names = list(network_names) networks = [{"name": x} for x in network_names] return networks
def call(self, xrns, creds, options={}): # use details=False by default, only when explicitly specified do we want # to mess with the testbed details if 'details' in options: details = options['details'] else: details = False type = None if not isinstance(xrns, types.ListType): type = Xrn(xrns).get_type() xrns = [xrns] hrns = [urn_to_hrn(xrn)[0] for xrn in xrns] #find valid credentials valid_creds = self.api.auth.checkCredentials(creds, 'resolve') #log the call origin_hrn = Credential( string=valid_creds[0]).get_gid_caller().get_hrn() self.api.logger.info( "interface: %s\tcaller-hrn: %s\ttarget-hrn: %s\tmethod-name: %s" % (self.api.interface, origin_hrn, hrns, self.name)) # send the call to the right manager return self.api.manager.Resolve(self.api, xrns, type, details=details)
def add_nodes(xml, nodes): network_elems = xml.xpath('//network') if len(network_elems) > 0: network_elem = network_elems[0] elif len(nodes) > 0 and nodes[0].get('component_manager_id'): network_urn = nodes[0]['component_manager_id'] network_elem = xml.add_element('network', name=Xrn(network_urn).get_hrn()) else: network_elem = xml node_elems = [] for node in nodes: node_fields = [ 'component_manager_id', 'component_id', 'client_id', 'sliver_id', 'exclusive' ] node_elem = network_elem.add_instance('node', node, node_fields) node_elems.append(node_elem) # set component name if node.get('component_id'): component_name = xrn_to_hostname(node['component_id']) node_elem.set('component_name', component_name) if node.get('hostname'): simple_elem = node_elem.add_element('hostname') simple_elem.set_text(node['hostname']) # set hardware types if node.get('hardware_types'): for hardware_type in node.get('hardware_types', []): for field in OcfVtServer.fields: #node_elem.add_instance(field,{field:hardware_type[field]},[])#XXX Ugly notation simple_elem = node_elem.add_element(field) simple_elem.set_text(hardware_type[field]) # set location if node.get('location'): node_elem.add_instance('location', node['location'], Location.fields) # set interfaces PGv2Interface.add_interfaces(node_elem, node.get('interfaces')) #if node.get('interfaces'): # for interface in node.get('interfaces', []): # node_elem.add_instance('interface', interface, ['component_id', 'client_id']) # set available element if node.get('boot_state'): if node.get('boot_state').lower() == 'boot': available_elem = node_elem.add_element('available', now='true') else: available_elem = node_elem.add_element('available', now='false') # add services if node.get('services'): for service in node.get('services', []): fields = service.fields s = node_elem.add_element('service', type=str( service.__class__.__name__)) for field in fields: if service[field]: simple_elem = s.add_element( field) #node_elem.add_element(field) simple_elem.set_text(service[field]) #PGv2Services.add_services(node_elem, node.get('services', [])) # add slivers slivers = node.get('slivers', []) if slivers: for sliver in slivers: fields = sliver.fields s = node_elem.add_element('sliver', type=str( sliver.__class__.__name__)) for field in fields: if sliver[field]: simple_elem = s.add_element( field) #node_elem.add_element(field) simple_elem.set_text(sliver[field]) # we must still advertise the available sliver types #slivers = Sliver({'type': 'plab-vserver'}) # we must also advertise the available initscripts #slivers['tags'] = [] #if node.get('pl_initscripts'): # for initscript in node.get('pl_initscripts', []): # slivers['tags'].append({'name': 'initscript', 'value': initscript['name']}) #PGv2SliverType.add_slivers(node_elem, slivers) return node_elems
def add_nodes(xml, nodes): network_elems = xml.xpath('//network') if len(network_elems) > 0: network_elem = network_elems[0] elif len(nodes) > 0 and nodes[0].get('component_manager_id'): network_urn = nodes[0]['component_manager_id'] network_elem = xml.add_element('network', name=Xrn(network_urn).get_hrn()) else: network_elem = xml # needs to be improuved to retreive the gateway addr dynamically. gateway_addr = 'nitlab.inf.uth.gr' node_elems = [] for node in nodes: node_fields = [ 'component_manager_id', 'component_id', 'boot_state' ] node_elem = network_elem.add_instance('node', node, node_fields) node_elems.append(node_elem) # determine network hrn network_hrn = None if 'component_manager_id' in node and node['component_manager_id']: network_hrn = Xrn(node['component_manager_id']).get_hrn() # set component_name attribute and hostname element if 'component_id' in node and node['component_id']: component_name = Xrn(xrn=node['component_id']).get_leaf() node_elem.set('component_name', component_name) hostname_elem = node_elem.add_element('hostname') hostname_elem.set_text(component_name) # set site id if 'authority_id' in node and node['authority_id']: node_elem.set('site_id', node['authority_id']) # add locaiton location = node.get('location') if location: node_elem.add_instance('location', location, Location.fields) # add 3D Position of the node position_3d = node.get('position_3d') if position_3d: node_elem.add_instance('position_3d', position_3d, Position3D.fields) # all nitos nodes are exculsive exclusive_elem = node_elem.add_element('exclusive') exclusive_elem.set_text('TRUE') # In order to access nitos nodes, one need to pass through the nitos gateway # here we advertise Nitos access gateway address gateway_elem = node_elem.add_element('gateway') gateway_elem.set_text(gateway_addr) # add granularity of the reservation system granularity = node.get('granularity')['grain'] if granularity: #node_elem.add_instance('granularity', granularity, granularity.fields) granularity_elem = node_elem.add_element('granularity') granularity_elem.set_text(str(granularity)) # add hardware type #hardware_type = node.get('hardware_type') #if hardware_type: # node_elem.add_instance('hardware_type', hardware_type) hardware_type_elem = node_elem.add_element('hardware_type') hardware_type_elem.set_text(node.get('hardware_type')) if isinstance(node.get('interfaces'), list): for interface in node.get('interfaces', []): node_elem.add_instance( 'interface', interface, ['component_id', 'client_id', 'ipv4']) #if 'bw_unallocated' in node and node['bw_unallocated']: # bw_unallocated = etree.SubElement(node_elem, 'bw_unallocated', units='kbps').text = str(int(node['bw_unallocated'])/1000) PGv2Services.add_services(node_elem, node.get('services', [])) tags = node.get('tags', []) if tags: for tag in tags: tag_elem = node_elem.add_element(tag['tagname']) tag_elem.set_text(tag['value']) NITOSv1Sliver.add_slivers(node_elem, node.get('slivers', []))