Update vulnerable package dependencies
[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 package org.openecomp.sdc.be.components.csar;
23
24 import static java.util.stream.Collectors.toList;
25 import static org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
26 import static org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
27 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaListElement;
28 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMapElement;
29 import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement;
30 import static org.openecomp.sdc.be.components.impl.ImportUtils.loadYamlAsStrictMap;
31 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
32 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITIES;
33 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITY;
34 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE;
35 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
36 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.FILE;
37 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GET_INPUT;
38 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GROUPS;
39 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IS_PASSWORD;
40 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.MEMBERS;
41 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE;
42 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES;
43 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.POLICIES;
44 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES;
45 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIREMENTS;
46 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS;
47 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TARGETS;
48 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE;
49 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;
50 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES;
51
52 import com.google.common.collect.Lists;
53 import com.google.common.collect.Maps;
54 import com.google.gson.Gson;
55 import fj.data.Either;
56 import java.util.ArrayList;
57 import java.util.Collection;
58 import java.util.Collections;
59 import java.util.EnumMap;
60 import java.util.HashMap;
61 import java.util.List;
62 import java.util.Map;
63 import java.util.Objects;
64 import java.util.Set;
65 import java.util.regex.Pattern;
66 import java.util.stream.Collectors;
67 import org.apache.commons.collections.CollectionUtils;
68 import org.apache.commons.collections.MapUtils;
69 import org.apache.commons.lang3.StringUtils;
70 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
71 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
72 import org.openecomp.sdc.be.components.impl.ImportUtils;
73 import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator;
74 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
75 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
76 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
77 import org.openecomp.sdc.be.config.BeEcompErrorManager;
78 import org.openecomp.sdc.be.dao.api.ActionStatus;
79 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
80 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
81 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
82 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
83 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
84 import org.openecomp.sdc.be.model.CapabilityDefinition;
85 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
86 import org.openecomp.sdc.be.model.GroupDefinition;
87 import org.openecomp.sdc.be.model.GroupTypeDefinition;
88 import org.openecomp.sdc.be.model.InputDefinition;
89 import org.openecomp.sdc.be.model.NodeTypeInfo;
90 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
91 import org.openecomp.sdc.be.model.PolicyDefinition;
92 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
93 import org.openecomp.sdc.be.model.Resource;
94 import org.openecomp.sdc.be.model.UploadArtifactInfo;
95 import org.openecomp.sdc.be.model.UploadCapInfo;
96 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
97 import org.openecomp.sdc.be.model.UploadPropInfo;
98 import org.openecomp.sdc.be.model.UploadReqInfo;
99 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
100 import org.openecomp.sdc.be.utils.TypeUtils;
101 import org.openecomp.sdc.common.log.wrappers.Logger;
102 import org.springframework.stereotype.Component;
103 import org.yaml.snakeyaml.parser.ParserException;
104
105 /**
106  * A handler class designed to parse the YAML file of the service template for a JAVA object
107  */
108 @Component
109 public class YamlTemplateParsingHandler {
110
111     private static final Pattern propertyValuePattern = Pattern.compile("[ ]*\\{[ ]*(str_replace=|token=|get_property=|concat=|get_attribute=)+");
112     private static final int SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX = 0;
113     private static final int SUB_MAPPING_CAPABILITY_NAME_IDX = 1;
114     private static final Logger log = Logger.getLogger(YamlTemplateParsingHandler.class);
115     private Gson gson = new Gson();
116     private JanusGraphDao janusGraphDao;
117     private GroupTypeBusinessLogic groupTypeBusinessLogic;
118     private AnnotationBusinessLogic annotationBusinessLogic;
119     private PolicyTypeBusinessLogic policyTypeBusinessLogic;
120
121     public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao, GroupTypeBusinessLogic groupTypeBusinessLogic,
122                                       AnnotationBusinessLogic annotationBusinessLogic, PolicyTypeBusinessLogic policyTypeBusinessLogic) {
123         this.janusGraphDao = janusGraphDao;
124         this.groupTypeBusinessLogic = groupTypeBusinessLogic;
125         this.annotationBusinessLogic = annotationBusinessLogic;
126         this.policyTypeBusinessLogic = policyTypeBusinessLogic;
127     }
128
129     public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames,
130                                                          Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName,
131                                                          org.openecomp.sdc.be.model.Component component) {
132         log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName);
133         Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName);
134         ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo();
135         findToscaElement(mappedToscaTemplate, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL).left().on(err -> failIfNotTopologyTemplate(fileName));
136         parsedToscaYamlInfo.setInputs(getInputs(mappedToscaTemplate));
137         parsedToscaYamlInfo.setInstances(getInstances(fileName, mappedToscaTemplate, createdNodesToscaResourceNames));
138         parsedToscaYamlInfo.setGroups(getGroups(fileName, mappedToscaTemplate));
139         if (component instanceof Resource) {
140             parsedToscaYamlInfo.setPolicies(getPolicies(fileName, mappedToscaTemplate));
141         }
142         log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName);
143         return parsedToscaYamlInfo;
144     }
145
146     private Map<String, Object> getMappedToscaTemplate(String fileName, String resourceYml, Map<String, NodeTypeInfo> nodeTypesInfo,
147                                                        String nodeName) {
148         Map<String, Object> mappedToscaTemplate;
149         if (isNodeExist(nodeTypesInfo, nodeName)) {
150             mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
151         } else {
152             mappedToscaTemplate = loadYaml(fileName, resourceYml);
153         }
154         return mappedToscaTemplate;
155     }
156
157     private Map<String, Object> loadYaml(String fileName, String resourceYml) {
158         Map<String, Object> mappedToscaTemplate = null;
159         try {
160             mappedToscaTemplate = loadYamlAsStrictMap(resourceYml);
161         } catch (ParserException e) {
162             log.debug("#getMappedToscaTemplate - Failed to load YAML file {}", fileName, e);
163             rollbackWithException(ActionStatus.TOSCA_PARSE_ERROR, fileName, e.getMessage());
164         }
165         return mappedToscaTemplate;
166     }
167
168     private boolean isNodeExist(Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
169         return nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName);
170     }
171
172     private Map<String, InputDefinition> getInputs(Map<String, Object> toscaJson) {
173         Map<String, InputDefinition> inputs = ImportUtils.getInputs(toscaJson, annotationBusinessLogic.getAnnotationTypeOperations()).left()
174             .on(err -> new HashMap<>());
175         annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(inputs);
176         return inputs;
177     }
178
179     private Map<String, PolicyDefinition> getPolicies(String fileName, Map<String, Object> toscaJson) {
180         Map<String, Object> foundPolicies = findFirstToscaMapElement(toscaJson, POLICIES).left().on(err -> logPoliciesNotFound(fileName));
181         if (MapUtils.isNotEmpty(foundPolicies)) {
182             return foundPolicies.entrySet().stream().map(this::createPolicy).collect(Collectors.toMap(PolicyDefinition::getName, p -> p));
183         }
184         return Collections.emptyMap();
185     }
186
187     private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue) {
188         PolicyDefinition emptyPolicyDef = new PolicyDefinition();
189         String policyName = policyNameValue.getKey();
190         emptyPolicyDef.setName(policyName);
191         try {
192             // 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.
193             if (policyNameValue.getValue() instanceof Map) {
194                 Map<String, Object> policyTemplateJsonMap = (Map<String, Object>) policyNameValue.getValue();
195                 validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap);
196             } else {
197                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
198             }
199         } catch (ClassCastException e) {
200             log.debug("#createPolicy - Failed to create the policy {}. The exception occurred", policyName, e);
201             rollbackWithException(ActionStatus.INVALID_YAML);
202         }
203         return emptyPolicyDef;
204     }
205
206     private Map<String, Object> logPoliciesNotFound(String fileName) {
207         log.debug("#logPoliciesNotFound - Policies were not found in the yaml template {}.", fileName);
208         return Collections.emptyMap();
209     }
210
211     private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, Map<String, Object> policyTemplateJsonMap) {
212         String policyTypeName = (String) policyTemplateJsonMap.get(TYPE.getElementName());
213         if (StringUtils.isEmpty(policyTypeName)) {
214             log.debug("#validateAndFillPolicy - The 'type' member is not found under policy {}", emptyPolicyDefinition.getName());
215             rollbackWithException(ActionStatus.POLICY_MISSING_POLICY_TYPE, emptyPolicyDefinition.getName());
216         }
217         emptyPolicyDefinition.setType(policyTypeName);
218         // set policy targets
219         emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap));
220         PolicyTypeDefinition policyTypeDefinition = validateGetPolicyTypeDefinition(policyTypeName);
221         // set policy properties
222         emptyPolicyDefinition.setProperties(validateFillPolicyProperties(policyTypeDefinition, policyTemplateJsonMap));
223     }
224
225     private PolicyTypeDefinition validateGetPolicyTypeDefinition(String policyType) {
226         PolicyTypeDefinition policyTypeDefinition = policyTypeBusinessLogic.getLatestPolicyTypeByType(policyType);
227         if (policyTypeDefinition == null) {
228             log.debug("#validateAndFillPolicy - The policy type {} not found", policyType);
229             rollbackWithException(ActionStatus.POLICY_TYPE_IS_INVALID, policyType);
230         }
231         return policyTypeDefinition;
232     }
233
234     private List<PropertyDataDefinition> validateFillPolicyProperties(PolicyTypeDefinition policyTypeDefinition,
235                                                                       Map<String, Object> policyTemplateJsonMap) {
236         if (MapUtils.isEmpty(policyTemplateJsonMap) || Objects.isNull(policyTypeDefinition)) {
237             return Collections.emptyList();
238         }
239         List<PropertyDataDefinition> propertyDataDefinitionList = new ArrayList<>();
240         Map<String, Object> propertiesMap = (Map<String, Object>) policyTemplateJsonMap.get(PROPERTIES.getElementName());
241         if (MapUtils.isEmpty(propertiesMap)) {
242             return Collections.emptyList();
243         }
244         if (CollectionUtils.isNotEmpty(policyTypeDefinition.getProperties())) {
245             propertyDataDefinitionList = policyTypeDefinition.getProperties().stream()
246                 .map(propertyDefinition -> setPropertyValue(propertiesMap, propertyDefinition)).collect(Collectors.toList());
247         }
248         return propertyDataDefinitionList;
249     }
250
251     private PropertyDataDefinition setPropertyValue(Map<String, Object> propertiesMap, PropertyDataDefinition srcPropertyDataDefinition) {
252         PropertyDataDefinition newPropertyDef = new PropertyDataDefinition(srcPropertyDataDefinition);
253         String propertyName = newPropertyDef.getName();
254         if (Objects.nonNull(propertiesMap.get(propertyName))) {
255             Object propValue = propertiesMap.get(propertyName);
256             newPropertyDef.setValue(PropertiesUtils.trimQuotes(gson.toJson(propValue)));
257         }
258         return newPropertyDef;
259     }
260
261     private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson) {
262         Map<PolicyTargetType, List<String>> targets = new EnumMap<>(PolicyTargetType.class);
263         if (policyTemplateJson.containsKey(TARGETS.getElementName()) && policyTemplateJson.get(TARGETS.getElementName()) instanceof List) {
264             List<String> targetsElement = (List<String>) policyTemplateJson.get(TARGETS.getElementName());
265             targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement);
266         }
267         return targets;
268     }
269
270     private Map<String, UploadComponentInstanceInfo> getInstances(String yamlName, Map<String, Object> toscaJson,
271                                                                   Map<String, String> createdNodesToscaResourceNames) {
272         Map<String, Object> nodeTemlates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES).left().on(err -> failIfNoNodeTemplates(yamlName));
273         return getInstances(toscaJson, createdNodesToscaResourceNames, nodeTemlates);
274     }
275
276     private Map<String, UploadComponentInstanceInfo> getInstances(Map<String, Object> toscaJson, Map<String, String> createdNodesToscaResourceNames,
277                                                                   Map<String, Object> nodeTemlates) {
278         Map<String, UploadComponentInstanceInfo> moduleComponentInstances;
279         Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
280         moduleComponentInstances = nodeTemlates.entrySet().stream()
281             .map(node -> buildModuleComponentInstanceInfo(node, substitutionMappings, createdNodesToscaResourceNames))
282             .collect(Collectors.toMap(UploadComponentInstanceInfo::getName, i -> i));
283         return moduleComponentInstances;
284     }
285
286     private Map<String, Object> getSubstitutionMappings(Map<String, Object> toscaJson) {
287         Map<String, Object> substitutionMappings = null;
288         Either<Map<String, Object>, ResultStatusEnum> eitherSubstitutionMappings = findFirstToscaMapElement(toscaJson, SUBSTITUTION_MAPPINGS);
289         if (eitherSubstitutionMappings.isLeft()) {
290             substitutionMappings = eitherSubstitutionMappings.left().value();
291         }
292         return substitutionMappings;
293     }
294
295     @SuppressWarnings("unchecked")
296     private Map<String, GroupDefinition> getGroups(String fileName, Map<String, Object> toscaJson) {
297         Map<String, Object> foundGroups = findFirstToscaMapElement(toscaJson, GROUPS).left().on(err -> logGroupsNotFound(fileName));
298         if (MapUtils.isNotEmpty(foundGroups) && matcheKey(foundGroups)) {
299             Map<String, GroupDefinition> groups = foundGroups.entrySet().stream().map(this::createGroup)
300                 .collect(Collectors.toMap(GroupDefinition::getName, g -> g));
301             Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
302             if (capabilitiesSubstitutionMappingsExist(substitutionMappings)) {
303                 groups.entrySet().forEach(entry -> updateCapabilitiesNames(entry.getValue(),
304                     getNamesToUpdate(entry.getKey(), (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName()))));
305             }
306             return groups;
307         }
308         return new HashMap<>();
309     }
310
311     private boolean matcheKey(Map<String, Object> foundGroups) {
312         if (foundGroups != null && !foundGroups.isEmpty()) {
313             for (Map.Entry<String, Object> stringObjectEntry : foundGroups.entrySet()) {
314                 String key = stringObjectEntry.getKey();
315                 if (key.contains("group")) {
316                     if (foundGroups.get(key) instanceof Map) {
317                         return true;
318                     }
319                 }
320             }
321         }
322         return false;
323     }
324
325     private Map<String, Object> logGroupsNotFound(String fileName) {
326         log.debug("#logGroupsNotFound - Groups were not found in the yaml template {}.", fileName);
327         return new HashMap<>();
328     }
329
330     private void updateCapabilitiesNames(GroupDefinition group, Map<String, String> capabilityNames) {
331         if (MapUtils.isNotEmpty(group.getCapabilities())) {
332             group.getCapabilities().values().stream().flatMap(Collection::stream).filter(cap -> capabilityNames.containsKey(cap.getName()))
333                 .forEach(cap -> cap.setName(capabilityNames.get(cap.getName())));
334         }
335     }
336
337     private Map<String, String> getNamesToUpdate(String name, Map<String, List<String>> pair) {
338         return pair.entrySet().stream().filter(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX).equalsIgnoreCase(name))
339             .collect(Collectors.toMap(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_NAME_IDX), Map.Entry::getKey, (n1, n2) -> n1));
340     }
341
342     private boolean capabilitiesSubstitutionMappingsExist(Map<String, Object> substitutionMappings) {
343         return substitutionMappings != null && substitutionMappings.containsKey(CAPABILITIES.getElementName());
344     }
345
346     private GroupDefinition createGroup(Map.Entry<String, Object> groupNameValue) {
347         GroupDefinition group = new GroupDefinition();
348         group.setName(groupNameValue.getKey());
349         try {
350             if (groupNameValue.getValue() instanceof Map) {
351                 Map<String, Object> groupTemplateJsonMap = (Map<String, Object>) groupNameValue.getValue();
352                 validateAndFillGroup(group, groupTemplateJsonMap);
353                 validateUpdateGroupProperties(group, groupTemplateJsonMap);
354                 validateUpdateGroupCapabilities(group, groupTemplateJsonMap);
355             } else {
356                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
357             }
358         } catch (ClassCastException e) {
359             log.debug("#createGroup - Failed to create the group {}. The exception occure", groupNameValue.getKey(), e);
360             rollbackWithException(ActionStatus.INVALID_YAML);
361         }
362         return group;
363     }
364
365     private Map<String, CapabilityDefinition> addCapabilities(Map<String, CapabilityDefinition> cap, Map<String, CapabilityDefinition> otherCap) {
366         cap.putAll(otherCap);
367         return cap;
368     }
369
370     private Map<String, CapabilityDefinition> addCapability(CapabilityDefinition cap) {
371         Map<String, CapabilityDefinition> map = Maps.newHashMap();
372         map.put(cap.getName(), cap);
373         return map;
374     }
375
376     private void setMembers(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
377         if (groupTemplateJsonMap.containsKey(MEMBERS.getElementName())) {
378             Object members = groupTemplateJsonMap.get(MEMBERS.getElementName());
379             if (members != null) {
380                 if (members instanceof List) {
381                     setMembersFromList(groupInfo, (List<?>) members);
382                 } else {
383                     log.debug("The 'members' member is not of type list under group {}", groupInfo.getName());
384                     rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
385                 }
386             }
387         }
388     }
389
390     private void setMembersFromList(GroupDefinition groupInfo, List<?> membersAsList) {
391         groupInfo.setMembers(membersAsList.stream().collect(Collectors.toMap(Object::toString, member -> "")));
392     }
393
394     @SuppressWarnings("unchecked")
395     private void validateUpdateGroupProperties(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
396         if (groupTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
397             Object propertiesElement = groupTemplateJsonMap.get(PROPERTIES.getElementName());
398             if (propertiesElement instanceof Map) {
399                 mergeGroupProperties(groupInfo, (Map<String, Object>) propertiesElement);
400             }
401         }
402     }
403
404     private void mergeGroupProperties(GroupDefinition groupInfo, Map<String, Object> parsedProperties) {
405         if (CollectionUtils.isNotEmpty(groupInfo.getProperties())) {
406             validateGroupProperties(parsedProperties, groupInfo);
407             groupInfo.getProperties().forEach(p -> mergeGroupProperty(p, parsedProperties));
408         }
409     }
410
411     private void mergeGroupProperty(PropertyDataDefinition property, Map<String, Object> parsedProperties) {
412         if (parsedProperties.containsKey(property.getName())) {
413             Object propValue = parsedProperties.get(property.getName());
414             if (valueNotContainsPattern(propertyValuePattern, propValue)) {
415                 setPropertyValueAndGetInputsValues(property, propValue);
416             }
417         }
418     }
419
420     private void setPropertyValueAndGetInputsValues(PropertyDataDefinition property, Object propValue) {
421         if (propValue != null) {
422             UploadPropInfo uploadPropInfo = buildProperty(property.getName(), propValue);
423             property.setValue(convertPropertyValue(ToscaPropertyType.isValidType(property.getType()), uploadPropInfo.getValue()));
424             property.setGetInputValues(uploadPropInfo.getGet_input());
425         }
426     }
427
428     private String convertPropertyValue(ToscaPropertyType type, Object value) {
429         String convertedValue = null;
430         if (value != null) {
431             if (type == null || value instanceof Map || value instanceof List) {
432                 convertedValue = gson.toJson(value);
433             } else {
434                 convertedValue = value.toString();
435             }
436         }
437         return convertedValue;
438     }
439
440     private void setDescription(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
441         if (groupTemplateJsonMap.containsKey(DESCRIPTION.getElementName())) {
442             groupInfo.setDescription((String) groupTemplateJsonMap.get(DESCRIPTION.getElementName()));
443         }
444     }
445
446     private void validateAndFillGroup(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
447         String type = (String) groupTemplateJsonMap.get(TYPE.getElementName());
448         if (StringUtils.isEmpty(type)) {
449             log.debug("#validateAndFillGroup - The 'type' member is not found under group {}", groupInfo.getName());
450             rollbackWithException(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupInfo.getName());
451         }
452         groupInfo.setType(type);
453         GroupTypeDefinition groupType = groupTypeBusinessLogic.getLatestGroupTypeByType(type);
454         if (groupType == null) {
455             log.debug("#validateAndFillGroup - The group type {} not found", groupInfo.getName());
456             rollbackWithException(ActionStatus.GROUP_TYPE_IS_INVALID, type);
457         }
458         groupInfo.convertFromGroupProperties(groupType.getProperties());
459         groupInfo.convertCapabilityDefinitions(groupType.getCapabilities());
460         setDescription(groupInfo, groupTemplateJsonMap);
461         setMembers(groupInfo, groupTemplateJsonMap);
462     }
463
464     @SuppressWarnings("unchecked")
465     private void validateUpdateGroupCapabilities(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
466         if (groupTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
467             Object capabilities = groupTemplateJsonMap.get(CAPABILITIES.getElementName());
468             if (capabilities instanceof List) {
469                 validateUpdateCapabilities(groupInfo, ((List<Object>) capabilities).stream().map(o -> buildGroupCapability(groupInfo, o))
470                     .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
471             } else if (capabilities instanceof Map) {
472                 validateUpdateCapabilities(groupInfo,
473                     ((Map<String, Object>) capabilities).entrySet().stream().map(e -> buildGroupCapability(groupInfo, e))
474                         .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
475             } else {
476                 log.debug("#setCapabilities - Failed to import the capabilities of the group {}. ", groupInfo.getName());
477                 rollbackWithException(ActionStatus.INVALID_YAML);
478             }
479         }
480     }
481
482     private void validateUpdateCapabilities(GroupDefinition groupInfo, Map<String, Map<String, CapabilityDefinition>> capabilityInfo) {
483         validateGroupCapabilities(groupInfo, capabilityInfo);
484         groupInfo.updateCapabilitiesProperties(capabilityInfo);
485     }
486
487     private void validateGroupCapabilities(GroupDefinition group, Map<String, Map<String, CapabilityDefinition>> parsedCapabilities) {
488         if (MapUtils.isNotEmpty(parsedCapabilities)) {
489             if (MapUtils.isEmpty(group.getCapabilities())) {
490                 failOnMissingCapabilityTypes(group, Lists.newArrayList(parsedCapabilities.keySet()));
491             }
492             List<String> missingCapTypes = parsedCapabilities.keySet().stream().filter(ct -> !group.getCapabilities().containsKey(ct))
493                 .collect(toList());
494             if (CollectionUtils.isNotEmpty(missingCapTypes)) {
495                 failOnMissingCapabilityTypes(group, missingCapTypes);
496             }
497             group.getCapabilities().entrySet().forEach(e -> validateCapabilities(group, e.getValue(), parsedCapabilities.get(e.getKey())));
498         }
499     }
500
501     private void validateCapabilities(GroupDefinition group, List<CapabilityDefinition> capabilities,
502                                       Map<String, CapabilityDefinition> parsedCapabilities) {
503         List<String> allowedCapNames = capabilities.stream().map(CapabilityDefinition::getName).distinct().collect(toList());
504         List<String> missingCapNames = parsedCapabilities.keySet().stream().filter(c -> !allowedCapNames.contains(c)).collect(toList());
505         if (CollectionUtils.isNotEmpty(missingCapNames)) {
506             failOnMissingCapabilityNames(group, missingCapNames);
507         }
508         validateCapabilitiesProperties(capabilities, parsedCapabilities);
509     }
510
511     private void validateCapabilitiesProperties(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
512         capabilities.forEach(c -> validateCapabilityProperties(c, parsedCapabilities.get(c.getName())));
513     }
514
515     private void validateCapabilityProperties(CapabilityDefinition capability, CapabilityDefinition parsedCapability) {
516         if (parsedCapability != null && parsedCapability.getProperties() != null) {
517             List<String> parsedPropertiesNames = parsedCapability.getProperties().stream().map(ComponentInstanceProperty::getName).collect(toList());
518             validateProperties(capability.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
519                 ActionStatus.PROPERTY_NOT_FOUND, capability.getName(), capability.getType());
520         }
521     }
522
523     private void validateGroupProperties(Map<String, Object> parsedProperties, GroupDefinition groupInfo) {
524         List<String> parsedPropertiesNames = parsedProperties.entrySet().stream().map(Map.Entry::getKey).collect(toList());
525         validateProperties(groupInfo.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
526             ActionStatus.GROUP_PROPERTY_NOT_FOUND, groupInfo.getName(), groupInfo.getType());
527     }
528
529     private void validateProperties(List<String> validProperties, List<String> parsedProperties, ActionStatus actionStatus, String name,
530                                     String type) {
531         if (CollectionUtils.isNotEmpty(parsedProperties)) {
532             verifyMissingProperties(actionStatus, name, type, parsedProperties.stream().filter(n -> !validProperties.contains(n)).collect(toList()));
533         }
534     }
535
536     private void verifyMissingProperties(ActionStatus actionStatus, String name, String type, List<String> missingProperties) {
537         if (CollectionUtils.isNotEmpty(missingProperties)) {
538             if (log.isDebugEnabled()) {
539                 log.debug("#validateProperties - Failed to validate properties. The properties {} are missing on {} of the type {}. ",
540                         missingProperties.toString(), name, type);
541             }
542             rollbackWithException(actionStatus, missingProperties.toString(), missingProperties.toString(), name, type);
543         }
544     }
545
546     @SuppressWarnings("unchecked")
547     private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Object capObject) {
548         if (!(capObject instanceof Map)) {
549             log.debug("#convertToGroupCapability - Failed to import the capability {}. ", capObject);
550             rollbackWithException(ActionStatus.INVALID_YAML);
551         }
552         return buildGroupCapability(groupInfo, ((Map<String, Object>) capObject).entrySet().iterator().next());
553     }
554
555     @SuppressWarnings("unchecked")
556     private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Map.Entry<String, Object> capEntry) {
557         CapabilityDefinition capability = new CapabilityDefinition();
558         capability.setOwnerType(CapabilityDataDefinition.OwnerType.GROUP);
559         capability.setName(capEntry.getKey());
560         capability.setParentName(capEntry.getKey());
561         capability.setOwnerId(groupInfo.getName());
562         if (!(capEntry.getValue() instanceof Map)) {
563             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
564             rollbackWithException(ActionStatus.INVALID_YAML);
565         }
566         Map<String, Object> capabilityValue = (Map<String, Object>) capEntry.getValue();
567         String type = (String) capabilityValue.get(TYPE.getElementName());
568         if (StringUtils.isEmpty(type)) {
569             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. Missing capability type. ", capEntry.getKey());
570             rollbackWithException(ActionStatus.INVALID_YAML);
571         }
572         capability.setType(type);
573         if (!(capabilityValue.get(PROPERTIES.getElementName()) instanceof Map)) {
574             log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
575             rollbackWithException(ActionStatus.INVALID_YAML);
576         }
577         Map<String, Object> properties = (Map<String, Object>) capabilityValue.get(PROPERTIES.getElementName());
578         capability.setProperties(properties.entrySet().stream().map(this::convertToProperty).collect(toList()));
579         return capability;
580     }
581
582     private ComponentInstanceProperty convertToProperty(Map.Entry<String, Object> e) {
583         ComponentInstanceProperty property = new ComponentInstanceProperty();
584         property.setName(e.getKey());
585         property.setValue((String) e.getValue());
586         return property;
587     }
588
589     @SuppressWarnings("unchecked")
590     private UploadComponentInstanceInfo buildModuleComponentInstanceInfo(Map.Entry<String, Object> nodeTemplateJsonEntry,
591                                                                          Map<String, Object> substitutionMappings,
592                                                                          Map<String, String> createdNodesToscaResourceNames) {
593         UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
594         nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey());
595         try {
596             if (nodeTemplateJsonEntry.getValue() instanceof String) {
597                 String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue();
598                 nodeTemplateInfo.setType(nodeTemplateJsonString);
599             } else if (nodeTemplateJsonEntry.getValue() instanceof Map) {
600                 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) nodeTemplateJsonEntry.getValue();
601                 setToscaResourceType(createdNodesToscaResourceNames, nodeTemplateInfo, nodeTemplateJsonMap);
602                 setRequirements(nodeTemplateInfo, nodeTemplateJsonMap);
603                 setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
604                 setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
605                 updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
606                 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
607                 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
608                 setSubstitutions(substitutionMappings, nodeTemplateInfo);
609             } else {
610                 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
611             }
612         } catch (ClassCastException e) {
613             BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
614             log.debug("error when creating capability, message:{}", e.getMessage(), e);
615             rollbackWithException(ActionStatus.INVALID_YAML);
616         }
617         return nodeTemplateInfo;
618     }
619
620     @SuppressWarnings("unchecked")
621     private void setSubstitutions(Map<String, Object> substitutionMappings, UploadComponentInstanceInfo nodeTemplateInfo) {
622         if (substitutionMappings != null) {
623             if (substitutionMappings.containsKey(CAPABILITIES.getElementName())) {
624                 nodeTemplateInfo.setCapabilitiesNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
625                     (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName())));
626             }
627             if (substitutionMappings.containsKey(REQUIREMENTS.getElementName())) {
628                 nodeTemplateInfo.setRequirementsNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
629                     (Map<String, List<String>>) substitutionMappings.get(REQUIREMENTS.getElementName())));
630             }
631         }
632     }
633
634     private void updateProperties(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
635         if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
636             Map<String, List<UploadPropInfo>> properties = buildPropModuleFromYaml(nodeTemplateJsonMap);
637             if (!properties.isEmpty()) {
638                 nodeTemplateInfo.setProperties(properties);
639             }
640         }
641     }
642
643     private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
644         if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
645             Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
646             if (!eitherCapRes.isEmpty()) {
647                 nodeTemplateInfo.setCapabilities(eitherCapRes);
648             }
649         }
650     }
651
652     private void setArtifacts(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
653         if (nodeTemplateJsonMap.containsKey(ARTIFACTS.getElementName())) {
654             Map<String, Map<String, UploadArtifactInfo>> eitherArtifactsRes = createArtifactsModuleFromYaml(nodeTemplateJsonMap);
655             if (!eitherArtifactsRes.isEmpty()) {
656                 nodeTemplateInfo.setArtifacts(eitherArtifactsRes);
657             }
658         }
659     }
660
661     private void setRequirements(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
662         if (nodeTemplateJsonMap.containsKey(REQUIREMENTS.getElementName())) {
663             Map<String, List<UploadReqInfo>> regResponse = createReqModuleFromYaml(nodeTemplateJsonMap);
664             if (!regResponse.isEmpty()) {
665                 nodeTemplateInfo.setRequirements(regResponse);
666             }
667         }
668     }
669
670     private void setToscaResourceType(Map<String, String> createdNodesToscaResourceNames, UploadComponentInstanceInfo nodeTemplateInfo,
671                                       Map<String, Object> nodeTemplateJsonMap) {
672         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
673             String toscaResourceType = (String) nodeTemplateJsonMap.get(TYPE.getElementName());
674             if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) {
675                 toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType);
676             }
677             nodeTemplateInfo.setType(toscaResourceType);
678         }
679     }
680
681     private void setDirectives(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
682         List<String> directives = (List<String>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.DIRECTIVES.getElementName());
683         nodeTemplateInfo.setDirectives(directives);
684     }
685
686     private void setNodeFilter(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
687         if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())) {
688             nodeTemplateInfo.setUploadNodeFilterInfo(new NodeFilterUploadCreator()
689                 .createNodeFilterData(nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())));
690         }
691     }
692
693     @SuppressWarnings("unchecked")
694     private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
695         Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
696         Either<List<Object>, ResultStatusEnum> requirementsListRes = findFirstToscaListElement(nodeTemplateJsonMap, REQUIREMENTS);
697         if (requirementsListRes.isLeft()) {
698             for (Object jsonReqObj : requirementsListRes.left().value()) {
699                 String reqName = ((Map<String, Object>) jsonReqObj).keySet().iterator().next();
700                 Object reqJson = ((Map<String, Object>) jsonReqObj).get(reqName);
701                 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName);
702             }
703         } else {
704             Either<Map<String, Object>, ResultStatusEnum> requirementsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, REQUIREMENTS);
705             if (requirementsMapRes.isLeft()) {
706                 for (Map.Entry<String, Object> entry : requirementsMapRes.left().value().entrySet()) {
707                     String reqName = entry.getKey();
708                     Object reqJson = entry.getValue();
709                     addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName);
710                 }
711             }
712         }
713         return moduleRequirements;
714     }
715
716     private void addModuleNodeTemplateReq(Map<String, List<UploadReqInfo>> moduleRequirements, Object requirementJson, String requirementName) {
717         UploadReqInfo requirement = buildModuleNodeTemplateReg(requirementJson);
718         requirement.setName(requirementName);
719         if (moduleRequirements.containsKey(requirementName)) {
720             moduleRequirements.get(requirementName).add(requirement);
721         } else {
722             List<UploadReqInfo> list = new ArrayList<>();
723             list.add(requirement);
724             moduleRequirements.put(requirementName, list);
725         }
726     }
727
728     @SuppressWarnings("unchecked")
729     private Map<String, Map<String, UploadArtifactInfo>> createArtifactsModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
730         Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts = new HashMap<>();
731         Either<List<Object>, ResultStatusEnum> artifactsListRes = findFirstToscaListElement(nodeTemplateJsonMap, ARTIFACTS);
732         if (artifactsListRes.isLeft()) {
733             for (Object jsonArtifactObj : artifactsListRes.left().value()) {
734                 String key = ((Map<String, Object>) jsonArtifactObj).keySet().iterator().next();
735                 Object artifactJson = ((Map<String, Object>) jsonArtifactObj).get(key);
736                 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, key);
737             }
738         } else {
739             Either<Map<String, Map<String, Object>>, ResultStatusEnum> artifactsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, ARTIFACTS);
740             if (artifactsMapRes.isLeft()) {
741                 for (Map.Entry<String, Map<String, Object>> entry : artifactsMapRes.left().value().entrySet()) {
742                     String artifactName = entry.getKey();
743                     Object artifactJson = entry.getValue();
744                     addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, artifactName);
745                 }
746             }
747         }
748         return moduleArtifacts;
749     }
750
751     private void addModuleNodeTemplateArtifacts(Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts, Object artifactJson,
752                                                 String artifactName) {
753         UploadArtifactInfo artifact = buildModuleNodeTemplateArtifact(artifactJson);
754         artifact.setName(artifactName);
755         if (moduleArtifacts.containsKey(ARTIFACTS.getElementName())) {
756             moduleArtifacts.get(ARTIFACTS.getElementName()).put(artifactName, artifact);
757         } else {
758             Map<String, UploadArtifactInfo> map = new HashMap<>();
759             map.put(artifactName, artifact);
760             moduleArtifacts.put(ARTIFACTS.getElementName(), map);
761         }
762     }
763
764     @SuppressWarnings("unchecked")
765     private UploadArtifactInfo buildModuleNodeTemplateArtifact(Object artifactObject) {
766         UploadArtifactInfo artifactTemplateInfo = new UploadArtifactInfo();
767         if (artifactObject instanceof Map) {
768             fillArtifact(artifactTemplateInfo, (Map<String, Object>) artifactObject);
769         }
770         return artifactTemplateInfo;
771     }
772
773     private void fillArtifact(UploadArtifactInfo artifactTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
774         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
775             artifactTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
776         }
777         if (nodeTemplateJsonMap.containsKey(FILE.getElementName())) {
778             artifactTemplateInfo.setFile((String) nodeTemplateJsonMap.get(FILE.getElementName()));
779         }
780     }
781
782     @SuppressWarnings("unchecked")
783     private Map<String, List<UploadCapInfo>> createCapModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
784         Map<String, List<UploadCapInfo>> moduleCap = new HashMap<>();
785         Either<List<Object>, ResultStatusEnum> capabilitiesListRes = findFirstToscaListElement(nodeTemplateJsonMap, CAPABILITIES);
786         if (capabilitiesListRes.isLeft()) {
787             for (Object jsonCapObj : capabilitiesListRes.left().value()) {
788                 String key = ((Map<String, Object>) jsonCapObj).keySet().iterator().next();
789                 Object capJson = ((Map<String, Object>) jsonCapObj).get(key);
790                 addModuleNodeTemplateCap(moduleCap, capJson, key);
791             }
792         } else {
793             Either<Map<String, Object>, ResultStatusEnum> capabilitiesMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, CAPABILITIES);
794             if (capabilitiesMapRes.isLeft()) {
795                 for (Map.Entry<String, Object> entry : capabilitiesMapRes.left().value().entrySet()) {
796                     String capName = entry.getKey();
797                     Object capJson = entry.getValue();
798                     addModuleNodeTemplateCap(moduleCap, capJson, capName);
799                 }
800             }
801         }
802         return moduleCap;
803     }
804
805     private void addModuleNodeTemplateCap(Map<String, List<UploadCapInfo>> moduleCap, Object capJson, String key) {
806         UploadCapInfo capabilityDef = buildModuleNodeTemplateCap(capJson);
807         capabilityDef.setKey(key);
808         if (moduleCap.containsKey(key)) {
809             moduleCap.get(key).add(capabilityDef);
810         } else {
811             List<UploadCapInfo> list = new ArrayList<>();
812             list.add(capabilityDef);
813             moduleCap.put(key, list);
814         }
815     }
816
817     @SuppressWarnings("unchecked")
818     private UploadCapInfo buildModuleNodeTemplateCap(Object capObject) {
819         UploadCapInfo capTemplateInfo = new UploadCapInfo();
820         if (capObject instanceof String) {
821             String nodeTemplateJsonString = (String) capObject;
822             capTemplateInfo.setNode(nodeTemplateJsonString);
823         } else if (capObject instanceof Map) {
824             fillCapability(capTemplateInfo, (Map<String, Object>) capObject);
825         }
826         return capTemplateInfo;
827     }
828
829     private void fillCapability(UploadCapInfo capTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
830         if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
831             capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
832         }
833         if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
834             capTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
835         }
836         if (nodeTemplateJsonMap.containsKey(VALID_SOURCE_TYPES.getElementName())) {
837             Either<List<Object>, ResultStatusEnum> validSourceTypesRes = findFirstToscaListElement(nodeTemplateJsonMap, VALID_SOURCE_TYPES);
838             if (validSourceTypesRes.isLeft()) {
839                 capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream().map(Object::toString).collect(toList()));
840             }
841         }
842         if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
843             Map<String, List<UploadPropInfo>> props = buildPropModuleFromYaml(nodeTemplateJsonMap);
844             if (!props.isEmpty()) {
845                 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
846                 capTemplateInfo.setProperties(properties);
847             }
848         }
849     }
850
851     @SuppressWarnings("unchecked")
852     private UploadReqInfo buildModuleNodeTemplateReg(Object regObject) {
853         UploadReqInfo regTemplateInfo = new UploadReqInfo();
854         if (regObject instanceof String) {
855             String nodeTemplateJsonString = (String) regObject;
856             regTemplateInfo.setNode(nodeTemplateJsonString);
857         } else if (regObject instanceof Map) {
858             Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) regObject;
859             if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
860                 regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
861             }
862             if (nodeTemplateJsonMap.containsKey(CAPABILITY.getElementName())) {
863                 regTemplateInfo.setCapabilityName((String) nodeTemplateJsonMap.get(CAPABILITY.getElementName()));
864             }
865         }
866         return regTemplateInfo;
867     }
868
869     private Map<String, List<UploadPropInfo>> buildPropModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
870         Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
871         Either<Map<String, Object>, ResultStatusEnum> toscaProperties = findFirstToscaMapElement(nodeTemplateJsonMap, PROPERTIES);
872         if (toscaProperties.isLeft()) {
873             Map<String, Object> jsonProperties = toscaProperties.left().value();
874             for (Map.Entry<String, Object> jsonPropObj : jsonProperties.entrySet()) {
875                 if (valueNotContainsPattern(propertyValuePattern, jsonPropObj.getValue())) {
876                     addProperty(moduleProp, jsonPropObj);
877                 }
878             }
879         }
880         return moduleProp;
881     }
882
883     private void addProperty(Map<String, List<UploadPropInfo>> moduleProp, Map.Entry<String, Object> jsonPropObj) {
884         UploadPropInfo propertyDef = buildProperty(jsonPropObj.getKey(), jsonPropObj.getValue());
885         if (moduleProp.containsKey(propertyDef.getName())) {
886             moduleProp.get(propertyDef.getName()).add(propertyDef);
887         } else {
888             List<UploadPropInfo> list = new ArrayList<>();
889             list.add(propertyDef);
890             moduleProp.put(propertyDef.getName(), list);
891         }
892     }
893
894     @SuppressWarnings("unchecked")
895     private UploadPropInfo buildProperty(String propName, Object propValue) {
896         UploadPropInfo propertyDef = new UploadPropInfo();
897         propertyDef.setValue(propValue);
898         propertyDef.setName(propName);
899         if (propValue instanceof Map) {
900             if (((Map<String, Object>) propValue).containsKey(TYPE.getElementName())) {
901                 propertyDef.setType(((Map<String, Object>) propValue).get(TYPE.getElementName()).toString());
902             }
903             if (containsGetInput(propValue)) {
904                 fillInputRecursively(propName, (Map<String, Object>) propValue, propertyDef);
905             }
906             if (((Map<String, Object>) propValue).containsKey(DESCRIPTION.getElementName())) {
907                 propertyDef.setDescription(((Map<String, Object>) propValue).get(DESCRIPTION.getElementName()).toString());
908             }
909             if (((Map<String, Object>) propValue).containsKey(DEFAULT_VALUE.getElementName())) {
910                 propertyDef.setValue(((Map<String, Object>) propValue).get(DEFAULT_VALUE.getElementName()));
911             }
912             if (((Map<String, Object>) propValue).containsKey(IS_PASSWORD.getElementName())) {
913                 propertyDef.setPassword(Boolean.getBoolean(((Map<String, Object>) propValue).get(IS_PASSWORD.getElementName()).toString()));
914             } else {
915                 propertyDef.setValue(propValue);
916             }
917         } else if (propValue instanceof List) {
918             List<Object> propValueList = (List<Object>) propValue;
919             fillInputsListRecursively(propertyDef, propValueList);
920             propertyDef.setValue(propValue);
921         }
922         return propertyDef;
923     }
924
925     @SuppressWarnings("unchecked")
926     private boolean containsGetInput(Object propValue) {
927         return ((Map<String, Object>) propValue).containsKey(GET_INPUT.getElementName()) || ImportUtils.containsGetInput(propValue);
928     }
929
930     @SuppressWarnings("unchecked")
931     private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
932         for (Object objValue : propValueList) {
933             if (objValue instanceof Map) {
934                 Map<String, Object> objMap = (Map<String, Object>) objValue;
935                 if (objMap.containsKey(GET_INPUT.getElementName())) {
936                     fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
937                 } else {
938                     Set<String> keys = objMap.keySet();
939                     findAndFillInputsListRecursively(propertyDef, objMap, keys);
940                 }
941             } else if (objValue instanceof List) {
942                 List<Object> propSubValueList = (List<Object>) objValue;
943                 fillInputsListRecursively(propertyDef, propSubValueList);
944             }
945         }
946     }
947
948     @SuppressWarnings("unchecked")
949     private void findAndFillInputsListRecursively(UploadPropInfo propertyDef, Map<String, Object> objMap, Set<String> keys) {
950         for (String key : keys) {
951             Object value = objMap.get(key);
952             if (value instanceof Map) {
953                 fillInputRecursively(key, (Map<String, Object>) value, propertyDef);
954             } else if (value instanceof List) {
955                 List<Object> propSubValueList = (List<Object>) value;
956                 fillInputsListRecursively(propertyDef, propSubValueList);
957             }
958         }
959     }
960
961     private void fillInputRecursively(String propName, Map<String, Object> propValue, UploadPropInfo propertyDef) {
962         if (propValue.containsKey(GET_INPUT.getElementName())) {
963             Object getInput = propValue.get(GET_INPUT.getElementName());
964             GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition();
965             List<GetInputValueDataDefinition> getInputs = propertyDef.getGet_input();
966             if (getInputs == null) {
967                 getInputs = new ArrayList<>();
968             }
969             if (getInput instanceof String) {
970                 getInputInfo.setInputName((String) getInput);
971                 getInputInfo.setPropName(propName);
972             } else if (getInput instanceof List) {
973                 fillInput(propName, getInput, getInputInfo);
974             }
975             getInputs.add(getInputInfo);
976             propertyDef.setGet_input(getInputs);
977             propertyDef.setValue(propValue);
978         } else {
979             findAndFillInputRecursively(propValue, propertyDef);
980         }
981     }
982
983     @SuppressWarnings("unchecked")
984     private void findAndFillInputRecursively(Map<String, Object> propValue, UploadPropInfo propertyDef) {
985         for (Map.Entry<String,Object> entry : propValue.entrySet()) {
986             String propName = entry.getKey();
987             Object value = entry.getValue();
988             if (value instanceof Map) {
989                 fillInputRecursively(propName, (Map<String, Object>) value, propertyDef);
990             } else if (value instanceof List) {
991                 fillInputsRecursively(propertyDef, propName, (List<Object>) value);
992             }
993         }
994     }
995
996     private void fillInputsRecursively(UploadPropInfo propertyDef, String propName, List<Object> inputs) {
997         inputs.stream()
998                 .filter(Map.class::isInstance)
999                 .forEach(o -> fillInputRecursively(propName, (Map<String, Object>) o, propertyDef));
1000     }
1001
1002     @SuppressWarnings("unchecked")
1003     private void fillInput(String propName, Object getInput, GetInputValueDataDefinition getInputInfo) {
1004         List<Object> getInputList = (List<Object>) getInput;
1005         getInputInfo.setPropName(propName);
1006         getInputInfo.setInputName((String) getInputList.get(0));
1007         if (getInputList.size() > 1) {
1008             Object indexObj = getInputList.get(1);
1009             if (indexObj instanceof Integer) {
1010                 getInputInfo.setIndexValue((Integer) indexObj);
1011             } else if (indexObj instanceof Float) {
1012                 int index = ((Float) indexObj).intValue();
1013                 getInputInfo.setIndexValue(index);
1014             } else if (indexObj instanceof Map && ((Map<String, Object>) indexObj).containsKey(GET_INPUT.getElementName())) {
1015                 Object index = ((Map<String, Object>) indexObj).get(GET_INPUT.getElementName());
1016                 GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition();
1017                 getInputInfoIndex.setInputName((String) index);
1018                 getInputInfoIndex.setPropName(propName);
1019                 getInputInfo.setGetInputIndex(getInputInfoIndex);
1020             }
1021             getInputInfo.setList(true);
1022         }
1023     }
1024
1025     private boolean valueNotContainsPattern(Pattern pattern, Object propValue) {
1026         return propValue == null || !pattern.matcher(propValue.toString()).find();
1027     }
1028
1029     private Map<String, Object> failIfNoNodeTemplates(String fileName) {
1030         janusGraphDao.rollback();
1031         throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
1032     }
1033
1034     private Object failIfNotTopologyTemplate(String fileName) {
1035         janusGraphDao.rollback();
1036         throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
1037     }
1038
1039     private void rollbackWithException(ActionStatus actionStatus, String... params) {
1040         janusGraphDao.rollback();
1041         throw new ByActionStatusComponentException(actionStatus, params);
1042     }
1043
1044     private void failOnMissingCapabilityTypes(GroupDefinition groupDefinition, List<String> missingCapTypes) {
1045         if (log.isDebugEnabled()) {
1046             log.debug(
1047                     "#failOnMissingCapabilityTypes - Failed to validate the capabilities of the group {}. The capability types {} are missing on the group type {}. ",
1048                     groupDefinition.getName(), missingCapTypes.toString(), groupDefinition.getType());
1049         }
1050         if (CollectionUtils.isNotEmpty(missingCapTypes)) {
1051             rollbackWithException(ActionStatus.MISSING_CAPABILITY_TYPE, missingCapTypes.toString());
1052         }
1053     }
1054
1055     private void failOnMissingCapabilityNames(GroupDefinition groupDefinition, List<String> missingCapNames) {
1056         if (log.isDebugEnabled()) {
1057             log.debug(
1058                     "#failOnMissingCapabilityNames - Failed to validate the capabilities of the group {}. The capabilities with the names {} are missing on the group type {}. ",
1059                     groupDefinition.getName(), missingCapNames.toString(), groupDefinition.getType());
1060         }
1061         rollbackWithException(ActionStatus.MISSING_CAPABILITIES, missingCapNames.toString(), CapabilityDataDefinition.OwnerType.GROUP.getValue(),
1062             groupDefinition.getName());
1063     }
1064 }