Commit 4f4ab9b9 by Madhan Neethiraj

ATLAS-1522: v1 to v2 entity converter fixes

parent d8c2a10e
...@@ -54,7 +54,7 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter { ...@@ -54,7 +54,7 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter {
@Override @Override
public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext context) throws AtlasBaseException { public Object fromV1ToV2(Object v1Obj, AtlasType type, ConverterContext context) throws AtlasBaseException {
Object ret = null; AtlasObjectId ret = null;
if (v1Obj != null) { if (v1Obj != null) {
AtlasEntityType entityType = (AtlasEntityType) type; AtlasEntityType entityType = (AtlasEntityType) type;
...@@ -64,43 +64,46 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter { ...@@ -64,43 +64,46 @@ public class AtlasEntityFormatConverter extends AtlasStructFormatConverter {
ret = new AtlasObjectId(id.getTypeName(), id._getId()); ret = new AtlasObjectId(id.getTypeName(), id._getId());
} else if (v1Obj instanceof IReferenceableInstance) { } else if (v1Obj instanceof IReferenceableInstance) {
IReferenceableInstance entity = (IReferenceableInstance) v1Obj; IReferenceableInstance entRef = (IReferenceableInstance) v1Obj;
Map<String, Object> v1Attribs = null;
ret = new AtlasObjectId(entity.getTypeName(), entity.getId()._getId()); ret = new AtlasObjectId(entRef.getTypeName(), entRef.getId()._getId());
try { if (!context.entityExists(ret.getGuid())) {
v1Attribs = entity.getValuesMap(); Map<String, Object> v1Attribs = null;
} catch (AtlasException excp) {
LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
}
AtlasEntityWithAssociations ret1 = new AtlasEntityWithAssociations(entity.getTypeName(), super.fromV1ToV2(entityType, v1Attribs, context)); try {
ret1.setGuid(entity.getId()._getId()); v1Attribs = entRef.getValuesMap();
ret1.setStatus(convertState(entity.getId().getState())); } catch (AtlasException excp) {
AtlasSystemAttributes systemAttributes = entity.getSystemAttributes(); LOG.error("IReferenceableInstance.getValuesMap() failed", excp);
ret1.setCreatedBy(systemAttributes.createdBy);
ret1.setCreateTime(systemAttributes.createdTime);
ret1.setUpdatedBy(systemAttributes.modifiedBy);
ret1.setUpdateTime(systemAttributes.modifiedTime);
ret1.setVersion(new Long(entity.getId().version));
if (CollectionUtils.isNotEmpty(entity.getTraits())) {
List<AtlasClassification> classifications = new ArrayList<>();
AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
for (String traitName : entity.getTraits()) {
IStruct trait = entity.getTrait(traitName);
AtlasType classifiType = typeRegistry.getType(traitName);
AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait, classifiType, context);
classifications.add(classification);
} }
ret1.setClassifications(classifications); AtlasEntityWithAssociations entity = new AtlasEntityWithAssociations(entRef.getTypeName(),
} super.fromV1ToV2(entityType, v1Attribs, context));
entity.setGuid(entRef.getId()._getId());
entity.setStatus(convertState(entRef.getId().getState()));
entity.setCreatedBy(entRef.getSystemAttributes().createdBy);
entity.setCreateTime(entRef.getSystemAttributes().createdTime);
entity.setUpdatedBy(entRef.getSystemAttributes().modifiedBy);
entity.setUpdateTime(entRef.getSystemAttributes().modifiedTime);
entity.setVersion(new Long(entRef.getId().version));
if (CollectionUtils.isNotEmpty(entRef.getTraits())) {
List<AtlasClassification> classifications = new ArrayList<>();
AtlasFormatConverter traitConverter = converterRegistry.getConverter(TypeCategory.CLASSIFICATION);
for (String traitName : entRef.getTraits()) {
IStruct trait = entRef.getTrait(traitName);
AtlasType classifiType = typeRegistry.getType(traitName);
AtlasClassification classification = (AtlasClassification) traitConverter.fromV1ToV2(trait, classifiType, context);
classifications.add(classification);
}
entity.setClassifications(classifications);
}
context.addEntity(ret1); context.addEntity(entity);
}
} else { } else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "IReferenceableInstance", throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "IReferenceableInstance",
v1Obj.getClass().getCanonicalName()); v1Obj.getClass().getCanonicalName());
......
...@@ -46,18 +46,14 @@ public interface AtlasFormatConverter { ...@@ -46,18 +46,14 @@ public interface AtlasFormatConverter {
} }
public void addEntity(AtlasEntity entity) { public void addEntity(AtlasEntity entity) {
if (entities == null) { if (entity instanceof AtlasEntityWithAssociations) {
entities = new HashMap<>(); this.addEntity((AtlasEntityWithAssociations)entity);
} else {
this.addEntity(new AtlasEntityWithAssociations(entity));
} }
entities.put(entity.getGuid(), new AtlasEntityWithAssociations(entity));
} }
public boolean exists(AtlasEntityWithAssociations entity) { public AtlasEntityWithAssociations getById(String guid) {
return entities != null ? entities.containsKey(entity.getGuid()) : false;
}
public AtlasEntity getById(String guid) {
if( entities != null) { if( entities != null) {
return entities.get(guid); return entities.get(guid);
} }
...@@ -65,17 +61,10 @@ public interface AtlasFormatConverter { ...@@ -65,17 +61,10 @@ public interface AtlasFormatConverter {
return null; return null;
} }
public boolean entityExists(String guid) { return entities != null && entities.containsKey(guid); }
public Map<String, AtlasEntityWithAssociations> getEntities() { public Map<String, AtlasEntityWithAssociations> getEntities() {
return entities; return entities;
} }
public void addEntities(Map<String, AtlasEntity> entities) {
if (this.entities == null) {
this.entities = new HashMap<>(entities.size());
}
for (String entityId : entities.keySet()) {
this.entities.put(entityId, new AtlasEntityWithAssociations(entities.get(entityId)));
}
}
} }
} }
...@@ -129,13 +129,16 @@ public class AtlasInstanceRestAdapters { ...@@ -129,13 +129,16 @@ public class AtlasInstanceRestAdapters {
public Map<String, AtlasEntityWithAssociations> getAtlasEntity(IReferenceableInstance referenceable) throws AtlasBaseException { public Map<String, AtlasEntityWithAssociations> getAtlasEntity(IReferenceableInstance referenceable) throws AtlasBaseException {
AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY); AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY);
AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName()); AtlasEntityType entityType = typeRegistry.getEntityTypeByName(referenceable.getTypeName());
if (entityType == null) { if (entityType == null) {
throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName()); throw new AtlasBaseException(AtlasErrorCode.TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), referenceable.getTypeName());
} }
AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext(); AtlasFormatConverter.ConverterContext ctx = new AtlasFormatConverter.ConverterContext();
converter.fromV1ToV2(referenceable, entityType, ctx); converter.fromV1ToV2(referenceable, entityType, ctx);
return ctx.getEntities(); return ctx.getEntities();
} }
......
...@@ -157,24 +157,17 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { ...@@ -157,24 +157,17 @@ public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter {
ret = new HashMap<>(); ret = new HashMap<>();
for (AtlasStructType.AtlasAttribute attr : structType.getAllAttributes().values()) { for (AtlasStructType.AtlasAttribute attr : structType.getAllAttributes().values()) {
AtlasType attrType = attr.getAttributeType(); AtlasType attrType = attr.getAttributeType();
if (attrType == null) { if (attrType == null) {
LOG.warn("ignored attribute {}.{}: failed to find AtlasType", structType.getTypeName(), attr.getName()); LOG.warn("ignored attribute {}.{}: failed to find AtlasType", structType.getTypeName(), attr.getName());
continue; continue;
} }
Object v1Value = attributes.get(attr.getName()); AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
Object v2Value = null; Object v1Value = attributes.get(attr.getName());
Object v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
AtlasFormatConverter attrConverter = null;
if (attrType.getTypeCategory() == TypeCategory.ENTITY && !attr.isContainedAttribute()) {
attrConverter = new AtlasObjectIdConverter(converterRegistry, typeRegistry);
v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
} else {
attrConverter = converterRegistry.getConverter(attrType.getTypeCategory());
v2Value = attrConverter.fromV1ToV2(v1Value, attrType, context);
}
ret.put(attr.getAttributeDef().getName(), v2Value); ret.put(attr.getAttributeDef().getName(), v2Value);
} }
} }
......
...@@ -84,13 +84,11 @@ public class EntityREST { ...@@ -84,13 +84,11 @@ public class EntityREST {
@Path("/guid/{guid}") @Path("/guid/{guid}")
@Produces(Servlets.JSON_MEDIA_TYPE) @Produces(Servlets.JSON_MEDIA_TYPE)
public List<AtlasEntityWithAssociations> getById(@PathParam("guid") String guid) throws AtlasBaseException { public List<AtlasEntityWithAssociations> getById(@PathParam("guid") String guid) throws AtlasBaseException {
List<AtlasEntityWithAssociations> entityList = new ArrayList<>();
try { try {
ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid); ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid);
Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref); Map<String, AtlasEntityWithAssociations> entities = restAdapters.getAtlasEntity(ref);
entityList.addAll(entityRet.values());
return entityList; return getOrderedEntityList(entities, guid);
} catch (AtlasException e) { } catch (AtlasException e) {
throw toAtlasBaseException(e); throw toAtlasBaseException(e);
} }
...@@ -106,20 +104,9 @@ public class EntityREST { ...@@ -106,20 +104,9 @@ public class EntityREST {
@Path("/guid/{guid}/associations") @Path("/guid/{guid}/associations")
@Produces(Servlets.JSON_MEDIA_TYPE) @Produces(Servlets.JSON_MEDIA_TYPE)
public List<AtlasEntityWithAssociations> getWithAssociationsByGuid(@PathParam("guid") String guid) throws AtlasBaseException { public List<AtlasEntityWithAssociations> getWithAssociationsByGuid(@PathParam("guid") String guid) throws AtlasBaseException {
return this.getById(guid);
List<AtlasEntityWithAssociations> entityList = new ArrayList<>();
try {
ITypedReferenceableInstance ref = metadataService.getEntityDefinition(guid);
Map<String, AtlasEntityWithAssociations> entityRet = restAdapters.getAtlasEntity(ref);
entityList.addAll(entityRet.values());
return entityList;
} catch (AtlasException e) {
throw toAtlasBaseException(e);
}
} }
/** /**
* Delete an entity identified by its GUID * Delete an entity identified by its GUID
* *
...@@ -212,7 +199,6 @@ public class EntityREST { ...@@ -212,7 +199,6 @@ public class EntityREST {
return entityList; return entityList;
} }
/** /**
* Gets the list of classifications for a given entity represented by a guid. * Gets the list of classifications for a given entity represented by a guid.
* *
...@@ -373,4 +359,18 @@ public class EntityREST { ...@@ -373,4 +359,18 @@ public class EntityREST {
throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, entityType.getTypeName(), attributeName); throw new AtlasBaseException(AtlasErrorCode.ATTRIBUTE_UNIQUE_INVALID, entityType.getTypeName(), attributeName);
} }
} }
private List<AtlasEntityWithAssociations> getOrderedEntityList(Map<String, AtlasEntityWithAssociations> entities, String firstItemGuid) {
List<AtlasEntityWithAssociations> ret = new ArrayList<>(entities.size());
for (AtlasEntityWithAssociations entity : entities.values()) {
if (StringUtils.equals(entity.getGuid(), firstItemGuid)) {
ret.add(0, entity);
} else {
ret.add(entity);
}
}
return ret;
}
} }
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