Search in sources :

Example 1 with HttpMethod

use of org.onap.aai.restcore.HttpMethod 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 HttpMethod

use of org.onap.aai.restcore.HttpMethod 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 HttpMethod

use of org.onap.aai.restcore.HttpMethod in project aai-aai-common by onap.

the class HttpEntry method process.

private Pair<Boolean, List<Pair<URI, Response>>> process(List<DBRequest> requests, String sourceOfTruth, Set<String> groups, boolean enableResourceVersion) throws AAIException {
    DBSerializer serializer = null;
    if (serverBase != null) {
        serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth, groups, notificationDepth, serverBase);
    } else {
        serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth, groups, notificationDepth);
    }
    Response response;
    Introspector obj;
    QueryParser query;
    URI uri;
    String transactionId = null;
    int depth;
    Format format = null;
    List<Pair<URI, Response>> responses = new ArrayList<>();
    MultivaluedMap<String, String> params;
    HttpMethod method;
    String uriTemp;
    boolean success = true;
    QueryEngine queryEngine = dbEngine.getQueryEngine();
    Set<Vertex> mainVertexesToNotifyOn = new LinkedHashSet<>();
    AaiDBMetricLog metricLog = new AaiDBMetricLog(AAIConstants.AAI_RESOURCES_MS);
    String outputMediaType = null;
    if (requests != null && !requests.isEmpty()) {
        HttpHeaders headers = requests.get(0).getHeaders();
        outputMediaType = getMediaType(headers.getAcceptableMediaTypes());
    }
    for (DBRequest request : requests) {
        response = null;
        Status status = Status.NOT_FOUND;
        method = request.getMethod();
        metricLog.pre(request);
        try {
            try {
                obj = request.getIntrospector();
                query = request.getParser();
                transactionId = request.getTransactionId();
                uriTemp = request.getUri().getRawPath().replaceFirst("^v\\d+/", "");
                uri = UriBuilder.fromPath(uriTemp).build();
                boolean groupsAvailable = serializer.getGroups() != null && !serializer.getGroups().isEmpty();
                List<Vertex> queryResult = query.getQueryBuilder().toList();
                List<Vertex> vertices;
                if (this.isPaginated()) {
                    List<Vertex> vertTemp = groupsAvailable ? queryResult.stream().filter((vx) -> {
                        return OwnerCheck.isAuthorized(groups, vx);
                    }).collect(Collectors.toList()) : queryResult;
                    this.setTotalsForPaging(vertTemp.size(), this.paginationBucket);
                    vertices = vertTemp.subList(((this.paginationIndex - 1) * this.paginationBucket), Math.min((this.paginationBucket * this.paginationIndex), vertTemp.size()));
                } else {
                    vertices = groupsAvailable && queryResult.size() > 1 ? queryResult.stream().filter((vx) -> {
                        return OwnerCheck.isAuthorized(groups, vx);
                    }).collect(Collectors.toList()) : queryResult;
                }
                boolean isNewVertex;
                HttpHeaders headers = request.getHeaders();
                outputMediaType = getMediaType(headers.getAcceptableMediaTypes());
                String result = null;
                params = request.getInfo().getQueryParameters(false);
                depth = setDepth(obj, params.getFirst("depth"));
                if (params.containsKey("format")) {
                    format = Format.getFormat(params.getFirst("format"));
                }
                String cleanUp = params.getFirst("cleanup");
                String requestContext = "";
                List<String> requestContextList = request.getHeaders().getRequestHeader("aai-request-context");
                if (requestContextList != null) {
                    requestContext = requestContextList.get(0);
                }
                if (cleanUp == null) {
                    cleanUp = "false";
                }
                if (vertices.size() > 1 && processSingle && !(method.equals(HttpMethod.GET) || method.equals(HttpMethod.GET_RELATIONSHIP))) {
                    if (method.equals(HttpMethod.DELETE)) {
                        throw new AAIException("AAI_6138");
                    } else {
                        throw new AAIException("AAI_6137");
                    }
                }
                if (method.equals(HttpMethod.PUT)) {
                    String resourceVersion = obj.getValue(AAIProperties.RESOURCE_VERSION);
                    if (vertices.isEmpty()) {
                        if (enableResourceVersion) {
                            serializer.verifyResourceVersion("create", query.getResultType(), "", resourceVersion, obj.getURI());
                        }
                        isNewVertex = true;
                    } else {
                        if (enableResourceVersion) {
                            serializer.verifyResourceVersion("update", query.getResultType(), vertices.get(0).<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), resourceVersion, obj.getURI());
                        }
                        isNewVertex = false;
                    }
                } else {
                    if (vertices.isEmpty()) {
                        String msg = createNotFoundMessage(query.getResultType(), request.getUri());
                        throw new AAIException("AAI_6114", msg);
                    } else {
                        isNewVertex = false;
                    }
                }
                Vertex v = null;
                if (!isNewVertex) {
                    v = vertices.get(0);
                }
                /*
                     * This skip-related-to query parameter is used to determine if the relationships object will omit the related-to-property
                     * If a GET is sent to resources without a format, if format=resource, or if format=resource_and_url with this param set to false
                     * then behavior will be keep the related-to properties. By default, set to true.
                     * Otherwise, for any other case, when the skip-related-to parameter exists, has value=true, or some unfamiliar input (e.g. skip-related-to=bogusvalue), the value is true.
                     */
                boolean isSkipRelatedTo = true;
                if (params.containsKey("skip-related-to")) {
                    String skipRelatedTo = params.getFirst("skip-related-to");
                    isSkipRelatedTo = !(skipRelatedTo != null && skipRelatedTo.equals("false"));
                } else {
                    // if skip-related-to param is missing, then default it to false;
                    isSkipRelatedTo = false;
                }
                HashMap<String, Introspector> relatedObjects = new HashMap<>();
                String nodeOnly = params.getFirst("nodes-only");
                boolean isNodeOnly = nodeOnly != null;
                switch(method) {
                    case GET:
                        if (format == null) {
                            obj = this.getObjectFromDb(vertices, serializer, query, obj, request.getUri(), depth, isNodeOnly, cleanUp, isSkipRelatedTo);
                            if (obj != null) {
                                status = Status.OK;
                                MarshallerProperties properties;
                                Optional<MarshallerProperties> marshallerPropOpt = request.getMarshallerProperties();
                                if (marshallerPropOpt.isPresent()) {
                                    properties = marshallerPropOpt.get();
                                } else {
                                    properties = new MarshallerProperties.Builder(org.onap.aai.restcore.MediaType.getEnum(outputMediaType)).build();
                                }
                                result = obj.marshal(properties);
                            }
                        } else {
                            FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath + "/", serverBase);
                            Formatter formatter = ff.get(format, params);
                            result = formatter.output(vertices.stream().map(vertex -> (Object) vertex).collect(Collectors.toList())).toString();
                            if (outputMediaType == null) {
                                outputMediaType = MediaType.APPLICATION_JSON;
                            }
                            if (MediaType.APPLICATION_XML_TYPE.isCompatible(MediaType.valueOf(outputMediaType))) {
                                result = xmlFormatTransformer.transform(result);
                            }
                            status = Status.OK;
                        }
                        break;
                    case GET_RELATIONSHIP:
                        if (format == null) {
                            obj = this.getRelationshipObjectFromDb(vertices, serializer, query, request.getInfo().getRequestUri(), isSkipRelatedTo);
                            if (obj != null) {
                                status = Status.OK;
                                MarshallerProperties properties;
                                if (!request.getMarshallerProperties().isPresent()) {
                                    properties = new MarshallerProperties.Builder(org.onap.aai.restcore.MediaType.getEnum(outputMediaType)).build();
                                } else {
                                    properties = request.getMarshallerProperties().get();
                                }
                                result = obj.marshal(properties);
                            } else {
                                String msg = createRelationshipNotFoundMessage(query.getResultType(), request.getUri());
                                throw new AAIException("AAI_6149", msg);
                            }
                        } else {
                            FormatFactory ff = new FormatFactory(loader, serializer, schemaVersions, basePath + "/", serverBase);
                            Formatter formatter = ff.get(format, params);
                            result = formatter.output(vertices.stream().map(vertex -> (Object) vertex).collect(Collectors.toList())).toString();
                            if (outputMediaType == null) {
                                outputMediaType = MediaType.APPLICATION_JSON;
                            }
                            if (MediaType.APPLICATION_XML_TYPE.isCompatible(MediaType.valueOf(outputMediaType))) {
                                result = xmlFormatTransformer.transform(result);
                            }
                            status = Status.OK;
                        }
                        break;
                    case PUT:
                        if (isNewVertex) {
                            v = serializer.createNewVertex(obj);
                        }
                        serializer.serializeToDb(obj, v, query, uri.getRawPath(), requestContext);
                        status = Status.OK;
                        if (isNewVertex) {
                            status = Status.CREATED;
                        }
                        mainVertexesToNotifyOn.add(v);
                        if (notificationDepth == AAIProperties.MINIMUM_DEPTH) {
                            Map<String, Pair<Introspector, LinkedHashMap<String, Introspector>>> allImpliedDeleteObjs = serializer.getImpliedDeleteUriObjectPair();
                            for (Map.Entry<String, Pair<Introspector, LinkedHashMap<String, Introspector>>> entry : allImpliedDeleteObjs.entrySet()) {
                                // The format is purposefully %s/%s%s due to the fact
                                // that every aai-uri will have a slash at the beginning
                                // If that assumption isn't true, then its best to change this code
                                String curUri = String.format("%s/%s%s", basePath, version, entry.getKey());
                                Introspector curObj = entry.getValue().getValue0();
                                HashMap<String, Introspector> curObjRelated = entry.getValue().getValue1();
                                notification.createNotificationEvent(transactionId, sourceOfTruth, Status.NO_CONTENT, URI.create(curUri), curObj, curObjRelated, basePath);
                            }
                        }
                        break;
                    case PUT_EDGE:
                        serializer.touchStandardVertexProperties(v, false);
                        Vertex relatedVertex = serializer.createEdge(obj, v);
                        status = Status.OK;
                        mainVertexesToNotifyOn.add(v);
                        serializer.addVertexToEdgeVertexes(relatedVertex);
                        break;
                    case MERGE_PATCH:
                        Introspector existingObj = loader.introspectorFromName(obj.getDbName());
                        existingObj = this.getObjectFromDb(vertices, serializer, query, existingObj, request.getUri(), 0, false, cleanUp);
                        String existingJson = existingObj.marshal(false);
                        String newJson;
                        if (request.getRawRequestContent().isPresent()) {
                            newJson = request.getRawRequestContent().get();
                        } else {
                            newJson = "";
                        }
                        Object relationshipList = request.getIntrospector().getValue("relationship-list");
                        ObjectMapper mapper = new ObjectMapper();
                        try {
                            JsonNode existingNode = mapper.readTree(existingJson);
                            JsonNode newNode = mapper.readTree(newJson);
                            JsonMergePatch patch = JsonMergePatch.fromJson(newNode);
                            JsonNode completed = patch.apply(existingNode);
                            String patched = mapper.writeValueAsString(completed);
                            Introspector patchedObj = loader.unmarshal(existingObj.getName(), patched);
                            if (relationshipList == null && patchedObj.hasProperty("relationship-list")) {
                                // if the caller didn't touch the relationship-list, we shouldn't either
                                patchedObj.setValue("relationship-list", null);
                            }
                            serializer.serializeToDb(patchedObj, v, query, uri.getRawPath(), requestContext);
                            status = Status.OK;
                            mainVertexesToNotifyOn.add(v);
                        } catch (IOException | JsonPatchException e) {
                            throw new AAIException("AAI_3000", "could not perform patch operation");
                        }
                        break;
                    case DELETE:
                        String resourceVersion = params.getFirst(AAIProperties.RESOURCE_VERSION);
                        obj = serializer.getLatestVersionView(v, notificationDepth);
                        if (query.isDependent()) {
                            relatedObjects = serializer.getRelatedObjects(queryEngine, v, obj, this.loader);
                        }
                        /*
                             * Find all Delete-other-vertex vertices and create structure for notify
                             * findDeleatble also returns the startVertex v and we dont want to create
                             * duplicate notification events for the same
                             * So remove the startvertex first
                             */
                        List<Vertex> deletableVertices = dbEngine.getQueryEngine().findDeletable(v);
                        Object vId = v.id();
                        /*
                             * I am assuming vertexId cant be null
                             */
                        deletableVertices.removeIf(s -> vId.equals(s.id()));
                        boolean isDelVerticesPresent = !deletableVertices.isEmpty();
                        Map<Vertex, Introspector> deleteObjects = new HashMap<>();
                        Map<String, URI> uriMap = new HashMap<>();
                        Map<String, HashMap<String, Introspector>> deleteRelatedObjects = new HashMap<>();
                        if (isDelVerticesPresent) {
                            deleteObjects = this.buildIntrospectorObjects(serializer, deletableVertices);
                            uriMap = this.buildURIMap(serializer, deleteObjects);
                            deleteRelatedObjects = this.buildRelatedObjects(serializer, queryEngine, deleteObjects);
                        }
                        serializer.delete(v, deletableVertices, resourceVersion, enableResourceVersion);
                        status = Status.NO_CONTENT;
                        notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects, basePath);
                        if (isDelVerticesPresent) {
                            this.buildNotificationEvent(sourceOfTruth, status, transactionId, notification, deleteObjects, uriMap, deleteRelatedObjects, basePath);
                        }
                        break;
                    case DELETE_EDGE:
                        serializer.touchStandardVertexProperties(v, false);
                        Optional<Vertex> otherV = serializer.deleteEdge(obj, v);
                        status = Status.NO_CONTENT;
                        if (otherV.isPresent()) {
                            mainVertexesToNotifyOn.add(v);
                            serializer.addVertexToEdgeVertexes(otherV.get());
                        }
                        break;
                    default:
                        break;
                }
                /*
                     * temporarily adding vertex id to the headers
                     * to be able to use for testing the vertex id endpoint functionality
                     * since we presently have no other way of generating those id urls
                     */
                if (response == null && v != null && (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.GET) || method.equals(HttpMethod.MERGE_PATCH) || method.equals(HttpMethod.GET_RELATIONSHIP))) {
                    String myvertid = v.id().toString();
                    if (this.isPaginated()) {
                        response = Response.status(status).header("vertex-id", myvertid).header("total-results", this.getTotalVertices()).header("total-pages", this.getTotalPaginationBuckets()).entity(result).type(outputMediaType).build();
                    } else {
                        response = Response.status(status).header("vertex-id", myvertid).entity(result).type(outputMediaType).build();
                    }
                } else if (response == null) {
                    response = Response.status(status).type(outputMediaType).build();
                }
                // else, response already set to something
                Pair<URI, Response> pairedResp = Pair.with(request.getUri(), response);
                responses.add(pairedResp);
            } catch (JanusGraphException e) {
                this.dbEngine.rollback();
                throw new AAIException("AAI_6134", e);
            }
        } catch (AAIException e) {
            success = false;
            ArrayList<String> templateVars = new ArrayList<>();
            // GET, PUT, etc
            templateVars.add(request.getMethod().toString());
            templateVars.add(request.getUri().getPath());
            templateVars.addAll(e.getTemplateVars());
            ErrorLogHelper.logException(e);
            response = Response.status(e.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), e, templateVars)).type(outputMediaType).build();
            Pair<URI, Response> pairedResp = Pair.with(request.getUri(), response);
            responses.add(pairedResp);
        } catch (Exception e) {
            success = false;
            AAIException ex = new AAIException("AAI_4000", e);
            ArrayList<String> templateVars = new ArrayList<>();
            // GET, PUT, etc
            templateVars.add(request.getMethod().toString());
            templateVars.add(request.getUri().getPath());
            ErrorLogHelper.logException(ex);
            response = Response.status(ex.getErrorObject().getHTTPResponseCode()).entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), ex, templateVars)).type(outputMediaType).build();
            Pair<URI, Response> pairedResp = Pair.with(request.getUri(), response);
            responses.add(pairedResp);
        } finally {
            if (response != null) {
                metricLog.post(request, response);
            }
        }
    }
    if (success) {
        generateEvents(sourceOfTruth, serializer, transactionId, queryEngine, mainVertexesToNotifyOn);
    } else {
        notification.clearEvents();
    }
    return Pair.with(success, responses);
}
Also used : Format(org.onap.aai.serialization.queryformats.Format) org.onap.aai.introspection(org.onap.aai.introspection) AAIProperties(org.onap.aai.db.props.AAIProperties) QueryParser(org.onap.aai.parsers.query.QueryParser) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) ErrorLogHelper(org.onap.aai.logging.ErrorLogHelper) ObjectMetadata(org.onap.aai.schema.enums.ObjectMetadata) Autowired(org.springframework.beans.factory.annotation.Autowired) QueryEngine(org.onap.aai.serialization.engines.query.QueryEngine) FormatFactory(org.onap.aai.serialization.queryformats.FormatFactory) AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) JsonNode(com.fasterxml.jackson.databind.JsonNode) SchemaVersion(org.onap.aai.setup.SchemaVersion) URI(java.net.URI) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) JanusGraphDBEngine(org.onap.aai.serialization.engines.JanusGraphDBEngine) UEBNotification(org.onap.aai.rest.ueb.UEBNotification) OwnerCheck(org.onap.aai.introspection.sideeffect.OwnerCheck) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Collectors(java.util.stream.Collectors) Pair(org.javatuples.Pair) InvocationTargetException(java.lang.reflect.InvocationTargetException) Formatter(org.onap.aai.serialization.queryformats.Formatter) UnsupportedEncodingException(java.io.UnsupportedEncodingException) java.util(java.util) AAIConfig(org.onap.aai.util.AAIConfig) NodeIngestor(org.onap.aai.nodes.NodeIngestor) AaiDBMetricLog(org.onap.aai.aailog.logs.AaiDBMetricLog) Value(org.springframework.beans.factory.annotation.Value) ValidationService(org.onap.aai.prevalidation.ValidationService) SchemaVersions(org.onap.aai.setup.SchemaVersions) AAIConstants(org.onap.aai.util.AAIConstants) TransactionalGraphEngine(org.onap.aai.serialization.engines.TransactionalGraphEngine) JanusGraphException(org.janusgraph.core.JanusGraphException) Status(javax.ws.rs.core.Response.Status) XmlFormatTransformer(org.onap.aai.transforms.XmlFormatTransformer) Logger(org.slf4j.Logger) DeltaEvents(org.onap.aai.util.delta.DeltaEvents) MalformedURLException(java.net.MalformedURLException) QueryStyle(org.onap.aai.serialization.engines.QueryStyle) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) IOException(java.io.IOException) HttpMethod(org.onap.aai.restcore.HttpMethod) javax.ws.rs.core(javax.ws.rs.core) DBSerializer(org.onap.aai.serialization.db.DBSerializer) ResponseEntity(org.springframework.http.ResponseEntity) AAIException(org.onap.aai.exceptions.AAIException) JsonNode(com.fasterxml.jackson.databind.JsonNode) QueryEngine(org.onap.aai.serialization.engines.query.QueryEngine) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Status(javax.ws.rs.core.Response.Status) JsonMergePatch(com.github.fge.jsonpatch.mergepatch.JsonMergePatch) HttpMethod(org.onap.aai.restcore.HttpMethod) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Formatter(org.onap.aai.serialization.queryformats.Formatter) JanusGraphException(org.janusgraph.core.JanusGraphException) URI(java.net.URI) Format(org.onap.aai.serialization.queryformats.Format) Pair(org.javatuples.Pair) FormatFactory(org.onap.aai.serialization.queryformats.FormatFactory) DBSerializer(org.onap.aai.serialization.db.DBSerializer) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) AAIUnknownObjectException(org.onap.aai.introspection.exceptions.AAIUnknownObjectException) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JanusGraphException(org.janusgraph.core.JanusGraphException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) AAIException(org.onap.aai.exceptions.AAIException) QueryParser(org.onap.aai.parsers.query.QueryParser) JsonPatchException(com.github.fge.jsonpatch.JsonPatchException) AaiDBMetricLog(org.onap.aai.aailog.logs.AaiDBMetricLog) AAIException(org.onap.aai.exceptions.AAIException)

Example 4 with HttpMethod

use of org.onap.aai.restcore.HttpMethod 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)

Aggregations

UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 URI (java.net.URI)4 AAIException (org.onap.aai.exceptions.AAIException)4 QueryParser (org.onap.aai.parsers.query.QueryParser)4 HttpMethod (org.onap.aai.restcore.HttpMethod)4 TransactionalGraphEngine (org.onap.aai.serialization.engines.TransactionalGraphEngine)4 SchemaVersion (org.onap.aai.setup.SchemaVersion)4 Introspector (org.onap.aai.introspection.Introspector)3 Loader (org.onap.aai.introspection.Loader)3 URIToObject (org.onap.aai.parsers.uri.URIToObject)3 DBRequest (org.onap.aai.rest.db.DBRequest)3 Pair (org.javatuples.Pair)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonPatchException (com.github.fge.jsonpatch.JsonPatchException)1 JsonMergePatch (com.github.fge.jsonpatch.mergepatch.JsonMergePatch)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1