X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-be%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fcomponents%2Fimpl%2FResourceImportManager.java;h=1e2cea5c640bb68022fe4e95bbdcd676610e158d;hb=adb7f7496af6e71e1cced44ee2f7485c9917a806;hp=cbae55d952b87577e1fc99fccb262b078210b09d;hpb=5cbf514addcd6eecf2f1b86c46cf641d49d6c059;p=sdc.git diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java index cbae55d952..1e2cea5c64 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java @@ -27,18 +27,23 @@ import static org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementO import fj.data.Either; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; +import java.util.Comparator; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.Set; import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.servlet.ServletContext; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; @@ -58,8 +63,13 @@ import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity; import org.openecomp.sdc.be.dao.api.ActionStatus; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao; import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus; +import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; +import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; +import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; @@ -70,9 +80,11 @@ import org.openecomp.sdc.be.model.AttributeDefinition; import org.openecomp.sdc.be.model.CapabilityDefinition; import org.openecomp.sdc.be.model.ComponentInstanceProperty; import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.DefaultUploadResourceInfo; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifecycleStateEnum; import org.openecomp.sdc.be.model.NodeTypesMetadataList; +import org.openecomp.sdc.be.model.NullNodeTypeMetadata; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RequirementDefinition; import org.openecomp.sdc.be.model.Resource; @@ -81,6 +93,7 @@ import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade; +import org.openecomp.sdc.be.model.jsonjanusgraph.utils.ModelConverter; import org.openecomp.sdc.be.model.mapper.NodeTypeMetadataMapper; import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; @@ -97,14 +110,13 @@ import org.openecomp.sdc.common.util.ThreadLocalsHolder; import org.openecomp.sdc.common.util.ValidationUtils; import org.openecomp.sdc.exception.ResponseFormat; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; import org.springframework.web.context.WebApplicationContext; import org.yaml.snakeyaml.Yaml; -@Component("resourceImportManager") +@org.springframework.stereotype.Component("resourceImportManager") public class ResourceImportManager { - static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("['\\w\\s\\-\\_\\d\\:]+"); + static final Pattern PROPERTY_NAME_PATTERN_IGNORE_LENGTH = Pattern.compile("['\\w\\s\\-\\.\\:]+"); private static final Logger log = Logger.getLogger(ResourceImportManager.class); private final InterfaceDefinitionHandler interfaceDefinitionHandler; private final ComponentsUtils componentsUtils; @@ -160,25 +172,34 @@ public class ResourceImportManager { log.error(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), "Could not parse node types YAML", e); throw new ByActionStatusComponentException(ActionStatus.INVALID_NODE_TYPES_YAML); } - if (!nodeTypesYamlMap.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName())) { return; } final Map nodeTypesMap = (Map) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()); + importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList, user, "", createNewVersion,needLock); + } + + public void importAllNormativeResource(final Map nodeTypesMap, final NodeTypesMetadataList nodeTypesMetadataList, + final User user, String model, final boolean createNewVersion, final boolean needLock) { try { nodeTypesMetadataList.getNodeMetadataList().forEach(nodeTypeMetadata -> { final String nodeTypeToscaName = nodeTypeMetadata.getToscaName(); final Map nodeTypeMap = (Map) nodeTypesMap.get(nodeTypeToscaName); if (nodeTypeMap == null) { log.warn(EcompLoggerErrorCode.BUSINESS_PROCESS_ERROR, ResourceImportManager.class.getName(), - "Could not find given node type '{}'. The node will not be created.", new Object[]{nodeTypeToscaName}); + "Could not find given node type '{}'. The node will not be created.", nodeTypeToscaName); } else { final Map>> nodeTypeDefinitionMap = Map.of(ToscaTagNamesEnum.NODE_TYPES.getElementName(), Map.of(nodeTypeToscaName, nodeTypeMap) ); final String nodeTypeYaml = new Yaml().dump(nodeTypeDefinitionMap); - importNormativeResource(nodeTypeYaml, NodeTypeMetadataMapper.mapTo(nodeTypeMetadata), user, createNewVersion, needLock, true); + UploadResourceInfo uploadResourceInfo = NodeTypeMetadataMapper.mapTo(nodeTypeMetadata); + if (uploadResourceInfo instanceof DefaultUploadResourceInfo) { + uploadResourceInfo.setModel(model); + uploadResourceInfo.setContactId(user.getUserId()); + } + importNormativeResource(nodeTypeYaml, uploadResourceInfo, user, createNewVersion, needLock, true); } }); janusGraphDao.commit(); @@ -188,15 +209,6 @@ public class ResourceImportManager { } } - public ImmutablePair importNormativeResourceFromCsar(String resourceYml, UploadResourceInfo resourceMetaData, - User creator, boolean createNewVersion, boolean needLock) { - LifecycleChangeInfoWithAction lifecycleChangeInfo = new LifecycleChangeInfoWithAction(); - lifecycleChangeInfo.setUserRemarks("certification on import"); - Function validator = resource -> resourceBusinessLogic.validatePropertiesDefaultValues(resource); - return importCertifiedResource(resourceYml, resourceMetaData, creator, validator, lifecycleChangeInfo, false, createNewVersion, needLock, - null, null, false, null, null, false); - } - public ImmutablePair importCertifiedResource(String resourceYml, UploadResourceInfo resourceMetaData, User creator, Function validationFunction, LifecycleChangeInfoWithAction lifecycleChangeInfo, boolean isInTransaction, @@ -212,9 +224,10 @@ public class ResourceImportManager { try { boolean shouldBeCertified = nodeTypeArtifactsToHandle == null || nodeTypeArtifactsToHandle.isEmpty(); setConstantMetaData(resource, shouldBeCertified); - setMetaDataFromJson(resourceMetaData, resource); + setResourceMetaData(resource, resourceYml, resourceMetaData); populateResourceFromYaml(resourceYml, resource); validationFunction.apply(resource); + resource.getComponentMetadataDefinition().getMetadataDataDefinition().setNormative(resourceMetaData.isNormative()); checkResourceExists(createNewVersion, csarInfo, resource); resource = resourceBusinessLogic .createOrUpdateResourceByImport(resource, creator, true, isInTransaction, needLock, csarInfo, nodeName, isNested).left; @@ -272,7 +285,8 @@ public class ResourceImportManager { final Either latestByName = toscaOperationFacade.getLatestByName(resource.getName(), model); if (latestByName.isLeft() && toscaOperationFacade.isNodeAssociatedToModel(model, resource)) { if (model == null) { - throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, resource.getName()); + throw new ByActionStatusComponentException(ActionStatus.COMPONENT_NAME_ALREADY_EXIST, + resource.getResourceType().name(), resource.getName()); } throw new ByActionStatusComponentException(ActionStatus.COMPONENT_WITH_MODEL_ALREADY_EXIST, resource.getName(), model); } @@ -308,6 +322,7 @@ public class ResourceImportManager { resource.setIcon(resourceMetaData.getResourceIconPath()); resource.setResourceVendorModelNumber(resourceMetaData.getResourceVendorModelNumber()); resource.setResourceType(ResourceTypeEnum.valueOf(resourceMetaData.getResourceType())); + resource.setTenant(resourceMetaData.getTenant()); if (resourceMetaData.getVendorName() != null) { resource.setVendorName(resourceMetaData.getVendorName()); } @@ -327,15 +342,13 @@ public class ResourceImportManager { try { setMetaDataFromJson(resourceMetaData, resource); populateResourceFromYaml(resourceYml, resource); - // currently import VF isn't supported. In future will be supported - - // import VF only with CSAR file!! + // currently import VF isn't supported. In future will be supported import VF only with CSAR file!! if (ResourceTypeEnum.VF == resource.getResourceType()) { log.debug("Now import VF isn't supported. It will be supported in future with CSAR file only"); throw new ByActionStatusComponentException(ActionStatus.RESTRICTED_OPERATION); } resourceBusinessLogic.validateDerivedFromNotEmpty(creator, resource, AuditingActionEnum.CREATE_RESOURCE); - Boolean validatePropertiesTypes = resourceBusinessLogic.validatePropertiesDefaultValues(resource); + resourceBusinessLogic.validatePropertiesDefaultValues(resource); responsePair = resourceBusinessLogic.createOrUpdateResourceByImport(resource, creator, false, isInTransaction, true, null, null, false); } catch (RuntimeException e) { handleImportResourceException(resourceMetaData, creator, false, e); @@ -343,17 +356,15 @@ public class ResourceImportManager { return responsePair; } - private void populateResourceFromYaml(String resourceYml, Resource resource) { + private void populateResourceFromYaml(final String resourceYml, Resource resource) { @SuppressWarnings("unchecked") Object ymlObj = new Yaml().load(resourceYml); if (ymlObj instanceof Map) { - Map toscaJsonAll = (Map) ymlObj; + final Either existingResource = getExistingResource(resource); + final Map toscaJsonAll = (Map) ymlObj; Map toscaJson = toscaJsonAll; - // Checks if exist and builds the node_types map - if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) - && resource.getResourceType() != ResourceTypeEnum.CVFC) { + if (toscaJsonAll.containsKey(ToscaTagNamesEnum.NODE_TYPES.getElementName()) && resource.getResourceType() != ResourceTypeEnum.CVFC) { toscaJson = new HashMap<>(); - toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), - toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName())); + toscaJson.put(ToscaTagNamesEnum.NODE_TYPES.getElementName(), toscaJsonAll.get(ToscaTagNamesEnum.NODE_TYPES.getElementName())); } final List foundElements = new ArrayList<>(); final Either, ResultStatusEnum> toscaElements = ImportUtils @@ -364,49 +375,107 @@ public class ResourceImportManager { resource.setDataTypes(extractDataTypeFromJson(resourceBusinessLogic, toscaAttributes, resource.getModel())); } } - // Derived From - Resource parentResource = setDerivedFrom(toscaJson, resource); + final Resource parentResource = setDerivedFrom(toscaJson, resource); if (StringUtils.isEmpty(resource.getToscaResourceName())) { setToscaResourceName(toscaJson, resource); } setCapabilities(toscaJson, resource, parentResource); - setProperties(toscaJson, resource); + setProperties(toscaJson, resource, existingResource); setAttributes(toscaJson, resource); setRequirements(toscaJson, resource, parentResource); - setInterfaceLifecycle(toscaJson, resource); + setInterfaceLifecycle(toscaJson, resource, existingResource); } else { throw new ByActionStatusComponentException(ActionStatus.GENERAL_ERROR); } } + private Either getExistingResource(final Resource resource) { + final Either, JanusGraphOperationStatus> byCriteria = janusGraphDao.getByCriteria( + getVertexTypeEnum(resource.getResourceType()), propertiesToMatch(resource), propertiesToNotMatch(), + JsonParseFlagEnum.ParseAll, resource.getModel(), false); + if (byCriteria.isLeft() && CollectionUtils.isNotEmpty(byCriteria.left().value())) { + final List graphVertexList = byCriteria.left().value(); + if (graphVertexList.size() == 1) { + return toscaOperationFacade.getToscaElement(graphVertexList.get(0).getUniqueId()); + } else { + final Optional vertex = graphVertexList.stream() + .max(Comparator.comparing(graphVertex -> (String) graphVertex.getMetadataProperties().get(GraphPropertyEnum.VERSION))); + if (vertex.isPresent()) { + return toscaOperationFacade.getToscaElement(vertex.get().getUniqueId()); + } + } + } + return Either.right(StorageOperationStatus.NOT_FOUND); + } + + private VertexTypeEnum getVertexTypeEnum(final ResourceTypeEnum resourceType) { + return ModelConverter.isAtomicComponent(resourceType) ? VertexTypeEnum.NODE_TYPE : VertexTypeEnum.TOPOLOGY_TEMPLATE; + } + + private Map propertiesToMatch(final Resource resource) { + final Map graphProperties = new EnumMap<>(GraphPropertyEnum.class); + graphProperties.put(GraphPropertyEnum.NORMALIZED_NAME, ValidationUtils.normaliseComponentName(resource.getName())); + graphProperties.put(GraphPropertyEnum.COMPONENT_TYPE, resource.getComponentType().name()); + graphProperties.put(GraphPropertyEnum.RESOURCE_TYPE, resource.getResourceType().name()); + graphProperties.put(GraphPropertyEnum.IS_HIGHEST_VERSION, true); + return graphProperties; + } + + private Map propertiesToNotMatch() { + final Map graphProperties = new EnumMap<>(GraphPropertyEnum.class); + graphProperties.put(GraphPropertyEnum.IS_DELETED, true); + graphProperties.put(GraphPropertyEnum.IS_ARCHIVED, true); + return graphProperties; + } + private void setToscaResourceName(Map toscaJson, Resource resource) { + resource.setToscaResourceName(getToscaResourceName(toscaJson)); + } + + private String getToscaResourceName(Map toscaJson) { Either, ResultStatusEnum> toscaElement = ImportUtils - .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES); + .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.NODE_TYPES); if (toscaElement.isLeft() && toscaElement.left().value().size() == 1) { String toscaResourceName = toscaElement.left().value().keySet().iterator().next(); - resource.setToscaResourceName(toscaResourceName); + return toscaResourceName; } + return null; } - private void setInterfaceLifecycle(Map toscaJson, Resource resource) { - Either, ResultStatusEnum> toscaInterfaces = ImportUtils + private void setInterfaceLifecycle(Map toscaJson, Resource resource, Either existingResource) { + final Either, ResultStatusEnum> toscaInterfaces = ImportUtils .findFirstToscaMapElement(toscaJson, ToscaTagNamesEnum.INTERFACES); + final Map moduleInterfaces = new HashMap<>(); + final Map map; if (toscaInterfaces.isLeft()) { - Map jsonInterfaces = toscaInterfaces.left().value(); - Map moduleInterfaces = new HashMap<>(); - for (final Entry interfaceNameValue : jsonInterfaces.entrySet()) { - final Either eitherInterface = createModuleInterface(interfaceNameValue.getValue(), resource.getModel()); + map = toscaInterfaces.left().value(); + for (final Entry interfaceNameValue : map.entrySet()) { + final Either eitherInterface = createModuleInterface(interfaceNameValue.getValue(), + resource.getModel()); if (eitherInterface.isRight()) { log.info("error when creating interface:{}, for resource:{}", interfaceNameValue.getKey(), resource.getName()); } else { final InterfaceDefinition interfaceDefinition = eitherInterface.left().value(); - moduleInterfaces.put(interfaceNameValue.getKey(), interfaceDefinition); + moduleInterfaces.put(interfaceDefinition.getType(), interfaceDefinition); } } - if (!moduleInterfaces.isEmpty()) { - resource.setInterfaces(moduleInterfaces); + } else { + map = Collections.emptyMap(); + } + if (existingResource.isLeft()) { + final Map userCreatedInterfaceDefinitions = + existingResource.left().value().getInterfaces().entrySet().stream() + .filter(i -> i.getValue().isUserCreated()) + .filter(i -> !map.containsKey(i.getValue().getType())) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue)); + if (MapUtils.isNotEmpty(userCreatedInterfaceDefinitions)) { + moduleInterfaces.putAll(userCreatedInterfaceDefinitions); } } + + if (MapUtils.isNotEmpty(moduleInterfaces)) { + resource.setInterfaces(moduleInterfaces); + } } private Either createModuleInterface(final Object interfaceJson, final String model) { @@ -438,9 +507,7 @@ public class ResourceImportManager { Map> moduleRequirements = new HashMap<>(); // Checking for name duplication Set reqNames = new HashSet<>(); - // Getting flattened list of capabilities of parent node - cap name - - // to cap type + // Getting flattened list of capabilities of parent node - cap name to cap type Map reqName2TypeMap = getReqName2Type(parentResource); for (Object jsonRequirementObj : jsonRequirements) { // Requirement @@ -504,33 +571,53 @@ public class ResourceImportManager { return requirement; } - private void setProperties(Map toscaJson, Resource resource) { - Map reducedToscaJson = new HashMap<>(toscaJson); + private void setProperties(final Map toscaJson, final Resource resource, + final Either existingResource) { + final Map reducedToscaJson = new HashMap<>(toscaJson); ImportUtils.removeElementFromJsonMap(reducedToscaJson, "capabilities"); - Either, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson); + final Either, ResultStatusEnum> properties = ImportUtils.getProperties(reducedToscaJson); if (properties.isLeft()) { - List propertiesList = new ArrayList<>(); - Map value = properties.left().value(); - if (value != null) { - for (Entry entry : value.entrySet()) { - String name = entry.getKey(); - if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(name).matches()) { - log.debug("The property with invalid name {} occured upon import resource {}. ", name, resource.getName()); - throw new ByActionStatusComponentException( - componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)); + final Map propertyDefinitionMap = properties.left().value(); + if (MapUtils.isNotEmpty(propertyDefinitionMap)) { + final List propertiesList = new ArrayList<>(); + for (final Entry entry : propertyDefinitionMap.entrySet()) { + addPropertyToList(resource.getName(), propertiesList, entry); + } + if (existingResource.isLeft()) { + if ( CollectionUtils.isNotEmpty(existingResource.left().value().getProperties())) { + final List userCreatedResourceProperties = + existingResource.left().value().getProperties().stream() + .filter(PropertyDataDefinition::isUserCreated) + .filter(propertyDefinition -> !propertyDefinitionMap.containsKey(propertyDefinition.getName())) + .collect(Collectors.toList()); + if (CollectionUtils.isNotEmpty(userCreatedResourceProperties)) { + propertiesList.addAll(userCreatedResourceProperties); + } } - PropertyDefinition propertyDefinition = entry.getValue(); - propertyDefinition.setName(name); - propertiesList.add(propertyDefinition); } + + resource.setProperties(propertiesList); } - resource.setProperties(propertiesList); } else if (properties.right().value() != ResultStatusEnum.ELEMENT_NOT_FOUND) { throw new ByActionStatusComponentException( componentsUtils.convertFromResultStatusEnum(properties.right().value(), JsonPresentationFields.PROPERTY)); } } + private void addPropertyToList(final String resourceName, + final List propertiesList, + final Entry entry) { + final String propertyName = entry.getKey(); + if (!PROPERTY_NAME_PATTERN_IGNORE_LENGTH.matcher(propertyName).matches()) { + log.debug("The property with invalid name {} occured upon import resource {}. ", propertyName, resourceName); + throw new ByActionStatusComponentException( + componentsUtils.convertFromResultStatusEnum(ResultStatusEnum.INVALID_PROPERTY_NAME, JsonPresentationFields.PROPERTY)); + } + final PropertyDefinition propertyDefinition = entry.getValue(); + propertyDefinition.setName(propertyName); + propertiesList.add(propertyDefinition); + } + private void setAttributes(final Map originalToscaJsonMap, final Resource resource) { final Map toscaJsonMap = new HashMap<>(originalToscaJsonMap); ImportUtils.removeElementFromJsonMap(toscaJsonMap, "capabilities"); @@ -556,6 +643,11 @@ public class ResourceImportManager { } final AttributeDefinition attributeDefinition = entry.getValue(); attributeDefinition.setName(name); + if (attributeDefinition.getEntry_schema() != null && attributeDefinition.getEntry_schema().getType() != null) { + attributeDefinition.setSchema(new SchemaDefinition()); + attributeDefinition.getSchema().setProperty(new PropertyDataDefinition()); + attributeDefinition.getSchema().getProperty().setType(entry.getValue().getEntry_schema().getType()); + } attributeDefinitionList.add(attributeDefinition); } resource.setAttributes(attributeDefinitionList); @@ -569,7 +661,8 @@ public class ResourceImportManager { String derivedFrom = toscaDerivedFromElement.left().value(); log.debug("Derived from TOSCA name is {}", derivedFrom); resource.setDerivedFrom(Arrays.asList(new String[]{derivedFrom})); - Either latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom, resource.getModel()); + Either latestByToscaResourceName = toscaOperationFacade.getLatestByToscaResourceName(derivedFrom, + resource.getModel()); if (latestByToscaResourceName.isRight()) { StorageOperationStatus operationStatus = latestByToscaResourceName.right().value(); if (operationStatus == StorageOperationStatus.NOT_FOUND) { @@ -793,6 +886,18 @@ public class ResourceImportManager { getAuditingManager().auditEvent(factory); } + private void setResourceMetaData(Resource resource, String resourceYml, UploadResourceInfo resourceMetaData) { + Map ymlObj = new Yaml().load(resourceYml); + String toscaName = getToscaResourceName(ymlObj); + final Either latestByToscaName = toscaOperationFacade + .getLatestByToscaResourceName(toscaName, resourceMetaData.getModel()); + if (latestByToscaName.isLeft() && resourceMetaData instanceof DefaultUploadResourceInfo) { + setMetaDataFromLatestResource(resource, latestByToscaName.left().value()); + } else { + setMetaDataFromJson(resourceMetaData, resource); + } + } + private void setMetaDataFromJson(final UploadResourceInfo resourceMetaData, final Resource resource) { this.populateResourceMetadata(resourceMetaData, resource); resource.setCreatorUserId(resourceMetaData.getContactId()); @@ -804,6 +909,34 @@ public class ResourceImportManager { calculateResourceIsAbstract(resource, categories); } + private void setMetaDataFromLatestResource(Resource resource, Resource latestResource) { + if (resource != null && latestResource != null) { + resource.setCreatorUserId(latestResource.getContactId()); + resource.setDescription(latestResource.getDescription()); + resource.setTags(latestResource.getTags()); + resource.setCategories(latestResource.getCategories()); + resource.setContactId(latestResource.getContactId()); + resource.setName(latestResource.getName()); + resource.setIcon(latestResource.getIcon()); + resource.setResourceVendorModelNumber(latestResource.getResourceVendorModelNumber()); + resource.setResourceType(latestResource.getResourceType()); + if (latestResource.getVendorName() != null) { + resource.setVendorName(latestResource.getVendorName()); + } + if (latestResource.getVendorRelease() != null) { + resource.setVendorRelease(latestResource.getVendorRelease()); + } + if (latestResource.getModel() != null) { + resource.setModel(latestResource.getModel()); + } + if (latestResource.getToscaVersion() != null) { + resource.setToscaVersion(latestResource.getToscaVersion()); + } + final List categories = latestResource.getCategories(); + calculateResourceIsAbstract(resource, categories); + } + } + private Map decodePayload(final String payloadData) { final String decodedPayload = new String(Base64.decodeBase64(payloadData)); return (Map) new Yaml().load(decodedPayload);