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" 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())
74 public ChampionDao(String championUrl, String certPassword) {
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));
82 baseUrl = championUrl;
83 } catch (Exception e) {
84 System.out.println("Error setting up Champion configuration");
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())));
97 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
99 if (getResult.getResultCode() == 200) {
100 return Vertex.fromJson(getResult.getResult());
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);
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 exception.
121 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
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);
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())));
137 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
139 if (getResult.getResultCode() == 200) {
140 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>(){}.getType());
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);
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);
151 String url = baseUrl + "objects/filter" + "?" +
152 URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
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())));
158 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
160 if (getResult.getResultCode() == 200) {
161 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>(){}.getType());
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);
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())));
175 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
177 if (getResult.getResultCode() == 200) {
178 Edge edge = Edge.fromJson(getResult.getResult());
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);
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);
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());
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())));
200 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
202 if (getResult.getResultCode() == 200) {
203 return championGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>(){}.getType());
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);
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())));
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);
221 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
222 properties.forEach(insertVertexBuilder::property);
223 Vertex insertVertex = insertVertexBuilder.build();
225 OperationResult getResult = client.post(url, insertVertex.toJson(), headers,
226 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
228 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
229 return Vertex.fromJson(getResult.getResult());
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()));
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())));
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);
247 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
248 insertVertexBuilder.id(id);
249 properties.forEach(insertVertexBuilder::property);
250 Vertex insertVertex = insertVertexBuilder.build();
252 String payload = insertVertex.toJson(championGson);
253 OperationResult getResult = client.put(url, payload, headers,
254 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
256 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
257 return Vertex.fromJson(getResult.getResult());
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()));
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())));
271 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
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()));
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())));
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());
290 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
291 properties.forEach(insertEdgeBuilder::property);
292 Edge insertEdge = insertEdgeBuilder.build();
294 String edgeJson = insertEdge.toJson(championGson);
295 OperationResult getResult = client.post(url, edgeJson, headers,
296 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
298 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
299 return Edge.fromJson(getResult.getResult());
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()));
307 public Edge updateEdge(Edge edge) throws CrudException {
308 if (!edge.getId().isPresent())
310 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
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())));
317 String edgeJson = edge.toJson(championGson);
318 OperationResult getResult = client.put(url, edgeJson, headers,
319 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
321 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
322 return Edge.fromJson(getResult.getResult());
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()));
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())));
336 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
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);
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())));
351 OperationResult getResult = client.post(url, "", headers, MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
353 if (getResult.getResultCode() == 200) {
354 return getResult.getResult();
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())));
367 OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", headers, MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE);
369 if (getResult.getResultCode() != 200) {
370 throw new CrudException("Unable to commit transaction", Response.Status.fromStatusCode(getResult.getResultCode()));
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())));
381 OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", headers, MediaType.APPLICATION_JSON_TYPE, MediaType.TEXT_PLAIN_TYPE);
383 if (getResult.getResultCode() != 200) {
384 throw new CrudException("Unable to rollback transaction", Response.Status.fromStatusCode(getResult.getResultCode()));
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())));
395 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
397 return getResult.getResultCode() == 200;
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())));
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);
411 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
412 properties.forEach(insertVertexBuilder::property);
413 Vertex insertVertex = insertVertexBuilder.build();
415 OperationResult getResult = client.post(url, insertVertex.toJson(), headers,
416 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
418 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
419 return Vertex.fromJson(getResult.getResult());
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()));
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())));
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);
437 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
438 properties.forEach(insertEdgeBuilder::property);
439 Edge insertEdge = insertEdgeBuilder.build();
441 OperationResult getResult = client.post(url, insertEdge.toJson(championGson), headers,
442 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
444 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
445 return Edge.fromJson(getResult.getResult());
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()));
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())));
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);
463 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
464 insertVertexBuilder.id(id);
465 properties.forEach(insertVertexBuilder::property);
466 Vertex insertVertex = insertVertexBuilder.build();
468 String payload = insertVertex.toJson(championGson);
469 OperationResult getResult = client.put(url, payload, headers,
470 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
472 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
473 return Vertex.fromJson(getResult.getResult());
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()));
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())));
487 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
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()));
496 public Edge updateEdge(Edge edge, String txId) throws CrudException {
497 if (!edge.getId().isPresent())
499 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
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())));
506 OperationResult getResult = client.put(url, edge.toJson(championGson), headers,
507 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
509 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
510 return Edge.fromJson(getResult.getResult());
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()));
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())));
524 OperationResult getResult = client.delete(url, headers, MediaType.APPLICATION_JSON_TYPE);
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);
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())));
539 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
541 if (getResult.getResultCode() == 200) {
542 Edge edge = Edge.fromJson(getResult.getResult());
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);
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);
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())));
561 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
563 if (getResult.getResultCode() == 200) {
564 Vertex vert = Vertex.fromJson(getResult.getResult());
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);
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);
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());
581 pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));