diff options
Diffstat (limited to 'catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java')
-rw-r--r-- | catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java | 187 |
1 files changed, 76 insertions, 111 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java index 0729ee28c6..0d43082dc4 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/ecomp/converters/AssetMetadataConverter.java @@ -7,9 +7,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,10 +17,14 @@ * limitations under the License. * ============LICENSE_END========================================================= */ - package org.openecomp.sdc.be.ecomp.converters; import fj.data.Either; +import java.util.Collection; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -46,31 +50,24 @@ import org.openecomp.sdc.common.log.wrappers.Logger; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; -import java.util.Collection; -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - @org.springframework.stereotype.Component("asset-metadata-utils") public class AssetMetadataConverter { - private static final Logger log = Logger.getLogger(DistributionCatalogServlet.class); - - @Autowired - private ComponentsUtils componentsUtils; + private static final Logger log = Logger.getLogger(DistributionCatalogServlet.class); @Autowired protected ToscaOperationFacade toscaOperationFacade; + @Autowired + private ComponentsUtils componentsUtils; /* * Relative asset’s URL. Should be used in REST GET API to download the asset’s CSAR. https://{serverBaseURL}/{csarPath} can be obtained from (HttpServletRequest)request.getServerName() */ - public Either<List<? extends AssetMetadata>, ResponseFormat> convertToAssetMetadata(List<? extends Component> componentList, String serverBaseURL, boolean detailed) { + public Either<List<? extends AssetMetadata>, ResponseFormat> convertToAssetMetadata(List<? extends Component> componentList, String serverBaseURL, + boolean detailed) { if (componentList == null || componentList.isEmpty()) { return Either.left(new LinkedList<>()); } List<AssetMetadata> retResList = new LinkedList<>(); - for (Component curr : componentList) { Either<? extends AssetMetadata, ResponseFormat> resMetaData = convertToSingleAssetMetadata(curr, serverBaseURL, detailed); if (resMetaData.isRight()) { @@ -78,40 +75,30 @@ public class AssetMetadataConverter { } retResList.add(resMetaData.left().value()); } - return Either.left(retResList); - } - public <T extends Component> Either<? extends AssetMetadata, ResponseFormat> convertToSingleAssetMetadata(T component, String serverBaseURL, boolean detailed) { + public <T extends Component> Either<? extends AssetMetadata, ResponseFormat> convertToSingleAssetMetadata(T component, String serverBaseURL, + boolean detailed) { ComponentTypeEnum componentType = component.getComponentType(); Either<? extends AssetMetadata, ResponseFormat> resMetaData = convertToMetadata(componentType, serverBaseURL, detailed, component); - if (resMetaData.isRight()) { return Either.right(resMetaData.right().value()); - } - else{ + } else { return Either.left(resMetaData.left().value()); } - } - private Either<? extends AssetMetadata, ResponseFormat> convertToMetadata(ComponentTypeEnum componentType, String serverBaseURL, boolean detailed, Component curr) { - + private Either<? extends AssetMetadata, ResponseFormat> convertToMetadata(ComponentTypeEnum componentType, String serverBaseURL, boolean detailed, + Component curr) { switch (componentType) { - - case RESOURCE: - - return generateResourceMeatdata(serverBaseURL, detailed, curr); - - case SERVICE: - - return generateServiceMetadata(serverBaseURL, detailed, curr); - - default: - - ResponseFormat responseFormat = componentsUtils.getResponseFormatAdditionalProperty(ActionStatus.COMPONENT_INVALID_CATEGORY); - return Either.right(responseFormat); + case RESOURCE: + return generateResourceMeatdata(serverBaseURL, detailed, curr); + case SERVICE: + return generateServiceMetadata(serverBaseURL, detailed, curr); + default: + ResponseFormat responseFormat = componentsUtils.getResponseFormatAdditionalProperty(ActionStatus.COMPONENT_INVALID_CATEGORY); + return Either.right(responseFormat); } } @@ -119,56 +106,53 @@ public class AssetMetadataConverter { AssetMetadata metaData; metaData = createMetadaObject(detailed, curr.getComponentType()); metaData = convertToResourceMetadata((ResourceAssetMetadata) metaData, (Resource) curr, serverBaseURL, detailed); - if (detailed) { - Either<ResourceAssetDetailedMetadata, StorageOperationStatus> converted = convertToResourceDetailedMetadata((ResourceAssetDetailedMetadata) metaData, (Resource) curr, serverBaseURL); + Either<ResourceAssetDetailedMetadata, StorageOperationStatus> converted = convertToResourceDetailedMetadata( + (ResourceAssetDetailedMetadata) metaData, (Resource) curr, serverBaseURL); if (converted.isRight()) { ActionStatus storageResponse = componentsUtils.convertFromStorageResponse(converted.right().value(), ComponentTypeEnum.RESOURCE); ResponseFormat responseFormat = componentsUtils.getResponseFormat(storageResponse); return Either.right(responseFormat); } } - return Either.left(metaData); } private AssetMetadata createMetadaObject(boolean detailed, ComponentTypeEnum type) { AssetMetadata metaData = null; switch (type) { - case SERVICE: - if (!detailed) { - metaData = new ServiceAssetMetadata(); - } else { - metaData = new ServiceAssetDetailedMetadata(); - } - break; - case RESOURCE: - if (!detailed) { - metaData = new ResourceAssetMetadata(); - } else { - metaData = new ResourceAssetDetailedMetadata(); - } - break; - default: - break; + case SERVICE: + if (!detailed) { + metaData = new ServiceAssetMetadata(); + } else { + metaData = new ServiceAssetDetailedMetadata(); + } + break; + case RESOURCE: + if (!detailed) { + metaData = new ResourceAssetMetadata(); + } else { + metaData = new ResourceAssetDetailedMetadata(); + } + break; + default: + break; } return metaData; } private Either<? extends AssetMetadata, ResponseFormat> generateServiceMetadata(String serverBaseURL, boolean detailed, Component curr) { AssetMetadata metaData = createMetadaObject(detailed, curr.getComponentType()); - metaData = convertToServiceAssetMetadata((ServiceAssetMetadata) metaData, (Service) curr, serverBaseURL, detailed); - if (detailed) { - Either<ServiceAssetDetailedMetadata, StorageOperationStatus> converted = convertToServiceDetailedMetadata((ServiceAssetDetailedMetadata) metaData, (Service) curr); + Either<ServiceAssetDetailedMetadata, StorageOperationStatus> converted = convertToServiceDetailedMetadata( + (ServiceAssetDetailedMetadata) metaData, (Service) curr); if (converted.isRight()) { ActionStatus storageResponse = componentsUtils.convertFromStorageResponse(converted.right().value(), ComponentTypeEnum.RESOURCE); ResponseFormat responseFormat = componentsUtils.getResponseFormat(storageResponse); return Either.right(responseFormat); } } - return Either.left(metaData); } @@ -183,13 +167,13 @@ public class AssetMetadataConverter { String toscaModelUrl = (new String(serverBaseURL)).replace("metadata", "toscaModel"); asset.setToscaModelURL(toscaModelUrl); } - return asset; } - private <T extends ResourceAssetMetadata> T convertToResourceMetadata(T assetToPopulate, Resource resource, String serverBaseURL, boolean detailed) { + private <T extends ResourceAssetMetadata> T convertToResourceMetadata(T assetToPopulate, Resource resource, String serverBaseURL, + boolean detailed) { assetToPopulate = convertToAsset(assetToPopulate, resource, serverBaseURL, detailed); - if(resource.getCategories() != null && !resource.getCategories().isEmpty()){ + if (resource.getCategories() != null && !resource.getCategories().isEmpty()) { CategoryDefinition categoryDefinition = resource.getCategories().get(0); assetToPopulate.setCategory(categoryDefinition.getName()); assetToPopulate.setSubCategory(categoryDefinition.getSubcategories().get(0).getName()); @@ -197,94 +181,81 @@ public class AssetMetadataConverter { assetToPopulate.setResourceType(resource.getResourceType().name()); assetToPopulate.setLifecycleState(resource.getLifecycleState().name()); assetToPopulate.setLastUpdaterUserId(resource.getLastUpdaterUserId()); - return (T) assetToPopulate; } - private <T extends ServiceAssetMetadata> T convertToServiceAssetMetadata(T assetToPopulate, Service service, String serverBaseURL, boolean detailed) { + private <T extends ServiceAssetMetadata> T convertToServiceAssetMetadata(T assetToPopulate, Service service, String serverBaseURL, + boolean detailed) { assetToPopulate = convertToAsset(assetToPopulate, service, serverBaseURL, detailed); - - if(service.getCategories() != null && !service.getCategories().isEmpty()){ + if (service.getCategories() != null && !service.getCategories().isEmpty()) { CategoryDefinition categoryDefinition = service.getCategories().get(0); assetToPopulate.setCategory(categoryDefinition.getName()); } - assetToPopulate.setLifecycleState(service.getLifecycleState().name()); assetToPopulate.setLastUpdaterUserId(service.getLastUpdaterUserId()); assetToPopulate.setDistributionStatus(service.getDistributionStatus().name()); - return (T) assetToPopulate; } - private <T extends ResourceAssetDetailedMetadata> Either<T, StorageOperationStatus> convertToResourceDetailedMetadata(T assetToPopulate, Resource resource, String serverBaseURL) { - + private <T extends ResourceAssetDetailedMetadata> Either<T, StorageOperationStatus> convertToResourceDetailedMetadata(T assetToPopulate, + Resource resource, + String serverBaseURL) { List<ComponentInstance> componentInstances = resource.getComponentInstances(); - if (componentInstances != null) { - Either<List<ResourceInstanceMetadata>, StorageOperationStatus> resourceInstanceMetadata = convertToResourceInstanceMetadata(componentInstances, ComponentTypeEnum.RESOURCE_PARAM_NAME, resource.getUUID()); + Either<List<ResourceInstanceMetadata>, StorageOperationStatus> resourceInstanceMetadata = convertToResourceInstanceMetadata( + componentInstances, ComponentTypeEnum.RESOURCE_PARAM_NAME, resource.getUUID()); if (resourceInstanceMetadata.isRight()) { return Either.right(resourceInstanceMetadata.right().value()); } - assetToPopulate.setResources(resourceInstanceMetadata.left().value()); } - Map<String, ArtifactDefinition> deploymentArtifacts = resource.getDeploymentArtifacts(); assetToPopulate = populateResourceWithArtifacts(assetToPopulate, resource, deploymentArtifacts); - assetToPopulate.setLastUpdaterFullName(resource.getLastUpdaterFullName()); assetToPopulate.setToscaResourceName(resource.getToscaResourceName()); assetToPopulate.setDescription(resource.getDescription()); return Either.left(assetToPopulate); } - private <T extends ServiceAssetDetailedMetadata> Either<T, StorageOperationStatus> convertToServiceDetailedMetadata(T assetToPopulate, Service service) { - + private <T extends ServiceAssetDetailedMetadata> Either<T, StorageOperationStatus> convertToServiceDetailedMetadata(T assetToPopulate, + Service service) { List<ComponentInstance> componentInstances = service.getComponentInstances(); - if (componentInstances != null) { - Either<List<ResourceInstanceMetadata>, StorageOperationStatus> resourceInstanceMetadata = convertToResourceInstanceMetadata(componentInstances, ComponentTypeEnum.SERVICE_PARAM_NAME, service.getUUID()); + Either<List<ResourceInstanceMetadata>, StorageOperationStatus> resourceInstanceMetadata = convertToResourceInstanceMetadata( + componentInstances, ComponentTypeEnum.SERVICE_PARAM_NAME, service.getUUID()); if (resourceInstanceMetadata.isRight()) { return Either.right(resourceInstanceMetadata.right().value()); } - assetToPopulate.setResources(resourceInstanceMetadata.left().value()); } - Map<String, ArtifactDefinition> deploymentArtifacts = service.getDeploymentArtifacts(); assetToPopulate = populateServiceWithArtifacts(assetToPopulate, service, deploymentArtifacts); - assetToPopulate.setLastUpdaterFullName(service.getLastUpdaterFullName()); - return Either.left(assetToPopulate); } - private <T extends ResourceAssetDetailedMetadata> T populateResourceWithArtifacts(T asset, Resource resource, Map<String, ArtifactDefinition> artifacts) { - + private <T extends ResourceAssetDetailedMetadata> T populateResourceWithArtifacts(T asset, Resource resource, + Map<String, ArtifactDefinition> artifacts) { List<ArtifactMetadata> artifactMetaList = populateAssetWithArtifacts(resource, artifacts); - asset.setArtifacts(artifactMetaList); - return asset; } - private <T extends ServiceAssetDetailedMetadata> T populateServiceWithArtifacts(T asset, Service service, Map<String, ArtifactDefinition> artifacts) { - + private <T extends ServiceAssetDetailedMetadata> T populateServiceWithArtifacts(T asset, Service service, + Map<String, ArtifactDefinition> artifacts) { List<ArtifactMetadata> artifactMetaList = populateAssetWithArtifacts(service, artifacts); - asset.setArtifacts(artifactMetaList); - return asset; } private List<ArtifactMetadata> populateAssetWithArtifacts(Component component, Map<String, ArtifactDefinition> artifacts) { - List<ArtifactMetadata> artifactMetaList = new LinkedList<>(); + List<ArtifactMetadata> artifactMetaList = new LinkedList<>(); if (artifacts != null) { Collection<ArtifactDefinition> artefactDefList = artifacts.values(); - for (ArtifactDefinition artifactDefinition : artefactDefList) { if (artifactDefinition.getEsId() != null && !artifactDefinition.getEsId().isEmpty()) { - ArtifactMetadata convertedArtifactMetadata = convertToArtifactMetadata(artifactDefinition, ComponentTypeEnum.findParamByType(component.getComponentType()), component.getUUID(), null); + ArtifactMetadata convertedArtifactMetadata = convertToArtifactMetadata(artifactDefinition, + ComponentTypeEnum.findParamByType(component.getComponentType()), component.getUUID(), null); artifactMetaList.add(convertedArtifactMetadata); } } @@ -292,22 +263,19 @@ public class AssetMetadataConverter { return artifactMetaList.isEmpty() ? null : artifactMetaList; } - private ArtifactMetadata convertToArtifactMetadata(ArtifactDefinition artifact, String componentType, String componentUUID, String resourceInstanceName) { + private ArtifactMetadata convertToArtifactMetadata(ArtifactDefinition artifact, String componentType, String componentUUID, + String resourceInstanceName) { final String COMPONENT_ARTIFACT_URL = "/sdc/v1/catalog/%s/%s/artifacts/%s"; - final String RESOURCE_INSTANCE_ARTIFACT_URL = "/sdc/v1/catalog/%s/%s/resourceInstances/%s/artifacts/%s"; - ArtifactMetadata metadata = new ArtifactMetadata(); - metadata.setArtifactName(artifact.getArtifactName()); metadata.setArtifactType(artifact.getArtifactType()); - if (resourceInstanceName == null || resourceInstanceName.isEmpty()) { metadata.setArtifactURL(String.format(COMPONENT_ARTIFACT_URL, componentType, componentUUID, artifact.getArtifactUUID())); } else { - metadata.setArtifactURL(String.format(RESOURCE_INSTANCE_ARTIFACT_URL, componentType, componentUUID, resourceInstanceName, artifact.getArtifactUUID())); + metadata.setArtifactURL( + String.format(RESOURCE_INSTANCE_ARTIFACT_URL, componentType, componentUUID, resourceInstanceName, artifact.getArtifactUUID())); } - metadata.setArtifactDescription(artifact.getDescription()); metadata.setArtifactTimeout(artifact.getTimeout() != null && artifact.getTimeout() > 0 ? artifact.getTimeout() : null); metadata.setArtifactChecksum(artifact.getArtifactChecksum()); @@ -319,15 +287,14 @@ public class AssetMetadataConverter { return metadata; } - private Either<List<ResourceInstanceMetadata>, StorageOperationStatus> convertToResourceInstanceMetadata(List<ComponentInstance> componentInstances, String componentType, String componentUUID) { + private Either<List<ResourceInstanceMetadata>, StorageOperationStatus> convertToResourceInstanceMetadata( + List<ComponentInstance> componentInstances, String componentType, String componentUUID) { List<ResourceInstanceMetadata> retList = new LinkedList<>(); Map<String, ImmutablePair<String, String>> uuidDuplicatesMap = new HashMap<>(); - for (ComponentInstance componentInstance : componentInstances) { ResourceInstanceMetadata metadata = new ResourceInstanceMetadata(); String componentUid = componentInstance.getComponentUid(); String invariantUUID, resourceUUID; - if (!uuidDuplicatesMap.containsKey(componentUid)) { Either<Resource, StorageOperationStatus> eitherResource = toscaOperationFacade.getToscaElement(componentInstance.getComponentUid()); if (eitherResource.isRight()) { @@ -342,21 +309,20 @@ public class AssetMetadataConverter { } } else { invariantUUID = uuidDuplicatesMap.get(componentUid).getRight(); - resourceUUID = uuidDuplicatesMap.get(componentUid).getLeft(); + resourceUUID = uuidDuplicatesMap.get(componentUid).getLeft(); } - metadata.setResourceInvariantUUID(invariantUUID); metadata.setResourceUUID(resourceUUID); metadata.setResourceInstanceName(componentInstance.getName()); metadata.setResourceName(componentInstance.getComponentName()); metadata.setResourceVersion(componentInstance.getComponentVersion()); metadata.setResoucreType(componentInstance.getOriginType().getValue()); - - if(MapUtils.isNotEmpty(componentInstance.getDeploymentArtifacts())){ + if (MapUtils.isNotEmpty(componentInstance.getDeploymentArtifacts())) { LinkedList<ArtifactMetadata> artifactMetaList = new LinkedList<>(); Collection<ArtifactDefinition> values = componentInstance.getDeploymentArtifacts().values(); for (ArtifactDefinition artifactDefinition : values) { - ArtifactMetadata converted = convertToArtifactMetadata(artifactDefinition, componentType, componentUUID, componentInstance.getNormalizedName()); + ArtifactMetadata converted = convertToArtifactMetadata(artifactDefinition, componentType, componentUUID, + componentInstance.getNormalizedName()); artifactMetaList.add(converted); } metadata.setArtifacts(artifactMetaList); @@ -365,5 +331,4 @@ public class AssetMetadataConverter { } return Either.left(retList); } - } |