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