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=cd0e66ff51e7860b344809fbc7a6ea9ea004194c;hpb=74879a7b7a5607c89aef5fa9d64aca5ddea8e3b7;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 cd0e66f..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; @@ -114,15 +112,22 @@ 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 Vertex getVertex(String id, String type, String version) 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(), version); @@ -137,35 +142,40 @@ 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 { - return getVertices(type, filter, new HashSet()); + 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) throws CrudException { + 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); @@ -176,21 +186,26 @@ public class ChampDao implements GraphDao { 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()); @@ -204,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"); } } @@ -221,7 +236,7 @@ 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"); } } @@ -292,8 +307,8 @@ public class ChampDao implements GraphDao { String url = baseRelationshipUrl; // Try requests to ensure source and target exist in Champ - Vertex dbSource = getVertex(source.getId().get(), source.getType(), version); - Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version); + 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); @@ -340,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"); } } @@ -509,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"); } } @@ -531,7 +546,7 @@ 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"); } } @@ -552,13 +567,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 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()))); @@ -578,7 +592,18 @@ public class ChampDao implements GraphDao { 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; + } + }