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.fasterxml.jackson.core.ObjectCodec;
24 import com.fasterxml.jackson.databind.DeserializationContext;
25 import com.fasterxml.jackson.databind.JsonNode;
26 import com.google.common.collect.Maps;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonDeserializationContext;
29 import com.google.gson.JsonDeserializer;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParseException;
33 import com.google.gson.JsonParser;
34 import com.google.gson.JsonSerializationContext;
35 import com.google.gson.JsonSerializer;
36 import org.janusgraph.core.JanusGraph;
37 import org.janusgraph.core.JanusGraphVertex;
38 import org.janusgraph.core.JanusGraphVertexProperty;
39 import fj.data.Either;
40 import java.io.IOException;
41 import java.lang.reflect.Type;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Map.Entry;
50 import java.util.StringJoiner;
51 import java.util.function.Consumer;
52 import java.util.regex.Matcher;
53 import java.util.regex.Pattern;
54 import java.util.stream.Collectors;
55 import org.apache.commons.collections.CollectionUtils;
56 import org.apache.commons.collections.MapUtils;
57 import org.apache.commons.lang3.StringUtils;
58 import org.apache.commons.lang3.tuple.ImmutablePair;
59 import org.apache.tinkerpop.gremlin.structure.Edge;
60 import org.apache.tinkerpop.gremlin.structure.Vertex;
61 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
62 import org.openecomp.sdc.be.config.BeEcompErrorManager;
63 import org.openecomp.sdc.be.config.BeEcompErrorManager.ErrorSeverity;
64 import org.openecomp.sdc.be.dao.graph.GraphElementFactory;
65 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
66 import org.openecomp.sdc.be.dao.graph.datatype.GraphElementTypeEnum;
67 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
68 import org.openecomp.sdc.be.dao.graph.datatype.GraphRelation;
69 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
70 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
71 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
72 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
73 import org.openecomp.sdc.be.dao.janusgraph.HealingJanusGraphGenericDao;
74 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.PropertyRule;
76 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
77 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
78 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
79 import org.openecomp.sdc.be.model.DataTypeDefinition;
80 import org.openecomp.sdc.be.model.IComplexDefaultValue;
81 import org.openecomp.sdc.be.model.PropertyConstraint;
82 import org.openecomp.sdc.be.model.PropertyDefinition;
83 import org.openecomp.sdc.be.model.operations.api.DerivedFromOperation;
84 import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
85 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
86 import org.openecomp.sdc.be.model.tosca.ToscaPropertyType;
87 import org.openecomp.sdc.be.model.tosca.constraints.ConstraintType;
88 import org.openecomp.sdc.be.model.tosca.constraints.GreaterOrEqualConstraint;
89 import org.openecomp.sdc.be.model.tosca.constraints.GreaterThanConstraint;
90 import org.openecomp.sdc.be.model.tosca.constraints.InRangeConstraint;
91 import org.openecomp.sdc.be.model.tosca.constraints.LessOrEqualConstraint;
92 import org.openecomp.sdc.be.model.tosca.constraints.LessThanConstraint;
93 import org.openecomp.sdc.be.model.tosca.constraints.MinLengthConstraint;
94 import org.openecomp.sdc.be.model.tosca.constraints.ValidValuesConstraint;
95 import org.openecomp.sdc.be.model.tosca.converters.PropertyValueConverter;
96 import org.openecomp.sdc.be.resources.data.ComponentInstanceData;
97 import org.openecomp.sdc.be.resources.data.DataTypeData;
98 import org.openecomp.sdc.be.resources.data.PropertyData;
99 import org.openecomp.sdc.be.resources.data.PropertyValueData;
100 import org.openecomp.sdc.be.resources.data.ResourceMetadataData;
101 import org.openecomp.sdc.be.resources.data.UniqueIdData;
102 import org.openecomp.sdc.common.log.wrappers.Logger;
103 import org.springframework.beans.factory.annotation.Autowired;
104 import org.springframework.stereotype.Component;
107 @Component("property-operation")
108 public class PropertyOperation extends AbstractOperation implements IPropertyOperation {
109 private static final String FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE = "Failed to fetch properties of data type {}";
110 private static final String DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS = "Data type {} cannot be found in graph. status is {}";
111 private static final String GOING_TO_EXECUTE_COMMIT_ON_GRAPH = "Going to execute commit on graph.";
112 private static final String GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH = "Going to execute rollback on graph.";
113 private static final String FAILED_TO_ASSOCIATE_RESOURCE_TO_PROPERTY_IN_GRAPH_STATUS_IS = "Failed to associate resource {} to property {} in graph. status is {}";
114 private static final String AFTER_ADDING_PROPERTY_TO_GRAPH = "After adding property to graph {}";
115 private static final String BEFORE_ADDING_PROPERTY_TO_GRAPH = "Before adding property to graph {}";
116 private static final String THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID = "The value {} of property from type {} is invalid";
117 private DerivedFromOperation derivedFromOperation;
118 private static final String PROPERTY = "Property";
119 private static final String UPDATE_DATA_TYPE = "UpdateDataType";
121 public static void main(String[] args) {
123 List<Pattern> buildFunctionPatterns = buildFunctionPatterns();
125 for (Pattern pattern : buildFunctionPatterns) {
127 String[] strs = { "str_replace", "{ str_replace:", " {str_replace:", " { str_replace:", "{str_replace:" };
128 for (String str : strs) {
129 Matcher m = pattern.matcher(str);
130 System.out.println(pattern.pattern() + " " + str + " " + m.find());
137 public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation) {
138 this.janusGraphGenericDao = janusGraphGenericDao;
139 this.derivedFromOperation = derivedFromOperation;
142 private static Logger log = Logger.getLogger(PropertyOperation.class.getName());
145 * The value of functions is in a json format. Build pattern for each function name
147 * { str_replace: .... } {str_replace: .... } {str_replace: .... } { str_replace: .... }
151 private static List<Pattern> buildFunctionPatterns() {
153 List<Pattern> functionPatterns = new ArrayList<>();
155 String[] functions = { "get_input", "get_property" };
157 for (String function : functions) {
158 Pattern pattern = Pattern.compile("^[ ]*\\{[ ]*" + function + ":");
159 functionPatterns.add(pattern);
162 return functionPatterns;
165 public PropertyDefinition convertPropertyDataToPropertyDefinition(PropertyData propertyDataResult, String propertyName, String resourceId) {
166 log.debug("The object returned after create property is {}", propertyDataResult);
168 PropertyDefinition propertyDefResult = new PropertyDefinition(propertyDataResult.getPropertyDataDefinition());
169 propertyDefResult.setConstraints(convertConstraints(propertyDataResult.getConstraints()));
170 propertyDefResult.setName(propertyName);
172 return propertyDefResult;
175 public static class PropertyConstraintSerialiser implements JsonSerializer<PropertyConstraint> {
178 public JsonElement serialize(PropertyConstraint src, Type typeOfSrc, JsonSerializationContext context) {
179 JsonParser parser = new JsonParser();
180 JsonObject result = new JsonObject();
181 JsonArray jsonArray = new JsonArray();
182 if (src instanceof InRangeConstraint) {
183 InRangeConstraint rangeConstraint = (InRangeConstraint) src;
184 jsonArray.add(parser.parse(rangeConstraint.getRangeMinValue()));
185 jsonArray.add(parser.parse(rangeConstraint.getRangeMaxValue()));
186 result.add("inRange", jsonArray);
187 } else if (src instanceof GreaterThanConstraint) {
188 GreaterThanConstraint greaterThanConstraint = (GreaterThanConstraint) src;
189 jsonArray.add(parser.parse(greaterThanConstraint.getGreaterThan()));
190 result.add("greaterThan", jsonArray);
191 } else if (src instanceof LessOrEqualConstraint) {
192 LessOrEqualConstraint lessOrEqualConstraint = (LessOrEqualConstraint) src;
193 jsonArray.add(parser.parse(lessOrEqualConstraint.getLessOrEqual()));
194 result.add("lessOrEqual", jsonArray);
196 log.warn("PropertyConstraint {} is not supported. Ignored.", src.getClass().getName());
204 public static class PropertyConstraintDeserialiser implements JsonDeserializer<PropertyConstraint> {
206 private static final String THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL = "The value of GreaterThanConstraint is null";
209 public PropertyConstraint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
211 PropertyConstraint propertyConstraint = null;
213 Set<Entry<String, JsonElement>> set = json.getAsJsonObject().entrySet();
215 if (set.size() == 1) {
216 Entry<String, JsonElement> element = set.iterator().next();
217 String key = element.getKey();
218 JsonElement value = element.getValue();
220 ConstraintType constraintType = ConstraintType.getByType(key);
221 if (constraintType == null) {
222 log.warn("ConstraintType was not found for constraint name:{}", key);
224 switch (constraintType) {
228 if (value instanceof JsonArray) {
229 JsonArray rangeArray = (JsonArray) value;
230 if (rangeArray.size() != 2) {
231 log.error("The range constraint content is invalid. value = {}", value);
233 InRangeConstraint rangeConstraint = new InRangeConstraint();
234 String minValue = rangeArray.get(0).getAsString();
236 JsonElement maxElement = rangeArray.get(1);
237 if(maxElement.isJsonNull()){
238 maxValue = String.valueOf(maxElement.getAsJsonNull());
240 maxValue = maxElement.getAsString();
242 rangeConstraint.setRangeMinValue(minValue);
243 rangeConstraint.setRangeMaxValue(maxValue);
244 propertyConstraint = rangeConstraint;
249 log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
254 String asString = value.getAsString();
255 log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
256 propertyConstraint = new GreaterThanConstraint(asString);
259 log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
265 String asString = value.getAsString();
266 log.debug("Before adding value to LessThanConstraint object. value = {}", asString);
267 propertyConstraint = new LessThanConstraint(asString);
270 log.warn("The value of LessThanConstraint is null");
273 case GREATER_OR_EQUAL:
275 String asString = value.getAsString();
276 log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
277 propertyConstraint = new GreaterOrEqualConstraint(asString);
280 log.warn("The value of GreaterOrEqualConstraint is null");
286 String asString = value.getAsString();
287 log.debug("Before adding value to LessOrEqualConstraint object. value = {}", asString);
288 propertyConstraint = new LessOrEqualConstraint(asString);
290 log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
297 JsonArray rangeArray = (JsonArray) value;
298 if (rangeArray.size() == 0) {
299 log.error("The valid values constraint content is invalid. value = {}", value);
301 ValidValuesConstraint vvConstraint = new ValidValuesConstraint();
302 List<String> validValues = new ArrayList<>();
303 for (JsonElement jsonElement : rangeArray) {
304 String item = jsonElement.getAsString();
305 validValues.add(item);
307 vvConstraint.setValidValues(validValues);
308 propertyConstraint = vvConstraint;
315 int asInt = value.getAsInt();
316 log.debug("Before adding value to Min Length object. value = {}", asInt);
317 propertyConstraint = new MinLengthConstraint(asInt);
320 log.warn("The value of MinLengthConstraint is null");
324 log.warn("Key {} is not supported. Ignored.", key);
329 return propertyConstraint;
335 public Either<PropertyData, StorageOperationStatus> addProperty(String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
337 Either<PropertyData, JanusGraphOperationStatus> either = addPropertyToGraph(propertyName, propertyDefinition, resourceId);
338 if (either.isRight()) {
339 StorageOperationStatus storageStatus = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(either.right().value());
340 return Either.right(storageStatus);
342 return Either.left(either.left().value());
346 * @param propertyDefinition
350 public StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
352 log.trace("Going to validate property type and value. {}", propertyDefinition);
354 String propertyType = propertyDefinition.getType();
355 String value = propertyDefinition.getDefaultValue();
357 ToscaPropertyType type = getType(propertyType);
361 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
362 if (dataTypeDefinition == null) {
363 log.debug("The type {} of property cannot be found.", propertyType);
364 return StorageOperationStatus.INVALID_TYPE;
367 return validateAndUpdateComplexValue(propertyDefinition, propertyType, value, dataTypeDefinition, dataTypes);
370 String innerType = null;
372 Either<String, JanusGraphOperationStatus> checkInnerType = getInnerType(type, propertyDefinition::getSchema);
373 if (checkInnerType.isRight()) {
374 return StorageOperationStatus.INVALID_TYPE;
376 innerType = checkInnerType.left().value();
378 log.trace("After validating property type {}", propertyType);
380 boolean isValidProperty = isValidValue(type, value, innerType, dataTypes);
381 if (!isValidProperty) {
382 log.info(THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID, value, type);
383 return StorageOperationStatus.INVALID_VALUE;
386 PropertyValueConverter converter = type.getConverter();
388 if (isEmptyValue(value)) {
389 log.debug("Default value was not sent for property {}. Set default value to {}", propertyDefinition.getName(), EMPTY_VALUE);
390 propertyDefinition.setDefaultValue(EMPTY_VALUE);
391 } else if (!isEmptyValue(value)) {
392 String convertedValue = converter.convert(value, innerType, dataTypes);
393 propertyDefinition.setDefaultValue(convertedValue);
395 return StorageOperationStatus.OK;
398 public Either<PropertyData, JanusGraphOperationStatus> addPropertyToGraph(String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
400 ResourceMetadataData resourceData = new ResourceMetadataData();
401 resourceData.getMetadataDataDefinition().setUniqueId(resourceId);
403 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
405 propertyDefinition.setUniqueId(UniqueIdBuilder.buildComponentPropertyUniqueId(resourceId, propertyName));
406 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
408 log.debug(BEFORE_ADDING_PROPERTY_TO_GRAPH, propertyData);
409 Either<PropertyData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
410 .createNode(propertyData, PropertyData.class);
411 log.debug(AFTER_ADDING_PROPERTY_TO_GRAPH, propertyData);
412 if (createNodeResult.isRight()) {
413 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
414 log.error("Failed to add property {} to graph. status is {}", propertyName, operationStatus);
415 return Either.right(operationStatus);
418 Map<String, Object> props = new HashMap<>();
419 props.put(GraphPropertiesDictionary.NAME.getProperty(), propertyName);
420 Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
421 .createRelation(resourceData, propertyData, GraphEdgeLabels.PROPERTY, props);
422 if (createRelResult.isRight()) {
423 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
424 log.error(FAILED_TO_ASSOCIATE_RESOURCE_TO_PROPERTY_IN_GRAPH_STATUS_IS, resourceId, propertyName, operationStatus);
425 return Either.right(operationStatus);
428 return Either.left(createNodeResult.left().value());
432 public JanusGraphOperationStatus addPropertyToGraphByVertex(JanusGraphVertex metadataVertex, String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
434 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
436 propertyDefinition.setUniqueId(UniqueIdBuilder.buildComponentPropertyUniqueId(resourceId, propertyName));
437 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
439 log.debug(BEFORE_ADDING_PROPERTY_TO_GRAPH, propertyData);
440 Either<JanusGraphVertex, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao.createNode(propertyData);
441 log.debug(AFTER_ADDING_PROPERTY_TO_GRAPH, propertyData);
442 if (createNodeResult.isRight()) {
443 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
444 log.error("Failed to add property {} to graph. status is ", propertyName, operationStatus);
445 return operationStatus;
448 Map<String, Object> props = new HashMap<>();
449 props.put(GraphPropertiesDictionary.NAME.getProperty(), propertyName);
450 JanusGraphVertex propertyVertex = createNodeResult.left().value();
451 JanusGraphOperationStatus
452 createRelResult = janusGraphGenericDao
453 .createEdge(metadataVertex, propertyVertex, GraphEdgeLabels.PROPERTY, props);
454 if (!createRelResult.equals(JanusGraphOperationStatus.OK)) {
455 log.error(FAILED_TO_ASSOCIATE_RESOURCE_TO_PROPERTY_IN_GRAPH_STATUS_IS, resourceId, propertyName, createRelResult);
456 return createRelResult;
459 return createRelResult;
463 public JanusGraphGenericDao getJanusGraphGenericDao() {
464 return janusGraphGenericDao;
467 public Either<PropertyData, JanusGraphOperationStatus> deletePropertyFromGraph(String propertyId) {
468 log.debug("Before deleting property from graph {}", propertyId);
469 return janusGraphGenericDao
470 .deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Property), propertyId, PropertyData.class);
473 public Either<PropertyData, StorageOperationStatus> updateProperty(String propertyId, PropertyDefinition newPropertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
475 StorageOperationStatus validateAndUpdateProperty = validateAndUpdateProperty(newPropertyDefinition, dataTypes);
476 if (validateAndUpdateProperty != StorageOperationStatus.OK) {
477 return Either.right(validateAndUpdateProperty);
480 Either<PropertyData, JanusGraphOperationStatus> either = updatePropertyFromGraph(propertyId, newPropertyDefinition);
481 if (either.isRight()) {
482 StorageOperationStatus storageStatus = DaoStatusConverter.convertJanusGraphStatusToStorageStatus(either.right().value());
483 return Either.right(storageStatus);
485 return Either.left(either.left().value());
488 public Either<PropertyData, JanusGraphOperationStatus> updatePropertyFromGraph(String propertyId, PropertyDefinition propertyDefinition) {
489 if (log.isDebugEnabled())
490 log.debug("Before updating property on graph {}", propertyId);
492 // get the original property data
493 Either<PropertyData, JanusGraphOperationStatus> statusProperty = janusGraphGenericDao
494 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.Property), propertyId, PropertyData.class);
495 if (statusProperty.isRight()) {
496 log.debug("Problem while get property with id {}. Reason - {}", propertyId, statusProperty.right().value().name());
497 return Either.right(statusProperty.right().value());
499 PropertyData orgPropertyData = statusProperty.left().value();
500 PropertyDataDefinition orgPropertyDataDefinition = orgPropertyData.getPropertyDataDefinition();
502 // create new property data to update
503 PropertyData newPropertyData = new PropertyData();
504 newPropertyData.setPropertyDataDefinition(propertyDefinition);
505 PropertyDataDefinition newPropertyDataDefinition = newPropertyData.getPropertyDataDefinition();
507 // update the original property data with new values
508 if (orgPropertyDataDefinition.getDefaultValue() == null) {
509 orgPropertyDataDefinition.setDefaultValue(newPropertyDataDefinition.getDefaultValue());
511 if (!orgPropertyDataDefinition.getDefaultValue().equals(newPropertyDataDefinition.getDefaultValue())) {
512 orgPropertyDataDefinition.setDefaultValue(newPropertyDataDefinition.getDefaultValue());
515 if (orgPropertyDataDefinition.getDescription() == null) {
516 orgPropertyDataDefinition.setDescription(newPropertyDataDefinition.getDescription());
518 if (!orgPropertyDataDefinition.getDescription().equals(newPropertyDataDefinition.getDescription())) {
519 orgPropertyDataDefinition.setDescription(newPropertyDataDefinition.getDescription());
522 if (!orgPropertyDataDefinition.getType().equals(newPropertyDataDefinition.getType())) {
523 orgPropertyDataDefinition.setType(newPropertyDataDefinition.getType());
525 if (newPropertyData.getConstraints() != null) {
526 orgPropertyData.setConstraints(newPropertyData.getConstraints());
528 orgPropertyDataDefinition.setSchema(newPropertyDataDefinition.getSchema());
530 return janusGraphGenericDao.updateNode(orgPropertyData, PropertyData.class);
536 * @param janusGraphGenericDao
538 public void setJanusGraphGenericDao(HealingJanusGraphGenericDao janusGraphGenericDao) {
539 this.janusGraphGenericDao = janusGraphGenericDao;
542 public Either<PropertyData, JanusGraphOperationStatus> addPropertyToNodeType(String propertyName, PropertyDefinition propertyDefinition, NodeTypeEnum nodeType, String uniqueId) {
544 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
546 propertyDefinition.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(uniqueId, propertyName));
547 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
549 if (log.isDebugEnabled())
550 log.debug(BEFORE_ADDING_PROPERTY_TO_GRAPH, propertyData);
551 Either<PropertyData, JanusGraphOperationStatus> createNodeResult = janusGraphGenericDao
552 .createNode(propertyData, PropertyData.class);
553 if (log.isDebugEnabled())
554 log.debug(AFTER_ADDING_PROPERTY_TO_GRAPH, propertyData);
555 if (createNodeResult.isRight()) {
556 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
557 log.error("Failed to add property {} to graph. status is {}", propertyName, operationStatus);
558 return Either.right(operationStatus);
561 Map<String, Object> props = new HashMap<>();
562 props.put(GraphPropertiesDictionary.NAME.getProperty(), propertyName);
564 UniqueIdData uniqueIdData = new UniqueIdData(nodeType, uniqueId);
565 log.debug("Before associating {} to property {}", uniqueIdData, propertyName);
566 Either<GraphRelation, JanusGraphOperationStatus> createRelResult = janusGraphGenericDao
567 .createRelation(uniqueIdData, propertyData, GraphEdgeLabels.PROPERTY, props);
568 if (createRelResult.isRight()) {
569 JanusGraphOperationStatus operationStatus = createNodeResult.right().value();
570 log.error(FAILED_TO_ASSOCIATE_RESOURCE_TO_PROPERTY_IN_GRAPH_STATUS_IS, uniqueId, propertyName, operationStatus);
571 return Either.right(operationStatus);
574 return Either.left(createNodeResult.left().value());
578 public Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode(NodeTypeEnum nodeType, String uniqueId) {
580 Map<String, PropertyDefinition> resourceProps = new HashMap<>();
582 Either<List<ImmutablePair<PropertyData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
583 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.PROPERTY, NodeTypeEnum.Property,
586 if (childrenNodes.isRight()) {
587 JanusGraphOperationStatus operationStatus = childrenNodes.right().value();
588 return Either.right(operationStatus);
591 List<ImmutablePair<PropertyData, GraphEdge>> values = childrenNodes.left().value();
592 if (values != null) {
594 for (ImmutablePair<PropertyData, GraphEdge> immutablePair : values) {
595 GraphEdge edge = immutablePair.getValue();
596 String propertyName = (String) edge.getProperties().get(GraphPropertiesDictionary.NAME.getProperty());
597 log.debug("Property {} is associated to node {}", propertyName, uniqueId);
598 PropertyData propertyData = immutablePair.getKey();
599 PropertyDefinition propertyDefinition = this.convertPropertyDataToPropertyDefinition(propertyData, propertyName, uniqueId);
600 resourceProps.put(propertyName, propertyDefinition);
605 log.debug("The properties associated to node {} are {}", uniqueId, resourceProps);
606 return Either.left(resourceProps);
609 public Either<Map<String, PropertyDefinition>, StorageOperationStatus> deletePropertiesAssociatedToNode(NodeTypeEnum nodeType, String uniqueId) {
610 return deleteAllPropertiesAssociatedToNode(nodeType, uniqueId)
612 .bind(err -> err == StorageOperationStatus.OK ? Either.left(Collections.emptyMap()) : Either.right(err));
615 public Either<Map<String, PropertyData>, JanusGraphOperationStatus> mergePropertiesAssociatedToNode(NodeTypeEnum nodeType, String uniqueId, Map<String, PropertyDefinition> newProperties) {
616 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> oldPropertiesRes = findPropertiesOfNode(nodeType, uniqueId);
618 Map<String, PropertyDefinition> reallyNewProperties;
619 Map<String, PropertyData> unchangedPropsData;
621 if (oldPropertiesRes.isRight()) {
622 JanusGraphOperationStatus err = oldPropertiesRes.right().value();
623 if (err == JanusGraphOperationStatus.NOT_FOUND) {
624 reallyNewProperties = newProperties;
625 unchangedPropsData = Collections.emptyMap();
628 return Either.right(err);
632 Map<String, PropertyDefinition> oldProperties = oldPropertiesRes.left().value();
633 reallyNewProperties = collectReallyNewProperties(newProperties, oldProperties);
635 for(Entry<String, PropertyDefinition> oldEntry: oldProperties.entrySet()) {
636 String key = oldEntry.getKey();
637 PropertyDefinition newPropDef = newProperties != null? newProperties.get(key): null;
638 PropertyDefinition oldPropDef = oldEntry.getValue();
640 JanusGraphOperationStatus status = updateOldProperty(newPropDef, oldPropDef);
641 if (status != JanusGraphOperationStatus.OK) {
642 return Either.right(status);
645 unchangedPropsData = oldProperties.entrySet().stream()
646 .collect(Collectors.toMap(Entry::getKey, e-> new PropertyData(e.getValue(), null)));
650 // add other properties
651 return addPropertiesToElementType(nodeType, uniqueId, reallyNewProperties, unchangedPropsData);
655 * @param newProperties
656 * @param oldProperties
659 private Map<String, PropertyDefinition> collectReallyNewProperties(Map<String, PropertyDefinition> newProperties, Map<String, PropertyDefinition> oldProperties) {
660 return newProperties != null? newProperties.entrySet().stream()
661 .filter(entry -> !oldProperties.containsKey(entry.getKey()))
662 .collect(Collectors.toMap(Entry::getKey, Entry::getValue) ): null;
669 private JanusGraphOperationStatus updateOldProperty(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
670 if (!isUpdateAllowed(newPropDef, oldPropDef)) {
671 return JanusGraphOperationStatus.MATCH_NOT_FOUND;
674 if (isUpdateRequired(newPropDef, oldPropDef)) {
675 modifyOldPropByNewOne(newPropDef, oldPropDef);
677 List<PropertyConstraint> constraints = oldPropDef.getConstraints();
678 PropertyData node = new PropertyData(oldPropDef, convertConstraintsToString(constraints));
679 Either<PropertyData, JanusGraphOperationStatus> updateResult = janusGraphGenericDao
680 .updateNode(node, PropertyData.class);
682 if (updateResult.isRight()) {
683 return updateResult.right().value();
687 return JanusGraphOperationStatus.OK;
694 private boolean isUpdateAllowed(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
695 if (newPropDef == null) {
696 log.error("#mergePropertiesAssociatedToNode - Failed due attempt to delete the property with id {}", oldPropDef.getUniqueId());
700 // If the property type is missing it's something that we could want to fix
701 if ( oldPropDef.getType() != null && !oldPropDef.getType().equals(newPropDef.getType())) {
702 log.error("#mergePropertiesAssociatedToNode - Failed due attempt to change type of the property with id {}", oldPropDef.getUniqueId());
710 * Update only fields which modification is permitted.
714 private void modifyOldPropByNewOne(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
715 oldPropDef.setDefaultValue(newPropDef.getDefaultValue());
716 oldPropDef.setDescription(newPropDef.getDescription());
717 oldPropDef.setRequired(newPropDef.isRequired());
719 // Type is updated to fix possible null type issue in janusGraph DB
720 oldPropDef.setType(newPropDef.getType());
724 private boolean isUpdateRequired(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
725 return !StringUtils.equals(oldPropDef.getDefaultValue(), newPropDef.getDefaultValue()) ||
726 !StringUtils.equals(oldPropDef.getDescription(), newPropDef.getDescription()) ||
727 oldPropDef.isRequired() != newPropDef.isRequired();
731 * Adds newProperties and returns in case of success (left part of Either)
732 * map of all properties i. e. added ones and contained in unchangedPropsData
735 * @param newProperties
736 * @param unchangedPropsData
739 private Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToElementType(NodeTypeEnum nodeType, String uniqueId, Map<String, PropertyDefinition> newProperties, Map<String, PropertyData> unchangedPropsData) {
740 return addPropertiesToElementType(uniqueId, nodeType, newProperties)
743 m.putAll(unchangedPropsData);
749 public Either<Map<String, PropertyDefinition>, StorageOperationStatus> deleteAllPropertiesAssociatedToNode(NodeTypeEnum nodeType, String uniqueId) {
751 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesOfNodeRes = findPropertiesOfNode(nodeType, uniqueId);
753 if (propertiesOfNodeRes.isRight()) {
754 JanusGraphOperationStatus status = propertiesOfNodeRes.right().value();
755 if (status == JanusGraphOperationStatus.NOT_FOUND) {
756 return Either.right(StorageOperationStatus.OK);
758 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
761 Map<String, PropertyDefinition> value = propertiesOfNodeRes.left().value();
762 for (PropertyDefinition propertyDefinition : value.values()) {
764 String propertyUid = propertyDefinition.getUniqueId();
765 Either<PropertyData, JanusGraphOperationStatus> deletePropertyRes = deletePropertyFromGraph(propertyUid);
766 if (deletePropertyRes.isRight()) {
767 log.error("Failed to delete property with id {}", propertyUid);
768 JanusGraphOperationStatus status = deletePropertyRes.right().value();
769 if (status == JanusGraphOperationStatus.NOT_FOUND) {
770 status = JanusGraphOperationStatus.INVALID_ID;
772 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
777 log.debug("The properties deleted from node {} are {}", uniqueId, value);
778 return Either.left(value);
782 * Checks existence of a property with the same name belonging to the same resource
783 * or existence of property with the same name and different type (including derived from hierarchy)
786 * @param propertyName
787 * @param propertyType
790 public boolean isPropertyExist(List<PropertyDefinition> properties, String resourceUid, String propertyName, String propertyType) {
791 boolean result = false;
792 if (!CollectionUtils.isEmpty(properties)) {
793 for (PropertyDefinition propertyDefinition : properties) {
795 if ( propertyDefinition.getName().equals(propertyName) &&
796 (propertyDefinition.getParentUniqueId().equals(resourceUid) || !propertyDefinition.getType().equals(propertyType)) ) {
805 public ImmutablePair<String, Boolean> validateAndUpdateRules(String propertyType, List<PropertyRule> rules, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean isValidate) {
807 if (rules == null || rules.isEmpty()) {
808 return new ImmutablePair<>(null, true);
811 for (PropertyRule rule : rules) {
812 String value = rule.getValue();
813 Either<Object, Boolean> updateResult = validateAndUpdatePropertyValue(propertyType, value, isValidate, innerType, dataTypes);
814 if (updateResult.isRight()) {
815 Boolean status = updateResult.right().value();
817 return new ImmutablePair<>(value, status);
820 String newValue = null;
821 Object object = updateResult.left().value();
822 if (object != null) {
823 newValue = object.toString();
825 rule.setValue(newValue);
829 return new ImmutablePair<>(null, true);
832 public void addRulesToNewPropertyValue(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty, String resourceInstanceId) {
834 List<PropertyRule> rules = resourceInstanceProperty.getRules();
836 PropertyRule propertyRule = buildRuleFromPath(propertyValueData, resourceInstanceProperty, resourceInstanceId);
837 rules = new ArrayList<>();
838 rules.add(propertyRule);
840 rules = sortRules(rules);
843 propertyValueData.setRules(rules);
846 private PropertyRule buildRuleFromPath(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty, String resourceInstanceId) {
847 List<String> path = resourceInstanceProperty.getPath();
848 // FOR BC. Since old Property values on VFC/VF does not have rules on
850 // Update could be done on one level only, thus we can use this
851 // operation to avoid migration.
852 if (path == null || path.isEmpty()) {
853 path = new ArrayList<>();
854 path.add(resourceInstanceId);
856 PropertyRule propertyRule = new PropertyRule();
857 propertyRule.setRule(path);
858 propertyRule.setValue(propertyValueData.getValue());
862 private List<PropertyRule> sortRules(List<PropertyRule> rules) {
864 // TODO: sort the rules by size and binary representation.
865 // (x, y, .+) --> 110 6 priority 1
866 // (x, .+, z) --> 101 5 priority 2
871 public ImmutablePair<JanusGraphOperationStatus, String> findPropertyValue(String resourceInstanceId, String propertyId) {
873 log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
875 Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> getAllRes = this.getAllPropertiesOfResourceInstanceOnlyPropertyDefId(resourceInstanceId);
876 if (getAllRes.isRight()) {
877 JanusGraphOperationStatus status = getAllRes.right().value();
878 log.trace("After fetching all properties of resource instance {}. Status is {}", resourceInstanceId, status);
879 return new ImmutablePair<>(status, null);
882 List<ComponentInstanceProperty> list = getAllRes.left().value();
884 for (ComponentInstanceProperty instanceProperty : list) {
885 String propertyUniqueId = instanceProperty.getUniqueId();
886 String valueUniqueUid = instanceProperty.getValueUniqueUid();
887 log.trace("Go over property {} under resource instance {}. valueUniqueId = {}", propertyUniqueId, resourceInstanceId, valueUniqueUid);
888 if (propertyId.equals(propertyUniqueId) && valueUniqueUid != null) {
889 log.debug("The property {} already created under resource instance {}", propertyId, resourceInstanceId);
890 return new ImmutablePair<>(JanusGraphOperationStatus.ALREADY_EXIST, valueUniqueUid);
895 return new ImmutablePair<>(JanusGraphOperationStatus.NOT_FOUND, null);
899 public void updateRulesInPropertyValue(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty, String resourceInstanceId) {
901 List<PropertyRule> currentRules = propertyValueData.getRules();
903 List<PropertyRule> rules = resourceInstanceProperty.getRules();
904 // if rules are not supported.
907 PropertyRule propertyRule = buildRuleFromPath(propertyValueData, resourceInstanceProperty, resourceInstanceId);
908 rules = new ArrayList<>();
909 rules.add(propertyRule);
911 if (currentRules != null) {
912 rules = mergeRules(currentRules, rules);
916 // Full mode. all rules are sent in update operation.
917 rules = sortRules(rules);
920 propertyValueData.setRules(rules);
924 private List<PropertyRule> mergeRules(List<PropertyRule> currentRules, List<PropertyRule> newRules) {
926 List<PropertyRule> mergedRules = new ArrayList<>();
928 if (newRules == null || newRules.isEmpty()) {
932 for (PropertyRule rule : currentRules) {
933 PropertyRule propertyRule = new PropertyRule(rule.getRule(), rule.getValue());
934 mergedRules.add(propertyRule);
937 for (PropertyRule rule : newRules) {
938 PropertyRule foundRule = findRuleInList(rule, mergedRules);
939 if (foundRule != null) {
940 foundRule.setValue(rule.getValue());
942 mergedRules.add(rule);
949 private PropertyRule findRuleInList(PropertyRule rule, List<PropertyRule> rules) {
951 if (rules == null || rules.isEmpty() || rule.getRule() == null || rule.getRule().isEmpty()) {
955 PropertyRule foundRule = null;
956 for (PropertyRule propertyRule : rules) {
957 if (rule.getRuleSize() != propertyRule.getRuleSize()) {
960 boolean equals = propertyRule.compareRule(rule);
962 foundRule = propertyRule;
971 * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
973 * @param resourceInstanceUid
976 public Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> getAllPropertiesOfResourceInstanceOnlyPropertyDefId(String resourceInstanceUid) {
978 return getAllPropertiesOfResourceInstanceOnlyPropertyDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
982 public Either<PropertyValueData, JanusGraphOperationStatus> removePropertyOfResourceInstance(String propertyValueUid, String resourceInstanceId) {
984 Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
985 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), resourceInstanceId, ComponentInstanceData.class);
987 if (findResInstanceRes.isRight()) {
988 JanusGraphOperationStatus status = findResInstanceRes.right().value();
989 if (status == JanusGraphOperationStatus.NOT_FOUND) {
990 status = JanusGraphOperationStatus.INVALID_ID;
992 return Either.right(status);
995 Either<PropertyValueData, JanusGraphOperationStatus> findPropertyDefRes = janusGraphGenericDao
996 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.PropertyValue), propertyValueUid, PropertyValueData.class);
998 if (findPropertyDefRes.isRight()) {
999 JanusGraphOperationStatus status = findPropertyDefRes.right().value();
1000 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1001 status = JanusGraphOperationStatus.INVALID_ID;
1003 return Either.right(status);
1006 Either<GraphRelation, JanusGraphOperationStatus> relation = janusGraphGenericDao
1007 .getRelation(findResInstanceRes.left().value(), findPropertyDefRes.left().value(), GraphEdgeLabels.PROPERTY_VALUE);
1008 if (relation.isRight()) {
1009 // TODO: add error in case of error
1010 JanusGraphOperationStatus status = relation.right().value();
1011 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1012 status = JanusGraphOperationStatus.INVALID_ID;
1014 return Either.right(status);
1017 Either<PropertyValueData, JanusGraphOperationStatus> deleteNode = janusGraphGenericDao
1018 .deleteNode(findPropertyDefRes.left().value(), PropertyValueData.class);
1019 if (deleteNode.isRight()) {
1020 return Either.right(deleteNode.right().value());
1022 PropertyValueData value = deleteNode.left().value();
1023 return Either.left(value);
1027 public Either<ComponentInstanceProperty, StorageOperationStatus> removePropertyValueFromResourceInstance(String propertyValueUid, String resourceInstanceId, boolean inTransaction) {
1029 Either<ComponentInstanceProperty, StorageOperationStatus> result = null;
1033 Either<PropertyValueData, JanusGraphOperationStatus> eitherStatus = this.removePropertyOfResourceInstance(propertyValueUid, resourceInstanceId);
1035 if (eitherStatus.isRight()) {
1036 log.error("Failed to remove property value {} from resource instance {} in Graph. status is {}", propertyValueUid, resourceInstanceId, eitherStatus.right().value().name());
1037 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherStatus.right().value()));
1040 PropertyValueData propertyValueData = eitherStatus.left().value();
1042 ComponentInstanceProperty propertyValueResult = new ComponentInstanceProperty();
1043 propertyValueResult.setUniqueId(resourceInstanceId);
1044 propertyValueResult.setValue(propertyValueData.getValue());
1046 log.debug("The returned ResourceInstanceProperty is {}", propertyValueResult);
1047 result = Either.left(propertyValueResult);
1053 if (!inTransaction) {
1054 if (result == null || result.isRight()) {
1055 log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1056 janusGraphGenericDao.rollback();
1058 log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1059 janusGraphGenericDao.commit();
1066 public ComponentInstanceProperty buildResourceInstanceProperty(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty) {
1068 String value = propertyValueData.getValue();
1069 String uid = propertyValueData.getUniqueId();
1070 ComponentInstanceProperty instanceProperty = new ComponentInstanceProperty(resourceInstanceProperty, value, uid);
1071 instanceProperty.setPath(resourceInstanceProperty.getPath());
1073 return instanceProperty;
1077 public static class PropertyConstraintJacksonDeserializer extends com.fasterxml.jackson.databind.JsonDeserializer<PropertyConstraint> {
1080 public PropertyConstraint deserialize(com.fasterxml.jackson.core.JsonParser json, DeserializationContext context) throws IOException {
1081 ObjectCodec oc = json.getCodec();
1082 JsonNode node = oc.readTree(json);
1088 public boolean isPropertyDefaultValueValid(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
1089 if (propertyDefinition == null) {
1092 String innerType = null;
1093 String propertyType = propertyDefinition.getType();
1094 ToscaPropertyType type = getType(propertyType);
1095 if (type == ToscaPropertyType.LIST || type == ToscaPropertyType.MAP) {
1096 SchemaDefinition def = propertyDefinition.getSchema();
1100 PropertyDataDefinition propDef = def.getProperty();
1101 if (propDef == null) {
1104 innerType = propDef.getType();
1106 String value = propertyDefinition.getDefaultValue();
1108 return isValidValue(type, value, innerType, dataTypes);
1110 log.trace("The given type {} is not a pre defined one.", propertyType);
1112 DataTypeDefinition foundDt = dataTypes.get(propertyType);
1113 if (foundDt != null) {
1114 return isValidComplexValue(foundDt, value, dataTypes);
1121 public boolean isPropertyTypeValid(IComplexDefaultValue property) {
1123 if (property == null) {
1127 if (ToscaPropertyType.isValidType(property.getType()) == null) {
1129 Either<Boolean, JanusGraphOperationStatus> definedInDataTypes = isDefinedInDataTypes(property.getType());
1131 if (definedInDataTypes.isRight()) {
1134 Boolean isExist = definedInDataTypes.left().value();
1135 return isExist.booleanValue();
1143 public ImmutablePair<String, Boolean> isPropertyInnerTypeValid(IComplexDefaultValue property, Map<String, DataTypeDefinition> dataTypes) {
1145 if (property == null) {
1146 return new ImmutablePair<>(null, false);
1149 SchemaDefinition schema;
1150 PropertyDataDefinition innerProp;
1151 String innerType = null;
1152 if ((schema = property.getSchema()) != null) {
1153 if ((innerProp = schema.getProperty()) != null) {
1154 innerType = innerProp.getType();
1158 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
1160 if (innerToscaType == null) {
1161 DataTypeDefinition dataTypeDefinition = dataTypes.get(innerType);
1162 if (dataTypeDefinition == null) {
1163 log.debug("The inner type {} is not a data type.", innerType);
1164 return new ImmutablePair<>(innerType, false);
1166 log.debug("The inner type {} is a data type. Data type definition is {}", innerType, dataTypeDefinition);
1170 return new ImmutablePair<>(innerType, true);
1173 private boolean isValidComplexValue(DataTypeDefinition foundDt, String value, Map<String, DataTypeDefinition> dataTypes) {
1174 ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter.validateAndUpdate(value, foundDt, dataTypes);
1176 log.trace("The result after validating complex value of type {} is {}", foundDt.getName(), validateAndUpdate);
1178 return validateAndUpdate.right.booleanValue();
1182 public Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> getAllPropertiesOfResourceInstanceOnlyPropertyDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
1184 Either<JanusGraphVertex, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
1185 .getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid);
1187 if (findResInstanceRes.isRight()) {
1188 JanusGraphOperationStatus status = findResInstanceRes.right().value();
1189 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1190 status = JanusGraphOperationStatus.INVALID_ID;
1192 return Either.right(status);
1195 Either<List<ImmutablePair<JanusGraphVertex, Edge>>, JanusGraphOperationStatus> propertyImplNodes = janusGraphGenericDao
1196 .getChildrenVertecies(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.PROPERTY_VALUE);
1198 if (propertyImplNodes.isRight()) {
1199 JanusGraphOperationStatus status = propertyImplNodes.right().value();
1200 return Either.right(status);
1203 List<ImmutablePair<JanusGraphVertex, Edge>> list = propertyImplNodes.left().value();
1204 if (list == null || list.isEmpty()) {
1205 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
1208 List<ComponentInstanceProperty> result = new ArrayList<>();
1209 for (ImmutablePair<JanusGraphVertex, Edge> propertyValue : list) {
1210 JanusGraphVertex propertyValueDataVertex = propertyValue.getLeft();
1211 String propertyValueUid = (String) janusGraphGenericDao
1212 .getProperty(propertyValueDataVertex, GraphPropertiesDictionary.UNIQUE_ID.getProperty());
1213 String value = (String) janusGraphGenericDao
1214 .getProperty(propertyValueDataVertex, GraphPropertiesDictionary.VALUE.getProperty());
1216 ImmutablePair<JanusGraphVertex, Edge> propertyDefPair = janusGraphGenericDao
1217 .getChildVertex(propertyValueDataVertex, GraphEdgeLabels.PROPERTY_IMPL);
1218 if (propertyDefPair == null) {
1219 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
1222 Map<String, Object> properties = janusGraphGenericDao.getProperties(propertyValueDataVertex);
1223 PropertyValueData propertyValueData = GraphElementFactory.createElement(NodeTypeEnum.PropertyValue.getName(), GraphElementTypeEnum.Node, properties, PropertyValueData.class);
1224 String propertyUniqueId = (String) janusGraphGenericDao
1225 .getProperty(propertyDefPair.left, GraphPropertiesDictionary.UNIQUE_ID.getProperty());
1227 ComponentInstanceProperty resourceInstanceProperty = new ComponentInstanceProperty();
1228 // set property original unique id
1229 resourceInstanceProperty.setUniqueId(propertyUniqueId);
1231 // TODO: esofer add resource id
1232 resourceInstanceProperty.setParentUniqueId(null);
1234 resourceInstanceProperty.setValue(value);
1235 // set property value unique id
1236 resourceInstanceProperty.setValueUniqueUid(propertyValueUid);
1238 resourceInstanceProperty.setRules(propertyValueData.getRules());
1240 result.add(resourceInstanceProperty);
1243 return Either.left(result);
1247 * Find the default value from the list of component instances. Start the search from the second component instance
1249 * @param pathOfComponentInstances
1250 * @param propertyUniqueId
1251 * @param defaultValue
1254 public Either<String, JanusGraphOperationStatus> findDefaultValueFromSecondPosition(List<String> pathOfComponentInstances, String propertyUniqueId, String defaultValue) {
1256 log.trace("In find default value: path= {} propertyUniqId={} defaultValue= {}", pathOfComponentInstances, propertyUniqueId, defaultValue);
1258 if (pathOfComponentInstances == null || pathOfComponentInstances.size() < 2) {
1259 return Either.left(defaultValue);
1262 String result = defaultValue;
1264 for (int i = 1; i < pathOfComponentInstances.size(); i++) {
1265 String compInstanceId = pathOfComponentInstances.get(i);
1267 Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> propertyValuesResult = this.getAllPropertiesOfResourceInstanceOnlyPropertyDefId(compInstanceId, NodeTypeEnum.ResourceInstance);
1269 log.trace("After fetching properties values of component instance {}. {}", compInstanceId, propertyValuesResult);
1271 if (propertyValuesResult.isRight()) {
1272 JanusGraphOperationStatus status = propertyValuesResult.right().value();
1273 if (status != JanusGraphOperationStatus.NOT_FOUND) {
1274 return Either.right(status);
1280 ComponentInstanceProperty foundCompInstanceProperty = fetchByPropertyUid(propertyValuesResult.left().value(), propertyUniqueId);
1281 log.trace("After finding the component instance property on{} . {}", compInstanceId, foundCompInstanceProperty);
1283 if (foundCompInstanceProperty == null) {
1287 List<PropertyRule> rules = getOrBuildRulesIfNotExists(pathOfComponentInstances.size() - i, pathOfComponentInstances.get(i), foundCompInstanceProperty.getRules(), foundCompInstanceProperty.getValue());
1289 log.trace("Rules of property {} on component instance {} are {}", propertyUniqueId, compInstanceId, rules);
1290 PropertyRule matchedRule = findMatchRule(pathOfComponentInstances, i, rules);
1291 log.trace("Match rule is {}", matchedRule);
1293 if (matchedRule != null) {
1294 result = matchedRule.getValue();
1300 return Either.left(result);
1304 private ComponentInstanceProperty fetchByPropertyUid(List<ComponentInstanceProperty> list, String propertyUniqueId) {
1306 ComponentInstanceProperty result = null;
1312 for (ComponentInstanceProperty instProperty : list) {
1313 if (instProperty.getUniqueId().equals(propertyUniqueId)) {
1314 result = instProperty;
1322 private List<PropertyRule> getOrBuildRulesIfNotExists(int ruleSize, String compInstanceId, List<PropertyRule> rules, String value) {
1324 if (rules != null) {
1328 rules = buildDefaultRule(compInstanceId, ruleSize, value);
1334 private List<PropertyRule> getRulesOfPropertyValue(int size, String instanceId, ComponentInstanceProperty componentInstanceProperty) {
1335 List<PropertyRule> rules = componentInstanceProperty.getRules();
1336 if (rules == null) {
1337 rules = buildDefaultRule(instanceId, size, componentInstanceProperty.getValue());
1342 private List<PropertyRule> buildDefaultRule(String componentInstanceId, int size, String value) {
1344 List<PropertyRule> rules = new ArrayList<>();
1345 List<String> rule = new ArrayList<>();
1346 rule.add(componentInstanceId);
1347 for (int i = 0; i < size - 1; i++) {
1348 rule.add(PropertyRule.getRuleAnyMatch());
1350 PropertyRule propertyRule = new PropertyRule(rule, value);
1351 rules.add(propertyRule);
1357 private PropertyRule findMatchRule(List<String> pathOfInstances, int level, List<PropertyRule> rules) {
1359 PropertyRule propertyRule = null;
1361 String stringForMatch = buildStringForMatch(pathOfInstances, level);
1363 String firstCompInstance = pathOfInstances.get(level);
1365 if (rules != null) {
1367 for (PropertyRule rule : rules) {
1369 int ruleSize = rule.getRule().size();
1370 // check the length of the rule equals to the length of the
1372 if (ruleSize != pathOfInstances.size() - level) {
1375 // check that the rule starts with correct component instance id
1376 if (!checkFirstItem(firstCompInstance, rule.getFirstToken())) {
1380 String secondToken = rule.getToken(2);
1381 if (secondToken != null && (secondToken.equals(PropertyRule.getForceAll()) || secondToken.equals(PropertyRule.getALL()))) {
1382 propertyRule = rule;
1386 String patternStr = buildStringForMatch(rule.getRule(), 0);
1388 Pattern pattern = Pattern.compile(patternStr);
1390 Matcher matcher = pattern.matcher(stringForMatch);
1392 if (matcher.matches()) {
1393 if (log.isTraceEnabled()) {
1394 log.trace("{} matches the rule {}", stringForMatch, patternStr);
1396 propertyRule = rule;
1403 return propertyRule;
1406 private boolean checkFirstItem(String left, String right) {
1407 if (left != null && left.equals(right)) {
1413 private String buildStringForMatch(List<String> pathOfInstances, int level) {
1414 StringBuilder builder = new StringBuilder();
1416 for (int i = level; i < pathOfInstances.size(); i++) {
1417 builder.append(pathOfInstances.get(i));
1418 if (i < pathOfInstances.size() - 1) {
1419 builder.append("#");
1422 return builder.toString();
1425 public void updatePropertyByBestMatch(String propertyUniqueId, ComponentInstanceProperty instanceProperty, Map<String, ComponentInstanceProperty> instanceIdToValue) {
1427 List<String> pathOfInstances = instanceProperty.getPath();
1429 int size = pathOfInstances.size();
1430 int numberOfMatches = 0;
1431 for (String instanceId : pathOfInstances) {
1432 ComponentInstanceProperty componentInstanceProperty = instanceIdToValue.get(instanceId);
1434 if (componentInstanceProperty != null) {
1436 List<PropertyRule> rules = getRulesOfPropertyValue(size - level, instanceId, componentInstanceProperty);
1437 // If it is the first level instance, then update valueUniuqeId
1438 // parameter in order to know on update that
1439 // we should update and not create new node on graph.
1441 instanceProperty.setValueUniqueUid(componentInstanceProperty.getValueUniqueUid());
1442 instanceProperty.setRules(rules);
1445 PropertyRule rule = findMatchRule(pathOfInstances, level, rules);
1448 String value = rule.getValue();
1449 if (numberOfMatches == 1) {
1450 instanceProperty.setValue(value);
1451 if (log.isDebugEnabled()) {
1452 log.debug("Set the value of property {} {} on path {} to be {}", propertyUniqueId, instanceProperty.getName(), pathOfInstances, value);
1454 } else if (numberOfMatches == 2) {
1455 // In case of another property value match, then use the
1456 // value to be the default value of the property.
1457 instanceProperty.setDefaultValue(value);
1458 if (log.isDebugEnabled()) {
1459 log.debug("Set the default value of property {} {} on path {} to be {}", propertyUniqueId, instanceProperty.getName(), pathOfInstances, value);
1472 * Add data type to graph.
1474 * 1. Add data type node
1476 * 2. Add edge between the former node to its parent(if exists)
1478 * 3. Add property node and associate it to the node created at #1. (per property & if exists)
1480 * @param dataTypeDefinition
1483 private Either<DataTypeData, JanusGraphOperationStatus> addDataTypeToGraph(DataTypeDefinition dataTypeDefinition) {
1485 log.debug("Got data type {}", dataTypeDefinition);
1487 String dtUniqueId = UniqueIdBuilder.buildDataTypeUid(dataTypeDefinition.getName());
1489 DataTypeData dataTypeData = buildDataTypeData(dataTypeDefinition, dtUniqueId);
1491 log.debug("Before adding data type to graph. dataTypeData = {}", dataTypeData);
1492 Either<DataTypeData, JanusGraphOperationStatus> createDataTypeResult = janusGraphGenericDao
1493 .createNode(dataTypeData, DataTypeData.class);
1494 log.debug("After adding data type to graph. status is = {}", createDataTypeResult);
1496 if (createDataTypeResult.isRight()) {
1497 JanusGraphOperationStatus operationStatus = createDataTypeResult.right().value();
1498 log.debug("Failed to data type {} to graph. status is {}", dataTypeDefinition.getName(), operationStatus);
1499 BeEcompErrorManager.getInstance().logBeFailedAddingNodeTypeError("AddDataType", NodeTypeEnum.DataType.getName());
1500 return Either.right(operationStatus);
1503 DataTypeData resultCTD = createDataTypeResult.left().value();
1504 List<PropertyDefinition> properties = dataTypeDefinition.getProperties();
1505 Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType(resultCTD.getUniqueId(), properties);
1506 if (addPropertiesToDataType.isRight()) {
1507 log.debug("Failed add properties {} to data type {}", properties, dataTypeDefinition.getName());
1508 return Either.right(addPropertiesToDataType.right().value());
1511 String derivedFrom = dataTypeDefinition.getDerivedFromName();
1512 if (derivedFrom != null) {
1513 log.debug("Before creating relation between data type {} to its parent {}", dtUniqueId, derivedFrom);
1514 UniqueIdData from = new UniqueIdData(NodeTypeEnum.DataType, dtUniqueId);
1516 String deriveFromUid = UniqueIdBuilder.buildDataTypeUid(derivedFrom);
1517 UniqueIdData to = new UniqueIdData(NodeTypeEnum.DataType, deriveFromUid);
1518 Either<GraphRelation, JanusGraphOperationStatus> createRelation = janusGraphGenericDao
1519 .createRelation(from, to, GraphEdgeLabels.DERIVED_FROM, null);
1520 log.debug("After create relation between capability type {} to its parent {}. status is {}", dtUniqueId, derivedFrom, createRelation);
1521 if (createRelation.isRight()) {
1522 return Either.right(createRelation.right().value());
1526 return Either.left(createDataTypeResult.left().value());
1530 private DataTypeData buildDataTypeData(DataTypeDefinition dataTypeDefinition, String ctUniqueId) {
1532 DataTypeData dataTypeData = new DataTypeData(dataTypeDefinition);
1534 dataTypeData.getDataTypeDataDefinition().setUniqueId(ctUniqueId);
1535 Long creationDate = dataTypeData.getDataTypeDataDefinition().getCreationTime();
1536 if (creationDate == null) {
1537 creationDate = System.currentTimeMillis();
1539 dataTypeData.getDataTypeDataDefinition().setCreationTime(creationDate);
1540 dataTypeData.getDataTypeDataDefinition().setModificationTime(creationDate);
1542 return dataTypeData;
1546 * add properties to capability type.
1548 * Per property, add a property node and associate it to the capability type
1554 private Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType(String uniqueId, List<PropertyDefinition> properties) {
1556 Map<String, PropertyData> propertiesData = new HashMap<>();
1558 if (properties != null && !properties.isEmpty()) {
1559 for (PropertyDefinition propertyDefinition : properties) {
1560 String propertyName = propertyDefinition.getName();
1562 String propertyType = propertyDefinition.getType();
1563 Either<Boolean, JanusGraphOperationStatus> validPropertyType = isValidPropertyType(propertyType);
1564 if (validPropertyType.isRight()) {
1565 log.debug("Data type {} contains invalid property type {}", uniqueId, propertyType);
1566 return Either.right(validPropertyType.right().value());
1568 Boolean isValid = validPropertyType.left().value();
1569 if (isValid == null || !isValid.booleanValue()) {
1570 log.debug("Data type {} contains invalid property type {}", uniqueId, propertyType);
1571 return Either.right(JanusGraphOperationStatus.INVALID_TYPE);
1574 Either<PropertyData, JanusGraphOperationStatus> addPropertyToNodeType = this.addPropertyToNodeType(propertyName, propertyDefinition, NodeTypeEnum.DataType, uniqueId);
1575 if (addPropertyToNodeType.isRight()) {
1576 JanusGraphOperationStatus operationStatus = addPropertyToNodeType.right().value();
1577 log.debug("Failed to associate data type {} to property {} in graph. status is {}", uniqueId, propertyName, operationStatus);
1578 BeEcompErrorManager.getInstance().logInternalFlowError("AddPropertyToDataType", "Failed to associate property to data type. Status is " + operationStatus, ErrorSeverity.ERROR);
1579 return Either.right(operationStatus);
1581 propertiesData.put(propertyName, addPropertyToNodeType.left().value());
1584 DataTypeData dataTypeData = new DataTypeData();
1585 dataTypeData.getDataTypeDataDefinition().setUniqueId(uniqueId);
1586 long modificationTime = System.currentTimeMillis();
1587 dataTypeData.getDataTypeDataDefinition().setModificationTime(modificationTime);
1589 Either<DataTypeData, JanusGraphOperationStatus> updateNode = janusGraphGenericDao
1590 .updateNode(dataTypeData, DataTypeData.class);
1591 if (updateNode.isRight()) {
1592 JanusGraphOperationStatus operationStatus = updateNode.right().value();
1593 log.debug("Failed to update modification time data type {} from graph. status is {}", uniqueId, operationStatus);
1594 BeEcompErrorManager.getInstance().logInternalFlowError("AddPropertyToDataType", "Failed to fetch data type. Status is " + operationStatus, ErrorSeverity.ERROR);
1595 return Either.right(operationStatus);
1597 log.debug("Update data type uid {}. Set modification time to {}", uniqueId, modificationTime);
1602 return Either.left(propertiesData);
1607 * Build Data type object from graph by unique id
1612 public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUid(String uniqueId) {
1614 Either<DataTypeDefinition, JanusGraphOperationStatus> result = null;
1616 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1617 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1619 if (dataTypesRes.isRight()) {
1620 JanusGraphOperationStatus status = dataTypesRes.right().value();
1621 log.debug(DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS, uniqueId, status);
1622 return Either.right(status);
1625 DataTypeData ctData = dataTypesRes.left().value();
1626 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1628 JanusGraphOperationStatus propertiesStatus = fillProperties(uniqueId, dataTypeDefinition);
1629 if (propertiesStatus != JanusGraphOperationStatus.OK) {
1630 log.error(FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, uniqueId);
1631 return Either.right(propertiesStatus);
1634 Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao
1635 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType,
1636 DataTypeData.class);
1637 log.debug("After retrieving DERIVED_FROM node of {}. status is {}", uniqueId, parentNode);
1638 if (parentNode.isRight()) {
1639 JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value();
1640 if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) {
1641 log.error("Failed to find the parent data type of data type {}. status is {}", uniqueId,
1642 janusGraphOperationStatus);
1643 result = Either.right(janusGraphOperationStatus);
1647 // derived from node was found
1648 ImmutablePair<DataTypeData, GraphEdge> immutablePair = parentNode.left().value();
1649 DataTypeData parentCT = immutablePair.getKey();
1651 String parentUniqueId = parentCT.getUniqueId();
1652 Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(parentUniqueId);
1654 if (dataTypeByUid.isRight()) {
1655 return Either.right(dataTypeByUid.right().value());
1658 DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value();
1660 dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
1663 result = Either.left(dataTypeDefinition);
1668 private JanusGraphOperationStatus fillProperties(String uniqueId, DataTypeDefinition dataTypeDefinition) {
1670 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode = this.findPropertiesOfNode(NodeTypeEnum.DataType, uniqueId);
1671 if (findPropertiesOfNode.isRight()) {
1672 JanusGraphOperationStatus janusGraphOperationStatus = findPropertiesOfNode.right().value();
1673 log.debug("After looking for properties of vertex {}. status is {}", uniqueId,
1674 janusGraphOperationStatus);
1675 if (JanusGraphOperationStatus.NOT_FOUND.equals(janusGraphOperationStatus)) {
1676 return JanusGraphOperationStatus.OK;
1678 return janusGraphOperationStatus;
1681 Map<String, PropertyDefinition> properties = findPropertiesOfNode.left().value();
1682 if (properties != null && !properties.isEmpty()) {
1683 List<PropertyDefinition> listOfProps = new ArrayList<>();
1685 for (Entry<String, PropertyDefinition> entry : properties.entrySet()) {
1686 String propName = entry.getKey();
1687 PropertyDefinition propertyDefinition = entry.getValue();
1688 PropertyDefinition newPropertyDefinition = new PropertyDefinition(propertyDefinition);
1689 newPropertyDefinition.setName(propName);
1690 listOfProps.add(newPropertyDefinition);
1692 dataTypeDefinition.setProperties(listOfProps);
1694 return JanusGraphOperationStatus.OK;
1698 private Either<DataTypeDefinition, StorageOperationStatus> addDataType(DataTypeDefinition dataTypeDefinition, boolean inTransaction) {
1700 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1704 Either<DataTypeData, JanusGraphOperationStatus> eitherStatus = addDataTypeToGraph(dataTypeDefinition);
1706 if (eitherStatus.isRight()) {
1707 log.debug("Failed to add data type {} to Graph. status is {}", dataTypeDefinition, eitherStatus.right().value().name());
1708 BeEcompErrorManager.getInstance().logBeFailedAddingNodeTypeError("AddDataType", "DataType");
1709 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(eitherStatus.right().value()));
1712 DataTypeData capabilityTypeData = eitherStatus.left().value();
1714 DataTypeDefinition dataTypeDefResult = convertDTDataToDTDefinition(capabilityTypeData);
1715 log.debug("The returned CapabilityTypeDefinition is {}", dataTypeDefResult);
1716 result = Either.left(dataTypeDefResult);
1720 if (!inTransaction) {
1721 if (result == null || result.isRight()) {
1722 log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1723 janusGraphGenericDao.rollback();
1725 log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1726 janusGraphGenericDao.commit();
1734 public Either<DataTypeDefinition, StorageOperationStatus> addDataType(DataTypeDefinition dataTypeDefinition) {
1735 return addDataType(dataTypeDefinition, true);
1739 public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, boolean inTransaction) {
1741 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1744 String dtUid = UniqueIdBuilder.buildDataTypeUid(name);
1745 Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUid(dtUid);
1747 if (ctResult.isRight()) {
1748 JanusGraphOperationStatus status = ctResult.right().value();
1749 if (status != JanusGraphOperationStatus.NOT_FOUND) {
1750 log.error("Failed to retrieve information on capability type {} status is {}", name, status);
1752 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(ctResult.right().value()));
1756 result = Either.left(ctResult.left().value());
1760 if (!inTransaction) {
1761 if (result == null || result.isRight()) {
1762 log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1763 janusGraphGenericDao.rollback();
1765 log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1766 janusGraphGenericDao.commit();
1774 public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name) {
1775 return getDataTypeByName(name, true);
1779 public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name) {
1780 return getDataTypeByNameWithoutDerived(name, true);
1783 private Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name, boolean inTransaction) {
1785 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1788 String uid = UniqueIdBuilder.buildDataTypeUid(name);
1789 Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUidWithoutDerivedDataTypes(uid);
1791 if (ctResult.isRight()) {
1792 JanusGraphOperationStatus status = ctResult.right().value();
1793 if (status != JanusGraphOperationStatus.NOT_FOUND) {
1794 log.error("Failed to retrieve information on capability type {} status is {}", name, status);
1796 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(ctResult.right().value()));
1800 result = Either.left(ctResult.left().value());
1804 if (!inTransaction) {
1805 if (result == null || result.isRight()) {
1806 log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1807 janusGraphGenericDao.rollback();
1809 log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1810 janusGraphGenericDao.commit();
1817 public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUidWithoutDerivedDataTypes(String uniqueId) {
1819 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1820 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1822 if (dataTypesRes.isRight()) {
1823 JanusGraphOperationStatus status = dataTypesRes.right().value();
1824 log.debug(DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS, uniqueId, status);
1825 return Either.right(status);
1828 DataTypeData ctData = dataTypesRes.left().value();
1829 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1831 JanusGraphOperationStatus propertiesStatus = fillProperties(uniqueId, dataTypeDefinition);
1832 if (propertiesStatus != JanusGraphOperationStatus.OK) {
1833 log.error(FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, uniqueId);
1834 return Either.right(propertiesStatus);
1837 return Either.left(dataTypeDefinition);
1842 * convert between graph Node object to Java object
1844 * @param dataTypeData
1847 protected DataTypeDefinition convertDTDataToDTDefinition(DataTypeData dataTypeData) {
1848 log.debug("The object returned after create data type is {}", dataTypeData);
1850 return new DataTypeDefinition(dataTypeData.getDataTypeDataDefinition());
1853 private Either<Boolean, JanusGraphOperationStatus> isValidPropertyType(String propertyType) {
1855 if (propertyType == null || propertyType.isEmpty()) {
1856 return Either.left(false);
1859 ToscaPropertyType toscaPropertyType = ToscaPropertyType.isValidType(propertyType);
1860 if (toscaPropertyType == null) {
1861 return isDefinedInDataTypes(propertyType);
1863 return Either.left(true);
1867 public Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(String propertyType) {
1869 String dataTypeUid = UniqueIdBuilder.buildDataTypeUid(propertyType);
1870 Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(dataTypeUid);
1871 if (dataTypeByUid.isRight()) {
1872 JanusGraphOperationStatus status = dataTypeByUid.right().value();
1873 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1874 return Either.left(false);
1876 return Either.right(status);
1879 return Either.left(true);
1883 public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAllDataTypes() {
1885 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
1886 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result = Either.left(dataTypes);
1888 Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypes = janusGraphGenericDao
1889 .getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class);
1890 if (getAllDataTypes.isRight()) {
1891 JanusGraphOperationStatus status = getAllDataTypes.right().value();
1892 if (status != JanusGraphOperationStatus.NOT_FOUND) {
1893 return Either.right(status);
1899 List<DataTypeData> list = getAllDataTypes.left().value();
1902 log.trace("Number of data types to load is {}" , list.size());
1904 List<String> collect = list.stream().map(p -> p.getDataTypeDataDefinition().getName()).collect(Collectors.toList());
1905 log.trace("The data types to load are {}" , collect);
1907 for (DataTypeData dataTypeData : list) {
1909 log.trace("Going to fetch data type {}. uid is {}", dataTypeData.getDataTypeDataDefinition().getName(), dataTypeData.getUniqueId());
1910 Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = this.getAndAddDataTypeByUid(dataTypeData.getUniqueId(), dataTypes);
1911 if (dataTypeByUid.isRight()) {
1912 JanusGraphOperationStatus status = dataTypeByUid.right().value();
1913 if (status == JanusGraphOperationStatus.NOT_FOUND) {
1914 status = JanusGraphOperationStatus.INVALID_ID;
1916 return Either.right(status);
1921 if (log.isTraceEnabled()) {
1922 if (result.isRight()) {
1923 log.trace("After fetching all data types {}" , result);
1925 Map<String, DataTypeDefinition> map = result.left().value();
1927 String types = map.keySet().stream().collect(Collectors.joining(",", "[", "]"));
1928 log.trace("After fetching all data types {} " , types);
1937 * Build Data type object from graph by unique id
1942 private Either<DataTypeDefinition, JanusGraphOperationStatus> getAndAddDataTypeByUid(String uniqueId, Map<String, DataTypeDefinition> allDataTypes) {
1944 Either<DataTypeDefinition, JanusGraphOperationStatus> result = null;
1946 if (allDataTypes.containsKey(uniqueId)) {
1947 return Either.left(allDataTypes.get(uniqueId));
1950 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1951 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1953 if (dataTypesRes.isRight()) {
1954 JanusGraphOperationStatus status = dataTypesRes.right().value();
1955 log.debug(DATA_TYPE_CANNOT_BE_FOUND_IN_GRAPH_STATUS_IS, uniqueId, status);
1956 return Either.right(status);
1959 DataTypeData ctData = dataTypesRes.left().value();
1960 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1962 JanusGraphOperationStatus propertiesStatus = fillProperties(uniqueId, dataTypeDefinition);
1963 if (propertiesStatus != JanusGraphOperationStatus.OK) {
1964 log.error(FAILED_TO_FETCH_PROPERTIES_OF_DATA_TYPE, uniqueId);
1965 return Either.right(propertiesStatus);
1968 allDataTypes.put(dataTypeDefinition.getName(), dataTypeDefinition);
1970 String derivedFrom = dataTypeDefinition.getDerivedFromName();
1971 if (allDataTypes.containsKey(derivedFrom)) {
1972 DataTypeDefinition parentDataTypeDefinition = allDataTypes.get(derivedFrom);
1974 dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
1976 return Either.left(dataTypeDefinition);
1979 Either<ImmutablePair<DataTypeData, GraphEdge>, JanusGraphOperationStatus> parentNode = janusGraphGenericDao
1980 .getChild(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, GraphEdgeLabels.DERIVED_FROM, NodeTypeEnum.DataType,
1981 DataTypeData.class);
1982 log.debug("After retrieving DERIVED_FROM node of {}. status is {}", uniqueId, parentNode);
1983 if (parentNode.isRight()) {
1984 JanusGraphOperationStatus janusGraphOperationStatus = parentNode.right().value();
1985 if (janusGraphOperationStatus != JanusGraphOperationStatus.NOT_FOUND) {
1986 log.error("Failed to find the parent data type of data type {}. status is {}", uniqueId,
1987 janusGraphOperationStatus);
1988 result = Either.right(janusGraphOperationStatus);
1992 // derived from node was found
1993 ImmutablePair<DataTypeData, GraphEdge> immutablePair = parentNode.left().value();
1994 DataTypeData parentCT = immutablePair.getKey();
1996 String parentUniqueId = parentCT.getUniqueId();
1997 Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(parentUniqueId);
1999 if (dataTypeByUid.isRight()) {
2000 return Either.right(dataTypeByUid.right().value());
2003 DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value();
2005 dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
2008 result = Either.left(dataTypeDefinition);
2013 private Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeUsingName(String name) {
2014 String uid = UniqueIdBuilder.buildDataTypeUid(name);
2015 return getDataTypeByUid(uid);
2018 public Either<String, JanusGraphOperationStatus> checkInnerType(PropertyDataDefinition propDataDef) {
2020 String propertyType = propDataDef.getType();
2022 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
2024 return getInnerType(type, propDataDef::getSchema);
2027 public Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypeNodes() {
2028 Either<List<DataTypeData>, JanusGraphOperationStatus> getAllDataTypes = janusGraphGenericDao
2029 .getByCriteria(NodeTypeEnum.DataType, null, DataTypeData.class);
2030 if (getAllDataTypes.isRight()) {
2031 JanusGraphOperationStatus status = getAllDataTypes.right().value();
2032 if (status == JanusGraphOperationStatus.NOT_FOUND) {
2033 status = JanusGraphOperationStatus.OK;
2034 return Either.right(status);
2037 return getAllDataTypes;
2040 public Either<Object, Boolean> validateAndUpdatePropertyValue(String propertyType, String value, boolean isValidate, String innerType, Map<String, DataTypeDefinition> dataTypes) {
2041 log.trace("Going to validate property value and its type. type = {}, value = {}", propertyType, value);
2042 ToscaPropertyType type = getType(propertyType);
2047 DataTypeDefinition dataTypeDefinition = dataTypes.get(propertyType);
2048 ImmutablePair<JsonElement, Boolean> validateResult = dataTypeValidatorConverter.validateAndUpdate(value, dataTypeDefinition, dataTypes);
2049 if (Boolean.FALSE.equals(validateResult.right)) {
2050 log.debug(THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID, value, propertyType);
2051 return Either.right(false);
2053 JsonElement jsonElement = validateResult.left;
2054 String valueFromJsonElement = getValueFromJsonElement(jsonElement);
2055 return Either.left(valueFromJsonElement);
2057 log.trace("before validating property type {}", propertyType);
2058 boolean isValidProperty = isValidValue(type, value, innerType, dataTypes);
2059 if (!isValidProperty) {
2060 log.debug(THE_VALUE_OF_PROPERTY_FROM_TYPE_IS_INVALID, value, type);
2061 return Either.right(false);
2064 Object convertedValue = value;
2065 if (!isEmptyValue(value) && isValidate) {
2066 PropertyValueConverter converter = type.getConverter();
2067 convertedValue = converter.convert(value, innerType, dataTypes);
2069 return Either.left(convertedValue);
2072 public Either<Object, Boolean> validateAndUpdatePropertyValue(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
2073 return validateAndUpdatePropertyValue(propertyType, value, true, innerType, dataTypes);
2076 public <T extends GraphNode> Either<List<PropertyDefinition>, StorageOperationStatus> getAllPropertiesRec(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz) {
2077 return this.findPropertiesOfNode(nodeType, uniqueId)
2079 .bind(this::handleNotFoundProperties)
2081 .bind(props -> getAllDerivedFromChainProperties(uniqueId, nodeType, clazz, props.values()));
2084 private Either<Map<String, PropertyDefinition>, StorageOperationStatus> handleNotFoundProperties(JanusGraphOperationStatus janusGraphOperationStatus) {
2085 if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
2086 return Either.left(new HashMap<>());
2088 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
2089 janusGraphOperationStatus));
2092 private <T extends GraphNode> Either<List<PropertyDefinition>, StorageOperationStatus> getAllDerivedFromChainProperties(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz, Collection<PropertyDefinition> nodeProps) {
2093 List<PropertyDefinition> accumulatedProps = new ArrayList<>(nodeProps);
2094 String currentNodeUid = uniqueId;
2095 Either<T, StorageOperationStatus> derivedFrom;
2096 while ((derivedFrom = derivedFromOperation.getDerivedFromChild(currentNodeUid, nodeType, clazz)).isLeft()) {
2097 currentNodeUid = derivedFrom.left().value().getUniqueId();
2098 JanusGraphOperationStatus
2099 janusGraphOperationStatus = fillPropertiesList(currentNodeUid, nodeType, accumulatedProps::addAll);
2100 if (janusGraphOperationStatus != JanusGraphOperationStatus.OK) {
2101 log.debug("failed to fetch properties for type {} with id {}", nodeType, currentNodeUid);
2102 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
2103 janusGraphOperationStatus));
2106 StorageOperationStatus getDerivedResult = derivedFrom.right().value();
2107 return isReachedEndOfDerivedFromChain(getDerivedResult) ? Either.left(accumulatedProps) : Either.right(getDerivedResult);
2110 private boolean isReachedEndOfDerivedFromChain(StorageOperationStatus getDerivedResult) {
2111 return getDerivedResult == StorageOperationStatus.NOT_FOUND;
2115 * @Override public PropertyOperation getPropertyOperation() { return this; }
2117 public JanusGraphOperationStatus fillPropertiesList(String uniqueId, NodeTypeEnum nodeType, Consumer<List<PropertyDefinition>> propertySetter) {
2118 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesRes = findPropertiesifExist(uniqueId, nodeType);
2119 if (findPropertiesRes.isRight()) {
2120 return findPropertiesRes.right().value();
2122 Map<String, PropertyDefinition> properties = findPropertiesRes.left().value();
2123 if (properties != null) {
2124 List<PropertyDefinition> propertiesAsList = properties.entrySet().stream().map(Entry::getValue).collect(Collectors.toList());
2125 propertySetter.accept(propertiesAsList);
2127 return JanusGraphOperationStatus.OK;
2130 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesifExist(String uniqueId, NodeTypeEnum nodeType){
2131 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode = this.findPropertiesOfNode(nodeType, uniqueId);
2132 if (findPropertiesOfNode.isRight()) {
2133 log.debug("After looking for properties of vertex {}. status is {}", uniqueId, findPropertiesOfNode.right().value());
2134 if(findPropertiesOfNode.right().value() == JanusGraphOperationStatus.NOT_FOUND)
2135 return Either.left(Maps.newHashMap());
2136 return findPropertiesOfNode;
2138 return findPropertiesOfNode;
2142 * add properties to element type.
2144 * Per property, add a property node and associate it to the element type
2147 * @param propertiesMap
2151 protected Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToElementType(String uniqueId, NodeTypeEnum nodeType, Map<String, PropertyDefinition> propertiesMap) {
2153 Map<String, PropertyData> propertiesData = new HashMap<>();
2155 if (propertiesMap != null) {
2157 for (Entry<String, PropertyDefinition> propertyDefinitionEntry : propertiesMap.entrySet()) {
2158 String propertyName = propertyDefinitionEntry.getKey();
2160 Either<PropertyData, JanusGraphOperationStatus> addPropertyToNodeType = this.addPropertyToNodeType(propertyName, propertyDefinitionEntry.getValue(), nodeType, uniqueId);
2162 if (addPropertyToNodeType.isRight()) {
2163 JanusGraphOperationStatus operationStatus = addPropertyToNodeType.right().value();
2164 log.error("Failed to associate {} {} to property {} in graph. status is {}", nodeType.getName(), uniqueId, propertyName, operationStatus);
2165 return Either.right(operationStatus);
2167 propertiesData.put(propertyName, addPropertyToNodeType.left().value());
2172 return Either.left(propertiesData);
2176 public Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToElementType(String uniqueId, NodeTypeEnum elementType, List<PropertyDefinition> properties) {
2178 Map<String, PropertyDefinition> propMap;
2179 if (properties == null) {
2182 propMap = properties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, propDef -> propDef));
2184 return addPropertiesToElementType(uniqueId, elementType, propMap);
2188 public Either<DataTypeDefinition, StorageOperationStatus> updateDataType(DataTypeDefinition newDataTypeDefinition, DataTypeDefinition oldDataTypeDefinition) {
2189 return updateDataType(newDataTypeDefinition, oldDataTypeDefinition, true);
2192 private Either<DataTypeDefinition, StorageOperationStatus> updateDataType(DataTypeDefinition newDataTypeDefinition, DataTypeDefinition oldDataTypeDefinition, boolean inTransaction) {
2194 Either<DataTypeDefinition, StorageOperationStatus> result = null;
2198 List<PropertyDefinition> newProperties = newDataTypeDefinition.getProperties();
2200 List<PropertyDefinition> oldProperties = oldDataTypeDefinition.getProperties();
2202 String newDerivedFromName = newDataTypeDefinition.getDerivedFromName();
2204 String oldDerivedFromName = oldDataTypeDefinition.getDerivedFromName();
2206 String dataTypeName = newDataTypeDefinition.getName();
2208 List<PropertyDefinition> propertiesToAdd = new ArrayList<>();
2209 if (isPropertyOmitted(newProperties, oldProperties, dataTypeName) || isPropertyTypeChanged(dataTypeName, newProperties, oldProperties, propertiesToAdd) || isDerivedFromNameChanged(dataTypeName, newDerivedFromName, oldDerivedFromName)) {
2211 log.debug("The new data type {} is invalid.", dataTypeName);
2213 result = Either.right(StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY);
2217 if (propertiesToAdd == null || propertiesToAdd.isEmpty()) {
2218 log.debug("No new properties has been defined in the new data type {}", newDataTypeDefinition);
2219 result = Either.right(StorageOperationStatus.OK);
2222 Map<String, String> newDescriptions = getPropertyDescriptionsToUpdate(oldProperties, newProperties);
2224 if(MapUtils.isNotEmpty(newDescriptions)){
2226 JanusGraphOperationStatus updatePropertiesStatus = updateDataTypePropertyDescriptions(oldDataTypeDefinition.getUniqueId(), newDescriptions);
2227 if (updatePropertiesStatus != JanusGraphOperationStatus.OK) {
2228 log.debug("#updateDataType - Failed to update the descriptions of the properties of the data type {}. Status is {}", oldDataTypeDefinition, updatePropertiesStatus);
2229 BeEcompErrorManager.getInstance().logBeFailedAddingNodeTypeError(UPDATE_DATA_TYPE, PROPERTY);
2230 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(updatePropertiesStatus));
2235 Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType(oldDataTypeDefinition.getUniqueId(), propertiesToAdd);
2237 if (addPropertiesToDataType.isRight()) {
2238 log.debug("Failed to update data type {} to Graph. Status is {}", oldDataTypeDefinition, addPropertiesToDataType.right().value().name());
2239 BeEcompErrorManager.getInstance().logBeFailedAddingNodeTypeError(UPDATE_DATA_TYPE, PROPERTY);
2240 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(addPropertiesToDataType.right().value()));
2244 Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = this.getDataTypeByUid(oldDataTypeDefinition.getUniqueId());
2245 if (dataTypeByUid.isRight()) {
2246 JanusGraphOperationStatus status = addPropertiesToDataType.right().value();
2247 log.debug("Failed to get data type {} after update. Status is {}", oldDataTypeDefinition.getUniqueId(), status.name());
2248 BeEcompErrorManager.getInstance().logBeFailedRetrieveNodeError(UPDATE_DATA_TYPE, PROPERTY, status.name());
2249 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
2251 result = Either.left(dataTypeByUid.left().value());
2258 if (!inTransaction) {
2259 if (result == null || result.isRight()) {
2260 log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
2261 janusGraphGenericDao.rollback();
2263 log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
2264 janusGraphGenericDao.commit();
2271 private boolean isPropertyTypeChanged(String dataTypeName, List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, List<PropertyDefinition> outputPropertiesToAdd) {
2273 if (newProperties != null && oldProperties != null) {
2275 Map<String, PropertyDefinition> newPropsMapper = newProperties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, p -> p));
2276 Map<String, PropertyDefinition> oldPropsMapper = oldProperties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, p -> p));
2278 for (Entry<String, PropertyDefinition> newPropertyEntry : newPropsMapper.entrySet()) {
2280 String propName = newPropertyEntry.getKey();
2281 PropertyDefinition propDef = newPropertyEntry.getValue();
2283 PropertyDefinition oldPropertyDefinition = oldPropsMapper.get(propName);
2284 if (oldPropertyDefinition == null) {
2285 log.debug("New property {} received in the data type {}", propName, dataTypeName);
2286 outputPropertiesToAdd.add(propDef);
2290 String oldType = oldPropertyDefinition.getType();
2291 String oldEntryType = getEntryType(oldPropertyDefinition);
2293 String newType = propDef.getType();
2294 String newEntryType = getEntryType(propDef);
2296 if (!oldType.equals(newType)) {
2297 log.debug("Existing property {} in data type {} has a differnet type {} than the new one {}", propName, dataTypeName, oldType, newType);
2301 if (!equalsEntryTypes(oldEntryType, newEntryType)) {
2302 log.debug("Existing property {} in data type {} has a differnet entry type {} than the new one {}", propName, dataTypeName, oldEntryType, newEntryType);
2313 private boolean equalsEntryTypes(String oldEntryType, String newEntryType) {
2315 if (oldEntryType == null && newEntryType == null) {
2317 } else if (oldEntryType != null && newEntryType != null) {
2318 return oldEntryType.equals(newEntryType);
2324 private String getEntryType(PropertyDefinition oldPropertyDefinition) {
2325 String entryType = null;
2326 SchemaDefinition schema = oldPropertyDefinition.getSchema();
2327 if (schema != null) {
2328 PropertyDataDefinition schemaProperty = schema.getProperty();
2329 if (schemaProperty != null) {
2330 entryType = schemaProperty.getType();
2336 private boolean isPropertyOmitted(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, String dataTypeName) {
2338 boolean isValid = validateChangeInCaseOfEmptyProperties(newProperties, oldProperties, dataTypeName);
2340 log.debug("At least one property is missing in the new data type {}", dataTypeName);
2344 if (newProperties != null && oldProperties != null) {
2346 List<String> newProps = newProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2347 List<String> oldProps = oldProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2349 if (!newProps.containsAll(oldProps)) {
2350 StringJoiner joiner = new StringJoiner(",", "[", "]");
2351 newProps.forEach(joiner::add);
2352 log.debug("Properties {} in data type {} are missing, but they already defined in the existing data type", joiner.toString(), dataTypeName);
2360 private boolean validateChangeInCaseOfEmptyProperties(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, String dataTypeName) {
2362 if (newProperties != null) {
2363 if (newProperties.isEmpty()) {
2364 newProperties = null;
2368 if (oldProperties != null) {
2369 if (oldProperties.isEmpty()) {
2370 oldProperties = null;
2374 if ((newProperties == null && oldProperties == null) || (newProperties != null && oldProperties != null)) {
2381 private boolean isDerivedFromNameChanged(String dataTypeName, String newDerivedFromName, String oldDerivedFromName) {
2383 if (newDerivedFromName != null) {
2384 boolean isEqual = newDerivedFromName.equals(oldDerivedFromName);
2386 log.debug("The new datatype {} derived from another data type {} than the existing one {}", dataTypeName, newDerivedFromName, oldDerivedFromName);
2389 } else if (oldDerivedFromName == null) {
2391 } else {// new=null, old != null
2392 log.debug("The new datatype {} derived from another data type {} than the existing one {}", dataTypeName, newDerivedFromName, oldDerivedFromName);
2403 public Either<Integer, StorageOperationStatus> increaseAndGetObjInstancePropertyCounter(String instanceId, NodeTypeEnum nodeType) {
2404 Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphGenericDao.getGraph();
2405 if (graphResult.isRight()) {
2406 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(graphResult.right().value()));
2408 Either<JanusGraphVertex, JanusGraphOperationStatus> vertexService = janusGraphGenericDao
2409 .getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(nodeType), instanceId);
2410 if (vertexService.isRight()) {
2411 log.debug("failed to fetch vertex of resource instance for id = {}", instanceId);
2412 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexService.right().value()));
2414 Vertex vertex = vertexService.left().value();
2416 VertexProperty<Object> vertexProperty = vertex.property(GraphPropertiesDictionary.PROPERTY_COUNTER.getProperty());
2417 Integer counter = 0;
2418 if (vertexProperty.isPresent() && vertexProperty.value() != null) {
2419 counter = (Integer) vertexProperty.value();
2423 vertex.property(GraphPropertiesDictionary.PROPERTY_COUNTER.getProperty(), counter);
2425 return Either.left(counter);
2429 public Either<List<PropertyDefinition>, JanusGraphOperationStatus> validatePropertiesUniqueness(Map<String, PropertyDefinition> inheritedProperties, List<PropertyDefinition> properties) {
2430 Either<List<PropertyDefinition>, JanusGraphOperationStatus> result = Either.left(properties);
2432 for (PropertyDefinition property : properties) {
2433 JanusGraphOperationStatus
2434 status = validatePropertyUniqueness(inheritedProperties, property);
2435 if (status != JanusGraphOperationStatus.OK) {
2436 result = Either.right(status);
2445 * Validates uniqueness of examined property by comparing it with properties in propertiesOfType
2446 * and updates if need type and inner type of the property.
2448 private JanusGraphOperationStatus validatePropertyUniqueness(Map<String, PropertyDefinition> inheritedProperties, PropertyDefinition property) {
2449 String propertyName = property.getName();
2450 String propertyType = property.getType();
2452 JanusGraphOperationStatus result = JanusGraphOperationStatus.OK;
2453 if (inheritedProperties.containsKey(propertyName)) {
2454 PropertyDefinition defaultProperty = inheritedProperties.get(propertyName);
2455 if (typesMismatch(propertyType, defaultProperty.getType())) {
2456 log.error("#validatePropertyUniqueness - Property with name {} and different type already exists.", propertyName);
2457 result = JanusGraphOperationStatus.PROPERTY_NAME_ALREADY_EXISTS;
2459 property.setType(defaultProperty.getType());
2460 String innerType = defaultProperty.getSchemaType();
2462 PropertyDataDefinition schemaProperty = property.getSchemaProperty();
2463 if (schemaProperty != null) {
2464 schemaProperty.setType(innerType);
2472 private boolean typesMismatch(String type1, String type2) {
2473 return type1 != null && type2 != null && !type2.equals(type1);
2477 public <T extends GraphNode> Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> getAllTypePropertiesFromAllDerivedFrom(String nextParentUid,
2478 NodeTypeEnum nodeType,
2480 Map<String, PropertyDefinition> allProperies = new HashMap<>();
2481 return getTypePropertiesFromDerivedFromRecursively(nextParentUid, allProperies, nodeType, clazz);
2484 private <T extends GraphNode> Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> getTypePropertiesFromDerivedFromRecursively(String nextParentUid,
2485 Map<String, PropertyDefinition> allProperies,
2486 NodeTypeEnum nodeType,
2488 JanusGraphOperationStatus error;
2489 Either<List<ImmutablePair<T, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
2490 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType),
2491 nextParentUid, GraphEdgeLabels.DERIVED_FROM, nodeType, clazz);
2492 if (childrenNodes.isRight()) {
2493 if (childrenNodes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
2494 error = childrenNodes.right().value();
2495 log.debug("#getTypePropertiesFromDerivedFromRecursively - Couldn't fetch derived from node with UID {}, error: {}", nextParentUid, error);
2496 return Either.right(error);
2499 log.debug("#getTypePropertiesFromDerivedFromRecursively - Derived from node is not found with UID {} - this is OK for root.", nextParentUid);
2500 return Either.left(allProperies);
2504 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> allPropertiesOfTypeRes = findPropertiesOfNode(nodeType, nextParentUid);
2505 if (allPropertiesOfTypeRes.isRight() && !allPropertiesOfTypeRes.right().value().equals(
2506 JanusGraphOperationStatus.NOT_FOUND)) {
2507 error = allPropertiesOfTypeRes.right().value();
2508 log.error("#getTypePropertiesFromDerivedFromRecursively - Failed to retrieve properties for node with UID {} from graph. status is {}", nextParentUid, error);
2509 return Either.right(error);
2510 } else if (allPropertiesOfTypeRes.isLeft()) {
2511 if (allProperies.isEmpty()) {
2512 allProperies.putAll(allPropertiesOfTypeRes.left().value());
2514 allProperies.putAll(allPropertiesOfTypeRes.left().value().entrySet().stream().filter(e -> !allProperies.containsKey(e.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
2517 return getTypePropertiesFromDerivedFromRecursively(childrenNodes.left().value().get(0).getLeft().getUniqueId(), allProperies, nodeType, clazz);
2520 private JanusGraphOperationStatus updateDataTypePropertyDescriptions(String uniqueId, Map<String, String> newDescriptions) {
2522 if (MapUtils.isNotEmpty(newDescriptions)) {
2523 Either<List<ImmutablePair<JanusGraphVertex, Edge>>, JanusGraphOperationStatus> getDataTypePropertiesRes = janusGraphGenericDao
2524 .getChildrenVertecies(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueId, GraphEdgeLabels.PROPERTY);
2526 if(getDataTypePropertiesRes.isRight()){
2527 log.debug("#updateDataTypePropertiesDescriptions - Failed to fetch the property verticies of the Data type {} ", uniqueId);
2528 return getDataTypePropertiesRes.right().value();
2530 getDataTypePropertiesRes.left().value().stream()
2531 .filter(pair -> newDescriptions.containsKey(getPropertyNameFromEdge(pair)))
2532 .forEach(pair -> setNewDescriptionToVertex(newDescriptions.get(getPropertyNameFromEdge(pair)), pair));
2535 return JanusGraphOperationStatus.OK;
2538 private JanusGraphVertexProperty<String> setNewDescriptionToVertex(String newDescription, ImmutablePair<JanusGraphVertex, Edge> pair) {
2539 return pair.getLeft().property(GraphPropertiesDictionary.DESCRIPTION.getProperty(), newDescription);
2542 private String getPropertyNameFromEdge(ImmutablePair<JanusGraphVertex, Edge> pair) {
2543 return (String) pair.getRight().property(GraphPropertiesDictionary.NAME.getProperty()).value();
2546 private Map<String, String> getPropertyDescriptionsToUpdate(List<PropertyDefinition> oldProperties, List<PropertyDefinition> newProperties) {
2548 Map<String, PropertyDefinition> newPropertiesMap = newProperties
2550 .collect(Collectors.toMap(PropertyDefinition::getName, p->p));
2552 return oldProperties
2554 .filter(p-> newPropertiesMap.containsKey(p.getName()) && !descriptionsEqual(p, newPropertiesMap.get(p.getName())))
2555 .collect(Collectors.toMap(PropertyDefinition::getName, p->newPropertiesMap.get(p.getName()).getDescription()));
2558 private boolean descriptionsEqual(PropertyDefinition property, PropertyDefinition otherProperty){
2559 if(StringUtils.isEmpty(property.getDescription()) && StringUtils.isEmpty(otherProperty.getDescription())){
2562 if(StringUtils.isNotEmpty(property.getDescription()) && StringUtils.isEmpty(otherProperty.getDescription())){
2565 if(StringUtils.isEmpty(property.getDescription()) && StringUtils.isNotEmpty(otherProperty.getDescription())){
2568 return property.getDescription().equals(otherProperty.getDescription());