Search in sources :

Example 16 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class DefaultMetadataService method updateEntityAttributeByGuid.

@Override
public CreateUpdateEntitiesResult updateEntityAttributeByGuid(String guid, String attributeName, String value) throws AtlasException {
    guid = ParamChecker.notEmpty(guid, "entity id");
    attributeName = ParamChecker.notEmpty(attributeName, "attribute name");
    value = ParamChecker.notEmpty(value, "attribute value");
    ITypedReferenceableInstance existInstance = validateEntityExists(guid);
    ClassType type = typeSystem.getDataType(ClassType.class, existInstance.getTypeName());
    ITypedReferenceableInstance newInstance = type.createInstance();
    AttributeInfo attributeInfo = type.fieldMapping.fields.get(attributeName);
    if (attributeInfo == null) {
        throw new AtlasException("Invalid property " + attributeName + " for entity " + existInstance.getTypeName());
    }
    DataTypes.TypeCategory attrTypeCategory = attributeInfo.dataType().getTypeCategory();
    switch(attrTypeCategory) {
        case PRIMITIVE:
            newInstance.set(attributeName, value);
            break;
        case CLASS:
            Id id = new Id(value, 0, attributeInfo.dataType().getName());
            newInstance.set(attributeName, id);
            break;
        default:
            throw new AtlasException("Update of " + attrTypeCategory + " is not supported");
    }
    ((ReferenceableInstance) newInstance).replaceWithNewId(new Id(guid, 0, newInstance.getTypeName()));
    CreateUpdateEntitiesResult result = repository.updatePartial(newInstance);
    onEntitiesAddedUpdated(result.getEntityResult());
    return result;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) Id(org.apache.atlas.typesystem.persistence.Id) ReferenceableInstance(org.apache.atlas.typesystem.persistence.ReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasException(org.apache.atlas.AtlasException)

Example 17 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class DefaultMetadataService method updateEntityPartialByGuid.

@Override
public CreateUpdateEntitiesResult updateEntityPartialByGuid(String guid, Referenceable newEntity) throws AtlasException {
    guid = ParamChecker.notEmpty(guid, "guid cannot be null");
    newEntity = ParamChecker.notNull(newEntity, "updatedEntity cannot be null");
    ITypedReferenceableInstance existInstance = validateEntityExists(guid);
    ITypedReferenceableInstance newInstance = validateAndConvertToTypedInstance(newEntity, existInstance.getTypeName());
    ((ReferenceableInstance) newInstance).replaceWithNewId(new Id(guid, 0, newInstance.getTypeName()));
    CreateUpdateEntitiesResult result = repository.updatePartial(newInstance);
    onEntitiesAddedUpdated(result.getEntityResult());
    return result;
}
Also used : ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) ReferenceableInstance(org.apache.atlas.typesystem.persistence.ReferenceableInstance) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id)

Example 18 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult 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 19 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class EntityResource method getResponse.

private JSONObject getResponse(EntityResult entityResult) throws AtlasException, JSONException {
    CreateUpdateEntitiesResult result = new CreateUpdateEntitiesResult();
    result.setEntityResult(entityResult);
    return getResponse(result);
}
Also used : CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult)

Example 20 with CreateUpdateEntitiesResult

use of org.apache.atlas.CreateUpdateEntitiesResult in project incubator-atlas by apache.

the class GraphBackedMetadataRepositoryTest method testCreateEntityWithThreeNestingLevels.

@Test
public void testCreateEntityWithThreeNestingLevels() throws AtlasException {
    List<Referenceable> toVerify = new ArrayList<>();
    Referenceable dept = new Referenceable(TestUtils.DEPARTMENT_TYPE);
    toVerify.add(dept);
    dept.set(TestUtils.NAME, "test3");
    Referenceable barry = new Referenceable(TestUtils.PERSON_TYPE);
    toVerify.add(barry);
    barry.set(TestUtils.NAME, "barry");
    barry.set(TestUtils.DEPARTMENT_ATTR, dept);
    Referenceable barryComputer = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(barryComputer);
    barryComputer.set("name", "barryComputer");
    barry.set(TestUtils.ASSETS_ATTR, ImmutableList.of(barryComputer));
    Referenceable barryHardDrive = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(barryHardDrive);
    barryHardDrive.set("name", "barryHardDrive");
    Referenceable barryCpuFan = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(barryCpuFan);
    barryCpuFan.set("name", "barryCpuFan");
    Referenceable barryVideoCard = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(barryVideoCard);
    barryVideoCard.set("name", "barryVideoCard");
    barryComputer.set("childAssets", ImmutableList.of(barryHardDrive, barryVideoCard, barryCpuFan));
    Referenceable jacob = new Referenceable(TestUtils.PERSON_TYPE);
    toVerify.add(jacob);
    jacob.set(TestUtils.NAME, "jacob");
    jacob.set(TestUtils.DEPARTMENT_ATTR, dept);
    Referenceable jacobComputer = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(jacobComputer);
    jacobComputer.set("name", "jacobComputer");
    jacob.set(TestUtils.ASSETS_ATTR, ImmutableList.of(jacobComputer));
    Referenceable jacobHardDrive = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(jacobHardDrive);
    jacobHardDrive.set("name", "jacobHardDrive");
    Referenceable jacobCpuFan = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(jacobCpuFan);
    jacobCpuFan.set("name", "jacobCpuFan");
    Referenceable jacobVideoCard = new Referenceable(TestUtils.ASSET_TYPE);
    toVerify.add(jacobVideoCard);
    jacobVideoCard.set("name", "jacobVideoCard");
    jacobComputer.set("childAssets", ImmutableList.of(jacobHardDrive, jacobVideoCard, jacobCpuFan));
    dept.set(TestUtils.EMPLOYEES_ATTR, ImmutableList.of(barry, jacob));
    Map<String, Referenceable> positions = new HashMap<>();
    final String JANITOR = "janitor";
    final String RECEPTIONIST = "receptionist";
    positions.put(JANITOR, barry);
    positions.put(RECEPTIONIST, jacob);
    dept.set(TestUtils.POSITIONS_ATTR, positions);
    ClassType deptType = TypeSystem.getInstance().getDataType(ClassType.class, TestUtils.DEPARTMENT_TYPE);
    ITypedReferenceableInstance deptInstance = deptType.convert(dept, Multiplicity.REQUIRED);
    CreateUpdateEntitiesResult result = repositoryService.createEntities(deptInstance);
    assertEquals(result.getCreatedEntities().size(), toVerify.size());
    validateGuidMapping(toVerify, result);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) HashMap(java.util.HashMap) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) CreateUpdateEntitiesResult(org.apache.atlas.CreateUpdateEntitiesResult) ArrayList(java.util.ArrayList) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test)

Aggregations

CreateUpdateEntitiesResult (org.apache.atlas.CreateUpdateEntitiesResult)22 AtlasException (org.apache.atlas.AtlasException)9 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)5 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)5 Referenceable (org.apache.atlas.typesystem.Referenceable)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)4 GuidMapping (org.apache.atlas.model.instance.GuidMapping)4 EntityExistsException (org.apache.atlas.typesystem.exception.EntityExistsException)4 Id (org.apache.atlas.typesystem.persistence.Id)4 Test (org.testng.annotations.Test)4 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)3 RepositoryException (org.apache.atlas.repository.RepositoryException)3 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)3 EntityNotFoundException (org.apache.atlas.typesystem.exception.EntityNotFoundException)3 ReferenceableInstance (org.apache.atlas.typesystem.persistence.ReferenceableInstance)3 ClassType (org.apache.atlas.typesystem.types.ClassType)3