Search in sources :

Example 26 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class NotificationEntityChangeListenerTest method testGetAllTraitsSuperTraits.

@Test
public void testGetAllTraitsSuperTraits() throws Exception {
    AtlasTypeRegistry typeSystem = mock(AtlasTypeRegistry.class);
    String traitName = "MyTrait";
    Struct myTrait = new Struct(traitName);
    String superTraitName = "MySuperTrait";
    AtlasClassificationType traitDef = mock(AtlasClassificationType.class);
    Set<String> superTypeNames = Collections.singleton(superTraitName);
    AtlasClassificationType superTraitDef = mock(AtlasClassificationType.class);
    Set<String> superSuperTypeNames = Collections.emptySet();
    Referenceable entity = getEntity("id", myTrait);
    when(typeSystem.getClassificationTypeByName(traitName)).thenReturn(traitDef);
    when(typeSystem.getClassificationTypeByName(superTraitName)).thenReturn(superTraitDef);
    when(traitDef.getAllSuperTypes()).thenReturn(superTypeNames);
    when(superTraitDef.getAllSuperTypes()).thenReturn(superSuperTypeNames);
    List<Struct> allTraits = NotificationEntityChangeListener.getAllTraits(entity, typeSystem);
    assertEquals(2, allTraits.size());
    for (Struct trait : allTraits) {
        String typeName = trait.getTypeName();
        assertTrue(typeName.equals(traitName) || typeName.equals(superTraitName));
    }
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) AtlasTypeRegistry(org.apache.atlas.type.AtlasTypeRegistry) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) Struct(org.apache.atlas.v1.model.instance.Struct) Test(org.testng.annotations.Test)

Example 27 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class NotificationEntityChangeListenerTest method getEntity.

private Referenceable getEntity(String id, Struct... traits) {
    String typeName = "typeName";
    Map<String, Object> values = new HashMap<>();
    List<String> traitNames = new LinkedList<>();
    Map<String, Struct> traitMap = new HashMap<>();
    for (Struct trait : traits) {
        String traitName = trait.getTypeName();
        traitNames.add(traitName);
        traitMap.put(traitName, trait);
    }
    return new Referenceable(id, typeName, values, traitNames, traitMap);
}
Also used : Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Struct(org.apache.atlas.v1.model.instance.Struct)

Example 28 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project atlas by apache.

the class NotificationEntityChangeListener method getAllTraits.

// ----- helper methods -------------------------------------------------
// ----- helper methods ----------------------------------------------------
@VisibleForTesting
public static List<Struct> getAllTraits(Referenceable entityDefinition, AtlasTypeRegistry typeRegistry) throws AtlasException {
    List<Struct> ret = new ArrayList<>();
    for (String traitName : entityDefinition.getTraitNames()) {
        Struct trait = entityDefinition.getTrait(traitName);
        AtlasClassificationType traitType = typeRegistry.getClassificationTypeByName(traitName);
        Set<String> superTypeNames = traitType != null ? traitType.getAllSuperTypes() : null;
        ret.add(trait);
        if (CollectionUtils.isNotEmpty(superTypeNames)) {
            for (String superTypeName : superTypeNames) {
                Struct superTypeTrait = new Struct(superTypeName);
                if (MapUtils.isNotEmpty(trait.getValues())) {
                    AtlasClassificationType superType = typeRegistry.getClassificationTypeByName(superTypeName);
                    if (superType != null && MapUtils.isNotEmpty(superType.getAllAttributes())) {
                        Map<String, Object> superTypeTraitAttributes = new HashMap<>();
                        for (Map.Entry<String, Object> attrEntry : trait.getValues().entrySet()) {
                            String attrName = attrEntry.getKey();
                            if (superType.getAllAttributes().containsKey(attrName)) {
                                superTypeTraitAttributes.put(attrName, attrEntry.getValue());
                            }
                        }
                        superTypeTrait.setValues(superTypeTraitAttributes);
                    }
                }
                ret.add(superTypeTrait);
            }
        }
    }
    return ret;
}
Also used : AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) Struct(org.apache.atlas.v1.model.instance.Struct) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 29 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project 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;
            Status status = entity.getStatus();
            if (status == null) {
                status = Status.ACTIVE;
            }
            Referenceable referenceable = new Referenceable(entity.getGuid(), entity.getTypeName(), status.name(), fromV2ToV1(entityType, entity.getAttributes(), context), new AtlasSystemAttributes(entity.getCreatedBy(), entity.getUpdatedBy(), entity.getCreateTime(), entity.getUpdateTime()));
            if (CollectionUtils.isNotEmpty(entity.getClassifications())) {
                for (AtlasClassification classification : entity.getClassifications()) {
                    String traitName = classification.getTypeName();
                    AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(traitName);
                    AtlasFormatConverter formatConverter = classificationType != null ? converterRegistry.getConverter(classificationType.getTypeCategory()) : null;
                    Struct trait = formatConverter != null ? (Struct) formatConverter.fromV2ToV1(classification, classificationType, context) : null;
                    if (trait != null) {
                        referenceable.getTraitNames().add(trait.getTypeName());
                        referenceable.getTraits().put(trait.getTypeName(), trait);
                    }
                }
            }
            ret = referenceable;
        } 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 : Status(org.apache.atlas.model.instance.AtlasEntity.Status) AtlasSystemAttributes(org.apache.atlas.v1.model.instance.AtlasSystemAttributes) AtlasClassificationType(org.apache.atlas.type.AtlasClassificationType) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Struct(org.apache.atlas.v1.model.instance.Struct) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) AtlasObjectId(org.apache.atlas.model.instance.AtlasObjectId) Id(org.apache.atlas.v1.model.instance.Id) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Map(java.util.Map)

Example 30 with Struct

use of org.apache.atlas.v1.model.instance.Struct in project 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 Referenceable) {
            Referenceable entRef = (Referenceable) v1Obj;
            String guid = entRef.getId().getId();
            if (!context.entityExists(guid)) {
                entity = new AtlasEntity(entRef.getTypeName(), super.fromV1ToV2(entityType, entRef.getValues(), context));
                entity.setGuid(entRef.getId().getId());
                entity.setStatus(convertState(entRef.getId().getState()));
                if (entRef.getSystemAttributes() != null) {
                    entity.setCreatedBy(entRef.getSystemAttributes().getCreatedBy());
                    entity.setCreateTime(entRef.getSystemAttributes().getCreatedTime());
                    entity.setUpdatedBy(entRef.getSystemAttributes().getModifiedBy());
                    entity.setUpdateTime(entRef.getSystemAttributes().getModifiedTime());
                }
                entity.setVersion((long) entRef.getId().getVersion());
                if (CollectionUtils.isNotEmpty(entRef.getTraitNames())) {
                    List<AtlasClassification> classifications = new ArrayList<>();
                    AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
                    for (String traitName : entRef.getTraitNames()) {
                        Struct trait = entRef.getTraits().get(traitName);
                        AtlasType classifiType = typeRegistry.getType(traitName);
                        AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait, classifiType, context);
                        classifications.add(classification);
                    }
                    entity.setClassifications(classifications);
                }
            }
        } else {
            throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Referenceable", v1Obj.getClass().getCanonicalName());
        }
    }
    return entity;
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) AtlasEntity(org.apache.atlas.model.instance.AtlasEntity) ArrayList(java.util.ArrayList) AtlasType(org.apache.atlas.type.AtlasType) AtlasClassification(org.apache.atlas.model.instance.AtlasClassification) AtlasEntityType(org.apache.atlas.type.AtlasEntityType) Struct(org.apache.atlas.v1.model.instance.Struct)

Aggregations

Struct (org.apache.atlas.v1.model.instance.Struct)36 Referenceable (org.apache.atlas.v1.model.instance.Referenceable)22 Test (org.testng.annotations.Test)17 HashMap (java.util.HashMap)9 Map (java.util.Map)9 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)9 EntityNotificationV1 (org.apache.atlas.v1.model.notification.EntityNotificationV1)7 AtlasClassificationType (org.apache.atlas.type.AtlasClassificationType)6 Id (org.apache.atlas.v1.model.instance.Id)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 AtlasClassification (org.apache.atlas.model.instance.AtlasClassification)5 ArrayList (java.util.ArrayList)4 LinkedList (java.util.LinkedList)3 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 AtlasException (org.apache.atlas.AtlasException)2 AtlasServiceException (org.apache.atlas.AtlasServiceException)2 EntityAuditEvent (org.apache.atlas.EntityAuditEvent)2 EntityChangeListener (org.apache.atlas.listener.EntityChangeListener)2 EntityChangeListenerV2 (org.apache.atlas.listener.EntityChangeListenerV2)2