81b5c9946004e056c909c8b5549f3d4ba950e00c
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / tosca / utils / InterfacesOperationsToscaUtil.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdc.be.tosca.utils;
18
19 import com.fasterxml.jackson.annotation.JsonInclude;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Objects;
27
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.datatypes.elements.OperationOutputDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.InterfaceDefinition;
34 import org.openecomp.sdc.be.model.Product;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.Service;
37 import org.openecomp.sdc.be.tosca.model.ToscaAttribute;
38 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
39 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
40 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
41 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
42 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
43
44
45 public class InterfacesOperationsToscaUtil {
46
47     private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
48     private static final String OPERATIONS_KEY = "operations";
49
50     private static final String DEFAULT = "default";
51     private static final String DEFAULT_HAS_UNDERSCORE = "_default";
52     private static final String DOT = ".";
53     private static final String DEFAULT_INPUT_TYPE = "string";
54     private static final String DEFAULT_OUTPUT_TYPE = "string";
55     private static final String SELF = "SELF";
56     private static final String GET_PROPERTY = "get_property";
57     private static final String GET_OPERATION_OUTPUT = "get_operation_output";
58     private static final String DEFAULTP = "defaultp";
59
60     private InterfacesOperationsToscaUtil() {
61     }
62
63     /**
64      * Creates the interface_types element
65      *
66      * @param component to work on
67      * @return the added element
68      */
69     public static Map<String, Object> addInterfaceTypeElement(Component component) {
70         Map<String, Object> toscaInterfaceTypes = new HashMap<>();
71         if ((component instanceof Service) || (component instanceof Product)) {
72             return null;
73         }
74
75         final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
76         if (MapUtils.isEmpty(interfaces)) {
77             return null;
78         }
79
80         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
81             ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
82             toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
83
84             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
85             Map<String, Object> toscaOperations = new HashMap<>();
86
87             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
88                 toscaOperations.put(operationEntry.getValue().getName(),
89                         null); //currently not initializing any of the operations' fields as it is not needed
90             }
91
92
93             toscaInterfaceType.setOperations(toscaOperations);
94             Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
95             Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
96             interfacesAsMap.putAll(operationsMap);
97
98             toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
99         }
100         return toscaInterfaceTypes;
101     }
102
103     /**
104      * Adds the 'interfaces' element to the node type provided
105      *
106      * @param component to work on
107      * @param nodeType  to which the interfaces element will be added
108      */
109     public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
110         Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
111
112         if ((component instanceof Service) || (component instanceof Product)) {
113             return;
114         }
115
116         final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
117         if (MapUtils.isEmpty(interfaces)) {
118             return;
119         }
120         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
121             ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
122             final String toscaResourceName = interfaceDefinition.getToscaResourceName();
123             toscaInterfaceDefinition.setType(toscaResourceName);
124             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
125             Map<String, Object> toscaOperations = new HashMap<>();
126             String interfaceName = getLastPartOfName(toscaResourceName);
127             String operationArtifactPath;
128             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
129                 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
130                 if (isArtifactPresent(operationEntry)) {
131                     operationArtifactPath = OperationArtifactUtil
132                                                     .createOperationArtifactPath(component.getNormalizedName(),
133                                                             interfaceDefinition.getToscaResourceName(),
134                                                             operationEntry.getValue());
135                     toscaOperation.setImplementation(operationArtifactPath);
136                 }
137                 toscaOperation.setDescription(operationEntry.getValue().getDescription());
138                 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation, nodeType);
139                 fillToscaOperationOutputs(operationEntry.getValue(), toscaOperation, interfaceName, nodeType);
140                 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
141             }
142
143             toscaInterfaceDefinition.setOperations(toscaOperations);
144             Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
145             Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
146             handleDefaults(operationsMap);
147             interfaceDefAsMap.putAll(operationsMap);
148             toscaInterfaceDefinitions.put(getLastPartOfName(toscaResourceName), interfaceDefAsMap);
149         }
150         nodeType.setInterfaces(toscaInterfaceDefinitions);
151     }
152
153     /***
154      * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
155      * so, any string Map key named "defaultp" will have its named changed to "default"
156      * @param operationsMap the map to update
157      */
158     private static void handleDefaults(Map<String, Object> operationsMap) {
159         for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
160             final Object value = operationEntry.getValue();
161             if (value instanceof Map) {
162                 handleDefaults((Map<String, Object>) value);
163             }
164             final String key = operationEntry.getKey();
165             if (key.equals(DEFAULTP)) {
166                 Object removed = operationsMap.remove(key);
167                 operationsMap.put(DEFAULT, removed);
168             }
169         }
170     }
171
172     private static String getLastPartOfName(String toscaResourceName) {
173         return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
174     }
175
176     private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
177         final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
178         if (isImplementationPresent) {
179             return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
180         }
181         return false;
182     }
183
184     private static void fillToscaOperationInputs(OperationDataDefinition operation,
185                                                  ToscaLifecycleOperationDefinition toscaOperation,
186                                                  ToscaNodeType nodeType) {
187         if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
188             toscaOperation.setInputs(null);
189             return;
190         }
191         Map<String, ToscaProperty> toscaInputs = new HashMap<>();
192
193         for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
194             ToscaProperty toscaInput = new ToscaProperty();
195             toscaInput.setDescription(input.getDescription());
196             String mappedPropertyName = getLastPartOfName(input.getInputId());
197             toscaInput.setType(getOperationInputType(mappedPropertyName, nodeType));
198             toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
199             toscaInputs.put(input.getName(), toscaInput);
200         }
201
202         toscaOperation.setInputs(toscaInputs);
203     }
204
205     private static void fillToscaOperationOutputs(OperationDataDefinition operation,
206                                                  ToscaLifecycleOperationDefinition toscaOperation,
207                                                  String interfaceName,
208                                                  ToscaNodeType nodeType) {
209         if (Objects.isNull(operation.getOutputs()) || operation.getOutputs().isEmpty()) {
210             toscaOperation.setOutputs(null);
211             return;
212         }
213         Map<String, ToscaAttribute> toscaOutputs = new HashMap<>();
214         for (OperationOutputDefinition output : operation.getOutputs().getListToscaDataDefinition()) {
215             if (Objects.nonNull(output.getInputId())) {
216                 ToscaAttribute toscaOutput = new ToscaAttribute();
217                 toscaOutput.setDescription(output.getDescription());
218                 String outputName = output.getName();
219                 String mappedAttributeName = getLastPartOfName(output.getInputId());
220                 toscaOutput.setType(getOperationOutputType(mappedAttributeName, nodeType));
221                 toscaOutputs.put(outputName, toscaOutput);
222                 createDefaultValueForMappedAttribute(nodeType, mappedAttributeName, outputName, interfaceName,
223                         operation.getName());
224             }
225         }
226         toscaOperation.setOutputs(toscaOutputs);
227     }
228
229     private static String getOperationInputType(String inputName, ToscaNodeType nodeType) {
230         if (nodeType.getProperties() != null
231                 &&  nodeType.getProperties().containsKey(inputName)) {
232             return nodeType.getProperties().get(inputName).getType();
233         }
234         return DEFAULT_INPUT_TYPE;
235     }
236
237     private static String getOperationOutputType(String inputName, ToscaNodeType nodeType) {
238         if (nodeType.getProperties() != null
239                 &&  nodeType.getProperties().containsKey(inputName)) {
240             return nodeType.getProperties().get(inputName).getType();
241         }
242         return DEFAULT_OUTPUT_TYPE;
243     }
244
245     private static Map<String, List<String>> createDefaultValue(String propertyName) {
246         Map<String, List<String>> getPropertyMap = new HashMap<>();
247         List<String> values = new ArrayList<>();
248         values.add(SELF);
249         values.add(propertyName);
250         getPropertyMap.put(GET_PROPERTY, values);
251
252         return getPropertyMap;
253     }
254
255     private static Map<String, List<String>> createAttributeDefaultValue(String outputName,
256                                                                          String interfaceName,
257                                                                          String operationName) {
258         Map<String, List<String>> attributeDefaultValue = new HashMap<>();
259         List<String> values = new ArrayList<>();
260         values.add(SELF);
261         values.add(interfaceName);
262         values.add(operationName);
263         values.add(outputName);
264         attributeDefaultValue.put(GET_OPERATION_OUTPUT, values);
265         return attributeDefaultValue;
266     }
267
268     private static void createDefaultValueForMappedAttribute(ToscaNodeType nodeType, String mappedAttributeName,
269                                                       String outputName, String interfaceName, String operationName) {
270         if (Objects.isNull(nodeType.getAttributes())) {
271             nodeType.setAttributes(new HashMap<>());
272         }
273         if (!nodeType.getAttributes().containsKey(mappedAttributeName)) {
274             ToscaAttribute toscaAttribute = new ToscaAttribute();
275             toscaAttribute.setType(getOperationOutputType(mappedAttributeName, nodeType));
276             nodeType.getAttributes().put(mappedAttributeName, toscaAttribute);
277         }
278         nodeType.getAttributes().get(mappedAttributeName)
279                 .setDefaultp(createAttributeDefaultValue(outputName, interfaceName, operationName));
280     }
281
282     private static Map<String, Object> getObjectAsMap(Object obj) {
283         ObjectMapper objectMapper = new ObjectMapper();
284         if (obj instanceof ToscaInterfaceDefinition) {
285             //Prevent empty field serialization in interface definition
286             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
287         }
288         Map<String, Object> objectAsMap =
289                 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
290
291         if (objectAsMap.containsKey(DEFAULT)) {
292             Object defaultValue = objectAsMap.get(DEFAULT);
293             objectAsMap.remove(DEFAULT);
294             objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);
295         }
296         return objectAsMap;
297     }
298 }