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 if (MapUtils.isEmpty(interfaces)) {
70 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
71 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
72 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
73 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
75 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
76 Map<String, Object> toscaOperations = new HashMap<>();
78 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
79 toscaOperations.put(operationEntry.getValue().getName(),
80 null); //currently not initializing any of the operations' fields as it is not needed
82 toscaInterfaceType.setOperations(toscaOperations);
83 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
84 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
85 interfacesAsMap.putAll(operationsMap);
87 toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
89 return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
93 * Adds the 'interfaces' element to the node type provided
95 * @param component to work on
96 * @param nodeType to which the interfaces element will be added
98 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
99 if (component instanceof Product) {
102 final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
103 if (MapUtils.isEmpty(interfaces)) {
106 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
107 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
108 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
109 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
110 toscaInterfaceDefinition.setType(toscaResourceName);
111 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
112 Map<String, Object> toscaOperations = new HashMap<>();
114 String operationArtifactPath;
115 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
116 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
117 if (isArtifactPresent(operationEntry)) {
118 operationArtifactPath = OperationArtifactUtil
119 .createOperationArtifactPath(component.getNormalizedName(),
120 interfaceDefinition.getToscaResourceName(),
121 operationEntry.getValue());
122 toscaOperation.setImplementation(operationArtifactPath);
124 toscaOperation.setDescription(operationEntry.getValue().getDescription());
125 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
127 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
130 toscaInterfaceDefinition.setOperations(toscaOperations);
131 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
132 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
133 handleDefaults(operationsMap);
134 interfaceDefAsMap.putAll(operationsMap);
135 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
137 if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
138 nodeType.setInterfaces(toscaInterfaceDefinitions);
143 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
144 * so, any string Map key named "defaultp" will have its named changed to "default"
145 * @param operationsMap the map to update
147 private static void handleDefaults(Map<String, Object> operationsMap) {
148 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
149 final Object value = operationEntry.getValue();
150 if (value instanceof Map) {
151 handleDefaults((Map<String, Object>) value);
153 final String key = operationEntry.getKey();
154 if (key.equals(DEFAULTP)) {
155 Object removed = operationsMap.remove(key);
156 operationsMap.put(DEFAULT, removed);
161 private static String getLastPartOfName(String toscaResourceName) {
162 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
165 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
166 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
167 if (isImplementationPresent) {
168 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
173 private static void fillToscaOperationInputs(OperationDataDefinition operation,
174 ToscaLifecycleOperationDefinition toscaOperation) {
175 if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
176 toscaOperation.setInputs(null);
179 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
181 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
182 ToscaProperty toscaInput = new ToscaProperty();
183 toscaInput.setDescription(input.getDescription());
184 String mappedPropertyName = getLastPartOfName(input.getInputId());
185 toscaInput.setType(input.getType());
186 toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
187 toscaInput.setRequired(input.isRequired());
188 toscaInputs.put(input.getName(), toscaInput);
191 toscaOperation.setInputs(toscaInputs);
194 private static Map<String, List<String>> createDefaultValue(String propertyName) {
195 Map<String, List<String>> getPropertyMap = new HashMap<>();
196 List<String> values = new ArrayList<>();
198 values.add(propertyName);
199 getPropertyMap.put(GET_PROPERTY, values);
201 return getPropertyMap;
205 private static Map<String, Object> getObjectAsMap(Object obj) {
206 ObjectMapper objectMapper = new ObjectMapper();
207 if (obj instanceof ToscaInterfaceDefinition) {
208 //Prevent empty field serialization in interface definition
209 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
211 Map<String, Object> objectAsMap =
212 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
214 if (objectAsMap.containsKey(DEFAULT)) {
215 Object defaultValue = objectAsMap.get(DEFAULT);
216 objectAsMap.remove(DEFAULT);
217 objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);