From 0514ec6635a08cdbaac5d664c3a4f13bcb0cbf51 Mon Sep 17 00:00:00 2001 From: KrupaNagabhushan Date: Wed, 14 Jul 2021 14:21:47 +0100 Subject: Consider component model when retrieving relationship types Issue-ID: SDC-3640 Signed-off-by: MichaelMorris Change-Id: Ic06a9085b8aa2f44b8d33d7de12eadf691106131 Signed-off-by: Vasyl Razinkov Signed-off-by: KrupaNagabhushan --- .../be/dao/janusgraph/JanusGraphGenericDao.java | 61 +++++++++++++++------- 1 file changed, 42 insertions(+), 19 deletions(-) (limited to 'catalog-dao/src/main') diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java index c6ad5f2c26..b2492cdb1a 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/janusgraph/JanusGraphGenericDao.java @@ -805,36 +805,59 @@ public class JanusGraphGenericDao { public Either, JanusGraphOperationStatus> getByCriteria(NodeTypeEnum type, Map props, Class clazz) { - Either graph = janusGraphClient.getGraph(); - if (graph.isLeft()) { - try { - JanusGraph tGraph = graph.left().value(); - JanusGraphQuery query = tGraph.query(); - query = query.has(GraphPropertiesDictionary.LABEL.getProperty(), type.getName()); - if (props != null && !props.isEmpty()) { - for (Map.Entry entry : props.entrySet()) { - query = query.has(entry.getKey(), entry.getValue()); - } - } - Iterable vertices = query.vertices(); - if (vertices == null) { + return getByCriteriaForModel(type, props, null, clazz); + } + + public Either, JanusGraphOperationStatus> getByCriteriaForModel(final NodeTypeEnum type, final Map props, + final String model, final Class clazz) { + try { + final Either, JanusGraphOperationStatus> vertices = getVerticesByCriteria(type, props); + + if (vertices.isLeft()) { + final Predicate filterPredicate = StringUtils.isEmpty(model) ? this::vertexNotConnectedToAnyModel : vertex -> vertexValidForModel(vertex, model); + final List verticesForModel = StreamSupport.stream(vertices.left().value().spliterator(), false).filter(filterPredicate).collect(Collectors.toList()); + + if (CollectionUtils.isEmpty(verticesForModel)) { + log.debug("No vertex in graph for props ={} ", props); return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - Iterator iterator = vertices.iterator(); - List result = new ArrayList<>(); + + final Iterator iterator = verticesForModel.iterator(); + final List result = new ArrayList<>(); while (iterator.hasNext()) { Vertex vertex = iterator.next(); Map newProp = getProperties(vertex); T element = GraphElementFactory.createElement(type.getName(), GraphElementTypeEnum.Node, newProp, clazz); result.add(element); } - if (log.isDebugEnabled()) { - log.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); + log.debug("Number of fetced nodes in graph for criteria : from type = {} and properties = {} is {}", type, props, result.size()); + return Either.left(result); + + } + return Either.right(vertices.right().value()); + } catch (Exception e) { + log.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); + return Either.right(JanusGraphClient.handleJanusGraphException(e)); + } + } + + private Either, JanusGraphOperationStatus> getVerticesByCriteria(final NodeTypeEnum type, final Map props) { + final Either graph = janusGraphClient.getGraph(); + if (graph.isLeft()) { + try { + final JanusGraph tGraph = graph.left().value(); + JanusGraphQuery query = tGraph.query(); + query = query.has(GraphPropertiesDictionary.LABEL.getProperty(), type.getName()); + if (props != null && !props.isEmpty()) { + for (Map.Entry entry : props.entrySet()) { + query = query.has(entry.getKey(), entry.getValue()); + } } - if (result.size() == 0) { + final Iterable vertices = query.vertices(); + if (vertices == null || !vertices.iterator().hasNext()) { return Either.right(JanusGraphOperationStatus.NOT_FOUND); } - return Either.left(result); + return Either.left(vertices); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Failed get by criteria for type = {} and properties = {}", type, props, e); -- cgit 1.2.3-korg