@@ -127,7 +127,7 @@ def _initialize(self, raw_tunnel_ranges):
127
127
# allocation during driver initialization, instead of using the
128
128
# directory.get_plugin() method - the normal way used elsewhere to
129
129
# check if a plugin is loaded.
130
- self .sync_allocations ()
130
+ self ._sync_allocations ()
131
131
132
132
def _parse_tunnel_ranges (self , tunnel_ranges , current_range ):
133
133
for entry in tunnel_ranges :
@@ -145,17 +145,15 @@ def _parse_tunnel_ranges(self, tunnel_ranges, current_range):
145
145
{'type' : self .get_type (), 'range' : current_range })
146
146
147
147
@db_api .retry_db_errors
148
- def _populate_new_default_network_segment_ranges (self , start_time ):
149
- ctx = context .get_admin_context ()
150
- with db_api .CONTEXT_WRITER .using (ctx ):
151
- for tun_min , tun_max in self .tunnel_ranges :
152
- range_obj .NetworkSegmentRange .new_default (
153
- ctx , self .get_type (), None , tun_min , tun_max , start_time )
148
+ def _populate_new_default_network_segment_ranges (self , ctx , start_time ):
149
+ for tun_min , tun_max in self .tunnel_ranges :
150
+ range_obj .NetworkSegmentRange .new_default (
151
+ ctx , self .get_type (), None , tun_min , tun_max , start_time )
154
152
155
153
@db_api .retry_db_errors
156
- def _get_network_segment_ranges_from_db (self ):
154
+ def _get_network_segment_ranges_from_db (self , ctx = None ):
157
155
ranges = []
158
- ctx = context .get_admin_context ()
156
+ ctx = ctx or context .get_admin_context ()
159
157
with db_api .CONTEXT_READER .using (ctx ):
160
158
range_objs = (range_obj .NetworkSegmentRange .get_objects (
161
159
ctx , network_type = self .get_type ()))
@@ -164,21 +162,27 @@ def _get_network_segment_ranges_from_db(self):
164
162
165
163
return ranges
166
164
165
+ @db_api .retry_db_errors
167
166
def initialize_network_segment_range_support (self , start_time ):
168
- self ._delete_expired_default_network_segment_ranges (start_time )
169
- self ._populate_new_default_network_segment_ranges (start_time )
170
- # Override self.tunnel_ranges with the network segment range
171
- # information from DB and then do a sync_allocations since the
172
- # segment range service plugin has not yet been loaded at this
173
- # initialization time.
174
- self .tunnel_ranges = self ._get_network_segment_ranges_from_db ()
175
- self .sync_allocations ()
167
+ admin_context = context .get_admin_context ()
168
+ with db_api .CONTEXT_WRITER .using (admin_context ):
169
+ self ._delete_expired_default_network_segment_ranges (
170
+ admin_context , start_time )
171
+ self ._populate_new_default_network_segment_ranges (
172
+ admin_context , start_time )
173
+ # Override self.tunnel_ranges with the network segment range
174
+ # information from DB and then do a sync_allocations since the
175
+ # segment range service plugin has not yet been loaded at this
176
+ # initialization time.
177
+ self .tunnel_ranges = self ._get_network_segment_ranges_from_db (
178
+ ctx = admin_context )
179
+ self ._sync_allocations (ctx = admin_context )
176
180
177
181
def update_network_segment_range_allocations (self ):
178
- self .sync_allocations ()
182
+ self ._sync_allocations ()
179
183
180
184
@db_api .retry_db_errors
181
- def sync_allocations (self ):
185
+ def _sync_allocations (self , ctx = None ):
182
186
# determine current configured allocatable tunnel ids
183
187
tunnel_ids = set ()
184
188
ranges = self .get_network_segment_ranges ()
@@ -187,7 +191,7 @@ def sync_allocations(self):
187
191
188
192
tunnel_id_getter = operator .attrgetter (self .segmentation_key )
189
193
tunnel_col = getattr (self .model , self .segmentation_key )
190
- ctx = context .get_admin_context ()
194
+ ctx = ctx or context .get_admin_context ()
191
195
with db_api .CONTEXT_WRITER .using (ctx ):
192
196
# Check if the allocations are updated: if the total number of
193
197
# allocations for this tunnel type matches the allocations of the
0 commit comments