| peusterm | 0019978 | 2017-05-17 08:48:12 +0200 | [diff] [blame] | 1 | from resources.net import Net |
| 2 | import threading |
| 3 | |
| 4 | lock = threading.Lock() |
| 5 | |
| 6 | __issued_ips = dict() |
| 7 | __default_subnet_size = 256 |
| 8 | __default_subnet_bitmask = 24 |
| 9 | __first_ip = Net.ip_2_int('10.0.0.0') |
| 10 | __last_ip = Net.ip_2_int('10.255.255.255') |
| 11 | __current_ip = __first_ip |
| 12 | |
| 13 | |
| 14 | def get_new_cidr(uuid): |
| 15 | """ |
| 16 | Calculates a unused cidr for a subnet. |
| 17 | |
| 18 | :param uuid: The UUID of the subnet - Thus it can store which subnet gets which CIDR |
| 19 | :type uuid: ``str`` |
| 20 | :return: Returns None if all available CIDR are used. Otherwise returns a valid CIDR. |
| 21 | :rtype: ``str`` |
| 22 | """ |
| 23 | global lock |
| 24 | lock.acquire() |
| 25 | |
| 26 | global __current_ip |
| 27 | while __first_ip <= __current_ip < __last_ip and __current_ip in __issued_ips: |
| 28 | __current_ip += __default_subnet_size |
| 29 | |
| 30 | if __current_ip >= __last_ip or __current_ip < __first_ip or __current_ip in __issued_ips: |
| 31 | return None |
| 32 | |
| 33 | __issued_ips[__current_ip] = uuid |
| 34 | lock.release() |
| 35 | |
| 36 | return Net.int_2_ip(__current_ip) + '/' + str(__default_subnet_bitmask) |
| 37 | |
| 38 | |
| 39 | def free_cidr(cidr, uuid): |
| 40 | """ |
| 41 | Frees a issued CIDR thus it can be reused. |
| 42 | |
| 43 | :param cidr: The currently used CIDR. |
| 44 | :type cidr: ``str`` |
| 45 | :param uuid: The UUID of the Subnet, which uses this CIDR. |
| 46 | :type uuid: ``str`` |
| 47 | :return: Returns False if the CIDR is None or the UUID did not correspond tho the used CIDR. Else it returns True. |
| 48 | :rtype: ``bool`` |
| 49 | """ |
| 50 | if cidr is None: |
| 51 | return False |
| 52 | |
| 53 | global __current_ip |
| 54 | int_ip = Net.cidr_2_int(cidr) |
| 55 | |
| 56 | global lock |
| 57 | lock.acquire() |
| 58 | |
| 59 | if int_ip in __issued_ips and __issued_ips[int_ip] == uuid: |
| 60 | del __issued_ips[int_ip] |
| 61 | if int_ip < __current_ip: |
| 62 | __current_ip = int_ip |
| 63 | lock.release() |
| 64 | return True |
| 65 | lock.release() |
| 66 | return False |
| 67 | |
| 68 | |
| 69 | def is_cidr_issued(cidr): |
| 70 | """ |
| 71 | Returns True if the CIDR is used. |
| 72 | |
| 73 | :param cidr: The requested CIDR. |
| 74 | :type cidr: ``str`` |
| 75 | :return: Returns True if the CIDR is used, else False. |
| 76 | :rtype: ``bool`` |
| 77 | """ |
| 78 | if cidr is None: |
| 79 | return False |
| 80 | |
| 81 | int_ip = Net.cidr_2_int(cidr) |
| 82 | |
| 83 | if int_ip in __issued_ips: |
| 84 | return True |
| 85 | return False |
| 86 | |
| 87 | |
| 88 | def is_my_cidr(cidr, uuid): |
| 89 | """ |
| 90 | Checks if the UUID and the used CIDR are related. |
| 91 | |
| 92 | :param cidr: The issued CIDR. |
| 93 | :type cidr: ``str`` |
| 94 | :param uuid: The Subnet UUID. |
| 95 | :type uuid: ``str`` |
| 96 | :return: Returns False if the CIDR is None or if the CIDR is not issued. Else returns True. |
| 97 | :rtype: ``bool`` |
| 98 | """ |
| 99 | if cidr is None: |
| 100 | return False |
| 101 | |
| 102 | int_ip = Net.cidr_2_int(cidr) |
| 103 | |
| 104 | if not int_ip in __issued_ips: |
| 105 | return False |
| 106 | |
| 107 | if __issued_ips[int_ip] == uuid: |
| 108 | return True |
| 109 | return False |
| 110 | |
| 111 | |
| 112 | def assign_cidr(cidr, uuid): |
| 113 | """ |
| 114 | Allows a subnet to request a specific CIDR. |
| 115 | |
| 116 | :param cidr: The requested CIDR. |
| 117 | :type cidr: ``str`` |
| 118 | :param uuid: The Subnet UUID. |
| 119 | :type uuid: ``str`` |
| 120 | :return: Returns False if the CIDR is None or if the CIDR is already issued. Returns True if the CIDR could be |
| 121 | assigned to the UUID. |
| 122 | """ |
| 123 | if cidr is None: |
| 124 | return False |
| 125 | |
| 126 | int_ip = Net.cidr_2_int(cidr) |
| 127 | |
| 128 | if int_ip in __issued_ips: |
| 129 | return False |
| 130 | |
| 131 | global lock |
| 132 | lock.acquire() |
| 133 | __issued_ips[int_ip] = uuid |
| 134 | lock.release() |
| 135 | return True |