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