DAO sonar fixes 48/73348/2
authorSonsino, Ofir (os0695) <os0695@intl.att.com>
Thu, 22 Nov 2018 08:13:46 +0000 (10:13 +0200)
committerSonsino, Ofir (os0695) <os0695@intl.att.com>
Thu, 22 Nov 2018 09:22:22 +0000 (11:22 +0200)
Change-Id: I453fdb5f2aa1488adbf0664a624a98b239d4a927
Issue-ID: SDC-1954
Signed-off-by: Sonsino, Ofir (os0695) <os0695@intl.att.com>
catalog-be/src/main/java/org/openecomp/sdc/be/externalapi/servlet/ArtifactExternalServlet.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/graph/datatype/GraphNode.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/impl/AuditingDao.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/impl/ESCatalogDAO.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/impl/ESTimeBasedDao.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/impl/Neo4jUsersDAO.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/TitanDao.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/titan/TitanGenericDao.java

index f3cf594..c53e343 100644 (file)
@@ -65,7 +65,6 @@ import javax.ws.rs.core.Response;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.EnumMap;
 import java.util.HashMap;
 import java.util.Map;
 /**
@@ -80,6 +79,8 @@ import java.util.Map;
 @Singleton
 public class ArtifactExternalServlet extends AbstractValidationsServlet {
 
+    private static final String FAILED_TO_UPDATE_ARTIFACT = "failed to update artifact";
+
     @Context
     private HttpServletRequest request;
 
@@ -146,7 +147,7 @@ public class ArtifactExternalServlet extends AbstractValidationsServlet {
                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnInterfaceOperationByResourceUUID(data, request, componentType, uuid, artifactUUID, operationUUID,
                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
                 if (uploadArtifactEither.isRight()) {
-                    log.debug("failed to update artifact");
+                    log.debug(FAILED_TO_UPDATE_ARTIFACT);
                     responseFormat = uploadArtifactEither.right().value();
                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
                 } else {
@@ -452,7 +453,7 @@ public class ArtifactExternalServlet extends AbstractValidationsServlet {
                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnComponentByUUID(data, request, componentType, uuid, artifactUUID,
                         resourceCommonInfo, artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
                 if (uploadArtifactEither.isRight()) {
-                    log.debug("failed to update artifact");
+                    log.debug(FAILED_TO_UPDATE_ARTIFACT);
                     responseFormat = uploadArtifactEither.right().value();
                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
                 } else {
@@ -552,7 +553,7 @@ public class ArtifactExternalServlet extends AbstractValidationsServlet {
                 Either<ArtifactDefinition, ResponseFormat> uploadArtifactEither = artifactsLogic.updateArtifactOnRiByUUID(data, request, componentType, uuid, resourceInstanceName, artifactUUID,
                          artifactsLogic.new ArtifactOperationInfo(true, false, ArtifactOperationEnum.UPDATE));
                 if (uploadArtifactEither.isRight()) {
-                    log.debug("failed to update artifact");
+                    log.debug(FAILED_TO_UPDATE_ARTIFACT);
                     responseFormat = uploadArtifactEither.right().value();
                     responseWrapper.setInnerElement(buildErrorResponse(responseFormat));
                 } else {
index 5e1efbf..35ab30c 100644 (file)
@@ -34,16 +34,16 @@ public abstract class GraphNode extends GraphElement {
 
        private NodeTypeEnum label;
 
+    protected GraphNode(NodeTypeEnum label) {
+        super(GraphElementTypeEnum.Node);
+
+        this.label = label;
+    }
+       
        protected Gson getGson() {
                return gson;
        }
 
-       protected GraphNode(NodeTypeEnum label) {
-               super(GraphElementTypeEnum.Node);
-
-               this.label = label;
-       }
-
        public String getLabel() {
                return label.getName();
        }
index 6a67a99..26b821a 100644 (file)
@@ -42,7 +42,9 @@ import java.util.Map.Entry;
 @Component("auditingDao")
 public class AuditingDao extends ESTimeBasedDao {
 
-       private static final String SERVICE_INSTANCE_ID_FIELD = AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID
+       private static final String FAILED_TO_GET_DISTRIBUTION_STATUSES_FOR_ACTION = "failed to get  distribution statuses for action {}";
+    private static final String GET_SERVICE_DISTRIBUTION_STATUSES_LIST = "Get Service DistributionStatuses List";
+    private static final String SERVICE_INSTANCE_ID_FIELD = AuditingFieldsKey.AUDIT_SERVICE_INSTANCE_ID
                        .getDisplayName(); // "serviceInstanceId";
        private static final String STATUS_FIELD = AuditingFieldsKey.AUDIT_STATUS.getDisplayName(); // "status";
        private static final String ACTION_FIELD = AuditingFieldsKey.AUDIT_ACTION.getDisplayName(); // "action";
@@ -130,8 +132,8 @@ public class AuditingDao extends ESTimeBasedDao {
                        remainingElements = customFindEvent(AuditingTypesConstants.RESOURCE_ADMIN_EVENT_TYPE,
                                        remainingElementQueryBuilder, null);
                } catch (Exception e) {
-                       BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
-                       log.debug("failed to get  distribution statuses for action {}",
+                       BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
+                       log.debug(FAILED_TO_GET_DISTRIBUTION_STATUSES_FOR_ACTION,
                                        AuditingActionEnum.DISTRIBUTION_STATE_CHANGE_REQUEST.getName(), e);
                        return Either.right(ActionStatus.GENERAL_ERROR);
                }
@@ -147,7 +149,7 @@ public class AuditingDao extends ESTimeBasedDao {
                        dResultElements = customFindEvent(AuditingTypesConstants.DISTRIBUTION_DEPLOY_EVENT_TYPE,
                                        remainingElementQueryBuilder, null);
                } catch (Exception e) {
-                       BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
+                       BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
                        log.debug("failed to get distribution statuses for action {}",
                                        AuditingActionEnum.DISTRIBUTION_DEPLOY.getName(), e);
                        return Either.right(ActionStatus.GENERAL_ERROR);
@@ -164,7 +166,7 @@ public class AuditingDao extends ESTimeBasedDao {
                        dNotifyElements = customFindEvent(AuditingTypesConstants.DISTRIBUTION_NOTIFICATION_EVENT_TYPE,
                                        remainingElementQueryBuilder, null);
                } catch (Exception e) {
-                       BeEcompErrorManager.getInstance().logBeDaoSystemError("Get Service DistributionStatuses List");
+                       BeEcompErrorManager.getInstance().logBeDaoSystemError(GET_SERVICE_DISTRIBUTION_STATUSES_LIST);
                        log.debug("failed to get distribution statuses for action {}",
                                        AuditingActionEnum.DISTRIBUTION_NOTIFY.getName(), e);
                        return Either.right(ActionStatus.GENERAL_ERROR);
index c3dc886..8d8e0ba 100644 (file)
@@ -48,6 +48,9 @@ public class ESCatalogDAO extends ESGenericSearchDAO implements ICatalogDAO {
 
        private static Logger log = Logger.getLogger(ESCatalogDAO.class.getName());
 
+       // Index Checking Variables
+    private boolean initCompleted = false;
+       
        //TODO use LoggerMetric instead
        private static Logger healthCheckLogger = Logger.getLogger("elasticsearch.healthcheck");
 
@@ -122,11 +125,8 @@ public class ESCatalogDAO extends ESGenericSearchDAO implements ICatalogDAO {
                initCompleted = true;
        }
 
-       // Index Checking Variables
-       private boolean initCompleted = false;
-
        @Override
-       public void writeArtifact(ESArtifactData artifactData) throws ResourceDAOException {
+       public void writeArtifact(ESArtifactData artifactData) {
                try {
                        saveResourceData(artifactData);
                } catch (Exception e) {
index dc944d0..556f8c1 100644 (file)
@@ -54,7 +54,12 @@ public abstract class ESTimeBasedDao {
        private Map<String, String> indexPrefix2CreationPeriod;
 
        private ConfigurationManager configurationManager;
+    
+       @Resource(name = "elasticsearch-client")
+    private ElasticSearchClient esClient;
 
+    protected final Map<String, Class<?>> typesToClasses = new HashMap<>();
+    
        protected ESTimeBasedDao() {
                gson = new GsonBuilder().setPrettyPrinting().create();
                configurationManager = ConfigurationManager.getConfigurationManager();
@@ -65,11 +70,6 @@ public abstract class ESTimeBasedDao {
                this.configurationManager = configurationManager;
        }
 
-       @Resource(name = "elasticsearch-client")
-       private ElasticSearchClient esClient;
-
-       protected final Map<String, Class<?>> typesToClasses = new HashMap<>();
-
        public abstract String getIndexPrefix();
 
        public ActionStatus write(String typeName, ESTimeBasedEvent data) {
@@ -240,8 +240,7 @@ public abstract class ESTimeBasedDao {
                }
        }
 
-       public List<ESTimeBasedEvent> customFindEvent(String typeName, QueryBuilder query, SortBuilder sortBuilder)
-                       throws JSONException {
+       public List<ESTimeBasedEvent> customFindEvent(String typeName, QueryBuilder query, SortBuilder sortBuilder) {
                List<ESTimeBasedEvent> results = doCustomFindForEvent(typeName, query, sortBuilder, MAX_SEARCH_SIZE);
                if (results == null) {
                        results = new ArrayList<>();
@@ -271,11 +270,8 @@ public abstract class ESTimeBasedDao {
        }
 
        private boolean somethingFound(final SearchResponse searchResponse) {
-               if (searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null
-                               || searchResponse.getHits().getHits().length == 0) {
-                       return false;
-               }
-               return true;
+               return !(searchResponse == null || searchResponse.getHits() == null || searchResponse.getHits().getHits() == null
+                               || searchResponse.getHits().getHits().length == 0);
        }
 
        public String getEsHost() {
index 448b3e3..5b2498a 100644 (file)
@@ -41,7 +41,9 @@ import java.util.Map;
 //@Component("users-dao")
 public class Neo4jUsersDAO implements IUsersDAO {
 
-       // @Resource
+       private static final String USER_ID = "userId";
+
+    // @Resource
        Neo4jClient neo4jClient;
 
        private static Logger logger = Logger.getLogger(Neo4jUsersDAO.class.getName());
@@ -75,7 +77,7 @@ public class Neo4jUsersDAO implements IUsersDAO {
                        if (createIndexStatus.equals(Neo4jOperationStatus.OK)) {
                                logger.info("Users indexes created in Neo4j");
                                List<String> propertyUnique = new ArrayList<>();
-                               propertyUnique.add("userId");
+                               propertyUnique.add(USER_ID);
 
                                logger.info("Start create Users constraints in Neo4jGraph");
                                Neo4jOperationStatus createUniquenessStatus = neo4jClient
@@ -100,7 +102,7 @@ public class Neo4jUsersDAO implements IUsersDAO {
        @Override
        public Either<UserData, ActionStatus> getUserData(String id) {
                MatchFilter filter = new MatchFilter();
-               filter.addToMatch("userId", id);
+               filter.addToMatch(USER_ID, id);
                Either<List<GraphElement>, Neo4jOperationStatus> status = neo4jClient.getByFilter(GraphElementTypeEnum.Node,
                                NodeTypeEnum.User.getName(), filter);
                if (status.isRight()) {
@@ -128,7 +130,7 @@ public class Neo4jUsersDAO implements IUsersDAO {
        @Override
        public ActionStatus updateUserData(UserData userData) {
                UpdateFilter filter = new UpdateFilter();
-               filter.addToMatch("userId", userData.getUserId());
+               filter.addToMatch(USER_ID, userData.getUserId());
                filter.setToUpdate(userData.toGraphMap());
                Neo4jOperationStatus status = neo4jClient.updateElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
                                filter);
@@ -142,7 +144,7 @@ public class Neo4jUsersDAO implements IUsersDAO {
        @Override
        public ActionStatus deleteUserData(String id) {
                MatchFilter filter = new MatchFilter();
-               filter.addToMatch("userId", id);
+               filter.addToMatch(USER_ID, id);
                Neo4jOperationStatus status = neo4jClient.deleteElement(GraphElementTypeEnum.Node, NodeTypeEnum.User.getName(),
                                filter);
                if (status.equals(Neo4jOperationStatus.OK)) {
index ab95cdb..e01cddc 100644 (file)
@@ -253,7 +253,7 @@ public class TitanDao {
         }
     }
 
-    public void setVertexProperties(Vertex vertex, Map<String, Object> properties) throws IOException {
+    public void setVertexProperties(Vertex vertex, Map<String, Object> properties) {
         for (Map.Entry<String, Object> entry : properties.entrySet()) {
             if (entry.getValue() != null) {
                 vertex.property(entry.getKey(), entry.getValue());
index 459e983..3c502ff 100644 (file)
@@ -44,7 +44,10 @@ import java.util.stream.StreamSupport;
 @Component("titan-generic-dao")
 public class TitanGenericDao {
 
-       private TitanGraphClient titanClient;
+       private static final String FAILED_TO_RETRIEVE_GRAPH_STATUS_IS = "Failed to retrieve graph. status is {}";
+    private static final String NO_EDGES_IN_GRAPH_FOR_CRITERIA = "No edges in graph for criteria";
+    private static final String FAILED_TO_CREATE_EDGE_FROM_TO = "Failed to create edge from [{}] to [{}]";
+    private TitanGraphClient titanClient;
        private static Logger log = Logger.getLogger(TitanGenericDao.class.getName());
        private static final String LOCK_NODE_PREFIX = "lock_";
 
@@ -210,7 +213,7 @@ public class TitanGenericDao {
 
                                return Either.left(newRelation);
                        } catch (Exception e) {
-                               log.debug("Failed to create edge from [{}] to [{}]", from, to, e);
+                               log.debug(FAILED_TO_CREATE_EDGE_FROM_TO, from, to, e);
                                return Either.right(TitanGraphClient.handleTitanException(e));
                        }
                } else {
@@ -223,7 +226,7 @@ public class TitanGenericDao {
                try {
                        Edge edge = addEdge(vertexOut, vertexIn, type, properties);
                } catch (Exception e) {
-                       log.debug("Failed to create edge from [{}] to [{}]", vertexOut, vertexIn, e);
+                       log.debug(FAILED_TO_CREATE_EDGE_FROM_TO, vertexOut, vertexIn, e);
                        return TitanGraphClient.handleTitanException(e);
                }
                return TitanOperationStatus.OK;
@@ -260,7 +263,7 @@ public class TitanGenericDao {
                        Edge edge = addEdge(vertexOut, vertexIn, type, properties);
                        return Either.left(edge);
                } catch (Exception e) {
-                       log.debug("Failed to create edge from [{}] to [{}]", vertexOut, vertexIn, e);
+                       log.debug(FAILED_TO_CREATE_EDGE_FROM_TO, vertexOut, vertexIn, e);
                        return Either.right(TitanGraphClient.handleTitanException(e));
                }
 
@@ -452,7 +455,7 @@ public class TitanGenericDao {
                Edge matchingEdge = null;
                Iterable<TitanEdge> edges = query.edges();
                if (edges == null) {
-                       log.debug("No edges in graph for criteria");
+                       log.debug(NO_EDGES_IN_GRAPH_FOR_CRITERIA);
                        return Either.right(TitanOperationStatus.NOT_FOUND);
                }
                Iterator<TitanEdge> eIter = edges.iterator();
@@ -461,7 +464,7 @@ public class TitanGenericDao {
                }
 
                if (matchingEdge == null) {
-                       log.debug("No edges in graph for criteria");
+                       log.debug(NO_EDGES_IN_GRAPH_FOR_CRITERIA);
                        return Either.right(TitanOperationStatus.NOT_FOUND);
                }
                return Either.left(matchingEdge);
@@ -476,7 +479,7 @@ public class TitanGenericDao {
                                if (vertexFrom.isRight()) {
                                        return Either.right(vertexFrom.right().value());
                                }
-                               Iterable<TitanEdge> edges = ((TitanVertex) vertexFrom.left().value()).query().labels(label).edges();
+                               Iterable<TitanEdge> edges = vertexFrom.left().value().query().labels(label).edges();
                                Iterator<TitanEdge> eIter = edges.iterator();
                                while (eIter.hasNext()) {
                                        Edge edge = eIter.next();
@@ -1295,7 +1298,7 @@ public class TitanGenericDao {
 
                Either<TitanGraph, TitanOperationStatus> graphRes = titanClient.getGraph();
                if (graphRes.isRight()) {
-                       log.error("Failed to retrieve graph. status is {}", graphRes);
+                       log.error(FAILED_TO_RETRIEVE_GRAPH_STATUS_IS, graphRes);
                        return Either.right(graphRes.right().value());
                }
 
@@ -1343,7 +1346,7 @@ public class TitanGenericDao {
 
                Either<TitanGraph, TitanOperationStatus> graphRes = titanClient.getGraph();
                if (graphRes.isRight()) {
-                       log.error("Failed to retrieve graph. status is {}", graphRes);
+                       log.error(FAILED_TO_RETRIEVE_GRAPH_STATUS_IS, graphRes);
                        return Either.right(graphRes.right().value());
                }
 
@@ -1467,7 +1470,7 @@ public class TitanGenericDao {
 
                Either<TitanGraph, TitanOperationStatus> graphRes = titanClient.getGraph();
                if (graphRes.isRight()) {
-                       log.error("Failed to retrieve graph. status is {}", graphRes);
+                       log.error(FAILED_TO_RETRIEVE_GRAPH_STATUS_IS, graphRes);
                        return Either.right(graphRes.right().value());
                }
 
@@ -1590,7 +1593,7 @@ public class TitanGenericDao {
                Edge matchingEdge = null;
                Iterable<TitanEdge> edges = query.edges();
                if (edges == null) {
-                       log.debug("No edges in graph for criteria");
+                       log.debug(NO_EDGES_IN_GRAPH_FOR_CRITERIA);
                        return Either.right(TitanOperationStatus.NOT_FOUND);
                }
                Iterator<TitanEdge> eIter = edges.iterator();
@@ -1599,7 +1602,7 @@ public class TitanGenericDao {
                }
 
                if (matchingEdge == null) {
-                       log.debug("No edges in graph for criteria");
+                       log.debug(NO_EDGES_IN_GRAPH_FOR_CRITERIA);
                        return Either.right(TitanOperationStatus.NOT_FOUND);
                }
                return Either.left(matchingEdge);