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