Search in sources :

Example 81 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityResource method addTrait.

/**
 * Adds a new trait to an existing entity represented by a guid.
 *
 * @param guid globally unique identifier for the entity
 */
@POST
@Path("{guid}/traits")
@Consumes({ Servlets.JSON_MEDIA_TYPE, MediaType.APPLICATION_JSON })
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response addTrait(@Context HttpServletRequest request, @PathParam("guid") final String guid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.addTrait({})", guid);
    }
    String traitDefinition = null;
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.addTrait(" + guid + ")");
        }
        traitDefinition = Servlets.getRequestPayload(request);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid);
        }
        List<String> guids = new ArrayList<String>() {

            {
                add(guid);
            }
        };
        entitiesStore.addClassification(guids, restAdapters.toAtlasClassification(AtlasType.fromV1Json(traitDefinition, Struct.class)));
        URI locationURI = getLocationURI(new ArrayList<String>() {

            {
                add(guid);
            }
        });
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        return Response.created(locationURI).entity(AtlasJson.toV1Json(response)).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to add trait for entity={} traitDef={}", guid, traitDefinition, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.addTrait({})", guid);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) ArrayList(java.util.ArrayList) URI(java.net.URI) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 82 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project 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);
        }
        String 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 (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);
        }
    }
}
Also used : HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) EntityResult(org.apache.atlas.model.legacy.EntityResult) AtlasException(org.apache.atlas.AtlasException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 83 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityResource method getEntityDefinition.

/**
 * Fetch the complete definition of an entity given its GUID.
 *
 * @param guid GUID for the entity
 */
@GET
@Path("{guid}")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getEntityDefinition(@PathParam("guid") String guid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getEntityDefinition({})", guid);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getEntityDefinition(" + guid + ")");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching entity definition for guid={} ", guid);
        }
        guid = ParamChecker.notEmpty(guid, "guid cannot be null");
        Referenceable entity = getEntity(guid);
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        Response.Status status = Response.Status.NOT_FOUND;
        if (entity != null) {
            response.put(AtlasClient.DEFINITION, entity);
            status = Response.Status.OK;
        } else {
            response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with GUID={%s} does not exist", guid)));
        }
        return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
    } catch (IllegalArgumentException e) {
        LOG.error("Bad GUID={} ", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get instance definition for GUID {}", guid, e);
        throw toWebApplicationException(e);
    } catch (WebApplicationException e) {
        LOG.error("Unable to get instance definition for GUID {}", guid, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get instance definition for GUID {}", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getEntityDefinition({})", guid);
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer)

Example 84 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project 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()");
        }
        entityJson = Servlets.getRequestPayload(request);
        // Handle backward compatibility - if entities is not JSONArray, convert to JSONArray
        String[] jsonStrings;
        try {
            ArrayNode jsonEntities = AtlasJson.parseToV1ArrayNode(entityJson);
            jsonStrings = new String[jsonEntities.size()];
            for (int i = 0; i < jsonEntities.size(); i++) {
                jsonStrings[i] = AtlasJson.toV1Json(jsonEntities.get(i));
            }
        } catch (IOException e) {
            jsonStrings = new String[1];
            jsonStrings[0] = entityJson;
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updating entities: count={}; entities-json={}", jsonStrings.length, entityJson);
        }
        AtlasEntitiesWithExtInfo entitiesInfo = restAdapters.toAtlasEntities(jsonStrings);
        EntityMutationResponse mutationResponse = entityREST.createOrUpdate(entitiesInfo);
        CreateUpdateEntitiesResult result = restAdapters.toCreateUpdateEntitiesResult(mutationResponse);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updated entities: {}", result.getEntityResult());
        }
        String 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 (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) IOException(java.io.IOException) AtlasException(org.apache.atlas.AtlasException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 85 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityResource method getEntityDefinitionByAttribute.

/**
 * Fetch the complete definition of an entity given its qualified name.
 *
 * @param entityType
 * @param attribute
 * @param value
 */
public Response getEntityDefinitionByAttribute(String entityType, String attribute, String value) {
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching entity definition for type={}, qualified name={}", entityType, value);
        }
        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);
        AtlasEntityWithExtInfo entityInfo;
        try {
            entityInfo = entitiesStore.getByUniqueAttributes(getEntityType(entityType), attributes);
        } catch (AtlasBaseException e) {
            LOG.error("Cannot find entity with type: {}, attribute: {} and value: {}", entityType, attribute, value);
            throw toWebApplicationException(e);
        }
        Referenceable entity = null;
        if (entityInfo != null) {
            entity = restAdapters.getReferenceable(entityInfo);
        }
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        Response.Status status = Response.Status.NOT_FOUND;
        if (entity != null) {
            response.put(AtlasClient.DEFINITION, entity);
            status = Response.Status.OK;
        } else {
            response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with type={%s}, " + "qualifiedName={%s} does not exist", entityType, value)));
        }
        return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Bad type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : Response(javax.ws.rs.core.Response) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap)

Aggregations

AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)437 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)129 ArrayList (java.util.ArrayList)60 Test (org.testng.annotations.Test)60 AtlasType (org.apache.atlas.type.AtlasType)51 AtlasException (org.apache.atlas.AtlasException)50 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)48 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)45 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)43 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)36 HashMap (java.util.HashMap)34 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)33 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)31 Produces (javax.ws.rs.Produces)29 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)29 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)29 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)26 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)26 Path (javax.ws.rs.Path)25 Map (java.util.Map)24