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;
}
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;
}
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);
}
}
}
}
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()));
}
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");
}
}
Aggregations