Search in sources :

Example 11 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityGraphMapper method updateClassifications.

public void updateClassifications(EntityMutationContext context, String guid, List<AtlasClassification> classifications) throws AtlasBaseException {
    if (CollectionUtils.isEmpty(classifications)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_CLASSIFICATION_PARAMS, "update", guid);
    }
    AtlasVertex entityVertex = AtlasGraphUtilsV1.findByGuid(guid);
    if (entityVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    }
    String entityTypeName = AtlasGraphUtilsV1.getTypeName(entityVertex);
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
    List<AtlasClassification> updatedClassifications = new ArrayList<>();
    List<AtlasVertex> entitiesToPropagateTo = new ArrayList<>();
    Map<AtlasVertex, List<AtlasClassification>> addedPropagations = null;
    Map<AtlasVertex, List<String>> removedPropagations = null;
    for (AtlasClassification classification : classifications) {
        String classificationName = classification.getTypeName();
        String classificationEntityGuid = classification.getEntityGuid();
        if (StringUtils.isNotEmpty(classificationEntityGuid) && !StringUtils.equalsIgnoreCase(guid, classificationEntityGuid)) {
            throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_UPDATE_FROM_PROPAGATED_ENTITY, classificationName);
        }
        AtlasVertex classificationVertex = getClassificationVertex(entityVertex, classificationName);
        if (classificationVertex == null) {
            throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_ASSOCIATED_WITH_ENTITY, classificationName);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Updating classification {} for entity {}", classification, guid);
        }
        AtlasClassification currentClassification = entityRetriever.toAtlasClassification(classificationVertex);
        validateAndNormalizeForUpdate(classification);
        boolean isClassificationUpdated = false;
        // check for attribute update
        Map<String, Object> updatedAttributes = classification.getAttributes();
        if (MapUtils.isNotEmpty(updatedAttributes)) {
            for (String attributeName : updatedAttributes.keySet()) {
                currentClassification.setAttribute(attributeName, updatedAttributes.get(attributeName));
            }
            isClassificationUpdated = true;
        }
        // check for validity period update
        List<TimeBoundary> currentValidityPeriods = currentClassification.getValidityPeriods();
        List<TimeBoundary> updatedValidityPeriods = classification.getValidityPeriods();
        if (!Objects.equals(currentValidityPeriods, updatedValidityPeriods)) {
            currentClassification.setValidityPeriods(updatedValidityPeriods);
            isClassificationUpdated = true;
        }
        if (isClassificationUpdated && CollectionUtils.isEmpty(entitiesToPropagateTo)) {
            entitiesToPropagateTo = graphHelper.getImpactedVertices(guid);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("updating vertex {} for trait {}", string(classificationVertex), classificationName);
        }
        mapClassification(EntityOperation.UPDATE, context, classification, entityType, entityVertex, classificationVertex);
        // handle update of 'propagate' flag
        boolean currentTagPropagation = currentClassification.isPropagate();
        boolean updatedTagPropagation = classification.isPropagate();
        // compute propagatedEntityVertices once and use it for subsequent iterations and notifications
        if (currentTagPropagation != updatedTagPropagation) {
            if (updatedTagPropagation) {
                if (CollectionUtils.isEmpty(entitiesToPropagateTo)) {
                    entitiesToPropagateTo = graphHelper.getImpactedVertices(guid);
                }
                if (CollectionUtils.isNotEmpty(entitiesToPropagateTo)) {
                    if (addedPropagations == null) {
                        addedPropagations = new HashMap<>(entitiesToPropagateTo.size());
                        for (AtlasVertex entityToPropagateTo : entitiesToPropagateTo) {
                            addedPropagations.put(entityToPropagateTo, new ArrayList<>());
                        }
                    }
                    List<AtlasVertex> entitiesPropagatedTo = addTagPropagation(classificationVertex, entitiesToPropagateTo);
                    if (entitiesPropagatedTo != null) {
                        for (AtlasVertex entityPropagatedTo : entitiesPropagatedTo) {
                            addedPropagations.get(entityPropagatedTo).add(classification);
                        }
                    }
                }
            } else {
                List<AtlasVertex> impactedVertices = removeTagPropagation(classificationVertex);
                if (CollectionUtils.isNotEmpty(impactedVertices)) {
                    if (removedPropagations == null) {
                        removedPropagations = new HashMap<>();
                        for (AtlasVertex impactedVertex : impactedVertices) {
                            List<String> removedClassifications = removedPropagations.get(impactedVertex);
                            if (removedClassifications == null) {
                                removedClassifications = new ArrayList<>();
                                removedPropagations.put(impactedVertex, removedClassifications);
                            }
                            removedClassifications.add(classification.getTypeName());
                        }
                    }
                }
            }
        }
        updatedClassifications.add(currentClassification);
    }
    // notify listeners on classification update
    List<AtlasVertex> notificationVertices = new ArrayList<AtlasVertex>() {

        {
            add(entityVertex);
        }
    };
    if (CollectionUtils.isNotEmpty(entitiesToPropagateTo)) {
        notificationVertices.addAll(entitiesToPropagateTo);
    }
    for (AtlasVertex vertex : notificationVertices) {
        String entityGuid = GraphHelper.getGuid(vertex);
        AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid);
        AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
        List<AtlasClassification> updatedClassificationList = StringUtils.equals(entityGuid, guid) ? updatedClassifications : Collections.emptyList();
        entityChangeNotifier.onClassificationUpdatedToEntity(entity, updatedClassificationList);
    }
    if (removedPropagations != null) {
        for (Map.Entry<AtlasVertex, List<String>> entry : removedPropagations.entrySet()) {
            AtlasVertex vertex = entry.getKey();
            List<String> removedClassifications = entry.getValue();
            String entityGuid = GraphHelper.getGuid(vertex);
            AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid);
            AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
            entityChangeNotifier.onClassificationDeletedFromEntity(entity, removedClassifications);
        }
    }
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) TimeBoundary(org.apache.atlas.model.TimeBoundary) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 12 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityGraphMapper method createVertex.

private AtlasEdge createVertex(AtlasStruct struct, AtlasVertex referringVertex, String edgeLabel, EntityMutationContext context) throws AtlasBaseException {
    AtlasVertex vertex = createStructVertex(struct);
    mapAttributes(struct, vertex, CREATE, context);
    try {
        // TODO - Map directly in AtlasGraphUtilsV1
        return graphHelper.getOrCreateEdge(referringVertex, vertex, edgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) RepositoryException(org.apache.atlas.repository.RepositoryException)

Example 13 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException in project atlas by apache.

the class EntityGraphMapper method addClassifications.

public void addClassifications(final EntityMutationContext context, String guid, List<AtlasClassification> classifications) throws AtlasBaseException {
    if (CollectionUtils.isNotEmpty(classifications)) {
        AtlasVertex entityVertex = AtlasGraphUtilsV1.findByGuid(guid);
        if (entityVertex == null) {
            throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
        }
        final String entityTypeName = AtlasGraphUtilsV1.getTypeName(entityVertex);
        final AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName);
        List<AtlasVertex> entitiesToPropagateTo = null;
        Map<AtlasVertex, List<AtlasClassification>> propagations = null;
        for (AtlasClassification classification : classifications) {
            String classificationName = classification.getTypeName();
            boolean propagateTags = classification.isPropagate();
            // set associated entity id to classification
            classification.setEntityGuid(guid);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding classification [{}] to [{}] using edge label: [{}]", classificationName, entityTypeName, getTraitLabel(classificationName));
            }
            GraphHelper.addProperty(entityVertex, TRAIT_NAMES_PROPERTY_KEY, classificationName);
            // add a new AtlasVertex for the struct or trait instance
            AtlasVertex classificationVertex = createClassificationVertex(classification);
            if (LOG.isDebugEnabled()) {
                LOG.debug("created vertex {} for trait {}", string(classificationVertex), classificationName);
            }
            // add the attributes for the trait instance
            mapClassification(EntityOperation.CREATE, context, classification, entityType, entityVertex, classificationVertex);
            if (propagateTags) {
                // compute propagatedEntityVertices only once
                if (entitiesToPropagateTo == null) {
                    entitiesToPropagateTo = graphHelper.getImpactedVertices(guid);
                }
                if (CollectionUtils.isNotEmpty(entitiesToPropagateTo)) {
                    if (propagations == null) {
                        propagations = new HashMap<>(entitiesToPropagateTo.size());
                        for (AtlasVertex entityToPropagateTo : entitiesToPropagateTo) {
                            propagations.put(entityToPropagateTo, new ArrayList<>());
                        }
                    }
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Propagating tag: [{}][{}] to {}", classificationName, entityTypeName, getTypeNames(entitiesToPropagateTo));
                    }
                    List<AtlasVertex> entitiesPropagatedTo = addTagPropagation(classificationVertex, entitiesToPropagateTo);
                    if (entitiesPropagatedTo != null) {
                        for (AtlasVertex entityPropagatedTo : entitiesPropagatedTo) {
                            propagations.get(entityPropagatedTo).add(classification);
                        }
                    }
                } else {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug(" --> Not propagating classification: [{}][{}] - no entities found to propagate to.", getTypeName(classificationVertex), entityTypeName);
                    }
                }
            } else {
                if (LOG.isDebugEnabled()) {
                    LOG.debug(" --> Not propagating classification: [{}][{}] - propagation is disabled.", getTypeName(classificationVertex), entityTypeName);
                }
            }
        }
        // notify listeners on classification addition
        List<AtlasVertex> notificationVertices = new ArrayList<AtlasVertex>() {

            {
                add(entityVertex);
            }
        };
        if (CollectionUtils.isNotEmpty(entitiesToPropagateTo)) {
            notificationVertices.addAll(entitiesToPropagateTo);
        }
        for (AtlasVertex vertex : notificationVertices) {
            String entityGuid = GraphHelper.getGuid(vertex);
            AtlasEntityWithExtInfo entityWithExtInfo = instanceConverter.getAndCacheEntity(entityGuid);
            AtlasEntity entity = (entityWithExtInfo != null) ? entityWithExtInfo.getEntity() : null;
            List<AtlasClassification> addedClassifications = StringUtils.equals(entityGuid, guid) ? classifications : propagations.get(vertex);
            if (CollectionUtils.isNotEmpty(addedClassifications)) {
                entityChangeNotifier.onClassificationAddedToEntity(entity, addedClassifications);
            }
        }
    }
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 14 with AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException 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 AtlasBaseException

use of org.apache.atlas.exception.AtlasBaseException 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

AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)437 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)129 Test (org.testng.annotations.Test)60 ArrayList (java.util.ArrayList)59 AtlasType (org.apache.atlas.type.AtlasType)51 AtlasException (org.apache.atlas.AtlasException)50 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)48 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)45 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)43 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)36 HashMap (java.util.HashMap)34 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)33 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)31 Produces (javax.ws.rs.Produces)29 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)29 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)29 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)26 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)26 Path (javax.ws.rs.Path)25 Map (java.util.Map)24