summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java
diff options
context:
space:
mode:
authorJvD_Ericsson <jeff.van.dam@est.tech>2022-08-15 13:40:19 +0100
committerMichael Morris <michael.morris@est.tech>2022-09-06 16:45:46 +0000
commit745e350c8595f7e44846a3de1d2c946485a63ad0 (patch)
treea6f78fdba6aaa243172d50ae3f1a8723975847b3 /catalog-be/src/main/java
parentc7d44853b881daadccc6c05cddcbb89743f1bffc (diff)
Support import of updated node types
Support addition of new properties Issue-ID: SDC-4131 Signed-off-by: JvD_Ericsson <jeff.van.dam@est.tech> Change-Id: I9f12d0f4f9727c33f8444980b780758533e714cf
Diffstat (limited to 'catalog-be/src/main/java')
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/ServiceCsarInfo.java41
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceImportManager.java80
-rw-r--r--catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java61
3 files changed, 131 insertions, 51 deletions
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<String, Object> 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<Map<String, Object>, ResultStatusEnum> mainMetadataEither = ImportUtils.findFirstToscaMapElement(getMappedToscaMainTemplate(),
- ToscaTagNamesEnum.METADATA);
- Map<String, Object> 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<CategoryDefinition> categories = new ArrayList<>();
- categories.add(category);
- nodeTypeMetadata.setCategories(categories);
- String[] nodeTemplateName = nodeTemplateType.split("\\.");
- String name = nodeTemplateName[nodeTemplateName.length - 1];
- nodeTypeMetadata.setName(name);
- List<String> 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<String, Object> nodeTypesMap = (Map<String, Object>) nodeTypesYamlMap.get(ToscaTagNamesEnum.NODE_TYPES.getElementName());
- importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList,user, createNewVersion,needLock);
+ importAllNormativeResource(nodeTypesMap, nodeTypesMetadataList, user, "", createNewVersion,needLock);
}
public void importAllNormativeResource(final Map<String, Object> 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<String, Object> toscaJson, Resource resource) {
+ resource.setToscaResourceName(getToscaResourceName(toscaJson));
+ }
+
+ private String getToscaResourceName(Map<String, Object> toscaJson) {
Either<Map<String, Object>, 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<String, Object> toscaJson, Resource resource, Either<Resource, StorageOperationStatus> existingResource) {
@@ -576,13 +588,15 @@ public class ResourceImportManager {
addPropertyToList(resource.getName(), propertiesList, entry);
}
if (existingResource.isLeft()) {
- final List<PropertyDefinition> 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<PropertyDefinition> 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<String, Object> ymlObj = new Yaml().load(resourceYml);
+ String toscaName = getToscaResourceName(ymlObj);
+ final Either<Resource, StorageOperationStatus> 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<CategoryDefinition> categories = latestResource.getCategories();
+ calculateResourceIsAbstract(resource, categories);
+ }
+ }
+
private Map<String, Object> decodePayload(final String payloadData) {
final String decodedPayload = new String(Base64.decodeBase64(payloadData));
return (Map<String, Object>) 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<NodeTypeDefinition> nodeTypesToCreate = getNodeTypesToCreate(service.getModel(), csarInfo);
if (CollectionUtils.isNotEmpty(nodeTypesToCreate)) {
- createNodeTypes(nodeTypesToCreate, csarInfo);
+ createNodeTypes(nodeTypesToCreate, service.getModel(), csarInfo.getModifier());
}
Map<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo();
Either<Map<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic
@@ -299,18 +301,16 @@ public class ServiceImportBusinessLogic {
&& result.left().value().getProperties().size() != dataType.get("properties").size();
}
- private void createNodeTypes(List<NodeTypeDefinition> nodeTypesToCreate, ServiceCsarInfo csarInfo) {
+ private void createNodeTypes(List<NodeTypeDefinition> nodeTypesToCreate, String model, User user) {
NodeTypesMetadataList nodeTypesMetadataList = new NodeTypesMetadataList();
List<NodeTypeMetadata> nodeTypeMetadataList = new ArrayList<>();
-
final Map<String, Object> 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<NodeTypeDefinition> 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<String, Object> latestMappedToscaTemplate = getResourceToscaTemplate(latestResource.getUniqueId(),
+ latestResource.getToscaArtifacts().get(ToscaExportHandler.ASSET_TOSCA_TEMPLATE), csarInfo.getModifier().getUserId());
+ Map<String, Object> mappedToscaTemplate = (Map<String, Object>) nodeTypeDefinition.getMappedNodeType().getValue();
+ Map<String, Object> newMappedToscaTemplate =
+ getNewChangesToToscaTemplate(mappedToscaTemplate, (Map<String, Object>) latestMappedToscaTemplate.getValue());
+ if (!newMappedToscaTemplate.equals(latestMappedToscaTemplate.getValue())) {
+ latestMappedToscaTemplate.setValue(newMappedToscaTemplate);
+ nodeTypeDefinition.setMappedNodeType(latestMappedToscaTemplate);
+ namesOfNodeTypesToCreate.add(nodeTypeDefinition);
+ }
}
}
return namesOfNodeTypesToCreate;
}
+ private Entry<String, Object> getResourceToscaTemplate(String uniqueId, ArtifactDefinition assetToscaTemplate, String userId) {
+ String assetToToscaTemplate = assetToscaTemplate.getUniqueId();
+ ImmutablePair<String, byte[]> toscaTemplate = artifactsBusinessLogic.
+ handleDownloadRequestById(uniqueId, assetToToscaTemplate, userId, ComponentTypeEnum.RESOURCE, null, null);
+ Map<String, Object> mappedToscaTemplate = new Yaml().load(new String(toscaTemplate.right));
+ Either<Map<String, Object>, ImportUtils.ResultStatusEnum> eitherNodeTypes =
+ findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES);
+ if (eitherNodeTypes.isRight()) {
+ throw new ComponentException(ActionStatus.INVALID_TOSCA_TEMPLATE);
+ }
+ Entry<String, Object> entry = eitherNodeTypes.left().value().entrySet().iterator().next();
+ return entry;
+ }
+
+ private Map<String, Object> getNewChangesToToscaTemplate(Map<String, Object> mappedToscaTemplate, Map<String, Object> latestMappedToscaTemplate) {
+ Map<String, Object> newMappedToscaTemplate = new HashMap<>(latestMappedToscaTemplate);
+
+ Map<String, Object> properties = (Map<String, Object>) mappedToscaTemplate.get("properties");
+ Map<String, Object> latestProperties = (Map<String, Object>) latestMappedToscaTemplate.get("properties");
+ Map<String, Object> 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<String, Object> combinedEntries(Map<String, Object> firstMap, Map<String, Object> secondMap) {
+ if (MapUtils.isEmpty(firstMap)) {
+ firstMap = new HashMap<>();
+ }
+ Map<String, Object> 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<String, NodeTypeInfo> nodeTypesInfo,
CsarInfo csarInfo,
Map<String, EnumMap<ArtifactsBusinessLogic.ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToCreate,