private void addClassificationType(AtlasClassificationType classificationType, ExportContext context) { if (!context.classificationTypes.contains(classificationType.getTypeName())) { context.classificationTypes.add(classificationType.getTypeName()); addAttributeTypes(classificationType, context); if (CollectionUtils.isNotEmpty(classificationType.getAllSuperTypes())) { for (String superType : classificationType.getAllSuperTypes()) { addClassificationType(superType, context); } } } }
@Override public boolean validateValue(Object obj, String objName, List<String> messages) { boolean ret = true; if (obj != null) { for (AtlasClassificationType superType : superTypes) { ret = superType.validateValue(obj, objName, messages) && ret; } ret = validateTimeBoundaries(obj, messages) && ret; ret = super.validateValue(obj, objName, messages) && ret; } return ret; }
private static AtlasClassificationType getClassificationType(AtlasClassificationDef classificationDef) { try { return new AtlasClassificationType(classificationDef, ModelTestUtil.getTypesRegistry()); } catch (AtlasBaseException excp) { return null; } } }
@Override public Object getNormalizedValue(Object obj) { Object ret = null; if (obj != null) { if (isValidValue(obj)) { if (obj instanceof AtlasClassification) { normalizeAttributeValues((AtlasClassification) obj); ret = obj; } else if (obj instanceof Map) { normalizeAttributeValues((Map) obj); ret = obj; } } } return ret; }
@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; }
private void addToAllSubTypes(AtlasClassificationType subType) { allSubTypes.add(subType.getTypeName()); typeAndAllSubTypes.add(subType.getTypeName()); }
@Override public boolean evaluate(Object o) { return (isClassificationType(o) && ((AtlasClassificationType) o).getAllSuperTypes().contains(supertype))|| (isEntityType(o) && ((AtlasEntityType)o).getAllSuperTypes().contains(supertype)); } };
@Test public void testClassificationSerDeWithSuperType() throws AtlasBaseException { AtlasClassificationDef classificationDef = ModelTestUtil.getClassificationDefWithSuperType(); AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry(); AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(classificationDef.getName()); assertNotNull(classificationType); AtlasClassification ent1 = classificationType.createDefaultValue(); String jsonString = AtlasType.toJson(ent1); AtlasClassification ent2 = AtlasType.fromJson(jsonString, AtlasClassification.class); classificationType.normalizeAttributeValues(ent2); assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasClassification with superType"); }
private void validateAndNormalize(AtlasClassification classification) throws AtlasBaseException { AtlasClassificationType type = typeRegistry.getClassificationTypeByName(classification.getTypeName()); if (type == null) { throw new AtlasBaseException(AtlasErrorCode.CLASSIFICATION_NOT_FOUND, classification.getTypeName()); } List<String> messages = new ArrayList<>(); type.validateValue(classification, classification.getTypeName(), messages); if (!messages.isEmpty()) { throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, messages); } type.getNormalizedValue(classification); }
@Override public void resolveReferences(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { super.resolveReferences(typeRegistry); List<AtlasClassificationType> s = new ArrayList<>(); Set<String> allS = new HashSet<>(); Map<String, AtlasAttribute> allA = new HashMap<>(); getTypeHierarchyInfo(typeRegistry, allS, allA); for (String superTypeName : classificationDef.getSuperTypes()) { AtlasType superType = typeRegistry.getType(superTypeName); if (superType instanceof AtlasClassificationType) { s.add((AtlasClassificationType)superType); } else { throw new AtlasBaseException(AtlasErrorCode.INCOMPATIBLE_SUPERTYPE, superTypeName, classificationDef.getName()); } } this.superTypes = Collections.unmodifiableList(s); this.allSuperTypes = Collections.unmodifiableSet(allS); this.allAttributes = Collections.unmodifiableMap(allA); this.uniqAttributes = getUniqueAttributes(this.allAttributes); this.allSubTypes = new HashSet<>(); // this will be populated in resolveReferencesPhase2() this.typeAndAllSubTypes = new HashSet<>(); // this will be populated in resolveReferencesPhase2() this.typeAndAllSubTypes.add(this.getTypeName()); }
typeAndSubTypes = classificationType.getTypeAndAllSubTypes(); typeAndSubTypesQryStr = classificationType.getTypeAndAllSubTypesQryStr(); } else { typeAndSubTypes = Collections.singleton(classificationType.getTypeName()); typeAndSubTypesQryStr = classificationType.getTypeQryStr(); SearchPredicateUtil.getContainsAnyPredicateGenerator().generatePredicate(TRAIT_NAMES_PROPERTY_KEY, classificationType.getTypeAndAllSubTypes(), List.class), SearchPredicateUtil.getContainsAnyPredicateGenerator().generatePredicate(PROPAGATED_TRAIT_NAMES_PROPERTY_KEY, classificationType.getTypeAndAllSubTypes(), List.class));
@Test public void testClassificationTypeDefaultValue() { AtlasClassification defValue = classificationType.createDefaultValue(); assertNotNull(defValue); assertEquals(defValue.getTypeName(), classificationType.getTypeName()); }
private void validateAttributeNames(AtlasTypeRegistry typeRegistry, String typeName, Set<String> attributeNames) { AtlasType type = null; try { type = typeRegistry.getType(typeName); } catch (AtlasBaseException excp) { } Map<String, AtlasStructType.AtlasAttribute> attributes = null; if (type != null) { if (type instanceof AtlasEntityType) { attributes = ((AtlasEntityType) type).getAllAttributes(); } else if (type instanceof AtlasClassificationType) { attributes = ((AtlasClassificationType) type).getAllAttributes(); } } assertNotNull(attributes); assertEquals(attributes.keySet(), attributeNames); } }
public FullTextSearchProcessor(SearchContext context) { super(context); SearchParameters searchParameters = context.getSearchParameters(); StringBuilder queryString = new StringBuilder(); queryString.append("v.\"").append(Constants.ENTITY_TEXT_PROPERTY_KEY).append("\":(").append(searchParameters.getQuery()); // if search includes entity-type criteria, adding a filter here can help avoid unnecessary // processing (and rejection) by subsequent EntitySearchProcessor if (context.getEntityType() != null) { String typeAndSubTypeNamesQryStr = context.getEntityType().getTypeAndAllSubTypesQryStr(); if (typeAndSubTypeNamesQryStr.length() <= MAX_QUERY_STR_LENGTH_TYPES) { queryString.append(AND_STR).append(typeAndSubTypeNamesQryStr); } else { LOG.warn("'{}' has too many subtypes (query-string-length={}) to include in index-query; might cause poor performance", context.getEntityType().getTypeName(), typeAndSubTypeNamesQryStr.length()); } } // if search includes classification criteria, adding a filter here can help avoid unnecessary // processing (and rejection) by subsequent ClassificationSearchProcessor or EntitySearchProcessor if (context.getClassificationType() != null) { String typeAndSubTypeNamesStr = context.getClassificationType().getTypeAndAllSubTypesQryStr(); if (typeAndSubTypeNamesStr.length() <= MAX_QUERY_STR_LENGTH_TAGS) { queryString.append(AND_STR).append(typeAndSubTypeNamesStr); } else { LOG.warn("'{}' has too many subtypes (query-string-length={}) to include in index-query; might cause poor performance", context.getClassificationType().getTypeName(), typeAndSubTypeNamesStr.length()); } } queryString.append(")"); indexQuery = context.getGraph().indexQuery(Constants.FULLTEXT_INDEX, queryString.toString()); }
/** * ClassificationType needs to be constructed with a type registry so that is can resolve references * at constructor time. This is only used by junits. * * @param classificationDef * @param typeRegistry * @throws AtlasBaseException */ public AtlasClassificationType(AtlasClassificationDef classificationDef, AtlasTypeRegistry typeRegistry) throws AtlasBaseException { super(classificationDef); this.classificationDef = classificationDef; this.typeQryStr = AtlasAttribute.escapeIndexQueryValue(Collections.singleton(getTypeName())); resolveReferences(typeRegistry); }
@Override public boolean validateValue(Object obj, String objName, List<String> messages) { boolean ret = true; if (obj != null) { for (AtlasClassificationType superType : superTypes) { ret = superType.validateValue(obj, objName, messages) && ret; } ret = super.validateValue(obj, objName, messages) && ret; } return ret; }
classificationTypeAndSubTypes = classificationType.getTypeAndAllSubTypes(); } else { classificationTypeAndSubTypes = Collections.singleton(classificationType.getTypeName());
@Override public boolean isValidValue(Object obj) { if (obj != null) { for (AtlasClassificationType superType : superTypes) { if (!superType.isValidValue(obj)) { return false; } } if (!validateTimeBoundaries(obj, null)) { return false; } return super.isValidValue(obj); } return true; }
public static AtlasClassification newClassification(AtlasClassificationDef classificationDef, AtlasTypeRegistry typesRegistry) { AtlasClassification ret = null; AtlasClassificationType classificationType = typesRegistry.getClassificationTypeByName(classificationDef.getName()); if (classificationType != null) { ret = classificationType.createDefaultValue(); } else { LOG.error("failed to get classification-type {}", classificationDef.getName()); } return ret; }
/** * Validate if classification is not already associated with the entities * * @param guid unique entity id * @param classifications list of classifications to be associated */ private void validateEntityAssociations(String guid, List<AtlasClassification> classifications) throws AtlasBaseException { List<String> entityClassifications = getClassificationNames(guid); String entityTypeName = AtlasGraphUtilsV2.getTypeNameFromGuid(guid); AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityTypeName); for (AtlasClassification classification : classifications) { String newClassification = classification.getTypeName(); if (CollectionUtils.isNotEmpty(entityClassifications) && entityClassifications.contains(newClassification)) { throw new AtlasBaseException(AtlasErrorCode.INVALID_PARAMETERS, "entity: " + guid + ", already associated with classification: " + newClassification); } // for each classification, check whether there are entities it should be restricted to AtlasClassificationType classificationType = typeRegistry.getClassificationTypeByName(newClassification); if (!classificationType.canApplyToEntityType(entityType)) { throw new AtlasBaseException(AtlasErrorCode.INVALID_ENTITY_FOR_CLASSIFICATION, guid, entityTypeName, newClassification); } } }