/**
* ============LICENSE_START=======================================================
- * Gizmo
+ * org.onap.aai
* ================================================================================
- * Copyright © 2017 AT&T Intellectual Property.
- * Copyright © 2017 Amdocs
- * All rights reserved.
+ * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2018 Amdocs
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- * <p>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p>
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
- * <p>
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
*/
-
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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
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;
+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.parser.BulkPayload;
+import org.onap.crud.parser.EdgePayload;
+import org.onap.crud.parser.VertexPayload;
+import org.onap.crud.util.CrudProperties;
+import org.onap.crud.util.CrudServiceConstants;
+import org.onap.crud.util.CrudServiceUtil;
+import org.slf4j.MDC;
+import com.google.gson.JsonElement;
+@Path("/services/inventory")
public class CrudRestService {
private AbstractGraphDataService graphDataService;
this.auth = new Auth(CrudServiceConstants.CRD_AUTH_FILE);
}
+ // For unit testing
+ public CrudRestService(AbstractGraphDataService graphDataService, Auth auth) throws Exception {
+ this.graphDataService = graphDataService;
+ this.auth = auth;
+ }
+
public enum Action {
POST, GET, PUT, DELETE, PATCH
}
logger.debug("Incoming request..." + content);
Response response = null;
+ Map<String, String> params = addParams ( uriInfo, false, type, version );
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();
+ 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();
}
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()) {
- if (!e.getKey().equals(propertiesKey)) {
- filter.put(e.getKey(), e.getValue().get(0));
- }
- }
+ Map<String, String> filter = addParams ( uriInfo, true, type, version );
HashSet<String> properties;
if (uriInfo.getQueryParameters().containsKey(propertiesKey)) {
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();
}
logger.debug("Incoming request..." + content);
Response response = null;
+ Map<String, String> params = addParams ( uriInfo, false, type, version );
try {
if (validateRequest(req, uri, content, Action.GET, CrudServiceConstants.CRD_AUTH_POLICY_NAME, headers)) {
- String result = graphDataService.getEdge(version, id, type);
- 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();
}
logger.debug("Incoming request..." + content);
Response response = null;
-
-
- 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 = addParams ( uriInfo, true, type, version );
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();
}
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();
}
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();
}
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);
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();
}
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();
}
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();
}
EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
- if (edgePayload.getType() == null) {
- throw new CrudException("Edge Type cannot be null for: " + item.getKey(), Status.BAD_REQUEST);
- }
-
if (!opr.getKey().equalsIgnoreCase("operation")) {
throw new CrudException("operation missing in item: " + item.getKey(), 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();
+ 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();
}
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();
}
}
if (payload.getType() == null || payload.getType().isEmpty()) {
- throw new CrudException("Missing Edge Type ", Status.BAD_REQUEST);
+ payload.setType(CrudServiceUtil.determineEdgeType(payload, version));
}
- 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();
}
X500Principal subjectDn = clientCert.getSubjectX500Principal();
authUser = subjectDn.toString();
}
- isValid = this.auth.validateRequest(authUser.toLowerCase(), action.toString() + ":" + authPolicyFunctionName);
+ if(null != authUser) {
+ 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");
- }
+ validateRequestHeader(headers);
+
+ return isValid;
+ }
+
+ public void validateRequestHeader(HttpHeaders headers) throws CrudException {
+ 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);
- }
+ if (sourceOfTruth == null || sourceOfTruth.trim() == "") {
+ throw new CrudException("Invalid request, Missing X-FromAppId header", Status.BAD_REQUEST);
+ }
+
+ String transId = null;
+ if (headers.getRequestHeaders().containsKey("X-TransactionId")) {
+ transId = headers.getRequestHeaders().getFirst("X-TransactionId");
+ }
- return isValid;
+ if (transId == null || transId.trim() == "") {
+ throw new CrudException("Invalid request, Missing X-TransactionId header", Status.BAD_REQUEST);
+ }
}
void logResult(Action op, String uri, Exception e) {
- logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, e.getStackTrace().toString());
+ logger.error(CrudServiceMsgs.EXCEPTION_DURING_METHOD_CALL, op.toString(), uri, Arrays.toString(e.getStackTrace()));
// Clear the MDC context so that no other transaction inadvertently
// uses our transaction id.
MDC.clear();
}
+
+ private Map<String, String> addParams ( UriInfo info, boolean filter, String type, String version ) {
+ String propertiesKey = CrudProperties.get ( CrudServiceConstants.CRD_COLLECTION_PROPERTIES_KEY );
+ Map<String, String> params = new HashMap<String, String> ();
+ params.put ( CrudServiceConstants.CRD_RESERVED_VERSION, version );
+ params.put ( CrudServiceConstants.CRD_RESERVED_NODE_TYPE, type );
+ if (filter) {
+ for (Map.Entry<String, List<String>> e : info.getQueryParameters ().entrySet ()) {
+ if (!e.getKey ().equals ( propertiesKey )) {
+ params.put ( e.getKey (), e.getValue ().get ( 0 ) );
+ }
+ }
+ } else {
+ for (Map.Entry<String, List<String>> e : info.getQueryParameters ().entrySet ()) {
+ params.put ( e.getKey (), e.getValue ().get ( 0 ) );
+ }
+ }
+ return params;
+ }
}