070f9921210dbf8c9db15242d814ab9706ced961
1 # Copyright (c) 2015 SONATA-NFV and Paderborn University
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
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 emuvim
.api
.openstack
.resources
.net
import Net
29 lock
= threading
.Lock()
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
39 def get_new_cidr(uuid
):
41 Calculates a unused cidr for a subnet.
43 :param uuid: The UUID of the subnet - Thus it can store which subnet gets which CIDR
45 :return: Returns None if all available CIDR are used. Otherwise returns a valid CIDR.
52 while __first_ip
<= __current_ip
< __last_ip
and __current_ip
in __issued_ips
:
53 __current_ip
+= __default_subnet_size
55 if __current_ip
>= __last_ip
or __current_ip
< __first_ip
or __current_ip
in __issued_ips
:
58 __issued_ips
[__current_ip
] = uuid
61 return Net
.int_2_ip(__current_ip
) + '/' + str(__default_subnet_bitmask
)
64 def free_cidr(cidr
, uuid
):
66 Frees a issued CIDR thus it can be reused.
68 :param cidr: The currently used CIDR.
70 :param uuid: The UUID of the Subnet, which uses this CIDR.
72 :return: Returns False if the CIDR is None or the UUID did not correspond tho the used CIDR. Else it returns True.
79 int_ip
= Net
.cidr_2_int(cidr
)
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
:
94 def is_cidr_issued(cidr
):
96 Returns True if the CIDR is used.
98 :param cidr: The requested CIDR.
100 :return: Returns True if the CIDR is used, else False.
106 int_ip
= Net
.cidr_2_int(cidr
)
108 if int_ip
in __issued_ips
:
113 def is_my_cidr(cidr
, uuid
):
115 Checks if the UUID and the used CIDR are related.
117 :param cidr: The issued CIDR.
119 :param uuid: The Subnet UUID.
121 :return: Returns False if the CIDR is None or if the CIDR is not issued. Else returns True.
127 int_ip
= Net
.cidr_2_int(cidr
)
129 if int_ip
not in __issued_ips
:
132 if __issued_ips
[int_ip
] == uuid
:
137 def assign_cidr(cidr
, uuid
):
139 Allows a subnet to request a specific CIDR.
141 :param cidr: The requested CIDR.
143 :param uuid: The Subnet UUID.
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.
151 int_ip
= Net
.cidr_2_int(cidr
)
153 if int_ip
in __issued_ips
:
158 __issued_ips
[int_ip
] = uuid