Search in sources :

Example 1 with AtlasEntitiesWithExtInfo

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

the class TestEntitiesREST method testUpdateWithSerializedEntities.

@Test
public void testUpdateWithSerializedEntities() throws Exception {
    //Check with serialization and deserialization of entity attributes for the case
    // where attributes which are de-serialized into a map
    AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    AtlasEntity tableEntity = TestUtilsV2.createTableEntity(dbEntity);
    final AtlasEntity colEntity = TestUtilsV2.createColumnEntity(tableEntity);
    List<AtlasEntity> columns = new ArrayList<AtlasEntity>() {

        {
            add(colEntity);
        }
    };
    tableEntity.setAttribute("columns", getObjIdList(columns));
    AtlasEntity newDBEntity = serDeserEntity(dbEntity);
    AtlasEntity newTableEntity = serDeserEntity(tableEntity);
    AtlasEntitiesWithExtInfo newEntities = new AtlasEntitiesWithExtInfo();
    newEntities.addEntity(newDBEntity);
    newEntities.addEntity(newTableEntity);
    for (AtlasEntity column : columns) {
        newEntities.addReferredEntity(serDeserEntity(column));
    }
    EntityMutationResponse response2 = entityREST.createOrUpdate(newEntities);
    List<AtlasEntityHeader> newGuids = response2.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
    Assert.assertNotNull(newGuids);
    Assert.assertEquals(newGuids.size(), 3);
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Test(org.testng.annotations.Test)

Example 2 with AtlasEntitiesWithExtInfo

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

the class TestEntitiesREST method testGetEntities.

@Test(dependsOnMethods = "testCreateOrUpdateEntities")
public void testGetEntities() throws Exception {
    final AtlasEntitiesWithExtInfo response = entityREST.getByGuids(createdGuids);
    final List<AtlasEntity> entities = response.getEntities();
    Assert.assertNotNull(entities);
    Assert.assertEquals(entities.size(), 3);
    verifyAttributes(entities);
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Test(org.testng.annotations.Test)

Example 3 with AtlasEntitiesWithExtInfo

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

the class EntityResource method partialUpdateEntityByGuid.

private Response partialUpdateEntityByGuid(String guid, HttpServletRequest request) {
    String entityJson = null;
    try {
        guid = ParamChecker.notEmpty(guid, "Guid property cannot be null");
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("partially updating entity for guid {} : {} ", guid, entityJson);
        }
        Referenceable updatedEntity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !updateId.isAssigned()) {
            updatedEntity.replaceWithNewId(new Id(guid, 0, updatedEntity.getTypeName()));
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntity(updatedEntity);
        EntityMutationResponse mutationResponse = entitiesStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), true);
        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 update entity by GUID {} {} ", guid, entityJson, e);
        throw toWebApplicationException(e);
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with GUID={} does not exist {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to update entity by GUID {} {}", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to update entity by GUID {} {} ", guid, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.typesystem.Referenceable) JSONObject(org.codehaus.jettison.json.JSONObject) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.typesystem.persistence.Id)

Example 4 with AtlasEntitiesWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo 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 5 with AtlasEntitiesWithExtInfo

use of org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo 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

AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)26 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)21 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)21 Test (org.testng.annotations.Test)17 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)15 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)8 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)7 BeforeTest (org.testng.annotations.BeforeTest)7 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)6 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AtlasException (org.apache.atlas.AtlasException)4 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)4 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)4 JSONObject (org.codehaus.jettison.json.JSONObject)4 Map (java.util.Map)3 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)3 ValueConversionException (org.apache.atlas.typesystem.types.ValueConversionException)3 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)3