Commit 65b613b8 by Venkatesh Seetharam

BUG-32815 BUG-32816 BUG-32827 Entity Trait Management Backend API. Contributed…

BUG-32815 BUG-32816 BUG-32827 Entity Trait Management Backend API. Contributed by Venkatesh Seetharam
parent ca9fd2f5
...@@ -905,11 +905,11 @@ ...@@ -905,11 +905,11 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> <artifactId>maven-surefire-plugin</artifactId>
<version>2.7.2</version> <version>2.7.2</version>
<!-- <!--
<configuration> <configuration>
<skipTests>true</skipTests> <skipTests>true</skipTests>
</configuration> </configuration>
--> -->
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.maven.surefire</groupId> <groupId>org.apache.maven.surefire</groupId>
......
...@@ -130,11 +130,10 @@ public interface MetadataRepository { ...@@ -130,11 +130,10 @@ public interface MetadataRepository {
* Adds a new trait to an existing entity represented by a guid. * Adds a new trait to an existing entity represented by a guid.
* *
* @param guid globally unique identifier for the entity * @param guid globally unique identifier for the entity
* @param traitName trait name for the instance that needs to be added to entity
* @param traitInstance trait instance that needs to be added to entity * @param traitInstance trait instance that needs to be added to entity
* @throws RepositoryException * @throws RepositoryException
*/ */
void addTrait(String guid, String traitName, void addTrait(String guid,
ITypedStruct traitInstance) throws RepositoryException; ITypedStruct traitInstance) throws RepositoryException;
/** /**
......
...@@ -207,9 +207,10 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -207,9 +207,10 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
* @throws RepositoryException * @throws RepositoryException
*/ */
@Override @Override
public void addTrait(String guid, String traitName, public void addTrait(String guid,
ITypedStruct traitInstance) throws RepositoryException { ITypedStruct traitInstance) throws RepositoryException {
Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null"); Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null");
final String traitName = traitInstance.getTypeName();
LOG.info("Adding a new trait={} for entity={}", traitName, guid); LOG.info("Adding a new trait={} for entity={}", traitName, guid);
try { try {
...@@ -219,7 +220,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -219,7 +220,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
// add the trait instance as a new vertex // add the trait instance as a new vertex
final String typeName = getTypeName(instanceVertex); final String typeName = getTypeName(instanceVertex);
instanceToGraphMapper.mapTraitInstanceToVertex( instanceToGraphMapper.mapTraitInstanceToVertex(
traitName, traitInstance, getIdFromVertex(typeName, instanceVertex), traitInstance, getIdFromVertex(typeName, instanceVertex),
typeName, instanceVertex, Collections.<Id, Vertex>emptyMap()); typeName, instanceVertex, Collections.<Id, Vertex>emptyMap());
// update the traits in entity once adding trait instance is successful // update the traits in entity once adding trait instance is successful
...@@ -446,7 +447,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -446,7 +447,7 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
ITypedStruct traitInstance = (ITypedStruct) typedInstance.getTrait(traitName); ITypedStruct traitInstance = (ITypedStruct) typedInstance.getTrait(traitName);
// add the attributes for the trait instance // add the attributes for the trait instance
mapTraitInstanceToVertex(traitName, traitInstance, typedInstance, mapTraitInstanceToVertex(traitInstance, typedInstance,
instanceVertex, entityProcessor.idToVertexMap); instanceVertex, entityProcessor.idToVertexMap);
} }
...@@ -655,22 +656,23 @@ public class GraphBackedMetadataRepository implements MetadataRepository { ...@@ -655,22 +656,23 @@ public class GraphBackedMetadataRepository implements MetadataRepository {
return structInstanceVertex; return structInstanceVertex;
} }
private void mapTraitInstanceToVertex(String traitName, ITypedStruct traitInstance, private void mapTraitInstanceToVertex(ITypedStruct traitInstance,
ITypedReferenceableInstance typedInstance, ITypedReferenceableInstance typedInstance,
Vertex parentInstanceVertex, Vertex parentInstanceVertex,
Map<Id, Vertex> idToVertexMap) Map<Id, Vertex> idToVertexMap)
throws MetadataException { throws MetadataException {
// add a new vertex for the struct or trait instance // add a new vertex for the struct or trait instance
mapTraitInstanceToVertex(traitName, traitInstance, typedInstance.getId(), mapTraitInstanceToVertex(traitInstance, typedInstance.getId(),
typedInstance.getTypeName(), parentInstanceVertex, idToVertexMap); typedInstance.getTypeName(), parentInstanceVertex, idToVertexMap);
} }
private void mapTraitInstanceToVertex(String traitName, ITypedStruct traitInstance, private void mapTraitInstanceToVertex(ITypedStruct traitInstance,
Id typedInstanceId, String typedInstanceTypeName, Id typedInstanceId, String typedInstanceTypeName,
Vertex parentInstanceVertex, Vertex parentInstanceVertex,
Map<Id, Vertex> idToVertexMap) Map<Id, Vertex> idToVertexMap)
throws MetadataException { throws MetadataException {
// add a new vertex for the struct or trait instance // add a new vertex for the struct or trait instance
final String traitName = traitInstance.getTypeName();
Vertex traitInstanceVertex = GraphHelper.createVertexWithoutIdentity( Vertex traitInstanceVertex = GraphHelper.createVertexWithoutIdentity(
graphService.getBlueprintsGraph(), traitInstance.getTypeName(), typedInstanceId); graphService.getBlueprintsGraph(), traitInstance.getTypeName(), typedInstanceId);
LOG.debug("created vertex {} for trait {}", traitInstanceVertex, traitName); LOG.debug("created vertex {} for trait {}", traitInstanceVertex, traitName);
......
...@@ -34,7 +34,6 @@ import org.apache.hadoop.metadata.typesystem.types.IDataType; ...@@ -34,7 +34,6 @@ import org.apache.hadoop.metadata.typesystem.types.IDataType;
import org.apache.hadoop.metadata.typesystem.types.TypeSystem; import org.apache.hadoop.metadata.typesystem.types.TypeSystem;
import org.codehaus.jettison.json.JSONException; import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -44,6 +43,10 @@ import java.util.List; ...@@ -44,6 +43,10 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
/**
* Simple wrapper over TypeSystem and MetadataRepository services with hooks
* for listening to changes to the repository.
*/
public class DefaultMetadataService implements MetadataService { public class DefaultMetadataService implements MetadataService {
private static final Logger LOG = private static final Logger LOG =
...@@ -212,25 +215,37 @@ public class DefaultMetadataService implements MetadataService { ...@@ -212,25 +215,37 @@ public class DefaultMetadataService implements MetadataService {
* Adds a new trait to an existing entity represented by a guid. * Adds a new trait to an existing entity represented by a guid.
* *
* @param guid globally unique identifier for the entity * @param guid globally unique identifier for the entity
* @param traitName trait name for the instance that needs to be added to entity * @param traitInstanceDefinition trait instance json that needs to be added to entity
* @param traitInstance trait instance that needs to be added to entity
* @throws MetadataException * @throws MetadataException
*/ */
@Override @Override
public void addTrait(String guid, String traitName, public void addTrait(String guid,
ITypedStruct traitInstance) throws MetadataException { String traitInstanceDefinition) throws MetadataException {
Preconditions.checkNotNull(guid, "entity GUID cannot be null"); Preconditions.checkNotNull(guid, "entity GUID cannot be null");
Preconditions.checkNotNull(traitName, "Trait name cannot be null"); Preconditions.checkNotNull(traitInstanceDefinition, "Trait instance cannot be null");
Preconditions.checkNotNull(traitInstance, "Trait instance cannot be null");
ITypedStruct traitInstance = deserializeTraitInstance(traitInstanceDefinition);
final String traitName = traitInstance.getTypeName();
// ensure trait type is already registered with the TS // ensure trait type is already registered with the TS
Preconditions.checkArgument(!typeSystem.isRegistered(traitName), Preconditions.checkArgument(typeSystem.isRegistered(traitName),
"trait=%s should be defined in type system before it can be added", traitName); "trait=%s should be defined in type system before it can be added", traitName);
repository.addTrait(guid, traitName, traitInstance); repository.addTrait(guid, traitInstance);
onTraitAddedToEntity(guid, traitName); onTraitAddedToEntity(guid, traitName);
} }
private ITypedStruct deserializeTraitInstance(String traitInstanceDefinition)
throws MetadataException {
try {
return (ITypedStruct) Serialization$.MODULE$.traitFromJson(traitInstanceDefinition);
} catch (Exception e) {
throw new MetadataException("Error deserializing trait instance");
}
}
/** /**
* Deletes a given trait from an existing entity represented by a guid. * Deletes a given trait from an existing entity represented by a guid.
* *
...@@ -245,11 +260,12 @@ public class DefaultMetadataService implements MetadataService { ...@@ -245,11 +260,12 @@ public class DefaultMetadataService implements MetadataService {
Preconditions.checkNotNull(traitNameToBeDeleted, "Trait name cannot be null"); Preconditions.checkNotNull(traitNameToBeDeleted, "Trait name cannot be null");
// ensure trait type is already registered with the TS // ensure trait type is already registered with the TS
Preconditions.checkArgument(!typeSystem.isRegistered(traitNameToBeDeleted), Preconditions.checkArgument(typeSystem.isRegistered(traitNameToBeDeleted),
"trait=%s should be defined in type system before it can be deleted", "trait=%s should be defined in type system before it can be deleted",
traitNameToBeDeleted); traitNameToBeDeleted);
repository.deleteTrait(guid, traitNameToBeDeleted); repository.deleteTrait(guid, traitNameToBeDeleted);
onTraitDeletedFromEntity(guid, traitNameToBeDeleted); onTraitDeletedFromEntity(guid, traitNameToBeDeleted);
} }
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
package org.apache.hadoop.metadata.services; package org.apache.hadoop.metadata.services;
import org.apache.hadoop.metadata.MetadataException; import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.typesystem.ITypedStruct;
import org.codehaus.jettison.json.JSONObject; import org.codehaus.jettison.json.JSONObject;
import java.util.List; import java.util.List;
...@@ -94,12 +93,11 @@ public interface MetadataService { ...@@ -94,12 +93,11 @@ public interface MetadataService {
* Adds a new trait to an existing entity represented by a guid. * Adds a new trait to an existing entity represented by a guid.
* *
* @param guid globally unique identifier for the entity * @param guid globally unique identifier for the entity
* @param traitName trait name for the instance that needs to be added to entity * @param traitInstanceDefinition trait instance that needs to be added to entity
* @param traitInstance trait instance that needs to be added to entity
* @throws MetadataException * @throws MetadataException
*/ */
void addTrait(String guid, String traitName, void addTrait(String guid,
ITypedStruct traitInstance) throws MetadataException; String traitInstanceDefinition) throws MetadataException;
/** /**
* Deletes a given trait from an existing entity represented by a guid. * Deletes a given trait from an existing entity represented by a guid.
......
...@@ -231,7 +231,7 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -231,7 +231,7 @@ public class GraphBackedMetadataRepositoryTest {
TraitType traitType = typeSystem.defineTraitType(piiTrait); TraitType traitType = typeSystem.defineTraitType(piiTrait);
ITypedStruct traitInstance = traitType.createInstance(); ITypedStruct traitInstance = traitType.createInstance();
repositoryService.addTrait(aGUID, PII, traitInstance); repositoryService.addTrait(aGUID, traitInstance);
// refresh trait names // refresh trait names
traitNames = repositoryService.getTraitNames(aGUID); traitNames = repositoryService.getTraitNames(aGUID);
...@@ -242,7 +242,7 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -242,7 +242,7 @@ public class GraphBackedMetadataRepositoryTest {
@Test (expectedExceptions = NullPointerException.class) @Test (expectedExceptions = NullPointerException.class)
public void testAddTraitWithNullInstance() throws Exception { public void testAddTraitWithNullInstance() throws Exception {
repositoryService.addTrait(getGUID(), PII, null); repositoryService.addTrait(getGUID(), null);
Assert.fail(); Assert.fail();
} }
...@@ -251,7 +251,7 @@ public class GraphBackedMetadataRepositoryTest { ...@@ -251,7 +251,7 @@ public class GraphBackedMetadataRepositoryTest {
TraitType traitType = typeSystem.getDataType(TraitType.class, PII); TraitType traitType = typeSystem.getDataType(TraitType.class, PII);
ITypedStruct traitInstance = traitType.createInstance(); ITypedStruct traitInstance = traitType.createInstance();
repositoryService.addTrait(UUID.randomUUID().toString(), PII, traitInstance); repositoryService.addTrait(UUID.randomUUID().toString(), traitInstance);
Assert.fail(); Assert.fail();
} }
......
...@@ -48,10 +48,8 @@ public class LoggingExceptionMapper<E extends Throwable> implements ExceptionMap ...@@ -48,10 +48,8 @@ public class LoggingExceptionMapper<E extends Throwable> implements ExceptionMap
@SuppressWarnings("UnusedParameters") @SuppressWarnings("UnusedParameters")
protected String formatErrorMessage(long id, E exception) { protected String formatErrorMessage(long id, E exception) {
return String return String.format(
.format("There was an error processing your request. It has been logged (ID " + "There was an error processing your request. It has been logged (ID %016x).", id);
"%016x).",
id);
} }
protected void logException(long id, E exception) { protected void logException(long id, E exception) {
......
...@@ -35,6 +35,7 @@ import javax.ws.rs.Consumes; ...@@ -35,6 +35,7 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
...@@ -43,6 +44,7 @@ import javax.ws.rs.WebApplicationException; ...@@ -43,6 +44,7 @@ import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context; import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.List; import java.util.List;
/** /**
...@@ -57,6 +59,9 @@ public class EntityResource { ...@@ -57,6 +59,9 @@ public class EntityResource {
private static final Logger LOG = LoggerFactory.getLogger(EntityResource.class); private static final Logger LOG = LoggerFactory.getLogger(EntityResource.class);
private static final String GUID = "GUID";
private static final String TRAIT_NAME = "traitName";
private final MetadataService metadataService; private final MetadataService metadataService;
/** /**
...@@ -87,14 +92,18 @@ public class EntityResource { ...@@ -87,14 +92,18 @@ public class EntityResource {
final String guid = metadataService.createEntity(typeName, entity); final String guid = metadataService.createEntity(typeName, entity);
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("GUID", guid); response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put("requestId", Thread.currentThread().getName()); response.put(GUID, guid);
return Response.ok(response).build(); return Response.ok(response).build();
} catch (Exception e) { } catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to persist instance for type {}", typeName, e); LOG.error("Unable to persist instance for type {}", typeName, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST)); Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (JSONException e) {
LOG.error("Unable to persist instance for type {}", typeName, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} }
} }
...@@ -107,13 +116,15 @@ public class EntityResource { ...@@ -107,13 +116,15 @@ public class EntityResource {
@Path("definition/{guid}") @Path("definition/{guid}")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response getEntityDefinition(@PathParam("guid") String guid) { public Response getEntityDefinition(@PathParam("guid") String guid) {
Preconditions.checkNotNull(guid, "guid cannot be null"); Preconditions.checkNotNull(guid, "Entity GUID cannot be null");
try { try {
LOG.debug("Fetching entity definition for guid={} ", guid);
final String entityDefinition = metadataService.getEntityDefinition(guid); final String entityDefinition = metadataService.getEntityDefinition(guid);
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("requestId", Thread.currentThread().getName()); response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
Response.Status status = Response.Status.NOT_FOUND; Response.Status status = Response.Status.NOT_FOUND;
if (entityDefinition != null) { if (entityDefinition != null) {
...@@ -123,7 +134,7 @@ public class EntityResource { ...@@ -123,7 +134,7 @@ public class EntityResource {
return Response.status(status).entity(response).build(); return Response.status(status).entity(response).build();
} catch (MetadataException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("An entity with GUID={} does not exist", guid, e); LOG.error("An entity with GUID={} does not exist", guid, e);
throw new WebApplicationException(e, Response throw new WebApplicationException(e, Response
.status(Response.Status.NOT_FOUND) .status(Response.Status.NOT_FOUND)
...@@ -153,15 +164,18 @@ public class EntityResource { ...@@ -153,15 +164,18 @@ public class EntityResource {
public Response getEntityList(@PathParam("entityType") String entityType, public Response getEntityList(@PathParam("entityType") String entityType,
@DefaultValue("0") @QueryParam("offset") Integer offset, @DefaultValue("0") @QueryParam("offset") Integer offset,
@QueryParam("numResults") Integer resultsPerPage) { @QueryParam("numResults") Integer resultsPerPage) {
Preconditions.checkNotNull(entityType, "Entity type cannot be null");
try { try {
LOG.debug("Fetching entity list for type={} ", entityType);
final List<String> entityList = metadataService.getEntityList(entityType); final List<String> entityList = metadataService.getEntityList(entityType);
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("requestId", Thread.currentThread().getName()); response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put("type", entityType);
response.put("list", new JSONArray(entityList)); response.put("list", new JSONArray(entityList));
return Response.ok(response).build(); return Response.ok(response).build();
} catch (MetadataException e) { } catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to get entity list for type {}", entityType, e); LOG.error("Unable to get entity list for type {}", entityType, e);
throw new WebApplicationException( throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST)); Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
...@@ -171,4 +185,110 @@ public class EntityResource { ...@@ -171,4 +185,110 @@ public class EntityResource {
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR)); Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
} }
} }
// Trait management functions
/**
* Gets the list of trait names for a given entity represented by a guid.
*
* @param guid globally unique identifier for the entity
* @return a list of trait names for the given entity guid
*/
@GET
@Path("traits/list/{guid}")
@Produces(MediaType.APPLICATION_JSON)
public Response getTraitNames(@PathParam("guid") String guid) {
Preconditions.checkNotNull(guid, "Entity GUID cannot be null");
try {
LOG.debug("Fetching trait names for entity={}", guid);
final List<String> traitNames = metadataService.getTraitNames(guid);
JSONObject response = new JSONObject();
response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put("list", new JSONArray(traitNames));
return Response.ok(response).build();
} catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to get trait names for entity {}", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (JSONException e) {
LOG.error("Unable to get trait names for entity {}", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
/**
* Adds a new trait to an existing entity represented by a guid.
*
* @param guid globally unique identifier for the entity
*/
@POST
@Path("traits/add/{guid}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response addTrait(@Context HttpServletRequest request,
@PathParam("guid") String guid) {
Preconditions.checkNotNull(guid, "Entity GUID cannot be null");
try {
final String traitDefinition = Servlets.getRequestPayload(request);
LOG.debug("Adding trait={} for entity={} ", traitDefinition, guid);
metadataService.addTrait(guid, traitDefinition);
JSONObject response = new JSONObject();
response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put("traitInstance", traitDefinition);
return Response.ok(response).build();
} catch (MetadataException | IOException | IllegalArgumentException e) {
LOG.error("Unable to add trait for entity={}", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (JSONException e) {
LOG.error("Unable to add trait for entity={}", guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
/**
* Deletes a given trait from an existing entity represented by a guid.
*
* @param guid globally unique identifier for the entity
* @param traitName name of the trait
*/
@PUT
@Path("traits/delete/{guid}/{traitName}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response deleteTrait(@Context HttpServletRequest request,
@PathParam("guid") String guid,
@PathParam(TRAIT_NAME) String traitName) {
Preconditions.checkNotNull(guid, "Entity GUID cannot be null");
Preconditions.checkNotNull(traitName, "Trait name cannot be null");
LOG.debug("Deleting trait={} from entity={} ", traitName, guid);
try {
metadataService.deleteTrait(guid, traitName);
JSONObject response = new JSONObject();
response.put(Servlets.REQUEST_ID, Servlets.getRequestId());
response.put(GUID, guid);
response.put(TRAIT_NAME, traitName);
return Response.ok(response).build();
} catch (MetadataException | IllegalArgumentException e) {
LOG.error("Unable to add trait name={} for entity={}", traitName, guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
} catch (JSONException e) {
LOG.error("Unable to add trait name={} for entity={}", traitName, guid, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.INTERNAL_SERVER_ERROR));
}
}
} }
...@@ -111,4 +111,8 @@ public final class Servlets { ...@@ -111,4 +111,8 @@ public final class Servlets {
IOUtils.copy(request.getInputStream(), writer); IOUtils.copy(request.getInputStream(), writer);
return writer.toString(); return writer.toString();
} }
public static String getRequestId() {
return Thread.currentThread().getName();
}
} }
...@@ -28,6 +28,7 @@ import org.apache.hadoop.metadata.typesystem.Referenceable; ...@@ -28,6 +28,7 @@ import org.apache.hadoop.metadata.typesystem.Referenceable;
import org.apache.hadoop.metadata.typesystem.Struct; import org.apache.hadoop.metadata.typesystem.Struct;
import org.apache.hadoop.metadata.typesystem.json.Serialization$; import org.apache.hadoop.metadata.typesystem.json.Serialization$;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization; import org.apache.hadoop.metadata.typesystem.json.TypesSerialization;
import org.apache.hadoop.metadata.typesystem.json.TypesSerialization$;
import org.apache.hadoop.metadata.typesystem.types.AttributeDefinition; import org.apache.hadoop.metadata.typesystem.types.AttributeDefinition;
import org.apache.hadoop.metadata.typesystem.types.AttributeInfo; import org.apache.hadoop.metadata.typesystem.types.AttributeInfo;
import org.apache.hadoop.metadata.typesystem.types.ClassType; import org.apache.hadoop.metadata.typesystem.types.ClassType;
...@@ -239,14 +240,127 @@ public class EntityJerseyResourceIT extends BaseResourceIT { ...@@ -239,14 +240,127 @@ public class EntityJerseyResourceIT extends BaseResourceIT {
ImmutableList.<String>of(), ImmutableList.<String>of(),
TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE), TypesUtil.createRequiredAttrDef("name", DataTypes.STRING_TYPE),
TypesUtil.createRequiredAttrDef("description", DataTypes.STRING_TYPE)); TypesUtil.createRequiredAttrDef("description", DataTypes.STRING_TYPE));
typeSystem.defineClassType(testTypeDefinition);
@SuppressWarnings("unchecked") String typesAsJSON = TypesSerialization.toJson(testTypeDefinition);
String typesAsJSON = TypesSerialization.toJson(typeSystem,
Arrays.asList(new String[]{"test"}));
sumbitType(typesAsJSON, "test"); sumbitType(typesAsJSON, "test");
} }
@Test (dependsOnMethods = "testSubmitEntity")
public void testGetTraitNames() throws Exception {
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/list")
.path(guid)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.GET, ClientResponse.class);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get("requestId"));
Assert.assertNotNull(response.get("GUID"));
final JSONArray list = response.getJSONArray("list");
Assert.assertEquals(list.length(), 7);
}
@Test (dependsOnMethods = "testGetTraitNames")
public void testAddTrait() throws Exception {
final String traitName = "PII_Trait";
HierarchicalTypeDefinition<TraitType> piiTrait =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
LOG.debug("traitDefinitionAsJSON = " + traitDefinitionAsJSON);
sumbitType(traitDefinitionAsJSON, traitName);
typeSystem.defineTraitType(piiTrait);
Struct s = new Struct(traitName);
TraitType tType = typeSystem.getDataType(TraitType.class, traitName);
ITypedInstance traitInstance = tType.convert(s, Multiplicity.REQUIRED);
String traitInstanceAsJSON = Serialization$.MODULE$.toJson(traitInstance);
LOG.debug("traitInstanceAsJSON = " + traitInstanceAsJSON);
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/add")
.path(guid)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get("requestId"));
Assert.assertNotNull(response.get("GUID"));
Assert.assertNotNull(response.get("traitInstance"));
}
@Test
public void testAddTraitWithNoRegistration() throws Exception {
final String traitName = "PII_Trait_Blah";
HierarchicalTypeDefinition<TraitType> piiTrait =
TypesUtil.createTraitTypeDef(traitName, ImmutableList.<String>of());
String traitDefinitionAsJSON = TypesSerialization$.MODULE$.toJson(piiTrait, true);
LOG.debug("traitDefinitionAsJSON = " + traitDefinitionAsJSON);
typeSystem.defineTraitType(piiTrait);
Struct s = new Struct(traitName);
TraitType tType = typeSystem.getDataType(TraitType.class, traitName);
ITypedInstance traitInstance = tType.convert(s, Multiplicity.REQUIRED);
String traitInstanceAsJSON = Serialization$.MODULE$.toJson(traitInstance);
LOG.debug("traitInstanceAsJSON = " + traitInstanceAsJSON);
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/add")
.path(guid)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.POST, ClientResponse.class, traitInstanceAsJSON);
Assert.assertEquals(clientResponse.getStatus(),
Response.Status.BAD_REQUEST.getStatusCode());
}
@Test (dependsOnMethods = "testAddTrait")
public void testDeleteTrait() throws Exception {
final String traitName = "PII_Trait";
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/delete")
.path(guid)
.path(traitName)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.PUT, ClientResponse.class);
Assert.assertEquals(clientResponse.getStatus(), Response.Status.OK.getStatusCode());
String responseAsString = clientResponse.getEntity(String.class);
Assert.assertNotNull(responseAsString);
JSONObject response = new JSONObject(responseAsString);
Assert.assertNotNull(response.get("requestId"));
Assert.assertNotNull(response.get("GUID"));
Assert.assertNotNull(response.get("traitName"));
}
@Test
public void testDeleteTraitNonExistent() throws Exception {
final String traitName = "blah_trait";
ClientResponse clientResponse = service
.path("api/metadata/entities/traits/delete")
.path(guid)
.path(traitName)
.accept(MediaType.APPLICATION_JSON)
.type(MediaType.APPLICATION_JSON)
.method(HttpMethod.PUT, ClientResponse.class);
Assert.assertEquals(clientResponse.getStatus(),
Response.Status.BAD_REQUEST.getStatusCode());
}
private void createHiveTypes() throws Exception { private void createHiveTypes() throws Exception {
HierarchicalTypeDefinition<ClassType> databaseTypeDefinition = HierarchicalTypeDefinition<ClassType> databaseTypeDefinition =
TypesUtil.createClassTypeDef(DATABASE_TYPE, TypesUtil.createClassTypeDef(DATABASE_TYPE,
......
...@@ -27,7 +27,6 @@ import org.apache.hadoop.metadata.typesystem.types.ClassType; ...@@ -27,7 +27,6 @@ import org.apache.hadoop.metadata.typesystem.types.ClassType;
import org.apache.hadoop.metadata.typesystem.types.DataTypes; import org.apache.hadoop.metadata.typesystem.types.DataTypes;
import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition; import org.apache.hadoop.metadata.typesystem.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.Multiplicity; import org.apache.hadoop.metadata.typesystem.types.Multiplicity;
import org.apache.hadoop.metadata.typesystem.types.StructTypeDefinition;
import org.apache.hadoop.metadata.typesystem.types.TraitType; import org.apache.hadoop.metadata.typesystem.types.TraitType;
import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil; import org.apache.hadoop.metadata.typesystem.types.utils.TypesUtil;
import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONArray;
...@@ -65,8 +64,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT { ...@@ -65,8 +64,7 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
@Test @Test
public void testSubmit() throws Exception { public void testSubmit() throws Exception {
for (HierarchicalTypeDefinition typeDefinition : typeDefinitions) { for (HierarchicalTypeDefinition typeDefinition : typeDefinitions) {
String typesAsJSON = TypesSerialization.toJson( String typesAsJSON = TypesSerialization.toJson(typeDefinition);
typeSystem, typeDefinition.typeName);
System.out.println("typesAsJSON = " + typesAsJSON); System.out.println("typesAsJSON = " + typesAsJSON);
WebResource resource = service WebResource resource = service
...@@ -174,11 +172,6 @@ public class TypesJerseyResourceIT extends BaseResourceIT { ...@@ -174,11 +172,6 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
TypesUtil.createRequiredAttrDef("level", DataTypes.INT_TYPE)); TypesUtil.createRequiredAttrDef("level", DataTypes.INT_TYPE));
typeDefinitions.add(fetlTypeDefinition); typeDefinitions.add(fetlTypeDefinition);
typeSystem.defineTypes(
ImmutableList.<StructTypeDefinition>of(),
ImmutableList.of(fetlTypeDefinition),
ImmutableList.of(databaseTypeDefinition, tableTypeDefinition));
return typeDefinitions; return typeDefinitions;
} }
} }
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