Search in sources :

Example 11 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance in project incubator-atlas by apache.

the class AtlasEntityFormatConverter method fromV1ToV2.

@Override
public AtlasEntity fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext context) throws AtlasBaseException {
    AtlasEntity entity = null;
    if (v1Obj != null) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        if (v1Obj instanceof IReferenceableInstance) {
            IReferenceableInstance entRef = (IReferenceableInstance) v1Obj;
            String guid = entRef.getId()._getId();
            if (!context.entityExists(guid)) {
                Map<String, Object> v1Attribs = null;
                try {
                    v1Attribs = entRef.getValuesMap();
                } catch (AtlasException excp) {
                    LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
                }
                entity = new AtlasEntity(entRef.getTypeName(), super.fromV1ToV2(entityType, v1Attribs, context));
                entity.setGuid(entRef.getId()._getId());
                entity.setStatus(convertState(entRef.getId().getState()));
                entity.setCreatedBy(entRef.getSystemAttributes().createdBy);
                entity.setCreateTime(entRef.getSystemAttributes().createdTime);
                entity.setUpdatedBy(entRef.getSystemAttributes().modifiedBy);
                entity.setUpdateTime(entRef.getSystemAttributes().modifiedTime);
                entity.setVersion((long) entRef.getId().version);
                if (CollectionUtils.isNotEmpty(entRef.getTraits())) {
                    List<AtlasClassification> classifications = new ArrayList<>();
                    AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
                    for (String traitName : entRef.getTraits()) {
                        IStruct trait = entRef.getTrait(traitName);
                        AtlasType classifiType = typeRegistry.getType(traitName);
                        AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait, classifiType, context);
                        classifications.add(classification);
                    }
                    entity.setClassifications(classifications);
                }
            } else {
                entity = context.getById(guid);
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "IReferenceableInstance", v1Obj.getClass().getCanonicalName());
        }
    }
    return entity;
}
Also used : ArrayList(java.util.ArrayList) AtlasType(org.apache.atlas.type.AtlasType) AtlasException(org.apache.atlas.AtlasException) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) IStruct(org.apache.atlas.typesystem.IStruct)

Example 12 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance in project incubator-atlas by apache.

the class AtlasInstanceConverter method toAtlasEntities.

public AtlasEntitiesWithExtInfo toAtlasEntities(String entitiesJson) throws AtlasBaseException, AtlasException {
    ITypedReferenceableInstance[] referenceables = metadataService.deserializeClassInstances(entitiesJson);
    AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) instanceFormatters.getConverter(TypeCategory.ENTITY);
    ConverterContext context = new ConverterContext();
    AtlasEntitiesWithExtInfo ret = null;
    if (referenceables != null) {
        for (IReferenceableInstance referenceable : referenceables) {
            AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
            if (entityType == null) {
                throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
            }
            AtlasEntity entity = converter.fromV1ToV2(referenceable, entityType, context);
            context.addEntity(entity);
        }
        ret = context.getEntities();
    }
    return ret;
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) ConverterContext(org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasEntitiesWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 13 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance in project incubator-atlas by apache.

the class DiscoverInstances method processNode.

@Override
public void processNode(ObjectGraphWalker.Node nd) throws AtlasException {
    IReferenceableInstance ref = null;
    Id id = null;
    if (nd.attributeName == null) {
        ref = (IReferenceableInstance) nd.instance;
        id = ref.getId();
    } else if (nd.aInfo.dataType().getTypeCategory() == DataTypes.TypeCategory.CLASS) {
        if (nd.value != null && (nd.value instanceof Id)) {
            id = (Id) nd.value;
        }
    }
    if (id != null) {
        if (id.isUnassigned()) {
            if (!idToNewIdMap.containsKey(id)) {
                idToNewIdMap.put(id, repository.newId(id.typeName));
            }
            if (ref != null && idToInstanceMap.containsKey(ref)) {
                // Oops
                throw new RepositoryException(String.format("Unexpected internal error: Id %s processed again", id));
            }
            if (ref != null) {
                idToInstanceMap.put(id, ref);
            }
        }
    }
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) Id(org.apache.atlas.typesystem.persistence.Id)

Example 14 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance in project incubator-atlas by apache.

the class DefaultMetadataServiceTest method testDeleteEntities.

@Test
public void testDeleteEntities() throws Exception {
    // Create a table entity, with 3 composite column entities
    Referenceable dbEntity = createDBEntity();
    String dbGuid = TestUtils.createInstance(metadataService, dbEntity);
    Referenceable table1Entity = createTableEntity(dbGuid);
    Referenceable col1 = createColumnEntity();
    Referenceable col2 = createColumnEntity();
    Referenceable col3 = createColumnEntity();
    table1Entity.set(COLUMNS_ATTR_NAME, ImmutableList.of(col1, col2, col3));
    TestUtils.createInstance(metadataService, table1Entity);
    // Retrieve the table entities from the repository,
    // to get their guids and the composite column guids.
    String entityJson = metadataService.getEntityDefinition(TestUtils.TABLE_TYPE, NAME, (String) table1Entity.get(NAME));
    Assert.assertNotNull(entityJson);
    table1Entity = InstanceSerialization.fromJsonReferenceable(entityJson, true);
    List<IReferenceableInstance> table1Columns = (List<IReferenceableInstance>) table1Entity.get(COLUMNS_ATTR_NAME);
    // Register an EntityChangeListener to verify the notification mechanism
    // is working for deleteEntities().
    EntitiesChangeListener listener = new EntitiesChangeListener();
    metadataService.registerListener(listener);
    // Delete one column
    String columnId = table1Columns.get(0).getId()._getId();
    EntityResult entityResult = deleteEntities(columnId);
    // column is deleted and table is updated
    assertEquals(entityResult.getDeletedEntities().get(0), columnId);
    assertEquals(entityResult.getUpdateEntities().get(0), table1Entity.getId()._getId());
    // verify listener was called for updates and deletes
    assertEquals(entityResult.getDeletedEntities(), listener.getDeletedEntities());
    assertEquals(entityResult.getUpdateEntities(), listener.getUpdatedEntities());
    // Delete the table entities.  The deletion should cascade
    // to their composite columns.
    entityResult = deleteEntities(table1Entity.getId()._getId());
    // Verify that deleteEntities() response has guids for tables and their composite columns.
    Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Entity.getId()._getId()));
    Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Columns.get(1).getId()._getId()));
    Assert.assertTrue(entityResult.getDeletedEntities().contains(table1Columns.get(2).getId()._getId()));
    // Verify that tables and their composite columns have been deleted from the repository.
    assertEntityDeleted(TABLE_TYPE, NAME, table1Entity.get(NAME));
    assertEntityDeleted(COLUMN_TYPE, NAME, col2.get(NAME));
    assertEntityDeleted(COLUMN_TYPE, NAME, col3.get(NAME));
    // Verify that the listener was notified about the deleted entities.
    List<String> deletedEntitiesFromListener = listener.getDeletedEntities();
    Assert.assertNotNull(deletedEntitiesFromListener);
    assertEquals(deletedEntitiesFromListener.size(), entityResult.getDeletedEntities().size());
    Assert.assertTrue(deletedEntitiesFromListener.containsAll(entityResult.getDeletedEntities()));
}
Also used : IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) Referenceable(org.apache.atlas.typesystem.Referenceable) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) EntityResult(org.apache.atlas.model.legacy.EntityResult) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Example 15 with IReferenceableInstance

use of org.apache.atlas.typesystem.IReferenceableInstance in project incubator-atlas by apache.

the class FieldMappingTest method testOutputReferenceableInstance.

@Test
public void testOutputReferenceableInstance() throws Exception {
    // ATLAS-645: verify that FieldMapping.output(IReferenceableInstance)
    // does not infinitely recurse when ITypedReferenceableInstance's reference each other.
    HierarchicalTypeDefinition<ClassType> valueDef = TypesUtil.createClassTypeDef("Value", ImmutableSet.<String>of(), new AttributeDefinition("owner", "Owner", Multiplicity.OPTIONAL, false, null));
    // Define class type with reference, where the value is a class reference to Value.
    HierarchicalTypeDefinition<ClassType> ownerDef = TypesUtil.createClassTypeDef("Owner", ImmutableSet.<String>of(), new AttributeDefinition("value", "Value", Multiplicity.OPTIONAL, false, null));
    TypesDef typesDef = TypesUtil.getTypesDef(ImmutableList.<EnumTypeDefinition>of(), ImmutableList.<StructTypeDefinition>of(), ImmutableList.<HierarchicalTypeDefinition<TraitType>>of(), ImmutableList.of(ownerDef, valueDef));
    TypeSystem typeSystem = TypeSystem.getInstance();
    typeSystem.defineTypes(typesDef);
    ClassType ownerType = typeSystem.getDataType(ClassType.class, "Owner");
    // Prior to fix for ATLAS-645, this call would throw a StackOverflowError
    try {
        ownerType.toString();
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in ClassType.toString() caused StackOverflowError");
    }
    ClassType valueType = typeSystem.getDataType(ClassType.class, "Value");
    // Create instances of Owner and Value that reference each other.
    ITypedReferenceableInstance ownerInstance = ownerType.createInstance();
    ITypedReferenceableInstance valueInstance = valueType.createInstance();
    // Set Owner.value reference to Value instance.
    ownerInstance.set("value", valueInstance);
    // Set Value.owner reference on Owner instance.
    valueInstance.set("owner", ownerInstance);
    // Prior to fix for ATLAS-645, this call would throw a StackOverflowError
    try {
        ownerInstance.fieldMapping().output(ownerInstance, new StringBuilder(), "", new HashSet<IReferenceableInstance>());
    } catch (StackOverflowError e) {
        Assert.fail("Infinite recursion in FieldMapping.output() caused StackOverflowError");
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) IReferenceableInstance(org.apache.atlas.typesystem.IReferenceableInstance) TypesDef(org.apache.atlas.typesystem.TypesDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) AttributeDefinition(org.apache.atlas.typesystem.types.AttributeDefinition) ClassType(org.apache.atlas.typesystem.types.ClassType) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)21 ArrayList (java.util.ArrayList)11 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)9 List (java.util.List)7 Id (org.apache.atlas.typesystem.persistence.Id)7 Test (org.testng.annotations.Test)7 ImmutableList (com.google.common.collect.ImmutableList)5 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5 BeforeTest (org.testng.annotations.BeforeTest)5 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)4 Referenceable (org.apache.atlas.typesystem.Referenceable)4 HashMap (java.util.HashMap)3 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)3 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)2 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)2 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)2