Search in sources :

Example 21 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class TestEntityREST method createTestEntity.

private void createTestEntity() throws Exception {
    AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    final EntityMutationResponse response = entityREST.createOrUpdate(new AtlasEntitiesWithExtInfo(dbEntity));
    Assert.assertNotNull(response);
    List<AtlasEntityHeader> entitiesMutated = response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
    Assert.assertNotNull(entitiesMutated);
    Assert.assertEquals(entitiesMutated.size(), 1);
    Assert.assertNotNull(entitiesMutated.get(0));
    dbEntity.setGuid(entitiesMutated.get(0).getGuid());
    this.dbEntity = dbEntity;
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader)

Example 22 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class AtlasInstanceConverter method toEntityMutationResponse.

public static EntityMutationResponse toEntityMutationResponse(CreateUpdateEntitiesResult result) {
    EntityMutationResponse response = new EntityMutationResponse();
    for (String guid : result.getCreatedEntities()) {
        AtlasEntityHeader header = new AtlasEntityHeader();
        header.setGuid(guid);
        response.addEntity(EntityMutations.EntityOperation.CREATE, header);
    }
    for (String guid : result.getUpdatedEntities()) {
        AtlasEntityHeader header = new AtlasEntityHeader();
        header.setGuid(guid);
        response.addEntity(EntityMutations.EntityOperation.UPDATE, header);
    }
    for (String guid : result.getDeletedEntities()) {
        AtlasEntityHeader header = new AtlasEntityHeader();
        header.setGuid(guid);
        response.addEntity(EntityMutations.EntityOperation.DELETE, header);
    }
    GuidMapping guidMapping = result.getGuidMapping();
    if (guidMapping != null) {
        response.setGuidAssignments(guidMapping.getGuidAssignments());
    }
    return response;
}
Also used : EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) GuidMapping(org.apache.atlas.model.instance.GuidMapping)

Example 23 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class EntityResource method deleteEntities.

/**
     * Delete entities from the repository identified by their guids (including their composite references)
     * or
     * Deletes a single entity identified by its type and unique attribute value from the repository (including their composite references)
     *
     * @param guids list of deletion candidate guids
     *              or
     * @param entityType the entity type
     * @param attribute the unique attribute used to identify the entity
     * @param value the unique attribute value used to identify the entity
     * @return response payload as json - including guids of entities(including composite references from that entity) that were deleted
     */
@DELETE
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response deleteEntities(@QueryParam("guid") List<String> guids, @QueryParam("type") String entityType, @QueryParam("property") final String attribute, @QueryParam("value") final String value) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.deleteEntities(" + guids + ", " + entityType + ", " + attribute + ", " + value + ")");
        }
        EntityResult entityResult;
        if (guids != null && !guids.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting entities {}", guids);
            }
            EntityMutationResponse mutationResponse = entityREST.deleteByGuids(guids);
            entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Deleting entity type={} with property {}={}", entityType, attribute, value);
            }
            Map<String, Object> attributes = new HashMap<>();
            attributes.put(attribute, value);
            EntityMutationResponse mutationResponse = entitiesStore.deleteByUniqueAttributes(getEntityType(entityType), attributes);
            entityResult = restAdapters.toCreateUpdateEntitiesResult(mutationResponse).getEntityResult();
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Deleted entity result: {}", entityResult);
        }
        JSONObject response = getResponse(entityResult);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        if (guids != null && !guids.isEmpty()) {
            LOG.error("An entity with GUID={} does not exist ", guids, e);
        } else {
            LOG.error("An entity with qualifiedName {}-{}-{} does not exist", entityType, attribute, value, e);
        }
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to delete entities {} {} {} {} ", guids, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.deleteEntities({}, {}, {}, {})", guids, entityType, attribute, value);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) EntityResult(org.apache.atlas.model.legacy.EntityResult) AtlasException(org.apache.atlas.AtlasException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 24 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class EntityResource method submit.

/**
     * Submits the entity definitions (instances).
     * The body contains the JSONArray of entity json. The service takes care of de-duping the entities based on any
     * unique attribute for the give type.
     */
@POST
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response submit(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.submit()");
    }
    String entityJson = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.submit()");
        }
        String entities = Servlets.getRequestPayload(request);
        //Handle backward compatibility - if entities is not JSONArray, convert to JSONArray
        try {
            new JSONArray(entities);
        } catch (JSONException e) {
            final String finalEntities = entities;
            entities = new JSONArray() {

                {
                    put(finalEntities);
                }
            }.toString();
        }
        entityJson = AtlasClient.toString(new JSONArray(entities));
        if (LOG.isDebugEnabled()) {
            LOG.debug("submitting entities {} ", entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(entities);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        final List<String> guids = restAdapters.getGuids(mutationResponse.getCreatedEntities());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Created entities {}", guids);
        }
        final CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        JSONObject response = getResponse(result);
        URI locationURI = getLocationURI(guids);
        return Response.created(locationURI).entity(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entity entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error entityDef={}", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause() != null ? ve.getCause() : ve, Response.Status.BAD_REQUEST));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.submit()");
        }
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) AtlasException(org.apache.atlas.AtlasException) URI(java.net.URI) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 25 with EntityMutationResponse

use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.

the class EntityResource method updateEntities.

/**
     * Complete update of a set of entities - the values not specified will be replaced with null/removed
     * Adds/Updates given entities identified by its GUID or unique attribute
     * @return response payload as json
     */
@PUT
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateEntities(@Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateEntities()");
    }
    String entityJson = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateEntities()");
        }
        final String entities = Servlets.getRequestPayload(request);
        entityJson = AtlasClient.toString(new JSONArray(entities));
        if (LOG.isDebugEnabled()) {
            LOG.info("updating entities {} ", entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(entities);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        JSONObject response = getResponse(result);
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error entityDef={}", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to persist entity instance entityDef={}", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.updateEntities()");
        }
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasException(org.apache.atlas.AtlasException) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Aggregations

EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)60 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)44 Test (org.testng.annotations.Test)40 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)34 BeforeTest (org.testng.annotations.BeforeTest)22 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)21 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)17 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)17 ArrayList (java.util.ArrayList)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)13 HashMap (java.util.HashMap)12 List (java.util.List)8 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)7 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)7 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)7 ImmutableList (com.google.common.collect.ImmutableList)6 AtlasException (org.apache.atlas.AtlasException)6 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)6 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)6 Map (java.util.Map)5