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