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.ws.rs.core.UriInfo;
import org.apache.cxf.jaxrs.ext.PATCH;
-import org.openecomp.auth.Auth;
+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.slf4j.MDC;
+import com.google.gson.Gson;
import com.google.gson.JsonElement;
+import com.google.gson.JsonPrimitive;
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;
+ Gson gson = new Gson();
+
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);
}
POST, GET, PUT, DELETE, PATCH
}
- ;
-
public void startup() {
}
if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
try {
- String result = crudGraphDataService.getVertex(version, id, type);
+ 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();
}
try {
- String result = crudGraphDataService.getVertices(version, type, filter);
+ String result = graphDataService.getVertices(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();
try {
- 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();
}
try {
- String result = crudGraphDataService.getEdges(version, type, filter);
+ 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();
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();
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();
throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
}
String result;
+
+ payload.setProperties(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) {
throw new CrudException("ID Mismatch", Status.BAD_REQUEST);
}
- String result = crudGraphDataService.patchVertex(version, id, type, payload);
+ payload.setProperties(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();
throw new CrudException("Vertex Type mismatch", Status.BAD_REQUEST);
}
- String result = crudGraphDataService.addVertex(version, type, payload);
+ payload.setProperties(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();
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<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());
+ }
+
+ if(!propertyKeys.contains(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH)) {
+ properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH,
+ (JsonElement)(new JsonPrimitive(sourceOfTruth))));
+ }
+
+ if(isAdd && !propertyKeys.contains(AAIProperties.SOURCE_OF_TRUTH)) {
+ properties.add(new AbstractMap.SimpleEntry<String, JsonElement>(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<propArray.length; i++) {
+
+ Map.Entry<String, JsonElement> entry = (Entry<String, JsonElement>) 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<String> vertices = new ArrayList<String>();
List<String> edges = new ArrayList<String>();
}
validateBulkPayload(payload);
- String result = crudGraphDataService.addBulk(version, payload);
+ String result = graphDataService.addBulk(version, payload);
response = Response.status(Status.OK).entity(result).type(mediaType).build();
} catch (CrudException ce) {
response = Response.status(ce.getHttpStatus()).entity(ce.getMessage()).build();
if (payload.getType() == null || payload.getType().isEmpty()) {
throw new CrudException("Missing Vertex Type ", Status.BAD_REQUEST);
}
- String result = crudGraphDataService.addVertex(version, payload.getType(), payload);
+ 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();
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();
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) {
if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
try {
- String result = crudGraphDataService.deleteVertex(version, id, type);
+ 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();
if (validateRequest(req, uri, content, Action.DELETE, CrudServiceConstants.CRD_AUTH_POLICY_NAME)) {
try {
- String result = crudGraphDataService.deleteEdge(version, id, type);
+ 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();