def validate_set_state_action(action, definitions_index):

    properties = action[TemplateFields.PROPERTIES]

    if TemplateFields.STATE not in properties:
        LOG.error('%s status code: %s' % (status_msgs[128], 128))
        return get_fault_result(RESULT_DESCRIPTION, 128)

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[129], 129))
        return get_fault_result(RESULT_DESCRIPTION, 129)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
Example #2
0
def _validate_set_state_action(action, definitions_index):

    properties = action[TemplateFields.PROPERTIES]

    if TemplateFields.STATE not in properties:
        LOG.error('%s status code: %s' % (status_msgs[128], 128))
        return get_fault_result(RESULT_DESCRIPTION, 128)

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[129], 129))
        return get_fault_result(RESULT_DESCRIPTION, 129)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
Example #3
0
def validate_template_id(definitions_index, id_to_check):
    if id_to_check not in definitions_index:
        msg = status_msgs[3] + ' template id: %s' % id_to_check
        LOG.error('%s status code: %s' % (msg, 3))
        return get_fault_result(RESULT_DESCRIPTION, 3, msg)

    return get_correct_result(RESULT_DESCRIPTION)
Example #4
0
def _validate_scenario_condition(condition, definitions_index):
    try:
        dnf_result = TemplateData.ScenarioData.convert_to_dnf_format(condition)
    except Exception:
        LOG.error('%s status code: %s' % (status_msgs[85], 85))
        return get_fault_result(RESULT_DESCRIPTION, 85)

    # not condition validation
    not_condition_result = \
        _validate_not_condition(dnf_result, definitions_index)
    if not not_condition_result.is_valid_config:
        return not_condition_result

    # template id validation
    values_to_replace = ' and ', ' or ', ' not ', 'not ', '(', ')'
    condition = reduce(lambda cond, v: cond.replace(v, ' '), values_to_replace,
                       condition)

    for condition_var in condition.split(' '):

        if len(condition_var.strip()) == 0:
            continue

        result = _validate_template_id(definitions_index, condition_var)
        if not result.is_valid_config:
            return result

    return get_correct_result(RESULT_DESCRIPTION)
def syntax_validation(template_conf):
    if template_conf.get(EXCEPTION):
        result = get_fault_result(RESULT_DESCRIPTION,
                                  5,
                                  msg=status_msgs[5] +
                                  template_conf.get(EXCEPTION))
    else:
        result = _validate_template_sections(template_conf)

        if result.is_valid_config:
            metadata = template_conf[TemplateFields.METADATA]
            result = _validate_metadata_section(metadata)

        if result.is_valid_config and TemplateFields.INCLUDES in template_conf:
            includes = template_conf[TemplateFields.INCLUDES]
            result = _validate_includes_section(includes)

        if result.is_valid_config and \
           TemplateFields.DEFINITIONS in template_conf:
            definitions = template_conf[TemplateFields.DEFINITIONS]
            has_includes = TemplateFields.INCLUDES in template_conf
            result = _validate_definitions_section(definitions, has_includes)

        if result.is_valid_config:
            scenarios = template_conf[TemplateFields.SCENARIOS]
            result = _validate_scenarios_section(scenarios)

    return result
Example #6
0
def _validate_not_condition(dnf_result, definitions_index):
    """Not operator validation

    Not operator can appear only on edges.

    :param dnf_result:
    :param definitions_index:
    :return:
    """

    if isinstance(dnf_result, Not):
        for arg in dnf_result.args:
            if isinstance(arg, Symbol):
                definition = definitions_index.get(str(arg), None)
                if not (definition
                        and definition.get(EProps.RELATIONSHIP_TYPE)):
                    msg = status_msgs[86] + ' template id: %s' % arg
                    LOG.error('%s status code: %s' % (msg, 86))
                    return get_fault_result(RESULT_DESCRIPTION, 86, msg)
            else:
                res = _validate_not_condition(arg, definitions_index)
                if not res.is_valid_config:
                    return res
        return get_correct_result(RESULT_DESCRIPTION)

    for arg in dnf_result.args:
        if not isinstance(arg, Symbol):
            res = _validate_not_condition(arg, definitions_index)
            if not res.is_valid_config:
                return res

    return get_correct_result(RESULT_DESCRIPTION)
Example #7
0
def _validate_entity_definition(entity_dict, entities_index):

    template_id = entity_dict[TemplateFields.TEMPLATE_ID]
    if template_id in entities_index:
        LOG.error('%s status code: %s' % (status_msgs[2], 2))
        return get_fault_result(RESULT_DESCRIPTION, 2)

    return get_correct_result(RESULT_DESCRIPTION)
def validate_entity_definition(entity_dict, entities_index):

    template_id = entity_dict[TemplateFields.TEMPLATE_ID]
    if template_id in entities_index:
        LOG.error('%s status code: %s' % (status_msgs[2], 2))
        return get_fault_result(RESULT_DESCRIPTION, 2)

    return get_correct_result(RESULT_DESCRIPTION)
def _validate_template_id(definitions_index, id_to_check):

    if id_to_check not in definitions_index:
        msg = status_msgs[3] + ' template id: %s' % id_to_check
        LOG.error('%s status code: %s' % (msg, 3))
        return get_fault_result(RESULT_DESCRIPTION, 3, msg)

    return get_correct_result(RESULT_DESCRIPTION)
def validate_mark_down_action(action, definitions_index):

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[131], 131))
        return get_fault_result(RESULT_DESCRIPTION, 131)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
Example #11
0
def _validate_mark_down_action(action, definitions_index):

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[131], 131))
        return get_fault_result(RESULT_DESCRIPTION, 131)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
def validate_raise_alarm_action(action, definitions_index):

    properties = action[TemplateFields.PROPERTIES]

    if TemplateFields.ALARM_NAME not in properties:
        LOG.error('%s status code: %s' % (status_msgs[125], 125))
        return get_fault_result(RESULT_DESCRIPTION, 125)

    if TemplateFields.SEVERITY not in properties:
        LOG.error('%s status code: %s' % (status_msgs[126], 126))
        return get_fault_result(RESULT_DESCRIPTION, 126)

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[127], 127))
        return get_fault_result(RESULT_DESCRIPTION, 127)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
def _validate_dict_schema(schema, value):
    try:
        schema(value)
    except Error as e:
        status_code = get_status_code(e)
        msg = status_msgs[status_code] + str(e)
        LOG.error('%s status code: %s' % (msg, status_code))
        return get_fault_result(RESULT_DESCRIPTION, status_code, msg)

    return get_correct_result(RESULT_DESCRIPTION)
Example #14
0
def _validate_raise_alarm_action(action, definitions_index):

    properties = action[TemplateFields.PROPERTIES]

    if TemplateFields.ALARM_NAME not in properties:
        LOG.error('%s status code: %s' % (status_msgs[125], 125))
        return get_fault_result(RESULT_DESCRIPTION, 125)

    if TemplateFields.SEVERITY not in properties:
        LOG.error('%s status code: %s' % (status_msgs[126], 126))
        return get_fault_result(RESULT_DESCRIPTION, 126)

    action_target = action[TemplateFields.ACTION_TARGET]
    if TemplateFields.TARGET not in action_target:
        LOG.error('%s status code: %s' % (status_msgs[127], 127))
        return get_fault_result(RESULT_DESCRIPTION, 127)

    target = action_target[TemplateFields.TARGET]
    return _validate_template_id(definitions_index, target)
Example #15
0
def _validate_entity_category(entity_to_check, category):

    if TemplateFields.CATEGORY not in entity_to_check \
            or entity_to_check[TemplateFields.CATEGORY] != category:
        msg = status_msgs[132] + ' expect %s to be %s' \
                                 % (entity_to_check, category)
        LOG.error('%s status code: %s' % (msg, 132))
        return get_fault_result(RESULT_DESCRIPTION, 132, msg)

    return get_correct_result(RESULT_DESCRIPTION)
def _validate_entity_category(entity_to_check, category):

    if TemplateFields.CATEGORY not in entity_to_check \
            or entity_to_check[TemplateFields.CATEGORY] != category:
        msg = status_msgs[132] + ' expect %s to be %s' \
                                 % (entity_to_check, category)
        LOG.error('%s status code: %s' % (msg, 132))
        return get_fault_result(RESULT_DESCRIPTION, 132, msg)

    return get_correct_result(RESULT_DESCRIPTION)
def _validate_includes_section(includes):
    any_str = Any(str, six.text_type)
    if not includes:
        LOG.error('%s status code: %s' % (status_msgs[140], 140))
        return get_fault_result(RESULT_DESCRIPTION, 140)

    for name in includes:
        schema = Schema({Required(TemplateFields.NAME, msg=141): any_str})
        result = _validate_name_schema(schema, name)
        if not result.is_valid_config:
            return result

    return result
Example #18
0
def _validate_relationship(relationship, relationships_index, entities_index):

    template_id = relationship[TemplateFields.TEMPLATE_ID]
    if template_id in relationships_index or template_id in entities_index:
        LOG.error('%s status code: %s' % (status_msgs[2], 2))
        return get_fault_result(RESULT_DESCRIPTION, 2)

    target = relationship[TemplateFields.TARGET]
    result = _validate_template_id(entities_index, target)

    if result.is_valid_config:
        source = relationship[TemplateFields.SOURCE]
        result = _validate_template_id(entities_index, source)

    return result
def validate_relationship(relationship, relationships_index, entities_index):

    template_id = relationship[TemplateFields.TEMPLATE_ID]
    if template_id in relationships_index or template_id in entities_index:
        LOG.error('%s status code: %s' % (status_msgs[2], 2))
        return get_fault_result(RESULT_DESCRIPTION, 2)

    target = relationship[TemplateFields.TARGET]
    result = _validate_template_id(entities_index, target)

    if result.is_valid_config:
        source = relationship[TemplateFields.SOURCE]
        result = _validate_template_id(entities_index, source)

    return result
Example #20
0
def _validate_scenario_action(action, definitions_index):

    action_type = action[TemplateFields.ACTION_TYPE]
    actions = {
        ActionType.RAISE_ALARM: _validate_raise_alarm_action,
        ActionType.SET_STATE: _validate_set_state_action,
        ActionType.ADD_CAUSAL_RELATIONSHIP:
        _validate_add_causal_relationship_action,
        ActionType.MARK_DOWN: _validate_mark_down_action,
    }

    if action_type not in actions.keys():
        LOG.error('%s status code: %s' % (status_msgs[120], 120))
        return get_fault_result(RESULT_DESCRIPTION, 120)

    return actions[action_type](action, definitions_index)
def validate_scenario_action(action, definitions_index):

    action_type = action[TemplateFields.ACTION_TYPE]
    actions = {
        ActionType.RAISE_ALARM: validate_raise_alarm_action,
        ActionType.SET_STATE: validate_set_state_action,
        ActionType.ADD_CAUSAL_RELATIONSHIP:
        validate_add_causal_relationship_action,
        ActionType.MARK_DOWN: validate_mark_down_action,
    }

    if action_type not in actions.keys():
        LOG.error('%s status code: %s' % (status_msgs[120], 120))
        return get_fault_result(RESULT_DESCRIPTION, 120)

    return actions[action_type](action, definitions_index)
def def_template_syntax_validation(def_template_conf):
    result = _validate_def_template_template_sections(def_template_conf)

    if TemplateFields.INCLUDES in def_template_conf or \
       TemplateFields.SCENARIOS in def_template_conf:
        LOG.error('%s status code: %s' % (status_msgs[143], 143))
        return get_fault_result(RESULT_DESCRIPTION, 143)

    if result.is_valid_config:
        metadata = def_template_conf[TemplateFields.METADATA]
        result = _validate_metadata_section(metadata)

    if result.is_valid_config:
        definitions = def_template_conf[TemplateFields.DEFINITIONS]
        result = _validate_definitions_section(definitions, False)

    return result
def _validate_name_schema(schema, name):
    try:
        schema(name)
    except Error as e:

        status_code = get_status_code(e)
        if status_code:
            msg = status_msgs[status_code]
        else:
            # General syntax error
            status_code = 4
            msg = status_msgs[4] + str(e)

        LOG.error('%s status code: %s' % (msg, status_code))
        return get_fault_result(RESULT_DESCRIPTION, status_code, msg)

    return get_correct_result(RESULT_DESCRIPTION)
def _validate_dict_schema(schema, value):

    try:
        schema(value)
    except Error as e:

        status_code = _get_status_code(e)
        if status_code:
            msg = status_msgs[status_code]
        else:
            # General syntax error
            status_code = 4
            msg = status_msgs[4] + str(e)

        LOG.error('%s status code: %s' % (msg, status_code))
        return get_fault_result(RESULT_DESCRIPTION, status_code, msg)

    return get_correct_result(RESULT_DESCRIPTION)
def _validate_actions_schema(actions):
    if not actions:
        LOG.error('%s status code: %s' % (status_msgs[121], 121))
        return get_fault_result(RESULT_DESCRIPTION, 121)

    for action in actions:

        schema = Schema({
            Required(TemplateFields.ACTION, msg=122): dict,
        })
        result = _validate_dict_schema(schema, action)

        if result.is_valid_config:
            result = _validate_action_schema(action[TemplateFields.ACTION])

        if not result.is_valid_config:
            return result

    return result
def _validate_scenarios_section(scenarios):
    if not scenarios:
        LOG.error('%s status code: %s' % (status_msgs[81], 81))
        return get_fault_result(RESULT_DESCRIPTION, 81)

    for scenario in scenarios:

        schema = Schema({
            Required(TemplateFields.SCENARIO, msg=82): dict,
        })
        result = _validate_dict_schema(schema, scenario)

        if result.is_valid_config:
            result = _validate_scenario(scenario[TemplateFields.SCENARIO])

            if not result.is_valid_config:
                return result

    return result
def _validate_entities(entities, has_includes):
    if not entities and not has_includes:
        LOG.error('%s status code: %s' % (status_msgs[43], 43))
        return get_fault_result(RESULT_DESCRIPTION, 43)

    for entity in entities:

        schema = Schema({
            Required(TemplateFields.ENTITY, msg=46): dict,
        })
        result = _validate_dict_schema(schema, entity)

        if result.is_valid_config:
            result = _validate_entity_dict(entity[TemplateFields.ENTITY])

        if not result.is_valid_config:
            return result

    return result
def validate_entities(entities):

    if not entities:
        LOG.error('%s status code: %s' % (status_msgs[43], 43))
        return get_fault_result(RESULT_DESCRIPTION, 43)

    for entity in entities:

        schema = Schema({
            Required(TemplateFields.ENTITY, msg=46): dict,
        })
        result = _validate_dict_schema(schema, entity)

        if result.is_valid_config:
            result = validate_entity_dict(entity[TemplateFields.ENTITY])

        if not result.is_valid_config:
            return result

    return result
def validate_actions_schema(actions):

    if not actions:
        LOG.error('%s status code: %s' % (status_msgs[121], 121))
        return get_fault_result(RESULT_DESCRIPTION, 121)

    for action in actions:

        schema = Schema({
            Required(TemplateFields.ACTION, msg=122): dict,
        })
        result = _validate_dict_schema(schema, action)

        if result.is_valid_config:
            result = validate_action_schema(action[TemplateFields.ACTION])

        if not result.is_valid_config:
            return result

    return result
def validate_scenarios_section(scenarios):

    if not scenarios:
        LOG.error('%s status code: %s' % (status_msgs[81], 81))
        return get_fault_result(RESULT_DESCRIPTION, 81)

    for scenario in scenarios:

        schema = Schema({
            Required(TemplateFields.SCENARIO, msg=82): dict,
        })
        result = _validate_dict_schema(schema, scenario)

        if result.is_valid_config:
            result = validate_scenario(scenario[TemplateFields.SCENARIO])

            if not result.is_valid_config:
                return result

    return result
Example #31
0
def _validate_add_causal_relationship_action(action, definitions_index):

    action_target = action[TemplateFields.ACTION_TARGET]

    for key in [TemplateFields.TARGET, TemplateFields.SOURCE]:
        if key not in action_target:
            LOG.error('%s status code: %s' % (status_msgs[130], 130))
            return get_fault_result(RESULT_DESCRIPTION, 130)

        template_id = action_target[key]
        result = _validate_template_id(definitions_index, template_id)

        if not result.is_valid_config:
            return result

        entity = definitions_index[template_id]
        result = _validate_entity_category(entity, EntityCategory.ALARM)
        if not result.is_valid_config:
            return result

    return get_correct_result(RESULT_DESCRIPTION)
def validate_add_causal_relationship_action(action, definitions_index):

    action_target = action[TemplateFields.ACTION_TARGET]

    for key in [TemplateFields.TARGET, TemplateFields.SOURCE]:
        if key not in action_target:
            LOG.error('%s status code: %s' % (status_msgs[130], 130))
            return get_fault_result(RESULT_DESCRIPTION, 130)

        template_id = action_target[key]
        result = _validate_template_id(definitions_index, template_id)

        if not result.is_valid_config:
            return result

        entity = definitions_index[template_id]
        result = _validate_entity_category(entity, EntityCategory.ALARM)
        if not result.is_valid_config:
            return result

    return get_correct_result(RESULT_DESCRIPTION)
def validate_scenario_condition(condition, definitions_index):

    try:
        TemplateData.convert_to_dnf_format(condition)
    except Exception:
        LOG.error('%s status code: %s' % (status_msgs[85], 85))
        return get_fault_result(RESULT_DESCRIPTION, 85)

    values_to_replace = ' and ', ' or ', ' not ', '(', ')'
    condition = reduce(lambda cond, v: cond.replace(v, ' '),
                       values_to_replace,
                       condition)

    for condition_var in condition.split(' '):

        if len(condition_var.strip()) == 0:
            continue

        result = _validate_template_id(definitions_index, condition_var)
        if not result.is_valid_config:
            return result

    return get_correct_result(RESULT_DESCRIPTION)
Example #34
0
def get_content_fault_result(code, msg=None):
    return get_fault_result(RESULT_DESCRIPTION, code, msg)