Search in sources :

Example 26 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.

the class EntityResource method getEntityDefinitionByAttribute.

/**
 * Fetch the complete definition of an entity given its qualified name.
 *
 * @param entityType
 * @param attribute
 * @param value
 */
public Response getEntityDefinitionByAttribute(String entityType, String attribute, String value) {
    try {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching entity definition for type={}, qualified name={}", entityType, value);
        }
        entityType = ParamChecker.notEmpty(entityType, "Entity type cannot be null");
        attribute = ParamChecker.notEmpty(attribute, "attribute name cannot be null");
        value = ParamChecker.notEmpty(value, "attribute value cannot be null");
        Map<String, Object> attributes = new HashMap<>();
        attributes.put(attribute, value);
        AtlasEntityWithExtInfo entityInfo;
        try {
            entityInfo = entitiesStore.getByUniqueAttributes(getEntityType(entityType), attributes);
        } catch (AtlasBaseException e) {
            LOG.error("Cannot find entity with type: {}, attribute: {} and value: {}", entityType, attribute, value);
            throw toWebApplicationException(e);
        }
        Referenceable entity = null;
        if (entityInfo != null) {
            entity = restAdapters.getReferenceable(entityInfo);
        }
        Map<String, Object> response = new HashMap<>();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        Response.Status status = Response.Status.NOT_FOUND;
        if (entity != null) {
            response.put(AtlasClient.DEFINITION, entity);
            status = Response.Status.OK;
        } else {
            response.put(AtlasClient.ERROR, Servlets.escapeJsonString(String.format("An entity with type={%s}, " + "qualifiedName={%s} does not exist", entityType, value)));
        }
        return Response.status(status).entity(AtlasJson.toV1Json(response)).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Bad type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get instance definition for type={}, qualifiedName={}", entityType, value, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    }
}
Also used : Response(javax.ws.rs.core.Response) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap)

Example 27 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.

the class EntityNotificationIT method testDeleteEntity.

public void testDeleteEntity() throws Exception {
    final String tableName = "table-" + randomString();
    final String dbName = "db-" + randomString();
    final Referenceable HiveDBInstance = createHiveDBInstanceBuiltIn(dbName);
    final Id dbId = createInstance(HiveDBInstance);
    final Referenceable tableInstance = createHiveTableInstanceBuiltIn(dbName, tableName, dbId);
    final Id tableId = createInstance(tableInstance);
    final String guid = tableId._getId();
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.ENTITY_CREATE, HIVE_TABLE_TYPE_BUILTIN, guid));
    final String name = (String) tableInstance.get(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME);
    atlasClientV1.deleteEntity(HIVE_TABLE_TYPE_BUILTIN, AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, name);
    waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.ENTITY_DELETE, HIVE_TABLE_TYPE_BUILTIN, guid));
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Id(org.apache.atlas.v1.model.instance.Id)

Example 28 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.

the class EntityNotificationIT method testAddTrait.

public void testAddTrait() throws Exception {
    String superSuperTraitName = "SuperTrait" + randomString();
    String superTraitName = "SuperTrait" + randomString();
    traitName = "Trait" + randomString();
    createTrait(superSuperTraitName);
    createTrait(superTraitName, superSuperTraitName);
    createTrait(traitName, superTraitName);
    Struct traitInstance = new Struct(traitName);
    String traitInstanceJSON = AtlasType.toV1Json(traitInstance);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    final String guid = tableId._getId();
    atlasClientV1.addTrait(guid, traitInstance);
    EntityNotificationV1 entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    Referenceable entity = entityNotification.getEntity();
    assertTrue(entity.getTraitNames().contains(traitName));
    List<Struct> allTraits = entityNotification.getAllTraits();
    List<String> allTraitNames = new LinkedList<>();
    for (Struct 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 = AtlasType.toV1Json(traitInstance);
    LOG.debug("Trait instance = {}", traitInstanceJSON);
    atlasClientV1.addTrait(guid, traitInstance);
    entityNotification = waitForNotification(notificationConsumer, MAX_WAIT_TIME, newNotificationPredicate(OperationType.TRAIT_ADD, HIVE_TABLE_TYPE_BUILTIN, guid));
    allTraits = entityNotification.getAllTraits();
    allTraitNames = new LinkedList<>();
    for (Struct 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 : EntityNotificationV1(org.apache.atlas.v1.model.notification.EntityNotificationV1) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 29 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.

the class NotificationHookConsumerIT method testUpdateEntityPartial.

@Test
public void testUpdateEntityPartial() throws Exception {
    final Referenceable entity = new Referenceable(DATABASE_TYPE_BUILTIN);
    final String dbName = "db" + randomString();
    entity.set(NAME, dbName);
    entity.set(DESCRIPTION, randomString());
    entity.set(QUALIFIED_NAME, dbName);
    entity.set(CLUSTER_NAME, randomString());
    atlasClientV1.createEntity(entity);
    final Referenceable newEntity = new Referenceable(DATABASE_TYPE_BUILTIN);
    newEntity.set("owner", randomString());
    sendHookMessage(new EntityPartialUpdateRequest(TEST_USER, DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName, newEntity));
    waitFor(MAX_WAIT_TIME, new Predicate() {

        @Override
        public boolean evaluate() throws Exception {
            Referenceable localEntity = atlasClientV1.getEntity(DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName);
            return (localEntity.get("owner") != null && localEntity.get("owner").equals(newEntity.get("owner")));
        }
    });
    // Its partial update and un-set fields are not updated
    Referenceable actualEntity = atlasClientV1.getEntity(DATABASE_TYPE_BUILTIN, QUALIFIED_NAME, dbName);
    assertEquals(actualEntity.get(DESCRIPTION), entity.get(DESCRIPTION));
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) EntityPartialUpdateRequest(org.apache.atlas.v1.model.notification.HookNotificationV1.EntityPartialUpdateRequest) Test(org.testng.annotations.Test)

Example 30 with Referenceable

use of org.apache.atlas.v1.model.instance.Referenceable in project atlas by apache.

the class NotificationHookConsumerIT method testMessageHandleFailureConsumerContinues.

@Test
public void testMessageHandleFailureConsumerContinues() throws Exception {
    // send invalid message - update with invalid type
    sendHookMessage(new EntityPartialUpdateRequest(TEST_USER, randomString(), null, null, new Referenceable(randomString())));
    // send valid message
    final Referenceable entity = new Referenceable(DATABASE_TYPE_BUILTIN);
    final String dbName = "db" + randomString();
    entity.set(NAME, dbName);
    entity.set(DESCRIPTION, randomString());
    entity.set(QUALIFIED_NAME, dbName);
    entity.set(CLUSTER_NAME, randomString());
    sendHookMessage(new EntityCreateRequest(TEST_USER, entity));
    waitFor(MAX_WAIT_TIME, new Predicate() {

        @Override
        public boolean evaluate() throws Exception {
            ArrayNode results = searchByDSL(String.format("%s where name='%s'", DATABASE_TYPE_BUILTIN, entity.get(NAME)));
            return results.size() == 1;
        }
    });
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) EntityPartialUpdateRequest(org.apache.atlas.v1.model.notification.HookNotificationV1.EntityPartialUpdateRequest) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) EntityCreateRequest(org.apache.atlas.v1.model.notification.HookNotificationV1.EntityCreateRequest) Test(org.testng.annotations.Test)

Aggregations

Referenceable (org.apache.atlas.v1.model.instance.Referenceable)143 Test (org.testng.annotations.Test)64 Id (org.apache.atlas.v1.model.instance.Id)37 Struct (org.apache.atlas.v1.model.instance.Struct)23 ArrayList (java.util.ArrayList)20 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)15 HashMap (java.util.HashMap)12 List (java.util.List)10 AtlasException (org.apache.atlas.AtlasException)10 HookNotification (org.apache.atlas.model.notification.HookNotification)10 Map (java.util.Map)9 EntityUpdateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityUpdateRequest)9 AtlasServiceException (org.apache.atlas.AtlasServiceException)8 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)8 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)7 EntityCreateRequest (org.apache.atlas.v1.model.notification.HookNotificationV1.EntityCreateRequest)7 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)6 EntityResult (org.apache.atlas.model.legacy.EntityResult)6 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)6 EntityNotificationTest (org.apache.atlas.notification.entity.EntityNotificationTest)5