def __split_route_policies(self):
        LOGGER.debug(
            'Starting method __split_route_policies in class {class_obj}'.
            format(class_obj=type(self)))
        route_policy_begin_regex = re.compile(r'^route-policy', re.IGNORECASE)
        route_policy_end_regex = re.compile(r'^end-policy', re.IGNORECASE)
        bang_regex = re.compile(r'^!$')
        route_policy_found = False
        route_policy_name = None

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if route_policy_begin_regex.match(match_line):
                route_policy_line_split = match_line.split()
                route_policy_name = route_policy_line_split[1]
                if route_policy_found:
                    if not self.route_maps.get(route_policy_name):
                        self.route_maps.update({route_policy_name: list()})
                    self.route_maps[route_policy_name].append(line)
                else:
                    route_policy_found = True
                    self.route_maps.update({route_policy_name: list()})
                    self.route_maps[route_policy_name].append(line)

            elif bang_regex.match(match_line):
                route_policy_found = False

            elif route_policy_end_regex.match(
                    match_line) and route_policy_found:
                self.route_maps[route_policy_name].append(line)
                route_policy_found = False

            elif route_policy_found:
                self.route_maps[route_policy_name].append(line)
    def __split_extended_acls(self):
        LOGGER.debug(
            'Starting method __split_extended_acls in class {class_obj}'.
            format(class_obj=type(self)))
        extended_acl_regex = re.compile(r'^ip access-list extended',
                                        re.IGNORECASE)
        permit_regex = re.compile(r'^permit')
        deny_regex = re.compile(r'^deny')
        bang_regex = re.compile(r'^!$')
        acl_name = None
        acl_found = False

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if extended_acl_regex.match(match_line):
                acl_line_split = match_line.split()
                acl_name = acl_line_split[3]
                if acl_found:
                    if not self.extended_acls.get(acl_name):
                        self.extended_acls.update({acl_name: list()})
                    self.extended_acls[acl_name].append(line)
                else:
                    acl_found = True
                    self.extended_acls.update({acl_name: list()})
                    self.extended_acls[acl_name].append(line)

            elif bang_regex.match(match_line):
                acl_found = False

            elif permit_regex.match(match_line) and acl_found:
                self.extended_acls[acl_name].append(line)

            elif deny_regex.match(match_line) and acl_found:
                self.extended_acls[acl_name].append(line)
    def __split_standard_community_lists(self):
        LOGGER.debug(
            'Starting method __split_standard_community_lists in class '
            '{class_obj}'.format(class_obj=type(self)))
        standard_community_list_regex = re.compile(
            r'^ip community-list standard', re.IGNORECASE)
        bang_regex = re.compile(r'^!$')
        community_list_found = False

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if standard_community_list_regex.match(match_line):
                community_list_line_split = match_line.split()
                community_name = community_list_line_split[3]
                if community_list_found:
                    if not self.standard_community_lists.get(community_name):
                        self.standard_community_lists.update(
                            {community_name: list()})
                    self.standard_community_lists[community_name].append(line)
                else:
                    community_list_found = True
                    self.standard_community_lists.update(
                        {community_name: list()})
                    self.standard_community_lists[community_name].append(line)

            elif bang_regex.match(match_line):
                community_list_found = False
    def __split_prefix_sets(self):
        LOGGER.debug(
            'Starting method __split_prefix_sets in class {class_obj}'.format(
                class_obj=type(self)))
        prefix_set_begin_regex = re.compile(r'^prefix-set', re.IGNORECASE)
        prefix_set_end_regex = re.compile(r'^end-set', re.IGNORECASE)
        bang_regex = re.compile(r'^!$')
        prefix_set_found = False
        prefix_list_name = None

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if prefix_set_begin_regex.match(match_line):
                prefix_list_line_split = match_line.split()
                prefix_list_name = prefix_list_line_split[1]
                if prefix_set_found:
                    if not self.prefix_lists.get(prefix_list_name):
                        self.prefix_lists.update({prefix_list_name: list()})
                    self.prefix_lists[prefix_list_name].append(line)
                else:
                    prefix_set_found = True
                    self.prefix_lists.update({prefix_list_name: list()})
                    self.prefix_lists[prefix_list_name].append(line)

            elif bang_regex.match(match_line):
                prefix_set_found = False

            elif prefix_set_end_regex.match(match_line) and prefix_set_found:
                self.prefix_lists[prefix_list_name].append(line)
                prefix_set_found = False

            elif prefix_set_found:
                self.prefix_lists[prefix_list_name].append(line)
 def __get_hostname(self):
     LOGGER.debug(
         'Starting method __get_hostname in class {class_obj}'.format(
             class_obj=type(self)))
     hostname_regex = re.compile(r'^hostname', re.IGNORECASE)
     for line in self.orig_config:
         match_line = pdt.remove_extra_spaces(line)
         if hostname_regex.match(match_line):
             hostname_split = match_line.split()
             self.hostname = hostname_split[1]
    def __split_interfaces(self):
        LOGGER.debug(
            'Starting method __split_interfaces in class {class_obj}'.format(
                class_obj=type(self)))
        interface_regex = re.compile(r'^interface', re.IGNORECASE)
        bang_regex = re.compile(r'^!', re.IGNORECASE)
        interface_found = False

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if interface_regex.match(match_line):
                interface_found = True
                self.interfaces.append('!')
                self.interfaces.append(line)

            elif bang_regex.match(match_line):
                interface_found = False

            elif interface_found:
                self.interfaces.append(line)
    def __split_route_maps(self):
        LOGGER.debug(
            'Starting method __split_route_maps in class {class_obj}'.format(
                class_obj=type(self)))
        route_map_regex = re.compile(
            r'^route-map ([A-Z]|_|[0-9]|-)+ (permit|deny)', re.IGNORECASE)
        description_regex = re.compile(r'^description', re.IGNORECASE)
        match_regex = re.compile(r'^match', re.IGNORECASE)
        set_regex = re.compile(r'^set', re.IGNORECASE)
        bang_regex = re.compile(r'^!$')
        route_map_name = None
        route_map_found = False

        for line in self.orig_config:
            match_line = pdt.remove_extra_spaces(line)
            if route_map_regex.match(match_line):
                route_map_line_split = match_line.split()
                route_map_name = route_map_line_split[1]
                if route_map_found:
                    if not self.route_maps.get(route_map_name):
                        self.route_maps.update({route_map_name: list()})
                    self.route_maps[route_map_name].append(line)
                else:
                    route_map_found = True
                    if not self.route_maps.get(route_map_name):
                        self.route_maps.update({route_map_name: list()})
                    self.route_maps[route_map_name].append(line)

            elif bang_regex.match(match_line):
                route_map_found = False

            elif description_regex.match(match_line) and route_map_found:
                self.route_maps[route_map_name].append(line)

            elif match_regex.match(match_line) and route_map_found:
                self.route_maps[route_map_name].append(line)

            elif set_regex.match(match_line) and route_map_found:
                self.route_maps[route_map_name].append(line)
Example #8
0
 def __init__(self,
              device_type,
              rp_ip_addr,
              acl_or_rmap_name,
              description=None):
     try:
         self.device_type_dict[device_type]
     except:
         LOGGER.critical('Invalid device type chosen %s in %s', device_type,
                         type(self))
         raise mod.util.BadDeviceType(
             'You have chosen an invalid device type, the valid types are '
             '{types}'.format(types=self.device_type_dict))
     self.device_type = device_type
     self.__check_unicast_address(rp_ip_addr)
     self.rp_ip_addr = rp_ip_addr
     acl_or_rmap_name = pdt.remove_spaces_add_hyphen(acl_or_rmap_name)
     self.acl_or_rmap_name = acl_or_rmap_name
     if description:
         description = pdt.remove_extra_spaces(description)
     self.description = description
     self.networks = list()
Example #9
0
    def __split_acl(self):
        """
        Method to split the ACL and check it
        :return:
            None

        """
        LOGGER.debug(
            'Starting method __split_acl in class {class_type}'.format(
                class_type=type(self)))
        new_rm_dict_key = 0
        mcast_ip = None
        mask = None

        for line in self.orig_config:
            line_match = pdt.remove_extra_spaces(line)
            line_split = ' '.join(line_match.split(','))
            line_split = line_split.split()

            if ipv4.mcast_ip(line_split[2], return_tuple=False):

                if ipv4.mcast_ip(line_split[2], return_tuple=False):
                    mcast_ip = line_split[2]

                try:
                    for cidr in mask_conversion:
                        if line_split[5] == mask_conversion[cidr]['INVMASK']:
                            mask = str(cidr)
                            break

                except IndexError as e:
                    LOGGER.info(
                        'Method __split_acl_matches_only in class {class_type} '
                        'error {e}'.format(class_type=type(self), e=e))
                    mask = '32'

                self.new_rm_dict[new_rm_dict_key] = '{mcast_ip}/{mask}'.format(
                    mcast_ip=mcast_ip, mask=mask)
                new_rm_dict_key += 1
Example #10
0
    def modify_mcast_base_data(self,
                               rp_ip_addr=None,
                               acl_or_rmap_name=None,
                               description=None):
        """
        Method to modify multicast base data
        Args:
            rp_ip_addr: The RP IP Address
            acl_or_rmap_name: The ACL or Route-Map name
            description: A descripton

        Returns:

        """
        LOGGER.debug('Method modify_mcast_base_data in %s', type(self))
        if rp_ip_addr:
            self.__check_unicast_address(rp_ip_addr)
            self.rp_ip_addr = rp_ip_addr
        elif acl_or_rmap_name:
            acl_or_rmap_name = pdt.remove_spaces_add_hyphen(acl_or_rmap_name)
            self.acl_or_rmap_name = acl_or_rmap_name
        elif description:
            description = pdt.remove_extra_spaces(description)
            self.description = description
    def __ask_random_question(self):
        """
        Method to ask the questions randomly
        :return:
            None
        """
        LOGGER.debug(
            'Starting Method __ask_random_question in Class: {}'.format(
                type(self)))
        while len(self.unanswered_questions) > 0:
            mnu.clear_screen()
            correct = False
            random_question = choice(self.unanswered_questions)

            if random_question.get('answer_opts'):
                temp_q_menu = mnu.make_menu_dict_from_list(
                    random_question.get('answer_opts'))
                answer_opt = int(
                    mnu.menu(temp_q_menu,
                             random_question.get('question'),
                             no_quit=True))
                if temp_q_menu.get(answer_opt).get(
                        'MENU') == random_question.get('answer'):
                    if random_question.get('feedback'):
                        print(
                            random_question.get('feedback').get(
                                'correct_answer'))

                    else:
                        print('CORRECT!!')

                    correct = True

                else:
                    if random_question.get('feedback'):
                        print(
                            random_question.get('feedback').get(
                                'incorrect_answer'))

                    else:
                        print('SORRY THAT IS NOT CORRECT!!')

            else:
                answer = input('{question}: '.format(
                    question=random_question.get('question')))
                answer = pdt.remove_extra_spaces(answer)
                if isinstance(random_question.get('answer'), list):
                    for correct_answer in random_question.get('answer'):
                        if answer.lower() == correct_answer.lower():
                            if random_question.get('feedback'):
                                print(
                                    random_question.get('feedback').get(
                                        'correct_answer'))

                            else:
                                print('CORRECT!!')

                            correct = True
                            break

                    if not correct:
                        if random_question.get('feedback'):
                            print(
                                random_question.get('feedback').get(
                                    'incorrect_answer'))

                        else:
                            print('SORRY THAT IS NOT CORRECT!!')

                else:
                    if answer.lower() == random_question.get('answer').lower():
                        if random_question.get('feedback'):
                            print(
                                random_question.get('feedback').get(
                                    'correct_answer'))

                        else:
                            print('CORRECT!!')

                        correct = True

                    else:
                        if random_question.get('feedback'):
                            print(
                                random_question.get('feedback').get(
                                    'incorrect_answer'))

                        else:
                            print('SORRY THAT IS NOT CORRECT!!')

            for index, answered_q in enumerate(self.unanswered_questions):
                if random_question.get('question') == answered_q.get(
                        'question'):
                    random_question['correct'] = correct
                    self.answered_questions.append(random_question)
                    self.unanswered_questions.pop(index)
                    break

            input('PRESS <ENTER> TO CONTINUE')
Example #12
0
def convert_route_map_to_our_format(directories=None,
                                    input_file_name=None,
                                    output_file_name=None,
                                    display_only=False,
                                    reset_sequences=False):
    """
    Function to convert a Route-Map to a YML format for QuickConfigTemplates
    :param directories:
    :param input_file_name: The input file name
    :param output_file_name: The output file name
    :param display_only: Boolean true = don't output to file
    :param reset_sequences: Set True to recount sequences
    :return:
        None

    """
    temp_list = list()
    rmap_obj = None

    try:
        route_maps = pdt.file_to_list(input_file_name,
                                      directories.get_yml_dir(input_file_name))
        route_maps = clean_list(route_maps)

        if len(route_maps) == 0:
            error = 'No data found in file {}'.format(
                os.path.join(directories.get_yml_dir(input_file_name),
                             input_file_name))
            LOGGER.critical(error)
            sys.exit(error)

    except FileNotFoundError as e:
        error = '{error}'.format(error=e)
        LOGGER.critical(error)
        sys.exit(error)

    for rm_line in route_maps:
        rm_line_split = pdt.remove_extra_spaces(rm_line).split()

        if rm_line_split[0] == 'route-map':
            try:
                if not rmap_obj:
                    rmap_obj = RouteMapData(rm_line_split[1], reset_sequences)
                    rmap_obj.set_sequence_info(rm_line_split[3],
                                               rm_line_split[2])

                elif rmap_obj:
                    rmap_obj.set_new_sequence()
                    rmap_obj.set_sequence_info(rm_line_split[3],
                                               rm_line_split[2])

            except IndexError:
                error = 'Cannot find Route-Map name in this statement "{}"'.format(
                    rm_line)
                LOGGER.error(error)
                print(error)
                error = 'Your data in file {} does not all seem to be a ' \
                        'Route-Map'.format(os.path.join(directories.get_yml_dir(input_file_name), input_file_name))
                LOGGER.critical(error)
                sys.exit(error)

        elif rm_line_split[0] == 'description':
            rmap_obj.set_description(rm_line)

        elif rm_line_split[0] == 'match':
            rmap_obj.set_matches(rm_line)

        elif rm_line_split[0] == 'set':
            rmap_obj.set_sets(rm_line)

    if not rmap_obj:
        error = 'Your data in file {} does not all seem to be a ' \
                'Route-Map'.format(os.path.join(directories.get_yml_dir(input_file_name), input_file_name))
        LOGGER.critical(error)
        sys.exit(error)

    else:
        rmap_obj.set_new_sequence()

    temp_list.append(
        '--- # Created from file: {} with rm_create'.format(input_file_name))
    temp_list.append('common:')
    temp_list.append('    template: <replace>')
    temp_list.append('    devices:')
    temp_list.append('    -   device:')
    temp_list.append('        -   devicename: <replace>')
    temp_list.append('            management_ip: <replace>')
    temp_list.append('            route_maps:')
    temp_list.append('            -   route_map_name: {}'.format(
        rmap_obj.get_name()))
    temp_list.append('                sequences:')
    for line in rmap_obj.get_sequences():
        temp_list.append('                -   sequence: {}'.format(
            line.get('sequence')))
        if line.get('description'):
            temp_list.append('                    description: {}'.format(
                line.get('description')))
        temp_list.append('                    permit_deny: {}'.format(
            line.get('permit_deny')))
        if line.get('match'):
            temp_list.append('                    match:')
            for enum, match_line in enumerate(line.get('match')):
                temp_list.append(
                    '                    -   match_item: {}'.format(
                        match_line.get('match_item')))
                temp_list.append('                        match_item_name: '
                                 '{}'.format(
                                     match_line.get('match_item_name')))

        if line.get('set'):
            temp_list.append('                    set:')
            for enum, set_line in enumerate(line.get('set')):
                temp_list.append('                    -   set_item: {}'.format(
                    set_line.get('set_item')))
                temp_list.append(
                    '                        set_item_to: {}'.format(
                        set_line.get('set_item_to')))

    if not display_only:
        file_name = pdt.file_name_increase(output_file_name,
                                           directories.get_output_dir())
        pdt.list_to_file(temp_list, file_name, directories.get_output_dir())
        output_notify = 'Filename: {} output to directory {}'.format(
            file_name, directories.get_output_dir())
        print(output_notify)
        LOGGER.debug(output_notify)

    for final_yml in temp_list:
        print(final_yml)