Support functions in TOSCA Simple Profile in YAML
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / utils / InterfaceOperationUtils.java
1 /*
2  * Copyright © 2016-2018 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.components.utils;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
22 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
23 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
24 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
25 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
26 import org.openecomp.sdc.be.model.Component;
27 import org.openecomp.sdc.be.model.InputDefinition;
28 import org.openecomp.sdc.be.model.InterfaceDefinition;
29 import org.openecomp.sdc.be.model.Operation;
30 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
31 import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41
42 public class InterfaceOperationUtils {
43
44     private InterfaceOperationUtils() {
45     }
46
47     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceType(Component component,
48             String interfaceType) {
49         if (MapUtils.isEmpty(component.getInterfaces())) {
50             return Optional.empty();
51         }
52         return component.getInterfaces().values().stream()
53                        .filter(interfaceDefinition -> interfaceDefinition.getType() != null && interfaceDefinition
54                                                                                                        .getType()
55                                                                                                        .equals(interfaceType))
56                        .findAny();
57     }
58
59     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceId(Component component,
60             String interfaceId) {
61         if (MapUtils.isEmpty(component.getInterfaces())) {
62             return Optional.empty();
63         }
64         return component.getInterfaces().values().stream()
65                        .filter(interfaceDefinition -> interfaceDefinition.getUniqueId() != null && interfaceDefinition
66                                                                                                            .getUniqueId()
67                                                                                                            .equals(interfaceId))
68                        .findAny();
69     }
70
71     public static Optional<Map.Entry<String, Operation>> getOperationFromInterfaceDefinition(
72             InterfaceDefinition interfaceDefinition, String operationId) {
73         if (MapUtils.isEmpty(interfaceDefinition.getOperationsMap())) {
74             return Optional.empty();
75         }
76         return interfaceDefinition.getOperationsMap().entrySet().stream()
77                        .filter(entry -> entry.getValue().getUniqueId().equals(operationId)).findAny();
78     }
79
80     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromOperationId(List<InterfaceDefinition> interfaces,
81             String operationId) {
82         if (CollectionUtils.isEmpty(interfaces)) {
83             return Optional.empty();
84         }
85         return interfaces.stream()
86                        .filter(interfaceDefinition -> interfaceDefinition.getOperationsMap().containsKey(operationId))
87                        .findAny();
88     }
89
90     public static boolean isOperationInputMappedToComponentInput(OperationInputDefinition input,
91                                                                                     List<InputDefinition> inputs) {
92         if (CollectionUtils.isEmpty(inputs)) {
93             return false;
94         }
95
96         boolean matchedInput = inputs.stream().anyMatch(inp -> inp.getUniqueId().equals(input.getInputId()));
97         if (!matchedInput && input.getInputId().contains(".")) {
98             return inputs.stream()
99                     .anyMatch(inp -> inp.getUniqueId()
100                             .equals(input.getInputId().substring(0, input.getInputId().lastIndexOf('.'))));
101         }
102         return matchedInput;
103     }
104
105     public static boolean isOperationInputMappedToOtherOperationOutput(String outputName,
106                                                                        List<OperationOutputDefinition>
107                                                                                otherOperationOutputs) {
108         if (CollectionUtils.isEmpty(otherOperationOutputs)) {
109             return false;
110         }
111         return otherOperationOutputs.stream()
112                 .anyMatch(output -> output.getName().equals(outputName));
113
114     }
115
116     public static Map<String, List<String>> createMappedInputPropertyDefaultValue(String propertyName) {
117         Map<String, List<String>> getPropertyMap = new HashMap<>();
118         List<String> values = new ArrayList<>();
119         values.add(InterfacesOperationsConverter.SELF);
120         if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
121             values.addAll(Arrays.asList(propertyName.split("\\.")));
122         }
123         getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
124         return getPropertyMap;
125     }
126
127     public static Map<String, List<String>> createMappedCapabilityPropertyDefaultValue(String capabilityName,
128                                                                                        String propertyName) {
129         Map<String, List<String>> getPropertyMap = new HashMap<>();
130         List<String> values = new ArrayList<>();
131         values.add(InterfacesOperationsConverter.SELF);
132         values.add(capabilityName);
133
134         if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
135             values.addAll(Arrays.asList(propertyName.split("\\.")));
136         }
137         getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
138         return getPropertyMap;
139     }
140
141     /**
142      * Get the list of outputs of other operations of all the interfaces in the component.
143      * @param currentOperationIdentifier Fully qualified operation name e.g. org.test.interfaces.node.lifecycle.Abc.stop
144      * @param componentInterfaces VF or service interfaces
145      */
146
147     public static ListDataDefinition<OperationOutputDefinition> getOtherOperationOutputsOfComponent(
148             String currentOperationIdentifier, Map<String, ? extends InterfaceDataDefinition> componentInterfaces) {
149         ListDataDefinition<OperationOutputDefinition> componentOutputs = new ListDataDefinition<>();
150         if (MapUtils.isEmpty(componentInterfaces)) {
151             return componentOutputs;
152         }
153         for (Map.Entry<String, ? extends InterfaceDataDefinition> interfaceDefinitionEntry :
154                 componentInterfaces.entrySet()) {
155             String interfaceName = interfaceDefinitionEntry.getKey();
156             final Map<String, OperationDataDefinition> operations = interfaceDefinitionEntry.getValue().getOperations();
157             if (MapUtils.isEmpty(operations)) {
158                 continue;
159             }
160             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
161                 ListDataDefinition<OperationOutputDefinition> outputs = operationEntry.getValue().getOutputs();
162                 String expectedOperationIdentifier = interfaceName + "." + operationEntry.getKey();
163                 if (!currentOperationIdentifier.equals(expectedOperationIdentifier) && !outputs.isEmpty()) {
164                     outputs.getListToscaDataDefinition().forEach(componentOutputs::add);
165                 }
166             }
167         }
168         return componentOutputs;
169     }
170
171     /**
172      * Create the value for operation input mapped to an operation output.
173      * @param propertyName the mapped other operation output full name
174      * @return input map for tosca
175      */
176     public static Map<String, List<String>> createMappedOutputDefaultValue(String componentName, String propertyName) {
177         Map<String, List<String>> getOperationOutputMap = new HashMap<>();
178         //For operation input mapped to other operation output parameter, the mapped property value
179         // should be of the format <interface name>.<operation name>.<output parameter name>
180         // Operation name and output param name should not contain "."
181         List<String> defaultMappedOperationOutputValue = new ArrayList<>();
182         String[] tokens = propertyName.split("\\.");
183         if (tokens.length > 2) {
184             defaultMappedOperationOutputValue.add(componentName);
185             String outputPropertyName = tokens[tokens.length - 1];
186             String operationName = tokens[tokens.length - 2];
187             String mappedPropertyInterfaceType =
188                     propertyName.substring(0, propertyName.indexOf(operationName + '.' + outputPropertyName) - 1);
189             defaultMappedOperationOutputValue.addAll(Arrays.asList(mappedPropertyInterfaceType, operationName,
190                     outputPropertyName));
191             getOperationOutputMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(),
192                     defaultMappedOperationOutputValue);
193         }
194         return getOperationOutputMap;
195     }
196
197     public static String getOperationOutputName(String fullOutputIdentifier) {
198         return fullOutputIdentifier.contains(".")
199                 ? fullOutputIdentifier.substring(fullOutputIdentifier.lastIndexOf('.') + 1)
200                 : fullOutputIdentifier;
201     }
202
203     public static boolean isArtifactInUse(Component component, String operationId, String artifactUniqueId) {
204         return MapUtils.emptyIfNull(component.getInterfaces()).values().stream()
205                        .filter(o -> MapUtils.isNotEmpty(o.getOperations()) && !o.getOperations().containsKey(operationId))
206                        .flatMap(o -> o.getOperations().values().stream()).collect(Collectors.toList()).stream()
207                        .anyMatch(op -> op.getImplementation().getUniqueId().equals(artifactUniqueId));
208     }
209 }