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