/**
* ============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,
* 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;
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 {
}
}
+ 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, String version) throws CrudException {
String url = baseObjectUrl + "/" + id;
} 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<String, String> 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);
} 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<Edge> getVertexEdges(String id) throws CrudException {
- String url = baseObjectUrl + "/relationships/" + id;
-
- OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
+ public List<Edge> getVertexEdges(String id, Map<String, String> 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<List<Edge>>() {
}.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<Vertex> getVertices(String type, Map<String, Object> filter) throws CrudException {
- return getVertices(type, filter, new HashSet<String>());
+ public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException {
+ return getVertices(type, filter, new HashSet<String>(), version);
}
@Override
- public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties) throws CrudException {
+ public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException {
filter.put(org.onap.schema.OxmModelValidator.Metadata.NODE_TYPE.propertyName(), type);
List<NameValuePair> queryParams = convertToNameValuePair(filter);
OperationResult getResult = client.get(url, createHeader(), MediaType.APPLICATION_JSON_TYPE);
if (getResult.getResultCode() == 200) {
- return champGson.fromJson(getResult.getResult(), new TypeToken<List<Vertex>>() {
- }.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<String, String> 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());
} 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");
}
}
} 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");
}
}
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<String, String>());
+ Vertex dbTarget = getVertex(target.getId().get(), target.getType(), version, new HashMap<String, String>());
Edge.Builder insertEdgeBuilder = new Edge.Builder(type).source(dbSource).target(dbTarget);
properties.forEach(insertEdgeBuilder::property);
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");
}
}
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");
}
}
} 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");
}
}
} 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<NameValuePair> convertToNameValuePair(Map<String, Object> pairs) {
+ private List<NameValuePair> convertToNameValuePair(Map<String, ? super String> pairs) {
List<NameValuePair> nvpList = new ArrayList<>(pairs.size());
pairs.forEach((key, value) -> nvpList.add(new BasicNameValuePair(key, value.toString())));
headers.put(HEADER_TRANS_ID, Arrays.asList(MDC.get(LoggingContext.LoggingField.REQUEST_ID.toString())));
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;
+ }
+
}