Search in sources :

Example 1 with DBRequest

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;
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector) URI(java.net.URI) AAIException(org.onap.aai.exceptions.AAIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) QueryParser(org.onap.aai.parsers.query.QueryParser) URIToObject(org.onap.aai.parsers.uri.URIToObject) DBRequest(org.onap.aai.rest.db.DBRequest) AAIException(org.onap.aai.exceptions.AAIException) HttpMethod(org.onap.aai.restcore.HttpMethod)

Example 2 with DBRequest

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;
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector) URI(java.net.URI) AAIException(org.onap.aai.exceptions.AAIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) QueryParser(org.onap.aai.parsers.query.QueryParser) DBRequest(org.onap.aai.rest.db.DBRequest) URIToObject(org.onap.aai.parsers.uri.URIToObject) AAIException(org.onap.aai.exceptions.AAIException) HttpMethod(org.onap.aai.restcore.HttpMethod) Pair(org.javatuples.Pair)

Example 3 with DBRequest

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;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) QueryParser(org.onap.aai.parsers.query.QueryParser) DBRequest(org.onap.aai.rest.db.DBRequest) ArrayList(java.util.ArrayList) URI(java.net.URI)

Example 4 with DBRequest

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;
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector) URI(java.net.URI) AAIException(org.onap.aai.exceptions.AAIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) QueryParser(org.onap.aai.parsers.query.QueryParser) DBRequest(org.onap.aai.rest.db.DBRequest) URIToObject(org.onap.aai.parsers.uri.URIToObject) AAIException(org.onap.aai.exceptions.AAIException) HttpMethod(org.onap.aai.restcore.HttpMethod)

Example 5 with DBRequest

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;
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector) URI(java.net.URI) AAIException(org.onap.aai.exceptions.AAIException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) QueryParser(org.onap.aai.parsers.query.QueryParser) URIToObject(org.onap.aai.parsers.uri.URIToObject) DBRequest(org.onap.aai.rest.db.DBRequest) AAIException(org.onap.aai.exceptions.AAIException)

Aggregations

URI (java.net.URI)5 QueryParser (org.onap.aai.parsers.query.QueryParser)5 DBRequest (org.onap.aai.rest.db.DBRequest)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 AAIException (org.onap.aai.exceptions.AAIException)4 Introspector (org.onap.aai.introspection.Introspector)4 Loader (org.onap.aai.introspection.Loader)4 URIToObject (org.onap.aai.parsers.uri.URIToObject)4 TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)4 SchemaVersion (org.onap.aai.setup.SchemaVersion)4 HttpMethod (org.onap.aai.restcore.HttpMethod)3 ArrayList (java.util.ArrayList)1 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)1 Pair (org.javatuples.Pair)1