Search in sources :

Example 26 with AtlasClassification

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

the class EntityResource method getTraitNames.

// Trait management functions
/**
     * Gets the list of trait names for a given entity represented by a guid.
     *
     * @param guid globally unique identifier for the entity
     * @return a list of trait names for the given entity guid
     */
@GET
@Path("{guid}/traits")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getTraitNames(@PathParam("guid") String guid) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> EntityResource.getTraitNames({})", guid);
    }
    AtlasPerfTracer perf = null;
    try {
        if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
            perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getTraitNames(" + guid + ")");
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Fetching trait names for entity={}", guid);
        }
        final List<AtlasClassification> classifications = entitiesStore.getClassifications(guid);
        List<String> traitNames = new ArrayList<>();
        for (AtlasClassification classification : classifications) {
            traitNames.add(classification.getTypeName());
        }
        JSONObject response = new JSONObject();
        response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
        response.put(AtlasClient.RESULTS, new JSONArray(traitNames));
        response.put(AtlasClient.COUNT, traitNames.size());
        return Response.ok(response).build();
    } catch (AtlasBaseException e) {
        LOG.error("Unable to get trait definition for entity {}", guid, e);
        throw toWebApplicationException(e);
    } catch (IllegalArgumentException e) {
        LOG.error("Unable to get trait definition for entity {}", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
    } catch (WebApplicationException e) {
        LOG.error("Unable to get trait names for entity {}", guid, e);
        throw e;
    } catch (Throwable e) {
        LOG.error("Unable to get trait names for entity {}", guid, e);
        throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
    } finally {
        AtlasPerfTracer.log(perf);
        if (LOG.isDebugEnabled()) {
            LOG.debug("<== EntityResource.getTraitNames({})", guid);
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) JSONObject(org.codehaus.jettison.json.JSONObject) AtlasPerfTracer(org.apache.atlas.utils.AtlasPerfTracer) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification)

Example 27 with AtlasClassification

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

the class AtlasClassificationFormatConverter method fromV1ToV2.

@Override
public AtlasClassification fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext ctx) throws AtlasBaseException {
    AtlasClassification ret = null;
    if (v1Obj != null) {
        AtlasClassificationType classificationType = (AtlasClassificationType) type;
        if (v1Obj instanceof Map) {
            final Map v1Map = (Map) v1Obj;
            final Map v1Attribs = (Map) v1Map.get(ATTRIBUTES_PROPERTY_KEY);
            if (MapUtils.isNotEmpty(v1Attribs)) {
                ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, v1Attribs, ctx));
            } else {
                ret = new AtlasClassification(type.getTypeName());
            }
        } else if (v1Obj instanceof IStruct) {
            IStruct struct = (IStruct) v1Obj;
            Map<String, Object> v1Attribs = null;
            try {
                v1Attribs = struct.getValuesMap();
            } catch (AtlasException excp) {
                LOG.error("IStruct.getValuesMap() failed", excp);
            }
            ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, v1Attribs, ctx));
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", v1Obj.getClass().getCanonicalName());
        }
    }
    return ret;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasException(org.apache.atlas.AtlasException) Map(java.util.Map) IStruct(org.apache.atlas.typesystem.IStruct)

Example 28 with AtlasClassification

use of org.apache.atlas.model.instance.AtlasClassification 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 29 with AtlasClassification

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

the class AtlasEntityStoreV1 method getClassificationNames.

private List<String> getClassificationNames(String guid) throws AtlasBaseException {
    List<String> ret = null;
    List<AtlasClassification> classifications = getClassifications(guid);
    if (CollectionUtils.isNotEmpty(classifications)) {
        ret = new ArrayList<>();
        for (AtlasClassification classification : classifications) {
            ret.add(classification.getTypeName());
        }
    }
    return ret;
}
Also used : AtlasClassification(org.apache.atlas.model.instance.AtlasClassification)

Example 30 with AtlasClassification

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

the class AtlasEntityStoreV1 method updateClassifications.

@Override
@GraphTransaction
public void updateClassifications(String guid, List<AtlasClassification> newClassifications) throws AtlasBaseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Updating classifications={} for entity={}", newClassifications, guid);
    }
    if (StringUtils.isEmpty(guid)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "Guid not specified");
    }
    if (CollectionUtils.isEmpty(newClassifications)) {
        throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "classifications(s) not specified");
    }
    List<AtlasClassification> updatedClassifications = new ArrayList<>();
    for (AtlasClassification newClassification : newClassifications) {
        String classificationName = newClassification.getTypeName();
        AtlasClassification oldClassification = getClassification(guid, classificationName);
        if (oldClassification == null) {
            throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_FOUND, classificationName);
        }
        validateAndNormalizeForUpdate(newClassification);
        Map<String, Object> newAttrs = newClassification.getAttributes();
        if (MapUtils.isNotEmpty(newAttrs)) {
            for (String attrName : newAttrs.keySet()) {
                oldClassification.setAttribute(attrName, newAttrs.get(attrName));
            }
        }
        entityGraphMapper.updateClassification(new EntityMutationContext(), guid, oldClassification);
        updatedClassifications.add(oldClassification);
    }
    // notify listeners on update to classifications
    entityChangeNotifier.onClassificationUpdatedToEntity(guid, updatedClassifications);
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) ArrayList(java.util.ArrayList) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) GraphTransaction(org.apache.atlas.annotation.GraphTransaction)

Aggregations

AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)30 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)13 Test (org.testng.annotations.Test)10 ArrayList (java.util.ArrayList)6 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)5 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)4 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)4 IStruct (org.apache.atlas.typesystem.IStruct)4 AtlasPerfTracer (org.apache.atlas.utils.AtlasPerfTracer)4 AtlasClassifications (org.apache.atlas.model.instance.AtlasClassification.AtlasClassifications)3 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 HashMap (java.util.HashMap)2 AtlasException (org.apache.atlas.AtlasException)2 GraphTransaction (org.apache.atlas.annotation.GraphTransaction)2 AtlasEntityWithExtInfo (org.apache.atlas.model.instance.AtlasEntity.AtlasEntityWithExtInfo)2 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)2 AtlasEntityType (org.apache.atlas.type.AtlasEntityType)2 JSONArray (org.codehaus.jettison.json.JSONArray)2 Map (java.util.Map)1