aboutsummaryrefslogtreecommitdiffstats
path: root/ONAP-PAP-REST/src/main/java/org/onap
diff options
context:
space:
mode:
authorAvinashS <avinash.s@huawei.com>2017-08-30 14:37:27 +0530
committerAvinashS <avinash.s@huawei.com>2017-08-30 14:37:27 +0530
commit76dc816cecf02b26b56b6fc40118fd8b9f44fd39 (patch)
treec828c956cfc5b7f6acb964e86ac851b3881afed4 /ONAP-PAP-REST/src/main/java/org/onap
parent6fdf2fd8d754e59977bf107d60725b8394771f69 (diff)
ONAP-PAP-REST sonar fixes
Few major and minor sonar issues fixes under policy engine. Change-Id: I3845ca4f050cdfada5c9e5cf2155a20974d6db37 IssueId: POLICY-174 Signed-off-by: AvinashS <avinash.s@huawei.com>
Diffstat (limited to 'ONAP-PAP-REST/src/main/java/org/onap')
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java2
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java70
-rw-r--r--ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java18
3 files changed, 51 insertions, 39 deletions
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java
index 117fc04e3..3b254b005 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/handler/APIRequestHandler.java
@@ -54,7 +54,7 @@ public class APIRequestHandler {
return;
}
if ("getMetrics".equalsIgnoreCase(apiflag)){
- MetricService.doGetPolicyMetrics(request, response);
+ MetricService.doGetPolicyMetrics(response);
return;
}
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java
index 8a9e8ffac..e942e6b3b 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/ImportService.java
@@ -41,6 +41,13 @@ import org.onap.policy.pap.xacml.rest.components.CreateNewMicroServiceModel;
public class ImportService {
private static final Logger logger = FlexLogger.getLogger(ImportService.class);
+ private static String errorMessage = "Error in reading in file from API call";
+ private static String errorMsg = "error";
+ private static String operation = "operation";
+ private static String importHeader = "import";
+ private static String service = "service";
+ private static String extractDir = "ExtractDir";
+ private static String successMessage = "success";
public void doImportMicroServicePut(HttpServletRequest request, HttpServletResponse response) {
String importServiceCreation = request.getParameter("importService");;
String fileName = request.getParameter("fileName");
@@ -48,8 +55,7 @@ public class ImportService {
String serviceName = request.getParameter("serviceName");
String description = request.getParameter("description");
Map<String, String> successMap = new HashMap<>();
- switch(importServiceCreation){
- case "BRMSPARAM":
+ if(("BRMSPARAM").equals(importServiceCreation)){
StringBuilder builder = new StringBuilder();
int ch;
try {
@@ -58,20 +64,20 @@ public class ImportService {
}
} catch (IOException e) {
logger.error(e);
- PolicyLogger.error("Error in reading in file from API call");
+ PolicyLogger.error(errorMessage);
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- response.addHeader("error", "missing");
- response.addHeader("operation", "import");
- response.addHeader("service", serviceName);
+ response.addHeader(errorMsg, "missing");
+ response.addHeader(operation, importHeader);
+ response.addHeader(service, serviceName);
}
CreateBRMSRuleTemplate brmsRuleTemplate = new CreateBRMSRuleTemplate();
successMap = brmsRuleTemplate.addRule(builder.toString(), serviceName, description, "API");
- break;
- case "MICROSERVICE":
+ }
+ else if(("MICROSERVICE").equals(importServiceCreation)){
CreateNewMicroServiceModel newMS = null;
String randomID = UUID.randomUUID().toString();
if ( fileName != null) {
- File extracDir = new File("ExtractDir");
+ File extracDir = new File(extractDir);
if (!extracDir.exists()){
extracDir.mkdirs();
}
@@ -86,21 +92,21 @@ public class ImportService {
scanner.close();
} catch (IOException e1) {
logger.error(e1);
- PolicyLogger.error("Error in reading in file from API call");
+ PolicyLogger.error(errorMessage);
return;
}
PolicyLogger.info("XML request from API for import new Service");
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream("ExtractDir" + File.separator + randomID+".xmi"), "utf-8"))) {
+ new FileOutputStream(extractDir + File.separator + randomID+".xmi"), "utf-8"))) {
writer.write(xmi);
} catch (IOException e) {
logger.error(e);
- PolicyLogger.error("Error in reading in file from API call");
+ PolicyLogger.error(errorMessage);
return;
}
}else{
InputStream inputStream = null;
- try(FileOutputStream outputStream = new FileOutputStream("ExtractDir" + File.separator + randomID+".zip")) {
+ try(FileOutputStream outputStream = new FileOutputStream(extractDir + File.separator + randomID+".zip")) {
inputStream = request.getInputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1 ;
@@ -122,40 +128,40 @@ public class ImportService {
}
newMS = new CreateNewMicroServiceModel(fileName, serviceName, "API", version, randomID);
successMap = newMS.addValuesToNewModel();
- if (successMap.containsKey("success")) {
+ if (successMap.containsKey(successMessage)) {
successMap.clear();
successMap = newMS.saveImportService();
}
}
- break;
}
+
// return a response to the PAP
- if (successMap.containsKey("success")) {
+ if (successMap.containsKey(successMessage)) {
response.setStatus(HttpServletResponse.SC_OK);
- response.addHeader("successMapKey", "success");
- response.addHeader("operation", "import");
- response.addHeader("service", serviceName);
+ response.addHeader("successMapKey", successMessage);
+ response.addHeader(operation, importHeader);
+ response.addHeader(service, serviceName);
} else if (successMap.containsKey("DBError")) {
if (successMap.get("DBError").contains("EXISTS")){
response.setStatus(HttpServletResponse.SC_CONFLICT);
- response.addHeader("service", serviceName);
- response.addHeader("error", "modelExistsDB");
+ response.addHeader(service, serviceName);
+ response.addHeader(errorMsg, "modelExistsDB");
}else{
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- response.addHeader("error", "importDB");
+ response.addHeader(errorMsg, "importDB");
}
- response.addHeader("operation", "import");
- response.addHeader("service", serviceName);
- }else if (successMap.get("error").contains("MISSING")){
+ response.addHeader(operation, importHeader);
+ response.addHeader(service, serviceName);
+ }else if (successMap.get(errorMsg).contains("MISSING")){
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- response.addHeader("error", "missing");
- response.addHeader("operation", "import");
- response.addHeader("service", serviceName);
- }else if (successMap.get("error").contains("VALIDATION")){
+ response.addHeader(errorMsg, "missing");
+ response.addHeader(operation, importHeader);
+ response.addHeader(service, serviceName);
+ }else if (successMap.get(errorMsg).contains("VALIDATION")){
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- response.addHeader("error", "validation");
- response.addHeader("operation", "import");
- response.addHeader("service", serviceName);
+ response.addHeader(errorMsg, "validation");
+ response.addHeader(operation, importHeader);
+ response.addHeader(service, serviceName);
}
}
diff --git a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java
index 416af4413..d239dfa1b 100644
--- a/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java
+++ b/ONAP-PAP-REST/src/main/java/org/onap/policy/pap/xacml/rest/service/MetricService.java
@@ -36,8 +36,14 @@ import org.onap.policy.xacml.api.pap.OnapPDPGroup;
import com.att.research.xacml.api.pap.PDPPolicy;
public class MetricService {
-
- public static void doGetPolicyMetrics(HttpServletRequest request, HttpServletResponse response) {
+ private static String errorMsg = "error";
+ /*
+ * This is a private constructor
+ * */
+ public MetricService(){
+
+ }
+ public static void doGetPolicyMetrics(HttpServletResponse response) {
Set<OnapPDPGroup> groups = new HashSet<>();
try {
//get the count of policies on the PDP
@@ -52,12 +58,12 @@ public class MetricService {
//get the count of policies on the PAP
EntityManager em = null;
if(XACMLPapServlet.getEmf()!=null){
- em = (EntityManager) XACMLPapServlet.getEmf().createEntityManager();
+ em = XACMLPapServlet.getEmf().createEntityManager();
}
if (em==null){
PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + " Error creating entity manager with persistence unit: " + XACMLPapServlet.getPersistenceUnit());
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
- response.addHeader("error", "Error creating entity manager with persistence unit");
+ response.addHeader(errorMsg, "Error creating entity manager with persistence unit");
return;
}
int papCount = ((Number) em.createNamedQuery("PolicyVersion.findAllCount").getSingleResult()).intValue();
@@ -78,14 +84,14 @@ public class MetricService {
}else{
String message = "The policy count on the PAP and PDP is 0. Please check the database and file system to correct this error.";
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
- response.addHeader("error", message);
+ response.addHeader(errorMsg, message);
return;
}
} catch (Exception e) {
String message = XACMLErrorConstants.ERROR_DATA_ISSUE + " Error Querying the Database: " + e.getMessage();
PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE, e, "XACMLPapServlet", " Error Querying the Database.");
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
- response.addHeader("error", message);
+ response.addHeader(errorMsg, message);
return;
}
}