Search in sources :

Example 31 with TransactionalGraphEngine

use of org.onap.aai.serialization.engines.TransactionalGraphEngine 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 32 with TransactionalGraphEngine

use of org.onap.aai.serialization.engines.TransactionalGraphEngine in project aai-aai-common by onap.

the class DataCopyTest method verifyNestedSideEffect.

@Test
public void verifyNestedSideEffect() throws AAIException, IllegalArgumentException, SecurityException, IOException {
    final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
    final Introspector obj = loader.unmarshal("customer", this.getJsonString("nested-case.json"));
    TransactionalGraphEngine spy = spy(dbEngine);
    TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
    Graph g = graph.newTransaction();
    GraphTraversalSource traversal = g.traversal();
    when(spy.tx()).thenReturn(g);
    when(spy.asAdmin()).thenReturn(adminSpy);
    when(adminSpy.getTraversalSource()).thenReturn(traversal);
    when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
    when(prop.orElse(null)).thenReturn(obj.getURI());
    when(uriQuery.isDependent()).thenReturn(false);
    DBSerializer serializer = new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
    Vertex v = serializer.createNewVertex(obj);
    serializer.serializeToDb(obj, v, uriQuery, obj.getURI(), "test");
    assertEquals("nested value populated", "testValue", g.traversal().V().has("service-instance-id", "nested-instance-key").next().property("persona-model-version").orElse(""));
    g.tx().rollback();
}
Also used : TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Graph(org.apache.tinkerpop.gremlin.structure.Graph) JanusGraph(org.janusgraph.core.JanusGraph) DBSerializer(org.onap.aai.serialization.db.DBSerializer) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector)

Example 33 with TransactionalGraphEngine

use of org.onap.aai.serialization.engines.TransactionalGraphEngine in project aai-aai-common by onap.

the class DataCopyTest method expectedMissingPropertyExceptionInURI.

@Test
public void expectedMissingPropertyExceptionInURI() throws AAIException, UnsupportedEncodingException {
    final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
    final Introspector obj = loader.introspectorFromName("generic-vnf");
    obj.setValue("vnf-id", "myId");
    obj.setValue("model-invariant-id", "key1");
    TransactionalGraphEngine spy = spy(dbEngine);
    TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
    Graph g = graph.newTransaction();
    GraphTraversalSource traversal = g.traversal();
    when(spy.asAdmin()).thenReturn(adminSpy);
    when(adminSpy.getTraversalSource()).thenReturn(traversal);
    when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
    when(prop.orElse(null)).thenReturn(obj.getURI());
    DBSerializer serializer = new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
    SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
    thrown.expect(AAIMissingRequiredPropertyException.class);
    runner.execute(obj, self);
}
Also used : TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Graph(org.apache.tinkerpop.gremlin.structure.Graph) JanusGraph(org.janusgraph.core.JanusGraph) DBSerializer(org.onap.aai.serialization.db.DBSerializer) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector)

Example 34 with TransactionalGraphEngine

use of org.onap.aai.serialization.engines.TransactionalGraphEngine in project aai-aai-common by onap.

the class DataCopyTest method expectNoProcessingWithNoProperties.

@Test
public void expectNoProcessingWithNoProperties() throws AAIException, UnsupportedEncodingException {
    final Loader loader = loaderFactory.createLoaderForVersion(ModelType.MOXY, schemaVersions.getDefaultVersion());
    final Introspector obj = loader.introspectorFromName("generic-vnf");
    obj.setValue("vnf-id", "myId");
    TransactionalGraphEngine spy = spy(dbEngine);
    TransactionalGraphEngine.Admin adminSpy = spy(dbEngine.asAdmin());
    Graph g = graph.newTransaction();
    GraphTraversalSource traversal = g.traversal();
    when(spy.asAdmin()).thenReturn(adminSpy);
    when(adminSpy.getTraversalSource()).thenReturn(traversal);
    when(self.<String>property(AAIProperties.AAI_URI)).thenReturn(prop);
    when(prop.orElse(null)).thenReturn(obj.getURI());
    DBSerializer serializer = new DBSerializer(schemaVersions.getDefaultVersion(), spy, introspectorFactoryType, "AAI_TEST");
    SideEffectRunner runner = new SideEffectRunner.Builder(spy, serializer).addSideEffect(DataCopy.class).build();
    runner.execute(obj, self);
    assertNull("no model-version-id", obj.getValue("model-version-id"));
    assertNull("no model-invariant-id", obj.getValue("model-invariant-id"));
}
Also used : TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Graph(org.apache.tinkerpop.gremlin.structure.Graph) JanusGraph(org.janusgraph.core.JanusGraph) DBSerializer(org.onap.aai.serialization.db.DBSerializer) Loader(org.onap.aai.introspection.Loader) Introspector(org.onap.aai.introspection.Introspector)

Example 35 with TransactionalGraphEngine

use of org.onap.aai.serialization.engines.TransactionalGraphEngine in project aai-aai-common by onap.

the class HttpEntryTest method getAbstractTest.

@Test
public void getAbstractTest() throws UnsupportedEncodingException, AAIException {
    traversalHttpEntry.setHttpEntryProperties(schemaVersions.getDefaultVersion());
    Loader loader = traversalHttpEntry.getLoader();
    TransactionalGraphEngine dbEngine = traversalHttpEntry.getDbEngine();
    // Put generic-vnf
    String uri = "/network/generic-vnfs/generic-vnf/junit-abstract-test-generic-vnf";
    String content = "{\"vnf-id\":\"junit-abstract-test-generic-vnf\",\"vnf-name\":\"junit-generic-vnf-name\"}";
    doRequest(traversalHttpEntry, loader, dbEngine, HttpMethod.PUT, uri, content);
    // Put pserver
    uri = "/cloud-infrastructure/pservers/pserver/junit-abstract-test-pserver";
    content = "{\"hostname\":\"junit-abstract-test-pserver\"}";
    doRequest(traversalHttpEntry, loader, dbEngine, HttpMethod.PUT, uri, content);
    // PutEdge
    uri = "/cloud-infrastructure/pservers/pserver/junit-abstract-test-pserver/relationship-list/relationship";
    content = "{\"related-to\":\"vnf\",\"relationship-data\":[{\"relationship-key\":\"vnf.vnf-id\",\"relationship-value\":\"junit-abstract-test-generic-vnf\"}]}";
    doRequest(traversalHttpEntry, loader, dbEngine, HttpMethod.PUT_EDGE, uri, content);
    // getRelatedTo
    uri = "/network/generic-vnfs/generic-vnf/junit-abstract-test-generic-vnf/related-to/pservers";
    content = "";
    Response response = doRequest(traversalHttpEntry, loader, dbEngine, HttpMethod.GET, uri, content);
    String respBody = response.getEntity().toString();
    dbEngine.rollback();
    assertThat("Related to pserver is returned.", respBody, containsString("\"hostname\":\"junit-abstract-test-pserver\""));
}
Also used : TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) Loader(org.onap.aai.introspection.Loader) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.Test)

Aggregations

TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)86 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)60 JanusGraphDBEngine (org.onap.aai.serialization.engines.JanusGraphDBEngine)45 Loader (org.onap.aai.introspection.Loader)42 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)33 Before (org.junit.Before)31 Test (org.junit.Test)24 Introspector (org.onap.aai.introspection.Introspector)23 Graph (org.apache.tinkerpop.gremlin.structure.Graph)18 Matchers.containsString (org.hamcrest.Matchers.containsString)18 JanusGraph (org.janusgraph.core.JanusGraph)18 DBSerializer (org.onap.aai.serialization.db.DBSerializer)18 AAIException (org.onap.aai.exceptions.AAIException)9 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)8 SchemaVersion (org.onap.aai.setup.SchemaVersion)8 URI (java.net.URI)6 QueryParser (org.onap.aai.parsers.query.QueryParser)6 JsonObject (com.google.gson.JsonObject)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 URIToObject (org.onap.aai.parsers.uri.URIToObject)4