diff options
author | 2017-11-16 15:19:58 -0500 | |
---|---|---|
committer | 2017-11-16 15:21:11 -0500 | |
commit | 2dd042556f9142fcd525c0277ea21ac3bd828e4f (patch) | |
tree | d1c5a3825ef509270c95ec954fa629743e9c7047 /src/main/java/org/openecomp/crud/dao | |
parent | be2eab9395a787ca4b13447f9d2c382d12f5dcd5 (diff) |
Fixing the formatting
Fixing the formatting
IssueID: AAI-481
Change-Id: I45c53cd24913707716f2dd34f9fc282786ce03b6
Signed-off-by: Bansal, Nitin (nb121v) <nitin.bansal@amdocs.com>
Diffstat (limited to 'src/main/java/org/openecomp/crud/dao')
3 files changed, 922 insertions, 795 deletions
diff --git a/src/main/java/org/openecomp/crud/dao/GraphDao.java b/src/main/java/org/openecomp/crud/dao/GraphDao.java index 67c1ff3..7e8c043 100644 --- a/src/main/java/org/openecomp/crud/dao/GraphDao.java +++ b/src/main/java/org/openecomp/crud/dao/GraphDao.java @@ -38,40 +38,48 @@ public interface GraphDao { public Vertex getVertex(String id, String type) throws CrudException; /** - * Retrieve all of the edges which are incident to the vertex with the specified identifier. + * Retrieve all of the edges which are incident to the vertex with the + * specified identifier. * - * @param id - The unique identifier of the vertex to retrieve the edges for. + * @param id + * - The unique identifier of the vertex to retrieve the edges for. * @return - A collection of edges. * @throws CrudException */ public List<Edge> getVertexEdges(String id) throws CrudException; /** - * Retrieve a collection of {@link Vertex} objects which match the supplied type label - * and filter properties. + * Retrieve a collection of {@link Vertex} objects which match the supplied + * type label and filter properties. * - * @param type - The vertex type that we want to retrieve. - * @param filter - The parameters to filter our results by. + * @param type + * - The vertex type that we want to retrieve. + * @param filter + * - The parameters to filter our results by. * @return - A collection of vertices. * @throws CrudException */ public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException; /** - * Retrieve an {@link Edge} from the graph database by specifying its unique identifier. + * Retrieve an {@link Edge} from the graph database by specifying its unique + * identifier. * - * @param id - The unique identifier for the Edge to be retrieved. + * @param id + * - The unique identifier for the Edge to be retrieved. * @return - The Edge corresponding to the specified identifier. * @throws CrudException */ public Edge getEdge(String id, String type) throws CrudException; /** - * Retrieve a collection of {@link Edge} objects with a given type and which match a set of - * supplied filter parameters. + * Retrieve a collection of {@link Edge} objects with a given type and which + * match a set of supplied filter parameters. * - * @param type - The type of edges that we are interested in. - * @param filter - The parameters that we want to filter our edges by. + * @param type + * - The type of edges that we are interested in. + * @param filter + * - The parameters that we want to filter our edges by. * @return - A collection of edges which match the supplied filter parameters. * @throws CrudException */ @@ -80,8 +88,10 @@ public interface GraphDao { /** * Insert a new {@link Vertex} into the graph data store. * - * @param type - The type label to assign to the vertex. - * @param properties - The properties to associated with this vertex. + * @param type + * - The type label to assign to the vertex. + * @param properties + * - The properties to associated with this vertex. * @return - The {@link Vertex} object that was created. * @throws CrudException */ @@ -90,20 +100,23 @@ public interface GraphDao { /** * Updates an existing {@link Vertex}. * - * @param id - The unique identifier of the vertex to be updated. - * @param properties - The properties to associate with the vertex. + * @param id + * - The unique identifier of the vertex to be updated. + * @param properties + * - The properties to associate with the vertex. * @return - The udpated vertex. * @throws CrudException */ - public Vertex updateVertex(String id, String type, Map<String, Object> properties) - throws CrudException; + public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException; /** * Removes the specified vertex from the graph data base. * - * <p>NOTE: The vertex MUST contain NO incident edges before it can be deleted. + * <p> + * NOTE: The vertex MUST contain NO incident edges before it can be deleted. * - * @param id - The unique identifier of the vertex to be deleted. + * @param id + * - The unique identifier of the vertex to be deleted. * @throws CrudException */ public void deleteVertex(String id, String type) throws CrudException; @@ -111,21 +124,26 @@ public interface GraphDao { /** * Adds an edge to the graph database. * - * @param type - The 'type' label to apply to the edge. - * @param source - The source vertex for this edge. - * @param target - The target vertex for this edge. - * @param properties - The properties map to associate with this edge. + * @param type + * - The 'type' label to apply to the edge. + * @param source + * - The source vertex for this edge. + * @param target + * - The target vertex for this edge. + * @param properties + * - The properties map to associate with this edge. * @return - The {@link Edge} object that was created. * @throws CrudException */ - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) - throws CrudException; + public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException; /** * Updates an existing {@link Edge}. * - * @param id - The unique identifier of the edge to be updated. - * @param properties - The properties to associate with the edge. + * @param id + * - The unique identifier of the edge to be updated. + * @param properties + * - The properties to associate with the edge. * @return - The update edge. * @throws CrudException */ @@ -134,25 +152,32 @@ public interface GraphDao { /** * Remove the specified edge from the graph data base. * - * @param id - The unique identifier of the edge to be deleted. + * @param id + * - The unique identifier of the edge to be deleted. * @throws CrudException */ public void deleteEdge(String id, String type) throws CrudException; - - + public String openTransaction(); + public void commitTransaction(String id) throws CrudException; + public void rollbackTransaction(String id) throws CrudException; + public boolean transactionExists(String id) throws CrudException; - + public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException; + public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId) - throws CrudException; - public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) - throws CrudException; - + throws CrudException; + + public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException; + public Edge updateEdge(Edge edge, String txId) throws CrudException; + public void deleteVertex(String id, String type, String txId) throws CrudException; + public void deleteEdge(String id, String type, String txId) throws CrudException; + public Edge getEdge(String id, String type, String txId) throws CrudException; } diff --git a/src/main/java/org/openecomp/crud/dao/champ/ChampDao.java b/src/main/java/org/openecomp/crud/dao/champ/ChampDao.java index ed01038..93c3cdd 100644 --- a/src/main/java/org/openecomp/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/openecomp/crud/dao/champ/ChampDao.java @@ -51,769 +51,833 @@ import org.openecomp.crud.exception.CrudException; import org.openecomp.crud.logging.CrudServiceMsgs; /** - * This is the integration layer between the CRUD API service and the low level Champ library for graph database - * interaction. + * This is the integration layer between the CRUD API service and the low level + * Champ library for graph database interaction. */ public class ChampDao implements GraphDao { - public static final String CONFIG_STORAGE_BACKEND = "storage.backend"; - public static final String CONFIG_STORAGE_BACKEND_DB = "storage.backend.db"; - public static final String STORAGE_HBASE_DB = "hbase"; - public static final String STORAGE_CASSANDRA_DB = "cassandra"; - public static final String CONFIG_STORAGE_HOSTNAMES = "storage.hostnames"; - public static final String CONFIG_STORAGE_PORT = "storage.port"; - public static final String CONFIG_HBASE_ZNODE_PARENT = "storage.hbase.ext.zookeeper.znode.parent"; - public static final String CONFIG_GRAPH_NAME = "graph.name"; - public static final String GRAPH_UNQ_INSTANCE_ID_SUFFIX = "graph.unique-instance-id-suffix"; + public static final String CONFIG_STORAGE_BACKEND = "storage.backend"; + public static final String CONFIG_STORAGE_BACKEND_DB = "storage.backend.db"; + public static final String STORAGE_HBASE_DB = "hbase"; + public static final String STORAGE_CASSANDRA_DB = "cassandra"; + public static final String CONFIG_STORAGE_HOSTNAMES = "storage.hostnames"; + public static final String CONFIG_STORAGE_PORT = "storage.port"; + public static final String CONFIG_HBASE_ZNODE_PARENT = "storage.hbase.ext.zookeeper.znode.parent"; + public static final String CONFIG_GRAPH_NAME = "graph.name"; + public static final String GRAPH_UNQ_INSTANCE_ID_SUFFIX = "graph.unique-instance-id-suffix"; - public static final String CONFIG_EVENT_STREAM_PUBLISHER = "event.stream.publisher"; - public static final String CONFIG_EVENT_STREAM_NUM_PUBLISHERS = "event.stream.num-publishers"; + public static final String CONFIG_EVENT_STREAM_PUBLISHER = "event.stream.publisher"; + public static final String CONFIG_EVENT_STREAM_NUM_PUBLISHERS = "event.stream.num-publishers"; - private static Map<String, ChampTransaction> transactions = new ConcurrentHashMap<String, ChampTransaction>(); - public static final String DEFAULT_GRAPH_NAME = "default_graph"; + private static Map<String, ChampTransaction> transactions = new ConcurrentHashMap<String, ChampTransaction>(); + public static final String DEFAULT_GRAPH_NAME = "default_graph"; - private enum GraphType { - IN_MEMORY, TITAN, DSE - } + private enum GraphType { + IN_MEMORY, TITAN, DSE + } - /** - * Instance of the API used for interacting with the Champ library. - */ - private ChampGraph champApi = null; + /** + * Instance of the API used for interacting with the Champ library. + */ + private ChampGraph champApi = null; - private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName()); + private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName()); - /** - * Creates a new instance of the ChampDao. - * - * @param champGraph - * - Concrete implementation of the graph dao layer - */ - public ChampDao(ChampGraph champGraph) { - this.champApi = champGraph; - } + /** + * Creates a new instance of the ChampDao. + * + * @param champGraph + * - Concrete implementation of the graph dao layer + */ + public ChampDao(ChampGraph champGraph) { + this.champApi = champGraph; + } - @Override - public Vertex getVertex(String id) throws CrudException { + @Override + public Vertex getVertex(String id) throws CrudException { - try { + try { - if (logger.isDebugEnabled()) { - logger.debug("getVertex with id: " + id); - } + if (logger.isDebugEnabled()) { + logger.debug("getVertex with id: " + id); + } - long idAsLong = Long.parseLong(id); + long idAsLong = Long.parseLong(id); - Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); + Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); - String nodeType = org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(); - if (retrievedVertex.isPresent() && retrievedVertex.get().getProperties().get(nodeType) != null) { - return vertexFromChampObject(retrievedVertex.get(), retrievedVertex.get().getProperties().get(nodeType).toString()); - } else { + String nodeType = org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(); + if (retrievedVertex.isPresent() && retrievedVertex.get().getProperties().get(nodeType) != null) { + return vertexFromChampObject(retrievedVertex.get(), + retrievedVertex.get().getProperties().get(nodeType).toString()); + } else { - // We didn't find a vertex with the supplied id, so just throw an - // exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } - } catch (ChampUnmarshallingException | ChampTransactionException e) { + } catch (ChampUnmarshallingException | ChampTransactionException e) { - // Something went wrong - throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } + // Something went wrong - throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } - @Override - public Vertex getVertex(String id, String type) throws CrudException { + @Override + public Vertex getVertex(String id, String type) throws CrudException { - try { + try { - if (logger.isDebugEnabled()) { - logger.debug("getVertex with id: " + id); - } + if (logger.isDebugEnabled()) { + logger.debug("getVertex with id: " + id); + } - long idAsLong = Long.parseLong(id); + long idAsLong = Long.parseLong(id); - // Request the vertex from the graph db. - Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); + // Request the vertex from the graph db. + Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); - // Did we find it? - if (retrievedVertex.isPresent() - && retrievedVertex.get().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) != null - && retrievedVertex.get().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString() - .equalsIgnoreCase(type)) { + // Did we find it? + if (retrievedVertex.isPresent() + && retrievedVertex.get().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) != null + && retrievedVertex.get().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString() + .equalsIgnoreCase(type)) { - // Yup, convert it to a Vector object and return it. - return vertexFromChampObject(retrievedVertex.get(), type); + // Yup, convert it to a Vector object and return it. + return vertexFromChampObject(retrievedVertex.get(), type); - } else { + } else { - // We didn't find a vertex with the supplied id, so just throw an - // exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } - } catch (ChampUnmarshallingException | ChampTransactionException e) { + } catch (ChampUnmarshallingException | ChampTransactionException e) { - // Something went wrong - throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } + // Something went wrong - throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } - @Override - public List<Edge> getVertexEdges(String id) throws CrudException { + @Override + public List<Edge> getVertexEdges(String id) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("get Edges incident to vertex with id: " + id + " from graph"); - } + if (logger.isDebugEnabled()) { + logger.debug("get Edges incident to vertex with id: " + id + " from graph"); + } - try { - long idAsLong = Long.parseLong(id); // GDF - what to do about id??? + try { + long idAsLong = Long.parseLong(id); // GDF - what to do about id??? - // Request the vertex from the graph db. - Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); + // Request the vertex from the graph db. + Optional<ChampObject> retrievedVertex = champApi.retrieveObject(idAsLong); - // Did we find it? - if (retrievedVertex.isPresent()) { + // Did we find it? + if (retrievedVertex.isPresent()) { - // Query the Champ library for the edges which are incident to the specified - // vertex. - Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get()); + // Query the Champ library for the edges which are incident to the + // specified + // vertex. + Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get()); - // Build an edge list from the result stream. - List<Edge> edges = new ArrayList<Edge>(); - relationships.forEach(r -> edges.add(edgeFromChampRelationship(r))); + // Build an edge list from the result stream. + List<Edge> edges = new ArrayList<Edge>(); + relationships.forEach(r -> edges.add(edgeFromChampRelationship(r))); - return edges; + return edges; - } else { + } else { - // We couldn't find the specified vertex, so throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } + // We couldn't find the specified vertex, so throw an exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } - } catch (ChampUnmarshallingException e) { + } catch (ChampUnmarshallingException e) { - // Something went wrong, so throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + // Something went wrong, so throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } catch (ChampObjectNotExistsException e) { + } catch (ChampObjectNotExistsException e) { - // We couldn't find the specified vertex, so throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } catch (ChampTransactionException e) { - throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } + // We couldn't find the specified vertex, so throw an exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } catch (ChampTransactionException e) { + throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } - @Override - public Vertex addVertex(String type, Map<String, Object> properties) throws CrudException { + @Override + public Vertex addVertex(String type, Map<String, Object> properties) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties)); - } + if (logger.isDebugEnabled()) { + logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties)); + } - // Add the aai_node_type so that AAI can read the data created by gizmo - properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + // Add the aai_node_type so that AAI can read the data created by gizmo + properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - // Create an object to represent our vertex in the format expected by the Champ library. - ChampObject objectToCreate = buildChampObject(type, properties); + // Create an object to represent our vertex in the format expected by the + // Champ library. + ChampObject objectToCreate = buildChampObject(type, properties); - try { + try { - // Ask the Champ library to store our vertex, placing the returned object into a - // list so that we can easily put that into our result object. - return vertexFromChampObject(champApi.storeObject(objectToCreate), type); + // Ask the Champ library to store our vertex, placing the returned object + // into a + // list so that we can easily put that into our result object. + return vertexFromChampObject(champApi.storeObject(objectToCreate), type); - } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException | ChampTransactionException e) { + } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException + | ChampTransactionException e) { - // Something went wrong - throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } + // Something went wrong - throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } - @Override - public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException { + @Override + public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties)); - } - // Add the aai_node_type so that AAI can read the data created by gizmo - properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + if (logger.isDebugEnabled()) { + logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties)); + } + // Add the aai_node_type so that AAI can read the data created by gizmo + properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - try { - // Now, build the updated version of the Champ Object... - ChampObject updateObject = buildChampObject(id, type, properties); - // ...and send it to the Champ library. - return vertexFromChampObject(champApi.replaceObject(updateObject), type); + try { + // Now, build the updated version of the Champ Object... + ChampObject updateObject = buildChampObject(id, type, properties); + // ...and send it to the Champ library. + return vertexFromChampObject(champApi.replaceObject(updateObject), type); - } catch (ChampObjectNotExistsException e) { - throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); - } catch (NumberFormatException | ChampMarshallingException | ChampSchemaViolationException e) { - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } catch (ChampTransactionException e) { - throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } + } catch (ChampObjectNotExistsException e) { + throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); + } catch (NumberFormatException | ChampMarshallingException | ChampSchemaViolationException e) { + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } catch (ChampTransactionException e) { + throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } - } + } - @Override - public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException { + @Override + public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Retrieve vertices with type label: " + type + " which map query parameters: " + propertiesMapToString(filter)); - } + if (logger.isDebugEnabled()) { + logger.debug("Retrieve vertices with type label: " + type + " which map query parameters: " + + propertiesMapToString(filter)); + } - filter.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + filter.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - Stream<ChampObject> retrievedVertices; - try { - retrievedVertices = champApi.queryObjects(filter); + Stream<ChampObject> retrievedVertices; + try { + retrievedVertices = champApi.queryObjects(filter); - } catch (ChampTransactionException e) { - throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } + } catch (ChampTransactionException e) { + throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } - List<Vertex> vertices = retrievedVertices.map(v -> vertexFromChampObject(v, type)).collect(Collectors.toList()); + List<Vertex> vertices = retrievedVertices.map(v -> vertexFromChampObject(v, type)).collect(Collectors.toList()); - if (logger.isDebugEnabled()) { - logger.debug("Resulting vertex list: " + retrievedVertices); - } + if (logger.isDebugEnabled()) { + logger.debug("Resulting vertex list: " + retrievedVertices); + } - // ...and return it to the caller. - return vertices; - } + // ...and return it to the caller. + return vertices; + } - private Object getRelKey(String id) { - Object key = id; - // convert into Long if applicable . TODO : revisit in story NUC-304 - try { - key = Long.parseLong(id); - } catch (NumberFormatException e) { - // The id isn't a Long, leave it as a string - } + private Object getRelKey(String id) { + Object key = id; + // convert into Long if applicable . TODO : revisit in story NUC-304 + try { + key = Long.parseLong(id); + } catch (NumberFormatException e) { + // The id isn't a Long, leave it as a string + } - return key; - } + return key; + } + + @Override + public Edge getEdge(String id, String type) throws CrudException { + + if (logger.isDebugEnabled()) { + logger.debug("Get edge with id: " + id); + } + + try { + + // Request the edge from the graph db. + Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id)); + + // Did we find it? + if (relationship.isPresent() && relationship.get().getType().equals(type)) { + + // Yup - return the result. + return edgeFromChampRelationship(relationship.get()); + + } else { - @Override - public Edge getEdge(String id, String type) throws CrudException { + // We didn't find an edge with the supplied id, so throw an exception. + throw new CrudException("No edge with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } + + } catch (ChampUnmarshallingException | ChampTransactionException e) { + + // Something went wrong, so throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException { + + // For now, assume source and target are straight ids... + try { + + Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get())); + if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) { + throw new CrudException( + "Error creating edge - source vertex with id " + source + " does not exist in graph data base", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } + + Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get())); + if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) { + throw new CrudException( + "Error creating edge - target vertex with id " + target + " does not exist in graph data base", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } - if (logger.isDebugEnabled()) { - logger.debug("Get edge with id: " + id); - } + // Now, create the ChampRelationship object for our edge and store it in + // the graph database. + return edgeFromChampRelationship(champApi.storeRelationship( + new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build())); - try { + } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException + | ChampRelationshipNotExistsException | ChampUnmarshallingException | NumberFormatException + | ChampTransactionException e) { - // Request the edge from the graph db. - Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id)); + throw new CrudException("Error creating edge: " + e.getMessage(), + javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } - // Did we find it? - if (relationship.isPresent() && relationship.get().getType().equals(type)) { + @Override + public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException { - // Yup - return the result. - return edgeFromChampRelationship(relationship.get()); + filter.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), type); - } else { + Stream<ChampRelationship> retrievedRelationships; + try { + retrievedRelationships = champApi.queryRelationships(filter); - // We didn't find an edge with the supplied id, so throw an exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } + } catch (ChampTransactionException e) { + throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + + // Process the result stream from the Champ library into an Edge list, + // keeping only + // edges of the specified type. + List<Edge> edges = retrievedRelationships.map(r -> edgeFromChampRelationship(r)).collect(Collectors.toList()); + + return edges; + } + + @Override + public Edge updateEdge(Edge edge) throws CrudException { + + if (logger.isDebugEnabled()) { + logger.debug( + "Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties())); + } - } catch (ChampUnmarshallingException | ChampTransactionException e) { + try { + // Now, build the updated version of the Champ Relationship... + ChampRelationship updateRelationship = new ChampRelationship.Builder( + buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(), + edge.getSource().getProperties()), + buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(), + edge.getTarget().getProperties()), + edge.getType()).key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build(); + // ...and send it to the Champ library. + return edgeFromChampRelationship(champApi.replaceRelationship(updateRelationship)); + + } catch (ChampRelationshipNotExistsException ex) { + throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); + } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException + | ChampSchemaViolationException | ChampTransactionException ex) { + + throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public void deleteVertex(String id, String type) throws CrudException { + + try { + + // First, retrieve the vertex that we intend to delete. + Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id)); + + // Did we find it? + if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) { + throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } + + // Now, verify that there are no edges incident to the vertex (they must + // be deleted + // first if so). + Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get()); + + if (relationships.count() > 0) { + throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } + + // Finally, we can attempt to delete our vertex. + champApi.deleteObject(Long.parseLong(id)); + + } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException + | ChampTransactionException e) { + + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public void deleteEdge(String id, String type) throws CrudException { + + try { + + // First, retrieve the edge that we want to delete. + Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id)); + + // Did we find it? + if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) { + throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } + + // Now we can delete the edge. + champApi.deleteRelationship(relationshipToDelete.get()); + + } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException + | ChampTransactionException e) { + + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + /** + * This helper method generates a string representation of a properties map + * for logging purposes. + * + * @param properties + * - The properties map to be converted. + * @return - The log statement friendly conversion of the properties map. + */ + private String propertiesMapToString(Map<String, Object> properties) { + + StringBuilder sb = new StringBuilder(); + sb.append("{"); + + for (String key : properties.keySet()) { + sb.append("(").append(key).append(" -> ").append(properties.get(key)).append(") "); + } + + sb.append("}"); + + return sb.toString(); + } + + /** + * This helper method constructs a {@link ChampObject} suitable for passing to + * the Champ library. + * + * @param type + * - The type to assign to our ChampObject + * @param properties + * - The set of properties to assign to our ChampObject + * @return - A populated ChampObject + */ + private ChampObject buildChampObject(String type, Map<String, Object> properties) { + + ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withoutKey(); + + for (String key : properties.keySet()) { + objectInProgress.withProperty(key, properties.get(key)); + } + return objectInProgress.build(); + } + + /** + * This helper method constructs a {@link ChampObject} suitable for passing to + * the Champ library. + * + * @param id + * - Unique identifier for this object. + * @param type + * - The type to assign to our ChampObject + * @param properties + * - The set of properties to assign to our ChampObject + * @return - A populated ChampObject + */ + private ChampObject buildChampObject(String id, String type, Map<String, Object> properties) { + + ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withKey(Long.parseLong(id)); + + for (String key : properties.keySet()) { + objectInProgress.withProperty(key, properties.get(key)); + } + return objectInProgress.build(); + } + + private Vertex vertexFromChampObject(ChampObject champObject, String type) { + + // Get the identifier for this vertex from the Champ object. + Object id = champObject.getKey().orElse(""); + + // Start building our {@link Vertex} object. + Vertex.Builder vertexBuilder = new Vertex.Builder(type); + vertexBuilder.id(id.toString()); + + // Convert the properties associated with the Champ object into the form + // expected for + // a Vertex object. + for (String key : champObject.getProperties().keySet()) { + vertexBuilder.property(key, champObject.getProperties().get(key)); + } + + // ...and return it. + return vertexBuilder.build(); + } + + /** + * This helper method converts a {@link ChampRelationship} from the Champ + * library into an equivalent {@link Edge} object that is understood by the + * CRUD Service. + * + * @param relationship + * - The ChampRelationship object to be converted. + * @return - An Edge object corresponding to the supplied ChampRelationship + */ + private Edge edgeFromChampRelationship(ChampRelationship relationship) { + + // Populate the edge's id, if available. + Object relationshipId = relationship.getKey().orElse(""); + + Edge.Builder edgeBuilder = new Edge.Builder(relationship.getType()).id(relationshipId.toString()); + edgeBuilder.source(vertexFromChampObject(relationship.getSource(), + relationship.getSource().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null + ? relationship.getSource().getType() + : relationship.getSource().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString())); + edgeBuilder.target(vertexFromChampObject(relationship.getTarget(), + relationship.getTarget().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null + ? relationship.getTarget().getType() + : relationship.getTarget().getProperties() + .get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString())); + + for (String key : relationship.getProperties().keySet()) { + edgeBuilder.property(key, relationship.getProperties().get(key).toString()); + } + + return edgeBuilder.build(); + } + + /** + * Performs any necessary shut down operations when the DAO is no longer + * needed. + */ + public void close() { + + if (champApi != null) { + + logger.info(CrudServiceMsgs.STOPPING_CHAMP_DAO); + + champApi.shutdown(); + } + } + + @Override + public String openTransaction() { + + ChampTransaction transaction = champApi.openTransaction(); + + transactions.put(transaction.id(), transaction); + logger.info(CrudServiceMsgs.TRANSACTION, "Stored transaction " + transaction.id() + " in hashmap"); + logger.info(CrudServiceMsgs.TRANSACTION, "Hash map contents:"); + for (String key : transactions.keySet()) { + logger.info(CrudServiceMsgs.TRANSACTION, key); + } + return transaction.id(); + } + + @Override + public void commitTransaction(String id) throws CrudException { + + try { + champApi.commitTransaction(getTransaction(id)); + } catch (ChampTransactionException e) { + throw new CrudException("Error while commiting transaction " + id, + javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + transactions.remove(id); + } + + @Override + public void rollbackTransaction(String id) throws CrudException { + + try { + champApi.rollbackTransaction(getTransaction(id)); + } catch (ChampTransactionException e) { + throw new CrudException("Error while transaction rollback " + id, + javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + transactions.remove(id); + } + + private ChampTransaction getTransaction(String id) throws CrudException { + + logger.info(CrudServiceMsgs.TRANSACTION, "Looking up transaction " + id); + if (transactions.containsKey(id)) { + logger.info(CrudServiceMsgs.TRANSACTION, "Found it!"); + return (transactions.get(id)); + } else { + logger.info(CrudServiceMsgs.TRANSACTION, "Didn't find transaction id " + id + ". Hash map contains: "); + for (String key : transactions.keySet()) { + logger.info(CrudServiceMsgs.TRANSACTION, key); + } + throw new CrudException("No open transaction with id: " + id, javax.ws.rs.core.Response.Status.NOT_FOUND); + } + } + + @Override + public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException { + if (logger.isDebugEnabled()) { + logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties)); + } + + // Add the aai_node_type so that AAI can read the data created by gizmo + properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + + // Create an object to represent our vertex in the format expected by the + // Champ library. + ChampObject objectToCreate = buildChampObject(type, properties); + + try { + + // Ask the Champ library to store our vertex, placing the returned object + // into a + // list so that we can easily put that into our result object. + return vertexFromChampObject(champApi.storeObject(objectToCreate, Optional.of(getTransaction(txId))), type); + + } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException + | ChampTransactionException e) { + + // Something went wrong - throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId) + throws CrudException { + // For now, assume source and target are straight ids... + try { + + Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get()), + Optional.of(getTransaction(txId))); + if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) { + throw new CrudException( + "Error creating edge - source vertex with id " + source + " does not exist in graph data base", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } + + Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get()), + Optional.of(getTransaction(txId))); + if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) { + throw new CrudException( + "Error creating edge - target vertex with id " + target + " does not exist in graph data base", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } + + // Now, create the ChampRelationship object for our edge and store it in + // the graph database. + return edgeFromChampRelationship(champApi.storeRelationship( + new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build(), + Optional.of(getTransaction(txId)))); + + } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException + | ChampTransactionException | ChampRelationshipNotExistsException | ChampUnmarshallingException e) { + + throw new CrudException("Error creating edge: " + e.getMessage(), + javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + + } + + @Override + public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException { + if (logger.isDebugEnabled()) { + logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties)); + } + // Add the aai_node_type so that AAI can read the data created by gizmo + properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + + try { + // Now, build the updated version of the Champ Object... + ChampObject updateObject = buildChampObject(id, type, properties); + // ...and send it to the Champ library. + return vertexFromChampObject(champApi.replaceObject(updateObject, Optional.of(getTransaction(txId))), type); + + } catch (ChampObjectNotExistsException e) { + throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); + } catch (NumberFormatException | ChampMarshallingException | ChampTransactionException + | ChampSchemaViolationException e) { + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public boolean transactionExists(String id) throws CrudException { + return transactions.containsKey(id); + } + + @Override + public void deleteVertex(String id, String type, String txId) throws CrudException { + try { + + // First, retrieve the vertex that we intend to delete. + Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id), + Optional.of(getTransaction(txId))); + + // Did we find it? + if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) { + throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } + + // Now, verify that there are no edges incident to the vertex (they must + // be deleted + // first if so). + Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get(), + Optional.of(getTransaction(txId))); + + if (relationships.count() > 0) { + throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.", + javax.ws.rs.core.Response.Status.BAD_REQUEST); + } + + // Finally, we can attempt to delete our vertex. + champApi.deleteObject(Long.parseLong(id), Optional.of(getTransaction(txId))); + + } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException + | ChampTransactionException e) { + + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + + } + + @Override + public Edge updateEdge(Edge edge, String txId) throws CrudException { + if (logger.isDebugEnabled()) { + logger.debug( + "Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties())); + } + + try { + // Now, build the updated version of the Champ Relationship... + ChampRelationship updateRelationship = new ChampRelationship.Builder( + buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(), + edge.getSource().getProperties()), + buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(), + edge.getTarget().getProperties()), + edge.getType()).key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build(); + // ...and send it to the Champ library. + return edgeFromChampRelationship( + champApi.replaceRelationship(updateRelationship, Optional.of(getTransaction(txId)))); + + } catch (ChampRelationshipNotExistsException ex) { + throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); + } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException + | ChampSchemaViolationException | ChampTransactionException ex) { + + throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } + + @Override + public void deleteEdge(String id, String type, String txId) throws CrudException { + try { + + // First, retrieve the edge that we want to delete. + Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id), + Optional.of(getTransaction(txId))); + + // Did we find it? + if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) { + throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } + + // Now we can delete the edge. + champApi.deleteRelationship(relationshipToDelete.get(), Optional.of(getTransaction(txId))); + + } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException + | ChampTransactionException e) { + + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + + } + + @Override + public Edge getEdge(String id, String type, String txId) throws CrudException { + if (logger.isDebugEnabled()) { + logger.debug("Get edge with id: " + id); + } + + try { + + // Request the edge from the graph db. + Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id), + Optional.of(getTransaction(txId))); + + // Did we find it? + if (relationship.isPresent() && relationship.get().getType().equals(type)) { + + // Yup - return the result. + return edgeFromChampRelationship(relationship.get()); + + } else { + + // We didn't find an edge with the supplied id, so throw an exception. + throw new CrudException("No edge with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); + } - // Something went wrong, so throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException { - - // For now, assume source and target are straight ids... - try { - - Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get())); - if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) { - throw new CrudException("Error creating edge - source vertex with id " + source + " does not exist in graph data base", - javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get())); - if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) { - throw new CrudException("Error creating edge - target vertex with id " + target + " does not exist in graph data base", - javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - // Now, create the ChampRelationship object for our edge and store it in - // the graph database. - return edgeFromChampRelationship( - champApi.storeRelationship(new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build())); - - } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException | ChampRelationshipNotExistsException - | ChampUnmarshallingException | NumberFormatException | ChampTransactionException e) { - - throw new CrudException("Error creating edge: " + e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException { - - filter.put(ChampRelationship.ReservedPropertyKeys.CHAMP_RELATIONSHIP_TYPE.toString(), type); - - Stream<ChampRelationship> retrievedRelationships; - try { - retrievedRelationships = champApi.queryRelationships(filter); - - } catch (ChampTransactionException e) { - throw new CrudException("Transaction error occured", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - - // Process the result stream from the Champ library into an Edge list, keeping only - // edges of the specified type. - List<Edge> edges = retrievedRelationships.map(r -> edgeFromChampRelationship(r)).collect(Collectors.toList()); - - return edges; - } - - @Override - public Edge updateEdge(Edge edge) throws CrudException { - - if (logger.isDebugEnabled()) { - logger.debug("Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties())); - } - - try { - // Now, build the updated version of the Champ Relationship... - ChampRelationship updateRelationship = new ChampRelationship.Builder( - buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(), edge.getSource().getProperties()), - buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(), edge.getTarget().getProperties()), edge.getType()) - .key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build(); - // ...and send it to the Champ library. - return edgeFromChampRelationship(champApi.replaceRelationship(updateRelationship)); - - } catch (ChampRelationshipNotExistsException ex) { - throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); - } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException | ChampSchemaViolationException - | ChampTransactionException ex) { - - throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public void deleteVertex(String id, String type) throws CrudException { - - try { - - // First, retrieve the vertex that we intend to delete. - Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id)); - - // Did we find it? - if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) { - throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.", javax.ws.rs.core.Response.Status.NOT_FOUND); - } - - // Now, verify that there are no edges incident to the vertex (they must be deleted - // first if so). - Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get()); - - if (relationships.count() > 0) { - throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.", javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - // Finally, we can attempt to delete our vertex. - champApi.deleteObject(Long.parseLong(id)); - - } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException | ChampTransactionException e) { - - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public void deleteEdge(String id, String type) throws CrudException { - - try { - - // First, retrieve the edge that we want to delete. - Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id)); - - // Did we find it? - if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) { - throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist", javax.ws.rs.core.Response.Status.NOT_FOUND); - } - - // Now we can delete the edge. - champApi.deleteRelationship(relationshipToDelete.get()); - - } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException | ChampTransactionException e) { - - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - /** - * This helper method generates a string representation of a properties map for logging purposes. - * - * @param properties - * - The properties map to be converted. - * @return - The log statement friendly conversion of the properties map. - */ - private String propertiesMapToString(Map<String, Object> properties) { - - StringBuilder sb = new StringBuilder(); - sb.append("{"); - - for (String key : properties.keySet()) { - sb.append("(").append(key).append(" -> ").append(properties.get(key)).append(") "); - } - - sb.append("}"); - - return sb.toString(); - } - - /** - * This helper method constructs a {@link ChampObject} suitable for passing to the Champ library. - * - * @param type - * - The type to assign to our ChampObject - * @param properties - * - The set of properties to assign to our ChampObject - * @return - A populated ChampObject - */ - private ChampObject buildChampObject(String type, Map<String, Object> properties) { - - ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withoutKey(); - - for (String key : properties.keySet()) { - objectInProgress.withProperty(key, properties.get(key)); - } - return objectInProgress.build(); - } - - /** - * This helper method constructs a {@link ChampObject} suitable for passing to the Champ library. - * - * @param id - * - Unique identifier for this object. - * @param type - * - The type to assign to our ChampObject - * @param properties - * - The set of properties to assign to our ChampObject - * @return - A populated ChampObject - */ - private ChampObject buildChampObject(String id, String type, Map<String, Object> properties) { - - ObjectBuildOrPropertiesStep objectInProgress = ChampObject.create().ofType(type).withKey(Long.parseLong(id)); - - for (String key : properties.keySet()) { - objectInProgress.withProperty(key, properties.get(key)); - } - return objectInProgress.build(); - } - - private Vertex vertexFromChampObject(ChampObject champObject, String type) { - - // Get the identifier for this vertex from the Champ object. - Object id = champObject.getKey().orElse(""); - - // Start building our {@link Vertex} object. - Vertex.Builder vertexBuilder = new Vertex.Builder(type); - vertexBuilder.id(id.toString()); - - // Convert the properties associated with the Champ object into the form expected for - // a Vertex object. - for (String key : champObject.getProperties().keySet()) { - vertexBuilder.property(key, champObject.getProperties().get(key)); - } - - // ...and return it. - return vertexBuilder.build(); - } - - /** - * This helper method converts a {@link ChampRelationship} from the Champ library into an equivalent {@link Edge} - * object that is understood by the CRUD Service. - * - * @param relationship - * - The ChampRelationship object to be converted. - * @return - An Edge object corresponding to the supplied ChampRelationship - */ - private Edge edgeFromChampRelationship(ChampRelationship relationship) { - - // Populate the edge's id, if available. - Object relationshipId = relationship.getKey().orElse(""); - - Edge.Builder edgeBuilder = new Edge.Builder(relationship.getType()).id(relationshipId.toString()); - edgeBuilder.source(vertexFromChampObject(relationship.getSource(), - relationship.getSource().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null - ? relationship.getSource().getType() - : relationship.getSource().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString())); - edgeBuilder.target(vertexFromChampObject(relationship.getTarget(), - relationship.getTarget().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()) == null - ? relationship.getTarget().getType() - : relationship.getTarget().getProperties().get(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName()).toString())); - - for (String key : relationship.getProperties().keySet()) { - edgeBuilder.property(key, relationship.getProperties().get(key).toString()); - } - - return edgeBuilder.build(); - } - - /** - * Performs any necessary shut down operations when the DAO is no longer needed. - */ - public void close() { - - if (champApi != null) { - - logger.info(CrudServiceMsgs.STOPPING_CHAMP_DAO); - - champApi.shutdown(); - } - } - - @Override - public String openTransaction() { - - ChampTransaction transaction = champApi.openTransaction(); - - transactions.put(transaction.id(), transaction); - logger.info(CrudServiceMsgs.TRANSACTION, "Stored transaction " + transaction.id() + " in hashmap"); - logger.info(CrudServiceMsgs.TRANSACTION, "Hash map contents:"); - for (String key : transactions.keySet()) { - logger.info(CrudServiceMsgs.TRANSACTION, key); - } - return transaction.id(); - } - - @Override - public void commitTransaction(String id) throws CrudException { - - try { - champApi.commitTransaction(getTransaction(id)); - } catch (ChampTransactionException e) { - throw new CrudException("Error while commiting transaction " + id, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - transactions.remove(id); - } - - @Override - public void rollbackTransaction(String id) throws CrudException { - - try { - champApi.rollbackTransaction(getTransaction(id)); - } catch (ChampTransactionException e) { - throw new CrudException("Error while transaction rollback " + id, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - transactions.remove(id); - } - - private ChampTransaction getTransaction(String id) throws CrudException { - - logger.info(CrudServiceMsgs.TRANSACTION, "Looking up transaction " + id); - if (transactions.containsKey(id)) { - logger.info(CrudServiceMsgs.TRANSACTION, "Found it!"); - return (transactions.get(id)); - } else { - logger.info(CrudServiceMsgs.TRANSACTION, "Didn't find transaction id " + id + ". Hash map contains: "); - for (String key : transactions.keySet()) { - logger.info(CrudServiceMsgs.TRANSACTION, key); - } - throw new CrudException("No open transaction with id: " + id, javax.ws.rs.core.Response.Status.NOT_FOUND); - } - } - - @Override - public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Add/update vertex: {label: " + type + " properties:" + propertiesMapToString(properties)); - } - - // Add the aai_node_type so that AAI can read the data created by gizmo - properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - - // Create an object to represent our vertex in the format expected by the Champ library. - ChampObject objectToCreate = buildChampObject(type, properties); - - try { - - // Ask the Champ library to store our vertex, placing the returned object into a - // list so that we can easily put that into our result object. - return vertexFromChampObject(champApi.storeObject(objectToCreate, Optional.of(getTransaction(txId))), type); - - } catch (ChampMarshallingException | ChampSchemaViolationException | ChampObjectNotExistsException | ChampTransactionException e) { - - // Something went wrong - throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId) throws CrudException { - // For now, assume source and target are straight ids... - try { - - Optional<ChampObject> sourceObject = champApi.retrieveObject(Long.parseLong(source.getId().get()), Optional.of(getTransaction(txId))); - if (!sourceObject.isPresent() || !sourceObject.get().getType().equals(source.getType())) { - throw new CrudException("Error creating edge - source vertex with id " + source + " does not exist in graph data base", - javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - Optional<ChampObject> targetObject = champApi.retrieveObject(Long.parseLong(target.getId().get()), Optional.of(getTransaction(txId))); - if (!targetObject.isPresent() || !targetObject.get().getType().equals(target.getType())) { - throw new CrudException("Error creating edge - target vertex with id " + target + " does not exist in graph data base", - javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - // Now, create the ChampRelationship object for our edge and store it in - // the graph database. - return edgeFromChampRelationship( - champApi.storeRelationship(new ChampRelationship.Builder(sourceObject.get(), targetObject.get(), type).properties(properties).build(), - Optional.of(getTransaction(txId)))); - - } catch (ChampMarshallingException | ChampObjectNotExistsException | ChampSchemaViolationException | ChampTransactionException - | ChampRelationshipNotExistsException | ChampUnmarshallingException e) { - - throw new CrudException("Error creating edge: " + e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - - } - - @Override - public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Update vertex with id: " + id + " with properties: " + propertiesMapToString(properties)); - } - // Add the aai_node_type so that AAI can read the data created by gizmo - properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - - try { - // Now, build the updated version of the Champ Object... - ChampObject updateObject = buildChampObject(id, type, properties); - // ...and send it to the Champ library. - return vertexFromChampObject(champApi.replaceObject(updateObject, Optional.of(getTransaction(txId))), type); - - } catch (ChampObjectNotExistsException e) { - throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); - } catch (NumberFormatException | ChampMarshallingException | ChampTransactionException | ChampSchemaViolationException e) { - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public boolean transactionExists(String id) throws CrudException { - return transactions.containsKey(id); - } - - @Override - public void deleteVertex(String id, String type, String txId) throws CrudException { - try { - - // First, retrieve the vertex that we intend to delete. - Optional<ChampObject> retrievedVertex = champApi.retrieveObject(Long.parseLong(id), Optional.of(getTransaction(txId))); - - // Did we find it? - if (!retrievedVertex.isPresent() || !retrievedVertex.get().getType().equals(type)) { - throw new CrudException("Failed to delete vertex with id: " + id + " - vertex does not exist.", javax.ws.rs.core.Response.Status.NOT_FOUND); - } - - // Now, verify that there are no edges incident to the vertex (they must be deleted - // first if so). - Stream<ChampRelationship> relationships = champApi.retrieveRelationships(retrievedVertex.get(), Optional.of(getTransaction(txId))); - - if (relationships.count() > 0) { - throw new CrudException("Attempt to delete vertex with id " + id + " which has incident edges.", javax.ws.rs.core.Response.Status.BAD_REQUEST); - } - - // Finally, we can attempt to delete our vertex. - champApi.deleteObject(Long.parseLong(id), Optional.of(getTransaction(txId))); - - } catch (NumberFormatException | ChampUnmarshallingException | ChampObjectNotExistsException | ChampTransactionException e) { - - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - - } - - @Override - public Edge updateEdge(Edge edge, String txId) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Update edge with id: " + edge.getId() + " with properties: " + propertiesMapToString(edge.getProperties())); - } - - try { - // Now, build the updated version of the Champ Relationship... - ChampRelationship updateRelationship = new ChampRelationship.Builder( - buildChampObject(edge.getSource().getId().get(), edge.getSource().getType(), edge.getSource().getProperties()), - buildChampObject(edge.getTarget().getId().get(), edge.getTarget().getType(), edge.getTarget().getProperties()), edge.getType()) - .key(getRelKey(edge.getId().get())).properties(edge.getProperties()).build(); - // ...and send it to the Champ library. - return edgeFromChampRelationship(champApi.replaceRelationship(updateRelationship, Optional.of(getTransaction(txId)))); - - } catch (ChampRelationshipNotExistsException ex) { - throw new CrudException("Not Found", javax.ws.rs.core.Response.Status.NOT_FOUND); - } catch (NumberFormatException | ChampUnmarshallingException | ChampMarshallingException | ChampSchemaViolationException - | ChampTransactionException ex) { - - throw new CrudException(ex.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } - - @Override - public void deleteEdge(String id, String type, String txId) throws CrudException { - try { - - // First, retrieve the edge that we want to delete. - Optional<ChampRelationship> relationshipToDelete = champApi.retrieveRelationship(getRelKey(id), Optional.of(getTransaction(txId))); - - // Did we find it? - if (!relationshipToDelete.isPresent() || !relationshipToDelete.get().getType().equals(type)) { - throw new CrudException("Failed to delete edge with id: " + id + " - edge does not exist", javax.ws.rs.core.Response.Status.NOT_FOUND); - } - - // Now we can delete the edge. - champApi.deleteRelationship(relationshipToDelete.get(), Optional.of(getTransaction(txId))); - - } catch (ChampRelationshipNotExistsException | NumberFormatException | ChampUnmarshallingException | ChampTransactionException e) { - - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - - } - - @Override - public Edge getEdge(String id, String type, String txId) throws CrudException { - if (logger.isDebugEnabled()) { - logger.debug("Get edge with id: " + id); - } - - try { - - // Request the edge from the graph db. - Optional<ChampRelationship> relationship = champApi.retrieveRelationship(getRelKey(id), Optional.of(getTransaction(txId))); - - // Did we find it? - if (relationship.isPresent() && relationship.get().getType().equals(type)) { - - // Yup - return the result. - return edgeFromChampRelationship(relationship.get()); - - } else { - - // We didn't find an edge with the supplied id, so throw an exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); - } - - } catch (ChampUnmarshallingException | ChampTransactionException e) { - - // Something went wrong, so throw an exception. - throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); - } - } + } catch (ChampUnmarshallingException | ChampTransactionException e) { + + // Something went wrong, so throw an exception. + throw new CrudException(e.getMessage(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR); + } + } } diff --git a/src/main/java/org/openecomp/crud/dao/champion/ChampionDao.java b/src/main/java/org/openecomp/crud/dao/champion/ChampionDao.java index e1bda7a..48fa6cb 100644 --- a/src/main/java/org/openecomp/crud/dao/champion/ChampionDao.java +++ b/src/main/java/org/openecomp/crud/dao/champion/ChampionDao.java @@ -64,19 +64,17 @@ public class ChampionDao implements GraphDao { private Logger logger = LoggerFactory.getInstance().getLogger(ChampionDao.class.getName()); - // We use a custom vertex serializer for Champion because it expects "key" instead of "id" + // We use a custom vertex serializer for Champion because it expects "key" + // instead of "id" private static final Gson championGson = new GsonBuilder() .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()) .registerTypeAdapter(Vertex.class, new ChampionVertexSerializer()) - .registerTypeAdapter(Edge.class, new ChampionEdgeSerializer()) - .create(); + .registerTypeAdapter(Edge.class, new ChampionEdgeSerializer()).create(); public ChampionDao(String championUrl, String certPassword) { try { - client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT) - .validateServerHostname(false) - .validateServerCertChain(false) - .clientCertFile(CrudServiceConstants.CRD_CHAMPION_AUTH_FILE) + client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false) + .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMPION_AUTH_FILE) .clientCertPassword(Password.deobfuscate(certPassword)); baseUrl = championUrl; @@ -99,8 +97,10 @@ public class ChampionDao implements GraphDao { if (getResult.getResultCode() == 200) { return Vertex.fromJson(getResult.getResult()); } else { - // We didn't find a vertex with the supplied id, so just throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -117,13 +117,17 @@ public class ChampionDao implements GraphDao { Vertex vert = Vertex.fromJson(getResult.getResult()); if (!vert.getType().equalsIgnoreCase(type)) { - // We didn't find a vertex with the supplied type, so just throw an exception. - throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied type, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } return vert; } else { - // We didn't find a vertex with the supplied id, so just throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -137,10 +141,13 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>(){}.getType()); + return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() { + }.getType()); } else { - // We didn't find a vertex with the supplied id, so just throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -148,8 +155,8 @@ public class ChampionDao implements GraphDao { public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException { filter.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); - String url = baseUrl + "objects/filter" + "?" + - URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); + String url = baseUrl + "objects/filter" + "?" + + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); Map<String, List<String>> headers = new HashMap<>(); headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); @@ -158,10 +165,13 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return championGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>(){}.getType()); + return championGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() { + }.getType()); } else { - // We didn't find a vertex with the supplied id, so just throw an exception. - throw new CrudException("No vertices found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertices found in graph for given filters", + javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -178,20 +188,23 @@ public class ChampionDao implements GraphDao { Edge edge = Edge.fromJson(getResult.getResult()); if (!edge.getType().equalsIgnoreCase(type)) { - // We didn't find an edge with the supplied type, so just throw an exception. - throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find an edge with the supplied type, so just throw an + // exception. + throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } return edge; } else { - // We didn't find a edge with the supplied type, so just throw an exception. + // We didn't find a edge with the supplied type, so just throw an + // exception. throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } } @Override public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException { - String url = baseUrl + "relationships/filter" + "?" + - URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); + String url = baseUrl + "relationships/filter" + "?" + + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); Map<String, List<String>> headers = new HashMap<>(); headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); @@ -200,9 +213,11 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>(){}.getType()); + return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() { + }.getType()); } else { - // We didn't find a vertex with the supplied id, so just throw an exception. + // We didn't find a vertex with the supplied id, so just throw an + // exception. throw new CrudException("No edges found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -222,13 +237,14 @@ public class ChampionDao implements GraphDao { properties.forEach(insertVertexBuilder::property); Vertex insertVertex = insertVertexBuilder.build(); - OperationResult getResult = client.post(url, insertVertex.toJson(), headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.post(url, insertVertex.toJson(), headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { return Vertex.fromJson(getResult.getResult()); } else { - // We didn't create a vertex with the supplied type, so just throw an exception. + // We didn't create a vertex with the supplied type, so just throw an + // exception. throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -250,13 +266,14 @@ public class ChampionDao implements GraphDao { Vertex insertVertex = insertVertexBuilder.build(); String payload = insertVertex.toJson(championGson); - OperationResult getResult = client.put(url, payload, headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { return Vertex.fromJson(getResult.getResult()); } else { - // We didn't create a vertex with the supplied type, so just throw an exception. + // We didn't create a vertex with the supplied type, so just throw an + // exception. throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -271,7 +288,8 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) { - // We didn't delete a vertex with the supplied id, so just throw an exception. + // We didn't delete a vertex with the supplied id, so just throw an + // exception. throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -292,21 +310,21 @@ public class ChampionDao implements GraphDao { Edge insertEdge = insertEdgeBuilder.build(); String edgeJson = insertEdge.toJson(championGson); - OperationResult getResult = client.post(url, edgeJson, headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.post(url, edgeJson, headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { return Edge.fromJson(getResult.getResult()); } else { - // We didn't create an edge with the supplied type, so just throw an exception. + // We didn't create an edge with the supplied type, so just throw an + // exception. throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override public Edge updateEdge(Edge edge) throws CrudException { - if (!edge.getId().isPresent()) - { + if (!edge.getId().isPresent()) { throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST); } String url = baseUrl + "relationships/" + edge.getId().get(); @@ -315,13 +333,14 @@ public class ChampionDao implements GraphDao { headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString()))); String edgeJson = edge.toJson(championGson); - OperationResult getResult = client.put(url, edgeJson, headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.put(url, edgeJson, headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { return Edge.fromJson(getResult.getResult()); } else { - // We didn't create an edge with the supplied type, so just throw an exception. + // We didn't create an edge with the supplied type, so just throw an + // exception. throw new CrudException("Failed to update edge", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -336,7 +355,8 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() != 200) { - // We didn't find an edge with the supplied type, so just throw an exception. + // We didn't find an edge with the supplied type, so just throw an + // exception. throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -364,10 +384,12 @@ public class ChampionDao implements GraphDao { headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString()))); - OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", headers, MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE); + OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.TEXT_PLAIN_TYPE); if (getResult.getResultCode() != 200) { - throw new CrudException("Unable to commit transaction", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Unable to commit transaction", + Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -378,10 +400,12 @@ public class ChampionDao implements GraphDao { headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString()))); - OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", headers, MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE); + OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.TEXT_PLAIN_TYPE); if (getResult.getResultCode() != 200) { - throw new CrudException("Unable to rollback transaction", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Unable to rollback transaction", + Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -412,19 +436,21 @@ public class ChampionDao implements GraphDao { properties.forEach(insertVertexBuilder::property); Vertex insertVertex = insertVertexBuilder.build(); - OperationResult getResult = client.post(url, insertVertex.toJson(), headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.post(url, insertVertex.toJson(), headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { return Vertex.fromJson(getResult.getResult()); } else { - // We didn't create a vertex with the supplied type, so just throw an exception. + // We didn't create a vertex with the supplied type, so just throw an + // exception. throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId) throws CrudException { + public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId) + throws CrudException { String url = baseUrl + "relationships?transactionId=" + txId; Map<String, List<String>> headers = new HashMap<>(); headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); @@ -444,7 +470,8 @@ public class ChampionDao implements GraphDao { if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { return Edge.fromJson(getResult.getResult()); } else { - // We didn't create an edge with the supplied type, so just throw an exception. + // We didn't create an edge with the supplied type, so just throw an + // exception. throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -466,13 +493,14 @@ public class ChampionDao implements GraphDao { Vertex insertVertex = insertVertexBuilder.build(); String payload = insertVertex.toJson(championGson); - OperationResult getResult = client.put(url, payload, headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { return Vertex.fromJson(getResult.getResult()); } else { - // We didn't create a vertex with the supplied type, so just throw an exception. + // We didn't create a vertex with the supplied type, so just throw an + // exception. throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -487,15 +515,15 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) { - // We didn't delete a vertex with the supplied id, so just throw an exception. + // We didn't delete a vertex with the supplied id, so just throw an + // exception. throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override public Edge updateEdge(Edge edge, String txId) throws CrudException { - if (!edge.getId().isPresent()) - { + if (!edge.getId().isPresent()) { throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST); } String url = baseUrl + "relationships/" + edge.getId().get() + "?transactionId=" + txId; @@ -503,14 +531,16 @@ public class ChampionDao implements GraphDao { headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo")); headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString()))); - OperationResult getResult = client.put(url, edge.toJson(championGson), headers, - MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE); + OperationResult getResult = client.put(url, edge.toJson(championGson), headers, MediaType.APPLICATION_JSON_TYPE, + MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { return Edge.fromJson(getResult.getResult()); } else { - // We didn't create an edge with the supplied type, so just throw an exception. - throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); + // We didn't create an edge with the supplied type, so just throw an + // exception. + throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), + Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -524,7 +554,8 @@ public class ChampionDao implements GraphDao { OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() != 200) { - // We didn't find an edge with the supplied type, so just throw an exception. + // We didn't find an edge with the supplied type, so just throw an + // exception. throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -542,12 +573,15 @@ public class ChampionDao implements GraphDao { Edge edge = Edge.fromJson(getResult.getResult()); if (!edge.getType().equalsIgnoreCase(type)) { - // We didn't find an edge with the supplied type, so just throw an exception. - throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find an edge with the supplied type, so just throw an + // exception. + throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } return edge; } else { - // We didn't find an edge with the supplied id, so just throw an exception. + // We didn't find an edge with the supplied id, so just throw an + // exception. throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } } @@ -564,13 +598,17 @@ public class ChampionDao implements GraphDao { Vertex vert = Vertex.fromJson(getResult.getResult()); if (!vert.getType().equalsIgnoreCase(type)) { - // We didn't find a vertex with the supplied type, so just throw an exception. - throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied type, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } return vert; } else { - // We didn't find a vertex with the supplied id, so just throw an exception. - throw new CrudException("No vertex with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + // We didn't find a vertex with the supplied id, so just throw an + // exception. + throw new CrudException("No vertex with id " + id + " found in graph", + javax.ws.rs.core.Response.Status.NOT_FOUND); } } |