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.tosca.utils;
19 import com.fasterxml.jackson.annotation.JsonInclude;
20 import com.fasterxml.jackson.databind.ObjectMapper;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Objects;
29 import org.apache.commons.collections.MapUtils;
30 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.InterfaceDefinition;
34 import org.openecomp.sdc.be.model.Product;
35 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
36 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
37 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
38 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
39 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
42 public class InterfacesOperationsToscaUtil {
44 private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
45 private static final String OPERATIONS_KEY = "operations";
47 private static final String DEFAULT = "default";
48 private static final String DEFAULT_HAS_UNDERSCORE = "_default";
49 private static final String DOT = ".";
50 private static final String SELF = "SELF";
51 private static final String GET_PROPERTY = "get_property";
52 private static final String DEFAULTP = "defaultp";
54 private InterfacesOperationsToscaUtil() {
58 * Creates the interface_types element
60 * @param component to work on
61 * @return the added element
63 public static Map<String, Object> addInterfaceTypeElement(Component component, List<String> allInterfaceTypes) {
64 if (component instanceof Product) {
67 final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
68 if (MapUtils.isEmpty(interfaces)) {
72 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
73 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
74 boolean isInterfaceTypeExistInGlobalType = allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType()));
75 if(!isInterfaceTypeExistInGlobalType){
76 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
77 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
79 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
80 Map<String, Object> toscaOperations = new HashMap<>();
82 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
83 toscaOperations.put(operationEntry.getValue().getName(),
84 null); //currently not initializing any of the operations' fields as it is not needed
86 toscaInterfaceType.setOperations(toscaOperations);
87 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
88 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
89 interfacesAsMap.putAll(operationsMap);
91 toscaInterfaceTypes.put(interfaceDefinition.getType(), interfacesAsMap);
94 return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
98 * Adds the 'interfaces' element to the node type provided
100 * @param component to work on
101 * @param nodeType to which the interfaces element will be added
103 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType,
104 boolean isAssociatedResourceComponent) {
105 if (component instanceof Product) {
108 final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
109 if (MapUtils.isEmpty(interfaces)) {
112 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
113 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
114 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
115 final String interfaceType = interfaceDefinition.getType();
116 toscaInterfaceDefinition.setType(interfaceType);
117 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
118 Map<String, Object> toscaOperations = new HashMap<>();
120 String operationArtifactPath;
121 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
122 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
123 if (isArtifactPresent(operationEntry)) {
124 operationArtifactPath = OperationArtifactUtil
125 .createOperationArtifactPath(component, operationEntry.getValue(),
126 isAssociatedResourceComponent);
127 toscaOperation.setImplementation(operationArtifactPath);
129 toscaOperation.setDescription(operationEntry.getValue().getDescription());
130 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
132 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
135 toscaInterfaceDefinition.setOperations(toscaOperations);
136 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
137 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
138 handleDefaults(operationsMap);
139 interfaceDefAsMap.putAll(operationsMap);
140 toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap);
142 if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
143 nodeType.setInterfaces(toscaInterfaceDefinitions);
148 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
149 * so, any string Map key named "defaultp" will have its named changed to "default"
150 * @param operationsMap the map to update
152 private static void handleDefaults(Map<String, Object> operationsMap) {
153 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
154 final Object value = operationEntry.getValue();
155 if (value instanceof Map) {
156 handleDefaults((Map<String, Object>) value);
158 final String key = operationEntry.getKey();
159 if (key.equals(DEFAULTP)) {
160 Object removed = operationsMap.remove(key);
161 operationsMap.put(DEFAULT, removed);
166 private static String getLastPartOfName(String toscaResourceName) {
167 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
170 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
171 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
172 if (isImplementationPresent) {
173 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
178 private static void fillToscaOperationInputs(OperationDataDefinition operation,
179 ToscaLifecycleOperationDefinition toscaOperation) {
180 if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
181 toscaOperation.setInputs(null);
184 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
186 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
187 ToscaProperty toscaInput = new ToscaProperty();
188 toscaInput.setDescription(input.getDescription());
189 String mappedPropertyName = null;
190 if (Objects.nonNull(input.getInputId())) {
191 mappedPropertyName = input.getInputId().substring(input.getInputId().indexOf(DOT) + 1);
193 toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
195 toscaInput.setType(input.getType());
196 toscaInput.setRequired(input.isRequired());
197 toscaInputs.put(input.getName(), toscaInput);
200 toscaOperation.setInputs(toscaInputs);
203 private static Map<String, List<String>> createDefaultValue(String propertyName) {
204 Map<String, List<String>> getPropertyMap = new HashMap<>();
205 List<String> values = new ArrayList<>();
207 if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
208 values.addAll(Arrays.asList(propertyName.split("\\.")));
211 getPropertyMap.put(GET_PROPERTY, values);
213 return getPropertyMap;
216 private static Map<String, Object> getObjectAsMap(Object obj) {
217 ObjectMapper objectMapper = new ObjectMapper();
218 if (obj instanceof ToscaInterfaceDefinition) {
219 //Prevent empty field serialization in interface definition
220 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
222 Map<String, Object> objectAsMap =
223 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
225 if (objectAsMap.containsKey(DEFAULT)) {
226 Object defaultValue = objectAsMap.get(DEFAULT);
227 objectAsMap.remove(DEFAULT);
228 objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);