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.databind.ObjectMapper;
20 import java.util.ArrayList;
21 import java.util.HashMap;
22 import java.util.List;
24 import java.util.Objects;
25 import org.apache.commons.collections.MapUtils;
26 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
27 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
28 import org.openecomp.sdc.be.model.Component;
29 import org.openecomp.sdc.be.model.InterfaceDefinition;
30 import org.openecomp.sdc.be.model.Product;
31 import org.openecomp.sdc.be.model.Resource;
32 import org.openecomp.sdc.be.model.Service;
33 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
34 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
35 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
36 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
37 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
41 * @since March 20, 2018
44 public class InterfacesOperationsToscaUtil {
46 private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
47 private static final String OPERATIONS_KEY = "operations";
49 private static final String DEFAULT = "default";
50 private static final String DEFAULT_HAS_UNDERSCORE = "_default";
51 private static final String DOT = ".";
52 private static final String DEFAULT_INPUT_TYPE = "string";
53 private static final String SELF = "SELF";
54 private static final String GET_PROPERTY = "get_property";
55 private static final String DEFAULTP = "defaultp";
57 private InterfacesOperationsToscaUtil() {
61 * Creates the interface_types element
63 * @param component to work on
64 * @return the added element
66 public static Map<String, Object> addInterfaceTypeElement(Component component) {
67 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
68 if ((component instanceof Service) || (component instanceof Product)) {
72 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
73 if (MapUtils.isEmpty(interfaces)) {
77 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
78 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
79 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
81 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
82 Map<String, Object> toscaOperations = new HashMap<>();
84 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
85 toscaOperations.put(operationEntry.getValue().getName(),
86 null); //currently not initializing any of the operations' fields as it is not needed
90 toscaInterfaceType.setOperations(toscaOperations);
91 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
92 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
93 interfacesAsMap.putAll(operationsMap);
95 toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
97 return toscaInterfaceTypes;
101 * Adds the 'interfaces' element to the node type provided
103 * @param component to work on
104 * @param nodeType to which the interfaces element will be added
106 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
107 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
109 if ((component instanceof Service) || (component instanceof Product)) {
113 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
114 if (MapUtils.isEmpty(interfaces)) {
117 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
118 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
119 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
120 toscaInterfaceDefinition.setType(toscaResourceName);
121 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
122 Map<String, Object> toscaOperations = new HashMap<>();
124 String operationArtifactPath;
125 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
126 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
127 if (isArtifactPresent(operationEntry)) {
128 operationArtifactPath = OperationArtifactUtil
129 .createOperationArtifactPath(component.getNormalizedName(),
130 interfaceDefinition.getToscaResourceName(),
131 operationEntry.getValue());
132 toscaOperation.setImplementation(operationArtifactPath);
134 toscaOperation.setDescription(operationEntry.getValue().getDescription());
135 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
137 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
140 toscaInterfaceDefinition.setOperations(toscaOperations);
141 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
142 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
143 handleDefaults(operationsMap);
144 interfaceDefAsMap.putAll(operationsMap);
145 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
147 nodeType.setInterfaces(toscaInterfaceDefinitions);
151 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
152 * so, any string Map key named "defaultp" will have its named changed to "default"
153 * @param operationsMap the map to update
155 private static void handleDefaults(Map<String, Object> operationsMap) {
156 for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
157 final Object value = operationEntry.getValue();
158 if (value instanceof Map) {
159 handleDefaults((Map<String, Object>) value);
161 final String key = operationEntry.getKey();
162 if (key.equals(DEFAULTP)) {
163 Object removed = operationsMap.remove(key);
164 operationsMap.put(DEFAULT, removed);
169 private static String getLastPartOfName(String toscaResourceName) {
170 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
173 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
174 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
175 if (isImplementationPresent) {
176 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
181 private static void fillToscaOperationInputs(OperationDataDefinition operation,
182 ToscaLifecycleOperationDefinition toscaOperation) {
183 if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
184 toscaOperation.setInputs(null);
187 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
189 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
190 ToscaProperty toscaInput = new ToscaProperty();
191 toscaInput.setDescription(input.getDescription());
192 toscaInput.setType(DEFAULT_INPUT_TYPE);
194 toscaInput.setDefaultp(createDefaultValue(getLastPartOfName(input.getInputId())));
195 toscaInputs.put(input.getName(), toscaInput);
198 toscaOperation.setInputs(toscaInputs);
201 private static Map<String, List<String>> createDefaultValue(String propertyName) {
202 Map<String, List<String>> getPropertyMap = new HashMap<>();
203 List<String> values = new ArrayList<>();
205 values.add(propertyName);
206 getPropertyMap.put(GET_PROPERTY, values);
208 return getPropertyMap;
212 private static Map<String, Object> getObjectAsMap(Object obj) {
213 Map<String, Object> objectAsMap =
214 obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
216 if (objectAsMap.containsKey(DEFAULT)) {
217 Object defaultValue = objectAsMap.get(DEFAULT);
218 objectAsMap.remove(DEFAULT);
219 objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);