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