For supporting t_k query
[aai/gizmo.git] / src / main / java / org / onap / crud / dao / champ / ChampDao.java
index ecbac36..5c6dbbe 100644 (file)
@@ -57,26 +57,29 @@ import javax.ws.rs.core.MediaType;
 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 {
@@ -94,13 +97,20 @@ 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;
+  }
+
   @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.
@@ -110,12 +120,20 @@ public class ChampDao implements GraphDao {
   }
 
   @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
@@ -133,11 +151,18 @@ public class ChampDao implements GraphDao {
   }
 
   @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());
@@ -150,12 +175,12 @@ public class ChampDao implements GraphDao {
   }
 
   @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);
@@ -166,8 +191,7 @@ public class ChampDao implements GraphDao {
     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.
@@ -177,10 +201,17 @@ public class ChampDao implements GraphDao {
   }
 
   @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());
 
@@ -216,7 +247,7 @@ public class ChampDao implements GraphDao {
   }
 
   @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
@@ -231,7 +262,7 @@ public class ChampDao implements GraphDao {
         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.
@@ -240,7 +271,7 @@ public class ChampDao implements GraphDao {
   }
 
   @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
@@ -257,11 +288,11 @@ public class ChampDao implements GraphDao {
         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()));
     }
   }
 
@@ -273,17 +304,17 @@ public class ChampDao implements GraphDao {
     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);
@@ -298,7 +329,7 @@ public class ChampDao implements GraphDao {
     } 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()));
     }
   }
 
@@ -318,7 +349,7 @@ public class ChampDao implements GraphDao {
     } 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()));
     }
   }
 
@@ -386,7 +417,7 @@ public class ChampDao implements GraphDao {
   }
 
   @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
@@ -401,22 +432,22 @@ public class ChampDao implements GraphDao {
         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);
@@ -430,12 +461,12 @@ public class ChampDao implements GraphDao {
     } 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
@@ -452,11 +483,11 @@ public class ChampDao implements GraphDao {
         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()));
     }
   }
 
@@ -468,7 +499,7 @@ public class ChampDao implements GraphDao {
     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()));
     }
   }
 
@@ -525,12 +556,12 @@ public class ChampDao implements GraphDao {
     }
   }
 
-  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
@@ -548,7 +579,7 @@ public class ChampDao implements GraphDao {
   }
 
   // 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())));