X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fonap%2Fcrud%2Fdao%2Fchamp%2FChampDao.java;h=7bd4754779ca1cabfbd20f71e53e8b04156cfbc2;hb=d10a218c76633374f083f7a2802c198e93a6abae;hp=95a736927d18d4d64a6235986b8174080db7d4ec;hpb=b6ec637f5ee03c573855431e65fcb6ab0f321851;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 95a7369..7bd4754 100644 --- a/src/main/java/org/onap/crud/dao/champ/ChampDao.java +++ b/src/main/java/org/onap/crud/dao/champ/ChampDao.java @@ -20,38 +20,36 @@ */ package org.onap.crud.dao.champ; -import net.dongliu.gson.GsonJava8TypeAdapterFactory; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.reflect.TypeToken; - +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; 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.logging.LoggingContext; 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; +import org.onap.aai.restclient.client.RestClient; +import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.onap.crud.dao.GraphDao; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; import org.onap.crud.exception.CrudException; import org.onap.crud.util.CrudServiceConstants; -import org.onap.aai.restclient.client.OperationResult; -import org.onap.aai.restclient.client.RestClient; -import org.onap.aai.restclient.enums.RestAuthenticationMode; import org.slf4j.MDC; - -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; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import net.dongliu.gson.GsonJava8TypeAdapterFactory; public class ChampDao implements GraphDao { protected RestClient client; @@ -74,7 +72,7 @@ public class ChampDao implements GraphDao { .registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory()) .registerTypeAdapter(Vertex.class, new ChampVertexSerializer()) .registerTypeAdapter(Edge.class, new ChampEdgeSerializer()).create(); - + public ChampDao() { } @@ -111,12 +109,12 @@ public class ChampDao implements GraphDao { } 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"); + 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, Map queryParams) throws CrudException { + public OperationResult getVertex(String id, String type, String version, Map queryParams) throws CrudException { StringBuilder strBuild = new StringBuilder(baseObjectUrl); strBuild.append("/"); strBuild.append(id); @@ -134,14 +132,14 @@ 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; + return getResult; } 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"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @@ -157,25 +155,25 @@ public class ChampDao implements GraphDao { } 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"); + 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, String version) throws CrudException { + public OperationResult 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); + public OperationResult getVertices(String type, Map filter, HashSet 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)); @@ -185,16 +183,16 @@ public class ChampDao implements GraphDao { OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == 200) { - return Vertex.collectionFromJson(getResult.getResult(), version); + return getResult; } 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 vertices found in graph for given filters"); + 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, Map queryParams) throws CrudException { + public OperationResult getEdge(String id, String type, Map queryParams) throws CrudException { StringBuilder strBuild = new StringBuilder(baseRelationshipUrl); strBuild.append("/"); strBuild.append(id); @@ -204,7 +202,7 @@ public class ChampDao implements GraphDao { 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()); @@ -214,38 +212,37 @@ public class ChampDao implements GraphDao { throw new CrudException("No edge with id " + id + "and type " + type + " found in graph", javax.ws.rs.core.Response.Status.NOT_FOUND); } - return edge; + return getResult; } else { // We didn't find a edge with the supplied type, so just throw an // exception. - throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @Override - public List getEdges(String type, Map filter) throws CrudException { + public OperationResult getEdges(String type, Map filter) throws CrudException { String url = baseRelationshipUrl + "/filter" + "?" + URLEncodedUtils.format(convertToNameValuePair(filter), 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 getResult; } 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 edges found in graph for given filters"); + 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, String version) throws CrudException { + public OperationResult 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 // 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); @@ -255,21 +252,21 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Vertex.fromJson(getResult.getResult(), version); + return getResult; } 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 Vertex updateVertex(String id, String type, Map properties, String version) throws CrudException { + public OperationResult 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 // 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); @@ -281,7 +278,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Vertex.fromJson(getResult.getResult(), version); + return getResult; } else { // We didn't create a vertex with the supplied type, so just throw an // exception. @@ -302,12 +299,14 @@ public class ChampDao implements GraphDao { } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map properties, String version) throws CrudException { + public OperationResult 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(), version, new HashMap()); - Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap()); + OperationResult dbSourceOpResult = getVertex(source.getId().get(), source.getType(), version, new HashMap()); + Vertex dbSource = Vertex.fromJson(dbSourceOpResult.getResult(), version); + OperationResult dbTargetOpResult = getVertex(target.getId().get(), target.getType(), version, new HashMap()); + Vertex dbTarget = Vertex.fromJson(dbTargetOpResult.getResult(), version); Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget); properties.forEach(insertEdgeBuilder::property); @@ -318,7 +317,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.CREATED.getStatusCode()) { - return Edge.fromJson(getResult.getResult()); + return getResult; } else { // We didn't create an edge with the supplied type, so just throw an // exception. @@ -327,7 +326,7 @@ public class ChampDao implements GraphDao { } @Override - public Edge updateEdge(Edge edge) throws CrudException { + public OperationResult updateEdge(Edge edge) throws CrudException { if (!edge.getId().isPresent()) { throw new CrudException("Unable to identify edge: " + edge.toString(), Response.Status.BAD_REQUEST); } @@ -338,7 +337,7 @@ public class ChampDao implements GraphDao { MediaType.APPLICATION_JSON_TYPE); if (getResult.getResultCode() == Response.Status.OK.getStatusCode()) { - return Edge.fromJson(getResult.getResult()); + return getResult; } else { // We didn't create an edge with the supplied type, so just throw an // exception. @@ -354,7 +353,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 createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -415,7 +414,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); @@ -464,7 +463,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); @@ -523,7 +522,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 createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -545,7 +544,7 @@ public class ChampDao implements GraphDao { } 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"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No edge with id " + id + " found in graph"); } } @@ -566,7 +565,7 @@ public class ChampDao implements GraphDao { } 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"); + throw createErrorException(getResult, javax.ws.rs.core.Response.Status.NOT_FOUND, "No vertex with id " + id + " found in graph"); } } @@ -580,22 +579,22 @@ 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, HashSet 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; } - + 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) + + private CrudException createErrorException(OperationResult result, javax.ws.rs.core.Response.Status defaultErrorCode , String defaultErrorMsg) { CrudException ce = null; if(result != null)