Search in sources :

Example 21 with AtlasEntitiesWithExtInfo

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

the class EntityResource method updateByUniqueAttribute.

/**
     * Adds/Updates given entity identified by its unique attribute( entityType, attributeName and value)
     * Updates support only partial update of an entity - Adds/updates any new values specified
     * Updates do not support removal of attribute values
     *
     * @param entityType the entity type
     * @param attribute the unique attribute used to identify the entity
     * @param value the unique attributes value
     * @param request The updated entity json
     * @return response payload as json
     * 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
@Path("qualifiedName")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response updateByUniqueAttribute(@QueryParam("type") String entityType, @QueryParam("property") String attribute, @QueryParam("value") String value, @Context HttpServletRequest request) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.updateByUniqueAttribute({}, {}, {})", entityType, attribute, value);
    }
    AtlasPerfTracer perf = null;
    String entityJson = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.updateByUniqueAttribute(" + entityType + ", " + attribute + ", " + value + ")");
        }
        entityJson = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Partially updating entity by unique attribute {} {} {} {} ", entityType, attribute, value, entityJson);
        }
        Referenceable updatedEntity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
        entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
        attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
        value = ParamChecker.notEmpty(value, "attribute value cannot be null");
        Map<String, Object> attributes = new HashMap<>();
        attributes.put(attribute, value);
        // update referenceable with Id if not specified in payload
        Id updateId = updatedEntity.getId();
        if (updateId != null && !updateId.isAssigned()) {
            String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(getEntityType(entityType), attributes);
            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 partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw toWebApplicationException(e);
    } catch (ValueConversionException ve) {
        LOG.error("Unable to persist entity instance due to a deserialization error {} ", entityJson, ve);
        throw new WebApplicationException(Servlets.getErrorResponse(ve.getCause(), Response.Status.BAD_REQUEST));
    } catch (EntityExistsException e) {
        LOG.error("Unique constraint violation for entity {} ", entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.CONFLICT));
    } catch (EntityNotFoundException e) {
        LOG.error("An entity with type={} and qualifiedName={} does not exist {} ", entityType, value, entityJson, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
    } catch (AtlasException | IllegalArgumentException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, entityType, attribute, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to partially update entity {} {}:{}.{}", entityJson, 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.updateByUniqueAttribute({}, {}, {})", 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) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) EntityExistsException(org.apache.atlas.typesystem.exception.EntityExistsException) 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) JSONObject(org.codehaus.jettison.json.JSONObject) Id(org.apache.atlas.typesystem.persistence.Id) ValueConversionException(org.apache.atlas.typesystem.types.ValueConversionException)

Example 22 with AtlasEntitiesWithExtInfo

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

the class AtlasInstanceConverter method toAtlasEntities.

public AtlasEntitiesWithExtInfo toAtlasEntities(String entitiesJson) throws AtlasBaseException, AtlasException {
    ITypedReferenceableInstance[] referenceables = metadataService.deserializeClassInstances(entitiesJson);
    AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) instanceFormatters.getConverter(TypeCategory.ENTITY);
    ConverterContext context = new ConverterContext();
    AtlasEntitiesWithExtInfo ret = null;
    if (referenceables != null) {
        for (IReferenceableInstance referenceable : referenceables) {
            AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
            if (entityType == null) {
                throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
            }
            AtlasEntity entity = converter.fromV1ToV2(referenceable, entityType, context);
            context.addEntity(entity);
        }
        ret = context.getEntities();
    }
    return ret;
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ConverterContext(org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 23 with AtlasEntitiesWithExtInfo

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

the class AtlasEntityStoreV1 method getByIds.

@Override
@GraphTransaction
public AtlasEntitiesWithExtInfo getByIds(List<String> guids) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> getByIds({})", guids);
    }
    EntityGraphRetriever entityRetriever = new EntityGraphRetriever(typeRegistry);
    AtlasEntitiesWithExtInfo ret = entityRetriever.toAtlasEntitiesWithExtInfo(guids);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== getByIds({}): {}", guids, ret);
    }
    return ret;
}
Also used : AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 24 with AtlasEntitiesWithExtInfo

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

the class TestEntitiesREST method testCreateOrUpdateEntities.

@Test
public void testCreateOrUpdateEntities() throws Exception {
    AtlasEntitiesWithExtInfo entities = new AtlasEntitiesWithExtInfo();
    entities.addEntity(dbEntity);
    entities.addEntity(tableEntity);
    for (AtlasEntity column : columns) {
        entities.addReferredEntity(column);
    }
    EntityMutationResponse response = entityREST.createOrUpdate(entities);
    List<AtlasEntityHeader> guids = response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE);
    Assert.assertNotNull(guids);
    Assert.assertEquals(guids.size(), 3);
    for (AtlasEntityHeader header : guids) {
        createdGuids.add(header.getGuid());
    }
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) Test(org.testng.annotations.Test)

Example 25 with AtlasEntitiesWithExtInfo

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

the class TestEntityREST method testUpdateGetDeleteEntityByUniqueAttribute.

@Test
public void testUpdateGetDeleteEntityByUniqueAttribute() throws Exception {
    AtlasEntity dbEntity = TestUtilsV2.createDBEntity();
    EntityMutationResponse response = entityREST.createOrUpdate(new AtlasEntitiesWithExtInfo(dbEntity));
    String dbGuid = response.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).get(0).getGuid();
    Assert.assertTrue(AtlasTypeUtil.isAssignedGuid(dbGuid));
    final String prevDBName = (String) dbEntity.getAttribute(TestUtilsV2.NAME);
    final String updatedDBName = prevDBName + ":updated";
    dbEntity.setAttribute(TestUtilsV2.NAME, updatedDBName);
    response = entityREST.partialUpdateEntityByUniqueAttrs(TestUtilsV2.DATABASE_TYPE, toHttpServletRequest(TestUtilsV2.NAME, prevDBName), new AtlasEntityWithExtInfo(dbEntity));
    Assert.assertEquals(response.getEntitiesByOperation(EntityMutations.EntityOperation.PARTIAL_UPDATE).get(0).getGuid(), dbGuid);
    //Get By unique attribute
    AtlasEntityWithExtInfo entity = entityREST.getByUniqueAttributes(TestUtilsV2.DATABASE_TYPE, toHttpServletRequest(TestUtilsV2.NAME, updatedDBName));
    Assert.assertNotNull(entity);
    Assert.assertNotNull(entity.getEntity().getGuid());
    Assert.assertEquals(entity.getEntity().getGuid(), dbGuid);
    TestEntitiesREST.verifyAttributes(entity.getEntity().getAttributes(), dbEntity.getAttributes());
    final EntityMutationResponse deleteResponse = entityREST.deleteByUniqueAttribute(TestUtilsV2.DATABASE_TYPE, toHttpServletRequest(TestUtilsV2.NAME, (String) dbEntity.getAttribute(TestUtilsV2.NAME)));
    Assert.assertNotNull(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE));
    Assert.assertEquals(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE).size(), 1);
    Assert.assertEquals(deleteResponse.getEntitiesByOperation(EntityMutations.EntityOperation.DELETE).get(0).getGuid(), dbGuid);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Test(org.testng.annotations.Test)

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