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.onap.aai.restclient.client.OperationResult;
45 import org.onap.aai.restclient.client.RestClient;
46 import org.onap.aai.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.HashSet;
54 import java.util.List;
56 import javax.ws.rs.core.MediaType;
57 import javax.ws.rs.core.Response;
59 public class ChampDao implements GraphDao {
60 protected RestClient client;
61 protected String baseObjectUrl;
62 protected String baseRelationshipUrl;
63 protected String baseTransactionUrl;
65 protected static final String HEADER_FROM_APP = "X-FromAppId";
66 protected static final String HEADER_TRANS_ID = "X-TransactionId";
67 protected static final String FROM_APP_NAME = "Gizmo";
68 protected static final String OBJECT_SUB_URL = "objects";
69 protected static final String RELATIONSHIP_SUB_URL = "relationships";
70 protected static final String TRANSACTION_SUB_URL = "transaction";
72 private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName());
74 // We use a custom vertex serializer for champ because it expects "key"
76 protected static final Gson champGson = new GsonBuilder()
77 .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
78 .registerTypeAdapter(Vertex.class, new ChampVertexSerializer())
79 .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create();
84 public ChampDao(String champUrl, String certPassword) {
86 client = new RestClient().authenticationMode(RestAuthenticationMode.SSL_CERT).validateServerHostname(false)
87 .validateServerCertChain(false).clientCertFile(CrudServiceConstants.CRD_CHAMP_AUTH_FILE)
88 .clientCertPassword(Password.deobfuscate(certPassword));
90 baseObjectUrl = champUrl + OBJECT_SUB_URL;
91 baseRelationshipUrl = champUrl + RELATIONSHIP_SUB_URL;
92 baseTransactionUrl = champUrl + TRANSACTION_SUB_URL;
93 } catch (Exception e) {
94 System.out.println("Error setting up Champ configuration");
100 public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) {
101 this.client = client;
102 this.baseObjectUrl = baseObjectUrl;
103 this.baseRelationshipUrl = baseRelationshipUrl;
104 this.baseTransactionUrl = baseTransactionUrl;
108 public Vertex getVertex(String id, String version) throws CrudException {
109 String url = baseObjectUrl + "/" + id;
110 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
112 if (getResult.getResultCode() == 200) {
113 return Vertex.fromJson(getResult.getResult(), version);
115 // We didn't find a vertex with the supplied id, so just throw an
117 throw new CrudException("No vertex with id " + id + " found in graph",
118 javax.ws.rs.core.Response.Status.NOT_FOUND);
123 public Vertex getVertex(String id, String type, String version) throws CrudException {
124 String url = baseObjectUrl + "/" + id;
125 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
127 if (getResult.getResultCode() == 200) {
128 Vertex vert = Vertex.fromJson(getResult.getResult(), version);
130 if (!vert.getType().equalsIgnoreCase(type)) {
131 // We didn't find a vertex with the supplied type, so just throw an
133 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
134 javax.ws.rs.core.Response.Status.NOT_FOUND);
138 // We didn't find a vertex with the supplied id, so just throw an
140 throw new CrudException("No vertex with id " + id + " found in graph",
141 javax.ws.rs.core.Response.Status.NOT_FOUND);
146 public List<Edge> getVertexEdges(String id) throws CrudException {
147 String url = baseObjectUrl + "/relationships/" + id;
149 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
151 if (getResult.getResultCode() == 200) {
152 return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
155 // We didn't find a vertex with the supplied id, so just throw an
157 throw new CrudException("No vertex with id " + id + " found in graph",
158 javax.ws.rs.core.Response.Status.NOT_FOUND);
163 public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException {
164 return getVertices(type, filter, new HashSet<String>(), version);
168 public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException {
169 filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
171 List<NameValuePair> queryParams = convertToNameValuePair(filter);
172 queryParams.addAll(convertToNameValuePair("properties", properties));
173 String url = baseObjectUrl + "/filter" + "?"
174 + URLEncodedUtils.format(queryParams, Charset.defaultCharset());
176 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
178 if (getResult.getResultCode() == 200) {
179 return Vertex.collectionFromJson(getResult.getResult(), version);
181 // We didn't find a vertex with the supplied id, so just throw an
183 throw new CrudException("No vertices found in graph for given filters",
184 javax.ws.rs.core.Response.Status.NOT_FOUND);
189 public Edge getEdge(String id, String type) throws CrudException {
190 String url = baseRelationshipUrl + "/" + id;
191 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
193 if (getResult.getResultCode() == 200) {
194 Edge edge = Edge.fromJson(getResult.getResult());
196 if (!edge.getType().equalsIgnoreCase(type)) {
197 // We didn't find an edge with the supplied type, so just throw an
199 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
200 javax.ws.rs.core.Response.Status.NOT_FOUND);
204 // We didn't find a edge with the supplied type, so just throw an
206 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
211 public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException {
212 String url = baseRelationshipUrl + "/filter" + "?"
213 + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset());
215 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
217 if (getResult.getResultCode() == 200) {
218 return champGson.fromJson(getResult.getResult(), new TypeToken<List<Edge>>() {
221 // We didn't find a vertex with the supplied id, so just throw an
223 throw new CrudException("No edges found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND);
228 public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException {
229 String url = baseObjectUrl;
231 // Add the aai_node_type so that AAI can read the data created by gizmo
232 // TODO: This probably shouldn't be here
233 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
235 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
236 properties.forEach(insertVertexBuilder::property);
237 Vertex insertVertex = insertVertexBuilder.build();
239 OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
240 MediaType.APPLICATION_JSON_TYPE);
242 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
243 return Vertex.fromJson(getResult.getResult(), version);
245 // We didn't create a vertex with the supplied type, so just throw an
247 throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode()));
252 public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException {
253 String url = baseObjectUrl + "/" + id;
255 // Add the aai_node_type so that AAI can read the data created by gizmo
256 // TODO: This probably shouldn't be here
257 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
259 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
260 insertVertexBuilder.id(id);
261 properties.forEach(insertVertexBuilder::property);
262 Vertex insertVertex = insertVertexBuilder.build();
264 String payload = insertVertex.toJson(champGson);
265 OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
266 MediaType.APPLICATION_JSON_TYPE);
268 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
269 return Vertex.fromJson(getResult.getResult(), version);
271 // We didn't create a vertex with the supplied type, so just throw an
273 throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
278 public void deleteVertex(String id, String type) throws CrudException {
279 String url = baseObjectUrl + "/" + id;
280 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
282 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
283 // We didn't delete a vertex with the supplied id, so just throw an
285 throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
290 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException {
291 String url = baseRelationshipUrl;
293 // Try requests to ensure source and target exist in Champ
294 Vertex dbSource = getVertex(source.getId().get(), source.getType(), version);
295 Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version);
297 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
298 properties.forEach(insertEdgeBuilder::property);
299 Edge insertEdge = insertEdgeBuilder.build();
301 String edgeJson = insertEdge.toJson(champGson);
302 OperationResult getResult = client.post(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
303 MediaType.APPLICATION_JSON_TYPE);
305 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
306 return Edge.fromJson(getResult.getResult());
308 // We didn't create an edge with the supplied type, so just throw an
310 throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
315 public Edge updateEdge(Edge edge) throws CrudException {
316 if (!edge.getId().isPresent()) {
317 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
319 String url = baseRelationshipUrl + "/" + edge.getId().get();
321 String edgeJson = edge.toJson(champGson);
322 OperationResult getResult = client.put(url, edgeJson, createHeader(), MediaType.APPLICATION_JSON_TYPE,
323 MediaType.APPLICATION_JSON_TYPE);
325 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
326 return Edge.fromJson(getResult.getResult());
328 // We didn't create an edge with the supplied type, so just throw an
330 throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
335 public void deleteEdge(String id, String type) throws CrudException {
336 String url = baseRelationshipUrl + "/" + id;
337 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
339 if (getResult.getResultCode() != 200) {
340 // We didn't find an edge with the supplied type, so just throw an
342 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
347 public String openTransaction() {
348 String url = baseTransactionUrl;
350 OperationResult getResult = client.post(url, "", createHeader(), MediaType.TEXT_PLAIN_TYPE, MediaType.TEXT_PLAIN_TYPE);
352 if (getResult.getResultCode() == 200) {
353 return getResult.getResult();
360 public void commitTransaction(String id) throws CrudException {
361 String url = baseTransactionUrl + "/" + id;
363 OperationResult getResult = client.put(url, "{\"method\": \"commit\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
364 MediaType.TEXT_PLAIN_TYPE);
366 if (getResult.getResultCode() != 200) {
367 throw new CrudException("Unable to commit transaction",
368 Response.Status.fromStatusCode(getResult.getResultCode()));
373 public void rollbackTransaction(String id) throws CrudException {
374 String url = baseTransactionUrl + "/" + id;
376 OperationResult getResult = client.put(url, "{\"method\": \"rollback\"}", createHeader(), MediaType.APPLICATION_JSON_TYPE,
377 MediaType.TEXT_PLAIN_TYPE);
379 if (getResult.getResultCode() != 200) {
380 throw new CrudException("Unable to rollback transaction",
381 Response.Status.fromStatusCode(getResult.getResultCode()));
386 public boolean transactionExists(String id) throws CrudException {
387 String url = baseTransactionUrl + "/" + id;
388 Map<String, List<String>> headers = new HashMap<>();
389 headers.put(HEADER_FROM_APP, Arrays.asList("Gizmo"));
390 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
392 OperationResult getResult = client.get(url, headers, MediaType.APPLICATION_JSON_TYPE);
394 return getResult.getResultCode() == 200;
398 public Vertex addVertex(String type, Map<String, Object> properties, String version, String txId) throws CrudException {
399 String url = baseObjectUrl + "?transactionId=" + txId;
401 // Add the aai_node_type so that AAI can read the data created by gizmo
402 // TODO: This probably shouldn't be here
403 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
405 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
406 properties.forEach(insertVertexBuilder::property);
407 Vertex insertVertex = insertVertexBuilder.build();
409 OperationResult getResult = client.post(url, insertVertex.toJson(), createHeader(), MediaType.APPLICATION_JSON_TYPE,
410 MediaType.APPLICATION_JSON_TYPE);
412 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
413 return Vertex.fromJson(getResult.getResult(), version);
415 // We didn't create a vertex with the supplied type, so just throw an
417 throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
422 public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version, String txId)
423 throws CrudException {
424 String url = baseRelationshipUrl + "?transactionId=" + txId;
426 // Try requests to ensure source and target exist in Champ
427 Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, txId);
428 Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, txId);
430 Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
431 properties.forEach(insertEdgeBuilder::property);
432 Edge insertEdge = insertEdgeBuilder.build();
434 OperationResult getResult = client.post(url, insertEdge.toJson(champGson), createHeader(),
435 MediaType.APPLICATION_JSON_TYPE, MediaType.APPLICATION_JSON_TYPE);
437 if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) {
438 return Edge.fromJson(getResult.getResult());
440 // We didn't create an edge with the supplied type, so just throw an
442 throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
447 public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version, String txId) throws CrudException {
448 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
450 // Add the aai_node_type so that AAI can read the data created by gizmo
451 // TODO: This probably shouldn't be here
452 properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
454 Vertex.Builder insertVertexBuilder = new Vertex.Builder(type);
455 insertVertexBuilder.id(id);
456 properties.forEach(insertVertexBuilder::property);
457 Vertex insertVertex = insertVertexBuilder.build();
459 String payload = insertVertex.toJson(champGson);
460 OperationResult getResult = client.put(url, payload, createHeader(), MediaType.APPLICATION_JSON_TYPE,
461 MediaType.APPLICATION_JSON_TYPE);
463 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
464 return Vertex.fromJson(getResult.getResult(), version);
466 // We didn't create a vertex with the supplied type, so just throw an
468 throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
473 public void deleteVertex(String id, String type, String txId) throws CrudException {
474 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
475 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
477 if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) {
478 // We didn't delete a vertex with the supplied id, so just throw an
480 throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode()));
485 public Edge updateEdge(Edge edge, String txId) throws CrudException {
486 if (!edge.getId().isPresent()) {
487 throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST);
489 String url = baseRelationshipUrl + "/" + edge.getId().get() + "?transactionId=" + txId;
490 OperationResult getResult = client.put(url, edge.toJson(champGson), createHeader(), MediaType.APPLICATION_JSON_TYPE,
491 MediaType.APPLICATION_JSON_TYPE);
493 if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) {
494 return Edge.fromJson(getResult.getResult());
496 // We didn't create an edge with the supplied type, so just throw an
498 throw new CrudException("Failed to update edge: " + getResult.getFailureCause(),
499 Response.Status.fromStatusCode(getResult.getResultCode()));
504 public void deleteEdge(String id, String type, String txId) throws CrudException {
505 String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
506 OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
508 if (getResult.getResultCode() != 200) {
509 // We didn't find an edge with the supplied type, so just throw an
511 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
516 public Edge getEdge(String id, String type, String txId) throws CrudException {
517 String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId;
518 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
520 if (getResult.getResultCode() == 200) {
521 Edge edge = Edge.fromJson(getResult.getResult());
523 if (!edge.getType().equalsIgnoreCase(type)) {
524 // We didn't find an edge with the supplied type, so just throw an
526 throw new CrudException("No edge with id " + id + "and type " + type + " found in graph",
527 javax.ws.rs.core.Response.Status.NOT_FOUND);
531 // We didn't find an edge with the supplied id, so just throw an
533 throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND);
537 public Vertex getVertex(String id, String type, String version, String txId) throws CrudException {
538 String url = baseObjectUrl + "/" + id + "?transactionId=" + txId;
539 OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
541 if (getResult.getResultCode() == 200) {
542 Vertex vert = Vertex.fromJson(getResult.getResult(), version);
544 if (!vert.getType().equalsIgnoreCase(type)) {
545 // We didn't find a vertex with the supplied type, so just throw an
547 throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph",
548 javax.ws.rs.core.Response.Status.NOT_FOUND);
552 // We didn't find a vertex with the supplied id, so just throw an
554 throw new CrudException("No vertex with id " + id + " found in graph",
555 javax.ws.rs.core.Response.Status.NOT_FOUND);
559 // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
560 private List<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
561 List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
563 pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));
568 // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient
569 private List<NameValuePair> convertToNameValuePair(String key, HashSet<String> values) {
570 List<NameValuePair> nvpList = new ArrayList<>(values.size());
572 values.forEach((value) -> nvpList.add(new BasicNameValuePair(key, value)));
577 private Map<String, List<String>> createHeader() {
578 Map<String, List<String>> headers = new HashMap<>();
579 headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME));
580 headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));