Search in sources :

Example 11 with AtlasEdge

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

the class EntityGraphMapper method deleteClassifications.

public void deleteClassifications(String entityGuid, List<String> classificationNames) throws AtlasBaseException {
    if (CollectionUtils.isEmpty(classificationNames)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_CLASSIFICATION_PARAMS, "delete", entityGuid);
    }
    AtlasVertex entityVertex = AtlasGraphUtilsV1.findByGuid(entityGuid);
    if (entityVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, entityGuid);
    }
    List<String> traitNames = getTraitNames(entityVertex);
    if (CollectionUtils.isEmpty(traitNames)) {
        throw new AtlasBaseException(AtlasErrorCode.NO_CLASSIFICATIONS_FOUND_FOR_ENTITY, entityGuid);
    }
    validateClassificationExists(traitNames, classificationNames);
    Map<AtlasVertex, List<String>> removedClassifications = new HashMap<>();
    for (String classificationName : classificationNames) {
        AtlasVertex classificationVertex = getClassificationVertex(entityVertex, classificationName);
        // remove classification from propagated entities if propagation is turned on
        if (isPropagationEnabled(classificationVertex)) {
            List<AtlasVertex> impactedVertices = removeTagPropagation(classificationVertex);
            if (CollectionUtils.isNotEmpty(impactedVertices)) {
                for (AtlasVertex impactedVertex : impactedVertices) {
                    List<String> classifications = removedClassifications.get(impactedVertex);
                    if (classifications == null) {
                        classifications = new ArrayList<>();
                        removedClassifications.put(impactedVertex, classifications);
                    }
                    classifications.add(classificationName);
                }
            }
        }
        // remove classifications from associated entity
        if (LOG.isDebugEnabled()) {
            LOG.debug("Removing classification: [{}] from: [{}][{}] with edge label: [{}]", classificationName, getTypeName(entityVertex), entityGuid, CLASSIFICATION_LABEL);
        }
        AtlasEdge edge = getClassificationEdge(entityVertex, classificationVertex);
        deleteHandler.deleteEdgeReference(edge, CLASSIFICATION, false, true, entityVertex);
        traitNames.remove(classificationName);
    }
    removedClassifications.put(entityVertex, classificationNames);
    updateTraitNamesProperty(entityVertex, traitNames);
    updateModificationMetadata(entityVertex);
    for (Map.Entry<AtlasVertex, List<String>> entry : removedClassifications.entrySet()) {
        String guid = GraphHelper.getGuid(entry.getKey());
        List<String> deletedClassificationNames = entry.getValue();
        AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(guid);
        AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
        entityChangeNotifier.onClassificationDeletedFromEntity(entity, deletedClassificationNames);
    }
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) 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)

Example 12 with AtlasEdge

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

the class EntityGraphMapper method removeUnusedMapEntries.

// Remove unused entries from map
private Map<String, Object> removeUnusedMapEntries(AtlasAttribute attribute, AtlasVertex vertex, String propertyName, Map<String, Object> currentMap, Map<String, Object> newMap) throws AtlasException, AtlasBaseException {
    AtlasMapType mapType = (AtlasMapType) attribute.getAttributeType();
    Map<String, Object> additionalMap = new HashMap<>();
    for (String currentKey : currentMap.keySet()) {
        boolean shouldDeleteKey = !newMap.containsKey(currentKey);
        if (AtlasGraphUtilsV1.isReference(mapType.getValueType())) {
            // Delete the edge reference if its not part of new edges created/updated
            AtlasEdge currentEdge = (AtlasEdge) currentMap.get(currentKey);
            if (!newMap.values().contains(currentEdge)) {
                boolean deleted = deleteHandler.deleteEdgeReference(currentEdge, mapType.getValueType().getTypeCategory(), attribute.isOwnedRef(), true, vertex);
                if (!deleted) {
                    additionalMap.put(currentKey, currentEdge);
                    shouldDeleteKey = false;
                }
            }
        }
        if (shouldDeleteKey) {
            String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(propertyName, GraphHelper.encodePropertyKey(currentKey));
            GraphHelper.setProperty(vertex, propertyNameForKey, null);
        }
    }
    return additionalMap;
}
Also used : AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType)

Example 13 with AtlasEdge

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

the class EntityGraphMapper method mapStructValue.

private AtlasEdge mapStructValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapStructValue({})", ctx);
    }
    AtlasEdge ret = null;
    if (ctx.getCurrentEdge() != null) {
        AtlasStruct structVal = null;
        if (ctx.getValue() instanceof AtlasStruct) {
            structVal = (AtlasStruct) ctx.getValue();
        } else if (ctx.getValue() instanceof Map) {
            structVal = new AtlasStruct(ctx.getAttrType().getTypeName(), (Map) AtlasTypeUtil.toStructAttributes((Map) ctx.getValue()));
        }
        if (structVal != null) {
            updateVertex(structVal, ctx.getCurrentEdge().getInVertex(), context);
        }
        ret = ctx.getCurrentEdge();
    } else if (ctx.getValue() != null) {
        String edgeLabel = AtlasGraphUtilsV1.getEdgeLabel(ctx.getVertexProperty());
        AtlasStruct structVal = null;
        if (ctx.getValue() instanceof AtlasStruct) {
            structVal = (AtlasStruct) ctx.getValue();
        } else if (ctx.getValue() instanceof Map) {
            structVal = new AtlasStruct(ctx.getAttrType().getTypeName(), (Map) AtlasTypeUtil.toStructAttributes((Map) ctx.getValue()));
        }
        if (structVal != null) {
            ret = createVertex(structVal, ctx.getReferringVertex(), edgeLabel, context);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapStructValue({})", ctx);
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 14 with AtlasEdge

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

the class EntityGraphMapper method mapMapValue.

private Map<String, Object> mapMapValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapMapValue({})", ctx);
    }
    @SuppressWarnings("unchecked") Map<Object, Object> newVal = (Map<Object, Object>) ctx.getValue();
    Map<String, Object> newMap = new HashMap<>();
    AtlasMapType mapType = (AtlasMapType) ctx.getAttrType();
    try {
        AtlasAttribute attribute = ctx.getAttribute();
        List<String> currentKeys = GraphHelper.getListProperty(ctx.getReferringVertex(), ctx.getVertexProperty());
        Map<String, Object> currentMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(currentKeys)) {
            for (String key : currentKeys) {
                String propertyNameForKey = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                Object propertyValueForKey = getMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyNameForKey);
                currentMap.put(key, propertyValueForKey);
            }
        }
        if (MapUtils.isNotEmpty(newVal)) {
            boolean isReference = AtlasGraphUtilsV1.isReference(mapType.getValueType());
            AtlasAttribute inverseRefAttribute = attribute.getInverseRefAttribute();
            for (Map.Entry<Object, Object> entry : newVal.entrySet()) {
                String key = entry.getKey().toString();
                String propertyName = GraphHelper.getQualifiedNameForMapKey(ctx.getVertexProperty(), GraphHelper.encodePropertyKey(key));
                AtlasEdge existingEdge = getEdgeIfExists(mapType, currentMap, key);
                AttributeMutationContext mapCtx = new AttributeMutationContext(ctx.getOp(), ctx.getReferringVertex(), attribute, entry.getValue(), propertyName, mapType.getValueType(), existingEdge);
                // Add/Update/Remove property value
                Object newEntry = mapCollectionElementsToVertex(mapCtx, context);
                setMapValueProperty(mapType.getValueType(), ctx.getReferringVertex(), propertyName, newEntry);
                newMap.put(key, newEntry);
                // update the inverse reference value.
                if (isReference && newEntry instanceof AtlasEdge && inverseRefAttribute != null) {
                    AtlasEdge newEdge = (AtlasEdge) newEntry;
                    addInverseReference(inverseRefAttribute, newEdge, getRelationshipAttributes(ctx.getValue()));
                }
            }
        }
        Map<String, Object> finalMap = removeUnusedMapEntries(attribute, ctx.getReferringVertex(), ctx.getVertexProperty(), currentMap, newMap);
        for (Object newEntry : newMap.values()) {
            updateInConsistentOwnedMapVertices(ctx, mapType, newEntry);
        }
        Set<String> newKeys = new LinkedHashSet<>(newMap.keySet());
        newKeys.addAll(finalMap.keySet());
        // for dereference on way out
        GraphHelper.setListProperty(ctx.getReferringVertex(), ctx.getVertexProperty(), new ArrayList<>(newKeys));
    } catch (AtlasException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapMapValue({})", ctx);
    }
    return newMap;
}
Also used : AtlasException(org.apache.atlas.AtlasException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasMapType(org.apache.atlas.type.AtlasMapType) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException)

Example 15 with AtlasEdge

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

the class EntityGraphMapper method createInverseReference.

// legacy method to create edges for inverse reference
private AtlasEdge createInverseReference(AtlasAttribute inverseAttribute, AtlasStructType inverseAttributeType, AtlasVertex inverseVertex, AtlasVertex vertex) throws AtlasBaseException {
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(inverseAttributeType, inverseAttribute.getName());
    String inverseEdgeLabel = AtlasGraphUtilsV1.getEdgeLabel(propertyName);
    AtlasEdge ret;
    try {
        ret = graphHelper.getOrCreateEdge(inverseVertex, vertex, inverseEdgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) RepositoryException(org.apache.atlas.repository.RepositoryException) 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