summaryrefslogtreecommitdiffstats
path: root/catalog-model/src/main/java
diff options
context:
space:
mode:
authorMichaelMorris <michael.morris@est.tech>2022-08-19 09:36:41 +0100
committerVasyl Razinkov <vasyl.razinkov@est.tech>2022-09-02 10:22:01 +0000
commit97b5fa431d5924d90e97adedf76f3ce5648cd938 (patch)
tree40b200066f88dff83ae7773d30d2b37b42594d0d /catalog-model/src/main/java
parent7e84536e49b49cd1721b324d1ccaa17ca96456a7 (diff)
Support updated data types in service import
Signed-off-by: MichaelMorris <michael.morris@est.tech> Issue-ID: SDC-4140 Change-Id: Ib66d47a0f566b648722ce86cfc4e208880551a29
Diffstat (limited to 'catalog-model/src/main/java')
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java1
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java15
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java51
-rw-r--r--catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java44
4 files changed, 70 insertions, 41 deletions
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java
index bf9f4d79a6..f208891e21 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/DataTypeDefinition.java
@@ -50,6 +50,7 @@ public class DataTypeDefinition extends DataTypeDataDefinition {
this.setConstraints(dataTypeDefinition.getConstraints());
this.setDescription(dataTypeDefinition.getDescription());
this.setModel(dataTypeDefinition.getModel());
+ this.setProperties(dataTypeDefinition.getProperties());
}
public List<PropertyConstraint> safeGetConstraints() {
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java
index 0b4d02a0ab..338bbe15a9 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/cache/ApplicationDataTypeCache.java
@@ -198,6 +198,21 @@ public class ApplicationDataTypeCache implements ApplicationCache<DataTypeDefini
readWriteLock.readLock().unlock();
}
}
+
+ public void reload(final String model, final String uniqueId) {
+ final Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeDefEither = propertyOperation.getDataTypeByUid(uniqueId);
+ if (dataTypeDefEither.isLeft()) {
+ DataTypeDefinition dataTypeDef = dataTypeDefEither.left().value();
+ if (getDataTypeDefinitionMapByModel(model).containsKey(dataTypeDef.getName())) {
+ try {
+ readWriteLock.readLock().lock();
+ getDataTypeDefinitionMapByModel(model).put(dataTypeDef.getName(), dataTypeDef);
+ } finally {
+ readWriteLock.readLock().unlock();
+ }
+ }
+ }
+ }
private Map<String, DataTypeDefinition> getDataTypeDefinitionMapByModel(final String model) {
return dataTypesByModelCacheMap.containsKey(model) ? dataTypesByModelCacheMap.get(model) : new HashMap<>();
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
index 01e5cdcd40..0d462c9874 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/ModelOperation.java
@@ -26,12 +26,16 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
+import java.util.Set;
import java.util.stream.Collectors;
+import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
@@ -50,6 +54,7 @@ import org.openecomp.sdc.be.data.model.ToscaImportByModel;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
+import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.Model;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.ModelOperationExceptionSupplier;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.exception.OperationException;
@@ -379,4 +384,50 @@ public class ModelOperation {
this.modelElementOperation = modelElementOperation;
}
+ @SuppressWarnings("unchecked")
+ public void updateTypesInAdditionalTypesImport(final ElementTypeEnum elementTypeEnum, final String typesYaml, final String modelName) {
+ final Optional<ToscaImportByModel> additionalTypeDefinitionsImportOptional = getAdditionalTypes(modelName);
+
+ if (additionalTypeDefinitionsImportOptional.isPresent()) {
+
+ final Map<String, Object> existingTypeContent = getExistingTypes(elementTypeEnum, additionalTypeDefinitionsImportOptional.get());
+ final Set<String> existingTypeNames = existingTypeContent.keySet();
+
+ final Map<String, Object> typesToUpate = new HashMap<>();
+
+ final Map<String, Object> newTypesYaml = new Yaml().load(typesYaml);
+ newTypesYaml.entrySet().stream().filter(entry -> existingTypeNames.contains(entry.getKey())).forEach(newTypeToUpdate -> {
+
+ final Map<String, Object> propertiesInNewDef = (Map<String, Object>) ((Map<String, Object>) newTypeToUpdate.getValue()).get("properties");
+ final Map<String, Object> existingProperties =
+ (Map<String, Object>) ((Map<String, Object>) existingTypeContent.get(newTypeToUpdate.getKey())).get("properties");
+
+ final List<Entry<String, Object>> propertiesMissingFromNewDef = MapUtils.isEmpty(existingProperties) ? Collections.emptyList()
+ : existingProperties.entrySet().stream()
+ .filter(existingPropEntry -> !propertiesInNewDef.keySet().contains(existingPropEntry.getKey()))
+ .collect(Collectors.toList());
+
+ if (CollectionUtils.isNotEmpty(propertiesMissingFromNewDef)) {
+ typesToUpate.put(newTypeToUpdate.getKey(), newTypeToUpdate.getValue());
+
+ propertiesMissingFromNewDef
+ .forEach(existingPropToAdd -> propertiesInNewDef.put(existingPropToAdd.getKey(), existingPropToAdd.getValue()));
+ }
+ });
+ if (MapUtils.isNotEmpty(typesToUpate)) {
+ addTypesToDefaultImports(elementTypeEnum, new Yaml().dumpAsMap(typesToUpate), modelName);
+ }
+ }
+ }
+
+ private Optional<ToscaImportByModel> getAdditionalTypes(final String modelName) {
+ final List<ToscaImportByModel> modelImportList = toscaModelImportCassandraDao.findAllByModel(modelName);
+ return modelImportList.stream().filter(t -> ADDITIONAL_TYPE_DEFINITIONS_PATH.equals(Path.of(t.getFullPath()))).findAny();
+ }
+
+ private Map<String, Object> getExistingTypes(final ElementTypeEnum elementTypeEnum, final ToscaImportByModel additionalTypeDefinitionsImport) {
+ final Map<String, Object> existingContent = new Yaml().load(additionalTypeDefinitionsImport.getContent());
+ return (Map<String, Object>) existingContent.get(elementTypeEnum.getToscaEntryName());
+ }
+
}
diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
index 7546e90a82..b5215dc848 100644
--- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
+++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/operations/impl/PropertyOperation.java
@@ -1822,13 +1822,13 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
String oldDerivedFromName = oldDataTypeDefinition.getDerivedFromName();
String dataTypeName = newDataTypeDefinition.getName();
List<PropertyDefinition> propertiesToAdd = new ArrayList<>();
- if (isPropertyOmitted(newProperties, oldProperties, dataTypeName) || isPropertyTypeChanged(dataTypeName, newProperties, oldProperties,
- propertiesToAdd) || isDerivedFromNameChanged(dataTypeName, newDerivedFromName, oldDerivedFromName)) {
+ if (isPropertyTypeChanged(dataTypeName, newProperties, oldProperties, propertiesToAdd)
+ || isDerivedFromNameChanged(dataTypeName, newDerivedFromName, oldDerivedFromName)) {
log.debug("The new data type {} is invalid.", dataTypeName);
result = Either.right(StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY);
return result;
}
- if (propertiesToAdd == null || propertiesToAdd.isEmpty()) {
+ if (CollectionUtils.isEmpty(propertiesToAdd)) {
log.debug("No new properties has been defined in the new data type {}", newDataTypeDefinition);
result = Either.right(StorageOperationStatus.OK);
return result;
@@ -1935,44 +1935,6 @@ public class PropertyOperation extends AbstractOperation implements IPropertyOpe
return entryType;
}
- private boolean isPropertyOmitted(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, String dataTypeName) {
- boolean isValid = validateChangeInCaseOfEmptyProperties(newProperties, oldProperties, dataTypeName);
- if (!isValid) {
- log.debug("At least one property is missing in the new data type {}", dataTypeName);
- return false;
- }
- if (newProperties != null && oldProperties != null) {
- List<String> newProps = newProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
- List<String> oldProps = oldProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
- if (!newProps.containsAll(oldProps)) {
- StringJoiner joiner = new StringJoiner(",", "[", "]");
- newProps.forEach(joiner::add);
- log.debug("Properties {} in data type {} are missing, but they already defined in the existing data type", joiner.toString(),
- dataTypeName);
- return true;
- }
- }
- return false;
- }
-
- private boolean validateChangeInCaseOfEmptyProperties(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties,
- String dataTypeName) {
- if (newProperties != null) {
- if (newProperties.isEmpty()) {
- newProperties = null;
- }
- }
- if (oldProperties != null) {
- if (oldProperties.isEmpty()) {
- oldProperties = null;
- }
- }
- if ((newProperties == null && oldProperties == null) || (newProperties != null && oldProperties != null)) {
- return true;
- }
- return false;
- }
-
private boolean isDerivedFromNameChanged(String dataTypeName, String newDerivedFromName, String oldDerivedFromName) {
if (newDerivedFromName != null) {
boolean isEqual = newDerivedFromName.equals(oldDerivedFromName);