Search in sources :

Example 16 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge 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 AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project atlas by apache.

the class EntityGraphMapper method mapArrayValue.

public List mapArrayValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapArrayValue({})", ctx);
    }
    AtlasAttribute attribute = ctx.getAttribute();
    List newElements = (List) ctx.getValue();
    AtlasArrayType arrType = (AtlasArrayType) attribute.getAttributeType();
    AtlasType elementType = arrType.getElementType();
    boolean isReference = AtlasGraphUtilsV1.isReference(elementType);
    AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
    Cardinality cardinality = attribute.getAttributeDef().getCardinality();
    List<Object> newElementsCreated = new ArrayList<>();
    List<Object> currentElements;
    if (isRelationshipAttribute(attribute)) {
        currentElements = getArrayElementsUsingRelationship(ctx.getReferringVertex(), attribute, elementType);
    } else {
        currentElements = getArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty());
    }
    if (CollectionUtils.isNotEmpty(newElements)) {
        if (cardinality == SET) {
            newElements = (List) newElements.stream().distinct().collect(Collectors.toList());
        }
        for (int index = 0; index < newElements.size(); index++) {
            AtlasEdge existingEdge = getEdgeAt(currentElements, index, elementType);
            AttributeMutationContext arrCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), ctx.getAttribute(), newElements.get(index), ctx.getVertexProperty(), elementType, existingEdge);
            Object newEntry = mapCollectionElementsToVertex(arrCtx, context);
            if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                // Update the inverse reference value.
                AtlasEdge newEdge = (AtlasEdge) newEntry;
                addInverseReference(inverseRefAttribute, newEdge, getRelationshipAttributes(ctx.getValue()));
            }
            newElementsCreated.add(newEntry);
        }
    }
    if (isReference) {
        List<AtlasEdge> additionalEdges = removeUnusedArrayEntries(attribute, (List) currentElements, (List) newElementsCreated, ctx.getReferringVertex());
        newElementsCreated.addAll(additionalEdges);
    }
    // for dereference on way out
    setArrayElementsProperty(elementType, ctx.getReferringVertex(), ctx.getVertexProperty(), newElementsCreated);
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapArrayValue({})", ctx);
    }
    return newElementsCreated;
}
Also used : AtlasArrayType(org.apache.atlas.type.AtlasArrayType) Cardinality(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef.Cardinality) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute)

Example 18 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge in project atlas by apache.

the class EntityGraphMapper method mapClassification.

private AtlasEdge mapClassification(EntityOperation operation, final EntityMutationContext context, AtlasClassification classification, AtlasEntityType entityType, AtlasVertex parentInstanceVertex, AtlasVertex traitInstanceVertex) throws AtlasBaseException {
    if (classification.getValidityPeriods() != null) {
        String strValidityPeriods = AtlasJson.toJson(classification.getValidityPeriods());
        AtlasGraphUtilsV1.setProperty(traitInstanceVertex, Constants.CLASSIFICATION_VALIDITY_PERIODS_KEY, strValidityPeriods);
    } else {
    // if 'null', don't update existing value in the classification
    }
    AtlasGraphUtilsV1.setProperty(traitInstanceVertex, Constants.CLASSIFICATION_VERTEX_PROPAGATE_KEY, classification.isPropagate());
    // map all the attributes to this newly created AtlasVertex
    mapAttributes(classification, traitInstanceVertex, operation, context);
    AtlasEdge ret = getClassificationEdge(parentInstanceVertex, traitInstanceVertex);
    if (ret == null) {
        ret = graphHelper.addClassificationEdge(parentInstanceVertex, traitInstanceVertex, false);
    }
    return ret;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 19 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge 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 20 with AtlasEdge

use of org.apache.atlas.repository.graphdb.AtlasEdge 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)

Aggregations

AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)138 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)60 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)28 AtlasType (org.apache.atlas.type.AtlasType)15 ArrayList (java.util.ArrayList)13 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)13 AtlasStructType (org.apache.atlas.type.AtlasStructType)12 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)12 RepositoryException (org.apache.atlas.repository.RepositoryException)11 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)11 Edge (org.apache.tinkerpop.gremlin.structure.Edge)10 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)8 AtlasRelationship (org.apache.atlas.model.instance.AtlasRelationship)8 AtlasMapType (org.apache.atlas.type.AtlasMapType)8 AtlasArrayType (org.apache.atlas.type.AtlasArrayType)7 HashSet (java.util.HashSet)6 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)6 Id (org.apache.atlas.typesystem.persistence.Id)6 AtlasException (org.apache.atlas.AtlasException)5 Iterator (java.util.Iterator)4