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.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.InterfaceDefinition;
29 import org.openecomp.sdc.be.model.Product;
30 import org.openecomp.sdc.be.model.Resource;
31 import org.openecomp.sdc.be.model.Service;
32 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
33 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
34 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
35 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
36 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
40 * @since March 20, 2018
43 public class InterfacesOperationsToscaUtil {
45 private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
46 private static final String OPERATIONS_KEY = "operations";
48 private static final String DEFAULT = "default";
49 private static final String _DEFAULT = "_default";
50 private static final String DOT = ".";
51 private static final String DEFAULT_INPUT_TYPE = "string";
52 private static final String SELF = "SELF";
53 private static final String GET_PROPERTY = "get_property";
54 public static final String DEFAULTP = "defaultp";
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 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
64 if ((component instanceof Service) || (component instanceof Product)) {
65 return toscaInterfaceTypes;
68 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
69 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
70 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
71 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
73 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
74 Map<String, Object> toscaOperations = new HashMap<>();
76 for (String operationName : operations.keySet()) {
77 toscaOperations.put(operationName,
78 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 toscaInterfaceTypes;
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 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
101 if ((component instanceof Service) || (component instanceof Product)) {
105 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
106 if (Objects.isNull(interfaces)) {
109 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
110 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
111 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
112 toscaInterfaceDefinition.setType(toscaResourceName);
113 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
114 Map<String, Object> toscaOperations = new HashMap<>();
115 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
117 String operationArtifactPath;
118 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
119 if (isArtifactPresent(operationEntry)) {
120 operationArtifactPath = OperationArtifactUtil
121 .createOperationArtifactPath(component.getNormalizedName(),
122 interfaceDefinition.getToscaResourceName(),
123 operationEntry.getValue());
124 toscaOperation.setImplementation(operationArtifactPath);
126 fillToscaOperation(operationEntry.getValue(), toscaOperation);
128 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
131 toscaInterfaceDefinition.setOperations(toscaOperations);
132 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
133 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
134 handleDefaults(operationsMap);
135 interfaceDefAsMap.putAll(operationsMap);
136 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
138 nodeType.setInterfaces(toscaInterfaceDefinitions);
142 * workaround : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
143 * any string key named "default" will have its named changed to "default"
144 * @param operationsMap the map to update
146 private static void handleDefaults(Map<String, Object> operationsMap) {
147 for (String key : operationsMap.keySet()) {
148 Object value = operationsMap.get(key);
149 if (value instanceof Map) {
150 handleDefaults((Map<String, Object>) value);
152 if (key.equals(DEFAULTP)) {
153 Object removed = operationsMap.remove(key);
154 operationsMap.put(DEFAULT, removed);
161 private static String getLastPartOfName(String toscaResourceName) {
162 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
166 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
167 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
168 if (isImplementationPresent) {
169 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
174 private static void fillToscaOperation(OperationDataDefinition operation,
175 ToscaLifecycleOperationDefinition toscaOperation) {
176 if (Objects.isNull(operation.getInputs())) {
179 Map<String, ToscaProperty> inputs = new HashMap<>();
181 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
182 ToscaProperty toscaInput = new ToscaProperty();
183 toscaInput.setDescription(input.getDescription());
184 toscaInput.setType(DEFAULT_INPUT_TYPE);
186 toscaInput.setDefaultp(createDefaultValue(getLastPartOfName(input.getInputId())));
187 inputs.put(input.getName(), toscaInput);
190 toscaOperation.setInputs(inputs);
193 private static Map<String, List<String>> createDefaultValue(String propertyName) {
194 Map<String, List<String>> getPropertyMap = new HashMap<>();
195 List<String> values = new ArrayList<>();
197 values.add(propertyName);
198 getPropertyMap.put(GET_PROPERTY, values);
200 return getPropertyMap;
204 private static Map<String, Object> getObjectAsMap(Object obj) {
205 Map<String, Object> objectAsMap =
206 obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
208 if (objectAsMap.containsKey(DEFAULT)) {
209 Object defaultValue = objectAsMap.get(DEFAULT);
210 objectAsMap.remove(DEFAULT);
211 objectAsMap.put(_DEFAULT, defaultValue);