Search in sources :

Example 26 with AtlasBaseException

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

the class DeleteHandlerV1 method getOwnedVertices.

/**
 * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity AtlasVertex.
 * The graph is traversed from the root entity through to the leaf nodes of the containment graph.
 *
 * @param entityVertex the root entity vertex
 * @return set of VertexInfo for all composite entities
 * @throws AtlasException
 */
public Collection<GraphHelper.VertexInfo> getOwnedVertices(AtlasVertex entityVertex) throws AtlasBaseException {
    Map<String, GraphHelper.VertexInfo> vertexInfoMap = new HashMap<>();
    Stack<AtlasVertex> vertices = new Stack<>();
    vertices.push(entityVertex);
    while (vertices.size() > 0) {
        AtlasVertex vertex = vertices.pop();
        AtlasEntity.Status state = getState(vertex);
        if (state == DELETED) {
            // If the reference vertex is marked for deletion, skip it
            continue;
        }
        String guid = GraphHelper.getGuid(vertex);
        if (vertexInfoMap.containsKey(guid)) {
            continue;
        }
        AtlasObjectId entity = entityRetriever.toAtlasObjectId(vertex);
        String typeName = entity.getTypeName();
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), typeName);
        }
        vertexInfoMap.put(guid, new GraphHelper.VertexInfo(entity, vertex));
        for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getAllAttributes().values()) {
            if (!attributeInfo.isOwnedRef()) {
                continue;
            }
            String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(entityType, attributeInfo.getName());
            AtlasType attrType = attributeInfo.getAttributeType();
            switch(attrType.getTypeCategory()) {
                case OBJECT_ID_TYPE:
                    {
                        AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel);
                        if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                            vertices.push(edge.getInVertex());
                        }
                    }
                    break;
                case ARRAY:
                    {
                        AtlasArrayType arrType = (AtlasArrayType) attrType;
                        if (arrType.getElementType().getTypeCategory() != TypeCategory.OBJECT_ID_TYPE) {
                            continue;
                        }
                        Iterator<AtlasEdge> edges = graphHelper.getOutGoingEdgesByLabel(vertex, edgeLabel);
                        if (edges != null) {
                            while (edges.hasNext()) {
                                AtlasEdge edge = edges.next();
                                if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                                    vertices.push(edge.getInVertex());
                                }
                            }
                        }
                    }
                    break;
                case MAP:
                    {
                        AtlasMapType mapType = (AtlasMapType) attrType;
                        TypeCategory valueTypeCategory = mapType.getValueType().getTypeCategory();
                        if (valueTypeCategory != TypeCategory.OBJECT_ID_TYPE) {
                            continue;
                        }
                        String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(entityType, attributeInfo.getName());
                        List<String> keys = vertex.getProperty(propertyName, List.class);
                        if (keys != null) {
                            for (String key : keys) {
                                String mapEdgeLabel = GraphHelper.getQualifiedNameForMapKey(edgeLabel, key);
                                AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, mapEdgeLabel);
                                if (edge != null && getState(edge) == AtlasEntity.Status.ACTIVE) {
                                    vertices.push(edge.getInVertex());
                                }
                            }
                        }
                    }
                    break;
            }
        }
    }
    return vertexInfoMap.values();
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasArrayType(org.apache.atlas.type.AtlasArrayType) GraphHelper(org.apache.atlas.repository.graph.GraphHelper) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) TypeCategory(org.apache.atlas.model.TypeCategory) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 27 with AtlasBaseException

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

the class DeleteHandlerV1 method deleteEdgeBetweenVertices.

/**
 * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
 * @param outVertex
 * @param inVertex
 * @param attribute
 * @throws AtlasException
 */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, AtlasAttribute attribute) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    }
    final String typeName = GraphHelper.getTypeName(outVertex);
    final String outId = GraphHelper.getGuid(outVertex);
    final AtlasEntity.Status state = getState(outVertex);
    if (state == DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(outId))) {
        // If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    AtlasStructType parentType = (AtlasStructType) typeRegistry.getType(typeName);
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(parentType, attribute.getName());
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    AtlasAttributeDef attrDef = attribute.getAttributeDef();
    AtlasType attrType = attribute.getAttributeType();
    switch(attrType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            {
                // If its class attribute, its the only edge between two vertices
                if (attrDef.getIsOptional()) {
                    edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                    if (shouldUpdateInverseReferences) {
                        GraphHelper.setProperty(outVertex, propertyName, null);
                    }
                } else {
                    // Cannot unset a required attribute.
                    throw new AtlasBaseException("Cannot unset required attribute " + propertyName + " on " + GraphHelper.vertexString(outVertex) + " edge = " + edgeLabel);
                }
            }
            break;
        case ARRAY:
            {
                // If its array attribute, find the right edge between the two vertices and update array property
                List<String> elements = GraphHelper.getListProperty(outVertex, propertyName);
                if (elements != null) {
                    // Make a copy, else list.remove reflects on titan.getProperty()
                    elements = new ArrayList<>(elements);
                    for (String elementEdgeId : elements) {
                        AtlasEdge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId);
                        if (elementEdge == null) {
                            continue;
                        }
                        AtlasVertex elementVertex = elementEdge.getInVertex();
                        if (elementVertex.equals(inVertex)) {
                            edge = elementEdge;
                            // TODO element.size includes deleted items as well. should exclude
                            if (!attrDef.getIsOptional() && elements.size() <= attrDef.getValuesMinCount()) {
                                // Deleting this edge would violate the attribute's lower bound.
                                throw new AtlasBaseException("Cannot remove array element from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                            }
                            if (shouldUpdateInverseReferences) {
                                // but when column is deleted, table will not reference the deleted column
                                if (LOG.isDebugEnabled()) {
                                    LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attribute.getName());
                                }
                                // Remove all occurrences of the edge ID from the list.
                                // This prevents dangling edge IDs (i.e. edge IDs for deleted edges)
                                // from the remaining in the list if there are duplicates.
                                elements.removeAll(Collections.singletonList(elementEdge.getId().toString()));
                                GraphHelper.setProperty(outVertex, propertyName, elements);
                                break;
                            }
                        }
                    }
                }
            }
            break;
        case MAP:
            {
                // If its map attribute, find the right edge between two vertices and update map property
                List<String> keys = GraphHelper.getListProperty(outVertex, propertyName);
                if (keys != null) {
                    // Make a copy, else list.remove reflects on titan.getProperty()
                    keys = new ArrayList<>(keys);
                    for (String key : keys) {
                        String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key);
                        String mapEdgeId = GraphHelper.getSingleValuedProperty(outVertex, keyPropertyName, String.class);
                        AtlasEdge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId);
                        if (mapEdge != null) {
                            AtlasVertex mapVertex = mapEdge.getInVertex();
                            if (mapVertex.getId().toString().equals(inVertex.getId().toString())) {
                                // TODO keys.size includes deleted items as well. should exclude
                                if (attrDef.getIsOptional() || keys.size() > attrDef.getValuesMinCount()) {
                                    edge = mapEdge;
                                } else {
                                    // Deleting this entry would violate the attribute's lower bound.
                                    throw new AtlasBaseException("Cannot remove map entry " + keyPropertyName + " from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                                }
                                if (shouldUpdateInverseReferences) {
                                    // remove this key
                                    if (LOG.isDebugEnabled()) {
                                        LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attribute.getName());
                                    }
                                    keys.remove(key);
                                    GraphHelper.setProperty(outVertex, propertyName, keys);
                                    GraphHelper.setProperty(outVertex, keyPropertyName, null);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case CLASSIFICATION:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attribute.getName() + " which is not class/array/map attribute. found " + attrType.getTypeCategory().name());
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContextV1 requestContext = RequestContextV1.get();
        if (!requestContext.isUpdatedEntity(outId)) {
            GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
            GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
            requestContext.recordEntityUpdate(entityRetriever.toAtlasObjectId(outVertex));
        }
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 28 with AtlasBaseException

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

the class EntityGraphRetriever method mapRelationshipAttributes.

private void mapRelationshipAttributes(AtlasVertex entityVertex, AtlasEntity entity) throws AtlasBaseException {
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName());
    if (entityType == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, entity.getTypeName());
    }
    for (AtlasAttribute attribute : entityType.getRelationshipAttributes().values()) {
        Object attrValue = mapVertexToRelationshipAttribute(entityVertex, entityType, attribute);
        entity.setRelationshipAttribute(attribute.getName(), attrValue);
    }
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 29 with AtlasBaseException

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

the class EntityGraphRetriever method mapAttributes.

private void mapAttributes(AtlasVertex entityVertex, AtlasStruct struct, AtlasEntityExtInfo entityExtInfo) throws AtlasBaseException {
    AtlasType objType = typeRegistry.getType(struct.getTypeName());
    if (!(objType instanceof AtlasStructType)) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, struct.getTypeName());
    }
    AtlasStructType structType = (AtlasStructType) objType;
    for (AtlasAttribute attribute : structType.getAllAttributes().values()) {
        Object attrValue = mapVertexToAttribute(entityVertex, attribute, entityExtInfo);
        struct.setAttribute(attribute.getName(), attrValue);
    }
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType) AtlasStructType(org.apache.atlas.type.AtlasStructType)

Example 30 with AtlasBaseException

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

the class EntityGraphRetriever method getEntityVertex.

private AtlasVertex getEntityVertex(AtlasObjectId objId) throws AtlasBaseException {
    AtlasVertex ret = null;
    if (!AtlasTypeUtil.isValid(objId)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, objId.toString());
    }
    if (AtlasTypeUtil.isAssignedGuid(objId)) {
        ret = AtlasGraphUtilsV1.findByGuid(objId.getGuid());
    } else {
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objId.getTypeName());
        Map<String, Object> uniqAttributes = objId.getUniqueAttributes();
        ret = AtlasGraphUtilsV1.getVertexByUniqueAttributes(entityType, uniqAttributes);
    }
    if (ret == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, objId.toString());
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

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