diff options
author | davsad <david.sadlier@est.tech> | 2021-11-01 16:35:05 +0000 |
---|---|---|
committer | Andr� Schmid <andre.schmid@est.tech> | 2021-11-02 11:18:22 +0000 |
commit | 31bfab860800308a86430bd03628c3ea17cd9115 (patch) | |
tree | a52672c84fe7195deb73a81c77f76c4b4f06f71d /catalog-be/src/main | |
parent | 453eb2bd29e65a9f680580ab39b70ed361a80520 (diff) |
Import VSP with non-standard policy types
Issue-ID: SDC-3715
Signed-off-by: davsad <david.sadlier@est.tech>
Change-Id: I48e9831b8a27341db2d33d85125cb9d42043e71b
Diffstat (limited to 'catalog-be/src/main')
5 files changed, 117 insertions, 50 deletions
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<String, Resource> createdNodes; private Map<String, Object> datatypeDefinitions; + private Map<String, Object> policytypeDefinitions; private List<Map.Entry<String, byte[]>> globalSubstitutes; @@ -195,16 +196,24 @@ public class CsarInfo { for (Map.Entry<String, byte[]> entry : globalSubstitutes) { final String yamlFileContents = new String(entry.getValue()); final Map<String, Object> 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<String, Object> getPolicyTypes() { + if (policytypeDefinitions == null) { + policytypeDefinitions = new HashMap<>(); + policytypeDefinitions.putAll(getTypesFromTemplate(mappedToscaMainTemplate, TypeUtils.ToscaTagNamesEnum.POLICY_TYPES)); + } + return policytypeDefinitions; + } @SuppressWarnings("unchecked") - private Map<String, Object> getDataTypesFromTemplate(final Map<String, Object> mappedToscaTemplate) { - final Either<Object, ResultStatusEnum> dataTypesEither = findToscaElement(mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum.DATA_TYPES, + private Map<String, Object> getTypesFromTemplate(final Map<String, Object> mappedToscaTemplate, TypeUtils.ToscaTagNamesEnum type) { + final Either<Object, ResultStatusEnum> dataTypesEither = findToscaElement(mappedToscaTemplate, type, ToscaElementTypeEnum.MAP); if (dataTypesEither != null && dataTypesEither.isLeft()) { return (Map<String, Object>) 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: + * <pre> + * tosca.datatypes.TimeInterval: + * derived_from: tosca.datatypes.Root + * [...] + * + * tosca.datatypes.network.NetworkInfo: + * derived_from: tosca.datatypes.Root + * [...] + * </pre> + * + * @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: - * <pre> - * tosca.datatypes.TimeInterval: - * derived_from: tosca.datatypes.Root - * [...] - * - * tosca.datatypes.network.NetworkInfo: - * derived_from: tosca.datatypes.Root - * [...] - * </pre> - * - * @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<PolicyTypeDefinition> 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<String> getExcludedPolicyTypes(String internalComponentType) { if (StringUtils.isEmpty(internalComponentType)) { return emptySet(); } - Map<String, Set<String>> excludedPolicyTypesMapping = ConfigurationManager.getConfigurationManager().getConfiguration() - .getExcludedPolicyTypesMapping(); + Map<String, Set<String>> excludedPolicyTypesMapping = + ConfigurationManager.getConfigurationManager().getConfiguration().getExcludedPolicyTypesMapping(); Set<String> 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<PolicyTypeDefinition> getPolicyTypes(Set<String> 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 <T> Either<T, RuntimeException> 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<String, NodeTypeInfo> nodeTypesInfo = csarInfo.extractTypesInfo(); - if (StringUtils.isNotEmpty(resource.getModel())) { - final Map<String, Object> dataTypesToCreate = new HashMap<>(); - for (final String dataType: csarInfo.getDataTypes().keySet()) { - final Either<DataTypeDefinition, StorageOperationStatus> 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<String, Object> dataTypesToCreate = getDatatypesToCreate(model, csarInfo.getDataTypes()); + final Map<String, Object> 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<String, Object> getDatatypesToCreate(final String model, final Map<String, Object> dataTypes) { + final Map<String, Object> dataTypesToCreate = new HashMap<>(); + for (final String dataType : dataTypes.keySet()) { + final Either<DataTypeDefinition, StorageOperationStatus> 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<String, Object> getPolicytypesToCreate(final String model, final Map<String, Object> policyTypes) { + final Map<String, Object> policyTypesToCreate = new HashMap<>(); + for (final String policyType : policyTypes.keySet()) { + final Either<PolicyTypeDefinition, StorageOperationStatus> 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<String, EnumMap<ArtifactOperationEnum, List<ArtifactDefinition>>> nodeTypesArtifactsToHandle, List<ArtifactDefinition> nodeTypesNewCreatedArtifacts, Map<String, NodeTypeInfo> nodeTypesInfo, CsarInfo csarInfo, |