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.
16 package org.openecomp.sdc.be.components.utils;
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections4.MapUtils;
28 import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
33 import org.openecomp.sdc.be.model.Component;
34 import org.openecomp.sdc.be.model.InputDefinition;
35 import org.openecomp.sdc.be.model.InterfaceDefinition;
36 import org.openecomp.sdc.be.model.Operation;
37 import org.openecomp.sdc.be.tosca.InterfacesOperationsConverter;
38 import org.openecomp.sdc.tosca.datatypes.ToscaFunctions;
40 public class InterfaceOperationUtils {
42 private InterfaceOperationUtils() {
45 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceType(Component component, String interfaceType) {
46 if (MapUtils.isEmpty(component.getInterfaces())) {
47 return Optional.empty();
49 return component.getInterfaces().values().stream()
50 .filter(interfaceDefinition -> interfaceDefinition.getType() != null && interfaceDefinition.getType().equals(interfaceType)).findAny();
53 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceId(Component component, String interfaceId) {
54 if (MapUtils.isEmpty(component.getInterfaces())) {
55 return Optional.empty();
57 return component.getInterfaces().values().stream()
58 .filter(interfaceDefinition -> interfaceDefinition.getUniqueId() != null && interfaceDefinition.getUniqueId().equals(interfaceId))
62 public static Optional<Map.Entry<String, Operation>> getOperationFromInterfaceDefinition(InterfaceDefinition interfaceDefinition,
64 if (MapUtils.isEmpty(interfaceDefinition.getOperationsMap())) {
65 return Optional.empty();
67 return interfaceDefinition.getOperationsMap().entrySet().stream().filter(entry -> entry.getValue().getUniqueId().equals(operationId))
71 public static Optional<InterfaceDefinition> getInterfaceDefinitionFromOperationId(List<InterfaceDefinition> interfaces, String operationId) {
72 if (CollectionUtils.isEmpty(interfaces)) {
73 return Optional.empty();
75 return interfaces.stream().filter(interfaceDefinition -> interfaceDefinition.getOperationsMap().containsKey(operationId)).findAny();
78 public static boolean isOperationInputMappedToComponentInput(OperationInputDefinition input, List<InputDefinition> inputs) {
79 if (CollectionUtils.isEmpty(inputs)) {
82 boolean matchedInput = inputs.stream().anyMatch(inp -> inp.getUniqueId().equals(input.getInputId()));
83 if (!matchedInput && input.getInputId().contains(".")) {
84 return inputs.stream().anyMatch(inp -> inp.getUniqueId().equals(input.getInputId().substring(0, input.getInputId().lastIndexOf('.'))));
89 public static boolean isOperationInputMappedToOtherOperationOutput(String outputName, List<OperationOutputDefinition> otherOperationOutputs) {
90 if (CollectionUtils.isEmpty(otherOperationOutputs)) {
93 return otherOperationOutputs.stream().anyMatch(output -> output.getName().equals(outputName));
96 public static Map<String, List<String>> createMappedInputPropertyDefaultValue(String propertyName) {
97 Map<String, List<String>> getPropertyMap = new HashMap<>();
98 List<String> values = new ArrayList<>();
99 values.add(InterfacesOperationsConverter.SELF);
100 if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
101 values.addAll(Arrays.asList(propertyName.split("\\.")));
103 getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
104 return getPropertyMap;
107 public static Map<String, List<String>> createMappedCapabilityPropertyDefaultValue(String capabilityName, String propertyName) {
108 Map<String, List<String>> getPropertyMap = new HashMap<>();
109 List<String> values = new ArrayList<>();
110 values.add(InterfacesOperationsConverter.SELF);
111 values.add(capabilityName);
112 if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
113 values.addAll(Arrays.asList(propertyName.split("\\.")));
115 getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
116 return getPropertyMap;
120 * Get the list of outputs of other operations of all the interfaces in the component.
122 * @param currentOperationIdentifier Fully qualified operation name e.g. org.test.interfaces.node.lifecycle.Abc.stop
123 * @param componentInterfaces VF or service interfaces
125 public static ListDataDefinition<OperationOutputDefinition> getOtherOperationOutputsOfComponent(String currentOperationIdentifier,
126 Map<String, ? extends InterfaceDataDefinition> componentInterfaces) {
127 ListDataDefinition<OperationOutputDefinition> componentOutputs = new ListDataDefinition<>();
128 if (MapUtils.isEmpty(componentInterfaces)) {
129 return componentOutputs;
131 for (Map.Entry<String, ? extends InterfaceDataDefinition> interfaceDefinitionEntry : componentInterfaces.entrySet()) {
132 String interfaceName = interfaceDefinitionEntry.getKey();
133 final Map<String, OperationDataDefinition> operations = interfaceDefinitionEntry.getValue().getOperations();
134 if (MapUtils.isEmpty(operations)) {
137 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
138 ListDataDefinition<OperationOutputDefinition> outputs = operationEntry.getValue().getOutputs();
139 String expectedOperationIdentifier = interfaceName + "." + operationEntry.getKey();
140 if (!currentOperationIdentifier.equals(expectedOperationIdentifier) && !outputs.isEmpty()) {
141 outputs.getListToscaDataDefinition().forEach(componentOutputs::add);
145 return componentOutputs;
149 * Create the value for operation input mapped to an operation output.
151 * @param propertyName the mapped other operation output full name
152 * @return input map for tosca
154 public static Map<String, List<String>> createMappedOutputDefaultValue(String componentName, String propertyName) {
155 Map<String, List<String>> getOperationOutputMap = new HashMap<>();
156 //For operation input mapped to other operation output parameter, the mapped property value
158 // should be of the format <interface name>.<operation name>.<output parameter name>
160 // Operation name and output param name should not contain "."
161 List<String> defaultMappedOperationOutputValue = new ArrayList<>();
162 String[] tokens = propertyName.split("\\.");
163 if (tokens.length > 2) {
164 defaultMappedOperationOutputValue.add(componentName);
165 String outputPropertyName = tokens[tokens.length - 1];
166 String operationName = tokens[tokens.length - 2];
167 String mappedPropertyInterfaceType = propertyName.substring(0, propertyName.indexOf(operationName + '.' + outputPropertyName) - 1);
168 defaultMappedOperationOutputValue.addAll(Arrays.asList(mappedPropertyInterfaceType, operationName, outputPropertyName));
169 getOperationOutputMap.put(ToscaFunctions.GET_OPERATION_OUTPUT.getFunctionName(), defaultMappedOperationOutputValue);
171 return getOperationOutputMap;
174 public static String getOperationOutputName(String fullOutputIdentifier) {
175 return fullOutputIdentifier.contains(".") ? fullOutputIdentifier.substring(fullOutputIdentifier.lastIndexOf('.') + 1) : fullOutputIdentifier;
178 public static boolean isArtifactInUse(Component component, String operationId, String artifactUniqueId) {
179 return MapUtils.emptyIfNull(component.getInterfaces()).values().stream()
180 .filter(o -> MapUtils.isNotEmpty(o.getOperations()) && !o.getOperations().containsKey(operationId))
181 .flatMap(o -> o.getOperations().values().stream()).collect(Collectors.toList()).stream()
182 .anyMatch(op -> op.getImplementation().getUniqueId().equals(artifactUniqueId));