Upgrade SDC from Titan to Janus Graph
[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 com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.JsonElement;
26 import com.google.gson.reflect.TypeToken;
27 import org.janusgraph.core.JanusGraphVertex;
28 import fj.data.Either;
29 import java.lang.reflect.Type;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.function.Supplier;
33 import java.util.stream.Collectors;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.openecomp.sdc.be.config.BeEcompErrorManager;
36 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
37 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
38 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
39 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
40 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
41 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
42 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
43 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
44 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
45 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
46 import org.openecomp.sdc.be.model.DataTypeDefinition;
47 import org.openecomp.sdc.be.model.IComplexDefaultValue;
48 import org.openecomp.sdc.be.model.PropertyConstraint;
49 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
50 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
51 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation.PropertyConstraintDeserialiser;
52 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
53 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
54 import org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter;
55 import org.openecomp.sdc.be.model.tosca.validators.PropertyTypeValidator;
56 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
57 import org.openecomp.sdc.be.resources.data.UniqueIdData;
58 import org.openecomp.sdc.common.log.wrappers.Logger;
59 import org.springframework.beans.factory.annotation.Autowired;
60
61 public abstract class AbstractOperation {
62
63     private static final Logger log = Logger.getLogger(AbstractOperation.class.getName());
64
65     @Autowired
66     protected HealingJanusGraphGenericDao janusGraphGenericDao;
67
68     public static final String EMPTY_VALUE = null;
69
70     protected Gson gson = new Gson();
71
72     @Autowired
73     protected ApplicationDataTypeCache applicationDataTypeCache;
74
75     protected DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
76
77     protected <SomeData extends GraphNode, SomeDefenition> Either<SomeData, JanusGraphOperationStatus> addDefinitionToNodeType(SomeDefenition someDefinition, NodeTypeEnum nodeType, String nodeUniqueId, final GraphEdgeLabels edgeType,
78                                                                                                                                Supplier<SomeData> dataBuilder, Supplier<String> defNameGenerator) {
79         String defName = defNameGenerator.get();
80         log.debug("Got {} {}", defName, someDefinition);
81
82         SomeData someData = dataBuilder.get();
83
84         log.debug("Before adding {} to graph. data = {}", defName, someData);
85
86         @SuppressWarnings("unchecked")
87         Either<SomeData, JanusGraphOperationStatus> eitherSomeData = janusGraphGenericDao
88             .createNode(someData, (Class<SomeData>) someData.getClass());
89
90         log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
91
92         if (eitherSomeData.isRight()) {
93             JanusGraphOperationStatus 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, JanusGraphOperationStatus> eitherRelations = janusGraphGenericDao
101             .createRelation(uniqueIdData, eitherSomeData.left().value(), edgeType, null);
102         if (eitherRelations.isRight()) {
103             JanusGraphOperationStatus operationStatus = eitherRelations.right().value();
104             BeEcompErrorManager.getInstance().logInternalFlowError("AddDefinitionToNodeType", "Failed to associate" + nodeType.getName() + " " + nodeUniqueId + "to " + defName + "in graph. status is " + operationStatus, ErrorSeverity.ERROR);
105             return Either.right(operationStatus);
106         }
107         return Either.left(eitherSomeData.left().value());
108     }
109
110     protected <SomeData extends GraphNode, SomeDefenition> JanusGraphOperationStatus addDefinitionToNodeType(JanusGraphVertex vertex, SomeDefenition someDefinition, NodeTypeEnum nodeType, String nodeUniqueId, final GraphEdgeLabels edgeType,
111                                                                                                              Supplier<SomeData> dataBuilder, Supplier<String> defNameGenerator) {
112         String defName = defNameGenerator.get();
113         log.debug("Got {} {}", defName, someDefinition);
114
115         SomeData someData = dataBuilder.get();
116
117         log.debug("Before adding {} to graph. data = {}", defName, someData);
118
119         @SuppressWarnings("unchecked")
120         Either<JanusGraphVertex, JanusGraphOperationStatus> eitherSomeData = janusGraphGenericDao.createNode(someData);
121
122         log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
123
124         if (eitherSomeData.isRight()) {
125             JanusGraphOperationStatus operationStatus = eitherSomeData.right().value();
126             log.error("Failed to add {}  to graph. status is {}", defName, operationStatus);
127             return operationStatus;
128         }
129
130         JanusGraphOperationStatus
131             relations = janusGraphGenericDao
132             .createEdge(vertex, eitherSomeData.left().value(), edgeType, null);
133         if (!relations.equals(JanusGraphOperationStatus.OK)) {
134             BeEcompErrorManager.getInstance().logInternalFlowError("AddDefinitionToNodeType", "Failed to associate" + nodeType.getName() + " " + nodeUniqueId + "to " + defName + "in graph. status is " + relations, ErrorSeverity.ERROR);
135             return relations;
136         }
137         return relations;
138     }
139
140     interface NodeElementFetcher<ElementDefinition> {
141         JanusGraphOperationStatus findAllNodeElements(String nodeId, List<ElementDefinition> listTofill);
142     }
143
144     public <ElementDefinition> JanusGraphOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
145
146         if (log.isTraceEnabled())
147             log.trace("Going to fetch elements under resource {}", resourceId);
148         JanusGraphOperationStatus
149             resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
150
151         if (resourceAttributesStatus != JanusGraphOperationStatus.OK) {
152             return resourceAttributesStatus;
153         }
154
155         Either<ImmutablePair<ResourceMetadataData, GraphEdge>, JanusGraphOperationStatus> parentNodes = janusGraphGenericDao
156             .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
157                 ResourceMetadataData.class);
158
159         if (parentNodes.isRight()) {
160             JanusGraphOperationStatus parentNodesStatus = parentNodes.right().value();
161             if (parentNodesStatus != JanusGraphOperationStatus.NOT_FOUND) {
162                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find parent elements of resource " + resourceId + ". status is " + parentNodesStatus, ErrorSeverity.ERROR);
163                 return parentNodesStatus;
164             }
165         }
166
167         if (parentNodes.isLeft()) {
168             ImmutablePair<ResourceMetadataData, GraphEdge> parnetNodePair = parentNodes.left().value();
169             String parentUniqueId = parnetNodePair.getKey().getMetadataDataDefinition().getUniqueId();
170             JanusGraphOperationStatus addParentIntStatus = findAllResourceElementsDefinitionRecursively(parentUniqueId, elements, singleNodeFetcher);
171
172             if (addParentIntStatus != JanusGraphOperationStatus.OK) {
173                 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
174
175                 return addParentIntStatus;
176             }
177         }
178         return JanusGraphOperationStatus.OK;
179     }
180
181     protected <T, TStatus> void handleTransactionCommitRollback(boolean inTransaction, Either<T, TStatus> result) {
182         if (!inTransaction) {
183             if (result == null || result.isRight()) {
184                 log.error("Going to execute rollback on graph.");
185                 janusGraphGenericDao.rollback();
186             } else {
187                 log.debug("Going to execute commit on graph.");
188                 janusGraphGenericDao.commit();
189             }
190         }
191     }
192
193
194     /**
195      * @param propertyDefinition
196      * @return
197      */
198
199     protected StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
200
201         log.trace("Going to validate property type and value. {}", propertyDefinition);
202
203         String propertyType = propertyDefinition.getType();
204         String value = propertyDefinition.getDefaultValue();
205
206         ToscaPropertyType type = getType(propertyType);
207
208         if (type == null) {
209
210             DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
211             if (dataTypeDefinition == null) {
212                 log.debug("The type {}  of property cannot be found.", propertyType);
213                 return StorageOperationStatus.INVALID_TYPE;
214             }
215
216             return validateAndUpdateComplexValue(propertyDefinition, propertyType, value, dataTypeDefinition, dataTypes);
217
218         }
219         String innerType = null;
220
221         Either<String, JanusGraphOperationStatus> checkInnerType = getInnerType(type, propertyDefinition::getSchema);
222         if (checkInnerType.isRight()) {
223             return StorageOperationStatus.INVALID_TYPE;
224         }
225         innerType = checkInnerType.left().value();
226
227         log.trace("After validating property type {}", propertyType);
228
229         boolean isValidProperty = isValidValue(type, value, innerType, dataTypes);
230         if (!isValidProperty) {
231             log.info("The value {} of property from type {} is invalid", value, type);
232             return StorageOperationStatus.INVALID_VALUE;
233         }
234
235         PropertyValueConverter converter = type.getConverter();
236
237         if (isEmptyValue(value)) {
238             log.debug("Default value was not sent for property {}. Set default value to {}", propertyDefinition.getName(), EMPTY_VALUE);
239             propertyDefinition.setDefaultValue(EMPTY_VALUE);
240         } else if (!isEmptyValue(value)) {
241             String convertedValue = converter.convert(value, innerType, dataTypes);
242             propertyDefinition.setDefaultValue(convertedValue);
243         }
244         return StorageOperationStatus.OK;
245     }
246
247     protected ToscaPropertyType getType(String propertyType) {
248
249         return ToscaPropertyType.isValidType(propertyType);
250
251     }
252
253     protected boolean isValidValue(ToscaPropertyType type, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
254         if (isEmptyValue(value)) {
255             return true;
256         }
257
258         PropertyTypeValidator validator = type.getValidator();
259
260         return validator.isValid(value, innerType, dataTypes);
261     }
262
263     public boolean isEmptyValue(String value) {
264         return value == null;
265     }
266
267     public boolean isNullParam(String value) {
268         return value == null;
269     }
270
271     protected StorageOperationStatus validateAndUpdateComplexValue(IComplexDefaultValue propertyDefinition, String propertyType,
272
273             String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> dataTypes) {
274
275         ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes);
276
277         if (!validateResult.right.booleanValue()) {
278             log.debug("The value {} of property from type {} is invalid", propertyType, propertyType);
279             return StorageOperationStatus.INVALID_VALUE;
280         }
281
282         JsonElement jsonElement = validateResult.left;
283
284         log.trace("Going to update value in property definition {} {}" , propertyDefinition.getName() , (jsonElement != null ? jsonElement.toString() : null));
285
286         updateValue(propertyDefinition, jsonElement);
287
288         return StorageOperationStatus.OK;
289     }
290
291     protected void updateValue(IComplexDefaultValue propertyDefinition, JsonElement jsonElement) {
292
293         propertyDefinition.setDefaultValue(getValueFromJsonElement(jsonElement));
294
295     }
296
297     protected String getValueFromJsonElement(JsonElement jsonElement) {
298         String value = null;
299
300         if (jsonElement == null || jsonElement.isJsonNull()) {
301             value = EMPTY_VALUE;
302         } else {
303             value = jsonElement.toString();
304         }
305
306         return value;
307     }
308
309     protected Either<String, JanusGraphOperationStatus> getInnerType(ToscaPropertyType type, Supplier<SchemaDefinition> schemeGen) {
310         String innerType = null;
311         if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
312
313             SchemaDefinition def = schemeGen.get();
314             if (def == null) {
315                 log.debug("Schema doesn't exists for property of type {}", type);
316                 return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
317             }
318             PropertyDataDefinition propDef = def.getProperty();
319             if (propDef == null) {
320                 log.debug("Property in Schema Definition inside property of type {} doesn't exist", type);
321                 return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
322             }
323             innerType = propDef.getType();
324         }
325         return Either.left(innerType);
326     }
327
328     /**
329      * Convert Constarint object to json in order to add it to the Graph
330      *
331      * @param constraints
332      * @return
333      */
334     public List<String> convertConstraintsToString(List<PropertyConstraint> constraints) {
335
336         if (constraints == null || constraints.isEmpty()) {
337             return null;
338         }
339
340         return constraints.stream().map(gson::toJson).collect(Collectors.toList());
341     }
342
343     public List<PropertyConstraint> convertConstraints(List<String> constraints) {
344
345         if (constraints == null || constraints.isEmpty()) {
346             return null;
347         }
348
349         Type constraintType = new TypeToken<PropertyConstraint>() {
350         }.getType();
351
352         Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
353
354         return constraints.stream().map(c -> gson.fromJson(c, PropertyConstraint.class)).collect(Collectors.toList());
355     }
356
357 }