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