Search in sources :

Example 6 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class AtlasInstanceConverter method toCreateUpdateEntitiesResult.

public CreateUpdateEntitiesResult toCreateUpdateEntitiesResult(EntityMutationResponse reponse) {
    CreateUpdateEntitiesResult ret = null;
    if (reponse != null) {
        Map<EntityOperation, List<AtlasEntityHeader>> mutatedEntities = reponse.getMutatedEntities();
        Map<String, String> guidAssignments = reponse.getGuidAssignments();
        ret = new CreateUpdateEntitiesResult();
        if (MapUtils.isNotEmpty(guidAssignments)) {
            ret.setGuidMapping(new GuidMapping(guidAssignments));
        }
        if (MapUtils.isNotEmpty(mutatedEntities)) {
            EntityResult entityResult = new EntityResult();
            for (Map.Entry<EntityOperation, List<AtlasEntityHeader>> e : mutatedEntities.entrySet()) {
                switch(e.getKey()) {
                    case CREATE:
                        List<AtlasEntityHeader> createdEntities = mutatedEntities.get(EntityOperation.CREATE);
                        if (CollectionUtils.isNotEmpty(createdEntities)) {
                            Collections.reverse(createdEntities);
                            entityResult.set(EntityResult.OP_CREATED, getGuids(createdEntities));
                        }
                        break;
                    case UPDATE:
                        List<AtlasEntityHeader> updatedEntities = mutatedEntities.get(EntityOperation.UPDATE);
                        if (CollectionUtils.isNotEmpty(updatedEntities)) {
                            Collections.reverse(updatedEntities);
                            entityResult.set(EntityResult.OP_UPDATED, getGuids(updatedEntities));
                        }
                        break;
                    case PARTIAL_UPDATE:
                        List<AtlasEntityHeader> partialUpdatedEntities = mutatedEntities.get(EntityOperation.PARTIAL_UPDATE);
                        if (CollectionUtils.isNotEmpty(partialUpdatedEntities)) {
                            Collections.reverse(partialUpdatedEntities);
                            entityResult.set(EntityResult.OP_UPDATED, getGuids(partialUpdatedEntities));
                        }
                        break;
                    case DELETE:
                        List<AtlasEntityHeader> deletedEntities = mutatedEntities.get(EntityOperation.DELETE);
                        if (CollectionUtils.isNotEmpty(deletedEntities)) {
                            Collections.reverse(deletedEntities);
                            entityResult.set(EntityResult.OP_DELETED, getGuids(deletedEntities));
                        }
                        break;
                }
            }
            ret.setEntityResult(entityResult);
        }
    }
    return ret;
}
Also used : EntityOperation(org.apache.atlas.model.instance.EntityMutations.EntityOperation) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) ArrayList(java.util.ArrayList) List(java.util.List) EntityResult(org.apache.atlas.model.legacy.EntityResult) Map(java.util.Map) GuidMapping(org.apache.atlas.model.instance.GuidMapping)

Example 7 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class AtlasInstanceConverter method toEntityMutationResponse.

public static EntityMutationResponse toEntityMutationResponse(EntityResult entityResult) {
    CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
    result.setEntityResult(entityResult);
    return toEntityMutationResponse(result);
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult)

Example 8 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult 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 9 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult 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)

Example 10 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class DefaultMetadataService method updateEntities.

/**
     * Updates an entity, instance of the type based on the guid set.
     *
     * @param entityInstanceDefinition json array of entity definitions
     * @return guids - json array of guids
     */
@Override
public CreateUpdateEntitiesResult updateEntities(String entityInstanceDefinition) throws AtlasException {
    entityInstanceDefinition = ParamChecker.notEmpty(entityInstanceDefinition, "Entity instance definition");
    ITypedReferenceableInstance[] typedInstances = deserializeClassInstances(entityInstanceDefinition);
    CreateUpdateEntitiesResult result = repository.updateEntities(typedInstances);
    onEntitiesAddedUpdated(result.getEntityResult());
    return result;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult)

Aggregations

CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)22 AtlasException (org.apache.atlas.AtlasException)9 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)5 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)5 Referenceable (org.apache.atlas.typesystem.Referenceable)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)4 GuidMapping (org.apache.atlas.model.instance.GuidMapping)4 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)4 Id (org.apache.atlas.typesystem.persistence.Id)4 Test (org.testng.annotations.Test)4 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)3 RepositoryException (org.apache.atlas.repository.RepositoryException)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)3 ReferenceableInstance (org.apache.atlas.typesystem.persistence.ReferenceableInstance)3 ClassType (org.apache.atlas.typesystem.types.ClassType)3