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