Remove dead code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / jsongraph / JanusGraphDao.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.dao.jsongraph;
22
23 import org.janusgraph.core.*;
24 import fj.data.Either;
25 import org.apache.commons.collections.MapUtils;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.tinkerpop.gremlin.structure.*;
28 import org.apache.tinkerpop.gremlin.structure.util.ElementHelper;
29 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphClient;
30 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
31 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
32 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
33 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
34 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
35 import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
38 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
39 import org.openecomp.sdc.common.jsongraph.util.CommonUtility;
40 import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum;
41 import org.openecomp.sdc.common.log.wrappers.Logger;
42 import org.springframework.beans.factory.annotation.Qualifier;
43
44 import java.io.IOException;
45 import java.util.*;
46 import java.util.Map.Entry;
47
48 import static org.apache.commons.collections.CollectionUtils.isEmpty;
49
50
51 public class JanusGraphDao {
52     JanusGraphClient janusGraphClient;
53
54     private static Logger logger = Logger.getLogger(JanusGraphDao.class.getName());
55
56     public JanusGraphDao(@Qualifier("janusgraph-client") JanusGraphClient janusGraphClient) {
57         this.janusGraphClient = janusGraphClient;
58         logger.info("** JanusGraphDao created");
59     }
60
61     public JanusGraphOperationStatus commit() {
62         logger.debug("#commit - The operation succeeded. Doing commit...");
63         return janusGraphClient.commit();
64     }
65
66     public JanusGraphOperationStatus rollback() {
67         logger.debug("#rollback - The operation failed. Doing rollback...");
68         return janusGraphClient.rollback();
69     }
70
71     public Either<JanusGraph, JanusGraphOperationStatus> getGraph() {
72         return janusGraphClient.getGraph();
73     }
74
75     /**
76      * 
77      * @param graphVertex
78      * @return
79      */
80     public Either<GraphVertex, JanusGraphOperationStatus> createVertex(GraphVertex graphVertex) {
81         logger.trace("try to create vertex for ID [{}]", graphVertex.getUniqueId());
82         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
83         if (graph.isLeft()) {
84             try {
85                 JanusGraph tGraph = graph.left().value();
86
87                 JanusGraphVertex vertex = tGraph.addVertex();
88
89                 setVertexProperties(vertex, graphVertex);
90
91                 graphVertex.setVertex(vertex);
92
93                 return Either.left(graphVertex);
94
95             } catch (Exception e) {
96                 logger.debug("Failed to create Node for ID [{}]", graphVertex.getUniqueId(), e);
97                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
98             }
99         } else {
100             logger.debug("Failed to create vertex for ID [{}]  {}", graphVertex.getUniqueId(), graph.right().value());
101             return Either.right(graph.right().value());
102         }
103     }
104
105     /**
106      * 
107      * @param name
108      * @param value
109      * @param label
110      * @return
111      */
112     public Either<GraphVertex, JanusGraphOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, VertexTypeEnum label) {
113         return getVertexByPropertyAndLabel(name, value, label, JsonParseFlagEnum.ParseAll);
114     }
115
116     public Either<GraphVertex, JanusGraphOperationStatus> getVertexByLabel(VertexTypeEnum label) {
117         return janusGraphClient.getGraph().left().map(graph -> graph.query().has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices()).left().bind(janusGraphVertices -> getFirstFoundVertex(JsonParseFlagEnum.NoParse, janusGraphVertices));
118     }
119
120     private Either<GraphVertex, JanusGraphOperationStatus> getFirstFoundVertex(JsonParseFlagEnum parseFlag, Iterable<JanusGraphVertex> vertices) {
121         Iterator<JanusGraphVertex> iterator = vertices.iterator();
122         if (iterator.hasNext()) {
123             JanusGraphVertex vertex = iterator.next();
124             GraphVertex graphVertex = createAndFill(vertex, parseFlag);
125
126             return Either.left(graphVertex);
127         }
128         return Either.right(JanusGraphOperationStatus.NOT_FOUND);
129     }
130
131     /**
132      * 
133      * @param name
134      * @param value
135      * @param label
136      * @param parseFlag
137      * @return
138      */
139     public Either<GraphVertex, JanusGraphOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, VertexTypeEnum label, JsonParseFlagEnum parseFlag) {
140
141         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
142         if (graph.isLeft()) {
143             try {
144                 JanusGraph tGraph = graph.left().value();
145
146                 @SuppressWarnings("unchecked")
147                 Iterable<JanusGraphVertex> vertecies = tGraph.query().has(name.getProperty(), value).has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices();
148
149                 java.util.Iterator<JanusGraphVertex> iterator = vertecies.iterator();
150                 if (iterator.hasNext()) {
151                     JanusGraphVertex vertex = iterator.next();
152                     GraphVertex graphVertex = createAndFill(vertex, parseFlag);
153
154                     return Either.left(graphVertex);
155                 }
156                 if (logger.isDebugEnabled()) {
157                     logger.debug("No vertex in graph for key = {}  and value = {}   label = {}", name, value, label);
158                 }
159                 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
160             } catch (Exception e) {
161                 if (logger.isDebugEnabled()) {
162                     logger.debug("Failed to get vertex in graph for key ={} and value = {}  label = {}", name, value, label);
163                 }
164                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
165             }
166
167         } else {
168             if (logger.isDebugEnabled()) {
169                 logger.debug("No vertex in graph for key ={} and value = {}  label = {} error :{}", name, value, label, graph.right().value());
170             }
171             return Either.right(graph.right().value());
172         }
173     }
174
175     /**
176      * 
177      * @param id
178      * @return
179      */
180     public Either<GraphVertex, JanusGraphOperationStatus> getVertexById(String id) {
181         return getVertexById(id, JsonParseFlagEnum.ParseAll);
182     }
183
184     /**
185      * 
186      * @param id
187      * @param parseFlag
188      * @return
189      */
190     public Either<GraphVertex, JanusGraphOperationStatus> getVertexById(String id, JsonParseFlagEnum parseFlag) {
191
192         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
193         if (id == null) {
194             if (logger.isDebugEnabled()) {
195                 logger.debug("No vertex in graph for id = {} ", id);
196             }
197             return Either.right(JanusGraphOperationStatus.NOT_FOUND);
198         }
199         if (graph.isLeft()) {
200             try {
201                 JanusGraph tGraph = graph.left().value();
202
203                 @SuppressWarnings("unchecked")
204                 Iterable<JanusGraphVertex> vertecies = tGraph.query().has(GraphPropertyEnum.UNIQUE_ID.getProperty(), id).vertices();
205
206                 java.util.Iterator<JanusGraphVertex> iterator = vertecies.iterator();
207                 if (iterator.hasNext()) {
208                     JanusGraphVertex vertex = iterator.next();
209                     GraphVertex graphVertex = createAndFill(vertex, parseFlag);
210                     return Either.left(graphVertex);
211                 } else {
212                     if (logger.isDebugEnabled()) {
213                         logger.debug("No vertex in graph for id = {}", id);
214                     }
215                     return Either.right(JanusGraphOperationStatus.NOT_FOUND);
216                 }
217             } catch (Exception e) {
218                 if (logger.isDebugEnabled()) {
219                     logger.debug("Failed to get vertex in graph for id {} ", id);
220                 }
221                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
222             }
223         } else {
224             if (logger.isDebugEnabled()) {
225                 logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value());
226             }
227             return Either.right(graph.right().value());
228         }
229     }
230
231     private void setVertexProperties(JanusGraphVertex vertex, GraphVertex graphVertex) throws IOException {
232
233         if (graphVertex.getMetadataProperties() != null) {
234             for (Map.Entry<GraphPropertyEnum, Object> entry : graphVertex.getMetadataProperties().entrySet()) {
235                 if (entry.getValue() != null) {
236                     vertex.property(entry.getKey().getProperty(), entry.getValue());
237                 }
238             }
239         }
240         vertex.property(GraphPropertyEnum.LABEL.getProperty(), graphVertex.getLabel().getName());
241
242         Map<String, ? extends ToscaDataDefinition> json = graphVertex.getJson();
243         if (json != null) {
244             String jsonStr = JsonParserUtils.toJson(json);
245             vertex.property(GraphPropertyEnum.JSON.getProperty(), jsonStr);
246
247         }
248         Map<String, Object> jsonMetadata = graphVertex.getMetadataJson();
249         if (jsonMetadata != null) {
250             String jsonMetadataStr = JsonParserUtils.toJson(jsonMetadata);
251             vertex.property(GraphPropertyEnum.METADATA.getProperty(), jsonMetadataStr);
252         }
253     }
254
255     public void setVertexProperties(Vertex vertex, Map<String, Object> properties) throws IOException {
256         for (Map.Entry<String, Object> entry : properties.entrySet()) {
257             if (entry.getValue() != null) {
258                 vertex.property(entry.getKey(), entry.getValue());
259             }
260         }
261     }
262
263     private GraphVertex createAndFill(JanusGraphVertex vertex, JsonParseFlagEnum parseFlag) {
264         GraphVertex graphVertex = new GraphVertex();
265         graphVertex.setVertex(vertex);
266         parseVertexProperties(graphVertex, parseFlag);
267         return graphVertex;
268     }
269
270     public void parseVertexProperties(GraphVertex graphVertex, JsonParseFlagEnum parseFlag) {
271         JanusGraphVertex vertex = graphVertex.getVertex();
272         Map<GraphPropertyEnum, Object> properties = getVertexProperties(vertex);
273         VertexTypeEnum label = VertexTypeEnum.getByName((String) (properties.get(GraphPropertyEnum.LABEL)));
274         for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) {
275             GraphPropertyEnum key = entry.getKey();
276             switch (key) {
277             case UNIQUE_ID:
278                 graphVertex.setUniqueId((String) entry.getValue());
279                 break;
280             case LABEL:
281                 graphVertex.setLabel(VertexTypeEnum.getByName((String) entry.getValue()));
282                 break;
283             case COMPONENT_TYPE:
284                 String type = (String) entry.getValue();
285                 if (type != null) {
286                     graphVertex.setType(ComponentTypeEnum.valueOf(type));
287                 }
288                 break;
289             case JSON:
290                 if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseJson) {
291                     String json = (String) entry.getValue();
292                     Map<String, ? extends ToscaDataDefinition> jsonObj = JsonParserUtils.toMap(json, label.getClassOfJson());
293                     graphVertex.setJson(jsonObj);
294                 }
295                 break;
296             case METADATA:
297                 if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseMetadata) {
298                     String json = (String) entry.getValue();
299                     Map<String, Object> metadatObj = JsonParserUtils.toMap(json);
300                     graphVertex.setMetadataJson(metadatObj);
301                 }
302                 break;
303             default:
304                 graphVertex.addMetadataProperty(key, entry.getValue());
305                 break;
306             }
307         }
308     }
309
310     public JanusGraphOperationStatus createEdge(GraphVertex from, GraphVertex to, EdgeLabelEnum label, Map<EdgePropertyEnum, Object> properties) {
311         return createEdge(from.getVertex(), to.getVertex(), label, properties);
312     }
313
314     public JanusGraphOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Map<EdgePropertyEnum, Object> properties) {
315         if (logger.isTraceEnabled()) {
316             logger.trace("Try to connect {} with {} label {} properties {}",
317                     from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()),
318                     to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), label, properties);
319         }
320         if (from == null || to == null) {
321             logger.trace("No JanusGraph vertex for id from {} or id to {}",
322                     from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()),
323                     to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()));
324             return JanusGraphOperationStatus.NOT_FOUND;
325         }
326         Edge edge = from.addEdge(label.name(), to);
327         JanusGraphOperationStatus status;
328         try {
329             setEdgeProperties(edge, properties);
330             status = JanusGraphOperationStatus.OK;
331         } catch (IOException e) {
332             logger.debug("Failed to set properties on edge  properties [{}]", properties, e);
333             status = JanusGraphOperationStatus.GENERAL_ERROR;
334         }
335         return status;
336     }
337
338     public Map<GraphPropertyEnum, Object> getVertexProperties(Element element) {
339
340         Map<GraphPropertyEnum, Object> result = new HashMap<>();
341
342         if (element != null && element.keys() != null && element.keys().size() > 0) {
343             Map<String, Property> propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()]));
344
345             for (Entry<String, Property> entry : propertyMap.entrySet()) {
346                 String key = entry.getKey();
347                 Object value = entry.getValue().value();
348
349                 GraphPropertyEnum valueOf = GraphPropertyEnum.getByProperty(key);
350                 if (valueOf != null) {
351                     result.put(valueOf, value);
352                 }
353             }
354             // add print to properties that can't be converted by enum
355         }
356         return result;
357     }
358
359     public Map<EdgePropertyEnum, Object> getEdgeProperties(Element element) {
360
361         Map<EdgePropertyEnum, Object> result = new HashMap<>();
362
363         if (element != null && element.keys() != null && element.keys().size() > 0) {
364             Map<String, Property> propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()]));
365
366             for (Entry<String, Property> entry : propertyMap.entrySet()) {
367                 String key = entry.getKey();
368                 Object value = entry.getValue().value();
369
370                 EdgePropertyEnum valueOf = EdgePropertyEnum.getByProperty(key);
371                 if (valueOf != null) {
372                     if (valueOf == EdgePropertyEnum.INSTANCES) {
373                         List<String> list = JsonParserUtils.toList((String) value, String.class);
374                         result.put(valueOf, list);
375                     } else {
376                         result.put(valueOf, value);
377                     }
378                 }
379             }
380         }
381         return result;
382     }
383
384     public void setEdgeProperties(Element element, Map<EdgePropertyEnum, Object> properties) throws IOException {
385
386         if (properties != null && !properties.isEmpty()) {
387
388             Object[] propertyKeyValues = new Object[properties.size() * 2];
389             int i = 0;
390             for (Entry<EdgePropertyEnum, Object> entry : properties.entrySet()) {
391                 propertyKeyValues[i++] = entry.getKey().getProperty();
392                 Object value = entry.getValue();
393                 if (entry.getKey() == EdgePropertyEnum.INSTANCES) {
394                     String jsonStr = JsonParserUtils.toJson(value);
395                     propertyKeyValues[i++] = jsonStr;
396                 } else {
397                     propertyKeyValues[i++] = entry.getValue();
398                 }
399             }
400             ElementHelper.attachProperties(element, propertyKeyValues);
401         }
402     }
403
404     public Either<List<GraphVertex>, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props) {
405         return getByCriteria(type, props, JsonParseFlagEnum.ParseAll);
406     }
407
408     public Either<List<GraphVertex>, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props, JsonParseFlagEnum parseFlag) {
409         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
410         if (graph.isLeft()) {
411             try {
412                 JanusGraph tGraph = graph.left().value();
413
414                 JanusGraphQuery<? extends JanusGraphQuery> query = tGraph.query();
415                 if (type != null) {
416                     query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName());
417                 }
418
419                 if (props != null && !props.isEmpty()) {
420                     for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) {
421                         query = query.has(entry.getKey().getProperty(), entry.getValue());
422                     }
423                 }
424                 Iterable<JanusGraphVertex> vertices = query.vertices();
425                 if (vertices == null) {
426                     return Either.right(JanusGraphOperationStatus.NOT_FOUND);
427                 }
428
429                 Iterator<JanusGraphVertex> iterator = vertices.iterator();
430                 List<GraphVertex> result = new ArrayList<>();
431
432                 while (iterator.hasNext()) {
433                     JanusGraphVertex vertex = iterator.next();
434
435                     Map<GraphPropertyEnum, Object> newProp = getVertexProperties(vertex);
436                     GraphVertex graphVertex = createAndFill(vertex, parseFlag);
437
438                     result.add(graphVertex);
439                 }
440                 if (logger.isDebugEnabled()) {
441                     logger.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size());
442                 }
443                 if (result.size() == 0) {
444                     return Either.right(JanusGraphOperationStatus.NOT_FOUND);
445                 }
446
447                 return Either.left(result);
448             } catch (Exception e) {
449                 if (logger.isDebugEnabled()) {
450                     logger.debug("Failed  get by  criteria for type = {} and properties = {}", type, props, e);
451                 }
452                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
453             }
454
455         } else {
456             if (logger.isDebugEnabled()) {
457                 logger.debug("Failed  get by  criteria for type ={} and properties = {} error : {}", type, props, graph.right().value());
458             }
459             return Either.right(graph.right().value());
460         }
461     }
462
463     public Either<List<GraphVertex>, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props, Map<GraphPropertyEnum, Object> hasNotProps, JsonParseFlagEnum parseFlag) {
464         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
465         if (graph.isLeft()) {
466             try {
467                 JanusGraph tGraph = graph.left().value();
468
469                 JanusGraphQuery<? extends JanusGraphQuery> query = tGraph.query();
470                 if (type != null) {
471                     query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName());
472                 }
473
474                 if (props != null && !props.isEmpty()) {
475                     for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) {
476                         query = query.has(entry.getKey().getProperty(), entry.getValue());
477                     }
478                 }
479                 if (hasNotProps != null && !hasNotProps.isEmpty()) {
480                     for (Map.Entry<GraphPropertyEnum, Object> entry : hasNotProps.entrySet()) {
481                         if (entry.getValue() instanceof List) {
482                             buildMultipleNegateQueryFromList(entry, query);
483                         } else {
484                             query = query.hasNot(entry.getKey().getProperty(), entry.getValue());
485                         }
486                     }
487                 }
488                 Iterable<JanusGraphVertex> vertices = query.vertices();
489                 if (vertices == null) {
490                     return Either.right(JanusGraphOperationStatus.NOT_FOUND);
491                 }
492
493                 Iterator<JanusGraphVertex> iterator = vertices.iterator();
494                 List<GraphVertex> result = new ArrayList<>();
495
496                 while (iterator.hasNext()) {
497                     JanusGraphVertex vertex = iterator.next();
498
499                     Map<GraphPropertyEnum, Object> newProp = getVertexProperties(vertex);
500                     GraphVertex graphVertex = createAndFill(vertex, parseFlag);
501
502                     result.add(graphVertex);
503                 }
504                 if (logger.isDebugEnabled()) {
505                     logger.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size());
506                 }
507                 if (result.size() == 0) {
508                     return Either.right(JanusGraphOperationStatus.NOT_FOUND);
509                 }
510
511                 return Either.left(result);
512             } catch (Exception e) {
513                 if (logger.isDebugEnabled()) {
514                     logger.debug("Failed  get by  criteria for type = {} and properties = {}", type, props, e);
515                 }
516                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
517             }
518
519         } else {
520             if (logger.isDebugEnabled()) {
521                 logger.debug("Failed  get by  criteria for type ={} and properties = {} error : {}", type, props, graph.right().value());
522             }
523             return Either.right(graph.right().value());
524         }
525     }
526
527     public Either<Iterator<Vertex>, JanusGraphOperationStatus> getCatalogOrArchiveVerticies(boolean isCatalog) {
528         Either<JanusGraph, JanusGraphOperationStatus> graph = janusGraphClient.getGraph();
529         if (graph.isLeft()) {
530             try {
531                 JanusGraph tGraph = graph.left().value();
532
533                 String name = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName();
534                 Iterable<JanusGraphVertex> vCatalogIter = tGraph.query().has(GraphPropertyEnum.LABEL.getProperty(), name).vertices();
535                 if (vCatalogIter == null) {
536                     logger.debug("Failed to fetch catalog vertex");
537                     return Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
538                 }
539                 JanusGraphVertex catalogV = vCatalogIter.iterator().next();
540                 if (catalogV == null) {
541                     logger.debug("Failed to fetch catalog vertex");
542                     return Either.right(JanusGraphOperationStatus.GENERAL_ERROR);
543                 }
544                 String edgeLabel = isCatalog ? EdgeLabelEnum.CATALOG_ELEMENT.name() : EdgeLabelEnum.ARCHIVE_ELEMENT.name();
545                 Iterator<Vertex> vertices = catalogV.vertices(Direction.OUT, edgeLabel);
546
547                 return Either.left(vertices);
548             } catch (Exception e) {
549                 if (logger.isDebugEnabled()) {
550                     logger.debug("Failed  get by  criteria: ", e);
551                 }
552                 return Either.right(JanusGraphClient.handleJanusGraphException(e));
553             }
554
555         } else {
556             if (logger.isDebugEnabled()) {
557                 logger.debug("Failed  get by  criteria : ", graph.right().value());
558             }
559             return Either.right(graph.right().value());
560         }
561     }
562
563     private void buildMultipleNegateQueryFromList(Map.Entry<GraphPropertyEnum, Object> entry, JanusGraphQuery query) {
564         List<Object> negateList = (List<Object>) entry.getValue();
565         for (Object listItem : negateList) {
566             query.hasNot(entry.getKey().getProperty(), listItem);
567         }
568     }
569
570     /**
571      * 
572      * @param parentVertex
573      * @param edgeLabel
574      * @param parseFlag
575      * @return
576      */
577     public Either<GraphVertex, JanusGraphOperationStatus> getChildVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
578         Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag);
579         if (childrenVertecies.isRight()) {
580             return Either.right(childrenVertecies.right().value());
581         }
582         return Either.left(childrenVertecies.left().value().get(0));
583     }
584
585     /**
586      *
587      * @param parentVertex
588      * @param edgeLabel
589      * @param parseFlag
590      * @return
591      */
592     public Either<Vertex, JanusGraphOperationStatus> getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
593         Either<List<Vertex>, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag);
594         if (childrenVertecies.isRight()) {
595             return Either.right(childrenVertecies.right().value());
596         }
597         return Either.left(childrenVertecies.left().value().get(0));
598     }
599
600     public Either<GraphVertex, JanusGraphOperationStatus> getParentVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
601         Either<List<GraphVertex>, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag);
602         if (childrenVertecies.isRight()) {
603             return Either.right(childrenVertecies.right().value());
604         }
605         if (isEmpty(childrenVertecies.left().value())){
606             return Either.right(JanusGraphOperationStatus.NOT_FOUND);
607         }
608         return Either.left(childrenVertecies.left().value().get(0));
609     }
610
611     public Either<Vertex, JanusGraphOperationStatus> getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
612         Either<List<Vertex>, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag);
613         if (childrenVertecies.isRight() ) {
614             return Either.right(childrenVertecies.right().value());
615         }
616         if (isEmpty(childrenVertecies.left().value())){
617             return Either.right(JanusGraphOperationStatus.NOT_FOUND);
618         }
619         return Either.left(childrenVertecies.left().value().get(0));
620     }
621
622     /**
623      * 
624      * @param parentVertex
625      * @param edgeLabel
626      * @param parseFlag
627      * @return
628      */
629     public Either<List<GraphVertex>, JanusGraphOperationStatus> getChildrenVertices(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
630         return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.OUT);
631     }
632
633     public Either<List<GraphVertex>, JanusGraphOperationStatus> getParentVertices(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
634         return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN);
635     }
636
637     public Either<List<Vertex>, JanusGraphOperationStatus> getParentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
638         return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN);
639     }
640
641     private Either<List<Vertex>, JanusGraphOperationStatus> getAdjacentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) {
642         List<Vertex> list = new ArrayList<>();
643         try {
644             Either<JanusGraph, JanusGraphOperationStatus> graphRes = janusGraphClient.getGraph();
645             if (graphRes.isRight()) {
646                 logger.error("Failed to retrieve graph. status is {}", graphRes);
647                 return Either.right(graphRes.right().value());
648             }
649             Iterator<Edge> edgesCreatorIterator = parentVertex.edges(direction, edgeLabel.name());
650             if (edgesCreatorIterator != null) {
651                 while (edgesCreatorIterator.hasNext()) {
652                     Edge edge = edgesCreatorIterator.next();
653                     JanusGraphVertex vertex;
654                     if (direction == Direction.IN) {
655                         vertex = (JanusGraphVertex) edge.outVertex();
656                     } else {
657                         vertex = (JanusGraphVertex) edge.inVertex();
658                     }
659                     // GraphVertex graphVertex = createAndFill(vertex, parseFlag);
660
661                     list.add(vertex);
662                 }
663             }
664             if (list.isEmpty()) {
665                 return Either.right(JanusGraphOperationStatus.NOT_FOUND);
666             }
667         } catch (Exception e) {
668             logger.error("Failed to perform graph operation ", e);
669             Either.right(JanusGraphClient.handleJanusGraphException(e));
670         }
671
672         return Either.left(list);
673     }
674
675     /**
676      *
677      * @param parentVertex
678      * @param edgeLabel
679      * @param parseFlag
680      * @return
681      */
682     public Either<List<Vertex>, JanusGraphOperationStatus> getChildrenVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) {
683         return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.OUT);
684     }
685
686     private Either<List<GraphVertex>, JanusGraphOperationStatus> getAdjacentVertices(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) {
687         List<GraphVertex> list = new ArrayList<>();
688
689         Either<List<Vertex>, JanusGraphOperationStatus> adjacentVerticies = getAdjacentVertices(parentVertex.getVertex(), edgeLabel, parseFlag, direction);
690         if (adjacentVerticies.isRight()) {
691             return Either.right(adjacentVerticies.right().value());
692         }
693         adjacentVerticies.left().value().stream().forEach(vertex -> {
694             list.add(createAndFill((JanusGraphVertex) vertex, parseFlag));
695         });
696
697         return Either.left(list);
698     }
699
700     /**
701      * Searches Edge by received label and criteria
702      * 
703      * @param vertex
704      * @param label
705      * @param properties
706      * @return found edge or JanusGraphOperationStatus
707      */
708     public Either<Edge, JanusGraphOperationStatus> getBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) {
709
710         Either<Edge, JanusGraphOperationStatus> result = null;
711         Edge matchingEdge = null;
712         String notFoundMsg = "No edges in graph for criteria";
713         try {
714             JanusGraphVertexQuery<?> query = vertex.getVertex().query().labels(label.name());
715
716             if (properties != null && !properties.isEmpty()) {
717                 for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) {
718                     query = query.has(entry.getKey().getProperty(), entry.getValue());
719                 }
720             }
721
722             Iterable<JanusGraphEdge> edges = query.edges();
723             if (edges == null) {
724                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg);
725                 result = Either.right(JanusGraphOperationStatus.NOT_FOUND);
726             } else {
727                 Iterator<JanusGraphEdge> eIter = edges.iterator();
728                 if (eIter.hasNext()) {
729                     matchingEdge = eIter.next();
730                 } else {
731                     CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg);
732                     result = Either.right(JanusGraphOperationStatus.NOT_FOUND);
733                 }
734             }
735             if (result == null) {
736                 result = Either.left(matchingEdge);
737             }
738         } catch (Exception e) {
739             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during getting edge by criteria for component with id {}. {}", vertex.getUniqueId(), e);
740             return Either.right(JanusGraphClient.handleJanusGraphException(e));
741         }
742         return result;
743     }
744
745     public Either<Edge, JanusGraphOperationStatus> getEdgeByChildrenVertexProperties(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) {
746         Either<Edge, JanusGraphOperationStatus> result = null;
747         Edge matchingEdge = null;
748         String notFoundMsg = "No edges in graph for criteria";
749         try {
750
751             Iterator<Edge> edges = vertex.getVertex().edges(Direction.OUT, label.name());
752             while (edges.hasNext()) {
753                 matchingEdge = edges.next();
754                 Vertex childV = matchingEdge.inVertex();
755                 Map<GraphPropertyEnum, Object> vertexProperties = getVertexProperties(childV);
756                 Optional<Entry<GraphPropertyEnum, Object>> findNotMatch = properties.entrySet().stream().filter(e -> vertexProperties.get(e.getKey()) == null || !vertexProperties.get(e.getKey()).equals(e.getValue())).findFirst();
757                 if (!findNotMatch.isPresent()) {
758                     result = Either.left(matchingEdge);
759                 }
760             }
761             if (result == null) {
762                 //no match 
763                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg);
764                 result = Either.right(JanusGraphOperationStatus.NOT_FOUND);
765             }
766         } catch (Exception e) {
767             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during getting edge by criteria for component with id {}. {}", vertex.getUniqueId(), e);
768             return Either.right(JanusGraphClient.handleJanusGraphException(e));
769         }
770         return result;
771     }
772
773     /**
774      * Deletes Edge by received label and criteria
775      * 
776      * @param vertex
777      * @param label
778      * @param properties
779      * @return
780      */
781     public Either<Edge, JanusGraphOperationStatus> deleteBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) {
782         Either<Edge, JanusGraphOperationStatus> result = null;
783         try {
784             result = getBelongingEdgeByCriteria(vertex, label, properties);
785             if (result.isLeft()) {
786                 Edge edge = result.left().value();
787                 CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to delete an edge with the label {} belonging to the vertex {} ", label.name(), vertex.getUniqueId());
788                 edge.remove();
789                 result = Either.left(edge);
790             } else {
791                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to find an edge with the label {} belonging to the vertex {} ", label.name(), vertex.getUniqueId());
792             }
793         } catch (Exception e) {
794             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge by criteria for the component with id {}. {}", vertex == null ? "NULL" : vertex.getUniqueId(), e);
795             return Either.right(JanusGraphClient.handleJanusGraphException(e));
796         }
797         return result;
798     }
799
800     @SuppressWarnings("unchecked")
801     /**
802      * Deletes an edge between vertices fromVertex and toVertex according to received label
803      * 
804      * @param fromVertex
805      * @param toVertex
806      * @param label
807      * @return
808      */
809
810     public Either<Edge, JanusGraphOperationStatus> deleteEdge(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) {
811         return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false);
812     }
813
814     public Either<Edge, JanusGraphOperationStatus> deleteAllEdges(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) {
815         return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true);
816     }
817
818     public Either<Edge, JanusGraphOperationStatus> deleteEdge(JanusGraphVertex fromVertex, JanusGraphVertex toVertex, EdgeLabelEnum label, String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) {
819         Either<Edge, JanusGraphOperationStatus> result = null;
820         Vertex problemV = null;
821         try {
822             Iterable<JanusGraphEdge> edges = fromVertex.query().labels(label.name()).edges();
823             Iterator<JanusGraphEdge> eIter = edges.iterator();
824             while (eIter.hasNext()) {
825                 Edge edge = eIter.next();
826                 problemV = edge.inVertex();
827                 String currVertexUniqueId = null;
828                 try {
829                     currVertexUniqueId = edge.inVertex().value(GraphPropertyEnum.UNIQUE_ID.getProperty());
830                 }catch (Exception e){
831                     // AutoHealing procedure
832                     logger.info( "Corrupted vertex and edge were found and deleted {}",e);
833                     if ( problemV != null ) {
834                         Map<GraphPropertyEnum, Object> props = getVertexProperties(problemV);
835                         logger.debug( "problematic Vertex properties:");
836                         logger.debug( "props size: {}",  props.size());
837                         for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) {
838                             logger.debug( "{}{}",entry.getKey() + ":" + entry.getValue());
839                         }
840                         Either<List<Vertex>, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(problemV, EdgeLabelEnum.VERSION, JsonParseFlagEnum.NoParse);
841                         if(childrenVertices.isLeft()){
842                             childrenVertices.left().value().size();
843                             logger.debug( "number of children that problematic Vertex has: {}",  props.size());
844                         }
845                         try {
846                             edge.remove();
847                         }catch (Exception e1){
848                             logger.debug( "failed to remove problematic edge. {}",  e1);
849                         }
850                         try {
851                             problemV.remove();
852                         }catch (Exception e2){
853                             logger.debug( "failed to remove problematic vertex . {}",  e2);
854                         }
855                     }
856                     continue;
857                 }
858
859                 if (currVertexUniqueId != null && currVertexUniqueId.equals(uniqueIdTo)) {
860                     CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to delete an edge with the label {} between vertices {} and {}. ", label.name(), uniqueIdFrom, uniqueIdTo);
861                     edge.remove();
862                     result = Either.left(edge);
863                     if (!deleteAll) {
864                         break;
865                     }
866                 }
867             }
868             if (result == null) {
869                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete an edge with the label {} between vertices {} and {}. ", label.name(), uniqueIdFrom, uniqueIdTo);
870                 result = Either.right(JanusGraphOperationStatus.NOT_FOUND);
871             }
872         } catch (Exception e) {
873
874             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge with the label {} between vertices {} and {}. {}", label.name(), uniqueIdFrom, uniqueIdTo, e);
875             return Either.right(JanusGraphClient.handleJanusGraphException(e));
876         }
877         return result;
878     }
879
880     public JanusGraphOperationStatus deleteEdgeByDirection(GraphVertex fromVertex, Direction direction, EdgeLabelEnum label) {
881         try {
882             Iterator<Edge> edges = fromVertex.getVertex().edges(direction, label.name());
883
884             while (edges.hasNext()) {
885                 Edge edge = edges.next();
886                 edge.remove();
887             }
888         } catch (Exception e) {
889             logger.debug("Failed to remove from vertex {} edges {} by direction {} ", fromVertex.getUniqueId(), label, direction, e);
890             return JanusGraphClient.handleJanusGraphException(e);
891         }
892         return JanusGraphOperationStatus.OK;
893     }
894
895     /**
896      * Updates vertex properties. Note that graphVertex argument should contain updated data
897      * 
898      * @param graphVertex
899      * @return
900      */
901     public Either<GraphVertex, JanusGraphOperationStatus> updateVertex(GraphVertex graphVertex) {
902         CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId());
903         try {
904             graphVertex.updateMetadataJsonWithCurrentMetadataProperties();
905             setVertexProperties(graphVertex.getVertex(), graphVertex);
906
907         } catch (Exception e) {
908             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId(), e);
909             return Either.right(JanusGraphClient.handleJanusGraphException(e));
910         }
911         return Either.left(graphVertex);
912     }
913
914     /**
915      * Fetches vertices by uniqueId according to received parse flag
916      * 
917      * @param verticesToGet
918      * @return
919      */
920     public Either<Map<String, GraphVertex>, JanusGraphOperationStatus> getVerticesByUniqueIdAndParseFlag(Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet) {
921
922         Either<Map<String, GraphVertex>, JanusGraphOperationStatus> result = null;
923         Map<String, GraphVertex> vertices = new HashMap<>();
924         JanusGraphOperationStatus titatStatus;
925         Either<GraphVertex, JanusGraphOperationStatus> getVertexRes = null;
926         for (Map.Entry<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> entry : verticesToGet.entrySet()) {
927             if (entry.getValue().getKey() == GraphPropertyEnum.UNIQUE_ID) {
928                 getVertexRes = getVertexById(entry.getKey(), entry.getValue().getValue());
929             } else if (entry.getValue().getKey() == GraphPropertyEnum.USERID) {
930                 getVertexRes = getVertexByPropertyAndLabel(entry.getValue().getKey(), entry.getKey(), VertexTypeEnum.USER, entry.getValue().getValue());
931             }
932             if (getVertexRes == null) {
933                 titatStatus = JanusGraphOperationStatus.ILLEGAL_ARGUMENT;
934                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Invalid vertex type label {} has been received. ", entry.getValue().getKey(), titatStatus);
935                 return Either.right(titatStatus);
936             }
937             if (getVertexRes.isRight()) {
938                 titatStatus = getVertexRes.right().value();
939                 CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get vertex by id {} . Status is {}. ", entry.getKey(), titatStatus);
940                 result = Either.right(titatStatus);
941                 break;
942             } else {
943                 vertices.put(entry.getKey(), getVertexRes.left().value());
944             }
945         }
946         if (result == null) {
947             result = Either.left(vertices);
948         }
949         return result;
950     }
951
952     /**
953      * Creates edge between "from" and "to" vertices with specified label and properties extracted from received edge
954      * 
955      * @param from
956      * @param to
957      * @param label
958      * @param edgeToCopy
959      * @return
960      */
961     public JanusGraphOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Edge edgeToCopy) {
962         return createEdge(from, to, label, getEdgeProperties(edgeToCopy));
963     }
964
965     public JanusGraphOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, Edge prevEdge, EdgeLabelEnum prevLabel, EdgeLabelEnum newLabel) {
966         CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel, fromVertex!=null ? fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()) : "NULL",
967                 toVertex!=null ? toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()) : "NULL");
968
969         JanusGraphOperationStatus result = createEdge(fromVertex, toVertex, newLabel, prevEdge);
970         if (result == JanusGraphOperationStatus.OK) {
971             prevEdge.remove();
972         }
973         return result;
974     }
975
976     /**
977      * Replaces previous label of edge with new label
978      * 
979      * @param fromVertex
980      * @param toVertex
981      * @param prevLabel
982      * @param newLabel
983      * @return
984      */
985     public JanusGraphOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, EdgeLabelEnum prevLabel,
986         EdgeLabelEnum newLabel) {
987
988         Iterator<Edge> prevEdgeIter = toVertex.edges(Direction.IN, prevLabel.name());
989         if (prevEdgeIter == null || !prevEdgeIter.hasNext()) {
990             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG,
991                 "Failed to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel,
992                 fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()),
993                 toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()));
994             return JanusGraphOperationStatus.NOT_FOUND;
995         } else {
996             return replaceEdgeLabel(fromVertex, toVertex, prevEdgeIter.next(), prevLabel, newLabel);
997         }
998     }
999
1000     /**
1001      * Updates metadata properties of vertex on graph. Json metadata property of the vertex will be updated with received properties too.
1002      * 
1003      * 
1004      * @param vertex
1005      * @param properties
1006      * @return
1007      */
1008     public JanusGraphOperationStatus updateVertexMetadataPropertiesWithJson(Vertex vertex, Map<GraphPropertyEnum, Object> properties) {
1009         try {
1010             if (!MapUtils.isEmpty(properties)) {
1011                 String jsonMetadataStr = (String) vertex.property(GraphPropertyEnum.METADATA.getProperty()).value();
1012                 Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap(jsonMetadataStr);
1013                 for (Map.Entry<GraphPropertyEnum, Object> property : properties.entrySet()) {
1014                     vertex.property(property.getKey().getProperty(), property.getValue());
1015                     jsonMetadataMap.put(property.getKey().getProperty(), property.getValue());
1016                 }
1017                 vertex.property(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap));
1018             }
1019         } catch (Exception e) {
1020             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occurred during update vertex metadata properties with json{}. {}", vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), e.getMessage());
1021             return JanusGraphClient.handleJanusGraphException(e);
1022         }
1023         return JanusGraphOperationStatus.OK;
1024     }
1025
1026     public JanusGraphOperationStatus disassociateAndDeleteLast(GraphVertex vertex, Direction direction, EdgeLabelEnum label) {
1027         try {
1028             Iterator<Edge> edges = vertex.getVertex().edges(direction, label.name());
1029
1030             while (edges.hasNext()) {
1031                 Edge edge = edges.next();
1032                 Vertex secondVertex;
1033                 Direction reverseDirection;
1034                 if (direction == Direction.IN) {
1035                     secondVertex = edge.outVertex();
1036                     reverseDirection = Direction.OUT;
1037                 } else {
1038                     secondVertex = edge.inVertex();
1039                     reverseDirection = Direction.IN;
1040                 }
1041                 edge.remove();
1042                 CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Edge  {} with direction {} was removed from {}", label.name(), direction, vertex.getVertex());
1043
1044                 Iterator<Edge> restOfEdges = secondVertex.edges(reverseDirection, label.name());
1045                 if (!restOfEdges.hasNext()) {
1046                     secondVertex.remove();
1047                     CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "This was last edge . Vertex  {} was removed ", vertex.getUniqueId());
1048                 }
1049             }
1050         } catch (Exception e) {
1051             CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge with the label {} direction {} from vertex {}. {}", label.name(), direction, vertex.getUniqueId(), e);
1052             return JanusGraphClient.handleJanusGraphException(e);
1053         }
1054         return JanusGraphOperationStatus.OK;
1055     }
1056
1057     public Object getProperty(JanusGraphVertex vertex, String key) {
1058         PropertyKey propertyKey = janusGraphClient.getGraph().left().value().getPropertyKey(key);
1059         return vertex.valueOrNull(propertyKey);
1060     }
1061
1062     public Object getProperty(Edge edge, EdgePropertyEnum key) {
1063         Object value = null;
1064         try {
1065             Property<Object> property = edge.property(key.getProperty());
1066             if (property != null) {
1067                 value = property.orElse(null);
1068                 if (value != null && key == EdgePropertyEnum.INSTANCES) {
1069                     return JsonParserUtils.toList((String) value, String.class);
1070                 }
1071                 return value;
1072             }
1073         } catch (Exception e) {
1074
1075         }
1076         return value;
1077     }
1078
1079     /**
1080      * 
1081      * @param vertexA
1082      * @param vertexB
1083      * @param label
1084      * @param direction
1085      * @return
1086      */
1087     public JanusGraphOperationStatus moveEdge(GraphVertex vertexA, GraphVertex vertexB, EdgeLabelEnum label, Direction direction) {
1088         JanusGraphOperationStatus result = deleteEdgeByDirection(vertexA, direction, label);
1089         if (result != JanusGraphOperationStatus.OK) {
1090             logger.error("Failed to diassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result);
1091             return result;
1092         }
1093         JanusGraphOperationStatus createRelation;
1094         if (direction == Direction.IN) {
1095             createRelation = createEdge(vertexB, vertexA, label, null);
1096         } else {
1097             createRelation = createEdge(vertexA, vertexB, label, null);
1098         }
1099         if (createRelation != JanusGraphOperationStatus.OK) {
1100             return createRelation;
1101         }
1102         return JanusGraphOperationStatus.OK;
1103     }
1104
1105     public Either<Edge, JanusGraphOperationStatus> getBelongingEdgeByCriteria(String parentId, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) {
1106         Either<GraphVertex, JanusGraphOperationStatus> getVertexRes = getVertexById(parentId, JsonParseFlagEnum.NoParse);
1107         if (getVertexRes.isRight()) {
1108             return Either.right(getVertexRes.right().value());
1109         }
1110         return getBelongingEdgeByCriteria(getVertexRes.left().value(), label, properties);
1111     }
1112 }