Search in sources :

Example 16 with AtlasBaseException

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

the class EntityGraphMapper method updateEdge.

private AtlasEdge updateEdge(AtlasAttributeDef attributeDef, Object value, AtlasEdge currentEdge, final AtlasVertex entityVertex) throws AtlasBaseException {
    LOG.debug("Updating entity reference {} for reference attribute {}", attributeDef.getName());
    // Update edge if it exists
    AtlasVertex currentVertex = currentEdge.getInVertex();
    String currentEntityId = getIdFromVertex(currentVertex);
    String newEntityId = getIdFromVertex(entityVertex);
    AtlasEdge newEdge = currentEdge;
    if (!currentEntityId.equals(newEntityId)) {
        // add an edge to the class vertex from the instance
        if (entityVertex != null) {
            try {
                newEdge = graphHelper.getOrCreateEdge(currentEdge.getOutVertex(), entityVertex, currentEdge.getLabel());
            } catch (RepositoryException e) {
                throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
            }
        }
    }
    return newEdge;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 17 with AtlasBaseException

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

the class EntityGraphMapper method mapObjectIdValueUsingRelationship.

private AtlasEdge mapObjectIdValueUsingRelationship(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValueUsingRelationship({})", ctx);
    }
    AtlasVertex attributeVertex = context.getDiscoveryContext().getResolvedEntityVertex(getGuid(ctx.getValue()));
    AtlasVertex entityVertex = ctx.getReferringVertex();
    AtlasEdge ret;
    if (attributeVertex == null) {
        AtlasObjectId objectId = getObjectId(ctx.getValue());
        attributeVertex = (objectId != null) ? context.getDiscoveryContext().getResolvedEntityVertex(objectId) : null;
    }
    if (attributeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    String attributeName = ctx.getAttribute().getName();
    AtlasType type = typeRegistry.getType(AtlasGraphUtilsV1.getTypeName(entityVertex));
    AtlasRelationshipEdgeDirection edgeDirection = ctx.getAttribute().getRelationshipEdgeDirection();
    String edgeLabel = ctx.getAttribute().getRelationshipEdgeLabel();
    if (type instanceof AtlasEntityType) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        // use relationship to create/update edges
        if (entityType.hasRelationshipAttribute(attributeName)) {
            Map<String, Object> relationshipAttributes = getRelationshipAttributes(ctx.getValue());
            if (ctx.getCurrentEdge() != null) {
                ret = updateRelationship(ctx.getCurrentEdge(), entityVertex, attributeVertex, edgeDirection, relationshipAttributes);
            } else {
                String relationshipName = graphHelper.getRelationshipDefName(entityVertex, entityType, attributeName);
                AtlasVertex fromVertex;
                AtlasVertex toVertex;
                if (edgeDirection == IN) {
                    fromVertex = attributeVertex;
                    toVertex = entityVertex;
                } else {
                    fromVertex = entityVertex;
                    toVertex = attributeVertex;
                }
                boolean relationshipExists = isRelationshipExists(fromVertex, toVertex, edgeLabel);
                ret = getOrCreateRelationship(fromVertex, toVertex, relationshipName, relationshipAttributes);
                // for import use the relationship guid provided
                if (context.isImport()) {
                    AtlasGraphUtilsV1.setProperty(ret, Constants.GUID_PROPERTY_KEY, getRelationshipGuid(ctx.getValue()));
                }
                // record entity update on both relationship vertices
                if (!relationshipExists) {
                    recordEntityUpdate(attributeVertex);
                }
            }
        } else {
            // use legacy way to create/update edges
            if (LOG.isDebugEnabled()) {
                LOG.debug("No RelationshipDef defined between {} and {} on attribute: {}", getTypeName(entityVertex), getTypeName(attributeVertex), attributeName);
            }
            ret = mapObjectIdValue(ctx, context);
        }
    } else {
        // if type is StructType having objectid as attribute
        ret = mapObjectIdValue(ctx, context);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValueUsingRelationship({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEdgeDirection(org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 18 with AtlasBaseException

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

the class AtlasStructDefStoreV1 method updateVertexPreUpdate.

public static void updateVertexPreUpdate(AtlasStructDef structDef, AtlasStructType structType, AtlasVertex vertex, AtlasTypeDefGraphStoreV1 typeDefStore) throws AtlasBaseException {
    List<String> attrNames = new ArrayList<>();
    if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) {
        for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
            attrNames.add(attributeDef.getName());
        }
    }
    List<String> currAttrNames = vertex.getProperty(AtlasGraphUtilsV1.getTypeDefPropertyKey(structDef), List.class);
    // delete attributes that are not present in updated structDef
    if (CollectionUtils.isNotEmpty(currAttrNames)) {
        for (String currAttrName : currAttrNames) {
            if (!attrNames.contains(currAttrName)) {
                throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_DELETION_NOT_SUPPORTED, structDef.getName(), currAttrName);
            }
        }
    }
    typeDefStore.updateTypeVertex(structDef, vertex);
    // Load up current struct definition for matching attributes
    AtlasStructDef currentStructDef = toStructDef(vertex, new AtlasStructDef(), typeDefStore);
    // add/update attributes that are present in updated structDef
    if (CollectionUtils.isNotEmpty(structDef.getAttributeDefs())) {
        for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
            if (CollectionUtils.isEmpty(currAttrNames) || !currAttrNames.contains(attributeDef.getName())) {
                // new attribute - only allow if optional
                if (!attributeDef.getIsOptional()) {
                    throw new AtlasBaseException(AtlasErrorCode.CANNOT_ADD_MANDATORY_ATTRIBUTE, structDef.getName(), attributeDef.getName());
                }
            }
            // Validate the mandatory features of an attribute (compatibility with legacy type system)
            if (StringUtils.isEmpty(attributeDef.getName())) {
                throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, structDef.getName(), "name");
            }
            if (StringUtils.isEmpty(attributeDef.getTypeName())) {
                throw new AtlasBaseException(AtlasErrorCode.MISSING_MANDATORY_ATTRIBUTE, structDef.getName(), "typeName");
            }
            AtlasAttributeDef existingAttribute = currentStructDef.getAttribute(attributeDef.getName());
            if (null != existingAttribute && !attributeDef.getTypeName().equals(existingAttribute.getTypeName())) {
                throw new AtlasBaseException(AtlasErrorCode.BAD_REQUEST, "Data type update for attribute is not supported");
            }
            String propertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(structDef, attributeDef.getName());
            AtlasGraphUtilsV1.setProperty(vertex, propertyKey, toJsonFromAttribute(structType.getAttribute(attributeDef.getName())));
        }
    }
    AtlasGraphUtilsV1.setProperty(vertex, AtlasGraphUtilsV1.getTypeDefPropertyKey(structDef), attrNames);
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) ArrayList(java.util.ArrayList)

Example 19 with AtlasBaseException

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

the class AtlasStructDefStoreV1 method preDeleteByGuid.

@Override
public AtlasVertex preDeleteByGuid(String guid) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasStructDefStoreV1.preDeleteByGuid({})", guid);
    }
    AtlasStructDef existingDef = typeRegistry.getStructDefByGuid(guid);
    AtlasAuthorizationUtils.verifyAccess(new AtlasTypeAccessRequest(AtlasPrivilege.TYPE_DELETE, existingDef), "delete struct-def ", (existingDef != null ? existingDef.getName() : guid));
    AtlasVertex ret = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.STRUCT);
    String typeName = AtlasGraphUtilsV1.getProperty(ret, Constants.TYPENAME_PROPERTY_KEY, String.class);
    if (AtlasGraphUtilsV1.typeHasInstanceVertex(typeName)) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_HAS_REFERENCES, typeName);
    }
    if (ret == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_GUID_NOT_FOUND, guid);
    }
    typeDefStore.deleteTypeVertexOutEdges(ret);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasStructDefStoreV1.preDeleteByGuid({}): {}", guid, ret);
    }
    return ret;
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasTypeAccessRequest(org.apache.atlas.authorize.AtlasTypeAccessRequest)

Example 20 with AtlasBaseException

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

the class AtlasStructDefStoreV1 method getByGuid.

@Override
public AtlasStructDef getByGuid(String guid) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasStructDefStoreV1.getByGuid({})", guid);
    }
    AtlasVertex vertex = typeDefStore.findTypeVertexByGuidAndCategory(guid, TypeCategory.STRUCT);
    if (vertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_GUID_NOT_FOUND, guid);
    }
    AtlasStructDef ret = toStructDef(vertex);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasStructDefStoreV1.getByGuid({}): {}", guid, ret);
    }
    return ret;
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex)

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