Search in sources :

Example 1 with EntityNotification

use of org.apache.atlas.notification.entity.EntityNotification in project incubator-atlas by apache.

the class NotificationEntityChangeListener method notifyOfEntityEvent.

// send notification of entity change
private void notifyOfEntityEvent(Collection<ITypedReferenceableInstance> entityDefinitions, EntityNotification.OperationType operationType) throws AtlasException {
    List<EntityNotification> messages = new LinkedList<>();
    for (IReferenceableInstance entityDefinition : entityDefinitions) {
        Referenceable entity = new Referenceable(entityDefinition);
        Map<String, Object> attributesMap = entity.getValuesMap();
        List<String> entityNotificationAttrs = getNotificationAttributes(entity.getTypeName());
        if (MapUtils.isNotEmpty(attributesMap) && CollectionUtils.isNotEmpty(entityNotificationAttrs)) {
            for (String entityAttr : attributesMap.keySet()) {
                if (!entityNotificationAttrs.contains(entityAttr)) {
                    entity.setNull(entityAttr);
                }
            }
        }
        EntityNotificationImpl notification = new EntityNotificationImpl(entity, operationType, getAllTraits(entity, typeSystem));
        messages.add(notification);
    }
    notificationInterface.send(NotificationInterface.NotificationType.ENTITIES, messages);
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) EntityNotificationImpl(org.apache.atlas.notification.entity.EntityNotificationImpl) Referenceable(org.apache.atlas.typesystem.Referenceable) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) LinkedList(java.util.LinkedList)

Example 2 with EntityNotification

use of org.apache.atlas.notification.entity.EntityNotification in project incubator-atlas by apache.

the class EntityJerseyResourceIT method testEntityDeduping.

@Test
public void testEntityDeduping() throws Exception {
    final String dbName = "db" + randomString();
    Referenceable HiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    Id dbIdReference = createInstance(HiveDBInstance);
    final String dbId = dbIdReference._getId();
    assertEntityAudit(dbId, EntityAuditEvent.EntityAuditAction.ENTITY_CREATE);
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, new NotificationPredicate() {

        @Override
        public boolean evaluate(EntityNotification notification) throws Exception {
            return notification != null && notification.getEntity().getId()._getId().equals(dbId);
        }
    });
    JSONArray results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
    assertEquals(results.length(), 1);
    //create entity again shouldn't create another instance with same unique attribute value
    List<String> entityResults = atlasClientV1.createEntity(HiveDBInstance);
    assertEquals(entityResults.size(), 0);
    try {
        waitForNotification(notificationConsumer, MAX_WAIT_TIME, new NotificationPredicate() {

            @Override
            public boolean evaluate(EntityNotification notification) throws Exception {
                return notification != null && notification.getEntity().getId()._getId().equals(dbId);
            }
        });
    } catch (Exception e) {
    //expected timeout
    }
    results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
    assertEquals(results.length(), 1);
    //Test the same across references
    Referenceable table = new Referenceable(HIVE_TABLE_TYPE_BUILTIN);
    final String tableName = randomString();
    Referenceable tableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbIdReference);
    atlasClientV1.createEntity(tableInstance);
    results = searchByDSL(String.format("%s where qualifiedName='%s'", DATABASE_TYPE_BUILTIN, dbName));
    assertEquals(results.length(), 1);
}
Also used : Referenceable(org.apache.atlas.typesystem.Referenceable) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) JSONArray(org.codehaus.jettison.json.JSONArray) Id(org.apache.atlas.typesystem.persistence.Id) AtlasServiceException(org.apache.atlas.AtlasServiceException) Test(org.testng.annotations.Test)

Example 3 with EntityNotification

use of org.apache.atlas.notification.entity.EntityNotification in project incubator-atlas by apache.

the class EntityNotificationIT method testAddTrait.

@Test(dependsOnMethods = "testCreateEntity")
public void testAddTrait() throws Exception {
    String superSuperTraitName = "SuperTrait" + randomString();
    createTrait(superSuperTraitName);
    String superTraitName = "SuperTrait" + randomString();
    createTrait(superTraitName, superSuperTraitName);
    traitName = "Trait" + randomString();
    createTrait(traitName, superTraitName);
    Struct traitInstance = new Struct(traitName);
    String traitInstanceJSON = InstanceSerialization.toJson(traitInstance, true);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    final String guid = tableId._getId();
    atlasClientV1.addTrait(guid, traitInstance);
    EntityNotification entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    IReferenceableInstance entity = entityNotification.getEntity();
    assertTrue(entity.getTraits().contains(traitName));
    List<IStruct> allTraits = entityNotification.getAllTraits();
    List<String> allTraitNames = new LinkedList<>();
    for (IStruct struct : allTraits) {
        allTraitNames.add(struct.getTypeName());
    }
    assertTrue(allTraitNames.contains(traitName));
    assertTrue(allTraitNames.contains(superTraitName));
    assertTrue(allTraitNames.contains(superSuperTraitName));
    String anotherTraitName = "Trait" + randomString();
    createTrait(anotherTraitName, superTraitName);
    traitInstance = new Struct(anotherTraitName);
    traitInstanceJSON = InstanceSerialization.toJson(traitInstance, true);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    atlasClientV1.addTrait(guid, traitInstance);
    entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    allTraits = entityNotification.getAllTraits();
    allTraitNames = new LinkedList<>();
    for (IStruct struct : allTraits) {
        allTraitNames.add(struct.getTypeName());
    }
    assertTrue(allTraitNames.contains(traitName));
    assertTrue(allTraitNames.contains(anotherTraitName));
    // verify that the super type shows up twice in all traits
    assertEquals(2, Collections.frequency(allTraitNames, superTraitName));
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 4 with EntityNotification

use of org.apache.atlas.notification.entity.EntityNotification in project incubator-atlas by apache.

the class EntityNotificationIT method testDeleteTrait.

@Test(dependsOnMethods = "testAddTrait")
public void testDeleteTrait() throws Exception {
    final String guid = tableId._getId();
    atlasClientV1.deleteTrait(guid, traitName);
    EntityNotification entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(EntityNotification.OperationType.TRAIT_DELETE, HIVE_TABLE_TYPE_BUILTIN, guid));
    assertFalse(entityNotification.getEntity().getTraits().contains(traitName));
}
Also used : EntityNotification(org.apache.atlas.notification.entity.EntityNotification) Test(org.testng.annotations.Test)

Example 5 with EntityNotification

use of org.apache.atlas.notification.entity.EntityNotification in project incubator-atlas by apache.

the class EntityV2JerseyResourceIT method testEntityDeduping.

@Test
public void testEntityDeduping() throws Exception {
    JSONArray results = searchByDSL(String.format("%s where name='%s'", DATABASE_TYPE_V2, DATABASE_NAME));
    assertEquals(results.length(), 1);
    final AtlasEntity hiveDBInstanceV2 = createHiveDB();
    // Do the notification thing here
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, new NotificationPredicate() {

        @Override
        public boolean evaluate(EntityNotification notification) throws Exception {
            return notification != null && notification.getEntity().getId()._getId().equals(hiveDBInstanceV2.getGuid());
        }
    });
    results = searchByDSL(String.format("%s where name='%s'", DATABASE_TYPE_V2, DATABASE_NAME));
    assertEquals(results.length(), 1);
    //Test the same across references
    final String tableName = randomString();
    AtlasEntity hiveTableInstanceV2 = createHiveTableInstanceV2(hiveDBInstanceV2, tableName);
    hiveTableInstanceV2.setAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, tableName);
    EntityMutationResponse entity = atlasClientV2.createEntity(new AtlasEntityWithExtInfo(hiveTableInstanceV2));
    assertNotNull(entity);
    assertNotNull(entity.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
    results = searchByDSL(String.format("%s where name='%s'", DATABASE_TYPE_V2, DATABASE_NAME));
    assertEquals(results.length(), 1);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityNotification(org.apache.atlas.notification.entity.EntityNotification) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasServiceException(org.apache.atlas.AtlasServiceException) Test(org.testng.annotations.Test)

Aggregations

EntityNotification (org.apache.atlas.notification.entity.EntityNotification)5 Test (org.testng.annotations.Test)4 LinkedList (java.util.LinkedList)2 AtlasServiceException (org.apache.atlas.AtlasServiceException)2 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)2 Referenceable (org.apache.atlas.typesystem.Referenceable)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)1 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)1 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)1 EntityNotificationImpl (org.apache.atlas.notification.entity.EntityNotificationImpl)1 IStruct (org.apache.atlas.typesystem.IStruct)1 Struct (org.apache.atlas.typesystem.Struct)1 Id (org.apache.atlas.typesystem.persistence.Id)1