Search in sources :

Example 16 with ITypedStruct

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

the class DefaultGraphPersistenceStrategy method constructInstance.

@Override
public <U> U constructInstance(IDataType<U> dataType, Object value) {
    try {
        switch(dataType.getTypeCategory()) {
            case PRIMITIVE:
            case ENUM:
                return dataType.convert(value, Multiplicity.OPTIONAL);
            case ARRAY:
                DataTypes.ArrayType arrType = (DataTypes.ArrayType) dataType;
                IDataType<?> elemType = arrType.getElemType();
                ImmutableCollection.Builder result = ImmutableList.builder();
                List list = (List) value;
                for (Object listElement : list) {
                    Object collectionEntry = constructCollectionEntry(elemType, listElement);
                    if (collectionEntry != null) {
                        result.add(collectionEntry);
                    }
                }
                return (U) result.build();
            case MAP:
                // todo
                break;
            case STRUCT:
                AtlasVertex structVertex = (AtlasVertex) value;
                StructType structType = (StructType) dataType;
                ITypedStruct structInstance = structType.createInstance();
                TypeSystem.IdType idType = TypeSystem.getInstance().getIdType();
                if (dataType.getName().equals(idType.getName())) {
                    structInstance.set(idType.typeNameAttrName(), GraphHelper.getSingleValuedProperty(structVertex, typeAttributeName(), String.class));
                    structInstance.set(idType.idAttrName(), GraphHelper.getSingleValuedProperty(structVertex, idAttributeName(), String.class));
                    String stateValue = GraphHelper.getSingleValuedProperty(structVertex, stateAttributeName(), String.class);
                    if (stateValue != null) {
                        structInstance.set(idType.stateAttrName(), stateValue);
                    }
                    structInstance.set(idType.versionAttrName(), structVertex.getProperty(versionAttributeName(), Integer.class));
                } else {
                    metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(structVertex, structInstance, structType.fieldMapping().fields);
                }
                return dataType.convert(structInstance, Multiplicity.OPTIONAL);
            case TRAIT:
                AtlasVertex traitVertex = (AtlasVertex) value;
                TraitType traitType = (TraitType) dataType;
                ITypedStruct traitInstance = traitType.createInstance();
                // todo - this is not right, we should load the Instance associated with this
                // trait. for now just loading the trait struct.
                // metadataRepository.getGraphToInstanceMapper().mapVertexToTraitInstance(
                //        traitVertex, dataType.getName(), , traitType, traitInstance);
                metadataRepository.getGraphToInstanceMapper().mapVertexToInstance(traitVertex, traitInstance, traitType.fieldMapping().fields);
                break;
            case CLASS:
                AtlasVertex classVertex = (AtlasVertex) value;
                String guid = classVertex.getProperty(Constants.GUID_PROPERTY_KEY, String.class);
                // Check if the instance we need was previously loaded.
                ITypedReferenceableInstance classInstance = RequestContext.get().getInstanceV1(guid);
                if (classInstance == null) {
                    classInstance = metadataRepository.getGraphToInstanceMapper().mapGraphToTypedInstance(guid, classVertex);
                }
                return dataType.convert(classInstance, Multiplicity.OPTIONAL);
            default:
                throw new UnsupportedOperationException("Load for type " + dataType + "is not supported");
        }
    } catch (AtlasException e) {
        LOG.error("error while constructing an instance", e);
    }
    return null;
}
Also used : ImmutableCollection(com.google.common.collect.ImmutableCollection) TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) StructType(org.apache.atlas.typesystem.types.StructType) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasException(org.apache.atlas.AtlasException) AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) DataTypes(org.apache.atlas.typesystem.types.DataTypes)

Example 17 with ITypedStruct

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

the class GraphToTypedInstanceMapper method mapVertexToStructInstance.

private ITypedStruct mapVertexToStructInstance(AtlasVertex instanceVertex, StructType structType, String relationshipLabel, AtlasEdge optionalEdge) throws AtlasException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("mapping {} to struct {}", string(instanceVertex), relationshipLabel);
    }
    ITypedStruct structInstance = null;
    AtlasEdge edge;
    if (optionalEdge == null) {
        edge = graphHelper.getEdgeForLabel(instanceVertex, relationshipLabel);
    } else {
        edge = optionalEdge;
    }
    if (GraphHelper.elementExists(edge)) {
        structInstance = structType.createInstance();
        AtlasVertex structInstanceVertex = edge.getInVertex();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Found struct instance {}, mapping to instance {} ", string(structInstanceVertex), structInstance.getTypeName());
        }
        mapVertexToInstance(structInstanceVertex, structInstance, structType.fieldMapping().fields);
    }
    return structInstance;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasEdge(org.apache.atlas.repository.graphdb.AtlasEdge)

Example 18 with ITypedStruct

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

the class DefaultMetadataService method addTrait.

/**
     * Adds a new trait to an existing entity represented by a guid.
     *
     * @param guid                    globally unique identifier for the entity
     * @param traitInstanceDefinition trait instance json that needs to be added to entity
     * @throws AtlasException
     */
@Override
public void addTrait(String guid, String traitInstanceDefinition) throws AtlasException {
    guid = ParamChecker.notEmpty(guid, "entity id");
    traitInstanceDefinition = ParamChecker.notEmpty(traitInstanceDefinition, "trait instance definition");
    ITypedStruct traitInstance = deserializeTraitInstance(traitInstanceDefinition);
    addTrait(guid, traitInstance);
}
Also used : ITypedStruct(org.apache.atlas.typesystem.ITypedStruct)

Example 19 with ITypedStruct

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

the class AtlasDeleteHandlerV1Test method testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes.

/**
     * Verify deleting entities that are the target of a unidirectional class array reference
     * from a struct or trait instance.
     */
@Test
public void testDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes() throws Exception {
    // Define class types.
    AtlasStructDef.AtlasAttributeDef[] structTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structTargetDef = new AtlasEntityDef("StructTarget", "StructTarget_description", "1.0", Arrays.asList(structTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] traitTargetAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef traitTargetDef = new AtlasEntityDef("TraitTarget", "TraitTarget_description", "1.0", Arrays.asList(traitTargetAttributes), Collections.<String>emptySet());
    AtlasStructDef.AtlasAttributeDef[] structContainerAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("struct", "TestStruct", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasEntityDef structContainerDef = new AtlasEntityDef("StructContainer", "StructContainer_description", "1.0", Arrays.asList(structContainerAttributes), Collections.<String>emptySet());
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] structDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<StructTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("nestedStructs", "array<NestedStruct>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef structDef = new AtlasStructDef("TestStruct", "TestStruct_desc", "1.0", Arrays.asList(structDefAttributes));
    // Define struct and trait types which have a unidirectional array reference
    // to a class type.
    AtlasStructDef.AtlasAttributeDef[] nestedStructDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("attr1", "string", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()), new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasStructDef nestedStructDef = new AtlasStructDef("NestedStruct", "NestedStruct_desc", "1.0", Arrays.asList(nestedStructDefAttributes));
    AtlasStructDef.AtlasAttributeDef[] traitDefAttributes = new AtlasStructDef.AtlasAttributeDef[] { new AtlasStructDef.AtlasAttributeDef("target", "array<TraitTarget>", true, AtlasStructDef.AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, false, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList()) };
    AtlasClassificationDef traitDef = new AtlasClassificationDef("TestTrait", "TestTrait_desc", "1.0", Arrays.asList(traitDefAttributes));
    AtlasTypesDef typesDef = AtlasTypeUtil.getTypesDef(ImmutableList.<AtlasEnumDef>of(), ImmutableList.<AtlasStructDef>of(structDef, nestedStructDef), ImmutableList.<AtlasClassificationDef>of(traitDef), ImmutableList.<AtlasEntityDef>of(structTargetDef, traitTargetDef, structContainerDef));
    typeDefStore.createTypesDef(typesDef);
    // Create instances of class, struct, and trait types.
    final AtlasEntity structTargetEntity = new AtlasEntity("StructTarget");
    final AtlasEntity traitTargetEntity = new AtlasEntity("TraitTarget");
    final AtlasEntity structContainerEntity = new AtlasEntity("StructContainer");
    AtlasStruct structInstance = new AtlasStruct("TestStruct");
    AtlasStruct nestedStructInstance = new AtlasStruct("NestedStruct");
    Struct traitInstance = new Struct("TestTrait");
    structContainerEntity.setAttribute("struct", structInstance);
    structInstance.setAttribute("target", ImmutableList.of(AtlasTypeUtil.getAtlasObjectId(structTargetEntity)));
    structInstance.setAttribute("nestedStructs", ImmutableList.of(nestedStructInstance));
    AtlasEntity.AtlasEntitiesWithExtInfo structCreationObj = new AtlasEntity.AtlasEntitiesWithExtInfo();
    structCreationObj.addEntity(structContainerEntity);
    structCreationObj.addEntity(traitTargetEntity);
    structCreationObj.addReferredEntity(structTargetEntity);
    init();
    AtlasEntityStream entityStream = new AtlasEntityStream(structCreationObj);
    EntityMutationResponse response = entityStore.createOrUpdate(entityStream, false);
    Assert.assertEquals(response.getCreatedEntities().size(), 3);
    final List<String> structTarget = metadataService.getEntityList("StructTarget");
    Assert.assertEquals(structTarget.size(), 1);
    final String structTargetGuid = structTarget.get(0);
    final List<String> traitTarget = metadataService.getEntityList("TraitTarget");
    Assert.assertEquals(traitTarget.size(), 1);
    final String traitTargetGuid = traitTarget.get(0);
    final List<String> structContainerTarget = metadataService.getEntityList("StructContainer");
    Assert.assertEquals(structContainerTarget.size(), 1);
    String structContainerGuid = structContainerTarget.get(0);
    // Add TestTrait to StructContainer instance
    traitInstance.set("target", ImmutableList.of(new Id(traitTargetGuid, 0, "TraitTarget")));
    TraitType traitType = typeSystem.getDataType(TraitType.class, "TestTrait");
    ITypedStruct convertedTrait = traitType.convert(traitInstance, Multiplicity.REQUIRED);
    metadataService.addTrait(structContainerGuid, convertedTrait);
    // Verify that the unidirectional references from the struct and trait instances
    // are pointing at the target entities.
    final ITypedReferenceableInstance structContainerConvertedEntity = metadataService.getEntityDefinition(structContainerGuid);
    Object object = structContainerConvertedEntity.get("struct");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof ITypedStruct);
    ITypedStruct struct = (ITypedStruct) object;
    object = struct.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    List<ITypedReferenceableInstance> refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), structTargetGuid);
    IStruct trait = structContainerConvertedEntity.getTrait("TestTrait");
    Assert.assertNotNull(trait);
    object = trait.get("target");
    Assert.assertNotNull(object);
    Assert.assertTrue(object instanceof List);
    refList = (List<ITypedReferenceableInstance>) object;
    Assert.assertEquals(refList.size(), 1);
    Assert.assertEquals(refList.get(0).getId()._getId(), traitTargetGuid);
    init();
    // Delete the entities that are targets of the struct and trait instances.
    EntityMutationResponse entityResult = entityStore.deleteByIds(new ArrayList<String>() {

        {
            add(structTargetGuid);
            add(traitTargetGuid);
        }
    });
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 2);
    Assert.assertTrue(extractGuids(entityResult.getDeletedEntities()).containsAll(Arrays.asList(structTargetGuid, traitTargetGuid)));
    assertEntityDeleted(structTargetGuid);
    assertEntityDeleted(traitTargetGuid);
    assertTestDisconnectUnidirectionalArrayReferenceFromStructAndTraitTypes(structContainerGuid);
    init();
    // Delete the entity which contains nested structs and has the TestTrait trait.
    entityResult = entityStore.deleteById(structContainerGuid);
    Assert.assertEquals(entityResult.getDeletedEntities().size(), 1);
    Assert.assertTrue(extractGuids(entityResult.getDeletedEntities()).contains(structContainerGuid));
    assertEntityDeleted(structContainerGuid);
    // Verify all TestStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestStruct"));
    // Verify all NestedStruct struct vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "NestedStruct"));
    // Verify all TestTrait trait vertices were removed.
    assertVerticesDeleted(getVertices(Constants.ENTITY_TYPE_PROPERTY_KEY, "TestTrait"));
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) TraitType(org.apache.atlas.typesystem.types.TraitType) ITypedReferenceableInstance(org.apache.atlas.typesystem.ITypedReferenceableInstance) EntityMutationResponse(org.apache.atlas.model.instance.EntityMutationResponse) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) IStruct(org.apache.atlas.typesystem.IStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) Struct(org.apache.atlas.typesystem.Struct) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.typesystem.persistence.Id) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 20 with ITypedStruct

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

the class AtlasInstanceConverter method getTrait.

public ITypedStruct getTrait(AtlasClassification classification) throws AtlasBaseException {
    AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.CLASSIFICATION);
    AtlasType classificationType = typeRegistry.getType(classification.getTypeName());
    Struct trait = (Struct) converter.fromV2ToV1(classification, classificationType, new ConverterContext());
    try {
        return metadataService.createTraitInstance(trait);
    } catch (AtlasException e) {
        LOG.error("Exception while getting a typed reference for the entity ", e);
        throw toAtlasBaseException(e);
    }
}
Also used : ConverterContext(org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext) AtlasType(org.apache.atlas.type.AtlasType) AtlasException(org.apache.atlas.AtlasException) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Aggregations

ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)29 Test (org.testng.annotations.Test)14 IStruct (org.apache.atlas.typesystem.IStruct)13 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)13 Struct (org.apache.atlas.typesystem.Struct)11 TraitType (org.apache.atlas.typesystem.types.TraitType)6 AtlasException (org.apache.atlas.AtlasException)5 ImmutableList (com.google.common.collect.ImmutableList)4 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)4 List (java.util.List)3 AtlasEdge (org.apache.atlas.repository.graphdb.AtlasEdge)3 Id (org.apache.atlas.typesystem.persistence.Id)3 ArrayList (java.util.ArrayList)2 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 Referenceable (org.apache.atlas.typesystem.Referenceable)2 TypesDef (org.apache.atlas.typesystem.TypesDef)2 StructType (org.apache.atlas.typesystem.types.StructType)2 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)2 BeforeTest (org.testng.annotations.BeforeTest)2