bdb659b259cc22229870c79c8b301709e3dbddd0
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / csar / YamlTemplateParsingHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22
23 package org.openecomp.sdc.be.components.csar;
24
25 import static java.util.stream.Collectors.toList;
26 import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.QUOTE;
27 import static org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
28 import static org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
29 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaListElement;
30 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMapElement;
31 import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement;
32 import static org.openecomp.sdc.be.components.impl.ImportUtils.loadYamlAsStrictMap;
33 import static org.openecomp.sdc.be.model.tosca.ToscaType.STRING;
34 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
35 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ATTRIBUTES;
36 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITIES;
37 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITY;
38 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE;
39 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
40 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.FILE;
41 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GET_INPUT;
42 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GROUPS;
43 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPLEMENTATION;
44 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS;
45 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INTERFACES;
46 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IS_PASSWORD;
47 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.MEMBERS;
48 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE;
49 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES;
50 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TYPE;
51 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS;
52 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OUTPUTS;
53 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.POLICIES;
54 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES;
55 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.RELATIONSHIP;
56 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.RELATIONSHIP_TEMPLATES;
57 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIREMENTS;
58 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_FILTERS;
59 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS;
60 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TARGETS;
61 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE;
62 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;
63 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES;
64
65 import com.att.aft.dme2.internal.gson.reflect.TypeToken;
66 import com.google.common.collect.Lists;
67 import com.google.common.collect.Maps;
68 import com.google.gson.Gson;
69 import fj.data.Either;
70 import java.lang.reflect.Type;
71 import java.util.ArrayList;
72 import java.util.Collection;
73 import java.util.Collections;
74 import java.util.EnumMap;
75 import java.util.HashMap;
76 import java.util.LinkedHashMap;
77 import java.util.List;
78 import java.util.Map;
79 import java.util.Map.Entry;
80 import java.util.Objects;
81 import java.util.Optional;
82 import java.util.Set;
83 import java.util.UUID;
84 import java.util.stream.Collectors;
85 import org.apache.commons.collections.CollectionUtils;
86 import org.apache.commons.collections.MapUtils;
87 import org.apache.commons.lang3.StringUtils;
88 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
89 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
90 import org.openecomp.sdc.be.components.impl.ImportUtils;
91 import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator;
92 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
93 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
94 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
95 import org.openecomp.sdc.be.config.BeEcompErrorManager;
96 import org.openecomp.sdc.be.dao.api.ActionStatus;
97 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
98 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
99 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
100 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
101 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
102 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
103 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
104 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
105 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
106 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
107 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
108 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition;
109 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
110 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
111 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
112 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
113 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
114 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
115 import org.openecomp.sdc.be.datatypes.enums.PropertySource;
116 import org.openecomp.sdc.be.datatypes.tosca.ToscaGetFunctionType;
117 import org.openecomp.sdc.be.model.CapabilityDefinition;
118 import org.openecomp.sdc.be.model.Component;
119 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
120 import org.openecomp.sdc.be.model.GroupDefinition;
121 import org.openecomp.sdc.be.model.GroupTypeDefinition;
122 import org.openecomp.sdc.be.model.InputDefinition;
123 import org.openecomp.sdc.be.model.NodeTypeInfo;
124 import org.openecomp.sdc.be.model.OutputDefinition;
125 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
126 import org.openecomp.sdc.be.model.PolicyDefinition;
127 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
128 import org.openecomp.sdc.be.model.PropertyDefinition;
129 import org.openecomp.sdc.be.model.UploadArtifactInfo;
130 import org.openecomp.sdc.be.model.UploadAttributeInfo;
131 import org.openecomp.sdc.be.model.UploadCapInfo;
132 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
133 import org.openecomp.sdc.be.model.UploadInterfaceInfo;
134 import org.openecomp.sdc.be.model.UploadPropInfo;
135 import org.openecomp.sdc.be.model.UploadReqInfo;
136 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
137 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
138 import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil;
139 import org.openecomp.sdc.be.ui.model.OperationUi;
140 import org.openecomp.sdc.be.ui.model.PropertyAssignmentUi;
141 import org.openecomp.sdc.be.utils.PropertyFilterConstraintDataDefinitionHelper;
142 import org.openecomp.sdc.be.utils.TypeUtils;
143 import org.openecomp.sdc.common.log.wrappers.Logger;
144 import org.yaml.snakeyaml.parser.ParserException;
145
146 /**
147  * A handler class designed to parse the YAML file of the service template for a JAVA object
148  */
149 @org.springframework.stereotype.Component
150 public class YamlTemplateParsingHandler {
151
152     private static final int SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX = 0;
153     private static final int SUB_MAPPING_CAPABILITY_NAME_IDX = 1;
154     private static final Logger log = Logger.getLogger(YamlTemplateParsingHandler.class);
155     private final Gson gson = new Gson();
156     private final JanusGraphDao janusGraphDao;
157     private final GroupTypeBusinessLogic groupTypeBusinessLogic;
158     private final AnnotationBusinessLogic annotationBusinessLogic;
159     private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
160     private final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
161
162     public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao,
163                                       GroupTypeBusinessLogic groupTypeBusinessLogic,
164                                       AnnotationBusinessLogic annotationBusinessLogic,
165                                       PolicyTypeBusinessLogic policyTypeBusinessLogic,
166                                       final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler
167     ) {
168         this.janusGraphDao = janusGraphDao;
169         this.groupTypeBusinessLogic = groupTypeBusinessLogic;
170         this.annotationBusinessLogic = annotationBusinessLogic;
171         this.policyTypeBusinessLogic = policyTypeBusinessLogic;
172         this.toscaFunctionYamlParsingHandler = toscaFunctionYamlParsingHandler;
173     }
174
175     public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames,
176                                                          Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName,
177                                                          Component component, String interfaceTemplateYaml) {
178         log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName);
179         Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName);
180         ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo();
181         Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(mappedToscaTemplate, TOPOLOGY_TEMPLATE,
182             ToscaElementTypeEnum.ALL).left().on(err -> failIfNotTopologyTemplate(fileName));
183         final Map<String, Object> mappedTopologyTemplateInputs = mappedTopologyTemplate.entrySet().stream()
184             .filter(entry -> entry.getKey().equals(INPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
185         final Map<String, Object> mappedTopologyTemplateOutputs = mappedTopologyTemplate.entrySet().stream()
186             .filter(entry -> entry.getKey().equals(OUTPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
187         parsedToscaYamlInfo.setInputs(getInputs(mappedTopologyTemplateInputs));
188         parsedToscaYamlInfo.setOutputs(getOutputs(mappedTopologyTemplateOutputs));
189         parsedToscaYamlInfo.setInstances(getInstances(
190             mappedToscaTemplate,
191             createdNodesToscaResourceNames
192         ));
193         associateRelationshipTemplatesToInstances(parsedToscaYamlInfo.getInstances(), mappedTopologyTemplate);
194         parsedToscaYamlInfo.setGroups(getGroups(mappedToscaTemplate, component.getModel()));
195         parsedToscaYamlInfo.setPolicies(getPolicies(mappedToscaTemplate, component.getModel()));
196         Map<String, Object> substitutionMappings = getSubstitutionMappings(mappedToscaTemplate);
197         if (substitutionMappings != null) {
198             if (component.isService() && !interfaceTemplateYaml.isEmpty()) {
199                 parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml)));
200                 parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
201             }
202             if (substitutionMappings.get("properties") != null) {
203                 parsedToscaYamlInfo.setSubstitutionMappingProperties((Map<String, List<String>>) substitutionMappings.get("properties"));
204             }
205             parsedToscaYamlInfo.setSubstitutionMappingNodeType((String) substitutionMappings.get(NODE_TYPE.getElementName()));
206         }
207         log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName);
208         return parsedToscaYamlInfo;
209     }
210
211     private Map<String, Object> getMappedToscaTemplate(String fileName, String resourceYml, Map<String, NodeTypeInfo> nodeTypesInfo,
212                                                        String nodeName) {
213         Map<String, Object> mappedToscaTemplate;
214         if (isNodeExist(nodeTypesInfo, nodeName)) {
215             mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
216         } else {
217             mappedToscaTemplate = loadYaml(fileName, resourceYml);
218         }
219         return mappedToscaTemplate;
220     }
221
222     private Map<String, Object> loadYaml(String fileName, String resourceYml) {
223         Map<String, Object> mappedToscaTemplate = null;
224         try {
225             mappedToscaTemplate = loadYamlAsStrictMap(resourceYml);
226         } catch (ParserException e) {
227             log.debug("#getMappedToscaTemplate - Failed to load YAML file {}", fileName, e);
228             rollbackWithException(ActionStatus.TOSCA_PARSE_ERROR, fileName, e.getMessage());
229         }
230         return mappedToscaTemplate;
231     }
232
233     private boolean isNodeExist(Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
234         return nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName);
235     }
236
237     private Map<String, InputDefinition> getInputs(Map<String, Object> toscaJson) {
238         Map<String, InputDefinition> inputs = ImportUtils.getInputs(toscaJson, annotationBusinessLogic.getAnnotationTypeOperations()).left()
239             .on(err -> new HashMap<>());
240         annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(inputs);
241         return inputs;
242     }
243
244     private Map<String, OutputDefinition> getOutputs(Map<String, Object> toscaJson) {
245         return ImportUtils.getOutputs(toscaJson).left().on(err -> new HashMap<>());
246     }
247
248     private Map<String, PropertyDefinition> getProperties(Map<String, Object> toscaJson) {
249         return ImportUtils.getProperties(toscaJson).left().on(err -> new HashMap<>());
250     }
251
252     private ListDataDefinition<SubstitutionFilterPropertyDataDefinition> getSubstitutionFilterProperties(Map<String, Object> toscaJson) {
253         ListDataDefinition<SubstitutionFilterPropertyDataDefinition> propertyList = new ListDataDefinition<>();
254         Map<String, Object> substitutionFilters = findFirstToscaMapElement(toscaJson, SUBSTITUTION_FILTERS).left().on(err -> new HashMap<>());
255         if (MapUtils.isEmpty(substitutionFilters)) {
256             return propertyList;
257         }
258         ArrayList<Map<String, List<Map<String, Object>>>> substitutionFilterProperties =
259             (ArrayList<Map<String, List<Map<String, Object>>>>) substitutionFilters.get("properties");
260         if (CollectionUtils.isEmpty(substitutionFilterProperties)) {
261             return propertyList;
262         }
263         for (Map<String, List<Map<String, Object>>> filterProps : substitutionFilterProperties) {
264             for (Map.Entry<String, List<Map<String, Object>>> propertyFilterEntry : filterProps.entrySet()) {
265                 final String propertyName = propertyFilterEntry.getKey();
266                 for (Map<String, Object> filterValueMap : propertyFilterEntry.getValue()) {
267                     final var substitutionFilterPropertyDataDefinition = new SubstitutionFilterPropertyDataDefinition();
268                     substitutionFilterPropertyDataDefinition.setName(propertyName);
269                     substitutionFilterPropertyDataDefinition.setConstraints(createSubstitutionFilterConstraints(propertyName, filterValueMap));
270                     propertyList.add(substitutionFilterPropertyDataDefinition);
271                 }
272             }
273         }
274         return propertyList;
275     }
276
277     private List<PropertyFilterConstraintDataDefinition> createSubstitutionFilterConstraints(final String name, final Map<String, Object> value) {
278         final List<PropertyFilterConstraintDataDefinition> constraints = new ArrayList<>();
279         for (final Map.Entry<String, Object> valueEntry : value.entrySet()) {
280             final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
281             propertyFilterConstraint.setPropertyName(name);
282             propertyFilterConstraint.setOperator(ConstraintType.findByType(valueEntry.getKey()).orElse(null));
283             propertyFilterConstraint.setTargetType(PropertyFilterTargetType.PROPERTY);
284             final Optional<ToscaFunction> toscaFunction = PropertyFilterConstraintDataDefinitionHelper
285                 .createToscaFunctionFromLegacyConstraintValue(valueEntry.getValue());
286             if (toscaFunction.isPresent()) {
287                 final ToscaFunction toscaFunction1 = toscaFunction.get();
288                 propertyFilterConstraint.setValue(toscaFunction1);
289                 propertyFilterConstraint.setValueType(
290                     PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(toscaFunction1.getType()).orElse(null)
291                 );
292             } else {
293                 propertyFilterConstraint.setValue(valueEntry.getValue());
294                 propertyFilterConstraint.setValueType(FilterValueType.STATIC);
295             }
296             constraints.add(propertyFilterConstraint);
297         }
298
299         return constraints;
300     }
301
302     private Map<String, PolicyDefinition> getPolicies(Map<String, Object> toscaJson, String model) {
303         Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
304             .left().on(err -> new HashMap<>());
305         Map<String, Object> foundPolicies = (Map<String, Object>) mappedTopologyTemplate.get(POLICIES.getElementName());
306         if (MapUtils.isNotEmpty(foundPolicies)) {
307             return foundPolicies.entrySet().stream().map(policyToCreate -> createPolicy(policyToCreate, model))
308                 .collect(Collectors.toMap(PolicyDefinition::getName, p -> p));
309         }
310         return Collections.emptyMap();
311     }
312
313     private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue, String model) {
314         PolicyDefinition emptyPolicyDef = new PolicyDefinition();
315         String policyName = policyNameValue.getKey();
316         emptyPolicyDef.setName(policyName);
317         try {
318             // There's no need to null test in conjunction with an instanceof test. null is not an instanceof anything, so a null check is redundant.
319             if (policyNameValue.getValue() instanceof Map) {
320                 Map<String, Object> policyTemplateJsonMap = (Map<String, Object>) policyNameValue.getValue();
321                 validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap, model);
322             } else {
323                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
324             }
325         } catch (ClassCastException e) {
326             log.debug("#createPolicy - Failed to create the policy {}. The exception occurred", policyName, e);
327             rollbackWithException(ActionStatus.INVALID_YAML);
328         }
329         return emptyPolicyDef;
330     }
331
332     private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, Map<String, Object> policyTemplateJsonMap, String model) {
333         String policyTypeName = (String) policyTemplateJsonMap.get(TYPE.getElementName());
334         if (StringUtils.isEmpty(policyTypeName)) {
335             log.debug("#validateAndFillPolicy - The 'type' member is not found under policy {}", emptyPolicyDefinition.getName());
336             rollbackWithException(ActionStatus.POLICY_MISSING_POLICY_TYPE, emptyPolicyDefinition.getName());
337         }
338         emptyPolicyDefinition.setType(policyTypeName);
339         // set policy targets
340         emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap));
341         PolicyTypeDefinition policyTypeDefinition = validateGetPolicyTypeDefinition(policyTypeName, model);
342         // set policy properties
343         emptyPolicyDefinition.setProperties(validateFillPolicyProperties(policyTypeDefinition, policyTemplateJsonMap));
344     }
345
346     private PolicyTypeDefinition validateGetPolicyTypeDefinition(String policyType, String modelName) {
347         PolicyTypeDefinition policyTypeDefinition = policyTypeBusinessLogic.getLatestPolicyTypeByType(policyType, modelName);
348         if (policyTypeDefinition == null) {
349             log.debug("#validateAndFillPolicy - The policy type {} not found", policyType);
350             rollbackWithException(ActionStatus.POLICY_TYPE_IS_INVALID, policyType);
351         }
352         return policyTypeDefinition;
353     }
354
355     private List<PropertyDataDefinition> validateFillPolicyProperties(final PolicyTypeDefinition policyTypeDefinition,
356                                                                       final Map<String, Object> policyTemplateJsonMap) {
357         if (policyTypeDefinition == null || CollectionUtils.isEmpty(policyTypeDefinition.getProperties())
358             || MapUtils.isEmpty(policyTemplateJsonMap)) {
359             return Collections.emptyList();
360         }
361         final Map<String, Object> propertiesJsonMap = (Map<String, Object>) policyTemplateJsonMap.get(PROPERTIES.getElementName());
362         if (MapUtils.isEmpty(propertiesJsonMap)) {
363             return Collections.emptyList();
364         }
365         return propertiesJsonMap.entrySet().stream()
366             .map(propertyJson -> {
367                 final PropertyDefinition originalProperty =
368                     policyTypeDefinition.getProperties().stream()
369                         .filter(propertyDefinition -> propertyDefinition.getName().equals(propertyJson.getKey()))
370                         .findFirst()
371                         .orElse(null);
372                 if (originalProperty == null) {
373                     return null;
374                 }
375                 final UploadPropInfo uploadPropInfo = buildProperty(propertyJson.getKey(), propertyJson.getValue());
376                 final PropertyDefinition propertyDefinition = new PropertyDefinition(originalProperty);
377                 propertyDefinition.setToscaFunction(uploadPropInfo.getToscaFunction());
378                 propertyDefinition.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
379                 propertyDefinition.setGetInputValues(uploadPropInfo.getGet_input());
380                 propertyDefinition.setDescription(uploadPropInfo.getDescription());
381                 String propertyValue = gson.toJson(uploadPropInfo.getValue());
382                 if (!propertyDefinition.isToscaFunction()) {
383                     propertyValue = PropertiesUtils.trimQuotes(propertyValue);
384                 }
385                 propertyDefinition.setValue(propertyValue);
386                 return propertyDefinition;
387             })
388             .filter(Objects::nonNull)
389             .collect(Collectors.toList());
390     }
391
392     private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson) {
393         Map<PolicyTargetType, List<String>> targets = new EnumMap<>(PolicyTargetType.class);
394         if (policyTemplateJson.containsKey(TARGETS.getElementName()) && policyTemplateJson.get(TARGETS.getElementName()) instanceof List) {
395             List<String> targetsElement = (List<String>) policyTemplateJson.get(TARGETS.getElementName());
396             targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement);
397         }
398         return targets;
399     }
400
401     private Map<String, UploadComponentInstanceInfo> getInstances(
402         Map<String, Object> toscaJson,
403         Map<String, String> createdNodesToscaResourceNames
404     ) {
405         Map<String, Object> nodeTemplates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES)
406             .left().on(err -> new HashMap<>());
407         if (nodeTemplates.isEmpty()) {
408             return Collections.emptyMap();
409         }
410         return getInstances(
411             toscaJson,
412             createdNodesToscaResourceNames,
413             nodeTemplates
414         );
415     }
416
417     private Map<String, UploadComponentInstanceInfo> getInstances(
418         Map<String, Object> toscaJson,
419         Map<String, String> createdNodesToscaResourceNames,
420         Map<String, Object> nodeTemplates
421     ) {
422         Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
423         return nodeTemplates.entrySet().stream()
424             .map(node -> buildModuleComponentInstanceInfo(
425                 node,
426                 substitutionMappings,
427                 createdNodesToscaResourceNames
428             ))
429             .collect(Collectors.toMap(UploadComponentInstanceInfo::getName, i -> i));
430     }
431
432     private Map<String, Object> getSubstitutionMappings(Map<String, Object> toscaJson) {
433         Either<Map<String, Object>, ResultStatusEnum> eitherSubstitutionMappings = findFirstToscaMapElement(toscaJson, SUBSTITUTION_MAPPINGS);
434         if (eitherSubstitutionMappings.isLeft()) {
435             return eitherSubstitutionMappings.left().value();
436         }
437         return null;
438     }
439
440     private void associateRelationshipTemplatesToInstances(final Map<String, UploadComponentInstanceInfo> instances,
441                                                            final Map<String, Object> toscaJson) {
442         if (MapUtils.isEmpty(instances)) {
443             return;
444         }
445         for (UploadComponentInstanceInfo instance : instances.values()) {
446             final Map<String, List<OperationUi>> operations = new HashMap<>();
447             final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
448             if (MapUtils.isNotEmpty(requirements)) {
449                 requirements.values()
450                     .forEach(requirementInfoList -> requirementInfoList.stream()
451                         .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
452                         .forEach(requirement -> operations.put(requirement.getRelationshipTemplate(),
453                             getOperationsFromRelationshipTemplate(toscaJson, requirement.getRelationshipTemplate()))));
454             }
455             instance.setOperations(operations);
456         }
457     }
458
459     private Map<String, Object> getRelationshipTemplates(final Map<String, Object> toscaJson, final String relationshipTemplate) {
460         final Either<Map<String, Object>, ResultStatusEnum> eitherRelationshipTemplates = findFirstToscaMapElement(toscaJson, RELATIONSHIP_TEMPLATES);
461         if (eitherRelationshipTemplates.isRight()) {
462             throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_NOT_FOUND);
463         }
464         final Map<String, Object> relationshipTemplateMap = eitherRelationshipTemplates.left().value();
465         final Map<String, Map<String, Object>> relationship = (Map<String, Map<String, Object>>) relationshipTemplateMap.get(relationshipTemplate);
466         if (relationship == null) {
467             throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_DEFINITION_NOT_FOUND);
468         }
469         return relationship.get(INTERFACES.getElementName());
470     }
471
472     private List<ToscaInterfaceDefinition> buildToscaInterfacesFromRelationship(final Map<String, Object> interfaces) {
473         return interfaces.entrySet().stream()
474             .map(entry -> {
475                 final var toscaInterfaceDefinition = new ToscaInterfaceDefinition();
476                 toscaInterfaceDefinition.setType(entry.getKey());
477                 final Map<String, Object> toscaInterfaceMap = (Map<String, Object>) entry.getValue();
478                 toscaInterfaceDefinition.setOperations((Map<String, Object>) toscaInterfaceMap.get(OPERATIONS.getElementName()));
479                 return toscaInterfaceDefinition;
480             })
481             .collect(toList());
482     }
483
484     private Optional<Object> getImplementation(final Map<String, Object> operationToscaMap) {
485         if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(IMPLEMENTATION.getElementName())) {
486             return Optional.empty();
487         }
488         return Optional.ofNullable(operationToscaMap.get(IMPLEMENTATION.getElementName()));
489     }
490
491     private List<PropertyAssignmentUi> getOperationsInputs(final Map<String, Object> operationToscaMap) {
492         if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(INPUTS.getElementName())) {
493             return Collections.emptyList();
494         }
495         final Map<String, Object> inputsMap = (Map<String, Object>) operationToscaMap.get(INPUTS.getElementName());
496         return inputsMap.entrySet().stream().map(this::buildInputAssignment).collect(toList());
497     }
498
499     private PropertyAssignmentUi buildInputAssignment(final Entry<String, Object> inputAssignmentMap) {
500         var propertyAssignmentUi = new PropertyAssignmentUi();
501         propertyAssignmentUi.setName(inputAssignmentMap.getKey());
502         propertyAssignmentUi.setValue(inputAssignmentMap.getValue().toString());
503         propertyAssignmentUi.setType(STRING.getType());
504         return propertyAssignmentUi;
505     }
506
507     private List<OperationUi> getOperationsFromRelationshipTemplate(final Map<String, Object> toscaJson, final String relationshipTemplate) {
508         final List<OperationUi> operationUiList = new ArrayList<>();
509         final List<ToscaInterfaceDefinition> interfaces =
510             buildToscaInterfacesFromRelationship(getRelationshipTemplates(toscaJson, relationshipTemplate));
511         interfaces.stream()
512             .filter(interfaceDefinition -> MapUtils.isNotEmpty(interfaceDefinition.getOperations()))
513             .forEach(interfaceDefinition ->
514                 interfaceDefinition.getOperations()
515                     .forEach((operationType, operationValue) ->
516                         operationUiList.add(buildOperation(interfaceDefinition.getType(), operationType, (Map<String, Object>) operationValue))
517                     ));
518         return operationUiList;
519     }
520
521     private OperationUi buildOperation(final String interfaceType, final String operationType, final Map<String, Object> operationToscaMap) {
522         var operationUi = new OperationUi();
523         operationUi.setInterfaceType(interfaceType);
524         operationUi.setOperationType(operationType);
525         getImplementation(operationToscaMap).ifPresent(operationUi::setImplementation);
526         final List<PropertyAssignmentUi> operationsInputs = getOperationsInputs(operationToscaMap);
527         if (CollectionUtils.isNotEmpty(operationsInputs)) {
528             operationUi.setInputs(operationsInputs);
529         }
530         return operationUi;
531     }
532
533     @SuppressWarnings("unchecked")
534     private Map<String, GroupDefinition> getGroups(Map<String, Object> toscaJson, String model) {
535         Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
536             .left().on(err -> new HashMap<>());
537         Map<String, Object> foundGroups = (Map<String, Object>) mappedTopologyTemplate.get(GROUPS.getElementName());
538         if (MapUtils.isNotEmpty(foundGroups)) {
539             Map<String, GroupDefinition> groups = foundGroups.entrySet().stream().map(groupToCreate -> createGroup(groupToCreate, model))
540                 .collect(Collectors.toMap(GroupDefinition::getName, g -> g));
541             Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
542             if (capabilitiesSubstitutionMappingsExist(substitutionMappings)) {
543                 groups.forEach((key, value) -> updateCapabilitiesNames(value,
544                     getNamesToUpdate(key, (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName()))));
545             }
546             return groups;
547         }
548         return new HashMap<>();
549     }
550
551     private void updateCapabilitiesNames(GroupDefinition group, Map<String, String> capabilityNames) {
552         if (MapUtils.isNotEmpty(group.getCapabilities())) {
553             group.getCapabilities().values().stream().flatMap(Collection::stream).filter(cap -> capabilityNames.containsKey(cap.getName()))
554                 .forEach(cap -> cap.setName(capabilityNames.get(cap.getName())));
555         }
556     }
557
558     private Map<String, String> getNamesToUpdate(String name, Map<String, List<String>> pair) {
559         return pair.entrySet().stream().filter(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX).equalsIgnoreCase(name))
560             .collect(Collectors.toMap(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_NAME_IDX), Map.Entry::getKey, (n1, n2) -> n1));
561     }
562
563     private boolean capabilitiesSubstitutionMappingsExist(Map<String, Object> substitutionMappings) {
564         return substitutionMappings != null && substitutionMappings.containsKey(CAPABILITIES.getElementName());
565     }
566
567     private GroupDefinition createGroup(Map.Entry<String, Object> groupNameValue, String model) {
568         GroupDefinition group = new GroupDefinition();
569         group.setName(groupNameValue.getKey());
570         try {
571             if (groupNameValue.getValue() instanceof Map) {
572                 Map<String, Object> groupTemplateJsonMap = (Map<String, Object>) groupNameValue.getValue();
573                 validateAndFillGroup(group, groupTemplateJsonMap, model);
574                 validateUpdateGroupProperties(group, groupTemplateJsonMap);
575                 validateUpdateGroupCapabilities(group, groupTemplateJsonMap);
576             } else {
577                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
578             }
579         } catch (ClassCastException e) {
580             log.debug("#createGroup - Failed to create the group {}. The exception occurres", groupNameValue.getKey(), e);
581             rollbackWithException(ActionStatus.INVALID_YAML);
582         }
583         return group;
584     }
585
586     private Map<String, CapabilityDefinition> addCapabilities(Map<String, CapabilityDefinition> cap, Map<String, CapabilityDefinition> otherCap) {
587         cap.putAll(otherCap);
588         return cap;
589     }
590
591     private Map<String, CapabilityDefinition> addCapability(CapabilityDefinition cap) {
592         Map<String, CapabilityDefinition> map = Maps.newHashMap();
593         map.put(cap.getName(), cap);
594         return map;
595     }
596
597     private void setMembers(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
598         if (groupTemplateJsonMap.containsKey(MEMBERS.getElementName())) {
599             Object members = groupTemplateJsonMap.get(MEMBERS.getElementName());
600             if (members != null) {
601                 if (members instanceof List) {
602                     setMembersFromList(groupInfo, (List<?>) members);
603                 } else {
604                     log.debug("The 'members' member is not of type list under group {}", groupInfo.getName());
605                     rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
606                 }
607             }
608         }
609     }
610
611     private void setMembersFromList(GroupDefinition groupInfo, List<?> membersAsList) {
612         groupInfo.setMembers(membersAsList.stream().collect(Collectors.toMap(Object::toString, member -> "")));
613     }
614
615     @SuppressWarnings("unchecked")
616     private void validateUpdateGroupProperties(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
617         if (groupTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
618             Object propertiesElement = groupTemplateJsonMap.get(PROPERTIES.getElementName());
619             if (propertiesElement instanceof Map) {
620                 mergeGroupProperties(groupInfo, (Map<String, Object>) propertiesElement);
621             }
622         }
623     }
624
625     private void mergeGroupProperties(final GroupDefinition groupDefinition, final Map<String, Object> parsedProperties) {
626         if (CollectionUtils.isEmpty(groupDefinition.getProperties())) {
627             return;
628         }
629         validateGroupProperties(parsedProperties, groupDefinition);
630         groupDefinition.getProperties().stream()
631             .filter(property -> parsedProperties.containsKey(property.getName()))
632             .forEach(property -> mergeGroupProperty(property, parsedProperties.get(property.getName())));
633     }
634
635     private void mergeGroupProperty(final PropertyDataDefinition property, final Object propertyYaml) {
636         final UploadPropInfo uploadPropInfo = buildProperty(property.getName(), propertyYaml);
637         property.setToscaFunction(uploadPropInfo.getToscaFunction());
638         property.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
639         property.setValue(convertPropertyValue(ToscaPropertyType.isValidType(property.getType()), uploadPropInfo.getValue()));
640         property.setGetInputValues(uploadPropInfo.getGet_input());
641     }
642
643     private String convertPropertyValue(ToscaPropertyType type, Object value) {
644         String convertedValue = null;
645         if (value != null) {
646             if (type == null || value instanceof Map || value instanceof List) {
647                 convertedValue = gson.toJson(value);
648             } else {
649                 convertedValue = value.toString();
650             }
651         }
652         return convertedValue;
653     }
654
655     private void setDescription(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
656         if (groupTemplateJsonMap.containsKey(DESCRIPTION.getElementName())) {
657             groupInfo.setDescription((String) groupTemplateJsonMap.get(DESCRIPTION.getElementName()));
658         }
659     }
660
661     private void validateAndFillGroup(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap, String model) {
662         String type = (String) groupTemplateJsonMap.get(TYPE.getElementName());
663         if (StringUtils.isEmpty(type)) {
664             log.debug("#validateAndFillGroup - The 'type' member is not found under group {}", groupInfo.getName());
665             rollbackWithException(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupInfo.getName());
666         }
667         groupInfo.setType(type);
668         GroupTypeDefinition groupType = groupTypeBusinessLogic.getLatestGroupTypeByType(type, model);
669         if (groupType == null) {
670             log.debug("#validateAndFillGroup - The group type {} not found", groupInfo.getName());
671             rollbackWithException(ActionStatus.GROUP_TYPE_IS_INVALID, type);
672         }
673         groupInfo.convertFromGroupProperties(groupType.getProperties());
674         groupInfo.convertCapabilityDefinitions(groupType.getCapabilities());
675         setDescription(groupInfo, groupTemplateJsonMap);
676         setMembers(groupInfo, groupTemplateJsonMap);
677     }
678
679     @SuppressWarnings("unchecked")
680     private void validateUpdateGroupCapabilities(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
681         if (groupTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
682             Object capabilities = groupTemplateJsonMap.get(CAPABILITIES.getElementName());
683             if (capabilities instanceof List) {
684                 validateUpdateCapabilities(groupInfo, ((List<Object>) capabilities).stream().map(o -> buildGroupCapability(groupInfo, o))
685                     .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
686             } else if (capabilities instanceof Map) {
687                 validateUpdateCapabilities(groupInfo,
688                     ((Map<String, Object>) capabilities).entrySet().stream().map(e -> buildGroupCapability(groupInfo, e))
689                         .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
690             } else {
691                 log.debug("#setCapabilities - Failed to import the capabilities of the group {}. ", groupInfo.getName());
692                 rollbackWithException(ActionStatus.INVALID_YAML);
693             }
694         }
695     }
696
697     private void validateUpdateCapabilities(GroupDefinition groupInfo, Map<String, Map<String, CapabilityDefinition>> capabilityInfo) {
698         validateGroupCapabilities(groupInfo, capabilityInfo);
699         groupInfo.updateCapabilitiesProperties(capabilityInfo);
700     }
701
702     private void validateGroupCapabilities(GroupDefinition group, Map<String, Map<String, CapabilityDefinition>> parsedCapabilities) {
703         if (MapUtils.isNotEmpty(parsedCapabilities)) {
704             if (MapUtils.isEmpty(group.getCapabilities())) {
705                 failOnMissingCapabilityTypes(group, Lists.newArrayList(parsedCapabilities.keySet()));
706             }
707             List<String> missingCapTypes = parsedCapabilities.keySet().stream().filter(ct -> !group.getCapabilities().containsKey(ct))
708                 .collect(toList());
709             if (CollectionUtils.isNotEmpty(missingCapTypes)) {
710                 failOnMissingCapabilityTypes(group, missingCapTypes);
711             }
712             group.getCapabilities().entrySet().forEach(e -> validateCapabilities(group, e.getValue(), parsedCapabilities.get(e.getKey())));
713         }
714     }
715
716     private void validateCapabilities(GroupDefinition group, List<CapabilityDefinition> capabilities,
717                                       Map<String, CapabilityDefinition> parsedCapabilities) {
718         List<String> allowedCapNames = capabilities.stream().map(CapabilityDefinition::getName).distinct().collect(toList());
719         List<String> missingCapNames = parsedCapabilities.keySet().stream().filter(c -> !allowedCapNames.contains(c)).collect(toList());
720         if (CollectionUtils.isNotEmpty(missingCapNames)) {
721             failOnMissingCapabilityNames(group, missingCapNames);
722         }
723         validateCapabilitiesProperties(capabilities, parsedCapabilities);
724     }
725
726     private void validateCapabilitiesProperties(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
727         capabilities.forEach(c -> validateCapabilityProperties(c, parsedCapabilities.get(c.getName())));
728     }
729
730     private void validateCapabilityProperties(CapabilityDefinition capability, CapabilityDefinition parsedCapability) {
731         if (parsedCapability != null && parsedCapability.getProperties() != null) {
732             List<String> parsedPropertiesNames = parsedCapability.getProperties().stream().map(ComponentInstanceProperty::getName).collect(toList());
733             validateProperties(capability.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
734                 ActionStatus.PROPERTY_NOT_FOUND, capability.getName(), capability.getType());
735         }
736     }
737
738     private void validateGroupProperties(Map<String, Object> parsedProperties, GroupDefinition groupInfo) {
739         List<String> parsedPropertiesNames = new ArrayList<>(parsedProperties.keySet());
740         validateProperties(groupInfo.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
741             ActionStatus.GROUP_PROPERTY_NOT_FOUND, groupInfo.getName(), groupInfo.getType());
742     }
743
744     private void validateProperties(List<String> validProperties, List<String> parsedProperties, ActionStatus actionStatus, String name,
745                                     String type) {
746         if (CollectionUtils.isNotEmpty(parsedProperties)) {
747             verifyMissingProperties(actionStatus, name, type, parsedProperties.stream().filter(n -> !validProperties.contains(n)).collect(toList()));
748         }
749     }
750
751     private void verifyMissingProperties(ActionStatus actionStatus, String name, String type, List<String> missingProperties) {
752         if (CollectionUtils.isNotEmpty(missingProperties)) {
753             if (log.isDebugEnabled()) {
754                 log.debug("#validateProperties - Failed to validate properties. The properties {} are missing on {} of the type {}. ",
755                     missingProperties.toString(), name, type);
756             }
757             rollbackWithException(actionStatus, missingProperties.toString(), missingProperties.toString(), name, type);
758         }
759     }
760
761     @SuppressWarnings("unchecked")
762     private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Object capObject) {
763         if (!(capObject instanceof Map)) {
764             log.debug("#convertToGroupCapability - Failed to import the capability {}. ", capObject);
765             rollbackWithException(ActionStatus.INVALID_YAML);
766         }
767         return buildGroupCapability(groupInfo, ((Map<String, Object>) capObject).entrySet().iterator().next());
768     }
769
770     @SuppressWarnings("unchecked")
771     private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Map.Entry<String, Object> capEntry) {
772         CapabilityDefinition capability = new CapabilityDefinition();
773         capability.setOwnerType(CapabilityDataDefinition.OwnerType.GROUP);
774         capability.setName(capEntry.getKey());
775         capability.setParentName(capEntry.getKey());
776         capability.setOwnerId(groupInfo.getName());
777         if (!(capEntry.getValue() instanceof Map)) {
778             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
779             rollbackWithException(ActionStatus.INVALID_YAML);
780         }
781         Map<String, Object> capabilityValue = (Map<String, Object>) capEntry.getValue();
782         String type = (String) capabilityValue.get(TYPE.getElementName());
783         if (StringUtils.isEmpty(type)) {
784             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. Missing capability type. ", capEntry.getKey());
785             rollbackWithException(ActionStatus.INVALID_YAML);
786         }
787         capability.setType(type);
788         if (!(capabilityValue.get(PROPERTIES.getElementName()) instanceof Map)) {
789             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
790             rollbackWithException(ActionStatus.INVALID_YAML);
791         }
792         Map<String, Object> properties = (Map<String, Object>) capabilityValue.get(PROPERTIES.getElementName());
793         capability.setProperties(properties.entrySet().stream().map(this::convertToProperty).collect(toList()));
794         return capability;
795     }
796
797     private ComponentInstanceProperty convertToProperty(Map.Entry<String, Object> e) {
798         ComponentInstanceProperty property = new ComponentInstanceProperty();
799         property.setName(e.getKey());
800         property.setValue((String) e.getValue());
801         return property;
802     }
803
804     @SuppressWarnings("unchecked")
805     private UploadComponentInstanceInfo buildModuleComponentInstanceInfo(
806         Map.Entry<String, Object> nodeTemplateJsonEntry,
807         Map<String, Object> substitutionMappings,
808         Map<String, String> createdNodesToscaResourceNames
809     ) {
810         UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
811         nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey());
812         try {
813             if (nodeTemplateJsonEntry.getValue() instanceof String) {
814                 String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue();
815                 nodeTemplateInfo.setType(nodeTemplateJsonString);
816             } else if (nodeTemplateJsonEntry.getValue() instanceof Map) {
817                 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) nodeTemplateJsonEntry.getValue();
818                 setToscaResourceType(createdNodesToscaResourceNames, nodeTemplateInfo, nodeTemplateJsonMap);
819                 setRequirements(nodeTemplateInfo, nodeTemplateJsonMap);
820                 setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
821                 setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
822                 updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
823                 updateAttributes(nodeTemplateInfo, nodeTemplateJsonMap);
824                 updateInterfaces(nodeTemplateInfo, nodeTemplateJsonMap);
825                 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
826                 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
827                 setSubstitutions(substitutionMappings, nodeTemplateInfo);
828             } else {
829                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
830             }
831         } catch (ClassCastException e) {
832             BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
833             log.debug("error when creating capability, message:{}", e.getMessage(), e);
834             rollbackWithException(ActionStatus.INVALID_YAML);
835         }
836         return nodeTemplateInfo;
837     }
838
839     @SuppressWarnings("unchecked")
840     private void setSubstitutions(Map<String, Object> substitutionMappings, UploadComponentInstanceInfo nodeTemplateInfo) {
841         if (substitutionMappings != null) {
842             if (substitutionMappings.containsKey(CAPABILITIES.getElementName())) {
843                 nodeTemplateInfo.setCapabilitiesNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
844                     (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName())));
845             }
846             if (substitutionMappings.containsKey(REQUIREMENTS.getElementName())) {
847                 nodeTemplateInfo.setRequirementsNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
848                     (Map<String, List<String>>) substitutionMappings.get(REQUIREMENTS.getElementName())));
849             }
850         }
851     }
852
853     private void updateProperties(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
854         if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
855             Map<String, List<UploadPropInfo>> properties =
856                 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
857             if (!properties.isEmpty()) {
858                 nodeTemplateInfo.setProperties(properties);
859             }
860         }
861     }
862
863     private void updateAttributes(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
864         if (nodeTemplateJsonMap.containsKey(ATTRIBUTES.getElementName())) {
865             Map<String, UploadAttributeInfo> attributes = buildAttributeModuleFromYaml(nodeTemplateJsonMap);
866             if (!attributes.isEmpty()) {
867                 nodeTemplateInfo.setAttributes(attributes);
868             }
869         }
870     }
871
872     private void updateInterfaces(
873         UploadComponentInstanceInfo nodeTemplateInfo,
874         Map<String, Object> nodeTemplateJsonMap
875     ) {
876         if (nodeTemplateJsonMap.containsKey(INTERFACES.getElementName())) {
877             Map<String, UploadInterfaceInfo> interfaces = buildInterfacesModuleFromYaml(
878                 nodeTemplateJsonMap
879             );
880             if (!interfaces.isEmpty()) {
881                 nodeTemplateInfo.setInterfaces(interfaces);
882             }
883         }
884     }
885
886     private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
887         if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
888             Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
889             if (!eitherCapRes.isEmpty()) {
890                 nodeTemplateInfo.setCapabilities(eitherCapRes);
891             }
892         }
893     }
894
895     private void setArtifacts(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
896         if (nodeTemplateJsonMap.containsKey(ARTIFACTS.getElementName())) {
897             Map<String, Map<String, UploadArtifactInfo>> eitherArtifactsRes = createArtifactsModuleFromYaml(nodeTemplateJsonMap);
898             if (!eitherArtifactsRes.isEmpty()) {
899                 nodeTemplateInfo.setArtifacts(eitherArtifactsRes);
900             }
901         }
902     }
903
904     private void setRequirements(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
905         if (nodeTemplateJsonMap.containsKey(REQUIREMENTS.getElementName())) {
906             Map<String, List<UploadReqInfo>> regResponse = createReqModuleFromYaml(nodeTemplateJsonMap, nodeTemplateInfo.getName());
907             if (!regResponse.isEmpty()) {
908                 nodeTemplateInfo.setRequirements(regResponse);
909             }
910         }
911     }
912
913     private void setToscaResourceType(Map<String, String> createdNodesToscaResourceNames, UploadComponentInstanceInfo nodeTemplateInfo,
914                                       Map<String, Object> nodeTemplateJsonMap) {
915         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
916             String toscaResourceType = (String) nodeTemplateJsonMap.get(TYPE.getElementName());
917             if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) {
918                 toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType);
919             }
920             nodeTemplateInfo.setType(toscaResourceType);
921         }
922     }
923
924     private void setDirectives(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
925         List<String> directives = (List<String>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.DIRECTIVES.getElementName());
926         nodeTemplateInfo.setDirectives(directives);
927     }
928
929     private void setNodeFilter(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
930         if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())) {
931             nodeTemplateInfo.setUploadNodeFilterInfo(new NodeFilterUploadCreator()
932                 .createNodeFilterData(nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())));
933         }
934     }
935
936     @SuppressWarnings("unchecked")
937     private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap, String nodeName) {
938         Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
939         Either<List<Object>, ResultStatusEnum> requirementsListRes = findFirstToscaListElement(nodeTemplateJsonMap, REQUIREMENTS);
940         if (requirementsListRes.isLeft()) {
941             for (Object jsonReqObj : requirementsListRes.left().value()) {
942                 String reqName = ((Map<String, Object>) jsonReqObj).keySet().iterator().next();
943                 Object reqJson = ((Map<String, Object>) jsonReqObj).get(reqName);
944                 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
945             }
946         } else {
947             Either<Map<String, Object>, ResultStatusEnum> requirementsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, REQUIREMENTS);
948             if (requirementsMapRes.isLeft()) {
949                 for (Map.Entry<String, Object> entry : requirementsMapRes.left().value().entrySet()) {
950                     String reqName = entry.getKey();
951                     Object reqJson = entry.getValue();
952                     addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
953                 }
954             }
955         }
956         return moduleRequirements;
957     }
958
959     private void addModuleNodeTemplateReq(Map<String, List<UploadReqInfo>> moduleRequirements, Object requirementJson, String requirementName,
960                                           String nodeName) {
961         UploadReqInfo requirement = buildModuleNodeTemplateReg(requirementJson, nodeName);
962         requirement.setName(requirementName);
963         if (moduleRequirements.containsKey(requirementName)) {
964             moduleRequirements.get(requirementName).add(requirement);
965         } else {
966             List<UploadReqInfo> list = new ArrayList<>();
967             list.add(requirement);
968             moduleRequirements.put(requirementName, list);
969         }
970     }
971
972     @SuppressWarnings("unchecked")
973     private Map<String, Map<String, UploadArtifactInfo>> createArtifactsModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
974         Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts = new HashMap<>();
975         Either<List<Object>, ResultStatusEnum> artifactsListRes = findFirstToscaListElement(nodeTemplateJsonMap, ARTIFACTS);
976         if (artifactsListRes.isLeft()) {
977             for (Object jsonArtifactObj : artifactsListRes.left().value()) {
978                 String key = ((Map<String, Object>) jsonArtifactObj).keySet().iterator().next();
979                 Object artifactJson = ((Map<String, Object>) jsonArtifactObj).get(key);
980                 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, key);
981             }
982         } else {
983             Either<Map<String, Map<String, Object>>, ResultStatusEnum> artifactsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, ARTIFACTS);
984             if (artifactsMapRes.isLeft()) {
985                 for (Map.Entry<String, Map<String, Object>> entry : artifactsMapRes.left().value().entrySet()) {
986                     String artifactName = entry.getKey();
987                     Object artifactJson = entry.getValue();
988                     addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, artifactName);
989                 }
990             }
991         }
992         return moduleArtifacts;
993     }
994
995     private void addModuleNodeTemplateArtifacts(Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts, Object artifactJson,
996                                                 String artifactName) {
997         UploadArtifactInfo artifact = buildModuleNodeTemplateArtifact(artifactJson);
998         artifact.setName(artifactName);
999         if (moduleArtifacts.containsKey(ARTIFACTS.getElementName())) {
1000             moduleArtifacts.get(ARTIFACTS.getElementName()).put(artifactName, artifact);
1001         } else {
1002             Map<String, UploadArtifactInfo> map = new HashMap<>();
1003             map.put(artifactName, artifact);
1004             moduleArtifacts.put(ARTIFACTS.getElementName(), map);
1005         }
1006     }
1007
1008     @SuppressWarnings("unchecked")
1009     private UploadArtifactInfo buildModuleNodeTemplateArtifact(Object artifactObject) {
1010         UploadArtifactInfo artifactTemplateInfo = new UploadArtifactInfo();
1011         if (artifactObject instanceof Map) {
1012             fillArtifact(artifactTemplateInfo, (Map<String, Object>) artifactObject);
1013         }
1014         return artifactTemplateInfo;
1015     }
1016
1017     private void fillArtifact(UploadArtifactInfo artifactTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1018         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1019             artifactTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1020         }
1021         if (nodeTemplateJsonMap.containsKey(FILE.getElementName())) {
1022             artifactTemplateInfo.setFile((String) nodeTemplateJsonMap.get(FILE.getElementName()));
1023         }
1024         if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1025             Map<String, List<UploadPropInfo>> props =
1026                 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1027             if (!props.isEmpty()) {
1028                 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1029                 artifactTemplateInfo.setProperties(properties);
1030             }
1031         }
1032     }
1033
1034     @SuppressWarnings("unchecked")
1035     private Map<String, List<UploadCapInfo>> createCapModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
1036         Map<String, List<UploadCapInfo>> moduleCap = new HashMap<>();
1037         Map<String, Object> capabilities = (Map<String, Object>) nodeTemplateJsonMap.get(CAPABILITIES.getElementName());
1038         if (MapUtils.isNotEmpty(capabilities)) {
1039             for (Map.Entry<String, Object> entry : capabilities.entrySet()) {
1040                 String capName = entry.getKey();
1041                 Object capJson = entry.getValue();
1042                 addModuleNodeTemplateCap(moduleCap, capJson, capName);
1043             }
1044         }
1045         return moduleCap;
1046     }
1047
1048     private void addModuleNodeTemplateCap(Map<String, List<UploadCapInfo>> moduleCap, Object capJson, String key) {
1049         UploadCapInfo capabilityDef = buildModuleNodeTemplateCap(capJson);
1050         capabilityDef.setKey(key);
1051         if (moduleCap.containsKey(key)) {
1052             moduleCap.get(key).add(capabilityDef);
1053         } else {
1054             List<UploadCapInfo> list = new ArrayList<>();
1055             list.add(capabilityDef);
1056             moduleCap.put(key, list);
1057         }
1058     }
1059
1060     @SuppressWarnings("unchecked")
1061     private UploadCapInfo buildModuleNodeTemplateCap(Object capObject) {
1062         UploadCapInfo capTemplateInfo = new UploadCapInfo();
1063         if (capObject instanceof String) {
1064             String nodeTemplateJsonString = (String) capObject;
1065             capTemplateInfo.setNode(nodeTemplateJsonString);
1066         } else if (capObject instanceof Map) {
1067             fillCapability(capTemplateInfo, (Map<String, Object>) capObject);
1068         }
1069         return capTemplateInfo;
1070     }
1071
1072     private void fillCapability(UploadCapInfo capTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1073         if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1074             capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1075         }
1076         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1077             capTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1078         }
1079         if (nodeTemplateJsonMap.containsKey(VALID_SOURCE_TYPES.getElementName())) {
1080             Either<List<Object>, ResultStatusEnum> validSourceTypesRes = findFirstToscaListElement(nodeTemplateJsonMap, VALID_SOURCE_TYPES);
1081             if (validSourceTypesRes.isLeft()) {
1082                 capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream().map(Object::toString).collect(toList()));
1083             }
1084         }
1085         if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1086             Map<String, List<UploadPropInfo>> props =
1087                 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1088             if (!props.isEmpty()) {
1089                 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1090                 capTemplateInfo.setProperties(properties);
1091             }
1092         }
1093     }
1094
1095     @SuppressWarnings("unchecked")
1096     private UploadReqInfo buildModuleNodeTemplateReg(Object regObject, String nodeName) {
1097         UploadReqInfo regTemplateInfo = new UploadReqInfo();
1098         if (regObject instanceof String) {
1099             String nodeTemplateJsonString = (String) regObject;
1100             regTemplateInfo.setNode(nodeTemplateJsonString);
1101         } else if (regObject instanceof Map) {
1102             Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) regObject;
1103             if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1104                 regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1105             }
1106             if (nodeTemplateJsonMap.containsKey(CAPABILITY.getElementName())) {
1107                 regTemplateInfo.setCapabilityName((String) nodeTemplateJsonMap.get(CAPABILITY.getElementName()));
1108             }
1109             if (nodeTemplateJsonMap.containsKey(RELATIONSHIP.getElementName())) {
1110                 final String template = (String) nodeTemplateJsonMap.get(RELATIONSHIP.getElementName());
1111                 if (StringUtils.isNotEmpty(nodeName) && template.contains(nodeName)) {
1112                     regTemplateInfo.setRelationshipTemplate(template);
1113                 }
1114             }
1115         }
1116         return regTemplateInfo;
1117     }
1118
1119     private Map<String, UploadAttributeInfo> buildAttributeModuleFromYaml(
1120         Map<String, Object> nodeTemplateJsonMap) {
1121         Map<String, UploadAttributeInfo> moduleAttribute = new HashMap<>();
1122         Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(nodeTemplateJsonMap, ATTRIBUTES);
1123         if (toscaAttributes.isLeft()) {
1124             Map<String, Object> jsonAttributes = toscaAttributes.left().value();
1125             for (Map.Entry<String, Object> jsonAttributeObj : jsonAttributes.entrySet()) {
1126                 UploadAttributeInfo attributeDef = buildAttribute(jsonAttributeObj.getKey(), jsonAttributeObj.getValue());
1127                 moduleAttribute.put(attributeDef.getName(), attributeDef);
1128             }
1129         }
1130         return moduleAttribute;
1131     }
1132
1133     private UploadAttributeInfo buildAttribute(String attributeName, Object attributeValue) {
1134         UploadAttributeInfo attributeDef = new UploadAttributeInfo();
1135         attributeDef.setValue(attributeValue);
1136         attributeDef.setName(attributeName);
1137         return attributeDef;
1138     }
1139
1140     private Map<String, List<UploadPropInfo>> buildPropModuleFromYaml(final Map<String, Object> propertyMap) {
1141         final Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
1142         propertyMap.entrySet().forEach(propertyMapEntry -> addProperty(moduleProp, propertyMapEntry));
1143         return moduleProp;
1144     }
1145
1146     private Map<String, UploadInterfaceInfo> buildInterfacesModuleFromYaml(
1147         Map<String, Object> nodeTemplateJsonMap
1148     ) {
1149         Map<String, UploadInterfaceInfo> moduleInterfaces = new HashMap<>();
1150         Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = findFirstToscaMapElement(nodeTemplateJsonMap, INTERFACES);
1151         if (toscaInterfaces.isLeft()) {
1152             Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
1153             for (Map.Entry<String, Object> jsonInterfacesObj : jsonInterfaces.entrySet()) {
1154                 addInterfaces(moduleInterfaces, jsonInterfacesObj);
1155             }
1156         }
1157         return moduleInterfaces;
1158     }
1159
1160     private void addProperty(Map<String, List<UploadPropInfo>> moduleProp, Map.Entry<String, Object> jsonPropObj) {
1161         UploadPropInfo propertyDef = buildProperty(jsonPropObj.getKey(), jsonPropObj.getValue());
1162         if (moduleProp.containsKey(propertyDef.getName())) {
1163             moduleProp.get(propertyDef.getName()).add(propertyDef);
1164         } else {
1165             List<UploadPropInfo> list = new ArrayList<>();
1166             list.add(propertyDef);
1167             moduleProp.put(propertyDef.getName(), list);
1168         }
1169     }
1170
1171     private void addInterfaces(Map<String, UploadInterfaceInfo> moduleInterface, Map.Entry<String, Object> jsonPropObj) {
1172         UploadInterfaceInfo interfaceInfo = buildInterface(jsonPropObj.getKey(), jsonPropObj.getValue());
1173         moduleInterface.put(jsonPropObj.getKey(), interfaceInfo);
1174     }
1175
1176     @SuppressWarnings("unchecked")
1177     private UploadPropInfo buildProperty(String propName, Object propValueObj) {
1178         final var propertyDef = new UploadPropInfo();
1179         propertyDef.setValue(propValueObj);
1180         propertyDef.setName(propName);
1181         if (propValueObj instanceof Map) {
1182             final Map<String, Object> propValueMap = (Map<String, Object>) propValueObj;
1183             if (propValueMap.containsKey(TYPE.getElementName())) {
1184                 propertyDef.setType(propValueMap.get(TYPE.getElementName()).toString());
1185             }
1186             if (containsGetInput(propValueObj)) {
1187                 fillInputRecursively(propName, propValueMap, propertyDef);
1188             }
1189             if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(propValueObj)) {
1190                 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(propValueMap).ifPresent(propertyDef::setToscaFunction);
1191             } else {
1192                 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1193                 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1194                     Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1195                     if (existingSubPropertyToscaFunctions == null) {
1196                         propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1197                     } else {
1198                         propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1199                     }
1200                 }
1201             }
1202             if (propValueMap.containsKey(DESCRIPTION.getElementName())) {
1203                 propertyDef.setDescription((propValueMap).get(DESCRIPTION.getElementName()).toString());
1204             }
1205             if (propValueMap.containsKey(DEFAULT_VALUE.getElementName())) {
1206                 propertyDef.setValue(propValueMap.get(DEFAULT_VALUE.getElementName()));
1207             }
1208             if (propValueMap.containsKey(IS_PASSWORD.getElementName())) {
1209                 propertyDef.setPassword(Boolean.getBoolean(propValueMap.get(IS_PASSWORD.getElementName()).toString()));
1210             } else {
1211                 propertyDef.setValue(propValueObj);
1212             }
1213         } else if (propValueObj instanceof List) {
1214             fillInputsListRecursively(propertyDef, (List<Object>) propValueObj);
1215             propertyDef.setValue(propValueObj);
1216         }
1217         return propertyDef;
1218     }
1219     
1220     private Collection<SubPropertyToscaFunction> buildSubPropertyToscaFunctions(final Map<String, Object> propValueMap, final List<String> path) {
1221         Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = new ArrayList<>();
1222         propValueMap.entrySet().stream().filter(entry -> entry.getValue() instanceof Map).forEach(entry -> {
1223             List<String> subPropertyPath = new ArrayList<>(path);
1224             subPropertyPath.add(entry.getKey());
1225             if (ToscaFunctionType.findType(((Map<String, Object>) entry.getValue()).keySet().iterator().next()).isPresent()) {
1226                 Optional<ToscaFunction> toscaFunction =
1227                         toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
1228                 if (toscaFunction.isPresent()) {
1229                     SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
1230                     subPropertyToscaFunction.setToscaFunction(toscaFunction.get());
1231                     subPropertyToscaFunction.setSubPropertyPath(subPropertyPath);
1232                     subPropertyToscaFunctions.add(subPropertyToscaFunction);
1233                 }
1234             } else {
1235                 subPropertyToscaFunctions.addAll(buildSubPropertyToscaFunctions((Map<String, Object>) entry.getValue(), subPropertyPath));
1236             }
1237         });
1238         return subPropertyToscaFunctions;
1239     }
1240
1241     private UploadInterfaceInfo buildInterface(String interfaceName, Object interfaceValue) {
1242         UploadInterfaceInfo interfaceDef = new UploadInterfaceInfo();
1243         interfaceDef.setValue(interfaceValue);
1244         interfaceDef.setName(interfaceName);
1245         interfaceDef.setKey(interfaceName);
1246         Map<String, OperationDataDefinition> operations = new HashMap<>();
1247         if (interfaceValue instanceof Map) {
1248             Map<String, Object> operationsMap = (Map<String, Object>) interfaceValue;
1249             for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
1250                 OperationDataDefinition operationDef = new OperationDataDefinition();
1251                 operationDef.setName(operationEntry.getKey());
1252                 Map<String, Object> operationValue = (Map<String, Object>) operationEntry.getValue();
1253                 if (operationValue.containsKey(DESCRIPTION.getElementName())) {
1254                     operationDef.setDescription(operationValue.get(DESCRIPTION.getElementName()).toString());
1255                 }
1256                 operationDef.setImplementation(handleOperationImplementation(operationValue).orElse(new ArtifactDataDefinition()));
1257                 if (operationValue.containsKey(INPUTS.getElementName())) {
1258                     final Map<String, Object> interfaceInputs = (Map<String, Object>) operationValue.get(INPUTS.getElementName());
1259                     operationDef.setInputs(handleInterfaceOperationInputs(interfaceInputs));
1260                 }
1261                 operations.put(operationEntry.getKey(), operationDef);
1262             }
1263             interfaceDef.setOperations(operations);
1264             if (operationsMap.containsKey(TYPE.getElementName())) {
1265                 interfaceDef.setType(((Map<String, Object>) interfaceValue).get(TYPE.getElementName()).toString());
1266             }
1267         }
1268         return interfaceDef;
1269     }
1270
1271     private ListDataDefinition<OperationInputDefinition> handleInterfaceOperationInputs(final Map<String, Object> interfaceInputs) {
1272         final ListDataDefinition<OperationInputDefinition> inputs = new ListDataDefinition<>();
1273         for (final Entry<String, Object> interfaceInput : interfaceInputs.entrySet()) {
1274             final OperationInputDefinition operationInput = new OperationInputDefinition();
1275             operationInput.setUniqueId(UUID.randomUUID().toString());
1276             operationInput.setInputId(operationInput.getUniqueId());
1277             operationInput.setName(interfaceInput.getKey());
1278
1279             handleInputToscaDefinition(interfaceInput.getKey(), interfaceInput.getValue(), operationInput);
1280             inputs.add(operationInput);
1281         }
1282         return inputs;
1283     }
1284
1285     private void handleInputToscaDefinition(
1286         final String inputName,
1287         final Object value,
1288         final OperationInputDefinition operationInput
1289     ) {
1290         if (value instanceof Map) {
1291             log.debug("Creating interface operation input '{}'", inputName);
1292             Type type = new TypeToken<LinkedHashMap<String, Object>>() {
1293             }.getType();
1294             String stringValue = gson.toJson(value, type);
1295             if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) {
1296                 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value).ifPresent(operationInput::setToscaFunction);
1297             }
1298             operationInput.setValue(stringValue);
1299         }
1300         if (value instanceof String) {
1301             final String stringValue = (String) value;
1302             operationInput.setDefaultValue(stringValue);
1303             operationInput.setToscaDefaultValue(stringValue);
1304             operationInput.setValue(stringValue);
1305         }
1306         operationInput.setType("string");
1307         if (operationInput.getValue() == null) {
1308             operationInput.setValue(String.valueOf(value));
1309         }
1310     }
1311
1312     private Optional<ArtifactDataDefinition> handleOperationImplementation(
1313         final Map<String, Object> operationDefinitionMap
1314     ) {
1315         if (!operationDefinitionMap.containsKey(IMPLEMENTATION.getElementName())) {
1316             return Optional.empty();
1317         }
1318         final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition();
1319         if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
1320             ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("primary")) {
1321
1322             final Object primary = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("primary");
1323             if (primary instanceof Map) {
1324                 Map<String, Object> implDetails = (Map) primary;
1325
1326                 if (implDetails.get("file") != null) {
1327                     final String file = implDetails.get("file").toString();
1328                     artifactDataDefinition.setArtifactName(generateArtifactName(file));
1329                 }
1330                 if (implDetails.get("type") != null) {
1331                     artifactDataDefinition.setArtifactType(implDetails.get("type").toString());
1332                 }
1333                 if (implDetails.get("artifact_version") != null) {
1334                     artifactDataDefinition.setArtifactVersion(implDetails.get("artifact_version").toString());
1335                 }
1336
1337                 if (implDetails.get("properties") instanceof Map) {
1338                     Map<String, Object> properties = (Map<String, Object>) implDetails.get("properties");
1339                     properties.forEach((k, v) -> {
1340                         ToscaPropertyType type = getTypeFromObject(v);
1341                         if (type != null) {
1342                             PropertyDataDefinition propertyDef = new PropertyDataDefinition();
1343                             propertyDef.setName(k);
1344                             propertyDef.setValue(v.toString());
1345                             artifactDataDefinition.addProperty(propertyDef);
1346                         }
1347                     });
1348                 }
1349             } else {
1350                 artifactDataDefinition.setArtifactName(generateArtifactName(primary.toString()));
1351             }
1352         }
1353         if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof String) {
1354             final String implementation = (String) operationDefinitionMap.get(IMPLEMENTATION.getElementName());
1355             artifactDataDefinition.setArtifactName(generateArtifactName(implementation));
1356         }
1357         return Optional.of(artifactDataDefinition);
1358     }
1359
1360     private String generateArtifactName(final String name) {
1361         if (OperationArtifactUtil.artifactNameIsALiteralValue(name)) {
1362             return name;
1363         } else {
1364             return QUOTE + name + QUOTE;
1365         }
1366     }
1367
1368     private ToscaPropertyType getTypeFromObject(final Object value) {
1369         if (value instanceof String) {
1370             return ToscaPropertyType.STRING;
1371         }
1372         if (value instanceof Integer) {
1373             return ToscaPropertyType.INTEGER;
1374         }
1375         if (value instanceof Boolean) {
1376             return ToscaPropertyType.BOOLEAN;
1377         }
1378         if (value instanceof Float || value instanceof Double) {
1379             return ToscaPropertyType.FLOAT;
1380         }
1381         return null;
1382     }
1383
1384     @SuppressWarnings("unchecked")
1385     private boolean containsGetInput(Object propValue) {
1386         return ((Map<String, Object>) propValue).containsKey(GET_INPUT.getElementName()) || ImportUtils.containsGetInput(propValue);
1387     }
1388
1389     @SuppressWarnings("unchecked")
1390     private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
1391         int index = 0;
1392         for (Object objValue : propValueList) {
1393             if (objValue instanceof Map) {
1394                 Map<String, Object> objMap = (Map<String, Object>) objValue;
1395                 Map<String, Object> propValueMap = new HashMap<String, Object>();
1396                 propValueMap.put(String.valueOf(index),objValue);
1397                 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1398                 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1399                     Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1400                     if (existingSubPropertyToscaFunctions == null) {
1401                         propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1402                     } else {
1403                         propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1404                     }
1405                 }
1406                 if (objMap.containsKey(GET_INPUT.getElementName())) {
1407                     fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
1408                 } else {
1409                     Set<String> keys = objMap.keySet();
1410                     findAndFillInputsListRecursively(propertyDef, objMap, keys);
1411                 }
1412             } else if (objValue instanceof List) {
1413                 List<Object> propSubValueList = (List<Object>) objValue;
1414                 fillInputsListRecursively(propertyDef, propSubValueList);
1415             }
1416             index++;
1417         }
1418     }
1419
1420     @SuppressWarnings("unchecked")
1421     private void findAndFillInputsListRecursively(UploadPropInfo propertyDef, Map<String, Object> objMap, Set<String> keys) {
1422         for (String key : keys) {
1423             Object value = objMap.get(key);
1424             if (value instanceof Map) {
1425                 fillInputRecursively(key, (Map<String, Object>) value, propertyDef);
1426             } else if (value instanceof List) {
1427                 List<Object> propSubValueList = (List<Object>) value;
1428                 fillInputsListRecursively(propertyDef, propSubValueList);
1429             }
1430         }
1431     }
1432
1433     private void fillInputRecursively(String propName, Map<String, Object> propValue, UploadPropInfo propertyDef) {
1434         if (propValue.containsKey(GET_INPUT.getElementName())) {
1435             Object getInput = propValue.get(GET_INPUT.getElementName());
1436             GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition();
1437             List<GetInputValueDataDefinition> getInputs = propertyDef.getGet_input();
1438             if (getInputs == null) {
1439                 getInputs = new ArrayList<>();
1440             }
1441             if (getInput instanceof String) {
1442                 getInputInfo.setInputName((String) getInput);
1443                 getInputInfo.setPropName(propName);
1444             } else if (getInput instanceof List) {
1445                 fillInput(propName, getInput, getInputInfo);
1446             }
1447             getInputs.add(getInputInfo);
1448             propertyDef.setGet_input(getInputs);
1449             propertyDef.setValue(propValue);
1450         } else {
1451             findAndFillInputRecursively(propValue, propertyDef);
1452         }
1453     }
1454
1455     @SuppressWarnings("unchecked")
1456     private void findAndFillInputRecursively(Map<String, Object> propValue, UploadPropInfo propertyDef) {
1457         for (Map.Entry<String, Object> entry : propValue.entrySet()) {
1458             String propName = entry.getKey();
1459             Object value = entry.getValue();
1460             if (value instanceof Map) {
1461                 fillInputRecursively(propName, (Map<String, Object>) value, propertyDef);
1462             } else if (value instanceof List) {
1463                 fillInputsRecursively(propertyDef, propName, (List<Object>) value);
1464             }
1465         }
1466     }
1467
1468     private void fillInputsRecursively(UploadPropInfo propertyDef, String propName, List<Object> inputs) {
1469         inputs.stream().filter(Map.class::isInstance).forEach(o -> fillInputRecursively(propName, (Map<String, Object>) o, propertyDef));
1470     }
1471
1472     @SuppressWarnings("unchecked")
1473     private void fillInput(String propName, Object getInput, GetInputValueDataDefinition getInputInfo) {
1474         List<Object> getInputList = (List<Object>) getInput;
1475         getInputInfo.setPropName(propName);
1476         getInputInfo.setInputName((String) getInputList.get(0));
1477         if (getInputList.size() > 1) {
1478             Object indexObj = getInputList.get(1);
1479             if (indexObj instanceof Integer) {
1480                 getInputInfo.setIndexValue((Integer) indexObj);
1481             } else if (indexObj instanceof Float) {
1482                 int index = ((Float) indexObj).intValue();
1483                 getInputInfo.setIndexValue(index);
1484             } else if (indexObj instanceof Map && ((Map<String, Object>) indexObj).containsKey(GET_INPUT.getElementName())) {
1485                 Object index = ((Map<String, Object>) indexObj).get(GET_INPUT.getElementName());
1486                 GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition();
1487                 getInputInfoIndex.setInputName((String) index);
1488                 getInputInfoIndex.setPropName(propName);
1489                 getInputInfo.setGetInputIndex(getInputInfoIndex);
1490             }
1491             getInputInfo.setList(true);
1492         }
1493     }
1494
1495     private Object failIfNotTopologyTemplate(String fileName) {
1496         janusGraphDao.rollback();
1497         throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
1498     }
1499
1500     private void rollbackWithException(ActionStatus actionStatus, String... params) {
1501         janusGraphDao.rollback();
1502         throw new ByActionStatusComponentException(actionStatus, params);
1503     }
1504
1505     private void failOnMissingCapabilityTypes(GroupDefinition groupDefinition, List<String> missingCapTypes) {
1506         if (log.isDebugEnabled()) {
1507             log.debug(
1508                 "#failOnMissingCapabilityTypes - Failed to validate the capabilities of the group {}. The capability types {} are missing on the group type {}. ",
1509                 groupDefinition.getName(), missingCapTypes.toString(), groupDefinition.getType());
1510         }
1511         if (CollectionUtils.isNotEmpty(missingCapTypes)) {
1512             rollbackWithException(ActionStatus.MISSING_CAPABILITY_TYPE, missingCapTypes.toString());
1513         }
1514     }
1515
1516     private void failOnMissingCapabilityNames(GroupDefinition groupDefinition, List<String> missingCapNames) {
1517         if (log.isDebugEnabled()) {
1518             log.debug(
1519                 "#failOnMissingCapabilityNames - Failed to validate the capabilities of the group {}. The capabilities with the names {} are missing on the group type {}. ",
1520                 groupDefinition.getName(), missingCapNames.toString(), groupDefinition.getType());
1521         }
1522         rollbackWithException(ActionStatus.MISSING_CAPABILITIES, missingCapNames.toString(), CapabilityDataDefinition.OwnerType.GROUP.getValue(),
1523             groupDefinition.getName());
1524     }
1525 }