Example #1
0
def _release_resource_for_manager(manager_name, used_resources, user_info):
    stub = get_stub_from_manager_name(manager_name)
    try:

        for ur in used_resources:
            if ur.node_type in get_mapping_managers().get(manager_name):
                if ur.value:
                    response = stub.execute(
                        messages_pb2.RequestMessage(
                            method=messages_pb2.RELEASE_RESOURCES,
                            payload=ur.value,
                            user_info=user_info))
                    if response.result != 0:
                        logger.error(
                            "release resources in manager %s returned %d: %s" %
                            (manager_name, response.result,
                             response.error_message))
                        raise RpcFailedCall(
                            "release resources in manager %s returned %d: %s" %
                            (manager_name, response.result,
                             response.error_message))
                        # for u in [u for u in used_resources if u.node_type in get_mapping_managers().get(manager_name)]:
                        #     logger.info("deleting %s" % u.name)
                        #     delete(u)
    except _Rendezvous:
        traceback.print_exc()
        logger.error(
            "Exception while calling gRPC, maybe %s Manager is down?" %
            manager_name)
        raise RpcFailedCall(
            "Exception while calling gRPC, maybe %s Manager is down?" %
            manager_name)
Example #2
0
def _validate_resource(node, username, request_metadata):
    for manager_endpoint in find(ManagerEndpoint):
        if node.type in get_mapping_managers().get(manager_endpoint.name):
            if request_metadata and request_metadata.properties and request_metadata.properties.get(
                    'nsd_file_name'):
                nsd_file_name = request_metadata.properties.get(
                    'nsd_file_name')
                node.entity_tpl.get(
                    'properties')['file_name'] = 'Files/%s' % nsd_file_name
            request_message = messages_pb2.RequestMessage(
                method=messages_pb2.VALIDATE_RESOURCES,
                payload=yaml.dump(node.entity_tpl),
                user_info=get_user_info(username))
            try:
                response = get_stub_from_manager_endpoint(
                    manager_endpoint).execute(request_message)
            except Exception as e:
                if hasattr(e, 'message'):
                    raise RpcFailedCall(e.message)
                raise RpcFailedCall(e.args)

            if response.result == messages_pb2.ERROR:
                raise RpcFailedCall(response.error_message)
            return

    raise ManagerNotFound("manager handling resource %s was not found" %
                          node.type)
Example #3
0
def update_experiment(username, manager_name, resources):
    experiments = find_by_element_value(entities.Experiment,
                                        entities.Experiment.username, username)
    try:
        for new_res in resources:
            new_res_dict = json.loads(new_res.content)
            for experiment in experiments:
                for ur in experiment.resources:
                    if manager_name == 'nfv-manager' and ur.node_type == 'NfvResource':
                        try:
                            val_dict = json.loads(ur.value)
                        except Exception as e:
                            logger.warning(
                                "Failed parsing of experiment: name: %s, username: %s"
                                % (experiment.name, experiment.username))
                            raise e
                        if ur.node_type in get_mapping_managers().get(manager_name) \
                                and val_dict.get('id') == new_res_dict.get('id'):
                            ur.value = json.dumps(new_res_dict)
                    elif manager_name == 'security-manager' and ur.node_type == 'SecurityResource':
                        try:
                            val_dict = json.loads(ur.value)
                        except Exception as e:
                            logger.warning(
                                "Failed parsing of experiment: name: %s, username: %s"
                                % (experiment.name, experiment.username))
                            raise e
                        if val_dict.get('random_id') == new_res_dict.get(
                                'random_id'):
                            ur.value = json.dumps(new_res_dict)
                    else:
                        if ur.node_type in get_mapping_managers().get(
                                manager_name):
                            ur.value = json.dumps(new_res_dict)
                save(experiment)
    except:
        logger.warning(
            "error while updating resource of manager %s. Error is: %s " %
            (manager_name, traceback._cause_message))
def unregister_endpoint(manager_endpoint_name: str) -> bool:
    deleted = False
    for manager_endpoint in find(ManagerEndpoint):
        if manager_endpoint.name == manager_endpoint_name:
            for resource_type in get_mapping_managers().get(
                    manager_endpoint.name):
                for rm in [
                        rm for rm in find(ResourceMetadata)
                        if rm.node_type.lower() == resource_type.lower()
                ]:
                    delete(rm)
            delete(manager_endpoint)
            deleted = True
    return deleted
Example #5
0
def _release_used_resource(res: entities.UsedResource):
    exp = find(entities.Experiment, _id=res.parent_id)
    user_info = get_user_info(exp.username)
    for man, node_types in get_mapping_managers().items():
        if res.node_type in node_types:
            response = get_stub_from_manager_name(man).execute(
                messages_pb2.RequestMessage(
                    method=messages_pb2.RELEASE_RESOURCES,
                    payload=res.value,
                    user_info=user_info))
            if response.result != 0:
                logger.error("release resources returned %d: %s" %
                             (response.result, response.error_message))
                raise RpcFailedCall("provide resources returned %d: %s" %
                                    (response.result, response.error_message))
Example #6
0
def _release_all_experiment_resources(experiment_to_delete, username):
    user_info = get_user_info(username)
    used_resources = experiment_to_delete.resources
    managers = [
        man_name for man_name, node_types in get_mapping_managers().items()
        for ur in used_resources if ur.node_type in node_types
    ]
    threads = []
    for manager_name in managers:
        thread = ExceptionHandlerThread(
            target=_release_resource_for_manager,
            args=[manager_name, used_resources, user_info])
        threads.append(thread)
        thread.start()
    for t in threads:
        t.join()
        if t.exception:
            raise t.exception
Example #7
0
def _provide_all_resources_for_manager(experiment_to_deploy,
                                       manager_name,
                                       user_info,
                                       value_to_pass=None):
    if value_to_pass is None:
        value_to_pass = {}
    stub = get_stub_from_manager_name(manager_name)
    ret = []
    for ur_to_deploy in experiment_to_deploy.resources:
        node_types = get_mapping_managers().get(manager_name)
        if ur_to_deploy.node_type in node_types:
            if value_to_pass:
                val = json.loads(ur_to_deploy.value)
                val.get('properties').update(value_to_pass)
                yaml_dump = json.dumps(val)
                ur_to_deploy.value = yaml_dump
            response = stub.execute(
                messages_pb2.RequestMessage(
                    method=messages_pb2.PROVIDE_RESOURCES,
                    payload=ur_to_deploy.value,
                    user_info=user_info))
            for ur in experiment_to_deploy.resources:
                if ur.id == ur_to_deploy.id:
                    if response.result == messages_pb2.ERROR:
                        logger.error("provide resources returned %d: %s" %
                                     (response.result, response.error_message))
                        ur.status = ResourceStatus.ERROR.value
                        ur.value = json.dumps(response.error_message)
                    else:
                        # TODO fix this in the api not here
                        for res in response.provide_resource.resources:
                            logger.debug("Received: %s" % str(res.content))
                            _res_dict = json.loads(res.content)
                            ur.value = json.dumps(_res_dict)
                            ret.append(_res_dict)
                            ur.status = ResourceStatus.DEPLOYED.value
                    save(ur)

    return ret
Example #8
0
def provide_resources(username, experiment_id):
    experiments_to_deploy = find_by_element_value(entities.Experiment,
                                                  entities.Experiment.id,
                                                  experiment_id)
    if len(experiments_to_deploy) == 0:
        logger.error("No experiment to be deployed....")
        raise ExperimentNotFound("No experiment to be deployed....")
    experiment_to_deploy = experiments_to_deploy[0]

    res_in_error_or_deployed = 0
    for ur in experiment_to_deploy.resources:
        if ur.status in [
                entities.ResourceStatus.ERROR.value,
                entities.ResourceStatus.DEPLOYED.value
        ]:
            res_in_error_or_deployed += 1
    if res_in_error_or_deployed > 0:
        logger.error("Deploying a resource in error or deployed state....")
        raise ExperimentValidationError(
            "You cannot deploy a resource in error or deployed state. Please delete it first"
        )
    for ur in experiment_to_deploy.resources:
        today = datetime.today().date()
        start_date = ur.start_date
        end_date = ur.end_date
        # to make sure that we don't run into any problems due to the wrong type, even though this should not happen
        if isinstance(start_date, datetime):
            start_date = start_date.date()
        if isinstance(end_date, datetime):
            end_date = end_date.date()
        if start_date > today:
            logger.error(
                'The resource {} is reserved for a time period which begins in the future. You cannot use it yet.'
                .format(ur.name))
            raise ExperimentValidationError(
                'The resource {} is reserved for a time period which begins in the future. You cannot use it yet.'
                .format(ur.name))
        if end_date < today:
            logger.error(
                'For the resource {} the reserved time period ended already.'.
                format(ur.name))
            raise ExperimentValidationError(
                'For the resource {} the reserved time period ended already.'.
                format(ur.name))
    user_info = get_user_info(username)
    if hasattr(user_info, 'name'):
        un = user_info.name
    else:
        un = user_info.username
    logger.debug("Received deploy resources from user %s" % un)
    logger.debug("Received deploy resources %s" % experiment_to_deploy.name)

    involved_managers = [
        man_name for ur in experiment_to_deploy.resources
        for man_name, node_types in get_mapping_managers().items()
        if ur.node_type in node_types
    ]

    manager_ordered = get_config('system', 'deployment-order', '').split(';')
    remaining_managers = set(list(
        get_mapping_managers().keys())) - set(manager_ordered)
    value_to_pass = {}

    for manager_name in manager_ordered:
        if manager_name in involved_managers:
            result = _provide_all_resources_for_manager(
                experiment_to_deploy, manager_name, user_info, value_to_pass)
            value_to_pass = {}
            for ktp in keys_to_pass:
                for v in result:
                    if ktp in v.keys():
                        value_to_pass[ktp] = v.get(ktp)
    for manager_name in remaining_managers:
        if manager_name in involved_managers:
            _provide_all_resources_for_manager(experiment_to_deploy,
                                               manager_name, user_info)
Example #9
0
 def get_nodes(self, manager_name):
     nodes = []
     for node in self.topology_template.nodetemplates:
         if node.type in get_mapping_managers()[manager_name]:
             nodes.append(node)
     return nodes