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