Search in sources :

Example 1 with TimeBoundary

use of org.apache.atlas.model.TimeBoundary in project ranger by apache.

the class EntityNotificationWrapper method convertTimeSpecFromAtlasToRanger.

public static List<RangerValiditySchedule> convertTimeSpecFromAtlasToRanger(List<TimeBoundary> atlasTimeSpec) {
    List<RangerValiditySchedule> rangerTimeSpec = null;
    if (CollectionUtils.isNotEmpty(atlasTimeSpec)) {
        rangerTimeSpec = new ArrayList<>();
        for (TimeBoundary validityPeriod : atlasTimeSpec) {
            RangerValiditySchedule validitySchedule = new RangerValiditySchedule();
            validitySchedule.setStartTime(validityPeriod.getStartTime());
            validitySchedule.setEndTime(validityPeriod.getEndTime());
            validitySchedule.setTimeZone(validityPeriod.getTimeZone());
            rangerTimeSpec.add(validitySchedule);
        }
    }
    return rangerTimeSpec;
}
Also used : RangerValiditySchedule(org.apache.ranger.plugin.model.RangerValiditySchedule) TimeBoundary(org.apache.atlas.model.TimeBoundary)

Example 2 with TimeBoundary

use of org.apache.atlas.model.TimeBoundary in project ranger by apache.

the class AtlasRESTTagSource method resolveTag.

/*
     * Returns a list of <EntityNotificationWrapper.RangerAtlasClassification>
     */
private List<EntityNotificationWrapper.RangerAtlasClassification> resolveTag(AtlasTypeRegistry typeRegistry, AtlasClassification classification) {
    List<EntityNotificationWrapper.RangerAtlasClassification> ret = new ArrayList<>();
    String typeName = classification.getTypeName();
    Map<String, Object> attributes = classification.getAttributes();
    try {
        AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(typeName);
        if (classificationType != null) {
            Map<String, String> allAttributes = new HashMap<>();
            if (MapUtils.isNotEmpty(attributes) && MapUtils.isNotEmpty(classificationType.getAllAttributes())) {
                for (Map.Entry<String, Object> attribute : attributes.entrySet()) {
                    String name = attribute.getKey();
                    Object value = attribute.getValue();
                    if (value != null) {
                        String stringValue = value.toString();
                        AtlasStructType.AtlasAttribute atlasAttribute = classificationType.getAttribute(name);
                        if (atlasAttribute != null) {
                            if (value instanceof Number) {
                                if (atlasAttribute.getAttributeType() instanceof AtlasBuiltInTypes.AtlasDateType) {
                                    stringValue = DATE_FORMATTER.get().format(value);
                                }
                            }
                            allAttributes.put(name, stringValue);
                        }
                    }
                }
            }
            List<TimeBoundary> validityPeriods = classification.getValidityPeriods();
            List<RangerValiditySchedule> validitySchedules = null;
            if (CollectionUtils.isNotEmpty(validityPeriods)) {
                validitySchedules = EntityNotificationWrapper.convertTimeSpecFromAtlasToRanger(validityPeriods);
            }
            // Add most derived classificationType with all attributes
            ret.add(new EntityNotificationWrapper.RangerAtlasClassification(typeName, allAttributes, validitySchedules));
            // Find base classification types
            Set<String> superTypeNames = classificationType.getAllSuperTypes();
            for (String superTypeName : superTypeNames) {
                AtlasClassificationType superType = typeRegistry.getClassificationTypeByName(superTypeName);
                if (superType != null) {
                    Map<String, String> attributeMap = new HashMap<>();
                    if (MapUtils.isNotEmpty(attributes) && MapUtils.isNotEmpty(superType.getAllAttributes())) {
                        for (String name : superType.getAllAttributes().keySet()) {
                            String stringValue = allAttributes.get(name);
                            if (stringValue != null) {
                                attributeMap.put(name, stringValue);
                            }
                        }
                    }
                    validityPeriods = classification.getValidityPeriods();
                    validitySchedules = null;
                    if (CollectionUtils.isNotEmpty(validityPeriods)) {
                        validitySchedules = EntityNotificationWrapper.convertTimeSpecFromAtlasToRanger(validityPeriods);
                    }
                    ret.add(new EntityNotificationWrapper.RangerAtlasClassification(superTypeName, attributeMap, validitySchedules));
                }
            }
        }
    } catch (Exception exception) {
        LOG.error("Error in resolving tags for type:[" + typeName + "]", exception);
    }
    return ret;
}
Also used : EntityNotificationWrapper(org.apache.ranger.tagsync.source.atlas.EntityNotificationWrapper) RangerValiditySchedule(org.apache.ranger.plugin.model.RangerValiditySchedule) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasServiceException(org.apache.atlas.AtlasServiceException) IOException(java.io.IOException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) TimeBoundary(org.apache.atlas.model.TimeBoundary) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with TimeBoundary

use of org.apache.atlas.model.TimeBoundary 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 4 with TimeBoundary

use of org.apache.atlas.model.TimeBoundary in project atlas by apache.

the class EntityV2JerseyResourceIT method testAddTraitWithValidityPeriod.

@Test(dependsOnMethods = "testGetTraitNames")
public void testAddTraitWithValidityPeriod() throws Exception {
    traitName = "PII_Trait" + randomString();
    AtlasClassificationDef piiTrait = AtlasTypeUtil.createTraitTypeDef(traitName, Collections.<String>emptySet());
    AtlasTypesDef typesDef = new AtlasTypesDef(Collections.emptyList(), Collections.emptyList(), Collections.singletonList(piiTrait), Collections.emptyList());
    createType(typesDef);
    String tableGuid = createHiveTable().getGuid();
    AtlasClassification classification = new AtlasClassification(piiTrait.getName());
    TimeBoundary validityPeriod = new TimeBoundary("2018/03/01 00:00:00", "2018/04/01 00:00:00", "GMT");
    classification.setEntityGuid(tableGuid);
    classification.addValityPeriod(validityPeriod);
    atlasClientV2.addClassifications(tableGuid, Collections.singletonList(classification));
    assertEntityAudit(tableGuid, EntityAuditEvent.EntityAuditAction.TAG_ADD);
    AtlasClassifications classifications = atlasClientV2.getClassifications(tableGuid);
    assertNotNull(classifications);
    assertNotNull(classifications.getList());
    assertTrue(classifications.getList().size() > 1);
    boolean foundClassification = false;
    for (AtlasClassification entityClassification : classifications.getList()) {
        if (StringUtils.equalsIgnoreCase(entityClassification.getTypeName(), piiTrait.getName())) {
            foundClassification = true;
            assertEquals(entityClassification.getTypeName(), piiTrait.getName());
            assertNotNull(entityClassification.getValidityPeriods());
            assertEquals(entityClassification.getValidityPeriods().size(), 1);
            assertEquals(entityClassification.getValidityPeriods().get(0), validityPeriod);
            assertEquals(entityClassification, classification);
            break;
        }
    }
    assertTrue(foundClassification, "classification '" + piiTrait.getName() + "' is missing for entity '" + tableGuid + "'");
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) TimeBoundary(org.apache.atlas.model.TimeBoundary) AtlasClassifications(org.apache.atlas.model.instance.AtlasClassification.AtlasClassifications) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test)

Aggregations

TimeBoundary (org.apache.atlas.model.TimeBoundary)4 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)2 RangerValiditySchedule (org.apache.ranger.plugin.model.RangerValiditySchedule)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AtlasServiceException (org.apache.atlas.AtlasServiceException)1 AtlasClassifications (org.apache.atlas.model.instance.AtlasClassification.AtlasClassifications)1 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)1 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)1 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)1 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)1 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)1 AtlasStructType (org.apache.atlas.type.AtlasStructType)1 EntityNotificationWrapper (org.apache.ranger.tagsync.source.atlas.EntityNotificationWrapper)1 Test (org.testng.annotations.Test)1