|
| 1 | +""" |
| 2 | +Interface validator module |
| 3 | +""" |
| 4 | +import re |
| 5 | +import logging |
| 6 | +from networkapi.api_rest.exceptions import NetworkAPIException |
| 7 | + |
| 8 | +log = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class InterfaceOverrideValidator: |
| 12 | + """ |
| 13 | + Class responsible to interfaces (or ports) valitions. |
| 14 | + """ |
| 15 | + |
| 16 | + def check_overriding(self, source_interface_str_list, target_interface_str_list): |
| 17 | + """ |
| 18 | + Public method to check if source interface overides target interfaces. |
| 19 | + |
| 20 | + Allowed interfaces sample: |
| 21 | + source_interface_str_list = ['eth1/1', 'eth3/2'] |
| 22 | + target_interface_str_list = ['eth2', 'eth3/1'] |
| 23 | +
|
| 24 | + Prohibited interfaces sample: |
| 25 | + source_interface_str_list = ['eth1/1', 'eth3/2'] |
| 26 | + target_interface_str_list = ['eth1', 'eth1/1', 'eth3'] |
| 27 | +
|
| 28 | + :param source_interface_str_list str list: String array of interfaces, ex.: ['eth1', 'eth2', 'eth2/1'] |
| 29 | + :param target_interface_str_list str list: String array of interfaces, ex.: ['eth2/1/3', 'eth2/1/2/1'] |
| 30 | + :return first prohibited interface as False, otherwise, allowed interface as True: |
| 31 | + """ |
| 32 | + |
| 33 | + try: |
| 34 | + |
| 35 | + log.info('check_overriding START') |
| 36 | + log.info('source_interface_str_list: %s', source_interface_str_list) |
| 37 | + log.info('target_interface_str_list: %s', target_interface_str_list) |
| 38 | + |
| 39 | + # Validate |
| 40 | + for source_interface_str in source_interface_str_list: |
| 41 | + for target_interface_str in target_interface_str_list: |
| 42 | + |
| 43 | + log.info("Validating '%s' with '%s'", source_interface_str, source_interface_str) |
| 44 | + source_interface_array = [int(num) for num in re.findall(r'\d+', source_interface_str)] |
| 45 | + target_interface_array = [int(num) for num in re.findall(r'\d+', target_interface_str)] |
| 46 | + response = self._is_overriding( |
| 47 | + source_list=source_interface_array, |
| 48 | + target_list=target_interface_array) |
| 49 | + if not response: |
| 50 | + raise NetworkAPIException("A interface requisitada '{}' sobrepoe a interface '{}' do equipamento".format( |
| 51 | + source_interface_str, target_interface_str |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + except Exception as ex: |
| 56 | + raise NetworkAPIException(str(ex)) |
| 57 | + |
| 58 | + def _is_overriding(self, source_list, target_list, lvl=0): |
| 59 | + """ |
| 60 | + Private method check if source interface overides target interfaces. |
| 61 | + The interfaces are represented by array, ex. [1,1] is 'eth1/1'. |
| 62 | + |
| 63 | + :param source interfaces: Represented array of interfaces, ex.: [1,1] [1,2,1] [2] |
| 64 | + :param source interfaces: Represented array of interfaces, ex.: [1] [1,2,3] [4] |
| 65 | + :param recursive level control. |
| 66 | + :return first prohibited interface as False, otherwise, allowed interface as True: |
| 67 | + """ |
| 68 | + |
| 69 | + try: |
| 70 | + # Identical |
| 71 | + if source_list == target_list: |
| 72 | + log.info('*** PROHIBITED ***') |
| 73 | + return False |
| 74 | + elif not source_list or not target_list: |
| 75 | + log.info('**** PROHIBITED ****') |
| 76 | + return False |
| 77 | + elif source_list and target_list and source_list[0] == target_list[0]: |
| 78 | + return self._is_overriding( |
| 79 | + source_list=source_list[1:], |
| 80 | + target_list=target_list[1:], |
| 81 | + lvl=lvl+1 |
| 82 | + ) |
| 83 | + else: |
| 84 | + log.info('**** ALLOWED ****') |
| 85 | + return True |
| 86 | + except Exception as ex: |
| 87 | + raise NetworkAPIException(str(ex)) |
0 commit comments