blob: 208a9106cc7edf81834b5f436e55f8277722e96c [file] [log] [blame]
peustermd7cbd212017-09-07 08:55:14 +02001"""
2Copyright (c) 2017 SONATA-NFV and Paderborn University
3ALL RIGHTS RESERVED.
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17Neither the name of the SONATA-NFV, Paderborn University
18nor the names of its contributors may be used to endorse or promote
19products derived from this software without specific prior written
20permission.
21
22This work has been performed in the framework of the SONATA project,
23funded by the European Commission under Grant number 671517 through
24the Horizon 2020 and 5G-PPP programmes. The authors would like to
25acknowledge the contributions of their colleagues of the SONATA
26partner consortium (www.sonata-nfv.eu).
27"""
peusterm00199782017-05-17 08:48:12 +020028from resources.net import Net
29import threading
30
31lock = 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
41def 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
66def 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
96def 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
115def 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
139def 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