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