Port champ-microservice project restructure
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / ie / GraphMLImporterExporter.java
@@ -19,7 +19,7 @@
  * ============LICENSE_END============================================
  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  */
-package org.onap.aai.champ.ie;
+package org.onap.aai.champcore.ie;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -42,16 +42,18 @@ import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamWriter;
 
-import org.onap.aai.champ.ChampAPI;
-import org.onap.aai.champ.ChampGraph;
-import org.onap.aai.champ.exceptions.ChampMarshallingException;
-import org.onap.aai.champ.exceptions.ChampObjectNotExistsException;
-import org.onap.aai.champ.exceptions.ChampRelationshipNotExistsException;
-import org.onap.aai.champ.exceptions.ChampSchemaViolationException;
-import org.onap.aai.champ.exceptions.ChampUnmarshallingException;
-import org.onap.aai.champ.model.ChampObject;
-import org.onap.aai.champ.model.ChampObjectIndex;
-import org.onap.aai.champ.model.ChampRelationship;
+import org.onap.aai.champcore.ChampAPI;
+import org.onap.aai.champcore.ChampGraph;
+import org.onap.aai.champcore.ChampTransaction;
+import org.onap.aai.champcore.exceptions.ChampMarshallingException;
+import org.onap.aai.champcore.exceptions.ChampObjectNotExistsException;
+import org.onap.aai.champcore.exceptions.ChampRelationshipNotExistsException;
+import org.onap.aai.champcore.exceptions.ChampSchemaViolationException;
+import org.onap.aai.champcore.exceptions.ChampTransactionException;
+import org.onap.aai.champcore.exceptions.ChampUnmarshallingException;
+import org.onap.aai.champcore.model.ChampObject;
+import org.onap.aai.champcore.model.ChampObjectIndex;
+import org.onap.aai.champcore.model.ChampRelationship;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -183,25 +185,27 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                final NodeList data = edge.getChildNodes();
                final Object sourceKey = edgeAttributes.getNamedItem("source").getNodeValue();
                final Object targetKey = edgeAttributes.getNamedItem("target").getNodeValue();
-               final ChampObject sourceObject;
-               final ChampObject targetObject;
-
+               ChampObject sourceObject=null;
+               ChampObject targetObject=null;
+               
                try {
-                       final Optional<ChampObject> source = graph.queryObjects(Collections.singletonMap("importAssignedId", sourceKey)).findFirst();
-                       final Optional<ChampObject> target = graph.queryObjects(Collections.singletonMap("importAssignedId", targetKey)).findFirst();
+                       final Optional<ChampObject> source = graph.queryObjects(Collections.singletonMap("importAssignedId", sourceKey), Optional.empty()).findFirst();
+                       final Optional<ChampObject> target = graph.queryObjects(Collections.singletonMap("importAssignedId", targetKey), Optional.empty()).findFirst();
 
                        if (!source.isPresent()) {
                                sourceObject = graph.storeObject(ChampObject.create()
                                                                                                                .ofType("undefined")
                                                                                                                .withoutKey()
-                                                                                                               .build());
+                                                                                                               .build(),
+                                                                                                Optional.empty());
                        } else sourceObject = source.get();
        
                        if (!target.isPresent()) {
                                targetObject = graph.storeObject(ChampObject.create()
                                                                                                                .ofType("undefined")
                                                                                                                .withoutKey()
-                                                                                                               .build());
+                                                                                                               .build(),
+                                                                                                Optional.empty());
                        } else targetObject = target.get();
 
                } catch (ChampMarshallingException e) {
@@ -213,6 +217,8 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                } catch (ChampObjectNotExistsException e) {
                        LOGGER.error("Failed to update existing source/target ChampObject", e);
                        return;
+               } catch (ChampTransactionException e) {
+            LOGGER.error("Failed to commit or rollback transaction", e);
                }
 
                final ChampRelationship.Builder champRelBuilder = new ChampRelationship.Builder(sourceObject, targetObject, "undefined");
@@ -256,7 +262,7 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                final ChampRelationship relToStore = champRelBuilder.build();
 
                try {
-                       graph.storeRelationship(relToStore);
+                       graph.storeRelationship(relToStore, Optional.empty());
                } catch (ChampMarshallingException e) {
                        LOGGER.warn("Failed to marshall ChampObject to backend type", e);
                } catch (ChampSchemaViolationException e) {
@@ -267,7 +273,10 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                        LOGGER.error("Objects bound to relationship do not exist (should never happen)");
                } catch (ChampUnmarshallingException e) {
                        LOGGER.error("Failed to unmarshall ChampObject to backend type");
+               } catch (ChampTransactionException e) {
+                   LOGGER.error("Failed to commit or rollback transaction");
                }
+               
        }
 
        private void writeNode(ChampGraph graph, Node node, Map<String, Map<String, String>> nodePropertyDefinitions, Set<Map<String, String>> nodeDefaults) {
@@ -323,15 +332,17 @@ public class GraphMLImporterExporter implements Importer, Exporter {
 
                final ChampObject objectToStore = champObjBuilder.build();
 
-               try {
-                       graph.storeObject(objectToStore);
+               try {  
+                 graph.storeObject(objectToStore, Optional.empty());
                } catch (ChampMarshallingException e) {
                        LOGGER.warn("Failed to marshall ChampObject to backend type", e);
                } catch (ChampSchemaViolationException e) {
                        LOGGER.error("Failed to store object (schema violated): " + objectToStore, e);
                } catch (ChampObjectNotExistsException e) {
                        LOGGER.error("Failed to update existing ChampObject", e);
-               }
+               } catch (ChampTransactionException e) {
+          LOGGER.error("Failed to commit or rollback transaction");
+        }
        }
 
        @Override
@@ -354,7 +365,7 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                        final Map<String, GraphMLKey> edgeKeys = new HashMap<String, GraphMLKey> ();
                        final AtomicInteger elementCount = new AtomicInteger();
 
-                       graph.queryObjects(Collections.emptyMap()).forEach(object -> {
+                       graph.queryObjects(Collections.emptyMap(), Optional.empty()).forEach(object -> {
                                nodes.add(object);
 
                                for (Map.Entry<String, Object> property : object.getProperties().entrySet()) {
@@ -366,7 +377,7 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                                nodeKeys.put("type", new GraphMLKey("d" + elementCount.incrementAndGet(), "type", String.class));
                        });
 
-                       graph.queryRelationships(Collections.emptyMap()).forEach(relationship -> {
+                       graph.queryRelationships(Collections.emptyMap(), Optional.empty()).forEach(relationship -> {
                                edges.add(relationship);
 
                                for (Map.Entry<String, Object> property : relationship.getProperties().entrySet()) {
@@ -453,7 +464,7 @@ public class GraphMLImporterExporter implements Importer, Exporter {
                        writer.writeEndElement();
                        writer.writeEndDocument();
                        writer.flush();
-               } catch (XMLStreamException e) {
+               } catch (XMLStreamException | ChampTransactionException e) {
                        throw new RuntimeException(e);
                }
        }