diff options
author | Lukasz Muszkieta <lukasz.muszkieta@nokia.com> | 2018-01-08 12:40:46 +0100 |
---|---|---|
committer | Lukasz Muszkieta <lukasz.muszkieta@nokia.com> | 2018-01-29 12:58:05 +0100 |
commit | 8d977df8ba8a63e8c76f58825854fd53f8e638ba (patch) | |
tree | 7a1e606d524ff5ea67715f4b1ef00b94545e9e61 /adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java | |
parent | 1672a4db05c0e313ce258efe26a75f995c162bb5 (diff) |
refactoring - add Optional
change possible null return value to Optional
Change-Id: I70bf78a51b901a74932b00d1d8735bef5eeacef7
Issue-ID: SO-360
Signed-off-by: Lukasz Muszkieta <lukasz.muszkieta@nokia.com>
Diffstat (limited to 'adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java')
-rw-r--r-- | adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java | 106 |
1 files changed, 12 insertions, 94 deletions
diff --git a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java index ee89840c50..be36d67e0b 100644 --- a/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java +++ b/adapters/mso-adapter-utils/src/main/java/org/openecomp/mso/openstack/utils/MsoKeystoneUtils.java @@ -26,6 +26,7 @@ import java.util.Calendar; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import org.openecomp.mso.cloud.CloudIdentity; import org.openecomp.mso.cloud.CloudSite; import org.openecomp.mso.logger.MsoAlarmLogger; @@ -58,7 +59,7 @@ public class MsoKeystoneUtils extends MsoTenantUtils { // token will be used until it expires. // // The cache key is "cloudId" - private static Map <String, KeystoneCacheEntry> adminClientCache = new HashMap <String, KeystoneCacheEntry> (); + private static Map <String, KeystoneCacheEntry> adminClientCache = new HashMap<>(); private static MsoLogger LOGGER = MsoLogger.getMsoLogger (MsoLogger.Catalog.RA); String msoPropID; @@ -92,13 +93,12 @@ public class MsoKeystoneUtils extends MsoTenantUtils { Map <String, String> metadata, boolean backout) throws MsoException { // Obtain the cloud site information where we will create the tenant - CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId); - if (cloudSite == null) { + Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId); + if (!cloudSiteOpt.isPresent()) { LOGGER.error(MessageEnum.RA_CREATE_TENANT_ERR, "MSOCloudSite not found", "", "", MsoLogger.ErrorCode.DataError, "MSOCloudSite not found"); throw new MsoCloudSiteNotFound (cloudSiteId); } - Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite); - + Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSiteOpt.get()); Tenant tenant = null; try { // Check if the tenant already exists @@ -129,7 +129,7 @@ public class MsoKeystoneUtils extends MsoTenantUtils { // Add MSO User to the tenant as a member and // apply tenant metadata if supported by the cloud site try { - CloudIdentity cloudIdentity = cloudSite.getIdentityService (); + CloudIdentity cloudIdentity = cloudSiteOpt.get().getIdentityService (); User msoUser = findUserByNameOrId (keystoneAdminClient, cloudIdentity.getMsoId ()); Role memberRole = findRoleByNameOrId (keystoneAdminClient, cloudIdentity.getMemberRole ()); @@ -197,10 +197,8 @@ public class MsoKeystoneUtils extends MsoTenantUtils { */ public MsoTenant queryTenant (String tenantId, String cloudSiteId) throws MsoException { // Obtain the cloud site information where we will query the tenant - CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId); - if (cloudSite == null) { - throw new MsoCloudSiteNotFound (cloudSiteId); - } + CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow( + () -> new MsoCloudSiteNotFound(cloudSiteId)); Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite); @@ -247,10 +245,8 @@ public class MsoKeystoneUtils extends MsoTenantUtils { */ public MsoTenant queryTenantByName (String tenantName, String cloudSiteId) throws MsoException { // Obtain the cloud site information where we will query the tenant - CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId); - if (cloudSite == null) { - throw new MsoCloudSiteNotFound (cloudSiteId); - } + CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow( + () -> new MsoCloudSiteNotFound(cloudSiteId)); Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite); try { @@ -294,10 +290,8 @@ public class MsoKeystoneUtils extends MsoTenantUtils { */ public boolean deleteTenant (String tenantId, String cloudSiteId) throws MsoException { // Obtain the cloud site information where we will query the tenant - CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId); - if (cloudSite == null) { - throw new MsoCloudSiteNotFound (cloudSiteId); - } + CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow( + () -> new MsoCloudSiteNotFound(cloudSiteId)); Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite); try { @@ -326,59 +320,6 @@ public class MsoKeystoneUtils extends MsoTenantUtils { return true; } - /** - * Delete the specified Tenant (by Name) in the given cloud. This method returns true or - * false, depending on whether the tenant existed and was successfully deleted, or if - * the tenant already did not exist. Both cases are treated as success (no Exceptions). - * <p> - * Note for the AIC Cloud (DCP/LCP): all admin requests go to the centralized identity - * service in DCP. So deleting a tenant from one cloudSiteId will remove it from all - * sites managed by that identity service. - * <p> - * - * @param tenantName The name of the tenant to delete - * @param cloudSiteId The cloud identifier from which to delete the tenant. - * @return true if the tenant was deleted, false if the tenant did not exist. - * @throws MsoOpenstackException If the Openstack API call returns an exception. - */ - public boolean deleteTenantByName (String tenantName, String cloudSiteId) throws MsoException { - // Obtain the cloud site information where we will query the tenant - CloudSite cloudSite = cloudConfig.getCloudSite (cloudSiteId); - if (cloudSite == null) { - throw new MsoCloudSiteNotFound (cloudSiteId); - } - Keystone keystoneAdminClient = getKeystoneAdminClient (cloudSite); - - try { - // Need the Tenant ID to delete (can't directly delete by name) - Tenant tenant = findTenantByName (keystoneAdminClient, tenantName); - if (tenant == null) { - // OK if tenant already doesn't exist. - LOGGER.error(MessageEnum.RA_TENANT_NOT_FOUND, tenantName, cloudSiteId, "", "", MsoLogger.ErrorCode.DataError, "Tenant not found"); - return false; - } - - // Execute the Delete. It has no return value. - OpenStackRequest <Void> request = keystoneAdminClient.tenants ().delete (tenant.getId ()); - executeAndRecordOpenstackRequest (request, msoProps); - - LOGGER.debug ("Deleted Tenant " + tenant.getId () + " (" + tenant.getName () + ")"); - - // Clear any cached clients. Not really needed, ID will not be reused. - MsoHeatUtils.expireHeatClient (tenant.getId (), cloudSiteId); - MsoNeutronUtils.expireNeutronClient (tenant.getId (), cloudSiteId); - } catch (OpenStackBaseException e) { - // Note: It doesn't seem to matter if tenant doesn't exist, no exception is thrown. - // Convert Keystone OpenStackResponseException to MsoOpenstackException - throw keystoneErrorToMsoException (e, "DeleteTenant"); - } catch (RuntimeException e) { - // Catch-all - throw runtimeExceptionToMsoException (e, "DeleteTenant"); - } - - return true; - } - // ------------------------------------------------------------------- // PRIVATE UTILITY FUNCTIONS FOR USE WITHIN THIS CLASS @@ -463,29 +404,6 @@ public class MsoKeystoneUtils extends MsoTenantUtils { } /* - * Find a tenant (or query its existance) by its Name or Id. Check first against the - * ID. If that fails, then try by name. - * - * @param adminClient an authenticated Keystone object - * - * @param tenantName the tenant name or ID to query - * - * @return a Tenant object or null if not found - */ - public Tenant findTenantByNameOrId (Keystone adminClient, String tenantNameOrId) { - if (tenantNameOrId == null) { - return null; - } - - Tenant tenant = findTenantById (adminClient, tenantNameOrId); - if (tenant == null) { - tenant = findTenantByName (adminClient, tenantNameOrId); - } - - return tenant; - } - - /* * Find a tenant (or query its existance) by its Id. * * @param adminClient an authenticated Keystone object |