2 Copyright (c) 2017 SONATA-NFV and Paderborn University
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
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
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).
28 from resources
.net
import Net
31 lock
= threading
.Lock()
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
41 def get_new_cidr(uuid
):
43 Calculates a unused cidr for a subnet.
45 :param uuid: The UUID of the subnet - Thus it can store which subnet gets which CIDR
47 :return: Returns None if all available CIDR are used. Otherwise returns a valid CIDR.
54 while __first_ip
<= __current_ip
< __last_ip
and __current_ip
in __issued_ips
:
55 __current_ip
+= __default_subnet_size
57 if __current_ip
>= __last_ip
or __current_ip
< __first_ip
or __current_ip
in __issued_ips
:
60 __issued_ips
[__current_ip
] = uuid
63 return Net
.int_2_ip(__current_ip
) + '/' + str(__default_subnet_bitmask
)
66 def free_cidr(cidr
, uuid
):
68 Frees a issued CIDR thus it can be reused.
70 :param cidr: The currently used CIDR.
72 :param uuid: The UUID of the Subnet, which uses this CIDR.
74 :return: Returns False if the CIDR is None or the UUID did not correspond tho the used CIDR. Else it returns True.
81 int_ip
= Net
.cidr_2_int(cidr
)
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
:
96 def is_cidr_issued(cidr
):
98 Returns True if the CIDR is used.
100 :param cidr: The requested CIDR.
102 :return: Returns True if the CIDR is used, else False.
108 int_ip
= Net
.cidr_2_int(cidr
)
110 if int_ip
in __issued_ips
:
115 def is_my_cidr(cidr
, uuid
):
117 Checks if the UUID and the used CIDR are related.
119 :param cidr: The issued CIDR.
121 :param uuid: The Subnet UUID.
123 :return: Returns False if the CIDR is None or if the CIDR is not issued. Else returns True.
129 int_ip
= Net
.cidr_2_int(cidr
)
131 if not int_ip
in __issued_ips
:
134 if __issued_ips
[int_ip
] == uuid
:
139 def assign_cidr(cidr
, uuid
):
141 Allows a subnet to request a specific CIDR.
143 :param cidr: The requested CIDR.
145 :param uuid: The Subnet UUID.
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.
153 int_ip
= Net
.cidr_2_int(cidr
)
155 if int_ip
in __issued_ips
:
160 __issued_ips
[int_ip
] = uuid