Ejemplo n.º 1
0
                def run(self):
                    result = ResultAvro()
                    slice_obj = self.actor.get_slice(slice_id=slice_id)
                    if slice_obj is None:
                        result.set_code(ErrorCodes.ErrorNoSuchSlice.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchSlice.interpret())
                        return result
                    slice_obj = ManagementUtils.update_slice(
                        slice_obj=slice_obj, slice_mng=slice_mng)
                    slice_obj.set_dirty()

                    try:
                        self.actor.get_plugin().get_database().update_slice(
                            slice_object=slice_obj)
                    except Exception as e:
                        print("Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))
                        result = ManagementObject.set_exception_details(
                            result=result, e=e)

                    return result
Ejemplo n.º 2
0
                def run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result
                    r = ManagementUtils.update_reservation(res_obj=r,
                                                           rsv_mng=reservation)
                    r.set_dirty()

                    try:
                        self.actor.get_plugin().get_database(
                        ).update_reservation(reservation=r)
                    except Exception as e:
                        print("Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))
                        result = ManagementObject.set_exception_details(
                            result=result, e=e)
                    return result
                def run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=reservation)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    rset = ResourceSet()
                    if new_units == Constants.EXTEND_SAME_UNITS:
                        rset.set_units(units=r.get_resources().get_units())
                    else:
                        rset.set_units(units=new_units)

                    rset.set_type(rtype=r.get_resources().get_type())

                    tmp_start_time = r.get_term().get_start_time()
                    new_term = r.get_term().extend()

                    new_term.set_end_time(date=new_end_time)
                    new_term.set_new_start_time(date=tmp_start_time)
                    new_term.set_start_time(date=tmp_start_time)

                    self.actor.extend(rid=r.get_reservation_id(),
                                      resources=rset,
                                      term=new_term)

                    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 unregister_client(self, *, guid: ID, caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        try:

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

                def run(self):
                    self.actor.unregister_client(guid=guid)
                    return None

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

        except Exception as e:
            self.logger.error("unregister_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 run(self):
                    result = ResultAvro()
                    rid = ID(uid=reservation.get_reservation_id())
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    ManagementUtils.update_reservation(res_obj=r,
                                                       rsv_mng=reservation)
                    if isinstance(reservation, LeaseReservationAvro):
                        predecessors = reservation.get_redeem_predecessors()
                        for pred in predecessors:
                            if pred.get_reservation_id() is None:
                                self.logger.warning(
                                    "Redeem predecessor specified for rid={} "
                                    "but missing reservation id of predecessor"
                                    .format(rid))
                                continue

                            predid = ID(uid=pred.get_reservation_id())
                            pr = self.actor.get_reservation(rid=predid)

                            if pr is None:
                                self.logger.warning(
                                    "Redeem predecessor for rid={} with rid={} does not exist. "
                                    "Ignoring it!".format(rid, predid))
                                continue

                            if not isinstance(pr, ABCControllerReservation):
                                self.logger.warning(
                                    "Redeem predecessor for rid={} is not an IControllerReservation: "
                                    "class={}".format(rid, type(pr)))
                                continue

                            self.logger.debug(
                                "Setting redeem predecessor on reservation # {} pred={}"
                                .format(r.get_reservation_id(),
                                        pr.get_reservation_id()))
                            r.add_redeem_predecessor(reservation=pr)

                    try:
                        self.actor.get_plugin().get_database(
                        ).update_reservation(reservation=r)
                    except Exception as e:
                        self.logger.error(
                            "Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))

                    self.actor.demand(rid=rid)

                    return result
    def extend_reservation(self, *, reservation: id, new_end_time: datetime,
                           new_units: int, caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        try:

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

                def run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=reservation)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    rset = ResourceSet()
                    if new_units == Constants.EXTEND_SAME_UNITS:
                        rset.set_units(units=r.get_resources().get_units())
                    else:
                        rset.set_units(units=new_units)

                    rset.set_type(rtype=r.get_resources().get_type())

                    tmp_start_time = r.get_term().get_start_time()
                    new_term = r.get_term().extend()

                    new_term.set_end_time(date=new_end_time)
                    new_term.set_new_start_time(date=tmp_start_time)
                    new_term.set_start_time(date=tmp_start_time)

                    self.actor.extend(rid=r.get_reservation_id(),
                                      resources=rset,
                                      term=new_term)

                    return result

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

        except Exception as e:
            self.logger.error("extend_reservation {}".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
Ejemplo n.º 8
0
    def update_slice(self, *, slice_mng: SliceAvro,
                     caller: AuthToken) -> ResultAvro:
        result = ResultAvro()
        if slice_mng is None or slice_mng.get_slice_id(
        ) is None or caller is None:
            result.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.set_message(ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            slice_id = ID(uid=slice_mng.get_slice_id())

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

                def run(self):
                    result = ResultAvro()
                    slice_obj = self.actor.get_slice(slice_id=slice_id)
                    if slice_obj is None:
                        result.set_code(ErrorCodes.ErrorNoSuchSlice.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchSlice.interpret())
                        return result
                    slice_obj = ManagementUtils.update_slice(
                        slice_obj=slice_obj, slice_mng=slice_mng)
                    slice_obj.set_dirty()

                    try:
                        self.actor.get_plugin().get_database().update_slice(
                            slice_object=slice_obj)
                    except Exception as e:
                        print("Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))
                        result = ManagementObject.set_exception_details(
                            result=result, e=e)

                    return result

            result = self.actor.execute_on_actor_thread_and_wait(
                runnable=Runner(actor=self.actor))
        except Exception as e:
            self.logger.error("update_slice: {}".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 run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    self.actor.modify(reservation_id=rid,
                                      modify_properties=modify_properties)

                    return result
    def modify_reservation(self, *, rid: ID, modify_properties: dict,
                           caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        self.logger.debug("reservation: {} | modifyProperties= {}".format(
            rid, modify_properties))
        try:

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

                def run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    self.actor.modify(reservation_id=rid,
                                      modify_properties=modify_properties)

                    return result

            result = self.client.execute_on_actor_thread_and_wait(
                runnable=Runner(actor=self.client))
        except Exception as e:
            self.logger.error("modify_reservation {}".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 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
Ejemplo n.º 12
0
    def remove_slice(self,
                     *,
                     slice_id: ID,
                     caller: AuthToken,
                     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.delete,
                                           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.remove_slice_by_slice_id(slice_id=slice_id)
                    return None

            self.actor.execute_on_actor_thread_and_wait(runnable=Runner(
                actor=self.actor))
        except Exception as e:
            self.logger.error("remove_slice: {}".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 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
Ejemplo n.º 14
0
    def update_reservation(self, *, reservation: ReservationMng,
                           caller: AuthToken) -> ResultAvro:
        result = ResultAvro()
        if reservation is None or caller is None:
            result.set_code(ErrorCodes.ErrorInvalidArguments.value)
            result.set_message(ErrorCodes.ErrorInvalidArguments.interpret())
            return result

        try:
            rid = ID(uid=reservation.get_reservation_id())

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

                def run(self):
                    result = ResultAvro()
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result
                    r = ManagementUtils.update_reservation(res_obj=r,
                                                           rsv_mng=reservation)
                    r.set_dirty()

                    try:
                        self.actor.get_plugin().get_database(
                        ).update_reservation(reservation=r)
                    except Exception as e:
                        print("Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))
                        result = ManagementObject.set_exception_details(
                            result=result, e=e)
                    return result

            result = self.actor.execute_on_actor_thread_and_wait(
                runnable=Runner(actor=self.actor))
        except ReservationNotFoundException as e:
            self.logger.error("update_reservation: {}".format(e))
            result.set_code(ErrorCodes.ErrorNoSuchReservation.value)
            result.set_message(e.text)
        except Exception as e:
            self.logger.error("update_reservation: {}".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
Ejemplo n.º 15
0
    def close_reservation(self,
                          *,
                          caller: AuthToken,
                          rid: ID,
                          id_token: str = None) -> ResultAvro:
        result = ResultAvro()

        if rid 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.sliver,
                                           token=id_token,
                                           logger=self.logger,
                                           actor_type=self.actor.get_type(),
                                           resource_id=str(rid))

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

                def run(self):
                    self.actor.close_by_rid(rid=rid)
                    return True

            self.actor.execute_on_actor_thread_and_wait(runnable=Runner(
                actor=self.actor))
        except ReservationNotFoundException as e:
            self.logger.error("close_reservation: {}".format(e))
            result.set_code(ErrorCodes.ErrorNoSuchReservation.value)
            result.set_message(e.text)
        except Exception as e:
            self.logger.error("close_reservation: {}".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 update_status(self, *, incoming: ResultAvro, outgoing: ResultAvro):
     outgoing.set_code(incoming.get_code())
     outgoing.set_details(incoming.get_details())
     outgoing.set_message(incoming.get_message())
     return outgoing
    def demand_reservation(self, *, reservation: ReservationMng,
                           caller: AuthToken) -> ResultAvro:
        result = ResultAvro()

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

        try:

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

                def run(self):
                    result = ResultAvro()
                    rid = ID(uid=reservation.get_reservation_id())
                    r = self.actor.get_reservation(rid=rid)
                    if r is None:
                        result.set_code(
                            ErrorCodes.ErrorNoSuchReservation.value)
                        result.set_message(
                            ErrorCodes.ErrorNoSuchReservation.interpret())
                        return result

                    ManagementUtils.update_reservation(res_obj=r,
                                                       rsv_mng=reservation)
                    if isinstance(reservation, LeaseReservationAvro):
                        predecessors = reservation.get_redeem_predecessors()
                        for pred in predecessors:
                            if pred.get_reservation_id() is None:
                                self.logger.warning(
                                    "Redeem predecessor specified for rid={} "
                                    "but missing reservation id of predecessor"
                                    .format(rid))
                                continue

                            predid = ID(uid=pred.get_reservation_id())
                            pr = self.actor.get_reservation(rid=predid)

                            if pr is None:
                                self.logger.warning(
                                    "Redeem predecessor for rid={} with rid={} does not exist. "
                                    "Ignoring it!".format(rid, predid))
                                continue

                            if not isinstance(pr, ABCControllerReservation):
                                self.logger.warning(
                                    "Redeem predecessor for rid={} is not an IControllerReservation: "
                                    "class={}".format(rid, type(pr)))
                                continue

                            self.logger.debug(
                                "Setting redeem predecessor on reservation # {} pred={}"
                                .format(r.get_reservation_id(),
                                        pr.get_reservation_id()))
                            r.add_redeem_predecessor(reservation=pr)

                    try:
                        self.actor.get_plugin().get_database(
                        ).update_reservation(reservation=r)
                    except Exception as e:
                        self.logger.error(
                            "Could not commit slice update {}".format(e))
                        result.set_code(ErrorCodes.ErrorDatabaseError.value)
                        result.set_message(
                            ErrorCodes.ErrorDatabaseError.interpret(
                                exception=e))

                    self.actor.demand(rid=rid)

                    return result

            result = self.client.execute_on_actor_thread_and_wait(
                runnable=Runner(actor=self.client, logger=self.logger))
        except Exception as e:
            self.logger.error("demand_reservation {}".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