Fix service proxy node type
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / utils / ToscaExportUtils.java
1 /*
2  * Copyright © 2016-2019 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.tosca.utils;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Optional;
24 import java.util.stream.Collectors;
25
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;
35
36 public class ToscaExportUtils {
37
38     private ToscaExportUtils() {
39         //Hiding implicit default constructor
40     }
41
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();
46         }
47         return Optional.ofNullable(InterfacesOperationsToscaUtil
48                 .getInterfacesMap(proxyComponent, null, proxyComponent.getInterfaces(), dataTypes, false, false));
49     }
50
51     public static Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent,
52                                                                                   Map<String, DataTypeDefinition>
53                                                                                           dataTypes) {
54         if (Objects.isNull(proxyComponent)) {
55             return Optional.empty();
56         }
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))));
64         }
65         resolvePropertyDefaultValueFromInput(proxyComponent.getInputs(), proxyProperties, dataTypes);
66
67         return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty();
68     }
69
70
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)) {
75             return;
76         }
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))
84                         .findFirst();
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);
91                 }
92             }
93         }
94     }
95
96     public static void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
97                                        List<InputDefinition> componentInputs,
98                                        Map<String, ToscaProperty> mergedProperties) {
99         if (CollectionUtils.isEmpty(componentInputs)) {
100             return;
101         }
102         for(InputDefinition input : componentInputs) {
103             ToscaProperty property = new PropertyConvertor().convertProperty(dataTypes, input,
104                     PropertyConvertor.PropertyType.INPUT);
105             mergedProperties.put(input.getName(), property);
106         }
107     }
108
109 }