예제 #1
0
    def _parse_global_options(self, item: Element) -> (dict, dict):
        """
        Parse global options within the xml
        :param item: Global Tag
        :return: dictionary of configuration items.
                list of actions.
        """
        global_misc = {}
        global_actions = {}

        for sub_item in item.iterchildren():
            if isinstance(sub_item.tag, str):
                if sub_item.tag.lower() == "pref":
                    config_param = ConfigParam()
                    config_param.parse_config_item(sub_item)
                    global_misc[config_param.name] = config_param

                if sub_item.tag.lower() == "action":
                    action = Action("", False, dict())
                    action.parse_action(sub_item)
                    default = sub_item.get("DEFAULT")
                    if not default:
                        raise ValueError(
                            "Auction Parser Error: missing name at line {0}".
                            format(str(sub_item.sourceline)))
                    else:
                        action.default_action = ParseFormats.parse_bool(
                            default)
                    global_actions[action.name] = action

        return global_misc, global_actions
예제 #2
0
    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
예제 #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
예제 #4
0
 def parse_field_value_from_config_param(self, config_param: ConfigParam):
     """
     Parses a field value from a config param
     :param config_param: config param to parse
     :return: Nothing
     """
     self.name = config_param.name
     self.type = config_param.get_type()
     self.parse_field_value(config_param.value)
예제 #5
0
 def insert_field(self, field_def: dict, value: str, config_params: dict):
     """
     Inserts a new field in a config param dictionary
     :param field_def: field definition
     :param value: value to be assigned to the field
     :param config_params: dictinary being fill.
     """
     field = ConfigParam(name=field_def['key'],
                         p_type=field_def['type'],
                         value=value)
     config_params[field.name] = field
예제 #6
0
    def parse_action(self, node: Element):
        """
        parsers an action from a xml element.
        :param node: xml node.
        """

        action_name = node.get("NAME").lower()

        # Verifies that a name was given to the action.
        if not action_name:
            raise ValueError("Auction Parser Error: missing name at line {0}".format(str(node.sourceline)))

        self.name = action_name

        for item in node.iterchildren():
            if isinstance(item.tag, str):
                if item.tag.lower() == "pref":
                    config_param = ConfigParam()
                    config_param.parse_config_item(item)
                    self.add_config_item(config_param)
예제 #7
0
    def _parse_interval(self, node: Element, start_at_least: datetime, resource_request_key: str) -> (
    datetime, Interval):
        """
        Parses an interval
        :param node: xml node
        :param start_at_least:start datetime
        :param resource_request_key: resource requets key
        :return: start date time for the interval and interval parsed.
        """
        misc_config = {}
        for subitem in node.iterchildren():
            if isinstance(subitem.tag, str):
                if subitem.tag.lower() == "pref":
                    config_param = ConfigParam()
                    config_param.parse_config_item(subitem)
                    misc_config[config_param.name] = config_param

        interval_dict = self._convert_interal_dict(misc_config)

        new_interval = ResourceRequestInterval(resource_request_key)
        new_interval.parse_interval(interval_dict, start_at_least)
        return new_interval.stop, new_interval
예제 #8
0
    def create_config_params(self):
        config_params = {}
        config_dict = {}
        config_param = ConfigParam('domainid', DataType.UINT32,
                                   str(self.domain))
        config_dict[config_param.name] = config_param

        for config_param_name in config_dict:
            config_param = config_dict[config_param_name]
            field_value = FieldValue()
            field_value.parse_field_value_from_config_param(config_param)
            config_params[config_param_name] = field_value

        return config_params
예제 #9
0
    def add_auction_process(self, auction: Auction):
        """
        adds a Auction to auction process list

        :param auction: auction to be added
        """
        key = auction.get_key()
        action = auction.get_action()
        module_name = action.name
        module = self.module_loader.get_module(module_name)
        config_params = deepcopy(action.get_config_params())
        if 'domainid' not in config_params:
            config_params['domainid'] = ConfigParam('domainid', DataType.UINT32, str(self.domain))

        action_process = AuctionProcess(key, module, auction, config_params)
        module.init_module(action_process.get_config_params())
        self.auctions[key] = action_process
        return key
예제 #10
0
    def get_items(self, config_group: str, module: str) -> list:
        """
        Gets the configuration paramaters associated with a config group
        :param config_group  configuration group to find.
        :param module        module parameters to find.
        :return:
        """
        ret = []
        if config_group in self.get_config():
            conf_grp_map = self.get_config()[config_group]
            ret_list = conf_grp_map[module]
            for param in ret:
                ret.append(
                    ConfigParam(ret[param]['Name'], ret[param]['Type'],
                                ret[param]['Value']))
        else:
            raise ValueError(
                "configuration group {0} was not found in config file".format(
                    config_group))

        return ret
예제 #11
0
    def _parse_auction(self, node: Element, global_set: str,
                       global_misc_config: dict, global_actions: dict,
                       field_container: IpapFieldContainer):
        """
        Parses an auction node in the xml.

        :param node: Element  element to parse
        :param global_set: str global set
        :param global_misc_config: dict  dictionary with miscelaneous field configurations.
        :param global_actions: dict  dictionary with actions
        :param field_container: IpapFieldContainer container with all fields in the system.
        :return:
        """
        # get the Id property
        # deep copy global dictionaries, so we can overide them
        misc_config = deepcopy(global_misc_config)
        actions = deepcopy(global_actions)

        auction_id = node.get("ID").lower()
        (set_name, name) = self.parse_name(auction_id)
        if not set_name:
            if global_set.isnumeric():
                set_name = global_set
            else:
                set_name = str(self.domain)

        # Get Resource set and resource Id
        resurce_set = node.get("RESOURCE_SET").lower()
        resource_id = node.get("RESOURCE_ID").lower()

        templ_fields = {}

        # Iterates over children nodes
        for subitem in node.iterchildren():
            if isinstance(subitem.tag, str):
                if subitem.tag.lower() == "pref":
                    config_param = ConfigParam()
                    config_param.parse_config_item(subitem)
                    misc_config[config_param.name] = config_param

                elif subitem.tag.lower() == "field":
                    field_name, field = self._parse_field(subitem)
                    templ_fields[field_name] = field

                elif subitem.tag.lower() == "action":
                    action = Action("", False, dict())
                    action.parse_action(subitem)
                    action.default_action = True  # This is the default action,
                    actions[action.name] = action

        # get the default action
        action_default = None
        for action_name in actions:
            if actions[action.name].default_action:
                action_default = action

        auction_key = set_name + '.' + name
        resource_key = resurce_set + '.' + resource_id
        auction = Auction(auction_key, resource_key, action_default,
                          misc_config)
        templates = auction.build_templates(templ_fields)

        return auction, templates