From 31bfab860800308a86430bd03628c3ea17cd9115 Mon Sep 17 00:00:00 2001 From: davsad Date: Mon, 1 Nov 2021 16:35:05 +0000 Subject: Import VSP with non-standard policy types Issue-ID: SDC-3715 Signed-off-by: davsad Change-Id: I48e9831b8a27341db2d33d85125cb9d42043e71b --- .../openecomp/sdc/be/components/csar/CsarInfo.java | 17 ++++-- .../be/components/impl/DataTypeBusinessLogic.java | 26 ++++++++- .../sdc/be/components/impl/ModelBusinessLogic.java | 27 +-------- .../components/impl/PolicyTypeBusinessLogic.java | 32 ++++++++--- .../be/components/impl/ResourceBusinessLogic.java | 65 ++++++++++++++++++---- 5 files changed, 117 insertions(+), 50 deletions(-) (limited to 'catalog-be/src/main/java') 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 0571f55c32..dc6f7c4416 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 @@ -88,6 +88,7 @@ public class CsarInfo { @Getter private Map createdNodes; private Map datatypeDefinitions; + private Map policytypeDefinitions; private List> globalSubstitutes; @@ -195,16 +196,24 @@ public class CsarInfo { 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(getTypesFromTemplate(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES)); } - datatypeDefinitions.putAll(getDataTypesFromTemplate(mappedToscaMainTemplate)); + datatypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES)); } return datatypeDefinitions; } + + public Map getPolicyTypes() { + if (policytypeDefinitions == null) { + policytypeDefinitions = new HashMap<>(); + policytypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.POLICY_TYPES)); + } + return policytypeDefinitions; + } @SuppressWarnings("unchecked") - private Map getDataTypesFromTemplate(final Map mappedToscaTemplate) { - final Either dataTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES, + private Map getTypesFromTemplate(final Map mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum type) { + final Either dataTypesEither = findToscaElement(mappedToscaTemplate, type, ToscaElementTypeEnum.MAP); if (dataTypesEither != null && dataTypesEither.isLeft()) { return (Map) dataTypesEither.left().value(); diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeBusinessLogic.java index 401222861b..892536ffa7 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/DataTypeBusinessLogic.java @@ -40,12 +40,15 @@ import org.springframework.util.CollectionUtils; @org.springframework.stereotype.Component("dataTypeBusinessLogic") public class DataTypeBusinessLogic extends BaseBusinessLogic { + final DataTypeImportManager dataTypeImportManager; + @Autowired public DataTypeBusinessLogic(IElementOperation elementDao, IGroupOperation groupOperation, IGroupInstanceOperation groupInstanceOperation, IGroupTypeOperation groupTypeOperation, InterfaceOperation interfaceOperation, - InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation) { + InterfaceLifecycleOperation interfaceLifecycleTypeOperation, ArtifactsOperations artifactToscaOperation, DataTypeImportManager dataTypeImportManager) { super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation); + this.dataTypeImportManager = dataTypeImportManager; } /** @@ -139,4 +142,25 @@ public class DataTypeBusinessLogic extends BaseBusinessLogic { // return deleted data type if ok return Either.left(dataTypeResult.get()); } + + /** + * Creates given data types. The data types must be provided in a yaml format, where each entry is one data type object, for + * example: + *
+     * tosca.datatypes.TimeInterval:
+     *   derived_from: tosca.datatypes.Root
+     *   [...]
+     *
+     * tosca.datatypes.network.NetworkInfo:
+     *   derived_from: tosca.datatypes.Root
+     *   [...]
+     * 
+ * + * @param dataTypesYaml the data types to create in yaml format. It can contain multiple data types entries. + * @param model Model name to associate with data type + * @param includeToModelDefaultImports Add data type entry to default imports for model + */ + public void createDataTypeFromYaml(final String dataTypesYaml, final String model, final boolean includeToModelDefaultImports) { + dataTypeImportManager.createDataTypes(dataTypesYaml, model, includeToModelDefaultImports); + } } 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 c80f993a71..b318f97c72 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 @@ -44,35 +44,10 @@ 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, final DataTypeImportManager dataTypeImportManager) { + public ModelBusinessLogic(final ModelOperation modelOperation) { this.modelOperation = modelOperation; - this.dataTypeImportManager = dataTypeImportManager; - } - - /** - * Creates a model along with given data types. The data types must be provided in a yaml format, where each entry is one data type object, for - * example: - *
-     * tosca.datatypes.TimeInterval:
-     *   derived_from: tosca.datatypes.Root
-     *   [...]
-     *
-     * tosca.datatypes.network.NetworkInfo:
-     *   derived_from: tosca.datatypes.Root
-     *   [...]
-     * 
- * - * @param model the model to create - * @param datatypesYaml the data types to create in yaml format. It can contain multiple data types entries. - * @return the created model. - */ - public Model createModel(final Model model, final String datatypesYaml) { - createModel(model); - dataTypeImportManager.createDataTypes(datatypesYaml, model.getName(), true); - return model; } public Model createModel(final Model model) { diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeBusinessLogic.java b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeBusinessLogic.java index c7e6a98d92..224b01a04a 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeBusinessLogic.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/PolicyTypeBusinessLogic.java @@ -17,15 +17,18 @@ * limitations under the License. * ============LICENSE_END========================================================= */ + package org.openecomp.sdc.be.components.impl; import static java.util.Collections.emptySet; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Set; import org.apache.commons.lang3.StringUtils; import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException; +import org.openecomp.sdc.be.components.impl.model.ToscaTypeImportData; import org.openecomp.sdc.be.components.validation.UserValidations; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.api.ActionStatus; @@ -39,15 +42,18 @@ import org.springframework.stereotype.Component; @Component("policyTypeBusinessLogic") public class PolicyTypeBusinessLogic { - private PolicyTypeOperation policyTypeOperation; - private JanusGraphDao janusGraphDao; - private UserValidations userValidations; + private final PolicyTypeOperation policyTypeOperation; + private final JanusGraphDao janusGraphDao; + private final UserValidations userValidations; + private final PolicyTypeImportManager policyTypeImportManager; @Autowired - public PolicyTypeBusinessLogic(PolicyTypeOperation policyTypeOperation, JanusGraphDao janusGraphDao, UserValidations userValidations) { + public PolicyTypeBusinessLogic(final PolicyTypeOperation policyTypeOperation, final JanusGraphDao janusGraphDao, + final UserValidations userValidations, final PolicyTypeImportManager policyTypeImportManager) { this.policyTypeOperation = policyTypeOperation; this.janusGraphDao = janusGraphDao; this.userValidations = userValidations; + this.policyTypeImportManager = policyTypeImportManager; } public List getAllPolicyTypes(String userId, String internalComponentType, String modelName) { @@ -57,19 +63,31 @@ public class PolicyTypeBusinessLogic { } public PolicyTypeDefinition getLatestPolicyTypeByType(String policyTypeName, String modelName) { - return policyTypeOperation.getLatestPolicyTypeByType(policyTypeName, modelName).left().on(e -> failOnPolicyType(e, policyTypeName)); + return policyTypeOperation.getLatestPolicyTypeByType(policyTypeName, modelName).left() + .on(e -> failOnPolicyType(e, policyTypeName)); } public Set getExcludedPolicyTypes(String internalComponentType) { if (StringUtils.isEmpty(internalComponentType)) { return emptySet(); } - Map> excludedPolicyTypesMapping = ConfigurationManager.getConfigurationManager().getConfiguration() - .getExcludedPolicyTypesMapping(); + Map> excludedPolicyTypesMapping = + ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping(); Set excludedTypes = excludedPolicyTypesMapping.get(internalComponentType); return excludedTypes == null ? emptySet() : excludedTypes; } + /** + * Create policy types based on yaml string. + * @param policyTypesYaml the policy types to create in yaml format. It can contain multiple entries. + * @param model Model name to associate with policy type + * @param includeToModelDefaultImports Add policy type entry to default imports for model + */ + public void createPolicyTypeFromYaml(final String policyTypesYaml, final String model, final boolean includeToModelDefaultImports) { + policyTypeImportManager + .createPolicyTypes(new ToscaTypeImportData(policyTypesYaml, Collections.emptyMap()), model, includeToModelDefaultImports); + } + private List getPolicyTypes(Set excludedTypes, String modelName) { return policyTypeOperation.getAllPolicyTypes(excludedTypes, modelName); } 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 9ba20f46a1..f7beca6200 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 @@ -29,6 +29,7 @@ import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaStr import static org.openecomp.sdc.be.components.impl.ImportUtils.getPropertyJsonStringValue; import static org.openecomp.sdc.be.tosca.CsarUtils.VF_NODE_TYPE_ARTIFACTS_PATH_PATTERN; import static org.openecomp.sdc.common.api.Constants.DEFAULT_GROUP_VF_MODULE; + import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -46,6 +47,7 @@ import java.util.Set; import java.util.function.Function; import java.util.regex.Pattern; import java.util.stream.Collectors; + import org.apache.commons.codec.binary.Base64; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -124,6 +126,7 @@ import org.openecomp.sdc.be.model.NodeTypeInfo; import org.openecomp.sdc.be.model.Operation; import org.openecomp.sdc.be.model.ParsedToscaYamlInfo; import org.openecomp.sdc.be.model.PolicyDefinition; +import org.openecomp.sdc.be.model.PolicyTypeDefinition; import org.openecomp.sdc.be.model.PropertyDefinition; import org.openecomp.sdc.be.model.RelationshipImpl; import org.openecomp.sdc.be.model.RelationshipInfo; @@ -182,7 +185,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; + import com.google.common.annotations.VisibleForTesting; + import fj.data.Either; @org.springframework.stereotype.Component("resourceBusinessLogic") @@ -217,6 +222,9 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { private final ModelBusinessLogic modelBusinessLogic; private IInterfaceLifecycleOperation interfaceTypeOperation; private LifecycleBusinessLogic lifecycleBusinessLogic; + private final DataTypeBusinessLogic dataTypeBusinessLogic; + private final PolicyTypeBusinessLogic policyTypeBusinessLogic; + @Autowired private ICapabilityTypeOperation capabilityTypeOperation; @Autowired @@ -247,7 +255,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { final ComponentValidator componentValidator, final ComponentIconValidator componentIconValidator, final ComponentProjectCodeValidator componentProjectCodeValidator, final ComponentDescriptionValidator componentDescriptionValidator, final PolicyBusinessLogic policyBusinessLogic, - final ModelBusinessLogic modelBusinessLogic) { + final ModelBusinessLogic modelBusinessLogic, + final DataTypeBusinessLogic dataTypeBusinessLogic, final PolicyTypeBusinessLogic policyTypeBusinessLogic) { super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, groupBusinessLogic, interfaceOperation, interfaceLifecycleTypeOperation, artifactsBusinessLogic, artifactToscaOperation, componentContactIdValidator, componentNameValidator, componentTagsValidator, componentValidator, componentIconValidator, componentProjectCodeValidator, componentDescriptionValidator); @@ -264,6 +273,8 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { this.propertyBusinessLogic = propertyBusinessLogic; this.policyBusinessLogic = policyBusinessLogic; this.modelBusinessLogic = modelBusinessLogic; + this.dataTypeBusinessLogic = dataTypeBusinessLogic; + this.policyTypeBusinessLogic = policyTypeBusinessLogic; } static Either rollbackWithEither(final JanusGraphDao janusGraphDao, final ActionStatus actionStatus, @@ -1031,19 +1042,18 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { user.getUserId()); CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, csarUIPayload, csarUUID); 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)); - } + final String model = resource.getModel(); + if (StringUtils.isNotEmpty(model)) { + final Map dataTypesToCreate = getDatatypesToCreate(model, csarInfo.getDataTypes()); + final Map policyTypesToCreate = getPolicytypesToCreate(model, csarInfo.getPolicyTypes()); + if (MapUtils.isNotEmpty(dataTypesToCreate) || MapUtils.isNotEmpty(policyTypesToCreate)) { + createModel(resource, csarInfo.getVfResourceName()); } if (MapUtils.isNotEmpty(dataTypesToCreate)) { - final String nameForGeneratedModel = resource.getModel() + "_" + csarInfo.getVfResourceName() + resource.getCsarVersion(); - final Model model = new Model(nameForGeneratedModel, resource.getModel(), ModelTypeEnum.NORMATIVE_EXTENSION); - modelBusinessLogic.createModel(model, new Yaml().dump(dataTypesToCreate)); - resource.setModel(nameForGeneratedModel); + dataTypeBusinessLogic.createDataTypeFromYaml(new Yaml().dump(dataTypesToCreate), model, true); + } + if (MapUtils.isNotEmpty(policyTypesToCreate)) { + policyTypeBusinessLogic.createPolicyTypeFromYaml(new Yaml().dump(policyTypesToCreate), model, true); } } @@ -1148,6 +1158,37 @@ public class ResourceBusinessLogic extends ComponentBusinessLogic { return ImportUtils.findFirstToscaMapElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.NODE_TYPES).left().orValue(HashMap::new); } + private void createModel(final Resource resource, final String vfResourcename) { + final String nameForGeneratedModel = resource.getModel() + "_" + vfResourcename + resource.getCsarVersion(); + Model model = new Model(nameForGeneratedModel, resource.getModel(), ModelTypeEnum.NORMATIVE_EXTENSION); + modelBusinessLogic.createModel(model); + resource.setModel(nameForGeneratedModel); + } + + private Map getDatatypesToCreate(final String model, final Map dataTypes) { + final Map dataTypesToCreate = new HashMap<>(); + for (final String dataType : dataTypes.keySet()) { + final Either result = + propertyOperation.getDataTypeByName(dataType, model); + if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + dataTypesToCreate.put(dataType, dataTypes.get(dataType)); + } + } + return dataTypesToCreate; + } + + private Map getPolicytypesToCreate(final String model, final Map policyTypes) { + final Map policyTypesToCreate = new HashMap<>(); + for (final String policyType : policyTypes.keySet()) { + final Either result = + policyTypeOperation.getLatestPolicyTypeByType(policyType, model); + if (result.isRight() && result.right().value().equals(StorageOperationStatus.NOT_FOUND)) { + policyTypesToCreate.put(policyType, policyTypes.get(policyType)); + } + } + return policyTypesToCreate; + } + private void createNodeTypes(String yamlName, Resource resource, boolean needLock, Map>> nodeTypesArtifactsToHandle, List nodeTypesNewCreatedArtifacts, Map nodeTypesInfo, CsarInfo csarInfo, -- cgit 1.2.3-korg