Catalog alignment
[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 org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections.MapUtils;
21 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
23 import org.openecomp.sdc.be.model.Component;
24 import org.openecomp.sdc.be.model.DataTypeDefinition;
25 import org.openecomp.sdc.be.model.InputDefinition;
26 import org.openecomp.sdc.be.model.InterfaceDefinition;
27 import org.openecomp.sdc.be.tosca.PropertyConvertor;
28 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
29
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Objects;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36
37 import static org.openecomp.sdc.be.components.utils.PropertiesUtils.resolvePropertyValueFromInput;
38
39 public class ToscaExportUtils {
40
41     private ToscaExportUtils() {
42         //Hiding implicit default constructor
43     }
44
45     public static Optional<Map<String, Object>> getProxyNodeTypeInterfaces(Component proxyComponent,
46                                                                            Map<String, DataTypeDefinition> dataTypes) {
47         if (Objects.isNull(proxyComponent) || MapUtils.isEmpty(proxyComponent.getInterfaces())) {
48             return Optional.empty();
49         }
50         Map<String, InterfaceDefinition> proxyComponentInterfaces = proxyComponent.getInterfaces();
51         //Unset artifact path for operation implementation for proxy node types as for operations with artifacts it is
52         // always available in the proxy node template
53         removeOperationImplementationForProxyNodeType(proxyComponentInterfaces);
54         return Optional.ofNullable(InterfacesOperationsToscaUtil
55                 .getInterfacesMap(proxyComponent, null, proxyComponentInterfaces, dataTypes, false, false));
56     }
57
58     public static Optional<Map<String, ToscaProperty>> getProxyNodeTypeProperties(Component proxyComponent,
59                                                                                   Map<String, DataTypeDefinition>
60                                                                                           dataTypes) {
61         if (Objects.isNull(proxyComponent)) {
62             return Optional.empty();
63         }
64         Map<String, ToscaProperty> proxyProperties = new HashMap<>();
65         addInputsToProperties(dataTypes, proxyComponent.getInputs(), proxyProperties);
66         if (CollectionUtils.isNotEmpty(proxyComponent.getProperties())) {
67             proxyProperties.putAll(proxyComponent.getProperties().stream()
68                     .map(propertyDefinition -> resolvePropertyValueFromInput(propertyDefinition,
69                             proxyComponent.getInputs()))
70                     .collect(Collectors.toMap(PropertyDataDefinition::getName,
71                             property -> PropertyConvertor.getInstance().convertProperty(dataTypes, property,
72                                     PropertyConvertor.PropertyType.PROPERTY))));
73         }
74         return MapUtils.isNotEmpty(proxyProperties) ? Optional.of(proxyProperties) : Optional.empty();
75     }
76
77     public static void addInputsToProperties(Map<String, DataTypeDefinition> dataTypes,
78                                        List<InputDefinition> componentInputs,
79                                        Map<String, ToscaProperty> mergedProperties) {
80         if (CollectionUtils.isEmpty(componentInputs)) {
81             return;
82         }
83         for(InputDefinition input : componentInputs) {
84             ToscaProperty property = new PropertyConvertor().convertProperty(dataTypes, input,
85                     PropertyConvertor.PropertyType.INPUT);
86             mergedProperties.put(input.getName(), property);
87         }
88     }
89
90     private static void removeOperationImplementationForProxyNodeType(Map<String, InterfaceDefinition>
91                                                                           proxyComponentInterfaces) {
92         if (MapUtils.isEmpty(proxyComponentInterfaces)) {
93             return;
94         }
95         proxyComponentInterfaces.values().stream()
96                 .map(InterfaceDataDefinition::getOperations)
97                 .filter(MapUtils::isNotEmpty)
98                 .forEach(operations -> operations.values()
99                         .forEach(operation -> operation.setImplementation(null)));
100     }
101 }