Revert "Interface operation feature enhancements"
[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) {
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         Map<String, Object> toscaInterfaceTypes = new HashMap<>();
72         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
73             ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
74             toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
75
76             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
77             Map<String, Object> toscaOperations = new HashMap<>();
78
79             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
80                 toscaOperations.put(operationEntry.getValue().getName(),
81                         null); //currently not initializing any of the operations' fields as it is not needed
82             }
83             toscaInterfaceType.setOperations(toscaOperations);
84             Map<String, Object> interfacesAsMap = getObjectAsMap(toscaInterfaceType);
85             Map<String, Object> operationsMap = (Map<String, Object>) interfacesAsMap.remove(OPERATIONS_KEY);
86             interfacesAsMap.putAll(operationsMap);
87
88             toscaInterfaceTypes.put(interfaceDefinition.getToscaResourceName(), interfacesAsMap);
89         }
90         return MapUtils.isNotEmpty(toscaInterfaceTypes) ? toscaInterfaceTypes : null;
91     }
92
93     /**
94      * Adds the 'interfaces' element to the node type provided
95      *
96      * @param component to work on
97      * @param nodeType  to which the interfaces element will be added
98      */
99     public static void addInterfaceDefinitionElement(Component component, ToscaNodeType nodeType,
100                                                      boolean isAssociatedResourceComponent) {
101         if (component instanceof Product) {
102             return;
103         }
104         final Map<String, InterfaceDefinition> interfaces = component.getInterfaces();
105         if (MapUtils.isEmpty(interfaces)) {
106             return;
107         }
108         Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
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
116             String operationArtifactPath;
117             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
118                 ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
119                 if (isArtifactPresent(operationEntry)) {
120                     operationArtifactPath = OperationArtifactUtil
121                             .createOperationArtifactPath(component, operationEntry.getValue(),
122                                     isAssociatedResourceComponent);
123                     toscaOperation.setImplementation(operationArtifactPath);
124                 }
125                 toscaOperation.setDescription(operationEntry.getValue().getDescription());
126                 fillToscaOperationInputs(operationEntry.getValue(), toscaOperation);
127
128                 toscaOperations.put(operationEntry.getValue().getName(), toscaOperation);
129             }
130
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);
137         }
138         if (MapUtils.isNotEmpty(toscaInterfaceDefinitions)) {
139             nodeType.setInterfaces(toscaInterfaceDefinitions);
140         }
141     }
142
143     /***
144      * workaround for : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
145      * so, any string Map key named "defaultp" will have its named changed to "default"
146      * @param operationsMap the map to update
147      */
148     private static void handleDefaults(Map<String, Object> operationsMap) {
149         for (Map.Entry<String, Object> operationEntry : operationsMap.entrySet()) {
150             final Object value = operationEntry.getValue();
151             if (value instanceof Map) {
152                 handleDefaults((Map<String, Object>) value);
153             }
154             final String key = operationEntry.getKey();
155             if (key.equals(DEFAULTP)) {
156                 Object removed = operationsMap.remove(key);
157                 operationsMap.put(DEFAULT, removed);
158             }
159         }
160     }
161
162     private static String getLastPartOfName(String toscaResourceName) {
163         return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
164     }
165
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());
170         }
171         return false;
172     }
173
174     private static void fillToscaOperationInputs(OperationDataDefinition operation,
175                                                  ToscaLifecycleOperationDefinition toscaOperation) {
176         if (Objects.isNull(operation.getInputs()) || operation.getInputs().isEmpty()) {
177             toscaOperation.setInputs(null);
178             return;
179         }
180         Map<String, ToscaProperty> toscaInputs = new HashMap<>();
181
182         for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
183             ToscaProperty toscaInput = new ToscaProperty();
184             toscaInput.setDescription(input.getDescription());
185             String mappedPropertyName = null;
186             if (Objects.nonNull(input.getInputId())) {
187                 mappedPropertyName = input.getInputId().substring(input.getInputId().indexOf(DOT) + 1);
188             }
189             toscaInput.setDefaultp(createDefaultValue(mappedPropertyName));
190
191             toscaInput.setType(input.getType());
192             toscaInput.setRequired(input.isRequired());
193             toscaInputs.put(input.getName(), toscaInput);
194         }
195
196         toscaOperation.setInputs(toscaInputs);
197     }
198
199     private static Map<String, List<String>> createDefaultValue(String propertyName) {
200         Map<String, List<String>> getPropertyMap = new HashMap<>();
201         List<String> values = new ArrayList<>();
202         values.add(SELF);
203         if (Objects.nonNull(propertyName) && !propertyName.isEmpty()) {
204             values.addAll(Arrays.asList(propertyName.split("\\.")));
205         }
206
207         getPropertyMap.put(GET_PROPERTY, values);
208
209         return getPropertyMap;
210     }
211
212     private static Map<String, Object> getObjectAsMap(Object obj) {
213         ObjectMapper objectMapper = new ObjectMapper();
214         if (obj instanceof ToscaInterfaceDefinition) {
215             //Prevent empty field serialization in interface definition
216             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
217         }
218         Map<String, Object> objectAsMap =
219                 obj instanceof Map ? (Map<String, Object>) obj : objectMapper.convertValue(obj, Map.class);
220
221         if (objectAsMap.containsKey(DEFAULT)) {
222             Object defaultValue = objectAsMap.get(DEFAULT);
223             objectAsMap.remove(DEFAULT);
224             objectAsMap.put(DEFAULT_HAS_UNDERSCORE, defaultValue);
225         }
226         return objectAsMap;
227     }
228 }