[SDC] rebase 1710 code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / AbstractOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import java.lang.reflect.Type;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.function.Function;
27 import java.util.function.Supplier;
28 import java.util.stream.Collectors;
29
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.openecomp.sdc.be.config.BeEcompErrorManager;
32 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
33 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
34 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
35 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
36 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
37 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
38 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
39 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
40 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
41 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
42 import org.openecomp.sdc.be.model.DataTypeDefinition;
43 import org.openecomp.sdc.be.model.IComplexDefaultValue;
44 import org.openecomp.sdc.be.model.PropertyConstraint;
45 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
46 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
47 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
48 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
49 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
50 import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
51 import org.openecomp.sdc.be.model.tosca.validators.PropertyTypeValidator;
52 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
53 import org.openecomp.sdc.be.resources.data.UniqueIdData;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 import com.google.gson.Gson;
58 import com.google.gson.GsonBuilder;
59 import com.google.gson.JsonElement;
60 import com.google.gson.reflect.TypeToken;
61 import com.thinkaurelius.titan.core.TitanVertex;
62
63 import fj.data.Either;
64
65 public abstract class AbstractOperation {
66         private static Logger log = LoggerFactory.getLogger(AbstractOperation.class.getName());
67         @javax.annotation.Resource
68         protected TitanGenericDao titanGenericDao;
69         public static final String EMPTY_VALUE = null;
70
71         protected Gson gson = new Gson();
72
73         @javax.annotation.Resource
74         protected ApplicationDataTypeCache applicationDataTypeCache;
75
76         protected DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
77
78         protected <SomeData extends GraphNode, SomeDefenition> Either<SomeData, TitanOperationStatus> addDefinitionToNodeType(SomeDefenition someDefinition, NodeTypeEnum nodeType, String nodeUniqueId, final GraphEdgeLabels edgeType,
79                         Supplier<SomeData> dataBuilder, Supplier<String> defNameGenerator) {
80                 String defName = defNameGenerator.get();
81                 log.debug("Got {} {}", defName, someDefinition);
82
83                 SomeData someData = dataBuilder.get();
84
85                 log.debug("Before adding {} to graph. data = {}", defName, someData);
86
87                 @SuppressWarnings("unchecked")
88                 Either<SomeData, TitanOperationStatus> eitherSomeData = titanGenericDao.createNode(someData, (Class<SomeData>) someData.getClass());
89
90                 log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
91
92                 if (eitherSomeData.isRight()) {
93                         TitanOperationStatus operationStatus = eitherSomeData.right().value();
94                         log.error("Failed to add {}  to graph. status is {}", defName, operationStatus);
95                         return Either.right(operationStatus);
96                 }
97                 UniqueIdData uniqueIdData = new UniqueIdData(nodeType, nodeUniqueId);
98                 log.debug("Before associating {} to {}.", uniqueIdData, defName);
99
100                 Either<GraphRelation, TitanOperationStatus> eitherRelations = titanGenericDao.createRelation(uniqueIdData, eitherSomeData.left().value(), edgeType, null);
101                 if (eitherRelations.isRight()) {
102                         TitanOperationStatus operationStatus = eitherRelations.right().value();
103                         BeEcompErrorManager.getInstance().logInternalFlowError("AddDefinitionToNodeType", "Failed to associate" + nodeType.getName() + " " + nodeUniqueId + "to " + defName + "in graph. status is " + operationStatus, ErrorSeverity.ERROR);
104                         return Either.right(operationStatus);
105                 }
106                 return Either.left(eitherSomeData.left().value());
107         }
108
109         protected <SomeData extends GraphNode, SomeDefenition> TitanOperationStatus addDefinitionToNodeType(TitanVertex vertex, SomeDefenition someDefinition, NodeTypeEnum nodeType, String nodeUniqueId, final GraphEdgeLabels edgeType,
110                         Supplier<SomeData> dataBuilder, Supplier<String> defNameGenerator) {
111                 String defName = defNameGenerator.get();
112                 log.debug("Got {} {}", defName, someDefinition);
113
114                 SomeData someData = dataBuilder.get();
115
116                 log.debug("Before adding {} to graph. data = {}", defName, someData);
117
118                 @SuppressWarnings("unchecked")
119                 Either<TitanVertex, TitanOperationStatus> eitherSomeData = titanGenericDao.createNode(someData);
120
121                 log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
122
123                 if (eitherSomeData.isRight()) {
124                         TitanOperationStatus operationStatus = eitherSomeData.right().value();
125                         log.error("Failed to add {}  to graph. status is {}", defName, operationStatus);
126                         return operationStatus;
127                 }
128
129                 TitanOperationStatus relations = titanGenericDao.createEdge(vertex, eitherSomeData.left().value(), edgeType, null);
130                 if (!relations.equals(TitanOperationStatus.OK)) {
131                         TitanOperationStatus operationStatus = relations;
132                         BeEcompErrorManager.getInstance().logInternalFlowError("AddDefinitionToNodeType", "Failed to associate" + nodeType.getName() + " " + nodeUniqueId + "to " + defName + "in graph. status is " + operationStatus, ErrorSeverity.ERROR);
133                         return operationStatus;
134                 }
135                 return relations;
136         }
137
138         interface NodeElementFetcher<ElementDefinition> {
139                 TitanOperationStatus findAllNodeElements(String nodeId, List<ElementDefinition> listTofill);
140         }
141
142         public <ElementDefinition> TitanOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
143
144                 if (log.isTraceEnabled())
145                         log.trace("Going to fetch elements under resource {}", resourceId);
146                 TitanOperationStatus resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
147
148                 if (resourceAttributesStatus != TitanOperationStatus.OK) {
149                         return resourceAttributesStatus;
150                 }
151
152                 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, TitanOperationStatus> parentNodes = titanGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
153                                 ResourceMetadataData.class);
154
155                 if (parentNodes.isRight()) {
156                         TitanOperationStatus parentNodesStatus = parentNodes.right().value();
157                         if (parentNodesStatus != TitanOperationStatus.NOT_FOUND) {
158                                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find parent elements of resource " + resourceId + ". status is " + parentNodesStatus, ErrorSeverity.ERROR);
159                                 return parentNodesStatus;
160                         }
161                 }
162
163                 if (parentNodes.isLeft()) {
164                         ImmutablePair<ResourceMetadataData, GraphEdge> parnetNodePair = parentNodes.left().value();
165                         String parentUniqueId = parnetNodePair.getKey().getMetadataDataDefinition().getUniqueId();
166                         TitanOperationStatus addParentIntStatus = findAllResourceElementsDefinitionRecursively(parentUniqueId, elements, singleNodeFetcher);
167
168                         if (addParentIntStatus != TitanOperationStatus.OK) {
169                                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
170
171                                 return addParentIntStatus;
172                         }
173                 }
174                 return TitanOperationStatus.OK;
175         }
176
177         protected <T, TStatus> void handleTransactionCommitRollback(boolean inTransaction, Either<T, TStatus> result) {
178                 if (!inTransaction) {
179                         if (result == null || result.isRight()) {
180                                 log.error("Going to execute rollback on graph.");
181                                 titanGenericDao.rollback();
182                         } else {
183                                 log.debug("Going to execute commit on graph.");
184                                 titanGenericDao.commit();
185                         }
186                 }
187         }
188
189         public <ElementTypeDefinition> Either<ElementTypeDefinition, StorageOperationStatus> getElementType(Function<String, Either<ElementTypeDefinition, TitanOperationStatus>> elementGetter, String uniqueId, boolean inTransaction) {
190                 Either<ElementTypeDefinition, StorageOperationStatus> result = null;
191                 try {
192
193                         Either<ElementTypeDefinition, TitanOperationStatus> ctResult = elementGetter.apply(uniqueId);
194
195                         if (ctResult.isRight()) {
196                                 TitanOperationStatus status = ctResult.right().value();
197                                 if (status != TitanOperationStatus.NOT_FOUND) {
198                                         log.error("Failed to retrieve information on element uniqueId: {}. status is {}", uniqueId, status);
199                                 }
200                                 result = Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(ctResult.right().value()));
201                                 return result;
202                         }
203
204                         result = Either.left(ctResult.left().value());
205
206                         return result;
207                 } finally {
208                         handleTransactionCommitRollback(inTransaction, result);
209
210                 }
211
212         }
213
214         /**
215          * @param propertyDefinition
216          * @return
217          */
218
219         protected StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
220
221                 log.trace("Going to validate property type and value. {}", propertyDefinition);
222
223                 String propertyType = propertyDefinition.getType();
224                 String value = propertyDefinition.getDefaultValue();
225
226                 ToscaPropertyType type = getType(propertyType);
227
228                 if (type == null) {
229
230                         DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
231                         if (dataTypeDefinition == null) {
232                                 log.debug("The type {}  of property cannot be found.", propertyType);
233                                 return StorageOperationStatus.INVALID_TYPE;
234                         }
235
236                         StorageOperationStatus status = validateAndUpdateComplexValue(propertyDefinition, propertyType, value, dataTypeDefinition, dataTypes);
237
238                         return status;
239
240                 }
241                 String innerType = null;
242
243                 Either<String, TitanOperationStatus> checkInnerType = getInnerType(type, () -> propertyDefinition.getSchema());
244                 if (checkInnerType.isRight()) {
245                         return StorageOperationStatus.INVALID_TYPE;
246                 }
247                 innerType = checkInnerType.left().value();
248
249                 log.trace("After validating property type {}", propertyType);
250
251                 boolean isValidProperty = isValidValue(type, value, innerType, dataTypes);
252                 if (false == isValidProperty) {
253                         log.info("The value {} of property from type {} is invalid", value, type);
254                         return StorageOperationStatus.INVALID_VALUE;
255                 }
256
257                 PropertyValueConverter converter = type.getConverter();
258
259                 if (isEmptyValue(value)) {
260                         log.debug("Default value was not sent for property {}. Set default value to {}", propertyDefinition.getName(), EMPTY_VALUE);
261                         propertyDefinition.setDefaultValue(EMPTY_VALUE);
262                 } else if (false == isEmptyValue(value)) {
263                         String convertedValue = converter.convert(value, innerType, dataTypes);
264                         propertyDefinition.setDefaultValue(convertedValue);
265                 }
266                 return StorageOperationStatus.OK;
267         }
268
269         protected ToscaPropertyType getType(String propertyType) {
270
271                 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
272
273                 return type;
274
275         }
276
277         protected boolean isValidValue(ToscaPropertyType type, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
278                 if (isEmptyValue(value)) {
279                         return true;
280                 }
281
282                 PropertyTypeValidator validator = type.getValidator();
283
284                 return validator.isValid(value, innerType, dataTypes);
285         }
286
287         public boolean isEmptyValue(String value) {
288                 return value == null;
289         }
290
291         public boolean isNullParam(String value) {
292                 return value == null;
293         }
294
295         protected StorageOperationStatus validateAndUpdateComplexValue(IComplexDefaultValue propertyDefinition, String propertyType,
296
297                         String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> dataTypes) {
298
299                 ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes);
300
301                 if (validateResult.right.booleanValue() == false) {
302                         log.debug("The value {} of property from type {} is invalid", propertyType, propertyType);
303                         return StorageOperationStatus.INVALID_VALUE;
304                 }
305
306                 JsonElement jsonElement = validateResult.left;
307
308                 log.trace("Going to update value in property definition {} {}" , propertyDefinition.getName() , (jsonElement != null ? jsonElement.toString() : null));
309
310                 updateValue(propertyDefinition, jsonElement);
311
312                 return StorageOperationStatus.OK;
313         }
314
315         protected void updateValue(IComplexDefaultValue propertyDefinition, JsonElement jsonElement) {
316
317                 propertyDefinition.setDefaultValue(getValueFromJsonElement(jsonElement));
318
319         }
320
321         protected String getValueFromJsonElement(JsonElement jsonElement) {
322                 String value = null;
323
324                 if (jsonElement == null || jsonElement.isJsonNull()) {
325                         value = EMPTY_VALUE;
326                 } else {
327                         value = jsonElement.toString();
328                 }
329
330                 return value; 
331         }
332
333         protected Either<String, TitanOperationStatus> getInnerType(ToscaPropertyType type, Supplier<SchemaDefinition> schemeGen) {
334                 String innerType = null;
335                 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
336
337                         SchemaDefinition def = schemeGen.get();// propDataDef.getSchema();
338                         if (def == null) {
339                                 log.debug("Schema doesn't exists for property of type {}", type);
340                                 return Either.right(TitanOperationStatus.ILLEGAL_ARGUMENT);
341                         }
342                         PropertyDataDefinition propDef = def.getProperty();
343                         if (propDef == null) {
344                                 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
345                                 return Either.right(TitanOperationStatus.ILLEGAL_ARGUMENT);
346                         }
347                         innerType = propDef.getType();
348                 }
349                 return Either.left(innerType);
350         }
351
352         /**
353          * Convert Constarint object to json in order to add it to the Graph
354          * 
355          * @param constraints
356          * @return
357          */
358         public List<String> convertConstraintsToString(List<PropertyConstraint> constraints) {
359
360                 if (constraints == null || constraints.isEmpty()) {
361                         return null;
362                 }
363
364                 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
365         }
366
367         public List<PropertyConstraint> convertConstraints(List<String> constraints) {
368
369                 if (constraints == null || constraints.isEmpty()) {
370                         return null;
371                 }
372
373                 Type constraintType = new TypeToken<PropertyConstraint>() {
374                 }.getType();
375
376                 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
377
378                 return constraints.stream().map(c -> gson.fromJson(c, PropertyConstraint.class)).collect(Collectors.toList());
379         }
380
381 }