edbad79e147f51da0ecd7346fe1210c27645571e
[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 package org.openecomp.sdc.be.components.utils;
17
18 import java.util.ArrayList;
19 import java.util.Arrays;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
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;
39
40 public class InterfaceOperationUtils {
41
42     private InterfaceOperationUtils() {
43     }
44
45     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceType(Component component, String interfaceType) {
46         if (MapUtils.isEmpty(component.getInterfaces())) {
47             return Optional.empty();
48         }
49         return component.getInterfaces().values().stream()
50             .filter(interfaceDefinition -> interfaceDefinition.getType() != null && interfaceDefinition.getType().equals(interfaceType)).findAny();
51     }
52
53     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromComponentByInterfaceId(Component component, String interfaceId) {
54         if (MapUtils.isEmpty(component.getInterfaces())) {
55             return Optional.empty();
56         }
57         return component.getInterfaces().values().stream()
58             .filter(interfaceDefinition -> interfaceDefinition.getUniqueId() != null && interfaceDefinition.getUniqueId().equals(interfaceId))
59             .findAny();
60     }
61
62     public static Optional<Map.Entry<String, Operation>> getOperationFromInterfaceDefinition(InterfaceDefinition interfaceDefinition,
63                                                                                              String operationId) {
64         if (MapUtils.isEmpty(interfaceDefinition.getOperationsMap())) {
65             return Optional.empty();
66         }
67         return interfaceDefinition.getOperationsMap().entrySet().stream().filter(entry -> entry.getValue().getUniqueId().equals(operationId))
68             .findAny();
69     }
70
71     public static Optional<InterfaceDefinition> getInterfaceDefinitionFromOperationId(List<InterfaceDefinition> interfaces, String operationId) {
72         if (CollectionUtils.isEmpty(interfaces)) {
73             return Optional.empty();
74         }
75         return interfaces.stream().filter(interfaceDefinition -> interfaceDefinition.getOperationsMap().containsKey(operationId)).findAny();
76     }
77
78     public static boolean isOperationInputMappedToComponentInput(OperationInputDefinition input, List<InputDefinition> inputs) {
79         if (CollectionUtils.isEmpty(inputs)) {
80             return false;
81         }
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('.'))));
85         }
86         return matchedInput;
87     }
88
89     public static boolean isOperationInputMappedToOtherOperationOutput(String outputName, List<OperationOutputDefinition> otherOperationOutputs) {
90         if (CollectionUtils.isEmpty(otherOperationOutputs)) {
91             return false;
92         }
93         return otherOperationOutputs.stream().anyMatch(output -> output.getName().equals(outputName));
94     }
95
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("\\.")));
102         }
103         getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
104         return getPropertyMap;
105     }
106
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("\\.")));
114         }
115         getPropertyMap.put(ToscaFunctions.GET_PROPERTY.getFunctionName(), values);
116         return getPropertyMap;
117     }
118
119     /**
120      * Get the list of outputs of other operations of all the interfaces in the component.
121      *
122      * @param currentOperationIdentifier Fully qualified operation name e.g. org.test.interfaces.node.lifecycle.Abc.stop
123      * @param componentInterfaces        VF or service interfaces
124      */
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;
130         }
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)) {
135                 continue;
136             }
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);
142                 }
143             }
144         }
145         return componentOutputs;
146     }
147
148     /**
149      * Create the value for operation input mapped to an operation output.
150      *
151      * @param propertyName the mapped other operation output full name
152      * @return input map for tosca
153      */
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
157
158         // should be of the format <interface name>.<operation name>.<output parameter name>
159
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);
170         }
171         return getOperationOutputMap;
172     }
173
174     public static String getOperationOutputName(String fullOutputIdentifier) {
175         return fullOutputIdentifier.contains(".") ? fullOutputIdentifier.substring(fullOutputIdentifier.lastIndexOf('.') + 1) : fullOutputIdentifier;
176     }
177
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));
183     }
184 }