Refactoring: Made complete codebase PEP8 compatible.
[osm/vim-emu.git] / src / emuvim / api / openstack / ip_handler.py
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
2 # ALL RIGHTS RESERVED.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 #
16 # Neither the name of the SONATA-NFV, Paderborn University
17 # nor the names of its contributors may be used to endorse or promote
18 # products derived from this software without specific prior written
19 # permission.
20 #
21 # This work has been performed in the framework of the SONATA project,
22 # funded by the European Commission under Grant number 671517 through
23 # the Horizon 2020 and 5G-PPP programmes. The authors would like to
24 # acknowledge the contributions of their colleagues of the SONATA
25 # partner consortium (www.sonata-nfv.eu).
26 from resources.net import Net
27 import threading
28
29 lock = threading.Lock()
30
31 __issued_ips = dict()
32 __default_subnet_size = 256
33 __default_subnet_bitmask = 24
34 __first_ip = Net.ip_2_int('10.0.0.0')
35 __last_ip = Net.ip_2_int('10.255.255.255')
36 __current_ip = __first_ip
37
38
39 def get_new_cidr(uuid):
40 """
41 Calculates a unused cidr for a subnet.
42
43 :param uuid: The UUID of the subnet - Thus it can store which subnet gets which CIDR
44 :type uuid: ``str``
45 :return: Returns None if all available CIDR are used. Otherwise returns a valid CIDR.
46 :rtype: ``str``
47 """
48 global lock
49 lock.acquire()
50
51 global __current_ip
52 while __first_ip <= __current_ip < __last_ip and __current_ip in __issued_ips:
53 __current_ip += __default_subnet_size
54
55 if __current_ip >= __last_ip or __current_ip < __first_ip or __current_ip in __issued_ips:
56 return None
57
58 __issued_ips[__current_ip] = uuid
59 lock.release()
60
61 return Net.int_2_ip(__current_ip) + '/' + str(__default_subnet_bitmask)
62
63
64 def free_cidr(cidr, uuid):
65 """
66 Frees a issued CIDR thus it can be reused.
67
68 :param cidr: The currently used CIDR.
69 :type cidr: ``str``
70 :param uuid: The UUID of the Subnet, which uses this CIDR.
71 :type uuid: ``str``
72 :return: Returns False if the CIDR is None or the UUID did not correspond tho the used CIDR. Else it returns True.
73 :rtype: ``bool``
74 """
75 if cidr is None:
76 return False
77
78 global __current_ip
79 int_ip = Net.cidr_2_int(cidr)
80
81 global lock
82 lock.acquire()
83
84 if int_ip in __issued_ips and __issued_ips[int_ip] == uuid:
85 del __issued_ips[int_ip]
86 if int_ip < __current_ip:
87 __current_ip = int_ip
88 lock.release()
89 return True
90 lock.release()
91 return False
92
93
94 def is_cidr_issued(cidr):
95 """
96 Returns True if the CIDR is used.
97
98 :param cidr: The requested CIDR.
99 :type cidr: ``str``
100 :return: Returns True if the CIDR is used, else False.
101 :rtype: ``bool``
102 """
103 if cidr is None:
104 return False
105
106 int_ip = Net.cidr_2_int(cidr)
107
108 if int_ip in __issued_ips:
109 return True
110 return False
111
112
113 def is_my_cidr(cidr, uuid):
114 """
115 Checks if the UUID and the used CIDR are related.
116
117 :param cidr: The issued CIDR.
118 :type cidr: ``str``
119 :param uuid: The Subnet UUID.
120 :type uuid: ``str``
121 :return: Returns False if the CIDR is None or if the CIDR is not issued. Else returns True.
122 :rtype: ``bool``
123 """
124 if cidr is None:
125 return False
126
127 int_ip = Net.cidr_2_int(cidr)
128
129 if int_ip not in __issued_ips:
130 return False
131
132 if __issued_ips[int_ip] == uuid:
133 return True
134 return False
135
136
137 def assign_cidr(cidr, uuid):
138 """
139 Allows a subnet to request a specific CIDR.
140
141 :param cidr: The requested CIDR.
142 :type cidr: ``str``
143 :param uuid: The Subnet UUID.
144 :type uuid: ``str``
145 :return: Returns False if the CIDR is None or if the CIDR is already issued. Returns True if the CIDR could be
146 assigned to the UUID.
147 """
148 if cidr is None:
149 return False
150
151 int_ip = Net.cidr_2_int(cidr)
152
153 if int_ip in __issued_ips:
154 return False
155
156 global lock
157 lock.acquire()
158 __issued_ips[int_ip] = uuid
159 lock.release()
160 return True