95
95
def obtain_auth_cookies(self):
96
96
"""Make a REST call to obtain cookies for ODL authenticiation."""
98
r = requests.get(self.url, auth=(self.username, self.password))
99
r = requests.get(self.url, auth=(self.username, self.password))
101
except requests.exceptions.HTTPError as e:
102
raise OpendaylightAuthError(msg=_("Failed to authenticate with "
103
"OpenDaylight: %s") % e)
104
except requests.exceptions.Timeout as e:
105
raise OpendaylightAuthError(msg=_("Authentication Timed"
100
108
jsessionid = r.cookies.get('JSESSIONID')
101
109
jsessionidsso = r.cookies.get('JSESSIONIDSSO')
102
110
if jsessionid and jsessionidsso:
199
207
urlpath = collection_name + '/' + resource['id']
200
208
self.sendjson('get', urlpath, None)
201
209
except requests.exceptions.HTTPError as e:
202
if e.response.status_code == 404:
203
attr_filter(resource, context, dbcontext)
204
to_be_synced.append(resource)
210
with excutils.save_and_reraise_exception() as ctx:
211
if e.response.status_code == requests.codes.not_found:
212
attr_filter(resource, context, dbcontext)
213
to_be_synced.append(resource)
206
216
key = resource_name if len(to_be_synced) == 1 else collection_name
208
218
# 400 errors are returned if an object exists, which we ignore.
209
self.sendjson('post', collection_name, {key: to_be_synced}, [400])
219
self.sendjson('post', collection_name, {key: to_be_synced},
220
[requests.codes.bad_request])
211
222
@utils.synchronized('odl-sync-full')
212
223
def sync_full(self, context):
213
224
"""Resync the entire database to ODL.
215
226
Transition to the in-sync state on success.
216
Note: we only allow a single thead in here at a time.
227
Note: we only allow a single thread in here at a time.
218
229
if not self.out_of_sync:
256
267
ODL_SUBNETS: filter_update_subnet_attributes,
257
268
ODL_PORTS: filter_update_port_attributes}
259
def sync_single_resource(self, operation, object_type, obj_id,
260
context, attr_filter_create, attr_filter_update):
270
def sync_single_resource(self, operation, object_type, context):
261
271
"""Sync over a single resource from Neutron to OpenDaylight.
263
273
Handle syncing a single operation over to OpenDaylight, and correctly
264
274
filter attributes out which are not required for the requisite
265
275
operation (create or update) being handled.
267
dbcontext = context._plugin_context
268
if operation == 'create':
269
urlpath = object_type
272
urlpath = object_type + '/' + obj_id
276
obj_getter = getattr(context._plugin, 'get_%s' % object_type[:-1])
277
resource = obj_getter(dbcontext, obj_id)
278
except not_found_exception_map[object_type]:
279
LOG.debug(_('%(object_type)s not found (%(obj_id)s)'),
280
{'object_type': object_type.capitalize(),
283
if operation == 'create':
284
attr_filter_create(self, resource, context, dbcontext)
285
elif operation == 'update':
286
attr_filter_update(self, resource, context, dbcontext)
278
obj_id = context.current['id']
279
if operation == 'delete':
280
self.sendjson('delete', object_type + '/' + obj_id, None)
282
if operation == 'create':
283
urlpath = object_type
285
attr_filter = self.create_object_map[object_type]
286
elif operation == 'update':
287
urlpath = object_type + '/' + obj_id
289
attr_filter = self.update_object_map[object_type]
290
resource = context.current.copy()
291
attr_filter(self, resource, context, context._plugin_context)
288
292
# 400 errors are returned if an object exists, which we ignore.
289
293
self.sendjson(method, urlpath, {object_type[:-1]: resource},
292
with excutils.save_and_reraise_exception():
293
self.out_of_sync = True
295
def sync_object(self, operation, object_type, context):
296
"""Synchronize the single modified record to ODL."""
297
obj_id = context.current['id']
299
self.sync_single_resource(operation, object_type, obj_id, context,
300
self.create_object_map[object_type],
301
self.update_object_map[object_type])
294
[requests.codes.bad_request])
296
with excutils.save_and_reraise_exception():
297
self.out_of_sync = True
303
299
def add_security_groups(self, context, dbcontext, port):
304
300
"""Populate the 'security_groups' field with entire records."""