From df1e300dad94d65a88d2012df9096961619d8272 Mon Sep 17 00:00:00 2001 From: MichaelMorris Date: Fri, 30 Jul 2021 13:06:11 +0100 Subject: Update base types based on model Also made some changes where model was not being considered Signed-off-by: MichaelMorris Issue-ID: SDC-3666 Change-Id: I450c5261239cf4104c494abe6711cb61368a2b4a --- .../sdc/be/dao/jsongraph/JanusGraphDao.java | 133 ++++++++++++++++++++- 1 file changed, 129 insertions(+), 4 deletions(-) (limited to 'catalog-dao/src/main/java') diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/JanusGraphDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/JanusGraphDao.java index 3e10c7941d..76fddc070b 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/JanusGraphDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/JanusGraphDao.java @@ -29,8 +29,12 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import java.util.Optional; import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.tinkerpop.gremlin.structure.Direction; import org.apache.tinkerpop.gremlin.structure.Edge; @@ -52,6 +56,7 @@ import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum; import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; 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.tosca.ToscaDataDefinition; @@ -441,20 +446,23 @@ public class JanusGraphDao { public Either, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, Map props, Map hasNotProps, - JsonParseFlagEnum parseFlag) { - return getByCriteria(type, props, hasNotProps, null, parseFlag); + JsonParseFlagEnum parseFlag, + String model) { + return getByCriteria(type, props, hasNotProps, null, parseFlag, model); } public Either, JanusGraphOperationStatus> getByCriteria(final VertexTypeEnum type, final Map hasProps, final Map hasNotProps, final Map> predicates, - final JsonParseFlagEnum parseFlag) { + final JsonParseFlagEnum parseFlag, + final String model) { 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()); } @@ -484,7 +492,14 @@ public class JanusGraphDao { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } List result = new ArrayList<>(); - vertices.forEach(vertex -> result.add(createAndFill(vertex, parseFlag))); + + 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) { + 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()); @@ -503,6 +518,116 @@ public class JanusGraphDao { return Either.right(graph.right().value()); } } + + public Either, JanusGraphOperationStatus> getByCriteria(VertexTypeEnum type, + 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(); + 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(); + 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()); + } + return Either.left(result); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + 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()); + } + return Either.right(graph.right().value()); + } + } + + private boolean vertexValidForModel(final JanusGraphVertex vertex, final String model) { + final Either>, JanusGraphOperationStatus> modelVertices = getParentVerticies(vertex, GraphEdgeLabels.MODEL_ELEMENT); + + if (modelVertices.isLeft()) { + for (ImmutablePair vertexPair : modelVertices.left().value()) { + if (model.equals((String)vertexPair.getLeft().property("name").value())) { + return true; + } + } + } + return false; + } + + private Either>, JanusGraphOperationStatus> getParentVerticies( + final JanusGraphVertex rootVertex, final GraphEdgeLabels edgeType) { + return getEdgeVerticies(rootVertex, Direction.IN, edgeType); + } + + private Either>, JanusGraphOperationStatus> getEdgeVerticies( + 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(); + ImmutablePair immutablePair = new ImmutablePair<>(vertex, edge); + immutablePairs.add(immutablePair); + } + } + if (immutablePairs.isEmpty()) { + return Either.right(JanusGraphOperationStatus.NOT_FOUND); + } + return Either.left(immutablePairs); + } + + private boolean vertexNotConnectedToAnyModel(final JanusGraphVertex vertex) { + 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; + return !vertex.edges(Direction.IN, edgeLabel.name()).hasNext(); + } public Either, JanusGraphOperationStatus> getCatalogOrArchiveVerticies(boolean isCatalog) { Either graph = janusGraphClient.getGraph(); -- cgit 1.2.3-korg