Commit 386d8d38 by Venkatesh Seetharam

ISSUE-35 Add typed instance put/get end to end. Contributed by Venkatesh Seetharam

parent 8285e3b9
......@@ -83,8 +83,10 @@ public class DefaultMetadataService implements MetadataService {
return response;
} catch (ParseException e) {
LOG.error("Unable to parse JSON for type {}", typeName, e);
throw new MetadataException("validation failed for: " + typeName);
} catch (JSONException e) {
LOG.error("Unable to persist type {}", typeName, e);
throw new MetadataException("Unable to create response for: " + typeName);
}
}
......@@ -147,6 +149,7 @@ public class DefaultMetadataService implements MetadataService {
Serialization$.MODULE$.fromJson(entityDefinition);
return repository.createEntity(entityInstance, entityType);
} catch (ParseException e) {
LOG.error("Unable to parse JSON {} for type {}", entityDefinition, entityType, e);
throw new MetadataException("validation failed for: " + entityType);
}
}
......@@ -154,7 +157,9 @@ public class DefaultMetadataService implements MetadataService {
private void validateEntity(String entity, String entityType) throws ParseException {
Preconditions.checkNotNull(entity, "entity cannot be null");
Preconditions.checkNotNull(entityType, "entity type cannot be null");
JSONValue.parseWithException(entity);
// todo: this is failing for instances but not types
// JSONValue.parseWithException(entity);
}
/**
......@@ -167,21 +172,9 @@ public class DefaultMetadataService implements MetadataService {
public String getEntityDefinition(String guid) throws MetadataException {
final ITypedReferenceableInstance instance =
repository.getEntityDefinition(guid);
return Serialization$.MODULE$.toJson(instance);
}
/**
* Return the definition for the given entity name and type.
*
* @param entityName name
* @param entityType type
* @return entity definition as JSON
*/
@Override
public String getEntityDefinition(String entityName,
String entityType) throws MetadataException {
throw new UnsupportedOperationException();
return instance == null
? null
: Serialization$.MODULE$.toJson(instance);
}
/**
......
......@@ -73,15 +73,6 @@ public interface MetadataService extends Service {
String getEntityDefinition(String guid) throws MetadataException;
/**
* Return the definition for the given entity name and type.
*
* @param entityName name
* @param entityType type
* @return entity definition as JSON
*/
String getEntityDefinition(String entityName, String entityType) throws MetadataException;
/**
* Return the list of entity names for the given type in the repository.
*
* @param entityType type
......
......@@ -23,9 +23,45 @@ public final class GraphUtils {
private static final Logger LOG = LoggerFactory.getLogger(GraphUtils.class);
private static final String GUID_PROPERTY_KEY = "guid";
private static final String TIMESTAMP_PROPERTY_KEY = "timestamp";
private GraphUtils() {
}
public static Edge addEdge(Vertex fromVertex, Vertex toVertex, String edgeLabel) {
return addEdge(fromVertex, toVertex, edgeLabel, null);
}
public static Edge addEdge(Vertex fromVertex, Vertex toVertex,
String edgeLabel, String timestamp) {
Edge edge = findEdge(fromVertex, toVertex, edgeLabel);
Edge edgeToVertex = edge != null ? edge : fromVertex.addEdge(edgeLabel, toVertex);
if (timestamp != null) {
edgeToVertex.setProperty(TIMESTAMP_PROPERTY_KEY, timestamp);
}
return edgeToVertex;
}
public static Edge findEdge(Vertex fromVertex, Vertex toVertex, String edgeLabel) {
return findEdge(fromVertex, toVertex.getProperty(GUID_PROPERTY_KEY), edgeLabel);
}
public static Edge findEdge(Vertex fromVertex, Object toVertexName, String edgeLabel) {
Edge edgeToFind = null;
for (Edge edge : fromVertex.getEdges(Direction.OUT, edgeLabel)) {
if (edge.getVertex(Direction.IN).getProperty(
GUID_PROPERTY_KEY).equals(toVertexName)) {
edgeToFind = edge;
break;
}
}
return edgeToFind;
}
public static Vertex findVertex(Graph blueprintsGraph,
String guid) {
LOG.debug("Finding vertex for: guid={}", guid);
......
......@@ -67,22 +67,23 @@ public class EntityResource {
}
@POST
@Path("submit/{entityType}")
@Path("submit/{typeName}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response submit(@Context HttpServletRequest request,
@PathParam("entityType") final String entityType) {
@PathParam("typeName") final String typeName) {
try {
final String entity = Servlets.getRequestPayload(request);
System.out.println("entity = " + entity);
LOG.debug("submitting entity {} ", entity);
final String guid = metadataService.createEntity(entity, entityType);
final String guid = metadataService.createEntity(typeName, entity);
JSONObject response = new JSONObject();
response.put("GUID", guid);
response.put("requestId", Thread.currentThread().getName());
return Response.ok(response).build();
} catch (Exception e) {
LOG.error("Unable to persist instance for type {}", typeName, e);
throw new WebApplicationException(
Servlets.getErrorResponse(e, Response.Status.BAD_REQUEST));
}
......@@ -109,8 +110,7 @@ public class EntityResource {
return Response.status(status).entity(response).build();
} catch (Exception e) {
LOG.error("Action failed: {}\nError: {}",
Response.Status.INTERNAL_SERVER_ERROR, e.getMessage());
LOG.error("Unable to get instance definition for GUID {}", guid, e);
throw new WebApplicationException(e, Response
.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(e.getMessage())
......
......@@ -10,16 +10,21 @@ import org.apache.hadoop.metadata.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.types.IDataType;
import org.apache.hadoop.metadata.types.Multiplicity;
import org.apache.hadoop.metadata.types.TraitType;
import org.apache.hadoop.metadata.types.TypeSystem;
import org.testng.annotations.BeforeClass;
import javax.ws.rs.core.UriBuilder;
public class BaseResourceIT {
public abstract class BaseResourceIT {
protected TypeSystem typeSystem;
protected WebResource service;
@BeforeClass
public void setUp() throws Exception {
typeSystem = TypeSystem.getInstance();
typeSystem.reset();
String baseUrl = "http://localhost:21000/";
DefaultClientConfig config = new DefaultClientConfig();
......
......@@ -21,7 +21,6 @@ package org.apache.hadoop.metadata.web.resources;
import com.google.common.collect.ImmutableList;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import org.apache.hadoop.metadata.MetadataException;
import org.apache.hadoop.metadata.json.TypesSerialization;
import org.apache.hadoop.metadata.types.AttributeDefinition;
import org.apache.hadoop.metadata.types.ClassType;
......@@ -30,7 +29,6 @@ import org.apache.hadoop.metadata.types.HierarchicalTypeDefinition;
import org.apache.hadoop.metadata.types.Multiplicity;
import org.apache.hadoop.metadata.types.StructTypeDefinition;
import org.apache.hadoop.metadata.types.TraitType;
import org.apache.hadoop.metadata.types.TypeSystem;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;
import org.testng.Assert;
......@@ -49,14 +47,12 @@ import java.util.List;
*/
public class TypesJerseyResourceIT extends BaseResourceIT {
private TypeSystem typeSystem;
private List<HierarchicalTypeDefinition> typeDefinitions;
@BeforeClass
public void setUp() throws Exception {
super.setUp();
typeSystem = TypeSystem.getInstance();
typeDefinitions = createHiveTypes();
}
......@@ -151,11 +147,12 @@ public class TypesJerseyResourceIT extends BaseResourceIT {
Assert.assertNotNull(list);
}
private List<HierarchicalTypeDefinition> createHiveTypes() throws MetadataException {
private List<HierarchicalTypeDefinition> createHiveTypes() throws Exception {
ArrayList<HierarchicalTypeDefinition> typeDefinitions = new ArrayList<>();
HierarchicalTypeDefinition<ClassType> databaseTypeDefinition =
createClassTypeDef("database", ImmutableList.<String>of(),
createClassTypeDef("database",
ImmutableList.<String>of(),
createRequiredAttrDef("name", DataTypes.STRING_TYPE),
createRequiredAttrDef("description", DataTypes.STRING_TYPE));
typeDefinitions.add(databaseTypeDefinition);
......
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