use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.
the class EntityResource method getTraitDefinitionsForEntity.
/**
* Fetches the trait definitions of all the traits associated to the given entity
* @param guid globally unique identifier for the entity
*/
@GET
@Path("{guid}/traitDefinitions")
@Produces(Servlets.JSON_MEDIA_TYPE)
public Response getTraitDefinitionsForEntity(@PathParam("guid") String guid) {
if (LOG.isDebugEnabled()) {
LOG.debug("==> EntityResource.getTraitDefinitionsForEntity({})", guid);
}
AtlasPerfTracer perf = null;
try {
if (AtlasPerfTracer.isPerfTraceEnabled(PERF_LOG)) {
perf = AtlasPerfTracer.getPerfTracer(PERF_LOG, "EntityResource.getTraitDefinitionsForEntity(" + guid + ")");
}
if (LOG.isDebugEnabled()) {
LOG.debug("Fetching all trait definitions for entity={}", guid);
}
final List<AtlasClassification> classifications = entitiesStore.getClassifications(guid);
JSONArray traits = new JSONArray();
for (AtlasClassification classification : classifications) {
IStruct trait = restAdapters.getTrait(classification);
traits.put(new JSONObject(InstanceSerialization.toJson(trait, true)));
}
JSONObject response = new JSONObject();
response.put(AtlasClient.REQUEST_ID, Servlets.getRequestId());
response.put(AtlasClient.RESULTS, traits);
response.put(AtlasClient.COUNT, traits.length());
return Response.ok(response).build();
} catch (AtlasBaseException e) {
LOG.error("Unable to get trait definition for entity {}", guid, e);
throw toWebApplicationException(e);
} catch (IllegalArgumentException e) {
LOG.error("Unable to get trait definition for entity {}", guid, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (WebApplicationException e) {
LOG.error("Unable to get trait definitions for entity {}", guid, e);
throw e;
} catch (Throwable e) {
LOG.error("Unable to get trait definitions for entity {}", guid, e);
throw new WebApplicationException(Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} finally {
AtlasPerfTracer.log(perf);
if (LOG.isDebugEnabled()) {
LOG.debug("<== EntityResource.getTraitDefinitionsForEntity({})", guid);
}
}
}
use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.
the class TypeInheritanceTest method testDiamondInheritance.
/*
* Type Hierarchy is:
* A(a,b,c,d)
* B(b) extends A
* C(c) extends A
* D(d) extends B,C
*
* - There are a total of 11 fields in an instance of D
* - an attribute that is hidden by a SubType can referenced by prefixing it with the
* complete Path.
* For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute
* in B.
* So it is availabel by the name 'A.B.D.b'
*
* - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D
* .b' attribute
* available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute
* available as 'b'.
*/
@Test
public void testDiamondInheritance() throws AtlasException {
HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
defineTraits(A, B, C, D);
TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
Struct s1 = new Struct("D");
s1.set("d", 1);
s1.set("c", 1);
s1.set("b", true);
s1.set("a", 1);
s1.set("A.B.D.b", true);
s1.set("A.B.D.c", 2);
s1.set("A.B.D.d", 2);
s1.set("A.C.D.a", 3);
s1.set("A.C.D.b", false);
s1.set("A.C.D.c", 3);
s1.set("A.C.D.d", 3);
ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
/*
* cast to B and set the 'b' attribute on A.
*/
TraitType BType = getTypeSystem().getDataType(TraitType.class, "B");
IStruct s2 = DType.castAs(ts, "B");
s2.set("A.B.b", false);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \tfalse\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
/*
* cast again to A and set the 'b' attribute on A.
*/
IStruct s3 = BType.castAs(s2, "A");
s3.set("b", true);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.
the class TraitTest method test1.
/*
* Type Hierarchy is:
* A(a,b,c,d)
* B(b) extends A
* C(c) extends A
* D(d) extends B,C
*
* - There are a total of 11 fields in an instance of D
* - an attribute that is hidden by a SubType can referenced by prefixing it with the
* complete Path.
* For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute
* in B.
* So it is available by the name 'A.B.D.b'
*
* - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D
* .b' attribute
* available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute
* available as 'b'.
*/
@Test
public void test1() throws AtlasException {
HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
defineTraits(A, B, C, D);
TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
// for(String aName : DType.fieldMapping().fields.keySet()) {
// System.out.println(String.format("nameToQualifiedName.put(\"%s\", \"%s\");", aName, DType
// .getQualifiedName(aName)));
// }
Map<String, String> nameToQualifiedName = new HashMap();
{
nameToQualifiedName.put("d", "D.d");
nameToQualifiedName.put("b", "B.b");
nameToQualifiedName.put("c", "C.c");
nameToQualifiedName.put("a", "A.a");
nameToQualifiedName.put("A.B.D.b", "A.B.D.b");
nameToQualifiedName.put("A.B.D.c", "A.B.D.c");
nameToQualifiedName.put("A.B.D.d", "A.B.D.d");
nameToQualifiedName.put("A.C.D.a", "A.C.D.a");
nameToQualifiedName.put("A.C.D.b", "A.C.D.b");
nameToQualifiedName.put("A.C.D.c", "A.C.D.c");
nameToQualifiedName.put("A.C.D.d", "A.C.D.d");
}
Struct s1 = new Struct("D");
s1.set("d", 1);
s1.set("c", 1);
s1.set("b", true);
s1.set("a", 1);
s1.set("A.B.D.b", true);
s1.set("A.B.D.c", 2);
s1.set("A.B.D.d", 2);
s1.set("A.C.D.a", 3);
s1.set("A.C.D.b", false);
s1.set("A.C.D.c", 3);
s1.set("A.C.D.d", 3);
ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
/*
* cast to B and set the 'b' attribute on A.
*/
TraitType BType = getTypeSystem().getDataType(TraitType.class, "B");
IStruct s2 = DType.castAs(ts, "B");
s2.set("A.B.b", false);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \tfalse\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
/*
* cast again to A and set the 'b' attribute on A.
*/
TraitType AType = getTypeSystem().getDataType(TraitType.class, "A");
IStruct s3 = BType.castAs(s2, "A");
s3.set("b", true);
Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.
the class EntityNotificationImpl method getAllTraits.
// ----- helper methods ----------------------------------------------------
private static List<IStruct> getAllTraits(IReferenceableInstance entityDefinition, TypeSystem typeSystem) throws AtlasException {
List<IStruct> traitInfo = new LinkedList<>();
for (String traitName : entityDefinition.getTraits()) {
IStruct trait = entityDefinition.getTrait(traitName);
String typeName = trait.getTypeName();
Map<String, Object> valuesMap = trait.getValuesMap();
traitInfo.add(new Struct(typeName, valuesMap));
traitInfo.addAll(getSuperTraits(typeName, valuesMap, typeSystem));
}
return traitInfo;
}
use of org.apache.atlas.typesystem.IStruct in project incubator-atlas by apache.
the class EntityNotificationImpl method getSuperTraits.
private static List<IStruct> getSuperTraits(String typeName, Map<String, Object> values, TypeSystem typeSystem) throws AtlasException {
List<IStruct> superTypes = new LinkedList<>();
TraitType traitDef = typeSystem.getDataType(TraitType.class, typeName);
Set<String> superTypeNames = traitDef.getAllSuperTypeNames();
for (String superTypeName : superTypeNames) {
TraitType superTraitDef = typeSystem.getDataType(TraitType.class, superTypeName);
Map<String, Object> superTypeValues = new HashMap<>();
FieldMapping fieldMapping = superTraitDef.fieldMapping();
if (fieldMapping != null) {
Set<String> superTypeAttributeNames = fieldMapping.fields.keySet();
for (String superTypeAttributeName : superTypeAttributeNames) {
if (values.containsKey(superTypeAttributeName)) {
superTypeValues.put(superTypeAttributeName, values.get(superTypeAttributeName));
}
}
}
IStruct superTrait = new Struct(superTypeName, superTypeValues);
superTypes.add(superTrait);
superTypes.addAll(getSuperTraits(superTypeName, values, typeSystem));
}
return superTypes;
}
Aggregations