Revision 5b34cc22
b/lib/network.py | ||
---|---|---|
19 | 19 |
# 02110-1301, USA. |
20 | 20 |
|
21 | 21 |
|
22 |
"""Ip address pool management functions.
|
|
22 |
"""IP address pool management functions.
|
|
23 | 23 |
|
24 | 24 |
""" |
25 | 25 |
|
... | ... | |
31 | 31 |
|
32 | 32 |
|
33 | 33 |
class AddressPool(object): |
34 |
"""Address pool class, wrapping an objects.Network object
|
|
34 |
"""Address pool class, wrapping an C{objects.Network} object.
|
|
35 | 35 |
|
36 | 36 |
This class provides methods to manipulate address pools, backed by |
37 | 37 |
L{objects.Network} objects. |
38 | 38 |
|
39 | 39 |
""" |
40 |
FREE = bitarray('0')
|
|
41 |
RESERVED = bitarray('1')
|
|
40 |
FREE = bitarray("0")
|
|
41 |
RESERVED = bitarray("1")
|
|
42 | 42 |
|
43 | 43 |
def __init__(self, network): |
44 |
"""Initialize a new IPv4 address pool from an objects.Network object
|
|
44 |
"""Initialize a new IPv4 address pool from an L{objects.Network} object.
|
|
45 | 45 |
|
46 | 46 |
@type network: L{objects.Network} |
47 | 47 |
@param network: the network object from which the pool will be generated |
... | ... | |
97 | 97 |
return int(addr) - int(self.network.network) |
98 | 98 |
|
99 | 99 |
def Update(self): |
100 |
"""Write address pools back to the network object""" |
|
100 |
"""Write address pools back to the network object. |
|
101 |
|
|
102 |
""" |
|
101 | 103 |
# pylint: disable=E1103 |
102 | 104 |
self.net.ext_reservations = self.ext_reservations.to01() |
103 | 105 |
self.net.reservations = self.reservations.to01() |
... | ... | |
115 | 117 |
|
116 | 118 |
@property |
117 | 119 |
def all_reservations(self): |
118 |
"""Return a combined map of internal + external reservations.""" |
|
120 |
"""Return a combined map of internal and external reservations. |
|
121 |
|
|
122 |
""" |
|
119 | 123 |
return (self.reservations | self.ext_reservations) |
120 | 124 |
|
121 | 125 |
def Validate(self): |
... | ... | |
135 | 139 |
return True |
136 | 140 |
|
137 | 141 |
def IsFull(self): |
138 |
"""Check whether the network is full""" |
|
142 |
"""Check whether the network is full. |
|
143 |
|
|
144 |
""" |
|
139 | 145 |
return self.all_reservations.all() |
140 | 146 |
|
141 | 147 |
def GetReservedCount(self): |
142 |
"""Get the count of reserved addresses""" |
|
148 |
"""Get the count of reserved addresses. |
|
149 |
|
|
150 |
""" |
|
143 | 151 |
return self.all_reservations.count(True) |
144 | 152 |
|
145 | 153 |
def GetFreeCount(self): |
146 |
"""Get the count of unused addresses""" |
|
154 |
"""Get the count of unused addresses. |
|
155 |
|
|
156 |
""" |
|
147 | 157 |
return self.all_reservations.count(False) |
148 | 158 |
|
149 | 159 |
def GetMap(self): |
150 |
"""Return a textual representation of the network's occupation status.""" |
|
160 |
"""Return a textual representation of the network's occupation status. |
|
161 |
|
|
162 |
""" |
|
151 | 163 |
return self.all_reservations.to01().replace("1", "X").replace("0", ".") |
152 | 164 |
|
153 | 165 |
def IsReserved(self, address): |
154 |
"""Checks if the given IP is reserved""" |
|
166 |
"""Checks if the given IP is reserved. |
|
167 |
|
|
168 |
""" |
|
155 | 169 |
idx = self._GetAddrIndex(address) |
156 | 170 |
return self.all_reservations[idx] |
157 | 171 |
|
158 | 172 |
def Reserve(self, address, external=False): |
159 |
"""Mark an address as used.""" |
|
173 |
"""Mark an address as used. |
|
174 |
|
|
175 |
""" |
|
160 | 176 |
if self.IsReserved(address): |
161 | 177 |
raise errors.AddressPoolError("%s is already reserved" % address) |
162 | 178 |
self._Mark(address, external=external) |
163 | 179 |
|
164 | 180 |
def Release(self, address, external=False): |
165 |
"""Release a given address reservation.""" |
|
181 |
"""Release a given address reservation. |
|
182 |
|
|
183 |
""" |
|
166 | 184 |
self._Mark(address, value=False, external=external) |
167 | 185 |
|
168 | 186 |
def GetFreeAddress(self): |
169 |
"""Returns the first available address.""" |
|
187 |
"""Returns the first available address. |
|
188 |
|
|
189 |
""" |
|
170 | 190 |
if self.IsFull(): |
171 | 191 |
raise errors.AddressPoolError("%s is full" % self.network) |
172 | 192 |
|
... | ... | |
176 | 196 |
return address |
177 | 197 |
|
178 | 198 |
def GenerateFree(self): |
179 |
"""Returns the first free address of the network if any or |
|
180 |
raises an error if it is full. |
|
199 |
"""Returns the first free address of the network. |
|
200 |
|
|
201 |
@raise errors.AddressPoolError: Pool is full |
|
181 | 202 |
|
182 | 203 |
""" |
183 | 204 |
if self.IsFull(): |
... | ... | |
186 | 207 |
return str(self.network[idx]) |
187 | 208 |
|
188 | 209 |
def GetExternalReservations(self): |
189 |
"""Returns a list of all externally reserved addresses""" |
|
210 |
"""Returns a list of all externally reserved addresses. |
|
211 |
|
|
212 |
""" |
|
190 | 213 |
# pylint: disable=E1103 |
191 | 214 |
idxs = self.ext_reservations.search(self.RESERVED) |
192 | 215 |
return [str(self.network[idx]) for idx in idxs] |
193 | 216 |
|
194 | 217 |
@classmethod |
195 | 218 |
def InitializeNetwork(cls, net): |
196 |
"""Initialize an L{objects.Network} object |
|
219 |
"""Initialize an L{objects.Network} object.
|
|
197 | 220 |
|
198 |
Reserve the network, broadcast and gateway IPs
|
|
221 |
Reserve the network, broadcast and gateway IP addresses.
|
|
199 | 222 |
|
200 | 223 |
""" |
201 | 224 |
obj = cls(net) |
Also available in: Unified diff