ef3023de6b0d21f064be66f16b78787472bfb3e3
[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.Arrays;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Objects;
28
29 import org.apache.commons.collections.MapUtils;
30 import org.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
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.tosca.model.ToscaInterfaceDefinition;
36 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
37 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
38 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
39 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
40
41
42 public class InterfacesOperationsToscaUtil {
43
44     private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
45     private static final String OPERATIONS_KEY = "operations";
46
47     private static final String DEFAULT = "default";
48     private static final String DEFAULT_HAS_UNDERSCORE = "_default";
49     private static final String DOT = ".";
50     private static final String SELF = "SELF";
51     private static final String GET_PROPERTY = "get_property";
52     private static final String DEFAULTP = "defaultp";
53
54     private InterfacesOperationsToscaUtil() {
55     }
56
57     /**
58      * Creates the interface_types element
59      *
60      * @param component to work on
61      * @return the added element
62      */
63     public static Map<String, Object> addInterfaceTypeElement(Component component, List<String> allInterfaceTypes) {
64         if (component instanceof Product) {
65             return null;
66         }
67         final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
68         if (MapUtils.isEmpty(interfaces)) {
69             return null;
70         }
71
72         Map<String, Object> toscaInterfaceTypes = new HashMap<>();
73         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
74             boolean isInterfaceTypeExistInGlobalType = allInterfaceTypes.stream().anyMatch(type -> type.equalsIgnoreCase(interfaceDefinition.getType()));
75             if(!isInterfaceTypeExistInGlobalType){
76                 ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
77                 toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
78
79                 final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
80                 Map<String, Object> toscaOperations = new HashMap<>();
81
82                 for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
83                     toscaOperations.put(operationEntry.getValue().getName(),
84                         null); //currently not initializing any of the operations' fields as it is not needed
85                 }
86                 toscaInterfaceType.setOperations(toscaOperations);
87                 Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
88                 Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
89                 interfacesAsMap.putAll(operationsMap);
90
91                 toscaInterfaceTypes.put(interfaceDefinition.getType(), interfacesAsMap);
92             }
93         }
94         return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
95     }
96
97     /**
98      * Adds the 'interfaces' element to the node type provided
99      *
100      * @param component to work on
101      * @param nodeType  to which the interfaces element will be added
102      */
103     public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType,
104                                                      boolean isAssociatedResourceComponent) {
105         if (component instanceof Product) {
106             return;
107         }
108         final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
109         if (MapUtils.isEmpty(interfaces)) {
110             return;
111         }
112         Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
113         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
114             ToscaInterfaceDefinition toscaInterfaceDefinition = new ToscaInterfaceDefinition();
115             final String interfaceType = interfaceDefinition.getType();
116             toscaInterfaceDefinition.setType(interfaceType);
117             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
118             Map<String, Object> toscaOperations = new HashMap<>();
119
120             String operationArtifactPath;
121             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
122                 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
123                 if (isArtifactPresent(operationEntry)) {
124                     operationArtifactPath = OperationArtifactUtil
125                             .createOperationArtifactPath(component, operationEntry.getValue(),
126                                     isAssociatedResourceComponent);
127                     toscaOperation.setImplementation(operationArtifactPath);
128                 }
129                 toscaOperation.setDescription(operationEntry.getValue().getDescription());
130                 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
131
132                 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
133             }
134
135             toscaInterfaceDefinition.setOperations(toscaOperations);
136             Map<String, Object> interfaceDefAsMap = getObjectAsMap(toscaInterfaceDefinition);
137             Map<String, Object> operationsMap = (Map<String, Object>) interfaceDefAsMap.remove(OPERATIONS_KEY);
138             handleDefaults(operationsMap);
139             interfaceDefAsMap.putAll(operationsMap);
140             toscaInterfaceDefinitions.put(getLastPartOfName(interfaceType), interfaceDefAsMap);
141         }
142         if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
143             nodeType.setInterfaces(toscaInterfaceDefinitions);
144         }
145     }
146
147     /***
148      * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
149      * so, any string Map key named "defaultp" will have its named changed to "default"
150      * @param operationsMap the map to update
151      */
152     private static void handleDefaults(Map<String, Object> operationsMap) {
153         for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
154             final Object value = operationEntry.getValue();
155             if (value instanceof Map) {
156                 handleDefaults((Map<String, Object>) value);
157             }
158             final String key = operationEntry.getKey();
159             if (key.equals(DEFAULTP)) {
160                 Object removed = operationsMap.remove(key);
161                 operationsMap.put(DEFAULT, removed);
162             }
163         }
164     }
165
166     private static String getLastPartOfName(String toscaResourceName) {
167         return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
168     }
169
170     private static boolean isArtifactPresent(Map.Entry<String, OperationDataDefinition> operationEntry) {
171         final boolean isImplementationPresent = !Objects.isNull(operationEntry.getValue().getImplementation());
172         if (isImplementationPresent) {
173             return !Objects.isNull(operationEntry.getValue().getImplementation().getArtifactName());
174         }
175         return false;
176     }
177
178     private static void fillToscaOperationInputs(OperationDataDefinition operation,
179                                                  ToscaLifecycleOperationDefinition toscaOperation) {
180         if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
181             toscaOperation.setInputs(null);
182             return;
183         }
184         Map<String, ToscaProperty> toscaInputs = new HashMap<>();
185
186         for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
187             ToscaProperty toscaInput = new ToscaProperty();
188             toscaInput.setDescription(input.getDescription());
189             String mappedPropertyName = null;
190             if (Objects.nonNull(input.getInputId())) {
191                 mappedPropertyName = input.getInputId().substring(input.getInputId().indexOf(DOT) + 1);
192             }
193             toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
194
195             toscaInput.setType(input.getType());
196             toscaInput.setRequired(input.isRequired());
197             toscaInputs.put(input.getName(), toscaInput);
198         }
199
200         toscaOperation.setInputs(toscaInputs);
201     }
202
203     private static Map<String, List<String>> createDefaultValue(String propertyName) {
204         Map<String, List<String>> getPropertyMap = new HashMap<>();
205         List<String> values = new ArrayList<>();
206         values.add(SELF);
207         if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
208             values.addAll(Arrays.asList(propertyName.split("\\.")));
209         }
210
211         getPropertyMap.put(GET_PROPERTY, values);
212
213         return getPropertyMap;
214     }
215
216     private static Map<String, Object> getObjectAsMap(Object obj) {
217         ObjectMapper objectMapper = new ObjectMapper();
218         if (obj instanceof ToscaInterfaceDefinition) {
219             //Prevent empty field serialization in interface definition
220             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
221         }
222         Map<String, Object> objectAsMap =
223                 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
224
225         if (objectAsMap.containsKey(DEFAULT)) {
226             Object defaultValue = objectAsMap.get(DEFAULT);
227             objectAsMap.remove(DEFAULT);
228             objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);
229         }
230         return objectAsMap;
231     }
232 }