Search in sources :

Example 11 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class TypedefsJerseyResourceIT method testListTypesByFilter.

@Test
public void testListTypesByFilter() throws Exception {
    AtlasAttributeDef attr = AtlasTypeUtil.createOptionalAttrDef("attr", "string");
    AtlasEntityDef classDefA = AtlasTypeUtil.createClassTypeDef("A" + randomString(), ImmutableSet.<String>of(), attr);
    AtlasEntityDef classDefA1 = AtlasTypeUtil.createClassTypeDef("A1" + randomString(), ImmutableSet.of(classDefA.getName()), attr);
    AtlasEntityDef classDefB = AtlasTypeUtil.createClassTypeDef("B" + randomString(), ImmutableSet.<String>of(), attr);
    AtlasEntityDef classDefC = AtlasTypeUtil.createClassTypeDef("C" + randomString(), ImmutableSet.of(classDefB.getName(), classDefA.getName()), attr);
    AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
    atlasTypesDef.getEntityDefs().add(classDefA);
    atlasTypesDef.getEntityDefs().add(classDefA1);
    atlasTypesDef.getEntityDefs().add(classDefB);
    atlasTypesDef.getEntityDefs().add(classDefC);
    AtlasTypesDef created = clientV2.createAtlasTypeDefs(atlasTypesDef);
    assertNotNull(created);
    assertEquals(created.getEntityDefs().size(), atlasTypesDef.getEntityDefs().size());
    MultivaluedMap<String, String> searchParams = new MultivaluedMapImpl();
    searchParams.add(SearchFilter.PARAM_TYPE, "CLASS");
    searchParams.add(SearchFilter.PARAM_SUPERTYPE, classDefA.getName());
    SearchFilter searchFilter = new SearchFilter(searchParams);
    AtlasTypesDef searchDefs = clientV2.getAllTypeDefs(searchFilter);
    assertNotNull(searchDefs);
    assertEquals(searchDefs.getEntityDefs().size(), 2);
    searchParams.add(SearchFilter.PARAM_NOT_SUPERTYPE, classDefB.getName());
    searchFilter = new SearchFilter(searchParams);
    searchDefs = clientV2.getAllTypeDefs(searchFilter);
    assertNotNull(searchDefs);
    assertEquals(searchDefs.getEntityDefs().size(), 1);
}
Also used : AtlasEntityDef(org.apache.atlas.model.typedef.AtlasEntityDef) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) SearchFilter(org.apache.atlas.model.SearchFilter) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test)

Example 12 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasTypeDefGraphStoreTest method testTypeDeletionAndRecreate.

@Test
public void testTypeDeletionAndRecreate() {
    AtlasClassificationDef aTag = new AtlasClassificationDef("testTag");
    AtlasAttributeDef attributeDef = new AtlasAttributeDef("testAttribute", "string", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
    aTag.addAttribute(attributeDef);
    AtlasTypesDef typesDef = new AtlasTypesDef();
    typesDef.setClassificationDefs(Arrays.asList(aTag));
    try {
        typeDefStore.createTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag creation should've succeeded");
    }
    try {
        typeDefStore.deleteTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag deletion should've succeeded");
    }
    aTag = new AtlasClassificationDef("testTag");
    attributeDef = new AtlasAttributeDef("testAttribute", "int", true, AtlasAttributeDef.Cardinality.SINGLE, 0, 1, false, true, Collections.<AtlasStructDef.AtlasConstraintDef>emptyList());
    aTag.addAttribute(attributeDef);
    typesDef.setClassificationDefs(Arrays.asList(aTag));
    try {
        typeDefStore.createTypesDef(typesDef);
    } catch (AtlasBaseException e) {
        fail("Tag re-creation should've succeeded");
    }
}
Also used : AtlasClassificationDef(org.apache.atlas.model.typedef.AtlasClassificationDef) AtlasStructDef(org.apache.atlas.model.typedef.AtlasStructDef) AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) AtlasTypesDef(org.apache.atlas.model.typedef.AtlasTypesDef) Test(org.testng.annotations.Test)

Example 13 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasStructType method validateValue.

@Override
public boolean validateValue(Object obj, String objName, List<String> messages) {
    boolean ret = true;
    if (obj != null) {
        if (obj instanceof AtlasStruct) {
            AtlasStruct structObj = (AtlasStruct) obj;
            for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
                String attrName = attributeDef.getName();
                AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
                if (attribute != null) {
                    AtlasType dataType = attribute.getAttributeType();
                    Object value = structObj.getAttribute(attrName);
                    String fieldName = objName + "." + attrName;
                    if (value != null) {
                        ret = dataType.validateValue(value, fieldName, messages) && ret;
                    } else if (!attributeDef.getIsOptional()) {
                        ret = false;
                        messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
                    }
                }
            }
        } else if (obj instanceof Map) {
            Map attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
            for (AtlasAttributeDef attributeDef : structDef.getAttributeDefs()) {
                String attrName = attributeDef.getName();
                AtlasAttribute attribute = allAttributes.get(attributeDef.getName());
                if (attribute != null) {
                    AtlasType dataType = attribute.getAttributeType();
                    Object value = attributes.get(attrName);
                    String fieldName = objName + "." + attrName;
                    if (value != null) {
                        ret = dataType.validateValue(value, fieldName, messages) && ret;
                    } else if (!attributeDef.getIsOptional()) {
                        ret = false;
                        messages.add(fieldName + ": mandatory attribute value missing in type " + getTypeName());
                    }
                }
            }
        } else {
            ret = false;
            messages.add(objName + "=" + obj + ": invalid value for type " + getTypeName());
        }
    }
    return ret;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttribute(org.apache.atlas.type.AtlasStructType.AtlasAttribute) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasStructType method isValidValueForUpdate.

@Override
public boolean isValidValueForUpdate(Object obj) {
    if (obj != null) {
        Map<String, Object> attributes;
        if (obj instanceof AtlasStruct) {
            AtlasStruct structObj = (AtlasStruct) obj;
            attributes = structObj.getAttributes();
        } else if (obj instanceof Map) {
            attributes = AtlasTypeUtil.toStructAttributes((Map) obj);
        } else {
            return false;
        }
        if (MapUtils.isNotEmpty(attributes)) {
            for (Map.Entry<String, Object> e : attributes.entrySet()) {
                String attrName = e.getKey();
                Object attrValue = e.getValue();
                AtlasAttributeDef attrDef = structDef.getAttribute(attrName);
                if (attrValue == null || attrDef == null) {
                    continue;
                }
                if (!isAssignableValueForUpdate(attrValue, attrDef)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : AtlasStruct(org.apache.atlas.model.instance.AtlasStruct) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with AtlasAttributeDef

use of org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef in project incubator-atlas by apache.

the class AtlasClassificationType method collectTypeHierarchyInfo.

/*
     * This method should not assume that resolveReferences() has been called on all superTypes.
     * this.classificationDef is the only safe member to reference here
     */
private void collectTypeHierarchyInfo(AtlasTypeRegistry typeRegistry, Set<String> allSuperTypeNames, Map<String, AtlasAttribute> allAttributes, List<String> visitedTypes) throws AtlasBaseException {
    if (visitedTypes.contains(classificationDef.getName())) {
        throw new AtlasBaseException(AtlasErrorCode.CIRCULAR_REFERENCE, classificationDef.getName(), visitedTypes.toString());
    }
    if (CollectionUtils.isNotEmpty(classificationDef.getSuperTypes())) {
        visitedTypes.add(classificationDef.getName());
        for (String superTypeName : classificationDef.getSuperTypes()) {
            AtlasClassificationType superType = typeRegistry.getClassificationTypeByName(superTypeName);
            if (superType != null) {
                superType.collectTypeHierarchyInfo(typeRegistry, allSuperTypeNames, allAttributes, visitedTypes);
            }
        }
        visitedTypes.remove(classificationDef.getName());
        allSuperTypeNames.addAll(classificationDef.getSuperTypes());
    }
    if (CollectionUtils.isNotEmpty(classificationDef.getAttributeDefs())) {
        for (AtlasAttributeDef attributeDef : classificationDef.getAttributeDefs()) {
            AtlasType type = typeRegistry.getType(attributeDef.getTypeName());
            allAttributes.put(attributeDef.getName(), new AtlasAttribute(this, attributeDef, type));
        }
    }
}
Also used : AtlasBaseException(org.apache.atlas.exception.AtlasBaseException) AtlasAttributeDef(org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)

Aggregations

AtlasAttributeDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef)52 AtlasConstraintDef (org.apache.atlas.model.typedef.AtlasStructDef.AtlasConstraintDef)19 AtlasEntityDef (org.apache.atlas.model.typedef.AtlasEntityDef)17 HashMap (java.util.HashMap)14 AtlasStructDef (org.apache.atlas.model.typedef.AtlasStructDef)13 AtlasBaseException (org.apache.atlas.exception.AtlasBaseException)12 ArrayList (java.util.ArrayList)11 Test (org.testng.annotations.Test)10 AtlasTypesDef (org.apache.atlas.model.typedef.AtlasTypesDef)9 AtlasClassificationDef (org.apache.atlas.model.typedef.AtlasClassificationDef)8 AtlasEnumDef (org.apache.atlas.model.typedef.AtlasEnumDef)7 AttributeDefinition (org.apache.atlas.typesystem.types.AttributeDefinition)5 AtlasEnumElementDef (org.apache.atlas.model.typedef.AtlasEnumDef.AtlasEnumElementDef)4 Map (java.util.Map)3 AtlasAttribute (org.apache.atlas.type.AtlasStructType.AtlasAttribute)3 AtlasTransientTypeRegistry (org.apache.atlas.type.AtlasTypeRegistry.AtlasTransientTypeRegistry)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 AtlasEntity (org.apache.atlas.model.instance.AtlasEntity)2 AtlasStruct (org.apache.atlas.model.instance.AtlasStruct)2 ClassType (org.apache.atlas.typesystem.types.ClassType)2