def include_data_record(self, template: IpapTemplate, allocation: Allocation, record_id: str,
                            config_params: dict, message: IpapMessage):

        ipap_data_record = IpapDataRecord(obj=None, templ_id=template.get_template_id())

        # Insert the auction id field.
        self.insert_string_field('auctionid', allocation.get_auction_key(), ipap_data_record)

        # Insert the bidding object id field.
        self.insert_string_field('biddingobjectid', allocation.get_bid_key(), ipap_data_record)

        # Insert the allocation id field.
        self.insert_string_field('allocationid', allocation.get_key(), ipap_data_record)

        # Add the Record Id
        self.insert_string_field('recordid', record_id, ipap_data_record)

        # Add the Status
        self.insert_integer_field('status', allocation.get_state().value, ipap_data_record)

        # Add bidding_object type
        self.insert_integer_field('biddingobjecttype', allocation.get_type().value, ipap_data_record)

        # Adds non mandatory fields.
        mandatory_fields = template.get_template_type_mandatory_field(template.get_type())
        self.include_non_mandatory_fields(mandatory_fields, config_params, ipap_data_record)

        # Include the data record in the message.
        message.include_data(template.get_template_id(), ipap_data_record)
    def include_option_data_record(self, template: IpapTemplate, auction: Auction, message: IpapMessage):
        """
        Inserts templates associated with the auction

        :param template    auction template
        :param auction     auction being included in the message.
        :param message:    message being built.
        :return:
        """
        ipap_options_record = IpapDataRecord(obj=None, templ_id=template.get_template_id())

        # Add the auction Id
        self.insert_string_field('auctionid', auction.get_key(), ipap_options_record)

        # Add the Record Id
        self.insert_string_field('recordid', "Record_1", ipap_options_record)

        # Add the action
        self.insert_string_field('algoritmname', auction.action.name, ipap_options_record)

        # Adds non mandatory fields.
        mandatory_fields = template.get_template_type_mandatory_field(TemplateType.IPAP_OPTNS_AUCTION_TEMPLATE)
        config_params = auction.action.get_config_params()
        self.include_non_mandatory_fields(mandatory_fields, config_params, ipap_options_record)

        message.include_data(template.get_template_id(), ipap_options_record)
    def include_options_record(self, template: IpapTemplate,
                               bidding_object: BiddingObject, record_id: str,
                               start: datetime, stop: datetime,
                               config_params: dict, message: IpapMessage):

        ipap_record = IpapDataRecord(obj=None,
                                     templ_id=template.get_template_id())

        # Insert the auction id field.
        self.insert_string_field('auctionid', bidding_object.get_parent_key(),
                                 ipap_record)

        # Insert the bidding object id field.
        self.insert_string_field('biddingobjectid', bidding_object.get_key(),
                                 ipap_record)

        # Add the Record Id
        self.insert_string_field('recordid', record_id, ipap_record)

        # Add the start time - Unix time is seconds from 1970-1-1 .
        self.insert_datetime_field('start', start, ipap_record)

        # Add the end time.
        self.insert_datetime_field('stop', stop, ipap_record)

        # Adds non mandatory fields.
        mandatory_fields = template.get_template_type_mandatory_field(
            template.get_type())
        self.include_non_mandatory_fields(mandatory_fields, config_params,
                                          ipap_record)

        message.include_data(template.get_template_id(), ipap_record)
Exemple #4
0
    def create_bidding_object_template(self, templ_fields: dict,
                                       field_container: IpapFieldContainer,
                                       mandatory_fields: list,
                                       object_type: ObjectType,
                                       templ_type: TemplateType) -> IpapTemplate:
        """
        Create a bidding object template
        :param templ_fields:        Fields to include given by the user
        :param field_container:     Container with all possible fields defined in the ipap_message
        :param mandatory_fields:    Mandatory fields to include given by the template type
        :param object_type:         object type (i.e., bid, allocation)
        :param templ_type:          template's types (data, options)
        :return: a new template
        """
        template_source_id = TemplateIdSource()

        field_keys = self.calculate_template_fields(object_type, templ_type, templ_fields, mandatory_fields)

        # Create the bid template associated with the auction
        template = IpapTemplate()
        template.set_id(template_source_id.new_id())
        template.set_max_fields(len(field_keys))
        template.set_type(templ_type)

        for key in field_keys:
            field_key = field_keys[key]
            self.add_template_field(template, field_container, field_key.get_eno(), field_key.get_ftype())

        return template
Exemple #5
0
    def add_template_field(template: IpapTemplate, ipap_field_container: IpapFieldContainer, eno: int,
                           ftype: int):
        """
        Adds a new field to a template
        :param template:                Template where we are going to add the field
        :param ipap_field_container:    field container where we maintain the possible fields
        :param eno:                     enterprise number identifier
        :param ftype:                   id of the field for the ipap_message
        """
        # By default network encoding
        encode_network = True

        field = ipap_field_container.get_field(eno, ftype)
        size = field.get_length()
        template.add_field(size, UnknownField.KNOWN, encode_network, field)
    def read_record(self, template: IpapTemplate,
                    record: IpapDataRecord) -> dict:
        """
        Reads an auction data record
        :param template: record's template
        :param record: data record
        :return: config values
        """
        config_params = {}
        for field_pos in range(0, record.get_num_fields()):
            ipap_field_key = record.get_field_at_pos(field_pos)
            ipap_field_value = record.get_field(ipap_field_key.get_eno(),
                                                ipap_field_key.get_ftype())
            f_item = self.field_def_manager.get_field_by_code(
                ipap_field_key.get_eno(), ipap_field_key.get_ftype())

            # ipap_field_value.print_value()

            ipap_field = template.get_field(ipap_field_key.get_eno(),
                                            ipap_field_key.get_ftype())
            config_param = ConfigParam(
                name=f_item['key'],
                p_type=f_item['type'],
                value=ipap_field.write_value(ipap_field_value))
            config_params[config_param.name] = config_param
        return config_params
Exemple #7
0
 def get_template_object(self, templ_id: int) -> IpapTemplate:
     obj = lib.ipap_message_get_template_object(self.obj, c_uint16(templ_id))
     if obj:  # not null
         template = IpapTemplate(obj)
         return template
     else:
         raise ValueError("Template with id:{} was not found".format(str(templ_id)))
Exemple #8
0
    def read_misc_data(self, ipap_template: IpapTemplate, ipap_record: IpapDataRecord) -> dict:
        """
        read the data given in the data record

        :param ipap_template: templete followed by data record
        :param ipap_record: record with the data.
        :return: dictionary with config values.
        """
        config_params = {}
        num_fields = ipap_record.get_num_fields()
        for pos in range(0, num_fields):
            try:

                field_key = ipap_record.get_field_at_pos(pos)

            except ValueError as e:
                self.logger.error(str(e))
                raise e

            try:
                field_def = self.field_def_manager.get_field_by_code(field_key.get_eno(), field_key.get_ftype())
                field = ipap_template.get_field(field_key.get_eno(), field_key.get_ftype())
                config_param = ConfigParam(field_def['name'], field_def['type'],
                                           field.write_value(ipap_record.get_field(
                                               field_key.get_eno(), field_key.get_ftype())))

                config_params[config_param.name.lower()] = config_param
            except ValueError as e:
                self.logger.error("Field with eno {0} and ftype {1} was \
                                    not parametrized".format(str(field_key.get_eno()),
                                                             str(field_key.get_ftype())))
                raise e
        return config_params
Exemple #9
0
 def get_template(self, templid: int) -> IpapTemplate:
     obj = lib.ipap_template_container_get_template(self.obj,
                                                    c_uint16(templid))
     if obj:  # not null
         ipap_template = IpapTemplate(obj=obj)
         return ipap_template
     else:
         raise ValueError('Template {0} not found'.format(str(templid)))
    def include_template_fields(self, template: IpapTemplate, template_fields: dict):
        """
        Includes in the template list field in the template given as parameter.

        :param template: template from where to take the fields
        :param template_fields: dictionary of field included
        """
        ipap_fields = template.get_fields()
        for ipap_field in ipap_fields:
            field = self.field_def_manager.get_field_by_code(ipap_field.get_eno(), ipap_field.get_type())

            # Find the field in the returning map, if not found insert templates' belongings.
            if field['key'] not in template_fields:
                template_fields[field['key']] = AuctionTemplateField(field, field['lenght'])

            auction_template_field = template_fields[field['key']]
            auction_template_field.add_belonging_tuple(template.get_object_type(template.get_type()),
                                                       template.get_type())
    def verify_insert_template(template: IpapTemplate,
                               ipap_template_container: IpapTemplateContainer):
        """
        Verifies and in case of not included before inserts the template given in the template container

        :param template: template to include
        :param ipap_template_container: template container where templates should be verified.
        """
        # Insert templates in case of not created in the template container.
        if ipap_template_container.exists_template(template.get_template_id()):
            template_ret = ipap_template_container.get_template(
                template.get_template_id())
            if not template_ret.__eq__(template):
                raise ValueError(
                    "Data Template {0} given is different from the template already stored"
                    .format(template.get_template_id()))

        else:
            ipap_template_container.add_template(template)
Exemple #12
0
    def test_add_template(self):
        ipap_field = IpapField()
        ipap_field.set_field_type(0, 30, 8, 3, b"campo_1", b"campo_2",
                                  b"campo_3")

        _id = 12
        template = IpapTemplate()
        template.set_id(_id)
        template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        template.add_field(8, UnknownField.KNOWN, True, ipap_field)

        self.template_container.add_template(template)
Exemple #13
0
    def setUp(self):

        self.ipap_field_container = IpapFieldContainer()
        self.ipap_field_container.initialize_forward()
        self.ipap_field_container.initialize_reverse()

        self.template = IpapTemplate()
        _id = 2
        self.template.set_id(_id)

        self.ipap_data_record = IpapDataRecord(templ_id=_id)

        self.template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        ipap_field1 = self.ipap_field_container.get_field(0, 30)
        ipap_field2 = self.ipap_field_container.get_field(0, 32)

        self.template.add_field(ipap_field1.get_length(), UnknownField.KNOWN,
                                True, ipap_field1)
        self.template.add_field(ipap_field2.get_length(), UnknownField.KNOWN,
                                True, ipap_field2)
Exemple #14
0
    def get_template_list(self) -> str:
        """
        Gets the list of templates applicable for the auction.
        :return:list of template ids separated by comma.
        """
        first_time: bool  = True
        template = IpapTemplate()
        ret = ""

        for object_type in range(1, ObjectType.IPAP_MAX_OBJECT_TYPE.value):

            list_templates = template.get_object_template_types(ObjectType(object_type));
            for templ_type in list_templates:
                template_id = self.get_bidding_object_template( ObjectType(object_type), templ_type)
                if first_time:
                    ret = ret + "{0}".format(str(template_id))
                    first_time = False
                else:
                    ret = ret + ",{0}".format(str(template_id))

        return ret
    def get_ipap_message(self, bidding_object: BiddingObject, auction: Auction,
                         template_container: IpapTemplateContainer,
                         message: IpapMessage):

        if bidding_object.get_parent_key() != auction.get_key():
            raise ValueError(
                "the auction is not the same as the one referenced in the bidding object"
            )

        # Find both templates types for the bidding object.
        tempType = IpapTemplate.get_data_template(
            bidding_object.get_template_object_type())
        data_template_id = auction.get_bidding_object_template(
            bidding_object.get_template_object_type(), tempType)

        tempType = IpapTemplate.get_opts_template(
            bidding_object.get_template_object_type())
        option_template_id = auction.get_bidding_object_template(
            bidding_object.get_template_object_type(), tempType)

        # Insert BiddingObject's templates.
        data_template = template_container.get_template(data_template_id)
        message.make_template(data_template)

        option_template = template_container.get_template(option_template_id)
        message.make_template(option_template)

        # Include data records.
        for element_name in bidding_object.elements:
            config_params = bidding_object.elements[element_name]
            self.include_data_record(data_template, bidding_object,
                                     element_name, config_params, message)

        # Include option records.
        for option_name in bidding_object.options:
            interval = bidding_object.calculate_interval(option_name)
            config_params = bidding_object.options[option_name]
            self.include_options_record(option_template, bidding_object,
                                        option_name, interval.start,
                                        interval.stop, config_params, message)
Exemple #16
0
    def test_delete_all_templates(self):
        ipap_field = IpapField()
        ipap_field.set_field_type(0, 30, 8, 3, b"campo_1", b"campo_2",
                                  b"campo_3")

        _id = 12
        template = IpapTemplate()
        template.set_id(_id)
        template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        template.add_field(8, UnknownField.KNOWN, True, ipap_field)

        self.template_container.add_template(template)
        self.template_container.delete_all_templates()
        val = self.template_container.get_num_templates()
        self.assertEqual(val, 0)
    def include_auction_templates(self, template: IpapTemplate, auction: Auction, message: IpapMessage):
        """
        Inserts templates associated with the auction

        :param template    auction template
        :param auction     auction being included in the message.
        :param message:    message being built.
        :return:
        """
        for i in range(1, ObjectType.IPAP_MAX_OBJECT_TYPE.value):
            list_types = template.get_object_template_types(ObjectType(i))
            for templ_type in list_types:
                templ_id = auction.get_bidding_object_template(ObjectType(i), templ_type)
                message.make_template(self.ipap_template_container.get_template(templ_id))
    def insert_auction_templates(
        self, object_type: ObjectType, templates: list,
        ipap_template_container: IpapTemplateContainer
    ) -> (IpapTemplate, IpapTemplate):
        """
        Insert auction templates (data and options)
        :param object_type: object type for which we want to insert the templates
        :param templates: list of templates
        :param ipap_template_container: template container where we have to include templates.
        :return: return data and options templates
        """
        data_template = None
        opts_template = None

        # Insert record and options templates for the auction.
        for template in templates:
            if template.get_type() == IpapTemplate.get_data_template(
                    object_type):
                data_template = template
            elif template.get_type() == IpapTemplate.get_opts_template(
                    object_type):
                opts_template = template

        if data_template is None:
            raise ValueError(
                "The message is incomplete Data template was not included")

        if opts_template is None:
            raise ValueError(
                "The message is incomplete Options template was not included")

        # Verify templates
        self.verify_insert_template(data_template, ipap_template_container)
        self.verify_insert_template(opts_template, ipap_template_container)

        return data_template, opts_template
    def include_options_record(self, template: IpapTemplate, allocation: Allocation, record_id: str,
                               start: datetime, stop: datetime, message: IpapMessage):

        ipap_record = IpapDataRecord(obj=None, templ_id=template.get_template_id())

        # Insert the auction id field.
        self.insert_string_field('auctionid', allocation.get_auction_key(), ipap_record)

        # Insert the bidding object id field.
        self.insert_string_field('biddingobjectid', allocation.get_bid_key(), ipap_record)

        # Insert the allocation id field.
        self.insert_string_field('allocationid', allocation.get_key(), ipap_record)

        # Add the Record Id
        self.insert_string_field('recordid', record_id, ipap_record)

        # Add the start time - Unix time is seconds from 1970-1-1 .
        self.insert_datetime_field('start', start, ipap_record)

        # Add the end time.
        self.insert_datetime_field('stop', stop, ipap_record)

        message.include_data(template.get_template_id(), ipap_record)
    def get_ipap_message(self, allocation: Allocation, template_container: IpapTemplateContainer) -> IpapMessage:
        """

        :return:
        """
        message = IpapMessage(self.domain, IpapMessage.IPAP_VERSION, True)

        auction_manager = AuctionManager(self.domain)
        auction = auction_manager.get_auction(allocation.get_auction_key())

        # Find both templates types for the bidding object.
        temp_type = IpapTemplate.get_data_template(allocation.get_template_object_type())
        data_template_id = auction.get_bidding_object_template(allocation.get_template_object_type(), temp_type)

        temp_type = IpapTemplate.get_opts_template(allocation.get_template_object_type())
        option_template_id = auction.get_bidding_object_template(allocation.get_template_object_type(), temp_type)

        # Insert allocations's templates.a
        data_template = template_container.get_template(data_template_id)
        message.make_template(data_template)

        option_template = template_container.get_template(option_template_id)
        message.make_template(option_template)

        # Include data records.
        self.include_data_record(data_template, allocation, 'Record_1', allocation.config_params, message)

        # Include option records.
        index = 1
        for interval in allocation.intervals:
            record_id = 'Record_{0}'.format(str(index))
            self.include_options_record(option_template, allocation,
                                        record_id, interval.start, interval.stop, message)
            index = index + 1

        return message
Exemple #21
0
    def create_auction_template(self, field_container, template_type: TemplateType) -> IpapTemplate:
        """
        Create an auction template based on its mandatory fields

        :rtype: IpapTemplate
        :param field_container: container with all possible fields defined
        :param template_type:   Type of template to use.
        :return: template created.
        """
        template_source_id = TemplateIdSource()

        # Create the bid template associated with the auction
        template = IpapTemplate()
        list_fields = template.get_template_type_mandatory_field(template_type)
        template.set_id(template_source_id.new_id())
        template.set_max_fields(len(list_fields))
        template.set_type(template_type)

        for i in range(0, len(list_fields)):
            self.add_template_field(template, field_container, list_fields[i].get_eno(), list_fields[i].get_ftype())

        return template
Exemple #22
0
class IpapDataRecordTest(unittest.TestCase):
    """
    IpapDataRecordTest
    """
    def setUp(self):

        self.ipap_field_container = IpapFieldContainer()
        self.ipap_field_container.initialize_forward()
        self.ipap_field_container.initialize_reverse()

        self.template = IpapTemplate()
        _id = 2
        self.template.set_id(_id)

        self.ipap_data_record = IpapDataRecord(templ_id=_id)

        self.template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        ipap_field1 = self.ipap_field_container.get_field(0, 30)
        ipap_field2 = self.ipap_field_container.get_field(0, 32)

        self.template.add_field(ipap_field1.get_length(), UnknownField.KNOWN,
                                True, ipap_field1)
        self.template.add_field(ipap_field2.get_length(), UnknownField.KNOWN,
                                True, ipap_field2)

    # def test_get_template_id(self):
    #     value = self.ipap_data_record.get_template_id()
    #     self.assertEqual(value,2)
    #
    # def test_insert_field(self):
    #
    #     ipap_field_value1 = IpapValueField()
    #     value = 12
    #     ipap_field_value1.set_value_uint8(value)
    #
    #     ipap_field_value2 = IpapValueField()
    #     value = 13
    #     ipap_field_value2.set_value_uint8(value)
    #
    #     # Replace the value
    #     self.ipap_data_record.insert_field(0, 30, ipap_field_value1)
    #     self.ipap_data_record.insert_field(0, 30, ipap_field_value2)
    #     num_fields = self.ipap_data_record.get_num_fields()
    #     self.assertEqual(num_fields,1)
    #
    #     self.ipap_data_record.insert_field(0, 31, ipap_field_value2)
    #     num_fields = self.ipap_data_record.get_num_fields()
    #     self.assertEqual(num_fields,2)

    def test_get_field(self):

        ipap_field_value1 = IpapValueField()
        value = 12
        ipap_field_value1.set_value_uint8(value)

        # Replace the value
        self.ipap_data_record.insert_field(0, 30, ipap_field_value1)
        num_fields = self.ipap_data_record.get_num_fields()
        self.assertEqual(num_fields, 1)

        field = self.ipap_data_record.get_field(0, 30)
        self.assertEqual(field.get_value_uint8(), 12)

        field = self.ipap_field_container.get_field(0, 32)

        value = "record_1"
        # It is required to encode as ascii because the C++ wrapper requires it.
        value_encoded = value.encode('ascii')
        field_val = field.get_ipap_field_value_string(value_encoded)
        self.ipap_data_record.insert_field(0, 32, field_val)

        field = self.template.get_field(0, 32)
        output = field.write_value(self.ipap_data_record.get_field(0, 32))
        print(output)

        num_fields = self.ipap_data_record.get_num_fields()
        self.assertEqual(num_fields, 2)

        with self.assertRaises(ValueError):
            field2 = self.ipap_data_record.get_field(0, 33)
Exemple #23
0
 def setUp(self):
     self.template = IpapTemplate()
     _id = 12
     self.template.set_id(_id)
Exemple #24
0
class TemplateTest(unittest.TestCase):
    def setUp(self):
        self.template = IpapTemplate()
        _id = 12
        self.template.set_id(_id)

    def test_set_max_fields(self):
        num_fields = 10
        self.template.set_max_fields(num_fields)

    def test_set_type(self):
        self.template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)

    def test_get_template_type_mandatory_field(self):
        mandatory_fields = self.template.get_template_type_mandatory_field(
            TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        # verifies the total number
        self.assertEqual(len(mandatory_fields), 12)

        # verifies that fields returned are correct ones.
        for field in mandatory_fields:
            self.assertEqual(field.get_eno(), 0)

    def test_add_field(self):
        ipap_field = IpapField()
        ipap_field.set_field_type(0, 30, 8, 3, b"campo_1", b"campo_2",
                                  b"campo_3")
        self.template.add_field(8, UnknownField.KNOWN, True, ipap_field)

    def test_get_type(self):
        self.template.set_type(TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        template_type = self.template.get_type()
        self.assertEqual(template_type,
                         TemplateType.IPAP_SETID_AUCTION_TEMPLATE)

    def test_get_object_template_types(self):
        with self.assertRaises(ValueError):
            lst = self.template.get_object_template_types(
                ObjectType.IPAP_INVALID)

        lst = self.template.get_object_template_types(ObjectType.IPAP_AUCTION)
        self.assertEqual(len(lst), 2)

        lst = self.template.get_object_template_types(ObjectType.IPAP_BID)
        self.assertEqual(len(lst), 2)

        lst = self.template.get_object_template_types(
            ObjectType.IPAP_ALLOCATION)
        self.assertEqual(len(lst), 2)

        lst = self.template.get_object_template_types(ObjectType.IPAP_ASK)
        self.assertEqual(len(lst), 2)
    def include_auction_data_record(self, template: IpapTemplate, auction: Auction,
                                    message: IpapMessage, use_ipv6: bool, s_address: str,
                                    port: int):
        """
        Adds the option data record template associated with the option data auction template

        :param template template used for the data record.
        :param auction  auction being included in the message.
        :param message: message being built.
        :param use_ipv6: whether or not it use ipv6
        :param s_address: source address
        :param port: source port
        """
        ipap_data_record = IpapDataRecord(obj=None, templ_id=template.get_template_id())

        # Insert the auction id field.
        self.insert_string_field('auctionid', auction.get_key(), ipap_data_record)

        # Add the Record Id
        self.insert_string_field('recordid', "Record_1", ipap_data_record)

        # Add the Status
        self.insert_integer_field('status', auction.get_state().value, ipap_data_record)

        # Add the IP Version
        if use_ipv6:
            ipversion = 6
        else:
            ipversion = 4
        self.insert_integer_field('ipversion', ipversion, ipap_data_record)

        # Add the Ipv6 Address value
        if use_ipv6:
            self.insert_ipv6_field('dstipv6', s_address, ipap_data_record)
        else:
            self.insert_ipv6_field('dstipv6', "0:0:0:0:0:0:0:0", ipap_data_record)

        # Add the Ipv4 Address value
        if use_ipv6:
            self.insert_ipv4_field('dstipv4', "0.0.0.0", ipap_data_record)
        else:
            self.insert_ipv4_field('dstipv4', s_address, ipap_data_record)

        # Add destination port
        self.insert_integer_field('dstauctionport', port, ipap_data_record)

        # Add the resource Id.
        self.insert_string_field('resourceid', auction.get_resource_key(), ipap_data_record)

        # Add the start time - Unix time is seconds from 1970-1-1 .
        self.insert_datetime_field('start', auction.get_start(), ipap_data_record)

        # Add the end time.
        self.insert_datetime_field('stop', auction.get_stop(), ipap_data_record)

        # Add the interval. How much time between executions (seconds).
        u_interval = auction.get_interval().interval
        self.insert_integer_field('interval', u_interval, ipap_data_record)

        # Add the template list.
        self.insert_string_field('templatelist', auction.get_template_list(), ipap_data_record)

        message.include_data(template.get_template_id(), ipap_data_record)