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 * ================================================================================
23 package org.openecomp.sdc.be.components.csar;
25 import static java.util.stream.Collectors.toList;
26 import static org.openecomp.sdc.be.components.impl.ImportUtils.Constants.QUOTE;
27 import static org.openecomp.sdc.be.components.impl.ImportUtils.ResultStatusEnum;
28 import static org.openecomp.sdc.be.components.impl.ImportUtils.ToscaElementTypeEnum;
29 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaListElement;
30 import static org.openecomp.sdc.be.components.impl.ImportUtils.findFirstToscaMapElement;
31 import static org.openecomp.sdc.be.components.impl.ImportUtils.findToscaElement;
32 import static org.openecomp.sdc.be.components.impl.ImportUtils.loadYamlAsStrictMap;
33 import static org.openecomp.sdc.be.model.tosca.ToscaType.STRING;
34 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ARTIFACTS;
35 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.ATTRIBUTES;
36 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITIES;
37 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.CAPABILITY;
38 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DEFAULT_VALUE;
39 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.DESCRIPTION;
40 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.FILE;
41 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GET_INPUT;
42 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.GROUPS;
43 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IMPLEMENTATION;
44 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INPUTS;
45 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.INTERFACES;
46 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.IS_PASSWORD;
47 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.MEMBERS;
48 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE;
49 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TEMPLATES;
50 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.NODE_TYPE;
51 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OPERATIONS;
52 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.OUTPUTS;
53 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.POLICIES;
54 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.PROPERTIES;
55 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.RELATIONSHIP;
56 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.RELATIONSHIP_TEMPLATES;
57 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.REQUIREMENTS;
58 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_FILTERS;
59 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.SUBSTITUTION_MAPPINGS;
60 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TARGETS;
61 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TOPOLOGY_TEMPLATE;
62 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.TYPE;
63 import static org.openecomp.sdc.be.utils.TypeUtils.ToscaTagNamesEnum.VALID_SOURCE_TYPES;
65 import com.att.aft.dme2.internal.gson.reflect.TypeToken;
66 import com.google.common.collect.Lists;
67 import com.google.common.collect.Maps;
68 import com.google.gson.Gson;
69 import fj.data.Either;
70 import java.lang.reflect.Type;
71 import java.util.ArrayList;
72 import java.util.Collection;
73 import java.util.Collections;
74 import java.util.EnumMap;
75 import java.util.HashMap;
76 import java.util.LinkedHashMap;
77 import java.util.List;
79 import java.util.Map.Entry;
80 import java.util.Objects;
81 import java.util.Optional;
83 import java.util.UUID;
84 import java.util.stream.Collectors;
85 import org.apache.commons.collections.CollectionUtils;
86 import org.apache.commons.collections.MapUtils;
87 import org.apache.commons.lang3.StringUtils;
88 import org.openecomp.sdc.be.components.impl.AnnotationBusinessLogic;
89 import org.openecomp.sdc.be.components.impl.GroupTypeBusinessLogic;
90 import org.openecomp.sdc.be.components.impl.ImportUtils;
91 import org.openecomp.sdc.be.components.impl.NodeFilterUploadCreator;
92 import org.openecomp.sdc.be.components.impl.PolicyTypeBusinessLogic;
93 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
94 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
95 import org.openecomp.sdc.be.components.utils.PropertiesUtils;
96 import org.openecomp.sdc.be.config.BeEcompErrorManager;
97 import org.openecomp.sdc.be.dao.api.ActionStatus;
98 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
99 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
100 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
101 import org.openecomp.sdc.be.datatypes.elements.GetInputValueDataDefinition;
102 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
103 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
104 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
105 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
106 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
107 import org.openecomp.sdc.be.datatypes.elements.PropertyFilterConstraintDataDefinition;
108 import org.openecomp.sdc.be.datatypes.elements.SubPropertyToscaFunction;
109 import org.openecomp.sdc.be.datatypes.elements.SubstitutionFilterPropertyDataDefinition;
110 import org.openecomp.sdc.be.datatypes.elements.ToscaFunction;
111 import org.openecomp.sdc.be.datatypes.elements.ToscaGetFunctionDataDefinition;
112 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
113 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
114 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
115 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
116 import org.openecomp.sdc.be.model.CapabilityDefinition;
117 import org.openecomp.sdc.be.model.Component;
118 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
119 import org.openecomp.sdc.be.model.GroupDefinition;
120 import org.openecomp.sdc.be.model.GroupTypeDefinition;
121 import org.openecomp.sdc.be.model.InputDefinition;
122 import org.openecomp.sdc.be.model.NodeTypeInfo;
123 import org.openecomp.sdc.be.model.OutputDefinition;
124 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
125 import org.openecomp.sdc.be.model.PolicyDefinition;
126 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
127 import org.openecomp.sdc.be.model.PropertyDefinition;
128 import org.openecomp.sdc.be.model.Resource;
129 import org.openecomp.sdc.be.model.UploadArtifactInfo;
130 import org.openecomp.sdc.be.model.UploadAttributeInfo;
131 import org.openecomp.sdc.be.model.UploadCapInfo;
132 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
133 import org.openecomp.sdc.be.model.UploadInterfaceInfo;
134 import org.openecomp.sdc.be.model.UploadPropInfo;
135 import org.openecomp.sdc.be.model.UploadReqInfo;
136 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
137 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
138 import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil;
139 import org.openecomp.sdc.be.ui.model.OperationUi;
140 import org.openecomp.sdc.be.ui.model.PropertyAssignmentUi;
141 import org.openecomp.sdc.be.utils.PropertyFilterConstraintDataDefinitionHelper;
142 import org.openecomp.sdc.be.utils.TypeUtils;
143 import org.openecomp.sdc.common.log.wrappers.Logger;
144 import org.yaml.snakeyaml.parser.ParserException;
147 * A handler class designed to parse the YAML file of the service template for a JAVA object
149 @org.springframework.stereotype.Component
150 public class YamlTemplateParsingHandler {
152 private static final int SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX = 0;
153 private static final int SUB_MAPPING_CAPABILITY_NAME_IDX = 1;
154 private static final Logger log = Logger.getLogger(YamlTemplateParsingHandler.class);
155 private final Gson gson = new Gson();
156 private final JanusGraphDao janusGraphDao;
157 private final GroupTypeBusinessLogic groupTypeBusinessLogic;
158 private final AnnotationBusinessLogic annotationBusinessLogic;
159 private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
160 private final ServiceBusinessLogic serviceBusinessLogic;
161 private final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
163 public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao,
164 GroupTypeBusinessLogic groupTypeBusinessLogic,
165 AnnotationBusinessLogic annotationBusinessLogic,
166 PolicyTypeBusinessLogic policyTypeBusinessLogic,
167 ServiceBusinessLogic serviceBusinessLogic,
168 final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler
170 this.janusGraphDao = janusGraphDao;
171 this.groupTypeBusinessLogic = groupTypeBusinessLogic;
172 this.annotationBusinessLogic = annotationBusinessLogic;
173 this.policyTypeBusinessLogic = policyTypeBusinessLogic;
174 this.serviceBusinessLogic = serviceBusinessLogic;
175 this.toscaFunctionYamlParsingHandler = toscaFunctionYamlParsingHandler;
178 public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames,
179 Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName,
180 Component component, String interfaceTemplateYaml) {
181 log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName);
182 Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName);
183 ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo();
184 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(mappedToscaTemplate, TOPOLOGY_TEMPLATE,
185 ToscaElementTypeEnum.ALL).left().on(err -> failIfNotTopologyTemplate(fileName));
186 final Map<String, Object> mappedTopologyTemplateInputs = mappedTopologyTemplate.entrySet().stream()
187 .filter(entry -> entry.getKey().equals(INPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
188 final Map<String, Object> mappedTopologyTemplateOutputs = mappedTopologyTemplate.entrySet().stream()
189 .filter(entry -> entry.getKey().equals(OUTPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
190 parsedToscaYamlInfo.setInputs(getInputs(mappedTopologyTemplateInputs));
191 parsedToscaYamlInfo.setOutputs(getOutputs(mappedTopologyTemplateOutputs));
192 parsedToscaYamlInfo.setInstances(getInstances(
194 createdNodesToscaResourceNames
196 associateRelationshipTemplatesToInstances(parsedToscaYamlInfo.getInstances(), mappedTopologyTemplate);
197 parsedToscaYamlInfo.setGroups(getGroups(mappedToscaTemplate, component.getModel()));
198 parsedToscaYamlInfo.setPolicies(getPolicies(mappedToscaTemplate, component.getModel()));
199 Map<String, Object> substitutionMappings = getSubstitutionMappings(mappedToscaTemplate);
200 if (substitutionMappings != null) {
201 if (component.isService()) {
202 if (interfaceTemplateYaml.isEmpty()) {
203 List<PropertyDefinition> properties = serviceBusinessLogic.fetchDerivedFromGenericType(component, null).getProperties();
204 parsedToscaYamlInfo.setProperties(properties.stream().collect(Collectors.toMap(PropertyDefinition::getName, prop -> prop)));
206 parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml)));
208 parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
210 if (substitutionMappings.get("properties") != null) {
211 parsedToscaYamlInfo.setSubstitutionMappingProperties((Map<String, List<String>>) substitutionMappings.get("properties"));
213 parsedToscaYamlInfo.setSubstitutionMappingNodeType((String) substitutionMappings.get(NODE_TYPE.getElementName()));
215 log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName);
216 return parsedToscaYamlInfo;
219 private Map<String, Object> getMappedToscaTemplate(String fileName, String resourceYml, Map<String, NodeTypeInfo> nodeTypesInfo,
221 Map<String, Object> mappedToscaTemplate;
222 if (isNodeExist(nodeTypesInfo, nodeName)) {
223 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
225 mappedToscaTemplate = loadYaml(fileName, resourceYml);
227 return mappedToscaTemplate;
230 private Map<String, Object> loadYaml(String fileName, String resourceYml) {
231 Map<String, Object> mappedToscaTemplate = null;
233 mappedToscaTemplate = loadYamlAsStrictMap(resourceYml);
234 } catch (ParserException e) {
235 log.debug("#getMappedToscaTemplate - Failed to load YAML file {}", fileName, e);
236 rollbackWithException(ActionStatus.TOSCA_PARSE_ERROR, fileName, e.getMessage());
238 return mappedToscaTemplate;
241 private boolean isNodeExist(Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
242 return nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName);
245 private Map<String, InputDefinition> getInputs(Map<String, Object> toscaJson) {
246 Map<String, InputDefinition> inputs = ImportUtils.getInputs(toscaJson, annotationBusinessLogic.getAnnotationTypeOperations()).left()
247 .on(err -> new HashMap<>());
248 annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(inputs);
252 private Map<String, OutputDefinition> getOutputs(Map<String, Object> toscaJson) {
253 return ImportUtils.getOutputs(toscaJson).left().on(err -> new HashMap<>());
256 private Map<String, PropertyDefinition> getProperties(Map<String, Object> toscaJson) {
257 return ImportUtils.getProperties(toscaJson).left().on(err -> new HashMap<>());
260 private ListDataDefinition<SubstitutionFilterPropertyDataDefinition> getSubstitutionFilterProperties(final Map<String, Object> toscaJson) {
261 final ListDataDefinition<SubstitutionFilterPropertyDataDefinition> propertyList = new ListDataDefinition<>();
262 final Map<String, Object> substitutionFilters = findFirstToscaMapElement(toscaJson, SUBSTITUTION_FILTERS).left().on(err -> new HashMap<>());
263 if (MapUtils.isEmpty(substitutionFilters)) {
266 final List<Map<String, Object>> substitutionFilterProperties = (List<Map<String, Object>>) substitutionFilters.get("properties");
267 if (CollectionUtils.isEmpty(substitutionFilterProperties)) {
270 for (final Map<String, Object> filterProps : substitutionFilterProperties) {
271 for (final Map.Entry<String, Object> propertyFilterEntry : filterProps.entrySet()) {
272 final String propertyName = propertyFilterEntry.getKey();
273 final Object value = propertyFilterEntry.getValue();
274 if (value instanceof List) {
275 final List<Map<String, Object>> propertyFilterEntryValue = (List<Map<String, Object>>) value;
276 for (final Map<String, Object> filterValueMap : propertyFilterEntryValue) {
277 final var substitutionFilterPropertyDataDefinition = new SubstitutionFilterPropertyDataDefinition();
278 substitutionFilterPropertyDataDefinition.setName(propertyName);
279 substitutionFilterPropertyDataDefinition.setConstraints(createSubstitutionFilterConstraints(propertyName, filterValueMap));
280 propertyList.add(substitutionFilterPropertyDataDefinition);
282 } else if (value instanceof Map) {
283 final Map<String, Object> filterValueMap = (Map<String, Object>) value;
284 final var substitutionFilterPropertyDataDefinition = new SubstitutionFilterPropertyDataDefinition();
285 substitutionFilterPropertyDataDefinition.setName(propertyName);
286 substitutionFilterPropertyDataDefinition.setConstraints(createSubstitutionFilterConstraints(propertyName, filterValueMap));
287 propertyList.add(substitutionFilterPropertyDataDefinition);
294 private List<PropertyFilterConstraintDataDefinition> createSubstitutionFilterConstraints(final String name, final Map<String, Object> value) {
295 final List<PropertyFilterConstraintDataDefinition> constraints = new ArrayList<>();
296 for (final Map.Entry<String, Object> valueEntry : value.entrySet()) {
297 final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
298 propertyFilterConstraint.setPropertyName(name);
299 propertyFilterConstraint.setOperator(ConstraintType.findByType(valueEntry.getKey()).orElse(null));
300 propertyFilterConstraint.setTargetType(PropertyFilterTargetType.PROPERTY);
301 final Optional<ToscaFunction> toscaFunction = PropertyFilterConstraintDataDefinitionHelper
302 .createToscaFunctionFromLegacyConstraintValue(valueEntry.getValue());
303 if (toscaFunction.isPresent()) {
304 final ToscaFunction toscaFunction1 = toscaFunction.get();
305 propertyFilterConstraint.setValue(toscaFunction1);
306 propertyFilterConstraint.setValueType(
307 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(toscaFunction1.getType()).orElse(null)
310 propertyFilterConstraint.setValue(valueEntry.getValue());
311 propertyFilterConstraint.setValueType(FilterValueType.STATIC);
313 constraints.add(propertyFilterConstraint);
319 private Map<String, PolicyDefinition> getPolicies(Map<String, Object> toscaJson, String model) {
320 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
321 .left().on(err -> new HashMap<>());
322 Map<String, Object> foundPolicies = (Map<String, Object>) mappedTopologyTemplate.get(POLICIES.getElementName());
323 if (MapUtils.isNotEmpty(foundPolicies)) {
324 return foundPolicies.entrySet().stream().map(policyToCreate -> createPolicy(policyToCreate, model))
325 .collect(Collectors.toMap(PolicyDefinition::getName, p -> p));
327 return Collections.emptyMap();
330 private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue, String model) {
331 PolicyDefinition emptyPolicyDef = new PolicyDefinition();
332 String policyName = policyNameValue.getKey();
333 emptyPolicyDef.setName(policyName);
335 // 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.
336 if (policyNameValue.getValue() instanceof Map) {
337 Map<String, Object> policyTemplateJsonMap = (Map<String, Object>) policyNameValue.getValue();
338 validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap, model);
340 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
342 } catch (ClassCastException e) {
343 log.debug("#createPolicy - Failed to create the policy {}. The exception occurred", policyName, e);
344 rollbackWithException(ActionStatus.INVALID_YAML);
346 return emptyPolicyDef;
349 private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, Map<String, Object> policyTemplateJsonMap, String model) {
350 String policyTypeName = (String) policyTemplateJsonMap.get(TYPE.getElementName());
351 if (StringUtils.isEmpty(policyTypeName)) {
352 log.debug("#validateAndFillPolicy - The 'type' member is not found under policy {}", emptyPolicyDefinition.getName());
353 rollbackWithException(ActionStatus.POLICY_MISSING_POLICY_TYPE, emptyPolicyDefinition.getName());
355 emptyPolicyDefinition.setType(policyTypeName);
356 // set policy targets
357 emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap));
358 PolicyTypeDefinition policyTypeDefinition = validateGetPolicyTypeDefinition(policyTypeName, model);
359 // set policy properties
360 emptyPolicyDefinition.setProperties(validateFillPolicyProperties(policyTypeDefinition, policyTemplateJsonMap));
363 private PolicyTypeDefinition validateGetPolicyTypeDefinition(String policyType, String modelName) {
364 PolicyTypeDefinition policyTypeDefinition = policyTypeBusinessLogic.getLatestPolicyTypeByType(policyType, modelName);
365 if (policyTypeDefinition == null) {
366 log.debug("#validateAndFillPolicy - The policy type {} not found", policyType);
367 rollbackWithException(ActionStatus.POLICY_TYPE_IS_INVALID, policyType);
369 return policyTypeDefinition;
372 private List<PropertyDataDefinition> validateFillPolicyProperties(final PolicyTypeDefinition policyTypeDefinition,
373 final Map<String, Object> policyTemplateJsonMap) {
374 if (policyTypeDefinition == null || CollectionUtils.isEmpty(policyTypeDefinition.getProperties())
375 || MapUtils.isEmpty(policyTemplateJsonMap)) {
376 return Collections.emptyList();
378 final Map<String, Object> propertiesJsonMap = (Map<String, Object>) policyTemplateJsonMap.get(PROPERTIES.getElementName());
379 if (MapUtils.isEmpty(propertiesJsonMap)) {
380 return Collections.emptyList();
382 return propertiesJsonMap.entrySet().stream()
383 .map(propertyJson -> {
384 final PropertyDefinition originalProperty =
385 policyTypeDefinition.getProperties().stream()
386 .filter(propertyDefinition -> propertyDefinition.getName().equals(propertyJson.getKey()))
389 if (originalProperty == null) {
392 final UploadPropInfo uploadPropInfo = buildProperty(propertyJson.getKey(), propertyJson.getValue());
393 final PropertyDefinition propertyDefinition = new PropertyDefinition(originalProperty);
394 propertyDefinition.setToscaFunction(uploadPropInfo.getToscaFunction());
395 propertyDefinition.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
396 propertyDefinition.setGetInputValues(uploadPropInfo.getGet_input());
397 propertyDefinition.setDescription(uploadPropInfo.getDescription());
398 String propertyValue = gson.toJson(uploadPropInfo.getValue());
399 if (!propertyDefinition.isToscaFunction()) {
400 propertyValue = PropertiesUtils.trimQuotes(propertyValue);
402 propertyDefinition.setValue(propertyValue);
403 return propertyDefinition;
405 .filter(Objects::nonNull)
406 .collect(Collectors.toList());
409 private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson) {
410 Map<PolicyTargetType, List<String>> targets = new EnumMap<>(PolicyTargetType.class);
411 if (policyTemplateJson.containsKey(TARGETS.getElementName()) && policyTemplateJson.get(TARGETS.getElementName()) instanceof List) {
412 List<String> targetsElement = (List<String>) policyTemplateJson.get(TARGETS.getElementName());
413 targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement);
418 private Map<String, UploadComponentInstanceInfo> getInstances(
419 Map<String, Object> toscaJson,
420 Map<String, String> createdNodesToscaResourceNames
422 Map<String, Object> nodeTemplates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES)
423 .left().on(err -> new HashMap<>());
424 if (nodeTemplates.isEmpty()) {
425 return Collections.emptyMap();
429 createdNodesToscaResourceNames,
434 private Map<String, UploadComponentInstanceInfo> getInstances(
435 Map<String, Object> toscaJson,
436 Map<String, String> createdNodesToscaResourceNames,
437 Map<String, Object> nodeTemplates
439 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
440 return nodeTemplates.entrySet().stream()
441 .map(node -> buildModuleComponentInstanceInfo(
443 substitutionMappings,
444 createdNodesToscaResourceNames
446 .collect(Collectors.toMap(UploadComponentInstanceInfo::getName, i -> i));
449 private Map<String, Object> getSubstitutionMappings(Map<String, Object> toscaJson) {
450 Either<Map<String, Object>, ResultStatusEnum> eitherSubstitutionMappings = findFirstToscaMapElement(toscaJson, SUBSTITUTION_MAPPINGS);
451 if (eitherSubstitutionMappings.isLeft()) {
452 return eitherSubstitutionMappings.left().value();
457 private void associateRelationshipTemplatesToInstances(final Map<String, UploadComponentInstanceInfo> instances,
458 final Map<String, Object> toscaJson) {
459 if (MapUtils.isEmpty(instances)) {
462 for (UploadComponentInstanceInfo instance : instances.values()) {
463 final Map<String, List<OperationUi>> operations = new HashMap<>();
464 final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
465 if (MapUtils.isNotEmpty(requirements)) {
466 requirements.values()
467 .forEach(requirementInfoList -> requirementInfoList.stream()
468 .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
469 .forEach(requirement -> operations.put(requirement.getRelationshipTemplate(),
470 getOperationsFromRelationshipTemplate(toscaJson, requirement.getRelationshipTemplate()))));
472 instance.setOperations(operations);
476 private Map<String, Object> getRelationshipTemplates(final Map<String, Object> toscaJson, final String relationshipTemplate) {
477 final Either<Map<String, Object>, ResultStatusEnum> eitherRelationshipTemplates = findFirstToscaMapElement(toscaJson, RELATIONSHIP_TEMPLATES);
478 if (eitherRelationshipTemplates.isRight()) {
479 throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_NOT_FOUND);
481 final Map<String, Object> relationshipTemplateMap = eitherRelationshipTemplates.left().value();
482 final Map<String, Map<String, Object>> relationship = (Map<String, Map<String, Object>>) relationshipTemplateMap.get(relationshipTemplate);
483 if (relationship == null) {
484 throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_DEFINITION_NOT_FOUND);
486 return relationship.get(INTERFACES.getElementName());
489 private List<ToscaInterfaceDefinition> buildToscaInterfacesFromRelationship(final Map<String, Object> interfaces) {
490 return interfaces.entrySet().stream()
492 final var toscaInterfaceDefinition = new ToscaInterfaceDefinition();
493 toscaInterfaceDefinition.setType(entry.getKey());
494 final Map<String, Object> toscaInterfaceMap = (Map<String, Object>) entry.getValue();
495 toscaInterfaceDefinition.setOperations((Map<String, Object>) toscaInterfaceMap.get(OPERATIONS.getElementName()));
496 return toscaInterfaceDefinition;
501 private Optional<Object> getImplementation(final Map<String, Object> operationToscaMap) {
502 if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(IMPLEMENTATION.getElementName())) {
503 return Optional.empty();
505 return Optional.ofNullable(operationToscaMap.get(IMPLEMENTATION.getElementName()));
508 private List<PropertyAssignmentUi> getOperationsInputs(final Map<String, Object> operationToscaMap) {
509 if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(INPUTS.getElementName())) {
510 return Collections.emptyList();
512 final Map<String, Object> inputsMap = (Map<String, Object>) operationToscaMap.get(INPUTS.getElementName());
513 return inputsMap.entrySet().stream().map(this::buildInputAssignment).collect(toList());
516 private PropertyAssignmentUi buildInputAssignment(final Entry<String, Object> inputAssignmentMap) {
517 var propertyAssignmentUi = new PropertyAssignmentUi();
518 propertyAssignmentUi.setName(inputAssignmentMap.getKey());
519 propertyAssignmentUi.setValue(inputAssignmentMap.getValue().toString());
520 propertyAssignmentUi.setType(STRING.getType());
521 return propertyAssignmentUi;
524 private List<OperationUi> getOperationsFromRelationshipTemplate(final Map<String, Object> toscaJson, final String relationshipTemplate) {
525 final List<OperationUi> operationUiList = new ArrayList<>();
526 final List<ToscaInterfaceDefinition> interfaces =
527 buildToscaInterfacesFromRelationship(getRelationshipTemplates(toscaJson, relationshipTemplate));
529 .filter(interfaceDefinition -> MapUtils.isNotEmpty(interfaceDefinition.getOperations()))
530 .forEach(interfaceDefinition ->
531 interfaceDefinition.getOperations()
532 .forEach((operationType, operationValue) ->
533 operationUiList.add(buildOperation(interfaceDefinition.getType(), operationType, (Map<String, Object>) operationValue))
535 return operationUiList;
538 private OperationUi buildOperation(final String interfaceType, final String operationType, final Map<String, Object> operationToscaMap) {
539 var operationUi = new OperationUi();
540 operationUi.setInterfaceType(interfaceType);
541 operationUi.setOperationType(operationType);
542 getImplementation(operationToscaMap).ifPresent(operationUi::setImplementation);
543 final List<PropertyAssignmentUi> operationsInputs = getOperationsInputs(operationToscaMap);
544 if (CollectionUtils.isNotEmpty(operationsInputs)) {
545 operationUi.setInputs(operationsInputs);
550 @SuppressWarnings("unchecked")
551 private Map<String, GroupDefinition> getGroups(Map<String, Object> toscaJson, String model) {
552 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
553 .left().on(err -> new HashMap<>());
554 Map<String, Object> foundGroups = (Map<String, Object>) mappedTopologyTemplate.get(GROUPS.getElementName());
555 if (MapUtils.isNotEmpty(foundGroups)) {
556 Map<String, GroupDefinition> groups = foundGroups.entrySet().stream().map(groupToCreate -> createGroup(groupToCreate, model))
557 .collect(Collectors.toMap(GroupDefinition::getName, g -> g));
558 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
559 if (capabilitiesSubstitutionMappingsExist(substitutionMappings)) {
560 groups.forEach((key, value) -> updateCapabilitiesNames(value,
561 getNamesToUpdate(key, (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName()))));
565 return new HashMap<>();
568 private void updateCapabilitiesNames(GroupDefinition group, Map<String, String> capabilityNames) {
569 if (MapUtils.isNotEmpty(group.getCapabilities())) {
570 group.getCapabilities().values().stream().flatMap(Collection::stream).filter(cap -> capabilityNames.containsKey(cap.getName()))
571 .forEach(cap -> cap.setName(capabilityNames.get(cap.getName())));
575 private Map<String, String> getNamesToUpdate(String name, Map<String, List<String>> pair) {
576 return pair.entrySet().stream().filter(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX).equalsIgnoreCase(name))
577 .collect(Collectors.toMap(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_NAME_IDX), Map.Entry::getKey, (n1, n2) -> n1));
580 private boolean capabilitiesSubstitutionMappingsExist(Map<String, Object> substitutionMappings) {
581 return substitutionMappings != null && substitutionMappings.containsKey(CAPABILITIES.getElementName());
584 private GroupDefinition createGroup(Map.Entry<String, Object> groupNameValue, String model) {
585 GroupDefinition group = new GroupDefinition();
586 group.setName(groupNameValue.getKey());
588 if (groupNameValue.getValue() instanceof Map) {
589 Map<String, Object> groupTemplateJsonMap = (Map<String, Object>) groupNameValue.getValue();
590 validateAndFillGroup(group, groupTemplateJsonMap, model);
591 validateUpdateGroupProperties(group, groupTemplateJsonMap);
592 validateUpdateGroupCapabilities(group, groupTemplateJsonMap);
594 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
596 } catch (ClassCastException e) {
597 log.debug("#createGroup - Failed to create the group {}. The exception occurres", groupNameValue.getKey(), e);
598 rollbackWithException(ActionStatus.INVALID_YAML);
603 private Map<String, CapabilityDefinition> addCapabilities(Map<String, CapabilityDefinition> cap, Map<String, CapabilityDefinition> otherCap) {
604 cap.putAll(otherCap);
608 private Map<String, CapabilityDefinition> addCapability(CapabilityDefinition cap) {
609 Map<String, CapabilityDefinition> map = Maps.newHashMap();
610 map.put(cap.getName(), cap);
614 private void setMembers(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
615 if (groupTemplateJsonMap.containsKey(MEMBERS.getElementName())) {
616 Object members = groupTemplateJsonMap.get(MEMBERS.getElementName());
617 if (members != null) {
618 if (members instanceof List) {
619 setMembersFromList(groupInfo, (List<?>) members);
621 log.debug("The 'members' member is not of type list under group {}", groupInfo.getName());
622 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
628 private void setMembersFromList(GroupDefinition groupInfo, List<?> membersAsList) {
629 groupInfo.setMembers(membersAsList.stream().collect(Collectors.toMap(Object::toString, member -> "")));
632 @SuppressWarnings("unchecked")
633 private void validateUpdateGroupProperties(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
634 if (groupTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
635 Object propertiesElement = groupTemplateJsonMap.get(PROPERTIES.getElementName());
636 if (propertiesElement instanceof Map) {
637 mergeGroupProperties(groupInfo, (Map<String, Object>) propertiesElement);
642 private void mergeGroupProperties(final GroupDefinition groupDefinition, final Map<String, Object> parsedProperties) {
643 if (CollectionUtils.isEmpty(groupDefinition.getProperties())) {
646 validateGroupProperties(parsedProperties, groupDefinition);
647 groupDefinition.getProperties().stream()
648 .filter(property -> parsedProperties.containsKey(property.getName()))
649 .forEach(property -> mergeGroupProperty(property, parsedProperties.get(property.getName())));
652 private void mergeGroupProperty(final PropertyDataDefinition property, final Object propertyYaml) {
653 final UploadPropInfo uploadPropInfo = buildProperty(property.getName(), propertyYaml);
654 property.setToscaFunction(uploadPropInfo.getToscaFunction());
655 property.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
656 property.setValue(convertPropertyValue(ToscaPropertyType.isValidType(property.getType()), uploadPropInfo.getValue()));
657 property.setGetInputValues(uploadPropInfo.getGet_input());
660 private String convertPropertyValue(ToscaPropertyType type, Object value) {
661 String convertedValue = null;
663 if (type == null || value instanceof Map || value instanceof List) {
664 convertedValue = gson.toJson(value);
666 convertedValue = value.toString();
669 return convertedValue;
672 private void setDescription(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
673 if (groupTemplateJsonMap.containsKey(DESCRIPTION.getElementName())) {
674 groupInfo.setDescription((String) groupTemplateJsonMap.get(DESCRIPTION.getElementName()));
678 private void validateAndFillGroup(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap, String model) {
679 String type = (String) groupTemplateJsonMap.get(TYPE.getElementName());
680 if (StringUtils.isEmpty(type)) {
681 log.debug("#validateAndFillGroup - The 'type' member is not found under group {}", groupInfo.getName());
682 rollbackWithException(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupInfo.getName());
684 groupInfo.setType(type);
685 GroupTypeDefinition groupType = groupTypeBusinessLogic.getLatestGroupTypeByType(type, model);
686 if (groupType == null) {
687 log.debug("#validateAndFillGroup - The group type {} not found", groupInfo.getName());
688 rollbackWithException(ActionStatus.GROUP_TYPE_IS_INVALID, type);
690 groupInfo.convertFromGroupProperties(groupType.getProperties());
691 groupInfo.convertCapabilityDefinitions(groupType.getCapabilities());
692 setDescription(groupInfo, groupTemplateJsonMap);
693 setMembers(groupInfo, groupTemplateJsonMap);
696 @SuppressWarnings("unchecked")
697 private void validateUpdateGroupCapabilities(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
698 if (groupTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
699 Object capabilities = groupTemplateJsonMap.get(CAPABILITIES.getElementName());
700 if (capabilities instanceof List) {
701 validateUpdateCapabilities(groupInfo, ((List<Object>) capabilities).stream().map(o -> buildGroupCapability(groupInfo, o))
702 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
703 } else if (capabilities instanceof Map) {
704 validateUpdateCapabilities(groupInfo,
705 ((Map<String, Object>) capabilities).entrySet().stream().map(e -> buildGroupCapability(groupInfo, e))
706 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
708 log.debug("#setCapabilities - Failed to import the capabilities of the group {}. ", groupInfo.getName());
709 rollbackWithException(ActionStatus.INVALID_YAML);
714 private void validateUpdateCapabilities(GroupDefinition groupInfo, Map<String, Map<String, CapabilityDefinition>> capabilityInfo) {
715 validateGroupCapabilities(groupInfo, capabilityInfo);
716 groupInfo.updateCapabilitiesProperties(capabilityInfo);
719 private void validateGroupCapabilities(GroupDefinition group, Map<String, Map<String, CapabilityDefinition>> parsedCapabilities) {
720 if (MapUtils.isNotEmpty(parsedCapabilities)) {
721 if (MapUtils.isEmpty(group.getCapabilities())) {
722 failOnMissingCapabilityTypes(group, Lists.newArrayList(parsedCapabilities.keySet()));
724 List<String> missingCapTypes = parsedCapabilities.keySet().stream().filter(ct -> !group.getCapabilities().containsKey(ct))
726 if (CollectionUtils.isNotEmpty(missingCapTypes)) {
727 failOnMissingCapabilityTypes(group, missingCapTypes);
729 group.getCapabilities().entrySet().forEach(e -> validateCapabilities(group, e.getValue(), parsedCapabilities.get(e.getKey())));
733 private void validateCapabilities(GroupDefinition group, List<CapabilityDefinition> capabilities,
734 Map<String, CapabilityDefinition> parsedCapabilities) {
735 List<String> allowedCapNames = capabilities.stream().map(CapabilityDefinition::getName).distinct().collect(toList());
736 List<String> missingCapNames = parsedCapabilities.keySet().stream().filter(c -> !allowedCapNames.contains(c)).collect(toList());
737 if (CollectionUtils.isNotEmpty(missingCapNames)) {
738 failOnMissingCapabilityNames(group, missingCapNames);
740 validateCapabilitiesProperties(capabilities, parsedCapabilities);
743 private void validateCapabilitiesProperties(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
744 capabilities.forEach(c -> validateCapabilityProperties(c, parsedCapabilities.get(c.getName())));
747 private void validateCapabilityProperties(CapabilityDefinition capability, CapabilityDefinition parsedCapability) {
748 if (parsedCapability != null && parsedCapability.getProperties() != null) {
749 List<String> parsedPropertiesNames = parsedCapability.getProperties().stream().map(ComponentInstanceProperty::getName).collect(toList());
750 validateProperties(capability.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
751 ActionStatus.PROPERTY_NOT_FOUND, capability.getName(), capability.getType());
755 private void validateGroupProperties(Map<String, Object> parsedProperties, GroupDefinition groupInfo) {
756 List<String> parsedPropertiesNames = new ArrayList<>(parsedProperties.keySet());
757 validateProperties(groupInfo.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
758 ActionStatus.GROUP_PROPERTY_NOT_FOUND, groupInfo.getName(), groupInfo.getType());
761 private void validateProperties(List<String> validProperties, List<String> parsedProperties, ActionStatus actionStatus, String name,
763 if (CollectionUtils.isNotEmpty(parsedProperties)) {
764 verifyMissingProperties(actionStatus, name, type, parsedProperties.stream().filter(n -> !validProperties.contains(n)).collect(toList()));
768 private void verifyMissingProperties(ActionStatus actionStatus, String name, String type, List<String> missingProperties) {
769 if (CollectionUtils.isNotEmpty(missingProperties)) {
770 if (log.isDebugEnabled()) {
771 log.debug("#validateProperties - Failed to validate properties. The properties {} are missing on {} of the type {}. ",
772 missingProperties.toString(), name, type);
774 rollbackWithException(actionStatus, missingProperties.toString(), missingProperties.toString(), name, type);
778 @SuppressWarnings("unchecked")
779 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Object capObject) {
780 if (!(capObject instanceof Map)) {
781 log.debug("#convertToGroupCapability - Failed to import the capability {}. ", capObject);
782 rollbackWithException(ActionStatus.INVALID_YAML);
784 return buildGroupCapability(groupInfo, ((Map<String, Object>) capObject).entrySet().iterator().next());
787 @SuppressWarnings("unchecked")
788 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Map.Entry<String, Object> capEntry) {
789 CapabilityDefinition capability = new CapabilityDefinition();
790 capability.setOwnerType(CapabilityDataDefinition.OwnerType.GROUP);
791 capability.setName(capEntry.getKey());
792 capability.setParentName(capEntry.getKey());
793 capability.setOwnerId(groupInfo.getName());
794 if (!(capEntry.getValue() instanceof Map)) {
795 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
796 rollbackWithException(ActionStatus.INVALID_YAML);
798 Map<String, Object> capabilityValue = (Map<String, Object>) capEntry.getValue();
799 String type = (String) capabilityValue.get(TYPE.getElementName());
800 if (StringUtils.isEmpty(type)) {
801 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. Missing capability type. ", capEntry.getKey());
802 rollbackWithException(ActionStatus.INVALID_YAML);
804 capability.setType(type);
805 if (!(capabilityValue.get(PROPERTIES.getElementName()) instanceof Map)) {
806 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
807 rollbackWithException(ActionStatus.INVALID_YAML);
809 Map<String, Object> properties = (Map<String, Object>) capabilityValue.get(PROPERTIES.getElementName());
810 capability.setProperties(properties.entrySet().stream().map(this::convertToProperty).collect(toList()));
814 private ComponentInstanceProperty convertToProperty(Map.Entry<String, Object> e) {
815 ComponentInstanceProperty property = new ComponentInstanceProperty();
816 property.setName(e.getKey());
817 property.setValue((String) e.getValue());
821 @SuppressWarnings("unchecked")
822 private UploadComponentInstanceInfo buildModuleComponentInstanceInfo(
823 Map.Entry<String, Object> nodeTemplateJsonEntry,
824 Map<String, Object> substitutionMappings,
825 Map<String, String> createdNodesToscaResourceNames
827 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
828 nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey());
830 if (nodeTemplateJsonEntry.getValue() instanceof String) {
831 String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue();
832 nodeTemplateInfo.setType(nodeTemplateJsonString);
833 } else if (nodeTemplateJsonEntry.getValue() instanceof Map) {
834 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) nodeTemplateJsonEntry.getValue();
835 setToscaResourceType(createdNodesToscaResourceNames, nodeTemplateInfo, nodeTemplateJsonMap);
836 setRequirements(nodeTemplateInfo, nodeTemplateJsonMap);
837 setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
838 setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
839 updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
840 updateAttributes(nodeTemplateInfo, nodeTemplateJsonMap);
841 updateInterfaces(nodeTemplateInfo, nodeTemplateJsonMap);
842 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
843 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
844 setSubstitutions(substitutionMappings, nodeTemplateInfo);
845 setOccurrencesAndInstanceCount(nodeTemplateInfo, nodeTemplateJsonMap);
847 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
849 } catch (ClassCastException e) {
850 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
851 log.debug("error when creating capability, message:{}", e.getMessage(), e);
852 rollbackWithException(ActionStatus.INVALID_YAML);
854 return nodeTemplateInfo;
857 @SuppressWarnings("unchecked")
858 private void setSubstitutions(Map<String, Object> substitutionMappings, UploadComponentInstanceInfo nodeTemplateInfo) {
859 if (substitutionMappings != null) {
860 if (substitutionMappings.containsKey(CAPABILITIES.getElementName())) {
861 nodeTemplateInfo.setCapabilitiesNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
862 (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName())));
864 if (substitutionMappings.containsKey(REQUIREMENTS.getElementName())) {
865 nodeTemplateInfo.setRequirementsNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
866 (Map<String, List<String>>) substitutionMappings.get(REQUIREMENTS.getElementName())));
871 private void updateProperties(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
872 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
873 Map<String, List<UploadPropInfo>> properties =
874 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
875 if (!properties.isEmpty()) {
876 nodeTemplateInfo.setProperties(properties);
881 private void updateAttributes(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
882 if (nodeTemplateJsonMap.containsKey(ATTRIBUTES.getElementName())) {
883 Map<String, UploadAttributeInfo> attributes = buildAttributeModuleFromYaml(nodeTemplateJsonMap);
884 if (!attributes.isEmpty()) {
885 nodeTemplateInfo.setAttributes(attributes);
890 private void updateInterfaces(
891 UploadComponentInstanceInfo nodeTemplateInfo,
892 Map<String, Object> nodeTemplateJsonMap
894 if (nodeTemplateJsonMap.containsKey(INTERFACES.getElementName())) {
895 Map<String, UploadInterfaceInfo> interfaces = buildInterfacesModuleFromYaml(
898 if (!interfaces.isEmpty()) {
899 nodeTemplateInfo.setInterfaces(interfaces);
904 private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
905 if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
906 Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
907 if (!eitherCapRes.isEmpty()) {
908 nodeTemplateInfo.setCapabilities(eitherCapRes);
913 private void setArtifacts(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
914 if (nodeTemplateJsonMap.containsKey(ARTIFACTS.getElementName())) {
915 Map<String, Map<String, UploadArtifactInfo>> eitherArtifactsRes = createArtifactsModuleFromYaml(nodeTemplateJsonMap);
916 if (!eitherArtifactsRes.isEmpty()) {
917 nodeTemplateInfo.setArtifacts(eitherArtifactsRes);
922 private void setRequirements(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
923 if (nodeTemplateJsonMap.containsKey(REQUIREMENTS.getElementName())) {
924 Map<String, List<UploadReqInfo>> regResponse = createReqModuleFromYaml(nodeTemplateJsonMap, nodeTemplateInfo.getName());
925 if (!regResponse.isEmpty()) {
926 nodeTemplateInfo.setRequirements(regResponse);
931 private void setToscaResourceType(Map<String, String> createdNodesToscaResourceNames, UploadComponentInstanceInfo nodeTemplateInfo,
932 Map<String, Object> nodeTemplateJsonMap) {
933 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
934 String toscaResourceType = (String) nodeTemplateJsonMap.get(TYPE.getElementName());
935 if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) {
936 toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType);
938 nodeTemplateInfo.setType(toscaResourceType);
942 private void setDirectives(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
943 List<String> directives = (List<String>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.DIRECTIVES.getElementName());
944 nodeTemplateInfo.setDirectives(directives);
947 private void setNodeFilter(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
948 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())) {
949 nodeTemplateInfo.setUploadNodeFilterInfo(new NodeFilterUploadCreator()
950 .createNodeFilterData(nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())));
954 @SuppressWarnings("unchecked")
955 private void setOccurrencesAndInstanceCount(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
956 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
957 List<Object> occurrences = (List<Object>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
958 nodeTemplateInfo.setMinOccurrences(occurrences.get(0).toString());
959 nodeTemplateInfo.setMaxOccurrences(occurrences.get(1).toString());
961 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName())) {
962 Object instanceCount = nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName());
963 if (instanceCount instanceof Map) {
964 String instanceCountAsString = "{get_input:" + (String)((Map)instanceCount).get("get_input") + "}";
965 nodeTemplateInfo.setInstanceCount(instanceCountAsString);
967 nodeTemplateInfo.setInstanceCount(instanceCount.toString());
972 @SuppressWarnings("unchecked")
973 private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap, String nodeName) {
974 Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
975 Either<List<Object>, ResultStatusEnum> requirementsListRes = findFirstToscaListElement(nodeTemplateJsonMap, REQUIREMENTS);
976 if (requirementsListRes.isLeft()) {
977 for (Object jsonReqObj : requirementsListRes.left().value()) {
978 String reqName = ((Map<String, Object>) jsonReqObj).keySet().iterator().next();
979 Object reqJson = ((Map<String, Object>) jsonReqObj).get(reqName);
980 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
983 Either<Map<String, Object>, ResultStatusEnum> requirementsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, REQUIREMENTS);
984 if (requirementsMapRes.isLeft()) {
985 for (Map.Entry<String, Object> entry : requirementsMapRes.left().value().entrySet()) {
986 String reqName = entry.getKey();
987 Object reqJson = entry.getValue();
988 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
992 return moduleRequirements;
995 private void addModuleNodeTemplateReq(Map<String, List<UploadReqInfo>> moduleRequirements, Object requirementJson, String requirementName,
997 UploadReqInfo requirement = buildModuleNodeTemplateReg(requirementJson, nodeName);
998 requirement.setName(requirementName);
999 if (moduleRequirements.containsKey(requirementName)) {
1000 moduleRequirements.get(requirementName).add(requirement);
1002 List<UploadReqInfo> list = new ArrayList<>();
1003 list.add(requirement);
1004 moduleRequirements.put(requirementName, list);
1008 @SuppressWarnings("unchecked")
1009 private Map<String, Map<String, UploadArtifactInfo>> createArtifactsModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
1010 Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts = new HashMap<>();
1011 Either<List<Object>, ResultStatusEnum> artifactsListRes = findFirstToscaListElement(nodeTemplateJsonMap, ARTIFACTS);
1012 if (artifactsListRes.isLeft()) {
1013 for (Object jsonArtifactObj : artifactsListRes.left().value()) {
1014 String key = ((Map<String, Object>) jsonArtifactObj).keySet().iterator().next();
1015 Object artifactJson = ((Map<String, Object>) jsonArtifactObj).get(key);
1016 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, key);
1019 Either<Map<String, Map<String, Object>>, ResultStatusEnum> artifactsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, ARTIFACTS);
1020 if (artifactsMapRes.isLeft()) {
1021 for (Map.Entry<String, Map<String, Object>> entry : artifactsMapRes.left().value().entrySet()) {
1022 String artifactName = entry.getKey();
1023 Object artifactJson = entry.getValue();
1024 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, artifactName);
1028 return moduleArtifacts;
1031 private void addModuleNodeTemplateArtifacts(Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts, Object artifactJson,
1032 String artifactName) {
1033 UploadArtifactInfo artifact = buildModuleNodeTemplateArtifact(artifactJson);
1034 artifact.setName(artifactName);
1035 if (moduleArtifacts.containsKey(ARTIFACTS.getElementName())) {
1036 moduleArtifacts.get(ARTIFACTS.getElementName()).put(artifactName, artifact);
1038 Map<String, UploadArtifactInfo> map = new HashMap<>();
1039 map.put(artifactName, artifact);
1040 moduleArtifacts.put(ARTIFACTS.getElementName(), map);
1044 @SuppressWarnings("unchecked")
1045 private UploadArtifactInfo buildModuleNodeTemplateArtifact(Object artifactObject) {
1046 UploadArtifactInfo artifactTemplateInfo = new UploadArtifactInfo();
1047 if (artifactObject instanceof Map) {
1048 fillArtifact(artifactTemplateInfo, (Map<String, Object>) artifactObject);
1050 return artifactTemplateInfo;
1053 private void fillArtifact(UploadArtifactInfo artifactTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1054 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1055 artifactTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1057 if (nodeTemplateJsonMap.containsKey(FILE.getElementName())) {
1058 artifactTemplateInfo.setFile((String) nodeTemplateJsonMap.get(FILE.getElementName()));
1060 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1061 Map<String, List<UploadPropInfo>> props =
1062 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1063 if (!props.isEmpty()) {
1064 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1065 artifactTemplateInfo.setProperties(properties);
1070 @SuppressWarnings("unchecked")
1071 private Map<String, List<UploadCapInfo>> createCapModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
1072 Map<String, List<UploadCapInfo>> moduleCap = new HashMap<>();
1073 Map<String, Object> capabilities = (Map<String, Object>) nodeTemplateJsonMap.get(CAPABILITIES.getElementName());
1074 if (MapUtils.isNotEmpty(capabilities)) {
1075 for (Map.Entry<String, Object> entry : capabilities.entrySet()) {
1076 String capName = entry.getKey();
1077 Object capJson = entry.getValue();
1078 addModuleNodeTemplateCap(moduleCap, capJson, capName);
1084 private void addModuleNodeTemplateCap(Map<String, List<UploadCapInfo>> moduleCap, Object capJson, String key) {
1085 UploadCapInfo capabilityDef = buildModuleNodeTemplateCap(capJson);
1086 capabilityDef.setKey(key);
1087 if (moduleCap.containsKey(key)) {
1088 moduleCap.get(key).add(capabilityDef);
1090 List<UploadCapInfo> list = new ArrayList<>();
1091 list.add(capabilityDef);
1092 moduleCap.put(key, list);
1096 @SuppressWarnings("unchecked")
1097 private UploadCapInfo buildModuleNodeTemplateCap(Object capObject) {
1098 UploadCapInfo capTemplateInfo = new UploadCapInfo();
1099 if (capObject instanceof String) {
1100 String nodeTemplateJsonString = (String) capObject;
1101 capTemplateInfo.setNode(nodeTemplateJsonString);
1102 } else if (capObject instanceof Map) {
1103 fillCapability(capTemplateInfo, (Map<String, Object>) capObject);
1105 return capTemplateInfo;
1108 private void fillCapability(UploadCapInfo capTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1109 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1110 capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1112 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1113 capTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1115 if (nodeTemplateJsonMap.containsKey(VALID_SOURCE_TYPES.getElementName())) {
1116 Either<List<Object>, ResultStatusEnum> validSourceTypesRes = findFirstToscaListElement(nodeTemplateJsonMap, VALID_SOURCE_TYPES);
1117 if (validSourceTypesRes.isLeft()) {
1118 capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream().map(Object::toString).collect(toList()));
1121 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1122 Map<String, List<UploadPropInfo>> props =
1123 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1124 if (!props.isEmpty()) {
1125 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1126 capTemplateInfo.setProperties(properties);
1131 @SuppressWarnings("unchecked")
1132 private UploadReqInfo buildModuleNodeTemplateReg(Object regObject, String nodeName) {
1133 UploadReqInfo regTemplateInfo = new UploadReqInfo();
1134 if (regObject instanceof String) {
1135 String nodeTemplateJsonString = (String) regObject;
1136 regTemplateInfo.setNode(nodeTemplateJsonString);
1137 } else if (regObject instanceof Map) {
1138 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) regObject;
1139 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1140 regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1142 if (nodeTemplateJsonMap.containsKey(CAPABILITY.getElementName())) {
1143 regTemplateInfo.setCapabilityName((String) nodeTemplateJsonMap.get(CAPABILITY.getElementName()));
1145 if (nodeTemplateJsonMap.containsKey(RELATIONSHIP.getElementName())) {
1146 final String template = (String) nodeTemplateJsonMap.get(RELATIONSHIP.getElementName());
1147 if (StringUtils.isNotEmpty(nodeName) && template.contains(nodeName)) {
1148 regTemplateInfo.setRelationshipTemplate(template);
1152 return regTemplateInfo;
1155 private Map<String, UploadAttributeInfo> buildAttributeModuleFromYaml(
1156 Map<String, Object> nodeTemplateJsonMap) {
1157 Map<String, UploadAttributeInfo> moduleAttribute = new HashMap<>();
1158 Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(nodeTemplateJsonMap, ATTRIBUTES);
1159 if (toscaAttributes.isLeft()) {
1160 Map<String, Object> jsonAttributes = toscaAttributes.left().value();
1161 for (Map.Entry<String, Object> jsonAttributeObj : jsonAttributes.entrySet()) {
1162 UploadAttributeInfo attributeDef = buildAttribute(jsonAttributeObj.getKey(), jsonAttributeObj.getValue());
1163 moduleAttribute.put(attributeDef.getName(), attributeDef);
1166 return moduleAttribute;
1169 private UploadAttributeInfo buildAttribute(String attributeName, Object attributeValue) {
1170 UploadAttributeInfo attributeDef = new UploadAttributeInfo();
1171 attributeDef.setValue(attributeValue);
1172 attributeDef.setName(attributeName);
1173 return attributeDef;
1176 private Map<String, List<UploadPropInfo>> buildPropModuleFromYaml(final Map<String, Object> propertyMap) {
1177 final Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
1178 propertyMap.entrySet().forEach(propertyMapEntry -> addProperty(moduleProp, propertyMapEntry));
1182 private Map<String, UploadInterfaceInfo> buildInterfacesModuleFromYaml(
1183 Map<String, Object> nodeTemplateJsonMap
1185 Map<String, UploadInterfaceInfo> moduleInterfaces = new HashMap<>();
1186 Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = findFirstToscaMapElement(nodeTemplateJsonMap, INTERFACES);
1187 if (toscaInterfaces.isLeft()) {
1188 Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
1189 for (Map.Entry<String, Object> jsonInterfacesObj : jsonInterfaces.entrySet()) {
1190 addInterfaces(moduleInterfaces, jsonInterfacesObj);
1193 return moduleInterfaces;
1196 private void addProperty(Map<String, List<UploadPropInfo>> moduleProp, Map.Entry<String, Object> jsonPropObj) {
1197 UploadPropInfo propertyDef = buildProperty(jsonPropObj.getKey(), jsonPropObj.getValue());
1198 if (moduleProp.containsKey(propertyDef.getName())) {
1199 moduleProp.get(propertyDef.getName()).add(propertyDef);
1201 List<UploadPropInfo> list = new ArrayList<>();
1202 list.add(propertyDef);
1203 moduleProp.put(propertyDef.getName(), list);
1207 private void addInterfaces(Map<String, UploadInterfaceInfo> moduleInterface, Map.Entry<String, Object> jsonPropObj) {
1208 UploadInterfaceInfo interfaceInfo = buildInterface(jsonPropObj.getKey(), jsonPropObj.getValue());
1209 moduleInterface.put(jsonPropObj.getKey(), interfaceInfo);
1212 @SuppressWarnings("unchecked")
1213 private UploadPropInfo buildProperty(String propName, Object propValueObj) {
1214 final var propertyDef = new UploadPropInfo();
1215 propertyDef.setValue(propValueObj);
1216 propertyDef.setName(propName);
1217 if (propValueObj instanceof Map) {
1218 final Map<String, Object> propValueMap = (Map<String, Object>) propValueObj;
1219 if (propValueMap.containsKey(TYPE.getElementName())) {
1220 propertyDef.setType(propValueMap.get(TYPE.getElementName()).toString());
1222 if (containsGetInput(propValueObj)) {
1223 fillInputRecursively(propName, propValueMap, propertyDef);
1225 if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(propValueObj)) {
1226 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(propValueMap).ifPresent(propertyDef::setToscaFunction);
1228 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
1229 buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1230 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1231 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1232 if (existingSubPropertyToscaFunctions == null) {
1233 propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1235 propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1239 if (propValueMap.containsKey(DESCRIPTION.getElementName())) {
1240 propertyDef.setDescription((propValueMap).get(DESCRIPTION.getElementName()).toString());
1242 if (propValueMap.containsKey(DEFAULT_VALUE.getElementName())) {
1243 propertyDef.setValue(propValueMap.get(DEFAULT_VALUE.getElementName()));
1245 if (propValueMap.containsKey(IS_PASSWORD.getElementName())) {
1246 propertyDef.setPassword(Boolean.getBoolean(propValueMap.get(IS_PASSWORD.getElementName()).toString()));
1248 propertyDef.setValue(propValueObj);
1250 } else if (propValueObj instanceof List) {
1251 fillInputsListRecursively(propertyDef, (List<Object>) propValueObj);
1252 propertyDef.setValue(propValueObj);
1257 private Collection<SubPropertyToscaFunction> buildSubPropertyToscaFunctions(final Map<String, Object> propValueMap, final List<String> path) {
1258 Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = new ArrayList<>();
1259 propValueMap.entrySet().stream().filter(entry -> entry.getValue() instanceof Map).forEach(entry -> {
1260 List<String> subPropertyPath = new ArrayList<>(path);
1261 subPropertyPath.add(entry.getKey());
1262 if (ToscaFunctionType.findType(((Map<String, Object>) entry.getValue()).keySet().iterator().next()).isPresent()) {
1263 Optional<ToscaFunction> toscaFunction =
1264 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
1265 if (toscaFunction.isPresent()) {
1266 SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
1267 subPropertyToscaFunction.setToscaFunction(toscaFunction.get());
1268 subPropertyToscaFunction.setSubPropertyPath(subPropertyPath);
1269 subPropertyToscaFunctions.add(subPropertyToscaFunction);
1272 subPropertyToscaFunctions.addAll(buildSubPropertyToscaFunctions((Map<String, Object>) entry.getValue(), subPropertyPath));
1275 return subPropertyToscaFunctions;
1278 private UploadInterfaceInfo buildInterface(String interfaceName, Object interfaceValue) {
1279 UploadInterfaceInfo interfaceDef = new UploadInterfaceInfo();
1280 interfaceDef.setValue(interfaceValue);
1281 interfaceDef.setName(interfaceName);
1282 interfaceDef.setKey(interfaceName);
1283 Map<String, OperationDataDefinition> operations = new HashMap<>();
1284 if (interfaceValue instanceof Map) {
1285 Map<String, Object> operationsMap = (Map<String, Object>) interfaceValue;
1286 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
1287 OperationDataDefinition operationDef = new OperationDataDefinition();
1288 operationDef.setName(operationEntry.getKey());
1289 Map<String, Object> operationValue = (Map<String, Object>) operationEntry.getValue();
1290 if (operationValue.containsKey(DESCRIPTION.getElementName())) {
1291 operationDef.setDescription(operationValue.get(DESCRIPTION.getElementName()).toString());
1293 operationDef.setImplementation(handleOperationImplementation(operationValue).orElse(new ArtifactDataDefinition()));
1294 if (operationValue.containsKey(INPUTS.getElementName())) {
1295 final Map<String, Object> interfaceInputs = (Map<String, Object>) operationValue.get(INPUTS.getElementName());
1296 operationDef.setInputs(handleInterfaceOperationInputs(interfaceInputs));
1298 operations.put(operationEntry.getKey(), operationDef);
1300 interfaceDef.setOperations(operations);
1301 if (operationsMap.containsKey(TYPE.getElementName())) {
1302 interfaceDef.setType(((Map<String, Object>) interfaceValue).get(TYPE.getElementName()).toString());
1305 return interfaceDef;
1308 private ListDataDefinition<OperationInputDefinition> handleInterfaceOperationInputs(final Map<String, Object> interfaceInputs) {
1309 final ListDataDefinition<OperationInputDefinition> inputs = new ListDataDefinition<>();
1310 for (final Entry<String, Object> interfaceInput : interfaceInputs.entrySet()) {
1311 final OperationInputDefinition operationInput = new OperationInputDefinition();
1312 operationInput.setUniqueId(UUID.randomUUID().toString());
1313 operationInput.setInputId(operationInput.getUniqueId());
1314 operationInput.setName(interfaceInput.getKey());
1316 handleInputToscaDefinition(interfaceInput.getKey(), interfaceInput.getValue(), operationInput);
1317 inputs.add(operationInput);
1322 private void handleInputToscaDefinition(
1323 final String inputName,
1325 final OperationInputDefinition operationInput
1327 if (value instanceof Map) {
1328 final Map<String, Object> valueMap = (Map<String, Object>) value;
1329 log.debug("Creating interface operation input '{}'", inputName);
1330 Type type = new TypeToken<LinkedHashMap<String, Object>>() {
1332 String stringValue = gson.toJson(value, type);
1333 if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) {
1334 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value)
1335 .ifPresent(operationInput::setToscaFunction);
1337 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>());
1338 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1339 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = operationInput.getSubPropertyToscaFunctions();
1340 if (existingSubPropertyToscaFunctions == null) {
1341 operationInput.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1343 operationInput.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1347 operationInput.setValue(stringValue);
1349 if (value instanceof String) {
1350 final String stringValue = (String) value;
1351 operationInput.setDefaultValue(stringValue);
1352 operationInput.setToscaDefaultValue(stringValue);
1353 operationInput.setValue(stringValue);
1355 operationInput.setType("string");
1356 if (operationInput.getValue() == null) {
1357 operationInput.setValue(String.valueOf(value));
1361 private Optional<ArtifactDataDefinition> handleOperationImplementation(
1362 final Map<String, Object> operationDefinitionMap
1364 if (!operationDefinitionMap.containsKey(IMPLEMENTATION.getElementName())) {
1365 return Optional.empty();
1367 final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition();
1368 if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
1369 ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("primary")) {
1371 final Object primary = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("primary");
1372 if (primary instanceof Map) {
1373 Map<String, Object> implDetails = (Map) primary;
1375 if (implDetails.get("file") != null) {
1376 final String file = implDetails.get("file").toString();
1377 artifactDataDefinition.setArtifactName(generateArtifactName(file));
1379 if (implDetails.get("type") != null) {
1380 artifactDataDefinition.setArtifactType(implDetails.get("type").toString());
1382 if (implDetails.get("artifact_version") != null) {
1383 artifactDataDefinition.setArtifactVersion(implDetails.get("artifact_version").toString());
1386 if (implDetails.get("properties") instanceof Map) {
1387 Map<String, Object> properties = (Map<String, Object>) implDetails.get("properties");
1388 properties.forEach((k, v) -> {
1389 ToscaPropertyType type = getTypeFromObject(v);
1391 PropertyDataDefinition propertyDef = new PropertyDataDefinition();
1392 propertyDef.setName(k);
1393 propertyDef.setValue(v.toString());
1394 artifactDataDefinition.addProperty(propertyDef);
1399 artifactDataDefinition.setArtifactName(generateArtifactName(primary.toString()));
1403 if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
1404 ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("timeout")) {
1405 final Object timeOut = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("timeout");
1406 artifactDataDefinition.setTimeout((Integer)timeOut);
1409 if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof String) {
1410 final String implementation = (String) operationDefinitionMap.get(IMPLEMENTATION.getElementName());
1411 artifactDataDefinition.setArtifactName(generateArtifactName(implementation));
1413 return Optional.of(artifactDataDefinition);
1416 private String generateArtifactName(final String name) {
1417 if (OperationArtifactUtil.artifactNameIsALiteralValue(name)) {
1420 return QUOTE + name + QUOTE;
1424 private ToscaPropertyType getTypeFromObject(final Object value) {
1425 if (value instanceof String) {
1426 return ToscaPropertyType.STRING;
1428 if (value instanceof Integer) {
1429 return ToscaPropertyType.INTEGER;
1431 if (value instanceof Boolean) {
1432 return ToscaPropertyType.BOOLEAN;
1434 if (value instanceof Float || value instanceof Double) {
1435 return ToscaPropertyType.FLOAT;
1440 @SuppressWarnings("unchecked")
1441 private boolean containsGetInput(Object propValue) {
1442 return ((Map<String, Object>) propValue).containsKey(GET_INPUT.getElementName()) || ImportUtils.containsGetInput(propValue);
1445 @SuppressWarnings("unchecked")
1446 private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
1448 for (Object objValue : propValueList) {
1449 if (objValue instanceof Map) {
1450 Map<String, Object> objMap = (Map<String, Object>) objValue;
1451 Map<String, Object> propValueMap = new HashMap<>();
1452 propValueMap.put(String.valueOf(index), objValue);
1453 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
1454 buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1455 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1456 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1457 if (existingSubPropertyToscaFunctions == null) {
1458 propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1460 propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1463 if (objMap.containsKey(GET_INPUT.getElementName())) {
1464 fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
1466 Set<String> keys = objMap.keySet();
1467 findAndFillInputsListRecursively(propertyDef, objMap, keys);
1469 } else if (objValue instanceof List) {
1470 List<Object> propSubValueList = (List<Object>) objValue;
1471 fillInputsListRecursively(propertyDef, propSubValueList);
1477 @SuppressWarnings("unchecked")
1478 private void findAndFillInputsListRecursively(UploadPropInfo propertyDef, Map<String, Object> objMap, Set<String> keys) {
1479 for (String key : keys) {
1480 Object value = objMap.get(key);
1481 if (value instanceof Map) {
1482 fillInputRecursively(key, (Map<String, Object>) value, propertyDef);
1483 } else if (value instanceof List) {
1484 List<Object> propSubValueList = (List<Object>) value;
1485 fillInputsListRecursively(propertyDef, propSubValueList);
1490 private void fillInputRecursively(String propName, Map<String, Object> propValue, UploadPropInfo propertyDef) {
1491 if (propValue.containsKey(GET_INPUT.getElementName())) {
1492 Object getInput = propValue.get(GET_INPUT.getElementName());
1493 GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition();
1494 List<GetInputValueDataDefinition> getInputs = propertyDef.getGet_input();
1495 if (getInputs == null) {
1496 getInputs = new ArrayList<>();
1498 if (getInput instanceof String) {
1499 getInputInfo.setInputName((String) getInput);
1500 getInputInfo.setPropName(propName);
1501 } else if (getInput instanceof List) {
1502 fillInput(propName, getInput, getInputInfo);
1504 getInputs.add(getInputInfo);
1505 propertyDef.setGet_input(getInputs);
1506 propertyDef.setValue(propValue);
1508 findAndFillInputRecursively(propValue, propertyDef);
1512 @SuppressWarnings("unchecked")
1513 private void findAndFillInputRecursively(Map<String, Object> propValue, UploadPropInfo propertyDef) {
1514 for (Map.Entry<String, Object> entry : propValue.entrySet()) {
1515 String propName = entry.getKey();
1516 Object value = entry.getValue();
1517 if (value instanceof Map) {
1518 fillInputRecursively(propName, (Map<String, Object>) value, propertyDef);
1519 } else if (value instanceof List) {
1520 fillInputsRecursively(propertyDef, propName, (List<Object>) value);
1525 private void fillInputsRecursively(UploadPropInfo propertyDef, String propName, List<Object> inputs) {
1526 inputs.stream().filter(Map.class::isInstance).forEach(o -> fillInputRecursively(propName, (Map<String, Object>) o, propertyDef));
1529 @SuppressWarnings("unchecked")
1530 private void fillInput(String propName, Object getInput, GetInputValueDataDefinition getInputInfo) {
1531 List<Object> getInputList = (List<Object>) getInput;
1532 getInputInfo.setPropName(propName);
1533 getInputInfo.setInputName((String) getInputList.get(0));
1534 if (getInputList.size() > 1) {
1535 Object indexObj = getInputList.get(1);
1536 if (indexObj instanceof Integer) {
1537 getInputInfo.setIndexValue((Integer) indexObj);
1538 } else if (indexObj instanceof Float) {
1539 int index = ((Float) indexObj).intValue();
1540 getInputInfo.setIndexValue(index);
1541 } else if (indexObj instanceof Map && ((Map<String, Object>) indexObj).containsKey(GET_INPUT.getElementName())) {
1542 Object index = ((Map<String, Object>) indexObj).get(GET_INPUT.getElementName());
1543 GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition();
1544 getInputInfoIndex.setInputName((String) index);
1545 getInputInfoIndex.setPropName(propName);
1546 getInputInfo.setGetInputIndex(getInputInfoIndex);
1548 getInputInfo.setList(true);
1552 private Object failIfNotTopologyTemplate(String fileName) {
1553 janusGraphDao.rollback();
1554 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
1557 private void rollbackWithException(ActionStatus actionStatus, String... params) {
1558 janusGraphDao.rollback();
1559 throw new ByActionStatusComponentException(actionStatus, params);
1562 private void failOnMissingCapabilityTypes(GroupDefinition groupDefinition, List<String> missingCapTypes) {
1563 if (log.isDebugEnabled()) {
1565 "#failOnMissingCapabilityTypes - Failed to validate the capabilities of the group {}. The capability types {} are missing on the group type {}. ",
1566 groupDefinition.getName(), missingCapTypes.toString(), groupDefinition.getType());
1568 if (CollectionUtils.isNotEmpty(missingCapTypes)) {
1569 rollbackWithException(ActionStatus.MISSING_CAPABILITY_TYPE, missingCapTypes.toString());
1573 private void failOnMissingCapabilityNames(GroupDefinition groupDefinition, List<String> missingCapNames) {
1574 if (log.isDebugEnabled()) {
1576 "#failOnMissingCapabilityNames - Failed to validate the capabilities of the group {}. The capabilities with the names {} are missing on the group type {}. ",
1577 groupDefinition.getName(), missingCapNames.toString(), groupDefinition.getType());
1579 rollbackWithException(ActionStatus.MISSING_CAPABILITIES, missingCapNames.toString(), CapabilityDataDefinition.OwnerType.GROUP.getValue(),
1580 groupDefinition.getName());