diff --git a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java index 80a539a..65726c3 100644 --- a/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java +++ b/intg/src/main/java/org/apache/atlas/AtlasErrorCode.java @@ -42,6 +42,7 @@ public enum AtlasErrorCode { CANNOT_ADD_MANDATORY_ATTRIBUTE(400, "ATLAS40012E", "{0}.{1} : can not add mandatory attribute"), ATTRIBUTE_DELETION_NOT_SUPPORTED(400, "ATLAS40013E", "{0}.{1} : attribute delete not supported"), SUPERTYPE_REMOVAL_NOT_SUPPORTED(400, "ATLAS40014E", "superType remove not supported"), + UNEXPECTED_TYPE(400, "ATLAS40015E", "expected type {0}; found {1}"), TYPE_NAME_NOT_FOUND(404, "ATLAS4041E", "Given typename {0} was invalid"), TYPE_GUID_NOT_FOUND(404, "ATLAS4042E", "Given type guid {0} was invalid"), diff --git a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java index 6cecd46..cdeb117 100755 --- a/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java +++ b/repository/src/main/java/org/apache/atlas/repository/graph/GraphHelper.java @@ -787,7 +787,7 @@ public final class GraphHelper { return instanceVertex.getProperty(actualPropertyName, AtlasEdge.class); } else { - return instanceVertex.getProperty(actualPropertyName, String.class).toString(); + return instanceVertex.getProperty(actualPropertyName, Object.class).toString(); } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasClassificationToTraitConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasAbstractFormatConverter.java similarity index 54% rename from webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasClassificationToTraitConverter.java rename to webapp/src/main/java/org/apache/atlas/web/adapters/AtlasAbstractFormatConverter.java index ece483c..f1f3d18 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasClassificationToTraitConverter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasAbstractFormatConverter.java @@ -7,7 +7,7 @@ * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -15,39 +15,27 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.atlas.web.adapters.v2; +package org.apache.atlas.web.adapters; + -import com.google.inject.Inject; -import com.google.inject.Singleton; -import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasClassification; -import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.model.typedef.AtlasStructDef; -import org.apache.atlas.type.AtlasClassificationType; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasStructType; -import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -@Singleton -public class AtlasClassificationToTraitConverter extends AtlasStructToStructConverter { +public abstract class AtlasAbstractFormatConverter implements AtlasFormatConverter { + protected final AtlasFormatConverters converterRegistry; + protected final AtlasTypeRegistry typeRegistry; + protected final TypeCategory typeCategory; - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - super.init(registry); + protected AtlasAbstractFormatConverter(AtlasFormatConverters converterRegistry, AtlasTypeRegistry typeRegistry, TypeCategory typeCategory) { + this.converterRegistry = converterRegistry; + this.typeRegistry = typeRegistry; + this.typeCategory = typeCategory; } @Override public TypeCategory getTypeCategory() { - return TypeCategory.CLASSIFICATION; + return typeCategory; } } + diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasArrayFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasArrayFormatConverter.java index 5485efa..e3b4efa 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasArrayFormatConverter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasArrayFormatConverter.java @@ -18,61 +18,81 @@ package org.apache.atlas.web.adapters; +import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; import org.apache.atlas.type.AtlasArrayType; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; -import javax.inject.Inject; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; -import java.util.Map; import java.util.Set; -import static org.apache.atlas.web.adapters.AtlasFormatConverters.VERSION_V1; -import static org.apache.atlas.web.adapters.AtlasFormatConverters.VERSION_V2; -public class AtlasArrayFormatConverter implements AtlasFormatAdapter { +public class AtlasArrayFormatConverter extends AtlasAbstractFormatConverter { - protected AtlasFormatConverters registry; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2); - registry.registerConverter(this, AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1); + public AtlasArrayFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.ARRAY); } @Override - public TypeCategory getTypeCategory() { - return TypeCategory.ARRAY; + public Collection fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + Collection ret = null; + + if (v1Obj != null) { + if (v1Obj instanceof List) { + ret = new ArrayList(); + } else if (v1Obj instanceof Set) { + ret = new LinkedHashSet(); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "List or Set", + v1Obj.getClass().getCanonicalName()); + } + + AtlasArrayType arrType = (AtlasArrayType) type; + AtlasType elemType = arrType.getElementType(); + AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory()); + Collection v1List = (Collection) v1Obj; + + for (Object v1Elem : v1List) { + Object convertedVal = elemConverter.fromV1ToV2(v1Elem, elemType); + + ret.add(convertedVal); + } + } + + return ret; } @Override - public Object convert(String sourceVersion, String targetVersion, AtlasType type, final Object source) throws AtlasBaseException { - Collection newCollection = null; - if ( source != null ) { - if (AtlasFormatConverters.isArrayListType(source.getClass())) { - newCollection = new ArrayList(); - } else if (AtlasFormatConverters.isSetType(source.getClass())) { - newCollection = new LinkedHashSet(); + public Collection fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + Collection ret = null; + + if (v2Obj != null) { + if (v2Obj instanceof List) { + ret = new ArrayList(); + } else if (v2Obj instanceof Set) { + ret = new LinkedHashSet(); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "List or Set", + v2Obj.getClass().getCanonicalName()); } - AtlasArrayType arrType = (AtlasArrayType) type; - AtlasType elemType = arrType.getElementType(); + AtlasArrayType arrType = (AtlasArrayType) type; + AtlasType elemType = arrType.getElementType(); + AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory()); + Collection v2List = (Collection) v2Obj; - Collection originalList = (Collection) source; - for (Object elem : originalList) { - AtlasFormatAdapter elemConverter = registry.getConverter(sourceVersion, targetVersion, elemType.getTypeCategory()); - Object convertedVal = elemConverter.convert(sourceVersion, targetVersion, elemType, elem); + for (Object v2Elem : v2List) { + Object convertedVal = elemConverter.fromV2ToV1(v2Elem, elemType); - newCollection.add(convertedVal); + ret.add(convertedVal); } } - return newCollection; - } + return ret; + } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasClassificationFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasClassificationFormatConverter.java new file mode 100644 index 0000000..da71c31 --- /dev/null +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasClassificationFormatConverter.java @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.adapters; + +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.AtlasException; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.TypeCategory; +import org.apache.atlas.model.instance.AtlasClassification; +import org.apache.atlas.type.AtlasClassificationType; +import org.apache.atlas.type.AtlasType; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.atlas.typesystem.IStruct; +import org.apache.commons.collections.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +public class AtlasClassificationFormatConverter extends AtlasStructFormatConverter { + private static final Logger LOG = LoggerFactory.getLogger(AtlasClassificationFormatConverter.class); + + public AtlasClassificationFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.CLASSIFICATION); + } + + @Override + public AtlasClassification fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + AtlasClassification ret = null; + + if (v1Obj != null) { + AtlasClassificationType classificationType = (AtlasClassificationType)type; + + if (v1Obj instanceof Map) { + final Map v1Map = (Map) v1Obj; + final Map v1Attribs = (Map) v1Map.get(ATTRIBUTES_PROPERTY_KEY); + + if (MapUtils.isNotEmpty(v1Attribs)) { + ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, v1Attribs)); + } else { + ret = new AtlasClassification(type.getTypeName()); + } + } else if (v1Obj instanceof IStruct) { + IStruct struct = (IStruct) v1Obj; + Map<String, Object> v1Attribs = null; + + try { + v1Attribs = struct.getValuesMap(); + } catch (AtlasException excp) { + LOG.error("IStruct.getValuesMap() failed", excp); + } + + ret = new AtlasClassification(type.getTypeName(), fromV1ToV2(classificationType, v1Attribs)); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", + v1Obj.getClass().getCanonicalName()); + } + } + + return ret; + } +} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java new file mode 100644 index 0000000..b8537c4 --- /dev/null +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEntityFormatConverter.java @@ -0,0 +1,137 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.adapters; + +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.AtlasException; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.TypeCategory; +import org.apache.atlas.model.instance.AtlasClassification; +import org.apache.atlas.model.instance.AtlasEntity; +import org.apache.atlas.model.instance.AtlasEntityWithAssociations; +import org.apache.atlas.model.instance.AtlasObjectId; +import org.apache.atlas.type.AtlasEntityType; +import org.apache.atlas.type.AtlasType; +import org.apache.atlas.type.AtlasTypeRegistry; +import org.apache.atlas.typesystem.IReferenceableInstance; +import org.apache.atlas.typesystem.IStruct; +import org.apache.atlas.typesystem.Referenceable; +import org.apache.atlas.typesystem.persistence.Id; +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.collections.MapUtils; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class AtlasEntityFormatConverter extends AtlasStructFormatConverter { + private static final Logger LOG = LoggerFactory.getLogger(AtlasEntityFormatConverter.class); + + public AtlasEntityFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.ENTITY); + } + + @Override + public AtlasEntityWithAssociations fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + AtlasEntityWithAssociations ret = null; + + if (v1Obj != null) { + AtlasEntityType entityType = (AtlasEntityType) type; + + if (v1Obj instanceof Id) { + Id id = (Id) v1Obj; + + ret = new AtlasEntityWithAssociations(id.getTypeName()); + ret.setGuid(id.getId()._getId()); + } else if (v1Obj instanceof IReferenceableInstance) { + IReferenceableInstance entity = (IReferenceableInstance) v1Obj; + Map<String, Object> v1Attribs = null; + + try { + v1Attribs = entity.getValuesMap(); + } catch (AtlasException excp) { + LOG.error("IReferenceableInstance.getValuesMap() failed", excp); + } + + ret = new AtlasEntityWithAssociations(entity.getTypeName(), super.fromV1ToV2(entityType, v1Attribs)); + ret.setGuid(entity.getId()._getId()); + + 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); + + classifications.add(classification); + } + + ret.setClassifications(classifications); + } + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Id or IReferenceableInstance", + v1Obj.getClass().getCanonicalName()); + } + } + return ret; + } + + @Override + public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + Object ret = null; + + if (v2Obj != null) { + AtlasEntityType entityType = (AtlasEntityType) type; + + if (v2Obj instanceof Map) { + Map v2Map = (Map) v2Obj; + String idStr = (String)v2Map.get(AtlasObjectId.KEY_GUID); + String typeName = type.getTypeName(); + + if (StringUtils.isEmpty(idStr)) { + throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND); + } + + final Map v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY); + + if (MapUtils.isEmpty(v2Attribs)) { + ret = new Id(idStr, 0, typeName); + } else { + ret = new Referenceable(idStr, typeName, super.fromV2ToV1(entityType, v2Attribs)); + } + } else if (v2Obj instanceof AtlasEntity) { + AtlasEntity entity = (AtlasEntity) v2Obj; + + ret = new Referenceable(entity.getGuid(), entity.getTypeName(), + fromV2ToV1(entityType, entity.getAttributes())); + } else if (v2Obj instanceof String) { // transient-id + ret = new Referenceable((String) v2Obj, type.getTypeName(), null); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasEntity or String", + v2Obj.getClass().getCanonicalName()); + } + } + + return ret; + } +} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEnumFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEnumFormatConverter.java index ec0661d..4f22437 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEnumFormatConverter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasEnumFormatConverter.java @@ -23,18 +23,20 @@ import org.apache.atlas.model.TypeCategory; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; -import javax.inject.Inject; -public class AtlasEnumFormatConverter extends AtlasPrimitiveFormatConverter { +public class AtlasEnumFormatConverter extends AtlasAbstractFormatConverter { + public AtlasEnumFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.ENUM); + } - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - super.init(registry); + @Override + public Object fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + return type.getNormalizedValue(v1Obj); } @Override - public TypeCategory getTypeCategory() { - return TypeCategory.ENUM; + public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + return type.getNormalizedValue(v2Obj); } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatAdapter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java similarity index 88% rename from webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatAdapter.java rename to webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java index c146d72..079f3be 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatAdapter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverter.java @@ -22,10 +22,10 @@ import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; import org.apache.atlas.type.AtlasType; -public interface AtlasFormatAdapter { +public interface AtlasFormatConverter { + Object fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException; - Object convert(String sourceVersion, String targetVersion, AtlasType type, Object source) throws AtlasBaseException; + Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException; TypeCategory getTypeCategory(); - } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverters.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverters.java index 5238a75..7c3f536 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverters.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConverters.java @@ -22,67 +22,39 @@ import com.google.inject.Singleton; import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; +import org.apache.atlas.type.AtlasTypeRegistry; -import java.util.Date; import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.Set; @Singleton public class AtlasFormatConverters { - public static String VERSION_V1 = "v1"; - public static String VERSION_V2 = "v2"; - - private Map<String, AtlasFormatAdapter> registry = new HashMap<>(); - - public void registerConverter(AtlasFormatAdapter adapter, String sourceVersion, String targetVersion) { - registry.put(getKey(sourceVersion, targetVersion, adapter.getTypeCategory()), adapter); - } - - public AtlasFormatAdapter getConverter(String sourceVersion, String targetVersion, TypeCategory typeCategory) throws AtlasBaseException { - if (registry.containsKey(getKey(sourceVersion, targetVersion, typeCategory))) { - return registry.get(getKey(sourceVersion, targetVersion, typeCategory)); - } - throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, "Could not find the converter for this type " + typeCategory); - } - - public static boolean isArrayListType(Class c) { - return List.class.isAssignableFrom(c); + private final Map<TypeCategory, AtlasFormatConverter> registry = new HashMap<>(); + + @Inject + public AtlasFormatConverters(AtlasTypeRegistry typeRegistry) { + registerConverter(new AtlasPrimitiveFormatConverter(this, typeRegistry)); + registerConverter(new AtlasEnumFormatConverter(this, typeRegistry)); + registerConverter(new AtlasStructFormatConverter(this, typeRegistry)); + registerConverter(new AtlasClassificationFormatConverter(this, typeRegistry)); + registerConverter(new AtlasEntityFormatConverter(this, typeRegistry)); + registerConverter(new AtlasArrayFormatConverter(this, typeRegistry)); + registerConverter(new AtlasMapFormatConverter(this, typeRegistry)); } - public static boolean isSetType(Class c) { - return Set.class.isAssignableFrom(c); + private void registerConverter(AtlasFormatConverter converter) { + registry.put(converter.getTypeCategory(), converter); } - public static boolean isPrimitiveType(final Class c) { - if (c != null) { - if (Number.class.isAssignableFrom(c)) { - return true; - } + public AtlasFormatConverter getConverter(TypeCategory typeCategory) throws AtlasBaseException { + AtlasFormatConverter ret = registry.get(typeCategory); - if (String.class.isAssignableFrom(c)) { - return true; - } - - if (Date.class.isAssignableFrom(c)) { - return true; - } - - return c.isPrimitive(); - } - return false; - } - - public static boolean isMapType(Object o) { - if ( o != null ) { - return Map.class.isAssignableFrom(o.getClass()); + if (ret == null) { + throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR, + "Could not find the converter for this type " + typeCategory); } - return false; - } - String getKey(String sourceVersion, String targetVersion, TypeCategory typeCategory) { - return sourceVersion + "-to-" + targetVersion + "-" + typeCategory.name(); + return ret; } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConvertersModule.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConvertersModule.java deleted file mode 100644 index c3dfb61..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasFormatConvertersModule.java +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters; - -import com.google.inject.AbstractModule; -import com.google.inject.multibindings.Multibinder; -import org.apache.atlas.web.adapters.v1.ReferenceableToAtlasEntityConverter; -import org.apache.atlas.web.adapters.v1.StructToAtlasStructConverter; -import org.apache.atlas.web.adapters.v1.TraitToAtlasClassificationConverter; -import org.apache.atlas.web.adapters.v2.AtlasClassificationToTraitConverter; -import org.apache.atlas.web.adapters.v2.AtlasEntityToReferenceableConverter; -import org.apache.atlas.web.adapters.v2.AtlasStructToStructConverter; - -public class AtlasFormatConvertersModule extends AbstractModule { - - protected void configure() { - Multibinder<AtlasFormatAdapter> multibinder - = Multibinder.newSetBinder(binder(), AtlasFormatAdapter.class); - multibinder.addBinding().to(AtlasStructToStructConverter.class).asEagerSingleton(); - multibinder.addBinding().to(AtlasEntityToReferenceableConverter.class).asEagerSingleton(); - multibinder.addBinding().to(AtlasClassificationToTraitConverter.class).asEagerSingleton(); - - multibinder.addBinding().to(AtlasPrimitiveFormatConverter.class).asEagerSingleton(); - multibinder.addBinding().to(AtlasEnumFormatConverter.class).asEagerSingleton(); - multibinder.addBinding().to(AtlasMapFormatConverter.class).asEagerSingleton(); - multibinder.addBinding().to(AtlasArrayFormatConverter.class).asEagerSingleton(); - - multibinder.addBinding().to(ReferenceableToAtlasEntityConverter.class).asEagerSingleton(); - multibinder.addBinding().to(StructToAtlasStructConverter.class).asEagerSingleton(); - multibinder.addBinding().to(TraitToAtlasClassificationConverter.class).asEagerSingleton(); - } - -} \ No newline at end of file diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java index dc42f10..5756adb 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasInstanceRestAdapters.java @@ -72,10 +72,8 @@ public class AtlasInstanceRestAdapters { } public ITypedReferenceableInstance getITypedReferenceable(AtlasEntity entity) throws AtlasBaseException { - AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.ENTITY); - AtlasType entityType = typeRegistry.getType(entity.getTypeName()); + Referenceable ref = getReferenceable(entity); - Referenceable ref = (Referenceable) entityFormatter.convert(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, entityType, entity); try { return metadataService.getTypedReferenceableInstance(ref); } catch (AtlasException e) { @@ -85,19 +83,20 @@ public class AtlasInstanceRestAdapters { } public Referenceable getReferenceable(AtlasEntity entity) throws AtlasBaseException { - AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.ENTITY); - AtlasType entityType = typeRegistry.getType(entity.getTypeName()); + AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY); + AtlasType entityType = typeRegistry.getType(entity.getTypeName()); + Referenceable ref = (Referenceable)converter.fromV2ToV1(entity, entityType); - return (Referenceable) entityFormatter.convert(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, entityType, entity); + return ref; } public ITypedStruct getTrait(AtlasClassification classification) throws AtlasBaseException { - AtlasFormatAdapter formatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.CLASSIFICATION); - AtlasType clsType = typeRegistry.getType(classification.getTypeName()); + AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.CLASSIFICATION); + AtlasType classificationType = typeRegistry.getType(classification.getTypeName()); + Struct trait = (Struct)converter.fromV2ToV1(classification, classificationType); - Struct ref = (Struct) formatter.convert(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, clsType, classification); try { - return metadataService.createTraitInstance(ref); + return metadataService.createTraitInstance(trait); } catch (AtlasException e) { LOG.error("Exception while getting a typed reference for the entity ", e); throw toAtlasBaseException(e); @@ -105,17 +104,19 @@ public class AtlasInstanceRestAdapters { } public AtlasClassification getClassification(IStruct classification) throws AtlasBaseException { - AtlasFormatAdapter formatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, TypeCategory.CLASSIFICATION); - AtlasType clsType = typeRegistry.getType(classification.getTypeName()); + AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.CLASSIFICATION); + AtlasType classificationType = typeRegistry.getType(classification.getTypeName()); + AtlasClassification ret = (AtlasClassification)converter.fromV1ToV2(classification, classificationType); - return (AtlasClassification) formatter.convert(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, clsType, classification); + return ret; } public AtlasEntityWithAssociations getAtlasEntity(IReferenceableInstance referenceable) throws AtlasBaseException { - AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, TypeCategory.ENTITY); - AtlasType entityType = typeRegistry.getType(referenceable.getTypeName()); + AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY); + AtlasType entityType = typeRegistry.getType(referenceable.getTypeName()); + AtlasEntityWithAssociations ret = (AtlasEntityWithAssociations)converter.fromV1ToV2(referenceable, entityType); - return (AtlasEntityWithAssociations) entityFormatter.convert(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, entityType, referenceable); + return ret; } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasMapFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasMapFormatConverter.java index 761c8a7..f390e82 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasMapFormatConverter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasMapFormatConverter.java @@ -18,57 +18,83 @@ package org.apache.atlas.web.adapters; +import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.model.TypeCategory; import org.apache.atlas.type.AtlasMapType; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; -import javax.inject.Inject; import java.util.HashMap; import java.util.Map; -public class AtlasMapFormatConverter implements AtlasFormatAdapter { +public class AtlasMapFormatConverter extends AtlasAbstractFormatConverter { - protected AtlasFormatConverters registry; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2); - registry.registerConverter(this, AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1); + public AtlasMapFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.MAP); } @Override - public Map convert(String sourceVersion, String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - Map newMap = new HashMap<>(); - - if ( source != null) { - Map origMap = (Map) source; - for (Object key : origMap.keySet()) { - - - AtlasMapType mapType = (AtlasMapType) type; - AtlasType keyType = mapType.getKeyType(); - AtlasType valueType = mapType.getValueType(); - AtlasFormatAdapter keyConverter = registry.getConverter(sourceVersion, targetVersion, keyType.getTypeCategory()); - Object convertedKey = keyConverter.convert(sourceVersion, targetVersion, keyType, key); - Object val = origMap.get(key); - - if (val != null) { - AtlasFormatAdapter valueConverter = registry.getConverter(sourceVersion, targetVersion, valueType.getTypeCategory()); - newMap.put(convertedKey, valueConverter.convert(sourceVersion, targetVersion, valueType, val)); - } else { - newMap.put(convertedKey, val); + public Map fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + Map ret = null; + + if (v1Obj != null) { + if (v1Obj instanceof Map) { + AtlasMapType mapType = (AtlasMapType)type; + AtlasType keyType = mapType.getKeyType(); + AtlasType valueType = mapType.getValueType(); + AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory()); + AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory()); + Map v1Map = (Map)v1Obj; + + ret = new HashMap<>(); + + for (Object key : v1Map.keySet()) { + Object value = v1Map.get(key); + + Object v2Key = keyConverter.fromV1ToV2(key, keyType); + Object v2Value = valueConverter.fromV1ToV2(value, valueType); + + ret.put(v2Key, v2Value); } + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v1Obj.getClass().getCanonicalName()); } + } - return newMap; + + return ret; } @Override - public TypeCategory getTypeCategory() { - return TypeCategory.MAP; + public Map fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + Map ret = null; + + if (v2Obj != null) { + if (v2Obj instanceof Map) { + AtlasMapType mapType = (AtlasMapType)type; + AtlasType keyType = mapType.getKeyType(); + AtlasType valueType = mapType.getValueType(); + AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory()); + AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory()); + Map v1Map = (Map)v2Obj; + + ret = new HashMap<>(); + + for (Object key : v1Map.keySet()) { + Object value = v1Map.get(key); + + Object v2Key = keyConverter.fromV2ToV1(key, keyType); + Object v2Value = valueConverter.fromV2ToV1(value, valueType); + + ret.put(v2Key, v2Value); + } + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v2Obj.getClass().getCanonicalName()); + } + } + + return ret; } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasPrimitiveFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasPrimitiveFormatConverter.java index 66be58e..382d1ef 100644 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasPrimitiveFormatConverter.java +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasPrimitiveFormatConverter.java @@ -23,29 +23,20 @@ import org.apache.atlas.model.TypeCategory; import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasTypeRegistry; -import javax.inject.Inject; -import java.util.HashMap; -import java.util.Map; -public class AtlasPrimitiveFormatConverter implements AtlasFormatAdapter { - - protected AtlasFormatConverters registry; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2); - registry.registerConverter(this, AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1); +public class AtlasPrimitiveFormatConverter extends AtlasAbstractFormatConverter { + public AtlasPrimitiveFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + super(registry, typeRegistry, TypeCategory.PRIMITIVE); } @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - return type.getNormalizedValue(source); + public Object fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + return type.getNormalizedValue(v1Obj); } @Override - public TypeCategory getTypeCategory() { - return TypeCategory.PRIMITIVE; + public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + return type.getNormalizedValue(v2Obj); } } diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java new file mode 100644 index 0000000..b469f95 --- /dev/null +++ b/webapp/src/main/java/org/apache/atlas/web/adapters/AtlasStructFormatConverter.java @@ -0,0 +1,176 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.atlas.web.adapters; + +import org.apache.atlas.AtlasErrorCode; +import org.apache.atlas.AtlasException; +import org.apache.atlas.exception.AtlasBaseException; +import org.apache.atlas.model.TypeCategory; +import org.apache.atlas.model.instance.AtlasStruct; +import org.apache.atlas.model.typedef.AtlasStructDef; +import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef; +import org.apache.atlas.type.*; +import org.apache.atlas.typesystem.IStruct; +import org.apache.atlas.typesystem.Struct; +import org.apache.commons.collections.MapUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +public class AtlasStructFormatConverter extends AtlasAbstractFormatConverter { + private static final Logger LOG = LoggerFactory.getLogger(AtlasStructFormatConverter.class); + + public static final String ATTRIBUTES_PROPERTY_KEY = "attributes"; + + public AtlasStructFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) { + this(registry, typeRegistry, TypeCategory.STRUCT); + } + + protected AtlasStructFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry, TypeCategory typeCategory) { + super(registry, typeRegistry, typeCategory); + } + + @Override + public AtlasStruct fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException { + AtlasStruct ret = null; + + if (v1Obj != null) { + AtlasStructType structType = (AtlasStructType)type; + + if (v1Obj instanceof Map) { + final Map v1Map = (Map) v1Obj; + final Map v1Attribs = (Map) v1Map.get(ATTRIBUTES_PROPERTY_KEY); + + if (MapUtils.isNotEmpty(v1Attribs)) { + ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs)); + } else { + ret = new AtlasStruct(type.getTypeName()); + } + } else if (v1Obj instanceof IStruct) { + IStruct struct = (IStruct) v1Obj; + Map<String, Object> v1Attribs = null; + + try { + v1Attribs = struct.getValuesMap(); + } catch (AtlasException excp) { + LOG.error("IStruct.getValuesMap() failed", excp); + } + + ret = new AtlasStruct(type.getTypeName(), fromV1ToV2(structType, v1Attribs)); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or IStruct", v1Obj.getClass().getCanonicalName()); + } + } + + return ret; + } + + @Override + public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException { + Struct ret = null; + + if (v2Obj != null) { + AtlasStructType structType = (AtlasStructType)type; + + if (v2Obj instanceof Map) { + final Map v2Map = (Map) v2Obj; + final Map v2Attribs; + + if (v2Map.containsKey(ATTRIBUTES_PROPERTY_KEY)) { + v2Attribs = (Map) v2Map.get(ATTRIBUTES_PROPERTY_KEY); + } else { + v2Attribs = v2Map; + } + + if (MapUtils.isNotEmpty(v2Attribs)) { + ret = new Struct(type.getTypeName(), fromV2ToV1(structType, v2Attribs)); + } else { + ret = new Struct(type.getTypeName()); + } + } else if (v2Obj instanceof AtlasStruct) { + AtlasStruct struct = (AtlasStruct) v2Obj; + + ret = new Struct(type.getTypeName(), fromV2ToV1(structType, struct.getAttributes())); + } else { + throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map or AtlasStruct", v2Obj.getClass().getCanonicalName()); + } + } + + return ret; + } + + protected Map<String, Object> fromV2ToV1(AtlasStructType structType, Map attributes) throws AtlasBaseException { + Map<String, Object> ret = null; + + if (MapUtils.isNotEmpty(attributes)) { + ret = new HashMap<>(); + + for (AtlasStructDef.AtlasAttributeDef attrDef : getAttributeDefs(structType)) { + AtlasType attrType = structType.getAttributeType(attrDef.getName()); + AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); + + Object v2Value = attributes.get(attrDef.getName()); + Object v1Value = attrConverter.fromV2ToV1(v2Value, attrType); + + ret.put(attrDef.getName(), v1Value); + } + } + + return ret; + } + + protected Map<String, Object> fromV1ToV2(AtlasStructType structType, Map attributes) throws AtlasBaseException { + Map<String, Object> ret = null; + + if (MapUtils.isNotEmpty(attributes)) { + ret = new HashMap<>(); + + for (AtlasStructDef.AtlasAttributeDef attrDef : getAttributeDefs(structType)) { + AtlasType attrType = structType.getAttributeType(attrDef.getName()); + AtlasFormatConverter attrConverter = converterRegistry.getConverter(attrType.getTypeCategory()); + + Object v1Value = attributes.get(attrDef.getName()); + Object v2Value = attrConverter.fromV1ToV2(v1Value, attrType); + + ret.put(attrDef.getName(), v2Value); + } + } + + return ret; + } + + private Collection<AtlasAttributeDef> getAttributeDefs(AtlasStructType structType) { + Collection<AtlasAttributeDef> ret = null; + + if (structType.getTypeCategory() == TypeCategory.STRUCT) { + ret = structType.getStructDef().getAttributeDefs(); + } else if (structType.getTypeCategory() == TypeCategory.CLASSIFICATION) { + ret = ((AtlasClassificationType)structType).getAllAttributeDefs().values(); + } else if (structType.getTypeCategory() == TypeCategory.ENTITY) { + ret = ((AtlasEntityType)structType).getAllAttributeDefs().values(); + } else { + ret = Collections.emptyList(); + } + + return ret; + } +} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/ReferenceableToAtlasEntityConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/v1/ReferenceableToAtlasEntityConverter.java deleted file mode 100644 index ba77c67..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/ReferenceableToAtlasEntityConverter.java +++ /dev/null @@ -1,111 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters.v1; - -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasClassification; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasEntityWithAssociations; -import org.apache.atlas.type.AtlasClassificationType; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.typesystem.IReferenceableInstance; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; - -import javax.inject.Inject; -import java.util.ArrayList; -import java.util.List; - -public class ReferenceableToAtlasEntityConverter implements AtlasFormatAdapter { - - protected AtlasTypeRegistry typeRegistry; - protected AtlasFormatConverters registry; - - @Inject - public ReferenceableToAtlasEntityConverter(AtlasTypeRegistry typeRegistry) { - this.typeRegistry = typeRegistry; - } - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2); - } - - @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - AtlasEntityWithAssociations result = null; - if ( source != null) { - if ( isId(source)) { - Id idObj = (Id) source; - result = new AtlasEntityWithAssociations(idObj.getTypeName()); - setId(idObj, result); - } else if (isEntityType(source) ) { - - IReferenceableInstance entity = (IReferenceableInstance) source; - - //Resolve attributes - StructToAtlasStructConverter converter = (StructToAtlasStructConverter) registry.getConverter(sourceVersion, targetVersion, TypeCategory.STRUCT); - result = new AtlasEntityWithAssociations(entity.getTypeName(), converter.convertAttributes((AtlasEntityType) type, entity)); - - //Id - setId(entity, result); - - //Resolve traits - List<AtlasClassification> classifications = new ArrayList<>(); - for (String traitName : entity.getTraits()) { - IStruct trait = entity.getTrait(traitName); - AtlasClassificationType traitType = (AtlasClassificationType) typeRegistry.getType(traitName); - AtlasClassification clsInstance = new AtlasClassification(traitType.getTypeName(), converter.convertAttributes(traitType, trait)); - classifications.add(clsInstance); - } - result.setClassifications(classifications); - } - } - return result; - } - - private boolean isEntityType(Object o) { - if ( o != null && o instanceof IReferenceableInstance) { - return true; - } - return false; - } - - private boolean isId(Object o) { - if ( o != null && o instanceof Id) { - return true; - } - return false; - } - - @Override - public TypeCategory getTypeCategory() { - return TypeCategory.ENTITY; - } - - - private void setId(IReferenceableInstance entity, AtlasEntity result) { - result.setGuid(entity.getId()._getId()); - } -} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/StructToAtlasStructConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/v1/StructToAtlasStructConverter.java deleted file mode 100644 index 15c41c8..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/StructToAtlasStructConverter.java +++ /dev/null @@ -1,114 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters.v1; - -import com.google.inject.Inject; -import com.google.inject.Singleton; -import org.apache.atlas.AtlasException; -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.model.typedef.AtlasStructDef; -import org.apache.atlas.type.AtlasClassificationType; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasStructType; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; -import org.apache.atlas.web.adapters.AtlasInstanceRestAdapters; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -@Singleton -public class StructToAtlasStructConverter implements AtlasFormatAdapter { - - protected AtlasFormatConverters registry; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2); - } - - @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - - if (source != null) { - if (isStructType(source)) { - IStruct entity = (IStruct) source; - //Resolve attributes - StructToAtlasStructConverter converter = (StructToAtlasStructConverter) registry.getConverter(sourceVersion, targetVersion, TypeCategory.STRUCT); - return new AtlasStruct(type.getTypeName(), converter.convertAttributes((AtlasStructType) type, entity)); - } - - } - return null; - } - - private boolean isStructType(Object o) { - if (o != null && o instanceof IStruct) { - return true; - } - return false; - } - - @Override - public TypeCategory getTypeCategory() { - return TypeCategory.STRUCT; - } - - public Map<String, Object> convertAttributes(AtlasStructType structType, Object entity) throws AtlasBaseException { - Collection<AtlasStructDef.AtlasAttributeDef> attributeDefs; - - if (structType.getTypeCategory() == TypeCategory.STRUCT) { - attributeDefs = structType.getStructDef().getAttributeDefs(); - } else if (structType.getTypeCategory() == TypeCategory.CLASSIFICATION) { - attributeDefs = ((AtlasClassificationType)structType).getAllAttributeDefs().values(); - } else if (structType.getTypeCategory() == TypeCategory.ENTITY) { - attributeDefs = ((AtlasEntityType)structType).getAllAttributeDefs().values(); - } else { - attributeDefs = Collections.emptyList(); - } - - Map<String, Object> newAttrMap = new HashMap<>(); - for (AtlasStructDef.AtlasAttributeDef attrDef : attributeDefs) { - AtlasType attrType = structType.getAttributeType(attrDef.getName()); - AtlasFormatAdapter attrConverter = registry.getConverter(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, attrType.getTypeCategory()); - - Object attrVal = null; - if ( AtlasFormatConverters.isMapType(entity)) { - attrVal = ((Map)entity).get(attrDef.getName()); - } else { - try { - attrVal = ((IStruct)entity).get(attrDef.getName()); - } catch (AtlasException e) { - throw AtlasInstanceRestAdapters.toAtlasBaseException(e); - } - } - final Object convertedVal = attrConverter.convert(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, attrType, attrVal); - newAttrMap.put(attrDef.getName(), convertedVal); - } - - return newAttrMap; - } -} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/TraitToAtlasClassificationConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/v1/TraitToAtlasClassificationConverter.java deleted file mode 100644 index 6ac9013..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v1/TraitToAtlasClassificationConverter.java +++ /dev/null @@ -1,64 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters.v1; - -import com.google.inject.Inject; -import com.google.inject.Singleton; -import org.apache.atlas.AtlasException; -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasClassification; -import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.model.typedef.AtlasStructDef; -import org.apache.atlas.type.AtlasClassificationType; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasStructType; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.type.AtlasTypeRegistry; -import org.apache.atlas.typesystem.IStruct; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; -import org.apache.atlas.web.adapters.AtlasInstanceRestAdapters; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -@Singleton -public class TraitToAtlasClassificationConverter extends StructToAtlasStructConverter { - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - super.init(registry); - } - - @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - AtlasStruct struct = (AtlasStruct) super.convert(sourceVersion, targetVersion, type, source); - if ( struct != null) { - return new AtlasClassification(struct.getTypeName(), struct.getAttributes()); - } - return null; - } - - @Override - public TypeCategory getTypeCategory() { - return TypeCategory.CLASSIFICATION; - } -} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasEntityToReferenceableConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasEntityToReferenceableConverter.java deleted file mode 100644 index fab051f..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasEntityToReferenceableConverter.java +++ /dev/null @@ -1,108 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters.v2; - - -import org.apache.atlas.AtlasErrorCode; -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasEntity; -import org.apache.atlas.model.instance.AtlasObjectId; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.typesystem.Referenceable; -import org.apache.atlas.typesystem.persistence.Id; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; -import org.apache.commons.collections.MapUtils; -import org.apache.commons.lang3.StringUtils; - -import javax.inject.Inject; -import java.util.Map; - -public class AtlasEntityToReferenceableConverter implements AtlasFormatAdapter { - - protected AtlasFormatConverters registry; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1); - } - - @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - - if ( source != null) { - //JSOn unmarshalling gives us a Map instead of AtlasObjectId or AtlasEntity - if ( AtlasFormatConverters.isMapType(source)) { - //Could be an entity or an Id - Map srcMap = (Map) source; - String idStr = (String)srcMap.get(AtlasObjectId.KEY_GUID); - String typeName = type.getTypeName(); - - if (StringUtils.isEmpty(idStr)) { - throw new AtlasBaseException(AtlasErrorCode.INSTANCE_GUID_NOT_FOUND); - } - - if (MapUtils.isEmpty((Map)srcMap.get(AtlasStructToStructConverter.ATTRIBUTES_PROPERTY_KEY))) { - //Convert to Id - Id id = new Id(idStr, 0, typeName); - return id; - } else { - final Map attrMap = (Map) srcMap.get(AtlasStructToStructConverter.ATTRIBUTES_PROPERTY_KEY); - //Resolve attributes - AtlasStructToStructConverter converter = (AtlasStructToStructConverter) registry.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.STRUCT); - return new Referenceable(idStr, typeName, converter.convertAttributes((AtlasEntityType)type, attrMap)); - - } - } else { - if ( isEntityType(source) ) { - AtlasEntity entity = (AtlasEntity) source; - String id = entity.getGuid(); - //Resolve attributes - AtlasStructToStructConverter converter = (AtlasStructToStructConverter) registry.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.STRUCT); - return new Referenceable(id, entity.getTypeName(), converter.convertAttributes((AtlasEntityType)type, entity)); - - } else if (isTransientId(source)) { - return new Referenceable((String) source, type.getTypeName(), null); - } - } - } - return null; - } - - private boolean isEntityType(Object o) { - if ( o != null && (o instanceof AtlasEntity)) { - return true; - } - return false; - } - - private boolean isTransientId(Object o) { - if ( o != null && (o instanceof String)) { - return true; - } - return false; - } - - @Override - public TypeCategory getTypeCategory() { - return TypeCategory.ENTITY; - } -} diff --git a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasStructToStructConverter.java b/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasStructToStructConverter.java deleted file mode 100644 index f0b4b12..0000000 --- a/webapp/src/main/java/org/apache/atlas/web/adapters/v2/AtlasStructToStructConverter.java +++ /dev/null @@ -1,124 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.atlas.web.adapters.v2; - -import com.google.inject.Inject; -import com.google.inject.Singleton; -import org.apache.atlas.exception.AtlasBaseException; -import org.apache.atlas.model.TypeCategory; -import org.apache.atlas.model.instance.AtlasStruct; -import org.apache.atlas.model.typedef.AtlasStructDef; -import org.apache.atlas.type.AtlasClassificationType; -import org.apache.atlas.type.AtlasEntityType; -import org.apache.atlas.type.AtlasStructType; -import org.apache.atlas.type.AtlasType; -import org.apache.atlas.typesystem.Struct; -import org.apache.atlas.web.adapters.AtlasFormatAdapter; -import org.apache.atlas.web.adapters.AtlasFormatConverters; - -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -@Singleton -public class AtlasStructToStructConverter implements AtlasFormatAdapter { - - protected AtlasFormatConverters registry; - - public static final String ATTRIBUTES_PROPERTY_KEY = "attributes"; - - @Inject - public void init(AtlasFormatConverters registry) throws AtlasBaseException { - this.registry = registry; - registry.registerConverter(this, AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1); - } - - @Override - public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { - - if (source != null) { - //Json unmarshalling gives us a Map instead of AtlasObjectId or AtlasEntity - if (AtlasFormatConverters.isMapType(source)) { - //Could be an entity or an Id - Map srcMap = (Map) source; - final Map attrMap = (Map) srcMap.get(ATTRIBUTES_PROPERTY_KEY); - - if ( attrMap != null) { - //Resolve attributes - AtlasStructToStructConverter converter = (AtlasStructToStructConverter) registry.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.STRUCT); - return new Struct(type.getTypeName(), converter.convertAttributes((AtlasStructType)type, attrMap)); - } - - } else if (isStructType(source)) { - - AtlasStruct entity = (AtlasStruct) source; - //Resolve attributes - AtlasStructToStructConverter converter = (AtlasStructToStructConverter) registry.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.STRUCT); - return new Struct(type.getTypeName(), converter.convertAttributes((AtlasStructType) type, entity)); - } - } - - return null; - - } - - private boolean isStructType(Object o) { - if (o != null && o instanceof AtlasStruct) { - return true; - } - return false; - } - - @Override - public TypeCategory getTypeCategory() { - return TypeCategory.STRUCT; - } - - public Map<String, Object> convertAttributes(AtlasStructType structType, Object entity) throws AtlasBaseException { - Collection<AtlasStructDef.AtlasAttributeDef> attributeDefs; - - if (structType.getTypeCategory() == TypeCategory.STRUCT) { - attributeDefs = structType.getStructDef().getAttributeDefs(); - } else if (structType.getTypeCategory() == TypeCategory.CLASSIFICATION) { - attributeDefs = ((AtlasClassificationType)structType).getAllAttributeDefs().values(); - } else if (structType.getTypeCategory() == TypeCategory.ENTITY) { - attributeDefs = ((AtlasEntityType)structType).getAllAttributeDefs().values(); - } else { - attributeDefs = Collections.emptyList(); - } - - Map<String, Object> newAttrMap = new HashMap<>(); - for (AtlasStructDef.AtlasAttributeDef attrDef : attributeDefs) { - AtlasType attrType = structType.getAttributeType(attrDef.getName()); - - AtlasFormatAdapter attrConverter = registry.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, attrType.getTypeCategory()); - - Object attrVal = null; - if ( AtlasFormatConverters.isMapType(entity)) { - attrVal = ((Map)entity).get(attrDef.getName()); - } else { - attrVal = ((AtlasStruct)entity).getAttribute(attrDef.getName()); - } - final Object convertedVal = attrConverter.convert(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, attrType, attrVal); - newAttrMap.put(attrDef.getName(), convertedVal); - } - - return newAttrMap; - } -} diff --git a/webapp/src/main/java/org/apache/atlas/web/listeners/GuiceServletConfig.java b/webapp/src/main/java/org/apache/atlas/web/listeners/GuiceServletConfig.java index c245596..3bab650 100755 --- a/webapp/src/main/java/org/apache/atlas/web/listeners/GuiceServletConfig.java +++ b/webapp/src/main/java/org/apache/atlas/web/listeners/GuiceServletConfig.java @@ -33,7 +33,6 @@ import org.apache.atlas.notification.NotificationModule; import org.apache.atlas.repository.graph.AtlasGraphProvider; import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.service.Services; -import org.apache.atlas.web.adapters.AtlasFormatConvertersModule; import org.apache.atlas.web.filters.ActiveServerFilter; import org.apache.atlas.web.filters.AuditFilter; import org.apache.atlas.web.service.ActiveInstanceElectorModule; @@ -74,7 +73,7 @@ public class GuiceServletConfig extends GuiceServletContextListener { LoginProcessor loginProcessor = new LoginProcessor(); loginProcessor.login(); - injector = Guice.createInjector(Stage.PRODUCTION, getRepositoryModule(), new AtlasFormatConvertersModule(), new ActiveInstanceElectorModule(), + injector = Guice.createInjector(Stage.PRODUCTION, getRepositoryModule(), new ActiveInstanceElectorModule(), new NotificationModule(), new ServiceModule(), new JerseyServletModule() { private Configuration appConfiguration = null; diff --git a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java index f8e18bf..265b650 100644 --- a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java +++ b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntitiesREST.java @@ -48,7 +48,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -@Guice(modules = {AtlasFormatConvertersModule.class, RepositoryMetadataModule.class}) +@Guice(modules = {RepositoryMetadataModule.class}) public class TestEntitiesREST { private static final Logger LOG = LoggerFactory.getLogger(TestEntitiesREST.class); diff --git a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java index 6dd21d1..2a75773 100644 --- a/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java +++ b/webapp/src/test/java/org/apache/atlas/web/adapters/TestEntityREST.java @@ -36,14 +36,13 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.AfterClass; import org.testng.annotations.Guice; import org.testng.annotations.Test; -import org.testng.internal.Invoker; import javax.inject.Inject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; -@Guice(modules = {AtlasFormatConvertersModule.class, RepositoryMetadataModule.class}) +@Guice(modules = {RepositoryMetadataModule.class}) public class TestEntityREST { @Inject