Search in sources :

Example 36 with AtlasEntityType

use of org.apache.atlas.type.AtlasEntityType 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 37 with AtlasEntityType

use of org.apache.atlas.type.AtlasEntityType in project incubator-atlas by apache.

the class AtlasEntityFormatConverter method fromV2ToV1.

@Override
public Object fromV2ToV1(Object v2Obj, AtlasType type, ConverterContext context) throws AtlasBaseException {
    Object ret = null;
    if (v2Obj != null) {
        AtlasEntityType entityType = (AtlasEntityType) type;
        if (v2Obj instanceof Map) {
            Map v2Map = (Map) v2Obj;
            String idStr = (String) v2Map.get(AtlasObjectId.KEY_GUID);
            String typeName = type.getTypeName();
            if (StringUtils.isEmpty(idStr)) {
                throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND);
            }
            final Map v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY);
            if (MapUtils.isEmpty(v2Attribs)) {
                ret = new Id(idStr, 0, typeName);
            } else {
                ret = new Referenceable(idStr, typeName, super.fromV2ToV1(entityType, v2Attribs, context));
            }
        } else if (v2Obj instanceof AtlasEntity) {
            AtlasEntity entity = (AtlasEntity) v2Obj;
            ret = new Referenceable(entity.getGuid(), entity.getTypeName(), fromV2ToV1(entityType, entity.getAttributes(), context));
        } else if (v2Obj instanceof AtlasObjectId) {
            // transient-id
            AtlasEntity entity = context.getById(((AtlasObjectId) v2Obj).getGuid());
            if (entity == null) {
                throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Could not find entity ", v2Obj.toString());
            }
            ret = this.fromV2ToV1(entity, typeRegistry.getType(((AtlasObjectId) v2Obj).getTypeName()), context);
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasEntity or String", v2Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.typesystem.Referenceable) 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) Map(java.util.Map)

Example 38 with AtlasEntityType

use of org.apache.atlas.type.AtlasEntityType 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 39 with AtlasEntityType

use of org.apache.atlas.type.AtlasEntityType in project incubator-atlas by apache.

the class AtlasInstanceConverter method toAtlasEntity.

public AtlasEntitiesWithExtInfo toAtlasEntity(IReferenceableInstance referenceable) throws AtlasBaseException {
    AtlasEntityFormatConverter converter = (AtlasEntityFormatConverter) instanceFormatters.getConverter(TypeCategory.ENTITY);
    AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
    if (entityType == null) {
        throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
    }
    // validate
    try {
        metadataService.validateAndConvertToTypedInstance(referenceable, entityType.getTypeName());
    } catch (AtlasException excp) {
        throw toAtlasBaseException(excp);
    }
    ConverterContext ctx = new ConverterContext();
    AtlasEntity entity = converter.fromV1ToV2(referenceable, entityType, ctx);
    ctx.addEntity(entity);
    return ctx.getEntities();
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ConverterContext(org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasException(org.apache.atlas.AtlasException) AtlasEntityType(org.apache.atlas.type.AtlasEntityType)

Example 40 with AtlasEntityType

use of org.apache.atlas.type.AtlasEntityType in project incubator-atlas by apache.

the class AtlasEntityStoreV1 method updateEntityAttributeByGuid.

@Override
@GraphTransaction
public EntityMutationResponse updateEntityAttributeByGuid(String guid, String attrName, Object attrValue) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> updateEntityAttributeByGuid({}, {}, {})", guid, attrName, attrValue);
    }
    AtlasEntityWithExtInfo entityInfo = getById(guid);
    if (entityInfo == null || entityInfo.getEntity() == null) {
        throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND, guid);
    }
    AtlasEntity entity = entityInfo.getEntity();
    AtlasEntityType entityType = (AtlasEntityType) typeRegistry.getType(entity.getTypeName());
    AtlasAttribute attr = entityType.getAttribute(attrName);
    if (attr == null) {
        throw new AtlasBaseException(AtlasErrorCode.UNKNOWN_ATTRIBUTE, attrName, entity.getTypeName());
    }
    AtlasType attrType = attr.getAttributeType();
    AtlasEntity updateEntity = new AtlasEntity();
    updateEntity.setGuid(guid);
    updateEntity.setTypeName(entity.getTypeName());
    switch(attrType.getTypeCategory()) {
        case PRIMITIVE:
            updateEntity.setAttribute(attrName, attrValue);
            break;
        case OBJECT_ID_TYPE:
            AtlasObjectId objId;
            if (attrValue instanceof String) {
                objId = new AtlasObjectId((String) attrValue, attr.getAttributeDef().getTypeName());
            } else {
                objId = (AtlasObjectId) attrType.getNormalizedValue(attrValue);
            }
            updateEntity.setAttribute(attrName, objId);
            break;
        default:
            throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UPDATE_NOT_SUPPORTED, attrName, attrType.getTypeName());
    }
    return createOrUpdate(new AtlasEntityStream(updateEntity), true);
}
Also used : AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasEntityWithExtInfo(org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasType(org.apache.atlas.type.AtlasType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasEntityType (org.apache.atlas.type.AtlasEntityType)45 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)18 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)16 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)12 AtlasObjectId (org.apache.atlas.model.instance.AtlasObjectId)10 Test (org.testng.annotations.Test)10 ArrayList (java.util.ArrayList)8 AtlasEntityHeader (org.apache.atlas.model.instance.AtlasEntityHeader)8 EntityMutationResponse (org.apache.atlas.model.instance.EntityMutationResponse)7 AtlasEntitiesWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntitiesWithExtInfo)6 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)6 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)6 AtlasType (org.apache.atlas.type.AtlasType)6 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)5 AtlasStructType (org.apache.atlas.type.AtlasStructType)5 HashMap (java.util.HashMap)4 IReferenceableInstance (org.apache.atlas.typesystem.IReferenceableInstance)4 List (java.util.List)3 Map (java.util.Map)3 Consumes (javax.ws.rs.Consumes)3