Search in sources :

Example 26 with AtlasAttributeDef

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

the class TestAtlasStructDef method testStructDefSetAttributeDefs.

@Test
public void testStructDefSetAttributeDefs() {
    AtlasStructDef structDef = ModelTestUtil.newStructDef();
    List<AtlasAttributeDef> oldAttributes = structDef.getAttributeDefs();
    List<AtlasAttributeDef> newttributes = ModelTestUtil.newAttributeDefsWithAllBuiltInTypes("newAttributes");
    structDef.setAttributeDefs(newttributes);
    for (AtlasAttributeDef attributeDef : oldAttributes) {
        assertFalse(structDef.hasAttribute(attributeDef.getName()));
    }
    for (AtlasAttributeDef attributeDef : newttributes) {
        assertTrue(structDef.hasAttribute(attributeDef.getName()));
    }
}
Also used : AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) Test(org.testng.annotations.Test)

Example 27 with AtlasAttributeDef

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

the class AtlasStructDefStoreV1 method toJsonFromAttribute.

@VisibleForTesting
public static String toJsonFromAttribute(AtlasAttribute attribute) {
    AtlasAttributeDef attributeDef = attribute.getAttributeDef();
    Map<String, Object> attribInfo = new HashMap<>();
    attribInfo.put("name", attributeDef.getName());
    attribInfo.put("dataType", attributeDef.getTypeName());
    attribInfo.put("isUnique", attributeDef.getIsUnique());
    attribInfo.put("isIndexable", attributeDef.getIsIndexable());
    attribInfo.put("isComposite", attribute.isOwnedRef());
    attribInfo.put("reverseAttributeName", attribute.getInverseRefAttributeName());
    final int lower;
    final int upper;
    if (attributeDef.getCardinality() == AtlasAttributeDef.Cardinality.SINGLE) {
        lower = attributeDef.getIsOptional() ? 0 : 1;
        upper = 1;
    } else {
        if (attributeDef.getIsOptional()) {
            lower = 0;
        } else {
            lower = attributeDef.getValuesMinCount() < 1 ? 1 : attributeDef.getValuesMinCount();
        }
        upper = attributeDef.getValuesMaxCount() < 2 ? Integer.MAX_VALUE : attributeDef.getValuesMaxCount();
    }
    Map<String, Object> multiplicity = new HashMap<>();
    multiplicity.put("lower", lower);
    multiplicity.put("upper", upper);
    multiplicity.put("isUnique", AtlasAttributeDef.Cardinality.SET.equals(attributeDef.getCardinality()));
    attribInfo.put("multiplicity", AtlasType.toJson(multiplicity));
    return AtlasType.toJson(attribInfo);
}
Also used : HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 28 with AtlasAttributeDef

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

the class AtlasStructDefStoreV1 method toStructDef.

public static AtlasStructDef toStructDef(AtlasVertex vertex, AtlasStructDef structDef, AtlasTypeDefGraphStoreV1 typeDefStore) throws AtlasBaseException {
    AtlasStructDef ret = (structDef != null) ? structDef : new AtlasStructDef();
    typeDefStore.vertexToTypeDef(vertex, ret);
    List<AtlasAttributeDef> attributeDefs = new ArrayList<>();
    List<String> attrNames = vertex.getProperty(AtlasGraphUtilsV1.getTypeDefPropertyKey(ret), List.class);
    if (CollectionUtils.isNotEmpty(attrNames)) {
        for (String attrName : attrNames) {
            String propertyKey = AtlasGraphUtilsV1.getTypeDefPropertyKey(ret, attrName);
            String attribJson = vertex.getProperty(GraphHelper.encodePropertyKey(propertyKey), String.class);
            attributeDefs.add(toAttributeDefFromJson(structDef, AtlasType.fromJson(attribJson, Map.class), typeDefStore));
        }
    }
    ret.setAttributeDefs(attributeDefs);
    return ret;
}
Also used : AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) ArrayList(java.util.ArrayList)

Example 29 with AtlasAttributeDef

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

the class AtlasStructDefStoreV1 method toAttributeDefFromJson.

@VisibleForTesting
public static AtlasAttributeDef toAttributeDefFromJson(AtlasStructDef structDef, Map attribInfo, AtlasTypeDefGraphStoreV1 typeDefStore) throws AtlasBaseException {
    AtlasAttributeDef ret = new AtlasAttributeDef();
    ret.setName((String) attribInfo.get("name"));
    ret.setTypeName((String) attribInfo.get("dataType"));
    ret.setIsUnique((Boolean) attribInfo.get("isUnique"));
    ret.setIsIndexable((Boolean) attribInfo.get("isIndexable"));
    if ((Boolean) attribInfo.get("isComposite")) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_OWNED_REF));
    }
    final String reverseAttributeName = (String) attribInfo.get("reverseAttributeName");
    if (StringUtils.isNotBlank(reverseAttributeName)) {
        ret.addConstraint(new AtlasConstraintDef(AtlasConstraintDef.CONSTRAINT_TYPE_INVERSE_REF, new HashMap<String, Object>() {

            {
                put(AtlasConstraintDef.CONSTRAINT_PARAM_ATTRIBUTE, reverseAttributeName);
            }
        }));
    }
    Map multiplicity = AtlasType.fromJson((String) attribInfo.get("multiplicity"), Map.class);
    Number minCount = (Number) multiplicity.get("lower");
    Number maxCount = (Number) multiplicity.get("upper");
    Boolean isUnique = (Boolean) multiplicity.get("isUnique");
    if (minCount == null || minCount.intValue() == 0) {
        ret.setIsOptional(true);
        ret.setValuesMinCount(0);
    } else {
        ret.setIsOptional(false);
        ret.setValuesMinCount(minCount.intValue());
    }
    if (maxCount == null || maxCount.intValue() < 2) {
        ret.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
        ret.setValuesMaxCount(1);
    } else {
        if (isUnique == null || isUnique == Boolean.FALSE) {
            ret.setCardinality(AtlasAttributeDef.Cardinality.LIST);
        } else {
            ret.setCardinality(AtlasAttributeDef.Cardinality.SET);
        }
        ret.setValuesMaxCount(maxCount.intValue());
    }
    return ret;
}
Also used : HashMap(java.util.HashMap) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasConstraintDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef) HashMap(java.util.HashMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 30 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-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 {
    LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getGuid(outVertex);
    AtlasObjectId objId = new AtlasObjectId(outId, typeName);
    AtlasEntity.Status state = AtlasGraphUtilsV1.getState(outVertex);
    if (state == AtlasEntity.Status.DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(objId))) {
        //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) {
                            //if composite attribute, remove the reference as well. else, just remove the edge
                            //for example, when table is deleted, process still references the table
                            //but when column is deleted, table will not reference the deleted column
                            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
                                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();
        GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
        GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
        requestContext.recordEntityUpdate(new AtlasObjectId(outId, typeName));
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) 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) 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)

Aggregations

AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)52 AtlasConstraintDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef)19 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)17 HashMap (java.util.HashMap)14 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)13 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)12 ArrayList (java.util.ArrayList)11 Test (org.testng.annotations.Test)10 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)9 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)8 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)7 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)5 AtlasEnumElementDef (org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef)4 Map (java.util.Map)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)2 ClassType (org.apache.atlas.typesystem.types.ClassType)2