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