Performance Improvements for Gizmo bulk API
[aai/gizmo.git] / src / main / java / org / onap / crud / dao / champ / ChampDao.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.dao.champ;
22
23 import java.nio.charset.Charset;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import javax.ws.rs.core.MediaType;
32 import javax.ws.rs.core.Response;
33 import org.apache.http.NameValuePair;
34 import org.apache.http.client.utils.URLEncodedUtils;
35 import org.apache.http.message.BasicNameValuePair;
36 import org.eclipse.jetty.util.security.Password;
37 import org.onap.aai.cl.mdc.MdcContext;
38 import org.onap.aai.restclient.client.OperationResult;
39 import org.onap.aai.restclient.client.RestClient;
40 import org.onap.aai.restclient.enums.RestAuthenticationMode;
41 import org.onap.crud.dao.GraphDao;
42 import org.onap.crud.entity.Edge;
43 import org.onap.crud.entity.Vertex;
44 import org.onap.crud.exception.CrudException;
45 import org.onap.crud.util.CrudServiceConstants;
46 import org.slf4j.MDC;
47 import com.google.gson.Gson;
48 import com.google.gson.GsonBuilder;
49 import com.google.gson.reflect.TypeToken;
50 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
51
52 public class ChampDao implements GraphDao {
53   protected RestClient client;
54   protected String baseObjectUrl;
55   protected String baseRelationshipUrl;
56   protected String baseTransactionUrl;
57   protected String baseBulkUrl;
58
59   protected static final String HEADER_FROM_APP = "X-FromAppId";
60   protected static final String HEADER_TRANS_ID = "X-TransactionId";
61   protected static final String FROM_APP_NAME = "Gizmo";
62   protected static final String OBJECT_SUB_URL = "objects";
63   protected static final String RELATIONSHIP_SUB_URL = "relationships";
64   protected static final String TRANSACTION_SUB_URL = "transaction";
65   protected static final String BULK_SUB_URL = "bulk";
66
67   // We use a custom vertex serializer for champ because it expects "key"
68   // instead of "id"
69   protected static final Gson champGson = new GsonBuilder()
70           .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
71           .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
72           .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
73
74   public ChampDao() {
75   }
76
77   public ChampDao(String champUrl, String certPassword) {
78     try {
79       String deobfuscatedCertPassword = certPassword.startsWith("OBF:")?Password.deobfuscate(certPassword):certPassword;
80       client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
81               .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
82               .clientCertPassword(deobfuscatedCertPassword);
83
84       baseObjectUrl = champUrl + OBJECT_SUB_URL;
85       baseRelationshipUrl = champUrl + RELATIONSHIP_SUB_URL;
86       baseTransactionUrl = champUrl + TRANSACTION_SUB_URL;
87       baseBulkUrl = champUrl + BULK_SUB_URL;
88     } catch (Exception e) {
89       System.out.println("Error setting up Champ configuration");
90       e.printStackTrace();
91       System.exit(1);
92     }
93   }
94
95   public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) {
96     this.client = client;
97     this.baseObjectUrl = baseObjectUrl;
98     this.baseRelationshipUrl = baseRelationshipUrl;
99     this.baseTransactionUrl = baseTransactionUrl;
100   }
101
102   @Override
103   public Vertex getVertex(String id, String version) throws CrudException {
104     String url = baseObjectUrl + "/" + id;
105     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
106
107     if (getResult.getResultCode() == 200) {
108       return Vertex.fromJson(getResult.getResult(), version);
109     } else {
110       // We didn't find a vertex with the supplied id, so just throw an
111       // exception.
112       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
113     }
114   }
115
116   @Override
117   public OperationResult getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException {
118     StringBuilder strBuild = new StringBuilder(baseObjectUrl);
119     strBuild.append("/");
120     strBuild.append(id);
121     if(queryParams != null && !queryParams.isEmpty())
122     {
123       strBuild.append("?");
124       strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
125     }
126
127     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
128
129     if (getResult.getResultCode() == 200) {
130       Vertex vert = Vertex.fromJson(getResult.getResult(), version);
131
132       if (!vert.getType().equalsIgnoreCase(type)) {
133         // We didn't find a vertex with the supplied type, so just throw an
134         // exception.
135         throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
136                 javax.ws.rs.core.Response.Status.NOT_FOUND);
137       }
138       return getResult;
139     } else {
140       // We didn't find a vertex with the supplied id, so just throw an
141       // exception.
142       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
143     }
144   }
145
146   @Override
147   public List<Edge> getVertexEdges(String id, Map<String, String> queryParams, String txId) throws CrudException {
148     StringBuilder strBuild = new StringBuilder(baseObjectUrl);
149     strBuild.append("/relationships/");
150     strBuild.append(id);
151
152     Map<String,String> queryParamsCopy = null;
153     if (queryParams != null) {
154       queryParamsCopy = new HashMap<String,String>(queryParams);
155     }
156     else {
157       queryParamsCopy = new HashMap<String,String>();
158     }
159
160     if (txId != null) {
161       queryParamsCopy.put("transactionId", txId);
162     }
163
164     if (!queryParamsCopy.isEmpty())
165     {
166       strBuild.append("?");
167       strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), Charset.defaultCharset()));
168     }
169
170     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
171
172     if (getResult.getResultCode() == 200) {
173       return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
174       }.getType());
175     } else {
176       // We didn't find a vertex with the supplied id, so just throw an
177       // exception.
178       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
179     }
180   }
181
182   @Override
183   public OperationResult getVertices(String type, Map<String, Object> filter, String version) throws CrudException {
184     return getVertices(type, filter, new HashSet<String>(), version);
185   }
186
187   @Override
188   public OperationResult getVertices(String type, Map<String, Object> filter, Set<String> properties, String version) throws CrudException {
189     filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
190
191     List<NameValuePair> queryParams = convertToNameValuePair(filter);
192     queryParams.addAll(convertToNameValuePair("properties", properties));
193     String url = baseObjectUrl + "/filter" + "?"
194             + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
195
196     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
197
198     if (getResult.getResultCode() == 200) {
199       return getResult;
200     } else {
201       // We didn't find a vertex with the supplied id, so just throw an
202       // exception.
203       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertices found in graph for given filters");
204     }
205   }
206
207   @Override
208   public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException {
209     StringBuilder strBuild = new StringBuilder(baseRelationshipUrl);
210     strBuild.append("/");
211     strBuild.append(id);
212     if(queryParams != null && !queryParams.isEmpty())
213     {
214       strBuild.append("?");
215       strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset()));
216     }
217     OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE);
218
219     if (getResult.getResultCode() == 200) {
220       Edge edge = Edge.fromJson(getResult.getResult());
221
222       if (!edge.getType().equalsIgnoreCase(type)) {
223         // We didn't find an edge with the supplied type, so just throw an
224         // exception.
225         throw new CrudException("No edge with id " + id + " and type " + type + " found in graph",
226                 javax.ws.rs.core.Response.Status.NOT_FOUND);
227       }
228       return getResult;
229     } else {
230       // We didn't find a edge with the supplied type, so just throw an
231       // exception.
232       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
233     }
234   }
235
236   @Override
237   public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException {
238     String url = baseRelationshipUrl + "/filter" + "?"
239             + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
240
241     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
242
243     if (getResult.getResultCode() == 200) {
244       return getResult;
245     } else {
246       // We didn't find a vertex with the supplied id, so just throw an
247       // exception.
248       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edges found in graph  for given filters");
249     }
250   }
251
252   @Override
253   public OperationResult addVertex(String type, Map<String, Object> properties, String version) throws CrudException {
254     String url = baseObjectUrl;
255
256     // Add the aai_node_type so that AAI can read the data created by gizmo
257     // TODO: This probably shouldn't be here
258     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
259
260     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
261     properties.forEach(insertVertexBuilder::property);
262     Vertex insertVertex = insertVertexBuilder.build();
263
264     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
265             MediaType.APPLICATION_JSON_TYPE);
266
267     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
268       return getResult;
269     } else {
270       // We didn't create a vertex with the supplied type, so just throw an
271       // exception.
272       throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
273     }
274   }
275
276   @Override
277   public OperationResult updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException {
278     String url = baseObjectUrl + "/" + id;
279
280     // Add the aai_node_type so that AAI can read the data created by gizmo
281     // TODO: This probably shouldn't be here
282     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
283
284     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
285     insertVertexBuilder.id(id);
286     properties.forEach(insertVertexBuilder::property);
287     Vertex insertVertex = insertVertexBuilder.build();
288
289     String payload = insertVertex.toJson(champGson);
290     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
291             MediaType.APPLICATION_JSON_TYPE);
292
293     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
294       return getResult;
295     } else {
296       // We didn't create a vertex with the supplied type, so just throw an
297       // exception.
298       throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
299     }
300   }
301
302   @Override
303   public void deleteVertex(String id, String type) throws CrudException {
304     String url = baseObjectUrl + "/" + id;
305     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
306
307     if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
308       // We didn't delete a vertex with the supplied id, so just throw an
309       // exception.
310       throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
311     }
312   }
313
314   @Override
315   public OperationResult addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException {
316     String url = baseRelationshipUrl;
317
318     // Try requests to ensure source and target exist in Champ
319     OperationResult dbSourceOpResult = getVertex(source.getId().get(), source.getType(), version, new HashMap<String, String>());
320     Vertex dbSource = Vertex.fromJson(dbSourceOpResult.getResult(), version);
321     OperationResult dbTargetOpResult = getVertex(target.getId().get(), target.getType(), version, new HashMap<String, String>());
322     Vertex dbTarget = Vertex.fromJson(dbTargetOpResult.getResult(), version);
323
324     Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
325     properties.forEach(insertEdgeBuilder::property);
326     Edge insertEdge = insertEdgeBuilder.build();
327
328     String edgeJson = insertEdge.toJson(champGson);
329     OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
330             MediaType.APPLICATION_JSON_TYPE);
331
332     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
333       return getResult;
334     } else {
335       // We didn't create an edge with the supplied type, so just throw an
336       // exception.
337       throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
338     }
339   }
340
341   @Override
342   public OperationResult updateEdge(Edge edge) throws CrudException {
343     if (!edge.getId().isPresent()) {
344       throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
345     }
346     String url = baseRelationshipUrl + "/" + edge.getId().get();
347
348     String edgeJson = edge.toJson(champGson);
349     OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
350             MediaType.APPLICATION_JSON_TYPE);
351
352     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
353       return getResult;
354     } else {
355       // We didn't create an edge with the supplied type, so just throw an
356       // exception.
357       throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
358     }
359   }
360
361   @Override
362   public void deleteEdge(String id) throws CrudException {
363     String url = baseRelationshipUrl + "/" + id;
364     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
365
366     if (getResult.getResultCode() != 200) {
367       // We didn't find an edge with the supplied type, so just throw an
368       // exception.
369       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
370     }
371   }
372
373   @Override
374   public String openTransaction() {
375     String url = baseTransactionUrl;
376
377     OperationResult getResult = client.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
378
379     if (getResult.getResultCode() == 200) {
380       return getResult.getResult();
381     } else {
382       return null;
383     }
384   }
385
386   @Override
387   public void commitTransaction(String id) throws CrudException {
388     String url = baseTransactionUrl + "/" + id;
389
390     OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
391             MediaType.TEXT_PLAIN_TYPE);
392
393     if (getResult.getResultCode() != 200) {
394       throw new CrudException("Unable to commit transaction",
395               Response.Status.fromStatusCode(getResult.getResultCode()));
396     }
397   }
398
399   @Override
400   public void rollbackTransaction(String id) throws CrudException {
401     String url = baseTransactionUrl + "/" + id;
402
403     OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
404             MediaType.TEXT_PLAIN_TYPE);
405
406     if (getResult.getResultCode() != 200) {
407       throw new CrudException("Unable to rollback transaction",
408               Response.Status.fromStatusCode(getResult.getResultCode()));
409     }
410   }
411
412   @Override
413   public boolean transactionExists(String id) throws CrudException {
414     String url = baseTransactionUrl + "/" + id;
415     Map<String, List<String>> headers = new HashMap<>();
416     headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
417     headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
418
419     OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
420
421     return getResult.getResultCode() == 200;
422   }
423
424   @Override
425   public Vertex addVertex(String type, Map<String, Object> properties, String version, String txId) throws CrudException {
426     String url = baseObjectUrl + "?transactionId=" + txId;
427
428     // Add the aai_node_type so that AAI can read the data created by gizmo
429     // TODO: This probably shouldn't be here
430     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
431
432     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
433     properties.forEach(insertVertexBuilder::property);
434     Vertex insertVertex = insertVertexBuilder.build();
435
436     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
437             MediaType.APPLICATION_JSON_TYPE);
438
439     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
440       return Vertex.fromJson(getResult.getResult(), version);
441     } else {
442       // We didn't create a vertex with the supplied type, so just throw an
443       // exception.
444       throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
445     }
446   }
447
448   @Override
449   public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
450           throws CrudException {
451     String url = baseRelationshipUrl + "?transactionId=" + txId;
452
453     // Try requests to ensure source and target exist in Champ
454     Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, txId);
455     Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, txId);
456
457     Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
458     properties.forEach(insertEdgeBuilder::property);
459     Edge insertEdge = insertEdgeBuilder.build();
460
461     OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
462             MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
463
464     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
465       return Edge.fromJson(getResult.getResult());
466     } else {
467       // We didn't create an edge with the supplied type, so just throw an
468       // exception.
469       throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
470     }
471   }
472
473   @Override
474   public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version, String txId) throws CrudException {
475     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
476
477     // Add the aai_node_type so that AAI can read the data created by gizmo
478     // TODO: This probably shouldn't be here
479     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
480
481     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
482     insertVertexBuilder.id(id);
483     properties.forEach(insertVertexBuilder::property);
484     Vertex insertVertex = insertVertexBuilder.build();
485
486     String payload = insertVertex.toJson(champGson);
487     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
488             MediaType.APPLICATION_JSON_TYPE);
489
490     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
491       return Vertex.fromJson(getResult.getResult(), version);
492     } else {
493       // We didn't create a vertex with the supplied type, so just throw an
494       // exception.
495       throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
496     }
497   }
498
499   @Override
500   public void deleteVertex(String id, String type, String txId) throws CrudException {
501     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
502     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
503
504     if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
505       // We didn't delete a vertex with the supplied id, so just throw an
506       // exception.
507       throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
508     }
509   }
510
511   @Override
512   public Edge updateEdge(Edge edge, String txId) throws CrudException {
513     if (!edge.getId().isPresent()) {
514       throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
515     }
516     String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
517     OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
518             MediaType.APPLICATION_JSON_TYPE);
519
520     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
521       return Edge.fromJson(getResult.getResult());
522     } else {
523       // We didn't create an edge with the supplied type, so just throw an
524       // exception.
525       throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
526               Response.Status.fromStatusCode(getResult.getResultCode()));
527     }
528   }
529
530   @Override
531   public void deleteEdge(String id, String txId) throws CrudException {
532     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
533     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
534
535     if (getResult.getResultCode() != 200) {
536       // We didn't find an edge with the supplied type, so just throw an
537       // exception.
538       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
539     }
540   }
541
542   @Override
543   public Edge getEdge(String id, String txId) throws CrudException {
544     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
545     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
546
547     if (getResult.getResultCode() == 200) {
548       Edge edge = Edge.fromJson(getResult.getResult());
549       return edge;
550     } else {
551       // We didn't find an edge with the supplied id, so just throw an
552       // exception.
553       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
554     }
555   }
556
557   @Override
558   public Edge getEdge(String id) throws CrudException {
559     String url = baseRelationshipUrl + "/" + id;
560     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
561
562     if (getResult.getResultCode() == 200) {
563       Edge edge = Edge.fromJson(getResult.getResult());
564       return edge;
565     } else {
566       // We didn't find an edge with the supplied id, so just throw an exception.
567       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph");
568     }
569   }
570
571   public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
572     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
573     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
574
575     if (getResult.getResultCode() == 200) {
576       Vertex vert = Vertex.fromJson(getResult.getResult(), version);
577
578       if (!vert.getType().equalsIgnoreCase(type)) {
579         // We didn't find a vertex with the supplied type, so just throw an
580         // exception.
581         throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph",
582                 javax.ws.rs.core.Response.Status.NOT_FOUND);
583       }
584       return vert;
585     } else {
586       // We didn't find a vertex with the supplied id, so just throw an
587       // exception.
588       throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph");
589     }
590   }
591
592   // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
593   private List<NameValuePair> convertToNameValuePair(Map<String, ? super String> pairs) {
594     List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
595
596     pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));
597
598     return nvpList;
599   }
600
601   // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
602   private List<NameValuePair> convertToNameValuePair(String k, Set<String> values) {
603     List<NameValuePair> nvpList = new ArrayList<>(values.size());
604
605     values.forEach((v) -> nvpList.add(new BasicNameValuePair(k, v)));
606
607     return nvpList;
608   }
609
610   private Map<String, List<String>> createHeader() {
611     Map<String, List<String>> headers = new HashMap<>();
612     headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
613     headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID)));
614     return headers;
615   }
616
617   private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg)
618   {
619     CrudException ce = null;
620     if(result != null)
621       ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode()));
622     else
623       ce = new CrudException(defaultErrorMsg, defaultErrorCode);
624     return ce;
625   }
626
627   @Override
628   public OperationResult bulkOperation(ChampBulkPayload champPayload) throws CrudException {
629     String url = baseBulkUrl;
630
631     OperationResult getResult = client.post(url, champPayload.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
632             MediaType.APPLICATION_JSON_TYPE);
633
634     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
635       return getResult;
636     } else {
637       throw new CrudException("Bulk request failed: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
638     }
639   }
640
641 }