def add_broker(self, *, broker: ProxyAvro,
                   caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        try:
            proxy = Converter.get_agent_proxy(mng=broker)
            if proxy is None:
                result.set_code(ErrorCodes.ErrorInvalidArguments.value)
                result.set_message(
                    ErrorCodes.ErrorInvalidArguments.interpret())
            else:
                self.client.add_broker(broker=proxy)
        except Exception as e:
            self.logger.error("add_broker {}".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
    def register_client(self, *, client: ClientMng, kafka_topic: str,
                        caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        try:
            client_obj = Converter.fill_client(client_mng=client)
            client_obj.set_kafka_topic(kafka_topic=kafka_topic)

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

                def run(self):
                    self.actor.register_client(client=client_obj)
                    return None

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

        except Exception as e:
            self.logger.error("register_client: {}".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
    def do_get_actors(self,
                      *,
                      atype: int,
                      caller: AuthToken,
                      id_token: str = None) -> ResultActorAvro:
        result = ResultActorAvro()
        result.status = ResultAvro()

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

        try:
            act_list = self.get_actors_from_registry(atype=atype,
                                                     caller=caller)
            result.actors = Converter.fill_actors(act_list=act_list)
        except Exception as e:
            self.logger.error("get_actors {}".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 update_reservation(*, res_obj: ABCReservationMixin,
                           rsv_mng: ReservationMng) -> ABCReservationMixin:
        if isinstance(res_obj, ABCClientReservation):
            res_obj.set_renewable(renewable=rsv_mng.is_renewable())

        return Converter.absorb_res_properties(rsv_mng=rsv_mng,
                                               res_obj=res_obj)
    def get_reservations_by_category(
            self,
            *,
            caller: AuthToken,
            category: ReservationCategory,
            slice_id: ID = None,
            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:
            res_list = None
            try:
                if category == ReservationCategory.Client:
                    if slice_id is None:
                        res_list = self.db.get_client_reservations()
                    else:
                        res_list = self.db.get_client_reservations_by_slice_id(
                            slice_id=slice_id)
                elif category == ReservationCategory.Broker:
                    res_list = self.db.get_broker_reservations()
                elif category == ReservationCategory.Inventory:
                    if slice_id is None:
                        res_list = self.db.get_holdings()
                    else:
                        res_list = self.db.get_holdings_by_slice_id(
                            slice_id=slice_id)
            except Exception as e:
                self.logger.error("do_get_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.result = []
                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.result.append(rr)
        except Exception as e:
            self.logger.error("do_get_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
    def do_get_actors_from_database(self,
                                    *,
                                    caller: AuthToken,
                                    status: int = None,
                                    id_token: str = None) -> ResultActorAvro:
        result = ResultActorAvro()
        result.status = ResultAvro()

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

        try:
            act_list = None
            try:
                act_list = self.get_container_management_database().get_actors(
                )
            except Exception as e:
                self.logger.error("get_actors_from_database {}".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 act_list is not None:
                if status is None:
                    result.actors = Converter.fill_actors_from_db(
                        act_list=act_list)
                else:
                    result.actors = Converter.fill_actors_from_db_status(
                        act_list=act_list, status=status)

        except Exception as e:
            self.logger.error("get_actors_from_database {}".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 add_reservation_private(self, *, reservation: TicketReservationAvro):
        result = ResultAvro()
        slice_id = ID(uid=reservation.get_slice_id())
        rset = Converter.get_resource_set(res_mng=reservation)
        term = Term(start=ActorClock.from_milliseconds(
            milli_seconds=reservation.get_start()),
                    end=ActorClock.from_milliseconds(
                        milli_seconds=reservation.get_end()))

        broker = None

        if reservation.get_broker() is not None:
            broker = ID(uid=reservation.get_broker())

        rid = None
        if reservation.get_reservation_id() is not None:
            rid = ID(uid=reservation.get_reservation_id())
        else:
            rid = ID()
        rc = ClientReservationFactory.create(rid=rid,
                                             resources=rset,
                                             term=term)
        rc.set_renewable(renewable=reservation.is_renewable())

        if rc.get_state() != ReservationStates.Nascent or rc.get_pending_state(
        ) != ReservationPendingStates.None_:
            result.set_code(ErrorCodes.ErrorInvalidReservation.value)
            result.set_message(
                "Only reservations in Nascent.None can be added")
            return None, result

        slice_obj = self.client.get_slice(slice_id=slice_id)

        if slice_obj is None:
            result.set_code(ErrorCodes.ErrorNoSuchSlice.value)
            result.set_message(ErrorCodes.ErrorNoSuchSlice.interpret())
            return None, result

        rc.set_slice(slice_object=slice_obj)

        proxy = None

        if broker is None:
            proxy = self.client.get_default_broker()
        else:
            proxy = self.client.get_broker(guid=broker)

        if proxy is None:
            result.set_code(ErrorCodes.ErrorNoSuchBroker.value)
            result.set_message(ErrorCodes.ErrorNoSuchBroker.interpret())
            return None, result

        rc.set_broker(broker=proxy)
        self.client.register(reservation=rc)
        return rc.get_reservation_id(), result
    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_brokers(self,
                    *,
                    caller: AuthToken,
                    broker_id: ID = None,
                    id_token: str = None) -> ResultProxyAvro:
        result = ResultProxyAvro()
        result.status = ResultAvro()

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

        try:
            brokers = None
            if broker_id is None:
                brokers = self.client.get_brokers()
            else:
                broker = self.client.get_broker(guid=broker_id)
                if broker is not None:
                    brokers = [broker]
                    result.proxies = Converter.fill_proxies(proxies=brokers)
                else:
                    result.status.set_code(ErrorCodes.ErrorNoSuchBroker.value)
                    result.status.set_message(
                        ErrorCodes.ErrorNoSuchBroker.interpret())
            if brokers is not None:
                result.proxies = Converter.fill_proxies(proxies=brokers)
        except Exception as e:
            self.logger.error("get_brokers {}".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
    def get_reservation_units(self,
                              *,
                              caller: AuthToken,
                              rid: 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:
            units_list = None
            try:
                units_list = self.db.get_units(rid=rid)
            except Exception as e:
                self.logger.error(
                    "get_reservation_units: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.result = Converter.fill_units(unit_list=units_list)
        except Exception as e:
            self.logger.error("get_reservation_units: {}".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_clients(self,
                    *,
                    caller: AuthToken,
                    guid: ID = None,
                    id_token: str = None) -> ResultClientMng:
        result = ResultClientMng()
        result.status = ResultAvro()

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

        try:
            cl_list = None
            if guid is None:
                cl_list = self.db.get_clients()
            else:
                cl = self.db.get_client(guid=guid)
                if cl is not None:
                    cl_list = [cl]
                else:
                    result.status.set_code(ErrorCodes.ErrorNoSuchActor.value)
                    result.status.set_message(
                        ErrorCodes.ErrorNoSuchActor.interpret())

            if cl_list is not None:
                result.result = Converter.fill_clients(client_list=cl_list)
        except Exception as e:
            self.logger.error("get_clients: {}".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)
    def do_get_proxies(self,
                       *,
                       atype: ActorType,
                       protocol: str,
                       caller: AuthToken,
                       id_token: str = None) -> ResultProxyAvro:
        result = ResultProxyAvro()
        result.status = ResultAvro()

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

        try:
            proxies = None
            if atype == ActorType.Broker:
                proxies = ActorRegistrySingleton.get().get_broker_proxies(
                    protocol=protocol)
            elif atype == ActorType.Authority:
                proxies = ActorRegistrySingleton.get().get_site_proxies(
                    protocol=protocol)
            else:
                proxies = ActorRegistrySingleton.get().get_proxies(
                    protocol=protocol)

            result.proxies = Converter.fill_proxies(proxies=proxies)
        except Exception as e:
            self.logger.error("get_broker_proxies {}".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
예제 #14
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
예제 #15
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