Allow filtering by reserved properties
[aai/gizmo.git] / src / main / java / org / onap / crud / service / CrudRestService.java
index 974dbb2..b3c5e7a 100644 (file)
  */
 package org.onap.crud.service;
 
+import com.google.gson.JsonElement;
+
+import org.apache.cxf.jaxrs.ext.PATCH;
+import org.onap.aai.cl.api.Logger;
+import org.onap.aai.cl.eelf.LoggerFactory;
+import org.onap.aaiauth.auth.Auth;
+import org.onap.crud.exception.CrudException;
+import org.onap.crud.logging.CrudServiceMsgs;
+import org.onap.crud.logging.LoggingUtil;
+import org.onap.crud.util.CrudProperties;
+import org.onap.crud.util.CrudServiceConstants;
+import org.onap.crud.util.CrudServiceUtil;
+import org.slf4j.MDC;
+
 import java.security.cert.X509Certificate;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
-
 import javax.security.auth.x500.X500Principal;
 import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.Consumes;
@@ -48,21 +61,9 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import javax.ws.rs.core.UriInfo;
 
-import org.apache.cxf.jaxrs.ext.PATCH;
-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;
-import org.onap.crud.util.CrudServiceConstants;
-import org.slf4j.MDC;
-
-import com.google.gson.JsonElement;
-
 public class CrudRestService {
 
-  private CrudGraphDataService crudGraphDataService;
+  private AbstractGraphDataService graphDataService;
   Logger logger = LoggerFactory.getInstance().getLogger(CrudRestService.class.getName());
   Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName());
   private Auth auth;
@@ -70,8 +71,8 @@ public class CrudRestService {
   private String mediaType = MediaType.APPLICATION_JSON;
   public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override";
 
-  public CrudRestService(CrudGraphDataService crudGraphDataService) throws Exception {
-    this.crudGraphDataService = crudGraphDataService;
+  public CrudRestService(AbstractGraphDataService graphDataService) throws Exception {
+    this.graphDataService = graphDataService;
     this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
   }
 
@@ -79,36 +80,35 @@ public class CrudRestService {
     POST, GET, PUT, DELETE, PATCH
   }
 
-  ;
-
   public void startup() {
 
   }
 
   @GET
   @Path("/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response getVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                            @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                            @Context UriInfo uriInfo, @Context HttpServletRequest req) {
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
-        String result = crudGraphDataService.getVertex(version, id, type);
+    try {
+      if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
+        String result = graphDataService.getVertex(version, id, type);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
+
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -117,33 +117,45 @@ public class CrudRestService {
 
   @GET
   @Path("/{version}/{type}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response getVertices(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
-      @Context HttpServletRequest req) {
+                              @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
+                              @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
-    if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
+    try {
+      if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
+        String propertiesKey = CrudProperties.get(CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY);
 
-      Map<String, String> filter = new HashMap<String, String>();
-      for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
-        filter.put(e.getKey(), e.getValue().get(0));
-      }
+        Map<String, String> filter = new HashMap<String, String>();
+
+        for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
+          if (!e.getKey().equals(propertiesKey)) {
+            filter.put(e.getKey(), e.getValue().get(0));
+          }
+        }
+
+        HashSet<String> properties;
+        if (uriInfo.getQueryParameters().containsKey(propertiesKey)) {
+          properties = new HashSet<>(uriInfo.getQueryParameters().get(propertiesKey));
+        } else {
+          properties = new HashSet<>();
+        }
 
-      try {
-        String result = crudGraphDataService.getVertices(version, type, filter);
+        String result = graphDataService.getVertices(version, type, filter, properties);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
+
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -152,29 +164,29 @@ public class CrudRestService {
 
   @GET
   @Path("/relationships/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response getEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                          @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                          @Context UriInfo uriInfo, @Context HttpServletRequest req) {
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
 
-        String result = crudGraphDataService.getEdge(version, id, type);
+        String result = graphDataService.getEdge(version, id, type);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -183,35 +195,34 @@ public class CrudRestService {
 
   @GET
   @Path("/relationships/{version}/{type}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response getEdges(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
-      @Context HttpServletRequest req) {
+                           @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
+                           @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      Map<String, String> filter = new HashMap<String, String>();
-      for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
-        filter.put(e.getKey(), e.getValue().get(0));
-      }
+    Map<String, String> filter = new HashMap<String, String>();
+    for (Map.Entry<String, List<String>> e : uriInfo.getQueryParameters().entrySet()) {
+      filter.put(e.getKey(), e.getValue().get(0));
+    }
 
-      try {
-        String result = crudGraphDataService.getEdges(version, type, filter);
+    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();
-      } 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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -220,20 +231,20 @@ public class CrudRestService {
 
   @PUT
   @Path("/relationships/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response updateEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                             @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                             @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         EdgePayload payload = EdgePayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -245,21 +256,20 @@ public class CrudRestService {
 
         if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
             && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
-          result = crudGraphDataService.patchEdge(version, id, type, payload);
+          result = graphDataService.patchEdge(version, id, type, payload);
         } else {
 
-          result = crudGraphDataService.updateEdge(version, id, type, payload);
+          result = graphDataService.updateEdge(version, id, type, payload);
         }
 
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -268,19 +278,19 @@ public class CrudRestService {
 
   @PATCH
   @Path("/relationships/{version}/{type}/{id}")
-  @Consumes({ "application/merge-patch+json" })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({"application/merge-patch+json"})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response patchEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                            @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                            @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
-    if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         EdgePayload payload = EdgePayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -289,15 +299,15 @@ public class CrudRestService {
           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
         }
 
-        String result = crudGraphDataService.patchEdge(version, id, type, payload);
+        String result = graphDataService.patchEdge(version, id, type, payload);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -306,20 +316,20 @@ public class CrudRestService {
 
   @PUT
   @Path("/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response updateVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                               @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                               @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.PUT, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         VertexPayload payload = VertexPayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -327,22 +337,26 @@ public class CrudRestService {
         if (payload.getId() != null && !payload.getId().equals(id)) {
           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
         }
+
         String result;
+
+        payload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(payload.getProperties(), headers, false));
+
         if (headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE) != null
             && headers.getRequestHeaders().getFirst(HTTP_PATCH_METHOD_OVERRIDE).equalsIgnoreCase("PATCH")) {
-          result = crudGraphDataService.patchVertex(version, id, type, payload);
+          result = graphDataService.patchVertex(version, id, type, payload);
         } else {
 
-          result = crudGraphDataService.updateVertex(version, id, type, payload);
+          result = graphDataService.updateVertex(version, id, type, payload);
         }
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -351,19 +365,19 @@ public class CrudRestService {
 
   @PATCH
   @Path("/{version}/{type}/{id}")
-  @Consumes({ "application/merge-patch+json" })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({"application/merge-patch+json"})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response patchVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                              @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                              @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.PATCH, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         VertexPayload payload = VertexPayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -372,15 +386,17 @@ public class CrudRestService {
           throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
         }
 
-        String result = crudGraphDataService.patchVertex(version, id, type, payload);
+        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();
-      } 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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -389,20 +405,20 @@ public class CrudRestService {
 
   @POST
   @Path("/{version}/{type}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response addVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
-      @Context HttpServletRequest req) {
+                            @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
+                            @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         VertexPayload payload = VertexPayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -415,15 +431,17 @@ public class CrudRestService {
           throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
         }
 
-        String result = crudGraphDataService.addVertex(version, type, payload);
+        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();
-      } 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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -458,11 +476,13 @@ public class CrudRestService {
 
       if (!opr.getValue().getAsString().equalsIgnoreCase("add")
           && !opr.getValue().getAsString().equalsIgnoreCase("modify")
+          && !opr.getValue().getAsString().equalsIgnoreCase("patch")
           && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
         throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
       }
-      // check if ID is populate for modify/delete operation
+      // check if ID is populate for modify/patch/delete operation
       if ((opr.getValue().getAsString().equalsIgnoreCase("modify")
+          || opr.getValue().getAsString().equalsIgnoreCase("patch")
           || opr.getValue().getAsString().equalsIgnoreCase("delete")) && (vertexPayload.getId() == null)) {
 
         throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
@@ -498,11 +518,13 @@ public class CrudRestService {
 
       if (!opr.getValue().getAsString().equalsIgnoreCase("add")
           && !opr.getValue().getAsString().equalsIgnoreCase("modify")
+          && !opr.getValue().getAsString().equalsIgnoreCase("patch")
           && !opr.getValue().getAsString().equalsIgnoreCase("delete")) {
         throw new CrudException("Invalid operation at item: " + item.getKey(), Status.BAD_REQUEST);
       }
-      // check if ID is populate for modify/delete operation
+      // check if ID is populate for modify/patch/delete operation
       if ((edgePayload.getId() == null) && (opr.getValue().getAsString().equalsIgnoreCase("modify")
+          || opr.getValue().getAsString().equalsIgnoreCase("patch")
           || opr.getValue().getAsString().equalsIgnoreCase("delete"))) {
 
         throw new CrudException("Mising ID at item: " + item.getKey(), Status.BAD_REQUEST);
@@ -532,37 +554,37 @@ public class CrudRestService {
 
   @POST
   @Path("/{version}/bulk/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response addBulk(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
-      @Context HttpServletRequest req) {
+                          @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
+                          @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         BulkPayload payload = BulkPayload.fromJson(content);
         if ((payload.getObjects() == null && payload.getRelationships() == null)
             || (payload.getObjects() != null && payload.getObjects().isEmpty() && payload.getRelationships() != null
-                && payload.getRelationships().isEmpty())) {
+            && payload.getRelationships().isEmpty())) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
         }
 
         validateBulkPayload(payload);
-        String result = crudGraphDataService.addBulk(version, payload);
+        String result = graphDataService.addBulk(version, payload, headers);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -571,19 +593,19 @@ public class CrudRestService {
 
   @POST
   @Path("/{version}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response addVertex(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
-      @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                            @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
-      try {
+    try {
 
+      if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         VertexPayload payload = VertexPayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -595,15 +617,18 @@ public class CrudRestService {
         if (payload.getType() == null || payload.getType().isEmpty()) {
           throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
         }
-        String result = crudGraphDataService.addVertex(version, payload.getType(), payload);
+
+        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();
-      } 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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -612,20 +637,20 @@ public class CrudRestService {
 
   @POST
   @Path("/relationships/{version}/{type}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response addEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
-      @Context HttpServletRequest req) {
+                          @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers, @Context UriInfo uriInfo,
+                          @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         EdgePayload payload = EdgePayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -637,15 +662,15 @@ public class CrudRestService {
         if (payload.getType() != null && !payload.getType().equals(type)) {
           throw new CrudException("Edge Type mismatch", Status.BAD_REQUEST);
         }
-        String result = crudGraphDataService.addEdge(version, type, payload);
+        String result = graphDataService.addEdge(version, type, payload);
         response = Response.status(Status.CREATED).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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -654,19 +679,19 @@ public class CrudRestService {
 
   @POST
   @Path("/relationships/{version}/")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response addEdge(String content, @PathParam("version") String version, @PathParam("uri") @Encoded String uri,
-      @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                          @Context HttpHeaders headers, @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
+    try {
+      if (validateRequest(req, uri, content, Action.POST, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
         EdgePayload payload = EdgePayload.fromJson(content);
         if (payload.getProperties() == null || payload.getProperties().isJsonNull()) {
           throw new CrudException("Invalid request Payload", Status.BAD_REQUEST);
@@ -678,16 +703,16 @@ public class CrudRestService {
         if (payload.getType() == null || payload.getType().isEmpty()) {
           throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
         }
-        String result = crudGraphDataService.addEdge(version, payload.getType(), payload);
+        String result = graphDataService.addEdge(version, payload.getType(), payload);
 
         response = Response.status(Status.CREATED).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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -696,29 +721,29 @@ public class CrudRestService {
 
   @DELETE
   @Path("/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response deleteVertex(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                               @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                               @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
 
-    if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
-        String result = crudGraphDataService.deleteVertex(version, id, type);
+    try {
+      if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
+        String result = graphDataService.deleteVertex(version, id, type);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -727,28 +752,28 @@ public class CrudRestService {
 
   @DELETE
   @Path("/relationships/{version}/{type}/{id}")
-  @Consumes({ MediaType.APPLICATION_JSON })
-  @Produces({ MediaType.APPLICATION_JSON })
+  @Consumes({MediaType.APPLICATION_JSON})
+  @Produces({MediaType.APPLICATION_JSON})
   public Response deleteEdge(String content, @PathParam("version") String version, @PathParam("type") String type,
-      @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
-      @Context UriInfo uriInfo, @Context HttpServletRequest req) {
+                             @PathParam("id") String id, @PathParam("uri") @Encoded String uri, @Context HttpHeaders headers,
+                             @Context UriInfo uriInfo, @Context HttpServletRequest req) {
 
     LoggingUtil.initMdcContext(req, headers);
 
     logger.debug("Incoming request..." + content);
     Response response = null;
-    if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
 
-      try {
-        String result = crudGraphDataService.deleteEdge(version, id, type);
+    try {
+      if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
+        String result = graphDataService.deleteEdge(version, id, type);
         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();
       }
-    } else {
-      response = Response.status(Status.FORBIDDEN).entity(content).type(MediaType.APPLICATION_JSON).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();
     }
 
     LoggingUtil.logRestRequest(logger, auditLogger, req, response);
@@ -756,7 +781,8 @@ public class CrudRestService {
   }
 
   protected boolean validateRequest(HttpServletRequest req, String uri, String content, Action action,
-      String authPolicyFunctionName) {
+                                    String authPolicyFunctionName, HttpHeaders headers) throws CrudException {
+    boolean isValid = false;
     try {
       String cipherSuite = (String) req.getAttribute("javax.servlet.request.cipher_suite");
       String authUser = null;
@@ -766,11 +792,22 @@ public class CrudRestService {
         X500Principal subjectDn = clientCert.getSubjectX500Principal();
         authUser = subjectDn.toString();
       }
-      return this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
+      isValid = this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
     } catch (Exception e) {
       logResult(action, uri, e);
       return false;
     }
+
+    String sourceOfTruth = null;
+    if (headers.getRequestHeaders().containsKey("X-FromAppId")) {
+      sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
+    }
+
+    if (sourceOfTruth == null || sourceOfTruth.trim() == "") {
+      throw new CrudException("Invalid request, Missing X-FromAppId header", Status.BAD_REQUEST);
+    }
+
+    return isValid;
   }
 
   void logResult(Action op, String uri, Exception e) {