2 * Copyright © 2016-2019 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdc.be.tosca.utils;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.MapUtils;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
29 import org.openecomp.sdc.be.model.Component;
30 import org.openecomp.sdc.be.model.DataTypeDefinition;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.tosca.ToscaFunctions;
33 import org.openecomp.sdc.be.tosca.PropertyConvertor;
34 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
36 public class ToscaExportUtils {
38 private ToscaExportUtils() {
39 //Hiding implicit default constructor
42 public static Optional<Map<String, Object>> getProxyNodeTypeInterfaces(Component proxyComponent,
43 Map<String, DataTypeDefinition> dataTypes) {
44 if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) {
45 return Optional.empty();
47 return Optional.ofNullable(InterfacesOperationsToscaUtil
48 .getInterfacesMap(proxyComponent, null, proxyComponent.getInterfaces(), dataTypes, false, false));
51 public static Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent,
52 Map<String, DataTypeDefinition>
54 if (Objects.isNull(proxyComponent)) {
55 return Optional.empty();
57 Map<String, ToscaProperty> proxyProperties = new HashMap<>();
58 addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties);
59 if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) {
60 proxyProperties.putAll(proxyComponent.getProperties().stream()
61 .collect(Collectors.toMap(PropertyDataDefinition::getName,
62 property -> PropertyConvertor.getInstance().convertProperty(dataTypes, property,
63 PropertyConvertor.PropertyType.PROPERTY))));
65 resolvePropertyDefaultValueFromInput(proxyComponent.getInputs(), proxyProperties, dataTypes);
67 return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty();
71 public static void resolvePropertyDefaultValueFromInput(List<InputDefinition> componentInputs,
72 Map<String, ToscaProperty> mergedProperties,
73 Map<String, DataTypeDefinition> dataTypes) {
74 if (MapUtils.isEmpty(mergedProperties) || CollectionUtils.isEmpty(componentInputs)) {
77 for (Map.Entry<String, ToscaProperty> mergedPropertyEntry : mergedProperties.entrySet()) {
78 ToscaProperty value = mergedPropertyEntry.getValue();
79 if (Objects.nonNull(value) && value.getDefaultp() instanceof Map) {
80 Map<String, String> valueAsMap = (Map<String, String>) value.getDefaultp();
81 String inputName = valueAsMap.get(ToscaFunctions.GET_INPUT.getFunctionName());
82 Optional<InputDefinition> matchedInputDefinition = componentInputs.stream()
83 .filter(componentInput -> componentInput.getName().equals(inputName))
85 if (matchedInputDefinition.isPresent()) {
86 InputDefinition matchedInput = matchedInputDefinition.get();
87 Object resolvedDefaultValue = new PropertyConvertor().convertToToscaObject(matchedInput.getType(),
88 matchedInput.getDefaultValue(), matchedInput.getSchemaType(), dataTypes, false);
89 value.setDefaultp(resolvedDefaultValue);
90 mergedProperties.put(mergedPropertyEntry.getKey(), value);
96 public static void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
97 List<InputDefinition> componentInputs,
98 Map<String, ToscaProperty> mergedProperties) {
99 if (CollectionUtils.isEmpty(componentInputs)) {
102 for(InputDefinition input : componentInputs) {
103 ToscaProperty property = new PropertyConvertor().convertProperty(dataTypes, input,
104 PropertyConvertor.PropertyType.INPUT);
105 mergedProperties.put(input.getName(), property);