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.HashMap;
24 import java.util.List;
26 import java.util.Objects;
28 import org.apache.commons.collections.MapUtils;
29 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.OperationOutputDefinition;
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.model.Resource;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.tosca.model.ToscaAttribute;
38 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
39 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
40 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
41 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
42 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
45 public class InterfacesOperationsToscaUtil {
47 private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
48 private static final String OPERATIONS_KEY = "operations";
50 private static final String DEFAULT = "default";
51 private static final String DEFAULT_HAS_UNDERSCORE = "_default";
52 private static final String DOT = ".";
53 private static final String DEFAULT_INPUT_TYPE = "string";
54 private static final String DEFAULT_OUTPUT_TYPE = "string";
55 private static final String SELF = "SELF";
56 private static final String GET_PROPERTY = "get_property";
57 private static final String GET_OPERATION_OUTPUT = "get_operation_output";
58 private static final String DEFAULTP = "defaultp";
60 private InterfacesOperationsToscaUtil() {
64 * Creates the interface_types element
66 * @param component to work on
67 * @return the added element
69 public static Map<String, Object> addInterfaceTypeElement(Component component) {
70 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
71 if ((component instanceof Service) || (component instanceof Product)) {
75 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
76 if (MapUtils.isEmpty(interfaces)) {
80 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
81 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
82 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
84 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
85 Map<String, Object> toscaOperations = new HashMap<>();
87 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
88 toscaOperations.put(operationEntry.getValue().getName(),
89 null); //currently not initializing any of the operations' fields as it is not needed
93 toscaInterfaceType.setOperations(toscaOperations);
94 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
95 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
96 interfacesAsMap.putAll(operationsMap);
98 toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
100 return toscaInterfaceTypes;
104 * Adds the 'interfaces' element to the node type provided
106 * @param component to work on
107 * @param nodeType to which the interfaces element will be added
109 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
110 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
112 if ((component instanceof Service) || (component instanceof Product)) {
116 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
117 if (MapUtils.isEmpty(interfaces)) {
120 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
121 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
122 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
123 toscaInterfaceDefinition.setType(toscaResourceName);
124 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
125 Map<String, Object> toscaOperations = new HashMap<>();
126 String interfaceName = getLastPartOfName(toscaResourceName);
127 String operationArtifactPath;
128 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
129 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
130 if (isArtifactPresent(operationEntry)) {
131 operationArtifactPath = OperationArtifactUtil
132 .createOperationArtifactPath(component.getNormalizedName(),
133 interfaceDefinition.getToscaResourceName(),
134 operationEntry.getValue());
135 toscaOperation.setImplementation(operationArtifactPath);
137 toscaOperation.setDescription(operationEntry.getValue().getDescription());
138 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation, nodeType);
139 fillToscaOperationOutputs(operationEntry.getValue(), toscaOperation, interfaceName, nodeType);
140 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
143 toscaInterfaceDefinition.setOperations(toscaOperations);
144 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
145 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
146 handleDefaults(operationsMap);
147 interfaceDefAsMap.putAll(operationsMap);
148 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
150 nodeType.setInterfaces(toscaInterfaceDefinitions);
154 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
155 * so, any string Map key named "defaultp" will have its named changed to "default"
156 * @param operationsMap the map to update
158 private static void handleDefaults(Map<String, Object> operationsMap) {
159 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
160 final Object value = operationEntry.getValue();
161 if (value instanceof Map) {
162 handleDefaults((Map<String, Object>) value);
164 final String key = operationEntry.getKey();
165 if (key.equals(DEFAULTP)) {
166 Object removed = operationsMap.remove(key);
167 operationsMap.put(DEFAULT, removed);
172 private static String getLastPartOfName(String toscaResourceName) {
173 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
176 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
177 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
178 if (isImplementationPresent) {
179 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
184 private static void fillToscaOperationInputs(OperationDataDefinition operation,
185 ToscaLifecycleOperationDefinition toscaOperation,
186 ToscaNodeType nodeType) {
187 if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
188 toscaOperation.setInputs(null);
191 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
193 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
194 ToscaProperty toscaInput = new ToscaProperty();
195 toscaInput.setDescription(input.getDescription());
196 String mappedPropertyName = getLastPartOfName(input.getInputId());
197 toscaInput.setType(getOperationInputType(mappedPropertyName, nodeType));
198 toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
199 toscaInputs.put(input.getName(), toscaInput);
202 toscaOperation.setInputs(toscaInputs);
205 private static void fillToscaOperationOutputs(OperationDataDefinition operation,
206 ToscaLifecycleOperationDefinition toscaOperation,
207 String interfaceName,
208 ToscaNodeType nodeType) {
209 if (Objects.isNull(operation.getOutputs()) || operation.getOutputs().isEmpty()) {
210 toscaOperation.setOutputs(null);
213 Map<String, ToscaAttribute> toscaOutputs = new HashMap<>();
214 for (OperationOutputDefinition output : operation.getOutputs().getListToscaDataDefinition()) {
215 if (Objects.nonNull(output.getInputId())) {
216 ToscaAttribute toscaOutput = new ToscaAttribute();
217 toscaOutput.setDescription(output.getDescription());
218 String outputName = output.getName();
219 String mappedAttributeName = getLastPartOfName(output.getInputId());
220 toscaOutput.setType(getOperationOutputType(mappedAttributeName, nodeType));
221 toscaOutputs.put(outputName, toscaOutput);
222 createDefaultValueForMappedAttribute(nodeType, mappedAttributeName, outputName, interfaceName,
223 operation.getName());
226 toscaOperation.setOutputs(toscaOutputs);
229 private static String getOperationInputType(String inputName, ToscaNodeType nodeType) {
230 if (nodeType.getProperties() != null
231 && nodeType.getProperties().containsKey(inputName)) {
232 return nodeType.getProperties().get(inputName).getType();
234 return DEFAULT_INPUT_TYPE;
237 private static String getOperationOutputType(String inputName, ToscaNodeType nodeType) {
238 if (nodeType.getProperties() != null
239 && nodeType.getProperties().containsKey(inputName)) {
240 return nodeType.getProperties().get(inputName).getType();
242 return DEFAULT_OUTPUT_TYPE;
245 private static Map<String, List<String>> createDefaultValue(String propertyName) {
246 Map<String, List<String>> getPropertyMap = new HashMap<>();
247 List<String> values = new ArrayList<>();
249 values.add(propertyName);
250 getPropertyMap.put(GET_PROPERTY, values);
252 return getPropertyMap;
255 private static Map<String, List<String>> createAttributeDefaultValue(String outputName,
256 String interfaceName,
257 String operationName) {
258 Map<String, List<String>> attributeDefaultValue = new HashMap<>();
259 List<String> values = new ArrayList<>();
261 values.add(interfaceName);
262 values.add(operationName);
263 values.add(outputName);
264 attributeDefaultValue.put(GET_OPERATION_OUTPUT, values);
265 return attributeDefaultValue;
268 private static void createDefaultValueForMappedAttribute(ToscaNodeType nodeType, String mappedAttributeName,
269 String outputName, String interfaceName, String operationName) {
270 if (Objects.isNull(nodeType.getAttributes())) {
271 nodeType.setAttributes(new HashMap<>());
273 if (!nodeType.getAttributes().containsKey(mappedAttributeName)) {
274 ToscaAttribute toscaAttribute = new ToscaAttribute();
275 toscaAttribute.setType(getOperationOutputType(mappedAttributeName, nodeType));
276 nodeType.getAttributes().put(mappedAttributeName, toscaAttribute);
278 nodeType.getAttributes().get(mappedAttributeName)
279 .setDefaultp(createAttributeDefaultValue(outputName, interfaceName, operationName));
282 private static Map<String, Object> getObjectAsMap(Object obj) {
283 ObjectMapper objectMapper = new ObjectMapper();
284 if (obj instanceof ToscaInterfaceDefinition) {
285 //Prevent empty field serialization in interface definition
286 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
288 Map<String, Object> objectAsMap =
289 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
291 if (objectAsMap.containsKey(DEFAULT)) {
292 Object defaultValue = objectAsMap.get(DEFAULT);
293 objectAsMap.remove(DEFAULT);
294 objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);