Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / DefaultDerivedFromOperation.java
index c207965..ddf7620 100644 (file)
@@ -7,9 +7,9 @@
  * 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
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * limitations under the License.
  * ============LICENSE_END=========================================================
  */
-
 package org.openecomp.sdc.be.model.operations.impl;
 
 import fj.data.Either;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
 import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 import org.openecomp.sdc.be.dao.graph.datatype.GraphEdge;
@@ -38,9 +43,6 @@ import org.openecomp.sdc.common.log.wrappers.Logger;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
-import java.util.*;
-import java.util.function.Function;
-
 @Component
 public class DefaultDerivedFromOperation implements DerivedFromOperation {
 
@@ -53,80 +55,66 @@ public class DefaultDerivedFromOperation implements DerivedFromOperation {
     }
 
     @Override
-    public Either<GraphRelation, StorageOperationStatus> addDerivedFromRelation(String parentUniqueId, String derivedFromUniqueId, NodeTypeEnum nodeType) {
+    public Either<GraphRelation, StorageOperationStatus> addDerivedFromRelation(String parentUniqueId, String derivedFromUniqueId,
+                                                                                NodeTypeEnum nodeType) {
         UniqueIdData from = new UniqueIdData(nodeType, parentUniqueId);
         UniqueIdData to = new UniqueIdData(nodeType, derivedFromUniqueId);
-        return janusGraphGenericDao.createRelation(from, to, GraphEdgeLabels.DERIVED_FROM, null)
-                .right()
-                .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
+        return janusGraphGenericDao.createRelation(from, to, GraphEdgeLabels.DERIVED_FROM, null).right()
+            .map(DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
     }
 
     @Override
     public <T extends GraphNode> Either<T, StorageOperationStatus> getDerivedFromChild(String uniqueId, NodeTypeEnum nodeType, Class<T> clazz) {
         log.debug("#getDerivedFromChild - fetching derived from entity for node type {} with id {}", nodeType, uniqueId);
-        return janusGraphGenericDao
-            .getChild(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.DERIVED_FROM, nodeType, clazz)
-                .bimap(Pair::getKey,
-                       DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
+        return janusGraphGenericDao.getChild(UniqueIdBuilder.getKeyByNodeType(nodeType), uniqueId, GraphEdgeLabels.DERIVED_FROM, nodeType, clazz)
+            .bimap(Pair::getKey, DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
     }
 
     @Override
     public StorageOperationStatus removeDerivedFromRelation(String uniqueId, String derivedFromUniqueId, NodeTypeEnum nodeType) {
         UniqueIdData from = new UniqueIdData(nodeType, uniqueId);
         UniqueIdData to = new UniqueIdData(nodeType, derivedFromUniqueId);
-        return isDerivedFromExists(from, to)
-                .either(isRelationExist -> isRelationExist ? deleteDerivedFrom(from, to) : StorageOperationStatus.OK,
-                        DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
-
-
+        return isDerivedFromExists(from, to).either(isRelationExist -> isRelationExist ? deleteDerivedFrom(from, to) : StorageOperationStatus.OK,
+            DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
     }
 
-    private StorageOperationStatus deleteDerivedFrom(UniqueIdData from,  UniqueIdData to) {
+    private StorageOperationStatus deleteDerivedFrom(UniqueIdData from, UniqueIdData to) {
         return janusGraphGenericDao.deleteRelation(from, to, GraphEdgeLabels.DERIVED_FROM)
-                .either(deletedRelation -> StorageOperationStatus.OK,
-                        DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
+            .either(deletedRelation -> StorageOperationStatus.OK, DaoStatusConverter::convertJanusGraphStatusToStorageStatus);
     }
 
     private Either<Boolean, JanusGraphOperationStatus> isDerivedFromExists(UniqueIdData from, UniqueIdData to) {
         return janusGraphGenericDao.isRelationExist(from, to, GraphEdgeLabels.DERIVED_FROM);
     }
-    
+
     @Override
-    public <T extends GraphNode> Either<Boolean, StorageOperationStatus> isTypeDerivedFrom(String childCandidateType, String parentCandidateType, String currentChildType, 
-                                                                                                    NodeTypeEnum nodeType, Class<T> clazz, Function<T, String> typeProvider) {
+    public <T extends GraphNode> Either<Boolean, StorageOperationStatus> isTypeDerivedFrom(String childCandidateType, String parentCandidateType,
+                                                                                           String currentChildType, NodeTypeEnum nodeType,
+                                                                                           Class<T> clazz, Function<T, String> typeProvider) {
         Map<String, Object> propertiesToMatch = new HashMap<>();
         propertiesToMatch.put(GraphPropertiesDictionary.TYPE.getProperty(), childCandidateType);
-        
-        Either<List<T>, JanusGraphOperationStatus> getResponse = janusGraphGenericDao
-            .getByCriteria(nodeType, propertiesToMatch, clazz);
+        Either<List<T>, JanusGraphOperationStatus> getResponse = janusGraphGenericDao.getByCriteria(nodeType, propertiesToMatch, clazz);
         if (getResponse.isRight()) {
             JanusGraphOperationStatus janusGraphOperationStatus = getResponse.right().value();
-            log.debug("Couldn't fetch type {}, error: {}", childCandidateType,
-                janusGraphOperationStatus);
-            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
-                janusGraphOperationStatus));
+            log.debug("Couldn't fetch type {}, error: {}", childCandidateType, janusGraphOperationStatus);
+            return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphOperationStatus));
         }
         T node = getResponse.left().value().get(0);
         String childUniqueId = node.getUniqueId();
         String childType = typeProvider.apply(node);
-        
         Set<String> travelledTypes = new HashSet<>();
         if (currentChildType != null) {
             travelledTypes.add(currentChildType);
         }
-        
         do {
             travelledTypes.add(childType);
             Either<List<ImmutablePair<T, GraphEdge>>, JanusGraphOperationStatus> childrenNodes = janusGraphGenericDao
-                .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), childUniqueId, GraphEdgeLabels.DERIVED_FROM,
-                    nodeType, clazz);
+                .getChildrenNodes(UniqueIdBuilder.getKeyByNodeType(nodeType), childUniqueId, GraphEdgeLabels.DERIVED_FROM, nodeType, clazz);
             if (childrenNodes.isRight()) {
                 if (childrenNodes.right().value() != JanusGraphOperationStatus.NOT_FOUND) {
                     JanusGraphOperationStatus janusGraphOperationStatus = getResponse.right().value();
-                    log.debug("Couldn't fetch derived from node for type {}, error: {}", childCandidateType,
-                        janusGraphOperationStatus);
-                    return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(
-                        janusGraphOperationStatus));
+                    log.debug("Couldn't fetch derived from node for type {}, error: {}", childCandidateType, janusGraphOperationStatus);
+                    return Either.right(DaoStatusConverter.convertJanusGraphStatusToStorageStatus(janusGraphOperationStatus));
                 } else {
                     log.debug("Derived from node is not found for type {} - this is OK for root capability.", childCandidateType);
                     return Either.left(false);
@@ -142,44 +130,41 @@ public class DefaultDerivedFromOperation implements DerivedFromOperation {
             childType = derivedFromType;
         } while (!travelledTypes.contains(childType));
         // this stop condition should never be used, if we use it, we have an
+
         // illegal cycle in graph - "derived from" hierarchy cannot be cycled.
+
         // It's here just to avoid infinite loop in case we have such cycle.
         log.error("Detected a cycle of \"derived from\" edges starting at type node {}", childType);
         return Either.right(StorageOperationStatus.GENERAL_ERROR);
     }
-    
-    
-    
+
     @Override
     public <T extends GraphNode> StorageOperationStatus isUpdateParentAllowed(String oldTypeParent, String newTypeParent, String childType,
                                                                               NodeTypeEnum nodeType, Class<T> clazz,
                                                                               Function<T, String> typeProvider) {
         StorageOperationStatus status;
         if (oldTypeParent != null) {
-            
-            Either<Boolean, StorageOperationStatus> result = isTypeDerivedFrom(newTypeParent, oldTypeParent, childType, nodeType, clazz, typeProvider);
+            Either<Boolean, StorageOperationStatus> result = isTypeDerivedFrom(newTypeParent, oldTypeParent, childType, nodeType, clazz,
+                typeProvider);
             if (result.isRight()) {
-                log.debug("#isUpdateParentAllowed - failed to detect that new parent {} is derived from the current parent {}",  newTypeParent, oldTypeParent);
+                log.debug("#isUpdateParentAllowed - failed to detect that new parent {} is derived from the current parent {}", newTypeParent,
+                    oldTypeParent);
                 status = result.right().value();
-            }
-            else {
+            } else {
                 if (result.left().value()) {
-                    log.debug("#isUpdateParentAllowed - update is allowed since new parent {} is derived from the current parent {}",  newTypeParent, oldTypeParent);
+                    log.debug("#isUpdateParentAllowed - update is allowed since new parent {} is derived from the current parent {}", newTypeParent,
+                        oldTypeParent);
                     status = StorageOperationStatus.OK;
-                }
-                else {
-                    log.debug("#isUpdateParentAllowed - update is not allowed since new parent {} is not derived from the current parent {}",  newTypeParent, oldTypeParent);
+                } else {
+                    log.debug("#isUpdateParentAllowed - update is not allowed since new parent {} is not derived from the current parent {}",
+                        newTypeParent, oldTypeParent);
                     status = StorageOperationStatus.CANNOT_UPDATE_EXISTING_ENTITY;
                 }
             }
-                        
-        }
-        else {
-            log.debug("#isUpdateParentAllowed - the update is allowed since the parent still has been not set." );
+        } else {
+            log.debug("#isUpdateParentAllowed - the update is allowed since the parent still has been not set.");
             status = StorageOperationStatus.OK;
         }
-        
         return status;
     }
-
 }