def register_delegation(self, *, delegation: ABCDelegation): if delegation.get_delegation_id() in self.delegations: raise SliceException( "Delegation #{} already exists in slice".format( delegation.get_delegation_id())) self.delegations[delegation.get_delegation_id()] = delegation
def donate_delegation(self, *, delegation: ABCDelegation): """ Donate an incoming delegation by merging to CBM; We take snapshot of CBM before merge, and rollback to snapshot in case merge fails :param delegation: :return: :raises: Exception in case of failure """ self.logger.debug("Donate Delegation") self.bind_delegation(delegation=delegation) try: self.lock.acquire() if delegation.get_delegation_id() in self.delegations: self.merge_adm(adm_graph=delegation.get_graph()) self.logger.debug( f"Donated Delegation: {delegation.get_delegation_id()}") else: self.logger.warning( f"Delegation ignored: {delegation.get_delegation_id()}") self.logger.debug(f"Active delegations: {self.delegations}") except Exception as e: self.logger.error(f"Failed to merge ADM: {delegation}") self.logger.error(traceback.format_exc()) raise e finally: self.lock.release()
def bind_delegation(self, *, delegation: ABCDelegation) -> bool: result = False if delegation.get_delegation_id() not in self.delegations: self.delegations[delegation.get_delegation_id()] = delegation result = True return result
def delegation_update_satisfies(self, *, incoming: ABCDelegation, update_data: UpdateData): """ Check if the incoming delegation satisfies the update @param incoming incoming delegation @param update_data update data """ if incoming.get_graph() is not None: incoming.get_graph().validate_graph() return True
def pass_delegation(*, delegation: ABCDelegation) -> ABCDelegation: slice_obj = delegation.get_slice_object().clone_request() delegation_new = DelegationFactory.create( did=delegation.get_delegation_id(), slice_id=delegation.get_slice_id()) delegation_new.set_slice_object(slice_object=slice_obj) # TODO if not delegation.is_reclaimed(): delegation_new.set_graph(delegation.get_graph()) return delegation_new
def reclaim_delegation(self, *, delegation: ABCDelegation, id_token: str = None): self.validate_delegation(delegation=delegation) self.do_reclaim_delegation( actor=delegation.get_actor(), proxy=delegation.get_broker(), delegation=delegation, callback=delegation.get_client_callback_proxy(), caller=delegation.get_slice_object().get_owner(), id_token=id_token)
def remove_source_calendar(self, *, source: ABCDelegation): """ Removes any data structures associated with a source reservation. @params source : source reservation """ try: self.lock.acquire() if source.get_delegation_id() in self.sources: self.sources.pop(source.get_delegation_id()) finally: self.lock.release()
def get_source_calendar(self, *, source: ABCDelegation) -> SourceCalendar: """ Returns the outlay calendar for the given source reservation. @params source : source reservation @returns source calendar """ calendar = self.sources.get(source.get_delegation_id()) if calendar is None: calendar = SourceCalendar(clock=self.clock, source=source) self.sources[source.get_delegation_id()] = calendar return calendar
def pass_delegation(self, *, delegation: ABCDelegation, auth: AuthToken) -> ABCDelegation: slice_obj = delegation.get_slice_object().clone_request() broker_delegation = BrokerDelegationFactory.create( did=str(delegation.get_delegation_id()), slice_id=slice_obj.get_slice_id(), broker=self) broker_delegation.set_sequence_in( sequence=delegation.get_sequence_out()) broker_delegation.set_owner(owner=self.get_identity()) return broker_delegation
def validate_delegation(*, delegation: ABCDelegation, check_requested: bool = False): if delegation is None: raise RPCException( message=Constants.NOT_SPECIFIED_PREFIX.format("delegation")) if delegation.get_slice_object() is None: raise RPCException( message=Constants.NOT_SPECIFIED_PREFIX.format("slice")) if check_requested and delegation.get_graph() is None: raise RPCException( message=Constants.NOT_SPECIFIED_PREFIX.format("graph"))
def do_reclaim_delegation(self, *, actor: ABCActorMixin, proxy: ABCBrokerProxy, delegation: ABCDelegation, callback: ABCClientCallbackProxy, caller: AuthToken, id_token: str = None): proxy.get_logger().info( "Outbound reclaim delegation request from <{}>: {}".format( caller.get_name(), delegation)) state = proxy.prepare_reclaim_delegation(delegation=delegation, callback=callback, caller=caller, id_token=id_token) state.set_caller(caller=caller) state.set_type(rtype=RPCRequestType.ReclaimDelegation) rpc = RPCRequest(request=state, actor=actor, proxy=proxy, delegation=delegation, sequence=delegation.get_sequence_out()) # Schedule a timeout rpc.timer = KernelTimer.schedule(queue=actor, task=ReclaimTimeout(req=rpc), delay=self.CLAIM_TIMEOUT_SECONDS) proxy.get_logger().info("Timer started: {} for Reclaim".format( rpc.timer)) self.enqueue(rpc=rpc)
def claim_delegation(self, *, delegation: ABCDelegation, callback: ABCClientCallbackProxy, caller: AuthToken, id_token: str = None): slice_obj = delegation.get_slice_object() if slice_obj is not None: slice_obj.set_broker_client() self.wrapper.claim_delegation_request(delegation=delegation, caller=caller, callback=callback, id_token=id_token)
def bind_delegation(self, *, delegation: ABCDelegation) -> bool: try: self.lock.acquire() self.delegations[delegation.get_delegation_id()] = delegation finally: self.lock.release() return False
def get_ticket(self, units: int, rtype: ResourceType, term: Term, source: ABCDelegation, actor: ABCActorMixin) -> Ticket: resource_ticket = ResourceTicketFactory.create(issuer=self.broker_guid, units=units, term=term, rtype=rtype) ticket = Ticket(resource_ticket=resource_ticket, plugin=actor.get_plugin(), authority=None) ticket.delegation_id = source.get_delegation_id() return ticket
def remove_delegation(self, *, delegation: ABCDelegation): try: self.lock.acquire() if delegation.get_delegation_id() in self.delegations: self.unmerge_adm(graph_id=delegation.get_delegation_id()) self.delegations.pop(delegation.get_delegation_id()) self.logger.debug( f"Removed Delegation: {delegation.get_delegation_id()}") else: self.logger.warning( f"Delegation ignored: {delegation.get_delegation_id()}") self.logger.debug(f"Active delegations: {self.delegations}") except Exception as e: self.logger.error(f"Failed to un-merge ADM: {delegation}") self.logger.error(traceback.format_exc()) raise e finally: self.lock.release()
def translate_delegation_to_avro(*, delegation: ABCDelegation) -> DelegationAvro: avro_delegation = DelegationAvro() avro_delegation.delegation_id = delegation.get_delegation_id() avro_delegation.state = delegation.get_state().value avro_delegation.slice = Translate.translate_slice_to_avro(slice_obj=delegation.get_slice_object()) if delegation.get_graph() is not None and not (delegation.is_reclaimed() or delegation.is_closed()): avro_delegation.graph = delegation.get_graph().serialize_graph() return avro_delegation
def absorb_delegation_update(self, *, incoming: ABCDelegation, update_data: UpdateData): """ Absorbs an incoming delegation update. @param incoming incoming delegation update @param update_data update data @throws Exception """ self.logger.debug("absorb_update: {}".format(incoming)) if self.authority is None and incoming.get_site_proxy() is not None: self.authority = incoming.get_site_proxy() self.graph = incoming.get_graph() self.policy.update_delegation_complete(delegation=self) if self.graph is not None: self.graph.delete_graph() self.graph = None
def extract(self, *, source: ABCDelegation, delegation: ResourceTicket) -> ResourceSet: """ Creates a new resource set using the source and the specified delegation. @param source source @param delegation delegation @return returns ResourceSet @throws Exception in case of error """ extracted = ResourceSet(units=delegation.get_units(), rtype=delegation.get_resource_type()) cset = Ticket(resource_ticket=delegation, plugin=self.actor.get_plugin(), authority=source.get_site_proxy(), delegation_id=source.get_delegation_id()) extracted.set_resources(cset=cset) return extracted
def add_source(self, *, source: ABCDelegation): """ Adds a source reservation. Creates a placeholder if necessary and adds the reservation to the holdings list. @params source: source reservation """ term = None try: self.lock.acquire() self.get_source_calendar(source=source) term = source.get_term() finally: self.lock.release() self.add_holdings(reservation=source, start=term.get_new_start_time(), end=term.get_end_time())
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))
def update_delegation(self, *, delegation: ABCDelegation): # Update the delegation only when there are changes to be reflected in database if not delegation.is_dirty(): return delegation.clear_dirty() try: self.lock.acquire() self.logger.debug("Updating delegation {} in slice {}".format( delegation.get_delegation_id(), delegation.get_slice_id())) properties = pickle.dumps(delegation) self.db.update_delegation(dlg_graph_id=str( delegation.get_delegation_id()), dlg_state=delegation.get_state().value, properties=properties) finally: self.lock.release()
def add_delegation(self, *, delegation: ABCDelegation): self.logger.debug("Adding delegation {} to slice {}".format( delegation.get_delegation_id(), delegation.get_slice_id())) slc_id = self.get_slice_id_from_guid( slice_id=delegation.get_slice_id()) if slc_id is None: raise DatabaseException("Slice with id: {} not found".format( delegation.get_slice_id())) try: self.lock.acquire() properties = pickle.dumps(delegation) self.db.add_delegation(dlg_slc_id=slc_id, dlg_graph_id=str( delegation.get_delegation_id()), dlg_state=delegation.get_state().value, properties=properties) self.logger.debug("Delegation {} added to slice {}".format( delegation.get_delegation_id(), delegation.get_slice_id())) finally: self.lock.release()
def do_update_delegation(self, *, actor: ABCActorMixin, proxy: ABCClientCallbackProxy, delegation: ABCDelegation, update_data: UpdateData, callback: ABCCallbackProxy, caller: AuthToken): proxy.get_logger().info( "Outbound update delegation request from <{}>: {}".format( caller.get_name(), delegation)) state = proxy.prepare_update_delegation(delegation=delegation, update_data=update_data, callback=callback, caller=caller) state.set_caller(caller=caller) state.set_type(rtype=RPCRequestType.UpdateDelegation) rpc = RPCRequest(request=state, actor=actor, proxy=proxy, delegation=delegation, sequence=delegation.get_sequence_out()) self.enqueue(rpc=rpc)
def update_delegation(self, *, delegation: ABCDelegation): self.validate_delegation(delegation=delegation, check_requested=True) # get a callback to the actor calling updateTicket, so that any # failures in the remote actor can be delivered back callback = Proxy.get_callback( actor=delegation.get_actor(), protocol=delegation.get_callback().get_type()) if callback is None: raise RPCException( message=Constants.NOT_SPECIFIED_PREFIX.format("callback")) self.do_update_delegation(actor=delegation.get_actor(), proxy=delegation.get_callback(), delegation=delegation, update_data=delegation.get_update_data(), callback=callback, caller=delegation.get_actor().get_identity())
def update_delegation(self, *, incoming: ABCDelegation, update_data: UpdateData): """ Update delegation @param incoming incoming delegation @param update_data update data """ if self.state == DelegationState.Nascent or self.state == DelegationState.Delegated: if self.accept_delegation_update(incoming=incoming, update_data=update_data): if incoming.get_graph() is not None: self.transition(prefix="Delegation update", state=DelegationState.Delegated) else: self.transition(prefix="Delegation reclaimed", state=DelegationState.Reclaimed) self.set_dirty() elif self.state == DelegationState.Closed: self.logger.warning("Delegation update after close") elif self.state == DelegationState.Failed: self.logger.error(message="Delegation update on failed delegation: {}".format(update_data)) elif self.state == DelegationState.Reclaimed: self.transition(prefix="ticket update", state=DelegationState.Delegated)
def donate_delegation(self, *, delegation: ABCDelegation): self.delegations[delegation.get_delegation_id()] = delegation
def pass_delegation(delegation: ABCDelegation, auth: AuthToken) -> DelegationAvro: avro_delegation = Translate.translate_delegation_to_avro( delegation=delegation) avro_delegation.sequence = delegation.get_sequence_out() return avro_delegation
def unregister_delegation(self, *, delegation: ABCDelegation): if delegation.get_delegation_id() in self.delegations: self.delegations.pop(delegation.get_delegation_id())
def revisit_delegation(self, *, delegation: ABCDelegation): if delegation.get_state() == DelegationState.Delegated: self.bind_delegation(delegation=delegation)