Search in sources :

Example 6 with AtlasRelationshipType

use of org.apache.atlas.type.AtlasRelationshipType in project incubator-atlas by apache.

the class AtlasRelationshipStoreV1 method createRelationship.

private AtlasRelationship createRelationship(AtlasRelationship relationship, AtlasVertex end1Vertex, AtlasVertex end2Vertex) throws AtlasBaseException {
    AtlasRelationship ret;
    try {
        AtlasEdge relationshipEdge = getRelationshipEdge(end1Vertex, end2Vertex, relationship);
        if (relationshipEdge == null) {
            relationshipEdge = createRelationshipEdge(end1Vertex, end2Vertex, relationship);
            AtlasRelationshipType relationType = typeRegistry.getRelationshipTypeByName(relationship.getTypeName());
            if (MapUtils.isNotEmpty(relationType.getAllAttributes())) {
                for (AtlasAttribute attr : relationType.getAllAttributes().values()) {
                    String attrName = attr.getName();
                    String attrVertexProperty = attr.getVertexPropertyName();
                    Object attrValue = relationship.getAttribute(attrName);
                    AtlasGraphUtilsV1.setProperty(relationshipEdge, attrVertexProperty, attrValue);
                }
            }
            ret = mapEdgeToAtlasRelationship(relationshipEdge);
        } else {
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_ALREADY_EXISTS, relationship.getTypeName(), relationship.getEnd1().getGuid(), relationship.getEnd2().getGuid());
        }
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    return ret;
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasRelationship(org.apache.atlas.model.instance.AtlasRelationship) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 7 with AtlasRelationshipType

use of org.apache.atlas.type.AtlasRelationshipType in project atlas by apache.

the class EntityGraphRetriever method mapAttributes.

private void mapAttributes(AtlasEdge edge, AtlasRelationship relationship) throws AtlasBaseException {
    AtlasType objType = typeRegistry.getType(relationship.getTypeName());
    if (!(objType instanceof AtlasRelationshipType)) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, relationship.getTypeName());
    }
    AtlasRelationshipType relationshipType = (AtlasRelationshipType) objType;
    for (AtlasAttribute attribute : relationshipType.getAllAttributes().values()) {
        // mapping only primitive attributes
        Object attrValue = mapVertexToPrimitive(edge, attribute.getQualifiedName(), attribute.getAttributeDef());
        relationship.setAttribute(attribute.getName(), attrValue);
    }
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasType(org.apache.atlas.type.AtlasType)

Example 8 with AtlasRelationshipType

use of org.apache.atlas.type.AtlasRelationshipType in project atlas by apache.

the class GraphHelper method getRelationshipDef.

public AtlasRelationshipDef getRelationshipDef(AtlasVertex entityVertex, AtlasEntityType entityType, String attributeName) {
    List<AtlasRelationshipType> relationshipTypes = entityType.getRelationshipAttributeType(attributeName);
    AtlasRelationshipDef ret = null;
    if (relationshipTypes.size() > 1) {
        Iterator<AtlasEdge> iter = entityVertex.getEdges(AtlasEdgeDirection.IN).iterator();
        while (iter.hasNext() && ret == null) {
            String edgeTypeName = AtlasGraphUtilsV1.getTypeName(iter.next());
            for (AtlasRelationshipType relationType : relationshipTypes) {
                AtlasRelationshipDef relationshipDef = relationType.getRelationshipDef();
                if (StringUtils.equals(edgeTypeName, relationshipDef.getName())) {
                    ret = relationshipDef;
                    break;
                }
            }
        }
        if (ret == null) {
            ret = relationshipTypes.get(0).getRelationshipDef();
        }
    } else {
        // relationshipTypes will have at least one relationshipDef
        ret = relationshipTypes.get(0).getRelationshipDef();
    }
    return ret;
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasRelationshipDef(org.apache.atlas.model.typedef.AtlasRelationshipDef) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 9 with AtlasRelationshipType

use of org.apache.atlas.type.AtlasRelationshipType in project atlas by apache.

the class AtlasRelationshipStoreV1 method getRelationshipEdgeLabel.

private String getRelationshipEdgeLabel(AtlasVertex fromVertex, AtlasVertex toVertex, String relationshipTypeName) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getRelationshipEdgeLabel({})", relationshipTypeName);
    }
    AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relationshipTypeName);
    String ret = relationshipType.getRelationshipDef().getRelationshipLabel();
    AtlasRelationshipEndDef endDef1 = relationshipType.getRelationshipDef().getEndDef1();
    AtlasRelationshipEndDef endDef2 = relationshipType.getRelationshipDef().getEndDef2();
    Set<String> fromVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV1.getTypeName(fromVertex));
    Set<String> toVertexTypes = getTypeAndAllSuperTypes(AtlasGraphUtilsV1.getTypeName(toVertex));
    AtlasAttribute attribute = null;
    // e.g. [hive_process -> hive_table] -> [Process -> DataSet]
    if (fromVertexTypes.contains(endDef1.getType()) && toVertexTypes.contains(endDef2.getType())) {
        String attributeName = endDef1.getName();
        attribute = relationshipType.getEnd1Type().getRelationshipAttribute(attributeName);
    } else if (fromVertexTypes.contains(endDef2.getType()) && toVertexTypes.contains(endDef1.getType())) {
        String attributeName = endDef2.getName();
        attribute = relationshipType.getEnd2Type().getRelationshipAttribute(attributeName);
    }
    if (attribute != null) {
        ret = attribute.getRelationshipEdgeLabel();
    }
    return ret;
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef)

Example 10 with AtlasRelationshipType

use of org.apache.atlas.type.AtlasRelationshipType in project atlas by apache.

the class AtlasRelationshipStoreV1 method validateAndNormalize.

private void validateAndNormalize(AtlasRelationship relationship) throws AtlasBaseException {
    List<String> messages = new ArrayList<>();
    if (!AtlasTypeUtil.isValidGuid(relationship.getGuid())) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_GUID_NOT_FOUND, relationship.getGuid());
    }
    AtlasRelationshipType type = typeRegistry.getRelationshipTypeByName(relationship.getTypeName());
    if (type == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.RELATIONSHIP.name(), relationship.getTypeName());
    }
    type.validateValue(relationship, relationship.getTypeName(), messages);
    if (!messages.isEmpty()) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIP_CRUD_INVALID_PARAMS, messages);
    }
    type.getNormalizedValue(relationship);
}
Also used : AtlasRelationshipType(org.apache.atlas.type.AtlasRelationshipType) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ArrayList(java.util.ArrayList)

Aggregations

AtlasRelationshipType (org.apache.atlas.type.AtlasRelationshipType)16 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)9 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)8 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)4 ArrayList (java.util.ArrayList)3 AtlasRelationshipDef (org.apache.atlas.model.typedef.AtlasRelationshipDef)3 AtlasRelationshipEndDef (org.apache.atlas.model.typedef.AtlasRelationshipEndDef)3 AtlasRelationship (org.apache.atlas.model.instance.AtlasRelationship)2 RepositoryException (org.apache.atlas.repository.RepositoryException)2 AtlasType (org.apache.atlas.type.AtlasType)2 PropagateTags (org.apache.atlas.model.typedef.AtlasRelationshipDef.PropagateTags)1 GraphHelper.getPropagateTags (org.apache.atlas.repository.graph.GraphHelper.getPropagateTags)1 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)1