Bugfix - interface_types missing from tosca
[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.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;
39
40
41 public class InterfacesOperationsToscaUtil {
42
43     private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
44     private static final String OPERATIONS_KEY = "operations";
45
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";
52
53     private InterfacesOperationsToscaUtil() {
54     }
55
56     /**
57      * Creates the interface_types element
58      *
59      * @param component to work on
60      * @return the added element
61      */
62     public static Map<String, Object> addInterfaceTypeElement(Component component) {
63         if (component instanceof Product) {
64             return null;
65         }
66         final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
67         if (MapUtils.isEmpty(interfaces)) {
68             return null;
69         }
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);
74
75             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
76             Map<String, Object> toscaOperations = new HashMap<>();
77
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
81             }
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);
86
87             toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
88         }
89         return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
90     }
91
92     /**
93      * Adds the 'interfaces' element to the node type provided
94      *
95      * @param component to work on
96      * @param nodeType  to which the interfaces element will be added
97      */
98     public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType) {
99         if (component instanceof Product) {
100             return;
101         }
102         final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
103         if (MapUtils.isEmpty(interfaces)) {
104             return;
105         }
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<>();
113
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);
123                 }
124                 toscaOperation.setDescription(operationEntry.getValue().getDescription());
125                 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
126
127                 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
128             }
129
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);
136         }
137         if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
138             nodeType.setInterfaces(toscaInterfaceDefinitions);
139         }
140     }
141
142     /***
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
146      */
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);
152             }
153             final String key = operationEntry.getKey();
154             if (key.equals(DEFAULTP)) {
155                 Object removed = operationsMap.remove(key);
156                 operationsMap.put(DEFAULT, removed);
157             }
158         }
159     }
160
161     private static String getLastPartOfName(String toscaResourceName) {
162         return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
163     }
164
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());
169         }
170         return false;
171     }
172
173     private static void fillToscaOperationInputs(OperationDataDefinition operation,
174                                                  ToscaLifecycleOperationDefinition toscaOperation) {
175         if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
176             toscaOperation.setInputs(null);
177             return;
178         }
179         Map<String, ToscaProperty> toscaInputs = new HashMap<>();
180
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);
189         }
190
191         toscaOperation.setInputs(toscaInputs);
192     }
193
194     private static Map<String, List<String>> createDefaultValue(String propertyName) {
195         Map<String, List<String>> getPropertyMap = new HashMap<>();
196         List<String> values = new ArrayList<>();
197         values.add(SELF);
198         values.add(propertyName);
199         getPropertyMap.put(GET_PROPERTY, values);
200
201         return getPropertyMap;
202     }
203
204
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);
210         }
211         Map<String, Object> objectAsMap =
212                 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
213
214         if (objectAsMap.containsKey(DEFAULT)) {
215             Object defaultValue = objectAsMap.get(DEFAULT);
216             objectAsMap.remove(DEFAULT);
217             objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);
218         }
219         return objectAsMap;
220     }
221 }