From f7776389f137cb881033d77d89cc3f1eb4974077 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Mon, 16 Aug 2021 17:28:10 +0100 Subject: Import VSP with non-standard data types Signed-off-by: MichaelMorris Issue-ID: SDC-3673 Change-Id: I0fd16410788da3a82c74b1d38ffa4458e85e6ccc --- .../openecomp/sdc/be/components/csar/CsarInfo.java | 76 +++++++++++++++------- .../sdc/be/components/impl/ModelBusinessLogic.java | 10 ++- .../be/components/impl/ResourceBusinessLogic.java | 27 +++++++- .../impl/ServiceImportBusinessLogic.java | 2 +- 4 files changed, 88 insertions(+), 27 deletions(-) (limited to 'catalog-be/src/main/java/org') diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java index f5a1409e68..14ede6305b 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/csar/CsarInfo.java @@ -85,6 +85,9 @@ public class CsarInfo { private boolean isUpdate; @Getter private Map createdNodes; + private Map datatypeDefinitions; + private List> globalSubstitutes; + @SuppressWarnings("unchecked") public CsarInfo(User modifier, String csarUUID, Map csar, String vfResourceName, String mainTemplateName, @@ -101,7 +104,18 @@ public class CsarInfo { this.isUpdate = isUpdate; this.createdNodes = new HashMap<>(); this.nonManoConfiguration = NonManoConfigurationManager.getInstance().getNonManoConfiguration(); + this.globalSubstitutes = getGlobalSubstitutes(csar); } + + private List> getGlobalSubstitutes(final Map csar){ + final List> globalSubstitutesInCsar = new ArrayList<>(); + for (Map.Entry entry : csar.entrySet()) { + if (isAServiceTemplate(entry.getKey()) && isGlobalSubstitute(entry.getKey())) { + globalSubstitutesInCsar.add(entry); + } + } + return globalSubstitutesInCsar; + } @VisibleForTesting CsarInfo(final NonManoConfiguration nonManoConfiguration) { @@ -153,37 +167,56 @@ public class CsarInfo { this.isUpdate = isUpdate; } - public Map extractNodeTypesInfo() { + public Map extractTypesInfo() { Map nodeTypesInfo = new HashMap<>(); - List> globalSubstitutes = new ArrayList<>(); final Set nodeTypesUsedInNodeTemplates = new HashSet<>(); for (Map.Entry entry : getCsar().entrySet()) { - extractNodeTypeInfo(nodeTypesInfo, globalSubstitutes, nodeTypesUsedInNodeTemplates, entry); + extractNodeTypeInfo(nodeTypesInfo, nodeTypesUsedInNodeTemplates, entry); } if (CollectionUtils.isNotEmpty(globalSubstitutes)) { - setDerivedFrom(nodeTypesInfo, globalSubstitutes); - addGlobalSubstitutionsToNodeTypes(globalSubstitutes, nodeTypesUsedInNodeTemplates, nodeTypesInfo); + setDerivedFrom(nodeTypesInfo); + addGlobalSubstitutionsToNodeTypes(nodeTypesUsedInNodeTemplates, nodeTypesInfo); } + markNestedVfc(getMappedToscaMainTemplate(), nodeTypesInfo); return nodeTypesInfo; } + + public Map getDataTypes() { + if (datatypeDefinitions == null) { + datatypeDefinitions = new HashMap<>(); + for (Map.Entry entry : globalSubstitutes) { + final String yamlFileContents = new String(entry.getValue()); + final Map mappedToscaTemplate = new Yaml().load(yamlFileContents); + datatypeDefinitions.putAll(getDataTypesFromTemplate(mappedToscaTemplate)); + } + datatypeDefinitions.putAll(getDataTypesFromTemplate(mappedToscaMainTemplate)); + } + return datatypeDefinitions; + } + + @SuppressWarnings("unchecked") + private Map getDataTypesFromTemplate(final Map mappedToscaTemplate) { + final Either dataTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES, + ToscaElementTypeEnum.MAP); + if (dataTypesEither != null && dataTypesEither.isLeft()) { + return (Map) dataTypesEither.left().value(); + } + return Collections.emptyMap(); + } @SuppressWarnings("unchecked") - private void extractNodeTypeInfo(Map nodeTypesInfo, List> globalSubstitutes, + private void extractNodeTypeInfo(Map nodeTypesInfo, final Set nodeTypesUsedInNodeTemplates, Map.Entry entry) { - if (isAServiceTemplate(entry.getKey())) { - if (isGlobalSubstitute(entry.getKey())) { - globalSubstitutes.add(entry); - } else { - Map mappedToscaTemplate = (Map) new Yaml().load(new String(entry.getValue())); - findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP).right() - .on(sub -> handleSubstitutionMappings(nodeTypesInfo, entry, mappedToscaTemplate, (Map) sub)); - final Either nodeTypesEither = findToscaElement(mappedToscaTemplate, - TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); - if (nodeTypesEither.isLeft()) { - final Map> nodeTemplates = (Map>) nodeTypesEither.left().value(); - nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates)); - } + if (isAServiceTemplate(entry.getKey()) && !isGlobalSubstitute(entry.getKey())) { + Map mappedToscaTemplate = (Map) new Yaml().load(new String(entry.getValue())); + findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS, ToscaElementTypeEnum.MAP).right() + .on(sub -> handleSubstitutionMappings(nodeTypesInfo, entry, mappedToscaTemplate, (Map) sub)); + final Either nodeTypesEither = findToscaElement(mappedToscaTemplate, + TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES, ToscaElementTypeEnum.MAP); + if (nodeTypesEither.isLeft()) { + final Map> nodeTemplates = (Map>) nodeTypesEither.left().value(); + nodeTypesUsedInNodeTemplates.addAll(findNodeTypesUsedInNodeTemplates(nodeTemplates)); } } } @@ -232,7 +265,7 @@ public class CsarInfo { } @SuppressWarnings("unchecked") - private void setDerivedFrom(Map nodeTypesInfo, List> globalSubstitutes) { + private void setDerivedFrom(Map nodeTypesInfo) { for (Map.Entry entry : globalSubstitutes) { String yamlFileContents = new String(entry.getValue()); Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); @@ -259,8 +292,7 @@ public class CsarInfo { } @SuppressWarnings("unchecked") - private void addGlobalSubstitutionsToNodeTypes(final List> globalSubstitutes, - final Set nodeTypesUsedInNodeTemplates, final Map nodeTypesInfo) { + private void addGlobalSubstitutionsToNodeTypes(final Set nodeTypesUsedInNodeTemplates, final Map nodeTypesInfo) { for (Map.Entry entry : globalSubstitutes) { final String yamlFileContents = new String(entry.getValue()); final Map mappedToscaTemplate = (Map) new Yaml().load(yamlFileContents); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java index de79ec165d..99349395b9 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ModelBusinessLogic.java @@ -43,10 +43,18 @@ public class ModelBusinessLogic { private static final Logger LOGGER = LoggerFactory.getLogger(ModelBusinessLogic.class); private final ModelOperation modelOperation; + private final DataTypeImportManager dataTypeImportManager; @Autowired - public ModelBusinessLogic(final ModelOperation modelOperation) { + public ModelBusinessLogic(final ModelOperation modelOperation, final DataTypeImportManager dataTypeImportManager) { this.modelOperation = modelOperation; + this.dataTypeImportManager = dataTypeImportManager; + } + + public Model createModel(final Model model, final String datatypesYaml) { + createModel(model); + dataTypeImportManager.createDataTypes(datatypesYaml, model.getName()); + return model; } public Model createModel(final Model model) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java index c291c571d7..6185dc9b0e 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ResourceBusinessLogic.java @@ -118,6 +118,7 @@ import org.openecomp.sdc.be.model.InputDefinition; import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.LifeCycleTransitionEnum; import org.openecomp.sdc.be.model.LifecycleStateEnum; +import org.openecomp.sdc.be.model.Model; import org.openecomp.sdc.be.model.NodeTypeInfo; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; @@ -212,6 +213,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { private final CsarBusinessLogic csarBusinessLogic; private final PropertyBusinessLogic propertyBusinessLogic; private final PolicyBusinessLogic policyBusinessLogic; + private final ModelBusinessLogic modelBusinessLogic; private IInterfaceLifecycleOperation interfaceTypeOperation; private LifecycleBusinessLogic lifecycleBusinessLogic; @Autowired @@ -225,6 +227,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { @Autowired private SoftwareInformationBusinessLogic softwareInformationBusinessLogic; + @Autowired public ResourceBusinessLogic(final IElementOperation elementDao, final IGroupOperation groupOperation, final IGroupInstanceOperation groupInstanceOperation, final IGroupTypeOperation groupTypeOperation, @@ -242,7 +245,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { final ComponentNameValidator componentNameValidator, final ComponentTagsValidator componentTagsValidator, final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator, final ComponentProjectCodeValidator componentProjectCodeValidator, - final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic) { + final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic, + final ModelBusinessLogic modelBusinessLogic) { super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator); @@ -258,6 +262,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { this.csarBusinessLogic = csarBusinessLogic; this.propertyBusinessLogic = propertyBusinessLogic; this.policyBusinessLogic = policyBusinessLogic; + this.modelBusinessLogic = modelBusinessLogic; } static Either rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus, @@ -508,7 +513,7 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { List createdArtifacts = new ArrayList<>(); CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(newResource, oldResource, user, csarUIPayload, csarUUID); lockComponent(lockedResourceId, oldResource, "update Resource From Csar"); - Map nodeTypesInfo = csarInfo.extractNodeTypesInfo(); + Map nodeTypesInfo = csarInfo.extractTypesInfo(); Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle( nodeTypesInfo, csarInfo, oldResource); if (findNodeTypesArtifactsToHandleRes.isRight()) { @@ -1017,7 +1022,23 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { .log(LoggerSupportabilityActions.CREATE_RESOURCE_FROM_YAML, StatusCode.STARTED, "Starting to create Resource From Csar by user {}", user.getUserId()); CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID); - Map nodeTypesInfo = csarInfo.extractNodeTypesInfo(); + Map nodeTypesInfo = csarInfo.extractTypesInfo(); + if (StringUtils.isNotEmpty(resource.getModel())) { + final Map dataTypesToCreate = new HashMap<>(); + for (final String dataType: csarInfo.getDataTypes().keySet()) { + final Either result = propertyOperation.getDataTypeByName(dataType, resource.getModel()); + if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + dataTypesToCreate.put(dataType, csarInfo.getDataTypes().get(dataType)); + } + } + if (MapUtils.isNotEmpty(dataTypesToCreate)) { + final String nameForGeneratedModel = resource.getModel() + "_" + csarInfo.getVfResourceName() + resource.getCsarVersion(); + final Model model = new Model(nameForGeneratedModel, resource.getModel()); + modelBusinessLogic.createModel(model, new Yaml().dump(dataTypesToCreate)); + resource.setModel(nameForGeneratedModel); + } + } + Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = findNodeTypesArtifactsToHandle( nodeTypesInfo, csarInfo, resource); if (findNodeTypesArtifactsToHandleRes.isRight()) { 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 1a5a996a46..6bb7fdadb9 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 @@ -226,7 +226,7 @@ public class ServiceImportBusinessLogic { log.trace("************* created successfully from YAML, resource TOSCA "); try { CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(service, null, user, csarUIPayload, csarUUID); - Map nodeTypesInfo = csarInfo.extractNodeTypesInfo(); + Map nodeTypesInfo = csarInfo.extractTypesInfo(); Either>>, ResponseFormat> findNodeTypesArtifactsToHandleRes = serviceImportParseLogic .findNodeTypesArtifactsToHandle(nodeTypesInfo, csarInfo, service); if (findNodeTypesArtifactsToHandleRes.isRight()) { -- cgit 1.2.3-korg