Fix instance declared inputs mapped to substitution mapping 47/133547/4
authorKrupaNagabhushan <krupa.nagabhushan@est.tech>
Tue, 21 Feb 2023 14:22:14 +0000 (14:22 +0000)
committerMichael Morris <michael.morris@est.tech>
Thu, 9 Mar 2023 16:52:05 +0000 (16:52 +0000)
Issue-ID: SDC-4410
Signed-off-by: KrupaNagabhushan <krupa.nagabhushan@est.tech>
Change-Id: I7514f197628ace6aff22b51724b9acea0e21944f

catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/ServiceImportBusinessLogic.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/property/PropertyDeclarationOrchestrator.java
integration-tests/src/test/java/org/onap/sdc/frontend/ci/tests/execute/sanity/ServiceTemplateDesignUiTests.java

index 828439e..b3f1d57 100644 (file)
@@ -43,6 +43,7 @@ import java.util.Map.Entry;
 import java.util.Optional;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.regex.Pattern;
 import java.util.stream.Collectors;
@@ -74,7 +75,6 @@ import org.openecomp.sdc.be.dao.api.ActionStatus;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
 import org.openecomp.sdc.be.datamodel.utils.ArtifactUtils;
-import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.CustomYamlFunction;
 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
@@ -763,10 +763,9 @@ public class ServiceImportBusinessLogic {
         if (CollectionUtils.isNotEmpty(inputs)) {
             final List<ComponentInstance> componentInstances = component.getComponentInstances();
             final String componentUniqueId = component.getUniqueId();
-            final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties = component.getComponentInstancesProperties();
             for (final InputDefinition input : inputs) {
-                if (isInputFromComponentInstanceProperty(input.getName(), componentInstances, componentInstancesProperties)) {
-                    associateInputToComponentInstanceProperty(userId, input, componentInstances, componentInstancesProperties, componentUniqueId);
+                if (isInputFromComponentInstanceProperty(input.getName(), componentInstances)) {
+                    associateInputToComponentInstanceProperty(userId, input, componentInstances, componentUniqueId);
                 } else {
                     associateInputToServiceProperty(userId, input, component);
                 }
@@ -781,50 +780,49 @@ public class ServiceImportBusinessLogic {
         return component;
     }
 
-    private boolean isInputFromComponentInstanceProperty(final String inputName, final List<ComponentInstance> componentInstances,
-                                                         final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties) {
+    private boolean isInputFromComponentInstanceProperty(final String inputName, final List<ComponentInstance> componentInstances) {
+
+        AtomicBoolean isInputFromCIProp = new AtomicBoolean(false);
         if (CollectionUtils.isNotEmpty(componentInstances)) {
-            // get instance's names
-            final List<String> componentInstancesNames = componentInstances.stream().map(ComponentInstanceDataDefinition::getNormalizedName)
-                .collect(toList());
-            final Optional<String> componentInstancesNameOptional = componentInstancesNames.stream()
-                .filter(cin -> inputName.startsWith(cin + "_")).findFirst();
-            if (componentInstancesNameOptional.isPresent() && MapUtils.isNotEmpty(componentInstancesProperties)) {
-                final Optional<String> componentInstanceIdOptional = componentInstancesProperties.keySet().stream()
-                    .filter(key -> key.endsWith("." + componentInstancesNameOptional.get())).findFirst();
-                if (componentInstanceIdOptional.isPresent()) {
-                    // get property's name
-                    final String propertyNameFromInput = extractPropertyNameFromInputName(inputName, componentInstancesNames);
-                    return componentInstancesProperties.get(componentInstanceIdOptional.get()).stream()
-                        .anyMatch(prop -> prop.getName().equals(propertyNameFromInput) && prop.getValue() != null
-                            && prop.getValue().contains(ToscaGetFunctionType.GET_INPUT.getFunctionName()));
+            outer: for (ComponentInstance instance : componentInstances) {
+                for (PropertyDefinition instanceProperty : instance.getProperties()) {
+                    if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
+                        for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
+                            if (inputName.equals(getInputValueDataDefinition.getInputName())) {
+                                isInputFromCIProp.set(true);
+                                break outer;
+                            }
+                        }
+                    }
                 }
             }
         }
-        return false;
+        return isInputFromCIProp.get();
     }
 
     private void associateInputToComponentInstanceProperty(final String userId, final InputDefinition input,
                                                            final List<ComponentInstance> componentInstances,
-                                                           final Map<String, List<ComponentInstanceProperty>> componentInstancesProperties,
                                                            String componentUniqueId) {
-        // From Instance
-        final List<String> componentInstancesNames = componentInstances.stream().map(ComponentInstanceDataDefinition::getNormalizedName)
-            .collect(toList());
-        final String propertyNameFromInput = extractPropertyNameFromInputName(input.getName(), componentInstancesNames);
-
-        final Optional<String> componentInstancesNameOptional = componentInstancesNames.stream()
-            .filter(cin -> input.getName().startsWith(cin + "_")).findFirst();
-
-        final Optional<String> componentInstanceIdOptional = componentInstancesProperties.keySet().stream()
-            .filter(key -> key.endsWith("." + componentInstancesNameOptional.get())).findFirst();
 
-        final String componentInstanceId = componentInstanceIdOptional.get();
-        final List<ComponentInstanceProperty> componentInstanceProperties = componentInstancesProperties.get(componentInstanceId);
+        String componentInstanceId = null;
+        ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
+
+        outer: for (ComponentInstance instance : componentInstances) {
+            for (PropertyDefinition instanceProperty : instance.getProperties()) {
+                if (CollectionUtils.isNotEmpty(instanceProperty.getGetInputValues())) {
+                    for (GetInputValueDataDefinition getInputValueDataDefinition : instanceProperty.getGetInputValues()) {
+                        if (input.getName().equals(getInputValueDataDefinition.getInputName())) {
+                            componentInstanceId = instance.getUniqueId();
+                            componentInstanceProperty = new ComponentInstanceProperty(instanceProperty);
+                            break outer;
+                        }
+                    }
+                }
+            }
+        }
 
-        final ComponentInstanceProperty componentInstanceProperty = componentInstanceProperties.stream()
-            .filter(prop -> prop.getName().equals(propertyNameFromInput) && prop.getValue() != null
-                && prop.getValue().contains(ToscaGetFunctionType.GET_INPUT.getFunctionName())).findFirst().get();
+        //unmapping instance property declared inputs from substitution mapping
+        input.setMappedToComponentProperty(false);
 
         // From Instance
         updateInput(input, componentInstanceProperty, userId, componentInstanceId);
@@ -881,12 +879,6 @@ public class ServiceImportBusinessLogic {
         input.setParentPropertyType(propertyDefinition.getType());
     }
 
-    private String extractPropertyNameFromInputName(final String inputName, final List<String> componentInstancesNames) {
-        final AtomicReference<String> result = new AtomicReference<>(inputName);
-        componentInstancesNames.forEach(cin -> result.set(result.get().replace(cin + "_", "")));
-        return result.get();
-    }
-
     protected Either<Resource, ResponseFormat> createOrUpdateArtifacts(ArtifactsBusinessLogic.ArtifactOperationEnum operation,
                                                                        List<ArtifactDefinition> createdArtifacts, String yamlFileName,
                                                                        CsarInfo csarInfo, Resource preparedResource,
index e5367c4..2d00913 100644 (file)
@@ -28,6 +28,7 @@ import java.util.Map;
 import java.util.Optional;
 import java.util.stream.Collectors;
 import org.apache.commons.collections.CollectionUtils;
+import org.apache.commons.collections.MapUtils;
 import org.apache.commons.lang3.tuple.Pair;
 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentInstancePropertyToPolicyDeclarator;
 import org.openecomp.sdc.be.components.property.propertytopolicydeclarators.ComponentPropertyToPolicyDeclarator;
@@ -77,10 +78,19 @@ public class PropertyDeclarationOrchestrator {
                                                                                            ComponentInstInputsMap componentInstInputsMap) {
         updatePropertiesConstraints(component, componentInstInputsMap);
         PropertyDeclarator propertyDeclarator = getPropertyDeclarator(componentInstInputsMap);
+        undoCISubstitutionMapping(componentInstInputsMap);
         Pair<String, List<ComponentInstancePropInput>> propsToDeclare = componentInstInputsMap.resolvePropertiesToDeclare();
         return propertyDeclarator.declarePropertiesAsInputs(component, propsToDeclare.getLeft(), propsToDeclare.getRight());
     }
 
+    private void undoCISubstitutionMapping(ComponentInstInputsMap componentInstInputsMap) {
+        if (MapUtils.isNotEmpty(componentInstInputsMap.getComponentInstanceProperties())) {
+            componentInstInputsMap.getComponentInstanceProperties().values()
+                .forEach(componentInstancePropInputsList -> componentInstancePropInputsList.stream()
+                    .forEach(componentInstancePropInput -> componentInstancePropInput.setMappedToComponentProperty(false)));
+        }
+    }
+
     private void updatePropertiesConstraints(Component component, ComponentInstInputsMap componentInstInputsMap) {
         componentInstInputsMap.getComponentInstanceProperties()
             .forEach((k, v) -> updatePropsConstraints(component.safeGetComponentInstancesProperties(), k, v));
index 3294523..7409588 100644 (file)
@@ -822,8 +822,8 @@ public class ServiceTemplateDesignUiTests extends SetupCDTest {
         final Map<String, Object> substitutionMappingProperties = getMapEntry(substitutionMapping, "properties");
         assertThat(String.format("'%s' should contain a properties entry", toscaYaml), substitutionMappingProperties,
             notNullValue());
-        assertEquals(2, substitutionMappingProperties.keySet().stream()
-            .filter(s -> (s.contains("resourceSubtype") || s.contains("property1"))).count());
+        assertEquals(1, substitutionMappingProperties.keySet().stream()
+            .filter(s -> (s.contains("property1"))).count());
     }
 
     private void verifyToscaTemplateAddInput(Map<?, ?> yaml) {