2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.crud.dao.champion;
26 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
28 import com.google.gson.Gson;
29 import com.google.gson.GsonBuilder;
30 import com.google.gson.reflect.TypeToken;
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;
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;
55 import javax.ws.rs.core.MediaType;
56 import javax.ws.rs.core.Response;
58 public class ChampionDao implements GraphDao {
59 private RestClient client;
60 private String baseUrl;
62 private static final String HEADER_FROM_APP = "X-FromAppId";
63 private static final String HEADER_TRANS_ID = "X-TransactionId";
65 private Logger logger = LoggerFactory.getInstance().getLogger(ChampionDao.class.getName());
67 // We use a custom vertex serializer for Champion because it expects "key"
69 private static final Gson championGson = new GsonBuilder()
70 .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
71 .registerTypeAdapter(Vertex.class, new ChampionVertexSerializer())
72 .registerTypeAdapter(Edge.class, new ChampionEdgeSerializer()).create();
74 public ChampionDao(String championUrl, String certPassword) {
76 client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
77 .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMPION_AUTH_FILE)
78 .clientCertPassword(Password.deobfuscate(certPassword));
80 baseUrl = championUrl;
81 } catch (Exception e) {
82 System.out.println("Error setting up Champion configuration");
89 public Vertex getVertex(String id) throws CrudException {
90 String url = baseUrl + "objects/" + id;
91 Map<String, List<String>> headers = new HashMap<>();
92 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
93 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
95 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
97 if (getResult.getResultCode() == 200) {
98 return Vertex.fromJson(getResult.getResult());
100 // We didn't find a vertex with the supplied id, so just throw an
102 throw new CrudException("No vertex with id " + id + " found in graph",
103 javax.ws.rs.core.Response.Status.NOT_FOUND);
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())));
114 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
116 if (getResult.getResultCode() == 200) {
117 Vertex vert = Vertex.fromJson(getResult.getResult());
119 if (!vert.getType().equalsIgnoreCase(type)) {
120 // We didn't find a vertex with the supplied type, so just throw an
122 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
123 javax.ws.rs.core.Response.Status.NOT_FOUND);
127 // We didn't find a vertex with the supplied id, so just throw an
129 throw new CrudException("No vertex with id " + id + " found in graph",
130 javax.ws.rs.core.Response.Status.NOT_FOUND);
135 public List<Edge> getVertexEdges(String id) throws CrudException {
136 String url = baseUrl + "objects/relationships/" + id;
137 Map<String, List<String>> headers = new HashMap<>();
138 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
139 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
141 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
143 if (getResult.getResultCode() == 200) {
144 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
147 // We didn't find a vertex with the supplied id, so just throw an
149 throw new CrudException("No vertex with id " + id + " found in graph",
150 javax.ws.rs.core.Response.Status.NOT_FOUND);
155 public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
156 filter.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
158 String url = baseUrl + "objects/filter" + "?"
159 + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
161 Map<String, List<String>> headers = new HashMap<>();
162 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
163 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
165 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
167 if (getResult.getResultCode() == 200) {
168 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
171 // We didn't find a vertex with the supplied id, so just throw an
173 throw new CrudException("No vertices found in graph for given filters",
174 javax.ws.rs.core.Response.Status.NOT_FOUND);
179 public Edge getEdge(String id, String type) throws CrudException {
180 String url = baseUrl + "relationships/" + id;
181 Map<String, List<String>> headers = new HashMap<>();
182 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
183 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
185 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
187 if (getResult.getResultCode() == 200) {
188 Edge edge = Edge.fromJson(getResult.getResult());
190 if (!edge.getType().equalsIgnoreCase(type)) {
191 // We didn't find an edge with the supplied type, so just throw an
193 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
194 javax.ws.rs.core.Response.Status.NOT_FOUND);
198 // We didn't find a edge with the supplied type, so just throw an
200 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
205 public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException {
206 String url = baseUrl + "relationships/filter" + "?"
207 + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
209 Map<String, List<String>> headers = new HashMap<>();
210 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
211 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
213 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
215 if (getResult.getResultCode() == 200) {
216 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
219 // We didn't find a vertex with the supplied id, so just throw an
221 throw new CrudException("No edges found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND);
226 public Vertex addVertex(String type, Map<String, Object> properties) throws CrudException {
227 String url = baseUrl + "objects";
228 Map<String, List<String>> headers = new HashMap<>();
229 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
230 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
232 // Add the aai_node_type so that AAI can read the data created by gizmo
233 // TODO: This probably shouldn't be here
234 properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
236 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
237 properties.forEach(insertVertexBuilder::property);
238 Vertex insertVertex = insertVertexBuilder.build();
240 OperationResult getResult = client.post(url, insertVertex.toJson(), headers, MediaType.APPLICATION_JSON_TYPE,
241 MediaType.APPLICATION_JSON_TYPE);
243 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
244 return Vertex.fromJson(getResult.getResult());
246 // We didn't create a vertex with the supplied type, so just throw an
248 throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
253 public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException {
254 String url = baseUrl + "objects/" + id;
255 Map<String, List<String>> headers = new HashMap<>();
256 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
257 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
259 // Add the aai_node_type so that AAI can read the data created by gizmo
260 // TODO: This probably shouldn't be here
261 properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
263 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
264 insertVertexBuilder.id(id);
265 properties.forEach(insertVertexBuilder::property);
266 Vertex insertVertex = insertVertexBuilder.build();
268 String payload = insertVertex.toJson(championGson);
269 OperationResult getResult = client.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE,
270 MediaType.APPLICATION_JSON_TYPE);
272 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
273 return Vertex.fromJson(getResult.getResult());
275 // We didn't create a vertex with the supplied type, so just throw an
277 throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
282 public void deleteVertex(String id, String type) throws CrudException {
283 String url = baseUrl + "objects/" + id;
284 Map<String, List<String>> headers = new HashMap<>();
285 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
286 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
288 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
290 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
291 // We didn't delete a vertex with the supplied id, so just throw an
293 throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
298 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException {
299 String url = baseUrl + "relationships";
300 Map<String, List<String>> headers = new HashMap<>();
301 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
302 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
304 // Try requests to ensure source and target exist in Champion
305 Vertex dbSource = getVertex(source.getId().get(), source.getType());
306 Vertex dbTarget = getVertex(target.getId().get(), target.getType());
308 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
309 properties.forEach(insertEdgeBuilder::property);
310 Edge insertEdge = insertEdgeBuilder.build();
312 String edgeJson = insertEdge.toJson(championGson);
313 OperationResult getResult = client.post(url, edgeJson, headers, MediaType.APPLICATION_JSON_TYPE,
314 MediaType.APPLICATION_JSON_TYPE);
316 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
317 return Edge.fromJson(getResult.getResult());
319 // We didn't create an edge with the supplied type, so just throw an
321 throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
326 public Edge updateEdge(Edge edge) throws CrudException {
327 if (!edge.getId().isPresent()) {
328 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
330 String url = baseUrl + "relationships/" + edge.getId().get();
331 Map<String, List<String>> headers = new HashMap<>();
332 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
333 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
335 String edgeJson = edge.toJson(championGson);
336 OperationResult getResult = client.put(url, edgeJson, headers, MediaType.APPLICATION_JSON_TYPE,
337 MediaType.APPLICATION_JSON_TYPE);
339 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
340 return Edge.fromJson(getResult.getResult());
342 // We didn't create an edge with the supplied type, so just throw an
344 throw new CrudException("Failed to update edge", Response.Status.fromStatusCode(getResult.getResultCode()));
349 public void deleteEdge(String id, String type) throws CrudException {
350 String url = baseUrl + "relationships/" + id;
351 Map<String, List<String>> headers = new HashMap<>();
352 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
353 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
355 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
357 if (getResult.getResultCode() != 200) {
358 // We didn't find an edge with the supplied type, so just throw an
360 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
365 public String openTransaction() {
366 String url = baseUrl + "transaction";
367 Map<String, List<String>> headers = new HashMap<>();
368 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
369 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
371 OperationResult getResult = client.post(url, "", headers, MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
373 if (getResult.getResultCode() == 200) {
374 return getResult.getResult();
381 public void commitTransaction(String id) throws CrudException {
382 String url = baseUrl + "transaction/" + id;
383 Map<String, List<String>> headers = new HashMap<>();
384 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
385 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
387 OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", headers, MediaType.APPLICATION_JSON_TYPE,
388 MediaType.TEXT_PLAIN_TYPE);
390 if (getResult.getResultCode() != 200) {
391 throw new CrudException("Unable to commit transaction",
392 Response.Status.fromStatusCode(getResult.getResultCode()));
397 public void rollbackTransaction(String id) throws CrudException {
398 String url = baseUrl + "transaction/" + id;
399 Map<String, List<String>> headers = new HashMap<>();
400 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
401 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
403 OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", headers, MediaType.APPLICATION_JSON_TYPE,
404 MediaType.TEXT_PLAIN_TYPE);
406 if (getResult.getResultCode() != 200) {
407 throw new CrudException("Unable to rollback transaction",
408 Response.Status.fromStatusCode(getResult.getResultCode()));
413 public boolean transactionExists(String id) throws CrudException {
414 String url = baseUrl + "transaction/" + id;
415 Map<String, List<String>> headers = new HashMap<>();
416 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
417 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
419 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
421 return getResult.getResultCode() == 200;
425 public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException {
426 String url = baseUrl + "objects?transactionId=" + txId;
427 Map<String, List<String>> headers = new HashMap<>();
428 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
429 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
431 // Add the aai_node_type so that AAI can read the data created by gizmo
432 // TODO: This probably shouldn't be here
433 properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
435 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
436 properties.forEach(insertVertexBuilder::property);
437 Vertex insertVertex = insertVertexBuilder.build();
439 OperationResult getResult = client.post(url, insertVertex.toJson(), headers, MediaType.APPLICATION_JSON_TYPE,
440 MediaType.APPLICATION_JSON_TYPE);
442 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
443 return Vertex.fromJson(getResult.getResult());
445 // We didn't create a vertex with the supplied type, so just throw an
447 throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
452 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId)
453 throws CrudException {
454 String url = baseUrl + "relationships?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())));
459 // Try requests to ensure source and target exist in Champion
460 Vertex dbSource = getVertex(source.getId().get(), source.getType(), txId);
461 Vertex dbTarget = getVertex(target.getId().get(), target.getType(), txId);
463 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
464 properties.forEach(insertEdgeBuilder::property);
465 Edge insertEdge = insertEdgeBuilder.build();
467 OperationResult getResult = client.post(url, insertEdge.toJson(championGson), headers,
468 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
470 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
471 return Edge.fromJson(getResult.getResult());
473 // We didn't create an edge with the supplied type, so just throw an
475 throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
480 public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException {
481 String url = baseUrl + "objects/" + id + "?transactionId=" + txId;
482 Map<String, List<String>> headers = new HashMap<>();
483 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
484 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
486 // Add the aai_node_type so that AAI can read the data created by gizmo
487 // TODO: This probably shouldn't be here
488 properties.put(org.openecomp.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
490 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
491 insertVertexBuilder.id(id);
492 properties.forEach(insertVertexBuilder::property);
493 Vertex insertVertex = insertVertexBuilder.build();
495 String payload = insertVertex.toJson(championGson);
496 OperationResult getResult = client.put(url, payload, headers, MediaType.APPLICATION_JSON_TYPE,
497 MediaType.APPLICATION_JSON_TYPE);
499 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
500 return Vertex.fromJson(getResult.getResult());
502 // We didn't create a vertex with the supplied type, so just throw an
504 throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
509 public void deleteVertex(String id, String type, String txId) throws CrudException {
510 String url = baseUrl + "objects/" + id + "?transactionId=" + txId;
511 Map<String, List<String>> headers = new HashMap<>();
512 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
513 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
515 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
517 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
518 // We didn't delete a vertex with the supplied id, so just throw an
520 throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
525 public Edge updateEdge(Edge edge, String txId) throws CrudException {
526 if (!edge.getId().isPresent()) {
527 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
529 String url = baseUrl + "relationships/" + edge.getId().get() + "?transactionId=" + txId;
530 Map<String, List<String>> headers = new HashMap<>();
531 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
532 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
534 OperationResult getResult = client.put(url, edge.toJson(championGson), headers, MediaType.APPLICATION_JSON_TYPE,
535 MediaType.APPLICATION_JSON_TYPE);
537 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
538 return Edge.fromJson(getResult.getResult());
540 // We didn't create an edge with the supplied type, so just throw an
542 throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
543 Response.Status.fromStatusCode(getResult.getResultCode()));
548 public void deleteEdge(String id, String type, String txId) throws CrudException {
549 String url = baseUrl + "relationships/" + id + "?transactionId=" + txId;
550 Map<String, List<String>> headers = new HashMap<>();
551 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
552 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
554 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
556 if (getResult.getResultCode() != 200) {
557 // We didn't find an edge with the supplied type, so just throw an
559 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
564 public Edge getEdge(String id, String type, String txId) throws CrudException {
565 String url = baseUrl + "relationships/" + id + "?transactionId=" + txId;
566 Map<String, List<String>> headers = new HashMap<>();
567 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
568 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
570 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
572 if (getResult.getResultCode() == 200) {
573 Edge edge = Edge.fromJson(getResult.getResult());
575 if (!edge.getType().equalsIgnoreCase(type)) {
576 // We didn't find an edge with the supplied type, so just throw an
578 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
579 javax.ws.rs.core.Response.Status.NOT_FOUND);
583 // We didn't find an edge with the supplied id, so just throw an
585 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
589 public Vertex getVertex(String id, String type, String txId) throws CrudException {
590 String url = baseUrl + "objects/" + id + "?transactionId=" + txId;
591 Map<String, List<String>> headers = new HashMap<>();
592 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
593 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
595 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
597 if (getResult.getResultCode() == 200) {
598 Vertex vert = Vertex.fromJson(getResult.getResult());
600 if (!vert.getType().equalsIgnoreCase(type)) {
601 // We didn't find a vertex with the supplied type, so just throw an
603 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
604 javax.ws.rs.core.Response.Status.NOT_FOUND);
608 // We didn't find a vertex with the supplied id, so just throw an
610 throw new CrudException("No vertex with id " + id + " found in graph",
611 javax.ws.rs.core.Response.Status.NOT_FOUND);
615 // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
616 private List<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
617 List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
619 pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));