2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
22 package org.openecomp.sdc.be.components.csar;
24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Maps;
26 import com.google.gson.Gson;
27 import fj.data.Either;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.collections.MapUtils;
30 import org.apache.commons.lang.StringUtils;
31 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
33 import org.openecomp.sdc.be.components.impl.ImportUtils;
34 import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator;
35 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager;
37 import org.openecomp.sdc.be.dao.api.ActionStatus;
38 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
39 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
41 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
42 import org.openecomp.sdc.be.model.*;
43 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
44 import org.openecomp.sdc.be.utils.TypeUtils;
45 import org.openecomp.sdc.common.log.wrappers.Logger;
46 import org.springframework.stereotype.Component;
47 import org.yaml.snakeyaml.parser.ParserException;
50 import java.util.regex.Pattern;
51 import java.util.stream.Collectors;
53 import static java.util.stream.Collectors.toList;
54 import static org.openecomp.sdc.be.components.impl.ImportUtils.*;
55 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.*;
58 * A handler class designed to parse the YAML file of the service template for a JAVA object
61 public class YamlTemplateParsingHandler {
63 private static final Pattern propertyValuePattern = Pattern.compile("[ ]*\\{[ ]*(str_replace=|token=|get_property=|concat=|get_attribute=)+");
64 private static final int SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX = 0;
65 private static final int SUB_MAPPING_CAPABILITY_NAME_IDX = 1;
66 private static final Logger log = Logger.getLogger(YamlTemplateParsingHandler.class);
69 private Gson gson = new Gson();
70 private JanusGraphDao janusGraphDao;
71 private GroupTypeBusinessLogic groupTypeBusinessLogic;
72 private AnnotationBusinessLogic annotationBusinessLogic;
74 public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao,
75 GroupTypeBusinessLogic groupTypeBusinessLogic, AnnotationBusinessLogic annotationBusinessLogic) {
76 this.janusGraphDao = janusGraphDao;
77 this.groupTypeBusinessLogic = groupTypeBusinessLogic;
78 this.annotationBusinessLogic = annotationBusinessLogic;
81 public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames,
82 Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
83 log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName);
84 Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName);
85 ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo();
86 findToscaElement(mappedToscaTemplate, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
88 .on(err -> failIfNotTopologyTemplate(fileName));
90 parsedToscaYamlInfo.setInputs(getInputs(mappedToscaTemplate));
91 parsedToscaYamlInfo.setInstances(getInstances(fileName, mappedToscaTemplate, createdNodesToscaResourceNames));
92 parsedToscaYamlInfo.setGroups(getGroups(fileName, mappedToscaTemplate));
93 log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName);
94 return parsedToscaYamlInfo;
97 private Map<String, Object> getMappedToscaTemplate(String fileName, String resourceYml, Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
98 Map<String, Object> mappedToscaTemplate;
99 if (isNodeExist(nodeTypesInfo, nodeName)) {
100 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
102 mappedToscaTemplate = loadYaml(fileName, resourceYml);
104 return mappedToscaTemplate;
107 private Map<String, Object> loadYaml(String fileName, String resourceYml) {
108 Map<String, Object> mappedToscaTemplate = null;
110 mappedToscaTemplate = loadYamlAsStrictMap(resourceYml);
111 } catch (ParserException e) {
112 log.debug("#getMappedToscaTemplate - Failed to load YAML file {}", fileName, e);
113 rollbackWithException(ActionStatus.TOSCA_PARSE_ERROR, fileName, e.getMessage());
115 return mappedToscaTemplate;
118 private boolean isNodeExist(Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
119 return nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName);
122 private Map<String, InputDefinition> getInputs(Map<String, Object> toscaJson) {
123 Map<String, InputDefinition> inputs = ImportUtils.getInputs(toscaJson, annotationBusinessLogic.getAnnotationTypeOperations())
125 .on(err -> new HashMap<>());
126 annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(inputs);
130 private Map<String, UploadComponentInstanceInfo> getInstances(String yamlName, Map<String, Object> toscaJson, Map<String, String> createdNodesToscaResourceNames) {
132 Map<String, Object> nodeTemlates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES)
134 .on(err -> failIfNoNodeTemplates(yamlName));
136 return getInstances(toscaJson, createdNodesToscaResourceNames, nodeTemlates);
139 private Map<String, UploadComponentInstanceInfo> getInstances(Map<String, Object> toscaJson, Map<String, String> createdNodesToscaResourceNames, Map<String, Object> nodeTemlates) {
140 Map<String, UploadComponentInstanceInfo> moduleComponentInstances;
141 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
142 moduleComponentInstances = nodeTemlates.entrySet()
144 .map(node -> buildModuleComponentInstanceInfo(node, substitutionMappings, createdNodesToscaResourceNames))
145 .collect(Collectors.toMap(UploadComponentInstanceInfo::getName, i -> i));
146 return moduleComponentInstances;
149 private Map<String, Object> getSubstitutionMappings(Map<String, Object> toscaJson) {
150 Map<String, Object> substitutionMappings = null;
151 Either<Map<String, Object>, ResultStatusEnum> eitherSubstitutionMappings = findFirstToscaMapElement(toscaJson, SUBSTITUTION_MAPPINGS);
152 if (eitherSubstitutionMappings.isLeft()) {
153 substitutionMappings = eitherSubstitutionMappings.left().value();
155 return substitutionMappings;
158 @SuppressWarnings("unchecked")
159 private Map<String, GroupDefinition> getGroups(String fileName, Map<String, Object> toscaJson) {
161 Map<String, Object> foundGroups = findFirstToscaMapElement(toscaJson, GROUPS)
163 .on(err -> logGroupsNotFound(fileName));
165 if (MapUtils.isNotEmpty(foundGroups)) {
166 Map<String, GroupDefinition> groups = foundGroups
169 .map(this::createGroup)
170 .collect(Collectors.toMap(GroupDefinition::getName, g -> g));
171 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
172 if (capabilitiesSubstitutionMappingsExist(substitutionMappings)) {
173 groups.entrySet().forEach(entry -> updateCapabilitiesNames(entry.getValue(), getNamesToUpdate(entry.getKey(),
174 (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName()))));
178 return new HashMap<>();
181 private Map<String, Object> logGroupsNotFound(String fileName) {
182 log.debug("#logGroupsNotFound - Groups were not found in the yaml template {}.", fileName);
183 return new HashMap<>();
186 private void updateCapabilitiesNames(GroupDefinition group, Map<String, String> capabilityNames) {
187 if (MapUtils.isNotEmpty(group.getCapabilities())) {
188 group.getCapabilities().values()
190 .flatMap(Collection::stream)
191 .filter(cap -> capabilityNames.containsKey(cap.getName()))
192 .forEach(cap -> cap.setName(capabilityNames.get(cap.getName())));
196 private Map<String, String> getNamesToUpdate(String name, Map<String, List<String>> pair) {
197 return pair.entrySet().stream()
198 .filter(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX).equalsIgnoreCase(name))
199 .collect(Collectors.toMap(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_NAME_IDX), Map.Entry::getKey, (n1 ,n2) -> n1));
202 private boolean capabilitiesSubstitutionMappingsExist(Map<String, Object> substitutionMappings) {
203 return substitutionMappings != null && substitutionMappings.containsKey(CAPABILITIES.getElementName());
206 private GroupDefinition createGroup(Map.Entry<String, Object> groupNameValue) {
207 GroupDefinition group = new GroupDefinition();
208 group.setName(groupNameValue.getKey());
210 if (groupNameValue.getValue() != null && groupNameValue.getValue() instanceof Map) {
211 Map<String, Object> groupTemplateJsonMap = (Map<String, Object>) groupNameValue.getValue();
212 validateAndFillGroup(group, groupTemplateJsonMap);
213 validateUpdateGroupProperties(group, groupTemplateJsonMap);
214 validateUpdateGroupCapabilities(group, groupTemplateJsonMap);
216 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
218 } catch (ClassCastException e) {
219 log.debug("#createGroup - Failed to create the group {}. The exception occure", groupNameValue.getKey(), e);
220 rollbackWithException(ActionStatus.INVALID_YAML);
225 private Map<String, CapabilityDefinition> addCapabilities(Map<String, CapabilityDefinition> cap, Map<String, CapabilityDefinition> otherCap) {
226 cap.putAll(otherCap);
230 private Map<String, CapabilityDefinition> addCapability(CapabilityDefinition cap) {
231 Map<String, CapabilityDefinition> map = Maps.newHashMap();
232 map.put(cap.getName(), cap);
236 private void setMembers(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
237 if (groupTemplateJsonMap.containsKey(MEMBERS.getElementName())) {
238 Object members = groupTemplateJsonMap.get(MEMBERS.getElementName());
239 if (members != null) {
240 if (members instanceof List) {
241 setMembersFromList(groupInfo, (List<?>) members);
243 log.debug("The 'members' member is not of type list under group {}", groupInfo.getName());
244 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
250 private void setMembersFromList(GroupDefinition groupInfo, List<?> membersAsList) {
251 groupInfo.setMembers(membersAsList
253 .collect(Collectors.toMap(Object::toString, member -> "")));
256 @SuppressWarnings("unchecked")
257 private void validateUpdateGroupProperties(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
258 if (groupTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
259 Object propertiesElement = groupTemplateJsonMap.get(PROPERTIES.getElementName());
260 if (propertiesElement instanceof Map){
261 mergeGroupProperties(groupInfo, (Map<String, Object>) propertiesElement);
266 private void mergeGroupProperties(GroupDefinition groupInfo, Map<String, Object> parsedProperties) {
267 if(CollectionUtils.isNotEmpty(groupInfo.getProperties())){
268 validateGroupProperties(parsedProperties, groupInfo);
269 groupInfo.getProperties().forEach(p -> mergeGroupProperty(p, parsedProperties));
273 private void mergeGroupProperty(PropertyDataDefinition property, Map<String, Object> parsedProperties) {
274 if(parsedProperties.containsKey(property.getName())){
275 Object propValue = parsedProperties.get(property.getName());
276 if (valueNotContainsPattern(propertyValuePattern, propValue)) {
277 setPropertyValueAndGetInputsValues(property, propValue);
282 private void setPropertyValueAndGetInputsValues(PropertyDataDefinition property, Object propValue) {
283 if(propValue != null){
284 UploadPropInfo uploadPropInfo = buildProperty(property.getName(), propValue);
285 property.setValue(convertPropertyValue(ToscaPropertyType.isValidType(property.getType()), uploadPropInfo.getValue()));
286 property.setGetInputValues(uploadPropInfo.getGet_input());
290 private String convertPropertyValue(ToscaPropertyType type, Object value) {
291 String convertedValue = null;
293 if (type == null || value instanceof Map || value instanceof List) {
294 convertedValue = gson.toJson(value);
296 convertedValue = value.toString();
299 return convertedValue;
302 private void setDescription(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
303 if (groupTemplateJsonMap.containsKey(DESCRIPTION.getElementName())) {
304 groupInfo.setDescription(
305 (String) groupTemplateJsonMap.get(DESCRIPTION.getElementName()));
309 private void validateAndFillGroup(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
310 String type = (String) groupTemplateJsonMap.get(TYPE.getElementName());
311 if(StringUtils.isEmpty(type)){
312 log.debug("#validateAndFillGroup - The 'type' member is not found under group {}", groupInfo.getName());
313 rollbackWithException(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupInfo.getName());
315 groupInfo.setType(type);
316 GroupTypeDefinition groupType = groupTypeBusinessLogic.getLatestGroupTypeByType(type);
317 if (groupType == null) {
318 log.debug("#validateAndFillGroup - The group type {} not found", groupInfo.getName());
319 rollbackWithException(ActionStatus.GROUP_TYPE_IS_INVALID, type);
321 groupInfo.convertFromGroupProperties(groupType.getProperties());
322 groupInfo.convertCapabilityDefinitions(groupType.getCapabilities());
323 setDescription(groupInfo, groupTemplateJsonMap);
324 setMembers(groupInfo, groupTemplateJsonMap);
327 @SuppressWarnings("unchecked")
328 private void validateUpdateGroupCapabilities(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
330 if (groupTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
331 Object capabilities = groupTemplateJsonMap.get(CAPABILITIES.getElementName());
332 if (capabilities instanceof List) {
333 validateUpdateCapabilities(groupInfo, ((List<Object>) capabilities).stream()
334 .map(o -> buildGroupCapability(groupInfo, o))
335 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
336 } else if (capabilities instanceof Map) {
337 validateUpdateCapabilities(groupInfo, ((Map<String, Object>) capabilities).entrySet()
339 .map(e -> buildGroupCapability(groupInfo, e))
340 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
342 log.debug("#setCapabilities - Failed to import the capabilities of the group {}. ", groupInfo.getName());
343 rollbackWithException(ActionStatus.INVALID_YAML);
348 private void validateUpdateCapabilities(GroupDefinition groupInfo, Map<String, Map<String, CapabilityDefinition>> capabilityInfo) {
349 validateGroupCapabilities(groupInfo, capabilityInfo);
350 groupInfo.updateCapabilitiesProperties(capabilityInfo);
353 private void validateGroupCapabilities(GroupDefinition group, Map<String, Map<String, CapabilityDefinition>> parsedCapabilities) {
354 if (MapUtils.isNotEmpty(parsedCapabilities)) {
355 if (MapUtils.isEmpty(group.getCapabilities())) {
356 failOnMissingCapabilityTypes(group, Lists.newArrayList(parsedCapabilities.keySet()));
358 List<String> missingCapTypes = parsedCapabilities.keySet().stream().filter(ct -> !group.getCapabilities().containsKey(ct)).collect(toList());
359 if (CollectionUtils.isNotEmpty(missingCapTypes)) {
360 failOnMissingCapabilityTypes(group, missingCapTypes);
362 group.getCapabilities().entrySet().forEach(e -> validateCapabilities(group, e.getValue(), parsedCapabilities.get(e.getKey())));
366 private void validateCapabilities(GroupDefinition group, List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
367 List<String> allowedCapNames = capabilities.stream().map(CapabilityDefinition::getName).distinct().collect(toList());
368 List<String> missingCapNames = parsedCapabilities.keySet().stream().filter(c -> !allowedCapNames.contains(c)).collect(toList());
369 if (CollectionUtils.isNotEmpty(missingCapNames)) {
370 failOnMissingCapabilityNames(group, missingCapNames);
372 validateCapabilitiesProperties(capabilities, parsedCapabilities);
375 private void validateCapabilitiesProperties(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
376 capabilities.forEach(c -> validateCapabilityProperties(c, parsedCapabilities.get(c.getName())));
379 private void validateCapabilityProperties(CapabilityDefinition capability, CapabilityDefinition parsedCapability) {
380 if(parsedCapability != null && parsedCapability.getProperties() != null){
381 List<String> parsedPropertiesNames = parsedCapability.getProperties()
383 .map(ComponentInstanceProperty::getName).collect(toList());
384 validateProperties(capability.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames, ActionStatus.PROPERTY_NOT_FOUND, capability.getName(), capability.getType());
388 private void validateGroupProperties(Map<String, Object> parsedProperties, GroupDefinition groupInfo) {
389 List<String> parsedPropertiesNames = parsedProperties.entrySet()
391 .map(Map.Entry::getKey).collect(toList());
392 validateProperties(groupInfo.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames, ActionStatus.GROUP_PROPERTY_NOT_FOUND, groupInfo.getName(), groupInfo.getType());
395 private void validateProperties(List<String> validProperties, List<String> parsedProperties, ActionStatus actionStatus, String name, String type) {
396 if (CollectionUtils.isNotEmpty(parsedProperties)) {
397 verifyMissingProperties(actionStatus, name, type, parsedProperties
399 .filter(n -> !validProperties.contains(n))
404 private void verifyMissingProperties(ActionStatus actionStatus, String name, String type, List<String> missingProperties) {
405 if (CollectionUtils.isNotEmpty(missingProperties)) {
406 log.debug("#validateProperties - Failed to validate properties. The properties {} are missing on {} of the type {}. ", missingProperties.toString(), name, type);
407 rollbackWithException(actionStatus, missingProperties.toString(), missingProperties.toString(), name, type);
411 @SuppressWarnings("unchecked")
412 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Object capObject) {
413 if (!(capObject instanceof Map)) {
414 log.debug("#convertToGroupCapability - Failed to import the capability {}. ", capObject);
415 rollbackWithException(ActionStatus.INVALID_YAML);
417 return buildGroupCapability(groupInfo, ((Map<String, Object>) capObject).entrySet().iterator().next());
420 @SuppressWarnings("unchecked")
421 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Map.Entry<String, Object> capEntry) {
422 CapabilityDefinition capability = new CapabilityDefinition();
423 capability.setOwnerType(CapabilityDataDefinition.OwnerType.GROUP);
424 capability.setName(capEntry.getKey());
425 capability.setParentName(capEntry.getKey());
426 capability.setOwnerId(groupInfo.getName());
427 if (!(capEntry.getValue() instanceof Map)) {
428 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
429 rollbackWithException(ActionStatus.INVALID_YAML);
431 Map<String, Object> capabilityValue = (Map<String, Object>) capEntry.getValue();
432 String type = (String) capabilityValue.get(TYPE.getElementName());
433 if (StringUtils.isEmpty(type)) {
434 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. Missing capability type. ", capEntry.getKey());
435 rollbackWithException(ActionStatus.INVALID_YAML);
437 capability.setType(type);
438 if (!(capabilityValue.get(PROPERTIES.getElementName()) instanceof Map)) {
439 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
440 rollbackWithException(ActionStatus.INVALID_YAML);
442 Map<String, Object> properties = (Map<String, Object>) capabilityValue.get(PROPERTIES.getElementName());
443 capability.setProperties(properties.entrySet().stream().map(this::convertToProperty).collect(toList()));
447 private ComponentInstanceProperty convertToProperty(Map.Entry<String, Object> e) {
448 ComponentInstanceProperty property = new ComponentInstanceProperty();
449 property.setName(e.getKey());
450 property.setValue((String) e.getValue());
454 @SuppressWarnings("unchecked")
455 private UploadComponentInstanceInfo buildModuleComponentInstanceInfo(
456 Map.Entry<String, Object> nodeTemplateJsonEntry, Map<String, Object> substitutionMappings,
457 Map<String, String> createdNodesToscaResourceNames) {
459 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
460 nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey());
462 if (nodeTemplateJsonEntry.getValue() instanceof String) {
463 String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue();
464 nodeTemplateInfo.setType(nodeTemplateJsonString);
465 } else if (nodeTemplateJsonEntry.getValue() instanceof Map) {
466 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) nodeTemplateJsonEntry.getValue();
467 setToscaResourceType(createdNodesToscaResourceNames, nodeTemplateInfo, nodeTemplateJsonMap);
468 setRequirements(nodeTemplateInfo, nodeTemplateJsonMap);
469 setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
470 setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
471 updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
472 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
473 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
474 setSubstitutions(substitutionMappings, nodeTemplateInfo);
476 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
478 } catch (ClassCastException e) {
479 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
480 log.debug("error when creating capability, message:{}", e.getMessage(), e);
481 rollbackWithException(ActionStatus.INVALID_YAML);
483 return nodeTemplateInfo;
486 @SuppressWarnings("unchecked")
487 private void setSubstitutions(Map<String, Object> substitutionMappings, UploadComponentInstanceInfo nodeTemplateInfo) {
488 if (substitutionMappings != null) {
489 if (substitutionMappings.containsKey(CAPABILITIES.getElementName())) {
490 nodeTemplateInfo.setCapabilitiesNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(), (Map<String, List<String>>) substitutionMappings
491 .get(CAPABILITIES.getElementName())));
493 if (substitutionMappings.containsKey(REQUIREMENTS.getElementName())) {
494 nodeTemplateInfo.setRequirementsNamesToUpdate(getNamesToUpdate(
495 nodeTemplateInfo.getName(), (Map<String, List<String>>) substitutionMappings
496 .get(REQUIREMENTS.getElementName())));
501 private void updateProperties(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
502 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
503 Map<String, List<UploadPropInfo>> properties = buildPropModuleFromYaml(nodeTemplateJsonMap);
504 if (!properties.isEmpty()) {
505 nodeTemplateInfo.setProperties(properties);
510 private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
511 if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
512 Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
513 if (!eitherCapRes.isEmpty()) {
514 nodeTemplateInfo.setCapabilities(eitherCapRes);
519 private void setArtifacts(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
520 if (nodeTemplateJsonMap.containsKey(ARTIFACTS.getElementName())) {
521 Map<String, Map<String, UploadArtifactInfo>> eitherArtifactsRes = createArtifactsModuleFromYaml(nodeTemplateJsonMap);
522 if (!eitherArtifactsRes.isEmpty()) {
523 nodeTemplateInfo.setArtifacts(eitherArtifactsRes);
528 private void setRequirements(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
529 if (nodeTemplateJsonMap.containsKey(REQUIREMENTS.getElementName())) {
530 Map<String, List<UploadReqInfo>> regResponse = createReqModuleFromYaml(nodeTemplateJsonMap);
531 if (!regResponse.isEmpty()) {
532 nodeTemplateInfo.setRequirements(regResponse);
537 private void setToscaResourceType(Map<String, String> createdNodesToscaResourceNames,
538 UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
539 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
540 String toscaResourceType = (String) nodeTemplateJsonMap.get(TYPE.getElementName());
541 if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) {
542 toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType);
544 nodeTemplateInfo.setType(toscaResourceType);
548 private void setDirectives(UploadComponentInstanceInfo nodeTemplateInfo,
549 Map<String, Object> nodeTemplateJsonMap) {
550 List<String> directives =
551 (List<String>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.DIRECTIVES.getElementName());
552 nodeTemplateInfo.setDirectives(directives);
555 private void setNodeFilter(UploadComponentInstanceInfo nodeTemplateInfo,
556 Map<String, Object> nodeTemplateJsonMap) {
557 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())) {
558 nodeTemplateInfo.setUploadNodeFilterInfo(
559 new NodeFilterUploadCreator().createNodeFilterData(nodeTemplateJsonMap.get(
560 TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())));
564 @SuppressWarnings("unchecked")
565 private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
566 Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
567 Either<List<Object>, ResultStatusEnum> requirementsListRes =
568 findFirstToscaListElement(nodeTemplateJsonMap, REQUIREMENTS);
570 if (requirementsListRes.isLeft()) {
571 for (Object jsonReqObj : requirementsListRes.left().value()) {
572 String reqName = ((Map<String, Object>) jsonReqObj).keySet().iterator().next();
573 Object reqJson = ((Map<String, Object>) jsonReqObj).get(reqName);
574 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName);
577 Either<Map<String, Object>, ResultStatusEnum> requirementsMapRes =
578 findFirstToscaMapElement(nodeTemplateJsonMap, REQUIREMENTS);
579 if (requirementsMapRes.isLeft()) {
580 for (Map.Entry<String, Object> entry : requirementsMapRes.left().value().entrySet()) {
581 String reqName = entry.getKey();
582 Object reqJson = entry.getValue();
583 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName);
587 return moduleRequirements;
590 private void addModuleNodeTemplateReq(Map<String, List<UploadReqInfo>> moduleRequirements, Object requirementJson, String requirementName) {
592 UploadReqInfo requirement = buildModuleNodeTemplateReg(requirementJson);
593 requirement.setName(requirementName);
594 if (moduleRequirements.containsKey(requirementName)) {
595 moduleRequirements.get(requirementName).add(requirement);
597 List<UploadReqInfo> list = new ArrayList<>();
598 list.add(requirement);
599 moduleRequirements.put(requirementName, list);
603 @SuppressWarnings("unchecked")
604 private Map<String, Map<String, UploadArtifactInfo>> createArtifactsModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
605 Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts = new HashMap<>();
606 Either<List<Object>, ResultStatusEnum> ArtifactsListRes =
607 findFirstToscaListElement(nodeTemplateJsonMap, ARTIFACTS);
608 if (ArtifactsListRes.isLeft()) {
609 for (Object jsonArtifactObj : ArtifactsListRes.left().value()) {
610 String key = ((Map<String, Object>) jsonArtifactObj).keySet().iterator().next();
611 Object artifactJson = ((Map<String, Object>) jsonArtifactObj).get(key);
612 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, key);
615 Either<Map<String, Map<String, Object>>, ResultStatusEnum> ArtifactsMapRes =
616 findFirstToscaMapElement(nodeTemplateJsonMap, ARTIFACTS);
617 if (ArtifactsMapRes.isLeft()) {
618 for (Map.Entry<String, Map<String, Object>> entry : ArtifactsMapRes.left().value().entrySet()) {
619 String artifactName = entry.getKey();
620 Object artifactJson = entry.getValue();
621 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, artifactName);
625 return moduleArtifacts;
628 private void addModuleNodeTemplateArtifacts(Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts, Object artifactJson, String artifactName) {
630 UploadArtifactInfo artifact = buildModuleNodeTemplateArtifact(artifactJson);
631 artifact.setName(artifactName);
632 if (moduleArtifacts.containsKey(ARTIFACTS.getElementName())) {
633 moduleArtifacts.get(ARTIFACTS.getElementName()).put(artifactName, artifact);
635 Map<String, UploadArtifactInfo> map = new HashMap<>();
636 map.put(artifactName, artifact);
637 moduleArtifacts.put(ARTIFACTS.getElementName(), map);
641 @SuppressWarnings("unchecked")
642 private UploadArtifactInfo buildModuleNodeTemplateArtifact(Object artifactObject) {
643 UploadArtifactInfo artifactTemplateInfo = new UploadArtifactInfo();
644 if (artifactObject instanceof Map) {
645 fillArtifact(artifactTemplateInfo, (Map<String, Object>) artifactObject);
647 return artifactTemplateInfo;
650 private void fillArtifact(UploadArtifactInfo artifactTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
651 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
652 artifactTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
654 if (nodeTemplateJsonMap.containsKey(FILE.getElementName())) {
655 artifactTemplateInfo.setFile((String) nodeTemplateJsonMap.get(FILE.getElementName()));
660 @SuppressWarnings("unchecked")
661 private Map<String, List<UploadCapInfo>> createCapModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
662 Map<String, List<UploadCapInfo>> moduleCap = new HashMap<>();
663 Either<List<Object>, ResultStatusEnum> capabilitiesListRes =
664 findFirstToscaListElement(nodeTemplateJsonMap, CAPABILITIES);
665 if (capabilitiesListRes.isLeft()) {
666 for (Object jsonCapObj : capabilitiesListRes.left().value()) {
667 String key = ((Map<String, Object>) jsonCapObj).keySet().iterator().next();
668 Object capJson = ((Map<String, Object>) jsonCapObj).get(key);
669 addModuleNodeTemplateCap(moduleCap, capJson, key);
672 Either<Map<String, Object>, ResultStatusEnum> capabilitiesMapRes =
673 findFirstToscaMapElement(nodeTemplateJsonMap, CAPABILITIES);
674 if (capabilitiesMapRes.isLeft()) {
675 for (Map.Entry<String, Object> entry : capabilitiesMapRes.left().value().entrySet()) {
676 String capName = entry.getKey();
677 Object capJson = entry.getValue();
678 addModuleNodeTemplateCap(moduleCap, capJson, capName);
685 private void addModuleNodeTemplateCap(Map<String, List<UploadCapInfo>> moduleCap, Object capJson, String key) {
687 UploadCapInfo capabilityDef = buildModuleNodeTemplateCap(capJson);
688 capabilityDef.setKey(key);
689 if (moduleCap.containsKey(key)) {
690 moduleCap.get(key).add(capabilityDef);
692 List<UploadCapInfo> list = new ArrayList<>();
693 list.add(capabilityDef);
694 moduleCap.put(key, list);
698 @SuppressWarnings("unchecked")
699 private UploadCapInfo buildModuleNodeTemplateCap(Object capObject) {
700 UploadCapInfo capTemplateInfo = new UploadCapInfo();
702 if (capObject instanceof String) {
703 String nodeTemplateJsonString = (String) capObject;
704 capTemplateInfo.setNode(nodeTemplateJsonString);
705 } else if (capObject instanceof Map) {
706 fillCapability(capTemplateInfo, (Map<String, Object>) capObject);
708 return capTemplateInfo;
711 private void fillCapability(UploadCapInfo capTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
712 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
713 capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
715 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
716 capTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
718 if (nodeTemplateJsonMap.containsKey(VALID_SOURCE_TYPES.getElementName())) {
719 Either<List<Object>, ResultStatusEnum> validSourceTypesRes =
720 findFirstToscaListElement(nodeTemplateJsonMap, VALID_SOURCE_TYPES);
721 if (validSourceTypesRes.isLeft()) {
722 capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream()
723 .map(Object::toString).collect(toList()));
726 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
727 Map<String, List<UploadPropInfo>> props = buildPropModuleFromYaml(nodeTemplateJsonMap);
728 if (!props.isEmpty()) {
729 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
730 capTemplateInfo.setProperties(properties);
735 @SuppressWarnings("unchecked")
736 private UploadReqInfo buildModuleNodeTemplateReg(Object regObject) {
738 UploadReqInfo regTemplateInfo = new UploadReqInfo();
739 if (regObject instanceof String) {
740 String nodeTemplateJsonString = (String) regObject;
741 regTemplateInfo.setNode(nodeTemplateJsonString);
742 } else if (regObject instanceof Map) {
743 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) regObject;
744 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
745 regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
747 if (nodeTemplateJsonMap.containsKey(CAPABILITY.getElementName())) {
748 regTemplateInfo.setCapabilityName(
749 (String) nodeTemplateJsonMap.get(CAPABILITY.getElementName()));
752 return regTemplateInfo;
755 private Map<String, List<UploadPropInfo>> buildPropModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
757 Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
758 Either<Map<String, Object>, ResultStatusEnum> toscaProperties =
759 findFirstToscaMapElement(nodeTemplateJsonMap, PROPERTIES);
760 if (toscaProperties.isLeft()) {
761 Map<String, Object> jsonProperties = toscaProperties.left().value();
762 for (Map.Entry<String, Object> jsonPropObj : jsonProperties.entrySet()) {
763 if (valueNotContainsPattern(propertyValuePattern, jsonPropObj.getValue())) {
764 addProperty(moduleProp, jsonPropObj);
771 private void addProperty(Map<String, List<UploadPropInfo>> moduleProp, Map.Entry<String, Object> jsonPropObj) {
772 UploadPropInfo propertyDef = buildProperty(jsonPropObj.getKey(), jsonPropObj.getValue());
773 if (moduleProp.containsKey(propertyDef.getName())) {
774 moduleProp.get(propertyDef.getName()).add(propertyDef);
776 List<UploadPropInfo> list = new ArrayList<>();
777 list.add(propertyDef);
778 moduleProp.put(propertyDef.getName(), list);
782 @SuppressWarnings("unchecked")
783 private UploadPropInfo buildProperty(String propName, Object propValue) {
785 UploadPropInfo propertyDef = new UploadPropInfo();
786 propertyDef.setValue(propValue);
787 propertyDef.setName(propName);
788 if (propValue instanceof Map) {
789 if (((Map<String, Object>) propValue).containsKey(TYPE.getElementName())) {
790 propertyDef.setType(((Map<String, Object>) propValue)
791 .get(TYPE.getElementName()).toString());
793 if (containsGetInput(propValue)) {
794 fillInputRecursively(propName, (Map<String, Object>) propValue, propertyDef);
797 if (((Map<String, Object>) propValue).containsKey(DESCRIPTION.getElementName())) {
798 propertyDef.setDescription(((Map<String, Object>) propValue)
799 .get(DESCRIPTION.getElementName()).toString());
801 if (((Map<String, Object>) propValue)
802 .containsKey(DEFAULT_VALUE.getElementName())) {
803 propertyDef.setValue(((Map<String, Object>) propValue)
804 .get(DEFAULT_VALUE.getElementName()));
806 if (((Map<String, Object>) propValue).containsKey(IS_PASSWORD.getElementName())) {
807 propertyDef.setPassword(Boolean.getBoolean(((Map<String, Object>) propValue)
808 .get(IS_PASSWORD.getElementName()).toString()));
810 propertyDef.setValue(propValue);
812 } else if (propValue instanceof List) {
813 List<Object> propValueList = (List<Object>) propValue;
815 fillInputsListRecursively(propertyDef, propValueList);
816 propertyDef.setValue(propValue);
822 @SuppressWarnings("unchecked")
823 private boolean containsGetInput(Object propValue) {
824 return ((Map<String, Object>) propValue).containsKey(GET_INPUT.getElementName())
825 || ImportUtils.containsGetInput(propValue);
828 @SuppressWarnings("unchecked")
829 private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
830 for (Object objValue : propValueList) {
832 if (objValue instanceof Map) {
833 Map<String, Object> objMap = (Map<String, Object>) objValue;
834 if (objMap.containsKey(GET_INPUT.getElementName())) {
835 fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
837 Set<String> keys = objMap.keySet();
838 findAndFillInputsListRecursively(propertyDef, objMap, keys);
840 } else if (objValue instanceof List) {
841 List<Object> propSubValueList = (List<Object>) objValue;
842 fillInputsListRecursively(propertyDef, propSubValueList);
847 @SuppressWarnings("unchecked")
848 private void findAndFillInputsListRecursively(UploadPropInfo propertyDef, Map<String, Object> objMap,
850 for (String key : keys) {
851 Object value = objMap.get(key);
852 if (value instanceof Map) {
853 fillInputRecursively(key, (Map<String, Object>) value, propertyDef);
854 } else if (value instanceof List) {
855 List<Object> propSubValueList = (List<Object>) value;
856 fillInputsListRecursively(propertyDef, propSubValueList);
861 private void fillInputRecursively(String propName, Map<String, Object> propValue, UploadPropInfo propertyDef) {
863 if (propValue.containsKey(GET_INPUT.getElementName())) {
864 Object getInput = propValue.get(GET_INPUT.getElementName());
865 GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition();
866 List<GetInputValueDataDefinition> getInputs = propertyDef.getGet_input();
867 if (getInputs == null) {
868 getInputs = new ArrayList<>();
870 if (getInput instanceof String) {
872 getInputInfo.setInputName((String) getInput);
873 getInputInfo.setPropName(propName);
875 } else if (getInput instanceof List) {
876 fillInput(propName, getInput, getInputInfo);
878 getInputs.add(getInputInfo);
879 propertyDef.setGet_input(getInputs);
880 propertyDef.setValue(propValue);
882 findAndFillInputRecursively(propValue, propertyDef);
886 @SuppressWarnings("unchecked")
887 private void findAndFillInputRecursively(Map<String, Object> propValue, UploadPropInfo propertyDef) {
888 for (String propName : propValue.keySet()) {
889 Object value = propValue.get(propName);
890 if (value instanceof Map) {
891 fillInputRecursively(propName, (Map<String, Object>) value, propertyDef);
893 } else if (value instanceof List) {
894 fillInputsRecursively(propertyDef, propName, (List<Object>) value);
899 private void fillInputsRecursively(UploadPropInfo propertyDef, String propName, List<Object> inputs) {
901 .filter(o -> o instanceof Map)
902 .forEach(o -> fillInputRecursively(propName, (Map<String, Object>)o, propertyDef));
905 @SuppressWarnings("unchecked")
906 private void fillInput(String propName, Object getInput, GetInputValueDataDefinition getInputInfo) {
907 List<Object> getInputList = (List<Object>) getInput;
908 getInputInfo.setPropName(propName);
909 getInputInfo.setInputName((String) getInputList.get(0));
910 if (getInputList.size() > 1) {
911 Object indexObj = getInputList.get(1);
912 if (indexObj instanceof Integer) {
913 getInputInfo.setIndexValue((Integer) indexObj);
914 } else if (indexObj instanceof Float) {
915 int index = ((Float) indexObj).intValue();
916 getInputInfo.setIndexValue(index);
917 } else if (indexObj instanceof Map && ((Map<String, Object>) indexObj)
918 .containsKey(GET_INPUT.getElementName())) {
919 Object index = ((Map<String, Object>) indexObj)
920 .get(GET_INPUT.getElementName());
921 GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition();
922 getInputInfoIndex.setInputName((String) index);
923 getInputInfoIndex.setPropName(propName);
924 getInputInfo.setGetInputIndex(getInputInfoIndex);
926 getInputInfo.setList(true);
930 private boolean valueNotContainsPattern(Pattern pattern, Object propValue) {
931 return propValue == null || !pattern.matcher(propValue.toString()).find();
934 private Map<String, Object> failIfNoNodeTemplates(String fileName) {
935 janusGraphDao.rollback();
936 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
939 private Object failIfNotTopologyTemplate(String fileName) {
940 janusGraphDao.rollback();
941 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
944 private void rollbackWithException(ActionStatus actionStatus, String... params) {
945 janusGraphDao.rollback();
946 throw new ByActionStatusComponentException(actionStatus, params);
949 private void failOnMissingCapabilityTypes(GroupDefinition groupDefinition, List<String> missingCapTypes) {
950 log.debug("#failOnMissingCapabilityTypes - Failed to validate the capabilities of the group {}. The capability types {} are missing on the group type {}. ", groupDefinition.getName(), missingCapTypes.toString(), groupDefinition.getType());
951 if(CollectionUtils.isNotEmpty(missingCapTypes)) {
952 rollbackWithException(ActionStatus.MISSING_CAPABILITY_TYPE, missingCapTypes.toString());
956 private void failOnMissingCapabilityNames(GroupDefinition groupDefinition, List<String> missingCapNames) {
957 log.debug("#failOnMissingCapabilityNames - Failed to validate the capabilities of the group {}. The capabilities with the names {} are missing on the group type {}. ", groupDefinition.getName(), missingCapNames.toString(), groupDefinition.getType());
958 rollbackWithException(ActionStatus.MISSING_CAPABILITIES, missingCapNames.toString(), CapabilityDataDefinition.OwnerType.GROUP.getValue(), groupDefinition.getName());