Remove dead code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / PropertyOperation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import com.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;
47 import java.util.Map;
48 import java.util.Map.Entry;
49 import java.util.Set;
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;
105
106
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";
120
121         public static void main(String[] args) {
122
123                 List<Pattern> buildFunctionPatterns = buildFunctionPatterns();
124
125                 for (Pattern pattern : buildFunctionPatterns) {
126
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());
131                         }
132                 }
133
134         }
135
136         @Autowired
137         public PropertyOperation(HealingJanusGraphGenericDao janusGraphGenericDao, DerivedFromOperation derivedFromOperation) {
138                 this.janusGraphGenericDao = janusGraphGenericDao;
139                 this.derivedFromOperation = derivedFromOperation;
140         }
141
142         private static Logger log = Logger.getLogger(PropertyOperation.class.getName());
143
144         /**
145          * The value of functions is in a json format. Build pattern for each function name
146          * 
147          * { str_replace: .... } {str_replace: .... } {str_replace: .... } { str_replace: .... }
148          * 
149          * @return
150          */
151         private static List<Pattern> buildFunctionPatterns() {
152
153                 List<Pattern> functionPatterns = new ArrayList<>();
154
155                 String[] functions = { "get_input", "get_property" };
156
157                 for (String function : functions) {
158                         Pattern pattern = Pattern.compile("^[ ]*\\{[ ]*" + function + ":");
159                         functionPatterns.add(pattern);
160                 }
161
162                 return functionPatterns;
163         }
164
165         public PropertyDefinition convertPropertyDataToPropertyDefinition(PropertyData propertyDataResult, String propertyName, String resourceId) {
166                 log.debug("The object returned after create property is {}", propertyDataResult);
167
168                 PropertyDefinition propertyDefResult = new PropertyDefinition(propertyDataResult.getPropertyDataDefinition());
169                 propertyDefResult.setConstraints(convertConstraints(propertyDataResult.getConstraints()));
170                 propertyDefResult.setName(propertyName);
171
172                 return propertyDefResult;
173         }
174
175         public static class PropertyConstraintSerialiser implements JsonSerializer<PropertyConstraint> {
176
177                 @Override
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);
195                         } else {
196                                 log.warn("PropertyConstraint {} is not supported. Ignored.", src.getClass().getName());
197                         }
198
199                         return result;
200                 }
201
202         }
203
204         public static class PropertyConstraintDeserialiser implements JsonDeserializer<PropertyConstraint> {
205
206                 private static final String THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL = "The value of GreaterThanConstraint is null";
207
208                 @Override
209                 public PropertyConstraint deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
210
211                         PropertyConstraint propertyConstraint = null;
212
213                         Set<Entry<String, JsonElement>> set = json.getAsJsonObject().entrySet();
214
215                         if (set.size() == 1) {
216                                 Entry<String, JsonElement> element = set.iterator().next();
217                                 String key = element.getKey();
218                                 JsonElement value = element.getValue();
219
220                                 ConstraintType constraintType = ConstraintType.getByType(key);
221                                 if (constraintType == null) {
222                                         log.warn("ConstraintType was not found for constraint name:{}", key);
223                                 } else {
224                                         switch (constraintType) {
225                                         case IN_RANGE:
226
227                                                 if (value != null) {
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);
232                                                                 } else {
233                                                                         InRangeConstraint rangeConstraint = new InRangeConstraint();
234                                                                         String minValue = rangeArray.get(0).getAsString();
235                                                                         String maxValue;
236                                                                         JsonElement maxElement = rangeArray.get(1);
237                                                                         if(maxElement.isJsonNull()){
238                                         maxValue = String.valueOf(maxElement.getAsJsonNull());
239                                     } else {
240                                         maxValue = maxElement.getAsString();
241                                     }
242                                                                         rangeConstraint.setRangeMinValue(minValue);
243                                                                         rangeConstraint.setRangeMaxValue(maxValue);
244                                                                         propertyConstraint = rangeConstraint;
245                                                                 }
246                                                         }
247
248                                                 } else {
249                                                         log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
250                                                 }
251                                                 break;
252                                         case GREATER_THAN:
253                                                 if (value != null) {
254                                                         String asString = value.getAsString();
255                                                         log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
256                                                         propertyConstraint = new GreaterThanConstraint(asString);
257                                                         break;
258                                                 } else {
259                                                         log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
260                                                 }
261                                                 break;
262
263                                         case LESS_THAN:
264                                                 if (value != null) {
265                                                         String asString = value.getAsString();
266                                                         log.debug("Before adding value to LessThanConstraint object. value = {}", asString);
267                                                         propertyConstraint = new LessThanConstraint(asString);
268                                                         break;
269                                                 } else {
270                                                         log.warn("The value of LessThanConstraint is null");
271                                                 }
272                                                 break;
273                                         case GREATER_OR_EQUAL:
274                                                 if (value != null) {
275                                                         String asString = value.getAsString();
276                                                         log.debug("Before adding value to GreaterThanConstraint object. value = {}", asString);
277                                                         propertyConstraint = new GreaterOrEqualConstraint(asString);
278                                                         break;
279                                                 } else {
280                                                         log.warn("The value of GreaterOrEqualConstraint is null");
281                                                 }
282                                                 break;
283                                         case LESS_OR_EQUAL:
284
285                                                 if (value != null) {
286                                                         String asString = value.getAsString();
287                                                         log.debug("Before adding value to LessOrEqualConstraint object. value = {}", asString);
288                                                         propertyConstraint = new LessOrEqualConstraint(asString);
289                                                 } else {
290                                                         log.warn(THE_VALUE_OF_GREATER_THAN_CONSTRAINT_IS_NULL);
291                                                 }
292                                                 break;
293
294                                         case VALID_VALUES:
295
296                                                 if (value != null) {
297                                                         JsonArray rangeArray = (JsonArray) value;
298                                                         if (rangeArray.size() == 0) {
299                                                                 log.error("The valid values constraint content is invalid. value = {}", value);
300                                                         } else {
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);
306                                                                 }
307                                                                 vvConstraint.setValidValues(validValues);
308                                                                 propertyConstraint = vvConstraint;
309                                                         }
310                                                 }
311                                                 break;
312
313                                         case MIN_LENGTH:
314                                                 if (value != null) {
315                                                         int asInt = value.getAsInt();
316                                                         log.debug("Before adding value to Min Length object. value = {}", asInt);
317                                                         propertyConstraint = new MinLengthConstraint(asInt);
318                                                         break;
319                                                 } else {
320                                                         log.warn("The value of MinLengthConstraint is null");
321                                                 }
322                                                 break;
323                                         default:
324                                                 log.warn("Key {} is not supported. Ignored.", key);
325                                         }
326                                 }
327                         }
328
329                         return propertyConstraint;
330                 }
331
332         }
333
334
335         public Either<PropertyData, StorageOperationStatus> addProperty(String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
336
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);
341                 }
342                 return Either.left(either.left().value());
343         }
344
345         /**
346          * @param propertyDefinition
347          * @return
348          */
349         @Override
350         public StorageOperationStatus validateAndUpdateProperty(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
351
352                 log.trace("Going to validate property type and value. {}", propertyDefinition);
353
354                 String propertyType = propertyDefinition.getType();
355                 String value = propertyDefinition.getDefaultValue();
356
357                 ToscaPropertyType type = getType(propertyType);
358
359                 if (type == null) {
360
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;
365                         }
366
367             return validateAndUpdateComplexValue(propertyDefinition, propertyType, value, dataTypeDefinition, dataTypes);
368
369                 }
370                 String innerType = null;
371
372                 Either<String, JanusGraphOperationStatus> checkInnerType = getInnerType(type, propertyDefinition::getSchema);
373                 if (checkInnerType.isRight()) {
374                         return StorageOperationStatus.INVALID_TYPE;
375                 }
376                 innerType = checkInnerType.left().value();
377
378                 log.trace("After validating property type {}", propertyType);
379
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;
384                 }
385
386                 PropertyValueConverter converter = type.getConverter();
387
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);
394                 }
395                 return StorageOperationStatus.OK;
396         }
397
398         public Either<PropertyData, JanusGraphOperationStatus> addPropertyToGraph(String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
399
400                 ResourceMetadataData resourceData = new ResourceMetadataData();
401                 resourceData.getMetadataDataDefinition().setUniqueId(resourceId);
402
403                 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
404
405                 propertyDefinition.setUniqueId(UniqueIdBuilder.buildComponentPropertyUniqueId(resourceId, propertyName));
406                 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
407
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);
416                 }
417
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);
426                 }
427
428                 return Either.left(createNodeResult.left().value());
429
430         }
431
432         public JanusGraphOperationStatus addPropertyToGraphByVertex(JanusGraphVertex metadataVertex, String propertyName, PropertyDefinition propertyDefinition, String resourceId) {
433
434                 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
435
436                 propertyDefinition.setUniqueId(UniqueIdBuilder.buildComponentPropertyUniqueId(resourceId, propertyName));
437                 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
438
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;
446                 }
447
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;
457                 }
458
459                 return createRelResult;
460
461         }
462
463         public JanusGraphGenericDao getJanusGraphGenericDao() {
464                 return janusGraphGenericDao;
465         }
466
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);
471         }
472
473         public Either<PropertyData, StorageOperationStatus> updateProperty(String propertyId, PropertyDefinition newPropertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
474
475                 StorageOperationStatus validateAndUpdateProperty = validateAndUpdateProperty(newPropertyDefinition, dataTypes);
476                 if (validateAndUpdateProperty != StorageOperationStatus.OK) {
477                         return Either.right(validateAndUpdateProperty);
478                 }
479
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);
484                 }
485                 return Either.left(either.left().value());
486         }
487
488         public Either<PropertyData, JanusGraphOperationStatus> updatePropertyFromGraph(String propertyId, PropertyDefinition propertyDefinition) {
489                 if (log.isDebugEnabled())
490                         log.debug("Before updating property on graph {}", propertyId);
491
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());
498                 }
499                 PropertyData orgPropertyData = statusProperty.left().value();
500                 PropertyDataDefinition orgPropertyDataDefinition = orgPropertyData.getPropertyDataDefinition();
501
502                 // create new property data to update
503                 PropertyData newPropertyData = new PropertyData();
504                 newPropertyData.setPropertyDataDefinition(propertyDefinition);
505                 PropertyDataDefinition newPropertyDataDefinition = newPropertyData.getPropertyDataDefinition();
506
507                 // update the original property data with new values
508                 if (orgPropertyDataDefinition.getDefaultValue() == null) {
509                         orgPropertyDataDefinition.setDefaultValue(newPropertyDataDefinition.getDefaultValue());
510                 } else {
511                         if (!orgPropertyDataDefinition.getDefaultValue().equals(newPropertyDataDefinition.getDefaultValue())) {
512                                 orgPropertyDataDefinition.setDefaultValue(newPropertyDataDefinition.getDefaultValue());
513                         }
514                 }
515                 if (orgPropertyDataDefinition.getDescription() == null) {
516                         orgPropertyDataDefinition.setDescription(newPropertyDataDefinition.getDescription());
517                 } else {
518                         if (!orgPropertyDataDefinition.getDescription().equals(newPropertyDataDefinition.getDescription())) {
519                                 orgPropertyDataDefinition.setDescription(newPropertyDataDefinition.getDescription());
520                         }
521                 }
522                 if (!orgPropertyDataDefinition.getType().equals(newPropertyDataDefinition.getType())) {
523                         orgPropertyDataDefinition.setType(newPropertyDataDefinition.getType());
524                 }
525                 if (newPropertyData.getConstraints() != null) {
526                         orgPropertyData.setConstraints(newPropertyData.getConstraints());
527                 }
528                 orgPropertyDataDefinition.setSchema(newPropertyDataDefinition.getSchema());
529
530                 return janusGraphGenericDao.updateNode(orgPropertyData, PropertyData.class);
531         }
532
533         /**
534          * FOR TEST ONLY
535          * 
536          * @param janusGraphGenericDao
537          */
538         public void setJanusGraphGenericDao(HealingJanusGraphGenericDao janusGraphGenericDao) {
539                 this.janusGraphGenericDao = janusGraphGenericDao;
540         }
541
542         public Either<PropertyData, JanusGraphOperationStatus> addPropertyToNodeType(String propertyName, PropertyDefinition propertyDefinition, NodeTypeEnum nodeType, String uniqueId) {
543
544                 List<PropertyConstraint> constraints = propertyDefinition.getConstraints();
545
546                 propertyDefinition.setUniqueId(UniqueIdBuilder.buildPropertyUniqueId(uniqueId, propertyName));
547                 PropertyData propertyData = new PropertyData(propertyDefinition, convertConstraintsToString(constraints));
548
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);
559                 }
560
561                 Map<String, Object> props = new HashMap<>();
562                 props.put(GraphPropertiesDictionary.NAME.getProperty(), propertyName);
563
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);
572                 }
573
574                 return Either.left(createNodeResult.left().value());
575
576         }
577
578         public Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> findPropertiesOfNode(NodeTypeEnum nodeType, String uniqueId) {
579
580                 Map<String, PropertyDefinition> resourceProps = new HashMap<>();
581
582                 Either<List<ImmutablePair<PropertyData, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
583                                 .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.PROPERTY, NodeTypeEnum.Property,
584                                 PropertyData.class);
585
586                 if (childrenNodes.isRight()) {
587                         JanusGraphOperationStatus operationStatus = childrenNodes.right().value();
588                         return Either.right(operationStatus);
589                 }
590
591                 List<ImmutablePair<PropertyData, GraphEdge>> values = childrenNodes.left().value();
592                 if (values != null) {
593
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);
601                         }
602
603                 }
604
605                 log.debug("The properties associated to node {} are {}", uniqueId, resourceProps);
606                 return Either.left(resourceProps);
607         }
608
609     public Either<Map<String, PropertyDefinition>, StorageOperationStatus> deletePropertiesAssociatedToNode(NodeTypeEnum nodeType, String uniqueId) {
610             return deleteAllPropertiesAssociatedToNode(nodeType, uniqueId)
611                 .right()
612                 .bind(err -> err == StorageOperationStatus.OK ? Either.left(Collections.emptyMap()) : Either.right(err));
613     }
614     
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);
617         
618         Map<String, PropertyDefinition> reallyNewProperties;
619         Map<String, PropertyData> unchangedPropsData;
620         
621         if (oldPropertiesRes.isRight()) {
622             JanusGraphOperationStatus err  = oldPropertiesRes.right().value();
623             if (err == JanusGraphOperationStatus.NOT_FOUND) {
624                 reallyNewProperties = newProperties;
625                 unchangedPropsData = Collections.emptyMap();
626             }
627             else {
628                 return Either.right(err);
629             }
630         }
631         else {
632             Map<String, PropertyDefinition> oldProperties = oldPropertiesRes.left().value();
633             reallyNewProperties = collectReallyNewProperties(newProperties, oldProperties);
634             
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();
639
640                 JanusGraphOperationStatus status = updateOldProperty(newPropDef, oldPropDef);
641                 if (status != JanusGraphOperationStatus.OK) {
642                     return Either.right(status);
643                 }
644             }
645             unchangedPropsData = oldProperties.entrySet().stream()
646                                         .collect(Collectors.toMap(Entry::getKey, e-> new PropertyData(e.getValue(), null)));
647         }
648             
649             
650         // add other properties
651         return addPropertiesToElementType(nodeType, uniqueId, reallyNewProperties, unchangedPropsData);
652     }
653
654     /**
655      * @param newProperties
656      * @param oldProperties
657      * @return
658      */
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;
663     }
664
665     /**
666      * @param newPropDef
667      * @param oldPropDef
668      */
669     private JanusGraphOperationStatus updateOldProperty(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
670         if (!isUpdateAllowed(newPropDef, oldPropDef)) {
671             return JanusGraphOperationStatus.MATCH_NOT_FOUND;
672         }
673         
674         if (isUpdateRequired(newPropDef, oldPropDef)) {
675             modifyOldPropByNewOne(newPropDef, oldPropDef);
676             
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);
681             
682             if (updateResult.isRight()) {
683                 return updateResult.right().value();
684             }
685         }
686         
687         return JanusGraphOperationStatus.OK;
688     }
689
690     /**
691      * @param newPropDef
692      * @param oldPropDef
693      */
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());
697             return false;
698         }
699         
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());
703             return false;
704         }
705         
706         return true;
707     }
708
709     /**
710      * Update only fields which modification is permitted.
711      * @param newPropDef
712      * @param oldPropDef
713      */
714     private void modifyOldPropByNewOne(PropertyDefinition newPropDef, PropertyDefinition oldPropDef) {
715         oldPropDef.setDefaultValue(newPropDef.getDefaultValue());
716         oldPropDef.setDescription(newPropDef.getDescription());
717         oldPropDef.setRequired(newPropDef.isRequired());
718
719         // Type is updated to fix possible null type issue in janusGraph DB
720         oldPropDef.setType(newPropDef.getType());
721     }
722
723
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();
728     }
729
730     /**
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
733      * @param nodeType
734      * @param uniqueId
735      * @param newProperties
736      * @param unchangedPropsData
737      * @return
738      */
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)
741                 .left()
742                 .map(m -> { 
743                     m.putAll(unchangedPropsData);
744                     return m;
745                  });
746     }
747
748
749         public Either<Map<String, PropertyDefinition>, StorageOperationStatus> deleteAllPropertiesAssociatedToNode(NodeTypeEnum nodeType, String uniqueId) {
750
751                 Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> propertiesOfNodeRes = findPropertiesOfNode(nodeType, uniqueId);
752
753                 if (propertiesOfNodeRes.isRight()) {
754                         JanusGraphOperationStatus status = propertiesOfNodeRes.right().value();
755                         if (status == JanusGraphOperationStatus.NOT_FOUND) {
756                                 return Either.right(StorageOperationStatus.OK);
757                         }
758                         return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
759                 }
760
761                 Map<String, PropertyDefinition> value = propertiesOfNodeRes.left().value();
762                 for (PropertyDefinition propertyDefinition : value.values()) {
763
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;
771                                 }
772                                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(status));
773                         }
774
775                 }
776
777                 log.debug("The properties deleted from node {} are {}", uniqueId, value);
778                 return Either.left(value);
779         }
780
781 /**
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)
784  * @param properties
785  * @param resourceUid
786  * @param propertyName
787  * @param propertyType
788  * @return
789  */
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) {
794         
795                                 if ( propertyDefinition.getName().equals(propertyName) &&
796                                                 (propertyDefinition.getParentUniqueId().equals(resourceUid) || !propertyDefinition.getType().equals(propertyType)) ) {
797                                         result = true;
798                                         break;
799                                 }
800                         }
801                 }
802                 return result;
803         }
804
805         public ImmutablePair<String, Boolean> validateAndUpdateRules(String propertyType, List<PropertyRule> rules, String innerType, Map<String, DataTypeDefinition> dataTypes, boolean isValidate) {
806
807                 if (rules == null || rules.isEmpty()) {
808                         return new ImmutablePair<>(null, true);
809                 }
810
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();
816                                 if (!status) {
817                                         return new ImmutablePair<>(value, status);
818                                 }
819                         } else {
820                                 String newValue = null;
821                                 Object object = updateResult.left().value();
822                                 if (object != null) {
823                                         newValue = object.toString();
824                                 }
825                                 rule.setValue(newValue);
826                         }
827                 }
828
829                 return new ImmutablePair<>(null, true);
830         }
831
832         public void addRulesToNewPropertyValue(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty, String resourceInstanceId) {
833
834                 List<PropertyRule> rules = resourceInstanceProperty.getRules();
835                 if (rules == null) {
836                         PropertyRule propertyRule = buildRuleFromPath(propertyValueData, resourceInstanceProperty, resourceInstanceId);
837                         rules = new ArrayList<>();
838                         rules.add(propertyRule);
839                 } else {
840                         rules = sortRules(rules);
841                 }
842
843                 propertyValueData.setRules(rules);
844         }
845
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
849                 // graph.
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);
855                 }
856                 PropertyRule propertyRule = new PropertyRule();
857                 propertyRule.setRule(path);
858                 propertyRule.setValue(propertyValueData.getValue());
859                 return propertyRule;
860         }
861
862         private List<PropertyRule> sortRules(List<PropertyRule> rules) {
863
864                 // TODO: sort the rules by size and binary representation.
865                 // (x, y, .+) --> 110 6 priority 1
866                 // (x, .+, z) --> 101 5 priority 2
867
868                 return rules;
869         }
870
871         public ImmutablePair<JanusGraphOperationStatus, String> findPropertyValue(String resourceInstanceId, String propertyId) {
872
873                 log.debug("Going to check whether the property {} already added to resource instance {}", propertyId, resourceInstanceId);
874
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);
880                 }
881
882                 List<ComponentInstanceProperty> list = getAllRes.left().value();
883                 if (list != null) {
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);
891                                 }
892                         }
893                 }
894
895                 return new ImmutablePair<>(JanusGraphOperationStatus.NOT_FOUND, null);
896         }
897
898
899         public void updateRulesInPropertyValue(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty, String resourceInstanceId) {
900
901                 List<PropertyRule> currentRules = propertyValueData.getRules();
902
903                 List<PropertyRule> rules = resourceInstanceProperty.getRules();
904                 // if rules are not supported.
905                 if (rules == null) {
906
907                         PropertyRule propertyRule = buildRuleFromPath(propertyValueData, resourceInstanceProperty, resourceInstanceId);
908                         rules = new ArrayList<>();
909                         rules.add(propertyRule);
910
911                         if (currentRules != null) {
912                                 rules = mergeRules(currentRules, rules);
913                         }
914
915                 } else {
916                         // Full mode. all rules are sent in update operation.
917                         rules = sortRules(rules);
918                 }
919
920                 propertyValueData.setRules(rules);
921
922         }
923
924         private List<PropertyRule> mergeRules(List<PropertyRule> currentRules, List<PropertyRule> newRules) {
925
926                 List<PropertyRule> mergedRules = new ArrayList<>();
927
928                 if (newRules == null || newRules.isEmpty()) {
929                         return currentRules;
930                 }
931
932                 for (PropertyRule rule : currentRules) {
933                         PropertyRule propertyRule = new PropertyRule(rule.getRule(), rule.getValue());
934                         mergedRules.add(propertyRule);
935                 }
936
937                 for (PropertyRule rule : newRules) {
938                         PropertyRule foundRule = findRuleInList(rule, mergedRules);
939                         if (foundRule != null) {
940                                 foundRule.setValue(rule.getValue());
941                         } else {
942                                 mergedRules.add(rule);
943                         }
944                 }
945
946                 return mergedRules;
947         }
948
949         private PropertyRule findRuleInList(PropertyRule rule, List<PropertyRule> rules) {
950
951                 if (rules == null || rules.isEmpty() || rule.getRule() == null || rule.getRule().isEmpty()) {
952                         return null;
953                 }
954
955                 PropertyRule foundRule = null;
956                 for (PropertyRule propertyRule : rules) {
957                         if (rule.getRuleSize() != propertyRule.getRuleSize()) {
958                                 continue;
959                         }
960                         boolean equals = propertyRule.compareRule(rule);
961                         if (equals) {
962                                 foundRule = propertyRule;
963                                 break;
964                         }
965                 }
966
967                 return foundRule;
968         }
969
970         /**
971          * return all properties associated to resource instance. The result does contains the property unique id but not its type, default value...
972          * 
973          * @param resourceInstanceUid
974          * @return
975          */
976         public Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> getAllPropertiesOfResourceInstanceOnlyPropertyDefId(String resourceInstanceUid) {
977
978                 return getAllPropertiesOfResourceInstanceOnlyPropertyDefId(resourceInstanceUid, NodeTypeEnum.ResourceInstance);
979
980         }
981
982         public Either<PropertyValueData, JanusGraphOperationStatus> removePropertyOfResourceInstance(String propertyValueUid, String resourceInstanceId) {
983
984                 Either<ComponentInstanceData, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
985                                 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.ResourceInstance), resourceInstanceId, ComponentInstanceData.class);
986
987                 if (findResInstanceRes.isRight()) {
988                         JanusGraphOperationStatus status = findResInstanceRes.right().value();
989                         if (status == JanusGraphOperationStatus.NOT_FOUND) {
990                                 status = JanusGraphOperationStatus.INVALID_ID;
991                         }
992                         return Either.right(status);
993                 }
994
995                 Either<PropertyValueData, JanusGraphOperationStatus> findPropertyDefRes = janusGraphGenericDao
996                                 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.PropertyValue), propertyValueUid, PropertyValueData.class);
997
998                 if (findPropertyDefRes.isRight()) {
999                         JanusGraphOperationStatus status = findPropertyDefRes.right().value();
1000                         if (status == JanusGraphOperationStatus.NOT_FOUND) {
1001                                 status = JanusGraphOperationStatus.INVALID_ID;
1002                         }
1003                         return Either.right(status);
1004                 }
1005
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;
1013                         }
1014                         return Either.right(status);
1015                 }
1016
1017                 Either<PropertyValueData, JanusGraphOperationStatus> deleteNode = janusGraphGenericDao
1018                                 .deleteNode(findPropertyDefRes.left().value(), PropertyValueData.class);
1019                 if (deleteNode.isRight()) {
1020                         return Either.right(deleteNode.right().value());
1021                 }
1022                 PropertyValueData value = deleteNode.left().value();
1023                 return Either.left(value);
1024
1025         }
1026
1027         public Either<ComponentInstanceProperty, StorageOperationStatus> removePropertyValueFromResourceInstance(String propertyValueUid, String resourceInstanceId, boolean inTransaction) {
1028
1029                 Either<ComponentInstanceProperty, StorageOperationStatus> result = null;
1030
1031                 try {
1032
1033                         Either<PropertyValueData, JanusGraphOperationStatus> eitherStatus = this.removePropertyOfResourceInstance(propertyValueUid, resourceInstanceId);
1034
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()));
1038                                 return result;
1039                         } else {
1040                                 PropertyValueData propertyValueData = eitherStatus.left().value();
1041
1042                                 ComponentInstanceProperty propertyValueResult = new ComponentInstanceProperty();
1043                                 propertyValueResult.setUniqueId(resourceInstanceId);
1044                                 propertyValueResult.setValue(propertyValueData.getValue());
1045
1046                                 log.debug("The returned ResourceInstanceProperty is  {}", propertyValueResult);
1047                                 result = Either.left(propertyValueResult);
1048                                 return result;
1049                         }
1050                 }
1051
1052                 finally {
1053                         if (!inTransaction) {
1054                                 if (result == null || result.isRight()) {
1055                                         log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1056                                         janusGraphGenericDao.rollback();
1057                                 } else {
1058                                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1059                                         janusGraphGenericDao.commit();
1060                                 }
1061                         }
1062                 }
1063
1064         }
1065
1066         public ComponentInstanceProperty buildResourceInstanceProperty(PropertyValueData propertyValueData, ComponentInstanceProperty resourceInstanceProperty) {
1067
1068                 String value = propertyValueData.getValue();
1069                 String uid = propertyValueData.getUniqueId();
1070                 ComponentInstanceProperty instanceProperty = new ComponentInstanceProperty(resourceInstanceProperty, value, uid);
1071                 instanceProperty.setPath(resourceInstanceProperty.getPath());
1072
1073                 return instanceProperty;
1074         }
1075
1076
1077         public static class PropertyConstraintJacksonDeserializer extends com.fasterxml.jackson.databind.JsonDeserializer<PropertyConstraint> {
1078
1079                 @Override
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);
1083                         return null;
1084                 }
1085         }
1086
1087         @Override
1088         public boolean isPropertyDefaultValueValid(IComplexDefaultValue propertyDefinition, Map<String, DataTypeDefinition> dataTypes) {
1089                 if (propertyDefinition == null) {
1090                         return false;
1091                 }
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();
1097                         if (def == null) {
1098                                 return false;
1099                         }
1100                         PropertyDataDefinition propDef = def.getProperty();
1101                         if (propDef == null) {
1102                                 return false;
1103                         }
1104                         innerType = propDef.getType();
1105                 }
1106                 String value = propertyDefinition.getDefaultValue();
1107                 if (type != null) {
1108                         return isValidValue(type, value, innerType, dataTypes);
1109                 } else {
1110                         log.trace("The given type {} is not a pre defined one.", propertyType);
1111
1112                         DataTypeDefinition foundDt = dataTypes.get(propertyType);
1113                         if (foundDt != null) {
1114                                 return isValidComplexValue(foundDt, value, dataTypes);
1115                         } else {
1116                                 return false;
1117                         }
1118                 }
1119         }
1120
1121         public boolean isPropertyTypeValid(IComplexDefaultValue property) {
1122
1123                 if (property == null) {
1124                         return false;
1125                 }
1126
1127                 if (ToscaPropertyType.isValidType(property.getType()) == null) {
1128
1129                         Either<Boolean, JanusGraphOperationStatus> definedInDataTypes = isDefinedInDataTypes(property.getType());
1130
1131                         if (definedInDataTypes.isRight()) {
1132                                 return false;
1133                         } else {
1134                                 Boolean isExist = definedInDataTypes.left().value();
1135                                 return isExist.booleanValue();
1136                         }
1137
1138                 }
1139                 return true;
1140         }
1141
1142         @Override
1143         public ImmutablePair<String, Boolean> isPropertyInnerTypeValid(IComplexDefaultValue property, Map<String, DataTypeDefinition> dataTypes) {
1144
1145                 if (property == null) {
1146                         return new ImmutablePair<>(null, false);
1147                 }
1148
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();
1155                         }
1156                 }
1157
1158                 ToscaPropertyType innerToscaType = ToscaPropertyType.isValidType(innerType);
1159
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);
1165                         } else {
1166                                 log.debug("The inner type {} is a data type. Data type definition is {}", innerType, dataTypeDefinition);
1167                         }
1168                 }
1169
1170                 return new ImmutablePair<>(innerType, true);
1171         }
1172
1173         private boolean isValidComplexValue(DataTypeDefinition foundDt, String value, Map<String, DataTypeDefinition> dataTypes) {
1174                 ImmutablePair<JsonElement, Boolean> validateAndUpdate = dataTypeValidatorConverter.validateAndUpdate(value, foundDt, dataTypes);
1175
1176                 log.trace("The result after validating complex value of type {} is {}", foundDt.getName(), validateAndUpdate);
1177
1178                 return validateAndUpdate.right.booleanValue();
1179
1180         }
1181
1182         public Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> getAllPropertiesOfResourceInstanceOnlyPropertyDefId(String resourceInstanceUid, NodeTypeEnum instanceNodeType) {
1183
1184                 Either<JanusGraphVertex, JanusGraphOperationStatus> findResInstanceRes = janusGraphGenericDao
1185                                 .getVertexByProperty(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid);
1186
1187                 if (findResInstanceRes.isRight()) {
1188                         JanusGraphOperationStatus status = findResInstanceRes.right().value();
1189                         if (status == JanusGraphOperationStatus.NOT_FOUND) {
1190                                 status = JanusGraphOperationStatus.INVALID_ID;
1191                         }
1192                         return Either.right(status);
1193                 }
1194
1195                 Either<List<ImmutablePair<JanusGraphVertex, Edge>>, JanusGraphOperationStatus> propertyImplNodes = janusGraphGenericDao
1196                                 .getChildrenVertecies(UniqueIdBuilder.getKeyByNodeType(instanceNodeType), resourceInstanceUid, GraphEdgeLabels.PROPERTY_VALUE);
1197
1198                 if (propertyImplNodes.isRight()) {
1199                         JanusGraphOperationStatus status = propertyImplNodes.right().value();
1200                         return Either.right(status);
1201                 }
1202
1203                 List<ImmutablePair<JanusGraphVertex, Edge>> list = propertyImplNodes.left().value();
1204                 if (list == null || list.isEmpty()) {
1205                         return Either.right(JanusGraphOperationStatus.NOT_FOUND);
1206                 }
1207
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());
1215
1216                         ImmutablePair<JanusGraphVertex, Edge> propertyDefPair = janusGraphGenericDao
1217                                         .getChildVertex(propertyValueDataVertex, GraphEdgeLabels.PROPERTY_IMPL);
1218                         if (propertyDefPair == null) {
1219                                 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
1220                         }
1221
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());
1226
1227                         ComponentInstanceProperty resourceInstanceProperty = new ComponentInstanceProperty();
1228                         // set property original unique id
1229                         resourceInstanceProperty.setUniqueId(propertyUniqueId);
1230                         // set resource id
1231                         // TODO: esofer add resource id
1232                         resourceInstanceProperty.setParentUniqueId(null);
1233                         // set value
1234                         resourceInstanceProperty.setValue(value);
1235                         // set property value unique id
1236                         resourceInstanceProperty.setValueUniqueUid(propertyValueUid);
1237                         // set rules
1238                         resourceInstanceProperty.setRules(propertyValueData.getRules());
1239
1240                         result.add(resourceInstanceProperty);
1241                 }
1242
1243                 return Either.left(result);
1244         }
1245
1246         /**
1247          * Find the default value from the list of component instances. Start the search from the second component instance
1248          * 
1249          * @param pathOfComponentInstances
1250          * @param propertyUniqueId
1251          * @param defaultValue
1252          * @return
1253          */
1254         public Either<String, JanusGraphOperationStatus> findDefaultValueFromSecondPosition(List<String> pathOfComponentInstances, String propertyUniqueId, String defaultValue) {
1255
1256                 log.trace("In find default value: path= {} propertyUniqId={} defaultValue= {}", pathOfComponentInstances, propertyUniqueId, defaultValue);
1257
1258                 if (pathOfComponentInstances == null || pathOfComponentInstances.size() < 2) {
1259                         return Either.left(defaultValue);
1260                 }
1261
1262                 String result = defaultValue;
1263
1264                 for (int i = 1; i < pathOfComponentInstances.size(); i++) {
1265                         String compInstanceId = pathOfComponentInstances.get(i);
1266
1267                         Either<List<ComponentInstanceProperty>, JanusGraphOperationStatus> propertyValuesResult = this.getAllPropertiesOfResourceInstanceOnlyPropertyDefId(compInstanceId, NodeTypeEnum.ResourceInstance);
1268
1269                         log.trace("After fetching properties values of component instance {}. {}", compInstanceId, propertyValuesResult);
1270
1271                         if (propertyValuesResult.isRight()) {
1272                                 JanusGraphOperationStatus status = propertyValuesResult.right().value();
1273                                 if (status != JanusGraphOperationStatus.NOT_FOUND) {
1274                                         return Either.right(status);
1275                                 } else {
1276                                         continue;
1277                                 }
1278                         }
1279
1280                         ComponentInstanceProperty foundCompInstanceProperty = fetchByPropertyUid(propertyValuesResult.left().value(), propertyUniqueId);
1281                         log.trace("After finding the component instance property on{} . {}", compInstanceId, foundCompInstanceProperty);
1282
1283                         if (foundCompInstanceProperty == null) {
1284                                 continue;
1285                         }
1286
1287                         List<PropertyRule> rules = getOrBuildRulesIfNotExists(pathOfComponentInstances.size() - i, pathOfComponentInstances.get(i), foundCompInstanceProperty.getRules(), foundCompInstanceProperty.getValue());
1288
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);
1292
1293                         if (matchedRule != null) {
1294                                 result = matchedRule.getValue();
1295                                 break;
1296                         }
1297
1298                 }
1299
1300                 return Either.left(result);
1301
1302         }
1303
1304         private ComponentInstanceProperty fetchByPropertyUid(List<ComponentInstanceProperty> list, String propertyUniqueId) {
1305
1306                 ComponentInstanceProperty result = null;
1307
1308                 if (list == null) {
1309                         return null;
1310                 }
1311
1312                 for (ComponentInstanceProperty instProperty : list) {
1313                         if (instProperty.getUniqueId().equals(propertyUniqueId)) {
1314                                 result = instProperty;
1315                                 break;
1316                         }
1317                 }
1318
1319                 return result;
1320         }
1321
1322         private List<PropertyRule> getOrBuildRulesIfNotExists(int ruleSize, String compInstanceId, List<PropertyRule> rules, String value) {
1323
1324                 if (rules != null) {
1325                         return rules;
1326                 }
1327
1328                 rules = buildDefaultRule(compInstanceId, ruleSize, value);
1329
1330                 return rules;
1331
1332         }
1333
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());
1338                 }
1339                 return rules;
1340         }
1341
1342         private List<PropertyRule> buildDefaultRule(String componentInstanceId, int size, String value) {
1343
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());
1349                 }
1350                 PropertyRule propertyRule = new PropertyRule(rule, value);
1351                 rules.add(propertyRule);
1352
1353                 return rules;
1354
1355         }
1356
1357         private PropertyRule findMatchRule(List<String> pathOfInstances, int level, List<PropertyRule> rules) {
1358
1359                 PropertyRule propertyRule = null;
1360
1361                 String stringForMatch = buildStringForMatch(pathOfInstances, level);
1362
1363                 String firstCompInstance = pathOfInstances.get(level);
1364
1365                 if (rules != null) {
1366
1367                         for (PropertyRule rule : rules) {
1368
1369                                 int ruleSize = rule.getRule().size();
1370                                 // check the length of the rule equals to the length of the
1371                                 // instances path.
1372                                 if (ruleSize != pathOfInstances.size() - level) {
1373                                         continue;
1374                                 }
1375                                 // check that the rule starts with correct component instance id
1376                                 if (!checkFirstItem(firstCompInstance, rule.getFirstToken())) {
1377                                         continue;
1378                                 }
1379
1380                                 String secondToken = rule.getToken(2);
1381                                 if (secondToken != null && (secondToken.equals(PropertyRule.getForceAll()) || secondToken.equals(PropertyRule.getALL()))) {
1382                                         propertyRule = rule;
1383                                         break;
1384                                 }
1385
1386                                 String patternStr = buildStringForMatch(rule.getRule(), 0);
1387
1388                                 Pattern pattern = Pattern.compile(patternStr);
1389
1390                                 Matcher matcher = pattern.matcher(stringForMatch);
1391
1392                                 if (matcher.matches()) {
1393                                         if (log.isTraceEnabled()) {
1394                                                 log.trace("{} matches the rule {}", stringForMatch, patternStr);
1395                                         }
1396                                         propertyRule = rule;
1397                                         break;
1398                                 }
1399                         }
1400
1401                 }
1402
1403                 return propertyRule;
1404         }
1405
1406         private boolean checkFirstItem(String left, String right) {
1407                 if (left != null && left.equals(right)) {
1408                         return true;
1409                 }
1410                 return false;
1411         }
1412
1413         private String buildStringForMatch(List<String> pathOfInstances, int level) {
1414                 StringBuilder builder = new StringBuilder();
1415
1416                 for (int i = level; i < pathOfInstances.size(); i++) {
1417                         builder.append(pathOfInstances.get(i));
1418                         if (i < pathOfInstances.size() - 1) {
1419                                 builder.append("#");
1420                         }
1421                 }
1422                 return builder.toString();
1423         }
1424
1425         public void updatePropertyByBestMatch(String propertyUniqueId, ComponentInstanceProperty instanceProperty, Map<String, ComponentInstanceProperty> instanceIdToValue) {
1426
1427                 List<String> pathOfInstances = instanceProperty.getPath();
1428                 int level = 0;
1429                 int size = pathOfInstances.size();
1430                 int numberOfMatches = 0;
1431                 for (String instanceId : pathOfInstances) {
1432                         ComponentInstanceProperty componentInstanceProperty = instanceIdToValue.get(instanceId);
1433
1434                         if (componentInstanceProperty != null) {
1435
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.
1440                                 if (level == 0) {
1441                                         instanceProperty.setValueUniqueUid(componentInstanceProperty.getValueUniqueUid());
1442                                         instanceProperty.setRules(rules);
1443                                 }
1444
1445                                 PropertyRule rule = findMatchRule(pathOfInstances, level, rules);
1446                                 if (rule != null) {
1447                                         numberOfMatches++;
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);
1453                                                 }
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);
1460                                                 }
1461                                                 break;
1462                                         }
1463                                 }
1464                         }
1465                         level++;
1466                 }
1467
1468         }
1469
1470         /**
1471          * 
1472          * Add data type to graph.
1473          * 
1474          * 1. Add data type node
1475          * 
1476          * 2. Add edge between the former node to its parent(if exists)
1477          * 
1478          * 3. Add property node and associate it to the node created at #1. (per property & if exists)
1479          * 
1480          * @param dataTypeDefinition
1481          * @return
1482          */
1483         private Either<DataTypeData, JanusGraphOperationStatus> addDataTypeToGraph(DataTypeDefinition dataTypeDefinition) {
1484
1485                 log.debug("Got data type {}", dataTypeDefinition);
1486
1487                 String dtUniqueId = UniqueIdBuilder.buildDataTypeUid(dataTypeDefinition.getName());
1488
1489                 DataTypeData dataTypeData = buildDataTypeData(dataTypeDefinition, dtUniqueId);
1490
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);
1495
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);
1501                 }
1502
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());
1509                 }
1510
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);
1515
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());
1523                         }
1524                 }
1525
1526                 return Either.left(createDataTypeResult.left().value());
1527
1528         }
1529
1530         private DataTypeData buildDataTypeData(DataTypeDefinition dataTypeDefinition, String ctUniqueId) {
1531
1532                 DataTypeData dataTypeData = new DataTypeData(dataTypeDefinition);
1533
1534                 dataTypeData.getDataTypeDataDefinition().setUniqueId(ctUniqueId);
1535                 Long creationDate = dataTypeData.getDataTypeDataDefinition().getCreationTime();
1536                 if (creationDate == null) {
1537                         creationDate = System.currentTimeMillis();
1538                 }
1539                 dataTypeData.getDataTypeDataDefinition().setCreationTime(creationDate);
1540                 dataTypeData.getDataTypeDataDefinition().setModificationTime(creationDate);
1541
1542                 return dataTypeData;
1543         }
1544
1545         /**
1546          * add properties to capability type.
1547          * 
1548          * Per property, add a property node and associate it to the capability type
1549          * 
1550          * @param uniqueId
1551          * @param properties
1552          * @return
1553          */
1554         private Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType(String uniqueId, List<PropertyDefinition> properties) {
1555
1556                 Map<String, PropertyData> propertiesData = new HashMap<>();
1557
1558                 if (properties != null && !properties.isEmpty()) {
1559                         for (PropertyDefinition propertyDefinition : properties) {
1560                                 String propertyName = propertyDefinition.getName();
1561
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());
1567                                 }
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);
1572                                 }
1573
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);
1580                                 }
1581                                 propertiesData.put(propertyName, addPropertyToNodeType.left().value());
1582                         }
1583
1584                         DataTypeData dataTypeData = new DataTypeData();
1585                         dataTypeData.getDataTypeDataDefinition().setUniqueId(uniqueId);
1586                         long modificationTime = System.currentTimeMillis();
1587                         dataTypeData.getDataTypeDataDefinition().setModificationTime(modificationTime);
1588
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);
1596                         } else {
1597                                 log.debug("Update data type uid {}. Set modification time to {}", uniqueId, modificationTime);
1598                         }
1599
1600                 }
1601
1602                 return Either.left(propertiesData);
1603
1604         }
1605
1606         /**
1607          * Build Data type object from graph by unique id
1608          * 
1609          * @param uniqueId
1610          * @return
1611          */
1612         public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUid(String uniqueId) {
1613
1614                 Either<DataTypeDefinition, JanusGraphOperationStatus> result = null;
1615
1616                 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1617                                 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1618
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);
1623                 }
1624
1625                 DataTypeData ctData = dataTypesRes.left().value();
1626                 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1627
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);
1632                 }
1633
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);
1644                                 return result;
1645                         }
1646                 } else {
1647                         // derived from node was found
1648                         ImmutablePair<DataTypeData, GraphEdge> immutablePair = parentNode.left().value();
1649                         DataTypeData parentCT = immutablePair.getKey();
1650
1651                         String parentUniqueId = parentCT.getUniqueId();
1652                         Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(parentUniqueId);
1653
1654                         if (dataTypeByUid.isRight()) {
1655                                 return Either.right(dataTypeByUid.right().value());
1656                         }
1657
1658                         DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value();
1659
1660                         dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
1661
1662                 }
1663                 result = Either.left(dataTypeDefinition);
1664
1665                 return result;
1666         }
1667
1668         private JanusGraphOperationStatus fillProperties(String uniqueId, DataTypeDefinition dataTypeDefinition) {
1669
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;
1677                         } else {
1678                                 return janusGraphOperationStatus;
1679                         }
1680                 } else {
1681                         Map<String, PropertyDefinition> properties = findPropertiesOfNode.left().value();
1682                         if (properties != null && !properties.isEmpty()) {
1683                                 List<PropertyDefinition> listOfProps = new ArrayList<>();
1684
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);
1691                                 }
1692                                 dataTypeDefinition.setProperties(listOfProps);
1693                         }
1694                         return JanusGraphOperationStatus.OK;
1695                 }
1696         }
1697
1698         private Either<DataTypeDefinition, StorageOperationStatus> addDataType(DataTypeDefinition dataTypeDefinition, boolean inTransaction) {
1699
1700                 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1701
1702                 try {
1703
1704                         Either<DataTypeData, JanusGraphOperationStatus> eitherStatus = addDataTypeToGraph(dataTypeDefinition);
1705
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()));
1710                                 return result;
1711                         } else {
1712                                 DataTypeData capabilityTypeData = eitherStatus.left().value();
1713
1714                                 DataTypeDefinition dataTypeDefResult = convertDTDataToDTDefinition(capabilityTypeData);
1715                                 log.debug("The returned CapabilityTypeDefinition is {}", dataTypeDefResult);
1716                                 result = Either.left(dataTypeDefResult);
1717                                 return result;
1718                         }
1719                 } finally {
1720                         if (!inTransaction) {
1721                                 if (result == null || result.isRight()) {
1722                                         log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1723                                         janusGraphGenericDao.rollback();
1724                                 } else {
1725                                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1726                                         janusGraphGenericDao.commit();
1727                                 }
1728                         }
1729                 }
1730
1731         }
1732
1733         @Override
1734         public Either<DataTypeDefinition, StorageOperationStatus> addDataType(DataTypeDefinition dataTypeDefinition) {
1735                 return addDataType(dataTypeDefinition, true);
1736         }
1737
1738         @Override
1739         public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name, boolean inTransaction) {
1740
1741                 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1742                 try {
1743
1744                         String dtUid = UniqueIdBuilder.buildDataTypeUid(name);
1745                         Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUid(dtUid);
1746
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);
1751                                 }
1752                                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(ctResult.right().value()));
1753                                 return result;
1754                         }
1755
1756                         result = Either.left(ctResult.left().value());
1757
1758                         return result;
1759                 } finally {
1760                         if (!inTransaction) {
1761                                 if (result == null || result.isRight()) {
1762                                         log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1763                                         janusGraphGenericDao.rollback();
1764                                 } else {
1765                                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1766                                         janusGraphGenericDao.commit();
1767                                 }
1768                         }
1769                 }
1770
1771         }
1772
1773         @Override
1774         public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByName(String name) {
1775                 return getDataTypeByName(name, true);
1776         }
1777
1778         @Override
1779         public Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name) {
1780                 return getDataTypeByNameWithoutDerived(name, true);
1781         }
1782
1783         private Either<DataTypeDefinition, StorageOperationStatus> getDataTypeByNameWithoutDerived(String name, boolean inTransaction) {
1784
1785                 Either<DataTypeDefinition, StorageOperationStatus> result = null;
1786                 try {
1787
1788                         String uid = UniqueIdBuilder.buildDataTypeUid(name);
1789                         Either<DataTypeDefinition, JanusGraphOperationStatus> ctResult = this.getDataTypeByUidWithoutDerivedDataTypes(uid);
1790
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);
1795                                 }
1796                                 result = Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(ctResult.right().value()));
1797                                 return result;
1798                         }
1799
1800                         result = Either.left(ctResult.left().value());
1801
1802                         return result;
1803                 } finally {
1804                         if (!inTransaction) {
1805                                 if (result == null || result.isRight()) {
1806                                         log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
1807                                         janusGraphGenericDao.rollback();
1808                                 } else {
1809                                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
1810                                         janusGraphGenericDao.commit();
1811                                 }
1812                         }
1813                 }
1814
1815         }
1816
1817         public Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeByUidWithoutDerivedDataTypes(String uniqueId) {
1818
1819                 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1820                                 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1821
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);
1826                 }
1827
1828                 DataTypeData ctData = dataTypesRes.left().value();
1829                 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1830
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);
1835                 }
1836
1837                 return Either.left(dataTypeDefinition);
1838         }
1839
1840         /**
1841          * 
1842          * convert between graph Node object to Java object
1843          * 
1844          * @param dataTypeData
1845          * @return
1846          */
1847         protected DataTypeDefinition convertDTDataToDTDefinition(DataTypeData dataTypeData) {
1848                 log.debug("The object returned after create data type is {}", dataTypeData);
1849
1850         return new DataTypeDefinition(dataTypeData.getDataTypeDataDefinition());
1851         }
1852
1853         private Either<Boolean, JanusGraphOperationStatus> isValidPropertyType(String propertyType) {
1854
1855                 if (propertyType == null || propertyType.isEmpty()) {
1856                         return Either.left(false);
1857                 }
1858
1859                 ToscaPropertyType toscaPropertyType = ToscaPropertyType.isValidType(propertyType);
1860                 if (toscaPropertyType == null) {
1861             return isDefinedInDataTypes(propertyType);
1862                 } else {
1863                         return Either.left(true);
1864                 }
1865         }
1866
1867         public Either<Boolean, JanusGraphOperationStatus> isDefinedInDataTypes(String propertyType) {
1868
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);
1875                         }
1876                         return Either.right(status);
1877                 }
1878
1879                 return Either.left(true);
1880
1881         }
1882
1883         public Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> getAllDataTypes() {
1884
1885                 Map<String, DataTypeDefinition> dataTypes = new HashMap<>();
1886                 Either<Map<String, DataTypeDefinition>, JanusGraphOperationStatus> result = Either.left(dataTypes);
1887
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);
1894                         } else {
1895                                 return result;
1896                         }
1897                 }
1898
1899                 List<DataTypeData> list = getAllDataTypes.left().value();
1900                 if (list != null) {
1901
1902                         log.trace("Number of data types to load is {}" , list.size());
1903
1904                         List<String> collect = list.stream().map(p -> p.getDataTypeDataDefinition().getName()).collect(Collectors.toList());
1905                         log.trace("The data types to load are {}" , collect);
1906
1907                         for (DataTypeData dataTypeData : list) {
1908
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;
1915                                         }
1916                                         return Either.right(status);
1917                                 }
1918                         }
1919                 }
1920
1921                 if (log.isTraceEnabled()) {
1922                         if (result.isRight()) {
1923                                 log.trace("After fetching all data types {}" , result);
1924                         } else {
1925                                 Map<String, DataTypeDefinition> map = result.left().value();
1926                                 if (map != null) {
1927                                         String types = map.keySet().stream().collect(Collectors.joining(",", "[", "]"));
1928                                         log.trace("After fetching all data types {} " , types);
1929                                 }
1930                         }
1931                 }
1932
1933                 return result;
1934         }
1935
1936         /**
1937          * Build Data type object from graph by unique id
1938          * 
1939          * @param uniqueId
1940          * @return
1941          */
1942         private Either<DataTypeDefinition, JanusGraphOperationStatus> getAndAddDataTypeByUid(String uniqueId, Map<String, DataTypeDefinition> allDataTypes) {
1943
1944                 Either<DataTypeDefinition, JanusGraphOperationStatus> result = null;
1945
1946                 if (allDataTypes.containsKey(uniqueId)) {
1947                         return Either.left(allDataTypes.get(uniqueId));
1948                 }
1949
1950                 Either<DataTypeData, JanusGraphOperationStatus> dataTypesRes = janusGraphGenericDao
1951                                 .getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.DataType), uniqueId, DataTypeData.class);
1952
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);
1957                 }
1958
1959                 DataTypeData ctData = dataTypesRes.left().value();
1960                 DataTypeDefinition dataTypeDefinition = new DataTypeDefinition(ctData.getDataTypeDataDefinition());
1961
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);
1966                 }
1967
1968                 allDataTypes.put(dataTypeDefinition.getName(), dataTypeDefinition);
1969
1970                 String derivedFrom = dataTypeDefinition.getDerivedFromName();
1971                 if (allDataTypes.containsKey(derivedFrom)) {
1972                         DataTypeDefinition parentDataTypeDefinition = allDataTypes.get(derivedFrom);
1973
1974                         dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
1975
1976                         return Either.left(dataTypeDefinition);
1977                 }
1978
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);
1989                                 return result;
1990                         }
1991                 } else {
1992                         // derived from node was found
1993                         ImmutablePair<DataTypeData, GraphEdge> immutablePair = parentNode.left().value();
1994                         DataTypeData parentCT = immutablePair.getKey();
1995
1996                         String parentUniqueId = parentCT.getUniqueId();
1997                         Either<DataTypeDefinition, JanusGraphOperationStatus> dataTypeByUid = getDataTypeByUid(parentUniqueId);
1998
1999                         if (dataTypeByUid.isRight()) {
2000                                 return Either.right(dataTypeByUid.right().value());
2001                         }
2002
2003                         DataTypeDefinition parentDataTypeDefinition = dataTypeByUid.left().value();
2004
2005                         dataTypeDefinition.setDerivedFrom(parentDataTypeDefinition);
2006
2007                 }
2008                 result = Either.left(dataTypeDefinition);
2009
2010                 return result;
2011         }
2012
2013         private Either<DataTypeDefinition, JanusGraphOperationStatus> getDataTypeUsingName(String name) {
2014                 String uid = UniqueIdBuilder.buildDataTypeUid(name);
2015                 return getDataTypeByUid(uid);
2016         }
2017
2018         public Either<String, JanusGraphOperationStatus> checkInnerType(PropertyDataDefinition propDataDef) {
2019
2020                 String propertyType = propDataDef.getType();
2021
2022                 ToscaPropertyType type = ToscaPropertyType.isValidType(propertyType);
2023
2024                 return getInnerType(type, propDataDef::getSchema);
2025         }
2026
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);
2035                         }
2036                 }
2037                 return getAllDataTypes;
2038         }
2039
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);
2043
2044                 if (isValidate) {
2045
2046                         if (type == null) {
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);
2052                                 }
2053                                 JsonElement jsonElement = validateResult.left;
2054                                 String valueFromJsonElement = getValueFromJsonElement(jsonElement);
2055                                 return Either.left(valueFromJsonElement);
2056                         }
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);
2062                         }
2063                 }
2064                 Object convertedValue = value;
2065                 if (!isEmptyValue(value) && isValidate) {
2066                         PropertyValueConverter converter = type.getConverter();
2067                         convertedValue = converter.convert(value, innerType, dataTypes);
2068                 }
2069                 return Either.left(convertedValue);
2070         }
2071
2072         public Either<Object, Boolean> validateAndUpdatePropertyValue(String propertyType, String value, String innerType, Map<String, DataTypeDefinition> dataTypes) {
2073                 return validateAndUpdatePropertyValue(propertyType, value, true, innerType, dataTypes);
2074         }
2075
2076         public <T extends GraphNode> Either<List<PropertyDefinition>, StorageOperationStatus> getAllPropertiesRec(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz) {
2077                 return this.findPropertiesOfNode(nodeType, uniqueId)
2078                                 .right()
2079                                 .bind(this::handleNotFoundProperties)
2080                                 .left()
2081                                 .bind(props -> getAllDerivedFromChainProperties(uniqueId, nodeType, clazz, props.values()));
2082         }
2083
2084         private Either<Map<String, PropertyDefinition>, StorageOperationStatus> handleNotFoundProperties(JanusGraphOperationStatus janusGraphOperationStatus) {
2085                 if (janusGraphOperationStatus == JanusGraphOperationStatus.NOT_FOUND) {
2086                         return Either.left(new HashMap<>());
2087                 }
2088                 return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
2089                                 janusGraphOperationStatus));
2090         }
2091
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));
2104                         }
2105                 }
2106                 StorageOperationStatus getDerivedResult = derivedFrom.right().value();
2107                 return isReachedEndOfDerivedFromChain(getDerivedResult) ? Either.left(accumulatedProps) : Either.right(getDerivedResult);
2108         }
2109
2110         private boolean isReachedEndOfDerivedFromChain(StorageOperationStatus getDerivedResult) {
2111                 return getDerivedResult == StorageOperationStatus.NOT_FOUND;
2112         }
2113
2114         /*
2115          * @Override public PropertyOperation getPropertyOperation() { return this; }
2116          */
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();
2121                 }
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);
2126                 }
2127                 return JanusGraphOperationStatus.OK;
2128         }
2129
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;
2137                 }
2138                 return findPropertiesOfNode;
2139                 }
2140         
2141         /**
2142          * add properties to element type.
2143          * 
2144          * Per property, add a property node and associate it to the element type
2145          * 
2146          * @param uniqueId
2147          * @param propertiesMap
2148          *            
2149          * @return
2150          */
2151         protected Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToElementType(String uniqueId, NodeTypeEnum nodeType, Map<String, PropertyDefinition> propertiesMap) {
2152
2153                 Map<String, PropertyData> propertiesData = new HashMap<>();
2154
2155                 if (propertiesMap != null) {
2156
2157                         for (Entry<String, PropertyDefinition> propertyDefinitionEntry : propertiesMap.entrySet()) {
2158                                 String propertyName = propertyDefinitionEntry.getKey();
2159
2160                                 Either<PropertyData, JanusGraphOperationStatus> addPropertyToNodeType = this.addPropertyToNodeType(propertyName, propertyDefinitionEntry.getValue(), nodeType, uniqueId);
2161
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);
2166                                 }
2167                                 propertiesData.put(propertyName, addPropertyToNodeType.left().value());
2168
2169                         }
2170                 }
2171
2172                 return Either.left(propertiesData);
2173
2174         }
2175
2176         public Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToElementType(String uniqueId, NodeTypeEnum elementType, List<PropertyDefinition> properties) {
2177
2178                 Map<String, PropertyDefinition> propMap;
2179                 if (properties == null) {
2180                         propMap = null;
2181                 } else {
2182                         propMap = properties.stream().collect(Collectors.toMap(PropertyDataDefinition::getName, propDef -> propDef));
2183                 }
2184                 return addPropertiesToElementType(uniqueId, elementType, propMap);
2185         }
2186
2187         @Override
2188         public Either<DataTypeDefinition, StorageOperationStatus> updateDataType(DataTypeDefinition newDataTypeDefinition, DataTypeDefinition oldDataTypeDefinition) {
2189                 return updateDataType(newDataTypeDefinition, oldDataTypeDefinition, true);
2190         }
2191
2192         private Either<DataTypeDefinition, StorageOperationStatus> updateDataType(DataTypeDefinition newDataTypeDefinition, DataTypeDefinition oldDataTypeDefinition, boolean inTransaction) {
2193
2194                 Either<DataTypeDefinition, StorageOperationStatus> result = null;
2195
2196                 try {
2197
2198                         List<PropertyDefinition> newProperties = newDataTypeDefinition.getProperties();
2199
2200                         List<PropertyDefinition> oldProperties = oldDataTypeDefinition.getProperties();
2201
2202                         String newDerivedFromName = newDataTypeDefinition.getDerivedFromName();
2203
2204                         String oldDerivedFromName = oldDataTypeDefinition.getDerivedFromName();
2205
2206                         String dataTypeName = newDataTypeDefinition.getName();
2207
2208                         List<PropertyDefinition> propertiesToAdd = new ArrayList<>();
2209                         if (isPropertyOmitted(newProperties, oldProperties, dataTypeName) || isPropertyTypeChanged(dataTypeName, newProperties, oldProperties, propertiesToAdd) || isDerivedFromNameChanged(dataTypeName, newDerivedFromName, oldDerivedFromName)) {
2210
2211                                 log.debug("The new data type {} is invalid.", dataTypeName);
2212
2213                                 result = Either.right(StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY);
2214                                 return result;
2215                         }
2216
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);
2220                                 return result;
2221                         }
2222             Map<String, String> newDescriptions = getPropertyDescriptionsToUpdate(oldProperties, newProperties);
2223
2224             if(MapUtils.isNotEmpty(newDescriptions)){
2225
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));
2231                     return result;
2232                 }
2233             }
2234
2235                         Either<Map<String, PropertyData>, JanusGraphOperationStatus> addPropertiesToDataType = addPropertiesToDataType(oldDataTypeDefinition.getUniqueId(), propertiesToAdd);
2236
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()));
2241                                 return result;
2242                         } else {
2243
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));
2250                                 } else {
2251                                         result = Either.left(dataTypeByUid.left().value());
2252                                 }
2253                         }
2254
2255                         return result;
2256
2257                 } finally {
2258                         if (!inTransaction) {
2259                                 if (result == null || result.isRight()) {
2260                                         log.error(GOING_TO_EXECUTE_ROLLBACK_ON_GRAPH);
2261                                         janusGraphGenericDao.rollback();
2262                                 } else {
2263                                         log.debug(GOING_TO_EXECUTE_COMMIT_ON_GRAPH);
2264                                         janusGraphGenericDao.commit();
2265                                 }
2266                         }
2267                 }
2268
2269         }
2270
2271         private boolean isPropertyTypeChanged(String dataTypeName, List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, List<PropertyDefinition> outputPropertiesToAdd) {
2272
2273                 if (newProperties != null && oldProperties != null) {
2274
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));
2277
2278                         for (Entry<String, PropertyDefinition> newPropertyEntry : newPropsMapper.entrySet()) {
2279
2280                                 String propName = newPropertyEntry.getKey();
2281                                 PropertyDefinition propDef = newPropertyEntry.getValue();
2282
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);
2287                                         continue;
2288                                 }
2289
2290                                 String oldType = oldPropertyDefinition.getType();
2291                                 String oldEntryType = getEntryType(oldPropertyDefinition);
2292
2293                                 String newType = propDef.getType();
2294                                 String newEntryType = getEntryType(propDef);
2295
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);
2298                                         return true;
2299                                 }
2300
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);
2303                                         return true;
2304                                 }
2305
2306                         }
2307
2308                 }
2309
2310                 return false;
2311         }
2312
2313         private boolean equalsEntryTypes(String oldEntryType, String newEntryType) {
2314
2315                 if (oldEntryType == null && newEntryType == null) {
2316                         return true;
2317                 } else if (oldEntryType != null && newEntryType != null) {
2318                         return oldEntryType.equals(newEntryType);
2319                 } else {
2320                         return false;
2321                 }
2322         }
2323
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();
2331                         }
2332                 }
2333                 return entryType;
2334         }
2335
2336         private boolean isPropertyOmitted(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, String dataTypeName) {
2337
2338                 boolean isValid = validateChangeInCaseOfEmptyProperties(newProperties, oldProperties, dataTypeName);
2339                 if (!isValid) {
2340                         log.debug("At least one property is missing in the new data type {}", dataTypeName);
2341                         return false;
2342                 }
2343
2344                 if (newProperties != null && oldProperties != null) {
2345
2346                         List<String> newProps = newProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2347                         List<String> oldProps = oldProperties.stream().map(PropertyDataDefinition::getName).collect(Collectors.toList());
2348
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);
2353                                 return true;
2354                         }
2355
2356                 }
2357                 return false;
2358         }
2359
2360         private boolean validateChangeInCaseOfEmptyProperties(List<PropertyDefinition> newProperties, List<PropertyDefinition> oldProperties, String dataTypeName) {
2361
2362                 if (newProperties != null) {
2363                         if (newProperties.isEmpty()) {
2364                                 newProperties = null;
2365                         }
2366                 }
2367
2368                 if (oldProperties != null) {
2369                         if (oldProperties.isEmpty()) {
2370                                 oldProperties = null;
2371                         }
2372                 }
2373
2374                 if ((newProperties == null && oldProperties == null) || (newProperties != null && oldProperties != null)) {
2375                         return true;
2376                 }
2377
2378                 return false;
2379         }
2380
2381         private boolean isDerivedFromNameChanged(String dataTypeName, String newDerivedFromName, String oldDerivedFromName) {
2382
2383                 if (newDerivedFromName != null) {
2384                         boolean isEqual = newDerivedFromName.equals(oldDerivedFromName);
2385                         if (!isEqual) {
2386                                 log.debug("The new datatype {} derived from another data type {} than the existing one {}", dataTypeName, newDerivedFromName, oldDerivedFromName);
2387                         }
2388                         return !isEqual;
2389                 } else if (oldDerivedFromName == null) {
2390                         return false;
2391                 } else {// new=null, old != null
2392                         log.debug("The new datatype {} derived from another data type {} than the existing one {}", dataTypeName, newDerivedFromName, oldDerivedFromName);
2393                         return true;
2394                 }
2395
2396         }
2397
2398         /**
2399          * @param instanceId
2400          * @param nodeType
2401          * @return
2402          */
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()));
2407             }
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()));
2413             }
2414             Vertex vertex = vertexService.left().value();
2415             
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();
2420             }
2421             
2422             counter++;
2423             vertex.property(GraphPropertiesDictionary.PROPERTY_COUNTER.getProperty(), counter);
2424             
2425             return Either.left(counter);
2426         }
2427
2428
2429         public Either<List<PropertyDefinition>, JanusGraphOperationStatus> validatePropertiesUniqueness(Map<String, PropertyDefinition> inheritedProperties, List<PropertyDefinition> properties) {
2430         Either<List<PropertyDefinition>, JanusGraphOperationStatus> result = Either.left(properties);
2431
2432         for (PropertyDefinition property : properties) {
2433             JanusGraphOperationStatus
2434                                                                 status = validatePropertyUniqueness(inheritedProperties, property);
2435             if (status != JanusGraphOperationStatus.OK) {
2436                 result = Either.right(status);
2437                 break;
2438             }
2439         }
2440
2441         return result;
2442     }
2443
2444     /**
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.
2447      */
2448     private JanusGraphOperationStatus validatePropertyUniqueness(Map<String, PropertyDefinition> inheritedProperties, PropertyDefinition property) {
2449         String propertyName = property.getName();
2450         String propertyType = property.getType();
2451
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;
2458             } else {
2459                 property.setType(defaultProperty.getType());
2460                 String innerType = defaultProperty.getSchemaType();
2461
2462                 PropertyDataDefinition schemaProperty = property.getSchemaProperty();
2463                 if (schemaProperty != null) {
2464                     schemaProperty.setType(innerType);
2465                 }
2466             }
2467         }
2468
2469         return result;
2470     }
2471
2472     private boolean typesMismatch(String type1, String type2) {
2473         return type1 != null && type2 != null && !type2.equals(type1);
2474     }
2475
2476
2477     public <T extends GraphNode> Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> getAllTypePropertiesFromAllDerivedFrom(String nextParentUid,
2478                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          NodeTypeEnum nodeType,
2479                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          Class<T> clazz) {
2480         Map<String, PropertyDefinition> allProperies = new HashMap<>();
2481         return getTypePropertiesFromDerivedFromRecursively(nextParentUid, allProperies, nodeType, clazz);
2482     }
2483
2484     private <T extends GraphNode> Either<Map<String, PropertyDefinition>, JanusGraphOperationStatus> getTypePropertiesFromDerivedFromRecursively(String nextParentUid,
2485                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Map<String, PropertyDefinition> allProperies,
2486                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  NodeTypeEnum nodeType,
2487                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  Class<T> clazz) {
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);
2497             }
2498             else {
2499                 log.debug("#getTypePropertiesFromDerivedFromRecursively - Derived from node is not found with UID {} - this is OK for root.", nextParentUid);
2500                 return Either.left(allProperies);
2501             }
2502         } else {
2503
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());
2513                 } else {
2514                     allProperies.putAll(allPropertiesOfTypeRes.left().value().entrySet().stream().filter(e -> !allProperies.containsKey(e.getKey())).collect(Collectors.toMap(Entry::getKey, Entry::getValue)));
2515                 }
2516             }
2517             return getTypePropertiesFromDerivedFromRecursively(childrenNodes.left().value().get(0).getLeft().getUniqueId(), allProperies, nodeType, clazz);
2518         }
2519     }
2520     private JanusGraphOperationStatus updateDataTypePropertyDescriptions(String uniqueId, Map<String, String> newDescriptions) {
2521
2522         if (MapUtils.isNotEmpty(newDescriptions)) {
2523             Either<List<ImmutablePair<JanusGraphVertex, Edge>>, JanusGraphOperationStatus> getDataTypePropertiesRes = janusGraphGenericDao
2524                                                                 .getChildrenVertecies(GraphPropertiesDictionary.UNIQUE_ID.getProperty(), uniqueId, GraphEdgeLabels.PROPERTY);
2525
2526             if(getDataTypePropertiesRes.isRight()){
2527                 log.debug("#updateDataTypePropertiesDescriptions - Failed to fetch the property verticies of the Data type {} ", uniqueId);
2528                 return getDataTypePropertiesRes.right().value();
2529             }
2530             getDataTypePropertiesRes.left().value().stream()
2531                     .filter(pair -> newDescriptions.containsKey(getPropertyNameFromEdge(pair)))
2532                     .forEach(pair -> setNewDescriptionToVertex(newDescriptions.get(getPropertyNameFromEdge(pair)), pair));
2533
2534         }
2535         return JanusGraphOperationStatus.OK;
2536     }
2537
2538     private JanusGraphVertexProperty<String> setNewDescriptionToVertex(String newDescription, ImmutablePair<JanusGraphVertex, Edge> pair) {
2539         return pair.getLeft().property(GraphPropertiesDictionary.DESCRIPTION.getProperty(), newDescription);
2540     }
2541
2542     private String getPropertyNameFromEdge(ImmutablePair<JanusGraphVertex, Edge> pair) {
2543         return (String) pair.getRight().property(GraphPropertiesDictionary.NAME.getProperty()).value();
2544     }
2545
2546     private Map<String, String> getPropertyDescriptionsToUpdate(List<PropertyDefinition> oldProperties, List<PropertyDefinition> newProperties) {
2547
2548         Map<String, PropertyDefinition> newPropertiesMap = newProperties
2549                 .stream()
2550                 .collect(Collectors.toMap(PropertyDefinition::getName, p->p));
2551
2552         return oldProperties
2553                 .stream()
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()));
2556     }
2557
2558     private boolean descriptionsEqual(PropertyDefinition property, PropertyDefinition otherProperty){
2559         if(StringUtils.isEmpty(property.getDescription()) && StringUtils.isEmpty(otherProperty.getDescription())){
2560             return true;
2561         }
2562         if(StringUtils.isNotEmpty(property.getDescription()) && StringUtils.isEmpty(otherProperty.getDescription())){
2563             return false;
2564         }
2565         if(StringUtils.isEmpty(property.getDescription()) && StringUtils.isNotEmpty(otherProperty.getDescription())){
2566             return false;
2567         }
2568         return property.getDescription().equals(otherProperty.getDescription());
2569     }
2570 }