X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fcrud%2Fdao%2Fchamp%2FChampDao.java;h=81980ccb36d4590006348ef4adc43e930ff61e20;hb=3bc6a702f2d3d8710c7aaa94cdc8c0ccf3deb759;hp=c8488ba058c5663005ad00f435e2c0dc8bba682e;hpb=b348af8ed2c4192f88169b37bf53fa25b8a7a681;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 c8488ba..81980cc 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -27,14 +27,13 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; 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.api.Logger; -import org.onap.aai.cl.eelf.LoggerFactory; import org.onap.aai.cl.mdc.MdcContext; import org.onap.aai.logging.LoggingContext; import org.onap.aai.restclient.client.OperationResult; @@ -64,8 +63,6 @@ public class ChampDao implements GraphDao { 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" protected static final Gson champGson = new GsonBuilder() @@ -132,7 +129,7 @@ public class ChampDao implements GraphDao { if (!vert.getType().equalsIgnoreCase(type)) { // We didn't find a vertex with the supplied type, so just throw an // exception. - throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", + throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } return getResult; @@ -144,26 +141,39 @@ public class ChampDao implements GraphDao { } @Override - 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())); - } + public List getVertexEdges(String id, Map queryParams, String txId) throws CrudException { + StringBuilder strBuild = new StringBuilder(baseObjectUrl); + strBuild.append("/relationships/"); + strBuild.append(id); + + Map queryParamsCopy = null; + if (queryParams != null) { + queryParamsCopy = new HashMap(queryParams); + } + else { + queryParamsCopy = new HashMap(); + } - OperationResult getResult = client.get(strBuild.toString(), createHeader(), MediaType.APPLICATION_JSON_TYPE); + if (txId != null) { + queryParamsCopy.put("transactionId", txId); + } - 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 createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); - } + if (!queryParamsCopy.isEmpty()) + { + strBuild.append("?"); + strBuild.append(URLEncodedUtils.format(convertToNameValuePair(queryParamsCopy), 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 createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); + } } @Override @@ -172,8 +182,8 @@ public class ChampDao implements GraphDao { } @Override - public OperationResult getVertices(String type, Map filter, HashSet properties, String version) throws CrudException { - filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + public OperationResult getVertices(String type, Map filter, Set properties, String version) throws CrudException { + filter.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); List queryParams = convertToNameValuePair(filter); queryParams.addAll(convertToNameValuePair("properties", properties)); @@ -209,7 +219,7 @@ public class ChampDao implements GraphDao { if (!edge.getType().equalsIgnoreCase(type)) { // We didn't find an edge with the supplied type, so just throw an // exception. - throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", + throw new CrudException("No edge with id " + id + " and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } return getResult; @@ -242,7 +252,7 @@ public class ChampDao implements GraphDao { // Add the aai_node_type so that AAI can read the data created by gizmo // TODO: This probably shouldn't be here - properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); Vertex.Builder insertVertexBuilder = new Vertex.Builder(type); properties.forEach(insertVertexBuilder::property); @@ -266,7 +276,7 @@ public class ChampDao implements GraphDao { // Add the aai_node_type so that AAI can read the data created by gizmo // TODO: This probably shouldn't be here - properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); Vertex.Builder insertVertexBuilder = new Vertex.Builder(type); insertVertexBuilder.id(id); @@ -346,7 +356,7 @@ public class ChampDao implements GraphDao { } @Override - public void deleteEdge(String id, String type) throws CrudException { + public void deleteEdge(String id) throws CrudException { String url = baseRelationshipUrl + "/" + id; OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); @@ -414,7 +424,7 @@ public class ChampDao implements GraphDao { // Add the aai_node_type so that AAI can read the data created by gizmo // TODO: This probably shouldn't be here - properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); Vertex.Builder insertVertexBuilder = new Vertex.Builder(type); properties.forEach(insertVertexBuilder::property); @@ -463,7 +473,7 @@ public class ChampDao implements GraphDao { // Add the aai_node_type so that AAI can read the data created by gizmo // TODO: This probably shouldn't be here - properties.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); + properties.put(org.onap.schema.validation.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type); Vertex.Builder insertVertexBuilder = new Vertex.Builder(type); insertVertexBuilder.id(id); @@ -515,7 +525,7 @@ public class ChampDao implements GraphDao { } @Override - public void deleteEdge(String id, String type, String txId) throws CrudException { + public void deleteEdge(String id, String txId) throws CrudException { String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId; OperationResult getResult = client.delete(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); @@ -527,19 +537,12 @@ public class ChampDao implements GraphDao { } @Override - public Edge getEdge(String id, String type, String txId) throws CrudException { + public Edge getEdge(String id, String txId) throws CrudException { String url = baseRelationshipUrl + "/" + id + "?transactionId=" + txId; OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { Edge edge = Edge.fromJson(getResult.getResult()); - - if (!edge.getType().equalsIgnoreCase(type)) { - // We didn't find an edge with the supplied type, so just throw an - // exception. - throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", - javax.ws.rs.core.Response.Status.NOT_FOUND); - } return edge; } else { // We didn't find an edge with the supplied id, so just throw an @@ -547,6 +550,20 @@ public class ChampDao implements GraphDao { throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } + + @Override + public Edge getEdge(String id) throws CrudException { + String url = baseRelationshipUrl + "/" + id; + OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); + + if (getResult.getResultCode() == 200) { + Edge edge = Edge.fromJson(getResult.getResult()); + return edge; + } else { + // We didn't find an edge with the supplied id, so just throw an exception. + 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 version, String txId) throws CrudException { String url = baseObjectUrl + "/" + id + "?transactionId=" + txId; @@ -558,7 +575,7 @@ public class ChampDao implements GraphDao { if (!vert.getType().equalsIgnoreCase(type)) { // We didn't find a vertex with the supplied type, so just throw an // exception. - throw new CrudException("No vertex with id " + id + "and type " + type + " found in graph", + throw new CrudException("No vertex with id " + id + " and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } return vert; @@ -579,10 +596,10 @@ public class ChampDao implements GraphDao { } // https://stackoverflow.com/questions/26942330/convert-mapstring-string-to-listnamevaluepair-is-this-the-most-efficient - private List convertToNameValuePair(String key, HashSet values) { + private List convertToNameValuePair(String k, Set values) { List nvpList = new ArrayList<>(values.size()); - values.forEach((value) -> nvpList.add(new BasicNameValuePair(key, value))); + values.forEach((v) -> nvpList.add(new BasicNameValuePair(k, v))); return nvpList; }