aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/crud/dao/champ/ChampDao.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/onap/crud/dao/champ/ChampDao.java')
-rw-r--r--src/main/java/org/onap/crud/dao/champ/ChampDao.java151
1 files changed, 84 insertions, 67 deletions
diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java
index 554b9fa..402d2cf 100644
--- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java
+++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java
@@ -54,6 +54,7 @@ public class ChampDao implements GraphDao {
protected String baseObjectUrl;
protected String baseRelationshipUrl;
protected String baseTransactionUrl;
+ protected String baseBulkUrl;
protected static final String HEADER_FROM_APP = "X-FromAppId";
protected static final String HEADER_TRANS_ID = "X-TransactionId";
@@ -61,13 +62,14 @@ public class ChampDao implements GraphDao {
protected static final String OBJECT_SUB_URL = "objects";
protected static final String RELATIONSHIP_SUB_URL = "relationships";
protected static final String TRANSACTION_SUB_URL = "transaction";
+ protected static final String BULK_SUB_URL = "bulk";
// We use a custom vertex serializer for champ because it expects "key"
// instead of "id"
protected static final Gson champGson = new GsonBuilder()
- .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
- .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
- .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
+ .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
+ .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
+ .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
public ChampDao() {
}
@@ -76,12 +78,13 @@ public class ChampDao implements GraphDao {
try {
String deobfuscatedCertPassword = certPassword.startsWith("OBF:")?Password.deobfuscate(certPassword):certPassword;
client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
- .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
- .clientCertPassword(deobfuscatedCertPassword);
+ .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
+ .clientCertPassword(deobfuscatedCertPassword);
baseObjectUrl = champUrl + OBJECT_SUB_URL;
baseRelationshipUrl = champUrl + RELATIONSHIP_SUB_URL;
baseTransactionUrl = champUrl + TRANSACTION_SUB_URL;
+ baseBulkUrl = champUrl + BULK_SUB_URL;
} catch (Exception e) {
System.out.println("Error setting up Champ configuration");
e.printStackTrace();
@@ -90,10 +93,10 @@ public class ChampDao implements GraphDao {
}
public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) {
- this.client = client;
- this.baseObjectUrl = baseObjectUrl;
- this.baseRelationshipUrl = baseRelationshipUrl;
- this.baseTransactionUrl = baseTransactionUrl;
+ this.client = client;
+ this.baseObjectUrl = baseObjectUrl;
+ this.baseRelationshipUrl = baseRelationshipUrl;
+ this.baseTransactionUrl = baseTransactionUrl;
}
@Override
@@ -117,8 +120,8 @@ public class ChampDao implements GraphDao {
strBuild.append(id);
if(queryParams != null && !queryParams.isEmpty())
{
- strBuild.append("?");
- strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
}
OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -130,50 +133,50 @@ public class ChampDao implements GraphDao {
// We didn't find a vertex with the supplied type, so just throw an
// exception.
throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
- javax.ws.rs.core.Response.Status.NOT_FOUND);
+ javax.ws.rs.core.Response.Status.NOT_FOUND);
}
return getResult;
} else {
// We didn't find a vertex with the supplied id, so just throw an
// exception.
- throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
+ throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
}
}
@Override
public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException {
- StringBuilder strBuild = new StringBuilder(baseObjectUrl);
- strBuild.append("/relationships/");
- strBuild.append(id);
+ StringBuilder strBuild = new StringBuilder(baseObjectUrl);
+ strBuild.append("/relationships/");
+ strBuild.append(id);
- Map<String,String> queryParamsCopy = null;
- if (queryParams != null) {
- queryParamsCopy = new HashMap<String,String>(queryParams);
- }
- else {
- queryParamsCopy = new HashMap<String,String>();
- }
+ Map<String,String> queryParamsCopy = null;
+ if (queryParams != null) {
+ queryParamsCopy = new HashMap<String,String>(queryParams);
+ }
+ else {
+ queryParamsCopy = new HashMap<String,String>();
+ }
- if (txId != null) {
- queryParamsCopy.put("transactionId", txId);
- }
+ if (txId != null) {
+ queryParamsCopy.put("transactionId", txId);
+ }
- if (!queryParamsCopy.isEmpty())
- {
- strBuild.append("?");
- strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset()));
- }
+ if (!queryParamsCopy.isEmpty())
+ {
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset()));
+ }
- OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
+ OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
- if (getResult.getResultCode() == 200) {
- return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
- }.getType());
- } else {
- // We didn't find a vertex with the supplied id, so just throw an
- // exception.
- throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
- }
+ if (getResult.getResultCode() == 200) {
+ return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
+ }.getType());
+ } else {
+ // We didn't find a vertex with the supplied id, so just throw an
+ // exception.
+ throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
+ }
}
@Override
@@ -188,7 +191,7 @@ public class ChampDao implements GraphDao {
List<NameValuePair> queryParams = convertToNameValuePair(filter);
queryParams.addAll(convertToNameValuePair("properties", properties));
String url = baseObjectUrl + "/filter" + "?"
- + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
+ + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -208,8 +211,8 @@ public class ChampDao implements GraphDao {
strBuild.append(id);
if(queryParams != null && !queryParams.isEmpty())
{
- strBuild.append("?");
- strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
}
OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
@@ -220,7 +223,7 @@ public class ChampDao implements GraphDao {
// We didn't find an edge with the supplied type, so just throw an
// exception.
throw new CrudException("No edge with id " + id + " and type " + type + " found in graph",
- javax.ws.rs.core.Response.Status.NOT_FOUND);
+ javax.ws.rs.core.Response.Status.NOT_FOUND);
}
return getResult;
} else {
@@ -233,12 +236,12 @@ public class ChampDao implements GraphDao {
@Override
public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException {
String url = baseRelationshipUrl + "/filter" + "?"
- + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
+ + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- return getResult;
+ return getResult;
} else {
// We didn't find a vertex with the supplied id, so just throw an
// exception.
@@ -259,7 +262,7 @@ public class ChampDao implements GraphDao {
Vertex insertVertex = insertVertexBuilder.build();
OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
return getResult;
@@ -285,7 +288,7 @@ public class ChampDao implements GraphDao {
String payload = insertVertex.toJson(champGson);
OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
return getResult;
@@ -324,7 +327,7 @@ public class ChampDao implements GraphDao {
String edgeJson = insertEdge.toJson(champGson);
OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
return getResult;
@@ -344,7 +347,7 @@ public class ChampDao implements GraphDao {
String edgeJson = edge.toJson(champGson);
OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
return getResult;
@@ -385,11 +388,11 @@ public class ChampDao implements GraphDao {
String url = baseTransactionUrl + "/" + id;
OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.TEXT_PLAIN_TYPE);
+ MediaType.TEXT_PLAIN_TYPE);
if (getResult.getResultCode() != 200) {
throw new CrudException("Unable to commit transaction",
- Response.Status.fromStatusCode(getResult.getResultCode()));
+ Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@@ -398,11 +401,11 @@ public class ChampDao implements GraphDao {
String url = baseTransactionUrl + "/" + id;
OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.TEXT_PLAIN_TYPE);
+ MediaType.TEXT_PLAIN_TYPE);
if (getResult.getResultCode() != 200) {
throw new CrudException("Unable to rollback transaction",
- Response.Status.fromStatusCode(getResult.getResultCode()));
+ Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@@ -431,7 +434,7 @@ public class ChampDao implements GraphDao {
Vertex insertVertex = insertVertexBuilder.build();
OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
return Vertex.fromJson(getResult.getResult(), version);
@@ -444,7 +447,7 @@ public class ChampDao implements GraphDao {
@Override
public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
- throws CrudException {
+ throws CrudException {
String url = baseRelationshipUrl + "?transactionId=" + txId;
// Try requests to ensure source and target exist in Champ
@@ -456,7 +459,7 @@ public class ChampDao implements GraphDao {
Edge insertEdge = insertEdgeBuilder.build();
OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
- MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
return Edge.fromJson(getResult.getResult());
@@ -482,7 +485,7 @@ public class ChampDao implements GraphDao {
String payload = insertVertex.toJson(champGson);
OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
return Vertex.fromJson(getResult.getResult(), version);
@@ -512,7 +515,7 @@ public class ChampDao implements GraphDao {
}
String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
- MediaType.APPLICATION_JSON_TYPE);
+ MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
return Edge.fromJson(getResult.getResult());
@@ -520,7 +523,7 @@ public class ChampDao implements GraphDao {
// We didn't create an edge with the supplied type, so just throw an
// exception.
throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
- Response.Status.fromStatusCode(getResult.getResultCode()));
+ Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@@ -576,7 +579,7 @@ public class ChampDao implements GraphDao {
// We didn't find a vertex with the supplied type, so just throw an
// exception.
throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
- javax.ws.rs.core.Response.Status.NOT_FOUND);
+ javax.ws.rs.core.Response.Status.NOT_FOUND);
}
return vert;
} else {
@@ -613,12 +616,26 @@ public class ChampDao implements GraphDao {
private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg)
{
- CrudException ce = null;
- if(result != null)
- ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode()));
- else
- ce = new CrudException(defaultErrorMsg, defaultErrorCode);
- return ce;
+ CrudException ce = null;
+ if(result != null)
+ ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode()));
+ else
+ ce = new CrudException(defaultErrorMsg, defaultErrorCode);
+ return ce;
+ }
+
+ @Override
+ public OperationResult bulkOperation(ChampBulkPayload champPayload) throws CrudException {
+ String url = baseBulkUrl;
+
+ OperationResult getResult = client.post(url, champPayload.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
+ MediaType.APPLICATION_JSON_TYPE);
+
+ if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
+ return getResult;
+ } else {
+ throw new CrudException("Bulk request failed: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
+ }
}
}