DbService sonar fixes 67/30467/4
authorJakub Dudycz <jakub.dudycz@nokia.com>
Tue, 6 Feb 2018 14:56:15 +0000 (15:56 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 7 Feb 2018 17:17:20 +0000 (17:17 +0000)
Change-Id: I3e9ed180c1c0d6be9182e65afc84dd2947404980
Issue-ID: APPC-570
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBDesignException.java [deleted file]
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBException.java [new file with mode: 0644]
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DbService.java
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DesignDBService.java

diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBDesignException.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBDesignException.java
deleted file mode 100644 (file)
index f7ae77c..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-package org.onap.appc.design.dbervices;
-
-public class DBDesignException extends Exception {
-
-    public DBDesignException(String message) {
-        super(message);
-    }
-}
diff --git a/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBException.java b/appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/dbervices/DBException.java
new file mode 100644 (file)
index 0000000..6ee961a
--- /dev/null
@@ -0,0 +1,12 @@
+package org.onap.appc.design.dbervices;
+
+public class DBException extends Exception {
+
+    public DBException(String message) {
+        super(message);
+    }
+
+    public DBException(String message, Throwable cause) {
+        super(message, cause);
+    }
+}
index 888e76a..b09ef0d 100644 (file)
 
 package org.onap.appc.design.dbervices;
 
-import java.sql.ResultSet;
-import java.util.ArrayList;
+import static com.google.common.collect.Lists.newArrayList;
 
-import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
+import java.sql.ResultSet;
+import java.util.List;
 import org.onap.ccsdk.sli.core.dblib.DBResourceManager;
 import org.onap.ccsdk.sli.core.dblib.DbLibService;
+import org.onap.ccsdk.sli.core.sli.provider.SvcLogicService;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
@@ -41,102 +42,99 @@ public class DbService {
 
     private static final Logger Log = LoggerFactory.getLogger(DbService.class);
     private static final String DBLIB_SERVICE = "org.onap.ccsdk.sli.core.dblib.DBResourceManager";
-    DbLibService dblibSvc = null;
-    String errorMsg = null;
+    private DbLibService dbLibSvc = null;
 
-    public DbService() throws Exception {
-        DbLibService dblibSvc = null;
+    public DbService() throws DBException {
         Log.info("Initializing DbService service");
         try
         {
-            dblibSvc = getDbLibService();
-            if (dblibSvc == null) {
+            dbLibSvc = getDbLibService();
+            if (dbLibSvc == null) {
                 Log.error("Got Exception While getting DB Connection");
-                throw new Exception("Got Exception While getting DB Connection");
+                throw new DBException("Got Exception While getting DB Connection");
             }
-            this.dblibSvc = dblibSvc;
         }
         catch (Exception e) {
             Log.error(e.getMessage());
-            throw e;
+            throw new DBException("An error occurred when instantiating DB service", e);
         }
     }
 
     private static DbLibService getDbLibService() {
         
-        DbLibService dblibSvc = null;
-        BundleContext bctx = null;
-        ServiceReference sref = null;
+        DbLibService dbLibService = null;
+        BundleContext bundleContext = null;
+        ServiceReference serviceRef = null;
 
         Bundle bundle =  FrameworkUtil.getBundle(SvcLogicService.class);
 
         if (bundle != null) {
-            bctx = bundle.getBundleContext();
+            bundleContext = bundle.getBundleContext();
         }
 
-        if (bctx != null) {
+        if (bundleContext != null) {
             Log.debug("Getting bundle Context");
-            sref = bctx.getServiceReference(DBLIB_SERVICE);
+            serviceRef = bundleContext.getServiceReference(DBLIB_SERVICE);
         }
 
-        if (sref == null) {
+        if (serviceRef == null) {
             Log.warn("Could not find service reference for DBLib service");
                     
         } else {
-            dblibSvc = (DbLibService) bctx.getService(sref);
-            if (dblibSvc == null) {
+            dbLibService = (DbLibService)bundleContext.getService(serviceRef);
+            if (dbLibService == null) {
                 Log.warn("DBLIB_SERVICE is null");
             }
         }
-        if (dblibSvc == null) {
+        if (dbLibService == null) {
             try {
-                dblibSvc = new DBResourceManager(System.getProperties());
+                dbLibService = new DBResourceManager(System.getProperties());
             } catch (Exception e) {
                 Log.error("Caught exception trying to create db service", e);
             }
 
-            if (dblibSvc == null) {
+            if (dbLibService == null) {
                 Log.warn("Could not create new DBResourceManager");
             }
         }
-        return (dblibSvc);
+        return dbLibService;
     }
 
-    public ResultSet getDBData(String query) throws Exception {
+    public ResultSet getDBData(String query) throws DBException{
         ResultSet resultSet;
         StringBuilder sqlBuilder = new StringBuilder(query);
         Log.info("Query: " + sqlBuilder.toString());
         try {
-            resultSet = dblibSvc.getData(sqlBuilder.toString(), null, null);
+            resultSet = dbLibSvc.getData(sqlBuilder.toString(), null, null);
         } catch (Exception e) {
             Log.error("SQL query "+sqlBuilder+" :: " + e.getMessage());
-            throw e;
+            throw new DBException("An error occurred when reading DB data", e);
         }
         return resultSet;
     }
 
-    public ResultSet getDBData(String query, ArrayList<String> paramList) throws Exception {
+    public ResultSet getDBData(String query, List<String> paramList) throws DBException {
         ResultSet resultSet;
         StringBuilder sqlBuilder = new StringBuilder(query);
         Log.info("Query :" + sqlBuilder.toString());
         try {
-            resultSet = dblibSvc.getData(sqlBuilder.toString(), paramList, null);
-        } catch (Exception expObj) {
-            Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
-            throw expObj;
+            resultSet = dbLibSvc.getData(sqlBuilder.toString(), newArrayList(paramList), null);
+        } catch (Exception e) {
+            Log.error("query "+sqlBuilder+" :: " + e.getMessage());
+            throw new DBException("An error occurred when reading DB data", e);
         }
         return resultSet;
     }
     
-    public boolean updateDBData(String query, ArrayList<String> paramList) throws Exception {
+    public boolean updateDBData(String query, List<String> paramList) throws DBException{
         boolean update;
         StringBuilder sqlBuilder = new StringBuilder(query);
         Log.info("Query :" + sqlBuilder.toString());
         try {
-            update = dblibSvc.writeData(sqlBuilder.toString(), paramList, null);
-        } catch (Exception expObj) {
-            Log.error("query "+sqlBuilder+" :: " + expObj.getMessage());
-            throw expObj;
+            update = dbLibSvc.writeData(sqlBuilder.toString(), newArrayList(paramList), null);
+        } catch (Exception e) {
+            Log.error("query "+sqlBuilder+" :: " + e.getMessage());
+            throw new DBException("An error occurred when updating DB data", e);
         }
         return update;
     }
index 5a20c06..02de360 100644 (file)
@@ -106,7 +106,7 @@ public class DesignDBService {
                 response = setProtocolReference(payload, requestID);
                 break;
             default:
-                throw new DBDesignException(" Action " + action + " not found while processing request ");
+                throw new DBException(" Action " + action + " not found while processing request ");
 
         }
         return response;
@@ -135,7 +135,7 @@ public class DesignDBService {
         boolean data = dbservice.updateDBData(queryString, argList);
 
         if (!data) {
-            throw new DBDesignException("Error while updating ProtocolReference");
+            throw new DBException("Error while updating ProtocolReference");
         }
         return SUCCESS_JSON;
     }
@@ -177,7 +177,7 @@ public class DesignDBService {
         data = dbservice.updateDBData(queryString, argList);
 
         if (!data) {
-            throw new DBDesignException("Error while updating ProtocolReference");
+            throw new DBException("Error while updating ProtocolReference");
         }
         return SUCCESS_JSON;
     }
@@ -235,7 +235,7 @@ public class DesignDBService {
         boolean data = dbservice.updateDBData(queryString, argList);
 
         if (!data) {
-            throw new DBDesignException("Error while updating RelationShip table");
+            throw new DBException("Error while updating RelationShip table");
         }
 
     }
@@ -324,7 +324,7 @@ public class DesignDBService {
 
         boolean updateStatus = dbservice.updateDBData(insertQuery, argList);
         if (!updateStatus)
-            throw new DBDesignException("Error while updating Action Status");
+            throw new DBException("Error while updating Action Status");
     }
 
     private void createArtifactTrackingRecord(String payload, String requestID, int sdcArtifactId, int sdcReferenceId)
@@ -356,7 +356,7 @@ public class DesignDBService {
         log.info(QUERY_STR + queryString);
         boolean data = dbservice.updateDBData(queryString, argList);
         if (!data) {
-            throw new DBDesignException("Error Updating DT_ARTIFACT_TRACKING ");
+            throw new DBException("Error Updating DT_ARTIFACT_TRACKING ");
         }
     }
 
@@ -404,7 +404,7 @@ public class DesignDBService {
                 }
             }
             if (artifactContent == null || artifactContent.isEmpty()) {
-                throw new DBDesignException(
+                throw new DBException(
                     "Sorry !!! I dont have any artifact Named : " + payloadObject.get("artifact-name").textValue());
             }
             DesignResponse designResponse = new DesignResponse();
@@ -454,7 +454,7 @@ public class DesignDBService {
             designResponse.setUserId(payloadObject.get(USER_ID).textValue());
             boolean update = dbservice.updateDBData(queryString, argList);
             if (!update) {
-                throw new DBDesignException("Sorry .....Something went wrong while updating the Status");
+                throw new DBException("Sorry .....Something went wrong while updating the Status");
             }
 
             ObjectMapper mapper = new ObjectMapper();
@@ -511,7 +511,7 @@ public class DesignDBService {
             }
 
             if (statusInfoList.isEmpty()) {
-                throw new DBDesignException(
+                throw new DBException(
                     "OOPS !!!! No VNF information available for VNF-TYPE : " + vnfType + " for User : " + userID);
             }
             designResponse.setStatusInfoList(statusInfoList);
@@ -587,7 +587,7 @@ public class DesignDBService {
                 designInfoList.add(designInfo);
             }
             if (designInfoList.isEmpty()) {
-                throw new DBDesignException(
+                throw new DBException(
                     " Welcome to CDT, Looks like you dont have Design Yet... Lets create some....");
             }
             designResponse.setDesignInfoList(designInfoList);