Config to be able to route GET through datarouter
[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) throws CrudException {
124     String url = baseObjectUrl + "/" + id;
125     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
126
127     if (getResult.getResultCode() == 200) {
128       Vertex vert = Vertex.fromJson(getResult.getResult(), version);
129
130       if (!vert.getType().equalsIgnoreCase(type)) {
131         // We didn't find a vertex with the supplied type, so just throw an
132         // exception.
133         throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
134             javax.ws.rs.core.Response.Status.NOT_FOUND);
135       }
136       return vert;
137     } else {
138       // We didn't find a vertex with the supplied id, so just throw an
139       // exception.
140       throw new CrudException("No vertex with id " + id + " found in graph",
141           javax.ws.rs.core.Response.Status.NOT_FOUND);
142     }
143   }
144
145   @Override
146   public List<Edge> getVertexEdges(String id) throws CrudException {
147     String url = baseObjectUrl + "/relationships/" + id;
148
149     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
150
151     if (getResult.getResultCode() == 200) {
152       return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
153       }.getType());
154     } else {
155       // We didn't find a vertex with the supplied id, so just throw an
156       // exception.
157       throw new CrudException("No vertex with id " + id + " found in graph",
158           javax.ws.rs.core.Response.Status.NOT_FOUND);
159     }
160   }
161
162   @Override
163   public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
164     return getVertices(type, filter, new HashSet<String>());
165   }
166
167   @Override
168   public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties) throws CrudException {
169     filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
170
171     List<NameValuePair> queryParams = convertToNameValuePair(filter);
172     queryParams.addAll(convertToNameValuePair("properties", properties));
173     String url = baseObjectUrl + "/filter" + "?"
174         + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
175
176     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
177
178     if (getResult.getResultCode() == 200) {
179       return champGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
180       }.getType());
181     } else {
182       // We didn't find a vertex with the supplied id, so just throw an
183       // exception.
184       throw new CrudException("No vertices found in graph for given filters",
185           javax.ws.rs.core.Response.Status.NOT_FOUND);
186     }
187   }
188
189   @Override
190   public Edge getEdge(String id, String type) throws CrudException {
191     String url = baseRelationshipUrl + "/" + id;
192     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
193
194     if (getResult.getResultCode() == 200) {
195       Edge edge = Edge.fromJson(getResult.getResult());
196
197       if (!edge.getType().equalsIgnoreCase(type)) {
198         // We didn't find an edge with the supplied type, so just throw an
199         // exception.
200         throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
201             javax.ws.rs.core.Response.Status.NOT_FOUND);
202       }
203       return edge;
204     } else {
205       // We didn't find a edge with the supplied type, so just throw an
206       // exception.
207       throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
208     }
209   }
210
211   @Override
212   public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException {
213     String url = baseRelationshipUrl + "/filter" + "?"
214         + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
215
216     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
217
218     if (getResult.getResultCode() == 200) {
219       return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
220       }.getType());
221     } else {
222       // We didn't find a vertex with the supplied id, so just throw an
223       // exception.
224       throw new CrudException("No edges found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND);
225     }
226   }
227
228   @Override
229   public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException {
230     String url = baseObjectUrl;
231
232     // Add the aai_node_type so that AAI can read the data created by gizmo
233     // TODO: This probably shouldn't be here
234     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
235
236     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
237     properties.forEach(insertVertexBuilder::property);
238     Vertex insertVertex = insertVertexBuilder.build();
239
240     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
241         MediaType.APPLICATION_JSON_TYPE);
242
243     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
244       return Vertex.fromJson(getResult.getResult(), version);
245     } else {
246       // We didn't create a vertex with the supplied type, so just throw an
247       // exception.
248       throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
249     }
250   }
251
252   @Override
253   public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException {
254     String url = baseObjectUrl + "/" + id;
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     insertVertexBuilder.id(id);
262     properties.forEach(insertVertexBuilder::property);
263     Vertex insertVertex = insertVertexBuilder.build();
264
265     String payload = insertVertex.toJson(champGson);
266     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
267         MediaType.APPLICATION_JSON_TYPE);
268
269     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
270       return Vertex.fromJson(getResult.getResult(), version);
271     } else {
272       // We didn't create a vertex with the supplied type, so just throw an
273       // exception.
274       throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
275     }
276   }
277
278   @Override
279   public void deleteVertex(String id, String type) throws CrudException {
280     String url = baseObjectUrl + "/" + id;
281     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
282
283     if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
284       // We didn't delete a vertex with the supplied id, so just throw an
285       // exception.
286       throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
287     }
288   }
289
290   @Override
291   public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException {
292     String url = baseRelationshipUrl;
293
294     // Try requests to ensure source and target exist in Champ
295     Vertex dbSource = getVertex(source.getId().get(), source.getType(), version);
296     Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version);
297
298     Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
299     properties.forEach(insertEdgeBuilder::property);
300     Edge insertEdge = insertEdgeBuilder.build();
301
302     String edgeJson = insertEdge.toJson(champGson);
303     OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
304         MediaType.APPLICATION_JSON_TYPE);
305
306     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
307       return Edge.fromJson(getResult.getResult());
308     } else {
309       // We didn't create an edge with the supplied type, so just throw an
310       // exception.
311       throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
312     }
313   }
314
315   @Override
316   public Edge updateEdge(Edge edge) throws CrudException {
317     if (!edge.getId().isPresent()) {
318       throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
319     }
320     String url = baseRelationshipUrl + "/" + edge.getId().get();
321
322     String edgeJson = edge.toJson(champGson);
323     OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
324         MediaType.APPLICATION_JSON_TYPE);
325
326     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
327       return Edge.fromJson(getResult.getResult());
328     } else {
329       // We didn't create an edge with the supplied type, so just throw an
330       // exception.
331       throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
332     }
333   }
334
335   @Override
336   public void deleteEdge(String id, String type) throws CrudException {
337     String url = baseRelationshipUrl + "/" + id;
338     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
339
340     if (getResult.getResultCode() != 200) {
341       // We didn't find an edge with the supplied type, so just throw an
342       // exception.
343       throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
344     }
345   }
346
347   @Override
348   public String openTransaction() {
349     String url = baseTransactionUrl;
350
351     OperationResult getResult = client.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
352
353     if (getResult.getResultCode() == 200) {
354       return getResult.getResult();
355     } else {
356       return null;
357     }
358   }
359
360   @Override
361   public void commitTransaction(String id) throws CrudException {
362     String url = baseTransactionUrl + "/" + id;
363
364     OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
365         MediaType.TEXT_PLAIN_TYPE);
366
367     if (getResult.getResultCode() != 200) {
368       throw new CrudException("Unable to commit transaction",
369           Response.Status.fromStatusCode(getResult.getResultCode()));
370     }
371   }
372
373   @Override
374   public void rollbackTransaction(String id) throws CrudException {
375     String url = baseTransactionUrl + "/" + id;
376
377     OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
378         MediaType.TEXT_PLAIN_TYPE);
379
380     if (getResult.getResultCode() != 200) {
381       throw new CrudException("Unable to rollback transaction",
382           Response.Status.fromStatusCode(getResult.getResultCode()));
383     }
384   }
385
386   @Override
387   public boolean transactionExists(String id) throws CrudException {
388     String url = baseTransactionUrl + "/" + id;
389     Map<String, List<String>> headers = new HashMap<>();
390     headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
391     headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
392
393     OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
394
395     return getResult.getResultCode() == 200;
396   }
397
398   @Override
399   public Vertex addVertex(String type, Map<String, Object> properties, String version, String txId) throws CrudException {
400     String url = baseObjectUrl + "?transactionId=" + txId;
401
402     // Add the aai_node_type so that AAI can read the data created by gizmo
403     // TODO: This probably shouldn't be here
404     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
405
406     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
407     properties.forEach(insertVertexBuilder::property);
408     Vertex insertVertex = insertVertexBuilder.build();
409
410     OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
411         MediaType.APPLICATION_JSON_TYPE);
412
413     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
414       return Vertex.fromJson(getResult.getResult(), version);
415     } else {
416       // We didn't create a vertex with the supplied type, so just throw an
417       // exception.
418       throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
419     }
420   }
421
422   @Override
423   public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
424       throws CrudException {
425     String url = baseRelationshipUrl + "?transactionId=" + txId;
426
427     // Try requests to ensure source and target exist in Champ
428     Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, txId);
429     Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, txId);
430
431     Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
432     properties.forEach(insertEdgeBuilder::property);
433     Edge insertEdge = insertEdgeBuilder.build();
434
435     OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
436         MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
437
438     if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
439       return Edge.fromJson(getResult.getResult());
440     } else {
441       // We didn't create an edge with the supplied type, so just throw an
442       // exception.
443       throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
444     }
445   }
446
447   @Override
448   public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version, String txId) throws CrudException {
449     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
450
451     // Add the aai_node_type so that AAI can read the data created by gizmo
452     // TODO: This probably shouldn't be here
453     properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
454
455     Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
456     insertVertexBuilder.id(id);
457     properties.forEach(insertVertexBuilder::property);
458     Vertex insertVertex = insertVertexBuilder.build();
459
460     String payload = insertVertex.toJson(champGson);
461     OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
462         MediaType.APPLICATION_JSON_TYPE);
463
464     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
465       return Vertex.fromJson(getResult.getResult(), version);
466     } else {
467       // We didn't create a vertex with the supplied type, so just throw an
468       // exception.
469       throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
470     }
471   }
472
473   @Override
474   public void deleteVertex(String id, String type, String txId) throws CrudException {
475     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
476     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
477
478     if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
479       // We didn't delete a vertex with the supplied id, so just throw an
480       // exception.
481       throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
482     }
483   }
484
485   @Override
486   public Edge updateEdge(Edge edge, String txId) throws CrudException {
487     if (!edge.getId().isPresent()) {
488       throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
489     }
490     String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
491     OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
492         MediaType.APPLICATION_JSON_TYPE);
493
494     if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
495       return Edge.fromJson(getResult.getResult());
496     } else {
497       // We didn't create an edge with the supplied type, so just throw an
498       // exception.
499       throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
500           Response.Status.fromStatusCode(getResult.getResultCode()));
501     }
502   }
503
504   @Override
505   public void deleteEdge(String id, String type, String txId) throws CrudException {
506     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
507     OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
508
509     if (getResult.getResultCode() != 200) {
510       // We didn't find an edge with the supplied type, so just throw an
511       // exception.
512       throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
513     }
514   }
515
516   @Override
517   public Edge getEdge(String id, String type, String txId) throws CrudException {
518     String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
519     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
520
521     if (getResult.getResultCode() == 200) {
522       Edge edge = Edge.fromJson(getResult.getResult());
523
524       if (!edge.getType().equalsIgnoreCase(type)) {
525         // We didn't find an edge with the supplied type, so just throw an
526         // exception.
527         throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
528             javax.ws.rs.core.Response.Status.NOT_FOUND);
529       }
530       return edge;
531     } else {
532       // We didn't find an edge with the supplied id, so just throw an
533       // exception.
534       throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
535     }
536   }
537
538   public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
539     String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
540     OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
541
542     if (getResult.getResultCode() == 200) {
543       Vertex vert = Vertex.fromJson(getResult.getResult(), version);
544
545       if (!vert.getType().equalsIgnoreCase(type)) {
546         // We didn't find a vertex with the supplied type, so just throw an
547         // exception.
548         throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
549             javax.ws.rs.core.Response.Status.NOT_FOUND);
550       }
551       return vert;
552     } else {
553       // We didn't find a vertex with the supplied id, so just throw an
554       // exception.
555       throw new CrudException("No vertex with id " + id + " found in graph",
556           javax.ws.rs.core.Response.Status.NOT_FOUND);
557     }
558   }
559
560   // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
561   private List<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
562     List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
563
564     pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));
565
566     return nvpList;
567   }
568
569   // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
570   private List<NameValuePair> convertToNameValuePair(String key, HashSet<String> values) {
571     List<NameValuePair> nvpList = new ArrayList<>(values.size());
572
573     values.forEach((value) -> nvpList.add(new BasicNameValuePair(key, value)));
574
575     return nvpList;
576   }
577   
578   private Map<String, List<String>> createHeader() {
579     Map<String, List<String>> headers = new HashMap<>();
580     headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
581     headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
582     return headers;
583   }
584 }