Debian Codegraph @

networking_bagpipe/bagpipe_bgp/vpn/identifier_allocators.py

raw
1 # Copyright 2018 Orange
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import itertools
16 import threading
17
18 from oslo_log import log as logging
19
20 from networking_bagpipe._i18n import _
21 from networking_bagpipe.bagpipe_bgp.common import looking_glass as lg
22 from networking_bagpipe.bagpipe_bgp.common import utils
23 from networking_bagpipe.bagpipe_bgp.engine import exa
24
25 from neutron_lib import exceptions
26
27 LOG = logging.getLogger(__name__)
28
29
30 class MaxIDReached(exceptions.NeutronException):
31 message = "Could not allocate identifier, maximum (%(max)d) was reached"
32
33
34 class IDAllocator(lg.LookingGlassMixin):
35
36 MIN = 0
37 MAX = 2 ** 32 - 1 # no id > MAX will be allocated
38
39 def __init__(self):
40 self.allocated_ids = dict()
41 self.released_ids = list()
42 self.current_id = self.MIN
43
44 self.lock = threading.Lock()
45
46 def _allocate(self, id, description, update_current=False):
47 self.allocated_ids[id] = description
48
49 # Update current_id to the next free id
50 if update_current and id == self.current_id:
51 for next_id in itertools.count(self.current_id + 1):
52 if next_id not in self.allocated_ids:
53 self.current_id = next_id
54 break
55
56 LOG.debug("Allocated id %d for '%s'", id, description)
57 return id
58
59 @utils.synchronized
60 def get_new_id(self, description=None, hint_value=None):
61
62 if hint_value is not None and hint_value > self.MAX:
63 LOG.warning("Allocator hint value cannot be beyond MAX")
64
65 if hint_value is not None and hint_value not in self.allocated_ids:
66 return self._allocate(hint_value, description, update_current=True)
67 elif self.current_id > self.MAX:
68 if len(self.released_ids) > 0:
69 # FIFO (pop the id that was released the earliest)
70 return self._allocate(self.released_ids.pop(0), description)
71 else:
72 raise MaxIDReached(max=self.MAX)
73 else:
74 return self._allocate(self.current_id, description,
75 update_current=True)
76
77 @utils.synchronized
78 def release(self, id):
79 if id in self.allocated_ids:
80 LOG.debug("Released id %d ('%s')", id, self.allocated_ids[id])
81 del self.allocated_ids[id]
82 self.released_ids.append(id)
83 else:
84 raise Exception(_("Asked to release a non-allocated id: %d" % id))
85
86 def get_lg_local_info(self, prefix):
87 return self.allocated_ids
88
89
90 class RDAllocator(IDAllocator):
91
92 MAX = 2 ** 16 - 1
93
94 def __init__(self, prefix):
95 super().__init__()
96 self.prefix = prefix
97
98 def get_new_rd(self, description):
99 new_id = self.get_new_id(description)
100
101 return exa.RouteDistinguisher.fromElements(self.prefix, new_id)
102
103 def release(self, rd):
104 super().release(int(str(rd).split(':')[1]))
105
106
107 class LabelAllocator(IDAllocator):
108
109 MIN = 16
110 MAX = 2 ** 20 - 1
111
112 def get_new_label(self, description):
113 return self.get_new_id(description)