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.enums.ConstraintType;
113 import org.openecomp.sdc.be.datatypes.elements.ToscaFunctionType;
114 import org.openecomp.sdc.be.datatypes.enums.ConstraintType;
115 import org.openecomp.sdc.be.datatypes.enums.FilterValueType;
116 import org.openecomp.sdc.be.datatypes.enums.PropertyFilterTargetType;
117 import org.openecomp.sdc.be.model.CapabilityDefinition;
118 import org.openecomp.sdc.be.model.Component;
119 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
120 import org.openecomp.sdc.be.model.GroupDefinition;
121 import org.openecomp.sdc.be.model.GroupTypeDefinition;
122 import org.openecomp.sdc.be.model.InputDefinition;
123 import org.openecomp.sdc.be.model.NodeTypeInfo;
124 import org.openecomp.sdc.be.model.OutputDefinition;
125 import org.openecomp.sdc.be.model.ParsedToscaYamlInfo;
126 import org.openecomp.sdc.be.model.PolicyDefinition;
127 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
128 import org.openecomp.sdc.be.model.PropertyDefinition;
129 import org.openecomp.sdc.be.model.Resource;
130 import org.openecomp.sdc.be.model.UploadArtifactInfo;
131 import org.openecomp.sdc.be.model.UploadAttributeInfo;
132 import org.openecomp.sdc.be.model.UploadCapInfo;
133 import org.openecomp.sdc.be.model.UploadComponentInstanceInfo;
134 import org.openecomp.sdc.be.model.UploadInterfaceInfo;
135 import org.openecomp.sdc.be.model.UploadPropInfo;
136 import org.openecomp.sdc.be.model.UploadReqInfo;
137 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
138 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
139 import org.openecomp.sdc.be.tosca.utils.OperationArtifactUtil;
140 import org.openecomp.sdc.be.ui.model.OperationUi;
141 import org.openecomp.sdc.be.ui.model.PropertyAssignmentUi;
142 import org.openecomp.sdc.be.utils.PropertyFilterConstraintDataDefinitionHelper;
143 import org.openecomp.sdc.be.utils.TypeUtils;
144 import org.openecomp.sdc.common.log.wrappers.Logger;
145 import org.yaml.snakeyaml.parser.ParserException;
148 * A handler class designed to parse the YAML file of the service template for a JAVA object
150 @org.springframework.stereotype.Component
151 public class YamlTemplateParsingHandler {
153 private static final int SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX = 0;
154 private static final int SUB_MAPPING_CAPABILITY_NAME_IDX = 1;
155 private static final Logger log = Logger.getLogger(YamlTemplateParsingHandler.class);
156 private final Gson gson = new Gson();
157 private final JanusGraphDao janusGraphDao;
158 private final GroupTypeBusinessLogic groupTypeBusinessLogic;
159 private final AnnotationBusinessLogic annotationBusinessLogic;
160 private final PolicyTypeBusinessLogic policyTypeBusinessLogic;
161 private final ServiceBusinessLogic serviceBusinessLogic;
162 private final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler;
164 public YamlTemplateParsingHandler(JanusGraphDao janusGraphDao,
165 GroupTypeBusinessLogic groupTypeBusinessLogic,
166 AnnotationBusinessLogic annotationBusinessLogic,
167 PolicyTypeBusinessLogic policyTypeBusinessLogic,
168 ServiceBusinessLogic serviceBusinessLogic,
169 final ToscaFunctionYamlParsingHandler toscaFunctionYamlParsingHandler
171 this.janusGraphDao = janusGraphDao;
172 this.groupTypeBusinessLogic = groupTypeBusinessLogic;
173 this.annotationBusinessLogic = annotationBusinessLogic;
174 this.policyTypeBusinessLogic = policyTypeBusinessLogic;
175 this.serviceBusinessLogic = serviceBusinessLogic;
176 this.toscaFunctionYamlParsingHandler = toscaFunctionYamlParsingHandler;
179 public ParsedToscaYamlInfo parseResourceInfoFromYAML(String fileName, String resourceYml, Map<String, String> createdNodesToscaResourceNames,
180 Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName,
181 Component component, String interfaceTemplateYaml) {
182 log.debug("#parseResourceInfoFromYAML - Going to parse yaml {} ", fileName);
183 Map<String, Object> mappedToscaTemplate = getMappedToscaTemplate(fileName, resourceYml, nodeTypesInfo, nodeName);
184 ParsedToscaYamlInfo parsedToscaYamlInfo = new ParsedToscaYamlInfo();
185 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(mappedToscaTemplate, TOPOLOGY_TEMPLATE,
186 ToscaElementTypeEnum.ALL).left().on(err -> failIfNotTopologyTemplate(fileName));
187 final Map<String, Object> mappedTopologyTemplateInputs = mappedTopologyTemplate.entrySet().stream()
188 .filter(entry -> entry.getKey().equals(INPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
189 final Map<String, Object> mappedTopologyTemplateOutputs = mappedTopologyTemplate.entrySet().stream()
190 .filter(entry -> entry.getKey().equals(OUTPUTS.getElementName())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
191 parsedToscaYamlInfo.setInputs(getInputs(mappedTopologyTemplateInputs));
192 parsedToscaYamlInfo.setOutputs(getOutputs(mappedTopologyTemplateOutputs));
193 parsedToscaYamlInfo.setInstances(getInstances(
195 createdNodesToscaResourceNames
197 associateRelationshipTemplatesToInstances(parsedToscaYamlInfo.getInstances(), mappedTopologyTemplate);
198 parsedToscaYamlInfo.setGroups(getGroups(mappedToscaTemplate, component.getModel()));
199 parsedToscaYamlInfo.setPolicies(getPolicies(mappedToscaTemplate, component.getModel()));
200 Map<String, Object> substitutionMappings = getSubstitutionMappings(mappedToscaTemplate);
201 if (substitutionMappings != null) {
202 if (component.isService()) {
203 if (!interfaceTemplateYaml.isEmpty()) {
204 parsedToscaYamlInfo.setProperties(getProperties(loadYamlAsStrictMap(interfaceTemplateYaml)));
205 parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
207 Resource resource = serviceBusinessLogic.fetchDerivedFromGenericType(component, null);
208 List<PropertyDefinition> properties = resource.getProperties();
209 parsedToscaYamlInfo.setProperties(properties.stream().collect(Collectors.toMap(PropertyDefinition::getName, prop -> prop)));
210 parsedToscaYamlInfo.setSubstitutionFilterProperties(getSubstitutionFilterProperties(mappedToscaTemplate));
213 if (substitutionMappings.get("properties") != null) {
214 parsedToscaYamlInfo.setSubstitutionMappingProperties((Map<String, List<String>>) substitutionMappings.get("properties"));
216 parsedToscaYamlInfo.setSubstitutionMappingNodeType((String) substitutionMappings.get(NODE_TYPE.getElementName()));
218 log.debug("#parseResourceInfoFromYAML - The yaml {} has been parsed ", fileName);
219 return parsedToscaYamlInfo;
222 private Map<String, Object> getMappedToscaTemplate(String fileName, String resourceYml, Map<String, NodeTypeInfo> nodeTypesInfo,
224 Map<String, Object> mappedToscaTemplate;
225 if (isNodeExist(nodeTypesInfo, nodeName)) {
226 mappedToscaTemplate = nodeTypesInfo.get(nodeName).getMappedToscaTemplate();
228 mappedToscaTemplate = loadYaml(fileName, resourceYml);
230 return mappedToscaTemplate;
233 private Map<String, Object> loadYaml(String fileName, String resourceYml) {
234 Map<String, Object> mappedToscaTemplate = null;
236 mappedToscaTemplate = loadYamlAsStrictMap(resourceYml);
237 } catch (ParserException e) {
238 log.debug("#getMappedToscaTemplate - Failed to load YAML file {}", fileName, e);
239 rollbackWithException(ActionStatus.TOSCA_PARSE_ERROR, fileName, e.getMessage());
241 return mappedToscaTemplate;
244 private boolean isNodeExist(Map<String, NodeTypeInfo> nodeTypesInfo, String nodeName) {
245 return nodeTypesInfo != null && nodeName != null && nodeTypesInfo.containsKey(nodeName);
248 private Map<String, InputDefinition> getInputs(Map<String, Object> toscaJson) {
249 Map<String, InputDefinition> inputs = ImportUtils.getInputs(toscaJson, annotationBusinessLogic.getAnnotationTypeOperations()).left()
250 .on(err -> new HashMap<>());
251 annotationBusinessLogic.validateAndMergeAnnotationsAndAssignToInput(inputs);
255 private Map<String, OutputDefinition> getOutputs(Map<String, Object> toscaJson) {
256 return ImportUtils.getOutputs(toscaJson).left().on(err -> new HashMap<>());
259 private Map<String, PropertyDefinition> getProperties(Map<String, Object> toscaJson) {
260 return ImportUtils.getProperties(toscaJson).left().on(err -> new HashMap<>());
263 private ListDataDefinition<SubstitutionFilterPropertyDataDefinition> getSubstitutionFilterProperties(Map<String, Object> toscaJson) {
264 ListDataDefinition<SubstitutionFilterPropertyDataDefinition> propertyList = new ListDataDefinition<>();
265 Map<String, Object> substitutionFilters = findFirstToscaMapElement(toscaJson, SUBSTITUTION_FILTERS).left().on(err -> new HashMap<>());
266 if (MapUtils.isEmpty(substitutionFilters)) {
269 ArrayList<Map<String, List<Map<String, Object>>>> substitutionFilterProperties =
270 (ArrayList<Map<String, List<Map<String, Object>>>>) substitutionFilters.get("properties");
271 if (CollectionUtils.isEmpty(substitutionFilterProperties)) {
274 for (Map<String, List<Map<String, Object>>> filterProps : substitutionFilterProperties) {
275 for (Map.Entry<String, List<Map<String, Object>>> propertyFilterEntry : filterProps.entrySet()) {
276 final String propertyName = propertyFilterEntry.getKey();
277 for (Map<String, Object> filterValueMap : propertyFilterEntry.getValue()) {
278 final var substitutionFilterPropertyDataDefinition = new SubstitutionFilterPropertyDataDefinition();
279 substitutionFilterPropertyDataDefinition.setName(propertyName);
280 substitutionFilterPropertyDataDefinition.setConstraints(createSubstitutionFilterConstraints(propertyName, filterValueMap));
281 propertyList.add(substitutionFilterPropertyDataDefinition);
288 private List<PropertyFilterConstraintDataDefinition> createSubstitutionFilterConstraints(final String name, final Map<String, Object> value) {
289 final List<PropertyFilterConstraintDataDefinition> constraints = new ArrayList<>();
290 for (final Map.Entry<String, Object> valueEntry : value.entrySet()) {
291 final var propertyFilterConstraint = new PropertyFilterConstraintDataDefinition();
292 propertyFilterConstraint.setPropertyName(name);
293 propertyFilterConstraint.setOperator(ConstraintType.findByType(valueEntry.getKey()).orElse(null));
294 propertyFilterConstraint.setTargetType(PropertyFilterTargetType.PROPERTY);
295 final Optional<ToscaFunction> toscaFunction = PropertyFilterConstraintDataDefinitionHelper
296 .createToscaFunctionFromLegacyConstraintValue(valueEntry.getValue());
297 if (toscaFunction.isPresent()) {
298 final ToscaFunction toscaFunction1 = toscaFunction.get();
299 propertyFilterConstraint.setValue(toscaFunction1);
300 propertyFilterConstraint.setValueType(
301 PropertyFilterConstraintDataDefinitionHelper.convertFromToscaFunctionType(toscaFunction1.getType()).orElse(null)
304 propertyFilterConstraint.setValue(valueEntry.getValue());
305 propertyFilterConstraint.setValueType(FilterValueType.STATIC);
307 constraints.add(propertyFilterConstraint);
313 private Map<String, PolicyDefinition> getPolicies(Map<String, Object> toscaJson, String model) {
314 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
315 .left().on(err -> new HashMap<>());
316 Map<String, Object> foundPolicies = (Map<String, Object>) mappedTopologyTemplate.get(POLICIES.getElementName());
317 if (MapUtils.isNotEmpty(foundPolicies)) {
318 return foundPolicies.entrySet().stream().map(policyToCreate -> createPolicy(policyToCreate, model))
319 .collect(Collectors.toMap(PolicyDefinition::getName, p -> p));
321 return Collections.emptyMap();
324 private PolicyDefinition createPolicy(Map.Entry<String, Object> policyNameValue, String model) {
325 PolicyDefinition emptyPolicyDef = new PolicyDefinition();
326 String policyName = policyNameValue.getKey();
327 emptyPolicyDef.setName(policyName);
329 // 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.
330 if (policyNameValue.getValue() instanceof Map) {
331 Map<String, Object> policyTemplateJsonMap = (Map<String, Object>) policyNameValue.getValue();
332 validateAndFillPolicy(emptyPolicyDef, policyTemplateJsonMap, model);
334 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
336 } catch (ClassCastException e) {
337 log.debug("#createPolicy - Failed to create the policy {}. The exception occurred", policyName, e);
338 rollbackWithException(ActionStatus.INVALID_YAML);
340 return emptyPolicyDef;
343 private void validateAndFillPolicy(PolicyDefinition emptyPolicyDefinition, Map<String, Object> policyTemplateJsonMap, String model) {
344 String policyTypeName = (String) policyTemplateJsonMap.get(TYPE.getElementName());
345 if (StringUtils.isEmpty(policyTypeName)) {
346 log.debug("#validateAndFillPolicy - The 'type' member is not found under policy {}", emptyPolicyDefinition.getName());
347 rollbackWithException(ActionStatus.POLICY_MISSING_POLICY_TYPE, emptyPolicyDefinition.getName());
349 emptyPolicyDefinition.setType(policyTypeName);
350 // set policy targets
351 emptyPolicyDefinition.setTargets(validateFillPolicyTargets(policyTemplateJsonMap));
352 PolicyTypeDefinition policyTypeDefinition = validateGetPolicyTypeDefinition(policyTypeName, model);
353 // set policy properties
354 emptyPolicyDefinition.setProperties(validateFillPolicyProperties(policyTypeDefinition, policyTemplateJsonMap));
357 private PolicyTypeDefinition validateGetPolicyTypeDefinition(String policyType, String modelName) {
358 PolicyTypeDefinition policyTypeDefinition = policyTypeBusinessLogic.getLatestPolicyTypeByType(policyType, modelName);
359 if (policyTypeDefinition == null) {
360 log.debug("#validateAndFillPolicy - The policy type {} not found", policyType);
361 rollbackWithException(ActionStatus.POLICY_TYPE_IS_INVALID, policyType);
363 return policyTypeDefinition;
366 private List<PropertyDataDefinition> validateFillPolicyProperties(final PolicyTypeDefinition policyTypeDefinition,
367 final Map<String, Object> policyTemplateJsonMap) {
368 if (policyTypeDefinition == null || CollectionUtils.isEmpty(policyTypeDefinition.getProperties())
369 || MapUtils.isEmpty(policyTemplateJsonMap)) {
370 return Collections.emptyList();
372 final Map<String, Object> propertiesJsonMap = (Map<String, Object>) policyTemplateJsonMap.get(PROPERTIES.getElementName());
373 if (MapUtils.isEmpty(propertiesJsonMap)) {
374 return Collections.emptyList();
376 return propertiesJsonMap.entrySet().stream()
377 .map(propertyJson -> {
378 final PropertyDefinition originalProperty =
379 policyTypeDefinition.getProperties().stream()
380 .filter(propertyDefinition -> propertyDefinition.getName().equals(propertyJson.getKey()))
383 if (originalProperty == null) {
386 final UploadPropInfo uploadPropInfo = buildProperty(propertyJson.getKey(), propertyJson.getValue());
387 final PropertyDefinition propertyDefinition = new PropertyDefinition(originalProperty);
388 propertyDefinition.setToscaFunction(uploadPropInfo.getToscaFunction());
389 propertyDefinition.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
390 propertyDefinition.setGetInputValues(uploadPropInfo.getGet_input());
391 propertyDefinition.setDescription(uploadPropInfo.getDescription());
392 String propertyValue = gson.toJson(uploadPropInfo.getValue());
393 if (!propertyDefinition.isToscaFunction()) {
394 propertyValue = PropertiesUtils.trimQuotes(propertyValue);
396 propertyDefinition.setValue(propertyValue);
397 return propertyDefinition;
399 .filter(Objects::nonNull)
400 .collect(Collectors.toList());
403 private Map<PolicyTargetType, List<String>> validateFillPolicyTargets(Map<String, Object> policyTemplateJson) {
404 Map<PolicyTargetType, List<String>> targets = new EnumMap<>(PolicyTargetType.class);
405 if (policyTemplateJson.containsKey(TARGETS.getElementName()) && policyTemplateJson.get(TARGETS.getElementName()) instanceof List) {
406 List<String> targetsElement = (List<String>) policyTemplateJson.get(TARGETS.getElementName());
407 targets.put(PolicyTargetType.COMPONENT_INSTANCES, targetsElement);
412 private Map<String, UploadComponentInstanceInfo> getInstances(
413 Map<String, Object> toscaJson,
414 Map<String, String> createdNodesToscaResourceNames
416 Map<String, Object> nodeTemplates = findFirstToscaMapElement(toscaJson, NODE_TEMPLATES)
417 .left().on(err -> new HashMap<>());
418 if (nodeTemplates.isEmpty()) {
419 return Collections.emptyMap();
423 createdNodesToscaResourceNames,
428 private Map<String, UploadComponentInstanceInfo> getInstances(
429 Map<String, Object> toscaJson,
430 Map<String, String> createdNodesToscaResourceNames,
431 Map<String, Object> nodeTemplates
433 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
434 return nodeTemplates.entrySet().stream()
435 .map(node -> buildModuleComponentInstanceInfo(
437 substitutionMappings,
438 createdNodesToscaResourceNames
440 .collect(Collectors.toMap(UploadComponentInstanceInfo::getName, i -> i));
443 private Map<String, Object> getSubstitutionMappings(Map<String, Object> toscaJson) {
444 Either<Map<String, Object>, ResultStatusEnum> eitherSubstitutionMappings = findFirstToscaMapElement(toscaJson, SUBSTITUTION_MAPPINGS);
445 if (eitherSubstitutionMappings.isLeft()) {
446 return eitherSubstitutionMappings.left().value();
451 private void associateRelationshipTemplatesToInstances(final Map<String, UploadComponentInstanceInfo> instances,
452 final Map<String, Object> toscaJson) {
453 if (MapUtils.isEmpty(instances)) {
456 for (UploadComponentInstanceInfo instance : instances.values()) {
457 final Map<String, List<OperationUi>> operations = new HashMap<>();
458 final Map<String, List<UploadReqInfo>> requirements = instance.getRequirements();
459 if (MapUtils.isNotEmpty(requirements)) {
460 requirements.values()
461 .forEach(requirementInfoList -> requirementInfoList.stream()
462 .filter(requirement -> StringUtils.isNotEmpty(requirement.getRelationshipTemplate()))
463 .forEach(requirement -> operations.put(requirement.getRelationshipTemplate(),
464 getOperationsFromRelationshipTemplate(toscaJson, requirement.getRelationshipTemplate()))));
466 instance.setOperations(operations);
470 private Map<String, Object> getRelationshipTemplates(final Map<String, Object> toscaJson, final String relationshipTemplate) {
471 final Either<Map<String, Object>, ResultStatusEnum> eitherRelationshipTemplates = findFirstToscaMapElement(toscaJson, RELATIONSHIP_TEMPLATES);
472 if (eitherRelationshipTemplates.isRight()) {
473 throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_NOT_FOUND);
475 final Map<String, Object> relationshipTemplateMap = eitherRelationshipTemplates.left().value();
476 final Map<String, Map<String, Object>> relationship = (Map<String, Map<String, Object>>) relationshipTemplateMap.get(relationshipTemplate);
477 if (relationship == null) {
478 throw new ByActionStatusComponentException(ActionStatus.RELATIONSHIP_TEMPLATE_DEFINITION_NOT_FOUND);
480 return relationship.get(INTERFACES.getElementName());
483 private List<ToscaInterfaceDefinition> buildToscaInterfacesFromRelationship(final Map<String, Object> interfaces) {
484 return interfaces.entrySet().stream()
486 final var toscaInterfaceDefinition = new ToscaInterfaceDefinition();
487 toscaInterfaceDefinition.setType(entry.getKey());
488 final Map<String, Object> toscaInterfaceMap = (Map<String, Object>) entry.getValue();
489 toscaInterfaceDefinition.setOperations((Map<String, Object>) toscaInterfaceMap.get(OPERATIONS.getElementName()));
490 return toscaInterfaceDefinition;
495 private Optional<Object> getImplementation(final Map<String, Object> operationToscaMap) {
496 if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(IMPLEMENTATION.getElementName())) {
497 return Optional.empty();
499 return Optional.ofNullable(operationToscaMap.get(IMPLEMENTATION.getElementName()));
502 private List<PropertyAssignmentUi> getOperationsInputs(final Map<String, Object> operationToscaMap) {
503 if (MapUtils.isEmpty(operationToscaMap) || !operationToscaMap.containsKey(INPUTS.getElementName())) {
504 return Collections.emptyList();
506 final Map<String, Object> inputsMap = (Map<String, Object>) operationToscaMap.get(INPUTS.getElementName());
507 return inputsMap.entrySet().stream().map(this::buildInputAssignment).collect(toList());
510 private PropertyAssignmentUi buildInputAssignment(final Entry<String, Object> inputAssignmentMap) {
511 var propertyAssignmentUi = new PropertyAssignmentUi();
512 propertyAssignmentUi.setName(inputAssignmentMap.getKey());
513 propertyAssignmentUi.setValue(inputAssignmentMap.getValue().toString());
514 propertyAssignmentUi.setType(STRING.getType());
515 return propertyAssignmentUi;
518 private List<OperationUi> getOperationsFromRelationshipTemplate(final Map<String, Object> toscaJson, final String relationshipTemplate) {
519 final List<OperationUi> operationUiList = new ArrayList<>();
520 final List<ToscaInterfaceDefinition> interfaces =
521 buildToscaInterfacesFromRelationship(getRelationshipTemplates(toscaJson, relationshipTemplate));
523 .filter(interfaceDefinition -> MapUtils.isNotEmpty(interfaceDefinition.getOperations()))
524 .forEach(interfaceDefinition ->
525 interfaceDefinition.getOperations()
526 .forEach((operationType, operationValue) ->
527 operationUiList.add(buildOperation(interfaceDefinition.getType(), operationType, (Map<String, Object>) operationValue))
529 return operationUiList;
532 private OperationUi buildOperation(final String interfaceType, final String operationType, final Map<String, Object> operationToscaMap) {
533 var operationUi = new OperationUi();
534 operationUi.setInterfaceType(interfaceType);
535 operationUi.setOperationType(operationType);
536 getImplementation(operationToscaMap).ifPresent(operationUi::setImplementation);
537 final List<PropertyAssignmentUi> operationsInputs = getOperationsInputs(operationToscaMap);
538 if (CollectionUtils.isNotEmpty(operationsInputs)) {
539 operationUi.setInputs(operationsInputs);
544 @SuppressWarnings("unchecked")
545 private Map<String, GroupDefinition> getGroups(Map<String, Object> toscaJson, String model) {
546 Map<String, Object> mappedTopologyTemplate = (Map<String, Object>) findToscaElement(toscaJson, TOPOLOGY_TEMPLATE, ToscaElementTypeEnum.ALL)
547 .left().on(err -> new HashMap<>());
548 Map<String, Object> foundGroups = (Map<String, Object>) mappedTopologyTemplate.get(GROUPS.getElementName());
549 if (MapUtils.isNotEmpty(foundGroups)) {
550 Map<String, GroupDefinition> groups = foundGroups.entrySet().stream().map(groupToCreate -> createGroup(groupToCreate, model))
551 .collect(Collectors.toMap(GroupDefinition::getName, g -> g));
552 Map<String, Object> substitutionMappings = getSubstitutionMappings(toscaJson);
553 if (capabilitiesSubstitutionMappingsExist(substitutionMappings)) {
554 groups.forEach((key, value) -> updateCapabilitiesNames(value,
555 getNamesToUpdate(key, (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName()))));
559 return new HashMap<>();
562 private void updateCapabilitiesNames(GroupDefinition group, Map<String, String> capabilityNames) {
563 if (MapUtils.isNotEmpty(group.getCapabilities())) {
564 group.getCapabilities().values().stream().flatMap(Collection::stream).filter(cap -> capabilityNames.containsKey(cap.getName()))
565 .forEach(cap -> cap.setName(capabilityNames.get(cap.getName())));
569 private Map<String, String> getNamesToUpdate(String name, Map<String, List<String>> pair) {
570 return pair.entrySet().stream().filter(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_OWNER_NAME_IDX).equalsIgnoreCase(name))
571 .collect(Collectors.toMap(e -> e.getValue().get(SUB_MAPPING_CAPABILITY_NAME_IDX), Map.Entry::getKey, (n1, n2) -> n1));
574 private boolean capabilitiesSubstitutionMappingsExist(Map<String, Object> substitutionMappings) {
575 return substitutionMappings != null && substitutionMappings.containsKey(CAPABILITIES.getElementName());
578 private GroupDefinition createGroup(Map.Entry<String, Object> groupNameValue, String model) {
579 GroupDefinition group = new GroupDefinition();
580 group.setName(groupNameValue.getKey());
582 if (groupNameValue.getValue() instanceof Map) {
583 Map<String, Object> groupTemplateJsonMap = (Map<String, Object>) groupNameValue.getValue();
584 validateAndFillGroup(group, groupTemplateJsonMap, model);
585 validateUpdateGroupProperties(group, groupTemplateJsonMap);
586 validateUpdateGroupCapabilities(group, groupTemplateJsonMap);
588 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
590 } catch (ClassCastException e) {
591 log.debug("#createGroup - Failed to create the group {}. The exception occurres", groupNameValue.getKey(), e);
592 rollbackWithException(ActionStatus.INVALID_YAML);
597 private Map<String, CapabilityDefinition> addCapabilities(Map<String, CapabilityDefinition> cap, Map<String, CapabilityDefinition> otherCap) {
598 cap.putAll(otherCap);
602 private Map<String, CapabilityDefinition> addCapability(CapabilityDefinition cap) {
603 Map<String, CapabilityDefinition> map = Maps.newHashMap();
604 map.put(cap.getName(), cap);
608 private void setMembers(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
609 if (groupTemplateJsonMap.containsKey(MEMBERS.getElementName())) {
610 Object members = groupTemplateJsonMap.get(MEMBERS.getElementName());
611 if (members != null) {
612 if (members instanceof List) {
613 setMembersFromList(groupInfo, (List<?>) members);
615 log.debug("The 'members' member is not of type list under group {}", groupInfo.getName());
616 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
622 private void setMembersFromList(GroupDefinition groupInfo, List<?> membersAsList) {
623 groupInfo.setMembers(membersAsList.stream().collect(Collectors.toMap(Object::toString, member -> "")));
626 @SuppressWarnings("unchecked")
627 private void validateUpdateGroupProperties(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
628 if (groupTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
629 Object propertiesElement = groupTemplateJsonMap.get(PROPERTIES.getElementName());
630 if (propertiesElement instanceof Map) {
631 mergeGroupProperties(groupInfo, (Map<String, Object>) propertiesElement);
636 private void mergeGroupProperties(final GroupDefinition groupDefinition, final Map<String, Object> parsedProperties) {
637 if (CollectionUtils.isEmpty(groupDefinition.getProperties())) {
640 validateGroupProperties(parsedProperties, groupDefinition);
641 groupDefinition.getProperties().stream()
642 .filter(property -> parsedProperties.containsKey(property.getName()))
643 .forEach(property -> mergeGroupProperty(property, parsedProperties.get(property.getName())));
646 private void mergeGroupProperty(final PropertyDataDefinition property, final Object propertyYaml) {
647 final UploadPropInfo uploadPropInfo = buildProperty(property.getName(), propertyYaml);
648 property.setToscaFunction(uploadPropInfo.getToscaFunction());
649 property.setSubPropertyToscaFunctions(uploadPropInfo.getSubPropertyToscaFunctions());
650 property.setValue(convertPropertyValue(ToscaPropertyType.isValidType(property.getType()), uploadPropInfo.getValue()));
651 property.setGetInputValues(uploadPropInfo.getGet_input());
654 private String convertPropertyValue(ToscaPropertyType type, Object value) {
655 String convertedValue = null;
657 if (type == null || value instanceof Map || value instanceof List) {
658 convertedValue = gson.toJson(value);
660 convertedValue = value.toString();
663 return convertedValue;
666 private void setDescription(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
667 if (groupTemplateJsonMap.containsKey(DESCRIPTION.getElementName())) {
668 groupInfo.setDescription((String) groupTemplateJsonMap.get(DESCRIPTION.getElementName()));
672 private void validateAndFillGroup(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap, String model) {
673 String type = (String) groupTemplateJsonMap.get(TYPE.getElementName());
674 if (StringUtils.isEmpty(type)) {
675 log.debug("#validateAndFillGroup - The 'type' member is not found under group {}", groupInfo.getName());
676 rollbackWithException(ActionStatus.GROUP_MISSING_GROUP_TYPE, groupInfo.getName());
678 groupInfo.setType(type);
679 GroupTypeDefinition groupType = groupTypeBusinessLogic.getLatestGroupTypeByType(type, model);
680 if (groupType == null) {
681 log.debug("#validateAndFillGroup - The group type {} not found", groupInfo.getName());
682 rollbackWithException(ActionStatus.GROUP_TYPE_IS_INVALID, type);
684 groupInfo.convertFromGroupProperties(groupType.getProperties());
685 groupInfo.convertCapabilityDefinitions(groupType.getCapabilities());
686 setDescription(groupInfo, groupTemplateJsonMap);
687 setMembers(groupInfo, groupTemplateJsonMap);
690 @SuppressWarnings("unchecked")
691 private void validateUpdateGroupCapabilities(GroupDefinition groupInfo, Map<String, Object> groupTemplateJsonMap) {
692 if (groupTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
693 Object capabilities = groupTemplateJsonMap.get(CAPABILITIES.getElementName());
694 if (capabilities instanceof List) {
695 validateUpdateCapabilities(groupInfo, ((List<Object>) capabilities).stream().map(o -> buildGroupCapability(groupInfo, o))
696 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
697 } else if (capabilities instanceof Map) {
698 validateUpdateCapabilities(groupInfo,
699 ((Map<String, Object>) capabilities).entrySet().stream().map(e -> buildGroupCapability(groupInfo, e))
700 .collect(Collectors.toMap(CapabilityDefinition::getType, this::addCapability, this::addCapabilities)));
702 log.debug("#setCapabilities - Failed to import the capabilities of the group {}. ", groupInfo.getName());
703 rollbackWithException(ActionStatus.INVALID_YAML);
708 private void validateUpdateCapabilities(GroupDefinition groupInfo, Map<String, Map<String, CapabilityDefinition>> capabilityInfo) {
709 validateGroupCapabilities(groupInfo, capabilityInfo);
710 groupInfo.updateCapabilitiesProperties(capabilityInfo);
713 private void validateGroupCapabilities(GroupDefinition group, Map<String, Map<String, CapabilityDefinition>> parsedCapabilities) {
714 if (MapUtils.isNotEmpty(parsedCapabilities)) {
715 if (MapUtils.isEmpty(group.getCapabilities())) {
716 failOnMissingCapabilityTypes(group, Lists.newArrayList(parsedCapabilities.keySet()));
718 List<String> missingCapTypes = parsedCapabilities.keySet().stream().filter(ct -> !group.getCapabilities().containsKey(ct))
720 if (CollectionUtils.isNotEmpty(missingCapTypes)) {
721 failOnMissingCapabilityTypes(group, missingCapTypes);
723 group.getCapabilities().entrySet().forEach(e -> validateCapabilities(group, e.getValue(), parsedCapabilities.get(e.getKey())));
727 private void validateCapabilities(GroupDefinition group, List<CapabilityDefinition> capabilities,
728 Map<String, CapabilityDefinition> parsedCapabilities) {
729 List<String> allowedCapNames = capabilities.stream().map(CapabilityDefinition::getName).distinct().collect(toList());
730 List<String> missingCapNames = parsedCapabilities.keySet().stream().filter(c -> !allowedCapNames.contains(c)).collect(toList());
731 if (CollectionUtils.isNotEmpty(missingCapNames)) {
732 failOnMissingCapabilityNames(group, missingCapNames);
734 validateCapabilitiesProperties(capabilities, parsedCapabilities);
737 private void validateCapabilitiesProperties(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> parsedCapabilities) {
738 capabilities.forEach(c -> validateCapabilityProperties(c, parsedCapabilities.get(c.getName())));
741 private void validateCapabilityProperties(CapabilityDefinition capability, CapabilityDefinition parsedCapability) {
742 if (parsedCapability != null && parsedCapability.getProperties() != null) {
743 List<String> parsedPropertiesNames = parsedCapability.getProperties().stream().map(ComponentInstanceProperty::getName).collect(toList());
744 validateProperties(capability.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
745 ActionStatus.PROPERTY_NOT_FOUND, capability.getName(), capability.getType());
749 private void validateGroupProperties(Map<String, Object> parsedProperties, GroupDefinition groupInfo) {
750 List<String> parsedPropertiesNames = new ArrayList<>(parsedProperties.keySet());
751 validateProperties(groupInfo.getProperties().stream().map(PropertyDataDefinition::getName).collect(toList()), parsedPropertiesNames,
752 ActionStatus.GROUP_PROPERTY_NOT_FOUND, groupInfo.getName(), groupInfo.getType());
755 private void validateProperties(List<String> validProperties, List<String> parsedProperties, ActionStatus actionStatus, String name,
757 if (CollectionUtils.isNotEmpty(parsedProperties)) {
758 verifyMissingProperties(actionStatus, name, type, parsedProperties.stream().filter(n -> !validProperties.contains(n)).collect(toList()));
762 private void verifyMissingProperties(ActionStatus actionStatus, String name, String type, List<String> missingProperties) {
763 if (CollectionUtils.isNotEmpty(missingProperties)) {
764 if (log.isDebugEnabled()) {
765 log.debug("#validateProperties - Failed to validate properties. The properties {} are missing on {} of the type {}. ",
766 missingProperties.toString(), name, type);
768 rollbackWithException(actionStatus, missingProperties.toString(), missingProperties.toString(), name, type);
772 @SuppressWarnings("unchecked")
773 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Object capObject) {
774 if (!(capObject instanceof Map)) {
775 log.debug("#convertToGroupCapability - Failed to import the capability {}. ", capObject);
776 rollbackWithException(ActionStatus.INVALID_YAML);
778 return buildGroupCapability(groupInfo, ((Map<String, Object>) capObject).entrySet().iterator().next());
781 @SuppressWarnings("unchecked")
782 private CapabilityDefinition buildGroupCapability(GroupDefinition groupInfo, Map.Entry<String, Object> capEntry) {
783 CapabilityDefinition capability = new CapabilityDefinition();
784 capability.setOwnerType(CapabilityDataDefinition.OwnerType.GROUP);
785 capability.setName(capEntry.getKey());
786 capability.setParentName(capEntry.getKey());
787 capability.setOwnerId(groupInfo.getName());
788 if (!(capEntry.getValue() instanceof Map)) {
789 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
790 rollbackWithException(ActionStatus.INVALID_YAML);
792 Map<String, Object> capabilityValue = (Map<String, Object>) capEntry.getValue();
793 String type = (String) capabilityValue.get(TYPE.getElementName());
794 if (StringUtils.isEmpty(type)) {
795 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. Missing capability type. ", capEntry.getKey());
796 rollbackWithException(ActionStatus.INVALID_YAML);
798 capability.setType(type);
799 if (!(capabilityValue.get(PROPERTIES.getElementName()) instanceof Map)) {
800 log.debug("#convertMapEntryToCapabilityDefinition - Failed to import the capability {}. ", capEntry.getKey());
801 rollbackWithException(ActionStatus.INVALID_YAML);
803 Map<String, Object> properties = (Map<String, Object>) capabilityValue.get(PROPERTIES.getElementName());
804 capability.setProperties(properties.entrySet().stream().map(this::convertToProperty).collect(toList()));
808 private ComponentInstanceProperty convertToProperty(Map.Entry<String, Object> e) {
809 ComponentInstanceProperty property = new ComponentInstanceProperty();
810 property.setName(e.getKey());
811 property.setValue((String) e.getValue());
815 @SuppressWarnings("unchecked")
816 private UploadComponentInstanceInfo buildModuleComponentInstanceInfo(
817 Map.Entry<String, Object> nodeTemplateJsonEntry,
818 Map<String, Object> substitutionMappings,
819 Map<String, String> createdNodesToscaResourceNames
821 UploadComponentInstanceInfo nodeTemplateInfo = new UploadComponentInstanceInfo();
822 nodeTemplateInfo.setName(nodeTemplateJsonEntry.getKey());
824 if (nodeTemplateJsonEntry.getValue() instanceof String) {
825 String nodeTemplateJsonString = (String) nodeTemplateJsonEntry.getValue();
826 nodeTemplateInfo.setType(nodeTemplateJsonString);
827 } else if (nodeTemplateJsonEntry.getValue() instanceof Map) {
828 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) nodeTemplateJsonEntry.getValue();
829 setToscaResourceType(createdNodesToscaResourceNames, nodeTemplateInfo, nodeTemplateJsonMap);
830 setRequirements(nodeTemplateInfo, nodeTemplateJsonMap);
831 setCapabilities(nodeTemplateInfo, nodeTemplateJsonMap);
832 setArtifacts(nodeTemplateInfo, nodeTemplateJsonMap);
833 updateProperties(nodeTemplateInfo, nodeTemplateJsonMap);
834 updateAttributes(nodeTemplateInfo, nodeTemplateJsonMap);
835 updateInterfaces(nodeTemplateInfo, nodeTemplateJsonMap);
836 setDirectives(nodeTemplateInfo, nodeTemplateJsonMap);
837 setNodeFilter(nodeTemplateInfo, nodeTemplateJsonMap);
838 setSubstitutions(substitutionMappings, nodeTemplateInfo);
839 setOccurrencesAndInstanceCount(nodeTemplateInfo, nodeTemplateJsonMap);
841 rollbackWithException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE);
843 } catch (ClassCastException e) {
844 BeEcompErrorManager.getInstance().logBeSystemError("Import Resource - create capability");
845 log.debug("error when creating capability, message:{}", e.getMessage(), e);
846 rollbackWithException(ActionStatus.INVALID_YAML);
848 return nodeTemplateInfo;
851 @SuppressWarnings("unchecked")
852 private void setSubstitutions(Map<String, Object> substitutionMappings, UploadComponentInstanceInfo nodeTemplateInfo) {
853 if (substitutionMappings != null) {
854 if (substitutionMappings.containsKey(CAPABILITIES.getElementName())) {
855 nodeTemplateInfo.setCapabilitiesNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
856 (Map<String, List<String>>) substitutionMappings.get(CAPABILITIES.getElementName())));
858 if (substitutionMappings.containsKey(REQUIREMENTS.getElementName())) {
859 nodeTemplateInfo.setRequirementsNamesToUpdate(getNamesToUpdate(nodeTemplateInfo.getName(),
860 (Map<String, List<String>>) substitutionMappings.get(REQUIREMENTS.getElementName())));
865 private void updateProperties(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
866 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
867 Map<String, List<UploadPropInfo>> properties =
868 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
869 if (!properties.isEmpty()) {
870 nodeTemplateInfo.setProperties(properties);
875 private void updateAttributes(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
876 if (nodeTemplateJsonMap.containsKey(ATTRIBUTES.getElementName())) {
877 Map<String, UploadAttributeInfo> attributes = buildAttributeModuleFromYaml(nodeTemplateJsonMap);
878 if (!attributes.isEmpty()) {
879 nodeTemplateInfo.setAttributes(attributes);
884 private void updateInterfaces(
885 UploadComponentInstanceInfo nodeTemplateInfo,
886 Map<String, Object> nodeTemplateJsonMap
888 if (nodeTemplateJsonMap.containsKey(INTERFACES.getElementName())) {
889 Map<String, UploadInterfaceInfo> interfaces = buildInterfacesModuleFromYaml(
892 if (!interfaces.isEmpty()) {
893 nodeTemplateInfo.setInterfaces(interfaces);
898 private void setCapabilities(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
899 if (nodeTemplateJsonMap.containsKey(CAPABILITIES.getElementName())) {
900 Map<String, List<UploadCapInfo>> eitherCapRes = createCapModuleFromYaml(nodeTemplateJsonMap);
901 if (!eitherCapRes.isEmpty()) {
902 nodeTemplateInfo.setCapabilities(eitherCapRes);
907 private void setArtifacts(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
908 if (nodeTemplateJsonMap.containsKey(ARTIFACTS.getElementName())) {
909 Map<String, Map<String, UploadArtifactInfo>> eitherArtifactsRes = createArtifactsModuleFromYaml(nodeTemplateJsonMap);
910 if (!eitherArtifactsRes.isEmpty()) {
911 nodeTemplateInfo.setArtifacts(eitherArtifactsRes);
916 private void setRequirements(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
917 if (nodeTemplateJsonMap.containsKey(REQUIREMENTS.getElementName())) {
918 Map<String, List<UploadReqInfo>> regResponse = createReqModuleFromYaml(nodeTemplateJsonMap, nodeTemplateInfo.getName());
919 if (!regResponse.isEmpty()) {
920 nodeTemplateInfo.setRequirements(regResponse);
925 private void setToscaResourceType(Map<String, String> createdNodesToscaResourceNames, UploadComponentInstanceInfo nodeTemplateInfo,
926 Map<String, Object> nodeTemplateJsonMap) {
927 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
928 String toscaResourceType = (String) nodeTemplateJsonMap.get(TYPE.getElementName());
929 if (createdNodesToscaResourceNames.containsKey(toscaResourceType)) {
930 toscaResourceType = createdNodesToscaResourceNames.get(toscaResourceType);
932 nodeTemplateInfo.setType(toscaResourceType);
936 private void setDirectives(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
937 List<String> directives = (List<String>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.DIRECTIVES.getElementName());
938 nodeTemplateInfo.setDirectives(directives);
941 private void setNodeFilter(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
942 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())) {
943 nodeTemplateInfo.setUploadNodeFilterInfo(new NodeFilterUploadCreator()
944 .createNodeFilterData(nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.NODE_FILTER.getElementName())));
948 @SuppressWarnings("unchecked")
949 private void setOccurrencesAndInstanceCount(UploadComponentInstanceInfo nodeTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
950 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName())) {
951 List<Object> occurrences = (List<Object>) nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.OCCURRENCES.getElementName());
952 nodeTemplateInfo.setMinOccurrences(occurrences.get(0).toString());
953 nodeTemplateInfo.setMaxOccurrences(occurrences.get(1).toString());
955 if (nodeTemplateJsonMap.containsKey(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName())) {
956 Object instanceCount = nodeTemplateJsonMap.get(TypeUtils.ToscaTagNamesEnum.INSTANCE_COUNT.getElementName());
957 if (instanceCount instanceof Map) {
958 String instanceCountAsString = "{get_input:" + (String)((Map)instanceCount).get("get_input") + "}";
959 nodeTemplateInfo.setInstanceCount(instanceCountAsString);
961 nodeTemplateInfo.setInstanceCount(instanceCount.toString());
966 @SuppressWarnings("unchecked")
967 private Map<String, List<UploadReqInfo>> createReqModuleFromYaml(Map<String, Object> nodeTemplateJsonMap, String nodeName) {
968 Map<String, List<UploadReqInfo>> moduleRequirements = new HashMap<>();
969 Either<List<Object>, ResultStatusEnum> requirementsListRes = findFirstToscaListElement(nodeTemplateJsonMap, REQUIREMENTS);
970 if (requirementsListRes.isLeft()) {
971 for (Object jsonReqObj : requirementsListRes.left().value()) {
972 String reqName = ((Map<String, Object>) jsonReqObj).keySet().iterator().next();
973 Object reqJson = ((Map<String, Object>) jsonReqObj).get(reqName);
974 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
977 Either<Map<String, Object>, ResultStatusEnum> requirementsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, REQUIREMENTS);
978 if (requirementsMapRes.isLeft()) {
979 for (Map.Entry<String, Object> entry : requirementsMapRes.left().value().entrySet()) {
980 String reqName = entry.getKey();
981 Object reqJson = entry.getValue();
982 addModuleNodeTemplateReq(moduleRequirements, reqJson, reqName, nodeName);
986 return moduleRequirements;
989 private void addModuleNodeTemplateReq(Map<String, List<UploadReqInfo>> moduleRequirements, Object requirementJson, String requirementName,
991 UploadReqInfo requirement = buildModuleNodeTemplateReg(requirementJson, nodeName);
992 requirement.setName(requirementName);
993 if (moduleRequirements.containsKey(requirementName)) {
994 moduleRequirements.get(requirementName).add(requirement);
996 List<UploadReqInfo> list = new ArrayList<>();
997 list.add(requirement);
998 moduleRequirements.put(requirementName, list);
1002 @SuppressWarnings("unchecked")
1003 private Map<String, Map<String, UploadArtifactInfo>> createArtifactsModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
1004 Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts = new HashMap<>();
1005 Either<List<Object>, ResultStatusEnum> artifactsListRes = findFirstToscaListElement(nodeTemplateJsonMap, ARTIFACTS);
1006 if (artifactsListRes.isLeft()) {
1007 for (Object jsonArtifactObj : artifactsListRes.left().value()) {
1008 String key = ((Map<String, Object>) jsonArtifactObj).keySet().iterator().next();
1009 Object artifactJson = ((Map<String, Object>) jsonArtifactObj).get(key);
1010 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, key);
1013 Either<Map<String, Map<String, Object>>, ResultStatusEnum> artifactsMapRes = findFirstToscaMapElement(nodeTemplateJsonMap, ARTIFACTS);
1014 if (artifactsMapRes.isLeft()) {
1015 for (Map.Entry<String, Map<String, Object>> entry : artifactsMapRes.left().value().entrySet()) {
1016 String artifactName = entry.getKey();
1017 Object artifactJson = entry.getValue();
1018 addModuleNodeTemplateArtifacts(moduleArtifacts, artifactJson, artifactName);
1022 return moduleArtifacts;
1025 private void addModuleNodeTemplateArtifacts(Map<String, Map<String, UploadArtifactInfo>> moduleArtifacts, Object artifactJson,
1026 String artifactName) {
1027 UploadArtifactInfo artifact = buildModuleNodeTemplateArtifact(artifactJson);
1028 artifact.setName(artifactName);
1029 if (moduleArtifacts.containsKey(ARTIFACTS.getElementName())) {
1030 moduleArtifacts.get(ARTIFACTS.getElementName()).put(artifactName, artifact);
1032 Map<String, UploadArtifactInfo> map = new HashMap<>();
1033 map.put(artifactName, artifact);
1034 moduleArtifacts.put(ARTIFACTS.getElementName(), map);
1038 @SuppressWarnings("unchecked")
1039 private UploadArtifactInfo buildModuleNodeTemplateArtifact(Object artifactObject) {
1040 UploadArtifactInfo artifactTemplateInfo = new UploadArtifactInfo();
1041 if (artifactObject instanceof Map) {
1042 fillArtifact(artifactTemplateInfo, (Map<String, Object>) artifactObject);
1044 return artifactTemplateInfo;
1047 private void fillArtifact(UploadArtifactInfo artifactTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1048 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1049 artifactTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1051 if (nodeTemplateJsonMap.containsKey(FILE.getElementName())) {
1052 artifactTemplateInfo.setFile((String) nodeTemplateJsonMap.get(FILE.getElementName()));
1054 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1055 Map<String, List<UploadPropInfo>> props =
1056 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1057 if (!props.isEmpty()) {
1058 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1059 artifactTemplateInfo.setProperties(properties);
1064 @SuppressWarnings("unchecked")
1065 private Map<String, List<UploadCapInfo>> createCapModuleFromYaml(Map<String, Object> nodeTemplateJsonMap) {
1066 Map<String, List<UploadCapInfo>> moduleCap = new HashMap<>();
1067 Map<String, Object> capabilities = (Map<String, Object>) nodeTemplateJsonMap.get(CAPABILITIES.getElementName());
1068 if (MapUtils.isNotEmpty(capabilities)) {
1069 for (Map.Entry<String, Object> entry : capabilities.entrySet()) {
1070 String capName = entry.getKey();
1071 Object capJson = entry.getValue();
1072 addModuleNodeTemplateCap(moduleCap, capJson, capName);
1078 private void addModuleNodeTemplateCap(Map<String, List<UploadCapInfo>> moduleCap, Object capJson, String key) {
1079 UploadCapInfo capabilityDef = buildModuleNodeTemplateCap(capJson);
1080 capabilityDef.setKey(key);
1081 if (moduleCap.containsKey(key)) {
1082 moduleCap.get(key).add(capabilityDef);
1084 List<UploadCapInfo> list = new ArrayList<>();
1085 list.add(capabilityDef);
1086 moduleCap.put(key, list);
1090 @SuppressWarnings("unchecked")
1091 private UploadCapInfo buildModuleNodeTemplateCap(Object capObject) {
1092 UploadCapInfo capTemplateInfo = new UploadCapInfo();
1093 if (capObject instanceof String) {
1094 String nodeTemplateJsonString = (String) capObject;
1095 capTemplateInfo.setNode(nodeTemplateJsonString);
1096 } else if (capObject instanceof Map) {
1097 fillCapability(capTemplateInfo, (Map<String, Object>) capObject);
1099 return capTemplateInfo;
1102 private void fillCapability(UploadCapInfo capTemplateInfo, Map<String, Object> nodeTemplateJsonMap) {
1103 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1104 capTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1106 if (nodeTemplateJsonMap.containsKey(TYPE.getElementName())) {
1107 capTemplateInfo.setType((String) nodeTemplateJsonMap.get(TYPE.getElementName()));
1109 if (nodeTemplateJsonMap.containsKey(VALID_SOURCE_TYPES.getElementName())) {
1110 Either<List<Object>, ResultStatusEnum> validSourceTypesRes = findFirstToscaListElement(nodeTemplateJsonMap, VALID_SOURCE_TYPES);
1111 if (validSourceTypesRes.isLeft()) {
1112 capTemplateInfo.setValidSourceTypes(validSourceTypesRes.left().value().stream().map(Object::toString).collect(toList()));
1115 if (nodeTemplateJsonMap.containsKey(PROPERTIES.getElementName())) {
1116 Map<String, List<UploadPropInfo>> props =
1117 buildPropModuleFromYaml((Map<String, Object>) nodeTemplateJsonMap.get(PROPERTIES.getElementName()));
1118 if (!props.isEmpty()) {
1119 List<UploadPropInfo> properties = props.values().stream().flatMap(Collection::stream).collect(toList());
1120 capTemplateInfo.setProperties(properties);
1125 @SuppressWarnings("unchecked")
1126 private UploadReqInfo buildModuleNodeTemplateReg(Object regObject, String nodeName) {
1127 UploadReqInfo regTemplateInfo = new UploadReqInfo();
1128 if (regObject instanceof String) {
1129 String nodeTemplateJsonString = (String) regObject;
1130 regTemplateInfo.setNode(nodeTemplateJsonString);
1131 } else if (regObject instanceof Map) {
1132 Map<String, Object> nodeTemplateJsonMap = (Map<String, Object>) regObject;
1133 if (nodeTemplateJsonMap.containsKey(NODE.getElementName())) {
1134 regTemplateInfo.setNode((String) nodeTemplateJsonMap.get(NODE.getElementName()));
1136 if (nodeTemplateJsonMap.containsKey(CAPABILITY.getElementName())) {
1137 regTemplateInfo.setCapabilityName((String) nodeTemplateJsonMap.get(CAPABILITY.getElementName()));
1139 if (nodeTemplateJsonMap.containsKey(RELATIONSHIP.getElementName())) {
1140 final String template = (String) nodeTemplateJsonMap.get(RELATIONSHIP.getElementName());
1141 if (StringUtils.isNotEmpty(nodeName) && template.contains(nodeName)) {
1142 regTemplateInfo.setRelationshipTemplate(template);
1146 return regTemplateInfo;
1149 private Map<String, UploadAttributeInfo> buildAttributeModuleFromYaml(
1150 Map<String, Object> nodeTemplateJsonMap) {
1151 Map<String, UploadAttributeInfo> moduleAttribute = new HashMap<>();
1152 Either<Map<String, Object>, ResultStatusEnum> toscaAttributes = findFirstToscaMapElement(nodeTemplateJsonMap, ATTRIBUTES);
1153 if (toscaAttributes.isLeft()) {
1154 Map<String, Object> jsonAttributes = toscaAttributes.left().value();
1155 for (Map.Entry<String, Object> jsonAttributeObj : jsonAttributes.entrySet()) {
1156 UploadAttributeInfo attributeDef = buildAttribute(jsonAttributeObj.getKey(), jsonAttributeObj.getValue());
1157 moduleAttribute.put(attributeDef.getName(), attributeDef);
1160 return moduleAttribute;
1163 private UploadAttributeInfo buildAttribute(String attributeName, Object attributeValue) {
1164 UploadAttributeInfo attributeDef = new UploadAttributeInfo();
1165 attributeDef.setValue(attributeValue);
1166 attributeDef.setName(attributeName);
1167 return attributeDef;
1170 private Map<String, List<UploadPropInfo>> buildPropModuleFromYaml(final Map<String, Object> propertyMap) {
1171 final Map<String, List<UploadPropInfo>> moduleProp = new HashMap<>();
1172 propertyMap.entrySet().forEach(propertyMapEntry -> addProperty(moduleProp, propertyMapEntry));
1176 private Map<String, UploadInterfaceInfo> buildInterfacesModuleFromYaml(
1177 Map<String, Object> nodeTemplateJsonMap
1179 Map<String, UploadInterfaceInfo> moduleInterfaces = new HashMap<>();
1180 Either<Map<String, Object>, ResultStatusEnum> toscaInterfaces = findFirstToscaMapElement(nodeTemplateJsonMap, INTERFACES);
1181 if (toscaInterfaces.isLeft()) {
1182 Map<String, Object> jsonInterfaces = toscaInterfaces.left().value();
1183 for (Map.Entry<String, Object> jsonInterfacesObj : jsonInterfaces.entrySet()) {
1184 addInterfaces(moduleInterfaces, jsonInterfacesObj);
1187 return moduleInterfaces;
1190 private void addProperty(Map<String, List<UploadPropInfo>> moduleProp, Map.Entry<String, Object> jsonPropObj) {
1191 UploadPropInfo propertyDef = buildProperty(jsonPropObj.getKey(), jsonPropObj.getValue());
1192 if (moduleProp.containsKey(propertyDef.getName())) {
1193 moduleProp.get(propertyDef.getName()).add(propertyDef);
1195 List<UploadPropInfo> list = new ArrayList<>();
1196 list.add(propertyDef);
1197 moduleProp.put(propertyDef.getName(), list);
1201 private void addInterfaces(Map<String, UploadInterfaceInfo> moduleInterface, Map.Entry<String, Object> jsonPropObj) {
1202 UploadInterfaceInfo interfaceInfo = buildInterface(jsonPropObj.getKey(), jsonPropObj.getValue());
1203 moduleInterface.put(jsonPropObj.getKey(), interfaceInfo);
1206 @SuppressWarnings("unchecked")
1207 private UploadPropInfo buildProperty(String propName, Object propValueObj) {
1208 final var propertyDef = new UploadPropInfo();
1209 propertyDef.setValue(propValueObj);
1210 propertyDef.setName(propName);
1211 if (propValueObj instanceof Map) {
1212 final Map<String, Object> propValueMap = (Map<String, Object>) propValueObj;
1213 if (propValueMap.containsKey(TYPE.getElementName())) {
1214 propertyDef.setType(propValueMap.get(TYPE.getElementName()).toString());
1216 if (containsGetInput(propValueObj)) {
1217 fillInputRecursively(propName, propValueMap, propertyDef);
1219 if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(propValueObj)) {
1220 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue(propValueMap).ifPresent(propertyDef::setToscaFunction);
1222 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
1223 buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1224 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1225 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1226 if (existingSubPropertyToscaFunctions == null) {
1227 propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1229 propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1233 if (propValueMap.containsKey(DESCRIPTION.getElementName())) {
1234 propertyDef.setDescription((propValueMap).get(DESCRIPTION.getElementName()).toString());
1236 if (propValueMap.containsKey(DEFAULT_VALUE.getElementName())) {
1237 propertyDef.setValue(propValueMap.get(DEFAULT_VALUE.getElementName()));
1239 if (propValueMap.containsKey(IS_PASSWORD.getElementName())) {
1240 propertyDef.setPassword(Boolean.getBoolean(propValueMap.get(IS_PASSWORD.getElementName()).toString()));
1242 propertyDef.setValue(propValueObj);
1244 } else if (propValueObj instanceof List) {
1245 fillInputsListRecursively(propertyDef, (List<Object>) propValueObj);
1246 propertyDef.setValue(propValueObj);
1251 private Collection<SubPropertyToscaFunction> buildSubPropertyToscaFunctions(final Map<String, Object> propValueMap, final List<String> path) {
1252 Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = new ArrayList<>();
1253 propValueMap.entrySet().stream().filter(entry -> entry.getValue() instanceof Map).forEach(entry -> {
1254 List<String> subPropertyPath = new ArrayList<>(path);
1255 subPropertyPath.add(entry.getKey());
1256 if (ToscaFunctionType.findType(((Map<String, Object>) entry.getValue()).keySet().iterator().next()).isPresent()) {
1257 Optional<ToscaFunction> toscaFunction =
1258 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map) entry.getValue());
1259 if (toscaFunction.isPresent()) {
1260 SubPropertyToscaFunction subPropertyToscaFunction = new SubPropertyToscaFunction();
1261 subPropertyToscaFunction.setToscaFunction(toscaFunction.get());
1262 subPropertyToscaFunction.setSubPropertyPath(subPropertyPath);
1263 subPropertyToscaFunctions.add(subPropertyToscaFunction);
1266 subPropertyToscaFunctions.addAll(buildSubPropertyToscaFunctions((Map<String, Object>) entry.getValue(), subPropertyPath));
1269 return subPropertyToscaFunctions;
1272 private UploadInterfaceInfo buildInterface(String interfaceName, Object interfaceValue) {
1273 UploadInterfaceInfo interfaceDef = new UploadInterfaceInfo();
1274 interfaceDef.setValue(interfaceValue);
1275 interfaceDef.setName(interfaceName);
1276 interfaceDef.setKey(interfaceName);
1277 Map<String, OperationDataDefinition> operations = new HashMap<>();
1278 if (interfaceValue instanceof Map) {
1279 Map<String, Object> operationsMap = (Map<String, Object>) interfaceValue;
1280 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
1281 OperationDataDefinition operationDef = new OperationDataDefinition();
1282 operationDef.setName(operationEntry.getKey());
1283 Map<String, Object> operationValue = (Map<String, Object>) operationEntry.getValue();
1284 if (operationValue.containsKey(DESCRIPTION.getElementName())) {
1285 operationDef.setDescription(operationValue.get(DESCRIPTION.getElementName()).toString());
1287 operationDef.setImplementation(handleOperationImplementation(operationValue).orElse(new ArtifactDataDefinition()));
1288 if (operationValue.containsKey(INPUTS.getElementName())) {
1289 final Map<String, Object> interfaceInputs = (Map<String, Object>) operationValue.get(INPUTS.getElementName());
1290 operationDef.setInputs(handleInterfaceOperationInputs(interfaceInputs));
1292 operations.put(operationEntry.getKey(), operationDef);
1294 interfaceDef.setOperations(operations);
1295 if (operationsMap.containsKey(TYPE.getElementName())) {
1296 interfaceDef.setType(((Map<String, Object>) interfaceValue).get(TYPE.getElementName()).toString());
1299 return interfaceDef;
1302 private ListDataDefinition<OperationInputDefinition> handleInterfaceOperationInputs(final Map<String, Object> interfaceInputs) {
1303 final ListDataDefinition<OperationInputDefinition> inputs = new ListDataDefinition<>();
1304 for (final Entry<String, Object> interfaceInput : interfaceInputs.entrySet()) {
1305 final OperationInputDefinition operationInput = new OperationInputDefinition();
1306 operationInput.setUniqueId(UUID.randomUUID().toString());
1307 operationInput.setInputId(operationInput.getUniqueId());
1308 operationInput.setName(interfaceInput.getKey());
1310 handleInputToscaDefinition(interfaceInput.getKey(), interfaceInput.getValue(), operationInput);
1311 inputs.add(operationInput);
1316 private void handleInputToscaDefinition(
1317 final String inputName,
1319 final OperationInputDefinition operationInput
1321 if (value instanceof Map) {
1322 final Map<String, Object> valueMap = (Map<String, Object>) value;
1323 log.debug("Creating interface operation input '{}'", inputName);
1324 Type type = new TypeToken<LinkedHashMap<String, Object>>() {
1326 String stringValue = gson.toJson(value, type);
1327 if (toscaFunctionYamlParsingHandler.isPropertyValueToscaFunction(value)) {
1328 toscaFunctionYamlParsingHandler.buildToscaFunctionBasedOnPropertyValue((Map<String, Object>) value)
1329 .ifPresent(operationInput::setToscaFunction);
1331 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions = buildSubPropertyToscaFunctions(valueMap, new ArrayList<>());
1332 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1333 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = operationInput.getSubPropertyToscaFunctions();
1334 if (existingSubPropertyToscaFunctions == null) {
1335 operationInput.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1337 operationInput.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1341 operationInput.setValue(stringValue);
1343 if (value instanceof String) {
1344 final String stringValue = (String) value;
1345 operationInput.setDefaultValue(stringValue);
1346 operationInput.setToscaDefaultValue(stringValue);
1347 operationInput.setValue(stringValue);
1349 operationInput.setType("string");
1350 if (operationInput.getValue() == null) {
1351 operationInput.setValue(String.valueOf(value));
1355 private Optional<ArtifactDataDefinition> handleOperationImplementation(
1356 final Map<String, Object> operationDefinitionMap
1358 if (!operationDefinitionMap.containsKey(IMPLEMENTATION.getElementName())) {
1359 return Optional.empty();
1361 final ArtifactDataDefinition artifactDataDefinition = new ArtifactDataDefinition();
1362 if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof Map &&
1363 ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).containsKey("primary")) {
1365 final Object primary = ((Map) operationDefinitionMap.get(IMPLEMENTATION.getElementName())).get("primary");
1366 if (primary instanceof Map) {
1367 Map<String, Object> implDetails = (Map) primary;
1369 if (implDetails.get("file") != null) {
1370 final String file = implDetails.get("file").toString();
1371 artifactDataDefinition.setArtifactName(generateArtifactName(file));
1373 if (implDetails.get("type") != null) {
1374 artifactDataDefinition.setArtifactType(implDetails.get("type").toString());
1376 if (implDetails.get("artifact_version") != null) {
1377 artifactDataDefinition.setArtifactVersion(implDetails.get("artifact_version").toString());
1380 if (implDetails.get("properties") instanceof Map) {
1381 Map<String, Object> properties = (Map<String, Object>) implDetails.get("properties");
1382 properties.forEach((k, v) -> {
1383 ToscaPropertyType type = getTypeFromObject(v);
1385 PropertyDataDefinition propertyDef = new PropertyDataDefinition();
1386 propertyDef.setName(k);
1387 propertyDef.setValue(v.toString());
1388 artifactDataDefinition.addProperty(propertyDef);
1393 artifactDataDefinition.setArtifactName(generateArtifactName(primary.toString()));
1396 if (operationDefinitionMap.get(IMPLEMENTATION.getElementName()) instanceof String) {
1397 final String implementation = (String) operationDefinitionMap.get(IMPLEMENTATION.getElementName());
1398 artifactDataDefinition.setArtifactName(generateArtifactName(implementation));
1400 return Optional.of(artifactDataDefinition);
1403 private String generateArtifactName(final String name) {
1404 if (OperationArtifactUtil.artifactNameIsALiteralValue(name)) {
1407 return QUOTE + name + QUOTE;
1411 private ToscaPropertyType getTypeFromObject(final Object value) {
1412 if (value instanceof String) {
1413 return ToscaPropertyType.STRING;
1415 if (value instanceof Integer) {
1416 return ToscaPropertyType.INTEGER;
1418 if (value instanceof Boolean) {
1419 return ToscaPropertyType.BOOLEAN;
1421 if (value instanceof Float || value instanceof Double) {
1422 return ToscaPropertyType.FLOAT;
1427 @SuppressWarnings("unchecked")
1428 private boolean containsGetInput(Object propValue) {
1429 return ((Map<String, Object>) propValue).containsKey(GET_INPUT.getElementName()) || ImportUtils.containsGetInput(propValue);
1432 @SuppressWarnings("unchecked")
1433 private void fillInputsListRecursively(UploadPropInfo propertyDef, List<Object> propValueList) {
1435 for (Object objValue : propValueList) {
1436 if (objValue instanceof Map) {
1437 Map<String, Object> objMap = (Map<String, Object>) objValue;
1438 Map<String, Object> propValueMap = new HashMap<String, Object>();
1439 propValueMap.put(String.valueOf(index), objValue);
1440 final Collection<SubPropertyToscaFunction> subPropertyToscaFunctions =
1441 buildSubPropertyToscaFunctions(propValueMap, new ArrayList<>());
1442 if (CollectionUtils.isNotEmpty(subPropertyToscaFunctions)) {
1443 Collection<SubPropertyToscaFunction> existingSubPropertyToscaFunctions = propertyDef.getSubPropertyToscaFunctions();
1444 if (existingSubPropertyToscaFunctions == null) {
1445 propertyDef.setSubPropertyToscaFunctions(subPropertyToscaFunctions);
1447 propertyDef.getSubPropertyToscaFunctions().addAll(subPropertyToscaFunctions);
1450 if (objMap.containsKey(GET_INPUT.getElementName())) {
1451 fillInputRecursively(propertyDef.getName(), objMap, propertyDef);
1453 Set<String> keys = objMap.keySet();
1454 findAndFillInputsListRecursively(propertyDef, objMap, keys);
1456 } else if (objValue instanceof List) {
1457 List<Object> propSubValueList = (List<Object>) objValue;
1458 fillInputsListRecursively(propertyDef, propSubValueList);
1464 @SuppressWarnings("unchecked")
1465 private void findAndFillInputsListRecursively(UploadPropInfo propertyDef, Map<String, Object> objMap, Set<String> keys) {
1466 for (String key : keys) {
1467 Object value = objMap.get(key);
1468 if (value instanceof Map) {
1469 fillInputRecursively(key, (Map<String, Object>) value, propertyDef);
1470 } else if (value instanceof List) {
1471 List<Object> propSubValueList = (List<Object>) value;
1472 fillInputsListRecursively(propertyDef, propSubValueList);
1477 private void fillInputRecursively(String propName, Map<String, Object> propValue, UploadPropInfo propertyDef) {
1478 if (propValue.containsKey(GET_INPUT.getElementName())) {
1479 Object getInput = propValue.get(GET_INPUT.getElementName());
1480 GetInputValueDataDefinition getInputInfo = new GetInputValueDataDefinition();
1481 List<GetInputValueDataDefinition> getInputs = propertyDef.getGet_input();
1482 if (getInputs == null) {
1483 getInputs = new ArrayList<>();
1485 if (getInput instanceof String) {
1486 getInputInfo.setInputName((String) getInput);
1487 getInputInfo.setPropName(propName);
1488 } else if (getInput instanceof List) {
1489 fillInput(propName, getInput, getInputInfo);
1491 getInputs.add(getInputInfo);
1492 propertyDef.setGet_input(getInputs);
1493 propertyDef.setValue(propValue);
1495 findAndFillInputRecursively(propValue, propertyDef);
1499 @SuppressWarnings("unchecked")
1500 private void findAndFillInputRecursively(Map<String, Object> propValue, UploadPropInfo propertyDef) {
1501 for (Map.Entry<String, Object> entry : propValue.entrySet()) {
1502 String propName = entry.getKey();
1503 Object value = entry.getValue();
1504 if (value instanceof Map) {
1505 fillInputRecursively(propName, (Map<String, Object>) value, propertyDef);
1506 } else if (value instanceof List) {
1507 fillInputsRecursively(propertyDef, propName, (List<Object>) value);
1512 private void fillInputsRecursively(UploadPropInfo propertyDef, String propName, List<Object> inputs) {
1513 inputs.stream().filter(Map.class::isInstance).forEach(o -> fillInputRecursively(propName, (Map<String, Object>) o, propertyDef));
1516 @SuppressWarnings("unchecked")
1517 private void fillInput(String propName, Object getInput, GetInputValueDataDefinition getInputInfo) {
1518 List<Object> getInputList = (List<Object>) getInput;
1519 getInputInfo.setPropName(propName);
1520 getInputInfo.setInputName((String) getInputList.get(0));
1521 if (getInputList.size() > 1) {
1522 Object indexObj = getInputList.get(1);
1523 if (indexObj instanceof Integer) {
1524 getInputInfo.setIndexValue((Integer) indexObj);
1525 } else if (indexObj instanceof Float) {
1526 int index = ((Float) indexObj).intValue();
1527 getInputInfo.setIndexValue(index);
1528 } else if (indexObj instanceof Map && ((Map<String, Object>) indexObj).containsKey(GET_INPUT.getElementName())) {
1529 Object index = ((Map<String, Object>) indexObj).get(GET_INPUT.getElementName());
1530 GetInputValueDataDefinition getInputInfoIndex = new GetInputValueDataDefinition();
1531 getInputInfoIndex.setInputName((String) index);
1532 getInputInfoIndex.setPropName(propName);
1533 getInputInfo.setGetInputIndex(getInputInfoIndex);
1535 getInputInfo.setList(true);
1539 private Object failIfNotTopologyTemplate(String fileName) {
1540 janusGraphDao.rollback();
1541 throw new ByActionStatusComponentException(ActionStatus.NOT_TOPOLOGY_TOSCA_TEMPLATE, fileName);
1544 private void rollbackWithException(ActionStatus actionStatus, String... params) {
1545 janusGraphDao.rollback();
1546 throw new ByActionStatusComponentException(actionStatus, params);
1549 private void failOnMissingCapabilityTypes(GroupDefinition groupDefinition, List<String> missingCapTypes) {
1550 if (log.isDebugEnabled()) {
1552 "#failOnMissingCapabilityTypes - Failed to validate the capabilities of the group {}. The capability types {} are missing on the group type {}. ",
1553 groupDefinition.getName(), missingCapTypes.toString(), groupDefinition.getType());
1555 if (CollectionUtils.isNotEmpty(missingCapTypes)) {
1556 rollbackWithException(ActionStatus.MISSING_CAPABILITY_TYPE, missingCapTypes.toString());
1560 private void failOnMissingCapabilityNames(GroupDefinition groupDefinition, List<String> missingCapNames) {
1561 if (log.isDebugEnabled()) {
1563 "#failOnMissingCapabilityNames - Failed to validate the capabilities of the group {}. The capabilities with the names {} are missing on the group type {}. ",
1564 groupDefinition.getName(), missingCapNames.toString(), groupDefinition.getType());
1566 rollbackWithException(ActionStatus.MISSING_CAPABILITIES, missingCapNames.toString(), CapabilityDataDefinition.OwnerType.GROUP.getValue(),
1567 groupDefinition.getName());