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