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