def include_non_mandatory_fields(self, mandatory_fields: list,
                                     config_params: dict,
                                     ipap_record: IpapDataRecord):
        """
        Includes non mandatory fields in record

        :param mandatory_fields: list of mandatory fields
        :param config_params: params to include. We should check that are non mandatory fields.
        :param ipap_record: record where we want to include config params
        :return:
        """
        for item in config_params.values():
            field = self.field_def_manager.get_field(item.name)

            # Checks it is not a mandatory field.
            is_mandatory: bool = False
            for mandatory_field in mandatory_fields:
                if mandatory_field.get_eno(
                ) == field['eno'] and mandatory_field.get_ftype(
                ) == field['ftype']:
                    is_mandatory = True
                    break

            if not is_mandatory:
                # check the field is a valid field for the message
                field_act = self.field_container.get_field(
                    field['eno'], field['ftype'])
                act_f_value = field_act.parse(item.value)
                ipap_record.insert_field(field['eno'], field['ftype'],
                                         act_f_value)
    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
Esempio n. 3
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
    def insert_double_field(self, field_name: str, value: float,
                            record: IpapDataRecord):
        """
        Inserts a field value (double) in the data record given as parameter

        :param field_name: field to be inserted
        :param value: value to insert
        :param record: data record where the field is going to be inserted.
        """
        field_def = self.field_def_manager.get_field(field_name)
        field: IpapField = self.field_container.get_field(
            int(field_def['eno']), int(field_def['ftype']))
        record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                            field.get_ipap_field_value_double(value))
    def insert_integer_field(self, field_name: str, value: int,
                             record: IpapDataRecord):
        """
        Inserts a field value in the data record given as parameter

        :param field_name: field to be inserted
        :param value: value to insert
        :param record: data record where the field is going to be inserted.
        """
        field_def = self.field_def_manager.get_field(field_name)
        field: IpapField = self.field_container.get_field(
            int(field_def['eno']), int(field_def['ftype']))

        if field.get_length() == 1:
            record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                                field.get_ipap_field_value_uint8(value))
        elif field.get_length() == 2:
            record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                                field.get_ipap_field_value_uint16(value))
        elif field.get_length() == 4:
            record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                                field.get_ipap_field_value_uint32(value))
        elif field.get_length() == 8:
            record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                                field.get_ipap_field_value_uint64(value))
Esempio n. 6
0
    def test_get_data_record_at_pos(self):
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        self.ipap_message.include_data(template_id, ipap_data_record)
        ipap_data_record2 = self.ipap_message.get_data_record_at_pos(0)
    def insert_ipv6_field(self, field_name: str, value: str,
                          record: IpapDataRecord):
        """
        Inserts a field value (ip address 6) in the data record given as parameter

        :param field_name: field to be inserted
        :param value: value to insert
        :param record: data record where the field is going to be inserted.
        """
        field_def = self.field_def_manager.get_field(field_name)
        field: IpapField = self.field_container.get_field(
            int(field_def['eno']), int(field_def['ftype']))
        value_encoded = value.encode('ascii')
        record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                            field.get_ipap_field_value_ipv6(value_encoded))
    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)
    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 insert_string_field(self, field_name: str, value: str,
                            record: IpapDataRecord):
        """
        Inserts a field value in the data record given as parameter

        :param field_name: field to be inserted
        :param value: value to insert
        :param record: data record where the field is going to be inserted.
        """
        field_def = self.field_def_manager.get_field(field_name)
        field: IpapField = self.field_container.get_field(
            int(field_def['eno']), int(field_def['ftype']))

        # 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)
        record.insert_field(int(field_def['eno']), int(field_def['ftype']),
                            field_val)
Esempio n. 12
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)
Esempio n. 13
0
    def test_read_data_records(self):
        ipap_message = IpapMessage(1, 1, False)
        template_id = ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        ipap_message.add_field(template_id, 0, 30)

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        ipap_message.include_data(template_id, ipap_data_record)

        lst = self.ipap_message_parser.read_data_records(
            ipap_message, template_id)
        self.assertEqual(len(lst), 1)

        lst = self.ipap_message_parser.read_data_records(ipap_message, 100)
        self.assertEqual(len(lst), 0)
Esempio n. 14
0
    def test_read_template(self):
        ipap_message = IpapMessage(1, 1, False)
        template_id = ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        ipap_message.add_field(template_id, 0, 30)

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        ipap_message.include_data(template_id, ipap_data_record)

        template = self.ipap_message_parser.read_template(
            ipap_message, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.assertEqual(template.get_type(),
                         TemplateType.IPAP_SETID_AUCTION_TEMPLATE)

        with self.assertRaises(ValueError):
            template = self.ipap_message_parser.read_template(
                ipap_message, TemplateType.IPAP_OPTNS_AUCTION_TEMPLATE)
Esempio n. 15
0
    def test_include_data(self):
        print('in test_include_data')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        template = self.ipap_message.get_template_object(template_id)
        lst = template.get_fields()

        ipap_data_record = IpapDataRecord(templ_id=template_id)
        ipap_field_value1 = IpapValueField()
        value = 12231213
        ipap_field_value1.set_value_uint64(value)

        template = self.ipap_message.get_template_object(template_id)
        lst = template.get_fields()

        # Replace the value
        ipap_data_record.insert_field(0, 30, ipap_field_value1)
        self.ipap_message.include_data(template_id, ipap_data_record)

        record_size = self.ipap_message.get_data_record_size()
        self.assertEqual(record_size, 1)
    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)
Esempio n. 17
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)
    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)
Esempio n. 19
0
 def get_data_record_at_pos(self, pos: int) -> IpapDataRecord:
     obj = lib.ipap_message_get_data_record_at_pos(self.obj, c_int(pos))
     if obj:  # not null
         return IpapDataRecord(obj=obj)
     else:
         raise ValueError("Data record at pos {0} was not found".format(str(int)))