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