X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fcrud%2Fdao%2Fchamp%2FChampDao.java;h=18d49466243acb6e591653d4615a973cd9baac36;hb=ff29ea71d508a2a8b20bfe22aeeca5af6a6e94be;hp=adf2a3a10d40609bf4e770abc6bc75f7210468df;hpb=be2d880b8a316ccbd7135f935a5005835b9e7aad;p=aai%2Fgizmo.git diff --git a/src/main/java/org/onap/crud/dao/champ/ChampDao.java b/src/main/java/org/onap/crud/dao/champ/ChampDao.java index adf2a3a..18d4946 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -1,16 +1,15 @@ /** * ============LICENSE_START======================================================= - * Gizmo + * org.onap.aai * ================================================================================ - * Copyright © 2017 AT&T Intellectual Property. - * Copyright © 2017 Amdocs - * All rights reserved. + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -18,8 +17,6 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - * - * ECOMP is a trademark and service mark of AT&T Intellectual Property. */ package org.onap.crud.dao.champ; @@ -33,6 +30,7 @@ import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.message.BasicNameValuePair; import org.eclipse.jetty.util.security.Password; +import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.logging.LoggingContext; import org.onap.aai.cl.api.Logger; import org.onap.aai.cl.eelf.LoggerFactory; @@ -50,32 +48,36 @@ import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; public class ChampDao implements GraphDao { - private RestClient client; - private String baseObjectUrl; - private String baseRelationshipUrl; - private String baseTransactionUrl; - - private static final String HEADER_FROM_APP = "X-FromAppId"; - private static final String HEADER_TRANS_ID = "X-TransactionId"; - private static final String FROM_APP_NAME = "Gizmo"; - private static final String OBJECT_SUB_URL = "objects"; - private static final String RELATIONSHIP_SUB_URL = "relationships"; - private static final String TRANSACTION_SUB_URL = "transaction"; + protected RestClient client; + protected String baseObjectUrl; + protected String baseRelationshipUrl; + protected String baseTransactionUrl; + + protected static final String HEADER_FROM_APP = "X-FromAppId"; + protected static final String HEADER_TRANS_ID = "X-TransactionId"; + protected static final String FROM_APP_NAME = "Gizmo"; + protected static final String OBJECT_SUB_URL = "objects"; + protected static final String RELATIONSHIP_SUB_URL = "relationships"; + protected static final String TRANSACTION_SUB_URL = "transaction"; private Logger logger = LoggerFactory.getInstance().getLogger(ChampDao.class.getName()); // We use a custom vertex serializer for champ because it expects "key" // instead of "id" - private static final Gson champGson = new GsonBuilder() + protected static final Gson champGson = new GsonBuilder() .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()) .registerTypeAdapter(Vertex.class, new ChampVertexSerializer()) .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create(); + + public ChampDao() { + } public ChampDao(String champUrl, String certPassword) { try { @@ -93,28 +95,42 @@ public class ChampDao implements GraphDao { } } + public ChampDao(RestClient client, String baseObjectUrl, String baseRelationshipUrl, String baseTransactionUrl) { + this.client = client; + this.baseObjectUrl = baseObjectUrl; + this.baseRelationshipUrl = baseRelationshipUrl; + this.baseTransactionUrl = baseTransactionUrl; + } + @Override - public Vertex getVertex(String id) throws CrudException { + public Vertex getVertex(String id, String version) throws CrudException { String url = baseObjectUrl + "/" + id; OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return Vertex.fromJson(getResult.getResult()); + return Vertex.fromJson(getResult.getResult(), version); } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No vertex with id " + id + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @Override - public Vertex getVertex(String id, String type) throws CrudException { - String url = baseObjectUrl + "/" + id; - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + public Vertex getVertex(String id, String type, String version, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - Vertex vert = Vertex.fromJson(getResult.getResult()); + Vertex vert = Vertex.fromJson(getResult.getResult(), version); if (!vert.getType().equalsIgnoreCase(type)) { // We didn't find a vertex with the supplied type, so just throw an @@ -126,53 +142,70 @@ public class ChampDao implements GraphDao { } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No vertex with id " + id + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @Override - public List getVertexEdges(String id) throws CrudException { - String url = baseObjectUrl + "/relationships/" + id; - - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + public List getVertexEdges(String id, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/relationships/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (getResult.getResultCode() == 200) { return champGson.fromJson(getResult.getResult(), new TypeToken>() { }.getType()); } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No vertex with id " + id + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @Override - public List getVertices(String type, Map filter) throws CrudException { + public List getVertices(String type, Map filter, String version) throws CrudException { + return getVertices(type, filter, new HashSet(), version); + } + + @Override + public List getVertices(String type, Map filter, HashSet properties, String version) throws CrudException { filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + List queryParams = convertToNameValuePair(filter); + queryParams.addAll(convertToNameValuePair("properties", properties)); String url = baseObjectUrl + "/filter" + "?" - + URLEncodedUtils.format(convertToNameValuePair(filter), Charset.defaultCharset()); + + URLEncodedUtils.format(queryParams, Charset.defaultCharset()); OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return champGson.fromJson(getResult.getResult(), new TypeToken>() { - }.getType()); + return Vertex.collectionFromJson(getResult.getResult(), version); } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No vertices found in graph for given filters", - javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertices found in graph for given filters"); } } @Override - public Edge getEdge(String id, String type) throws CrudException { - String url = baseRelationshipUrl + "/" + id; - OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); - + public Edge getEdge(String id, String type, Map queryParams) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseRelationshipUrl); + strBuild.append("/"); + strBuild.append(id); + if(queryParams != null && !queryParams.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParams), Charset.defaultCharset())); + } + OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (getResult.getResultCode() == 200) { Edge edge = Edge.fromJson(getResult.getResult()); @@ -186,7 +219,7 @@ public class ChampDao implements GraphDao { } else { // We didn't find a edge with the supplied type, so just throw an // exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -203,12 +236,12 @@ public class ChampDao implements GraphDao { } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No edges found in graph for given filters", javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edges found in graph for given filters"); } } @Override - public Vertex addVertex(String type, Map properties) throws CrudException { + public Vertex addVertex(String type, Map properties, String version) throws CrudException { String url = baseObjectUrl; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -223,7 +256,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Vertex.fromJson(getResult.getResult()); + return Vertex.fromJson(getResult.getResult(), version); } else { // We didn't create a vertex with the supplied type, so just throw an // exception. @@ -232,7 +265,7 @@ public class ChampDao implements GraphDao { } @Override - public Vertex updateVertex(String id, String type, Map properties) throws CrudException { + public Vertex updateVertex(String id, String type, Map properties, String version) throws CrudException { String url = baseObjectUrl + "/" + id; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -249,11 +282,11 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Vertex.fromJson(getResult.getResult()); + return Vertex.fromJson(getResult.getResult(), version); } else { // We didn't create a vertex with the supplied type, so just throw an // exception. - throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -265,17 +298,17 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) { // We didn't delete a vertex with the supplied id, so just throw an // exception. - throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map properties) throws CrudException { + public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String version) throws CrudException { String url = baseRelationshipUrl; // Try requests to ensure source and target exist in Champ - Vertex dbSource = getVertex(source.getId().get(), source.getType()); - Vertex dbTarget = getVertex(target.getId().get(), target.getType()); + Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, new HashMap()); + Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap()); Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget); properties.forEach(insertEdgeBuilder::property); @@ -290,7 +323,7 @@ public class ChampDao implements GraphDao { } else { // We didn't create an edge with the supplied type, so just throw an // exception. - throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -310,7 +343,7 @@ public class ChampDao implements GraphDao { } else { // We didn't create an edge with the supplied type, so just throw an // exception. - throw new CrudException("Failed to update edge", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to update edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -322,7 +355,7 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != 200) { // We didn't find an edge with the supplied type, so just throw an // exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -378,7 +411,7 @@ public class ChampDao implements GraphDao { } @Override - public Vertex addVertex(String type, Map properties, String txId) throws CrudException { + public Vertex addVertex(String type, Map properties, String version, String txId) throws CrudException { String url = baseObjectUrl + "?transactionId=" + txId; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -393,22 +426,22 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Vertex.fromJson(getResult.getResult()); + return Vertex.fromJson(getResult.getResult(), version); } else { // We didn't create a vertex with the supplied type, so just throw an // exception. - throw new CrudException("Failed to create vertex", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to create vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String txId) + public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String version, String txId) throws CrudException { String url = baseRelationshipUrl + "?transactionId=" + txId; // Try requests to ensure source and target exist in Champ - Vertex dbSource = getVertex(source.getId().get(), source.getType(), txId); - Vertex dbTarget = getVertex(target.getId().get(), target.getType(), txId); + Vertex dbSource = getVertex(source.getId().get(), source.getType(), version, txId); + Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, txId); Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget); properties.forEach(insertEdgeBuilder::property); @@ -422,12 +455,12 @@ public class ChampDao implements GraphDao { } else { // We didn't create an edge with the supplied type, so just throw an // exception. - throw new CrudException("Failed to create edge", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to create edge: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @Override - public Vertex updateVertex(String id, String type, Map properties, String txId) throws CrudException { + public Vertex updateVertex(String id, String type, Map properties, String version, String txId) throws CrudException { String url = baseObjectUrl + "/" + id + "?transactionId=" + txId; // Add the aai_node_type so that AAI can read the data created by gizmo @@ -444,11 +477,11 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Vertex.fromJson(getResult.getResult()); + return Vertex.fromJson(getResult.getResult(), version); } else { // We didn't create a vertex with the supplied type, so just throw an // exception. - throw new CrudException("Failed to update vertex", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to update vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -460,7 +493,7 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != Response.Status.OK.getStatusCode()) { // We didn't delete a vertex with the supplied id, so just throw an // exception. - throw new CrudException("Failed to delete vertex", Response.Status.fromStatusCode(getResult.getResultCode())); + throw new CrudException("Failed to delete vertex: " + getResult.getFailureCause(), Response.Status.fromStatusCode(getResult.getResultCode())); } } @@ -491,7 +524,7 @@ public class ChampDao implements GraphDao { if (getResult.getResultCode() != 200) { // We didn't find an edge with the supplied type, so just throw an // exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -513,16 +546,16 @@ public class ChampDao implements GraphDao { } else { // We didn't find an edge with the supplied id, so just throw an // exception. - throw new CrudException("No edge with id " + id + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } - public Vertex getVertex(String id, String type, String txId) throws CrudException { + public Vertex getVertex(String id, String type, String version, String txId) throws CrudException { String url = baseObjectUrl + "/" + id + "?transactionId=" + txId; OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - Vertex vert = Vertex.fromJson(getResult.getResult()); + Vertex vert = Vertex.fromJson(getResult.getResult(), version); if (!vert.getType().equalsIgnoreCase(type)) { // We didn't find a vertex with the supplied type, so just throw an @@ -534,24 +567,43 @@ public class ChampDao implements GraphDao { } else { // We didn't find a vertex with the supplied id, so just throw an // exception. - throw new CrudException("No vertex with id " + id + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient - private List convertToNameValuePair(Map pairs) { + private List convertToNameValuePair(Map pairs) { List nvpList = new ArrayList<>(pairs.size()); pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString()))); return nvpList; } + + // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient + private List convertToNameValuePair(String key, HashSet values) { + List nvpList = new ArrayList<>(values.size()); + + values.forEach((value) -> nvpList.add(new BasicNameValuePair(key, value))); + + return nvpList; + } private Map> createHeader() { Map> headers = new HashMap<>(); headers.put(HEADER_FROM_APP, Arrays.asList(FROM_APP_NAME)); - headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString()))); + headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(MdcContext.MDC_REQUEST_ID))); return headers; } + + private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg) + { + CrudException ce = null; + if(result != null) + ce = new CrudException(result.getFailureCause(), Response.Status.fromStatusCode(result.getResultCode())); + else + ce = new CrudException(defaultErrorMsg, defaultErrorCode); + return ce; + } + }