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