use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.
the class AtlasEntityStoreV1Test method testStructs.
@Test(dependsOnMethods = "testCreate")
public void testStructs() throws Exception {
AtlasEntity databaseEntity = dbEntity.getEntity();
AtlasEntity tableEntity = new AtlasEntity(tblEntity.getEntity());
AtlasEntitiesWithExtInfo entitiesInfo = new AtlasEntitiesWithExtInfo(tableEntity);
AtlasStruct serdeInstance = new AtlasStruct(TestUtils.SERDE_TYPE, TestUtilsV2.NAME, "serde1Name");
serdeInstance.setAttribute("serde", "test");
serdeInstance.setAttribute("description", "testDesc");
tableEntity.setAttribute("serde1", serdeInstance);
tableEntity.setAttribute("database", new AtlasObjectId(databaseEntity.getTypeName(), TestUtilsV2.NAME, databaseEntity.getAttribute(TestUtilsV2.NAME)));
init();
EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
AtlasEntityHeader updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//update struct attribute
serdeInstance.setAttribute("serde", "testUpdated");
init();
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
//set to null
tableEntity.setAttribute("description", null);
init();
response = entityStore.createOrUpdate(new AtlasEntityStream(entitiesInfo), false);
updatedTable = response.getFirstUpdatedEntityByTypeName(TABLE_TYPE);
Assert.assertNull(updatedTable.getAttribute("description"));
validateEntity(entitiesInfo, getEntityFromStore(updatedTable));
}
use of org.apache.atlas.model.instance.EntityMutationResponse 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);
}
}
}
use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.
the class AtlasEntityStoreV1 method deleteByIds.
@Override
@GraphTransaction
public EntityMutationResponse deleteByIds(final List<String> guids) throws AtlasBaseException {
if (CollectionUtils.isEmpty(guids)) {
throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Guid(s) not specified");
}
Collection<AtlasVertex> deletionCandidates = new ArrayList<>();
for (String guid : guids) {
// Retrieve vertices for requested guids.
AtlasVertex vertex = AtlasGraphUtilsV1.findByGuid(guid);
if (vertex != null) {
deletionCandidates.add(vertex);
} else {
if (LOG.isDebugEnabled()) {
// Entity does not exist - treat as non-error, since the caller
// wanted to delete the entity and it's already gone.
LOG.debug("Deletion request ignored for non-existent entity with guid " + guid);
}
}
}
if (deletionCandidates.isEmpty()) {
LOG.info("No deletion candidate entities were found for guids %s", guids);
}
EntityMutationResponse ret = deleteVertices(deletionCandidates);
// Notify the change listeners
entityChangeNotifier.onEntitiesMutated(ret, false);
return ret;
}
use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.
the class AtlasEntityStoreV1 method deleteById.
@Override
@GraphTransaction
public EntityMutationResponse deleteById(final String guid) throws AtlasBaseException {
if (StringUtils.isEmpty(guid)) {
throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
}
// Retrieve vertices for requested guids.
AtlasVertex vertex = AtlasGraphUtilsV1.findByGuid(guid);
Collection<AtlasVertex> deletionCandidates = new ArrayList<>();
if (vertex != null) {
deletionCandidates.add(vertex);
} else {
if (LOG.isDebugEnabled()) {
// Entity does not exist - treat as non-error, since the caller
// wanted to delete the entity and it's already gone.
LOG.debug("Deletion request ignored for non-existent entity with guid " + guid);
}
}
EntityMutationResponse ret = deleteVertices(deletionCandidates);
// Notify the change listeners
entityChangeNotifier.onEntitiesMutated(ret, false);
return ret;
}
use of org.apache.atlas.model.instance.EntityMutationResponse in project incubator-atlas by apache.
the class AtlasEntityStoreV1 method deleteVertices.
private EntityMutationResponse deleteVertices(Collection<AtlasVertex> deletionCandidates) throws AtlasBaseException {
EntityMutationResponse response = new EntityMutationResponse();
deleteHandler.deleteEntities(deletionCandidates);
RequestContextV1 req = RequestContextV1.get();
for (AtlasObjectId id : req.getDeletedEntityIds()) {
response.addEntity(DELETE, EntityGraphMapper.constructHeader(id));
}
for (AtlasObjectId id : req.getUpdatedEntityIds()) {
response.addEntity(UPDATE, EntityGraphMapper.constructHeader(id));
}
return response;
}
Aggregations