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 CreateSliver(self,xrn,rspec_string,users,creds,options): xrn = Xrn(xrn, 'slice') slice_leaf = xrn.get_leaf() slice_hrn = xrn.get_hrn() authority = xrn.get_authority_hrn() expiration_date = self.driver.get_expiration(creds, slice_hrn) return self.driver.create_sliver (slice_leaf,authority,rspec_string, users, options, expiration_date)
def ListResources(self, options): slice_xrn = options.get('geni_slice_urn', None) if slice_xrn: xrn = Xrn(slice_xrn,'slice') slice_leaf = xrn.get_leaf() options['slice'] = slice_leaf rspec = self.driver.list_resources(options) if options.has_key('geni_compressed') and options['geni_compressed'] == True: rspec = zlib.compress(rspec).encode('base64') return rspec
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_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 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 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 = 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 reset_slice(self,xrn): xrn = Xrn(xrn) slice_leaf = xrn.get_leaf() authority = xrn.get_authority_hrn() return self.driver.crud_slice (slice_leaf,authority,action='reset_slice')
def DeleteSliver(self, xrn, options): #TODO: Check the options or xrn to get a single vm. xrn = Xrn(xrn) slice_leaf = xrn.get_leaf() authority = xrn.get_authority_hrn() return self.driver.crud_slice(slice_leaf,authority,action='delete_slice')
def SliverStatus (self, xrn, options): xrn = Xrn(xrn,'slice') slice_leaf = xrn.get_leaf() authority = xrn.get_authority_hrn() return self.driver.sliver_status(slice_leaf,authority,options)
def create_sliver(self, slice_leaf, authority, rspec_string, users, options, expiration_date): rspec = RSpec(rspec_string, 'OcfVt') requested_attributes = rspec.version.get_slice_attributes() requested_attributes = self.shell.convert_to_uuid( requested_attributes) #Converts component_id URNs to UUIDs projectName = authority #users[0]['slice_record']['authority'] sliceName = slice_leaf self.shell.CreateSliver(requested_attributes, projectName, sliceName, expiration_date) created_vms = list() nodes = list() for slivers in requested_attributes: node = self.shell.GetNodes(uuid=slivers['component_id']) #print "\n\n\n\n\n\nslivers: %s\n\n\n\n\n\n" % slivers['slivers'] for vm in slivers['slivers']: #node = self.shell.GetNodes(uuid=vm['server-id']) if not node in nodes: nodes.append(node) try: #print "\n\n\n\n\n\nusers: %s\n\n\n\n\n\n" % str(users) for user in users: xrn = Xrn(user['urn'], 'user') user_name = xrn.get_leaf() # Store user SSH key for future use for user_key in user['keys']: if not VirtualMachineKeys.objects.filter( project_uuid=vm["project-id"], slice_uuid=vm["slice-id"], vm_uuid=vm['uuid'], user_name=user_name, ssh_key=user_key): key_entry = VirtualMachineKeys( project_uuid=vm["project-id"], slice_uuid=vm["slice-id"], vm_uuid=vm['uuid'], user_name=user_name, ssh_key=user_key) key_entry.save() except Exception as e: logging.error( "create_sliver > Could not store user SSH key. Details: %s" % str(e)) #ip = self.shell.get_ip_from_vm(vm_name=vm['name'],slice_name=vm['slice-name'],project=authority) #created_vms.append({'vm-name':vm['name'],'vm-ip':ip,'vm-state':'ongoing','slice-name':slice_leaf,'node-name':node.name}) #add ssh keys to ldap of ssh gateway if len(nodes): logging.info("create_slice > Connecting to LDAP") session = ldapManager() con = session.bind() logging.info("create_slice > Connected to LDAP. Connection: %s" % str(con)) if con: logging.info("LDAP: trying to create the following users: %s" % str(users)) for user in users: logging.info("Sending users to LDAP") #logging.error("project: "+str(projectName)+" slicename"+str(sliceName)) ldapprj = "%s.%s" % (projectName, sliceName) ldapprj = ldapprj.replace("\\", "") logging.info("LDAP project: " + ldapprj) session.add_modify_project_users(con, user["urn"], ldapprj, user["keys"]) logging.info("User added to project: " + str(ldapprj) + ", SSH key: " + str(user['keys'])) #status=session.add_project(con,projectName+"."+sliceName) #if status != 0: #logging.error("users"+users) # for index,user in enumerate(users): #logging.error(user) # status=session.add_user(con,"user"+str(index),user['urn'],projectName+"."+sliceName,user['keys']) # if status ==0: # logging.error("can't add user to ldap of ssh gateway:"+ str(user)) #else: # logging.error("can't add project to ldap of ssh gateway: "+projectName+"."+sliceName) else: logging.error("Cannot contact LDAP of the SSH gateway") return self.aggregate.get_rspec(slice_leaf=slice_leaf, projectName=projectName, version=rspec.version, created_vms=created_vms, new_nodes=nodes)
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', []))
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', '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_to_hostname(node['component_id']) 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 exclusive tag to distinguish between Reservable and Shared nodes exclusive_elem = node_elem.add_element('exclusive') if node.get('exclusive') and node.get('exclusive') == 'true': exclusive_elem.set_text('TRUE') # add granularity of the reservation system granularity = node.get('granularity') if granularity: node_elem.add_instance('granularity', granularity, granularity.fields) else: exclusive_elem.set_text('FALSE') 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']) SFAv1Sliver.add_slivers(node_elem, node.get('slivers', []))