From ecd2386278632c473881f5d0b17e5cc984314819 Mon Sep 17 00:00:00 2001 From: Kent Wu Date: Wed, 4 Sep 2019 11:23:50 -0700 Subject: [PATCH] Some refactoring regarding merge aim statuses Put those common codes into a function. Change-Id: I921d1b64b7c33aa05ce541bad6f571599d1e7c82 (cherry picked from commit d9ca10d5a5c3e92337eef1c7e6e058e9ddca07b2) (cherry picked from commit 2477e719f224b3635d4401e3294799ddb0b85f48) (cherry picked from commit da2d018d67b963edf8976f54f7ce438a07baa9da) (cherry picked from commit 9e69bfeb7390f82e53f139910093fcf270c1c9d2) --- .../drivers/apic_aim/mechanism_driver.py | 91 ++++++------------- 1 file changed, 29 insertions(+), 62 deletions(-) diff --git a/gbpservice/neutron/plugins/ml2plus/drivers/apic_aim/mechanism_driver.py b/gbpservice/neutron/plugins/ml2plus/drivers/apic_aim/mechanism_driver.py index 2939ab7db..0d76aee57 100644 --- a/gbpservice/neutron/plugins/ml2plus/drivers/apic_aim/mechanism_driver.py +++ b/gbpservice/neutron/plugins/ml2plus/drivers/apic_aim/mechanism_driver.py @@ -901,6 +901,29 @@ class ApicMechanismDriver(api_plus.MechanismDriver, self.aim.delete(aim_ctx, epg) session.delete(mapping) + def _merge_aim_status_bulk(self, aim_ctx, aim_resources_aggregate, + res_dict_by_aim_res_dn): + for status in self.aim.get_statuses(aim_ctx, aim_resources_aggregate): + res_dict, aim_status_track = res_dict_by_aim_res_dn.get( + status.resource_dn, ({}, {})) + if res_dict and aim_status_track: + aim_status_track[SYNC_STATE_TMP] = self._merge_status( + aim_ctx, + aim_status_track.get(SYNC_STATE_TMP, + cisco_apic.SYNC_NOT_APPLICABLE), + None, status=status) + aim_status_track[AIM_RESOURCES_CNT] -= 1 + if (aim_status_track[AIM_RESOURCES_CNT] == 0 or + (aim_status_track[SYNC_STATE_TMP] is + cisco_apic.SYNC_ERROR)): + # if this is zero then all the AIM resources corresponding, + # to this neutron resource are processed and we can + # accurately reflect the actual sync_state. Anytime we + # encounter an error - we reflect that immediately even + # if we are not done with the AIM resources processing. + res_dict[cisco_apic.SYNC_STATE] = ( + aim_status_track[SYNC_STATE_TMP]) + def extend_network_dict_bulk(self, session, results): # Gather db objects aim_ctx = aim_context.AimContext(session) @@ -974,27 +997,8 @@ class ApicMechanismDriver(api_plus.MechanismDriver, aim_status_track[AIM_RESOURCES_CNT] = len(aim_resources) aim_resources_aggregate.extend(aim_resources) - # Merge statuses - for status in self.aim.get_statuses(aim_ctx, aim_resources_aggregate): - res_dict, aim_status_track = res_dict_by_aim_res_dn.get( - status.resource_dn, ({}, {})) - if res_dict and aim_status_track: - aim_status_track[SYNC_STATE_TMP] = self._merge_status( - aim_ctx, - aim_status_track.get(SYNC_STATE_TMP, - cisco_apic.SYNC_NOT_APPLICABLE), - None, status=status) - aim_status_track[AIM_RESOURCES_CNT] -= 1 - if (aim_status_track[AIM_RESOURCES_CNT] == 0 or - (aim_status_track[SYNC_STATE_TMP] is - cisco_apic.SYNC_ERROR)): - # if this is zero then all the AIM resources corresponding, - # to this neutron resource are processed and we can - # accurately reflect the actual sync_state. Anytime we - # encounter an error - we reflect that immediately even - # if we are not done with the AIM resources processing. - res_dict[cisco_apic.SYNC_STATE] = ( - aim_status_track[SYNC_STATE_TMP]) + self._merge_aim_status_bulk(aim_ctx, aim_resources_aggregate, + res_dict_by_aim_res_dn) def extend_network_dict(self, session, network_db, result): if result.get(api_plus.BULK_EXTENDED): @@ -1246,26 +1250,8 @@ class ApicMechanismDriver(api_plus.MechanismDriver, aim_status_track[AIM_RESOURCES_CNT] = len(aim_resources) aim_resources_aggregate.extend(aim_resources) - for status in self.aim.get_statuses(aim_ctx, aim_resources_aggregate): - res_dict, aim_status_track = res_dict_by_aim_res_dn.get( - status.resource_dn, ({}, {})) - if res_dict and aim_status_track: - aim_status_track[SYNC_STATE_TMP] = self._merge_status( - aim_ctx, - aim_status_track.get(SYNC_STATE_TMP, - cisco_apic.SYNC_NOT_APPLICABLE), - None, status=status) - aim_status_track[AIM_RESOURCES_CNT] -= 1 - if (aim_status_track[AIM_RESOURCES_CNT] == 0 or - (aim_status_track[SYNC_STATE_TMP] is - cisco_apic.SYNC_ERROR)): - # if this is zero then all the AIM resources corresponding, - # to this neutron resource are processed and we can - # accurately reflect the actual sync_state. Anytime we - # encounter an error - we reflect that immediately even - # if we are not done with the AIM resources processing. - res_dict[cisco_apic.SYNC_STATE] = ( - aim_status_track[SYNC_STATE_TMP]) + self._merge_aim_status_bulk(aim_ctx, aim_resources_aggregate, + res_dict_by_aim_res_dn) def extend_subnet_dict(self, session, subnet_db, result): if result.get(api_plus.BULK_EXTENDED): @@ -1619,27 +1605,8 @@ class ApicMechanismDriver(api_plus.MechanismDriver, aim_status_track[AIM_RESOURCES_CNT] = len(aim_resources) aim_resources_aggregate.extend(aim_resources) - # Merge statuses - for status in self.aim.get_statuses(aim_ctx, aim_resources_aggregate): - res_dict, aim_status_track = res_dict_by_aim_res_dn.get( - status.resource_dn, ({}, {})) - if res_dict and aim_status_track: - aim_status_track[SYNC_STATE_TMP] = self._merge_status( - aim_ctx, - aim_status_track.get(SYNC_STATE_TMP, - cisco_apic.SYNC_NOT_APPLICABLE), - None, status=status) - aim_status_track[AIM_RESOURCES_CNT] -= 1 - if (aim_status_track[AIM_RESOURCES_CNT] == 0 or - (aim_status_track[SYNC_STATE_TMP] is - cisco_apic.SYNC_ERROR)): - # if this is zero then all the AIM resources corresponding, - # to this neutron resource are processed and we can - # accurately reflect the actual sync_state. Anytime we - # encounter an error - we reflect that immediately even - # if we are not done with the AIM resources processing. - res_dict[cisco_apic.SYNC_STATE] = ( - aim_status_track[SYNC_STATE_TMP]) + self._merge_aim_status_bulk(aim_ctx, aim_resources_aggregate, + res_dict_by_aim_res_dn) def extend_router_dict(self, session, router_db, result): LOG.debug("APIC AIM MD extending dict for router: %s", result)