Search in sources :

Example 1 with AtlasGraphUtilsV1

use of org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1 in project atlas by apache.

the class RestUtilsTest method makeTypeStore.

private AtlasTypeDefGraphStoreV1 makeTypeStore(AtlasTypeRegistry reg) {
    AtlasTypeDefGraphStoreV1 result = mock(AtlasTypeDefGraphStoreV1.class);
    for (AtlasEntityType type : reg.getAllEntityTypes()) {
        String typeName = type.getTypeName();
        AtlasVertex typeVertex = mock(AtlasVertex.class);
        when(result.isTypeVertex(eq(typeVertex), any(TypeCategory.class))).thenReturn(true);
        when(typeVertex.getProperty(eq(Constants.TYPE_CATEGORY_PROPERTY_KEY), eq(TypeCategory.class))).thenReturn(TypeCategory.CLASS);
        String attributeListPropertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(typeName);
        when(typeVertex.getProperty(eq(attributeListPropertyKey), eq(List.class))).thenReturn(new ArrayList<>(type.getAllAttributes().keySet()));
        for (AtlasAttribute attribute : type.getAllAttributes().values()) {
            String attributeDefPropertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(typeName, attribute.getName());
            String attributeJson = AtlasStructDefStoreV1.toJsonFromAttribute(attribute);
            when(typeVertex.getProperty(eq(attributeDefPropertyKey), eq(String.class))).thenReturn(attributeJson);
        }
        when(result.findTypeVertexByName(eq(typeName))).thenReturn(typeVertex);
    }
    return result;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasTypeDefGraphStoreV1(org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) TypeCategory(org.apache.atlas.typesystem.types.DataTypes.TypeCategory) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 2 with AtlasGraphUtilsV1

use of org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1 in project 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 = AtlasType.fromV1Json(entityJson, Referenceable.class);
        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 && !AtlasTypeUtil.isAssignedGuid(updateId.getId())) {
            String guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(getEntityType(entityType), attributes);
            updatedEntity.setId(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());
        }
        String 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 (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) AtlasException(org.apache.atlas.AtlasException) AtlasEntityStream(org.apache.atlas.repository.store.graph.v1.AtlasEntityStream) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) Id(org.apache.atlas.v1.model.instance.Id)

Example 3 with AtlasGraphUtilsV1

use of org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1 in project incubator-atlas by apache.

the class RestUtilsTest method makeTypeStore.

private AtlasTypeDefGraphStoreV1 makeTypeStore(AtlasTypeRegistry reg) {
    AtlasTypeDefGraphStoreV1 result = mock(AtlasTypeDefGraphStoreV1.class);
    for (AtlasEntityType type : reg.getAllEntityTypes()) {
        String typeName = type.getTypeName();
        AtlasVertex typeVertex = mock(AtlasVertex.class);
        when(result.isTypeVertex(eq(typeVertex), any(TypeCategory.class))).thenReturn(true);
        when(typeVertex.getProperty(eq(Constants.TYPE_CATEGORY_PROPERTY_KEY), eq(TypeCategory.class))).thenReturn(TypeCategory.CLASS);
        String attributeListPropertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(typeName);
        when(typeVertex.getProperty(eq(attributeListPropertyKey), eq(List.class))).thenReturn(new ArrayList<>(type.getAllAttributes().keySet()));
        for (AtlasAttribute attribute : type.getAllAttributes().values()) {
            String attributeDefPropertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(typeName, attribute.getName());
            String attributeJson = AtlasStructDefStoreV1.toJsonFromAttribute(attribute);
            when(typeVertex.getProperty(eq(attributeDefPropertyKey), eq(String.class))).thenReturn(attributeJson);
        }
        when(result.findTypeVertexByName(eq(typeName))).thenReturn(typeVertex);
    }
    return result;
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasTypeDefGraphStoreV1(org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) TypeCategory(org.apache.atlas.typesystem.types.DataTypes.TypeCategory) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 4 with AtlasGraphUtilsV1

use of org.apache.atlas.repository.store.graph.v1.AtlasGraphUtilsV1 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)

Aggregations

HashMap (java.util.HashMap)2 AtlasException (org.apache.atlas.AtlasException)2 CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)2 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasEntityStream (org.apache.atlas.repository.store.graph.v1.AtlasEntityStream)2 AtlasTypeDefGraphStoreV1 (org.apache.atlas.repository.store.graph.v1.AtlasTypeDefGraphStoreV1)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)2 TypeCategory (org.apache.atlas.typesystem.types.DataTypes.TypeCategory)2 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Referenceable (org.apache.atlas.typesystem.Referenceable)1 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)1 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)1 Id (org.apache.atlas.typesystem.persistence.Id)1