def get_authority_reservations(
            self,
            *,
            caller: AuthToken,
            id_token: str = None) -> ResultReservationAvro:
        result = ResultReservationAvro()
        result.status = ResultAvro()

        if caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result
        try:
            AccessChecker.check_access(action_id=ActionId.query,
                                       resource_type=ResourceType.sliver,
                                       token=id_token,
                                       logger=self.logger,
                                       actor_type=self.actor.get_type())
            res_list = None
            try:
                res_list = self.db.get_authority_reservations()
            except Exception as e:
                self.logger.error(
                    "get_authority_reservations:db access {}".format(e))
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)
                return result

            if res_list is not None:
                result.reservations = []
                for r in res_list:
                    slice_obj = self.get_slice_by_guid(guid=r.get_slice_id())
                    r.restore(actor=self.actor,
                              slice_obj=slice_obj,
                              logger=self.logger)
                    rr = Converter.fill_reservation(reservation=r, full=True)
                    result.reservations.append(rr)
        except ReservationNotFoundException as e:
            self.logger.error("get_authority_reservations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorNoSuchReservation.value)
            result.status.set_message(e.text)
        except Exception as e:
            self.logger.error("get_authority_reservations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
    def get_broker_query_model(
            self, *, broker: ID, caller: AuthToken, id_token: str, level: int,
            graph_format: GraphFormat) -> ResultBrokerQueryModelAvro:
        result = ResultBrokerQueryModelAvro()
        result.status = ResultAvro()

        if broker is None or caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            if id_token is not None:
                AccessChecker.check_access(
                    action_id=ActionId.query,
                    resource_type=AuthResourceType.resources,
                    token=id_token,
                    logger=self.logger,
                    actor_type=self.client.get_type())

            b = self.client.get_broker(guid=broker)
            if b is not None:
                request = BrokerPolicy.get_broker_query_model_query(
                    level=level, bqm_format=graph_format)
                response = ManagementUtils.query(actor=self.client,
                                                 actor_proxy=b,
                                                 query=request,
                                                 id_token=id_token)
                result.model = Translate.translate_to_broker_query_model(
                    query_response=response, level=level)
            else:
                result.status.set_code(ErrorCodes.ErrorNoSuchBroker.value)
                result.status.set_message(
                    ErrorCodes.ErrorNoSuchBroker.interpret())
        except Exception as e:
            self.logger.error("get_broker_query_model {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInternalError.value)
            result.status.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
    def get_reservation_unit(self,
                             *,
                             caller: AuthToken,
                             uid: ID,
                             id_token: str = None) -> ResultUnitsAvro:
        result = ResultUnitsAvro()
        result.status = ResultAvro()

        if caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result
        try:
            AccessChecker.check_access(action_id=ActionId.query,
                                       resource_type=ResourceType.sliver,
                                       token=id_token,
                                       logger=self.logger,
                                       actor_type=self.actor.get_type())
            units_list = None
            try:
                unit = self.db.get_unit(uid=uid)
                units_list = [unit]
            except Exception as e:
                self.logger.error(
                    "get_reservation_unit:db access {}".format(e))
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)
                return result

            if units_list is not None:
                result.units = Converter.fill_units(unit_list=units_list)
        except Exception as e:
            self.logger.error("get_reservation_unit: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
Пример #4
0
    def close_slice_reservations(self,
                                 *,
                                 caller: AuthToken,
                                 slice_id: ID,
                                 id_token: str = None) -> ResultAvro:
        result = ResultAvro()

        if slice_id is None or caller is None:
            result.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.set_message(ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            if id_token is not None:
                AccessChecker.check_access(action_id=ActionId.close,
                                           resource_type=ResourceType.slice,
                                           token=id_token,
                                           logger=self.logger,
                                           actor_type=self.actor.get_type(),
                                           resource_id=str(slice_id))

            class Runner(ABCActorRunnable):
                def __init__(self, *, actor: ABCActorMixin):
                    self.actor = actor

                def run(self):
                    self.actor.close_slice_reservations(slice_id=slice_id)
                    return None

            self.actor.execute_on_actor_thread_and_wait(runnable=Runner(
                actor=self.actor))
        except SliceNotFoundException as e:
            self.logger.error("close_slice_reservations: {}".format(e))
            result.set_code(ErrorCodes.ErrorNoSuchSlice.value)
            result.set_message(e.text)
            result = ManagementObject.set_exception_details(result=result, e=e)
        except Exception as e:
            self.logger.error("close_slice_reservations: {}".format(e))
            result.set_code(ErrorCodes.ErrorInternalError.value)
            result.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result = ManagementObject.set_exception_details(result=result, e=e)
        return result
Пример #5
0
    def get_delegations(self,
                        *,
                        caller: AuthToken,
                        id_token: str = None,
                        slice_id: ID = None,
                        did: str = None,
                        state: int = None) -> ResultDelegationAvro:
        result = ResultDelegationAvro()
        result.status = ResultAvro()

        if caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            if id_token is not None:
                AccessChecker.check_access(
                    action_id=ActionId.query,
                    resource_type=ResourceType.delegation,
                    token=id_token,
                    logger=self.logger,
                    actor_type=self.actor.get_type())
            dlg_list = None
            try:
                if did is not None:
                    dlg = self.db.get_delegation(dlg_graph_id=did)
                    if dlg is not None:
                        dlg_list = [dlg]
                    else:
                        result.status.set_code(
                            ErrorCodes.ErrorNoSuchDelegation.value)
                        result.status.set_message(
                            ErrorCodes.ErrorNoSuchDelegation.interpret())
                elif slice_id is not None:
                    dlg_list = self.db.get_delegations_by_slice_id(
                        slice_id=slice_id, state=state)
                else:
                    dlg_list = self.db.get_delegations(state=state)
            except Exception as e:
                self.logger.error("get_delegations db access {}".format(e))
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret())
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)

            if dlg_list is not None:
                result.delegations = []
                for d in dlg_list:
                    slice_id = d.get_slice_id()
                    slice_obj = None
                    if slice_id is not None:
                        slice_obj = self.get_slice_by_guid(guid=slice_id)
                    d.restore(actor=self.actor, slice_obj=slice_obj)
                    dd = Translate.translate_delegation_to_avro(delegation=d)
                    result.delegations.append(dd)
        except DelegationNotFoundException as e:
            self.logger.error("get_delegations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorNoSuchDelegation.value)
            result.status.set_message(e.text)
        except Exception as e:
            self.logger.error("getDelegations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInternalError.value)
            result.status.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
Пример #6
0
    def get_reservation_state_for_reservations(self,
                                               *,
                                               caller: AuthToken,
                                               rids: List[str],
                                               id_token: str = None
                                               ) -> ResultReservationStateAvro:
        result = ResultReservationStateAvro()
        result.status = ResultAvro()

        if rids is None or len(rids) == 0 or caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        for rid in rids:
            if rid is None:
                result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
                result.status.set_message(
                    ErrorCodes.ErrorInvalidArguments.interpret())
                return result

        try:
            if id_token is not None:
                AccessChecker.check_access(action_id=ActionId.query,
                                           resource_type=ResourceType.sliver,
                                           token=id_token,
                                           logger=self.logger,
                                           actor_type=self.actor.get_type())

            res_list = None
            try:
                res_list = self.db.get_reservations_by_rids(rid=rids)
            except Exception as e:
                self.logger.error(
                    "get_reservation_state_for_reservations db access {}".
                    format(e))
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)
                return result

            result.reservation_states = []
            for r in res_list:
                result.reservation_states.append(
                    Converter.fill_reservation_state(res=r))

        except ReservationNotFoundException as e:
            self.logger.error(
                "get_reservation_state_for_reservations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorNoSuchReservation.value)
            result.status.set_message(e.text)
        except Exception as e:
            self.logger.error(
                "get_reservation_state_for_reservations {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInternalError.value)
            result.status.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
Пример #7
0
    def get_reservations(self,
                         *,
                         caller: AuthToken,
                         id_token: str = None,
                         state: int = None,
                         slice_id: ID = None,
                         rid: ID = None,
                         oidc_claim_sub: str = None,
                         email: str = None,
                         rid_list: List[str] = None) -> ResultReservationAvro:
        result = ResultReservationAvro()
        result.status = ResultAvro()

        if caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            user_dn = oidc_claim_sub
            user_email = email
            if id_token is not None:
                fabric_token = AccessChecker.check_access(
                    action_id=ActionId.query,
                    resource_type=ResourceType.sliver,
                    token=id_token,
                    logger=self.logger,
                    actor_type=self.actor.get_type(),
                    resource_id=str(rid))
                user_dn = fabric_token.get_decoded_token().get(
                    Constants.CLAIMS_SUB, None)
                user_email = fabric_token.get_decoded_token().get(
                    Constants.CLAIMS_EMAIL, None)

                if user_dn is None:
                    result.status.set_code(ErrorCodes.ErrorInvalidToken.value)
                    result.status.set_message(
                        ErrorCodes.ErrorInvalidToken.interpret())
                    return result

            res_list = None
            try:
                if rid is not None:
                    res = self.db.get_reservation(rid=rid)
                    if res is not None:
                        res_list = [res]
                    else:
                        result.status.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.status.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                elif slice_id is not None and state is not None:
                    res_list = self.db.get_reservations_by_slice_id_state(
                        slice_id=slice_id, state=state)
                elif slice_id is not None:
                    res_list = self.db.get_reservations_by_slice_id(
                        slice_id=slice_id)
                elif state is not None:
                    res_list = self.db.get_reservations_by_state(state=state)
                elif user_dn is not None:
                    res_list = self.db.get_reservations_by_oidc_claim_sub(
                        oidc_claim_sub=user_dn)
                elif user_email is not None:
                    if state is None:
                        res_list = self.db.get_reservations_by_email(
                            email=user_email)
                    else:
                        res_list = self.db.get_reservations_by_email_state(
                            email=user_email, state=state)
                elif rid_list is not None:
                    res_list = self.db.get_reservations_by_rids(rid=rid_list)
                else:
                    res_list = self.db.get_reservations()
            except Exception as e:
                self.logger.error("getReservations:db access {}".format(e))
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)

            if res_list is not None:
                result.reservations = []
                for r in res_list:
                    slice_id = r.get_slice_id()
                    slice_obj = self.get_slice_by_guid(guid=slice_id)
                    r.restore(actor=self.actor, slice_obj=slice_obj)
                    rr = Converter.fill_reservation(reservation=r, full=True)
                    result.reservations.append(rr)
        except ReservationNotFoundException as e:
            self.logger.error("getReservations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorNoSuchReservation.value)
            result.status.set_message(e.text)
        except Exception as e:
            self.logger.error("getReservations: {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInternalError.value)
            result.status.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result
Пример #8
0
    def add_slice(self,
                  *,
                  slice_obj: SliceAvro,
                  caller: AuthToken,
                  id_token: str = None) -> ResultStringAvro:
        result = ResultStringAvro()
        result.status = ResultAvro()

        if slice_obj is None or caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())

        else:
            try:
                user_dn = None
                user_email = None
                if id_token is not None:
                    fabric_token = AccessChecker.check_access(
                        action_id=ActionId.query,
                        resource_type=ResourceType.slice,
                        token=id_token,
                        logger=self.logger,
                        actor_type=self.actor.get_type(),
                        resource_id=str(slice_obj.slice_name))
                    user_dn = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_SUB, None)
                    user_email = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_EMAIL, None)

                    if user_dn is None:
                        result.status.set_code(
                            ErrorCodes.ErrorInvalidToken.value)
                        result.status.set_message(
                            ErrorCodes.ErrorInvalidToken.interpret())
                        return result

                slice_obj_new = SliceFactory.create(
                    slice_id=ID(), name=slice_obj.get_slice_name())

                slice_obj_new.set_description(
                    description=slice_obj.get_description())
                slice_obj_new.set_owner(owner=self.actor.get_identity())
                slice_obj_new.get_owner().set_oidc_sub_claim(
                    oidc_sub_claim=user_dn)
                slice_obj_new.get_owner().set_email(email=user_email)
                slice_obj_new.set_graph_id(graph_id=slice_obj.graph_id)
                slice_obj_new.set_config_properties(
                    value=slice_obj.get_config_properties())
                slice_obj_new.set_lease_end(
                    lease_end=slice_obj.get_lease_end())
                slice_obj_new.set_lease_start(lease_start=datetime.utcnow())

                if slice_obj.get_inventory():
                    slice_obj_new.set_inventory(value=True)

                elif slice_obj.is_broker_client_slice():
                    slice_obj.set_broker_client_slice(value=True)

                elif slice_obj.is_client_slice():
                    slice_obj.set_client_slice(value=True)

                class Runner(ABCActorRunnable):
                    def __init__(self, *, actor: ABCActorMixin):
                        self.actor = actor

                    def run(self):
                        try:
                            self.actor.register_slice(
                                slice_object=slice_obj_new)
                        except Exception as e:
                            self.actor.get_plugin().release_slice(
                                slice_obj=slice_obj_new)
                            raise e

                        return None

                self.actor.execute_on_actor_thread_and_wait(runnable=Runner(
                    actor=self.actor))

                result.set_result(str(slice_obj_new.get_slice_id()))
            except Exception as e:
                self.logger.error("add_slice: {}".format(e))
                result.status.set_code(ErrorCodes.ErrorInternalError.value)
                result.status.set_message(
                    ErrorCodes.ErrorInternalError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)

        return result
Пример #9
0
    def get_slices(self,
                   *,
                   slice_id: ID,
                   caller: AuthToken,
                   id_token: str = None,
                   slice_name: str = None,
                   email: str = None) -> ResultSliceAvro:
        result = ResultSliceAvro()
        result.status = ResultAvro()

        if caller is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
        else:
            slice_list = None
            user_dn = None
            user_email = email
            try:
                if id_token is not None:
                    fabric_token = AccessChecker.check_access(
                        action_id=ActionId.query,
                        resource_type=ResourceType.slice,
                        token=id_token,
                        logger=self.logger,
                        actor_type=self.actor.get_type(),
                        resource_id=str(slice_id))
                    user_dn = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_SUB, None)
                    user_email = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_EMAIL, None)

                    if user_dn is None:
                        result.status.set_code(
                            ErrorCodes.ErrorInvalidToken.value)
                        result.status.set_message(
                            ErrorCodes.ErrorInvalidToken.interpret())
                        return result

                try:
                    slice_list = None
                    if slice_id is not None:
                        slice_obj = self.db.get_slice(slice_id=slice_id)
                        if slice_obj is not None:
                            slice_list = [slice_obj]

                    elif slice_name is not None:
                        slice_list = self.db.get_slice_by_name(
                            slice_name=slice_name,
                            oidc_claim_sub=user_dn,
                            email=user_email)
                    elif user_dn is not None:
                        slice_list = self.db.get_slice_by_oidc_claim_sub(
                            oidc_claim_sub=user_dn)
                    elif user_email is not None:
                        slice_list = self.db.get_slice_by_email(
                            email=user_email)
                    else:
                        slice_list = self.db.get_slices()

                except Exception as e:
                    self.logger.error("getSlices:db access {}".format(e))
                    result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                    result.status.set_message(
                        ErrorCodes.ErrorDatabaseError.interpret(exception=e))
                    result.status = ManagementObject.set_exception_details(
                        result=result.status, e=e)
                if slice_list is not None:
                    result.slices = Translate.fill_slices(
                        slice_list=slice_list, full=True)
                else:
                    result.status.set_code(ErrorCodes.ErrorNoSuchSlice.value)
                    result.status.set_message(
                        ErrorCodes.ErrorNoSuchSlice.interpret())
            except Exception as e:
                self.logger.error("getSlices {}".format(e))
                result.status.set_code(ErrorCodes.ErrorInternalError.value)
                result.status.set_message(
                    ErrorCodes.ErrorInternalError.interpret(exception=e))
                result.status = ManagementObject.set_exception_details(
                    result=result.status, e=e)
        return result
    def reclaim_delegations(self,
                            *,
                            broker: ID,
                            did: str,
                            caller: AuthToken,
                            id_token: str = None) -> ResultDelegationAvro:
        result = ResultReservationAvro()
        result.status = ResultAvro()

        if caller is None or did is None or broker is None:
            result.status.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.status.set_message(
                ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            if id_token is not None:
                AccessChecker.check_access(
                    action_id=ActionId.query,
                    resource_type=AuthResourceType.resources,
                    token=id_token,
                    logger=self.logger,
                    actor_type=self.client.get_type(),
                    resource_id=did)

            my_broker = self.client.get_broker(guid=broker)

            if my_broker is None:
                result.status.set_code(ErrorCodes.ErrorNoSuchBroker.value)
                result.status.set_message(
                    ErrorCodes.ErrorNoSuchBroker.interpret())
                return result

            class Runner(ABCActorRunnable):
                def __init__(self, *, actor: ABCActorMixin):
                    self.actor = actor

                def run(self):
                    return self.actor.reclaim_delegation_client(
                        delegation_id=did, broker=my_broker)

            rc = self.client.execute_on_actor_thread_and_wait(runnable=Runner(
                actor=self.client))

            if rc is not None:
                result.delegations = []
                delegation = Translate.translate_delegation_to_avro(
                    delegation=rc)
                result.delegations.append(delegation)
            else:
                raise ManageException("Internal Error")
        except Exception as e:
            self.logger.error(traceback.format_exc())
            self.logger.error("reclaim_delegations {}".format(e))
            result.status.set_code(ErrorCodes.ErrorInternalError.value)
            result.status.set_message(
                ErrorCodes.ErrorInternalError.interpret(exception=e))
            result.status = ManagementObject.set_exception_details(
                result=result.status, e=e)

        return result