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