Search in sources :

Example 21 with AtlasObjectId

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

the class EntityGraphMapper method getInstanceType.

private AtlasEntityType getInstanceType(Object val) throws AtlasBaseException {
    AtlasEntityType ret = null;
    if (val != null) {
        String typeName = null;
        if (val instanceof AtlasObjectId) {
            typeName = ((AtlasObjectId) val).getTypeName();
        } else if (val instanceof Map) {
            Object typeNameVal = ((Map) val).get(AtlasObjectId.KEY_TYPENAME);
            if (typeNameVal != null) {
                typeName = typeNameVal.toString();
            }
        }
        ret = typeName != null ? typeRegistry.getEntityTypeByName(typeName) : null;
        if (ret == null) {
            throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, val.toString());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 22 with AtlasObjectId

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

the class EntityGraphMapper method mapObjectIdValue.

private AtlasEdge mapObjectIdValue(AttributeMutationContext ctx, EntityMutationContext context) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> mapObjectIdValue({})", ctx);
    }
    AtlasEdge ret = null;
    String guid = getGuid(ctx.getValue());
    AtlasVertex entityVertex = context.getDiscoveryContext().getResolvedEntityVertex(guid);
    if (entityVertex == null) {
        AtlasObjectId objId = getObjectId(ctx.getValue());
        if (objId != null) {
            entityVertex = context.getDiscoveryContext().getResolvedEntityVertex(objId);
        }
    }
    if (entityVertex == null) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, (ctx.getValue() == null ? null : ctx.getValue().toString()));
    }
    if (ctx.getCurrentEdge() != null) {
        ret = updateEdge(ctx.getAttributeDef(), ctx.getValue(), ctx.getCurrentEdge(), entityVertex);
    } else if (ctx.getValue() != null) {
        String edgeLabel = AtlasGraphUtilsV1.getEdgeLabel(ctx.getVertexProperty());
        try {
            ret = graphHelper.getOrCreateEdge(ctx.getReferringVertex(), entityVertex, edgeLabel);
        } catch (RepositoryException e) {
            throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== mapObjectIdValue({})", ctx);
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 23 with AtlasObjectId

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

the class EntityV2JerseyResourceIT method testCreateNestedEntities.

@Test
public void testCreateNestedEntities() throws Exception {
    AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    AtlasEntity databaseInstance = new AtlasEntity(DATABASE_TYPE_V2, "name", "db1");
    databaseInstance.setAttribute("name", "db1");
    databaseInstance.setAttribute("description", "foo database");
    databaseInstance.setAttribute("owner", "user1");
    databaseInstance.setAttribute("locationUri", "/tmp");
    databaseInstance.setAttribute("createTime", 1000);
    entities.addEntity(databaseInstance);
    int nTables = 5;
    int colsPerTable = 3;
    for (int i = 0; i < nTables; i++) {
        String tableName = "db1-table-" + i;
        AtlasEntity tableInstance = new AtlasEntity(HIVE_TABLE_TYPE_V2, "name", tableName);
        tableInstance.setAttribute(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, tableName);
        tableInstance.setAttribute("db", AtlasTypeUtil.getAtlasObjectId(databaseInstance));
        tableInstance.setAttribute("description", tableName + " table");
        entities.addEntity(tableInstance);
        List<AtlasObjectId> columns = new ArrayList<>();
        for (int j = 0; j < colsPerTable; j++) {
            AtlasEntity columnInstance = new AtlasEntity(COLUMN_TYPE_V2);
            columnInstance.setAttribute("name", tableName + "-col-" + j);
            columnInstance.setAttribute("dataType", "String");
            columnInstance.setAttribute("comment", "column " + j + " for table " + i);
            columns.add(AtlasTypeUtil.getAtlasObjectId(columnInstance));
            entities.addReferredEntity(columnInstance);
        }
        tableInstance.setAttribute("columns", columns);
    }
    //Create the tables.  The database and columns should be created automatically, since
    //the tables reference them.
    EntityMutationResponse response = atlasClientV2.createEntities(entities);
    Assert.assertNotNull(response);
    Map<String, String> guidsCreated = response.getGuidAssignments();
    assertEquals(guidsCreated.size(), nTables * colsPerTable + nTables + 1);
    assertNotNull(guidsCreated.get(databaseInstance.getGuid()));
    for (AtlasEntity r : entities.getEntities()) {
        assertNotNull(guidsCreated.get(r.getGuid()));
    }
    for (AtlasEntity r : entities.getReferredEntities().values()) {
        assertNotNull(guidsCreated.get(r.getGuid()));
    }
}
Also used : AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Test(org.testng.annotations.Test)

Example 24 with AtlasObjectId

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

the class DeleteHandlerV1 method deleteEdgeBetweenVertices.

/**
     * Deletes the edge between outvertex and inVertex. The edge is for attribute attributeName of outVertex
     * @param outVertex
     * @param inVertex
     * @param attribute
     * @throws AtlasException
     */
protected void deleteEdgeBetweenVertices(AtlasVertex outVertex, AtlasVertex inVertex, AtlasAttribute attribute) throws AtlasBaseException {
    LOG.debug("Removing edge from {} to {} with attribute name {}", string(outVertex), string(inVertex), attribute.getName());
    String typeName = GraphHelper.getTypeName(outVertex);
    String outId = GraphHelper.getGuid(outVertex);
    AtlasObjectId objId = new AtlasObjectId(outId, typeName);
    AtlasEntity.Status state = AtlasGraphUtilsV1.getState(outVertex);
    if (state == AtlasEntity.Status.DELETED || (outId != null && RequestContextV1.get().isDeletedEntity(objId))) {
        //If the reference vertex is marked for deletion, skip updating the reference
        return;
    }
    AtlasStructType parentType = (AtlasStructType) typeRegistry.getType(typeName);
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(parentType, attribute.getName());
    String edgeLabel = EDGE_LABEL_PREFIX + propertyName;
    AtlasEdge edge = null;
    AtlasAttributeDef attrDef = attribute.getAttributeDef();
    AtlasType attrType = attribute.getAttributeType();
    switch(attrType.getTypeCategory()) {
        case OBJECT_ID_TYPE:
            //If its class attribute, its the only edge between two vertices
            if (attrDef.getIsOptional()) {
                edge = graphHelper.getEdgeForLabel(outVertex, edgeLabel);
                if (shouldUpdateInverseReferences) {
                    GraphHelper.setProperty(outVertex, propertyName, null);
                }
            } else {
                // Cannot unset a required attribute.
                throw new AtlasBaseException("Cannot unset required attribute " + propertyName + " on " + GraphHelper.vertexString(outVertex) + " edge = " + edgeLabel);
            }
            break;
        case ARRAY:
            //If its array attribute, find the right edge between the two vertices and update array property
            List<String> elements = GraphHelper.getListProperty(outVertex, propertyName);
            if (elements != null) {
                //Make a copy, else list.remove reflects on titan.getProperty()
                elements = new ArrayList<>(elements);
                for (String elementEdgeId : elements) {
                    AtlasEdge elementEdge = graphHelper.getEdgeByEdgeId(outVertex, edgeLabel, elementEdgeId);
                    if (elementEdge == null) {
                        continue;
                    }
                    AtlasVertex elementVertex = elementEdge.getInVertex();
                    if (elementVertex.equals(inVertex)) {
                        edge = elementEdge;
                        //TODO element.size includes deleted items as well. should exclude
                        if (!attrDef.getIsOptional() && elements.size() <= attrDef.getValuesMinCount()) {
                            // Deleting this edge would violate the attribute's lower bound.
                            throw new AtlasBaseException("Cannot remove array element from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(elementEdge));
                        }
                        if (shouldUpdateInverseReferences) {
                            //if composite attribute, remove the reference as well. else, just remove the edge
                            //for example, when table is deleted, process still references the table
                            //but when column is deleted, table will not reference the deleted column
                            LOG.debug("Removing edge {} from the array attribute {}", string(elementEdge), attribute.getName());
                            // Remove all occurrences of the edge ID from the list.
                            // This prevents dangling edge IDs (i.e. edge IDs for deleted edges)
                            // from the remaining in the list if there are duplicates.
                            elements.removeAll(Collections.singletonList(elementEdge.getId().toString()));
                            GraphHelper.setProperty(outVertex, propertyName, elements);
                            break;
                        }
                    }
                }
            }
            break;
        case MAP:
            //If its map attribute, find the right edge between two vertices and update map property
            List<String> keys = GraphHelper.getListProperty(outVertex, propertyName);
            if (keys != null) {
                //Make a copy, else list.remove reflects on titan.getProperty()
                keys = new ArrayList<>(keys);
                for (String key : keys) {
                    String keyPropertyName = GraphHelper.getQualifiedNameForMapKey(propertyName, key);
                    String mapEdgeId = GraphHelper.getSingleValuedProperty(outVertex, keyPropertyName, String.class);
                    AtlasEdge mapEdge = graphHelper.getEdgeByEdgeId(outVertex, keyPropertyName, mapEdgeId);
                    if (mapEdge != null) {
                        AtlasVertex mapVertex = mapEdge.getInVertex();
                        if (mapVertex.getId().toString().equals(inVertex.getId().toString())) {
                            //TODO keys.size includes deleted items as well. should exclude
                            if (attrDef.getIsOptional() || keys.size() > attrDef.getValuesMinCount()) {
                                edge = mapEdge;
                            } else {
                                // Deleting this entry would violate the attribute's lower bound.
                                throw new AtlasBaseException("Cannot remove map entry " + keyPropertyName + " from required attribute " + propertyName + " on " + GraphHelper.getVertexDetails(outVertex) + " " + GraphHelper.getEdgeDetails(mapEdge));
                            }
                            if (shouldUpdateInverseReferences) {
                                //remove this key
                                LOG.debug("Removing edge {}, key {} from the map attribute {}", string(mapEdge), key, attribute.getName());
                                keys.remove(key);
                                GraphHelper.setProperty(outVertex, propertyName, keys);
                                GraphHelper.setProperty(outVertex, keyPropertyName, null);
                            }
                            break;
                        }
                    }
                }
            }
            break;
        case STRUCT:
        case CLASSIFICATION:
            break;
        default:
            throw new IllegalStateException("There can't be an edge from " + GraphHelper.getVertexDetails(outVertex) + " to " + GraphHelper.getVertexDetails(inVertex) + " with attribute name " + attribute.getName() + " which is not class/array/map attribute. found " + attrType.getTypeCategory().name());
    }
    if (edge != null) {
        deleteEdge(edge, false);
        RequestContextV1 requestContext = RequestContextV1.get();
        GraphHelper.setProperty(outVertex, Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, requestContext.getRequestTime());
        GraphHelper.setProperty(outVertex, Constants.MODIFIED_BY_KEY, requestContext.getUser());
        requestContext.recordEntityUpdate(new AtlasObjectId(outId, typeName));
    }
}
Also used : RequestContextV1(org.apache.atlas.RequestContextV1) AtlasStructType(org.apache.atlas.type.AtlasStructType) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Example 25 with AtlasObjectId

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

the class EntityGraphMapper method addInverseReference.

private void addInverseReference(AttributeMutationContext ctx, AtlasAttribute inverseAttribute, AtlasEdge edge) throws AtlasBaseException {
    AtlasStructType inverseType = inverseAttribute.getDefinedInType();
    String propertyName = AtlasGraphUtilsV1.getQualifiedAttributePropertyKey(inverseType, inverseAttribute.getName());
    AtlasVertex vertex = edge.getOutVertex();
    AtlasVertex inverseVertex = edge.getInVertex();
    String inverseEdgeLabel = AtlasGraphUtilsV1.getEdgeLabel(propertyName);
    AtlasEdge inverseEdge = graphHelper.getEdgeForLabel(inverseVertex, inverseEdgeLabel);
    AtlasEdge newEdge;
    try {
        newEdge = graphHelper.getOrCreateEdge(inverseVertex, vertex, inverseEdgeLabel);
    } catch (RepositoryException e) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, e);
    }
    boolean inverseUpdated = true;
    switch(inverseAttribute.getAttributeType().getTypeCategory()) {
        case OBJECT_ID_TYPE:
            if (inverseEdge != null) {
                if (!inverseEdge.equals(newEdge)) {
                    // Disconnect old reference
                    deleteHandler.deleteEdgeReference(inverseEdge, inverseAttribute.getAttributeType().getTypeCategory(), inverseAttribute.isOwnedRef(), true);
                } else {
                    // Edge already exists for this attribute between these vertices.
                    inverseUpdated = false;
                }
            }
            break;
        case ARRAY:
            // Add edge ID to property value
            List<String> elements = inverseVertex.getProperty(propertyName, List.class);
            if (elements == null) {
                elements = new ArrayList<>();
                elements.add(newEdge.getId().toString());
                inverseVertex.setProperty(propertyName, elements);
            } else {
                if (!elements.contains(newEdge.getId().toString())) {
                    elements.add(newEdge.getId().toString());
                    inverseVertex.setProperty(propertyName, elements);
                } else {
                    // Property value list already contains the edge ID.
                    inverseUpdated = false;
                }
            }
            break;
        default:
            break;
    }
    if (inverseUpdated) {
        updateModificationMetadata(inverseVertex);
        AtlasObjectId inverseEntityId = new AtlasObjectId(AtlasGraphUtilsV1.getIdFromVertex(inverseVertex), inverseType.getTypeName());
        RequestContextV1.get().recordEntityUpdate(inverseEntityId);
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasStructType(org.apache.atlas.type.AtlasStructType) RepositoryException(org.apache.atlas.repository.RepositoryException) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Aggregations

AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)46 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)27 Test (org.testng.annotations.Test)17 Map (java.util.Map)15 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)15 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)13 ArrayList (java.util.ArrayList)12 HashMap (java.util.HashMap)11 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)11 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)11 BeforeTest (org.testng.annotations.BeforeTest)11 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)10 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)7 List (java.util.List)6 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)5 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)5 RequestContextV1 (org.apache.atlas.RequestContextV1)4 AtlasExportRequest (org.apache.atlas.model.impexp.AtlasExportRequest)4 TestUtils.randomString (org.apache.atlas.TestUtils.randomString)3 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)3