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 java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
23 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;
38 import com.fasterxml.jackson.databind.ObjectMapper;
42 * @since March 20, 2018
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 = "_default";
52 private static final String DOT = ".";
53 private static final String DEFAULT_INPUT_TYPE = "string";
54 private static final String SELF = "SELF";
55 private static final String GET_PROPERTY = "get_property";
56 public static final String DEFAULTP = "defaultp";
59 * Creates the interface_types element
61 * @param component to work on
62 * @return the added element
64 public static Map<String, Object> addInterfaceTypeElement(Component component) {
65 Map<String, Object> toscaInterfaceTypes = new HashMap<>();
66 if ((component instanceof Service) || (component instanceof Product)) {
70 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
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 (String operationId : operations.keySet()) {
79 toscaOperations.put(operations.get(operationId).getName(),
80 null); //currently not initializing any of the operations' fields as it is not needed
84 toscaInterfaceType.setOperations(toscaOperations);
85 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
86 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
87 interfacesAsMap.putAll(operationsMap);
89 toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
91 return toscaInterfaceTypes;
95 * Adds the 'interfaces' element to the node type provided
97 * @param component to work on
98 * @param nodeType to which the interfaces element will be added
100 public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
101 Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
103 if ((component instanceof Service) || (component instanceof Product)) {
107 final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
108 if (Objects.isNull(interfaces)) {
111 for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
112 ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
113 final String toscaResourceName = interfaceDefinition.getToscaResourceName();
114 toscaInterfaceDefinition.setType(toscaResourceName);
115 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
116 Map<String, Object> toscaOperations = new HashMap<>();
118 String operationArtifactPath;
119 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
120 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
121 if (isArtifactPresent(operationEntry)) {
122 operationArtifactPath = OperationArtifactUtil
123 .createOperationArtifactPath(component.getNormalizedName(),
124 interfaceDefinition.getToscaResourceName(),
125 operationEntry.getValue());
126 toscaOperation.setImplementation(operationArtifactPath);
128 toscaOperation.setDescription(operationEntry.getValue().getDescription());
129 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
131 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
134 toscaInterfaceDefinition.setOperations(toscaOperations);
135 Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
136 Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
137 handleDefaults(operationsMap);
138 interfaceDefAsMap.putAll(operationsMap);
139 toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
141 nodeType.setInterfaces(toscaInterfaceDefinitions);
145 * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
146 * any string key named "defaultp" will have its named changed to "default"
147 * @param operationsMap the map to update
149 private static void handleDefaults(Map<String, Object> operationsMap) {
150 for (String key : operationsMap.keySet()) {
151 Object value = operationsMap.get(key);
152 if (value instanceof Map) {
153 handleDefaults((Map<String, Object>) value);
155 if (key.equals(DEFAULTP)) {
156 Object removed = operationsMap.remove(key);
157 operationsMap.put(DEFAULT, removed);
164 private static String getLastPartOfName(String toscaResourceName) {
165 return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
169 private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
170 final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
171 if (isImplementationPresent) {
172 return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
177 private static void fillToscaOperationInputs(OperationDataDefinition operation,
178 ToscaLifecycleOperationDefinition toscaOperation) {
179 if (Objects.isNull(operation.getInputs())) {
182 Map<String, ToscaProperty> toscaInputs = new HashMap<>();
184 for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
185 ToscaProperty toscaInput = new ToscaProperty();
186 toscaInput.setDescription(input.getDescription());
187 toscaInput.setType(DEFAULT_INPUT_TYPE);
189 toscaInput.setDefaultp(createDefaultValue(getLastPartOfName(input.getInputId())));
190 toscaInputs.put(input.getName(), toscaInput);
193 toscaOperation.setInputs(toscaInputs);
196 private static Map<String, List<String>> createDefaultValue(String propertyName) {
197 Map<String, List<String>> getPropertyMap = new HashMap<>();
198 List<String> values = new ArrayList<>();
200 values.add(propertyName);
201 getPropertyMap.put(GET_PROPERTY, values);
203 return getPropertyMap;
207 private static Map<String, Object> getObjectAsMap(Object obj) {
208 Map<String, Object> objectAsMap =
209 obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
211 if (objectAsMap.containsKey(DEFAULT)) {
212 Object defaultValue = objectAsMap.get(DEFAULT);
213 objectAsMap.remove(DEFAULT);
214 objectAsMap.put(_DEFAULT, defaultValue);