def get_or_create_counts(self, *, resource_type: ResourceType) -> CountsPerType: """ Returns or creates a new count entry for the given resource type. Args: resource_type: resource type Returns: count entry for the given resource type """ result = self.map.get(resource_type.get_type(), None) if result is None: result = CountsPerType(resource_type=resource_type) self.map[resource_type.get_type()] = result return result
def create_sliver(self, name: str, pci_address: str, gpu_name: str) -> Unit: u = Unit(rid=ID(uid=name)) sliver = NodeSliver() cap = Capacities(core=2, ram=6, disk=10) sliver.set_properties(type=NodeType.VM, site="UKY", capacity_allocations=cap) sliver.label_allocations = Labels(instance_parent="uky-w2.fabric-testbed.net") catalog = InstanceCatalog() instance_type = catalog.map_capacities_to_instance(cap=cap) cap_hints = CapacityHints(instance_type=instance_type) sliver.set_properties(capacity_hints=cap_hints, capacity_allocations=catalog.get_instance_capacities(instance_type=instance_type)) sliver.set_properties(name=name) sliver.set_properties(image_type='qcow2', image_ref='default_ubuntu_20') component = ComponentSliver() labels = Labels(bdf=pci_address) component.set_properties(type=ComponentType.GPU, model='Tesla T4', name=gpu_name, label_allocations=labels) sliver.attached_components_info = AttachedComponentsInfo() sliver.attached_components_info.add_device(device_info=component) u.set_sliver(sliver=sliver) u.set_resource_type(rtype=ResourceType(resource_type=NodeType.VM.name)) return u
def test_c_fail(self): controller = self.get_controller() clock = controller.get_actor_clock() Term.clock = clock resources = ResourceSet(units=1, rtype=ResourceType(resource_type="1")) slice_obj = SliceFactory.create(slice_id=ID(), name="fail") controller.register_slice(slice_object=slice_obj) start = 5 end = 10 term = Term(start=clock.cycle_start_date(cycle=start), end=clock.cycle_end_date(cycle=end)) r1 = ClientReservationFactory.create(rid=ID(), resources=resources, term=term, slice_object=slice_obj) r1.set_renewable(renewable=False) controller.register(reservation=r1) controller.demand(rid=r1.get_reservation_id()) r2 = ClientReservationFactory.create(rid=ID(), resources=resources, term=term, slice_object=slice_obj) r2.set_renewable(renewable=False) controller.register(reservation=r2) controller.demand(rid=r2.get_reservation_id()) for i in range(1, end + 3): controller.external_tick(cycle=i) while controller.get_current_cycle() != i: time.sleep(0.001) if i >= start and (i < (end - 1)): self.assertTrue(r1.is_closed()) self.assertTrue(r2.is_closed()) self.assertTrue(r2.get_notices().__contains__(Constants.CLOSURE_BY_TICKET_REVIEW_POLICY))
def read_resource_config( *, config: ActorConfig) -> Dict[ResourceType, ResourceConfig]: """ Read resource config and create ARM and inventory slices @param config actor config @raises ConfigurationException in case of error """ result = {} resources = config.get_resources() if resources is None or len(resources) == 0: return result for r in resources: for resource_type in r.get_type(): descriptor = ResourceConfig() descriptor.set_resource_type(rtype=ResourceType( resource_type=resource_type)) descriptor.set_resource_type_label(rtype_label=r.get_label()) handler = r.get_handler() if handler is not None: descriptor.set_handler_class( handler_class=handler.get_class_name()) descriptor.set_handler_module( module=handler.get_module_name()) descriptor.set_handler_properties( properties=handler.get_properties()) result[descriptor.get_resource_type()] = descriptor return result
def make_site_policy(self, *, config: ActorConfig): """ Creates AM Policy instance and set up controls @param config actor config @raises ConfigurationException in case of error """ policy = None if config.get_policy() is not None: policy = self.make_policy(policy=config.get_policy()) else: policy = AuthorityCalendarPolicy() for c in config.get_controls(): try: if c.get_module_name() is None or c.get_class_name() is None: raise ConfigurationException("Missing control class name") control = ReflectionUtils.create_instance( module_name=c.get_module_name(), class_name=c.get_class_name()) control.set_actor(actor=self.actor) if c.get_type() is None: raise ConfigurationException( "No type specified for control") for t in c.get_type(): control.add_type(rtype=ResourceType(resource_type=t)) policy.register_control(control=control) except Exception as e: self.logger.error(traceback.format_exc()) raise ConfigurationException( "Could not create control {}".format(e)) return policy
def process_neo4j(self, substrate_file: str, actor_name: str) -> Dict: """ Create ARM and Inventory Slices """ from fabric_cf.actor.core.container.globals import GlobalsSingleton self.container = GlobalsSingleton.get().get_container() result = self.substrate.get_inventory_slice_manager().create_inventory_slice( slice_id=ID(), name=actor_name, rtype=ResourceType(resource_type=Constants.PROPERTY_AGGREGATE_RESOURCE_MODEL)) if result.code != InventorySliceManagerError.ErrorNone: raise AggregateResourceModelCreatorException(f"Could not create ARM: {actor_name}. error={result.code}") self.logger.debug(f"Created aggregate manager resource slice# {result.slice}") if result.slice.get_graph_id() is not None: # load the graph from Neo4j database self.logger.debug(f"Reloading an existing graph for resource slice# {result.slice}") self.arm_graph = FimHelper.get_arm_graph(graph_id=result.slice.get_graph_id()) result.slice.set_graph(graph=self.arm_graph) else: self.arm_graph = FimHelper.get_arm_graph_from_file(filename=substrate_file) result.slice.set_graph(graph=self.arm_graph) self.substrate.get_inventory_slice_manager().update_inventory_slice(slice_obj=result.slice) self.logger.debug(f"Created new graph for resource slice# {result.slice}") for r in self.resources.values(): self.logger.debug(f"Registering resource_handler for resource_type: {r.get_resource_type_label()} " f"for Actor {actor_name}") self.register_handler(resource_config=r) return self.arm_graph.generate_adms()
def get_broker_policy(self) -> ABCBrokerPolicyMixin: policy = BrokerSimplerUnitsPolicy() resource_type = ResourceType(resource_type=NodeType.VM.name) inventory = NetworkNodeInventory() policy.register_inventory(resource_type=resource_type, inventory=inventory) return policy
def extend_reservation(self, *, request: ExtendReservationAvro) -> ResultStringAvro: result = ResultStringAvro() result.message_id = request.message_id try: if request.guid is None or request.reservation_id is None: result.status.set_code(ErrorCodes.ErrorInvalidArguments.value) result.status.set_message(ErrorCodes.ErrorInvalidArguments.interpret()) return result auth = Translate.translate_auth_from_avro(auth_avro=request.auth) mo = self.get_actor_mo(guid=ID(uid=request.guid)) end_time = ActorClock.from_milliseconds(milli_seconds=request.end_time) rtype = None if request.new_resource_type is not None: rtype = ResourceType(resource_type=request.new_resource_type) result.status = mo.extend_reservation(rid=ID(uid=request.reservation_id), new_end_time=end_time, new_units=request.new_units, new_resource_type=rtype, request_properties=request.request_properties, config_properties=request.config_properties, caller=auth) except Exception as 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) result.message_id = request.message_id return result
def get_counts(self, *, resource_type: ResourceType) -> CountsPerType: """ Returns the count entry for the given resource type. Args: resource_type: resource type Returns: count entry for the given resource type """ return self.map.get(resource_type.get_type(), None)
def get_extend_lease_request(self, authority: ABCAuthority, delegation: ABCDelegation, request: ABCAuthorityReservation): req_start = authority.get_actor_clock().cycle_start_date(cycle=self.TicketStartCycle) req_new_start = authority.get_actor_clock().cycle_start_date(cycle=self.TicketEndCycle + 1) req_end = authority.get_actor_clock().cycle_end_date(cycle=self.TicketNewEndCycle) req_term = Term(start=req_start, end=req_end, new_start=req_new_start) rtype = ResourceType(resource_type=request.get_requested_resources().get_sliver().resource_type.name) ticket = self.get_ticket(self.TicketUnits, rtype, req_term, delegation, authority) new_request = self.get_request_from_request(request, req_term, ticket) return new_request
def get_redeem_request(self, authority: ABCAuthority, delegation: ABCDelegation): req_start = authority.get_actor_clock().cycle_start_date(cycle=self.TicketStartCycle) req_end = authority.get_actor_clock().cycle_end_date(cycle=self.TicketEndCycle) req_term = Term(start=req_start, end=req_end) sliver = self.build_sliver() ticket = self.get_ticket(units=self.TicketUnits, rtype=ResourceType(resource_type=sliver.resource_type.name), term=req_term, source=delegation, actor=authority) request = self.get_request(req_term, ticket, sliver) return request
def assign( self, *, reservation: ABCAuthorityReservation, delegation_name: str, graph_node: BaseSliver, existing_reservations: List[ABCReservationMixin]) -> ResourceSet: """ Assign a reservation :param reservation: reservation :param delegation_name: Name of delegation serving the request :param graph_node: ARM Graph Node serving the reservation :param existing_reservations: Existing Reservations served by the same ARM node :return: ResourceSet with updated sliver annotated with properties :raises: AuthorityException in case the request cannot be satisfied """ reservation.set_send_with_deficit(value=True) requested = reservation.get_requested_resources().get_sliver() self.__dump_sliver(sliver=requested) if not isinstance(requested, NetworkServiceSliver): raise AuthorityException( f"Invalid resource type {requested.get_type()}") current = reservation.get_resources() resource_type = ResourceType(resource_type=str(requested.get_type())) gained = None lost = None if current is None: self.logger.debug("check if sliver can be provisioned") # FIXME Add validation to check ticketed sliver against ARM # Refer Network Node Control for reference self.logger.debug( f"Slice properties: {reservation.get_slice().get_config_properties()}" ) unit = Unit( rid=reservation.get_reservation_id(), slice_id=reservation.get_slice_id(), actor_id=self.authority.get_guid(), sliver=requested, rtype=resource_type, properties=reservation.get_slice().get_config_properties()) gained = UnitSet(plugin=self.authority.get_plugin(), units={unit.reservation_id: unit}) else: # FIXME: handle modify self.logger.info( f"Extend Lease for now, no modify supported res# {reservation}" ) return current result = ResourceSet(gained=gained, lost=lost, rtype=resource_type) result.set_sliver(sliver=requested) return result
def get_reservation_for_network_node(self, start: datetime, end: datetime, sliver: NodeSliver): slice_obj = SliceFactory.create(slice_id=ID(), name="test-slice") rtype = ResourceType(resource_type=sliver.resource_type.name) rset = ResourceSet(units=1, rtype=rtype, sliver=sliver) term = Term(start=start, end=end) request = BrokerReservationFactory.create(rid=ID(), resources=rset, term=term, slice_obj=slice_obj) return request
def test_unit(self): rid = ID() u1 = Unit(rid=rid) self.assertIsNotNone(u1.get_id()) self.assertEqual(UnitState.DEFAULT, u1.get_state()) self.assertIsNone(u1.get_property(name="foo")) self.assertIsNone(u1.get_parent_id()) self.assertIsNotNone(u1.get_reservation_id()) self.assertIsNone(u1.get_slice_id()) self.assertIsNone(u1.get_actor_id()) self.assertEqual(0, u1.get_sequence()) u1.increment_sequence() self.assertEqual(1, u1.get_sequence()) u1.decrement_sequence() self.assertEqual(0, u1.get_sequence()) db = self.make_actor_database() slice_id = ID() from fabric_cf.actor.core.container.globals import GlobalsSingleton actor_id = GlobalsSingleton.get().get_container().get_actor().get_guid( ) slice_obj = SliceFactory.create(slice_id=slice_id, name="test_slice") db.add_slice(slice_object=slice_obj) reservation = ClientReservationFactory.create(rid=rid, slice_object=slice_obj) u1.set_actor_id(actor_id=actor_id) u1.set_reservation(reservation=reservation) u1.set_slice_id(slice_id=slice_id) db.add_reservation(reservation=reservation) u1.start_prime() self.assertEqual(UnitState.PRIMING, u1.get_state()) u1.set_property(name="foo", value="bar") u1.increment_sequence() u1.increment_sequence() resource_type = ResourceType(resource_type="1") u1.set_resource_type(rtype=resource_type) self.assertEqual(2, u1.get_sequence()) db.add_unit(u=u1) self.assertIsNotNone(db.get_unit(uid=rid))
def test_d_nascent(self): controller = self.get_controller() clock = controller.get_actor_clock() Term.clock = clock resources = ResourceSet(units=1, rtype=ResourceType(resource_type="1")) slice_obj = SliceFactory.create(slice_id=ID(), name="nascent") controller.register_slice(slice_object=slice_obj) start = 5 end = 10 term = Term(start=clock.cycle_start_date(cycle=start), end=clock.cycle_end_date(cycle=end)) r1 = ClientReservationFactory.create(rid=ID(), resources=resources, term=term, slice_object=slice_obj) r1.set_renewable(renewable=False) controller.register(reservation=r1) controller.demand(rid=r1.get_reservation_id()) r2 = ClientReservationFactory.create(rid=ID(), resources=resources, term=term, slice_object=slice_obj) r2.set_renewable(renewable=False) controller.register(reservation=r2) r2demanded = False for i in range(1, end + 3): controller.external_tick(cycle=i) while controller.get_current_cycle() != i: time.sleep(0.001) if i == (start -3) and not r2demanded: self.assertTrue(r1.is_ticketed()) self.assertTrue(r2.is_nascent()) controller.demand(rid=r2.get_reservation_id()) r2demanded = True if i >= start and (i < end - 1): self.assertTrue(r1.is_active()) self.assertTrue(r2.is_active()) if i > end: self.assertTrue(r1.is_closed()) self.assertTrue(r2.is_closed())
def translate_unit_from_avro(*, unit_avro: UnitAvro) -> Unit: unit = Unit(rid=ID(uid=unit_avro.reservation_id)) if unit_avro.properties is not None: unit.properties = unit_avro.properties if unit_avro.rtype is not None: unit.rtype = ResourceType(resource_type=unit.rtype) if unit_avro.parent_id is not None: unit.parent_id = ID(uid=unit_avro.parent_id) if unit_avro.slice_id is not None: unit.slice_id = ID(uid=unit_avro.slice_id) if unit_avro.actor_id is not None: unit.actor_id = ID(uid=unit_avro.actor_id) if unit_avro.state is not None: unit.state = UnitState(unit_avro.state) return unit
def translate_resource_ticket_from_avro(*, resource_ticket: ResourceTicketAvro) -> ResourceTicket: rd = ResourceTicket() rd.units = resource_ticket.units if resource_ticket.term is not None: rd.term = Translate.translate_term_from_avro(term=resource_ticket.term) if resource_ticket.type is not None: rd.type = ResourceType(resource_type=resource_ticket.type) if resource_ticket.guid is not None: rd.guid = ID(uid=resource_ticket.guid) if resource_ticket.properties is not None: rd.properties = resource_ticket.properties if resource_ticket.issuer is not None: rd.issuer = ID(uid=resource_ticket.issuer) if resource_ticket.holder is not None: rd.holder = ID(uid=resource_ticket.holder) return rd
def make_broker_policy(self, *, config: ActorConfig): """ Creates AM Policy instance and set up controls @param config actor config @raises ConfigurationException in case of error """ policy = None if config.get_policy() is not None: policy = self.make_policy(policy=config.get_policy()) properties = config.get_policy().get_properties() policy.set_properties(properties=properties) else: policy = BrokerSimplerUnitsPolicy() for i in config.get_controls(): try: if i.get_module_name() is None or i.get_class_name() is None: raise ConfigurationException( "Missing inventory class name") inventory = ReflectionUtils.create_instance( module_name=i.get_module_name(), class_name=i.get_class_name()) inventory.set_logger(logger=self.logger) if i.get_type() is None: raise ConfigurationException( "No type specified for control") for t in i.get_type(): policy.inventory.add_inventory_by_type( rtype=ResourceType(resource_type=t), inventory=inventory) except Exception as e: self.logger.error(traceback.format_exc()) raise ConfigurationException( "Could not create control {}".format(e)) return policy
def test_d_add_update_get_unit(self): actor = self.prepare_actor_database() db = actor.get_plugin().get_database() slice_obj = SliceFactory.create(slice_id=ID(), name="slice-1") db.add_slice(slice_object=slice_obj) rset = ResourceSet() term = Term(start=datetime.now(), end=datetime.now().replace(minute=20)) res = AuthorityReservationFactory.create(resources=rset, term=term, slice_obj=slice_obj, rid=ID()) db.add_reservation(reservation=res) rtype = ResourceType(resource_type="12") u = Unit(rid=res.get_reservation_id(), slice_id=slice_obj.get_slice_id(), actor_id=actor.get_guid()) u.set_resource_type(rtype=rtype) db.add_unit(u=u) self.assertIsNotNone(db.get_unit(uid=u.get_id()))
def get_request(self, term: Term, ticket: Ticket, sliver: BaseSliver): rset = ResourceSet(units=1, rtype=ResourceType(resource_type=sliver.resource_type.name), sliver=sliver) rset.set_resources(cset=ticket) slice_object = self.get_request_slice() return AuthorityReservationFactory.create(resources=rset, term=term, slice_obj=slice_object, rid=ID())
def get_control(self) -> ResourceControl: control = NetworkNodeControl() control.add_type(rtype=ResourceType(resource_type=NodeType.VM.name)) return control
def get_resource_set(*, res_mng: ReservationMng) -> ResourceSet: return ResourceSet( units=res_mng.get_units(), rtype=ResourceType(resource_type=res_mng.get_resource_type()), sliver=res_mng.get_sliver())
def assign( self, *, reservation: ABCAuthorityReservation, delegation_name: str, graph_node: BaseSliver, existing_reservations: List[ABCReservationMixin]) -> ResourceSet: """ Assign a reservation :param reservation: reservation :param delegation_name: Name of delegation serving the request :param graph_node: ARM Graph Node serving the reservation :param existing_reservations: Existing Reservations served by the same ARM node :return: ResourceSet with updated sliver annotated with properties :raises: AuthorityException in case the request cannot be satisfied """ if graph_node.capacity_delegations is None or reservation is None: raise AuthorityException(Constants.INVALID_ARGUMENT) delegated_capacities = graph_node.get_capacity_delegations() available_delegated_capacity = FimHelper.get_delegation( delegated_capacities=delegated_capacities, delegation_name=delegation_name) if available_delegated_capacity is None: raise AuthorityException( f"Allocated node {graph_node.node_id} does not have delegation: {delegation_name}" ) reservation.set_send_with_deficit(value=True) requested = reservation.get_requested_resources().get_sliver() if not isinstance(requested, NodeSliver): raise AuthorityException( f"Invalid resource type {requested.get_type()}") current = reservation.get_resources() resource_type = ResourceType(resource_type=str(requested.get_type())) gained = None lost = None if current is None: # Check if Capacities can be satisfied by Delegated Capacities self.__check_capacities( rid=reservation.get_reservation_id(), requested_capacities=requested.get_capacity_allocations(), available_capacities=available_delegated_capacity, existing_reservations=existing_reservations) # Check if Capacities can be satisfied by Capacities self.__check_capacities( rid=reservation.get_reservation_id(), requested_capacities=requested.get_capacity_allocations(), available_capacities=graph_node.get_capacities(), existing_reservations=existing_reservations) # Check components # Check if Components can be allocated if requested.attached_components_info is not None: self.__check_components( rid=reservation.get_reservation_id(), requested_components=requested.attached_components_info, graph_node=graph_node, existing_reservations=existing_reservations) self.logger.debug( f"Slice properties: {reservation.get_slice().get_config_properties()}" ) unit = Unit( rid=reservation.get_reservation_id(), slice_id=reservation.get_slice_id(), actor_id=self.authority.get_guid(), sliver=requested, rtype=resource_type, properties=reservation.get_slice().get_config_properties()) gained = UnitSet(plugin=self.authority.get_plugin(), units={unit.reservation_id: unit}) else: # FIX ME: handle modify self.logger.info( f"Extend Lease for now, no modify supported res# {reservation}" ) return current result = ResourceSet(gained=gained, lost=lost, rtype=resource_type) result.set_sliver(sliver=requested) return result
def translate_resource_set_from_avro(*, rset: ResourceSetAvro) -> ResourceSet: result = ResourceSet(units=rset.units, rtype=ResourceType(resource_type=rset.type), sliver=rset.get_sliver()) return result