@Override public void resolveReferencesPhase3(AtlasTypeRegistry typeRegistry) throws AtlasBaseException { for (AtlasAttributeDef attributeDef : getStructDef().getAttributeDefs()) { String attributeName = attributeDef.getName(); AtlasType attributeType = typeRegistry.getType(attributeDef.getTypeName()); AtlasEntityType attributeEntityType = getReferencedEntityType(attributeType); // validate if RelationshipDefs is defined for all entityDefs if (attributeEntityType != null && !hasRelationshipAttribute(attributeName)) { LOG.warn("No RelationshipDef defined between {} and {} on attribute: {}.{}", getTypeName(), attributeEntityType.getTypeName(), getTypeName(), attributeName); } } for (String superTypeName : allSuperTypes) { AtlasEntityType superType = typeRegistry.getEntityTypeByName(superTypeName); Map<String, AtlasAttribute> superTypeRelationshipAttributes = superType.getRelationshipAttributes(); if (MapUtils.isNotEmpty(superTypeRelationshipAttributes)) { relationshipAttributes.putAll(superTypeRelationshipAttributes); } Map<String, List<AtlasRelationshipType>> superTypeRelationshipAttributesType = superType.getRelationshipAttributesType(); if (MapUtils.isNotEmpty(superTypeRelationshipAttributesType)) { relationshipAttributesType.putAll(superTypeRelationshipAttributesType); } } allSubTypes = Collections.unmodifiableSet(allSubTypes); typeAndAllSubTypes = Collections.unmodifiableSet(typeAndAllSubTypes); typeAndAllSubTypesQryStr = ""; // will be computed on next access relationshipAttributes = Collections.unmodifiableMap(relationshipAttributes); relationshipAttributesType = Collections.unmodifiableMap(relationshipAttributesType); }
private void restoreEntityAttributes(AtlasEntity entity, Map<String, Object> prunedAttributes) { if (MapUtils.isEmpty(prunedAttributes)) { return; } AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entity.getTypeName()); if (entityType != null && MapUtils.isNotEmpty(entityType.getAllAttributes())) { for (AtlasAttribute attribute : entityType.getAllAttributes().values()) { String attrName = attribute.getName(); if (prunedAttributes.containsKey(attrName)) { entity.setAttribute(attrName, prunedAttributes.get(attrName)); } } } }
private String getRelationshipEdgeLabel(String typeName, String relationshipTypeName) { AtlasRelationshipType relationshipType = typeRegistry.getRelationshipTypeByName(relationshipTypeName); AtlasRelationshipDef relationshipDef = relationshipType.getRelationshipDef(); AtlasEntityType end1Type = relationshipType.getEnd1Type(); AtlasEntityType end2Type = relationshipType.getEnd2Type(); Set<String> vertexTypes = getTypeAndAllSuperTypes(typeName); AtlasAttribute attribute = null; if (vertexTypes.contains(end1Type.getTypeName())) { String attributeName = relationshipDef.getEndDef1().getName(); attribute = (attributeName != null) ? end1Type.getAttribute(attributeName) : null; } else if (vertexTypes.contains(end2Type.getTypeName())) { String attributeName = relationshipDef.getEndDef2().getName(); attribute = (attributeName != null) ? end2Type.getAttribute(attributeName) : null; } return (attribute != null) ? attribute.getRelationshipEdgeLabel() : null; }
private void addEntityType(AtlasEntityType entityType, ExportContext context) { if (!context.entityTypes.contains(entityType.getTypeName())) { context.entityTypes.add(entityType.getTypeName()); addAttributeTypes(entityType, context); if (CollectionUtils.isNotEmpty(entityType.getAllSuperTypes())) { for (String superType : entityType.getAllSuperTypes()) { addEntityType(superType, context); } } } }
public AtlasEntityType(AtlasEntityDef entityDef, AtlasTypeRegistry typeRegistry) throws AtlasBaseException { super(entityDef); this.entityDef = entityDef; 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) { if (obj instanceof AtlasEntity || obj instanceof Map) { for (AtlasEntityType superType : superTypes) { ret = superType.validateValue(obj, objName, messages) && ret; } ret = super.validateValue(obj, objName, messages) && validateRelationshipAttributes(obj, objName, messages) && ret; } else { ret = false; messages.add(objName + ": invalid value type '" + obj.getClass().getName()); } } return ret; }
private String getLegacyEdgeLabel(AtlasEntityType entityType, String attributeName) { String ret = null; AtlasAttribute attribute = entityType.getAttribute(attributeName); if (attribute != null) { ret = "__" + attribute.getQualifiedName(); } return ret; } }
private void addSubType(AtlasEntityType subType) { subTypes.add(subType.getTypeName()); }
public static AtlasVertex findByUniqueAttributes(AtlasEntityType entityType, Map<String, Object> attrValues) { AtlasVertex vertex = null; final Map<String, AtlasAttribute> uniqueAttributes = entityType.getUniqAttributes(); if (MapUtils.isNotEmpty(uniqueAttributes) && MapUtils.isNotEmpty(attrValues)) { for (AtlasAttribute attribute : uniqueAttributes.values()) { Object attrValue = attrValues.get(attribute.getName()); if (attrValue == null) { continue; } if (canUseIndexQuery(entityType, attribute.getName())) { vertex = AtlasGraphUtilsV2.getAtlasVertexFromIndexQuery(entityType, attribute, attrValue); } else { vertex = AtlasGraphUtilsV2.findByTypeAndPropertyName(entityType.getTypeName(), attribute.getVertexPropertyName(), attrValue); // if no instance of given typeName is found, try to find an instance of type's sub-type if (vertex == null && !entityType.getAllSubTypes().isEmpty()) { vertex = AtlasGraphUtilsV2.findBySuperTypeAndPropertyName(entityType.getTypeName(), attribute.getVertexPropertyName(), attrValue); } } if (vertex != null) { if (LOG.isDebugEnabled()) { LOG.debug("findByUniqueAttributes(type={}, attrName={}, attrValue={}: found vertex {}", entityType.getTypeName(), attribute.getName(), attrValue, vertex); } break; } } } return vertex; }
@Test public void testEntitySerDe() throws AtlasBaseException { AtlasEntityDef entityDef = ModelTestUtil.getEntityDef(); AtlasTypeRegistry typeRegistry = ModelTestUtil.getTypesRegistry(); AtlasEntityType entityType = typeRegistry.getEntityTypeByName(entityDef.getName()); assertNotNull(entityType); AtlasEntity ent1 = entityType.createDefaultValue(); String jsonString = AtlasType.toJson(ent1); AtlasEntity ent2 = AtlasType.fromJson(jsonString, AtlasEntity.class); entityType.normalizeAttributeValues(ent2); assertEquals(ent2, ent1, "Incorrect serialization/deserialization of AtlasEntity"); }
@Test public void testEntityTypeDefaultValue() { AtlasEntity defValue = entityType.createDefaultValue(); assertNotNull(defValue); assertEquals(defValue.getTypeName(), entityType.getTypeName()); }
private void visitRelationships(AtlasEntityType entityType, AtlasEntity entity, List<String> visitedAttributes) throws AtlasBaseException { for (AtlasAttribute attribute : entityType.getRelationshipAttributes().values()) { AtlasType attrType = attribute.getAttributeType(); String attrName = attribute.getName(); Object attrVal = entity.getRelationshipAttribute(attrName); if (entity.hasRelationshipAttribute(attrName)) { visitAttribute(attrType, attrVal); visitedAttributes.add(attrName); } } }
boolean isAssignableFrom(AtlasObjectId objId) { boolean ret = AtlasTypeUtil.isValid(objId) && (StringUtils.equals(objId.getTypeName(), getTypeName()) || isSuperTypeOf(objId.getTypeName())); return ret; }
@Override public void validateAndNormalize(AtlasEntity entity) throws AtlasBaseException { List<String> messages = new ArrayList<>(); if (! AtlasTypeUtil.isValidGuid(entity.getGuid())) { throw new AtlasBaseException(AtlasErrorCode.INVALID_OBJECT_ID, "invalid guid " + entity.getGuid()); } AtlasEntityType type = typeRegistry.getEntityTypeByName(entity.getTypeName()); if (type == null) { throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), entity.getTypeName()); } type.validateValue(entity, entity.getTypeName(), messages); if (!messages.isEmpty()) { throw new AtlasBaseException(AtlasErrorCode.INSTANCE_CRUD_INVALID_PARAMS, messages); } type.getNormalizedValue(entity); }
protected AtlasEntity getDefaultAtlasEntity(T obj) throws AtlasBaseException { AtlasEntity ret = getEntityType().createDefaultValue(); if (obj != null) { if (StringUtils.isNotEmpty(obj.getGuid())) { ret.setGuid(obj.getGuid()); } } return ret; }
public static ClassTypeDefinition toClassTypeDefinition(final AtlasEntityType entityType) { ClassTypeDefinition ret = null; if (entityType != null) { AtlasEntityDef entityDef = entityType.getEntityDef(); ret = new ClassTypeDefinition(); ret.setTypeName(entityDef.getName()); ret.setTypeDescription(entityDef.getDescription()); ret.setTypeVersion(entityDef.getTypeVersion()); ret.setSuperTypes(entityDef.getSuperTypes()); if (MapUtils.isNotEmpty(entityType.getAllAttributes())) { List<AttributeDefinition> attributeDefinitions = entityType.getAllAttributes() .entrySet() .stream() .map(e -> toV1AttributeDefinition(e.getValue())) .collect(Collectors.toList()); ret.setAttributeDefinitions(attributeDefinitions); } } return ret; }
private static boolean isDataSet(String typeName, AtlasTypeRegistry registry) throws AtlasBaseException { boolean ret = false; AtlasType type = registry.getType(typeName); if (type instanceof AtlasEntityType) { AtlasEntityType entityType = (AtlasEntityType) type; ret = entityType.getAllSuperTypes().contains(AtlasBaseTypeDef.ATLAS_TYPE_DATASET); } return ret; }
public void normalizeRelationshipAttributeValues(Map<String, Object> obj) { if (obj != null) { for (AtlasAttribute attribute : relationshipAttributes.values()) { String attributeName = attribute.getName(); AtlasAttributeDef attributeDef = attribute.getAttributeDef(); if (obj.containsKey(attributeName)) { Object attributeValue = getNormalizedValue(obj.get(attributeName), attributeDef); obj.put(attributeName, attributeValue); } } } }
private static AtlasEntityType getEntityType(AtlasEntityDef entityDef) { try { return new AtlasEntityType(entityDef, ModelTestUtil.getTypesRegistry()); } catch (AtlasBaseException excp) { return null; } }
@Override public boolean validateValue(Object obj, String objName, List<String> messages) { boolean ret = true; if (obj != null) { if (obj instanceof AtlasEntity || obj instanceof Map) { for (AtlasEntityType superType : superTypes) { ret = superType.validateValue(obj, objName, messages) && ret; } ret = super.validateValue(obj, objName, messages) && ret; } else { ret = false; messages.add(objName + ": invalid value type '" + obj.getClass().getName()); } } return ret; }