FlowControlDBService sonar fixes 71/31071/2
authorJakub Dudycz <jakub.dudycz@nokia.com>
Fri, 9 Feb 2018 12:36:55 +0000 (13:36 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 14 Feb 2018 23:08:31 +0000 (23:08 +0000)
Change-Id: I522d6e9e4f581687ce36bf7873c4fb4643f33244
Issue-ID: APPC-592
Signed-off-by: Jakub Dudycz <jakub.dudycz@nokia.com>
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/dbervices/FlowControlDBService.java

index f326094..3f95c73 100644 (file)
  */
 package org.onap.appc.flow.controller.dbervices;
 
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
 import java.util.Map;
-
 import org.apache.commons.lang.StringUtils;
 import org.onap.appc.flow.controller.data.Transaction;
 import org.onap.appc.flow.controller.utils.EscapeUtils;
 import org.onap.appc.flow.controller.utils.FlowControllerConstants;
+import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
-import org.onap.ccsdk.sli.adaptors.resource.sql.SqlResource;
-
-import com.att.eelf.configuration.EELFLogger;
-import com.att.eelf.configuration.EELFManager;
 
 public class FlowControlDBService {
 
     private static final EELFLogger log = EELFManager.getInstance().getLogger(FlowControlDBService.class);
+    private static final String QUERY_STR = "Query String : ";
+    private static final String FAILURE_PARAM = "FAILURE";
+    private static final String GET_FLOW_REF_DATA_ERROR = "Error - while getting FlowReferenceData ";
+    private static final String SELECT_AS_QUERY_STR = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from ";
+    private static final String WHERE_ART_NAME_QUERY_STR = " where artifact_name in (select artifact_name from ";
+    private static final String WHERE_VNF_TYPE_QUERY_STR = " where vnf_type= $";
+    private static final String SELECT_ART_CONTENT_QUERY_STR = "select artifact_content from ";
+    private static final String WHERE_ARTIFACT_NAME_QUERY_STR = " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
+    private static final String ARTIFACT_CONTENT_PARAM = "artifact-content";
+    private static final String COUNT_PROTOCOL_PARAM = "count(protocol)";
+    private static final String WHERE_ACTION_QUERY_STR = " where action = '";
+    private static final String AND_ACTION_LEVEL_QUERY_STR = " and action_level = '";
+
     private SvcLogicResource serviceLogic;
     private static FlowControlDBService dgGeneralDBService = null;
 
+    private FlowControlDBService() {
+        if (serviceLogic == null) {
+            serviceLogic = new SqlResource();
+        }
+    }
+    
     public static FlowControlDBService initialise() {
         if (dgGeneralDBService == null) {
             dgGeneralDBService = new FlowControlDBService();
@@ -49,31 +66,28 @@ public class FlowControlDBService {
         return dgGeneralDBService;
     }
 
-    private FlowControlDBService() {
-        if (serviceLogic == null) {
-            serviceLogic = new SqlResource();
-        }
-    }
-
     public void getFlowReferenceData(SvcLogicContext ctx, Map<String, String> inParams, SvcLogicContext localContext)
-            throws SvcLogicException {
+        throws SvcLogicException {
 
         String fn = "DBService.getflowModelInfo";
         String whereClause = " where ACTION = $" + FlowControllerConstants.REQUEST_ACTION;
 
-        if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE)))
+        if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.VNF_TYPE))) {
             whereClause = whereClause.concat(" and VNF_TYPE = $" + FlowControllerConstants.VNF_TYPE);
-        if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL)))
+        }
+        if (StringUtils.isNotBlank(ctx.getAttribute(FlowControllerConstants.ACTION_LEVEL))) {
             whereClause = whereClause.concat(" and ACTION_LEVEL = $" + FlowControllerConstants.ACTION_LEVEL);
+        }
 
-        QueryStatus status = null;
-        if (serviceLogic != null& localContext != null) {
+        QueryStatus status;
+        if (serviceLogic != null && localContext != null) {
             String key = "select SEQUENCE_TYPE, CATEGORY, GENERATION_NODE, EXECUTION_NODE from "
-                    + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
-            log.debug(fn + "Query String : " + key);
+                + FlowControllerConstants.DB_MULTISTEP_FLOW_REFERENCE + whereClause;
+            log.debug(fn + QUERY_STR + key);
             status = serviceLogic.query("SQL", false, null, key, null, null, localContext);
-            if (status.toString().equals("FAILURE"))
-                throw new SvcLogicException("Error - while getting FlowReferenceData ");
+            if (status.toString().equals(FAILURE_PARAM)) {
+                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+            }
         }
     }
 
@@ -83,43 +97,46 @@ public class FlowControlDBService {
 
     public String getDesignTimeFlowModel(SvcLogicContext localContext) throws SvcLogicException {
         String fn = "DBService.getDesignTimeFlowModel ";
-        QueryStatus status = null;
-        if (serviceLogic != null& localContext != null) {
-            String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
-                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
-                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
+        QueryStatus status;
+        if (serviceLogic != null && localContext != null) {
+            String queryString =
+                SELECT_AS_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
+                    + FlowControllerConstants.VNF_TYPE
                     + " and  vnfc_type = $" + FlowControllerConstants.VNFC_TYPE + " and  action = $"
                     + FlowControllerConstants.REQUEST_ACTION + " and file_category =  $"
                     + FlowControllerConstants.CATEGORY + " )";
 
-            log.debug(fn + "Query String : " + queryString);
+            log.debug(fn + QUERY_STR + queryString);
             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
 
-            if (status.toString().equals("FAILURE"))
-                throw new SvcLogicException("Error - while getting FlowReferenceData ");
+            if (status.toString().equals(FAILURE_PARAM)) {
+                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+            }
 
-            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
-                    + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
+            String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
+                + WHERE_ARTIFACT_NAME_QUERY_STR;
 
-            log.debug(fn + "Query String : " + queryString1);
+            log.debug(fn + QUERY_STR + queryString1);
             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
-            if (status.toString().equals("FAILURE"))
-                throw new SvcLogicException("Error - while getting FlowReferenceData ");
+            if (status.toString().equals(FAILURE_PARAM)) {
+                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+            }
         }
-        return localContext != null ? localContext.getAttribute("artifact-content") : null;
+        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
     }
 
     public QueryStatus loadSequenceIntoDB(SvcLogicContext localContext) throws SvcLogicException {
 
         QueryStatus status = null;
-
         if (localContext != null) {
             String fn = "DBService.saveArtifacts";
 
             localContext.setAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED,
-                    EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
+                EscapeUtils.escapeSql(localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT)));
             log.debug("ESCAPED sequence for DB : "
-                    + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
+                + localContext.getAttribute(FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED));
 
             for (Object key : localContext.getAttributeKeySet()) {
                 String parmName = (String) key;
@@ -128,39 +145,39 @@ public class FlowControlDBService {
             }
 
             String queryString = "INSERT INTO " + FlowControllerConstants.DB_REQUEST_ARTIFACTS + " set request_id =  $"
-                    + FlowControllerConstants.REQUEST_ID + " , action =  $" + FlowControllerConstants.REQUEST_ACTION
-                    + " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
-                    + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
-                    + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
-                    + " , updated_date = sysdate() ";
+                + FlowControllerConstants.REQUEST_ID + " , action =  $" + FlowControllerConstants.REQUEST_ACTION
+                + " , action_level =  $" + FlowControllerConstants.ACTION_LEVEL + " , vnf_type = $"
+                + FlowControllerConstants.VNF_TYPE + " , category = $" + FlowControllerConstants.CATEGORY
+                + " , artifact_content = $" + FlowControllerConstants.ARTIFACT_CONTENT_ESCAPED
+                + " , updated_date = sysdate() ";
 
-            log.debug(fn + "Query String : " + queryString);
+            log.debug(fn + QUERY_STR + queryString);
             status = serviceLogic.save("SQL", false, false, queryString, null, null, localContext);
-            if (status.toString().equals("FAILURE"))
+            if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error While processing storing Artifact: "
-                        + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
+                    + localContext.getAttribute(FlowControllerConstants.ARTIFACT_NAME));
+            }
         }
         return status;
 
     }
 
-    public void populateModuleAndRPC(Transaction transaction, String vnf_type) throws Exception {
+    public void populateModuleAndRPC(Transaction transaction, String vnfType) throws SvcLogicException {
         String fn = "FlowControlDBService.populateModuleAndRPC ";
-        QueryStatus status = null;
+        QueryStatus status;
         SvcLogicContext context = new SvcLogicContext();
-        String protocolType = null;
-
-        protocolType = getProtocolType(transaction, vnf_type, fn, context, protocolType);
+        String protocolType = getProtocolType(transaction, vnfType, fn, context);
 
         String key = "select execution_type, execution_module, execution_rpc from "
-                + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + " where action = '" + transaction.getAction()
-                + "'" + " and action_level = '" + transaction.getActionLevel() + "'" + " and protocol = '"
-                + protocolType + "'";
+            + FlowControllerConstants.DB_PROCESS_FLOW_REFERENCE + WHERE_ACTION_QUERY_STR + transaction.getAction()
+            + "'" + AND_ACTION_LEVEL_QUERY_STR + transaction.getActionLevel() + "'" + " and protocol = '"
+            + protocolType + "'";
 
-        log.debug(fn + "Query String : " + key);
+        log.debug(fn + QUERY_STR + key);
         status = serviceLogic.query("SQL", false, null, key, null, null, context);
-        if (status.toString().equals("FAILURE"))
-            throw new SvcLogicException("Error - while getting FlowReferenceData ");
+        if (status.toString().equals(FAILURE_PARAM)) {
+            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+        }
 
         transaction.setExecutionModule(context.getAttribute(FlowControllerConstants.EXECUTTION_MODULE));
         transaction.setExecutionRPC(context.getAttribute(FlowControllerConstants.EXECUTION_RPC));
@@ -168,113 +185,135 @@ public class FlowControlDBService {
 
     }
 
-    private String getProtocolType(Transaction transaction, String vnf_type, String fn, SvcLogicContext context,
-            String protocolType) throws Exception {
+    private String getProtocolType(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
+        throws SvcLogicException {
         QueryStatus status;
         String protocolQuery;
         int protocolCount;
         protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
-                + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
-                + transaction.getActionLevel() + "'";
+            + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
+            + transaction.getActionLevel() + "'";
 
-        log.debug(fn + "Query String : " + protocolQuery);
+        log.debug(fn + QUERY_STR + protocolQuery);
         status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
-        if (status.toString().equals("FAILURE"))
-            throw new SvcLogicException("Error - while getting FlowReferenceData ");
+        if (status.toString().equals(FAILURE_PARAM)) {
+            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+        }
 
-        log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
-        protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
+        log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
+        protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
 
         if (protocolCount == 1) {
             protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
-                    + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
-                    + transaction.getActionLevel() + "'";
+                + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
+                + transaction.getActionLevel() + "'";
 
-            log.debug(fn + "Query String : " + protocolQuery);
+            log.debug(fn + QUERY_STR + protocolQuery);
             status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
-            if (status.toString().equals("FAILURE"))
-                throw new SvcLogicException("Error - while getting FlowReferenceData ");
-            protocolType = context.getAttribute("protocol");
+            if (status.toString().equals(FAILURE_PARAM)) {
+                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+            }
+            return context.getAttribute("protocol");
+        } else {
+            if (hasSingleProtocol(transaction, vnfType, fn, context)) {
+                return context.getAttribute("protocol");
+            }
         }
-            else  {
-                protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
-                        + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
-                        + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnf_type + "'";
-
-                log.debug(fn + "Query String : " + protocolQuery);
-                status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
-                if (status.toString().equals("FAILURE"))
-                    throw new SvcLogicException("Error - while getting FlowReferenceData ");
-
-                log.debug(" Protocol Count " + context.getAttribute("count(protocol)"));
-                protocolCount = Integer.parseInt(context.getAttribute("count(protocol)"));
-                if(protocolCount > 1){
-                    throw new Exception("Got more than 2 values..");
-                }else if(protocolCount == 1){
-                    protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
-                            + " where action = '" + transaction.getAction() + "'" + " and action_level = '"
-                            + transaction.getActionLevel() + "'"+ " and vnf_type = '" + vnf_type + "'";
-                    log.debug(fn + "Query String : " + protocolQuery);
-                    status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
-                    if (status.toString().equals("FAILURE"))
-                        throw new SvcLogicException("Error - while getting FlowReferenceData ");
-                    protocolType = context.getAttribute("protocol");
-                }
+        return null;
+    }
+
+    private boolean hasSingleProtocol(Transaction transaction, String vnfType, String fn, SvcLogicContext context)
+        throws SvcLogicException {
+        String protocolQuery;
+        QueryStatus status;
+        int protocolCount;
+        protocolQuery = "select count(protocol) from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
+            + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
+            + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
+
+        log.debug(fn + QUERY_STR + protocolQuery);
+        status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
+        if (status.toString().equals(FAILURE_PARAM)) {
+            throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
+        }
+
+        log.debug(" Protocol Count " + context.getAttribute(COUNT_PROTOCOL_PARAM));
+        protocolCount = Integer.parseInt(context.getAttribute(COUNT_PROTOCOL_PARAM));
+        if (protocolCount > 1) {
+            throw new SvcLogicException("Got more than 2 values..");
+        } else if (protocolCount == 1) {
+            protocolQuery = "select protocol from " + FlowControllerConstants.DB_PROTOCOL_REFERENCE
+                + WHERE_ACTION_QUERY_STR + transaction.getAction() + "'" + AND_ACTION_LEVEL_QUERY_STR
+                + transaction.getActionLevel() + "'" + " and vnf_type = '" + vnfType + "'";
+            log.debug(fn + QUERY_STR + protocolQuery);
+            status = serviceLogic.query("SQL", false, null, protocolQuery, null, null, context);
+            if (status.toString().equals(FAILURE_PARAM)) {
+                throw new SvcLogicException(GET_FLOW_REF_DATA_ERROR);
             }
-        return protocolType;
+            return true;
+        }
+        return false;
     }
 
     public String getDependencyInfo(SvcLogicContext localContext) throws SvcLogicException {
         String fn = "DBService.getDependencyInfo ";
-        QueryStatus status = null;
-        if (serviceLogic != null& localContext != null) {
-            String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
-                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
-                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
+        QueryStatus status;
+        if (serviceLogic != null && localContext != null) {
+            String queryString =
+                SELECT_AS_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
+                    + FlowControllerConstants.VNF_TYPE
                     + " and file_category = '" + FlowControllerConstants.DEPENDENCYMODEL + "' )";
 
-            log.debug(fn + "Query String : " + queryString);
+            log.debug(fn + QUERY_STR + queryString);
             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
 
-            if (status.toString().equals("FAILURE"))
+            if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error - while getting dependencydata ");
+            }
 
-            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
-                    + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
+            String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
+                + WHERE_ARTIFACT_NAME_QUERY_STR;
 
-            log.debug(fn + "Query String : " + queryString1);
+            log.debug(fn + QUERY_STR + queryString1);
             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
-            if (status.toString().equals("FAILURE"))
+            if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error - while getting dependencyData ");
+            }
         }
 
-        return localContext != null ? localContext.getAttribute("artifact-content") : null;
+        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
 
     }
 
     public String getCapabilitiesData(SvcLogicContext localContext) throws SvcLogicException {
         String fn = "DBService.getCapabilitiesData ";
-        QueryStatus status = null;
-        if (serviceLogic != null& localContext != null) {
-            String queryString = "select max(internal_version) as maxInternalVersion, artifact_name as artifactName from "
-                    + FlowControllerConstants.DB_SDC_ARTIFACTS + " where artifact_name in (select artifact_name from "
-                    + FlowControllerConstants.DB_SDC_REFERENCE + " where vnf_type= $" + FlowControllerConstants.VNF_TYPE
+        QueryStatus status;
+        if (serviceLogic != null && localContext != null) {
+            String queryString =
+                SELECT_AS_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_ARTIFACTS + WHERE_ART_NAME_QUERY_STR
+                    + FlowControllerConstants.DB_SDC_REFERENCE + WHERE_VNF_TYPE_QUERY_STR
+                    + FlowControllerConstants.VNF_TYPE
                     + " and file_category = '" + FlowControllerConstants.CAPABILITY + "' )";
 
-            log.info(fn + "Query String : " + queryString);
+            log.info(fn + QUERY_STR + queryString);
             status = serviceLogic.query("SQL", false, null, queryString, null, null, localContext);
 
-            if (status.toString().equals("FAILURE"))
+            if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error - while getting capabilitiesData ");
+            }
 
-            String queryString1 = "select artifact_content from " + FlowControllerConstants.DB_SDC_ARTIFACTS
-                    + " where artifact_name = $artifactName  and internal_version = $maxInternalVersion ";
+            String queryString1 = SELECT_ART_CONTENT_QUERY_STR + FlowControllerConstants.DB_SDC_ARTIFACTS
+                + WHERE_ARTIFACT_NAME_QUERY_STR;
 
-            log.debug(fn + "Query String : " + queryString1);
+            log.debug(fn + QUERY_STR + queryString1);
             status = serviceLogic.query("SQL", false, null, queryString1, null, null, localContext);
-            if (status.toString().equals("FAILURE"))
+            if (status.toString().equals(FAILURE_PARAM)) {
                 throw new SvcLogicException("Error - while getting capabilitiesData ");
+            }
         }
-        return localContext != null ? localContext.getAttribute("artifact-content") : null;
+        return localContext != null ? localContext.getAttribute(ARTIFACT_CONTENT_PARAM) : null;
     }
 }