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