Tosca for Workflow operations
[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.openecomp.sdc.be.datatypes.elements.OperationDataDefinition;
26 import org.openecomp.sdc.be.datatypes.elements.OperationInputDefinition;
27 import org.openecomp.sdc.be.model.Component;
28 import org.openecomp.sdc.be.model.InterfaceDefinition;
29 import org.openecomp.sdc.be.model.Product;
30 import org.openecomp.sdc.be.model.Resource;
31 import org.openecomp.sdc.be.model.Service;
32 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceDefinition;
33 import org.openecomp.sdc.be.tosca.model.ToscaInterfaceNodeType;
34 import org.openecomp.sdc.be.tosca.model.ToscaLifecycleOperationDefinition;
35 import org.openecomp.sdc.be.tosca.model.ToscaNodeType;
36 import org.openecomp.sdc.be.tosca.model.ToscaProperty;
37
38 /**
39  * @author KATYR
40  * @since March 20, 2018
41  */
42
43 public class InterfacesOperationsToscaUtil {
44
45     private static final String DERIVED_FROM_STANDARD_INTERFACE = "tosca.interfaces.node.lifecycle.Standard";
46     private static final String OPERATIONS_KEY = "operations";
47
48     private static final String DEFAULT = "default";
49     private static final String _DEFAULT = "_default";
50     private static final String DOT = ".";
51     private static final String DEFAULT_INPUT_TYPE = "string";
52     private static final String SELF = "SELF";
53     private static final String GET_PROPERTY = "get_property";
54     public static final String DEFAULTP = "defaultp";
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         Map<String, Object> toscaInterfaceTypes = new HashMap<>();
64         if ((component instanceof Service) || (component instanceof Product)) {
65             return toscaInterfaceTypes;
66         }
67
68         final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
69         for (InterfaceDefinition interfaceDefinition : interfaces.values()) {
70             ToscaInterfaceNodeType toscaInterfaceType = new ToscaInterfaceNodeType();
71             toscaInterfaceType.setDerived_from(DERIVED_FROM_STANDARD_INTERFACE);
72
73             final Map<String, OperationDataDefinition> operations = interfaceDefinition.getOperations();
74             Map<String, Object> toscaOperations = new HashMap<>();
75
76             for (String operationName : operations.keySet()) {
77                 toscaOperations.put(operationName,
78                         null); //currently not initializing any of the operations' fields as it is not needed
79             }
80
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 toscaInterfaceTypes;
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         Map<String, Object> toscaInterfaceDefinitions = new HashMap<>();
100
101         if ((component instanceof Service) || (component instanceof Product)) {
102             return;
103         }
104
105         final Map<String, InterfaceDefinition> interfaces = ((Resource) component).getInterfaces();
106         if (Objects.isNull(interfaces)) {
107             return;
108         }
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             ToscaLifecycleOperationDefinition toscaOperation = new ToscaLifecycleOperationDefinition();
116
117             String operationArtifactPath;
118             for (Map.Entry<String, OperationDataDefinition> operationEntry : operations.entrySet()) {
119                 if (isArtifactPresent(operationEntry)) {
120                     operationArtifactPath = OperationArtifactUtil
121                                                     .createOperationArtifactPath(component.getNormalizedName(),
122                                                             interfaceDefinition.getToscaResourceName(),
123                                                             operationEntry.getValue());
124                     toscaOperation.setImplementation(operationArtifactPath);
125                 }
126                 fillToscaOperation(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         nodeType.setInterfaces(toscaInterfaceDefinitions);
139     }
140
141     /***
142      * workaround : currently "defaultp" is not being converted to "default" by the relevant code in ToscaExportHandler
143      * any string key named "default" will have its named changed to "default"
144      * @param operationsMap the map to update
145      */
146     private static void handleDefaults(Map<String, Object> operationsMap) {
147         for (String key : operationsMap.keySet()) {
148             Object value = operationsMap.get(key);
149             if (value instanceof Map) {
150                 handleDefaults((Map<String, Object>) value);
151             }
152             if (key.equals(DEFAULTP)) {
153                 Object removed = operationsMap.remove(key);
154                 operationsMap.put(DEFAULT, removed);
155             }
156         }
157
158
159     }
160
161     private static String getLastPartOfName(String toscaResourceName) {
162         return toscaResourceName.substring(toscaResourceName.lastIndexOf(DOT) + 1);
163
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 fillToscaOperation(OperationDataDefinition operation,
175             ToscaLifecycleOperationDefinition toscaOperation) {
176         if (Objects.isNull(operation.getInputs())) {
177             return;
178         }
179         Map<String, ToscaProperty> inputs = new HashMap<>();
180
181         for (OperationInputDefinition input : operation.getInputs().getListToscaDataDefinition()) {
182             ToscaProperty toscaInput = new ToscaProperty();
183             toscaInput.setDescription(input.getDescription());
184             toscaInput.setType(DEFAULT_INPUT_TYPE);
185
186             toscaInput.setDefaultp(createDefaultValue(getLastPartOfName(input.getInputId())));
187             inputs.put(input.getName(), toscaInput);
188         }
189
190         toscaOperation.setInputs(inputs);
191     }
192
193     private static Map<String, List<String>> createDefaultValue(String propertyName) {
194         Map<String, List<String>> getPropertyMap = new HashMap<>();
195         List<String> values = new ArrayList<>();
196         values.add(SELF);
197         values.add(propertyName);
198         getPropertyMap.put(GET_PROPERTY, values);
199
200         return getPropertyMap;
201     }
202
203
204     private static Map<String, Object> getObjectAsMap(Object obj) {
205         Map<String, Object> objectAsMap =
206                 obj instanceof Map ? (Map<String, Object>) obj : new ObjectMapper().convertValue(obj, Map.class);
207
208         if (objectAsMap.containsKey(DEFAULT)) {
209             Object defaultValue = objectAsMap.get(DEFAULT);
210             objectAsMap.remove(DEFAULT);
211             objectAsMap.put(_DEFAULT, defaultValue);
212         }
213         return objectAsMap;
214     }
215
216 }