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