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.onap.crud.dao.champ;
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.onap.aai.logging.LoggingContext;
37 import org.onap.aai.cl.api.Logger;
38 import org.onap.aai.cl.eelf.LoggerFactory;
39 import org.onap.crud.dao.GraphDao;
40 import org.onap.crud.entity.Edge;
41 import org.onap.crud.entity.Vertex;
42 import org.onap.crud.exception.CrudException;
43 import org.onap.crud.util.CrudServiceConstants;
44 import org.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 ChampDao implements GraphDao {
59 private RestClient client;
60 private String baseObjectUrl;
61 private String baseRelationshipUrl;
62 private String baseTransactionUrl;
64 private static final String HEADER_FROM_APP = "X-FromAppId";
65 private static final String HEADER_TRANS_ID = "X-TransactionId";
66 private static final String FROM_APP_NAME = "Gizmo";
67 private static final String OBJECT_SUB_URL = "objects";
68 private static final String RELATIONSHIP_SUB_URL = "relationships";
69 private static final String TRANSACTION_SUB_URL = "transaction";
71 private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName());
73 // We use a custom vertex serializer for champ because it expects "key"
75 private static final Gson champGson = new GsonBuilder()
76 .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
77 .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
78 .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
80 public ChampDao(String champUrl, String certPassword) {
82 client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
83 .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
84 .clientCertPassword(Password.deobfuscate(certPassword));
86 baseObjectUrl = champUrl + OBJECT_SUB_URL;
87 baseRelationshipUrl = champUrl + RELATIONSHIP_SUB_URL;
88 baseTransactionUrl = champUrl + TRANSACTION_SUB_URL;
89 } catch (Exception e) {
90 System.out.println("Error setting up Champ configuration");
97 public Vertex getVertex(String id) throws CrudException {
98 String url = baseObjectUrl + "/" + id;
99 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
101 if (getResult.getResultCode() == 200) {
102 return Vertex.fromJson(getResult.getResult());
104 // We didn't find a vertex with the supplied id, so just throw an
106 throw new CrudException("No vertex with id " + id + " found in graph",
107 javax.ws.rs.core.Response.Status.NOT_FOUND);
112 public Vertex getVertex(String id, String type) throws CrudException {
113 String url = baseObjectUrl + "/" + id;
114 OperationResult getResult = client.get(url, createHeader(), 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 = baseObjectUrl + "/relationships/" + id;
138 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
140 if (getResult.getResultCode() == 200) {
141 return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
144 // We didn't find a vertex with the supplied id, so just throw an
146 throw new CrudException("No vertex with id " + id + " found in graph",
147 javax.ws.rs.core.Response.Status.NOT_FOUND);
152 public List<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
153 filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
155 String url = baseObjectUrl + "/filter" + "?"
156 + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
158 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
160 if (getResult.getResultCode() == 200) {
161 return champGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
164 // We didn't find a vertex with the supplied id, so just throw an
166 throw new CrudException("No vertices found in graph for given filters",
167 javax.ws.rs.core.Response.Status.NOT_FOUND);
172 public Edge getEdge(String id, String type) throws CrudException {
173 String url = baseRelationshipUrl + "/" + id;
174 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
176 if (getResult.getResultCode() == 200) {
177 Edge edge = Edge.fromJson(getResult.getResult());
179 if (!edge.getType().equalsIgnoreCase(type)) {
180 // We didn't find an edge with the supplied type, so just throw an
182 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
183 javax.ws.rs.core.Response.Status.NOT_FOUND);
187 // We didn't find a edge with the supplied type, so just throw an
189 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
194 public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException {
195 String url = baseRelationshipUrl + "/filter" + "?"
196 + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
198 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
200 if (getResult.getResultCode() == 200) {
201 return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
204 // We didn't find a vertex with the supplied id, so just throw an
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 = baseObjectUrl;
214 // Add the aai_node_type so that AAI can read the data created by gizmo
215 // TODO: This probably shouldn't be here
216 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
218 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
219 properties.forEach(insertVertexBuilder::property);
220 Vertex insertVertex = insertVertexBuilder.build();
222 OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
223 MediaType.APPLICATION_JSON_TYPE);
225 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
226 return Vertex.fromJson(getResult.getResult());
228 // We didn't create a vertex with the supplied type, so just throw an
230 throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
235 public Vertex updateVertex(String id, String type, Map<String, Object> properties) throws CrudException {
236 String url = baseObjectUrl + "/" + id;
238 // Add the aai_node_type so that AAI can read the data created by gizmo
239 // TODO: This probably shouldn't be here
240 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
242 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
243 insertVertexBuilder.id(id);
244 properties.forEach(insertVertexBuilder::property);
245 Vertex insertVertex = insertVertexBuilder.build();
247 String payload = insertVertex.toJson(champGson);
248 OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
249 MediaType.APPLICATION_JSON_TYPE);
251 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
252 return Vertex.fromJson(getResult.getResult());
254 // We didn't create a vertex with the supplied type, so just throw an
256 throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
261 public void deleteVertex(String id, String type) throws CrudException {
262 String url = baseObjectUrl + "/" + id;
263 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
265 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
266 // We didn't delete a vertex with the supplied id, so just throw an
268 throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
273 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties) throws CrudException {
274 String url = baseRelationshipUrl;
276 // Try requests to ensure source and target exist in Champ
277 Vertex dbSource = getVertex(source.getId().get(), source.getType());
278 Vertex dbTarget = getVertex(target.getId().get(), target.getType());
280 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
281 properties.forEach(insertEdgeBuilder::property);
282 Edge insertEdge = insertEdgeBuilder.build();
284 String edgeJson = insertEdge.toJson(champGson);
285 OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
286 MediaType.APPLICATION_JSON_TYPE);
288 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
289 return Edge.fromJson(getResult.getResult());
291 // We didn't create an edge with the supplied type, so just throw an
293 throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
298 public Edge updateEdge(Edge edge) throws CrudException {
299 if (!edge.getId().isPresent()) {
300 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
302 String url = baseRelationshipUrl + "/" + edge.getId().get();
304 String edgeJson = edge.toJson(champGson);
305 OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
306 MediaType.APPLICATION_JSON_TYPE);
308 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
309 return Edge.fromJson(getResult.getResult());
311 // We didn't create an edge with the supplied type, so just throw an
313 throw new CrudException("Failed to update edge", Response.Status.fromStatusCode(getResult.getResultCode()));
318 public void deleteEdge(String id, String type) throws CrudException {
319 String url = baseRelationshipUrl + "/" + id;
320 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
322 if (getResult.getResultCode() != 200) {
323 // We didn't find an edge with the supplied type, so just throw an
325 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
330 public String openTransaction() {
331 String url = baseTransactionUrl;
333 OperationResult getResult = client.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
335 if (getResult.getResultCode() == 200) {
336 return getResult.getResult();
343 public void commitTransaction(String id) throws CrudException {
344 String url = baseTransactionUrl + "/" + id;
346 OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
347 MediaType.TEXT_PLAIN_TYPE);
349 if (getResult.getResultCode() != 200) {
350 throw new CrudException("Unable to commit transaction",
351 Response.Status.fromStatusCode(getResult.getResultCode()));
356 public void rollbackTransaction(String id) throws CrudException {
357 String url = baseTransactionUrl + "/" + id;
359 OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
360 MediaType.TEXT_PLAIN_TYPE);
362 if (getResult.getResultCode() != 200) {
363 throw new CrudException("Unable to rollback transaction",
364 Response.Status.fromStatusCode(getResult.getResultCode()));
369 public boolean transactionExists(String id) throws CrudException {
370 String url = baseTransactionUrl + "/" + id;
371 Map<String, List<String>> headers = new HashMap<>();
372 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
373 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
375 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
377 return getResult.getResultCode() == 200;
381 public Vertex addVertex(String type, Map<String, Object> properties, String txId) throws CrudException {
382 String url = baseObjectUrl + "?transactionId=" + txId;
384 // Add the aai_node_type so that AAI can read the data created by gizmo
385 // TODO: This probably shouldn't be here
386 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
388 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
389 properties.forEach(insertVertexBuilder::property);
390 Vertex insertVertex = insertVertexBuilder.build();
392 OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
393 MediaType.APPLICATION_JSON_TYPE);
395 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
396 return Vertex.fromJson(getResult.getResult());
398 // We didn't create a vertex with the supplied type, so just throw an
400 throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
405 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String txId)
406 throws CrudException {
407 String url = baseRelationshipUrl + "?transactionId=" + txId;
409 // Try requests to ensure source and target exist in Champ
410 Vertex dbSource = getVertex(source.getId().get(), source.getType(), txId);
411 Vertex dbTarget = getVertex(target.getId().get(), target.getType(), txId);
413 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
414 properties.forEach(insertEdgeBuilder::property);
415 Edge insertEdge = insertEdgeBuilder.build();
417 OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
418 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
420 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
421 return Edge.fromJson(getResult.getResult());
423 // We didn't create an edge with the supplied type, so just throw an
425 throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode()));
430 public Vertex updateVertex(String id, String type, Map<String, Object> properties, String txId) throws CrudException {
431 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
433 // Add the aai_node_type so that AAI can read the data created by gizmo
434 // TODO: This probably shouldn't be here
435 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
437 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
438 insertVertexBuilder.id(id);
439 properties.forEach(insertVertexBuilder::property);
440 Vertex insertVertex = insertVertexBuilder.build();
442 String payload = insertVertex.toJson(champGson);
443 OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
444 MediaType.APPLICATION_JSON_TYPE);
446 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
447 return Vertex.fromJson(getResult.getResult());
449 // We didn't create a vertex with the supplied type, so just throw an
451 throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
456 public void deleteVertex(String id, String type, String txId) throws CrudException {
457 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
458 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
460 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
461 // We didn't delete a vertex with the supplied id, so just throw an
463 throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
468 public Edge updateEdge(Edge edge, String txId) throws CrudException {
469 if (!edge.getId().isPresent()) {
470 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
472 String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
473 OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
474 MediaType.APPLICATION_JSON_TYPE);
476 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
477 return Edge.fromJson(getResult.getResult());
479 // We didn't create an edge with the supplied type, so just throw an
481 throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
482 Response.Status.fromStatusCode(getResult.getResultCode()));
487 public void deleteEdge(String id, String type, String txId) throws CrudException {
488 String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
489 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
491 if (getResult.getResultCode() != 200) {
492 // We didn't find an edge with the supplied type, so just throw an
494 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
499 public Edge getEdge(String id, String type, String txId) throws CrudException {
500 String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
501 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
503 if (getResult.getResultCode() == 200) {
504 Edge edge = Edge.fromJson(getResult.getResult());
506 if (!edge.getType().equalsIgnoreCase(type)) {
507 // We didn't find an edge with the supplied type, so just throw an
509 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
510 javax.ws.rs.core.Response.Status.NOT_FOUND);
514 // We didn't find an edge with the supplied id, so just throw an
516 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
520 public Vertex getVertex(String id, String type, String txId) throws CrudException {
521 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
522 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
524 if (getResult.getResultCode() == 200) {
525 Vertex vert = Vertex.fromJson(getResult.getResult());
527 if (!vert.getType().equalsIgnoreCase(type)) {
528 // We didn't find a vertex with the supplied type, so just throw an
530 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
531 javax.ws.rs.core.Response.Status.NOT_FOUND);
535 // We didn't find a vertex with the supplied id, so just throw an
537 throw new CrudException("No vertex with id " + id + " found in graph",
538 javax.ws.rs.core.Response.Status.NOT_FOUND);
542 // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
543 private List<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
544 List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
546 pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));
551 private Map<String, List<String>> createHeader() {
552 Map<String, List<String>> headers = new HashMap<>();
553 headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
554 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));