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