Search in sources :

Example 1 with URIToObject

use of org.onap.aai.parsers.uri.URIToObject in project aai-graphadmin by onap.

the class CommandLineArgs method createNotificationEvent.

public void createNotificationEvent(String transactionId, String sourceOfTruth, URI uri, Introspector obj, Map<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
    String action = "CREATE";
    final Introspector notificationEvent = loader.introspectorFromName("notification-event");
    try {
        Introspector eventHeader = loader.introspectorFromName("notification-event-header");
        URIToObject parser = new URIToObject(loader, uri, (HashMap) relatedObjects);
        String entityLink = urlBase + version + uri;
        notificationEvent.setValue("cambria-partition", "AAI");
        eventHeader.setValue("entity-link", entityLink);
        eventHeader.setValue("action", action);
        eventHeader.setValue("entity-type", obj.getDbName());
        eventHeader.setValue("top-entity-type", parser.getTopEntityName());
        eventHeader.setValue("source-name", sourceOfTruth);
        eventHeader.setValue("version", version.toString());
        eventHeader.setValue("id", transactionId);
        eventHeader.setValue("event-type", "AAI-BASELINE");
        if (eventHeader.getValue("domain") == null) {
            eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
        }
        if (eventHeader.getValue("sequence-number") == null) {
            eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
        }
        if (eventHeader.getValue("severity") == null) {
            eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
        }
        if (eventHeader.getValue("id") == null) {
            eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
        }
        if (eventHeader.getValue("timestamp") == null) {
            eventHeader.setValue("timestamp", genDate());
        }
        List<Object> parentList = parser.getParentList();
        parentList.clear();
        if (!parser.getTopEntity().equals(parser.getEntity())) {
            Introspector child;
            String json = obj.marshal(false);
            child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
            parentList.add(child.getUnderlyingObject());
        }
        final Introspector eventObject;
        String json = "";
        if (parser.getTopEntity().equals(parser.getEntity())) {
            json = obj.marshal(false);
            eventObject = loader.unmarshal(obj.getName(), json);
        } else {
            json = parser.getTopEntity().marshal(false);
            eventObject = loader.unmarshal(parser.getTopEntity().getName(), json);
        }
        notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
        notificationEvent.setValue("entity", eventObject.getUnderlyingObject());
        String entityJson = notificationEvent.marshal(false);
        bw.newLine();
        bw.write(entityJson);
    } catch (AAIUnknownObjectException e) {
        LOGGER.error("Fatal error - notification-event-header object not found!");
    } catch (Exception e) {
        LOGGER.error("Unmarshalling error occurred while generating Notification " + LogFormatTools.getStackTop(e));
    }
}
Also used : URIToObject(org.onap.aai.parsers.uri.URIToObject) AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) Introspector(org.onap.aai.introspection.Introspector) URIToObject(org.onap.aai.parsers.uri.URIToObject) EdgeRuleNotFoundException(org.onap.aai.edges.exceptions.EdgeRuleNotFoundException) AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) AmbiguousRuleChoiceException(org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException) InvalidPathException(java.nio.file.InvalidPathException) AAIException(org.onap.aai.exceptions.AAIException)

Example 2 with URIToObject

use of org.onap.aai.parsers.uri.URIToObject 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 3 with URIToObject

use of org.onap.aai.parsers.uri.URIToObject 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 4 with URIToObject

use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.

the class InjectKeysFromURI method resolveIssue.

/**
 * {@inheritDoc}
 */
@Override
public boolean resolveIssue(Issue issue) {
    boolean result = false;
    Introspector obj = issue.getIntrospector();
    if (issue.getType().equals(IssueType.MISSING_KEY_PROP)) {
        try {
            URIToObject toObject = new URIToObject(loader, uri);
            Introspector minimumObj = toObject.getEntity();
            if (toObject.getEntityName().equals(obj.getDbName())) {
                obj.setValue(issue.getPropName(), minimumObj.getValue(issue.getPropName()));
                result = true;
            }
        } catch (Exception e) {
            // log something probably
            result = false;
        }
    }
    return result;
}
Also used : URIToObject(org.onap.aai.parsers.uri.URIToObject) Introspector(org.onap.aai.introspection.Introspector)

Example 5 with URIToObject

use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.

the class DataLinkWriter method processURI.

@Override
protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException {
    if (completeUri.isPresent()) {
        URI uri = new URI(completeUri.get());
        MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
        QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
        List<Vertex> results = uriQuery.getQueryBuilder().toList();
        if (results.size() == 1) {
            if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
                // delete vertex because property was removed
                serializer.delete(results.get(0), "", false);
            } else {
                // link vertex that already exists
                this.addLinkedProperty(results.get(0));
            }
        } else {
            if (results.isEmpty()) {
                // locate previously linked vertex
                List<Vertex> linkedVertices = uriQuery.getQueryBuilder().getContainerQuery().getVerticesByProperty(AAIProperties.LINKED, true).toList();
                if (!linkedVertices.isEmpty()) {
                    if (linkedVertices.size() > 1) {
                        throw new AAIMultiplePropertiesException("multiple vertices found for single cardinality propery found when searching " + uri);
                    } else {
                        // found one, remove the linked property because it didn't match the uri
                        linkedVertices.get(0).property(AAIProperties.LINKED).remove();
                    }
                }
                if (obj.getValue(entry.getKey()) != null) {
                    // add new vertex to database if we have values
                    URIToObject parser = new URIToObject(this.latestLoader, uri);
                    Introspector resultObj = parser.getEntity();
                    Vertex newV = serializer.createNewVertex(resultObj);
                    serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(), this.latestLoader.getVersion().toString());
                    this.addLinkedProperty(newV);
                }
            } else if (results.size() > 1) {
                throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
            }
        }
    } else {
    // skip processing because no required properties were specified
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) QueryParser(org.onap.aai.parsers.query.QueryParser) URIToObject(org.onap.aai.parsers.uri.URIToObject) Introspector(org.onap.aai.introspection.Introspector) AAIMultiplePropertiesException(org.onap.aai.introspection.sideeffect.exceptions.AAIMultiplePropertiesException) URI(java.net.URI)

Aggregations

Introspector (org.onap.aai.introspection.Introspector)10 URIToObject (org.onap.aai.parsers.uri.URIToObject)10 AAIException (org.onap.aai.exceptions.AAIException)7 URI (java.net.URI)6 QueryParser (org.onap.aai.parsers.query.QueryParser)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Loader (org.onap.aai.introspection.Loader)4 DBRequest (org.onap.aai.rest.db.DBRequest)4 TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)4 SchemaVersion (org.onap.aai.setup.SchemaVersion)4 HttpMethod (org.onap.aai.restcore.HttpMethod)3 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)2 Pair (org.javatuples.Pair)2 EdgeRuleNotFoundException (org.onap.aai.edges.exceptions.EdgeRuleNotFoundException)2 AAIUnknownObjectException (org.onap.aai.introspection.exceptions.AAIUnknownObjectException)2 QueryBuilder (org.onap.aai.query.builder.QueryBuilder)2 URISyntaxException (java.net.URISyntaxException)1 InvalidPathException (java.nio.file.InvalidPathException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1