Search in sources :

Example 1 with AtlasBaseException

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

the class AtlasEntityStoreV1 method validateAndNormalize.

private void validateAndNormalize(AtlasClassification classification) throws AtlasBaseException {
    AtlasClassificationType type = typeRegistry.getClassificationTypeByName(classification.getTypeName());
    if (type == null) {
        throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_FOUND, classification.getTypeName());
    }
    List<String> messages = new ArrayList<>();
    type.validateValue(classification, classification.getTypeName(), messages);
    if (!messages.isEmpty()) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, messages);
    }
    type.getNormalizedValue(classification);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType)

Example 2 with AtlasBaseException

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

the class AtlasEntityStoreV1 method addClassifications.

@Override
@GraphTransaction
public void addClassifications(final String guid, final List<AtlasClassification> classifications) throws AtlasBaseException {
    if (StringUtils.isEmpty(guid)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Guid(s) not specified");
    }
    if (CollectionUtils.isEmpty(classifications)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "classifications(s) not specified");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Adding classifications={} to entity={}", classifications, guid);
    }
    AtlasEntityHeader entityHeader = entityRetriever.toAtlasEntityHeaderWithClassifications(guid);
    for (AtlasClassification classification : classifications) {
        AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, AtlasPrivilege.ENTITY_ADD_CLASSIFICATION, entityHeader, classification), "add classification: guid=", guid, ", classification=", classification.getTypeName());
    }
    GraphTransactionInterceptor.lockObjectAndReleasePostCommit(guid);
    for (AtlasClassification classification : classifications) {
        validateAndNormalize(classification);
    }
    // validate if entity, not already associated with classifications
    validateEntityAssociations(guid, classifications);
    entityGraphMapper.addClassifications(new EntityMutationContext(), guid, classifications);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityAccessRequest(org.apache.atlas.authorize.AtlasEntityAccessRequest) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 3 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project 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) {
        AtlasVertex vertex = AtlasGraphUtilsV1.findByGuid(guid);
        if (vertex == null) {
            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);
            }
            continue;
        }
        AtlasEntityHeader entityHeader = entityRetriever.toAtlasEntityHeaderWithClassifications(vertex);
        AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, AtlasPrivilege.ENTITY_DELETE, entityHeader), "delete entity: guid=", guid);
        deletionCandidates.add(vertex);
    }
    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;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityAccessRequest(org.apache.atlas.authorize.AtlasEntityAccessRequest) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Example 4 with AtlasBaseException

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

the class AtlasEntityStoreV1 method createOrUpdate.

private EntityMutationResponse createOrUpdate(EntityStream entityStream, boolean isPartialUpdate, boolean replaceClassifications) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> createOrUpdate()");
    }
    if (entityStream == null || !entityStream.hasNext()) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "no entities to create/update.");
    }
    AtlasPerfTracer perf = null;
    if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
        perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "createOrUpdate()");
    }
    try {
        final boolean isImport = entityStream instanceof EntityImportStream;
        final EntityMutationContext context = preCreateOrUpdate(entityStream, entityGraphMapper, isPartialUpdate);
        // Check if authorized to create entities
        if (!isImport && CollectionUtils.isNotEmpty(context.getCreatedEntities())) {
            for (AtlasEntity entity : context.getCreatedEntities()) {
                AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, AtlasPrivilege.ENTITY_CREATE, new AtlasEntityHeader(entity)), "create entity: type=", entity.getTypeName());
            }
        }
        // for existing entities, skip update if incoming entity doesn't have any change
        if (CollectionUtils.isNotEmpty(context.getUpdatedEntities())) {
            List<AtlasEntity> entitiesToSkipUpdate = null;
            for (AtlasEntity entity : context.getUpdatedEntities()) {
                String guid = entity.getGuid();
                AtlasVertex vertex = context.getVertex(guid);
                AtlasEntity entityInStore = entityRetriever.toAtlasEntity(vertex);
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
                if (!AtlasEntityUtil.hasAnyAttributeUpdate(entityType, entity, entityInStore)) {
                    // if classifications are to be replaced as well, then skip updates only when no change in classifications as well
                    if (!replaceClassifications || Objects.equals(entity.getClassifications(), entityInStore.getClassifications())) {
                        if (entitiesToSkipUpdate == null) {
                            entitiesToSkipUpdate = new ArrayList<>();
                        }
                        entitiesToSkipUpdate.add(entity);
                    }
                }
            }
            if (entitiesToSkipUpdate != null) {
                context.getUpdatedEntities().removeAll(entitiesToSkipUpdate);
            }
            // Check if authorized to update entities
            if (!isImport) {
                for (AtlasEntity entity : context.getUpdatedEntities()) {
                    AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, AtlasPrivilege.ENTITY_UPDATE, new AtlasEntityHeader(entity)), "update entity: type=", entity.getTypeName());
                }
            }
        }
        EntityMutationResponse ret = entityGraphMapper.mapAttributesAndClassifications(context, isPartialUpdate, replaceClassifications);
        ret.setGuidAssignments(context.getGuidAssignments());
        // Notify the change listeners
        entityChangeNotifier.onEntitiesMutated(ret, isImport);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== createOrUpdate()");
        }
        return ret;
    } finally {
        AtlasPerfTracer.log(perf);
    }
}
Also used : AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityAccessRequest(org.apache.atlas.authorize.AtlasEntityAccessRequest) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 5 with AtlasBaseException

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

the class AtlasEntityStoreV1 method updateEntity.

@Override
@GraphTransaction
public EntityMutationResponse updateEntity(AtlasObjectId objectId, AtlasEntityWithExtInfo updatedEntityInfo, boolean isPartialUpdate) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> updateEntity({}, {}, {})", objectId, updatedEntityInfo, isPartialUpdate);
    }
    if (objectId == null || updatedEntityInfo == null || updatedEntityInfo.getEntity() == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "null entity-id/entity");
    }
    final String guid;
    if (AtlasTypeUtil.isAssignedGuid(objectId.getGuid())) {
        guid = objectId.getGuid();
    } else {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objectId.getTypeName());
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_TYPENAME, objectId.getTypeName());
        }
        guid = AtlasGraphUtilsV1.getGuidByUniqueAttributes(typeRegistry.getEntityTypeByName(objectId.getTypeName()), objectId.getUniqueAttributes());
    }
    AtlasEntity entity = updatedEntityInfo.getEntity();
    entity.setGuid(guid);
    return createOrUpdate(new AtlasEntityStream(updatedEntityInfo), isPartialUpdate, false);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)437 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)129 Test (org.testng.annotations.Test)60 ArrayList (java.util.ArrayList)59 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