Search in sources :

Example 26 with AtlasEdge

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

the class EntityGraphRetriever method addTagPropagation.

private void addTagPropagation(AtlasVertex fromVertex, AtlasVertex toVertex, AtlasEdge edge) throws AtlasBaseException {
    final List<AtlasVertex> classificationVertices = getPropagationEnabledClassificationVertices(fromVertex);
    final List<AtlasVertex> impactedEntityVertices = CollectionUtils.isNotEmpty(classificationVertices) ? graphHelper.getIncludedImpactedVerticesWithReferences(toVertex, getRelationshipGuid(edge)) : null;
    if (CollectionUtils.isNotEmpty(impactedEntityVertices)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Propagate {} tags: from {} entity to {} entities", classificationVertices.size(), getTypeName(fromVertex), impactedEntityVertices.size());
        }
        for (AtlasVertex classificationVertex : classificationVertices) {
            String classificationName = getTypeName(classificationVertex);
            AtlasVertex associatedEntityVertex = getAssociatedEntityVertex(classificationVertex);
            AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationName);
            for (AtlasVertex impactedEntityVertex : impactedEntityVertices) {
                if (getClassificationEdge(impactedEntityVertex, classificationVertex) != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Classification edge already exists from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), classificationName);
                    }
                    continue;
                } else if (getPropagatedClassificationEdge(impactedEntityVertex, classificationVertex) != null) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Propagated classification edge already exists from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), CLASSIFICATION_LABEL);
                    }
                    continue;
                }
                String entityTypeName = getTypeName(impactedEntityVertex);
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
                if (!classificationType.canApplyToEntityType(entityType)) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Not creating propagated classification edge from [{}] --> [{}][{}], classification is not applicable for entity type", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex));
                    }
                    continue;
                }
                if (LOG.isDebugEnabled()) {
                    LOG.debug(" --> Creating propagated classification edge from [{}] --> [{}][{}] using edge label: [{}]", getTypeName(impactedEntityVertex), getTypeName(classificationVertex), getTypeName(associatedEntityVertex), CLASSIFICATION_LABEL);
                }
                AtlasEdge existingEdge = getPropagatedClassificationEdge(impactedEntityVertex, classificationVertex);
                if (existingEdge != null) {
                    continue;
                }
                graphHelper.addClassificationEdge(impactedEntityVertex, classificationVertex, true);
                addToPropagatedTraitNames(impactedEntityVertex, classificationName);
            }
        }
    }
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 27 with AtlasEdge

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

the class AtlasRelationshipDefStoreV1 method preCreate.

@Override
public AtlasVertex preCreate(AtlasRelationshipDef relationshipDef) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AtlasRelationshipDefStoreV1.preCreate({})", relationshipDef);
    }
    validateType(relationshipDef);
    AtlasType type = typeRegistry.getType(relationshipDef.getName());
    if (type.getTypeCategory() != org.apache.atlas.model.TypeCategory.RELATIONSHIP) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_MATCH_FAILED, relationshipDef.getName(), TypeCategory.RELATIONSHIP.name());
    }
    AtlasVertex relationshipDefVertex = typeDefStore.findTypeVertexByName(relationshipDef.getName());
    if (relationshipDefVertex != null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_ALREADY_EXISTS, relationshipDef.getName());
    }
    relationshipDefVertex = typeDefStore.createTypeVertex(relationshipDef);
    updateVertexPreCreate(relationshipDef, (AtlasRelationshipType) type, relationshipDefVertex);
    final AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1();
    final AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2();
    final String type1 = endDef1.getType();
    final String type2 = endDef2.getType();
    final String name1 = endDef1.getName();
    final String name2 = endDef2.getName();
    final AtlasVertex end1TypeVertex = typeDefStore.findTypeVertexByName(type1);
    final AtlasVertex end2TypeVertex = typeDefStore.findTypeVertexByName(type2);
    if (end1TypeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_END_TYPE_NAME_NOT_FOUND, relationshipDef.getName(), type1);
    }
    if (end2TypeVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_END_TYPE_NAME_NOT_FOUND, relationshipDef.getName(), type2);
    }
    // create an edge between the relationshipDef and each of the entityDef vertices.
    AtlasEdge edge1 = typeDefStore.getOrCreateEdge(relationshipDefVertex, end1TypeVertex, AtlasGraphUtilsV1.RELATIONSHIPTYPE_EDGE_LABEL);
    if (type1.equals(type2) && name1.equals(name2)) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("AtlasRelationshipDefStoreV1.preCreate({}): created relationshipDef vertex {}," + " and one edge as {}, because end1 and end2 have the same type and name", relationshipDef, relationshipDefVertex, edge1);
        }
    } else {
        AtlasEdge edge2 = typeDefStore.getOrCreateEdge(relationshipDefVertex, end2TypeVertex, AtlasGraphUtilsV1.RELATIONSHIPTYPE_EDGE_LABEL);
        if (LOG.isDebugEnabled()) {
            LOG.debug("AtlasRelationshipDefStoreV1.preCreate({}): created relationshipDef vertex {}," + " edge1 as {}, edge2 as {} ", relationshipDef, relationshipDefVertex, edge1, edge2);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AtlasRelationshipDefStoreV1.preCreate({}): {}", relationshipDef, relationshipDefVertex);
    }
    return relationshipDefVertex;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasType(org.apache.atlas.type.AtlasType) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef)

Example 28 with AtlasEdge

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

the class GraphHelper method getPropagatedClassificationEdge.

public static AtlasEdge getPropagatedClassificationEdge(AtlasVertex entityVertex, AtlasVertex classificationVertex) {
    AtlasEdge ret = null;
    Iterable edges = entityVertex.query().direction(AtlasEdgeDirection.OUT).label(CLASSIFICATION_LABEL).has(CLASSIFICATION_EDGE_IS_PROPAGATED_PROPERTY_KEY, true).has(CLASSIFICATION_EDGE_NAME_PROPERTY_KEY, getTypeName(classificationVertex)).edges();
    if (edges != null && classificationVertex != null) {
        Iterator<AtlasEdge> iterator = edges.iterator();
        while (iterator != null && iterator.hasNext()) {
            AtlasEdge edge = iterator.next();
            if (edge != null && edge.getInVertex().equals(classificationVertex)) {
                ret = edge;
                break;
            }
        }
    }
    return ret;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 29 with AtlasEdge

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

the class GraphHelper method getAdjacentEdgesByLabel.

// In some cases of parallel APIs, the edge is added, but get edge by label doesn't return the edge. ATLAS-1104
// So traversing all the edges
public static Iterator<AtlasEdge> getAdjacentEdgesByLabel(AtlasVertex instanceVertex, AtlasEdgeDirection direction, final String edgeLabel) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Finding edges for {} with label {}", string(instanceVertex), edgeLabel);
    }
    if (instanceVertex != null && edgeLabel != null) {
        final Iterator<AtlasEdge> iterator = instanceVertex.getEdges(direction).iterator();
        return new Iterator<AtlasEdge>() {

            private AtlasEdge edge = null;

            @Override
            public boolean hasNext() {
                while (edge == null && iterator.hasNext()) {
                    AtlasEdge localEdge = iterator.next();
                    if (localEdge.getLabel().equals(edgeLabel)) {
                        edge = localEdge;
                    }
                }
                return edge != null;
            }

            @Override
            public AtlasEdge next() {
                if (hasNext()) {
                    AtlasEdge localEdge = edge;
                    edge = null;
                    return localEdge;
                }
                return null;
            }

            @Override
            public void remove() {
                throw new IllegalStateException("Not handled");
            }
        };
    }
    return null;
}
Also used : Iterator(java.util.Iterator) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 30 with AtlasEdge

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

the class GraphHelper method getPropagatedEdges.

public static List<AtlasEdge> getPropagatedEdges(AtlasVertex classificationVertex) {
    List<AtlasEdge> ret = new ArrayList<>();
    Iterable edges = classificationVertex.query().direction(AtlasEdgeDirection.IN).label(CLASSIFICATION_LABEL).has(CLASSIFICATION_EDGE_IS_PROPAGATED_PROPERTY_KEY, true).has(CLASSIFICATION_EDGE_NAME_PROPERTY_KEY, getTypeName(classificationVertex)).edges();
    if (edges != null) {
        Iterator<AtlasEdge> iterator = edges.iterator();
        while (iterator.hasNext()) {
            AtlasEdge edge = iterator.next();
            ret.add(edge);
        }
    }
    return ret;
}
Also used : ArrayList(java.util.ArrayList) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

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