diff options
Diffstat (limited to 'src/main/java/org/onap/crud/service')
7 files changed, 412 insertions, 279 deletions
diff --git a/src/main/java/org/onap/crud/service/AaiResourceService.java b/src/main/java/org/onap/crud/service/AaiResourceService.java index c9a5805..afabe7e 100644 --- a/src/main/java/org/onap/crud/service/AaiResourceService.java +++ b/src/main/java/org/onap/crud/service/AaiResourceService.java @@ -26,7 +26,6 @@ import java.util.HashSet; import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
-
import javax.security.auth.x500.X500Principal;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
@@ -37,20 +36,21 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
+import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.Status;
-
+import javax.ws.rs.core.UriInfo;
+import org.apache.commons.lang3.tuple.ImmutablePair;
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.serialization.db.EdgeProperty;
import org.onap.aai.serialization.db.EdgeRule;
import org.onap.aai.serialization.db.EdgeRules;
import org.onap.aai.serialization.db.EdgeType;
import org.onap.aaiauth.auth.Auth;
-import org.onap.aai.cl.api.Logger;
-import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.crud.exception.CrudException;
import org.onap.crud.logging.CrudServiceMsgs;
import org.onap.crud.logging.LoggingUtil;
@@ -59,7 +59,6 @@ import org.onap.crud.util.CrudServiceConstants; import org.onap.schema.EdgeRulesLoader;
import org.onap.schema.RelationshipSchemaValidator;
import org.slf4j.MDC;
-
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
@@ -74,78 +73,78 @@ public class AaiResourceService { private String mediaType = MediaType.APPLICATION_JSON;
public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
-
+
private Auth auth;
AbstractGraphDataService graphDataService;
Gson gson = new Gson();
-
+
private Logger logger = LoggerFactory.getInstance().getLogger(AaiResourceService.class.getName());
private Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(AaiResourceService.class.getName());
-
+
public AaiResourceService() {}
-
+
/**
* Creates a new instance of the AaiResourceService.
- *
+ *
* @param crudGraphDataService - Service used for interacting with the graph.
- *
+ *
* @throws Exception
*/
public AaiResourceService(AbstractGraphDataService graphDataService) throws Exception {
this.graphDataService = graphDataService;
this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
}
-
+
/**
* Perform any one-time initialization required when starting the service.
*/
public void startup() {
-
+
if(logger.isDebugEnabled()) {
logger.debug("AaiResourceService started!");
}
}
-
-
+
+
/**
* Creates a new relationship in the graph, automatically populating the edge
* properties based on the A&AI edge rules.
- *
+ *
* @param content - Json structure describing the relationship to create.
* @param type - Relationship type supplied as a URI parameter.
* @param uri - Http request uri
* @param headers - Http request headers
* @param uriInfo - Http URI info field
* @param req - Http request structure.
- *
+ *
* @return - Standard HTTP response.
*/
@POST
@Path("/relationships/{type}/")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
- public Response createRelationship(String content,
- @PathParam("type") String type,
+ public Response createRelationship(String content,
+ @PathParam("type") String type,
@PathParam("uri") @Encoded String uri,
- @Context HttpHeaders headers,
+ @Context HttpHeaders headers,
@Context UriInfo uriInfo,
@Context HttpServletRequest req) {
-
+
LoggingUtil.initMdcContext(req, headers);
if(logger.isDebugEnabled()) {
logger.debug("Incoming request..." + content);
}
-
+
Response response = null;
if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
-
+
try {
-
+
// Extract the edge payload from the request.
- EdgePayload payload = EdgePayload.fromJson(content);
-
+ EdgePayload payload = EdgePayload.fromJson(content);
+
// Do some basic validation on the payload.
if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -156,50 +155,50 @@ public class AaiResourceService { if (payload.getType() != null && !payload.getType().equals(type)) {
throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
}
-
+
// Apply the edge rules to our edge.
payload = applyEdgeRulesToPayload(payload);
-
+
if(logger.isDebugEnabled()) {
logger.debug("Creating AAI edge using version " + EdgeRulesLoader.getLatestSchemaVersion() );
}
-
+
// Now, create our edge in the graph store.
- String result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), type, payload);
- response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
-
+ ImmutablePair<EntityTag, String> result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), type, payload);
+ response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build();
+
} catch (CrudException e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
- }
+ }
}
-
+
LoggingUtil.logRestRequest(logger, auditLogger, req, response);
return response;
}
-
-
+
+
/**
* Creates a new relationship in the graph, automatically populating the edge
* properties based on the A&AI edge rules.
- *
+ *
* @param content - Json structure describing the relationship to create.
* @param uri - Http request uri
* @param headers - Http request headers
* @param uriInfo - Http URI info field
* @param req - Http request structure.
- *
+ *
* @return - Standard HTTP response.
- *
+ *
*/
@POST
@Path("/relationships/")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
- public Response createRelationship(String content,
- @PathParam("uri") @Encoded String uri,
+ public Response createRelationship(String content,
+ @PathParam("uri") @Encoded String uri,
@Context HttpHeaders headers,
- @Context UriInfo uriInfo,
+ @Context UriInfo uriInfo,
@Context HttpServletRequest req) {
LoggingUtil.initMdcContext(req, headers);
@@ -210,10 +209,10 @@ public class AaiResourceService { if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
try {
-
+
// Extract the edge payload from the request.
EdgePayload payload = EdgePayload.fromJson(content);
-
+
// Do some basic validation on the payload.
if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -224,14 +223,14 @@ public class AaiResourceService { if (payload.getType() == null || payload.getType().isEmpty()) {
throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
}
-
+
// Apply the edge rules to our edge.
payload = applyEdgeRulesToPayload(payload);
-
+
// Now, create our edge in the graph store.
- String result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), payload.getType(), payload);
- response = Response.status(Status.CREATED).entity(result).type(mediaType).build();
-
+ ImmutablePair<EntityTag, String> result = graphDataService.addEdge(EdgeRulesLoader.getLatestSchemaVersion(), payload.getType(), payload);
+ response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build();
+
} catch (CrudException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
} catch (Exception e) {
@@ -246,17 +245,17 @@ public class AaiResourceService { return response;
}
-
-
+
+
/**
* Upserts a relationship into the graph, automatically populating the edge properties
* based on the A&AI edge rules. The behaviour is as follows:
* <p>
- * <li>If no relationship with the supplied identifier already exists, then a new relationship
+ * <li>If no relationship with the supplied identifier already exists, then a new relationship
* is created with that id.<br>
- * <li>If a relationship with the supplied id DOES exist, then it is replaced with the supplied
+ * <li>If a relationship with the supplied id DOES exist, then it is replaced with the supplied
* content.
- *
+ *
* @param content - Json structure describing the relationship to create.
* @param type - Relationship type supplied as a URI parameter.
* @param id - Edge identifier.
@@ -264,19 +263,19 @@ public class AaiResourceService { * @param headers - Http request headers
* @param uriInfo - Http URI info field
* @param req - Http request structure.
- *
+ *
* @return - Standard HTTP response.
*/
@PUT
@Path("/relationships/{type}/{id}")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
- public Response upsertEdge(String content,
- @PathParam("type") String type,
+ public Response upsertEdge(String content,
+ @PathParam("type") String type,
@PathParam("id") String id,
- @PathParam("uri") @Encoded String uri,
+ @PathParam("uri") @Encoded String uri,
@Context HttpHeaders headers,
- @Context UriInfo uriInfo,
+ @Context UriInfo uriInfo,
@Context HttpServletRequest req) {
LoggingUtil.initMdcContext(req, headers);
@@ -284,12 +283,12 @@ public class AaiResourceService { Response response = null;
if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
-
+
try {
-
+
// Extract the edge payload from the request.
EdgePayload payload = EdgePayload.fromJson(content);
-
+
// Do some basic validation on the payload.
if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -297,47 +296,45 @@ public class AaiResourceService { if (payload.getId() != null && !payload.getId().equals(id)) {
throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
}
-
+
// Apply the edge rules to our edge.
payload = applyEdgeRulesToPayload(payload);
-
- String result;
+ ImmutablePair<EntityTag, String> result;
if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null &&
headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
result = graphDataService.patchEdge(EdgeRulesLoader.getLatestSchemaVersion(), id, type, payload);
+ response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build();
} else {
-
result = graphDataService.updateEdge(EdgeRulesLoader.getLatestSchemaVersion(), id, type, payload);
+ response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build();
}
- response = Response.status(Status.OK).entity(result).type(mediaType).build();
-
} catch (CrudException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
} catch (Exception e) {
response = Response.status(Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
-
+
} else {
-
+
response = Response.status(Status.FORBIDDEN).entity(content)
.type(MediaType.APPLICATION_JSON).build();
}
-
+
LoggingUtil.logRestRequest(logger, auditLogger, req, response);
return response;
}
-
-
+
+
/**
- * Retrieves the properties defined in the edge rules for a relationship between the
+ * Retrieves the properties defined in the edge rules for a relationship between the
* supplied vertex types.
- *
+ *
* @param sourceVertexType - Type of source vertex for the relationship.
* @param targetVertexType - Type of target vertex for the relationship.
- *
+ *
* @return - The defined properties for the relationship type.
- *
+ *
* @throws CrudException
*/
private Map<EdgeProperty, String> getEdgeRuleProperties(String sourceVertexType, String targetVertexType) throws CrudException {
@@ -345,119 +342,119 @@ public class AaiResourceService { if(logger.isDebugEnabled()) {
logger.debug("Lookup db edge rules for " + sourceVertexType + " -> " + targetVertexType);
}
-
+
EdgeRules rules = EdgeRules.getInstance();
EdgeRule rule;
try {
-
+
if(logger.isDebugEnabled()) {
logger.debug("Lookup by edge type TREE");
}
-
+
// We have no way of knowing in advance whether our relationship is considered to
// be a tree or cousing relationship, so try looking it up as a tree type first.
rule = rules.getEdgeRule(EdgeType.TREE, sourceVertexType, targetVertexType);
-
+
} catch (AAIException e) {
try {
-
+
if(logger.isDebugEnabled()) {
logger.debug("Lookup by edge type COUSIN");
}
-
+
// If we are here, then our lookup by 'tree' type failed, so try looking it up
// as a 'cousin' relationship.
rule = rules.getEdgeRule(EdgeType.COUSIN, sourceVertexType, targetVertexType);
-
+
} catch (AAIException e1) {
-
+
// If we're here then we failed to find edge rules for this relationship. Time to
// give up...
throw new CrudException("No edge rules for " + sourceVertexType + " -> " + targetVertexType, Status.NOT_FOUND);
}
} catch (Exception e) {
-
- throw new CrudException("General failure getting edge rule properties - " +
+
+ throw new CrudException("General failure getting edge rule properties - " +
e.getMessage(), Status.INTERNAL_SERVER_ERROR);
}
-
+
return rule.getEdgeProperties();
}
-
-
+
+
/**
* This method takes an inbound edge request payload, looks up the edge rules for the
* sort of relationship defined in the payload, and automatically applies the defined
* edge properties to it.
- *
+ *
* @param payload - The original edge request payload
- *
+ *
* @return - An updated edge request payload, with the properties defined in the edge
* rules automatically populated.
- *
+ *
* @throws CrudException
*/
public EdgePayload applyEdgeRulesToPayload(EdgePayload payload) throws CrudException {
-
+
// Extract the types for both the source and target vertices.
String srcType = RelationshipSchemaValidator.vertexTypeFromUri(payload.getSource());
String tgtType = RelationshipSchemaValidator.vertexTypeFromUri(payload.getTarget());
// Now, get the default properties for this edge based on the edge rules definition...
Map<EdgeProperty, String> props = getEdgeRuleProperties(srcType, tgtType);
-
+
// ...and merge them with any custom properties provided in the request.
JsonElement mergedProperties = mergeProperties(payload.getProperties(), props);
payload.setProperties(mergedProperties);
-
-
+
+
if(logger.isDebugEnabled()) {
logger.debug("Edge properties after applying rules for '" + srcType + " -> " + tgtType + "': " + mergedProperties);
}
-
+
return payload;
}
-
-
+
+
/**
* Given a set of edge properties extracted from an edge request payload and a set of properties
* taken from the db edge rules, this method merges them into one set of properties.
* <p>
* If the client has attempted to override the defined value for a property in the db edge rules
* then the request will be rejected as invalid.
- *
+ *
* @param propertiesFromRequest - Set of properties from the edge request.
* @param propertyDefaults - Set of properties from the db edge rules.
- *
+ *
* @return - A merged set of properties.
- *
+ *
* @throws CrudException
*/
public JsonElement mergeProperties(JsonElement propertiesFromRequest, Map<EdgeProperty, String> propertyDefaults) throws CrudException {
-
+
// Convert the properties from the edge payload into something we can
// manipulate.
Set<Map.Entry<String, JsonElement>> properties = new HashSet<Map.Entry<String, JsonElement>>();
properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet());
-
+
Set<String> propertyKeys = new HashSet<String>();
for(Map.Entry<String, JsonElement> property : properties) {
propertyKeys.add(property.getKey());
}
-
+
// Now, merge in the properties specified in the Db Edge Rules.
for(EdgeProperty defProperty : propertyDefaults.keySet()) {
-
+
// If the edge rules property was explicitly specified by the
// client then we will reject the request...
if(!propertyKeys.contains(defProperty.toString())) {
properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(defProperty.toString(),
- (JsonElement)(new JsonPrimitive(propertyDefaults.get(defProperty)))));
-
+ (new JsonPrimitive(propertyDefaults.get(defProperty)))));
+
} else {
- throw new CrudException("Property " + defProperty + " defined in db edge rules can not be overriden by the client.",
+ throw new CrudException("Property " + defProperty + " defined in db edge rules can not be overriden by the client.",
Status.BAD_REQUEST);
- }
+ }
}
Object[] propArray = properties.toArray();
@@ -465,7 +462,7 @@ public class AaiResourceService { sb.append("{");
boolean first=true;
for(int i=0; i<propArray.length; i++) {
-
+
Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) propArray[i];
if(!first) {
sb.append(",");
@@ -474,7 +471,7 @@ public class AaiResourceService { first=false;
}
sb.append("}");
-
+
// We're done. Return the result as a JsonElement.
return gson.fromJson(sb.toString(), JsonElement.class);
}
@@ -482,45 +479,45 @@ public class AaiResourceService { /**
* Invokes authentication validation on an incoming HTTP request.
- *
+ *
* @param req - The HTTP request.
* @param uri - HTTP URI
* @param content - Payload of the HTTP request.
* @param action - What HTTP action is being performed (GET/PUT/POST/PATCH/DELETE)
* @param authPolicyFunctionName - Policy function being invoked.
- *
+ *
* @return true - if the request passes validation,
* false - otherwise.
*/
- protected boolean validateRequest(HttpServletRequest req,
- String uri,
+ protected boolean validateRequest(HttpServletRequest req,
+ String uri,
String content,
- Action action,
+ Action action,
String authPolicyFunctionName) {
try {
String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
String authUser = null;
if (cipherSuite != null) {
-
+
X509Certificate[] certChain = (X509Certificate[]) req.getAttribute("javax.servlet.request.X509Certificate");
X509Certificate clientCert = certChain[0];
X500Principal subjectDn = clientCert.getSubjectX500Principal();
authUser = subjectDn.toString();
}
-
+
return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
-
+
} catch (Exception e) {
logResult(action, uri, e);
return false;
}
}
-
+
protected void logResult(Action op, String uri, Exception e) {
- logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL,
- op.toString(),
- uri,
+ logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL,
+ op.toString(),
+ uri,
e.getStackTrace().toString());
// Clear the MDC context so that no other transaction inadvertently
diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java index 9c7e0d4..7c1168e 100644 --- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java +++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java @@ -25,11 +25,14 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; - +import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response.Status; - +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.onap.aai.restclient.client.OperationResult; import org.onap.crud.dao.GraphDao; +import org.onap.crud.dao.champ.ChampEdgeSerializer; +import org.onap.crud.dao.champ.ChampVertexSerializer; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; import org.onap.crud.exception.CrudException; @@ -37,50 +40,65 @@ import org.onap.crud.parser.CrudResponseBuilder; import org.onap.crud.util.CrudServiceUtil; import org.onap.schema.OxmModelValidator; import org.onap.schema.RelationshipSchemaValidator; - +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; +import net.dongliu.gson.GsonJava8TypeAdapterFactory; public abstract class AbstractGraphDataService { protected GraphDao daoForGet; protected GraphDao dao; - + public AbstractGraphDataService() throws CrudException { CrudServiceUtil.loadModels(); } - public String getEdge(String version, String id, String type, Map<String, String> queryParams) throws CrudException { + public ImmutablePair<EntityTag, String> getEdge(String version, String id, String type, Map<String, String> queryParams) throws CrudException { RelationshipSchemaValidator.validateType(version, type); - Edge edge = daoForGet.getEdge(id, type, queryParams); - - return CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version); + OperationResult operationResult = daoForGet.getEdge(id, type, queryParams); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders()); + Edge edge = Edge.fromJson(operationResult.getResult()); + return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, edge), version)); } - - public String getEdges(String version, String type, Map<String, String> filter) throws CrudException { + + public ImmutablePair<EntityTag, String> getEdges(String version, String type, Map<String, String> filter) throws CrudException { + Gson champGson = new GsonBuilder() + .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()) + .registerTypeAdapter(Vertex.class, new ChampVertexSerializer()) + .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create(); RelationshipSchemaValidator.validateType(version, type); - List<Edge> items = daoForGet.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter)); - return CrudResponseBuilder.buildGetEdgesResponse(items, version); + OperationResult operationResult = daoForGet.getEdges(type, RelationshipSchemaValidator.resolveCollectionfilter(version, type, filter)); + List<Edge> items = champGson.fromJson(operationResult.getResult(), new TypeToken<List<Edge>>() { + }.getType()); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders()); + return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetEdgesResponse(items, version)); } - - public String getVertex(String version, String id, String type, Map<String, String> queryParams) throws CrudException { + + public ImmutablePair<EntityTag, String> getVertex(String version, String id, String type, Map<String, String> queryParams) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); - Vertex vertex = daoForGet.getVertex(id, type, version, queryParams); + OperationResult vertexOpResult = daoForGet.getVertex(id, type, version, queryParams); + Vertex vertex = Vertex.fromJson(vertexOpResult.getResult(), version); List<Edge> edges = daoForGet.getVertexEdges(id, queryParams); - return CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges, - version); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(vertexOpResult.getHeaders()); + return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVertexResponse(OxmModelValidator.validateOutgoingPayload(version, vertex), edges, + version)); } - public String getVertices(String version, String type, Map<String, String> filter, HashSet<String> properties) throws CrudException { + public ImmutablePair<EntityTag, String> getVertices(String version, String type, Map<String, String> filter, HashSet<String> properties) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); - List<Vertex> items = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties, version); - return CrudResponseBuilder.buildGetVerticesResponse(items, version); + OperationResult operationResult = daoForGet.getVertices(type, OxmModelValidator.resolveCollectionfilter(version, type, filter), properties, version); + List<Vertex> vertices = Vertex.collectionFromJson(operationResult.getResult(), version); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(operationResult.getHeaders()); + return new ImmutablePair<>(entityTag, CrudResponseBuilder.buildGetVerticesResponse(vertices, version)); } - + public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException { - HashMap<String, Vertex> vertices = new HashMap<String, Vertex>(); - HashMap<String, Edge> edges = new HashMap<String, Edge>(); - - String txId = dao.openTransaction(); - + HashMap<String, Vertex> vertices = new HashMap<>(); + HashMap<String, Edge> edges = new HashMap<>(); + + String txId = dao.openTransaction(); + try { // Step 1. Handle edge deletes (must happen before vertex deletes) for (JsonElement v : payload.getRelationships()) { @@ -98,8 +116,8 @@ public abstract class AbstractGraphDataService { RelationshipSchemaValidator.validateType(version, edgePayload.getType()); deleteBulkEdge(edgePayload.getId(), version, edgePayload.getType(), txId); } - } - + } + // Step 2: Handle vertex deletes for (JsonElement v : payload.getObjects()) { List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>( @@ -118,7 +136,7 @@ public abstract class AbstractGraphDataService { deleteBulkVertex(vertexPayload.getId(), version, type, txId); } } - + // Step 3: Handle vertex add/modify (must happen before edge adds) for (JsonElement v : payload.getObjects()) { List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>( @@ -130,21 +148,21 @@ public abstract class AbstractGraphDataService { Map.Entry<String, JsonElement> opr = entries.get(0); Map.Entry<String, JsonElement> item = entries.get(1); VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString()); - + // Add vertex if (opr.getValue().getAsString().equalsIgnoreCase("add")) { - vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), - headers, true)); + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + headers, true)); Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), vertexPayload.getProperties()); Vertex persistedVertex = addBulkVertex(validatedVertex, version, txId); Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex); vertices.put(item.getKey(), outgoingVertex); } - - // Update vertex + + // Update vertex else if (opr.getValue().getAsString().equalsIgnoreCase("modify")) { - vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), headers, false)); Vertex validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, vertexPayload.getType(), vertexPayload.getProperties()); @@ -152,18 +170,19 @@ public abstract class AbstractGraphDataService { Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex); vertices.put(item.getKey(), outgoingVertex); } - - // Patch vertex + + // Patch vertex else if (opr.getValue().getAsString().equalsIgnoreCase("patch")) { if ( (vertexPayload.getId() == null) || (vertexPayload.getType() == null) ) { throw new CrudException("id and type must be specified for patch request", Status.BAD_REQUEST); } - - vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), headers, false)); - - Vertex existingVertex = dao.getVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), version, new HashMap<String, String>()); - Vertex validatedVertex = OxmModelValidator.validateIncomingPatchPayload(vertexPayload.getId(), + + OperationResult existingVertexOpResult = dao.getVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), version, new HashMap<String, String>()); + Vertex existingVertex = Vertex.fromJson(existingVertexOpResult.getResult(), version); + Vertex validatedVertex = OxmModelValidator.validateIncomingPatchPayload(vertexPayload.getId(), version, vertexPayload.getType(), vertexPayload.getProperties(), existingVertex); Vertex persistedVertex = updateBulkVertex(validatedVertex, vertexPayload.getId(), version, txId); Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex); @@ -171,7 +190,7 @@ public abstract class AbstractGraphDataService { } } - // Step 4: Handle edge add/modify + // Step 4: Handle edge add/modify for (JsonElement v : payload.getRelationships()) { List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>( v.getAsJsonObject().entrySet()); @@ -185,7 +204,7 @@ public abstract class AbstractGraphDataService { // Add/Update edge if (opr.getValue().getAsString().equalsIgnoreCase("add") - || opr.getValue().getAsString().equalsIgnoreCase("modify") + || opr.getValue().getAsString().equalsIgnoreCase("modify") || opr.getValue().getAsString().equalsIgnoreCase("patch")) { Edge validatedEdge; Edge persistedEdge; @@ -224,13 +243,13 @@ public abstract class AbstractGraphDataService { Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(existingEdge, version, edgePayload); persistedEdge = updateBulkEdge(patchedEdge, version, txId); } - + Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge); edges.put(item.getKey(), outgoingEdge); - } - } - + } + } + // commit transaction dao.commitTransaction(txId); } catch (CrudException ex) { @@ -244,26 +263,32 @@ public abstract class AbstractGraphDataService { dao.rollbackTransaction(txId); } } - + return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload); } - public abstract String addVertex(String version, String type, VertexPayload payload) throws CrudException; - public abstract String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException; - public abstract String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException; + public abstract ImmutablePair<EntityTag, String> addVertex(String version, String type, VertexPayload payload) + throws CrudException; + public abstract ImmutablePair<EntityTag, String> updateVertex(String version, String id, String type, + VertexPayload payload) throws CrudException; + public abstract ImmutablePair<EntityTag, String> patchVertex(String version, String id, String type, + VertexPayload payload) throws CrudException; public abstract String deleteVertex(String version, String id, String type) throws CrudException; - public abstract String addEdge(String version, String type, EdgePayload payload) throws CrudException; + public abstract ImmutablePair<EntityTag, String> addEdge(String version, String type, EdgePayload payload) + throws CrudException; public abstract String deleteEdge(String version, String id, String type) throws CrudException; - public abstract String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException; - public abstract String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException; - + public abstract ImmutablePair<EntityTag, String> updateEdge(String version, String id, String type, + EdgePayload payload) throws CrudException; + public abstract ImmutablePair<EntityTag, String> patchEdge(String version, String id, String type, + EdgePayload payload) throws CrudException; + protected abstract Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException; protected abstract Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException; protected abstract void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException; - + protected abstract Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException; protected abstract Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException; protected abstract void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException; - + } diff --git a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java index 5d37acb..6b447a1 100644 --- a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java @@ -20,6 +20,8 @@ */ package org.onap.crud.service; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Timer; @@ -32,7 +34,9 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.annotation.PreDestroy; +import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.Response.Status; +import org.apache.commons.lang3.tuple.ImmutablePair; import org.onap.aai.cl.api.LogFields; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; @@ -40,6 +44,7 @@ import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.cl.mdc.MdcOverride; import org.onap.aai.event.api.EventConsumer; import org.onap.aai.event.api.EventPublisher; +import org.onap.aai.restclient.client.OperationResult; import org.onap.crud.dao.GraphDao; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; @@ -53,6 +58,7 @@ import org.onap.crud.exception.CrudException; import org.onap.crud.logging.CrudServiceMsgs; import org.onap.crud.util.CrudProperties; import org.onap.crud.util.CrudServiceConstants; +import org.onap.crud.util.etag.EtagGenerator; import org.onap.schema.OxmModelValidator; import org.onap.schema.RelationshipSchemaValidator; @@ -71,6 +77,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { private static Logger metricsLogger = LoggerFactory.getInstance().getMetricsLogger(CrudAsyncGraphDataService.class.getName()); private static LogFields okFields = new LogFields(); + private EtagGenerator etagGenerator; static { okFields.setField(Status.OK, Status.OK.toString()); @@ -83,12 +90,12 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } public CrudAsyncGraphDataService(GraphDao dao, EventPublisher asyncRequestPublisher, - EventConsumer asyncResponseConsumer) throws CrudException { + EventConsumer asyncResponseConsumer) throws CrudException, NoSuchAlgorithmException { this(dao, dao, asyncRequestPublisher, asyncResponseConsumer); } public CrudAsyncGraphDataService(GraphDao dao, GraphDao daoForGet, EventPublisher asyncRequestPublisher, - EventConsumer asyncResponseConsumer) throws CrudException { + EventConsumer asyncResponseConsumer) throws CrudException, NoSuchAlgorithmException { super(); this.dao = dao; @@ -116,6 +123,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { timer.schedule(crudAsyncResponseConsumer, responsePollInterval, responsePollInterval); this.asyncRequestPublisher = asyncRequestPublisher; + this.etagGenerator = new EtagGenerator(); logger.info(CrudServiceMsgs.ASYNC_DATA_SERVICE_INFO, "CrudAsyncGraphDataService initialized SUCCESSFULLY!"); } @@ -199,7 +207,8 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } @Override - public String addVertex(String version, String type, VertexPayload payload) throws CrudException { + public ImmutablePair<EntityTag, String> addVertex(String version, String type, VertexPayload payload) + throws CrudException { // Validate the incoming payload Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, payload.getProperties()); vertex.getProperties().put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); @@ -208,41 +217,81 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { .vertex(GraphEventVertex.fromVertex(vertex, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleVertexResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForVertex(response.getBody().getVertex())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleVertexResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @Override - public String addEdge(String version, String type, EdgePayload payload) throws CrudException { + public ImmutablePair<EntityTag, String> addEdge(String version, String type, EdgePayload payload) + throws CrudException { Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload); // Create graph request event GraphEvent event = GraphEvent.builder(GraphEventOperation.CREATE).edge(GraphEventEdge.fromEdge(edge, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleEdgeResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForEdge(response.getBody().getEdge())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleEdgeResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @Override - public String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException { + public ImmutablePair<EntityTag, String> updateVertex(String version, String id, String type, VertexPayload payload) + throws CrudException { Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, payload.getProperties()); GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE) .vertex(GraphEventVertex.fromVertex(vertex, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleVertexResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForVertex(response.getBody().getVertex())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleVertexResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @Override - public String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException { - Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, + public ImmutablePair<EntityTag, String> patchVertex(String version, String id, String type, VertexPayload payload) + throws CrudException { + OperationResult existingVertexOpResult = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>()); + Vertex existingVertex = Vertex.fromJson(existingVertexOpResult.getResult(), version); Vertex patchedVertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(), existingVertex); GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE) .vertex(GraphEventVertex.fromVertex(patchedVertex, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleVertexResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForVertex(response.getBody().getVertex())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleVertexResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @Override @@ -266,25 +315,47 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } @Override - public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException { - Edge edge = dao.getEdge(id, type, new HashMap<String, String>()); + public ImmutablePair<EntityTag, String> updateEdge(String version, String id, String type, EdgePayload payload) + throws CrudException { + OperationResult operationResult = dao.getEdge(id, type, new HashMap<String, String>()); + Edge edge = Edge.fromJson(operationResult.getResult()); Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload); GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE) .edge(GraphEventEdge.fromEdge(validatedEdge, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleEdgeResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForEdge(response.getBody().getEdge())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleEdgeResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @Override - public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException { - Edge edge = dao.getEdge(id, type, new HashMap<String, String>()); + public ImmutablePair<EntityTag, String> patchEdge(String version, String id, String type, EdgePayload payload) + throws CrudException { + OperationResult operationResult = dao.getEdge(id, type, new HashMap<String, String>()); + Edge edge = Edge.fromJson(operationResult.getResult()); Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload); GraphEvent event = GraphEvent.builder(GraphEventOperation.UPDATE) .edge(GraphEventEdge.fromEdge(patchedEdge, version)).build(); GraphEventEnvelope response = sendAndWait(event); - return responseHandler.handleEdgeResponse(version, event, response); + + EntityTag entityTag; + try { + entityTag = new EntityTag(etagGenerator.computeHashForEdge(response.getBody().getEdge())); + } catch (IOException e) { + throw new CrudException(e); + } + String responsePayload = responseHandler.handleEdgeResponse(version, event, response); + + return new ImmutablePair<EntityTag, String>(entityTag, responsePayload); } @PreDestroy @@ -349,4 +420,4 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { responseHandler.handleBulkEventResponse(event, response); return response.getBody(); } -} +}
\ No newline at end of file diff --git a/src/main/java/org/onap/crud/service/CrudGraphDataService.java b/src/main/java/org/onap/crud/service/CrudGraphDataService.java index 5a2710d..5b1c2dd 100644 --- a/src/main/java/org/onap/crud/service/CrudGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudGraphDataService.java @@ -22,20 +22,22 @@ package org.onap.crud.service; import java.util.HashMap; - +import javax.ws.rs.core.EntityTag; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.onap.aai.restclient.client.OperationResult; 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.parser.CrudResponseBuilder; +import org.onap.crud.util.CrudServiceUtil; import org.onap.schema.OxmModelValidator; import org.onap.schema.RelationshipSchemaValidator; public class CrudGraphDataService extends AbstractGraphDataService { - - + + public CrudGraphDataService(GraphDao dao) throws CrudException { super(); this.dao = dao; @@ -48,103 +50,146 @@ public class CrudGraphDataService extends AbstractGraphDataService { this.daoForGet = daoForGet; } - public String addVertex(String version, String type, VertexPayload payload) throws CrudException { + @Override + public ImmutablePair<EntityTag, String> addVertex(String version, String type, VertexPayload payload) + throws CrudException { Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type, payload.getProperties()); return addVertex(version, vertex); } - private String addVertex(String version, Vertex vertex) throws CrudException { - Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties(), version); - return CrudResponseBuilder + private ImmutablePair<EntityTag, String> addVertex(String version, Vertex vertex) throws CrudException { + OperationResult addedVertexResult = dao.addVertex(vertex.getType(), vertex.getProperties(), version); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(addedVertexResult.getHeaders()); + Vertex addedVertex = Vertex.fromJson(addedVertexResult.getResult(), version); + String payload = CrudResponseBuilder .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, addedVertex), version); + + return new ImmutablePair<EntityTag, String>(entityTag, payload); } - public String addEdge(String version, String type, EdgePayload payload) throws CrudException { + @Override + public ImmutablePair<EntityTag, String> addEdge(String version, String type, EdgePayload payload) + throws CrudException { Edge edge = RelationshipSchemaValidator.validateIncomingAddPayload(version, type, payload); return addEdge(version, edge); } - private String addEdge(String version, Edge edge) throws CrudException { - Edge addedEdge = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version); - return CrudResponseBuilder - .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version); + private ImmutablePair<EntityTag, String> addEdge(String version, Edge edge) throws CrudException { + OperationResult addedEdgeResult = dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(addedEdgeResult.getHeaders()); + Edge addedEdge = Edge.fromJson(addedEdgeResult.getResult()); + String payload = CrudResponseBuilder + .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, addedEdge), version); + + return new ImmutablePair<EntityTag, String>(entityTag, payload); } - public String updateVertex(String version, String id, String type, VertexPayload payload) throws CrudException { + @Override + public ImmutablePair<EntityTag, String> updateVertex(String version, String id, String type, VertexPayload payload) + throws CrudException { Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(id, version, type, payload.getProperties()); return updateVertex(version, vertex); + } + private ImmutablePair<EntityTag, String> updateVertex(String version, Vertex vertex) throws CrudException { + OperationResult updatedVertexResult = dao.updateVertex(vertex.getId().get(), vertex.getType(), vertex.getProperties(), version); + String payload = getUpdatedVertexPayload(version, updatedVertexResult); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(updatedVertexResult.getHeaders()); + + return new ImmutablePair<EntityTag, String>(entityTag, payload); } - private String updateVertex(String version, Vertex vertex) throws CrudException { - Vertex updatedVertex = dao.updateVertex(vertex.getId().get(), vertex.getType(), vertex.getProperties(), version); + private String getUpdatedVertexPayload(String version, OperationResult updatedVertexResult) throws CrudException { + Vertex updatedVertex = Vertex.fromJson(updatedVertexResult.getResult(), version); + return CrudResponseBuilder - .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, updatedVertex), version); + .buildUpsertVertexResponse(OxmModelValidator.validateOutgoingPayload(version, updatedVertex), version); } - public String patchVertex(String version, String id, String type, VertexPayload payload) throws CrudException { - Vertex existingVertex = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>()); + @Override + public ImmutablePair<EntityTag, String> patchVertex(String version, String id, String type, VertexPayload payload) + throws CrudException { + OperationResult existingVertexOpResult = dao.getVertex(id, OxmModelValidator.resolveCollectionType(version, type), version, new HashMap<String, String>()); + Vertex existingVertex = Vertex.fromJson(existingVertexOpResult.getResult(), version); Vertex vertex = OxmModelValidator.validateIncomingPatchPayload(id, version, type, payload.getProperties(), - existingVertex); + existingVertex); return updateVertex(version, vertex); } + @Override public String deleteVertex(String version, String id, String type) throws CrudException { type = OxmModelValidator.resolveCollectionType(version, type); dao.deleteVertex(id, type); return ""; } + @Override public String deleteEdge(String version, String id, String type) throws CrudException { RelationshipSchemaValidator.validateType(version, type); dao.deleteEdge(id, type); return ""; } - public String updateEdge(String version, String id, String type, EdgePayload payload) throws CrudException { - Edge edge = dao.getEdge(id, type, new HashMap<String, String>()); - Edge validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, payload); + @Override + public ImmutablePair<EntityTag, String> updateEdge(String version, String id, String type, EdgePayload payload) + throws CrudException { + Edge validatedEdge = getValidatedEdge(version, id, type, payload); return updateEdge(version, validatedEdge); } - private String updateEdge(String version, Edge edge) throws CrudException { - Edge updatedEdge = dao.updateEdge(edge); + private ImmutablePair<EntityTag, String> updateEdge(String version, Edge edge) throws CrudException { + OperationResult updatedEdgeResult = dao.updateEdge(edge); + String payload = getUpdatedEdgePayload(version, updatedEdgeResult); + EntityTag entityTag = CrudServiceUtil.getETagFromHeader(updatedEdgeResult.getHeaders()); + + return new ImmutablePair<EntityTag, String>(entityTag, payload); + } + + private String getUpdatedEdgePayload(String version, OperationResult updatedEdgeResult) throws CrudException { + Edge updatedEdge = Edge.fromJson(updatedEdgeResult.getResult()); + return CrudResponseBuilder - .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version); + .buildUpsertEdgeResponse(RelationshipSchemaValidator.validateOutgoingPayload(version, updatedEdge), version); + } + + private Edge getValidatedEdge(String version, String id, String type, EdgePayload payload) throws CrudException { + OperationResult operationResult = dao.getEdge(id, type, new HashMap<String, String>()); + return RelationshipSchemaValidator.validateIncomingUpdatePayload(Edge.fromJson(operationResult.getResult()), version, payload); } - - public String patchEdge(String version, String id, String type, EdgePayload payload) throws CrudException { - Edge edge = dao.getEdge(id, type, new HashMap<String, String>()); - Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(edge, version, payload); - return updateEdge(version, patchedEdge); + @Override + public ImmutablePair<EntityTag, String> patchEdge(String version, String id, String type, EdgePayload payload) + throws CrudException { + OperationResult operationResult = dao.getEdge(id, type, new HashMap<String, String>()); + Edge patchedEdge = RelationshipSchemaValidator.validateIncomingPatchPayload(Edge.fromJson(operationResult.getResult()), version, payload); + return updateEdge(version, patchedEdge); } @Override protected Vertex addBulkVertex(Vertex vertex, String version, String dbTransId) throws CrudException { return dao.addVertex(vertex.getType(), vertex.getProperties(), version, dbTransId); } - + @Override protected Vertex updateBulkVertex(Vertex vertex, String id, String version, String dbTransId) throws CrudException { return dao.updateVertex(id, vertex.getType(), vertex.getProperties(), version, dbTransId); } - + @Override protected void deleteBulkVertex(String id, String version, String type, String dbTransId) throws CrudException { dao.deleteVertex(id, type, dbTransId); } - + @Override protected Edge addBulkEdge(Edge edge, String version, String dbTransId) throws CrudException { return dao.addEdge(edge.getType(), edge.getSource(), edge.getTarget(), edge.getProperties(), version, dbTransId); } - + @Override protected Edge updateBulkEdge(Edge edge, String version, String dbTransId) throws CrudException { return dao.updateEdge(edge, dbTransId); } - + @Override protected void deleteBulkEdge(String id, String version, String type, String dbTransId) throws CrudException { dao.deleteEdge(id, type, dbTransId); diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index 2cbb87c..583fee6 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -38,11 +38,13 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.Context; +import javax.ws.rs.core.EntityTag; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; +import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.cxf.jaxrs.ext.PATCH; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; @@ -102,8 +104,8 @@ public class CrudRestService { try { if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String result = graphDataService.getVertex(version, id, type, params); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.getVertex(version, id, type, params); + response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -142,8 +144,8 @@ public class CrudRestService { properties = new HashSet<>(); } - String result = graphDataService.getVertices(version, type, filter, properties); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.getVertices(version, type, filter, properties); + response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -175,8 +177,8 @@ public class CrudRestService { try { if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String result = graphDataService.getEdge(version, id, type, params); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.getEdge(version, id, type, params); + response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -206,8 +208,8 @@ public class CrudRestService { try { if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { - String result = graphDataService.getEdges(version, type, filter); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.getEdges(version, type, filter); + response = Response.status(Status.OK).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -244,17 +246,16 @@ public class CrudRestService { if (payload.getId() != null && !payload.getId().equals(id)) { throw new CrudException("ID Mismatch", Status.BAD_REQUEST); } - String result; - + ImmutablePair<EntityTag, String> result; if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { result = graphDataService.patchEdge(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } else { - result = graphDataService.updateEdge(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } - - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -291,8 +292,8 @@ public class CrudRestService { throw new CrudException("ID Mismatch", Status.BAD_REQUEST); } - String result = graphDataService.patchEdge(version, id, type, payload); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.patchEdge(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -319,7 +320,6 @@ public class CrudRestService { logger.debug("Incoming request..." + content); Response response = null; - try { if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) { VertexPayload payload = VertexPayload.fromJson(content); @@ -330,18 +330,18 @@ public class CrudRestService { throw new CrudException("ID Mismatch", Status.BAD_REQUEST); } - String result; - payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + ImmutablePair<EntityTag, String> result; if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) { result = graphDataService.patchVertex(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } else { - result = graphDataService.updateVertex(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -380,8 +380,8 @@ public class CrudRestService { payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); - String result = graphDataService.patchVertex(version, id, type, payload); - response = Response.status(Status.OK).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.patchVertex(version, id, type, payload); + response = Response.status(Status.OK).entity(result.getValue()).type(mediaType).tag(result.getKey()).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -425,8 +425,8 @@ public class CrudRestService { payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); - String result = graphDataService.addVertex(version, type, payload); - response = Response.status(Status.CREATED).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.addVertex(version, type, payload); + response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -612,8 +612,8 @@ public class CrudRestService { payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); - String result = graphDataService.addVertex(version, payload.getType(), payload); - response = Response.status(Status.CREATED).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.addVertex(version, payload.getType(), payload); + response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -654,8 +654,8 @@ public class CrudRestService { if (payload.getType() != null && !payload.getType().equals(type)) { throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST); } - String result = graphDataService.addEdge(version, type, payload); - response = Response.status(Status.CREATED).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, type, payload); + response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } @@ -695,9 +695,8 @@ public class CrudRestService { if (payload.getType() == null || payload.getType().isEmpty()) { throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST); } - String result = graphDataService.addEdge(version, payload.getType(), payload); - - response = Response.status(Status.CREATED).entity(result).type(mediaType).build(); + ImmutablePair<EntityTag, String> result = graphDataService.addEdge(version, payload.getType(), payload); + response = Response.status(Status.CREATED).entity(result.getValue()).tag(result.getKey()).type(mediaType).build(); } else { response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).build(); } diff --git a/src/main/java/org/onap/crud/service/EdgePayload.java b/src/main/java/org/onap/crud/service/EdgePayload.java index 630ec02..a670b54 100644 --- a/src/main/java/org/onap/crud/service/EdgePayload.java +++ b/src/main/java/org/onap/crud/service/EdgePayload.java @@ -20,14 +20,12 @@ */ package org.onap.crud.service; +import javax.ws.rs.core.Response.Status; +import org.onap.crud.exception.CrudException; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; -import org.onap.crud.exception.CrudException; - -import javax.ws.rs.core.Response.Status; - public class EdgePayload { private String id; diff --git a/src/main/java/org/onap/crud/service/VertexPayload.java b/src/main/java/org/onap/crud/service/VertexPayload.java index 594dc1a..172d03b 100644 --- a/src/main/java/org/onap/crud/service/VertexPayload.java +++ b/src/main/java/org/onap/crud/service/VertexPayload.java @@ -20,15 +20,13 @@ */ package org.onap.crud.service; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonElement; - -import org.onap.crud.exception.CrudException; - import java.util.ArrayList; import java.util.List; import javax.ws.rs.core.Response.Status; +import org.onap.crud.exception.CrudException; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; public class VertexPayload { |