2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
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;
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;
61 public abstract class AbstractOperation {
63 private static final Logger log = Logger.getLogger(AbstractOperation.class.getName());
66 protected HealingJanusGraphGenericDao janusGraphGenericDao;
68 public static final String EMPTY_VALUE = null;
70 protected Gson gson = new Gson();
73 protected ApplicationDataTypeCache applicationDataTypeCache;
75 protected DataTypeValidatorConverter dataTypeValidatorConverter = DataTypeValidatorConverter.getInstance();
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);
82 SomeData someData = dataBuilder.get();
84 log.debug("Before adding {} to graph. data = {}", defName, someData);
86 @SuppressWarnings("unchecked")
87 Either<SomeData, JanusGraphOperationStatus> eitherSomeData = janusGraphGenericDao
88 .createNode(someData, (Class<SomeData>) someData.getClass());
90 log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
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);
97 UniqueIdData uniqueIdData = new UniqueIdData(nodeType, nodeUniqueId);
98 log.debug("Before associating {} to {}.", uniqueIdData, defName);
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);
107 return Either.left(eitherSomeData.left().value());
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);
115 SomeData someData = dataBuilder.get();
117 log.debug("Before adding {} to graph. data = {}", defName, someData);
119 @SuppressWarnings("unchecked")
120 Either<JanusGraphVertex, JanusGraphOperationStatus> eitherSomeData = janusGraphGenericDao.createNode(someData);
122 log.debug("After adding {} to graph. status is = {}", defName, eitherSomeData);
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;
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);
140 interface NodeElementFetcher<ElementDefinition> {
141 JanusGraphOperationStatus findAllNodeElements(String nodeId, List<ElementDefinition> listTofill);
144 public <ElementDefinition> JanusGraphOperationStatus findAllResourceElementsDefinitionRecursively(String resourceId, List<ElementDefinition> elements, NodeElementFetcher<ElementDefinition> singleNodeFetcher) {
146 if (log.isTraceEnabled())
147 log.trace("Going to fetch elements under resource {}", resourceId);
148 JanusGraphOperationStatus
149 resourceAttributesStatus = singleNodeFetcher.findAllNodeElements(resourceId, elements);
151 if (resourceAttributesStatus != JanusGraphOperationStatus.OK) {
152 return resourceAttributesStatus;
155 Either<ImmutablePair<ResourceMetadataData, GraphEdge>, JanusGraphOperationStatus> parentNodes = janusGraphGenericDao
156 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Resource), resourceId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.Resource,
157 ResourceMetadataData.class);
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;
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);
172 if (addParentIntStatus != JanusGraphOperationStatus.OK) {
173 BeEcompErrorManager.getInstance().logInternalFlowError("findAllResourceElementsDefinitionRecursively", "Failed to find all resource elements of resource " + parentUniqueId, ErrorSeverity.ERROR);
175 return addParentIntStatus;
178 return JanusGraphOperationStatus.OK;
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();
187 log.debug("Going to execute commit on graph.");
188 janusGraphGenericDao.commit();
195 * @param propertyDefinition
199 protected StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
201 log.trace("Going to validate property type and value. {}", propertyDefinition);
203 String propertyType = propertyDefinition.getType();
204 String value = propertyDefinition.getDefaultValue();
206 ToscaPropertyType type = getType(propertyType);
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;
216 return validateAndUpdateComplexValue(propertyDefinition, propertyType, value, dataTypeDefinition, dataTypes);
219 String innerType = null;
221 Either<String, JanusGraphOperationStatus> checkInnerType = getInnerType(type, propertyDefinition::getSchema);
222 if (checkInnerType.isRight()) {
223 return StorageOperationStatus.INVALID_TYPE;
225 innerType = checkInnerType.left().value();
227 log.trace("After validating property type {}", propertyType);
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;
235 PropertyValueConverter converter = type.getConverter();
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);
244 return StorageOperationStatus.OK;
247 protected ToscaPropertyType getType(String propertyType) {
249 return ToscaPropertyType.isValidType(propertyType);
253 protected boolean isValidValue(ToscaPropertyType type, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
254 if (isEmptyValue(value)) {
258 PropertyTypeValidator validator = type.getValidator();
260 return validator.isValid(value, innerType, dataTypes);
263 public boolean isEmptyValue(String value) {
264 return value == null;
267 public boolean isNullParam(String value) {
268 return value == null;
271 protected StorageOperationStatus validateAndUpdateComplexValue(IComplexDefaultValue propertyDefinition, String propertyType,
273 String value, DataTypeDefinition dataTypeDefinition, Map<String, DataTypeDefinition> dataTypes) {
275 ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes);
277 if (!validateResult.right.booleanValue()) {
278 log.debug("The value {} of property from type {} is invalid", propertyType, propertyType);
279 return StorageOperationStatus.INVALID_VALUE;
282 JsonElement jsonElement = validateResult.left;
284 log.trace("Going to update value in property definition {} {}" , propertyDefinition.getName() , (jsonElement != null ? jsonElement.toString() : null));
286 updateValue(propertyDefinition, jsonElement);
288 return StorageOperationStatus.OK;
291 protected void updateValue(IComplexDefaultValue propertyDefinition, JsonElement jsonElement) {
293 propertyDefinition.setDefaultValue(getValueFromJsonElement(jsonElement));
297 protected String getValueFromJsonElement(JsonElement jsonElement) {
300 if (jsonElement == null || jsonElement.isJsonNull()) {
303 value = jsonElement.toString();
309 protected Either<String, JanusGraphOperationStatus> getInnerType(ToscaPropertyType type, Supplier<SchemaDefinition> schemeGen) {
310 String innerType = null;
311 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
313 SchemaDefinition def = schemeGen.get();
315 log.debug("Schema doesn't exists for property of type {}", type);
316 return Either.right(JanusGraphOperationStatus.ILLEGAL_ARGUMENT);
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);
323 innerType = propDef.getType();
325 return Either.left(innerType);
329 * Convert Constarint object to json in order to add it to the Graph
334 public List<String> convertConstraintsToString(List<PropertyConstraint> constraints) {
336 if (constraints == null || constraints.isEmpty()) {
340 return constraints.stream().map(gson::toJson).collect(Collectors.toList());
343 public List<PropertyConstraint> convertConstraints(List<String> constraints) {
345 if (constraints == null || constraints.isEmpty()) {
349 Type constraintType = new TypeToken<PropertyConstraint>() {
352 Gson gson = new GsonBuilder().registerTypeAdapter(constraintType, new PropertyConstraintDeserialiser()).create();
354 return constraints.stream().map(c -> gson.fromJson(c, PropertyConstraint.class)).collect(Collectors.toList());