use of org.apache.atlas.typesystem.exception.EntityNotFoundException in project incubator-atlas by apache.
the class GraphBackedMetadataRepository method getEntityDefinitions.
@Override
@GraphTransaction
public List<ITypedReferenceableInstance> getEntityDefinitions(String... guids) throws RepositoryException, EntityNotFoundException {
if (LOG.isDebugEnabled()) {
LOG.debug("Retrieving entities with guids={}", Arrays.toString(guids));
}
RequestContext context = RequestContext.get();
ITypedReferenceableInstance[] result = new ITypedReferenceableInstance[guids.length];
// Map of the guids of instances not in the cache to their index(es) in the result.
// This is used to put the loaded instances into the location(s) corresponding
// to their guid in the result. Note that a set is needed since guids can
// appear more than once in the list.
Map<String, Set<Integer>> uncachedGuids = new HashMap<>();
for (int i = 0; i < guids.length; i++) {
String guid = guids[i];
// First, check the cache.
ITypedReferenceableInstance cached = context.getInstanceV1(guid);
if (cached != null) {
result[i] = cached;
} else {
Set<Integer> indices = uncachedGuids.get(guid);
if (indices == null) {
indices = new HashSet<>(1);
uncachedGuids.put(guid, indices);
}
indices.add(i);
}
}
List<String> guidsToFetch = new ArrayList<>(uncachedGuids.keySet());
Map<String, AtlasVertex> instanceVertices = graphHelper.getVerticesForGUIDs(guidsToFetch);
// search for missing entities
if (instanceVertices.size() != guidsToFetch.size()) {
Set<String> missingGuids = new HashSet<String>(guidsToFetch);
missingGuids.removeAll(instanceVertices.keySet());
if (!missingGuids.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to find guids={}", missingGuids);
}
throw new EntityNotFoundException("Could not find entities in the repository with guids: " + missingGuids.toString());
}
}
for (String guid : guidsToFetch) {
try {
ITypedReferenceableInstance entity = graphToInstanceMapper.mapGraphToTypedInstance(guid, instanceVertices.get(guid));
for (int index : uncachedGuids.get(guid)) {
result[index] = entity;
}
} catch (AtlasException e) {
throw new RepositoryException(e);
}
}
return Arrays.asList(result);
}
use of org.apache.atlas.typesystem.exception.EntityNotFoundException in project incubator-atlas by apache.
the class GraphHelper method findElement.
private AtlasElement findElement(boolean isVertexSearch, Object... args) throws EntityNotFoundException {
AtlasGraphQuery query = graph.query();
for (int i = 0; i < args.length; i += 2) {
query = query.has((String) args[i], args[i + 1]);
}
Iterator<AtlasElement> results = isVertexSearch ? query.vertices().iterator() : query.edges().iterator();
AtlasElement element = (results != null && results.hasNext()) ? results.next() : null;
if (element == null) {
throw new EntityNotFoundException("Could not find " + (isVertexSearch ? "vertex" : "edge") + " with condition: " + getConditionString(args));
}
if (LOG.isDebugEnabled()) {
LOG.debug("Found {} with condition {}", string(element), getConditionString(args));
}
return element;
}
use of org.apache.atlas.typesystem.exception.EntityNotFoundException 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));
}
}
use of org.apache.atlas.typesystem.exception.EntityNotFoundException 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);
}
}
}
use of org.apache.atlas.typesystem.exception.EntityNotFoundException in project incubator-atlas by apache.
the class EntityResource method partialUpdateEntityAttrByGuid.
/**
* Supports Partial updates
* Adds/Updates given entity specified by its GUID
* Supports updation of only simple primitive attributes like strings, ints, floats, enums, class references and
* does not support updation of complex types like arrays, maps
* @param guid entity id
* @param property property to add
* @postbody property's value
* @return response payload as json
*/
private Response partialUpdateEntityAttrByGuid(String guid, String property, HttpServletRequest request) {
String value = null;
try {
Preconditions.checkNotNull(property, "Entity property cannot be null");
value = Servlets.getRequestPayload(request);
Preconditions.checkNotNull(value, "Entity value cannot be null");
if (LOG.isDebugEnabled()) {
LOG.debug("Updating entity {} for property {} = {}", guid, property, value);
}
EntityMutationResponse mutationResponse = entitiesStore.updateEntityAttributeByGuid(guid, property, value);
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 add property {} to entity id {} {} ", property, guid, value, e);
throw toWebApplicationException(e);
} catch (EntityNotFoundException e) {
LOG.error("An entity with GUID={} does not exist {} ", guid, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.NOT_FOUND));
} catch (AtlasException | IllegalArgumentException e) {
LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to add property {} to entity id {} {} ", property, guid, value, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
Aggregations