From f5854fd32c19c44d32a3e6739b30271d4dccd393 Mon Sep 17 00:00:00 2001 From: Avi Ziv Date: Mon, 31 Jul 2017 15:50:46 +0300 Subject: [SDC] code rebase for sdc resync to LF Change-Id: If6d87c9e324f508a8a6b80b10a03d1843901472e Signed-off-by: Michael Lando --- .../model/jsontitan/operations/BaseOperation.java | 57 ++++--- .../operations/NodeTemplateOperation.java | 10 +- .../operations/TopologyTemplateOperation.java | 12 +- .../operations/ToscaElementLifecycleOperation.java | 6 + .../operations/ToscaElementOperation.java | 8 +- .../jsontitan/operations/ToscaOperationFacade.java | 43 ++++- .../sdc/be/model/tosca/ToscaFunctions.java | 19 +++ .../converters/DataTypePropertyConverter.java | 130 ++++++++++++++ .../model/tosca/converters/HeatJsonConverter.java | 13 +- .../tosca/converters/ToscaConverterUtils.java | 13 ++ .../tosca/converters/ToscaMapValueConverter.java | 18 +- .../converters/DataTypePropertyConverterTest.java | 187 +++++++++++++++++++++ 12 files changed, 458 insertions(+), 58 deletions(-) create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaFunctions.java create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverter.java create mode 100644 catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaConverterUtils.java create mode 100644 catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java (limited to 'catalog-model') diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/BaseOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/BaseOperation.java index 0b60a07a28..1726c397ad 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/BaseOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/BaseOperation.java @@ -53,8 +53,6 @@ import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -import org.openecomp.sdc.be.model.ComponentInstance; -import org.openecomp.sdc.be.model.GroupDefinition; import org.openecomp.sdc.be.model.User; import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum; import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus; @@ -1189,13 +1187,8 @@ public abstract class BaseOperation { } if (result == null) { toscaDataVertex = toscaDataVertexRes.left().value(); - existingToscaDataMap = (Map) getDeepElements(toscaDataVertexRes.left().value(), pathKeys); - for (String uniqueKey : uniqueKeys) { - result = removeToscaDataElement(toscaElement, edgeLabel, uniqueKey, toscaDataVertex, existingToscaDataMap); - if (result != StorageOperationStatus.OK) { - break; - } - } + existingToscaDataMap = getDeepElements(toscaDataVertexRes.left().value(), pathKeys); + result = deleteElementsFromDataVertex(toscaElement, edgeLabel, uniqueKeys, toscaDataVertex, existingToscaDataMap); } if (result == null) { result = StorageOperationStatus.OK; @@ -1203,6 +1196,19 @@ public abstract class BaseOperation { return result; } + private StorageOperationStatus deleteElementsFromDataVertex(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, List uniqueKeys, GraphVertex toscaDataVertex, Map existingToscaDataMap) { + StorageOperationStatus result; + for (String uniqueKey : uniqueKeys) { + result = removeKeyFromDataVertex(uniqueKey, existingToscaDataMap); + if (result != StorageOperationStatus.OK) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete tosca data element of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result); + break; + } + } + result = updateToscaDataElement(toscaElement, edgeLabel, toscaDataVertex); + return result; + } + /** * Deletes tosca data element belonging to tosca element according label * @@ -1229,7 +1235,6 @@ public abstract class BaseOperation { * @return */ public StorageOperationStatus deleteToscaDataElements(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, List uniqueKeys) { - StorageOperationStatus result = null; GraphVertex toscaDataVertex; Map existingToscaDataMap; @@ -1242,12 +1247,7 @@ public abstract class BaseOperation { if (result == null) { toscaDataVertex = toscaDataVertexRes.left().value(); existingToscaDataMap = (Map) toscaDataVertex.getJson(); - for (String uniqueKey : uniqueKeys) { - result = removeToscaDataElement(toscaElement, edgeLabel, uniqueKey, toscaDataVertex, existingToscaDataMap); - if (result != StorageOperationStatus.OK) { - break; - } - } + result = deleteElementsFromDataVertex(toscaElement, edgeLabel, uniqueKeys, toscaDataVertex, existingToscaDataMap); } if (result == null) { result = StorageOperationStatus.OK; @@ -1255,23 +1255,24 @@ public abstract class BaseOperation { return result; } - private StorageOperationStatus removeToscaDataElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, String uniqueKey, GraphVertex toscaDataVertex, Map existingToscaDataMap) { - + private StorageOperationStatus updateToscaDataElement(GraphVertex toscaElement, EdgeLabelEnum edgeLabel, GraphVertex toscaDataVertex) { StorageOperationStatus result = StorageOperationStatus.OK; - if (!existingToscaDataMap.containsKey(uniqueKey)) { - result = StorageOperationStatus.NOT_FOUND; - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete tosca data element of the tosca element {} by label {}. Status is {}. ", toscaElement.getUniqueId(), edgeLabel, result); - } else { - existingToscaDataMap.remove(uniqueKey); - Either updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel); - if (updateOrCopyRes.isRight()) { - result = DaoStatusConverter.convertTitanStatusToStorageStatus(updateOrCopyRes.right().value()); - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to update tosca data {} of the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), result); - } + Either updateOrCopyRes = updateOrCopyOnUpdate(toscaDataVertex, toscaElement, edgeLabel); + if (updateOrCopyRes.isRight()) { + result = DaoStatusConverter.convertTitanStatusToStorageStatus(updateOrCopyRes.right().value()); + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to update tosca data {} of the tosca element {}. Status is {}. ", edgeLabel, toscaElement.getUniqueId(), result); } return result; } + private StorageOperationStatus removeKeyFromDataVertex(String uniqueKey, Map existingToscaDataMap) { + if (!existingToscaDataMap.containsKey(uniqueKey)) { + return StorageOperationStatus.NOT_FOUND; + } + existingToscaDataMap.remove(uniqueKey); + return StorageOperationStatus.OK; + } + protected StorageOperationStatus handleToscaData(GraphVertex toscaElement, VertexTypeEnum vertexLabel, EdgeLabelEnum edgeLabel, GraphVertex toscaDataVertex, Map mergedToscaDataMap) { StorageOperationStatus result = StorageOperationStatus.OK; diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperation.java index 66d26c748f..b642410548 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/NodeTemplateOperation.java @@ -1620,6 +1620,14 @@ public class NodeTemplateOperation extends BaseOperation { } public StorageOperationStatus addDeploymentArtifactsToInstance(String toscaElementId, String instanceId, Map instDeplArtifacts) { + return addArtifactsToInstance(toscaElementId, instanceId, instDeplArtifacts, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS); + } + + public StorageOperationStatus addInformationalArtifactsToInstance(String toscaElementId, String instanceId, Map instDeplArtifacts) { + return addArtifactsToInstance(toscaElementId, instanceId, instDeplArtifacts, EdgeLabelEnum.INSTANCE_ARTIFACTS, VertexTypeEnum.INSTANCE_ARTIFACTS); + } + + public StorageOperationStatus addArtifactsToInstance(String toscaElementId, String instanceId, Map instDeplArtifacts, EdgeLabelEnum edgeLabel, VertexTypeEnum vertexType) { Either metadataVertex = titanDao.getVertexById(toscaElementId, JsonParseFlagEnum.NoParse); if (metadataVertex.isRight()) { TitanOperationStatus status = metadataVertex.right().value(); @@ -1629,7 +1637,7 @@ public class NodeTemplateOperation extends BaseOperation { return DaoStatusConverter.convertTitanStatusToStorageStatus(status); } MapArtifactDataDefinition instArtifacts = new MapArtifactDataDefinition(instDeplArtifacts); - return addToscaDataDeepElementsBlockToToscaElement(metadataVertex.left().value(), EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, instArtifacts, instanceId); + return addToscaDataDeepElementsBlockToToscaElement(metadataVertex.left().value(),edgeLabel, vertexType, instArtifacts, instanceId); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java index 2d160bfc82..989707bf0c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/TopologyTemplateOperation.java @@ -349,9 +349,17 @@ public class TopologyTemplateOperation extends ToscaElementOperation { return StorageOperationStatus.OK; } - public StorageOperationStatus associateInstArtifactToComponent(GraphVertex nodeTypeVertex, Map instProps) { + public StorageOperationStatus associateInstDeploymentArtifactsToComponent(GraphVertex nodeTypeVertex, Map instArtifacts) { + return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS); + } + + public StorageOperationStatus associateInstArtifactsToComponent(GraphVertex nodeTypeVertex, Map instArtifacts) { + return associateInstanceArtifactsToComponent(nodeTypeVertex, instArtifacts, VertexTypeEnum.INSTANCE_ARTIFACTS, EdgeLabelEnum.INSTANCE_ARTIFACTS); + } + + private StorageOperationStatus associateInstanceArtifactsToComponent(GraphVertex nodeTypeVertex, Map instProps, VertexTypeEnum vertexType, EdgeLabelEnum edgeLabel) { if (instProps != null && !instProps.isEmpty()) { - Either assosiateElementToData = assosiateElementToData(nodeTypeVertex, VertexTypeEnum.INST_DEPLOYMENT_ARTIFACTS, EdgeLabelEnum.INST_DEPLOYMENT_ARTIFACTS, instProps); + Either assosiateElementToData = assosiateElementToData(nodeTypeVertex, vertexType, edgeLabel, instProps); if (assosiateElementToData.isRight()) { return assosiateElementToData.right().value(); } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementLifecycleOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementLifecycleOperation.java index d34d3aa9f9..51d122524c 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementLifecycleOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementLifecycleOperation.java @@ -766,6 +766,12 @@ public class ToscaElementLifecycleOperation extends BaseOperation { result = DaoStatusConverter.convertTitanStatusToStorageStatus(status); } } + if (result == null) { + Either updateRelationsRes = updateLastModifierEdge(toscaElement, owner, modifier); + if (updateRelationsRes.isRight()) { + result = updateRelationsRes.right().value(); + } + } if (result == null) { result = StorageOperationStatus.OK; } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperation.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperation.java index 5aceb2ffd2..708ef783f1 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperation.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaElementOperation.java @@ -1068,7 +1068,7 @@ public abstract class ToscaElementOperation extends BaseOperation { public Either, StorageOperationStatus> getElementCatalogData(ComponentTypeEnum componentType, List excludeTypes, boolean isHighestVersions) { Either, TitanOperationStatus> listOfComponents; if (isHighestVersions) { - listOfComponents = getListOfHighestComponents(componentType, excludeTypes); + listOfComponents = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.NoParse); } else { listOfComponents = getListOfHighestAndAllCertifiedComponents(componentType, excludeTypes); @@ -1095,7 +1095,7 @@ public abstract class ToscaElementOperation extends BaseOperation { return Either.left(result); } - private Either, TitanOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType, List excludeTypes) { + private Either, TitanOperationStatus> getListOfHighestComponents(ComponentTypeEnum componentType, List excludeTypes, JsonParseFlagEnum parseFlag) { Map propertiesToMatch = new HashMap<>(); Map propertiesHasNotToMatch = new HashMap<>(); propertiesToMatch.put(GraphPropertyEnum.COMPONENT_TYPE, componentType.name()); @@ -1107,13 +1107,13 @@ public abstract class ToscaElementOperation extends BaseOperation { } propertiesHasNotToMatch.put(GraphPropertyEnum.IS_DELETED, true); - return titanDao.getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, JsonParseFlagEnum.ParseMetadata); + return titanDao.getByCriteria(null, propertiesToMatch, propertiesHasNotToMatch, parseFlag); } // highest + (certified && !highest) public Either, TitanOperationStatus> getListOfHighestAndAllCertifiedComponents(ComponentTypeEnum componentType, List excludeTypes) { long startFetchAllStates = System.currentTimeMillis(); - Either, TitanOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes); + Either, TitanOperationStatus> highestNodes = getListOfHighestComponents(componentType, excludeTypes, JsonParseFlagEnum.ParseMetadata); Map propertiesToMatchCertified = new HashMap<>(); Map propertiesHasNotToMatchCertified = new HashMap<>(); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java index 45a1f2bd31..e3ed9d6471 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/jsontitan/operations/ToscaOperationFacade.java @@ -25,6 +25,7 @@ import fj.data.Either; import java.util.Map.Entry; 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; import org.apache.commons.lang3.tuple.Pair; @@ -1097,7 +1098,34 @@ public class ToscaOperationFacade { } - public StorageOperationStatus associateArtifactToInstances(Map> instArtifacts, String componentId, User user) { + public StorageOperationStatus associateDeploymentArtifactsToInstances(Map> instDeploymentArtifacts, String componentId, User user) { + + Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); + if (getVertexEither.isRight()) { + log.debug("Couldn't fetch component with and unique id {}, error: {}", componentId, getVertexEither.right().value()); + return DaoStatusConverter.convertTitanStatusToStorageStatus(getVertexEither.right().value()); + + } + + GraphVertex vertex = getVertexEither.left().value(); + Map instArtMap = new HashMap<>(); + if (instDeploymentArtifacts != null) { + + MapArtifactDataDefinition artifactsMap; + for (Entry> entry : instDeploymentArtifacts.entrySet()) { + Map artList = entry.getValue(); + Map artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); + artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME); + + instArtMap.put(entry.getKey(), artifactsMap); + } + } + + return topologyTemplateOperation.associateInstDeploymentArtifactsToComponent(vertex, instArtMap); + + } + + public StorageOperationStatus associateArtifactsToInstances(Map> instArtifacts, String componentId, User user) { Either getVertexEither = titanDao.getVertexById(componentId, JsonParseFlagEnum.NoParse); if (getVertexEither.isRight()) { @@ -1114,13 +1142,13 @@ public class ToscaOperationFacade { for (Entry> entry : instArtifacts.entrySet()) { Map artList = entry.getValue(); Map artifacts = artList.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); - artifactsMap = nodeTemplateOperation.prepareInstDeploymentArtifactPerInstance(artifacts, entry.getKey(), user, NodeTemplateOperation.HEAT_VF_ENV_NAME); + artifactsMap = new MapArtifactDataDefinition(artifacts); instArtMap.put(entry.getKey(), artifactsMap); } } - return topologyTemplateOperation.associateInstArtifactToComponent(vertex, instArtMap); + return topologyTemplateOperation.associateInstArtifactsToComponent(vertex, instArtMap); } @@ -1913,6 +1941,15 @@ public class ToscaOperationFacade { return nodeTemplateOperation.addDeploymentArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts); } + + public StorageOperationStatus addInformationalArtifactsToInstance(String componentId, ComponentInstance componentInstance, Map artifacts) { + StorageOperationStatus status = StorageOperationStatus.OK; + if(MapUtils.isNotEmpty(artifacts)){ + Map instDeplArtifacts = artifacts.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new ArtifactDataDefinition(e.getValue()))); + status= nodeTemplateOperation.addInformationalArtifactsToInstance(componentId, componentInstance.getUniqueId(), instDeplArtifacts); + } + return status; + } public StorageOperationStatus generateCustomizationUUIDOnInstance(String componentId, String instanceId) { return nodeTemplateOperation.generateCustomizationUUIDOnInstance(componentId, instanceId); diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaFunctions.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaFunctions.java new file mode 100644 index 0000000000..e6969bc5c2 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/ToscaFunctions.java @@ -0,0 +1,19 @@ +package org.openecomp.sdc.be.model.tosca; + +/** + * tosca functions supported by sdc + */ +public enum ToscaFunctions { + + GET_INPUT("get_input"); + + private String functionName; + + ToscaFunctions(String functionName) { + this.functionName = functionName; + } + + public String getFunctionName() { + return functionName; + } +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverter.java new file mode 100644 index 0000000000..2d389f5fe5 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverter.java @@ -0,0 +1,130 @@ +package org.openecomp.sdc.be.model.tosca.converters; + +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.google.gson.stream.JsonReader; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; +import org.openecomp.sdc.common.util.JsonUtils; + +import java.io.StringReader; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DataTypePropertyConverter { + + private static final DataTypePropertyConverter INSTANCE = new DataTypePropertyConverter(); + private static final JsonParser jsonParser = new JsonParser(); + private static final Gson gson = new Gson(); + + private DataTypePropertyConverter() { + } + + public static DataTypePropertyConverter getInstance() { + return INSTANCE; + } + + /** + * + * @param propertyDataType the data type + * @param dataTypes all data types in the system mapped by their name + * @return a json representation of all the given data type properties default values and recursively their data type properties + */ + public String getDataTypePropertiesDefaultValuesRec(String propertyDataType, Map dataTypes) { + JsonObject defaultValues = getDataTypePropsDefaultValuesRec(propertyDataType, dataTypes); + return !defaultValues.isJsonNull() ? gson.toJson(defaultValues) : null; + } + + /** + * Takes a json representation of a tosca property value and merges all the default values of the property data type + * @param value the json representation of a tosca property value + * @param propertyDataType data type from which to merge default values + * @param dataTypes all data types in the system mapped by their name + * for example: for value {a: {b: c}} we could have a default value {a: {d: e}} (property a has two sub properties but only b was overridden) + * so the complete value is {a: {b: c, d: e}} + */ + public void mergeDataTypeDefaultValuesWithPropertyValue(JsonObject value, String propertyDataType, Map dataTypes) { + JsonObject dataTypeDefaultValues = getDataTypePropsDefaultValuesRec(propertyDataType, dataTypes); + mergeDefaultValuesRec(dataTypeDefaultValues, value); + } + + private void mergeDefaultValuesRec(JsonObject defaultValue, JsonObject value) { + if (ToscaConverterUtils.isGetInputValue(value)) { + return; + } + for (Map.Entry defVal : defaultValue.entrySet()) { + mergeDefaultValue(value, defVal.getKey(), defVal.getValue()); + } + } + + private void mergeDefaultValue(JsonObject value, String propName, JsonElement defValue) { + if (defValueWasNotOverridden(value, propName)) { + value.add(propName, defValue); + } else if (notPrimitivePropValue(value, defValue, propName)) { + JsonElement propValue = value.get(propName); + mergeDefaultValuesRec(defValue.getAsJsonObject(), propValue.getAsJsonObject()); + } + } + + private boolean notPrimitivePropValue(JsonObject value, JsonElement defValue, String propName) { + return value.get(propName).isJsonObject() && defValue.isJsonObject(); + } + + private boolean defValueWasNotOverridden(JsonObject value, String propName) { + return !value.has(propName); + } + + private JsonObject getDataTypePropsDefaultValuesRec(String propertyDataType, Map dataTypes) { + Map allParentsProps = getAllDataTypeProperties(dataTypes.get(propertyDataType)); + JsonObject dataTypeDefaultsJson = new JsonObject(); + for (Map.Entry propertyEntry : allParentsProps.entrySet()) { + PropertyDefinition propertyDefinition = propertyEntry.getValue(); + addDefaultValueToJson(dataTypes, dataTypeDefaultsJson, propertyDefinition); + } + return dataTypeDefaultsJson; + } + + private void addDefaultValueToJson(Map dataTypes, JsonObject dataTypePropsDefaults, PropertyDefinition propertyDefinition) { + String propName = propertyDefinition.getName(); + JsonElement defVal = getDefaultValue(dataTypes, dataTypePropsDefaults, propertyDefinition); + if (!JsonUtils.isEmptyJson(defVal)) { + dataTypePropsDefaults.add(propName, defVal); + } + } + + private JsonElement getDefaultValue(Map dataTypes, JsonObject dataTypePropsDefaults, PropertyDefinition propertyDefinition) { + JsonElement defVal = new JsonObject(); + String propName = propertyDefinition.getName(); + String propDefaultVal = propertyDefinition.getDefaultValue(); + if(!JsonUtils.containsEntry(dataTypePropsDefaults, propName) && propDefaultVal != null){ + defVal = convertToJson(propDefaultVal); + } else if (!JsonUtils.containsEntry(dataTypePropsDefaults, propName)) { + defVal = getDataTypePropsDefaultValuesRec(propertyDefinition.getType(), dataTypes); + } + return defVal; + } + + private JsonElement convertToJson(String propDefaultVal) { + JsonReader jsonReader = new JsonReader(new StringReader(propDefaultVal)); + jsonReader.setLenient(true); + return jsonParser.parse(jsonReader); + } + + private Map getAllDataTypeProperties(DataTypeDefinition dataTypeDefinition) { + Map allParentsProps = new HashMap<>(); + while (dataTypeDefinition != null) { + + List currentParentsProps = dataTypeDefinition.getProperties(); + if (currentParentsProps != null) { + currentParentsProps.stream().forEach(p -> allParentsProps.put(p.getName(), p)); + } + + dataTypeDefinition = dataTypeDefinition.getDerivedFrom(); + } + return allParentsProps; + } + +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatJsonConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatJsonConverter.java index 6e077d60a1..9922c0f05d 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatJsonConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/HeatJsonConverter.java @@ -39,12 +39,15 @@ public class HeatJsonConverter implements PropertyValueConverter { @Override public String convert(String original, String innerType, Map dataTypes) { - String coverted = ValidationUtils.removeNoneUtf8Chars(original); - coverted = ValidationUtils.removeHtmlTagsOnly(coverted); - coverted = ValidationUtils.normaliseWhitespace(coverted); - coverted = ValidationUtils.stripOctets(coverted); + if (original == null) { + return null; + } + String converted = ValidationUtils.removeNoneUtf8Chars(original); + converted = ValidationUtils.removeHtmlTagsOnly(converted); + converted = ValidationUtils.normaliseWhitespace(converted); + converted = ValidationUtils.stripOctets(converted); // As opposed to string converter, keeping the " and ' symbols - return coverted; + return converted; } } diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaConverterUtils.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaConverterUtils.java new file mode 100644 index 0000000000..159addcbc9 --- /dev/null +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaConverterUtils.java @@ -0,0 +1,13 @@ +package org.openecomp.sdc.be.model.tosca.converters; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import org.openecomp.sdc.be.model.tosca.ToscaFunctions; + +public class ToscaConverterUtils { + + public static boolean isGetInputValue(JsonObject value) { + return value.get(ToscaFunctions.GET_INPUT.getFunctionName()) != null; + } + +} diff --git a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java index 0673033546..5b565bf62a 100644 --- a/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java +++ b/catalog-model/src/main/java/org/openecomp/sdc/be/model/tosca/converters/ToscaMapValueConverter.java @@ -193,22 +193,8 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T } JsonObject asJsonObjectIn = entryValue.getAsJsonObject(); - DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); - Map allProperties = getAllProperties(dataTypeDefinition); + DataTypePropertyConverter.getInstance().mergeDataTypeDefaultValuesWithPropertyValue(asJsonObjectIn, innerType, dataTypes); Map toscaObjectPresentation = new HashMap<>(); - - for (Entry entry : allProperties.entrySet()) { - String propName = entry.getKey(); - PropertyDefinition propertyDefinition = entry.getValue(); - JsonElement elementValue = asJsonObjectIn.get(propName); - if(elementValue == null && propertyDefinition.getDefaultValue() != null){ - JsonReader jsonReader = new JsonReader(new StringReader(propertyDefinition.getDefaultValue())); - jsonReader.setLenient(true); - elementValue = jsonParser.parse(jsonReader); - asJsonObjectIn.add(propName, elementValue); - } - } - Set> entrySetIn = asJsonObjectIn.entrySet(); for (Entry entry : entrySetIn) { @@ -217,6 +203,8 @@ public class ToscaMapValueConverter extends ToscaValueBaseConverter implements T JsonElement elementValue = entry.getValue(); Object convValue; if (isScalarF == false) { + DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType); + Map allProperties = getAllProperties(dataTypeDefinition); PropertyDefinition propertyDefinition = allProperties.get(propName); if (propertyDefinition == null) { log.trace("The property {} was not found under data type . Parse as map", propName); diff --git a/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java new file mode 100644 index 0000000000..84f2254bbb --- /dev/null +++ b/catalog-model/src/test/java/org/openecomp/sdc/be/model/tosca/converters/DataTypePropertyConverterTest.java @@ -0,0 +1,187 @@ +package org.openecomp.sdc.be.model.tosca.converters; + +import com.google.gson.JsonObject; +import org.junit.Before; +import org.junit.Test; +import org.openecomp.sdc.be.model.DataTypeDefinition; +import org.openecomp.sdc.be.model.PropertyDefinition; + +import javax.json.Json; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class DataTypePropertyConverterTest { + + private static final String EMPTY_JSON_STR = "{}"; + public static final String PROPERTY2_DEFAULT = "{\"prop1\":\"def1\",\"prop3\":\"def3\"}"; + private DataTypePropertyConverter testInstance = DataTypePropertyConverter.getInstance(); + private Map dataTypes; + private DataTypeDefinition noDefaultValue, dataType1, dataType2, dataType3; + private PropertyDefinition prop1, prop2, prop3, noDefaultProp; + + @Before + public void setUp() throws Exception { + dataTypes = new HashMap<>(); + + prop1 = new PropertyDefinition(); + prop1.setDefaultValue("def1"); + prop1.setName("prop1"); + + prop2 = new PropertyDefinition(); + prop2.setType("dataType1"); + prop2.setName("prop2"); + + prop3 = new PropertyDefinition(); + prop3.setDefaultValue("def3"); + prop3.setName("prop3"); + + noDefaultProp = new PropertyDefinition(); + noDefaultProp.setName("noDefaultProp"); + + noDefaultValue = new DataTypeDefinition(); + noDefaultValue.setProperties(Collections.singletonList(noDefaultProp)); + + dataType1 = new DataTypeDefinition(); + dataType1.setProperties(Arrays.asList(prop1, prop3)); + + dataType2 = new DataTypeDefinition(); + dataType2.setDerivedFrom(dataType1); + + dataType3 = new DataTypeDefinition(); + dataType3.setProperties(Collections.singletonList(prop2)); + dataType3.setDerivedFrom(noDefaultValue); + + dataTypes.put("noDefault", noDefaultValue); + dataTypes.put("dataType1", dataType1); + dataTypes.put("dataType2", dataType2); + dataTypes.put("dataType3", dataType3); + } + + @Test + public void testGetPropertyDefaultValuesRec_dataTypeNotExist() throws Exception { + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("someType", dataTypes); + assertEquals(EMPTY_JSON_STR, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec_NoDefaultValue() throws Exception { + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("noDefault", dataTypes); + assertEquals(EMPTY_JSON_STR, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec() throws Exception { + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType1", dataTypes); + assertEquals(PROPERTY2_DEFAULT, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType_derivedDataTypeHasNoDefaults() throws Exception { + dataType2.setDerivedFrom(noDefaultValue); + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); + assertEquals(EMPTY_JSON_STR, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec_defaultFromDerivedDataType() throws Exception { + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType2", dataTypes); + assertEquals(PROPERTY2_DEFAULT, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties_dataTypeOfPropertyHasNoDefault() throws Exception { + dataType3.getProperties().get(0).setType(noDefaultValue.getName()); + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); + assertEquals(EMPTY_JSON_STR, defaultValue); + } + + @Test + public void testGetPropertyDefaultValuesRec_defaultFromDataTypesOfProperties() throws Exception { + String defaultValue = testInstance.getDataTypePropertiesDefaultValuesRec("dataType3", dataTypes); + assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", defaultValue);//data type 3 has property prop2 which has a data type with property prop1 which has a default value + } + + @Test + public void testMergeDefaultValues_allDefaultValuesAreOverridden() throws Exception { + JsonObject value = new JsonObject(); + value.addProperty(noDefaultProp.getName(), "override1"); + + JsonObject prop1Val = new JsonObject(); + prop1Val.addProperty(prop1.getName(), "prop1Override"); + + JsonObject prop3Val = new JsonObject(); + prop3Val.addProperty(prop3.getName(), "prop3Override"); + + JsonObject prop2Value = new JsonObject(); + prop2Value.add(prop3.getName(), prop3Val); + prop2Value.add(prop1.getName(), prop1Val); + + value.add(prop2.getName(), prop2Value); + + String valBeforeMerge = value.toString(); + + testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + assertEquals(valBeforeMerge, value.toString()); + } + + @Test + public void testMergeDefaultValues() throws Exception { + JsonObject value = new JsonObject(); + value.addProperty(noDefaultProp.getName(), "override1"); + + JsonObject prop1Val = new JsonObject(); + prop1Val.addProperty(prop1.getName(), "prop1Override"); + + value.add(prop2.getName(), prop1Val); + + testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + + assertEquals("{\"noDefaultProp\":\"override1\",\"prop2\":{\"prop1\":\"prop1Override\",\"prop3\":\"def3\"}}", + value.toString());//expect to merge prop 3 default as it was not overridden + } + + @Test + public void testMergeDefaultValues_mergeAll() throws Exception { + JsonObject value = new JsonObject(); + testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + + assertEquals("{\"prop2\":" + PROPERTY2_DEFAULT + "}", + value.toString());//expect to merge prop 3 default as it was not overridden + } + + @Test + public void testMergeDefaultValues_doNotAddDefaultsForGetInputValues() throws Exception { + + JsonObject getInputValue = new JsonObject(); + getInputValue.addProperty("get_input", "in1"); + + JsonObject value = new JsonObject(); + value.add(prop2.getName(), getInputValue); + + testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + + assertEquals("{\"prop2\":{\"get_input\":\"in1\"}}", value.toString()); + } + + @Test + public void testMergeDefaultValues_doNotAddDefaultsForGetInputInnerValues() throws Exception { + JsonObject getInputValue = new JsonObject(); + getInputValue.addProperty("get_input", "in1"); + + JsonObject prop1Val = new JsonObject(); + prop1Val.add(prop1.getName(), getInputValue); + + JsonObject value = new JsonObject(); + value.add(prop2.getName(), prop1Val); + + testInstance.mergeDataTypeDefaultValuesWithPropertyValue(value, "dataType3", dataTypes); + + assertEquals("{\"prop2\":{\"prop1\":{\"get_input\":\"in1\"},\"prop3\":\"def3\"}}", value.toString()); + + } +} -- cgit 1.2.3-korg