Пример #1
0
    def recover_delegations(self, *, slice_obj: ABCSlice):
        """
        Recover delegations for a slice
        @param slice_obj slice object
        """
        self.logger.info(
            "Starting to recover delegations in slice {}({})".format(
                slice_obj.get_name(), slice_obj.get_slice_id()))
        try:
            delegations = self.plugin.get_database(
            ).get_delegations_by_slice_id(slice_id=slice_obj.get_slice_id())
        except Exception as e:
            self.logger.error(e)
            raise ActorException(
                "Could not fetch delegations records for slice {}({}) from database"
                .format(slice_obj.get_name(), slice_obj.get_slice_id()))

        self.logger.debug("There are {} delegations(s) in slice".format(
            len(delegations)))

        for d in delegations:
            try:
                self.logger.info("Delegation has properties: {}".format(d))
                self.recover_delegation(d=d, slice_obj=slice_obj)
            except Exception as e:
                self.logger.error(
                    "Unexpected error while recovering delegation {}".format(
                        e))

        self.logger.info(
            "Recovery for delegations in slice {} completed".format(slice_obj))
Пример #2
0
    def recover_reservations(self, *, slice_obj: ABCSlice):
        """
        Recover reservations
        @param slice_obj slice object
        """
        self.logger.info(
            "Starting to recover reservations in slice {}({})".format(
                slice_obj.get_name(), slice_obj.get_slice_id()))
        reservations = None
        try:
            reservations = self.plugin.get_database(
            ).get_reservations_by_slice_id(slice_id=slice_obj.get_slice_id())
        except Exception as e:
            self.logger.error(e)
            raise ActorException(
                "Could not fetch reservation records for slice {}({}) from database"
                .format(slice_obj.get_name(), slice_obj.get_slice_id()))

        self.logger.debug("There are {} reservations(s) in slice".format(
            len(reservations)))

        for r in reservations:
            try:
                self.recover_reservation(r=r, slice_obj=slice_obj)
            except Exception as e:
                self.logger.error(
                    "Unexpected error while recovering reservation {}".format(
                        e))

        self.logger.info(
            "Recovery for reservations in slice {} completed".format(
                slice_obj))
Пример #3
0
    def initialize(self):
        from fabric_cf.actor.core.container.globals import GlobalsSingleton

        if not self.initialized:
            if self.identity is None or self.plugin is None or self.policy is None:
                raise ActorException(
                    f"The actor is not properly created: identity: {self.identity} "
                    f"plugin: {self.plugin} policy: {self.policy}")

            if self.name is None:
                self.name = self.identity.get_name()

            if self.name is None:
                raise ActorException(
                    "The actor is not properly created: no name")

            if self.clock is None:
                self.clock = GlobalsSingleton.get().get_container(
                ).get_actor_clock()

            if self.clock is None:
                raise ActorException(
                    "The actor is not properly created: no clock")

            if self.logger is None:
                self.logger = GlobalsSingleton.get().get_logger()

            self.plugin.set_actor(actor=self)
            self.plugin.set_logger(logger=self.logger)
            self.plugin.initialize()

            self.policy.set_actor(actor=self)
            self.policy.initialize()
            self.policy.set_logger(logger=self.logger)

            self.wrapper = KernelWrapper(actor=self,
                                         plugin=self.plugin,
                                         policy=self.policy)

            self.current_cycle = -1

            self.setup_message_service()

            self.initialized = True
Пример #4
0
    def recover_broker_slice(self, *, slice_obj: ABCSlice):
        """
        Recover broker slice at the AM, do the following if the model.reload file is detected
        - Close the existing delegations
        - Create the new delegations from the reloaded ARM
        - Add the delegations to the Broker Slice

        @param slice_obj Slice object
        """
        if self.get_type() != ActorType.Authority:
            return False

        if not slice_obj.is_broker_client():
            return False

        from fabric_cf.actor.core.container.globals import GlobalsSingleton
        if not GlobalsSingleton.get().can_reload_model():
            return False

        self.logger.info(
            f"Closing old delegations and adding new delegations to the slice: {slice_obj}!"
        )
        delegation_names = []

        try:
            delegations = self.plugin.get_database(
            ).get_delegations_by_slice_id(slice_id=slice_obj.get_slice_id())
        except Exception as e:
            self.logger.error(e)
            raise ActorException(
                f"Could not fetch delegations records for slice {slice_obj} from database"
            )

        for d in delegations:
            self.logger.info(f"Closing delegation: {d}!")
            d.set_graph(graph=None)
            d.transition(prefix="closed as part of recovers",
                         state=DelegationState.Closed)
            delegation_names.append(d.get_delegation_name())
            self.plugin.get_database().update_delegation(delegation=d)

        adms = self.policy.aggregate_resource_model.generate_adms()

        # Create new delegations and add to the broker slice; they will be re-registered with the policy in the recovery
        for name in delegation_names:
            new_delegation_graph = adms.get(name)
            dlg_obj = DelegationFactory.create(
                did=new_delegation_graph.get_graph_id(),
                slice_id=slice_obj.get_slice_id(),
                delegation_name=name)
            dlg_obj.set_slice_object(slice_object=slice_obj)
            dlg_obj.set_graph(graph=new_delegation_graph)
            dlg_obj.transition(prefix="Reload Model",
                               state=DelegationState.Delegated)
            self.plugin.get_database().add_delegation(delegation=dlg_obj)
Пример #5
0
    def error(self, *, err: str):
        """
        Logs and propagates a general error.

        @param err
                   log/exception message.
        @throws Exception
                    always
        """
        self.logger.error(err)
        raise ActorException(err)
Пример #6
0
    def recover_delegation(self, *, d: ABCDelegation, slice_obj: ABCSlice):
        """
        Recover delegation
        @param d delegation
        @param slice_obj slice object
        """
        try:

            d.restore(actor=self, slice_obj=slice_obj)

            self.logger.info("Found delegation # {} in state {}".format(
                d.get_delegation_id(), d.get_state_name()))
            if d.is_closed():
                self.logger.info(
                    "Delegation #{} is closed. Nothing to recover.".format(
                        d.get_delegation_id()))
                return

            self.logger.info("Recovering delegation #{}".format(
                d.get_delegation_id()))
            self.logger.debug("Recovering delegation object d={}".format(d))

            self.logger.debug("Registering the delegation with the actor")
            self.re_register_delegation(delegation=d)

            self.logger.info(d)

            self.logger.debug("Revisiting with the Plugin")

            self.plugin.revisit(delegation=d)

            self.logger.info(d)

            self.logger.debug("Revisiting with the actor policy")
            self.policy.revisit_delegation(delegation=d)

            self.logger.info("Recovered delegation #{}".format(
                d.get_delegation_id()))
        except Exception as e:
            self.logger.error(traceback.format_exc())
            self.logger.error(
                "Exception occurred in recovering delegation e={}".format(e))
            raise ActorException("Could not recover delegation #{}".format(d))
Пример #7
0
    def recover_reservation(self, *, r: ABCReservationMixin,
                            slice_obj: ABCSlice):
        """
        Recover reservation
        @param r reservation
        @param slice_obj slice object
        """
        try:
            r.restore(actor=self, slice_obj=slice_obj)

            self.logger.info("Found reservation # {} in state {}".format(
                r.get_reservation_id(), r.get_reservation_state()))
            if r.is_closed():
                self.logger.info(
                    "Reservation #{} is closed. Nothing to recover.".format(
                        r.get_reservation_id()))
                return

            self.logger.info("Recovering reservation #{}".format(
                r.get_reservation_id()))
            self.logger.debug("Recovering reservation object r={}".format(r))

            self.logger.debug("Registering the reservation with the actor")
            self.re_register(reservation=r)

            self.logger.info(r)

            self.logger.debug("Revisiting with the Plugin")

            self.plugin.revisit(reservation=r)

            self.logger.info(r)

            self.logger.debug("Revisiting with the actor policy")
            self.policy.revisit(reservation=r)

            self.logger.info("Recovered reservation #{}".format(
                r.get_reservation_id()))
        except Exception as e:
            self.logger.error(traceback.format_exc())
            self.logger.error(
                "Exception occurred in recovering reservation e={}".format(e))
            raise ActorException("Could not recover Reservation #{}".format(r))
Пример #8
0
    def start(self):
        """
        Start an Actor
        """
        try:
            self.thread_lock.acquire()
            if self.thread is not None:
                raise ActorException("This actor has already been started")

            self.thread = threading.Thread(target=self.run)
            self.thread.setName(self.get_name())
            self.thread.setDaemon(True)
            self.thread.start()
        finally:
            self.thread_lock.release()

        self.message_service.start()

        if self.plugin.get_handler_processor() is not None:
            self.plugin.get_handler_processor().start()