import javax.ws.rs.core.Response;
public class ChampDao implements GraphDao {
- private RestClient client;
- private String baseObjectUrl;
- private String baseRelationshipUrl;
- private String baseTransactionUrl;
-
- private static final String HEADER_FROM_APP = "X-FromAppId";
- private static final String HEADER_TRANS_ID = "X-TransactionId";
- private static final String FROM_APP_NAME = "Gizmo";
- private static final String OBJECT_SUB_URL = "objects";
- private static final String RELATIONSHIP_SUB_URL = "relationships";
- private static final String TRANSACTION_SUB_URL = "transaction";
+ protected RestClient client;
+ protected String baseObjectUrl;
+ protected String baseRelationshipUrl;
+ protected String baseTransactionUrl;
+
+ protected static final String HEADER_FROM_APP = "X-FromAppId";
+ protected static final String HEADER_TRANS_ID = "X-TransactionId";
+ protected static final String FROM_APP_NAME = "Gizmo";
+ protected static final String OBJECT_SUB_URL = "objects";
+ protected static final String RELATIONSHIP_SUB_URL = "relationships";
+ protected static final String TRANSACTION_SUB_URL = "transaction";
private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName());
// We use a custom vertex serializer for champ because it expects "key"
// instead of "id"
- private static final Gson champGson = new GsonBuilder()
+ protected static final Gson champGson = new GsonBuilder()
.registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
.registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
.registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
+
+ public ChampDao() {
+ }
public ChampDao(String champUrl, String certPassword) {
try {
}
}
+ public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) {
+ this.client = client;
+ this.baseObjectUrl = baseObjectUrl;
+ this.baseRelationshipUrl = baseRelationshipUrl;
+ this.baseTransactionUrl = baseTransactionUrl;
+ }
+
@Override
- public Vertex getVertex(String id) throws CrudException {
+ public Vertex getVertex(String id, String version) throws CrudException {
String url = baseObjectUrl + "/" + id;
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- return Vertex.fromJson(getResult.getResult());
+ return Vertex.fromJson(getResult.getResult(), version);
} else {
// We didn't find a vertex with the supplied id, so just throw an
// exception.
}
@Override
- public Vertex getVertex(String id, String type) throws CrudException {
- String url = baseObjectUrl + "/" + id;
- OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
+ public Vertex getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException {
+ StringBuilder strBuild = new StringBuilder(baseObjectUrl);
+ strBuild.append("/");
+ strBuild.append(id);
+ if(queryParams != null && !queryParams.isEmpty())
+ {
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+ }
+
+ OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- Vertex vert = Vertex.fromJson(getResult.getResult());
+ Vertex vert = Vertex.fromJson(getResult.getResult(), version);
if (!vert.getType().equalsIgnoreCase(type)) {
// We didn't find a vertex with the supplied type, so just throw an
}
@Override
- public List<Edge> getVertexEdges(String id) throws CrudException {
- String url = baseObjectUrl + "/relationships/" + id;
-
- OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
+ public List<Edge> getVertexEdges(String id, Map<String, String> queryParams) throws CrudException {
+ StringBuilder strBuild = new StringBuilder(baseObjectUrl);
+ strBuild.append("/relationships/");
+ strBuild.append(id);
+ if(queryParams != null && !queryParams.isEmpty())
+ {
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+ }
+ 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());
}
@Override
- public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
- return getVertices(type, filter, new HashSet<String>());
+ public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException {
+ return getVertices(type, filter, new HashSet<String>(), version);
}
@Override
- public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties) throws CrudException {
+ public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException {
filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
List<NameValuePair> queryParams = convertToNameValuePair(filter);
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- return champGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
- }.getType());
+ return Vertex.collectionFromJson(getResult.getResult(), version);
} else {
// We didn't find a vertex with the supplied id, so just throw an
// exception.
}
@Override
- public Edge getEdge(String id, String type) throws CrudException {
- String url = baseRelationshipUrl + "/" + id;
- OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
-
+ public Edge getEdge(String id, String type, Map<String, String> queryParams) throws CrudException {
+ StringBuilder strBuild = new StringBuilder(baseRelationshipUrl);
+ strBuild.append("/");
+ strBuild.append(id);
+ if(queryParams != null && !queryParams.isEmpty())
+ {
+ strBuild.append("?");
+ strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
+ }
+ OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
+
if (getResult.getResultCode() == 200) {
Edge edge = Edge.fromJson(getResult.getResult());
}
@Override
- public Vertex addVertex(String type, Map<String, Object> properties) throws CrudException {
+ public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException {
String url = baseObjectUrl;
// Add the aai_node_type so that AAI can read the data created by gizmo
MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
- return Vertex.fromJson(getResult.getResult());
+ return Vertex.fromJson(getResult.getResult(), version);
} else {
// We didn't create a vertex with the supplied type, so just throw an
// exception.
}
@Override
- public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException {
+ public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException {
String url = baseObjectUrl + "/" + id;
// Add the aai_node_type so that AAI can read the data created by gizmo
MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
- return Vertex.fromJson(getResult.getResult());
+ return Vertex.fromJson(getResult.getResult(), version);
} else {
// We didn't create a vertex with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
// We didn't delete a vertex with the supplied id, so just throw an
// exception.
- throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@Override
- public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException {
+ public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException {
String url = baseRelationshipUrl;
// Try requests to ensure source and target exist in Champ
- Vertex dbSource = getVertex(source.getId().get(), source.getType());
- Vertex dbTarget = getVertex(target.getId().get(), target.getType());
+ Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, new HashMap<String, String>());
+ Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap<String, String>());
Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
properties.forEach(insertEdgeBuilder::property);
} else {
// We didn't create an edge with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
} else {
// We didn't create an edge with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to update edge", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
}
@Override
- public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException {
+ public Vertex addVertex(String type, Map<String, Object> properties, String version, String txId) throws CrudException {
String url = baseObjectUrl + "?transactionId=" + txId;
// Add the aai_node_type so that AAI can read the data created by gizmo
MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
- return Vertex.fromJson(getResult.getResult());
+ return Vertex.fromJson(getResult.getResult(), version);
} else {
// We didn't create a vertex with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@Override
- public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId)
+ public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
throws CrudException {
String url = baseRelationshipUrl + "?transactionId=" + txId;
// Try requests to ensure source and target exist in Champ
- Vertex dbSource = getVertex(source.getId().get(), source.getType(), txId);
- Vertex dbTarget = getVertex(target.getId().get(), target.getType(), txId);
+ Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, txId);
+ Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, txId);
Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
properties.forEach(insertEdgeBuilder::property);
} else {
// We didn't create an edge with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
@Override
- public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException {
+ public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version, String txId) throws CrudException {
String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
// Add the aai_node_type so that AAI can read the data created by gizmo
MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
- return Vertex.fromJson(getResult.getResult());
+ return Vertex.fromJson(getResult.getResult(), version);
} else {
// We didn't create a vertex with the supplied type, so just throw an
// exception.
- throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
// We didn't delete a vertex with the supplied id, so just throw an
// exception.
- throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
+ throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
}
}
}
}
- public Vertex getVertex(String id, String type, String txId) throws CrudException {
+ public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- Vertex vert = Vertex.fromJson(getResult.getResult());
+ Vertex vert = Vertex.fromJson(getResult.getResult(), version);
if (!vert.getType().equalsIgnoreCase(type)) {
// We didn't find a vertex with the supplied type, so just throw an
}
// https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
- private List<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
+ private List<NameValuePair> convertToNameValuePair(Map<String, ? super String> pairs) {
List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));