use of org.onap.aai.rest.db.DBRequest in project aai-aai-common by onap.
the class HttpTestUtil method doPatch.
public Response doPatch(String uri, String payload) throws UnsupportedEncodingException, AAIException {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
if (uri.startsWith("/aai/")) {
uri = uri.substring(5);
}
logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
Mockito.when(uriInfo.getPath()).thenReturn(uri);
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = uriToObject.getEntityName();
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj;
HttpMethod httpMethod;
obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
httpMethod = HttpMethod.MERGE_PATCH;
this.validateIntrospector(obj, loader, uriObject, httpMethod);
DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").rawRequestContent(payload).build();
List<DBRequest> dbRequestList = new ArrayList<>();
dbRequestList.add(dbRequest);
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.PUT, response);
}
}
dbEngine.commit();
} else {
if (response != null) {
logFailure(HttpMethod.PUT, response);
}
dbEngine.rollback();
}
}
return response;
}
use of org.onap.aai.rest.db.DBRequest in project aai-aai-common by onap.
the class HttpTestUtil method doDelete.
public Response doDelete(Map<String, Pair<String, String>> deletes) {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
List<DBRequest> dbRequestList = new ArrayList<>();
for (Map.Entry<String, Pair<String, String>> delete : deletes.entrySet()) {
String uri = delete.getKey();
String resourceVersion = delete.getValue().getValue0();
String content = delete.getValue().getValue1();
uri = uri.replaceAll("/aai/", "");
logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
Mockito.when(uriInfo.getPath()).thenReturn(uri);
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = uriToObject.getEntityName();
queryParameters.add("resource-version", resourceVersion);
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj;
HttpMethod httpMethod;
if (uri.contains("/relationship-list/relationship")) {
httpMethod = HttpMethod.DELETE_EDGE;
obj = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum("application/json"));
} else {
obj = loader.introspectorFromName(objType);
httpMethod = HttpMethod.DELETE;
}
DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").build();
dbRequestList.add(dbRequest);
}
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.DELETE, response);
}
}
dbEngine.commit();
} else {
logFailure(HttpMethod.DELETE, response);
dbEngine.rollback();
}
}
return response;
}
use of org.onap.aai.rest.db.DBRequest in project aai-aai-common by onap.
the class OrphanLInterfaceHandler method createOrphanLInterfaceDelRequests.
public List<DBRequest> createOrphanLInterfaceDelRequests(AAIExtensionMap aaiReqMap, Introspector newvce) throws AAIException, UnsupportedEncodingException, URISyntaxException {
List<DBRequest> requests = new ArrayList<>();
QueryBuilder<Vertex> query = createLInterfaceQuery(aaiReqMap, newvce);
List<Vertex> linterfaces = query.toList();
for (Vertex lint : linterfaces) {
URI lintURI = buildLInterfaceURI(lint, aaiReqMap);
QueryParser parser = createLInterfaceQuery(aaiReqMap, newvce).createQueryFromObjectName("l-interface");
DBRequest originalDbRequest = aaiReqMap.getDbRequest();
DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, lintURI, parser, newvce, originalDbRequest.getHeaders(), originalDbRequest.getInfo(), originalDbRequest.getTransactionId()).build();
requests.add(request);
}
return requests;
}
use of org.onap.aai.rest.db.DBRequest in project aai-aai-common by onap.
the class HttpTestUtil method doPut.
public Response doPut(Map<String, String> uriPayload) throws UnsupportedEncodingException, AAIException {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
List<DBRequest> dbRequestList = new ArrayList<>();
for (Map.Entry<String, String> entry : uriPayload.entrySet()) {
String uri = entry.getKey();
String payload = entry.getValue();
if (uri.startsWith("/aai/")) {
uri = uri.substring(5);
}
logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
Mockito.when(uriInfo.getPath()).thenReturn(uri);
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = uriToObject.getEntityName();
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj;
HttpMethod httpMethod;
if (uri.contains("/relationship-list/relationship")) {
obj = loader.unmarshal("relationship", payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
httpMethod = HttpMethod.PUT_EDGE;
} else {
obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
httpMethod = HttpMethod.PUT;
this.validateIntrospector(obj, loader, uriObject, httpMethod);
}
DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").rawRequestContent(payload).build();
dbRequestList.add(dbRequest);
}
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.PUT, response);
}
}
dbEngine.commit();
} else {
if (response != null) {
logFailure(HttpMethod.PUT, response);
}
dbEngine.rollback();
}
}
return response;
}
use of org.onap.aai.rest.db.DBRequest in project aai-aai-common by onap.
the class HttpTestUtil method doGet.
public Response doGet(String uri, String depth, String format) {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
if (uri.startsWith("/aai/")) {
uri = uri.substring(5);
}
logger.info("Starting the GET request for the uri {} with depth {}", uri, depth);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
if (depth != null) {
queryParameters.add("depth", depth);
}
if (format != null) {
queryParameters.add("format", format);
}
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
Mockito.when(uriInfo.getPath()).thenReturn(uri);
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = "";
if (!uriQuery.getContainerType().equals("")) {
objType = uriQuery.getContainerType();
} else {
objType = uriQuery.getResultType();
}
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj = loader.introspectorFromName(objType);
DBRequest dbRequest = new DBRequest.Builder(HttpMethod.GET, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").build();
List<DBRequest> dbRequestList = new ArrayList<>();
dbRequestList.add(dbRequest);
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the GET request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.GET, response);
}
}
dbEngine.commit();
} else {
logFailure(HttpMethod.GET, response);
dbEngine.rollback();
}
}
return response;
}
Aggregations