Implement 'Update Service by importing Tosca Template'-story
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / YamlTemplateParsingHandler.java
index 9734a0a..2ec342e 100644 (file)
@@ -90,6 +90,7 @@ import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
 import org.openecomp.sdc.be.components.impl.ImportUtils;
 import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator;
 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
+import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
 import org.openecomp.sdc.be.config.BeEcompErrorManager;
@@ -107,8 +108,10 @@ import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefin
 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
+import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
+import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
 import org.openecomp.sdc.be.model.CapabilityDefinition;
@@ -123,6 +126,7 @@ 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.Resource;
 import org.openecomp.sdc.be.model.UploadArtifactInfo;
 import org.openecomp.sdc.be.model.UploadAttributeInfo;
 import org.openecomp.sdc.be.model.UploadCapInfo;
@@ -154,18 +158,21 @@ public class YamlTemplateParsingHandler {
     private final GroupTypeBusinessLogic groupTypeBusinessLogic;
     private final AnnotationBusinessLogic annotationBusinessLogic;
     private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
+    private final ServiceBusinessLogic serviceBusinessLogic;
     private final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
 
     public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao,
                                       GroupTypeBusinessLogic groupTypeBusinessLogic,
                                       AnnotationBusinessLogic annotationBusinessLogic,
                                       PolicyTypeBusinessLogic policyTypeBusinessLogic,
+                                      ServiceBusinessLogic serviceBusinessLogic,
                                       final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler
     ) {
         this.janusGraphDao = janusGraphDao;
         this.groupTypeBusinessLogic = groupTypeBusinessLogic;
         this.annotationBusinessLogic = annotationBusinessLogic;
         this.policyTypeBusinessLogic = policyTypeBusinessLogic;
+        this.serviceBusinessLogic = serviceBusinessLogic;
         this.toscaFunctionYamlParsingHandler = toscaFunctionYamlParsingHandler;
     }
 
@@ -192,9 +199,19 @@ public class YamlTemplateParsingHandler {
         parsedToscaYamlInfo.setPolicies(getPolicies(mappedToscaTemplate, component.getModel()));
         Map<String, Object> substitutionMappings = getSubstitutionMappings(mappedToscaTemplate);
         if (substitutionMappings != null) {
-            if (component.isService() && !interfaceTemplateYaml.isEmpty()) {
-                parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml)));
-                parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
+            if (component.isService()) {
+                if (interfaceTemplateYaml.isEmpty()) {
+                    Resource resource = serviceBusinessLogic.fetchDerivedFromGenericType(component, null);
+                    List<PropertyDefinition> properties = resource.getProperties();
+                    parsedToscaYamlInfo.setProperties(properties.stream().collect(Collectors.toMap(PropertyDefinition::getName, prop -> prop)));
+                    parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
+                } else {
+                    parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml)));
+                    parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
+                }
+            }
+            if (substitutionMappings.get("properties") != null) {
+                parsedToscaYamlInfo.setSubstitutionMappingProperties((Map<String, List<String>>) substitutionMappings.get("properties"));
             }
             parsedToscaYamlInfo.setSubstitutionMappingNodeType((String) substitutionMappings.get(NODE_TYPE.getElementName()));
         }
@@ -819,6 +836,7 @@ public class YamlTemplateParsingHandler {
                 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
                 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
                 setSubstitutions(substitutionMappings, nodeTemplateInfo);
+                setOccurrencesAndInstanceCount(nodeTemplateInfo, nodeTemplateJsonMap);
             } else {
                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
             }
@@ -927,6 +945,24 @@ public class YamlTemplateParsingHandler {
         }
     }
 
+    @SuppressWarnings("unchecked")
+    private void setOccurrencesAndInstanceCount(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
+        if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
+            List<Object> occurrences = (List<Object>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
+            nodeTemplateInfo.setMinOccurrences(occurrences.get(0).toString());
+            nodeTemplateInfo.setMaxOccurrences(occurrences.get(1).toString());
+        }
+        if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName())) {
+            Object instanceCount = nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName());
+            if (instanceCount instanceof Map) {
+                String instanceCountAsString = "{get_input:" + (String)((Map)instanceCount).get("get_input") + "}";
+                nodeTemplateInfo.setInstanceCount(instanceCountAsString);
+            } else {
+                nodeTemplateInfo.setInstanceCount(instanceCount.toString());
+            }
+        }
+    }
+
     @SuppressWarnings("unchecked")
     private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap, String nodeName) {
         Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
@@ -1183,7 +1219,8 @@ public class YamlTemplateParsingHandler {
             if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(propValueObj)) {
                 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(propValueMap).ifPresent(propertyDef::setToscaFunction);
             } else {
-                final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
+                final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
+                    buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
                 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
                     Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
                     if (existingSubPropertyToscaFunctions == null) {
@@ -1210,7 +1247,7 @@ public class YamlTemplateParsingHandler {
         }
         return propertyDef;
     }
-    
+
     private Collection<SubPropertyToscaFunction> buildSubPropertyToscaFunctions(final Map<String, Object> propValueMap, final List<String> path) {
         Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = new ArrayList<>();
         propValueMap.entrySet().stream().filter(entry -> entry.getValue() instanceof Map).forEach(entry -> {
@@ -1218,7 +1255,7 @@ public class YamlTemplateParsingHandler {
             subPropertyPath.add(entry.getKey());
             if (ToscaFunctionType.findType(((Map<String, Object>) entry.getValue()).keySet().iterator().next()).isPresent()) {
                 Optional<ToscaFunction> toscaFunction =
-                        toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
+                    toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
                 if (toscaFunction.isPresent()) {
                     SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
                     subPropertyToscaFunction.setToscaFunction(toscaFunction.get());
@@ -1269,6 +1306,7 @@ public class YamlTemplateParsingHandler {
             operationInput.setUniqueId(UUID.randomUUID().toString());
             operationInput.setInputId(operationInput.getUniqueId());
             operationInput.setName(interfaceInput.getKey());
+
             handleInputToscaDefinition(interfaceInput.getKey(), interfaceInput.getValue(), operationInput);
             inputs.add(operationInput);
         }
@@ -1281,10 +1319,25 @@ public class YamlTemplateParsingHandler {
         final OperationInputDefinition operationInput
     ) {
         if (value instanceof Map) {
+            final Map<String, Object> valueMap = (Map<String, Object>) value;
             log.debug("Creating interface operation input '{}'", inputName);
             Type type = new TypeToken<LinkedHashMap<String, Object>>() {
             }.getType();
             String stringValue = gson.toJson(value, type);
+            if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) {
+                toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value)
+                    .ifPresent(operationInput::setToscaFunction);
+            } else {
+                final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>());
+                if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
+                    Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = operationInput.getSubPropertyToscaFunctions();
+                    if (existingSubPropertyToscaFunctions == null) {
+                        operationInput.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
+                    } else {
+                        operationInput.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
+                    }
+                }
+            }
             operationInput.setValue(stringValue);
         }
         if (value instanceof String) {
@@ -1293,6 +1346,10 @@ public class YamlTemplateParsingHandler {
             operationInput.setToscaDefaultValue(stringValue);
             operationInput.setValue(stringValue);
         }
+        operationInput.setType("string");
+        if (operationInput.getValue() == null) {
+            operationInput.setValue(String.valueOf(value));
+        }
     }
 
     private Optional<ArtifactDataDefinition> handleOperationImplementation(
@@ -1374,9 +1431,22 @@ public class YamlTemplateParsingHandler {
 
     @SuppressWarnings("unchecked")
     private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
+        int index = 0;
         for (Object objValue : propValueList) {
             if (objValue instanceof Map) {
                 Map<String, Object> objMap = (Map<String, Object>) objValue;
+                Map<String, Object> propValueMap = new HashMap<String, Object>();
+                propValueMap.put(String.valueOf(index), objValue);
+                final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
+                    buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
+                if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
+                    Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
+                    if (existingSubPropertyToscaFunctions == null) {
+                        propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
+                    } else {
+                        propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
+                    }
+                }
                 if (objMap.containsKey(GET_INPUT.getElementName())) {
                     fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
                 } else {
@@ -1387,6 +1457,7 @@ public class YamlTemplateParsingHandler {
                 List<Object> propSubValueList = (List<Object>) objValue;
                 fillInputsListRecursively(propertyDef, propSubValueList);
             }
+            index++;
         }
     }