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