From 745e350c8595f7e44846a3de1d2c946485a63ad0 Mon Sep 17 00:00:00 2001 From: JvD_Ericsson Date: Mon, 15 Aug 2022 13:40:19 +0100 Subject: Support import of updated node types Support addition of new properties Issue-ID: SDC-4131 Signed-off-by: JvD_Ericsson Change-Id: I9f12d0f4f9727c33f8444980b780758533e714cf --- .../sdc/be/components/csar/ServiceCsarInfo.java | 41 +++-------- .../be/components/impl/ResourceImportManager.java | 80 ++++++++++++++++++---- .../impl/ServiceImportBusinessLogic.java | 61 +++++++++++++++-- 3 files changed, 131 insertions(+), 51 deletions(-) (limited to 'catalog-be/src/main/java') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java index 5c948323a4..08f777db1f 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java @@ -44,6 +44,7 @@ import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum; import org.openecomp.sdc.be.model.NodeTypeDefinition; import org.openecomp.sdc.be.model.NodeTypeInfo; import org.openecomp.sdc.be.model.NodeTypeMetadata; +import org.openecomp.sdc.be.model.NullNodeTypeMetadata; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.category.CategoryDefinition; import org.openecomp.sdc.be.model.category.SubCategoryDefinition; @@ -173,7 +174,7 @@ public class ServiceCsarInfo extends CsarInfo { highestDependencyIndex = indexOfDependency > highestDependencyIndex ? indexOfDependency : highestDependencyIndex; } sortedNodeTypeDefinitions.add(highestDependencyIndex + 1, nodeType); - nodeTypeDefinitionsMap.put(nodeType.getNodeTypeMetadata().getToscaName(), nodeType); + nodeTypeDefinitionsMap.put(nodeType.getMappedNodeType().getKey(), nodeType); }); return sortedNodeTypeDefinitions; } @@ -185,7 +186,7 @@ public class ServiceCsarInfo extends CsarInfo { if (derivedFromTypeEither.isLeft() && derivedFromTypeEither.left().value() != null) { final String derivedFrom = (String) derivedFromTypeEither.left().value(); dependencies.add(derivedFrom); - nodeTypes.stream().filter(derivedFromCandidate -> derivedFrom.contentEquals(derivedFromCandidate.getNodeTypeMetadata().getToscaName())) + nodeTypes.stream().filter(derivedFromCandidate -> derivedFrom.contentEquals(derivedFromCandidate.getMappedNodeType().getKey())) .forEach(derivedFromNodeType -> dependencies.addAll(getDependencyTypes(derivedFromNodeType, nodeTypes))); } return dependencies; @@ -247,7 +248,7 @@ public class ServiceCsarInfo extends CsarInfo { Map metadata = metadataEither.left().value(); createMetadataFromTemplate(nodeTypeMetadata, metadata, nodeTemplateType); } else { - createDefaultMetadata(nodeTypeMetadata, nodeTemplateType); + nodeTypeMetadata = createDefaultMetadata(nodeTemplateType); } return nodeTypeMetadata; } @@ -275,40 +276,14 @@ public class ServiceCsarInfo extends CsarInfo { nodeTypeMetadata.setResourceVendorModelNumber((String) metadata.get("resourceVendorModelNumber")); nodeTypeMetadata.setResourceType((String) metadata.get("type")); nodeTypeMetadata.setVendorName((String) metadata.get("resourceVendor")); - nodeTypeMetadata.setVendorRelease((String) metadata.get("resourceVendorRelease")); + nodeTypeMetadata.setVendorRelease(String.valueOf(metadata.get("resourceVendorRelease"))); nodeTypeMetadata.setModel((String) metadata.get("model")); nodeTypeMetadata.setNormative(false); } - private void createDefaultMetadata(NodeTypeMetadata nodeTypeMetadata, String nodeTemplateType) { + private NullNodeTypeMetadata createDefaultMetadata(String nodeTemplateType) { + NullNodeTypeMetadata nodeTypeMetadata = new NullNodeTypeMetadata(); nodeTypeMetadata.setToscaName(nodeTemplateType); - nodeTypeMetadata.setContactId(getModifier().getUserId()); - nodeTypeMetadata.setDescription("A vfc of type " + nodeTemplateType); - Either, ResultStatusEnum> mainMetadataEither = ImportUtils.findFirstToscaMapElement(getMappedToscaMainTemplate(), - ToscaTagNamesEnum.METADATA); - Map mainMetadata = mainMetadataEither.left().value(); - nodeTypeMetadata.setModel((String) mainMetadata.get("model")); - SubCategoryDefinition subCategory = new SubCategoryDefinition(); - subCategory.setName("Network Elements"); - CategoryDefinition category = new CategoryDefinition(); - category.setName("Generic"); - category.setNormalizedName("generic"); - category.setIcons(List.of(DEFAULT_ICON)); - category.setNormalizedName("generic"); - category.addSubCategory(subCategory); - List categories = new ArrayList<>(); - categories.add(category); - nodeTypeMetadata.setCategories(categories); - String[] nodeTemplateName = nodeTemplateType.split("\\."); - String name = nodeTemplateName[nodeTemplateName.length - 1]; - nodeTypeMetadata.setName(name); - List tags = new ArrayList<>(); - tags.add(name); - nodeTypeMetadata.setTags(tags); - nodeTypeMetadata.setIcon("defaulticon"); - nodeTypeMetadata.setVendorName((String) mainMetadata.get("name")); - nodeTypeMetadata.setVendorRelease("1"); - nodeTypeMetadata.setNormative(false); + return nodeTypeMetadata; } - } 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 a20c30624e..79b3c961f7 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 @@ -79,9 +79,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; @@ -173,11 +175,11 @@ public class ResourceImportManager { return; } final Map nodeTypesMap = (Map) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName()); - importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList,user, createNewVersion,needLock); + importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList, user, "", createNewVersion,needLock); } public void importAllNormativeResource(final Map nodeTypesMap, final NodeTypesMetadataList nodeTypesMetadataList, - final User user, final boolean createNewVersion, final boolean needLock) { + final User user, String model, final boolean createNewVersion, final boolean needLock) { try { nodeTypesMetadataList.getNodeMetadataList().forEach(nodeTypeMetadata -> { final String nodeTypeToscaName = nodeTypeMetadata.getToscaName(); @@ -191,7 +193,12 @@ public class ResourceImportManager { 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(); @@ -225,7 +232,7 @@ 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()); @@ -429,12 +436,17 @@ public class ResourceImportManager { } 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 existingResource) { @@ -576,13 +588,15 @@ public class ResourceImportManager { addPropertyToList(resource.getName(), propertiesList, entry); } if (existingResource.isLeft()) { - 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); + 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); + } } } @@ -876,6 +890,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()); @@ -887,6 +913,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); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java index b4079ac0d2..0a7ce20015 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java @@ -19,6 +19,7 @@ import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toMap; import static org.apache.commons.collections.CollectionUtils.isNotEmpty; +import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMapElement; import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStringElement; import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue; import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN; @@ -131,6 +132,7 @@ import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder; import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum; import org.openecomp.sdc.be.tosca.CsarUtils; +import org.openecomp.sdc.be.tosca.ToscaExportHandler; import org.openecomp.sdc.be.ui.model.OperationUi; import org.openecomp.sdc.be.utils.TypeUtils; import org.openecomp.sdc.common.api.ArtifactGroupTypeEnum; @@ -256,7 +258,7 @@ public class ServiceImportBusinessLogic { } final List nodeTypesToCreate = getNodeTypesToCreate(service.getModel(), csarInfo); if (CollectionUtils.isNotEmpty(nodeTypesToCreate)) { - createNodeTypes(nodeTypesToCreate, csarInfo); + createNodeTypes(nodeTypesToCreate, service.getModel(), csarInfo.getModifier()); } Map nodeTypesInfo = csarInfo.extractTypesInfo(); Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic @@ -299,18 +301,16 @@ public class ServiceImportBusinessLogic { && result.left().value().getProperties().size() != dataType.get("properties").size(); } - private void createNodeTypes(List nodeTypesToCreate, ServiceCsarInfo csarInfo) { + private void createNodeTypes(List nodeTypesToCreate, String model, User user) { NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList(); List nodeTypeMetadataList = new ArrayList<>(); - final Map allTypesToCreate = new HashMap<>(); nodeTypesToCreate.stream().forEach(nodeType -> { allTypesToCreate.put(nodeType.getMappedNodeType().getKey(), nodeType.getMappedNodeType().getValue()); nodeTypeMetadataList.add(nodeType.getNodeTypeMetadata()); }); - nodeTypesMetadataList.setNodeMetadataList(nodeTypeMetadataList); - resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, csarInfo.getModifier(), true, false); + resourceImportManager.importAllNormativeResource(allTypesToCreate, nodeTypesMetadataList, user, model,true, false); } private List getNodeTypesToCreate(final String model, final ServiceCsarInfo csarInfo) { @@ -321,11 +321,62 @@ public class ServiceImportBusinessLogic { .getLatestByToscaResourceName(nodeTypeDefinition.getMappedNodeType().getKey(), model); if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { namesOfNodeTypesToCreate.add(nodeTypeDefinition); + } else if (result.isLeft()) { + Resource latestResource = (Resource) result.left().value(); + Entry latestMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(), + latestResource.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE), csarInfo.getModifier().getUserId()); + Map mappedToscaTemplate = (Map) nodeTypeDefinition.getMappedNodeType().getValue(); + Map newMappedToscaTemplate = + getNewChangesToToscaTemplate(mappedToscaTemplate, (Map) latestMappedToscaTemplate.getValue()); + if (!newMappedToscaTemplate.equals(latestMappedToscaTemplate.getValue())) { + latestMappedToscaTemplate.setValue(newMappedToscaTemplate); + nodeTypeDefinition.setMappedNodeType(latestMappedToscaTemplate); + namesOfNodeTypesToCreate.add(nodeTypeDefinition); + } } } return namesOfNodeTypesToCreate; } + private Entry getResourceToscaTemplate(String uniqueId, ArtifactDefinition assetToscaTemplate, String userId) { + String assetToToscaTemplate = assetToscaTemplate.getUniqueId(); + ImmutablePair toscaTemplate = artifactsBusinessLogic. + handleDownloadRequestById(uniqueId, assetToToscaTemplate, userId, ComponentTypeEnum.RESOURCE, null, null); + Map mappedToscaTemplate = new Yaml().load(new String(toscaTemplate.right)); + Either, ImportUtils.ResultStatusEnum> eitherNodeTypes = + findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES); + if (eitherNodeTypes.isRight()) { + throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE); + } + Entry entry = eitherNodeTypes.left().value().entrySet().iterator().next(); + return entry; + } + + private Map getNewChangesToToscaTemplate(Map mappedToscaTemplate, Map latestMappedToscaTemplate) { + Map newMappedToscaTemplate = new HashMap<>(latestMappedToscaTemplate); + + Map properties = (Map) mappedToscaTemplate.get("properties"); + Map latestProperties = (Map) latestMappedToscaTemplate.get("properties"); + Map allProperties = combinedEntries(properties, latestProperties); + if ((MapUtils.isEmpty(latestProperties) && MapUtils.isNotEmpty(allProperties)) || + (MapUtils.isNotEmpty(latestProperties) && !allProperties.equals(latestProperties))) { + newMappedToscaTemplate.put("properties", allProperties); + } + return newMappedToscaTemplate; + } + + private Map combinedEntries(Map firstMap, Map secondMap) { + if (MapUtils.isEmpty(firstMap)) { + firstMap = new HashMap<>(); + } + Map combinedEntries = new HashMap<>(firstMap); + if (MapUtils.isEmpty(secondMap)) { + return combinedEntries; + } + combinedEntries.putAll(secondMap); + return combinedEntries; + } + protected Service createServiceFromYaml(Service service, String topologyTemplateYaml, String yamlName, Map nodeTypesInfo, CsarInfo csarInfo, Map>> nodeTypesArtifactsToCreate, -- cgit 1.2.3-korg