def allocate_vdu_task(ks_path, event_id, cloud_account, request_msg): response_xpath = ks_path.to_xpath( RwResourceMgrYang.get_schema()) + "/resource-info" schema = RwResourceMgrYang.VDUEventData().schema() pathentry = schema.keyspec_to_entry(ks_path) try: response_info = yield from self._parent.allocate_virtual_compute( event_id, cloud_account, request_msg, ) except Exception as e: self._log.error( "Encountered exception : %s while creating virtual compute", str(e)) response_info = RwResourceMgrYang.VDUEventData_ResourceInfo() response_info.resource_state = 'failed' yield from self._dts.query_update(response_xpath, rwdts.XactFlag.ADVISE, response_info) else: if response_info.resource_state == 'failed' or response_info.resource_state == 'active': self._log.info( "Virtual compute create task completed. Publishing VDU info: %s at path: %s", response_info, response_xpath) yield from self._dts.query_update(response_xpath, rwdts.XactFlag.ADVISE, response_info) else: asyncio.ensure_future(monitor_vdu_state( response_xpath, pathentry), loop=self._loop)
def on_vdu_request_prepare(xact_info, action, ks_path, request_msg): self._log.debug( "Received vdu on_prepare callback (xact_info: %s, action: %s): %s", xact_info, action, request_msg) response_xpath = ks_path.to_xpath( RwResourceMgrYang.get_schema()) + "/resource-info" schema = RwResourceMgrYang.VDUEventData().schema() pathentry = schema.keyspec_to_entry(ks_path) if action == rwdts.QueryAction.CREATE: response_info = RwResourceMgrYang.VDUEventData_ResourceInfo() response_info.resource_state = 'pending' request_msg.resource_info = response_info self.create_record_dts( self._vdu_reg, None, ks_path.to_xpath(RwResourceMgrYang.get_schema()), request_msg) asyncio.ensure_future(allocate_vdu_task( ks_path, pathentry.key00.event_id, request_msg.cloud_account, request_msg.request_info), loop=self._loop) elif action == rwdts.QueryAction.DELETE: response_info = None yield from self._parent.release_virtual_compute( pathentry.key00.event_id) self.delete_record_dts( self._vdu_reg, None, ks_path.to_xpath(RwResourceMgrYang.get_schema())) elif action == rwdts.QueryAction.READ: response_info = yield from self._parent.read_virtual_compute_info( pathentry.key00.event_id) else: raise ValueError( "Only create/delete actions available. Received action: %s" % (action)) self._log.debug("Responding with VDUInfo at xpath %s: %s", response_xpath, response_info) xact_info.respond_xpath(rwdts.XactRspCode.ACK, response_xpath, response_info)
def get_compute_reserve_msg(self, name, cloud_type, xpath, vlinks): event_id = str(uuid.uuid4()) msg = rmgryang.VDUEventData() msg.event_id = event_id msg.request_info.name = name msg.request_info.image_id = resource_requests[cloud_type]['compute'][name].image_id attributes = ['image_id', 'vcpu_count', 'memory_mb', 'storage_gb'] if resource_requests[cloud_type]['compute'][name].has_field('vm_flavor'): for attr in attributes: if resource_requests[cloud_type]['compute'][name].vm_flavor.has_field(attr): setattr(msg.request_info.vm_flavor, attr, getattr(resource_requests[cloud_type]['compute'][name].vm_flavor , attr)) attributes = ['mempage_size', 'cpu_pinning_policy'] if resource_requests[cloud_type]['compute'][name].has_field('guest_epa'): for attr in attributes: if resource_requests[cloud_type]['compute'][name].guest_epa.has_field(attr): setattr(msg.request_info.guest_epa, attr, getattr(resource_requests[cloud_type]['compute'][name].guest_epa , attr)) if resource_requests[cloud_type]['compute'][name].has_field('allocate_public_address'): msg.request_info.allocate_public_address = resource_requests[cloud_type]['compute'][name].allocate_public_address cnt = 0 for link in vlinks: c1 = msg.request_info.connection_points.add() c1.name = name+"-port-"+str(cnt) cnt += 1 c1.virtual_link_id = link self.log.info("Sending message :%s", msg) return msg, xpath.format(event_id)
def on_vdu_request_prepare(self, xact_info, action, ks_path, request_msg): if not self._registered: self._log.error("Got a prepare callback when not registered!") xact_info.respond_xpath(rwdts.XactRspCode.NA) return @asyncio.coroutine def monitor_vdu_state(response_xpath, pathentry): self._log.info("Initiating VDU state monitoring for xpath: %s ", response_xpath) loop_cnt = 120 while loop_cnt > 0: self._log.debug("VDU state monitoring: Sleeping for 1 second ") yield from asyncio.sleep(1, loop = self._loop) try: response_info = self._read_virtual_compute( pathentry.key00.event_id ) except Exception as e: self._log.error( "VDU state monitoring: Received exception %s " "in VDU state monitoring for %s. Aborting monitoring", str(e), response_xpath ) raise if response_info.resource_state == 'active' or response_info.resource_state == 'failed': self._log.info( "VDU state monitoring: VDU reached terminal state." "Publishing VDU info: %s at path: %s", response_info, response_xpath ) yield from self._dts.query_update(response_xpath, rwdts.XactFlag.ADVISE, response_info) return else: loop_cnt -= 1 ### End of while loop. This is only possible if VDU did not reach active state self._log.info("VDU state monitoring: VDU at xpath :%s did not reached active state in 120 seconds. Aborting monitoring", response_xpath) response_info = RwResourceMgrYang.VDUEventData_ResourceInfo() response_info.resource_state = 'failed' yield from self._dts.query_update(response_xpath, rwdts.XactFlag.ADVISE, response_info) return self._log.debug("Received vdu on_prepare callback (xact_info: %s, action: %s): %s", xact_info, action, request_msg) response_info = None response_xpath = ks_path.to_xpath(RwResourceMgrYang.get_schema()) + "/resource-info" schema = RwResourceMgrYang.VDUEventData().schema() pathentry = schema.keyspec_to_entry(ks_path) if action == rwdts.QueryAction.CREATE: response_info = self._allocate_virtual_compute( pathentry.key00.event_id, request_msg.request_info, ) if response_info.resource_state == 'pending': asyncio.ensure_future(monitor_vdu_state(response_xpath, pathentry), loop = self._loop) elif action == rwdts.QueryAction.DELETE: self._release_virtual_compute( pathentry.key00.event_id ) elif action == rwdts.QueryAction.READ: response_info = self._read_virtual_compute( pathentry.key00.event_id ) else: raise ValueError("Only create/delete actions available. Received action: %s" %(action)) self._log.debug("Responding with VDUInfo at xpath %s: %s", response_xpath, response_info) xact_info.respond_xpath(rwdts.XactRspCode.ACK, response_xpath, response_info)