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))
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))
def reclaim_delegation_client(self, *, delegation_id: str = None, slice_object: ABCSlice = None, broker: ABCBrokerProxy = None, id_token: str = None) -> ABCDelegation: if delegation_id is None: raise BrokerException(error_code=ExceptionErrorCode.INVALID_ARGUMENT, msg="delegation_id") if broker is None: broker = self.get_default_broker() if slice_object is None: slice_object = self.get_default_slice() if slice_object is None: slice_object = SliceFactory.create(slice_id=ID(), name=self.identity.get_name()) slice_object.set_owner(owner=self.identity) slice_object.set_inventory(value=True) delegation = BrokerDelegationFactory.create(did=delegation_id, slice_id=slice_object.get_slice_id(), broker=broker) delegation.set_slice_object(slice_object=slice_object) delegation.set_exported(value=True) callback = ActorRegistrySingleton.get().get_callback(protocol=Constants.PROTOCOL_KAFKA, actor_name=self.get_name()) if callback is None: raise BrokerException(error_code=ExceptionErrorCode.NOT_SUPPORTED, msg="callback is None") delegation.prepare(callback=callback, logger=self.logger) delegation.validate_outgoing() self.wrapper.reclaim_delegation_request(delegation=delegation, caller=broker, callback=callback, id_token=id_token) return delegation
def update_inventory_slice(self, *, slice_obj: ABCSlice): """ Update the resource pool @param slice_obj slice object """ try: slice_obj.set_dirty() self.db.update_slice(slice_object=slice_obj) except Exception as e: raise ResourcesException("Could not update slice {}".format(e))
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)
def recover_slice(self, *, slice_obj: ABCSlice): """ Recover slice @param slice_obj slice_obj """ slice_id = slice_obj.get_slice_id() if self.get_slice(slice_id=slice_id) is not None: self.logger.debug("Found slice_id: {} slice:{}".format( slice_id, slice_obj)) else: self.logger.info("Recovering slice: {}".format(slice_id)) self.recover_inventory_slice(slice_obj=slice_obj) self.recover_broker_slice(slice_obj=slice_obj) self.logger.debug("Informing the plugin about the slice") self.plugin.revisit(slice_obj=slice_obj) self.logger.debug("Registering slice: {}".format(slice_id)) self.re_register_slice(slice_object=slice_obj) self.logger.debug( "Recovering reservations in slice: {}".format(slice_id)) self.recover_reservations(slice_obj=slice_obj) self.logger.debug( "Recovering delegations in slice: {}".format(slice_id)) self.recover_delegations(slice_obj=slice_obj) self.logger.info("Recovery of slice {} complete".format(slice_id))
def revisit(self, *, slice_obj: ABCSlice = None, reservation: ABCReservationMixin = None, delegation: ABCDelegation = None): if slice_obj is not None and slice_obj.is_inventory(): self.logger.debug("Recovering inventory slice") self.recover_inventory_slice(slice_obj=slice_obj)
def update_slice(self, *, slice_object: ABCSlice): # Update the slice only when there are changes to be reflected in database if not slice_object.is_dirty(): return slice_object.clear_dirty() try: self.lock.acquire() properties = pickle.dumps(slice_object) self.db.update_slice(slc_guid=str(slice_object.get_slice_id()), slc_name=slice_object.get_name(), slc_type=slice_object.get_slice_type().value, slc_resource_type=str( slice_object.get_resource_type()), properties=properties, slc_graph_id=slice_object.get_graph_id(), lease_end=slice_object.get_lease_end(), lease_start=slice_object.get_lease_start()) finally: self.lock.release()
def get_source_delegation(self, broker: ABCBrokerMixin, slice_obj: ABCSlice, adm_index: int = 0): adm = self.adms[adm_index] broker_delegation = BrokerDelegationFactory.create( adm.graph_id, slice_id=slice_obj.get_slice_id(), broker=broker) broker_delegation.set_graph(graph=adm) broker_delegation.set_slice_object(slice_object=slice_obj) return broker_delegation
def recover_inventory_slice(self, *, slice_obj: ABCSlice) -> bool: """ Check and Reload ARM for an inventory slice for an AM @param slice_obj slice object @return True if ARM was reloaded; otherwise False """ if self.get_type() != ActorType.Authority: return False if not slice_obj.is_inventory(): return False # Check and Reload ARM if needed from fabric_cf.actor.core.container.globals import GlobalsSingleton arm_graph = GlobalsSingleton.get().check_and_reload_model( graph_id=slice_obj.get_graph_id()) if arm_graph is not None: slice_obj.set_graph(graph=arm_graph) return arm_graph is not None
def claim_delegation_client(self, *, delegation_id: str = None, slice_object: ABCSlice = None, broker: ABCBrokerProxy = None, id_token: str = None) -> ABCDelegation: if delegation_id is None: raise BrokerException(error_code=ExceptionErrorCode.INVALID_ARGUMENT, msg="delegation_id") if broker is None: broker = self.get_default_broker() if slice_object is None: slice_object = self.get_default_slice() if slice_object is None: slice_object = SliceFactory.create(slice_id=ID(), name=self.identity.get_name()) slice_object.set_owner(owner=self.identity) slice_object.set_inventory(value=True) delegation = BrokerDelegationFactory.create(did=delegation_id, slice_id=slice_object.get_slice_id(), broker=broker) delegation.set_exported(value=True) delegation.set_slice_object(slice_object=slice_object) self.wrapper.delegate(delegation=delegation, destination=self, id_token=id_token) return delegation
def translate_slice_to_avro(*, slice_obj: ABCSlice) -> SliceAvro: avro_slice = SliceAvro() avro_slice.slice_name = slice_obj.get_name() avro_slice.guid = str(slice_obj.get_slice_id()) avro_slice.description = slice_obj.get_description() avro_slice.owner = Translate.translate_auth_to_avro(auth=slice_obj.get_owner()) avro_slice.state = slice_obj.get_state().value avro_slice.config_properties = slice_obj.get_config_properties() avro_slice.set_lease_end(lease_end=slice_obj.get_lease_end()) avro_slice.set_lease_start(lease_start=slice_obj.get_lease_start()) if slice_obj.get_resource_type() is not None: avro_slice.set_resource_type(str(slice_obj.get_resource_type())) avro_slice.set_client_slice(slice_obj.is_client()) if slice_obj.get_graph_id() is not None: avro_slice.graph_id = slice_obj.get_graph_id() return avro_slice
def add_slice(self, *, slice_object: ABCSlice): try: if self.get_slice( slice_id=slice_object.get_slice_id()) is not None: raise DatabaseException("Slice # {} already exists".format( slice_object.get_slice_id())) self.lock.acquire() properties = pickle.dumps(slice_object) oidc_claim_sub = None email = None if slice_object.get_owner() is not None: oidc_claim_sub = slice_object.get_owner().get_oidc_sub_claim() email = slice_object.get_owner().get_email() self.db.add_slice(slc_guid=str(slice_object.get_slice_id()), slc_name=slice_object.get_name(), slc_type=slice_object.get_slice_type().value, slc_resource_type=str( slice_object.get_resource_type()), properties=properties, slc_graph_id=slice_object.get_graph_id(), oidc_claim_sub=oidc_claim_sub, email=email, lease_end=slice_object.get_lease_end(), lease_start=slice_object.get_lease_start()) finally: self.lock.release()
def remove_slice(self, *, slice_object: ABCSlice): self.wrapper.remove_slice(slice_id=slice_object.get_slice_id())
def recover_inventory_slice(self, *, slice_obj: ABCSlice): if slice_obj.get_graph_id() is not None: self.actor.load_model(graph_id=slice_obj.get_graph_id())
def unregister_slice(self, *, slice_object: ABCSlice): """ Unregister slice @param slice_obj slice object """ self.wrapper.unregister_slice(slice_id=slice_object.get_slice_id())
def attach_properties(*, slice_mng: SliceAvro, slice_obj: ABCSlice) -> SliceAvro: if slice_obj.get_config_properties() is not None: slice_mng.set_config_properties(slice_obj.get_config_properties()) return slice_mng