From eabf0a08faa6581c88c2e4ca5be56d4630891046 Mon Sep 17 00:00:00 2001 From: Gurjeet Bedi Date: Sun, 7 Jan 2018 21:07:50 -0500 Subject: OXM which tracks provenance Support for source-of-truth and last-mod-source-of-truth Issue-ID: AAI-482 Change-Id: Idc7ec47c0561f2dad6f930d0ac0dd3fff9585b4f Signed-off-by: Gurjeet Bedi --- .../crud/service/AbstractGraphDataService.java | 4 +- .../crud/service/CrudAsyncGraphDataService.java | 10 +++- .../onap/crud/service/CrudGraphDataService.java | 12 ++++- .../org/onap/crud/service/CrudRestService.java | 62 +++------------------- .../java/org/onap/crud/util/CrudServiceUtil.java | 56 ++++++++++++++++++- 5 files changed, 84 insertions(+), 60 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java index e627def..013845e 100644 --- a/src/main/java/org/onap/crud/service/AbstractGraphDataService.java +++ b/src/main/java/org/onap/crud/service/AbstractGraphDataService.java @@ -26,6 +26,8 @@ package org.onap.crud.service; import java.util.List; import java.util.Map; +import javax.ws.rs.core.HttpHeaders; + import org.onap.crud.dao.GraphDao; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; @@ -79,5 +81,5 @@ public abstract class AbstractGraphDataService { 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 String addBulk(String version, BulkPayload payload) throws CrudException; + public abstract String addBulk(String version, BulkPayload payload, HttpHeaders headers) 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 840576e..d825d79 100644 --- a/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudAsyncGraphDataService.java @@ -45,6 +45,7 @@ import org.onap.crud.logging.CrudServiceMsgs; import org.onap.crud.parser.CrudResponseBuilder; import org.onap.crud.util.CrudProperties; import org.onap.crud.util.CrudServiceConstants; +import org.onap.crud.util.CrudServiceUtil; import org.onap.schema.OxmModelValidator; import org.onap.schema.RelationshipSchemaValidator; @@ -63,6 +64,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import javax.annotation.PreDestroy; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response.Status; public class CrudAsyncGraphDataService extends AbstractGraphDataService { @@ -372,7 +374,7 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { } @Override - public String addBulk(String version, BulkPayload payload) throws CrudException { + public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException { HashMap vertices = new HashMap(); HashMap edges = new HashMap(); String txId = dao.openTransaction(); @@ -397,13 +399,17 @@ public class CrudAsyncGraphDataService extends AbstractGraphDataService { GraphEvent event; if (opr.getValue().getAsString().equalsIgnoreCase("add")) { + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + headers, true)); // Publish add-vertex event validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), vertexPayload.getProperties()); event = GraphEvent.builder(GraphEventOperation.CREATE) .vertex(GraphEventVertex.fromVertex(validatedVertex, version)).build(); } else { - // Publish update-vertex event + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + headers, false)); + // Publish update-vertex event validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, vertexPayload.getType(), vertexPayload.getProperties()); event = GraphEvent.builder(GraphEventOperation.UPDATE) diff --git a/src/main/java/org/onap/crud/service/CrudGraphDataService.java b/src/main/java/org/onap/crud/service/CrudGraphDataService.java index 49bf370..cd72533 100644 --- a/src/main/java/org/onap/crud/service/CrudGraphDataService.java +++ b/src/main/java/org/onap/crud/service/CrudGraphDataService.java @@ -28,6 +28,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response.Status; import org.onap.crud.dao.GraphDao; @@ -36,6 +37,7 @@ 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; @@ -52,7 +54,7 @@ public class CrudGraphDataService extends AbstractGraphDataService { return addVertex(version, vertex); } - public String addBulk(String version, BulkPayload payload) throws CrudException { + public String addBulk(String version, BulkPayload payload, HttpHeaders headers) throws CrudException { HashMap vertices = new HashMap(); HashMap edges = new HashMap(); String txId = dao.openTransaction(); @@ -75,11 +77,17 @@ public class CrudGraphDataService extends AbstractGraphDataService { Vertex validatedVertex; Vertex persistedVertex; if (opr.getValue().getAsString().equalsIgnoreCase("add")) { - validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + headers, true)); + + validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(), vertexPayload.getProperties()); + // Call champDAO to add the vertex persistedVertex = dao.addVertex(validatedVertex.getType(), validatedVertex.getProperties(), txId); } else { + vertexPayload.setProperties(CrudServiceUtil.mergeHeaderInFoToPayload(vertexPayload.getProperties(), + headers, false)); validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, vertexPayload.getType(), vertexPayload.getProperties()); // Call champDAO to update the vertex diff --git a/src/main/java/org/onap/crud/service/CrudRestService.java b/src/main/java/org/onap/crud/service/CrudRestService.java index b9161a1..fcf19c0 100644 --- a/src/main/java/org/onap/crud/service/CrudRestService.java +++ b/src/main/java/org/onap/crud/service/CrudRestService.java @@ -24,14 +24,10 @@ package org.onap.crud.service; import java.security.cert.X509Certificate; -import java.util.AbstractMap; import java.util.ArrayList; import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; import javax.security.auth.x500.X500Principal; import javax.servlet.http.HttpServletRequest; @@ -55,16 +51,14 @@ 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.aai.db.props.AAIProperties; 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.onap.crud.util.CrudServiceUtil; import org.slf4j.MDC; -import com.google.gson.Gson; import com.google.gson.JsonElement; -import com.google.gson.JsonPrimitive; public class CrudRestService { @@ -73,8 +67,6 @@ public class CrudRestService { Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(CrudRestService.class.getName()); private Auth auth; - Gson gson = new Gson(); - private String mediaType = MediaType.APPLICATION_JSON; public static final String HTTP_PATCH_METHOD_OVERRIDE = "X-HTTP-Method-Override"; @@ -335,7 +327,7 @@ public class CrudRestService { } String result; - payload.setProperties(mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + 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")) { @@ -381,7 +373,7 @@ public class CrudRestService { throw new CrudException("ID Mismatch", Status.BAD_REQUEST); } - payload.setProperties(mergeHeaderInFoToPayload(payload.getProperties(), headers, false)); + 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(); @@ -426,7 +418,7 @@ public class CrudRestService { throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST); } - payload.setProperties(mergeHeaderInFoToPayload(payload.getProperties(), headers, true)); + 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(); @@ -443,47 +435,6 @@ public class CrudRestService { return response; } - private JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest, HttpHeaders headers, boolean isAdd) { - if(!headers.getRequestHeaders().containsKey("X-FromAppId")) - return propertiesFromRequest; - - String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); - Set> properties = new HashSet>(); - properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet()); - - Set propertyKeys = new HashSet(); - for(Map.Entry property : properties) { - propertyKeys.add(property.getKey()); - } - - if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) { - properties.add(new AbstractMap.SimpleEntry(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, - (JsonElement)(new JsonPrimitive(sourceOfTruth)))); - } - - if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) { - properties.add(new AbstractMap.SimpleEntry(AAIProperties.SOURCE_OF_TRUTH, - (JsonElement)(new JsonPrimitive(sourceOfTruth)))); - } - - Object[] propArray = properties.toArray(); - StringBuilder sb = new StringBuilder(); - sb.append("{"); - boolean first=true; - for(int i=0; i entry = (Entry) propArray[i]; - if(!first) { - sb.append(","); - } - sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue()); - first=false; - } - sb.append("}"); - - return gson.fromJson(sb.toString(), JsonElement.class); - } - private void validateBulkPayload(BulkPayload payload) throws CrudException { List vertices = new ArrayList(); List edges = new ArrayList(); @@ -608,7 +559,7 @@ public class CrudRestService { } validateBulkPayload(payload); - String result = graphDataService.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(); @@ -649,6 +600,9 @@ public class CrudRestService { if (payload.getType() == null || payload.getType().isEmpty()) { throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST); } + + 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) { diff --git a/src/main/java/org/onap/crud/util/CrudServiceUtil.java b/src/main/java/org/onap/crud/util/CrudServiceUtil.java index 88df8d1..45f2414 100644 --- a/src/main/java/org/onap/crud/util/CrudServiceUtil.java +++ b/src/main/java/org/onap/crud/util/CrudServiceUtil.java @@ -23,15 +23,27 @@ */ package org.onap.crud.util; +import org.onap.aai.db.props.AAIProperties; import org.onap.aaiutils.oxm.OxmModelLoader; import org.onap.crud.exception.CrudException; import org.onap.schema.RelationshipSchemaLoader; +import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonPrimitive; + +import java.util.AbstractMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.Map.Entry; + +import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response.Status; public class CrudServiceUtil { - + private static Gson gson = new Gson(); public static Object validateFieldType(String value, Class clazz) throws CrudException { try { if (clazz.isAssignableFrom(Integer.class)) { @@ -71,4 +83,46 @@ public class CrudServiceUtil { } RelationshipSchemaLoader.loadModels(); } + + public static JsonElement mergeHeaderInFoToPayload(JsonElement propertiesFromRequest, HttpHeaders headers, boolean isAdd) { + if(!headers.getRequestHeaders().containsKey("X-FromAppId")) + return propertiesFromRequest; + + String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); + Set> properties = new HashSet>(); + properties.addAll(propertiesFromRequest.getAsJsonObject().entrySet()); + + Set propertyKeys = new HashSet(); + for(Map.Entry property : properties) { + propertyKeys.add(property.getKey()); + } + + if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) { + properties.add(new AbstractMap.SimpleEntry(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, + (JsonElement)(new JsonPrimitive(sourceOfTruth)))); + } + + if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) { + properties.add(new AbstractMap.SimpleEntry(AAIProperties.SOURCE_OF_TRUTH, + (JsonElement)(new JsonPrimitive(sourceOfTruth)))); + } + + Object[] propArray = properties.toArray(); + StringBuilder sb = new StringBuilder(); + sb.append("{"); + boolean first=true; + for(int i=0; i entry = (Entry) propArray[i]; + if(!first) { + sb.append(","); + } + sb.append("\"").append(entry.getKey()).append("\"").append(":").append(entry.getValue()); + first=false; + } + sb.append("}"); + + return gson.fromJson(sb.toString(), JsonElement.class); + } + } -- cgit 1.2.3-korg