aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/openecomp/crud/service/CrudGraphDataService.java')
-rw-r--r--src/main/java/org/openecomp/crud/service/CrudGraphDataService.java143
1 files changed, 135 insertions, 8 deletions
diff --git a/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java b/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java
index f38fe0f..c6b6a48 100644
--- a/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java
+++ b/src/main/java/org/openecomp/crud/service/CrudGraphDataService.java
@@ -23,11 +23,19 @@
*/
package org.openecomp.crud.service;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.ws.rs.core.Response.Status;
+
import org.onap.aaiutils.oxm.OxmModelLoader;
import org.openecomp.aai.champcore.ChampGraph;
import org.openecomp.crud.dao.GraphDao;
import org.openecomp.crud.dao.champ.ChampDao;
import org.openecomp.crud.entity.Edge;
+
import org.openecomp.crud.entity.Vertex;
import org.openecomp.crud.exception.CrudException;
import org.openecomp.crud.parser.CrudResponseBuilder;
@@ -35,32 +43,149 @@ import org.openecomp.schema.OxmModelValidator;
import org.openecomp.schema.RelationshipSchemaLoader;
import org.openecomp.schema.RelationshipSchemaValidator;
-import java.util.List;
-import java.util.Map;
+import com.google.gson.JsonElement;
public class CrudGraphDataService {
private GraphDao dao;
public CrudGraphDataService(ChampGraph graphImpl) throws CrudException {
+ this.dao = new ChampDao(graphImpl);
+
+ loadModels();
+ }
- this.dao = new ChampDao(graphImpl);
- //load the schemas
- try {
+ public CrudGraphDataService(GraphDao dao) throws CrudException {
+ this.dao = dao;
+
+ loadModels();
+ }
+
+ private void loadModels() throws CrudException {
+ //load the schemas
+ try {
OxmModelLoader.loadModels();
} catch (Exception e) {
throw new CrudException(e);
}
- RelationshipSchemaLoader.loadModels();
- }
-
+ RelationshipSchemaLoader.loadModels();
+ }
public String addVertex(String version, String type, VertexPayload payload) throws CrudException {
Vertex vertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, type,
payload.getProperties());
return addVertex(version, vertex);
}
+
+ public String addBulk(String version, BulkPayload payload) throws CrudException {
+ HashMap<String, Vertex> vertices = new HashMap<String, Vertex>();
+ HashMap<String, Edge> edges = new HashMap<String, Edge>();
+ String txId = dao.openTransaction();
+ try {
+ // Handle vertices
+ for (JsonElement v : payload.getObjects()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+
+ VertexPayload vertexPayload = VertexPayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ if (opr.getValue().getAsString().equalsIgnoreCase("add") || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
+ Vertex validatedVertex;
+ Vertex persistedVertex;
+ if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
+ validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(null, version, vertexPayload.getType(),
+ vertexPayload.getProperties());
+ // Call champDAO to add the vertex
+ persistedVertex = dao.addVertex(validatedVertex.getType(), validatedVertex.getProperties(), txId);
+ } else {
+ validatedVertex = OxmModelValidator.validateIncomingUpsertPayload(vertexPayload.getId(), version, vertexPayload.getType(),
+ vertexPayload.getProperties());
+ // Call champDAO to update the vertex
+ persistedVertex = dao.updateVertex(vertexPayload.getId(), validatedVertex.getType(), validatedVertex.getProperties(), txId);
+ }
+
+ Vertex outgoingVertex = OxmModelValidator.validateOutgoingPayload(version, persistedVertex);
+
+ vertices.put(item.getKey(), outgoingVertex);
+
+ } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
+ dao.deleteVertex(vertexPayload.getId(), OxmModelValidator.resolveCollectionType(version, vertexPayload.getType()), txId);
+ }
+
+ }
+ // Handle Edges
+ for (JsonElement v : payload.getRelationships()) {
+ List<Map.Entry<String, JsonElement>> entries = new ArrayList<Map.Entry<String, JsonElement>>(v.getAsJsonObject().entrySet());
+
+ if (entries.size() != 2) {
+ throw new CrudException("", Status.BAD_REQUEST);
+ }
+ Map.Entry<String, JsonElement> opr = entries.get(0);
+ Map.Entry<String, JsonElement> item = entries.get(1);
+
+ EdgePayload edgePayload = EdgePayload.fromJson(item.getValue().getAsJsonObject().toString());
+
+ if (opr.getValue().getAsString().equalsIgnoreCase("add") || opr.getValue().getAsString().equalsIgnoreCase("modify")) {
+ Edge validatedEdge;
+ Edge persistedEdge;
+ if (opr.getValue().getAsString().equalsIgnoreCase("add")) {
+ // Fix the source/detination
+ if (edgePayload.getSource().startsWith("$")) {
+ Vertex source = vertices.get(edgePayload.getSource().substring(1));
+ if (source == null) {
+ throw new CrudException("Not able to find vertex: " + edgePayload.getSource().substring(1), Status.INTERNAL_SERVER_ERROR);
+ }
+ edgePayload.setSource("services/inventory/" + version + "/" + source.getType() + "/" + source.getId().get());
+ }
+ if (edgePayload.getTarget().startsWith("$")) {
+ Vertex target = vertices.get(edgePayload.getTarget().substring(1));
+ if (target == null) {
+ throw new CrudException("Not able to find vertex: " + edgePayload.getTarget().substring(1), Status.INTERNAL_SERVER_ERROR);
+ }
+ edgePayload.setTarget("services/inventory/" + version + "/" + target.getType() + "/" + target.getId().get());
+ }
+ validatedEdge = RelationshipSchemaValidator.validateIncomingAddPayload(version, edgePayload.getType(), edgePayload);
+ persistedEdge = dao.addEdge(validatedEdge.getType(), validatedEdge.getSource(), validatedEdge.getTarget(),
+ validatedEdge.getProperties(), txId);
+ } else {
+ Edge edge = dao.getEdge(edgePayload.getId(), edgePayload.getType(), txId);
+ validatedEdge = RelationshipSchemaValidator.validateIncomingUpdatePayload(edge, version, edgePayload);
+ persistedEdge = dao.updateEdge(edge, txId);
+ }
+
+ Edge outgoingEdge = RelationshipSchemaValidator.validateOutgoingPayload(version, persistedEdge);
+
+ edges.put(item.getKey(), outgoingEdge);
+
+ } else if (opr.getValue().getAsString().equalsIgnoreCase("delete")) {
+ RelationshipSchemaValidator.validateType(version, edgePayload.getType());
+ dao.deleteEdge(edgePayload.getId(), edgePayload.getType(), txId);
+ }
+
+ }
+ // close champ TX
+ dao.commitTransaction(txId);
+ } catch (CrudException ex) {
+ dao.rollbackTransaction(txId);
+ throw ex;
+ } catch (Exception ex) {
+ dao.rollbackTransaction(txId);
+ throw ex;
+ } finally {
+ if (dao.transactionExists(txId)) {
+ dao.rollbackTransaction(txId);
+ }
+ }
+
+
+ return CrudResponseBuilder.buildUpsertBulkResponse(vertices, edges, version, payload);
+ }
private String addVertex(String version, Vertex vertex) throws CrudException {
Vertex addedVertex = dao.addVertex(vertex.getType(), vertex.getProperties());
@@ -182,5 +307,7 @@ public class CrudGraphDataService {
type, filter));
return CrudResponseBuilder.buildGetVerticesResponse(items, version);
}
+
+
}