Search in sources :

Example 86 with AtlasObjectId

use of org.apache.atlas.model.instance.AtlasObjectId in project atlas by apache.

the class HookNotificationTest method testEntityPartialUpdateV2SerDe.

@Test
public void testEntityPartialUpdateV2SerDe() throws Exception {
    AtlasEntity entity1 = new AtlasEntity("sometype");
    AtlasEntity entity2 = new AtlasEntity("newtype");
    AtlasEntity entity3 = new AtlasEntity("othertype");
    setAttributes(entity1);
    entity1.setAttribute("complex", new AtlasObjectId(entity3.getGuid(), entity3.getTypeName()));
    AtlasEntityWithExtInfo entity = new AtlasEntityWithExtInfo(entity1);
    entity.addReferredEntity(entity2);
    entity.addReferredEntity(entity3);
    String user = "user";
    EntityPartialUpdateRequestV2 request = new EntityPartialUpdateRequestV2(user, AtlasTypeUtil.getAtlasObjectId(entity1), entity);
    String notificationJson = AtlasJson.toJson(request);
    HookNotification actualNotification = deserializer.deserialize(notificationJson);
    assertEquals(actualNotification.getType(), HookNotificationType.ENTITY_PARTIAL_UPDATE_V2);
    assertEquals(actualNotification.getUser(), user);
    EntityPartialUpdateRequestV2 updateRequest = (EntityPartialUpdateRequestV2) actualNotification;
    assertEquals(updateRequest.getEntity().getReferredEntities().size(), 2);
    AtlasEntity actualEntity1 = updateRequest.getEntity().getEntity();
    AtlasEntity actualEntity2 = updateRequest.getEntity().getReferredEntity(entity2.getGuid());
    AtlasEntity actualEntity3 = updateRequest.getEntity().getReferredEntity(entity3.getGuid());
    Map actualComplexAttr = (Map) actualEntity1.getAttribute("complex");
    assertEquals(actualEntity1.getGuid(), entity1.getGuid());
    assertEquals(actualEntity1.getTypeName(), entity1.getTypeName());
    assertAttributes(actualEntity1);
    assertEquals(actualComplexAttr.get(AtlasObjectId.KEY_GUID), entity3.getGuid());
    assertEquals(actualComplexAttr.get(AtlasObjectId.KEY_TYPENAME), entity3.getTypeName());
    assertEquals(actualEntity2.getGuid(), entity2.getGuid());
    assertEquals(actualEntity2.getTypeName(), entity2.getTypeName());
    assertEquals(actualEntity3.getGuid(), entity3.getGuid());
    assertEquals(actualEntity3.getTypeName(), entity3.getTypeName());
}
Also used : EntityPartialUpdateRequestV2(org.apache.atlas.model.notification.HookNotification.EntityPartialUpdateRequestV2) HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 87 with AtlasObjectId

use of org.apache.atlas.model.instance.AtlasObjectId in project atlas by apache.

the class HookNotificationTest method testEntityDeleteV2SerDe.

@Test
public void testEntityDeleteV2SerDe() throws Exception {
    AtlasEntity entity1 = new AtlasEntity("sometype");
    AtlasEntity entity2 = new AtlasEntity("newtype");
    AtlasEntity entity3 = new AtlasEntity("othertype");
    List<AtlasObjectId> objectsToDelete = new ArrayList<>();
    objectsToDelete.add(new AtlasObjectId(entity1.getGuid(), entity1.getTypeName()));
    objectsToDelete.add(new AtlasObjectId(entity2.getGuid(), entity2.getTypeName()));
    objectsToDelete.add(new AtlasObjectId(entity3.getGuid(), entity3.getTypeName()));
    String user = "user";
    EntityDeleteRequestV2 request = new EntityDeleteRequestV2(user, objectsToDelete);
    String notificationJson = AtlasJson.toJson(request);
    HookNotification actualNotification = deserializer.deserialize(notificationJson);
    assertEquals(actualNotification.getType(), HookNotificationType.ENTITY_DELETE_V2);
    assertEquals(actualNotification.getUser(), user);
    EntityDeleteRequestV2 deleteRequest = (EntityDeleteRequestV2) actualNotification;
    assertEquals(deleteRequest.getEntities().size(), objectsToDelete.size());
    assertEquals(deleteRequest.getEntities(), objectsToDelete);
}
Also used : HookNotification(org.apache.atlas.model.notification.HookNotification) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) EntityDeleteRequestV2(org.apache.atlas.model.notification.HookNotification.EntityDeleteRequestV2) Test(org.testng.annotations.Test)

Example 88 with AtlasObjectId

use of org.apache.atlas.model.instance.AtlasObjectId in project atlas by apache.

the class DeleteHandlerV1 method getOwnedVertices.

/**
 * Get the GUIDs and vertices for all composite entities owned/contained by the specified root entity AtlasVertex.
 * The graph is traversed from the root entity through to the leaf nodes of the containment graph.
 *
 * @param entityVertex the root entity vertex
 * @return set of VertexInfo for all composite entities
 * @throws AtlasException
 */
public Collection<GraphHelper.VertexInfo> getOwnedVertices(AtlasVertex entityVertex) throws AtlasBaseException {
    final Map<String, GraphHelper.VertexInfo> vertexInfoMap = new HashMap<>();
    final Stack<AtlasVertex> vertices = new Stack<>();
    final boolean isPurgeRequested = RequestContext.get().isPurgeRequested();
    vertices.push(entityVertex);
    while (vertices.size() > 0) {
        AtlasVertex vertex = vertices.pop();
        AtlasEntity.Status state = getState(vertex);
        // If the vertex marked for deletion, skip it
        if (state == (isPurgeRequested ? ACTIVE : DELETED)) {
            continue;
        }
        String guid = GraphHelper.getGuid(vertex);
        if (vertexInfoMap.containsKey(guid)) {
            continue;
        }
        AtlasEntityHeader entity = entityRetriever.toAtlasEntityHeader(vertex);
        String typeName = entity.getTypeName();
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), typeName);
        }
        vertexInfoMap.put(guid, new GraphHelper.VertexInfo(entity, vertex));
        for (AtlasStructType.AtlasAttribute attributeInfo : entityType.getOwnedRefAttributes()) {
            String edgeLabel = attributeInfo.getRelationshipEdgeLabel();
            AtlasType attrType = attributeInfo.getAttributeType();
            TypeCategory typeCategory = attrType.getTypeCategory();
            if (typeCategory == OBJECT_ID_TYPE) {
                if (attributeInfo.getAttributeDef().isSoftReferenced()) {
                    String softRefVal = vertex.getProperty(attributeInfo.getVertexPropertyName(), String.class);
                    AtlasObjectId refObjId = AtlasEntityUtil.parseSoftRefValue(softRefVal);
                    AtlasVertex refVertex = refObjId != null ? AtlasGraphUtilsV2.findByGuid(this.graphHelper.getGraph(), refObjId.getGuid()) : null;
                    if (refVertex != null) {
                        vertices.push(refVertex);
                    }
                } else {
                    AtlasEdge edge = graphHelper.getEdgeForLabel(vertex, edgeLabel);
                    if (edge == null || (getState(edge) == (isPurgeRequested ? ACTIVE : DELETED))) {
                        continue;
                    }
                    vertices.push(edge.getInVertex());
                }
            } else if (typeCategory == ARRAY || typeCategory == MAP) {
                TypeCategory elementType = null;
                if (typeCategory == ARRAY) {
                    elementType = ((AtlasArrayType) attrType).getElementType().getTypeCategory();
                } else if (typeCategory == MAP) {
                    elementType = ((AtlasMapType) attrType).getValueType().getTypeCategory();
                }
                if (elementType != OBJECT_ID_TYPE) {
                    continue;
                }
                if (attributeInfo.getAttributeDef().isSoftReferenced()) {
                    if (typeCategory == ARRAY) {
                        List softRefVal = vertex.getListProperty(attributeInfo.getVertexPropertyName(), List.class);
                        List<AtlasObjectId> refObjIds = AtlasEntityUtil.parseSoftRefValue(softRefVal);
                        if (CollectionUtils.isNotEmpty(refObjIds)) {
                            for (AtlasObjectId refObjId : refObjIds) {
                                AtlasVertex refVertex = AtlasGraphUtilsV2.findByGuid(this.graphHelper.getGraph(), refObjId.getGuid());
                                if (refVertex != null) {
                                    vertices.push(refVertex);
                                }
                            }
                        }
                    } else if (typeCategory == MAP) {
                        Map softRefVal = vertex.getProperty(attributeInfo.getVertexPropertyName(), Map.class);
                        Map<String, AtlasObjectId> refObjIds = AtlasEntityUtil.parseSoftRefValue(softRefVal);
                        if (MapUtils.isNotEmpty(refObjIds)) {
                            for (AtlasObjectId refObjId : refObjIds.values()) {
                                AtlasVertex refVertex = AtlasGraphUtilsV2.findByGuid(this.graphHelper.getGraph(), refObjId.getGuid());
                                if (refVertex != null) {
                                    vertices.push(refVertex);
                                }
                            }
                        }
                    }
                } else {
                    List<AtlasEdge> edges = getCollectionElementsUsingRelationship(vertex, attributeInfo);
                    if (CollectionUtils.isNotEmpty(edges)) {
                        for (AtlasEdge edge : edges) {
                            if (edge == null || (getState(edge) == (isPurgeRequested ? ACTIVE : DELETED))) {
                                continue;
                            }
                            vertices.push(edge.getInVertex());
                        }
                    }
                }
            }
        }
    }
    return vertexInfoMap.values();
}
Also used : GraphHelper(org.apache.atlas.repository.graph.GraphHelper) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) TypeCategory(org.apache.atlas.model.TypeCategory) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader)

Example 89 with AtlasObjectId

use of org.apache.atlas.model.instance.AtlasObjectId in project atlas by apache.

the class AtlasComplexAttributesTest method testDeleteEntityRemoveReferences.

@Test(dependsOnMethods = "testEntityMap")
public void testDeleteEntityRemoveReferences() throws Exception {
    init();
    complexCollectionAttrEntityForDelete.getEntity().setAttribute(NAME, ENTITY_TYPE_WITH_COMPLEX_COLLECTION_ATTR_DELETE);
    EntityMutationResponse response = entityStore.createOrUpdate(new AtlasEntityStream(complexCollectionAttrEntityForDelete), false);
    AtlasEntityHeader entityCreated = response.getFirstCreatedEntityByTypeName(ENTITY_TYPE_WITH_COMPLEX_COLLECTION_ATTR);
    validateEntity(complexCollectionAttrEntityForDelete, getEntityFromStore(entityCreated));
    // delete entity and check if referenced complex attribute edges are deleted
    response = entityStore.deleteById(entityCreated.getGuid());
    AtlasEntityHeader entityDeleted = response.getFirstDeletedEntityByTypeName(ENTITY_TYPE_WITH_COMPLEX_COLLECTION_ATTR);
    GraphTransactionInterceptor.clearCache();
    AtlasEntityWithExtInfo deletedEntityWithExtInfo = entityStore.getById(entityDeleted.getGuid());
    AtlasVertex deletedEntityVertex = AtlasGraphUtilsV2.findByGuid(entityDeleted.getGuid());
    Iterator<AtlasEdge> edges = deletedEntityVertex.getEdges(AtlasEdgeDirection.OUT).iterator();
    // validate all attribute edges are deleted
    while (edges != null && edges.hasNext()) {
        assertEquals(getStatus(edges.next()), AtlasEntity.Status.DELETED);
    }
    AtlasEntity deletedEntity = deletedEntityWithExtInfo.getEntity();
    List<AtlasObjectId> listOfEntities = (List<AtlasObjectId>) deletedEntity.getAttribute("listOfEntities");
    Map<String, AtlasObjectId> mapOfEntities = (Map<String, AtlasObjectId>) deletedEntity.getAttribute("mapOfEntities");
    // validate entity attributes are deleted
    for (AtlasObjectId o : listOfEntities) {
        AtlasEntity entity = deletedEntityWithExtInfo.getEntity(o.getGuid());
        assertEquals(entity.getStatus(), AtlasEntity.Status.DELETED);
    }
    for (AtlasObjectId o : mapOfEntities.values()) {
        AtlasEntity entity = deletedEntityWithExtInfo.getEntity(o.getGuid());
        assertEquals(entity.getStatus(), AtlasEntity.Status.DELETED);
    }
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) AtlasTypeUtil.getAtlasObjectId(org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 90 with AtlasObjectId

use of org.apache.atlas.model.instance.AtlasObjectId in project atlas by apache.

the class AtlasRelationshipStoreHardDeleteV2Test method verifyRelationshipAttributeUpdate_OneToOne_Sibling.

protected void verifyRelationshipAttributeUpdate_OneToOne_Sibling(AtlasEntity julius, AtlasEntity jane, AtlasEntity mike) throws Exception {
    AtlasObjectId juliusId = employeeNameIdMap.get("Julius");
    AtlasObjectId mikeId = employeeNameIdMap.get("Mike");
    // Julius sibling updated to Mike
    AtlasObjectId juliusSiblingId = toAtlasObjectId(julius.getRelationshipAttribute("sibling"));
    assertNotNull(juliusSiblingId);
    assertObjectIdEquals(juliusSiblingId, mikeId);
    // Mike's sibling is Julius
    AtlasObjectId mikeSiblingId = toAtlasObjectId(mike.getRelationshipAttribute("sibling"));
    assertNotNull(mikeSiblingId);
    assertObjectIdEquals(mikeSiblingId, juliusId);
    // Julius removed from Jane's sibling (hard delete)
    AtlasObjectId janeSiblingId = toAtlasObjectId(jane.getRelationshipAttribute("sibling"));
    assertNull(janeSiblingId);
}
Also used : AtlasTypeUtil.getAtlasObjectId(org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId)

Aggregations

AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)255 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)124 Test (org.testng.annotations.Test)70 ArrayList (java.util.ArrayList)69 Map (java.util.Map)47 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)44 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)41 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)41 HashMap (java.util.HashMap)40 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)38 List (java.util.List)36 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)33 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)32 BeforeTest (org.testng.annotations.BeforeTest)32 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)29 AtlasTypeUtil.getAtlasObjectId (org.apache.atlas.type.AtlasTypeUtil.getAtlasObjectId)24 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)22 AtlasExportRequest (org.apache.atlas.model.impexp.AtlasExportRequest)14 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)13 AtlasType (org.apache.atlas.type.AtlasType)11