Search in sources :

Example 31 with AtlasRelationshipEndDef

use of org.apache.atlas.model.typedef.AtlasRelationshipEndDef in project incubator-atlas by apache.

the class AtlasRelationshipDefStoreV1 method preUpdateCheck.

/**
 * Check ends are the same and relationshipCategory is the same.
 *
 * We do this by comparing 2 relationshipDefs to avoid exposing the AtlasVertex to unit testing.
 *
 * @param newRelationshipDef
 * @param existingRelationshipDef
 * @throws AtlasBaseException
 */
public static void preUpdateCheck(AtlasRelationshipDef newRelationshipDef, AtlasRelationshipDef existingRelationshipDef) throws AtlasBaseException {
    // do not allow renames of the Def.
    String existingName = existingRelationshipDef.getName();
    String newName = newRelationshipDef.getName();
    if (!existingName.equals(newName)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_NAME_UPDATE, newRelationshipDef.getGuid(), existingName, newName);
    }
    RelationshipCategory existingRelationshipCategory = existingRelationshipDef.getRelationshipCategory();
    RelationshipCategory newRelationshipCategory = newRelationshipDef.getRelationshipCategory();
    if (!existingRelationshipCategory.equals(newRelationshipCategory)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_CATEGORY_UPDATE, newRelationshipDef.getName(), newRelationshipCategory.name(), existingRelationshipCategory.name());
    }
    AtlasRelationshipEndDef existingEnd1 = existingRelationshipDef.getEndDef1();
    AtlasRelationshipEndDef newEnd1 = newRelationshipDef.getEndDef1();
    if (!newEnd1.equals(existingEnd1)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_END1_UPDATE, newRelationshipDef.getName(), newEnd1.toString(), existingEnd1.toString());
    }
    AtlasRelationshipEndDef existingEnd2 = existingRelationshipDef.getEndDef2();
    AtlasRelationshipEndDef newEnd2 = newRelationshipDef.getEndDef2();
    if (!newEnd2.equals(existingEnd2)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_INVALID_END2_UPDATE, newRelationshipDef.getName(), newEnd2.toString(), existingEnd2.toString());
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef) RelationshipCategory(org.apache.atlas.model.typedef.AtlasRelationshipDef.RelationshipCategory)

Example 32 with AtlasRelationshipEndDef

use of org.apache.atlas.model.typedef.AtlasRelationshipEndDef in project incubator-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 33 with AtlasRelationshipEndDef

use of org.apache.atlas.model.typedef.AtlasRelationshipEndDef in project incubator-atlas by apache.

the class AtlasRelationshipType method resolveReferencesPhase2.

@Override
public void resolveReferencesPhase2(AtlasTypeRegistry typeRegistry) throws AtlasBaseException {
    super.resolveReferencesPhase2(typeRegistry);
    AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1();
    AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2();
    String relationshipLabel = null;
    // if legacyLabel is specified at both ends use the respective end's legacyLabel as relationship label (legacy case).
    if (!endDef1.getIsLegacyAttribute() && !endDef2.getIsLegacyAttribute()) {
        relationshipLabel = relationshipDef.getRelationshipLabel();
    } else if (endDef1.getIsLegacyAttribute() && !endDef2.getIsLegacyAttribute()) {
        relationshipLabel = getLegacyEdgeLabel(end1Type, endDef1.getName());
    } else if (!endDef1.getIsLegacyAttribute() && endDef2.getIsLegacyAttribute()) {
        relationshipLabel = getLegacyEdgeLabel(end2Type, endDef2.getName());
    }
    addRelationshipAttributeToEndType(endDef1, end1Type, end2Type.getTypeName(), typeRegistry, relationshipLabel);
    addRelationshipAttributeToEndType(endDef2, end2Type, end1Type.getTypeName(), typeRegistry, relationshipLabel);
    // add relationship edge direction information
    addRelationshipEdgeDirection();
}
Also used : AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef)

Example 34 with AtlasRelationshipEndDef

use of org.apache.atlas.model.typedef.AtlasRelationshipEndDef in project incubator-atlas by apache.

the class AtlasRelationshipType method validateAtlasRelationshipDef.

/**
 * Throw an exception so we can junit easily.
 *
 * This method assumes that the 2 ends are not null.
 *
 * @param relationshipDef
 * @throws AtlasBaseException
 */
public static void validateAtlasRelationshipDef(AtlasRelationshipDef relationshipDef) throws AtlasBaseException {
    AtlasRelationshipEndDef endDef1 = relationshipDef.getEndDef1();
    AtlasRelationshipEndDef endDef2 = relationshipDef.getEndDef2();
    RelationshipCategory relationshipCategory = relationshipDef.getRelationshipCategory();
    String name = relationshipDef.getName();
    boolean isContainer1 = endDef1.getIsContainer();
    boolean isContainer2 = endDef2.getIsContainer();
    if ((endDef1.getCardinality() == AtlasAttributeDef.Cardinality.LIST) || (endDef2.getCardinality() == AtlasAttributeDef.Cardinality.LIST)) {
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_LIST_ON_END, name);
    }
    if (isContainer1 && isContainer2) {
        // we support 0 or 1 of these flags.
        throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_DOUBLE_CONTAINERS, name);
    }
    if ((isContainer1 || isContainer2)) {
        // we have an isContainer defined in an end
        if (relationshipCategory == RelationshipCategory.ASSOCIATION) {
            // associations are not containment relationships - so do not allow an endpoint with isContainer
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_ASSOCIATION_AND_CONTAINER, name);
        }
    } else {
        // we do not have an isContainer defined on an end
        if (relationshipCategory == RelationshipCategory.COMPOSITION) {
            // COMPOSITION needs one end to be the container.
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_COMPOSITION_NO_CONTAINER, name);
        } else if (relationshipCategory == RelationshipCategory.AGGREGATION) {
            // AGGREGATION needs one end to be the container.
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_AGGREGATION_NO_CONTAINER, name);
        }
    }
    if (relationshipCategory == RelationshipCategory.COMPOSITION) {
        // composition children should not be multiple cardinality
        if (endDef1.getCardinality() == AtlasAttributeDef.Cardinality.SET && !endDef1.getIsContainer()) {
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_COMPOSITION_MULTIPLE_PARENTS, name);
        }
        if ((endDef2.getCardinality() == AtlasAttributeDef.Cardinality.SET) && !endDef2.getIsContainer()) {
            throw new AtlasBaseException(AtlasErrorCode.RELATIONSHIPDEF_COMPOSITION_MULTIPLE_PARENTS, name);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef) RelationshipCategory(org.apache.atlas.model.typedef.AtlasRelationshipDef.RelationshipCategory)

Example 35 with AtlasRelationshipEndDef

use of org.apache.atlas.model.typedef.AtlasRelationshipEndDef in project incubator-atlas by apache.

the class TestAtlasRelationshipType method createRelationshipTypes.

private void createRelationshipTypes() throws AtlasBaseException {
    AtlasRelationshipDef deptEmployeeRelationDef = new AtlasRelationshipDef(DEPT_EMPLOYEE_RELATION_TYPE, getDescription(DEPT_EMPLOYEE_RELATION_TYPE), "1.0", RelationshipCategory.ASSOCIATION, PropagateTags.ONE_TO_TWO, new AtlasRelationshipEndDef(EMPLOYEE_TYPE, "department", Cardinality.SINGLE), new AtlasRelationshipEndDef(DEPARTMENT_TYPE, "employees", Cardinality.SET));
    AtlasRelationshipDef employeeAddrRelationDef = new AtlasRelationshipDef(EMPLOYEE_ADDRESS_RELATION_TYPE, getDescription(EMPLOYEE_ADDRESS_RELATION_TYPE), "1.0", RelationshipCategory.ASSOCIATION, PropagateTags.ONE_TO_TWO, new AtlasRelationshipEndDef(EMPLOYEE_TYPE, "address", Cardinality.SINGLE), new AtlasRelationshipEndDef(ADDRESS_TYPE, "employees", Cardinality.SET));
    createTypes(new ArrayList<>(Arrays.asList(deptEmployeeRelationDef, employeeAddrRelationDef)));
}
Also used : AtlasRelationshipDef(org.apache.atlas.model.typedef.AtlasRelationshipDef) AtlasRelationshipEndDef(org.apache.atlas.model.typedef.AtlasRelationshipEndDef)

Aggregations

AtlasRelationshipEndDef (org.apache.atlas.model.typedef.AtlasRelationshipEndDef)40 AtlasRelationshipDef (org.apache.atlas.model.typedef.AtlasRelationshipDef)25 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)13 DataProvider (org.testng.annotations.DataProvider)10 RelationshipCategory (org.apache.atlas.model.typedef.AtlasRelationshipDef.RelationshipCategory)6 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)4 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)4 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)4 Test (org.testng.annotations.Test)4 PropagateTags (org.apache.atlas.model.typedef.AtlasRelationshipDef.PropagateTags)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 AtlasRelationshipType (org.apache.atlas.type.AtlasRelationshipType)3 AtlasException (org.apache.atlas.AtlasException)2 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)2 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)2 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)2 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 AtlasRelationshipEdgeDirection (org.apache.atlas.type.AtlasStructType.AtlasAttribute.AtlasRelationshipEdgeDirection)2 AtlasType (org.apache.atlas.type.AtlasType)2