Search in sources :

Example 1 with SchemaVersion

use of org.onap.aai.setup.SchemaVersion in project aai-graphadmin by onap.

the class SchemaMod method execute.

public void execute(String[] args) {
    logger = LoggerFactory.getLogger(SchemaMod.class.getSimpleName());
    // NOTE -- We're just working with properties that are used for NODES
    // for now.
    String propName = "";
    String targetDataType = "";
    String targetIndexInfo = "";
    String preserveDataFlag = "";
    String consistencyLockFlag = "";
    String commitBlockSizeStr = "";
    long commitBlockSize = 120000;
    String usageString = "Usage: SchemaMod propertyName targetDataType targetIndexInfo preserveDataFlag consistencyLockFlag [blockSize] \n";
    if (args.length == 5) {
        propName = args[0];
        targetDataType = args[1];
        targetIndexInfo = args[2];
        preserveDataFlag = args[3];
        consistencyLockFlag = args[4];
    } else if (args.length == 6) {
        propName = args[0];
        targetDataType = args[1];
        targetIndexInfo = args[2];
        preserveDataFlag = args[3];
        consistencyLockFlag = args[4];
        commitBlockSizeStr = args[5];
    } else {
        String emsg = "Incorrect number of Parameters passed.  \n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    }
    if (propName.equals("")) {
        String emsg = "Bad parameter - propertyName cannot be empty.  \n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    } else if (!targetDataType.equals("String") && !targetDataType.equals("Set<String>") && !targetDataType.equals("Integer") && !targetDataType.equals("Long") && !targetDataType.equals("Boolean")) {
        String emsg = "Unsupported targetDataType.  We only support String, Set<String>, Integer, Long or Boolean for now.\n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    } else if (!targetIndexInfo.equals("uniqueIndex") && !targetIndexInfo.equals("index") && !targetIndexInfo.equals("noIndex")) {
        String emsg = "Unsupported IndexInfo.  We only support: 'uniqueIndex', 'index' or 'noIndex'.\n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    }
    try {
        if (!commitBlockSizeStr.equals("")) {
            // They're over-riding the commitBlockSize
            commitBlockSize = Long.parseLong(commitBlockSizeStr);
        }
    } catch (NumberFormatException nfe) {
        String emsg = "NumberFormatException - Bad block size passed in: [" + commitBlockSizeStr + "]. ";
        logAndPrint(logger, emsg);
        System.exit(1);
    }
    try {
        AAIConfig.init();
        ErrorLogHelper.loadProperties();
    } catch (Exception ae) {
        String emsg = "Problem with either AAIConfig.init() or ErrorLogHelper.LoadProperties(). ";
        logAndPrint(logger, emsg + "[" + ae.getMessage() + "]");
        System.exit(1);
    }
    logAndPrint(logger, ">>> Processing will begin in 5 seconds (unless interrupted). <<<");
    try {
        // Give them a chance to back out of this
        Thread.sleep(5000);
    } catch (java.lang.InterruptedException ie) {
        logAndPrint(logger, " DB Schema Update has been aborted. ");
        System.exit(1);
    }
    logAndPrint(logger, "    ---- NOTE --- about to open graph (takes a little while)\n");
    SchemaVersion version = schemaVersions.getDefaultVersion();
    QueryStyle queryStyle = QueryStyle.TRAVERSAL;
    ModelType introspectorFactoryType = ModelType.MOXY;
    Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
    TransactionalGraphEngine engine = null;
    try {
        engine = new JanusGraphDBEngine(queryStyle, loader);
        SchemaModInternalBatch internal = new SchemaModInternalBatch(engine, logger, propName, targetDataType, targetIndexInfo, Boolean.parseBoolean(preserveDataFlag), Boolean.parseBoolean(consistencyLockFlag), commitBlockSize);
        internal.execute();
        engine.startTransaction();
        engine.tx().close();
        logAndPrint(logger, "------ Completed the SchemaMod -------- ");
    } catch (Exception e) {
        String emsg = "Not able to complete the requested SchemaMod \n";
        logAndPrint(logger, e.getMessage());
        logAndPrint(logger, emsg);
        System.exit(1);
    }
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) QueryStyle(org.onap.aai.serialization.engines.QueryStyle) AAIException(org.onap.aai.exceptions.AAIException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) JanusGraphDBEngine(org.onap.aai.serialization.engines.JanusGraphDBEngine) ModelType(org.onap.aai.introspection.ModelType)

Example 2 with SchemaVersion

use of org.onap.aai.setup.SchemaVersion in project aai-graphadmin by onap.

the class SchemaMod4Hist method execute.

public void execute(String[] args) {
    // Set the logging file properties to be used by EELFManager
    Properties props = System.getProperties();
    props.setProperty(Configuration.PROPERTY_LOGGING_FILE_NAME, AAIConstants.AAI_SCHEMA_MOD_LOGBACK_PROPS);
    props.setProperty(Configuration.PROPERTY_LOGGING_FILE_PATH, AAIConstants.AAI_HOME_BUNDLECONFIG);
    Logger logger = LoggerFactory.getLogger(SchemaMod4Hist.class.getSimpleName());
    MDC.put("logFilenameAppender", SchemaMod4Hist.class.getSimpleName());
    // NOTE -- We're just working with properties that are used for NODES
    // for now.
    String propName = "";
    String targetDataType = "";
    String targetIndexInfo = "";
    String preserveDataFlag = "";
    String usageString = "Usage: SchemaMod4Hist propertyName targetDataType targetIndexInfo preserveDataFlag \n";
    if (args.length != 4) {
        String emsg = "Four Parameters are required.  \n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    } else {
        propName = args[0];
        targetDataType = args[1];
        targetIndexInfo = args[2];
        // Note - even if they pass in "false", History will preserve the data
        preserveDataFlag = args[3];
    }
    if (propName.equals("")) {
        String emsg = "Bad parameter - propertyName cannot be empty.  \n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    } else if (!targetDataType.equals("String") && !targetDataType.equals("Set<String>") && !targetDataType.equals("Integer") && !targetDataType.equals("Long") && !targetDataType.equals("Boolean")) {
        String emsg = "Unsupported targetDataType.  We only support String, Set<String>, Integer, Long or Boolean for now.\n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    } else if (!targetIndexInfo.equals("index") && !targetIndexInfo.equals("noIndex")) {
        String emsg = "Unsupported IndexInfo.  We only support: 'index' or 'noIndex' for History db.\n" + usageString;
        logAndPrint(logger, emsg);
        System.exit(1);
    }
    try {
        AAIConfig.init();
        ErrorLogHelper.loadProperties();
    } catch (Exception ae) {
        String emsg = "Problem with either AAIConfig.init() or ErrorLogHelper.LoadProperties(). ";
        logAndPrint(logger, emsg + "[" + ae.getMessage() + "]");
        System.exit(1);
    }
    // Give a big warning if the DbMaps.PropertyDataTypeMap value does not
    // agree with what we're doing
    String warningMsg = "";
    if (!warningMsg.equals("")) {
        logAndPrint(logger, "\n>>> WARNING <<<< ");
        logAndPrint(logger, ">>> " + warningMsg + " <<<");
    }
    logAndPrint(logger, ">>> Processing will begin in 5 seconds (unless interrupted). <<<");
    try {
        // Give them a chance to back out of this
        Thread.sleep(5000);
    } catch (java.lang.InterruptedException ie) {
        logAndPrint(logger, " DB Schema Update has been aborted. ");
        System.exit(1);
    }
    logAndPrint(logger, "    ---- NOTE --- about to open graph (takes a little while)\n");
    SchemaVersion version = schemaVersions.getDefaultVersion();
    QueryStyle queryStyle = QueryStyle.TRAVERSAL;
    ModelType introspectorFactoryType = ModelType.MOXY;
    Loader loader = loaderFactory.createLoaderForVersion(introspectorFactoryType, version);
    TransactionalGraphEngine engine = null;
    try {
        engine = new JanusGraphDBEngine(queryStyle, loader);
        // NOTE - no matter what they passed in, we're passing in "true" for the
        // preserve-data-flag last parameter since this is for HISTORY.
        SchemaModInternal4Hist internal = new SchemaModInternal4Hist(engine, logger, propName, targetDataType, targetIndexInfo, true);
        internal.execute();
        engine.startTransaction();
        engine.tx().close();
        logAndPrint(logger, "------ Completed the SchemaMod -------- ");
    } catch (Exception e) {
        String emsg = "Not able to complete the requested SchemaMod4Hist \n";
        logAndPrint(logger, e.getMessage());
        logAndPrint(logger, emsg);
        System.exit(1);
    }
}
Also used : SchemaVersion(org.onap.aai.setup.SchemaVersion) Loader(org.onap.aai.introspection.Loader) QueryStyle(org.onap.aai.serialization.engines.QueryStyle) Properties(java.util.Properties) Logger(org.slf4j.Logger) AAIException(org.onap.aai.exceptions.AAIException) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) JanusGraphDBEngine(org.onap.aai.serialization.engines.JanusGraphDBEngine) ModelType(org.onap.aai.introspection.ModelType)

Example 3 with SchemaVersion

use of org.onap.aai.setup.SchemaVersion in project aai-graphadmin by onap.

the class QueryConsumer method processExecuteQuery.

public Response processExecuteQuery(String content, @PathParam("version") String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("graphson") @QueryParam("format") String queryFormat, @DefaultValue("no_op") @QueryParam("subgraph") String subgraph, @Context HttpHeaders headers, @Context UriInfo info, @Context HttpServletRequest req) {
    String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId");
    String queryProcessor = headers.getRequestHeaders().getFirst("QueryProcessor");
    QueryProcessorType processorType = this.processorType;
    Response response = null;
    TransactionalGraphEngine dbEngine = null;
    try {
        this.checkQueryParams(info.getQueryParameters());
        Format format = Format.getFormat(queryFormat);
        if (queryProcessor != null) {
            processorType = QueryProcessorType.valueOf(queryProcessor);
        }
        SubGraphStyle subGraphStyle = SubGraphStyle.valueOf(subgraph);
        JsonParser parser = new JsonParser();
        JsonObject input = parser.parse(content).getAsJsonObject();
        JsonElement gremlinElement = input.get("gremlin");
        JsonElement dslElement = input.get("dsl");
        String gremlin = "";
        String dsl = "";
        SchemaVersion version = new SchemaVersion(versionParam);
        traversalUriHttpEntry.setHttpEntryProperties(version);
        dbEngine = traversalUriHttpEntry.getDbEngine();
        if (gremlinElement != null) {
            gremlin = gremlinElement.getAsString();
        }
        if (dslElement != null) {
            dsl = dslElement.getAsString();
        }
        GenericQueryProcessor processor;
        StopWatch.conditionalStart();
        if (!dsl.equals("")) {
            processor = new GenericQueryProcessor.Builder(dbEngine).queryFrom(dsl, "dsl").queryProcessor(dslQueryProcessor).processWith(processorType).create();
        } else {
            processor = new GenericQueryProcessor.Builder(dbEngine).queryFrom(gremlin, "gremlin").processWith(processorType).create();
        }
        String result = "";
        List<Object> vertices = processor.execute(subGraphStyle);
        DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth);
        FormatFactory ff = new FormatFactory(traversalUriHttpEntry.getLoader(), serializer, schemaVersions, basePath);
        Formatter formater = ff.get(format, info.getQueryParameters());
        result = formater.output(vertices).toString();
        LOGGER.info("Completed");
        response = Response.status(Status.OK).type(MediaType.APPLICATION_JSON).entity(result).build();
    } catch (AAIException e) {
        response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, e);
    } catch (Exception e) {
        AAIException ex = new AAIException("AAI_4000", e);
        response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, ex);
    } finally {
        if (dbEngine != null) {
            dbEngine.rollback();
        }
    }
    return response;
}
Also used : GenericQueryProcessor(org.onap.aai.rest.search.GenericQueryProcessor) FormatFactory(org.onap.aai.serialization.queryformats.FormatFactory) DBSerializer(org.onap.aai.serialization.db.DBSerializer) SchemaVersion(org.onap.aai.setup.SchemaVersion) Formatter(org.onap.aai.serialization.queryformats.Formatter) JsonObject(com.google.gson.JsonObject) AAIException(org.onap.aai.exceptions.AAIException) QueryProcessorType(org.onap.aai.rest.search.QueryProcessorType) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) Format(org.onap.aai.serialization.queryformats.Format) JsonElement(com.google.gson.JsonElement) AAIException(org.onap.aai.exceptions.AAIException) JsonObject(com.google.gson.JsonObject) SubGraphStyle(org.onap.aai.serialization.queryformats.SubGraphStyle) JsonParser(com.google.gson.JsonParser)

Example 4 with SchemaVersion

use of org.onap.aai.setup.SchemaVersion 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 5 with SchemaVersion

use of org.onap.aai.setup.SchemaVersion 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)

Aggregations

SchemaVersion (org.onap.aai.setup.SchemaVersion)78 Test (org.junit.Test)32 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)25 ArrayList (java.util.ArrayList)19 List (java.util.List)16 Loader (org.onap.aai.introspection.Loader)13 TreeMap (java.util.TreeMap)12 AAIException (org.onap.aai.exceptions.AAIException)9 TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)8 Introspector (org.onap.aai.introspection.Introspector)7 JanusGraphDBEngine (org.onap.aai.serialization.engines.JanusGraphDBEngine)7 URI (java.net.URI)6 DynamicJAXBContext (org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext)6 QueryParser (org.onap.aai.parsers.query.QueryParser)5 IOException (java.io.IOException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 Before (org.junit.Before)4 ModelType (org.onap.aai.introspection.ModelType)4 URIToObject (org.onap.aai.parsers.uri.URIToObject)4 DBRequest (org.onap.aai.rest.db.DBRequest)4