2 * Copyright © 2016-2018 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.components.utils;
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.be.model.tosca.ToscaFunctions;
31 import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.List;
38 import java.util.Objects;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
42 public class InterfaceOperationUtils {
44 private InterfaceOperationUtils() {
47 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceType(Component component,
48 String interfaceType) {
49 if (MapUtils.isEmpty(component.getInterfaces())) {
50 return Optional.empty();
52 return component.getInterfaces().values().stream()
53 .filter(interfaceDefinition -> interfaceDefinition.getType() != null && interfaceDefinition
55 .equals(interfaceType))
59 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceId(Component component,
61 if (MapUtils.isEmpty(component.getInterfaces())) {
62 return Optional.empty();
64 return component.getInterfaces().values().stream()
65 .filter(interfaceDefinition -> interfaceDefinition.getUniqueId() != null && interfaceDefinition
71 public static Optional<Map.Entry<String, Operation>> getOperationFromInterfaceDefinition(
72 InterfaceDefinition interfaceDefinition, String operationId) {
73 if (MapUtils.isEmpty(interfaceDefinition.getOperationsMap())) {
74 return Optional.empty();
76 return interfaceDefinition.getOperationsMap().entrySet().stream()
77 .filter(entry -> entry.getValue().getUniqueId().equals(operationId)).findAny();
80 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromOperationId(List<InterfaceDefinition> interfaces,
82 if (CollectionUtils.isEmpty(interfaces)) {
83 return Optional.empty();
85 return interfaces.stream()
86 .filter(interfaceDefinition -> interfaceDefinition.getOperationsMap().containsKey(operationId))
90 public static boolean isOperationInputMappedToComponentInput(OperationInputDefinition input,
91 List<InputDefinition> inputs) {
92 if (CollectionUtils.isEmpty(inputs)) {
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('.'))));
105 public static boolean isOperationInputMappedToOtherOperationOutput(String outputName,
106 List<OperationOutputDefinition>
107 otherOperationOutputs) {
108 if (CollectionUtils.isEmpty(otherOperationOutputs)) {
111 return otherOperationOutputs.stream()
112 .anyMatch(output -> output.getName().equals(outputName));
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("\\.")));
123 getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
124 return getPropertyMap;
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);
134 if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
135 values.addAll(Arrays.asList(propertyName.split("\\.")));
137 getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
138 return getPropertyMap;
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
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;
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)) {
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);
168 return componentOutputs;
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
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);
194 return getOperationOutputMap;
197 public static String getOperationOutputName(String fullOutputIdentifier) {
198 return fullOutputIdentifier.contains(".")
199 ? fullOutputIdentifier.substring(fullOutputIdentifier.lastIndexOf('.') + 1)
200 : fullOutputIdentifier;
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));