Commit d4efc378 by Madhan Neethiraj

ATLAS-1319: fix converters between v1 and v2 to handle struct type attributes

parent f694a910
...@@ -42,6 +42,7 @@ public enum AtlasErrorCode { ...@@ -42,6 +42,7 @@ public enum AtlasErrorCode {
CANNOT_ADD_MANDATORY_ATTRIBUTE(400, "ATLAS40012E", "{0}.{1} : can not add mandatory attribute"), 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"), ATTRIBUTE_DELETION_NOT_SUPPORTED(400, "ATLAS40013E", "{0}.{1} : attribute delete not supported"),
SUPERTYPE_REMOVAL_NOT_SUPPORTED(400, "ATLAS40014E", "superType remove 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_NAME_NOT_FOUND(404, "ATLAS4041E", "Given typename {0} was invalid"),
TYPE_GUID_NOT_FOUND(404, "ATLAS4042E", "Given type guid {0} was invalid"), TYPE_GUID_NOT_FOUND(404, "ATLAS4042E", "Given type guid {0} was invalid"),
......
...@@ -787,7 +787,7 @@ public final class GraphHelper { ...@@ -787,7 +787,7 @@ public final class GraphHelper {
return instanceVertex.getProperty(actualPropertyName, AtlasEdge.class); return instanceVertex.getProperty(actualPropertyName, AtlasEdge.class);
} }
else { else {
return instanceVertex.getProperty(actualPropertyName, String.class).toString(); return instanceVertex.getProperty(actualPropertyName, Object.class).toString();
} }
} }
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * 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 * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
...@@ -15,39 +15,27 @@ ...@@ -15,39 +15,27 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * 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.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.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 abstract class AtlasAbstractFormatConverter implements AtlasFormatConverter {
public class AtlasClassificationToTraitConverter extends AtlasStructToStructConverter { protected final AtlasFormatConverters converterRegistry;
protected final AtlasTypeRegistry typeRegistry;
protected final TypeCategory typeCategory;
@Inject protected AtlasAbstractFormatConverter(AtlasFormatConverters converterRegistry, AtlasTypeRegistry typeRegistry, TypeCategory typeCategory) {
public void init(AtlasFormatConverters registry) throws AtlasBaseException { this.converterRegistry = converterRegistry;
super.init(registry); this.typeRegistry = typeRegistry;
this.typeCategory = typeCategory;
} }
@Override @Override
public TypeCategory getTypeCategory() { public TypeCategory getTypeCategory() {
return TypeCategory.CLASSIFICATION; return typeCategory;
} }
} }
...@@ -18,61 +18,81 @@ ...@@ -18,61 +18,81 @@
package org.apache.atlas.web.adapters; package org.apache.atlas.web.adapters;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasArrayType; import org.apache.atlas.type.AtlasArrayType;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; 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; public AtlasArrayFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) {
super(registry, typeRegistry, TypeCategory.ARRAY);
@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);
} }
@Override @Override
public TypeCategory getTypeCategory() { public Collection fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException {
return TypeCategory.ARRAY; 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 @Override
public Object convert(String sourceVersion, String targetVersion, AtlasType type, final Object source) throws AtlasBaseException { public Collection fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException {
Collection newCollection = null; Collection ret = null;
if ( source != null ) {
if (AtlasFormatConverters.isArrayListType(source.getClass())) { if (v2Obj != null) {
newCollection = new ArrayList(); if (v2Obj instanceof List) {
} else if (AtlasFormatConverters.isSetType(source.getClass())) { ret = new ArrayList();
newCollection = new LinkedHashSet(); } 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; AtlasArrayType arrType = (AtlasArrayType) type;
AtlasType elemType = arrType.getElementType(); AtlasType elemType = arrType.getElementType();
AtlasFormatConverter elemConverter = converterRegistry.getConverter(elemType.getTypeCategory());
Collection v2List = (Collection) v2Obj;
Collection originalList = (Collection) source; for (Object v2Elem : v2List) {
for (Object elem : originalList) { Object convertedVal = elemConverter.fromV2ToV1(v2Elem, elemType);
AtlasFormatAdapter elemConverter = registry.getConverter(sourceVersion, targetVersion, elemType.getTypeCategory());
Object convertedVal = elemConverter.convert(sourceVersion, targetVersion, elemType, elem);
newCollection.add(convertedVal); ret.add(convertedVal);
} }
} }
return newCollection;
}
return ret;
}
} }
/**
* 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;
}
}
/**
* 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;
}
}
...@@ -23,18 +23,20 @@ import org.apache.atlas.model.TypeCategory; ...@@ -23,18 +23,20 @@ import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; 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 @Override
public void init(AtlasFormatConverters registry) throws AtlasBaseException { public Object fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException {
super.init(registry); return type.getNormalizedValue(v1Obj);
} }
@Override @Override
public TypeCategory getTypeCategory() { public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException {
return TypeCategory.ENUM; return type.getNormalizedValue(v2Obj);
} }
} }
...@@ -22,10 +22,10 @@ import org.apache.atlas.exception.AtlasBaseException; ...@@ -22,10 +22,10 @@ import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasType; 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(); TypeCategory getTypeCategory();
} }
...@@ -22,67 +22,39 @@ import com.google.inject.Singleton; ...@@ -22,67 +22,39 @@ import com.google.inject.Singleton;
import org.apache.atlas.AtlasErrorCode; import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasTypeRegistry;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set;
@Singleton @Singleton
public class AtlasFormatConverters { public class AtlasFormatConverters {
public static String VERSION_V1 = "v1"; private final Map<TypeCategory, AtlasFormatConverter> registry = new HashMap<>();
public static String VERSION_V2 = "v2";
@Inject
private Map<String, AtlasFormatAdapter> registry = new HashMap<>(); public AtlasFormatConverters(AtlasTypeRegistry typeRegistry) {
registerConverter(new AtlasPrimitiveFormatConverter(this, typeRegistry));
public void registerConverter(AtlasFormatAdapter adapter, String sourceVersion, String targetVersion) { registerConverter(new AtlasEnumFormatConverter(this, typeRegistry));
registry.put(getKey(sourceVersion, targetVersion, adapter.getTypeCategory()), adapter); registerConverter(new AtlasStructFormatConverter(this, typeRegistry));
} registerConverter(new AtlasClassificationFormatConverter(this, typeRegistry));
registerConverter(new AtlasEntityFormatConverter(this, typeRegistry));
public AtlasFormatAdapter getConverter(String sourceVersion, String targetVersion, TypeCategory typeCategory) throws AtlasBaseException { registerConverter(new AtlasArrayFormatConverter(this, typeRegistry));
if (registry.containsKey(getKey(sourceVersion, targetVersion, typeCategory))) { registerConverter(new AtlasMapFormatConverter(this, typeRegistry));
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);
} }
public static boolean isSetType(Class c) { private void registerConverter(AtlasFormatConverter converter) {
return Set.class.isAssignableFrom(c); registry.put(converter.getTypeCategory(), converter);
} }
public static boolean isPrimitiveType(final Class c) { public AtlasFormatConverter getConverter(TypeCategory typeCategory) throws AtlasBaseException {
if (c != null) { AtlasFormatConverter ret = registry.get(typeCategory);
if (Number.class.isAssignableFrom(c)) {
return true;
}
if (String.class.isAssignableFrom(c)) { if (ret == null) {
return true; throw new AtlasBaseException(AtlasErrorCode.INTERNAL_ERROR,
} "Could not find the converter for this type " + typeCategory);
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());
} }
return false;
}
String getKey(String sourceVersion, String targetVersion, TypeCategory typeCategory) { return ret;
return sourceVersion + "-to-" + targetVersion + "-" + typeCategory.name();
} }
} }
/**
* 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
...@@ -72,10 +72,8 @@ public class AtlasInstanceRestAdapters { ...@@ -72,10 +72,8 @@ public class AtlasInstanceRestAdapters {
} }
public ITypedReferenceableInstance getITypedReferenceable(AtlasEntity entity) throws AtlasBaseException { public ITypedReferenceableInstance getITypedReferenceable(AtlasEntity entity) throws AtlasBaseException {
AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.ENTITY); Referenceable ref = getReferenceable(entity);
AtlasType entityType = typeRegistry.getType(entity.getTypeName());
Referenceable ref = (Referenceable) entityFormatter.convert(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, entityType, entity);
try { try {
return metadataService.getTypedReferenceableInstance(ref); return metadataService.getTypedReferenceableInstance(ref);
} catch (AtlasException e) { } catch (AtlasException e) {
...@@ -85,19 +83,20 @@ public class AtlasInstanceRestAdapters { ...@@ -85,19 +83,20 @@ public class AtlasInstanceRestAdapters {
} }
public Referenceable getReferenceable(AtlasEntity entity) throws AtlasBaseException { public Referenceable getReferenceable(AtlasEntity entity) throws AtlasBaseException {
AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.ENTITY); AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY);
AtlasType entityType = typeRegistry.getType(entity.getTypeName()); 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 { public ITypedStruct getTrait(AtlasClassification classification) throws AtlasBaseException {
AtlasFormatAdapter formatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V2, AtlasFormatConverters.VERSION_V1, TypeCategory.CLASSIFICATION); AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.CLASSIFICATION);
AtlasType clsType = typeRegistry.getType(classification.getTypeName()); 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 { try {
return metadataService.createTraitInstance(ref); return metadataService.createTraitInstance(trait);
} catch (AtlasException e) { } catch (AtlasException e) {
LOG.error("Exception while getting a typed reference for the entity ", e); LOG.error("Exception while getting a typed reference for the entity ", e);
throw toAtlasBaseException(e); throw toAtlasBaseException(e);
...@@ -105,17 +104,19 @@ public class AtlasInstanceRestAdapters { ...@@ -105,17 +104,19 @@ public class AtlasInstanceRestAdapters {
} }
public AtlasClassification getClassification(IStruct classification) throws AtlasBaseException { public AtlasClassification getClassification(IStruct classification) throws AtlasBaseException {
AtlasFormatAdapter formatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, TypeCategory.CLASSIFICATION); AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.CLASSIFICATION);
AtlasType clsType = typeRegistry.getType(classification.getTypeName()); 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 { public AtlasEntityWithAssociations getAtlasEntity(IReferenceableInstance referenceable) throws AtlasBaseException {
AtlasFormatAdapter entityFormatter = instanceFormatters.getConverter(AtlasFormatConverters.VERSION_V1, AtlasFormatConverters.VERSION_V2, TypeCategory.ENTITY); AtlasFormatConverter converter = instanceFormatters.getConverter(TypeCategory.ENTITY);
AtlasType entityType = typeRegistry.getType(referenceable.getTypeName()); 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;
} }
......
...@@ -18,57 +18,83 @@ ...@@ -18,57 +18,83 @@
package org.apache.atlas.web.adapters; package org.apache.atlas.web.adapters;
import org.apache.atlas.AtlasErrorCode;
import org.apache.atlas.exception.AtlasBaseException; import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory; import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasMapType; import org.apache.atlas.type.AtlasMapType;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import javax.inject.Inject;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
public class AtlasMapFormatConverter implements AtlasFormatAdapter { public class AtlasMapFormatConverter extends AtlasAbstractFormatConverter {
protected AtlasFormatConverters registry; public AtlasMapFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) {
super(registry, typeRegistry, TypeCategory.MAP);
@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);
} }
@Override @Override
public Map convert(String sourceVersion, String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { public Map fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException {
Map newMap = new HashMap<>(); Map ret = null;
if ( source != null) { if (v1Obj != null) {
Map origMap = (Map) source; if (v1Obj instanceof Map) {
for (Object key : origMap.keySet()) { AtlasMapType mapType = (AtlasMapType)type;
AtlasType keyType = mapType.getKeyType();
AtlasType valueType = mapType.getValueType();
AtlasMapType mapType = (AtlasMapType) type; AtlasFormatConverter keyConverter = converterRegistry.getConverter(keyType.getTypeCategory());
AtlasType keyType = mapType.getKeyType(); AtlasFormatConverter valueConverter = converterRegistry.getConverter(valueType.getTypeCategory());
AtlasType valueType = mapType.getValueType(); Map v1Map = (Map)v1Obj;
AtlasFormatAdapter keyConverter = registry.getConverter(sourceVersion, targetVersion, keyType.getTypeCategory());
Object convertedKey = keyConverter.convert(sourceVersion, targetVersion, keyType, key); ret = new HashMap<>();
Object val = origMap.get(key);
for (Object key : v1Map.keySet()) {
if (val != null) { Object value = v1Map.get(key);
AtlasFormatAdapter valueConverter = registry.getConverter(sourceVersion, targetVersion, valueType.getTypeCategory());
newMap.put(convertedKey, valueConverter.convert(sourceVersion, targetVersion, valueType, val)); Object v2Key = keyConverter.fromV1ToV2(key, keyType);
} else { Object v2Value = valueConverter.fromV1ToV2(value, valueType);
newMap.put(convertedKey, val);
ret.put(v2Key, v2Value);
} }
} else {
throw new AtlasBaseException(AtlasErrorCode.UNEXPECTED_TYPE, "Map", v1Obj.getClass().getCanonicalName());
} }
} }
return newMap;
return ret;
} }
@Override @Override
public TypeCategory getTypeCategory() { public Map fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException {
return TypeCategory.MAP; 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;
} }
} }
...@@ -23,29 +23,20 @@ import org.apache.atlas.model.TypeCategory; ...@@ -23,29 +23,20 @@ import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.type.AtlasType; import org.apache.atlas.type.AtlasType;
import org.apache.atlas.type.AtlasTypeRegistry; import org.apache.atlas.type.AtlasTypeRegistry;
import javax.inject.Inject;
import java.util.HashMap;
import java.util.Map;
public class AtlasPrimitiveFormatConverter implements AtlasFormatAdapter { public class AtlasPrimitiveFormatConverter extends AtlasAbstractFormatConverter {
public AtlasPrimitiveFormatConverter(AtlasFormatConverters registry, AtlasTypeRegistry typeRegistry) {
protected AtlasFormatConverters registry; super(registry, typeRegistry, TypeCategory.PRIMITIVE);
@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);
} }
@Override @Override
public Object convert(final String sourceVersion, final String targetVersion, final AtlasType type, final Object source) throws AtlasBaseException { public Object fromV1ToV2(Object v1Obj, AtlasType type) throws AtlasBaseException {
return type.getNormalizedValue(source); return type.getNormalizedValue(v1Obj);
} }
@Override @Override
public TypeCategory getTypeCategory() { public Object fromV2ToV1(Object v2Obj, AtlasType type) throws AtlasBaseException {
return TypeCategory.PRIMITIVE; return type.getNormalizedValue(v2Obj);
} }
} }
/**
* 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;
}
}
/**
* 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());
}
}
/**
* 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;
}
}
/**
* 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;
}
}
/**
* 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;
}
}
/**
* 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;
}
}
...@@ -33,7 +33,6 @@ import org.apache.atlas.notification.NotificationModule; ...@@ -33,7 +33,6 @@ import org.apache.atlas.notification.NotificationModule;
import org.apache.atlas.repository.graph.AtlasGraphProvider; import org.apache.atlas.repository.graph.AtlasGraphProvider;
import org.apache.atlas.repository.graphdb.AtlasGraph; import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.service.Services; 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.ActiveServerFilter;
import org.apache.atlas.web.filters.AuditFilter; import org.apache.atlas.web.filters.AuditFilter;
import org.apache.atlas.web.service.ActiveInstanceElectorModule; import org.apache.atlas.web.service.ActiveInstanceElectorModule;
...@@ -74,7 +73,7 @@ public class GuiceServletConfig extends GuiceServletContextListener { ...@@ -74,7 +73,7 @@ public class GuiceServletConfig extends GuiceServletContextListener {
LoginProcessor loginProcessor = new LoginProcessor(); LoginProcessor loginProcessor = new LoginProcessor();
loginProcessor.login(); 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() { new NotificationModule(), new ServiceModule(), new JerseyServletModule() {
private Configuration appConfiguration = null; private Configuration appConfiguration = null;
......
...@@ -48,7 +48,7 @@ import java.util.ArrayList; ...@@ -48,7 +48,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Guice(modules = {AtlasFormatConvertersModule.class, RepositoryMetadataModule.class}) @Guice(modules = {RepositoryMetadataModule.class})
public class TestEntitiesREST { public class TestEntitiesREST {
private static final Logger LOG = LoggerFactory.getLogger(TestEntitiesREST.class); private static final Logger LOG = LoggerFactory.getLogger(TestEntitiesREST.class);
......
...@@ -36,14 +36,13 @@ import org.testng.annotations.BeforeClass; ...@@ -36,14 +36,13 @@ import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
import org.testng.annotations.Guice; import org.testng.annotations.Guice;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.testng.internal.Invoker;
import javax.inject.Inject; import javax.inject.Inject;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@Guice(modules = {AtlasFormatConvertersModule.class, RepositoryMetadataModule.class}) @Guice(modules = {RepositoryMetadataModule.class})
public class TestEntityREST { public class TestEntityREST {
@Inject @Inject
......
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