Пример #1
0
    def test_h_unregister_slice_empty(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slices = []

        for i in range(10):
            slice_obj = SliceFactory.create(slice_id=ID(),
                                            name="Slice:{}".format(i))
            slices.append(slice_obj)
            kernel.register_slice(slice_object=slice_obj)

        for s in slices:
            kernel.unregister_slice(slice_id=s.get_slice_id())
            check = kernel.get_slice(slice_id=s.get_slice_id())
            self.assertIsNone(check)
            self.enforceSliceExistsInDatabase(db=db, slice_id=s.get_slice_id())

            failed = False
            try:
                kernel.unregister_slice(slice_id=s.get_slice_id())
            except Exception:
                failed = True

            self.assertTrue(failed)
            self.enforceSliceExistsInDatabase(db=db, slice_id=s.get_slice_id())
Пример #2
0
    def test_d_remove_slice_empty(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slice_list = []

        for i in range(10):
            slice_obj = SliceFactory.create(slice_id=ID(),
                                            name="Slice:{}".format(i))
            slice_obj.set_inventory(value=True)
            kernel.register_slice(slice_object=slice_obj)
            self.enforceSliceExistsInDatabase(
                db=db, slice_id=slice_obj.get_slice_id())
            slice_list.append(slice_obj)

        for s in slice_list:
            kernel.remove_slice(slice_id=s.get_slice_id())

            check = kernel.get_slice(slice_id=s.get_slice_id())
            self.assertIsNone(check)
            self.enforceSliceNotInDatabase(db=db, slice_id=s.get_slice_id())

            kernel.remove_slice(slice_id=s.get_slice_id())
            self.enforceSliceNotInDatabase(db=db, slice_id=s.get_slice_id())

            kernel.register_slice(slice_object=s)
            self.enforceSliceExistsInDatabase(db=db, slice_id=s.get_slice_id())
            self.assertIsNotNone(kernel.get_slice(slice_id=s.get_slice_id()))
Пример #3
0
    def test_a_register_reservation(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slice_obj = SliceFactory.create(slice_id=ID(), name="test_slice")
        kernel.register_slice(slice_object=slice_obj)
        self.base_slices_count += 1

        for i in range(10):
            rid = ID()
            reservation = ClientReservationFactory.create(
                rid=rid, slice_object=slice_obj)
            kernel.register_reservation(reservation=reservation)

            self.assertIsNotNone(kernel.get_reservation(rid=rid))
            self.enforceReservationExistsInDatabase(db=db, rid=rid)

            failed = False

            try:
                kernel.register_reservation(reservation=reservation)
            except Exception:
                failed = True

            self.assertTrue(failed)
            self.assertIsNotNone(kernel.get_reservation(rid=rid))
            self.enforceReservationExistsInDatabase(db=db, rid=rid)
Пример #4
0
    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 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 test_f_query(self):
        broker = self.get_broker()
        policy = broker.get_policy()

        request = BrokerPolicy.get_broker_query_model_query(level=1)
        response = policy.query(p=request)

        print(response)

        self.check_query_response(response, 0)

        slice_obj = SliceFactory.create(slice_id=ID(), name="inventory_slice")
        slice_obj.set_inventory(value=True)

        adm_list_len = len(self.adms)
        adm_list_len -= 1

        self.broker.register_slice(slice_object=slice_obj)
        for i in range(0, adm_list_len):
            request = BrokerPolicy.get_broker_query_model_query(level=i + 1)
            source = self.get_source_delegation(self.broker,
                                                slice_obj,
                                                adm_index=i)
            self.broker.register_delegation(delegation=source)
            self.broker.donate_delegation(delegation=source)

            response = policy.query(p=request)
            print(response)
            self.check_query_response(response, i + 1)
Пример #7
0
    def test_j_register_reservation_error(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slice_obj = SliceFactory.create(slice_id=ID(), name="test_slice")
        kernel.register_slice(slice_object=slice_obj)

        # Set the database to null to cause errors while registering slices.
        actor.get_plugin().set_database(db=None)

        for i in range(10):
            rid = ID()
            reservation = ClientReservationFactory.create(
                rid=rid, slice_object=slice_obj)
            failed = False

            try:
                kernel.register_reservation(reservation=reservation)
            except Exception:
                failed = True

            self.assertTrue(failed)
            self.assertIsNone(kernel.get_reservation(rid=rid))
            self.enforceReservationNotInDatabase(db=db, rid=rid)
 def test_c_add_slice(self):
     db = self.get_database_to_test()
     slice_obj = SliceFactory.create(slice_id=ID(), name="slice_to_add")
     self.assertEqual("slice_to_add", slice_obj.get_name())
     db.add_slice(slice_object=slice_obj)
     slice2 = db.get_slice(slice_id=slice_obj.get_slice_id())
     self.assertIsNotNone(slice2)
     db.remove_slice(slice_id=slice_obj.get_slice_id())
    def test_b_allocate_ticket(self):
        """
        Requests a ticket for all resources. Checks if the ticket is
        allocated for what was asked. Checks the term. Checks whether the
        reservation is closed when it expires.
        """
        self.broker = self.get_broker()
        controller = self.get_controller()
        clock = self.broker.get_actor_clock()

        proxy = ClientCallbackHelper(name=controller.get_name(),
                                     guid=controller.get_guid())
        broker_callback = ClientCallbackHelper(name=self.broker.get_name(),
                                               guid=self.broker.get_guid())
        ActorRegistrySingleton.get().register_callback(callback=proxy)
        ActorRegistrySingleton.get().register_callback(
            callback=broker_callback)
        last_called = proxy.get_called()

        inv_slice = SliceFactory.create(slice_id=ID(), name="inventory-slice")
        inv_slice.set_inventory(value=True)
        source = self.get_source_delegation(self.broker, inv_slice)

        self.broker.register_slice(slice_object=inv_slice)
        self.broker.register_delegation(delegation=source)
        self.broker.donate_delegation(delegation=source)

        cycle = 1
        self.broker.external_tick(cycle=cycle)
        cycle += 1

        units = 1
        start = clock.cycle_start_date(cycle=self.DonateStartCycle)
        end = clock.cycle_end_date(cycle=self.DonateEndCycle - 1)

        sliver = self.build_sliver()
        request = self.get_reservation_for_network_node(start, end, sliver)

        self.broker.ticket(reservation=request,
                           callback=proxy,
                           caller=proxy.get_identity())

        for c in range(cycle, self.DonateEndCycle):
            self.broker.external_tick(cycle=c)
            while self.broker.get_current_cycle() != c:
                time.sleep(0.001)

            if last_called < proxy.get_called():
                self.assert_ticketed(proxy.get_reservation(), units,
                                     request.get_type(), start, end)
                last_called = proxy.get_called()

        self.broker.await_no_pending_reservations()

        self.assertEqual(1, proxy.get_called())
        self.assertTrue(request.is_closed())
 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
Пример #11
0
    def test_g_unregister_reservation(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slice_obj = SliceFactory.create(slice_id=ID(), name="testslice")
        kernel.register_slice(slice_object=slice_obj)

        res_list = []

        for i in range(10):
            res = ClientReservationFactory.create(rid=ID())
            res.set_slice(slice_object=slice_obj)
            res_list.append(res)

            self.assertIsNone(
                kernel.get_reservation(rid=res.get_reservation_id()))
            self.enforceReservationNotInDatabase(db=db,
                                                 rid=res.get_reservation_id())

            failed = False

            try:
                kernel.unregister_reservation(rid=res.get_reservation_id())
            except Exception:
                failed = True

            self.assertTrue(failed)

            self.assertIsNone(
                kernel.get_reservation(rid=res.get_reservation_id()))
            self.enforceReservationNotInDatabase(db=db,
                                                 rid=res.get_reservation_id())
            kernel.register_reservation(reservation=res)
            self.assertIsNotNone(
                kernel.get_reservation(rid=res.get_reservation_id()))
            self.enforceReservationExistsInDatabase(
                db=db, rid=res.get_reservation_id())

        for res in res_list:
            self.assertIsNotNone(
                kernel.get_slice(slice_id=slice_obj.get_slice_id()))
            self.enforceSliceExistsInDatabase(
                db=db, slice_id=slice_obj.get_slice_id())
            res.fail(message="forced")
            kernel.unregister_reservation(rid=res.get_reservation_id())
            self.assertIsNone(
                kernel.get_reservation(rid=res.get_reservation_id()))
            self.enforceReservationExistsInDatabase(
                db=db, rid=res.get_reservation_id())

        check = kernel.get_reservations(slice_id=slice_obj.get_slice_id())
        self.assertIsNotNone(check)
        self.assertEqual(0, len(check))
Пример #12
0
    def advertise(self, *, delegation: ABCPropertyGraph, delegation_name: str, client: AuthToken) -> str:
        slice_obj = SliceFactory.create(slice_id=ID(), name=client.get_name())
        slice_obj.set_owner(owner=client)
        slice_obj.set_broker_client()

        dlg_obj = DelegationFactory.create(did=delegation.get_graph_id(), slice_id=slice_obj.get_slice_id(),
                                           delegation_name=delegation_name)
        dlg_obj.set_slice_object(slice_object=slice_obj)
        dlg_obj.set_graph(graph=delegation)
        self.wrapper.advertise(delegation=dlg_obj, client=client)
        return dlg_obj.get_delegation_id()
    def get_delegation(self, actor: ABCActorMixin) -> ABCDelegation:
        slice_object = SliceFactory.create(slice_id=ID(), name="inventory-slice")
        slice_object.set_inventory(value=True)
        actor.register_slice(slice_object=slice_object)

        delegation_name = 'primary'

        dlg_obj = DelegationFactory.create(did=self.adm.get_graph_id(), slice_id=slice_object.get_slice_id(),
                                           delegation_name=delegation_name)
        dlg_obj.set_slice_object(slice_object=slice_object)
        dlg_obj.set_graph(graph=self.adm)
        actor.register_delegation(delegation=dlg_obj)
        return dlg_obj
Пример #14
0
    def test_c_register_slice_inventory(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        slices = kernel.get_slices()
        self.assertIsNotNone(slices)
        self.assertEqual(0, len(slices))

        for i in range(10):
            slice_obj = SliceFactory.create(slice_id=ID(),
                                            name="Slice:{}".format(i))
            slice_obj.set_inventory(value=True)
            kernel.register_slice(slice_object=slice_obj)

            check = kernel.get_slice(slice_id=slice_obj.get_slice_id())
            self.assertIsNotNone(check)
            self.assertEqual(check, slice_obj)

            slices = kernel.get_slices()
            self.assertIsNotNone(slices)
            self.assertEqual(i + 1, len(slices))

            slices = kernel.get_inventory_slices()
            self.assertIsNotNone(slices)
            self.assertEqual(i + 1, len(slices))

            self.enforceSliceExistsInDatabase(
                db=db, slice_id=slice_obj.get_slice_id())

            slices = db.get_slices()
            self.assertIsNotNone(slice_obj)
            self.assertEqual(i + 1, len(slices))

            slices = db.get_inventory_slices()
            self.assertIsNotNone(slice_obj)
            self.assertEqual(i + 1, len(slices))

            failed = False

            try:
                kernel.register_slice(slice_object=slice_obj)
            except Exception:
                failed = True

            self.assertTrue(failed)

            ss = kernel.get_slice(slice_id=slice_obj.get_slice_id())
            self.assertIsNotNone(ss)
            self.assertEqual(ss, slice_obj)
Пример #15
0
    def translate_slice(*, slice_avro: SliceAvro) -> ABCSlice:
        if slice_avro is None:
            return None
        if slice_avro.guid is None and slice_avro.slice_name is None:
            return None

        if slice_avro.guid is None:
            raise ProxyException(Constants.NOT_SPECIFIED_PREFIX.format("Slice id"))

        slice_obj = SliceFactory.create(slice_id=slice_avro.guid, name=slice_avro.slice_name)
        slice_obj.set_description(description=slice_avro.description)
        slice_obj.set_config_properties(value=slice_avro.config_properties)
        slice_obj.set_lease_start(lease_start=slice_avro.get_lease_start())
        slice_obj.set_lease_end(lease_end=slice_avro.get_lease_end())
        return slice_obj
 def create_default_slice(self):
     """
     Create default slice
     @raises ConfigurationException in case of error
     """
     if self.actor.get_type() != ActorType.Authority:
         slice_obj = SliceFactory.create(slice_id=ID(),
                                         name=self.actor.get_name())
         slice_obj.set_inventory(value=True)
         try:
             self.actor.register_slice(slice_object=slice_obj)
         except Exception as e:
             self.logger.error(traceback.format_exc())
             raise ConfigurationException(
                 f"Could not create default slice for actor: {self.actor.get_name()} {e}"
             )
Пример #17
0
    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())
Пример #19
0
    def create_inventory_slice(
            self, *, slice_id: ID, name: str,
            rtype: ResourceType) -> CreateInventorySliceResult:
        """
        Create Inventory Pool at boot
        @param slice_id slice id
        @param name name
        @param rtype resource type
        """
        result = CreateInventorySliceResult()
        if slice_id is None or name is None or rtype is None:
            result.code = InventorySliceManagerError.ErrorInvalidArguments
            return result
        try:
            temp = self.db.get_slice(slice_id=slice_id)

            if temp is not None:
                result.code = InventorySliceManagerError.ErrorPoolExists
                return result

            slice_list = self.db.get_inventory_slices()
            if slice_list is not None and len(slice_list) > 0:
                for slice_obj in slice_list:
                    rt = slice_obj.get_resource_type()
                    if rt == rtype:
                        result.slice = slice_obj
                        result.code = InventorySliceManagerError.ErrorTypeExists
                        return result

            slice_obj = SliceFactory.create(slice_id=slice_id, name=name)
            slice_obj.set_inventory(value=True)
            slice_obj.set_owner(owner=self.identity.get_identity())
            slice_obj.set_resource_type(resource_type=rtype)

            try:
                self.db.add_slice(slice_object=slice_obj)
                result.slice = slice_obj
            except Exception:
                self.logger.error(traceback.format_exc())
                result.code = InventorySliceManagerError.ErrorDatabaseError
        except Exception:
            self.logger.error(traceback.format_exc())
            result.code = InventorySliceManagerError.ErrorInternalError
        return result
    def test_e_donate(self):
        broker = self.get_broker()
        policy = broker.get_policy()

        slice_obj = SliceFactory.create(slice_id=ID(), name="inventory_slice")
        slice_obj.set_inventory(value=True)

        adm_list_len = len(self.adms)
        adm_list_len -= 1

        self.broker.register_slice(slice_object=slice_obj)
        for i in range(0, adm_list_len):
            source = self.get_source_delegation(self.broker,
                                                slice_obj,
                                                adm_index=i)
            self.broker.register_delegation(delegation=source)
            self.broker.donate_delegation(delegation=source)

            self.assertEqual(i + 1, len(policy.delegations))
Пример #21
0
    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
Пример #22
0
    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()))
Пример #23
0
    def test_k_register_slice_error(self):
        actor = self.prepare_actor()
        kernel = self.get_kernel_wrapper(actor=actor)
        db = actor.get_plugin().get_database()

        actor.get_plugin().set_database(db=None)

        for i in range(10):
            slice_obj = SliceFactory.create(slice_id=ID(),
                                            name="Slice:{}".format(i))

            failed = False

            try:
                kernel.register_slice(slice_object=slice_obj)
            except Exception:
                failed = True

            self.assertTrue(failed)

            check = kernel.get_slice(slice_id=slice_obj.get_slice_id())
            self.assertIsNone(check)
            self.enforceSliceNotInDatabase(db=db,
                                           slice_id=slice_obj.get_slice_id())
Пример #24
0
    def add_slice(self,
                  *,
                  slice_obj: SliceAvro,
                  caller: AuthToken,
                  id_token: str = None) -> ResultStringAvro:
        result = ResultStringAvro()
        result.status = ResultAvro()

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

        else:
            try:
                user_dn = None
                user_email = None
                if id_token is not None:
                    fabric_token = AccessChecker.check_access(
                        action_id=ActionId.query,
                        resource_type=ResourceType.slice,
                        token=id_token,
                        logger=self.logger,
                        actor_type=self.actor.get_type(),
                        resource_id=str(slice_obj.slice_name))
                    user_dn = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_SUB, None)
                    user_email = fabric_token.get_decoded_token().get(
                        Constants.CLAIMS_EMAIL, None)

                    if user_dn is None:
                        result.status.set_code(
                            ErrorCodes.ErrorInvalidToken.value)
                        result.status.set_message(
                            ErrorCodes.ErrorInvalidToken.interpret())
                        return result

                slice_obj_new = SliceFactory.create(
                    slice_id=ID(), name=slice_obj.get_slice_name())

                slice_obj_new.set_description(
                    description=slice_obj.get_description())
                slice_obj_new.set_owner(owner=self.actor.get_identity())
                slice_obj_new.get_owner().set_oidc_sub_claim(
                    oidc_sub_claim=user_dn)
                slice_obj_new.get_owner().set_email(email=user_email)
                slice_obj_new.set_graph_id(graph_id=slice_obj.graph_id)
                slice_obj_new.set_config_properties(
                    value=slice_obj.get_config_properties())
                slice_obj_new.set_lease_end(
                    lease_end=slice_obj.get_lease_end())
                slice_obj_new.set_lease_start(lease_start=datetime.utcnow())

                if slice_obj.get_inventory():
                    slice_obj_new.set_inventory(value=True)

                elif slice_obj.is_broker_client_slice():
                    slice_obj.set_broker_client_slice(value=True)

                elif slice_obj.is_client_slice():
                    slice_obj.set_client_slice(value=True)

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

                    def run(self):
                        try:
                            self.actor.register_slice(
                                slice_object=slice_obj_new)
                        except Exception as e:
                            self.actor.get_plugin().release_slice(
                                slice_obj=slice_obj_new)
                            raise e

                        return None

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

                result.set_result(str(slice_obj_new.get_slice_id()))
            except Exception as e:
                self.logger.error("add_slice: {}".format(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)

        return result
    def add_client_slice(self, *, caller: AuthToken,
                         slice_mng: SliceAvro) -> ResultSliceAvro:
        result = ResultSliceAvro()
        result.status = ResultAvro()

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

        try:
            owner_mng = slice_mng.get_owner()
            owner = None
            owner_is_ok = False

            if owner_mng is not None:
                owner = Translate.translate_auth_from_avro(auth_avro=owner_mng)
                if owner is not None and owner.get_name(
                ) is not None and owner.get_guid() is not None:
                    owner_is_ok = True

            if not owner_is_ok:
                result.status.set_code(ErrorCodes.ErrorDatabaseError.value)
                result.status.set_message(
                    ErrorCodes.ErrorDatabaseError.interpret())
                return result

            slice_obj = SliceFactory.create(slice_id=ID(),
                                            name=slice_mng.get_slice_name())
            slice_obj.set_description(description=slice_mng.get_description())
            slice_obj.set_inventory(value=False)

            assert owner is not None
            slice_obj.set_owner(owner=owner)

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

                def run(self):
                    try:
                        self.actor.register_slice(slice_object=slice_obj)
                    except Exception as e:
                        self.actor.get_plugin().release_slice(
                            slice_obj=slice_obj)
                        raise e

                    return None

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

            result.result = str(slice_obj.get_slice_id())

        except Exception as e:
            self.logger.error("addClientSlice: {}".format(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)

        return result
 def get_request_slice(self):
     return SliceFactory.create(slice_id=ID(), name="test-slice")
    def _test_h_request_insufficient_cores(self):
        broker = self.get_broker()
        controller = self.get_controller()
        policy = broker.get_policy()
        policy.allocation_horizon = 10

        clock = broker.get_actor_clock()

        proxy = ClientCallbackHelper(name=controller.get_name(),
                                     guid=controller.get_guid())
        broker_callback = ClientCallbackHelper(name=broker.get_name(),
                                               guid=broker.get_guid())
        ActorRegistrySingleton.get().register_callback(callback=proxy)
        ActorRegistrySingleton.get().register_callback(
            callback=broker_callback)

        slice_obj = SliceFactory.create(slice_id=ID(), name="inventory_slice")
        slice_obj.set_inventory(value=True)

        source = self.get_source_delegation(self.broker, slice_obj)
        self.broker.register_slice(slice_object=slice_obj)
        self.broker.register_delegation(delegation=source)
        self.broker.donate_delegation(delegation=source)

        cycle = 1
        broker.external_tick(cycle=cycle)
        cycle += 1

        start = clock.cycle_start_date(cycle=self.DonateStartCycle)
        end = clock.cycle_end_date(cycle=self.DonateEndCycle - 1)
        sliver = self.build_sliver_with_components()
        caphint = CapacityHints(instance_type="fabric.c64.m384.d4000")
        sliver.set_capacity_hints(caphint=caphint)

        request = self.get_reservation_for_network_node(start,
                                                        end,
                                                        sliver=sliver)
        broker.ticket(reservation=request,
                      callback=proxy,
                      caller=proxy.get_identity())

        self.assertEqual(proxy.prepared, 0)
        self.assertEqual(proxy.called, 0)

        for c in range(cycle, self.DonateEndCycle):
            broker.external_tick(cycle=c)
            while broker.get_current_cycle() != c:
                time.sleep(0.001)

            while proxy.prepared != 1:
                time.sleep(0.001)

            self.assert_failed(request)
            self.assertEqual(1, proxy.prepared)

            if proxy.called > 0:
                print("Proxy Called")
            #    self.assert_failed(proxy.get_reservation(), proxy.update_data)

        broker.await_no_pending_reservations()

        self.assertEqual(1, proxy.get_called())
        self.assertTrue(request.is_closed())
Пример #28
0
 def create_slice(self, *, slice_id: ID, name: str):
     slice_obj = SliceFactory.create(slice_id=slice_id, name=name)
     return slice_obj