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_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_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)
Example #4
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)
Example #5
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)
    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)
Example #7
0
class IpapMessageTest(unittest.TestCase):
    """
    IpapMessageTest
    """
    def setUp(self):
        self.ipap_message = IpapMessage(1, 1, False)
        self.ipap_message2 = IpapMessage(1, 1, True)

    def test_new_data_template(self):
        val = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.assertEqual(val, 256)

    def test_add_field(self):
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)

        # test adding an invalid field
        with self.assertRaises(ValueError):
            self.ipap_message.add_field(template_id, 0, 3000)

        # test adding an invalid template id.
        with self.assertRaises(ValueError):
            self.ipap_message.add_field(2, 0, 30)

    def test_delete_template(self):
        print('start test_delete_template')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        self.ipap_message.delete_template(template_id)

        lst = self.ipap_message.get_template_list()
        self.assertEqual(len(lst), 0)

    def test_delete_all_templates(self):
        print('start test_delete_all_templates')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        self.ipap_message.delete_all_templates()
        lst = self.ipap_message.get_template_list()
        self.assertEqual(len(lst), 0)

    def test_get_template_list(self):
        print('start test_get_template_list')
        template_id = self.ipap_message.new_data_template(
            10, TemplateType.IPAP_SETID_AUCTION_TEMPLATE)
        self.ipap_message.add_field(template_id, 0, 30)
        lst = self.ipap_message.get_template_list()
        self.assertEqual(lst[0], 256)

    def test_get_template_object(self):
        print('start test_get_template_object')
        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)
        template_type = template.get_type()
        self.assertEqual(template_type,
                         TemplateType.IPAP_SETID_AUCTION_TEMPLATE)

        lst = template.get_fields()
        print('num fields:', len(lst))

        with self.assertRaises(ValueError):
            template = self.ipap_message.get_template_object(4)

    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 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 test_import(self):
        print('In test import')
        self.ipap_message2.set_syn(True)
        self.ipap_message2.set_seqno(300)
        self.ipap_message2.output()

        str_msg = self.ipap_message2.get_message()
        ipap_message3 = IpapMessage(1, 1, True, str_msg)

        str_msg = 'aqui estoy'
        with self.assertRaises(ValueError):
            ipap_message4 = IpapMessage(1, 1, True, str_msg)
    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)