AAI-1523 Batch reformat aai-core
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / introspection / sideeffect / PrivateEdge.java
index 5b719e3..f4f0bfa 100644 (file)
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
+
 package org.onap.aai.introspection.sideeffect;
 
 import com.google.common.collect.Multimap;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.*;
+import java.util.Map.Entry;
+
+import javax.ws.rs.core.MultivaluedMap;
+
 import org.apache.tinkerpop.gremlin.structure.Edge;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
 import org.onap.aai.config.SpringContextAware;
 import org.onap.aai.db.props.AAIProperties;
 import org.onap.aai.edges.EdgeIngestor;
+import org.onap.aai.edges.EdgeRule;
 import org.onap.aai.edges.EdgeRuleQuery;
+import org.onap.aai.edges.enums.EdgeType;
 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
 import org.onap.aai.exceptions.AAIException;
@@ -36,20 +48,10 @@ import org.onap.aai.parsers.query.QueryParser;
 import org.onap.aai.restcore.util.URITools;
 import org.onap.aai.schema.enums.PropertyMetadata;
 import org.onap.aai.serialization.db.DBSerializer;
-import org.onap.aai.edges.EdgeRule;
-import org.onap.aai.edges.enums.EdgeType;
 import org.onap.aai.serialization.db.EdgeSerializer;
 import org.onap.aai.serialization.db.exceptions.EdgeMultiplicityException;
 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
 
-import javax.ws.rs.core.MultivaluedMap;
-import java.io.UnsupportedEncodingException;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.*;
-import java.util.Map.Entry;
-
-
 public class PrivateEdge extends SideEffect {
 
     public PrivateEdge(Introspector obj, Vertex self, TransactionalGraphEngine dbEngine, DBSerializer serializer) {
@@ -57,16 +59,18 @@ public class PrivateEdge extends SideEffect {
     }
 
     @Override
-    protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
+    protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException,
+            UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
         if (completeUri.isPresent()) {
             process(completeUri, entry);
         } else {
             // Check if the vertex self has the template keys or the db aliased keys
             // If it does check if the self vertex has a edge to that model
             // If it does, then remove the edge since the update happened and doesn't have required props anymore
-                       // "service-design-and-creation/models/model/{model-invariant-id}/model-vers/model-ver/{model-version-id}"
+            // "service-design-and-creation/models/model/{model-invariant-id}/model-vers/model-ver/{model-version-id}"
             // If the vertex does have
-               Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY, obj.getVersion());
+            Loader loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(ModelType.MOXY,
+                    obj.getVersion());
             Introspector introspector = loader.introspectorFromName(obj.getDbName());
             List<Vertex> vertices = new ArrayList<>();
             vertices.add(self);
@@ -76,8 +80,9 @@ public class PrivateEdge extends SideEffect {
         }
     }
 
-    private void process(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
-        if(completeUri.isPresent()){
+    private void process(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException,
+            UnsupportedEncodingException, AAIException, EdgeRuleNotFoundException, AmbiguousRuleChoiceException {
+        if (completeUri.isPresent()) {
             URI uri = new URI(completeUri.get());
             MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
             QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
@@ -88,34 +93,39 @@ public class PrivateEdge extends SideEffect {
 
                 if (otherVProperty.isPresent()) {
 
-                    EdgeRuleQuery edgeQuery = new EdgeRuleQuery.Builder(obj.getName(), otherVProperty.value().toString()).edgeType(EdgeType.COUSIN).setPrivate(true).build();
+                    EdgeRuleQuery edgeQuery =
+                            new EdgeRuleQuery.Builder(obj.getName(), otherVProperty.value().toString())
+                                    .edgeType(EdgeType.COUSIN).setPrivate(true).build();
                     EdgeIngestor edgeIngestor = serializer.getEdgeIngestor();
                     EdgeSerializer edgeSerializer = serializer.getEdgeSeriailizer();
 
                     Multimap<String, EdgeRule> edgeRulesMap = edgeIngestor.getRules(edgeQuery);
 
                     if (edgeRulesMap.isEmpty()) {
-                        String message = String.format("Unable to find edge between %s and %s", obj.getName(), otherVProperty.value().toString());
+                        String message = String.format("Unable to find edge between %s and %s", obj.getName(),
+                                otherVProperty.value().toString());
                         throw new AAIException("AAI_6127", message);
                     } else if (edgeRulesMap.size() > 1) {
-                        String message = String.format("Found multiple edges between %s and %s", obj.getName(), otherVProperty.value().toString());
+                        String message = String.format("Found multiple edges between %s and %s", obj.getName(),
+                                otherVProperty.value().toString());
                         throw new EdgeMultiplicityException(message);
                     }
 
-
                     for (Entry<String, EdgeRule> edgeEntry : edgeRulesMap.entries()) {
                         EdgeRule edgeRule = edgeIngestor.getRule(edgeQuery);
                         Iterator<Edge> edges = self.edges(edgeRule.getDirection(), edgeRule.getLabel().toString());
-                        if(edges.hasNext()){
+                        if (edges.hasNext()) {
                             Edge edge = edges.next();
                             EdgeStatus status = checkStatus(obj, self);
-                            switch(status){
+                            switch (status) {
                                 case CREATED:
-                                    edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel());
+                                    edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
+                                            otherVertex, edgeRule.getLabel());
                                     break;
                                 case MODIFIED:
                                     edge.remove();
-                                    edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel());
+                                    edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
+                                            otherVertex, edgeRule.getLabel());
                                     break;
                                 case REMOVED:
                                     edge.remove();
@@ -124,7 +134,8 @@ public class PrivateEdge extends SideEffect {
                                     break;
                             }
                         } else {
-                            edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self, otherVertex, edgeRule.getLabel());
+                            edgeSerializer.addPrivateEdge(this.dbEngine.asAdmin().getTraversalSource(), self,
+                                    otherVertex, edgeRule.getLabel());
                         }
                     }
                 }
@@ -132,49 +143,47 @@ public class PrivateEdge extends SideEffect {
                 if (results.isEmpty()) {
                     throw new AAIException("AAI_6114", "object located at " + uri + " not found");
                 } else if (results.size() > 1) {
-                    throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
+                    throw new AAIMultiplePropertiesException(
+                            "multiple values of " + entry.getKey() + " found when searching " + uri);
                 }
             }
         }
     }
 
     public enum EdgeStatus {
-        CREATED,
-        REMOVED,
-        MODIFIED,
-        UNCHANGED
+        CREATED, REMOVED, MODIFIED, UNCHANGED
     }
 
     private EdgeStatus checkStatus(Introspector obj, Vertex self) {
 
-        for(String key : templateKeys){
+        for (String key : templateKeys) {
             String currentObjValue = obj.getValue(key);
             Map<PropertyMetadata, String> map = obj.getPropertyMetadata(key);
             String oldVertexValue = null;
 
-            if(map.containsKey(PropertyMetadata.DB_ALIAS)){
+            if (map.containsKey(PropertyMetadata.DB_ALIAS)) {
                 oldVertexValue = self.<String>property(key + AAIProperties.DB_ALIAS_SUFFIX).orElse(null);
             } else {
                 oldVertexValue = self.<String>property(key).orElse(null);
             }
 
-            if(currentObjValue == null && oldVertexValue == null){
+            if (currentObjValue == null && oldVertexValue == null) {
                 continue;
             }
 
-            if(currentObjValue == null){
-                if(oldVertexValue != null){
+            if (currentObjValue == null) {
+                if (oldVertexValue != null) {
                     return EdgeStatus.REMOVED;
                 }
             }
 
-            if(oldVertexValue == null){
-                if(currentObjValue != null){
+            if (oldVertexValue == null) {
+                if (currentObjValue != null) {
                     return EdgeStatus.CREATED;
                 }
             }
 
-            if(!oldVertexValue.equals(currentObjValue)){
+            if (!oldVertexValue.equals(currentObjValue)) {
                 return EdgeStatus.MODIFIED;
             }
         }