Search in sources :

Example 6 with AtlasObjectId

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

the class AtlasObjectIdConverter method fromV1ToV2.

@Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, AtlasFormatConverter.ConverterContext converterContext) throws AtlasBaseException {
    Object ret = null;
    if (v1Obj != null) {
        if (v1Obj instanceof Id) {
            Id id = (Id) v1Obj;
            ret = new AtlasObjectId(id._getId(), id.getTypeName());
        } else if (v1Obj instanceof IReferenceableInstance) {
            IReferenceableInstance refInst = (IReferenceableInstance) v1Obj;
            String guid = refInst.getId()._getId();
            ret = new AtlasObjectId(guid, refInst.getTypeName());
            if (!converterContext.entityExists(guid) && hasAnyAssignedAttribute(refInst)) {
                AtlasEntityType entityType = typeRegistry.getEntityTypeByName(refInst.getTypeName());
                AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) converterRegistry.getConverter(TypeCategory.ENTITY);
                AtlasEntity entity = converter.fromV1ToV2(v1Obj, entityType, converterContext);
                converterContext.addReferredEntity(entity);
            }
        }
    }
    return ret;
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 7 with AtlasObjectId

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

the class UniqAttrBasedEntityResolver method resolveEntityReferences.

@Override
public EntityGraphDiscoveryContext resolveEntityReferences(EntityGraphDiscoveryContext context) throws AtlasBaseException {
    if (context == null) {
        throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "UniqAttrBasedEntityResolver.resolveEntityReferences(): context is null");
    }
    //Resolve attribute references
    List<AtlasObjectId> resolvedReferences = new ArrayList<>();
    for (AtlasObjectId objId : context.getReferencedByUniqAttribs()) {
        //query in graph repo that given unique attribute - check for deleted also?
        AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objId.getTypeName());
        if (entityType == null) {
            throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), objId.getTypeName());
        }
        AtlasVertex vertex = AtlasGraphUtilsV1.findByUniqueAttributes(entityType, objId.getUniqueAttributes());
        if (vertex != null) {
            context.addResolvedIdByUniqAttribs(objId, vertex);
            resolvedReferences.add(objId);
        } else {
            throw new AtlasBaseException(AtlasErrorCode.REFERENCED_ENTITY_NOT_FOUND, objId.toString());
        }
    }
    return context;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ArrayList(java.util.ArrayList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 8 with AtlasObjectId

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

the class EntityV2JerseyResourceIT method testCompleteUpdate.

@Test(dependsOnMethods = "testSubmitEntity")
public void testCompleteUpdate() throws Exception {
    final List<AtlasEntity> columns = new ArrayList<>();
    Map<String, Object> values1 = new HashMap<>();
    values1.put("name", "col3");
    values1.put(NAME, "qualifiedName.col3");
    values1.put("type", "string");
    values1.put("comment", "col3 comment");
    Map<String, Object> values2 = new HashMap<>();
    values2.put("name", "col4");
    values2.put(NAME, "qualifiedName.col4");
    values2.put("type", "string");
    values2.put("comment", "col4 comment");
    AtlasEntity colEntity1 = new AtlasEntity(BaseResourceIT.COLUMN_TYPE_V2, values1);
    AtlasEntity colEntity2 = new AtlasEntity(BaseResourceIT.COLUMN_TYPE_V2, values2);
    columns.add(colEntity1);
    columns.add(colEntity2);
    AtlasEntity hiveTable = createHiveTable();
    hiveTable.setAttribute("columns", AtlasTypeUtil.toObjectIds(columns));
    AtlasEntityWithExtInfo entityInfo = new AtlasEntityWithExtInfo(hiveTable);
    entityInfo.addReferredEntity(colEntity1);
    entityInfo.addReferredEntity(colEntity2);
    EntityMutationResponse updateEntityResult = atlasClientV2.updateEntity(entityInfo);
    assertNotNull(updateEntityResult);
    assertNotNull(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE));
    assertNotNull(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE));
    //2 columns are being created, and 1 hiveTable is being updated
    assertEquals(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.UPDATE).size(), 1);
    assertEquals(updateEntityResult.getEntitiesByOperation(EntityMutations.EntityOperation.CREATE).size(), 2);
    AtlasEntity entityByGuid = getEntityByGuid(hiveTable.getGuid());
    List<AtlasObjectId> refs = (List<AtlasObjectId>) entityByGuid.getAttribute("columns");
    assertEquals(refs.size(), 2);
}
Also used : AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) HashMap(java.util.HashMap) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Test(org.testng.annotations.Test)

Example 9 with AtlasObjectId

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

the class EntityGraphDiscoveryContext method getResolvedEntityVertex.

public AtlasVertex getResolvedEntityVertex(AtlasObjectId objId) {
    AtlasVertex vertex = resolvedIdsByUniqAttribs.get(objId);
    // check also for sub-types; ref={typeName=Asset; guid=abcd} should match {typeName=hive_table; guid=abcd}
    if (vertex == null) {
        final AtlasEntityType entityType = typeRegistry.getEntityTypeByName(objId.getTypeName());
        final Set<String> allSubTypes = entityType.getAllSubTypes();
        for (String subType : allSubTypes) {
            AtlasObjectId subTypeObjId = new AtlasObjectId(objId.getGuid(), subType, objId.getUniqueAttributes());
            vertex = resolvedIdsByUniqAttribs.get(subTypeObjId);
            if (vertex != null) {
                resolvedIdsByUniqAttribs.put(objId, vertex);
                break;
            }
        }
    }
    return vertex;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 10 with AtlasObjectId

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

the class AtlasDeleteHandlerV1Test method testDeleteTargetOfRequiredMapReference.

@Test
public void testDeleteTargetOfRequiredMapReference() throws Exception {
    // Define type for map value.
    AtlasEntityDef mapValueDef = new AtlasEntityDef("RequiredMapValue", "RequiredMapValue_description", "1.0", Collections.<AtlasStructDef.AtlasAttributeDef>emptyList(), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] mapOwnerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("map", "map<string,RequiredMapValue>", false, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 1, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef mapOwnerDef = new AtlasEntityDef("RequiredMapOwner", "RequiredMapOwner_description", "1.0", Arrays.asList(mapOwnerAttributes), Collections.<String>emptySet());
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(), ImmutableList.<AtlasClassificationDef>of(), ImmutableList.<AtlasEntityDef>of(mapValueDef, mapOwnerDef));
    typeDefStore.createTypesDef(typesDef);
    AtlasEntityType mapOwnerType = typeRegistry.getEntityTypeByName("RequiredMapOwner");
    AtlasEntityType mapValueType = typeRegistry.getEntityTypeByName("RequiredMapValue");
    // Create instances of RequiredMapOwner and RequiredMapValue.
    // Set RequiredMapOwner.map with one entry that references RequiredMapValue instance.
    AtlasEntity mapOwnerInstance = new AtlasEntity(mapOwnerType.getTypeName());
    AtlasEntity mapValueInstance = new AtlasEntity(mapValueType.getTypeName());
    mapOwnerInstance.setAttribute("map", Collections.singletonMap("value1", AtlasTypeUtil.getAtlasObjectId(mapValueInstance)));
    AtlasEntity.AtlasEntitiesWithExtInfo entities = new AtlasEntity.AtlasEntitiesWithExtInfo();
    entities.addReferredEntity(mapValueInstance);
    entities.addEntity(mapOwnerInstance);
    List<AtlasEntityHeader> createEntitiesResult = entityStore.createOrUpdate(new AtlasEntityStream(entities), false).getCreatedEntities();
    Assert.assertEquals(createEntitiesResult.size(), 2);
    List<String> guids = metadataService.getEntityList("RequiredMapOwner");
    Assert.assertEquals(guids.size(), 1);
    String mapOwnerGuid = guids.get(0);
    guids = metadataService.getEntityList("RequiredMapValue");
    Assert.assertEquals(guids.size(), 1);
    String mapValueGuid = guids.get(0);
    // Verify MapOwner.map attribute has expected value.
    final AtlasEntity.AtlasEntityWithExtInfo mapOwnerInstance1 = entityStore.getById(mapOwnerGuid);
    Object object = mapOwnerInstance1.getEntity().getAttribute("map");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof Map);
    Map<String, AtlasObjectId> map = (Map<String, AtlasObjectId>) object;
    Assert.assertEquals(map.size(), 1);
    AtlasObjectId mapValueInstance1 = map.get("value1");
    Assert.assertNotNull(mapValueInstance1);
    Assert.assertEquals(mapValueInstance1.getGuid(), mapValueGuid);
    String edgeLabel = AtlasGraphUtilsV1.getAttributeEdgeLabel(mapOwnerType, "map");
    String mapEntryLabel = edgeLabel + "." + "value1";
    AtlasEdgeLabel atlasEdgeLabel = new AtlasEdgeLabel(mapEntryLabel);
    AtlasVertex mapOwnerVertex = GraphHelper.getInstance().getVertexForGUID(mapOwnerGuid);
    object = mapOwnerVertex.getProperty(atlasEdgeLabel.getQualifiedMapKey(), Object.class);
    Assert.assertNotNull(object);
    // Verify deleting the target of required map attribute throws a AtlasBaseException.
    try {
        entityStore.deleteById(mapValueGuid);
        Assert.fail(AtlasBaseException.class.getSimpleName() + " was expected but none thrown.");
    } catch (Exception e) {
        verifyExceptionThrown(e, AtlasBaseException.class);
    }
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasEdgeLabel(org.apache.atlas.repository.graph.AtlasEdgeLabel) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) EntityNotFoundException(org.apache.atlas.typesystem.exception.EntityNotFoundException) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasException(org.apache.atlas.AtlasException) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntityHeader(org.apache.atlas.model.instance.AtlasEntityHeader) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Map(java.util.Map) HashMap(java.util.HashMap) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

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