Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / operations / UpgradeOperation.java
@@ -1,20 +1,20 @@
-package org.openecomp.sdc.be.model.jsontitan.operations;
+package org.openecomp.sdc.be.model.jsonjanusgraph.operations;
 
-import com.thinkaurelius.titan.core.TitanVertex;
+import org.janusgraph.core.JanusGraphVertex;
 import fj.data.Either;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Edge;
+import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
 import org.openecomp.sdc.be.dao.jsongraph.types.EdgeLabelEnum;
 import org.openecomp.sdc.be.dao.jsongraph.types.EdgePropertyEnum;
 import org.openecomp.sdc.be.dao.jsongraph.types.JsonParseFlagEnum;
-import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
 import org.openecomp.sdc.be.datatypes.elements.ComponentInstanceDataDefinition;
 import org.openecomp.sdc.be.datatypes.elements.CompositionDataDefinition;
 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
 import org.openecomp.sdc.be.model.ComponentDependency;
-import org.openecomp.sdc.be.model.jsontitan.enums.JsonConstantKeysEnum;
+import org.openecomp.sdc.be.model.jsonjanusgraph.enums.JsonConstantKeysEnum;
 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
 import org.openecomp.sdc.be.model.operations.impl.DaoStatusConverter;
 import org.openecomp.sdc.common.log.wrappers.Logger;
@@ -29,10 +29,10 @@ public class UpgradeOperation extends BaseOperation {
     private static final Logger log = Logger.getLogger(UpgradeOperation.class.getName());
 
     public Either<List<ComponentDependency>, StorageOperationStatus> getComponentDependencies(String componentId) {
-        Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(componentId);
+        Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(componentId);
         if (vertexById.isRight()) {
             log.debug("Failed to fetch vertex with id {} error {}", componentId, vertexById.right().value());
-            return Either.right(DaoStatusConverter.convertTitanStatusToStorageStatus(vertexById.right().value()));
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(vertexById.right().value()));
         }
         List<ComponentDependency> dependencies = new ArrayList<>();
 
@@ -44,24 +44,26 @@ public class UpgradeOperation extends BaseOperation {
         }
 
         GraphVertex vertexToStart = vertex;
-        Function<GraphVertex, Either<GraphVertex, TitanOperationStatus>> getNextElement = vertexP -> titanDao.getParentVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
+        Function<GraphVertex, Either<GraphVertex, JanusGraphOperationStatus>> getNextElement = vertexP -> janusGraphDao
+            .getParentVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
         status = handleVersionChain(componentId, dependencies, vertex, getNextElement);
         if (status != StorageOperationStatus.OK) {
             return Either.right(status);
         }
         vertex = vertexToStart;
-        getNextElement = vertexP -> titanDao.getChildVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
+        getNextElement = vertexP -> janusGraphDao
+            .getChildVertex(vertexP, EdgeLabelEnum.VERSION, JsonParseFlagEnum.ParseAll);
         status = handleVersionChain(componentId, dependencies, vertex, getNextElement);
 
         return status == StorageOperationStatus.OK ? Either.left(dependencies) : Either.right(status);
     }
 
-    private StorageOperationStatus handleVersionChain(String componentId, List<ComponentDependency> dependencies, GraphVertex vertexToStart, Function<GraphVertex, Either<GraphVertex, TitanOperationStatus>> getNextElement) {
+    private StorageOperationStatus handleVersionChain(String componentId, List<ComponentDependency> dependencies, GraphVertex vertexToStart, Function<GraphVertex, Either<GraphVertex, JanusGraphOperationStatus>> getNextElement) {
 
         StorageOperationStatus status;
         boolean nextInChain = true;
         GraphVertex vertex = vertexToStart;
-        Either<GraphVertex, TitanOperationStatus> nextInChainV;
+        Either<GraphVertex, JanusGraphOperationStatus> nextInChainV;
         while (nextInChain) {
             nextInChainV = getNextElement.apply(vertex);
             if (nextInChainV.isRight()) {
@@ -103,10 +105,11 @@ public class UpgradeOperation extends BaseOperation {
     }
 
     private StorageOperationStatus fillDependenciesByLabel(String componentId, GraphVertex vertex, ComponentDependency dependency, EdgeLabelEnum label) {
-        Either<List<GraphVertex>, TitanOperationStatus> parentVertecies = titanDao.getParentVertecies(vertex, label, JsonParseFlagEnum.ParseAll);
-        if (parentVertecies.isRight() && parentVertecies.right().value() != TitanOperationStatus.NOT_FOUND) {
+        Either<List<GraphVertex>, JanusGraphOperationStatus> parentVertecies = janusGraphDao
+            .getParentVertecies(vertex, label, JsonParseFlagEnum.ParseAll);
+        if (parentVertecies.isRight() && parentVertecies.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
             log.debug("Failed to fetch parent verticies by label INSTANCE_OF for vertex with id {} error {}", componentId, parentVertecies.right().value());
-            return DaoStatusConverter.convertTitanStatusToStorageStatus(parentVertecies.right().value());
+            return DaoStatusConverter.convertJanusGraphStatusToStorageStatus(parentVertecies.right().value());
         }
         if (parentVertecies.isLeft()) {
             List<ComponentDependency> existIn = new ArrayList<>( );
@@ -119,8 +122,8 @@ public class UpgradeOperation extends BaseOperation {
     private void handleHighestVersion(GraphVertex vertexOrigin, EdgeLabelEnum label, List<ComponentDependency> exisIn, GraphVertex containerVertex) {
         Boolean isHighest = (Boolean) containerVertex.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
         if ( isHighest && needToAddToDepenedency(containerVertex) ) {  
-            TitanVertex titanVertex = containerVertex.getVertex();
-            Iterator<Edge> edges = titanVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
+            JanusGraphVertex janusGraphVertex = containerVertex.getVertex();
+            Iterator<Edge> edges = janusGraphVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
             //verify that it is a last version - highest by version number
             if ( edges == null || !edges.hasNext() ){
                 ComponentDependency container = fillDataFromVertex(containerVertex, vertexOrigin.getUniqueId(), label);
@@ -137,14 +140,15 @@ public class UpgradeOperation extends BaseOperation {
     }
 
     private boolean findAllottedChain(GraphVertex vertex, ComponentDependency container) {
-        Either<List<GraphVertex>, TitanOperationStatus> parentVertecies = titanDao.getParentVertecies(vertex, EdgeLabelEnum.INSTANCE_OF, JsonParseFlagEnum.ParseAll);
+        Either<List<GraphVertex>, JanusGraphOperationStatus> parentVertecies = janusGraphDao
+            .getParentVertecies(vertex, EdgeLabelEnum.INSTANCE_OF, JsonParseFlagEnum.ParseAll);
         if (parentVertecies.isLeft()) {
             List<ComponentDependency> existIn = new ArrayList<>();
             parentVertecies.left().value().forEach(v -> {
                 Boolean isHighest = (Boolean) v.getMetadataProperty(GraphPropertyEnum.IS_HIGHEST_VERSION);
                 if ( isHighest && needToAddToDepenedency(v) ) {
-                   TitanVertex titanVertex = v.getVertex();
-                   Iterator<Edge> edges = titanVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
+                   JanusGraphVertex janusGraphVertex = v.getVertex();
+                   Iterator<Edge> edges = janusGraphVertex.edges(Direction.OUT, EdgeLabelEnum.VERSION.name());
                    //verify that it is a last version - highest by version number
                    if ( edges == null || !edges.hasNext() ){
                        ComponentDependency parentContainer = fillDataFromVertex(v, vertex.getUniqueId(), EdgeLabelEnum.INSTANCE_OF);
@@ -178,14 +182,15 @@ public class UpgradeOperation extends BaseOperation {
     private void findInstanceNames(GraphVertex v, String originId, EdgeLabelEnum edgeLabel, ComponentDependency container) {
         Map<String, CompositionDataDefinition> jsonComposition = (Map<String, CompositionDataDefinition>) v.getJson();
         CompositionDataDefinition compositionDataDefinition = jsonComposition.get(JsonConstantKeysEnum.COMPOSITION.getValue());
-        TitanVertex vertex = v.getVertex();
+        JanusGraphVertex vertex = v.getVertex();
         Iterator<Edge> edges = vertex.edges(Direction.OUT, edgeLabel.name());
         while (edges != null && edges.hasNext()) {
             Edge edge = edges.next();
-            TitanVertex inVertex = (TitanVertex) edge.inVertex();
-            String id = (String) titanDao.getProperty(inVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
+            JanusGraphVertex inVertex = (JanusGraphVertex) edge.inVertex();
+            String id = (String) janusGraphDao.getProperty(inVertex, GraphPropertyEnum.UNIQUE_ID.getProperty());
             if (id.equals(originId)) {
-                List<String> instanceOnEdge = (List<String>) titanDao.getProperty(edge, EdgePropertyEnum.INSTANCES);
+                List<String> instanceOnEdge = (List<String>) janusGraphDao
+                    .getProperty(edge, EdgePropertyEnum.INSTANCES);
                 Map<String, ComponentInstanceDataDefinition> componentInstances = compositionDataDefinition.getComponentInstances();
 
                 if (componentInstances != null) {
@@ -205,17 +210,18 @@ public class UpgradeOperation extends BaseOperation {
     }
 
     public List<String> getInstanceIdFromAllottedEdge(String resourceId, String serviceInvariantUUID) {
-      Either<GraphVertex, TitanOperationStatus> vertexById = titanDao.getVertexById(resourceId);
+      Either<GraphVertex, JanusGraphOperationStatus> vertexById = janusGraphDao.getVertexById(resourceId);
       if ( vertexById.isLeft() ){
           GraphVertex vertexG = vertexById.left().value();
-          TitanVertex vertex = vertexG.getVertex();
+          JanusGraphVertex vertex = vertexG.getVertex();
           Iterator<Edge> edges = vertex.edges(Direction.OUT, EdgeLabelEnum.ALLOTTED_OF.name());
           while ( edges != null && edges.hasNext() ){
               Edge edge = edges.next();
-              TitanVertex inVertex = (TitanVertex)edge.inVertex();
-              String vertexInInvUUID = (String) titanDao.getProperty(inVertex, GraphPropertyEnum.INVARIANT_UUID.getProperty());
+              JanusGraphVertex inVertex = (JanusGraphVertex)edge.inVertex();
+              String vertexInInvUUID = (String) janusGraphDao
+                  .getProperty(inVertex, GraphPropertyEnum.INVARIANT_UUID.getProperty());
               if ( vertexInInvUUID.equals(serviceInvariantUUID) ){
-                  return (List<String>) titanDao.getProperty(edge, EdgePropertyEnum.INSTANCES) ;
+                  return (List<String>) janusGraphDao.getProperty(edge, EdgePropertyEnum.INSTANCES) ;
               }
           }
       }