Commit 44df46cf by Madhan Neethiraj

ATLAS-1614: updated v1 partial-update to perform attribute validation

parent 6fd04d9a
......@@ -66,6 +66,7 @@ public enum AtlasErrorCode {
INVALID_STRUCT_VALUE(400, "ATLAS40036E", "not a valid struct value {0}"),
INSTANCE_LINEAGE_INVALID_PARAMS(400, "ATLAS40037E", "Invalid lineage query parameters passed {0}: {1}"),
ATTRIBUTE_UPDATE_NOT_SUPPORTED(400, "ATLAS40038E", "{0}.{1} : attribute update not supported"),
INVALID_VALUE(400, "ATLAS40039E", "invalid value: {0}"),
// All Not found enums go here
TYPE_NAME_NOT_FOUND(404, "ATLAS4041E", "Given typename {0} was invalid"),
......
......@@ -49,6 +49,7 @@ import org.apache.atlas.typesystem.exception.EntityNotFoundException;
import org.apache.atlas.typesystem.exception.TraitNotFoundException;
import org.apache.atlas.typesystem.exception.TypeNotFoundException;
import org.apache.atlas.repository.converters.AtlasFormatConverter.ConverterContext;
import org.apache.atlas.typesystem.types.ValueConversionException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
......@@ -149,6 +150,13 @@ public class AtlasInstanceConverter {
throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
}
// validate
try {
metadataService.validateAndConvertToTypedInstance(referenceable, entityType.getTypeName());
} catch (AtlasException excp) {
throw toAtlasBaseException(excp);
}
ConverterContext ctx = new ConverterContext();
AtlasEntity entity = converter.fromV1ToV2(referenceable, entityType, ctx);
......@@ -199,6 +207,10 @@ public class AtlasInstanceConverter {
return new AtlasBaseException(AtlasErrorCode.TYPE_NAME_NOT_FOUND, e);
}
if (e instanceof ValueConversionException) {
return new AtlasBaseException(AtlasErrorCode.INVALID_VALUE, e, e.getMessage());
}
return new AtlasBaseException(e);
}
......
......@@ -42,6 +42,7 @@ import org.apache.atlas.repository.audit.EntityAuditRepository;
import org.apache.atlas.repository.graph.GraphHelper;
import org.apache.atlas.repository.typestore.ITypeStore;
import org.apache.atlas.type.AtlasTypeUtil;
import org.apache.atlas.typesystem.IReferenceableInstance;
import org.apache.atlas.typesystem.IStruct;
import org.apache.atlas.typesystem.ITypedReferenceableInstance;
import org.apache.atlas.typesystem.ITypedStruct;
......@@ -476,7 +477,7 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
newEntity = ParamChecker.notNull(newEntity, "updatedEntity cannot be null");
ITypedReferenceableInstance existInstance = validateEntityExists(guid);
ITypedReferenceableInstance newInstance = convertToTypedInstance(newEntity, existInstance.getTypeName());
ITypedReferenceableInstance newInstance = validateAndConvertToTypedInstance(newEntity, existInstance.getTypeName());
((ReferenceableInstance)newInstance).replaceWithNewId(new Id(guid, 0, newInstance.getTypeName()));
CreateUpdateEntitiesResult result = repository.updatePartial(newInstance);
......@@ -484,10 +485,11 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
return result;
}
private ITypedReferenceableInstance convertToTypedInstance(Referenceable updatedEntity, String typeName)
@Override
public ITypedReferenceableInstance validateAndConvertToTypedInstance(IReferenceableInstance updatedEntity, String typeName)
throws AtlasException {
ClassType type = typeSystem.getDataType(ClassType.class, typeName);
ITypedReferenceableInstance newInstance = type.createInstance();
ITypedReferenceableInstance newInstance = type.createInstance(updatedEntity.getId());
for (String attributeName : updatedEntity.getValuesMap().keySet()) {
AttributeInfo attributeInfo = type.fieldMapping.fields.get(attributeName);
......@@ -538,7 +540,7 @@ public class DefaultMetadataService implements MetadataService, ActiveStateChang
ITypedReferenceableInstance oldInstance = getEntityDefinitionReference(typeName, uniqueAttributeName, attrValue);
final ITypedReferenceableInstance newInstance = convertToTypedInstance(updatedEntity, typeName);
final ITypedReferenceableInstance newInstance = validateAndConvertToTypedInstance(updatedEntity, typeName);
((ReferenceableInstance)newInstance).replaceWithNewId(oldInstance.getId());
CreateUpdateEntitiesResult result = repository.updatePartial(newInstance);
......
......@@ -23,6 +23,7 @@ import org.apache.atlas.AtlasException;
import org.apache.atlas.CreateUpdateEntitiesResult;
import org.apache.atlas.EntityAuditEvent;
import org.apache.atlas.listener.EntityChangeListener;
import org.apache.atlas.typesystem.IReferenceableInstance;
import org.apache.atlas.typesystem.ITypedReferenceableInstance;
import org.apache.atlas.typesystem.ITypedStruct;
import org.apache.atlas.typesystem.Referenceable;
......@@ -309,4 +310,7 @@ public interface MetadataService {
* @throws AtlasException
*/
ITypedReferenceableInstance[] deserializeClassInstances(String entityInstanceDefinition) throws AtlasException;
ITypedReferenceableInstance validateAndConvertToTypedInstance(IReferenceableInstance updatedEntity, String typeName)
throws AtlasException;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment