diff options
author | andre.schmid <andre.schmid@est.tech> | 2022-03-07 18:48:09 +0000 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-03-11 16:48:13 +0000 |
commit | e5488e5e3623646125802b8ab7e12b7159a2c0d3 (patch) | |
tree | 58c896b9d2f434041cff1cafad7835dd9cd691f3 /catalog-be/src | |
parent | f13f58eb867c763e6ed1c3b674fd99b1081a0664 (diff) |
Support complex types in artifact properties
Adds support to complex types in artifact properties of an interface
operation implementation.
Change-Id: I7a82a3652541b35230fe4ce87bf703a1dbe72d50
Issue-ID: SDC-3899
Signed-off-by: andre.schmid <andre.schmid@est.tech>
Diffstat (limited to 'catalog-be/src')
4 files changed, 170 insertions, 39 deletions
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java index 64e61699ed..b689959572 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverter.java @@ -22,6 +22,7 @@ import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; import com.google.gson.Gson; import java.util.Collections; import java.util.HashMap; @@ -38,6 +39,7 @@ import org.apache.commons.lang3.math.NumberUtils; import org.openecomp.sdc.be.datatypes.elements.InputDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; +import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.ComponentInstance; import org.openecomp.sdc.be.model.DataTypeDefinition; @@ -53,6 +55,8 @@ import org.openecomp.sdc.be.tosca.model.ToscaInterfaceOperationImplementation; import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaProperty; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignment; +import org.openecomp.sdc.be.tosca.model.ToscaPropertyAssignmentJsonSerializer; import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil; import org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum; import org.openecomp.sdc.tosca.datatypes.ToscaFunctions; @@ -147,12 +151,8 @@ public class InterfacesOperationsConverter { return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1); } - private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) { - final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation()); - if (isImplementationPresent) { - return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName()); - } - return false; + private static boolean isArtifactPresent(final OperationDataDefinition operationDataDefinition) { + return operationDataDefinition.getImplementation() != null && operationDataDefinition.getImplementation().getArtifactName() != null; } private static String getInputValue(final OperationInputDefinition input) { @@ -178,13 +178,22 @@ public class InterfacesOperationsConverter { return interfaceType; } - private static Map<String, Object> getObjectAsMap(Object obj) { - ObjectMapper objectMapper = new ObjectMapper(); - if (obj instanceof ToscaInterfaceDefinition) { - //Prevent empty field serialization in interface definition - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + private static Map<String, Object> getObjectAsMap(final Object obj) { + final Map<String, Object> objectAsMap; + if (obj instanceof Map) { + objectAsMap = (Map<String, Object>) obj; + } else { + final ObjectMapper objectMapper = new ObjectMapper(); + final SimpleModule module = new SimpleModule("ToscaPropertyAssignmentSerializer"); + module.addSerializer(ToscaPropertyAssignment.class, new ToscaPropertyAssignmentJsonSerializer()); + objectMapper.registerModule(module); + if (obj instanceof ToscaInterfaceDefinition) { + //Prevent empty field serialization in interface definition + objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); + } + objectAsMap = objectMapper.convertValue(obj, Map.class); } - Map<String, Object> objectAsMap = obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class); + final String defaultEntry = DEFAULT.getElementName(); if (objectAsMap.containsKey(defaultEntry)) { objectAsMap.put(DEFAULT_HAS_UNDERSCORE, objectAsMap.remove(defaultEntry)); @@ -263,10 +272,10 @@ public class InterfacesOperationsConverter { final Map<String, Object> toscaOperationMap = new HashMap<>(); for (final Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) { final ToscaLifecycleOperationDefinition toscaLifecycleOperationDefinition = new ToscaLifecycleOperationDefinition(); - handleInterfaceOperationImplementation(component, componentInstance, isAssociatedComponent, operationEntry, - toscaLifecycleOperationDefinition); + handleInterfaceOperationImplementation(component, componentInstance, isAssociatedComponent, operationEntry.getValue(), + toscaLifecycleOperationDefinition, dataTypes); toscaLifecycleOperationDefinition.setDescription(operationEntry.getValue().getDescription()); - fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaLifecycleOperationDefinition, isServiceProxyInterface); + fillToscaOperationInputs(operationEntry.getValue(), dataTypes, toscaLifecycleOperationDefinition); toscaOperationMap.put(operationEntry.getValue().getName(), toscaLifecycleOperationDefinition); } toscaInterfaceDefinition.setOperations(toscaOperationMap); @@ -286,37 +295,56 @@ public class InterfacesOperationsConverter { private void handleInterfaceOperationImplementation(final Component component, final ComponentInstance componentInstance, final boolean isAssociatedComponent, - final Entry<String, OperationDataDefinition> operationEntry, - final ToscaLifecycleOperationDefinition toscaOperation) { + final OperationDataDefinition operationDataDefinition, + final ToscaLifecycleOperationDefinition toscaOperation, + final Map<String, DataTypeDefinition> dataTypes) { final String operationArtifactPath; final ToscaInterfaceOperationImplementation toscaInterfaceOperationImplementation = new ToscaInterfaceOperationImplementation(); toscaInterfaceOperationImplementation.setPrimary(new ToscaArtifactDefinition()); final ToscaArtifactDefinition toscaArtifactDefinition = toscaInterfaceOperationImplementation.getPrimary(); - if (isArtifactPresent(operationEntry) && StringUtils.isNotEmpty(operationEntry.getValue().getImplementation().getArtifactName())) { + if (isArtifactPresent(operationDataDefinition) && StringUtils.isNotEmpty(operationDataDefinition.getImplementation().getArtifactName())) { operationArtifactPath = OperationArtifactUtil - .createOperationArtifactPath(component, componentInstance, operationEntry.getValue(), isAssociatedComponent); + .createOperationArtifactPath(component, componentInstance, operationDataDefinition, isAssociatedComponent); toscaArtifactDefinition.setFile(operationArtifactPath); - toscaArtifactDefinition.setArtifact_version(!operationEntry.getValue().getImplementation().getArtifactVersion() - .equals(NumberUtils.INTEGER_ZERO.toString()) ? operationEntry.getValue().getImplementation().getArtifactVersion() : null); - toscaArtifactDefinition.setType(operationEntry.getValue().getImplementation().getArtifactType()); - handleInterfaceOperationImplementationProperties(operationEntry, toscaArtifactDefinition); + toscaArtifactDefinition.setArtifact_version(!operationDataDefinition.getImplementation().getArtifactVersion() + .equals(NumberUtils.INTEGER_ZERO.toString()) ? operationDataDefinition.getImplementation().getArtifactVersion() : null); + toscaArtifactDefinition.setType(operationDataDefinition.getImplementation().getArtifactType()); + final Map<String, ToscaPropertyAssignment> propertiesMap = handleImplementationProperties(operationDataDefinition, dataTypes); + if (!propertiesMap.isEmpty()) { + toscaArtifactDefinition.setProperties(propertiesMap); + } toscaOperation.setImplementation( toscaArtifactDefinition.getType() != null ? toscaInterfaceOperationImplementation : operationArtifactPath); } else { - toscaArtifactDefinition.setFile(operationEntry.getValue().getImplementation().getArtifactName()); + toscaArtifactDefinition.setFile(operationDataDefinition.getImplementation().getArtifactName()); toscaOperation.setImplementation(toscaInterfaceOperationImplementation); } } - private void handleInterfaceOperationImplementationProperties(final Entry<String, OperationDataDefinition> operationEntry, - final ToscaArtifactDefinition toscaArtifactDefinition) { - final var properties = operationEntry.getValue().getImplementation().getProperties(); - if (CollectionUtils.isNotEmpty(properties)) { - final Map<String, String> propertiesMap = new HashMap<>(); - properties.stream().filter(propertyDataDefinition -> StringUtils.isNotEmpty(propertyDataDefinition.getValue())) - .forEach(propertyDataDefinition -> propertiesMap.put(propertyDataDefinition.getName(), propertyDataDefinition.getValue())); - toscaArtifactDefinition.setProperties(propertiesMap); + private Map<String, ToscaPropertyAssignment> handleImplementationProperties(final OperationDataDefinition operationDataDefinition, + final Map<String, DataTypeDefinition> dataTypes) { + if (operationDataDefinition.getImplementation() == null) { + return new HashMap<>(); } + + final List<PropertyDataDefinition> properties = operationDataDefinition.getImplementation().getProperties(); + if (CollectionUtils.isEmpty(properties)) { + return new HashMap<>(); + } + + final Map<String, ToscaPropertyAssignment> propertiesMap = new HashMap<>(); + properties.stream() + .filter(propertyDataDefinition -> StringUtils.isNotEmpty(propertyDataDefinition.getValue())) + .forEach(propertyDataDefinition -> { + final String propertyValue = + propertyDataDefinition.getValue() != null ? propertyDataDefinition.getValue() : propertyDataDefinition.getDefaultValue(); + final ToscaPropertyAssignment toscaPropertyAssignment = new ToscaPropertyAssignment(); + toscaPropertyAssignment.setValue(propertyConvertor.convertToToscaObject(propertyDataDefinition, propertyValue, dataTypes, false)); + propertiesMap.put(propertyDataDefinition.getName(), toscaPropertyAssignment); + } + ); + + return propertiesMap; } public void removeInterfacesWithoutOperations(final Map<String, Object> interfaceMap) { @@ -373,7 +401,7 @@ public class InterfacesOperationsConverter { } private void fillToscaOperationInputs(OperationDataDefinition operation, Map<String, DataTypeDefinition> dataTypes, - ToscaLifecycleOperationDefinition toscaOperation, boolean isServiceProxyInterface) { + ToscaLifecycleOperationDefinition toscaOperation) { if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) { toscaOperation.setInputs(null); return; @@ -389,4 +417,5 @@ public class InterfacesOperationsConverter { } toscaOperation.setInputs(toscaInputs); } + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaArtifactDefinition.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaArtifactDefinition.java index 689dc91d23..ac306073c1 100644 --- a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaArtifactDefinition.java +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaArtifactDefinition.java @@ -36,5 +36,6 @@ public class ToscaArtifactDefinition { private String artifact_version; private String checksum; private String checksum_algorithm; - private Map<String, String> properties; + private Map<String, ToscaPropertyAssignment> properties; + } diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignmentJsonSerializer.java b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignmentJsonSerializer.java new file mode 100644 index 0000000000..9c695f6924 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/tosca/model/ToscaPropertyAssignmentJsonSerializer.java @@ -0,0 +1,42 @@ +/* + * - + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.sdc.be.tosca.model; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.std.StdSerializer; +import java.io.IOException; + +/** + * Jackson JSON serializer for the ToscaPropertyAssignment class + */ +public class ToscaPropertyAssignmentJsonSerializer extends StdSerializer<ToscaPropertyAssignment> { + + public ToscaPropertyAssignmentJsonSerializer() { + super(ToscaPropertyAssignment.class); + } + + @Override + public void serialize(ToscaPropertyAssignment toscaPropertyAssignment, JsonGenerator gen, SerializerProvider provider) throws IOException { + gen.writeObject(toscaPropertyAssignment.getValue()); + } +}
\ No newline at end of file diff --git a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java index e55a92dd0b..e3e4590d0f 100644 --- a/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java +++ b/catalog-be/src/test/java/org/openecomp/sdc/be/tosca/InterfacesOperationsConverterTest.java @@ -66,6 +66,8 @@ import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition; import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition; import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition; +import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.model.Component; import org.openecomp.sdc.be.model.DataTypeDefinition; import org.openecomp.sdc.be.model.InputDefinition; @@ -73,6 +75,7 @@ import org.openecomp.sdc.be.model.InterfaceDefinition; import org.openecomp.sdc.be.model.Resource; import org.openecomp.sdc.be.model.Service; import org.openecomp.sdc.be.model.ServiceMetadataDefinition; +import org.openecomp.sdc.be.model.tosca.ToscaType; import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition; import org.openecomp.sdc.be.tosca.model.ToscaNodeType; import org.openecomp.sdc.be.tosca.model.ToscaTemplate; @@ -366,10 +369,10 @@ class InterfacesOperationsConverterTest { @Test void interfaceWithInputsToscaExportTest() { final Component component = new Service(); - final InterfaceDefinition aInterfaceWithInput = new InterfaceDefinition(); + final InterfaceDefinition anInterfaceWithInput = new InterfaceDefinition(); final String interfaceName = "myInterfaceName"; final String interfaceType = "my.type." + interfaceName; - aInterfaceWithInput.setType(interfaceType); + anInterfaceWithInput.setType(interfaceType); final String input1Name = "input1"; final InputDataDefinition input1 = createInput("string", "input1 description", false, "input1 value"); final String input2Name = "input2"; @@ -377,9 +380,9 @@ class InterfacesOperationsConverterTest { final Map<String, InputDataDefinition> inputMap = new HashMap<>(); inputMap.put(input1Name, input1); inputMap.put(input2Name, input2); - aInterfaceWithInput.setInputs(inputMap); + anInterfaceWithInput.setInputs(inputMap); component.setInterfaces(new HashMap<>()); - component.getInterfaces().put(interfaceName, aInterfaceWithInput); + component.getInterfaces().put(interfaceName, anInterfaceWithInput); final ToscaNodeType nodeType = new ToscaNodeType(); interfacesOperationsConverter.addInterfaceDefinitionElement(component, nodeType, dataTypes, false); final ToscaExportHandler handler = new ToscaExportHandler(null, null, null, null, null, null, null, null, null, null, @@ -395,6 +398,62 @@ class InterfacesOperationsConverterTest { validateInterfaceInputs(toscaTemplateYaml, interfaceName, inputMap); } + @Test + void interfaceWithOperationImplementationArtifactPropertiesTest() { + //given + final Component component = new Service(); + final InterfaceDefinition interfaceDefinition = new InterfaceDefinition(); + final String interfaceName = "myInterfaceName"; + interfaceDefinition.setType("my.type." + interfaceName); + final var operation1DataDefinition = new OperationDataDefinition(); + operation1DataDefinition.setName("anOperation"); + + final PropertyDataDefinition listOfStringProperty = new PropertyDataDefinition(); + listOfStringProperty.setName("listProperty"); + listOfStringProperty.setType(ToscaType.LIST.getType()); + final PropertyDataDefinition listOfStringSchemaProperty = new PropertyDataDefinition(); + listOfStringSchemaProperty.setType(ToscaType.STRING.getType()); + final SchemaDefinition listPropertySchema = new SchemaDefinition(); + listPropertySchema.setProperty(listOfStringProperty); + listOfStringProperty.setSchema(listPropertySchema); + listOfStringProperty.setValue("[ \"value1\", \"value2\", \"value3\" ]"); + final ArrayList<Object> propertyList = new ArrayList<>(); + propertyList.add(listOfStringProperty); + final HashMap<String, Object> artifactDefinitionMapInitializer = new HashMap<>(); + artifactDefinitionMapInitializer.put(JsonPresentationFields.PROPERTIES.getPresentation(), propertyList); + final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition(artifactDefinitionMapInitializer); + artifactDataDefinition.setArtifactName("artifact1"); + artifactDataDefinition.setArtifactType("my.artifact.Type"); + operation1DataDefinition.setImplementation(artifactDataDefinition); + interfaceDefinition.setOperations(Map.of(operation1DataDefinition.getName(), operation1DataDefinition)); + component.setInterfaces(new HashMap<>()); + component.getInterfaces().put(interfaceName, interfaceDefinition); + //when + Map<String, Object> interfacesMap = interfacesOperationsConverter + .getInterfacesMap(component, null, component.getInterfaces(), null, false, true); + //then + assertTrue(interfacesMap.containsKey(interfaceName)); + final Map<String, Object> actualInterfaceMap = (Map<String, Object>) interfacesMap.get(interfaceName); + assertTrue(actualInterfaceMap.containsKey(operation1DataDefinition.getName())); + final Map<String, Object> actualOperationMap = (Map<String, Object>) actualInterfaceMap.get(operation1DataDefinition.getName()); + assertTrue(actualOperationMap.containsKey("implementation")); + final Map<String, Object> actualImplementationMap = (Map<String, Object>) actualOperationMap.get("implementation"); + assertTrue(actualImplementationMap.containsKey("primary")); + final Map<String, Object> actualArtifactImplementationMap = (Map<String, Object>) actualImplementationMap.get("primary"); + assertTrue(actualArtifactImplementationMap.containsKey("properties")); + final Map<String, Object> actualArtifactPropertiesMap = (Map<String, Object>) actualArtifactImplementationMap.get("properties"); + assertEquals(actualArtifactPropertiesMap.keySet().size(), 1); + assertTrue(actualArtifactPropertiesMap.containsKey(listOfStringProperty.getName())); + final Object expectedListObject = actualArtifactPropertiesMap.get(listOfStringProperty.getName()); + assertTrue(expectedListObject instanceof List); + final List<String> expectedListOfStringPropValue = (List<String>) expectedListObject; + assertEquals(expectedListOfStringPropValue.size(), 3); + assertTrue(expectedListOfStringPropValue.contains("value1")); + assertTrue(expectedListOfStringPropValue.contains("value2")); + assertTrue(expectedListOfStringPropValue.contains("value3")); + } + + private void validateInterfaceInputs(final String yaml, final String interfaceName, final Map<String, InputDataDefinition> expectedInputMap) { String fixedMainYaml = yaml; final String nullString = "null"; @@ -404,7 +463,7 @@ class InterfacesOperationsConverterTest { if (fixedMainYaml.endsWith(nullString)) { fixedMainYaml = fixedMainYaml.substring(0, fixedMainYaml.length() - nullString.length()); } - final Map<String, Object> yamlMap = (Map<String, Object>) new Yaml().load(fixedMainYaml); + final Map<String, Object> yamlMap = new Yaml().load(fixedMainYaml); final Map<String, Object> nodeTypesMap = (Map<String, Object>) yamlMap.get(NODE_TYPES.getElementName()); final Map<String, Object> node = (Map<String, Object>) nodeTypesMap.get(NODE_TYPE_NAME); final Map<String, Object> interfacesMap = (Map<String, Object>) node.get(INTERFACES.getElementName()); |