X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=catalog-dao%2Fsrc%2Fmain%2Fjava%2Forg%2Fopenecomp%2Fsdc%2Fbe%2Fdao%2Fjanusgraph%2FJanusGraphDao.java;h=606caeb576896baeb57474fddb894544e5c1b77f;hb=0e1a3ddc9fae2b9ce74be0f14c4f6e08fac7e85b;hp=5d0c4cffb614c08bd09a69ae27ec516260f93ddf;hpb=bc741a1eeb2455903734a5603281cc17ca898d11;p=sdc.git diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java index 5d0c4cffb6..606caeb576 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphDao.java @@ -24,16 +24,20 @@ import static org.apache.commons.collections.CollectionUtils.isEmpty; import fj.data.Either; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import java.util.Optional; +import lombok.Getter; import org.apache.commons.collections.MapUtils; +import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.tinkerpop.gremlin.structure.Direction; @@ -49,6 +53,7 @@ import org.janusgraph.core.JanusGraphVertex; import org.janusgraph.core.JanusGraphVertexQuery; import org.janusgraph.core.PropertyKey; import org.janusgraph.graphdb.query.JanusGraphPredicate; +import org.openecomp.sdc.be.dao.api.exception.JanusGraphException; import org.openecomp.sdc.be.dao.jsongraph.GraphVertex; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum; @@ -58,6 +63,7 @@ import org.openecomp.sdc.be.dao.jsongraph.utils.JsonParserUtils; import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.ModelTypeEnum; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; import org.openecomp.sdc.common.jsongraph.util.CommonUtility; import org.openecomp.sdc.common.jsongraph.util.CommonUtility.LogLevelEnum; @@ -68,7 +74,9 @@ import org.springframework.beans.factory.annotation.Qualifier; public class JanusGraphDao { private static final Logger logger = Logger.getLogger(JanusGraphDao.class); - JanusGraphClient janusGraphClient; + private static final String FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES = "Failed to get by criteria for type '{}' and properties '{}'"; + @Getter + private final JanusGraphClient janusGraphClient; public JanusGraphDao(@Qualifier("janusgraph-client") JanusGraphClient janusGraphClient) { this.janusGraphClient = janusGraphClient; @@ -85,10 +93,6 @@ public class JanusGraphDao { return janusGraphClient.rollback(); } - public Either getGraph() { - return janusGraphClient.getGraph(); - } - /** * @param graphVertex * @return @@ -153,28 +157,22 @@ public class JanusGraphDao { if (graph.isLeft()) { try { JanusGraph tGraph = graph.left().value(); - @SuppressWarnings("unchecked") Iterable vertecies = tGraph.query().has(name.getProperty(), value) + @SuppressWarnings("unchecked") Iterable vertices = tGraph.query().has(name.getProperty(), value) .has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices(); - java.util.Iterator iterator = vertecies.iterator(); + java.util.Iterator iterator = vertices.iterator(); if (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); GraphVertex graphVertex = createAndFill(vertex, parseFlag); return Either.left(graphVertex); } - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for key = {} and value = {} label = {}", name, value, label); - } + logger.debug("No vertex in graph for key = {} and value = {} label = {}", name, value, label); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, label); - } + logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, label); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, graph.right().value()); - } + logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, graph.right().value()); return Either.right(graph.right().value()); } } @@ -195,37 +193,29 @@ public class JanusGraphDao { public Either getVertexById(String id, JsonParseFlagEnum parseFlag) { Either graph = janusGraphClient.getGraph(); if (id == null) { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id = {} ", id); - } + logger.debug("No vertex in graph for id = {} ", id); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } if (graph.isLeft()) { try { JanusGraph tGraph = graph.left().value(); - @SuppressWarnings("unchecked") Iterable vertecies = tGraph.query() + @SuppressWarnings("unchecked") Iterable vertices = tGraph.query() .has(GraphPropertyEnum.UNIQUE_ID.getProperty(), id).vertices(); - java.util.Iterator iterator = vertecies.iterator(); + java.util.Iterator iterator = vertices.iterator(); if (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); GraphVertex graphVertex = createAndFill(vertex, parseFlag); return Either.left(graphVertex); } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id = {}", id); - } + logger.debug("No vertex in graph for id = {}", id); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for id {} ", id); - } + logger.debug("Failed to get vertex in graph for id {} ", id); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); - } + logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); return Either.right(graph.right().value()); } } @@ -251,7 +241,7 @@ public class JanusGraphDao { } } - public void setVertexProperties(Vertex vertex, Map properties) throws IOException { + public void setVertexProperties(Vertex vertex, Map properties) { for (Map.Entry entry : properties.entrySet()) { if (entry.getValue() != null) { vertex.property(entry.getKey(), entry.getValue()); @@ -311,11 +301,9 @@ public class JanusGraphDao { } public JanusGraphOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Map properties) { - if (logger.isTraceEnabled()) { - logger.trace("Try to connect {} with {} label {} properties {}", - from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), - to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), label, properties); - } + logger.trace("Try to connect {} with {} label {} properties {}", + from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), + to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), label, properties); if (from == null || to == null) { logger.trace("No JanusGraph vertex for id from {} or id to {}", from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), @@ -336,7 +324,7 @@ public class JanusGraphDao { public Map getVertexProperties(Element element) { Map result = new HashMap<>(); - if (element != null && element.keys() != null && element.keys().size() > 0) { + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { Map propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); for (Entry entry : propertyMap.entrySet()) { String key = entry.getKey(); @@ -353,7 +341,7 @@ public class JanusGraphDao { public Map getEdgeProperties(Element element) { Map result = new HashMap<>(); - if (element != null && element.keys() != null && element.keys().size() > 0) { + if (element != null && CollectionUtils.isNotEmpty(element.keys())) { Map propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); for (Entry entry : propertyMap.entrySet()) { String key = entry.getKey(); @@ -417,37 +405,38 @@ public class JanusGraphDao { List result = new ArrayList<>(); while (iterator.hasNext()) { JanusGraphVertex vertex = iterator.next(); - Map newProp = getVertexProperties(vertex); + getVertexProperties(vertex); GraphVertex graphVertex = createAndFill(vertex, parseFlag); result.add(graphVertex); } - if (logger.isDebugEnabled()) { - logger - .debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); - } + logger.debug("Number of fetched nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); if (result.size() == 0) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); - } + logger.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); - } + logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); return Either.right(graph.right().value()); } } - public Either, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, Map props, - Map hasNotProps, - JsonParseFlagEnum parseFlag, - String model) { - return getByCriteria(type, props, hasNotProps, null, parseFlag, model); + public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, final Map props, + final Map hasNotProps, + final JsonParseFlagEnum parseFlag, + final String model) { + return getByCriteria(type, props, hasNotProps, null, parseFlag, model, false); + } + + public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, final Map props, + final Map hasNotProps, + final JsonParseFlagEnum parseFlag, + final String model, + final boolean includeNormativeExtensionModels) { + return getByCriteria(type, props, hasNotProps, null, parseFlag, model, includeNormativeExtensionModels); } public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, @@ -456,166 +445,162 @@ public class JanusGraphDao { final Map> predicates, final JsonParseFlagEnum parseFlag, final String model) { + return getByCriteria(type, hasProps, hasNotProps, predicates, parseFlag, model, false); + } + + public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, + final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JsonParseFlagEnum parseFlag, + final String model, + final boolean includeNormativeExtensionModels) { Either graph = janusGraphClient.getGraph(); if (graph.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - JanusGraphQuery query = tGraph.query(); - - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - if (hasProps != null && !hasProps.isEmpty()) { - for (Map.Entry entry : hasProps.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - if (hasNotProps != null && !hasNotProps.isEmpty()) { - for (Map.Entry entry : hasNotProps.entrySet()) { - if (entry.getValue() instanceof List) { - buildMultipleNegateQueryFromList(entry, query); - } else { - query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); - } - } - } - if (predicates != null && !predicates.isEmpty()) { - for (Map.Entry> entry : predicates.entrySet()) { - JanusGraphPredicate predicate = entry.getValue().getKey(); - Object object = entry.getValue().getValue(); - query = query.has(entry.getKey(), predicate, object); - } - } - Iterable vertices = query.vertices(); + JanusGraphQuery query = getJanusGraphQuery(type, hasProps, hasNotProps, predicates, graph.left().value()); + final Iterable vertices = query.vertices(); if (vertices == null || !vertices.iterator().hasNext()) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - List result = new ArrayList<>(); - final Predicate filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel : vertex -> vertexValidForModel(vertex, model); - final List verticesForModel = StreamSupport.stream(vertices.spliterator(), false).filter(filterPredicate).collect(Collectors.toList()); - if (verticesForModel == null || verticesForModel.size() == 0) { + final Predicate filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel + : vertex -> vertexValidForModel(vertex, model, includeNormativeExtensionModels); + final List verticesForModel = StreamSupport.stream(vertices.spliterator(), false).filter(filterPredicate) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(verticesForModel)) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - - verticesForModel.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); - if (logger.isDebugEnabled()) { - logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, - result.size()); - } + + final List result = new ArrayList<>(); + verticesForModel.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); + logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, + result.size()); return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'", type, hasProps, e); - } + logger.debug(FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES, type, hasProps, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); - } + logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); return Either.right(graph.right().value()); } } - + public Either, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, - Map props, Map hasNotProps, - JsonParseFlagEnum parseFlag) { + Map props, + Map hasNotProps, + JsonParseFlagEnum parseFlag) { return getByCriteria(type, props, hasNotProps, null, parseFlag); } public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, - final Map hasProps, final Map hasNotProps, - final Map> predicates, final JsonParseFlagEnum parseFlag) { - Either graph = janusGraphClient.getGraph(); + final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JsonParseFlagEnum parseFlag) { + final Either graph = janusGraphClient.getGraph(); if (graph.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - JanusGraphQuery query = tGraph.query(); - - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - if (hasProps != null && !hasProps.isEmpty()) { - for (Map.Entry entry : hasProps.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - if (hasNotProps != null && !hasNotProps.isEmpty()) { - for (Map.Entry entry : hasNotProps.entrySet()) { - if (entry.getValue() instanceof List) { - buildMultipleNegateQueryFromList(entry, query); - } else { - query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); - } - } - } - if (predicates != null && !predicates.isEmpty()) { - for (Map.Entry> entry : predicates.entrySet()) { - JanusGraphPredicate predicate = entry.getValue().getKey(); - Object object = entry.getValue().getValue(); - query = query.has(entry.getKey(), predicate, object); - } - } + JanusGraphQuery query = getJanusGraphQuery(type, hasProps, hasNotProps, predicates, graph.left().value()); Iterable vertices = query.vertices(); if (vertices == null || !vertices.iterator().hasNext()) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } List result = new ArrayList<>(); - vertices.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); - if (logger.isDebugEnabled()) { - logger.debug( - "Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", - type, hasProps, result.size()); - } + logger.debug("Number of fetched nodes in graph for criteria : from type '{}' and properties '{}' is '{}'", type, hasProps, + result.size()); return Either.left(result); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'", type, hasProps, e); - } + logger.debug(FAILED_TO_GET_BY_CRITERIA_FOR_TYPE_AND_PROPERTIES, type, hasProps, e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, - hasProps, graph.right().value()); - } + logger.debug("Failed to get by criteria for type '{}' and properties '{}'. Error : '{}'", type, hasProps, graph.right().value()); return Either.right(graph.right().value()); } } - private boolean vertexValidForModel(final JanusGraphVertex vertex, final String model) { - final String vertexLabel = (String)vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); + /** + * Finds the vertices that have the given invariant id and any additional property provided. + * + * @param invariantUuid the invariant uuid + * @param additionalPropertiesToMatch any additional property to match along with the {@link GraphPropertyEnum#INVARIANT_UUID} + * @return the list of vertex that has the given invariant uuid + * @throws JanusGraphException if the find operation was returned an error status + */ + public List findAllVertexByInvariantUuid(final String invariantUuid, + final Map additionalPropertiesToMatch) { + final Map propertiesToMatch = new EnumMap<>(GraphPropertyEnum.class); + if (MapUtils.isNotEmpty(additionalPropertiesToMatch)) { + propertiesToMatch.putAll(additionalPropertiesToMatch); + } + propertiesToMatch.put(GraphPropertyEnum.INVARIANT_UUID, invariantUuid); + final Either, JanusGraphOperationStatus> vertexEither = + getByCriteria(null, propertiesToMatch, JsonParseFlagEnum.ParseMetadata); + if (vertexEither.isRight()) { + final JanusGraphOperationStatus status = vertexEither.right().value(); + if (status == JanusGraphOperationStatus.NOT_FOUND) { + return Collections.emptyList(); + } + final String errorMsg = String.format("Couldn't fetch vertex with invariantUUId '%s'. Status was '%s'", invariantUuid, status); + throw new JanusGraphException(status, errorMsg); + } + final List vertices = vertexEither.left().value(); + if (vertices == null || vertices.isEmpty()) { + return Collections.emptyList(); + } + return vertices; + } + + private boolean vertexValidForModel(final JanusGraphVertex vertex, final String model, final boolean includeNormativeExtensions) { + final String vertexLabel = (String) vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); final VertexTypeEnum vertexType = VertexTypeEnum.getByName(vertexLabel); - final GraphEdgeLabels edgeLabel = vertexType.equals(VertexTypeEnum.TOPOLOGY_TEMPLATE) ? GraphEdgeLabels.MODEL : GraphEdgeLabels.MODEL_ELEMENT; - final Either>, JanusGraphOperationStatus> modelVertices = getParentVerticies(vertex, edgeLabel); + final GraphEdgeLabels edgeLabel = VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(vertexType) ? GraphEdgeLabels.MODEL : GraphEdgeLabels.MODEL_ELEMENT; + final Either>, JanusGraphOperationStatus> modelVertices = getParentVertices(vertex, edgeLabel); - return modelVertices.isLeft() && modelVertices.left().value().stream().anyMatch(vertexPair -> modelVertexMatchesModel(vertexPair.getLeft(), model)); + return modelVertices.isLeft() && modelVertices.left().value().stream() + .anyMatch(vertexPair -> modelVertexMatchesModel(vertexPair.getLeft(), model, includeNormativeExtensions)); } - - private boolean modelVertexMatchesModel(final JanusGraphVertex modelVertex, final String model) { - if (model.equals((String)modelVertex.property("name").value())) { + + private boolean modelVertexMatchesModel(final JanusGraphVertex modelVertex, final String model, final boolean includeNormativeExtensions) { + if (model.equals((String) modelVertex.property("name").value())) { return true; } final Either>, JanusGraphOperationStatus> derivedModels = - getParentVerticies(modelVertex, GraphEdgeLabels.DERIVED_FROM); - return derivedModels.isLeft() && derivedModels.left().value().stream().anyMatch(derivedModel ->modelVertexMatchesModel(derivedModel.left, model)); + getParentVertices(modelVertex, GraphEdgeLabels.DERIVED_FROM); + if (derivedModels.isLeft() && derivedModels.left().value().stream() + .anyMatch(derivedModel -> modelVertexMatchesModel(derivedModel.left, model, includeNormativeExtensions))) { + return true; + } + + if (includeNormativeExtensions && isANormativeExtension(modelVertex)) { + final Either, JanusGraphOperationStatus> derivedFromModels = + getChildrenVertices(modelVertex, EdgeLabelEnum.DERIVED_FROM); + return derivedFromModels.isLeft() && derivedFromModels.left().value().stream() + .anyMatch(derivedFromModel -> model.equals((String) derivedFromModel.property("name").value())); + } + return false; + } + + private boolean isANormativeExtension(final JanusGraphVertex modelVertex) { + return ModelTypeEnum.NORMATIVE_EXTENSION.getValue().equals((String) modelVertex.property(GraphPropertyEnum.MODEL_TYPE.getProperty()).value()); } - - private Either>, JanusGraphOperationStatus> getParentVerticies( - final JanusGraphVertex rootVertex, final GraphEdgeLabels edgeType) { - return getEdgeVerticies(rootVertex, Direction.IN, edgeType); + + private Either>, JanusGraphOperationStatus> getParentVertices( + final JanusGraphVertex rootVertex, final GraphEdgeLabels edgeType) { + return getEdgeVertices(rootVertex, Direction.IN, edgeType); } - - private Either>, JanusGraphOperationStatus> getEdgeVerticies( - final JanusGraphVertex rootVertex, final Direction direction, final GraphEdgeLabels edgeType) { + + private Either>, JanusGraphOperationStatus> getEdgeVertices( + final JanusGraphVertex rootVertex, final Direction direction, final GraphEdgeLabels edgeType) { final List> immutablePairs = new ArrayList<>(); final Iterator edgesCreatorIterator = rootVertex.edges(direction, edgeType.getProperty()); if (edgesCreatorIterator != null) { while (edgesCreatorIterator.hasNext()) { Edge edge = edgesCreatorIterator.next(); - JanusGraphVertex vertex = Direction.OUT.equals(direction)? (JanusGraphVertex) edge.inVertex() : (JanusGraphVertex) edge.outVertex(); + JanusGraphVertex vertex = Direction.OUT.equals(direction) ? (JanusGraphVertex) edge.inVertex() : (JanusGraphVertex) edge.outVertex(); ImmutablePair immutablePair = new ImmutablePair<>(vertex, edge); immutablePairs.add(immutablePair); } @@ -625,50 +610,45 @@ public class JanusGraphDao { } return Either.left(immutablePairs); } - + private boolean vertexNotConnectedToAnyModel(final JanusGraphVertex vertex) { - String vt = (String)vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); + String vt = (String) vertex.property(GraphPropertyEnum.LABEL.getProperty()).value(); VertexTypeEnum vertexType = VertexTypeEnum.getByName(vt); - EdgeLabelEnum edgeLabel = vertexType.equals(VertexTypeEnum.TOPOLOGY_TEMPLATE) ? EdgeLabelEnum.MODEL : EdgeLabelEnum.MODEL_ELEMENT; + EdgeLabelEnum edgeLabel = VertexTypeEnum.TOPOLOGY_TEMPLATE.equals(vertexType) ? EdgeLabelEnum.MODEL : EdgeLabelEnum.MODEL_ELEMENT; return !vertex.edges(Direction.IN, edgeLabel.name()).hasNext(); } - public Either, JanusGraphOperationStatus> getCatalogOrArchiveVerticies(boolean isCatalog) { - Either graph = janusGraphClient.getGraph(); - if (graph.isLeft()) { + public Either, JanusGraphOperationStatus> getCatalogOrArchiveVertices(boolean isCatalog) { + Either graphEither = janusGraphClient.getGraph(); + if (graphEither.isLeft()) { try { - JanusGraph tGraph = graph.left().value(); - String name = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName(); - Iterable vCatalogIter = tGraph.query().has(GraphPropertyEnum.LABEL.getProperty(), name).vertices(); - if (vCatalogIter == null) { + JanusGraph graph = graphEither.left().value(); + String vertexType = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName(); + Iterable vertexIterable = graph.query().has(GraphPropertyEnum.LABEL.getProperty(), vertexType).vertices(); + if (vertexIterable == null) { logger.debug("Failed to fetch catalog vertex"); return Either.right(JanusGraphOperationStatus.GENERAL_ERROR); } - JanusGraphVertex catalogV = vCatalogIter.iterator().next(); - if (catalogV == null) { + JanusGraphVertex catalogVertex = vertexIterable.iterator().next(); + if (catalogVertex == null) { logger.debug("Failed to fetch catalog vertex"); return Either.right(JanusGraphOperationStatus.GENERAL_ERROR); } String edgeLabel = isCatalog ? EdgeLabelEnum.CATALOG_ELEMENT.name() : EdgeLabelEnum.ARCHIVE_ELEMENT.name(); - Iterator vertices = catalogV.vertices(Direction.OUT, edgeLabel); - return Either.left(vertices); + Iterator adjacentVertices = catalogVertex.vertices(Direction.OUT, edgeLabel); + return Either.left(adjacentVertices); } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria: ", e); - } + logger.debug("Failed get by criteria: ", e); return Either.right(JanusGraphClient.handleJanusGraphException(e)); } } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria : ", graph.right().value()); - } - return Either.right(graph.right().value()); + logger.debug("Failed get by criteria : {}", graphEither.right().value()); + return Either.right(graphEither.right().value()); } } private void buildMultipleNegateQueryFromList(Map.Entry entry, JanusGraphQuery query) { - List negateList = (List) entry.getValue(); - for (Object listItem : negateList) { + for (Object listItem : (List) entry.getValue()) { query.hasNot(entry.getKey().getProperty(), listItem); } } @@ -681,48 +661,47 @@ public class JanusGraphDao { */ public Either getChildVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(parentVertex, edgeLabel, parseFlag); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } /** * @param parentVertex * @param edgeLabel - * @param parseFlag * @return */ - public Either getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getChildrenVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + public Either getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(parentVertex, edgeLabel); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } public Either getParentVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + Either, JanusGraphOperationStatus> childrenVertices = getParentVertices(parentVertex, edgeLabel, parseFlag); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - if (isEmpty(childrenVertecies.left().value())) { + if (isEmpty(childrenVertices.left().value())) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } - public Either getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - Either, JanusGraphOperationStatus> childrenVertecies = getParentVertices(parentVertex, edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); + public Either getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + Either, JanusGraphOperationStatus> childrenVertices = getParentVertices(parentVertex, edgeLabel); + if (childrenVertices.isRight()) { + return Either.right(childrenVertices.right().value()); } - if (isEmpty(childrenVertecies.left().value())) { + if (isEmpty(childrenVertices.left().value())) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - return Either.left(childrenVertecies.left().value().get(0)); + return Either.left(childrenVertices.left().value().get(0)); } /** @@ -741,13 +720,11 @@ public class JanusGraphDao { return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN); } - public Either, JanusGraphOperationStatus> getParentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.IN); + public Either, JanusGraphOperationStatus> getParentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + return getAdjacentVertices(parentVertex, edgeLabel, Direction.IN); } - private Either, JanusGraphOperationStatus> getAdjacentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag, Direction direction) { + private Either, JanusGraphOperationStatus> getAdjacentVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, Direction direction) { List list = new ArrayList<>(); try { Either graphRes = janusGraphClient.getGraph(); @@ -765,7 +742,6 @@ public class JanusGraphDao { } else { vertex = (JanusGraphVertex) edge.inVertex(); } - // GraphVertex graphVertex = createAndFill(vertex, parseFlag); list.add(vertex); } } @@ -782,25 +758,21 @@ public class JanusGraphDao { /** * @param parentVertex * @param edgeLabel - * @param parseFlag * @return */ - public Either, JanusGraphOperationStatus> getChildrenVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVertices(parentVertex, edgeLabel, parseFlag, Direction.OUT); + public Either, JanusGraphOperationStatus> getChildrenVertices(Vertex parentVertex, EdgeLabelEnum edgeLabel) { + return getAdjacentVertices(parentVertex, edgeLabel, Direction.OUT); } private Either, JanusGraphOperationStatus> getAdjacentVertices(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { List list = new ArrayList<>(); - Either, JanusGraphOperationStatus> adjacentVerticies = getAdjacentVertices(parentVertex.getVertex(), edgeLabel, parseFlag, + Either, JanusGraphOperationStatus> adjacentVertices = getAdjacentVertices(parentVertex.getVertex(), edgeLabel, direction); - if (adjacentVerticies.isRight()) { - return Either.right(adjacentVerticies.right().value()); + if (adjacentVertices.isRight()) { + return Either.right(adjacentVertices.right().value()); } - adjacentVerticies.left().value().stream().forEach(vertex -> { - list.add(createAndFill((JanusGraphVertex) vertex, parseFlag)); - }); + adjacentVertices.left().value().stream().forEach(vertex -> list.add(createAndFill((JanusGraphVertex) vertex, parseFlag))); return Either.left(list); } @@ -922,15 +894,15 @@ public class JanusGraphDao { * @return */ public Either deleteEdge(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { - return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false); + return deleteEdge(fromVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false); } public Either deleteAllEdges(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { - return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true); + return deleteEdge(fromVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true); } - public Either deleteEdge(JanusGraphVertex fromVertex, JanusGraphVertex toVertex, EdgeLabelEnum label, - String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) { + private Either deleteEdge(JanusGraphVertex fromVertex, EdgeLabelEnum label, + String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) { Either result = null; Vertex problemV = null; try { @@ -944,16 +916,15 @@ public class JanusGraphDao { currVertexUniqueId = edge.inVertex().value(GraphPropertyEnum.UNIQUE_ID.getProperty()); } catch (Exception e) { // AutoHealing procedure - logger.info("Corrupted vertex and edge were found and deleted {}", e); + logger.info("Corrupted vertex and edge were found and deleted ", e); if (problemV != null) { Map props = getVertexProperties(problemV); logger.debug("problematic Vertex properties:"); logger.debug("props size: {}", props.size()); for (Map.Entry entry : props.entrySet()) { - logger.debug("{}{}", entry.getKey() + ":" + entry.getValue()); + logger.debug("{}:{}", entry.getKey(), entry.getValue()); } - Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(problemV, EdgeLabelEnum.VERSION, - JsonParseFlagEnum.NoParse); + Either, JanusGraphOperationStatus> childrenVertices = getChildrenVertices(problemV, EdgeLabelEnum.VERSION); if (childrenVertices.isLeft()) { childrenVertices.left().value().size(); logger.debug("number of children that problematic Vertex has: {}", props.size()); @@ -961,12 +932,12 @@ public class JanusGraphDao { try { edge.remove(); } catch (Exception e1) { - logger.debug("failed to remove problematic edge. {}", e1); + logger.debug("failed to remove problematic edge.", e1); } try { problemV.remove(); } catch (Exception e2) { - logger.debug("failed to remove problematic vertex . {}", e2); + logger.debug("failed to remove problematic vertex.", e2); } } continue; @@ -1207,7 +1178,7 @@ public class JanusGraphDao { public JanusGraphOperationStatus moveEdge(GraphVertex vertexA, GraphVertex vertexB, EdgeLabelEnum label, Direction direction) { JanusGraphOperationStatus result = deleteEdgeByDirection(vertexA, direction, label); if (result != JanusGraphOperationStatus.OK) { - logger.error("Failed to diassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); + logger.error("Failed to disassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); return result; } JanusGraphOperationStatus createRelation; @@ -1230,4 +1201,35 @@ public class JanusGraphDao { } return getBelongingEdgeByCriteria(getVertexRes.left().value(), label, properties); } + + private JanusGraphQuery getJanusGraphQuery(final VertexTypeEnum type, final Map hasProps, + final Map hasNotProps, + final Map> predicates, + final JanusGraph graph) { + JanusGraphQuery query = graph.query(); + + if (type != null) { + query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); + } + if (hasProps != null && !hasProps.isEmpty()) { + for (Entry entry : hasProps.entrySet()) { + query = query.has(entry.getKey().getProperty(), entry.getValue()); + } + } + if (hasNotProps != null && !hasNotProps.isEmpty()) { + for (Entry entry : hasNotProps.entrySet()) { + if (entry.getValue() instanceof List) { + buildMultipleNegateQueryFromList(entry, query); + } else { + query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); + } + } + } + if (predicates != null && !predicates.isEmpty()) { + for (Entry> entry : predicates.entrySet()) { + query = query.has(entry.getKey(), entry.getValue().getKey(), entry.getValue().getValue()); + } + } + return query; + } }