diff options
Diffstat (limited to 'catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph')
7 files changed, 1090 insertions, 1118 deletions
diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/GraphVertex.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/GraphVertex.java index d38e9c03df..74852944d0 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/GraphVertex.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/GraphVertex.java @@ -20,19 +20,19 @@ package org.openecomp.sdc.be.dao.jsongraph; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - +import com.thinkaurelius.titan.core.TitanVertex; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang.StringUtils; import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum; import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; +import org.openecomp.sdc.be.datatypes.enums.InstantiationTypes; import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields; import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -import com.thinkaurelius.titan.core.TitanVertex; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; public class GraphVertex { private String uniqueId; @@ -59,6 +59,7 @@ public class GraphVertex { public void setUniqueId(String uniqueId) { this.uniqueId = uniqueId; + addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId); } public Map<String, ? extends ToscaDataDefinition> getJson() { @@ -86,8 +87,7 @@ public class GraphVertex { } public ComponentTypeEnum getType() { - ComponentTypeEnum type = ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)); - return type; + return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE)); } public void setType(ComponentTypeEnum type) { @@ -118,6 +118,14 @@ public class GraphVertex { this.metadataProperties = metadataProperties; } + public void getOrSetDefaultInstantiationTypeForToscaElementJson(){ + String toscaVertexJsonInstantiationType; + toscaVertexJsonInstantiationType = (String)(this.getJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE)); + if (toscaVertexJsonInstantiationType == StringUtils.EMPTY || toscaVertexJsonInstantiationType == null){ + this.setJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE, InstantiationTypes.A_LA_CARTE.getValue()); + }; + }; + public Map<String, Object> getMetadataJson() { return metadataJson; } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/TitanDao.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/TitanDao.java index d17a3d5b17..ab95cdbba9 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/TitanDao.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/TitanDao.java @@ -20,21 +20,11 @@ package org.openecomp.sdc.be.dao.jsongraph; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - +import com.thinkaurelius.titan.core.*; +import fj.data.Either; import org.apache.commons.collections.MapUtils; import org.apache.commons.lang3.tuple.ImmutablePair; -import org.apache.tinkerpop.gremlin.structure.Direction; -import org.apache.tinkerpop.gremlin.structure.Edge; -import org.apache.tinkerpop.gremlin.structure.Element; -import org.apache.tinkerpop.gremlin.structure.Property; -import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.apache.tinkerpop.gremlin.structure.*; import org.apache.tinkerpop.gremlin.structure.util.ElementHelper; import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum; import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum; @@ -48,1073 +38,1041 @@ import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum; 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; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import org.openecomp.sdc.common.log.wrappers.Logger; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; -import com.thinkaurelius.titan.core.PropertyKey; -import com.thinkaurelius.titan.core.TitanEdge; -import com.thinkaurelius.titan.core.TitanGraph; -import com.thinkaurelius.titan.core.TitanGraphQuery; -import com.thinkaurelius.titan.core.TitanVertex; -import com.thinkaurelius.titan.core.TitanVertexQuery; +import java.io.IOException; +import java.util.*; +import java.util.Map.Entry; -import fj.data.Either; +import static org.apache.commons.collections.CollectionUtils.isEmpty; @Component("titan-dao") public class TitanDao { - TitanGraphClient titanClient; - - private static Logger logger = LoggerFactory.getLogger(TitanDao.class.getName()); - - public TitanDao(@Qualifier("titan-client") TitanGraphClient titanClient) { - this.titanClient = titanClient; - logger.info("** TitanDao created"); - } - - public TitanOperationStatus commit() { - logger.debug("doing commit."); - return titanClient.commit(); - } - - public TitanOperationStatus rollback() { - return titanClient.rollback(); - } - - public Either<TitanGraph, TitanOperationStatus> getGraph() { - return titanClient.getGraph(); - } - - /** - * - * @param graphVertex - * @return - */ - public Either<GraphVertex, TitanOperationStatus> createVertex(GraphVertex graphVertex) { - logger.trace("try to create vertex for ID [{}]", graphVertex.getUniqueId()); - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - TitanVertex vertex = tGraph.addVertex(); - - setVertexProperties(vertex, graphVertex); - - graphVertex.setVertex(vertex); - - return Either.left(graphVertex); - - } catch (Exception e) { - logger.debug("Failed to create Node for ID [{}]", graphVertex.getUniqueId(), e); - return Either.right(TitanGraphClient.handleTitanException(e)); - } - } else { - logger.debug("Failed to create vertex for ID [{}] {}", graphVertex.getUniqueId(), graph.right().value()); - return Either.right(graph.right().value()); - } - } - - /** - * - * @param name - * @param value - * @param label - * @return - */ - public Either<GraphVertex, TitanOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, - VertexTypeEnum label) { - return getVertexByPropertyAndLabel(name, value, label, JsonParseFlagEnum.ParseAll); - } - - public Either<GraphVertex, TitanOperationStatus> getVertexByLabel(VertexTypeEnum label) { - return titanClient.getGraph().left() - .map(graph -> graph.query().has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices()) - .left().bind(titanVertices -> getFirstFoundVertex(JsonParseFlagEnum.NoParse, titanVertices)); - } - - private Either<GraphVertex, TitanOperationStatus> getFirstFoundVertex(JsonParseFlagEnum parseFlag, - Iterable<TitanVertex> vertices) { - Iterator<TitanVertex> iterator = vertices.iterator(); - if (iterator.hasNext()) { - TitanVertex vertex = iterator.next(); - GraphVertex graphVertex = createAndFill(vertex, parseFlag); - - return Either.left(graphVertex); - } - return Either.right(TitanOperationStatus.NOT_FOUND); - } - - /** - * - * @param name - * @param value - * @param label - * @param parseFlag - * @return - */ - public Either<GraphVertex, TitanOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, - VertexTypeEnum label, JsonParseFlagEnum parseFlag) { - - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - @SuppressWarnings("unchecked") - Iterable<TitanVertex> vertecies = tGraph.query().has(name.getProperty(), value) - .has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices(); - - java.util.Iterator<TitanVertex> iterator = vertecies.iterator(); - if (iterator.hasNext()) { - TitanVertex 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); - } - return Either.right(TitanOperationStatus.NOT_FOUND); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, - label); - } - return Either.right(TitanGraphClient.handleTitanException(e)); - } - - } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, - graph.right().value()); - } - return Either.right(graph.right().value()); - } - } - - /** - * - * @param id - * @return - */ - public Either<GraphVertex, TitanOperationStatus> getVertexById(String id) { - return getVertexById(id, JsonParseFlagEnum.ParseAll); - } - - /** - * - * @param id - * @param parseFlag - * @return - */ - public Either<GraphVertex, TitanOperationStatus> getVertexById(String id, JsonParseFlagEnum parseFlag) { - - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (id == null) { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id = {} ", id); - } - return Either.right(TitanOperationStatus.NOT_FOUND); - } - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - @SuppressWarnings("unchecked") - Iterable<TitanVertex> vertecies = tGraph.query().has(GraphPropertyEnum.UNIQUE_ID.getProperty(), id) - .vertices(); - - java.util.Iterator<TitanVertex> iterator = vertecies.iterator(); - if (iterator.hasNext()) { - TitanVertex 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); - } - return Either.right(TitanOperationStatus.NOT_FOUND); - } - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed to get vertex in graph for id {} ", id); - } - return Either.right(TitanGraphClient.handleTitanException(e)); - } - } else { - if (logger.isDebugEnabled()) { - logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); - } - return Either.right(graph.right().value()); - } - } - - private void setVertexProperties(TitanVertex vertex, GraphVertex graphVertex) throws IOException { - - if (graphVertex.getMetadataProperties() != null) { - for (Map.Entry<GraphPropertyEnum, Object> entry : graphVertex.getMetadataProperties().entrySet()) { - if (entry.getValue() != null) { - vertex.property(entry.getKey().getProperty(), entry.getValue()); - } - } - } - vertex.property(GraphPropertyEnum.LABEL.getProperty(), graphVertex.getLabel().getName()); - - Map<String, ? extends ToscaDataDefinition> json = graphVertex.getJson(); - if (json != null) { - String jsonStr = JsonParserUtils.toJson(json); - vertex.property(GraphPropertyEnum.JSON.getProperty(), jsonStr); - - } - Map<String, Object> jsonMetadata = graphVertex.getMetadataJson(); - if (jsonMetadata != null) { - String jsonMetadataStr = JsonParserUtils.toJson(jsonMetadata); - vertex.property(GraphPropertyEnum.METADATA.getProperty(), jsonMetadataStr); - } - } - - public void setVertexProperties(Vertex vertex, Map<String, Object> properties) throws IOException { - for (Map.Entry<String, Object> entry : properties.entrySet()) { - if (entry.getValue() != null) { - vertex.property(entry.getKey(), entry.getValue()); - } - } - } - - private GraphVertex createAndFill(TitanVertex vertex, JsonParseFlagEnum parseFlag) { - GraphVertex graphVertex = new GraphVertex(); - graphVertex.setVertex(vertex); - parseVertexProperties(graphVertex, parseFlag); - return graphVertex; - } - - public void parseVertexProperties(GraphVertex graphVertex, JsonParseFlagEnum parseFlag) { - TitanVertex vertex = graphVertex.getVertex(); - Map<GraphPropertyEnum, Object> properties = getVertexProperties(vertex); - VertexTypeEnum label = VertexTypeEnum.getByName((String) (properties.get(GraphPropertyEnum.LABEL))); - for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) { - GraphPropertyEnum key = entry.getKey(); - switch (key) { - case UNIQUE_ID: - graphVertex.setUniqueId((String) entry.getValue()); - break; - case LABEL: - graphVertex.setLabel(VertexTypeEnum.getByName((String) entry.getValue())); - break; - case COMPONENT_TYPE: - String type = (String) entry.getValue(); - if (type != null) { - graphVertex.setType(ComponentTypeEnum.valueOf(type)); - } - break; - case JSON: - if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseJson) { - String json = (String) entry.getValue(); - Map<String, ? extends ToscaDataDefinition> jsonObj = JsonParserUtils.toMap(json, - label.getClassOfJson()); - graphVertex.setJson(jsonObj); - } - break; - case METADATA: - if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseMetadata) { - String json = (String) entry.getValue(); - Map<String, Object> metadatObj = JsonParserUtils.toMap(json); - graphVertex.setMetadataJson(metadatObj); - } - break; - default: - graphVertex.addMetadataProperty(key, entry.getValue()); - break; - } - } - } - - public TitanOperationStatus createEdge(GraphVertex from, GraphVertex to, EdgeLabelEnum label, - Map<EdgePropertyEnum, Object> properties) { - return createEdge(from.getVertex(), to.getVertex(), label, properties); - } - - public TitanOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, - Map<EdgePropertyEnum, Object> 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 Titan vertex for id from {} or id to {}", - from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), - to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty())); - return TitanOperationStatus.NOT_FOUND; - } - Edge edge = from.addEdge(label.name(), to); - setEdgeProperties(edge, properties); - return TitanOperationStatus.OK; - } - - public Map<GraphPropertyEnum, Object> getVertexProperties(Element element) { - - Map<GraphPropertyEnum, Object> result = new HashMap<GraphPropertyEnum, Object>(); - - if (element != null && element.keys() != null && element.keys().size() > 0) { - Map<String, Property> propertyMap = ElementHelper.propertyMap(element, - element.keys().toArray(new String[element.keys().size()])); - - for (Entry<String, Property> entry : propertyMap.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue().value(); - - GraphPropertyEnum valueOf = GraphPropertyEnum.getByProperty(key); - if (valueOf != null) { - result.put(valueOf, value); - } - } - } - return result; - } - - public Map<EdgePropertyEnum, Object> getEdgeProperties(Element element) { - - Map<EdgePropertyEnum, Object> result = new HashMap<EdgePropertyEnum, Object>(); - - if (element != null && element.keys() != null && element.keys().size() > 0) { - Map<String, Property> propertyMap = ElementHelper.propertyMap(element, - element.keys().toArray(new String[element.keys().size()])); - - for (Entry<String, Property> entry : propertyMap.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue().value(); - - EdgePropertyEnum valueOf = EdgePropertyEnum.getByProperty(key); - if (valueOf != null) { - result.put(valueOf, value); - } - } - } - return result; - } - - public void setEdgeProperties(Element element, Map<EdgePropertyEnum, Object> properties) { - - if (properties != null && !properties.isEmpty()) { - - Object[] propertyKeyValues = new Object[properties.size() * 2]; - int i = 0; - for (Entry<EdgePropertyEnum, Object> entry : properties.entrySet()) { - propertyKeyValues[i++] = entry.getKey().getProperty(); - propertyKeyValues[i++] = entry.getValue(); - } - - ElementHelper.attachProperties(element, propertyKeyValues); - - } - - } - - public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, - Map<GraphPropertyEnum, Object> props) { - return getByCriteria(type, props, JsonParseFlagEnum.ParseAll); - } - - public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, - Map<GraphPropertyEnum, Object> props, JsonParseFlagEnum parseFlag) { - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query(); - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - - if (props != null && !props.isEmpty()) { - for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - Iterable<TitanVertex> vertices = query.vertices(); - if (vertices == null) { - return Either.right(TitanOperationStatus.NOT_FOUND); - } - - Iterator<TitanVertex> iterator = vertices.iterator(); - List<GraphVertex> result = new ArrayList<GraphVertex>(); - - while (iterator.hasNext()) { - TitanVertex vertex = iterator.next(); - - Map<GraphPropertyEnum, Object> newProp = 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()); - } - if (result.size() == 0) { - return Either.right(TitanOperationStatus.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); - } - return Either.right(TitanGraphClient.handleTitanException(e)); - } - - } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, - graph.right().value()); - } - return Either.right(graph.right().value()); - } - } - - public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, - Map<GraphPropertyEnum, Object> props, Map<GraphPropertyEnum, Object> hasNotProps, - JsonParseFlagEnum parseFlag) { - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query(); - if (type != null) { - query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); - } - - if (props != null && !props.isEmpty()) { - for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - if (hasNotProps != null && !hasNotProps.isEmpty()) { - for (Map.Entry<GraphPropertyEnum, Object> entry : hasNotProps.entrySet()) { - if (entry.getValue() instanceof List) { - buildMultipleNegateQueryFromList(entry, query); - } else { - query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); - } - } - } - Iterable<TitanVertex> vertices = query.vertices(); - if (vertices == null) { - return Either.right(TitanOperationStatus.NOT_FOUND); - } - - Iterator<TitanVertex> iterator = vertices.iterator(); - List<GraphVertex> result = new ArrayList<GraphVertex>(); - - while (iterator.hasNext()) { - TitanVertex vertex = iterator.next(); - - Map<GraphPropertyEnum, Object> newProp = 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()); - } - if (result.size() == 0) { - return Either.right(TitanOperationStatus.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); - } - return Either.right(TitanGraphClient.handleTitanException(e)); - } - - } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, - graph.right().value()); - } - return Either.right(graph.right().value()); - } - } - - public Either<Iterator<Vertex>, TitanOperationStatus> getCatalogVerticies() { - Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); - if (graph.isLeft()) { - try { - TitanGraph tGraph = graph.left().value(); - - Iterable<TitanVertex> vCatalogIter = tGraph.query() - .has(GraphPropertyEnum.LABEL.getProperty(), VertexTypeEnum.CATALOG_ROOT.getName()).vertices(); - if (vCatalogIter == null) { - logger.debug("Failed to fetch catalog vertex"); - return Either.right(TitanOperationStatus.GENERAL_ERROR); - } - TitanVertex catalogV = vCatalogIter.iterator().next(); - if (catalogV == null) { - logger.debug("Failed to fetch catalog vertex"); - return Either.right(TitanOperationStatus.GENERAL_ERROR); - } - Iterator<Vertex> vertices = catalogV.vertices(Direction.OUT, EdgeLabelEnum.CATALOG_ELEMENT.name()); - - return Either.left(vertices); - } catch (Exception e) { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria: ", e); - } - return Either.right(TitanGraphClient.handleTitanException(e)); - } - - } else { - if (logger.isDebugEnabled()) { - logger.debug("Failed get by criteria : ", graph.right().value()); - } - return Either.right(graph.right().value()); - } - } - - private void buildMultipleNegateQueryFromList(Map.Entry<GraphPropertyEnum, Object> entry, TitanGraphQuery query) { - List<Object> negateList = (List<Object>) entry.getValue(); - for (Object listItem : negateList) { - query.hasNot(entry.getKey().getProperty(), listItem); - } - } - - /** - * - * @param parentVertex - * @param edgeLabel - * @param parseFlag - * @return - */ - public Either<GraphVertex, TitanOperationStatus> getChildVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = getChildrenVertecies(parentVertex, - edgeLabel, parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); - } - return Either.left(childrenVertecies.left().value().get(0)); - } - - /** - * - * @param parentVertex - * @param edgeLabel - * @param parseFlag - * @return - */ - public Either<Vertex, TitanOperationStatus> getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - Either<List<Vertex>, TitanOperationStatus> childrenVertecies = getChildrenVertecies(parentVertex, edgeLabel, - parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); - } - return Either.left(childrenVertecies.left().value().get(0)); - } - - public Either<GraphVertex, TitanOperationStatus> getParentVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = getParentVertecies(parentVertex, edgeLabel, - parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); - } - return Either.left(childrenVertecies.left().value().get(0)); - } - - public Either<Vertex, TitanOperationStatus> getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - Either<List<Vertex>, TitanOperationStatus> childrenVertecies = getParentVertecies(parentVertex, edgeLabel, - parseFlag); - if (childrenVertecies.isRight()) { - return Either.right(childrenVertecies.right().value()); - } - List<Vertex> value = childrenVertecies.left().value(); - if (value.isEmpty()) { - return Either.right(TitanOperationStatus.NOT_FOUND); - } - return Either.left(value.get(0)); - } - - /** - * - * @param parentVertex - * @param edgeLabel - * @param parseFlag - * @return - */ - public Either<List<GraphVertex>, TitanOperationStatus> getChildrenVertecies(GraphVertex parentVertex, - EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.OUT); - } - - public Either<List<GraphVertex>, TitanOperationStatus> getParentVertecies(GraphVertex parentVertex, - EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { - return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.IN); - } - - public Either<List<Vertex>, TitanOperationStatus> getParentVertecies(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.IN); - } - - private Either<List<Vertex>, TitanOperationStatus> getAdjacentVerticies(Vertex parentVertex, - EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { - List<Vertex> list = new ArrayList<>(); - try { - Either<TitanGraph, TitanOperationStatus> graphRes = titanClient.getGraph(); - if (graphRes.isRight()) { - logger.error("Failed to retrieve graph. status is {}", graphRes); - return Either.right(graphRes.right().value()); - } - Iterator<Edge> edgesCreatorIterator = parentVertex.edges(direction, edgeLabel.name()); - if (edgesCreatorIterator != null) { - while (edgesCreatorIterator.hasNext()) { - Edge edge = edgesCreatorIterator.next(); - TitanVertex vertex; - if (direction == Direction.IN) { - vertex = (TitanVertex) edge.outVertex(); - } else { - vertex = (TitanVertex) edge.inVertex(); - } - // GraphVertex graphVertex = createAndFill(vertex, parseFlag); - - list.add(vertex); - } - } - if (true == list.isEmpty()) { - return Either.right(TitanOperationStatus.NOT_FOUND); - } - } catch (Exception e) { - logger.error("Failed to perform graph operation ", e); - Either.right(TitanGraphClient.handleTitanException(e)); - } - - return Either.left(list); - } - - /** - * - * @param parentVertex - * @param edgeLabel - * @param parseFlag - * @return - */ - public Either<List<Vertex>, TitanOperationStatus> getChildrenVertecies(Vertex parentVertex, EdgeLabelEnum edgeLabel, - JsonParseFlagEnum parseFlag) { - return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.OUT); - } - - private Either<List<GraphVertex>, TitanOperationStatus> getAdjacentVerticies(GraphVertex parentVertex, - EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { - List<GraphVertex> list = new ArrayList<GraphVertex>(); - - Either<List<Vertex>, TitanOperationStatus> adjacentVerticies = getAdjacentVerticies(parentVertex.getVertex(), - edgeLabel, parseFlag, direction); - if (adjacentVerticies.isRight()) { - return Either.right(adjacentVerticies.right().value()); - } - adjacentVerticies.left().value().stream().forEach(vertex -> { - list.add(createAndFill((TitanVertex) vertex, parseFlag)); - }); - - return Either.left(list); - } - - /** - * Searches Edge by received label and criteria - * - * @param vertex - * @param label - * @param properties - * @return found edge or TitanOperationStatus - */ - public Either<Edge, TitanOperationStatus> getBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, - Map<GraphPropertyEnum, Object> properties) { - - Either<Edge, TitanOperationStatus> result = null; - Edge matchingEdge = null; - String notFoundMsg = "No edges in graph for criteria"; - try { - TitanVertexQuery<?> query = vertex.getVertex().query().labels(label.name()); - - if (properties != null && !properties.isEmpty()) { - for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) { - query = query.has(entry.getKey().getProperty(), entry.getValue()); - } - } - - Iterable<TitanEdge> edges = query.edges(); - if (edges == null) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg); - result = Either.right(TitanOperationStatus.NOT_FOUND); - } else { - Iterator<TitanEdge> eIter = edges.iterator(); - if (eIter.hasNext()) { - matchingEdge = eIter.next(); - } else { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg); - result = Either.right(TitanOperationStatus.NOT_FOUND); - } - } - if (result == null) { - result = Either.left(matchingEdge); - } - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Exception occured during getting edge by criteria for component with id {}. {}", - vertex.getUniqueId(), e); - return Either.right(TitanGraphClient.handleTitanException(e)); - } - return result; - } - - /** - * Deletes Edge by received label and criteria - * - * @param vertex - * @param label - * @param properties - * @return - */ - public Either<Edge, TitanOperationStatus> deleteBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, - Map<GraphPropertyEnum, Object> properties) { - Either<Edge, TitanOperationStatus> result = null; - try { - result = getBelongingEdgeByCriteria(vertex, label, properties); - if (result.isLeft()) { - Edge edge = result.left().value(); - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "Going to delete an edge with the label {} belonging to the vertex {} ", label.name(), - vertex.getUniqueId()); - edge.remove(); - result = Either.left(edge); - } else { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Failed to find an edge with the label {} belonging to the vertex {} ", label.name(), - vertex.getUniqueId()); - } - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Exception occured during deleting an edge by criteria for the component with id {}. {}", - vertex == null ? "NULL" : vertex.getUniqueId(), e); - return Either.right(TitanGraphClient.handleTitanException(e)); - } - return result; - } - - @SuppressWarnings("unchecked") - /** - * Deletes an edge between vertices fromVertex and toVertex according to - * received label - * - * @param fromVertex - * @param toVertex - * @param label - * @return - */ - - public Either<Edge, TitanOperationStatus> deleteEdge(GraphVertex fromVertex, GraphVertex toVertex, - EdgeLabelEnum label) { - return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), - toVertex.getUniqueId()); - } - - public Either<Edge, TitanOperationStatus> deleteEdge(TitanVertex fromVertex, TitanVertex toVertex, - EdgeLabelEnum label, String uniqueIdFrom, String uniqueIdTo) { - Either<Edge, TitanOperationStatus> result = null; - try { - Iterable<TitanEdge> edges = fromVertex.query().labels(label.name()).edges(); - Iterator<TitanEdge> eIter = edges.iterator(); - while (eIter.hasNext()) { - Edge edge = eIter.next(); - String currVertexUniqueId = edge.inVertex().value(GraphPropertyEnum.UNIQUE_ID.getProperty()); - if (currVertexUniqueId != null && currVertexUniqueId.equals(uniqueIdTo)) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "Going to delete an edge with the label {} between vertices {} and {}. ", label.name(), - uniqueIdFrom, uniqueIdTo); - edge.remove(); - result = Either.left(edge); - break; - } - } - if (result == null) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Failed to delete an edge with the label {} between vertices {} and {}. ", label.name(), - uniqueIdFrom, uniqueIdTo); - result = Either.right(TitanOperationStatus.NOT_FOUND); - } - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Exception occured during deleting an edge with the label {} between vertices {} and {}. {}", - label.name(), uniqueIdFrom, uniqueIdTo, e); - return Either.right(TitanGraphClient.handleTitanException(e)); - } - return result; - } - - public TitanOperationStatus deleteEdgeByDirection(GraphVertex fromVertex, Direction direction, - EdgeLabelEnum label) { - try { - Iterator<Edge> edges = fromVertex.getVertex().edges(direction, label.name()); - - while (edges.hasNext()) { - Edge edge = edges.next(); - edge.remove(); - } - } catch (Exception e) { - logger.debug("Failed to remove from vertex {} edges {} by direction {} ", fromVertex.getUniqueId(), label, - direction, e); - return TitanGraphClient.handleTitanException(e); - } - return TitanOperationStatus.OK; - } - - /** - * Updates vertex properties. Note that graphVertex argument should contain - * updated data - * - * @param graphVertex - * @return - */ - public Either<GraphVertex, TitanOperationStatus> updateVertex(GraphVertex graphVertex) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "Going to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId()); - try { - graphVertex.updateMetadataJsonWithCurrentMetadataProperties(); - setVertexProperties(graphVertex.getVertex(), graphVertex); - - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Failed to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId(), e); - return Either.right(TitanGraphClient.handleTitanException(e)); - } - return Either.left(graphVertex); - } - - /** - * Fetches vertices by uniqueId according to received parse flag - * - * @param verticesToGet - * @return - */ - public Either<Map<String, GraphVertex>, TitanOperationStatus> getVerticesByUniqueIdAndParseFlag( - Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet) { - - Either<Map<String, GraphVertex>, TitanOperationStatus> result = null; - Map<String, GraphVertex> vertices = new HashMap<>(); - TitanOperationStatus titatStatus; - Either<GraphVertex, TitanOperationStatus> getVertexRes = null; - for (Map.Entry<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> entry : verticesToGet.entrySet()) { - if (entry.getValue().getKey() == GraphPropertyEnum.UNIQUE_ID) { - getVertexRes = getVertexById(entry.getKey(), entry.getValue().getValue()); - } else if (entry.getValue().getKey() == GraphPropertyEnum.USERID) { - getVertexRes = getVertexByPropertyAndLabel(entry.getValue().getKey(), entry.getKey(), - VertexTypeEnum.USER, entry.getValue().getValue()); - } - if (getVertexRes == null) { - titatStatus = TitanOperationStatus.ILLEGAL_ARGUMENT; - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Invalid vertex type label {} has been received. ", entry.getValue().getKey(), titatStatus); - result = Either.right(titatStatus); - } - if (getVertexRes.isRight()) { - titatStatus = getVertexRes.right().value(); - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Failed to get vertex by id {} . Status is {}. ", entry.getKey(), titatStatus); - result = Either.right(titatStatus); - break; - } else { - vertices.put(entry.getKey(), getVertexRes.left().value()); - } - } - if (result == null) { - result = Either.left(vertices); - } - return result; - } - - /** - * Creates edge between "from" and "to" vertices with specified label and - * properties extracted from received edge - * - * @param from - * @param to - * @param label - * @param edgeToCopy - * @return - */ - public TitanOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Edge edgeToCopy) { - return createEdge(from, to, label, getEdgeProperties(edgeToCopy)); - } - - public TitanOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, Edge prevEdge, - EdgeLabelEnum prevLabel, EdgeLabelEnum newLabel) { - - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "Going to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel, - fromVertex == null ? "NULL" : fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), - toVertex == null ? "NULL" : toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty())); - TitanOperationStatus result = createEdge(fromVertex, toVertex, newLabel, prevEdge); - if (result == TitanOperationStatus.OK) { - prevEdge.remove(); - } - return result; - } - - /** - * Replaces previous label of edge with new label - * - * @param fromVertex - * @param toVertex - * @param prevLabel - * @param newLabel - * @return - */ - public TitanOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, EdgeLabelEnum prevLabel, - EdgeLabelEnum newLabel) { - - TitanOperationStatus result = null; - Iterator<Edge> prevEdgeIter = toVertex.edges(Direction.IN, prevLabel.name()); - if (prevEdgeIter == null || !prevEdgeIter.hasNext()) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Failed to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel, - fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), - toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty())); - result = TitanOperationStatus.NOT_FOUND; - } - if (result == null) { - result = replaceEdgeLabel(fromVertex, toVertex, prevEdgeIter.next(), prevLabel, newLabel); - } - return result; - } - - /** - * Updates metadata properties of vertex on graph. Json metadata property of the - * vertex will be updated with received properties too. - * - * - * @param vertex - * @param properties - * @return - */ - public TitanOperationStatus updateVertexMetadataPropertiesWithJson(Vertex vertex, - Map<GraphPropertyEnum, Object> properties) { - try { - if (!MapUtils.isEmpty(properties)) { - String jsonMetadataStr = (String) vertex.property(GraphPropertyEnum.METADATA.getProperty()).value(); - Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap(jsonMetadataStr); - for (Map.Entry<GraphPropertyEnum, Object> property : properties.entrySet()) { - vertex.property(property.getKey().getProperty(), property.getValue()); - jsonMetadataMap.put(property.getKey().getProperty(), property.getValue()); - } - vertex.property(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap)); - } - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Exception occurred during update vertex metadata properties with json{}. {}", - vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), e.getMessage()); - return TitanGraphClient.handleTitanException(e); - } - return TitanOperationStatus.OK; - } - - public TitanOperationStatus disassociateAndDeleteLast(GraphVertex vertex, Direction direction, - EdgeLabelEnum label) { - try { - Iterator<Edge> edges = vertex.getVertex().edges(direction, label.name()); - - while (edges.hasNext()) { - Edge edge = edges.next(); - Vertex secondVertex; - Direction reverseDirection; - if (direction == Direction.IN) { - secondVertex = edge.outVertex(); - reverseDirection = Direction.OUT; - } else { - secondVertex = edge.inVertex(); - reverseDirection = Direction.IN; - } - edge.remove(); - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "Edge {} with direction {} was removed from {}", label.name(), direction, vertex.getVertex()); - - Iterator<Edge> restOfEdges = secondVertex.edges(reverseDirection, label.name()); - if (restOfEdges.hasNext() == false) { - secondVertex.remove(); - CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, - "This was last edge . Vertex {} was removed ", vertex.getUniqueId()); - } - } - } catch (Exception e) { - CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, - "Exception occured during deleting an edge with the label {} direction {} from vertex {}. {}", - label.name(), direction, vertex.getUniqueId(), e); - return TitanGraphClient.handleTitanException(e); - } - return TitanOperationStatus.OK; - } - - public Object getProperty(TitanVertex vertex, String key) { - PropertyKey propertyKey = titanClient.getGraph().left().value().getPropertyKey(key); - Object value = vertex.valueOrNull(propertyKey); - return value; - } - - public Object getProperty(Edge edge, EdgePropertyEnum key) { - Object value = null; - try { - Property<Object> property = edge.property(key.getProperty()); - if (property != null) { - return property.orElse(null); - } - } catch (Exception e) { - - } - return value; - } - - /** - * - * @param vertexA - * @param vertexB - * @param label - * @param direction - * @return - */ - public TitanOperationStatus moveEdge(GraphVertex vertexA, GraphVertex vertexB, EdgeLabelEnum label, - Direction direction) { - TitanOperationStatus result = deleteEdgeByDirection(vertexA, direction, label); - if (result != TitanOperationStatus.OK) { - logger.error("Failed to diassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); - return result; - } - TitanOperationStatus createRelation; - if (direction == Direction.IN) { - createRelation = createEdge(vertexB, vertexA, label, null); - } else { - createRelation = createEdge(vertexA, vertexB, label, null); - } - if (createRelation != TitanOperationStatus.OK) { - return createRelation; - } - return TitanOperationStatus.OK; - } - - public Either<Edge, TitanOperationStatus> getBelongingEdgeByCriteria(String parentId, EdgeLabelEnum label, - Map<GraphPropertyEnum, Object> properties) { - Either<GraphVertex, TitanOperationStatus> getVertexRes = getVertexById(parentId, JsonParseFlagEnum.NoParse); - if (getVertexRes.isRight()) { - return Either.right(getVertexRes.right().value()); - } - return getBelongingEdgeByCriteria(getVertexRes.left().value(), label, properties); - } + TitanGraphClient titanClient; + + private static Logger logger = Logger.getLogger(TitanDao.class.getName()); + + public TitanDao(@Qualifier("titan-client") TitanGraphClient titanClient) { + this.titanClient = titanClient; + logger.info("** TitanDao created"); + } + + public TitanOperationStatus commit() { + logger.debug("#commit - The operation succeeded. Doing commit..."); + return titanClient.commit(); + } + + public TitanOperationStatus rollback() { + logger.debug("#rollback - The operation failed. Doing rollback..."); + return titanClient.rollback(); + } + + public Either<TitanGraph, TitanOperationStatus> getGraph() { + return titanClient.getGraph(); + } + + /** + * + * @param graphVertex + * @return + */ + public Either<GraphVertex, TitanOperationStatus> createVertex(GraphVertex graphVertex) { + logger.trace("try to create vertex for ID [{}]", graphVertex.getUniqueId()); + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + TitanVertex vertex = tGraph.addVertex(); + + setVertexProperties(vertex, graphVertex); + + graphVertex.setVertex(vertex); + + return Either.left(graphVertex); + + } catch (Exception e) { + logger.debug("Failed to create Node for ID [{}]", graphVertex.getUniqueId(), e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + } else { + logger.debug("Failed to create vertex for ID [{}] {}", graphVertex.getUniqueId(), graph.right().value()); + return Either.right(graph.right().value()); + } + } + + /** + * + * @param name + * @param value + * @param label + * @return + */ + public Either<GraphVertex, TitanOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, VertexTypeEnum label) { + return getVertexByPropertyAndLabel(name, value, label, JsonParseFlagEnum.ParseAll); + } + + public Either<GraphVertex, TitanOperationStatus> getVertexByLabel(VertexTypeEnum label) { + return titanClient.getGraph().left().map(graph -> graph.query().has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices()).left().bind(titanVertices -> getFirstFoundVertex(JsonParseFlagEnum.NoParse, titanVertices)); + } + + private Either<GraphVertex, TitanOperationStatus> getFirstFoundVertex(JsonParseFlagEnum parseFlag, Iterable<TitanVertex> vertices) { + Iterator<TitanVertex> iterator = vertices.iterator(); + if (iterator.hasNext()) { + TitanVertex vertex = iterator.next(); + GraphVertex graphVertex = createAndFill(vertex, parseFlag); + + return Either.left(graphVertex); + } + return Either.right(TitanOperationStatus.NOT_FOUND); + } + + /** + * + * @param name + * @param value + * @param label + * @param parseFlag + * @return + */ + public Either<GraphVertex, TitanOperationStatus> getVertexByPropertyAndLabel(GraphPropertyEnum name, Object value, VertexTypeEnum label, JsonParseFlagEnum parseFlag) { + + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + @SuppressWarnings("unchecked") + Iterable<TitanVertex> vertecies = tGraph.query().has(name.getProperty(), value).has(GraphPropertyEnum.LABEL.getProperty(), label.getName()).vertices(); + + java.util.Iterator<TitanVertex> iterator = vertecies.iterator(); + if (iterator.hasNext()) { + TitanVertex 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); + } + return Either.right(TitanOperationStatus.NOT_FOUND); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Failed to get vertex in graph for key ={} and value = {} label = {}", name, value, label); + } + return Either.right(TitanGraphClient.handleTitanException(e)); + } + + } else { + if (logger.isDebugEnabled()) { + logger.debug("No vertex in graph for key ={} and value = {} label = {} error :{}", name, value, label, graph.right().value()); + } + return Either.right(graph.right().value()); + } + } + + /** + * + * @param id + * @return + */ + public Either<GraphVertex, TitanOperationStatus> getVertexById(String id) { + return getVertexById(id, JsonParseFlagEnum.ParseAll); + } + + /** + * + * @param id + * @param parseFlag + * @return + */ + public Either<GraphVertex, TitanOperationStatus> getVertexById(String id, JsonParseFlagEnum parseFlag) { + + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (id == null) { + if (logger.isDebugEnabled()) { + logger.debug("No vertex in graph for id = {} ", id); + } + return Either.right(TitanOperationStatus.NOT_FOUND); + } + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + @SuppressWarnings("unchecked") + Iterable<TitanVertex> vertecies = tGraph.query().has(GraphPropertyEnum.UNIQUE_ID.getProperty(), id).vertices(); + + java.util.Iterator<TitanVertex> iterator = vertecies.iterator(); + if (iterator.hasNext()) { + TitanVertex 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); + } + return Either.right(TitanOperationStatus.NOT_FOUND); + } + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Failed to get vertex in graph for id {} ", id); + } + return Either.right(TitanGraphClient.handleTitanException(e)); + } + } else { + if (logger.isDebugEnabled()) { + logger.debug("No vertex in graph for id {} error : {}", id, graph.right().value()); + } + return Either.right(graph.right().value()); + } + } + + private void setVertexProperties(TitanVertex vertex, GraphVertex graphVertex) throws IOException { + + if (graphVertex.getMetadataProperties() != null) { + for (Map.Entry<GraphPropertyEnum, Object> entry : graphVertex.getMetadataProperties().entrySet()) { + if (entry.getValue() != null) { + vertex.property(entry.getKey().getProperty(), entry.getValue()); + } + } + } + vertex.property(GraphPropertyEnum.LABEL.getProperty(), graphVertex.getLabel().getName()); + + Map<String, ? extends ToscaDataDefinition> json = graphVertex.getJson(); + if (json != null) { + String jsonStr = JsonParserUtils.toJson(json); + vertex.property(GraphPropertyEnum.JSON.getProperty(), jsonStr); + + } + Map<String, Object> jsonMetadata = graphVertex.getMetadataJson(); + if (jsonMetadata != null) { + String jsonMetadataStr = JsonParserUtils.toJson(jsonMetadata); + vertex.property(GraphPropertyEnum.METADATA.getProperty(), jsonMetadataStr); + } + } + + public void setVertexProperties(Vertex vertex, Map<String, Object> properties) throws IOException { + for (Map.Entry<String, Object> entry : properties.entrySet()) { + if (entry.getValue() != null) { + vertex.property(entry.getKey(), entry.getValue()); + } + } + } + + private GraphVertex createAndFill(TitanVertex vertex, JsonParseFlagEnum parseFlag) { + GraphVertex graphVertex = new GraphVertex(); + graphVertex.setVertex(vertex); + parseVertexProperties(graphVertex, parseFlag); + return graphVertex; + } + + public void parseVertexProperties(GraphVertex graphVertex, JsonParseFlagEnum parseFlag) { + TitanVertex vertex = graphVertex.getVertex(); + Map<GraphPropertyEnum, Object> properties = getVertexProperties(vertex); + VertexTypeEnum label = VertexTypeEnum.getByName((String) (properties.get(GraphPropertyEnum.LABEL))); + for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) { + GraphPropertyEnum key = entry.getKey(); + switch (key) { + case UNIQUE_ID: + graphVertex.setUniqueId((String) entry.getValue()); + break; + case LABEL: + graphVertex.setLabel(VertexTypeEnum.getByName((String) entry.getValue())); + break; + case COMPONENT_TYPE: + String type = (String) entry.getValue(); + if (type != null) { + graphVertex.setType(ComponentTypeEnum.valueOf(type)); + } + break; + case JSON: + if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseJson) { + String json = (String) entry.getValue(); + Map<String, ? extends ToscaDataDefinition> jsonObj = JsonParserUtils.toMap(json, label.getClassOfJson()); + graphVertex.setJson(jsonObj); + } + break; + case METADATA: + if (parseFlag == JsonParseFlagEnum.ParseAll || parseFlag == JsonParseFlagEnum.ParseMetadata) { + String json = (String) entry.getValue(); + Map<String, Object> metadatObj = JsonParserUtils.toMap(json); + graphVertex.setMetadataJson(metadatObj); + } + break; + default: + graphVertex.addMetadataProperty(key, entry.getValue()); + break; + } + } + } + + public TitanOperationStatus createEdge(GraphVertex from, GraphVertex to, EdgeLabelEnum label, Map<EdgePropertyEnum, Object> properties) { + return createEdge(from.getVertex(), to.getVertex(), label, properties); + } + + public TitanOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Map<EdgePropertyEnum, Object> 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); + } + if (from == null || to == null) { + logger.trace("No Titan vertex for id from {} or id to {}", + from == null ? "NULL" : from.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), + to == null ? "NULL" : to.property(GraphPropertyEnum.UNIQUE_ID.getProperty())); + return TitanOperationStatus.NOT_FOUND; + } + Edge edge = from.addEdge(label.name(), to); + TitanOperationStatus status; + try { + setEdgeProperties(edge, properties); + status = TitanOperationStatus.OK; + } catch (IOException e) { + logger.debug("Failed to set properties on edge properties [{}]", properties, e); + status = TitanOperationStatus.GENERAL_ERROR; + } + return status; + } + + public Map<GraphPropertyEnum, Object> getVertexProperties(Element element) { + + Map<GraphPropertyEnum, Object> result = new HashMap<>(); + + if (element != null && element.keys() != null && element.keys().size() > 0) { + Map<String, Property> propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); + + for (Entry<String, Property> entry : propertyMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue().value(); + + GraphPropertyEnum valueOf = GraphPropertyEnum.getByProperty(key); + if (valueOf != null) { + result.put(valueOf, value); + } + } + } + return result; + } + + public Map<EdgePropertyEnum, Object> getEdgeProperties(Element element) { + + Map<EdgePropertyEnum, Object> result = new HashMap<>(); + + if (element != null && element.keys() != null && element.keys().size() > 0) { + Map<String, Property> propertyMap = ElementHelper.propertyMap(element, element.keys().toArray(new String[element.keys().size()])); + + for (Entry<String, Property> entry : propertyMap.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue().value(); + + EdgePropertyEnum valueOf = EdgePropertyEnum.getByProperty(key); + if (valueOf != null) { + if (valueOf == EdgePropertyEnum.INSTANCES) { + List<String> list = JsonParserUtils.toList((String) value, String.class); + result.put(valueOf, list); + } else { + result.put(valueOf, value); + } + } + } + } + return result; + } + + public void setEdgeProperties(Element element, Map<EdgePropertyEnum, Object> properties) throws IOException { + + if (properties != null && !properties.isEmpty()) { + + Object[] propertyKeyValues = new Object[properties.size() * 2]; + int i = 0; + for (Entry<EdgePropertyEnum, Object> entry : properties.entrySet()) { + propertyKeyValues[i++] = entry.getKey().getProperty(); + Object value = entry.getValue(); + if (entry.getKey() == EdgePropertyEnum.INSTANCES) { + String jsonStr = JsonParserUtils.toJson(value); + propertyKeyValues[i++] = jsonStr; + } else { + propertyKeyValues[i++] = entry.getValue(); + } + } + ElementHelper.attachProperties(element, propertyKeyValues); + } + } + + public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props) { + return getByCriteria(type, props, JsonParseFlagEnum.ParseAll); + } + + public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props, JsonParseFlagEnum parseFlag) { + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query(); + if (type != null) { + query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); + } + + if (props != null && !props.isEmpty()) { + for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) { + query = query.has(entry.getKey().getProperty(), entry.getValue()); + } + } + Iterable<TitanVertex> vertices = query.vertices(); + if (vertices == null) { + return Either.right(TitanOperationStatus.NOT_FOUND); + } + + Iterator<TitanVertex> iterator = vertices.iterator(); + List<GraphVertex> result = new ArrayList<>(); + + while (iterator.hasNext()) { + TitanVertex vertex = iterator.next(); + + Map<GraphPropertyEnum, Object> newProp = 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()); + } + if (result.size() == 0) { + return Either.right(TitanOperationStatus.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); + } + return Either.right(TitanGraphClient.handleTitanException(e)); + } + + } else { + if (logger.isDebugEnabled()) { + logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); + } + return Either.right(graph.right().value()); + } + } + + public Either<List<GraphVertex>, TitanOperationStatus> getByCriteria(VertexTypeEnum type, Map<GraphPropertyEnum, Object> props, Map<GraphPropertyEnum, Object> hasNotProps, JsonParseFlagEnum parseFlag) { + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + TitanGraphQuery<? extends TitanGraphQuery> query = tGraph.query(); + if (type != null) { + query = query.has(GraphPropertyEnum.LABEL.getProperty(), type.getName()); + } + + if (props != null && !props.isEmpty()) { + for (Map.Entry<GraphPropertyEnum, Object> entry : props.entrySet()) { + query = query.has(entry.getKey().getProperty(), entry.getValue()); + } + } + if (hasNotProps != null && !hasNotProps.isEmpty()) { + for (Map.Entry<GraphPropertyEnum, Object> entry : hasNotProps.entrySet()) { + if (entry.getValue() instanceof List) { + buildMultipleNegateQueryFromList(entry, query); + } else { + query = query.hasNot(entry.getKey().getProperty(), entry.getValue()); + } + } + } + Iterable<TitanVertex> vertices = query.vertices(); + if (vertices == null) { + return Either.right(TitanOperationStatus.NOT_FOUND); + } + + Iterator<TitanVertex> iterator = vertices.iterator(); + List<GraphVertex> result = new ArrayList<>(); + + while (iterator.hasNext()) { + TitanVertex vertex = iterator.next(); + + Map<GraphPropertyEnum, Object> newProp = 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()); + } + if (result.size() == 0) { + return Either.right(TitanOperationStatus.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); + } + return Either.right(TitanGraphClient.handleTitanException(e)); + } + + } else { + if (logger.isDebugEnabled()) { + logger.debug("Failed get by criteria for type ={} and properties = {} error : {}", type, props, graph.right().value()); + } + return Either.right(graph.right().value()); + } + } + + public Either<Iterator<Vertex>, TitanOperationStatus> getCatalogOrArchiveVerticies(boolean isCatalog) { + Either<TitanGraph, TitanOperationStatus> graph = titanClient.getGraph(); + if (graph.isLeft()) { + try { + TitanGraph tGraph = graph.left().value(); + + String name = isCatalog ? VertexTypeEnum.CATALOG_ROOT.getName() : VertexTypeEnum.ARCHIVE_ROOT.getName(); + Iterable<TitanVertex> vCatalogIter = tGraph.query().has(GraphPropertyEnum.LABEL.getProperty(), name).vertices(); + if (vCatalogIter == null) { + logger.debug("Failed to fetch catalog vertex"); + return Either.right(TitanOperationStatus.GENERAL_ERROR); + } + TitanVertex catalogV = vCatalogIter.iterator().next(); + if (catalogV == null) { + logger.debug("Failed to fetch catalog vertex"); + return Either.right(TitanOperationStatus.GENERAL_ERROR); + } + String edgeLabel = isCatalog ? EdgeLabelEnum.CATALOG_ELEMENT.name() : EdgeLabelEnum.ARCHIVE_ELEMENT.name(); + Iterator<Vertex> vertices = catalogV.vertices(Direction.OUT, edgeLabel); + + return Either.left(vertices); + } catch (Exception e) { + if (logger.isDebugEnabled()) { + logger.debug("Failed get by criteria: ", e); + } + return Either.right(TitanGraphClient.handleTitanException(e)); + } + + } else { + if (logger.isDebugEnabled()) { + logger.debug("Failed get by criteria : ", graph.right().value()); + } + return Either.right(graph.right().value()); + } + } + + private void buildMultipleNegateQueryFromList(Map.Entry<GraphPropertyEnum, Object> entry, TitanGraphQuery query) { + List<Object> negateList = (List<Object>) entry.getValue(); + for (Object listItem : negateList) { + query.hasNot(entry.getKey().getProperty(), listItem); + } + } + + /** + * + * @param parentVertex + * @param edgeLabel + * @param parseFlag + * @return + */ + public Either<GraphVertex, TitanOperationStatus> getChildVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = getChildrenVertecies(parentVertex, edgeLabel, parseFlag); + if (childrenVertecies.isRight()) { + return Either.right(childrenVertecies.right().value()); + } + return Either.left(childrenVertecies.left().value().get(0)); + } + + /** + * + * @param parentVertex + * @param edgeLabel + * @param parseFlag + * @return + */ + public Either<Vertex, TitanOperationStatus> getChildVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + Either<List<Vertex>, TitanOperationStatus> childrenVertecies = getChildrenVertecies(parentVertex, edgeLabel, parseFlag); + if (childrenVertecies.isRight()) { + return Either.right(childrenVertecies.right().value()); + } + return Either.left(childrenVertecies.left().value().get(0)); + } + + public Either<GraphVertex, TitanOperationStatus> getParentVertex(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + Either<List<GraphVertex>, TitanOperationStatus> childrenVertecies = getParentVertecies(parentVertex, edgeLabel, parseFlag); + if (childrenVertecies.isRight()) { + return Either.right(childrenVertecies.right().value()); + } + if (isEmpty(childrenVertecies.left().value())){ + return Either.right(TitanOperationStatus.NOT_FOUND); + } + return Either.left(childrenVertecies.left().value().get(0)); + } + + public Either<Vertex, TitanOperationStatus> getParentVertex(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + Either<List<Vertex>, TitanOperationStatus> childrenVertecies = getParentVertecies(parentVertex, edgeLabel, parseFlag); + if (childrenVertecies.isRight() ) { + return Either.right(childrenVertecies.right().value()); + } + if (isEmpty(childrenVertecies.left().value())){ + return Either.right(TitanOperationStatus.NOT_FOUND); + } + return Either.left(childrenVertecies.left().value().get(0)); + } + + /** + * + * @param parentVertex + * @param edgeLabel + * @param parseFlag + * @return + */ + public Either<List<GraphVertex>, TitanOperationStatus> getChildrenVertecies(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.OUT); + } + + public Either<List<GraphVertex>, TitanOperationStatus> getParentVertecies(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.IN); + } + + public Either<List<Vertex>, TitanOperationStatus> getParentVertecies(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.IN); + } + + private Either<List<Vertex>, TitanOperationStatus> getAdjacentVerticies(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { + List<Vertex> list = new ArrayList<>(); + try { + Either<TitanGraph, TitanOperationStatus> graphRes = titanClient.getGraph(); + if (graphRes.isRight()) { + logger.error("Failed to retrieve graph. status is {}", graphRes); + return Either.right(graphRes.right().value()); + } + Iterator<Edge> edgesCreatorIterator = parentVertex.edges(direction, edgeLabel.name()); + if (edgesCreatorIterator != null) { + while (edgesCreatorIterator.hasNext()) { + Edge edge = edgesCreatorIterator.next(); + TitanVertex vertex; + if (direction == Direction.IN) { + vertex = (TitanVertex) edge.outVertex(); + } else { + vertex = (TitanVertex) edge.inVertex(); + } + // GraphVertex graphVertex = createAndFill(vertex, parseFlag); + + list.add(vertex); + } + } + if (list.isEmpty()) { + return Either.right(TitanOperationStatus.NOT_FOUND); + } + } catch (Exception e) { + logger.error("Failed to perform graph operation ", e); + Either.right(TitanGraphClient.handleTitanException(e)); + } + + return Either.left(list); + } + + /** + * + * @param parentVertex + * @param edgeLabel + * @param parseFlag + * @return + */ + public Either<List<Vertex>, TitanOperationStatus> getChildrenVertecies(Vertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag) { + return getAdjacentVerticies(parentVertex, edgeLabel, parseFlag, Direction.OUT); + } + + private Either<List<GraphVertex>, TitanOperationStatus> getAdjacentVerticies(GraphVertex parentVertex, EdgeLabelEnum edgeLabel, JsonParseFlagEnum parseFlag, Direction direction) { + List<GraphVertex> list = new ArrayList<>(); + + Either<List<Vertex>, TitanOperationStatus> adjacentVerticies = getAdjacentVerticies(parentVertex.getVertex(), edgeLabel, parseFlag, direction); + if (adjacentVerticies.isRight()) { + return Either.right(adjacentVerticies.right().value()); + } + adjacentVerticies.left().value().stream().forEach(vertex -> { + list.add(createAndFill((TitanVertex) vertex, parseFlag)); + }); + + return Either.left(list); + } + + /** + * Searches Edge by received label and criteria + * + * @param vertex + * @param label + * @param properties + * @return found edge or TitanOperationStatus + */ + public Either<Edge, TitanOperationStatus> getBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) { + + Either<Edge, TitanOperationStatus> result = null; + Edge matchingEdge = null; + String notFoundMsg = "No edges in graph for criteria"; + try { + TitanVertexQuery<?> query = vertex.getVertex().query().labels(label.name()); + + if (properties != null && !properties.isEmpty()) { + for (Map.Entry<GraphPropertyEnum, Object> entry : properties.entrySet()) { + query = query.has(entry.getKey().getProperty(), entry.getValue()); + } + } + + Iterable<TitanEdge> edges = query.edges(); + if (edges == null) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg); + result = Either.right(TitanOperationStatus.NOT_FOUND); + } else { + Iterator<TitanEdge> eIter = edges.iterator(); + if (eIter.hasNext()) { + matchingEdge = eIter.next(); + } else { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg); + result = Either.right(TitanOperationStatus.NOT_FOUND); + } + } + if (result == null) { + result = Either.left(matchingEdge); + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during getting edge by criteria for component with id {}. {}", vertex.getUniqueId(), e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + return result; + } + + public Either<Edge, TitanOperationStatus> getEdgeByChildrenVertexProperties(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) { + Either<Edge, TitanOperationStatus> result = null; + Edge matchingEdge = null; + String notFoundMsg = "No edges in graph for criteria"; + try { + + Iterator<Edge> edges = vertex.getVertex().edges(Direction.OUT, label.name()); + while (edges.hasNext()) { + matchingEdge = edges.next(); + Vertex childV = matchingEdge.inVertex(); + Map<GraphPropertyEnum, Object> vertexProperties = getVertexProperties(childV); + Optional<Entry<GraphPropertyEnum, Object>> findNotMatch = properties.entrySet().stream().filter(e -> vertexProperties.get(e.getKey()) == null || !vertexProperties.get(e.getKey()).equals(e.getValue())).findFirst(); + if (!findNotMatch.isPresent()) { + result = Either.left(matchingEdge); + } + } + if (result == null) { + //no match + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, notFoundMsg); + result = Either.right(TitanOperationStatus.NOT_FOUND); + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during getting edge by criteria for component with id {}. {}", vertex.getUniqueId(), e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + return result; + } + + /** + * Deletes Edge by received label and criteria + * + * @param vertex + * @param label + * @param properties + * @return + */ + public Either<Edge, TitanOperationStatus> deleteBelongingEdgeByCriteria(GraphVertex vertex, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) { + Either<Edge, TitanOperationStatus> result = null; + try { + result = getBelongingEdgeByCriteria(vertex, label, properties); + if (result.isLeft()) { + Edge edge = result.left().value(); + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to delete an edge with the label {} belonging to the vertex {} ", label.name(), vertex.getUniqueId()); + edge.remove(); + result = Either.left(edge); + } else { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to find an edge with the label {} belonging to the vertex {} ", label.name(), vertex.getUniqueId()); + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge by criteria for the component with id {}. {}", vertex == null ? "NULL" : vertex.getUniqueId(), e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + return result; + } + + @SuppressWarnings("unchecked") + /** + * Deletes an edge between vertices fromVertex and toVertex according to received label + * + * @param fromVertex + * @param toVertex + * @param label + * @return + */ + + public Either<Edge, TitanOperationStatus> deleteEdge(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { + return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), false); + } + + public Either<Edge, TitanOperationStatus> deleteAllEdges(GraphVertex fromVertex, GraphVertex toVertex, EdgeLabelEnum label) { + return deleteEdge(fromVertex.getVertex(), toVertex.getVertex(), label, fromVertex.getUniqueId(), toVertex.getUniqueId(), true); + } + + public Either<Edge, TitanOperationStatus> deleteEdge(TitanVertex fromVertex, TitanVertex toVertex, EdgeLabelEnum label, String uniqueIdFrom, String uniqueIdTo, boolean deleteAll) { + Either<Edge, TitanOperationStatus> result = null; + try { + Iterable<TitanEdge> edges = fromVertex.query().labels(label.name()).edges(); + Iterator<TitanEdge> eIter = edges.iterator(); + while (eIter.hasNext()) { + Edge edge = eIter.next(); + String currVertexUniqueId = edge.inVertex().value(GraphPropertyEnum.UNIQUE_ID.getProperty()); + if (currVertexUniqueId != null && currVertexUniqueId.equals(uniqueIdTo)) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to delete an edge with the label {} between vertices {} and {}. ", label.name(), uniqueIdFrom, uniqueIdTo); + edge.remove(); + result = Either.left(edge); + if (!deleteAll) { + break; + } + } + } + if (result == null) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to delete an edge with the label {} between vertices {} and {}. ", label.name(), uniqueIdFrom, uniqueIdTo); + result = Either.right(TitanOperationStatus.NOT_FOUND); + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge with the label {} between vertices {} and {}. {}", label.name(), uniqueIdFrom, uniqueIdTo, e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + return result; + } + + public TitanOperationStatus deleteEdgeByDirection(GraphVertex fromVertex, Direction direction, EdgeLabelEnum label) { + try { + Iterator<Edge> edges = fromVertex.getVertex().edges(direction, label.name()); + + while (edges.hasNext()) { + Edge edge = edges.next(); + edge.remove(); + } + } catch (Exception e) { + logger.debug("Failed to remove from vertex {} edges {} by direction {} ", fromVertex.getUniqueId(), label, direction, e); + return TitanGraphClient.handleTitanException(e); + } + return TitanOperationStatus.OK; + } + + /** + * Updates vertex properties. Note that graphVertex argument should contain updated data + * + * @param graphVertex + * @return + */ + public Either<GraphVertex, TitanOperationStatus> updateVertex(GraphVertex graphVertex) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId()); + try { + graphVertex.updateMetadataJsonWithCurrentMetadataProperties(); + setVertexProperties(graphVertex.getVertex(), graphVertex); + + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to update metadata of vertex with uniqueId {}. ", graphVertex.getUniqueId(), e); + return Either.right(TitanGraphClient.handleTitanException(e)); + } + return Either.left(graphVertex); + } + + /** + * Fetches vertices by uniqueId according to received parse flag + * + * @param verticesToGet + * @return + */ + public Either<Map<String, GraphVertex>, TitanOperationStatus> getVerticesByUniqueIdAndParseFlag(Map<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> verticesToGet) { + + Either<Map<String, GraphVertex>, TitanOperationStatus> result = null; + Map<String, GraphVertex> vertices = new HashMap<>(); + TitanOperationStatus titatStatus; + Either<GraphVertex, TitanOperationStatus> getVertexRes = null; + for (Map.Entry<String, ImmutablePair<GraphPropertyEnum, JsonParseFlagEnum>> entry : verticesToGet.entrySet()) { + if (entry.getValue().getKey() == GraphPropertyEnum.UNIQUE_ID) { + getVertexRes = getVertexById(entry.getKey(), entry.getValue().getValue()); + } else if (entry.getValue().getKey() == GraphPropertyEnum.USERID) { + getVertexRes = getVertexByPropertyAndLabel(entry.getValue().getKey(), entry.getKey(), VertexTypeEnum.USER, entry.getValue().getValue()); + } + if (getVertexRes == null) { + titatStatus = TitanOperationStatus.ILLEGAL_ARGUMENT; + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Invalid vertex type label {} has been received. ", entry.getValue().getKey(), titatStatus); + return Either.right(titatStatus); + } + if (getVertexRes.isRight()) { + titatStatus = getVertexRes.right().value(); + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to get vertex by id {} . Status is {}. ", entry.getKey(), titatStatus); + result = Either.right(titatStatus); + break; + } else { + vertices.put(entry.getKey(), getVertexRes.left().value()); + } + } + if (result == null) { + result = Either.left(vertices); + } + return result; + } + + /** + * Creates edge between "from" and "to" vertices with specified label and properties extracted from received edge + * + * @param from + * @param to + * @param label + * @param edgeToCopy + * @return + */ + public TitanOperationStatus createEdge(Vertex from, Vertex to, EdgeLabelEnum label, Edge edgeToCopy) { + return createEdge(from, to, label, getEdgeProperties(edgeToCopy)); + } + + public TitanOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, Edge prevEdge, EdgeLabelEnum prevLabel, EdgeLabelEnum newLabel) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Going to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel, fromVertex!=null ? fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()) : "NULL", + toVertex!=null ? toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()) : "NULL"); + + TitanOperationStatus result = createEdge(fromVertex, toVertex, newLabel, prevEdge); + if (result == TitanOperationStatus.OK) { + prevEdge.remove(); + } + return result; + } + + /** + * Replaces previous label of edge with new label + * + * @param fromVertex + * @param toVertex + * @param prevLabel + * @param newLabel + * @return + */ + public TitanOperationStatus replaceEdgeLabel(Vertex fromVertex, Vertex toVertex, EdgeLabelEnum prevLabel, EdgeLabelEnum newLabel) { + + TitanOperationStatus result = null; + Iterator<Edge> prevEdgeIter = toVertex.edges(Direction.IN, prevLabel.name()); + if (prevEdgeIter == null || !prevEdgeIter.hasNext()) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Failed to replace edge with label {} to {} between vertices {} and {}", prevLabel, newLabel, fromVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), + toVertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty())); + result = TitanOperationStatus.NOT_FOUND; + } + if (result == null) { + result = replaceEdgeLabel(fromVertex, toVertex, prevEdgeIter.next(), prevLabel, newLabel); + } + return result; + } + + /** + * Updates metadata properties of vertex on graph. Json metadata property of the vertex will be updated with received properties too. + * + * + * @param vertex + * @param properties + * @return + */ + public TitanOperationStatus updateVertexMetadataPropertiesWithJson(Vertex vertex, Map<GraphPropertyEnum, Object> properties) { + try { + if (!MapUtils.isEmpty(properties)) { + String jsonMetadataStr = (String) vertex.property(GraphPropertyEnum.METADATA.getProperty()).value(); + Map<String, Object> jsonMetadataMap = JsonParserUtils.toMap(jsonMetadataStr); + for (Map.Entry<GraphPropertyEnum, Object> property : properties.entrySet()) { + vertex.property(property.getKey().getProperty(), property.getValue()); + jsonMetadataMap.put(property.getKey().getProperty(), property.getValue()); + } + vertex.property(GraphPropertyEnum.METADATA.getProperty(), JsonParserUtils.toJson(jsonMetadataMap)); + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occurred during update vertex metadata properties with json{}. {}", vertex.property(GraphPropertyEnum.UNIQUE_ID.getProperty()), e.getMessage()); + return TitanGraphClient.handleTitanException(e); + } + return TitanOperationStatus.OK; + } + + public TitanOperationStatus disassociateAndDeleteLast(GraphVertex vertex, Direction direction, EdgeLabelEnum label) { + try { + Iterator<Edge> edges = vertex.getVertex().edges(direction, label.name()); + + while (edges.hasNext()) { + Edge edge = edges.next(); + Vertex secondVertex; + Direction reverseDirection; + if (direction == Direction.IN) { + secondVertex = edge.outVertex(); + reverseDirection = Direction.OUT; + } else { + secondVertex = edge.inVertex(); + reverseDirection = Direction.IN; + } + edge.remove(); + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "Edge {} with direction {} was removed from {}", label.name(), direction, vertex.getVertex()); + + Iterator<Edge> restOfEdges = secondVertex.edges(reverseDirection, label.name()); + if (!restOfEdges.hasNext()) { + secondVertex.remove(); + CommonUtility.addRecordToLog(logger, LogLevelEnum.TRACE, "This was last edge . Vertex {} was removed ", vertex.getUniqueId()); + } + } + } catch (Exception e) { + CommonUtility.addRecordToLog(logger, LogLevelEnum.DEBUG, "Exception occured during deleting an edge with the label {} direction {} from vertex {}. {}", label.name(), direction, vertex.getUniqueId(), e); + return TitanGraphClient.handleTitanException(e); + } + return TitanOperationStatus.OK; + } + + public Object getProperty(TitanVertex vertex, String key) { + PropertyKey propertyKey = titanClient.getGraph().left().value().getPropertyKey(key); + return vertex.valueOrNull(propertyKey); + } + + public Object getProperty(Edge edge, EdgePropertyEnum key) { + Object value = null; + try { + Property<Object> property = edge.property(key.getProperty()); + if (property != null) { + value = property.orElse(null); + if (value != null && key == EdgePropertyEnum.INSTANCES) { + return JsonParserUtils.toList((String) value, String.class); + } + return value; + } + } catch (Exception e) { + + } + return value; + } + + /** + * + * @param vertexA + * @param vertexB + * @param label + * @param direction + * @return + */ + public TitanOperationStatus moveEdge(GraphVertex vertexA, GraphVertex vertexB, EdgeLabelEnum label, Direction direction) { + TitanOperationStatus result = deleteEdgeByDirection(vertexA, direction, label); + if (result != TitanOperationStatus.OK) { + logger.error("Failed to diassociate {} from element {}. error {} ", label, vertexA.getUniqueId(), result); + return result; + } + TitanOperationStatus createRelation; + if (direction == Direction.IN) { + createRelation = createEdge(vertexB, vertexA, label, null); + } else { + createRelation = createEdge(vertexA, vertexB, label, null); + } + if (createRelation != TitanOperationStatus.OK) { + return createRelation; + } + return TitanOperationStatus.OK; + } + + public Either<Edge, TitanOperationStatus> getBelongingEdgeByCriteria(String parentId, EdgeLabelEnum label, Map<GraphPropertyEnum, Object> properties) { + Either<GraphVertex, TitanOperationStatus> getVertexRes = getVertexById(parentId, JsonParseFlagEnum.NoParse); + if (getVertexRes.isRight()) { + return Either.right(getVertexRes.right().value()); + } + return getBelongingEdgeByCriteria(getVertexRes.left().value(), label, properties); + } } diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgeLabelEnum.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgeLabelEnum.java index eda2805087..1621a13c67 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgeLabelEnum.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgeLabelEnum.java @@ -57,9 +57,13 @@ public enum EdgeLabelEnum { CALCULATED_CAP_PROPERTIES, POLICIES, EXTERNAL_REFS, - CATALOG_ELEMENT; - - /** + CATALOG_ELEMENT, + ARCHIVE_ELEMENT, + INSTANCE_OF, + PROXY_OF, + ALLOTTED_OF; + + /** * Returns EdgeLabelEnum according received name * @param name * @return diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgePropertyEnum.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgePropertyEnum.java index e52c92e74b..e47a78d9f0 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgePropertyEnum.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/EdgePropertyEnum.java @@ -22,7 +22,8 @@ package org.openecomp.sdc.be.dao.jsongraph.types; public enum EdgePropertyEnum { - STATE ("state"); + STATE ("state"), + INSTANCES("instances"); private String property; diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/VertexTypeEnum.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/VertexTypeEnum.java index 92880c1c97..3917179d5e 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/VertexTypeEnum.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/types/VertexTypeEnum.java @@ -20,23 +20,7 @@ package org.openecomp.sdc.be.dao.jsongraph.types; -import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ForwardingPathDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.InterfaceDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapArtifactDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapCapabiltyProperty; -import org.openecomp.sdc.be.datatypes.elements.MapComponentInstanceExternalRefs; -import org.openecomp.sdc.be.datatypes.elements.MapGroupsDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapListCapabiltyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapListRequirementDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PolicyDataDefinition; -import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition; +import org.openecomp.sdc.be.datatypes.elements.*; public enum VertexTypeEnum { @@ -65,15 +49,16 @@ public enum VertexTypeEnum { INST_INPUTS ("instInputs", MapPropertiesDataDefinition.class), INST_GROUPS ("instGroups", MapGroupsDataDefinition.class), SERVICE_API_ARTIFACTS ("serviceApiArtifacts", ArtifactDataDefinition.class), - CALCULATED_CAPABILITIES ("calculatedCapabilities", MapListCapabiltyDataDefinition.class), - FULLFILLED_CAPABILITIES ("fullfilledCapabilities", MapListCapabiltyDataDefinition.class), + CALCULATED_CAPABILITIES ("calculatedCapabilities", MapListCapabilityDataDefinition.class), + FULLFILLED_CAPABILITIES ("fullfilledCapabilities", MapListCapabilityDataDefinition.class), CALCULATED_REQUIREMENTS ("calculatedRequirements", MapListRequirementDataDefinition.class), FULLFILLED_REQUIREMENTS ("fullfilledRequirements", MapListRequirementDataDefinition.class), - CALCULATED_CAP_PROPERTIES ("calculatedCapProperties", MapCapabiltyProperty.class), + CALCULATED_CAP_PROPERTIES ("calculatedCapProperties", MapCapabilityProperty.class), FORWARDING_PATH ("path", ForwardingPathDataDefinition.class), POLICIES ("policies", PolicyDataDefinition.class), EXTERNAL_REF ("componentInstanceExtRefs", MapComponentInstanceExternalRefs.class), - CATALOG_ROOT ("catalogRoot", null); + CATALOG_ROOT ("catalogRoot", null), + ARCHIVE_ROOT ("archiveRoot", null); private String name; private Class classOfJson; diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/IdBuilderUtils.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/IdBuilderUtils.java index 7b0719e063..2d2d9b8962 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/IdBuilderUtils.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/IdBuilderUtils.java @@ -20,10 +20,10 @@ package org.openecomp.sdc.be.dao.jsongraph.utils; -import java.util.UUID; - import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum; +import java.util.UUID; + public class IdBuilderUtils { private static String DOT = "."; diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/JsonParserUtils.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/JsonParserUtils.java index 198d3b456c..9a6e70b4d8 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/JsonParserUtils.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/utils/JsonParserUtils.java @@ -20,13 +20,6 @@ package org.openecomp.sdc.be.dao.jsongraph.utils; -import java.io.IOException; -import java.util.Map; - -import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; @@ -34,9 +27,15 @@ import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.google.common.base.Strings; +import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition; +import org.openecomp.sdc.common.log.wrappers.Logger; + +import java.io.IOException; +import java.util.List; +import java.util.Map; public class JsonParserUtils { - private static final Logger log = LoggerFactory.getLogger(JsonParserUtils.class.getName()); + private static Logger log = Logger.getLogger(JsonParserUtils.class.getName()); private static final ObjectMapper mapper = buildObjectMapper(); private JsonParserUtils() { @@ -86,7 +85,24 @@ public class JsonParserUtils { .readValue(json); } catch (Exception e) { - log.debug("Failed to parse json {}", json, e); + log.debug("Failed to parse json {} to map", json, e); + } + return object; + } + public static <T> List<T> toList(String json, Class<T> clazz) { + if (Strings.isNullOrEmpty(json)) { + return null; + } + List<T> object = null; + try { + JavaType type = mapper.getTypeFactory() + .constructCollectionType(List.class, clazz); + + object = mapper.readerFor(type) + .readValue(json); + } + catch (Exception e) { + log.debug("Failed to parse json {} to list", json, e); } return object; } |