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.model.Component;
32 import org.openecomp.sdc.be.model.InterfaceDefinition;
33 import org.openecomp.sdc.be.model.Product;
34 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
35 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
36 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
37 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
38 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
41 public class InterfacesOperationsToscaUtil {
43 private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
44 private static final String OPERATIONS_KEY = "operations";
46 private static final String DEFAULT = "default";
47 private static final String DEFAULT_HAS_UNDERSCORE = "_default";
48 private static final String DOT = ".";
49 private static final String SELF = "SELF";
50 private static final String GET_PROPERTY = "get_property";
51 private static final String DEFAULTP = "defaultp";
53 private InterfacesOperationsToscaUtil() {
57 * Creates the interface_types element
59 * @param component to work on
60 * @return the added element
62 public static Map<String, Object> addInterfaceTypeElement(Component component) {
63 if (component instanceof Product) {
66 final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
67 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
68 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
69 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
70 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
72 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
73 Map<String, Object> toscaOperations = new HashMap<>();
75 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
76 toscaOperations.put(operationEntry.getValue().getName(),
77 null); //currently not initializing any of the operations' fields as it is not needed
79 toscaInterfaceType.setOperations(toscaOperations);
80 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
81 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
82 interfacesAsMap.putAll(operationsMap);
84 toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
86 return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
90 * Adds the 'interfaces' element to the node type provided
92 * @param component to work on
93 * @param nodeType to which the interfaces element will be added
95 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
96 if (component instanceof Product) {
99 final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
100 if (MapUtils.isEmpty(interfaces)) {
103 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
104 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
105 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
106 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
107 toscaInterfaceDefinition.setType(toscaResourceName);
108 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
109 Map<String, Object> toscaOperations = new HashMap<>();
111 String operationArtifactPath;
112 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
113 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
114 if (isArtifactPresent(operationEntry)) {
115 operationArtifactPath = OperationArtifactUtil
116 .createOperationArtifactPath(component.getNormalizedName(),
117 interfaceDefinition.getToscaResourceName(),
118 operationEntry.getValue());
119 toscaOperation.setImplementation(operationArtifactPath);
121 toscaOperation.setDescription(operationEntry.getValue().getDescription());
122 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
124 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
127 toscaInterfaceDefinition.setOperations(toscaOperations);
128 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
129 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
130 handleDefaults(operationsMap);
131 interfaceDefAsMap.putAll(operationsMap);
132 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
134 if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
135 nodeType.setInterfaces(toscaInterfaceDefinitions);
140 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
141 * so, any string Map key named "defaultp" will have its named changed to "default"
142 * @param operationsMap the map to update
144 private static void handleDefaults(Map<String, Object> operationsMap) {
145 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
146 final Object value = operationEntry.getValue();
147 if (value instanceof Map) {
148 handleDefaults((Map<String, Object>) value);
150 final String key = operationEntry.getKey();
151 if (key.equals(DEFAULTP)) {
152 Object removed = operationsMap.remove(key);
153 operationsMap.put(DEFAULT, removed);
158 private static String getLastPartOfName(String toscaResourceName) {
159 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
162 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
163 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
164 if (isImplementationPresent) {
165 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
170 private static void fillToscaOperationInputs(OperationDataDefinition operation,
171 ToscaLifecycleOperationDefinition toscaOperation) {
172 if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
173 toscaOperation.setInputs(null);
176 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
178 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
179 ToscaProperty toscaInput = new ToscaProperty();
180 toscaInput.setDescription(input.getDescription());
181 String mappedPropertyName = getLastPartOfName(input.getInputId());
182 toscaInput.setType(input.getType());
183 toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
184 toscaInput.setRequired(input.isRequired());
185 toscaInputs.put(input.getName(), toscaInput);
188 toscaOperation.setInputs(toscaInputs);
191 private static Map<String, List<String>> createDefaultValue(String propertyName) {
192 Map<String, List<String>> getPropertyMap = new HashMap<>();
193 List<String> values = new ArrayList<>();
195 values.add(propertyName);
196 getPropertyMap.put(GET_PROPERTY, values);
198 return getPropertyMap;
202 private static Map<String, Object> getObjectAsMap(Object obj) {
203 ObjectMapper objectMapper = new ObjectMapper();
204 if (obj instanceof ToscaInterfaceDefinition) {
205 //Prevent empty field serialization in interface definition
206 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
208 Map<String, Object> objectAsMap =
209 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
211 if (objectAsMap.containsKey(DEFAULT)) {
212 Object defaultValue = objectAsMap.get(DEFAULT);
213 objectAsMap.remove(DEFAULT);
214 objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);