diff options
author | 2018-06-15 15:32:01 +0100 | |
---|---|---|
committer | 2018-06-18 11:33:18 +0100 | |
commit | b348af8ed2c4192f88169b37bf53fa25b8a7a681 (patch) | |
tree | 3ce66feb7909f645536a8d0eb5ad2a7879d3ab6c /src/main/java/org/onap/crud/dao | |
parent | 00832f054dd0c21492af531548e321ea25cdb8b4 (diff) |
ETags on resources
An etag should be generated (by Champ) when creating
a resource (edge or vertex). The Champ microservice should return
this etag in the response header. Gizmo should also return the etag
in it's response header (Gizmo will receive the etag from Champ).
Issue-ID: AAI-1196
Change-Id: Ie16f871eccbceeccde037e73e0de0d96eeba18bd
Signed-off-by: Sotiropoulos, Ioannis (is948x) <Ioannis.Sotiropoulos@amdocs.com>
Diffstat (limited to 'src/main/java/org/onap/crud/dao')
-rw-r--r-- | src/main/java/org/onap/crud/dao/GraphDao.java | 47 | ||||
-rw-r--r-- | src/main/java/org/onap/crud/dao/champ/ChampDao.java | 112 |
2 files changed, 77 insertions, 82 deletions
diff --git a/src/main/java/org/onap/crud/dao/GraphDao.java b/src/main/java/org/onap/crud/dao/GraphDao.java index 29ea6da..fe638ce 100644 --- a/src/main/java/org/onap/crud/dao/GraphDao.java +++ b/src/main/java/org/onap/crud/dao/GraphDao.java @@ -23,9 +23,8 @@ package org.onap.crud.dao; import java.util.HashSet; import java.util.List; import java.util.Map; - +import org.onap.aai.restclient.client.OperationResult; import org.onap.crud.entity.Edge; - import org.onap.crud.entity.Vertex; import org.onap.crud.exception.CrudException; @@ -33,7 +32,7 @@ public interface GraphDao { public Vertex getVertex(String id, String version) throws CrudException; - public Vertex getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException; + public OperationResult getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException; /** * Retrieve all of the edges which are incident to the vertex with the @@ -42,7 +41,7 @@ public interface GraphDao { * @param id * - The unique identifier of the vertex to retrieve the edges for. * @param queryParams - * - query parameters to be passed + * - query parameters to be passed * @return - A collection of edges. * @throws CrudException */ @@ -56,10 +55,10 @@ public interface GraphDao { * - The vertex type that we want to retrieve. * @param filter * - The parameters to filter our results by. - * @return - A collection of vertices. + * @return - The {@link OperationResult} OperationResult * @throws CrudException */ - public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException; + public OperationResult getVertices(String type, Map<String, Object> filter, String version) throws CrudException; /** * Retrieve a collection of {@link Vertex} objects which match the supplied @@ -71,10 +70,10 @@ public interface GraphDao { * - The parameters to filter our results by. * @param properties * - The properties to retrieve with the vertex - * @return - A collection of vertices. + * @return - The {@link OperationResult} OperationResult * @throws CrudException */ - public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException; + public OperationResult getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException; /** * Retrieve an {@link Edge} from the graph database by specifying its unique @@ -85,11 +84,11 @@ public interface GraphDao { * @param type * - The type that we want to retrieve. * @param queryParams - * - query parameters to be passed - * @return - The Edge corresponding to the specified identifier. + * - query parameters to be passed + * @return - The {@link OperationResult} OperationResult corresponding to the specified identifier. * @throws CrudException */ - public Edge getEdge(String id, String type, Map<String, String> queryParams) throws CrudException; + public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException; /** * Retrieve a collection of {@link Edge} objects with a given type and which @@ -99,10 +98,10 @@ public interface GraphDao { * - 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. + * @return - The {@link OperationResult} OperationResult * @throws CrudException */ - public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException; + public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException; /** * Insert a new {@link Vertex} into the graph data store. @@ -111,10 +110,10 @@ public interface GraphDao { * - 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. + * @return - The result of the Vertex creation. * @throws CrudException */ - public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException; + public OperationResult addVertex(String type, Map<String, Object> properties, String version) throws CrudException; /** * Updates an existing {@link Vertex}. @@ -123,10 +122,10 @@ public interface GraphDao { * - The unique identifier of the vertex to be updated. * @param properties * - The properties to associate with the vertex. - * @return - The udpated vertex. + * @return - The result of the update OperationResult. * @throws CrudException */ - public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException; + public OperationResult updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException; /** * Removes the specified vertex from the graph data base. @@ -151,22 +150,20 @@ public interface GraphDao { * - The target vertex for this edge. * @param properties * - The properties map to associate with this edge. - * @return - The {@link Edge} object that was created. + * @return - The {@link OperationResult} OperationResult containing the Edge that was created. * @throws CrudException */ - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException; + public OperationResult addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) 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. - * @return - The update edge. + * @param edge + * - The edge to be updated. + * @return - The result of the update OperationResult. * @throws CrudException */ - public Edge updateEdge(Edge edge) throws CrudException; + public OperationResult updateEdge(Edge edge) throws CrudException; /** * Remove the specified edge from the graph data base. diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index 344d797..c8488ba 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -20,39 +20,36 @@ */ package org.onap.crud.dao.champ; -import net.dongliu.gson.GsonJava8TypeAdapterFactory; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; - +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import org.eclipse.jetty.util.security.Password; -import org.onap.aai.cl.mdc.MdcContext; -import org.onap.aai.logging.LoggingContext; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.aai.cl.mdc.MdcContext; +import org.onap.aai.logging.LoggingContext; +import org.onap.aai.restclient.client.OperationResult; +import org.onap.aai.restclient.client.RestClient; +import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.onap.crud.dao.GraphDao; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; import org.onap.crud.exception.CrudException; import org.onap.crud.util.CrudServiceConstants; -import org.onap.aai.restclient.client.OperationResult; -import org.onap.aai.restclient.client.RestClient; -import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.slf4j.MDC; - -import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import net.dongliu.gson.GsonJava8TypeAdapterFactory; public class ChampDao implements GraphDao { protected RestClient client; @@ -75,7 +72,7 @@ public class ChampDao implements GraphDao { .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()) .registerTypeAdapter(Vertex.class, new ChampVertexSerializer()) .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create(); - + public ChampDao() { } @@ -112,12 +109,12 @@ public class ChampDao implements GraphDao { } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @Override - public Vertex getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException { + public OperationResult getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException { StringBuilder strBuild = new StringBuilder(baseObjectUrl); strBuild.append("/"); strBuild.append(id); @@ -138,11 +135,11 @@ public class ChampDao implements GraphDao { throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } - return vert; + return getResult; } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @@ -158,24 +155,24 @@ public class ChampDao implements GraphDao { } OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); - + if (getResult.getResultCode() == 200) { return champGson.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 createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @Override - public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException { + public OperationResult getVertices(String type, Map<String, Object> filter, String version) throws CrudException { return getVertices(type, filter, new HashSet<String>(), version); } @Override - public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException { + public OperationResult getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException { filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); List<NameValuePair> queryParams = convertToNameValuePair(filter); @@ -186,16 +183,16 @@ public class ChampDao implements GraphDao { OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return Vertex.collectionFromJson(getResult.getResult(), version); + return getResult; } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertices found in graph for given filters"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertices found in graph for given filters"); } } @Override - public Edge getEdge(String id, String type, Map<String, String> queryParams) throws CrudException { + public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException { StringBuilder strBuild = new StringBuilder(baseRelationshipUrl); strBuild.append("/"); strBuild.append(id); @@ -205,7 +202,7 @@ public class ChampDao implements GraphDao { strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); } OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); - + if (getResult.getResultCode() == 200) { Edge edge = Edge.fromJson(getResult.getResult()); @@ -215,33 +212,32 @@ public class ChampDao implements GraphDao { throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } - return edge; + return getResult; } else { // We didn't find a edge with the supplied type, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @Override - public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException { + public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException { String url = baseRelationshipUrl + "/filter" + "?" + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() { - }.getType()); + return getResult; } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edges found in graph for given filters"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edges found in graph for given filters"); } } @Override - public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException { + public OperationResult addVertex(String type, Map<String, Object> properties, String version) throws CrudException { String url = baseObjectUrl; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -256,7 +252,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Vertex.fromJson(getResult.getResult(), version); + return getResult; } else { // We didn't create a vertex with the supplied type, so just throw an // exception. @@ -265,7 +261,7 @@ public class ChampDao implements GraphDao { } @Override - public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException { + public OperationResult updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException { String url = baseObjectUrl + "/" + id; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -282,7 +278,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Vertex.fromJson(getResult.getResult(), version); + return getResult; } else { // We didn't create a vertex with the supplied type, so just throw an // exception. @@ -303,12 +299,14 @@ public class ChampDao implements GraphDao { } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException { + public OperationResult addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException { String url = baseRelationshipUrl; // Try requests to ensure source and target exist in Champ - Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, new HashMap<String, String>()); - Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap<String, String>()); + OperationResult dbSourceOpResult = getVertex(source.getId().get(), source.getType(), version, new HashMap<String, String>()); + Vertex dbSource = Vertex.fromJson(dbSourceOpResult.getResult(), version); + OperationResult dbTargetOpResult = getVertex(target.getId().get(), target.getType(), version, new HashMap<String, String>()); + Vertex dbTarget = Vertex.fromJson(dbTargetOpResult.getResult(), version); Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget); properties.forEach(insertEdgeBuilder::property); @@ -319,7 +317,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Edge.fromJson(getResult.getResult()); + return getResult; } else { // We didn't create an edge with the supplied type, so just throw an // exception. @@ -328,7 +326,7 @@ public class ChampDao implements GraphDao { } @Override - public Edge updateEdge(Edge edge) throws CrudException { + public OperationResult updateEdge(Edge edge) throws CrudException { if (!edge.getId().isPresent()) { throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST); } @@ -339,7 +337,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Edge.fromJson(getResult.getResult()); + return getResult; } else { // We didn't create an edge with the supplied type, so just throw an // exception. @@ -355,7 +353,7 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != 200) { // We didn't find an edge with the supplied type, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -524,7 +522,7 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != 200) { // We didn't find an edge with the supplied type, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -546,7 +544,7 @@ public class ChampDao implements GraphDao { } else { // We didn't find an edge with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -567,7 +565,7 @@ public class ChampDao implements GraphDao { } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @@ -588,15 +586,15 @@ public class ChampDao implements GraphDao { return nvpList; } - + private Map<String, List<String>> createHeader() { Map<String, List<String>> headers = new HashMap<>(); headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME)); headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID))); return headers; } - - private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg) + + private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg) { CrudException ce = null; if(result != null) |