CHeckstyle and JUnit for base package in ONAP-REST
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyRestController.java
index 9d078a9..76bb049 100644 (file)
@@ -56,7 +56,7 @@ import org.onap.policy.controller.CreateDcaeMicroServiceController;
 import org.onap.policy.controller.CreateFirewallController;
 import org.onap.policy.controller.CreateOptimizationController;
 import org.onap.policy.controller.PolicyController;
-import org.onap.policy.rest.XACMLRestProperties;
+import org.onap.policy.rest.XacmlRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
 import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.PolicyVersion;
@@ -124,27 +124,32 @@ public class PolicyRestController extends RestrictedBaseController {
         PolicyRestController.commonClassDao = commonClassDao;
     }
 
+    /**
+     * policyCreationController.
+     *
+     * @param request Request
+     * @param response Response
+     */
     @RequestMapping(value = {"/policycreation/save_policy"}, method = {RequestMethod.POST})
     public void policyCreationController(HttpServletRequest request, HttpServletResponse response) {
-        String userId = UserUtils.getUserSession(request).getOrgUserId();
         ObjectMapper mapper = new ObjectMapper();
         mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         try {
-            updateAndSendToPAP(request, response, userId, mapper);
+            updateAndSendToPap(request, response, UserUtils.getUserSession(request).getOrgUserId(), mapper);
         } catch (Exception e) {
             policyLogger.error("Exception Occured while saving policy", e);
         }
     }
 
-    private void updateAndSendToPAP(HttpServletRequest request, HttpServletResponse response, String userId,
+    private void updateAndSendToPap(HttpServletRequest request, HttpServletResponse response, String userId,
             ObjectMapper mapper) throws IOException {
         JsonNode root = mapper.readTree(request.getReader());
         policyLogger.info(
-                "****************************************Logging UserID while Create/Update Policy**************************************************");
+                "********************Logging UserID while Create/Update Policy***********************************");
         policyLogger.info(USER_ID + userId + "Policy Data Object:  "
                 + root.get(PolicyController.getPolicydata()).get("policy").toString());
         policyLogger.info(
-                "***********************************************************************************************************************************");
+                "************************************************************************************************");
 
         PolicyRestAdapter policyData = mapper.readValue(
                 root.get(PolicyController.getPolicydata()).get("policy").toString(), PolicyRestAdapter.class);
@@ -165,17 +170,16 @@ public class PolicyRestController extends RestrictedBaseController {
         policyData.setUserId(userId);
 
         String result;
-        String body = PolicyUtils.objectToJsonString(policyData);
-        String uri = request.getRequestURI();
-        ResponseEntity<?> responseEntity = sendToPAP(body, uri, HttpMethod.POST);
+        ResponseEntity<?> responseEntity = sendToPap(PolicyUtils.objectToJsonString(policyData),
+                request.getRequestURI(), HttpMethod.POST);
         if (responseEntity != null && responseEntity.getBody().equals(HttpServletResponse.SC_CONFLICT)) {
             result = "PolicyExists";
         } else if (responseEntity != null) {
             result = responseEntity.getBody().toString();
             String policyName = responseEntity.getHeaders().get(POLICY_NAME).get(0);
             if (policyData.isEditPolicy() && SUCCESS.equalsIgnoreCase(result)) {
-                PolicyNotificationMail email = new PolicyNotificationMail();
-                String mode = "EditPolicy";
+                final PolicyNotificationMail email = new PolicyNotificationMail();
+                final String mode = "EditPolicy";
                 String watchPolicyName = policyName.replace(XML, "");
                 String version = watchPolicyName.substring(watchPolicyName.lastIndexOf('.') + 1);
                 watchPolicyName =
@@ -198,8 +202,8 @@ public class PolicyRestController extends RestrictedBaseController {
 
         PrintWriter out = response.getWriter();
         String responseString = mapper.writeValueAsString(result);
-        JSONObject j = new JSONObject("{policyData: " + responseString + "}");
-        out.write(j.toString());
+        JSONObject json = new JSONObject("{policyData: " + responseString + "}");
+        out.write(json.toString());
     }
 
     private void modifyPolicyData(JsonNode root, PolicyRestAdapter policyData) {
@@ -228,12 +232,12 @@ public class PolicyRestController extends RestrictedBaseController {
         }
     }
 
-    private ResponseEntity<?> sendToPAP(String body, String requestURI, HttpMethod method) {
+    private ResponseEntity<?> sendToPap(String body, String requestUri, HttpMethod method) {
         String papUrl = PolicyController.getPapUrl();
-        String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
-        String papPass = PeCryptoUtils.decrypt(XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS));
-        Base64.Encoder encoder = Base64.getEncoder();
-        String encoding = encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8));
+        String papPass = PeCryptoUtils.decrypt(XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_PASS));
+        String encoding = Base64.getEncoder().encodeToString(
+                (XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_USERID)
+                + ":" + papPass).getBytes(StandardCharsets.UTF_8));
         HttpHeaders headers = new HttpHeaders();
         headers.set(AUTHORIZATION, BASIC + encoding);
         headers.set(CONTENT_TYPE, PolicyController.getContenttype());
@@ -242,7 +246,7 @@ public class PolicyRestController extends RestrictedBaseController {
         HttpEntity<?> requestEntity = new HttpEntity<>(body, headers);
         ResponseEntity<?> result = null;
         HttpClientErrorException exception = null;
-        String uri = requestURI;
+        String uri = requestUri;
         if (uri.startsWith("/")) {
             uri = uri.substring(uri.indexOf('/') + 1);
         }
@@ -279,26 +283,24 @@ public class PolicyRestController extends RestrictedBaseController {
         return result;
     }
 
-    private String callPAP(HttpServletRequest request, String method, String uriValue) {
-        String uri = uriValue;
-        String papUrl = PolicyController.getPapUrl();
-        String papID = XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_USERID);
-        PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XACMLRestProperties.PROP_AES_KEY));
-        String papPass = PeCryptoUtils.decrypt((XACMLProperties.getProperty(XACMLRestProperties.PROP_PAP_PASS)));
+    private String callPap(HttpServletRequest request, String method, String uriValue) {
+        PeCryptoUtils.initAesKey(XACMLProperties.getProperty(XacmlRestProperties.PROP_AES_KEY));
+        String papPass = PeCryptoUtils.decrypt((XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_PASS)));
 
         Base64.Encoder encoder = Base64.getEncoder();
-        String encoding = encoder.encodeToString((papID + ":" + papPass).getBytes(StandardCharsets.UTF_8));
+        String encoding = encoder.encodeToString((XACMLProperties.getProperty(XacmlRestProperties.PROP_PAP_USERID)
+                + ":" + papPass).getBytes(StandardCharsets.UTF_8));
         HttpHeaders headers = new HttpHeaders();
         headers.set(AUTHORIZATION, BASIC + encoding);
         headers.set(CONTENT_TYPE, PolicyController.getContenttype());
 
         HttpURLConnection connection = null;
-        List<FileItem> items;
         FileItem item = null;
         File file = null;
+        String uri = uriValue;
         if (uri.contains(IMPORT_DICTIONARY)) {
             try {
-                items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
+                List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
                 item = items.get(0);
                 file = new File(item.getName());
                 String newFile = file.toString();
@@ -309,7 +311,7 @@ public class PolicyRestController extends RestrictedBaseController {
         }
 
         try {
-            URL url = new URL(papUrl + uri);
+            URL url = new URL(PolicyController.getPapUrl() + uri);
             connection = (HttpURLConnection) url.openConnection();
             connection.setRequestMethod(method);
             connection.setUseCaches(false);
@@ -323,7 +325,7 @@ public class PolicyRestController extends RestrictedBaseController {
                 return doConnect(connection);
             }
 
-            checkURI(request, uri, connection, item);
+            checkUri(request, uri, connection, item);
 
             return doConnect(connection);
         } catch (Exception e) {
@@ -350,9 +352,8 @@ public class PolicyRestController extends RestrictedBaseController {
         return null;
     }
 
-    private void checkURI(HttpServletRequest request, String uri, HttpURLConnection connection, FileItem item)
+    private void checkUri(HttpServletRequest request, String uri, HttpURLConnection connection, FileItem item)
             throws IOException {
-        String boundary;
         if (!(uri.endsWith("set_BRMSParamData") || uri.contains(IMPORT_DICTIONARY))) {
             connection.setRequestProperty(CONTENT_TYPE, PolicyController.getContenttype());
             ObjectMapper mapper = new ObjectMapper();
@@ -379,7 +380,7 @@ public class PolicyRestController extends RestrictedBaseController {
                 IOUtils.copy(request.getInputStream(), os);
             }
         } else {
-            boundary = "===" + System.currentTimeMillis() + "===";
+            String boundary = "===" + System.currentTimeMillis() + "===";
             connection.setRequestProperty(CONTENT_TYPE, "multipart/form-data; boundary=" + boundary);
             try (OutputStream os = connection.getOutputStream()) {
                 if (item != null) {
@@ -401,8 +402,7 @@ public class PolicyRestController extends RestrictedBaseController {
 
     private String doConnect(final HttpURLConnection connection) throws IOException {
         connection.connect();
-        int responseCode = connection.getResponseCode();
-        if (responseCode == 200) {
+        if (connection.getResponseCode() == 200) {
             // get the response content into a String
             String responseJson = null;
             // read the inputStream into a buffer (trick found online scans entire input looking for end-of-file)
@@ -424,11 +424,17 @@ public class PolicyRestController extends RestrictedBaseController {
         return null;
     }
 
+    /**
+     * getDictionaryController.
+     *
+     * @param request Request
+     * @param response Response
+     */
     @RequestMapping(value = {"/getDictionary/*"}, method = {RequestMethod.GET})
     public void getDictionaryController(HttpServletRequest request, HttpServletResponse response) {
-        String uri = request.getRequestURI().replace("/getDictionary", "");
         String body;
-        ResponseEntity<?> responseEntity = sendToPAP(null, uri, HttpMethod.GET);
+        ResponseEntity<?> responseEntity = sendToPap(null, request.getRequestURI().replace("/getDictionary", ""),
+                HttpMethod.GET);
         if (responseEntity != null) {
             body = responseEntity.getBody().toString();
         } else {
@@ -441,6 +447,13 @@ public class PolicyRestController extends RestrictedBaseController {
         }
     }
 
+    /**
+     * saveDictionaryController.
+     *
+     * @param request Request
+     * @param response Response
+     * @throws IOException IO Exception
+     */
     @RequestMapping(value = {"/saveDictionary/*/*"}, method = {RequestMethod.POST})
     public void saveDictionaryController(HttpServletRequest request, HttpServletResponse response) throws IOException {
         String userId = "";
@@ -455,12 +468,12 @@ public class PolicyRestController extends RestrictedBaseController {
         }
 
         policyLogger.info(
-                "****************************************Logging UserID while Saving Dictionary*****************************************************");
+                "********************Logging UserID while Saving Dictionary**************************************");
         policyLogger.info(USER_ID + userId);
         policyLogger.info(
-                "***********************************************************************************************************************************");
+                "************************************************************************************************");
 
-        String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
+        String body = callPap(request, "POST", uri.replaceFirst("/", "").trim());
         if (body != null && !body.isEmpty()) {
             response.getWriter().write(body);
         } else {
@@ -468,6 +481,13 @@ public class PolicyRestController extends RestrictedBaseController {
         }
     }
 
+    /**
+     * deletetDictionaryController.
+     *
+     * @param request Request
+     * @param response Response
+     * @throws IOException IO Exception
+     */
     @RequestMapping(value = {"/deleteDictionary/*/*"}, method = {RequestMethod.POST})
     public void deletetDictionaryController(HttpServletRequest request, HttpServletResponse response)
             throws IOException {
@@ -479,12 +499,12 @@ public class PolicyRestController extends RestrictedBaseController {
 
         String userId = UserUtils.getUserSession(request).getOrgUserId();
         policyLogger.info(
-                "****************************************Logging UserID while Deleting Dictionary*****************************************************");
+                "**********************Logging UserID while Deleting Dictionary*************************************");
         policyLogger.info(USER_ID + userId);
         policyLogger.info(
-                "*************************************************************************************************************************************");
+                "***************************************************************************************************");
 
-        String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
+        String body = callPap(request, "POST", uri.replaceFirst("/", "").trim());
         if (body != null && !body.isEmpty()) {
             response.getWriter().write(body);
         } else {
@@ -492,6 +512,14 @@ public class PolicyRestController extends RestrictedBaseController {
         }
     }
 
+    /**
+     * searchDictionaryController.
+     *
+     * @param request Request
+     * @param response Response
+     * @return ModelAndView object
+     * @throws IOException IO Exception
+     */
     @RequestMapping(value = {"/searchDictionary"}, method = {RequestMethod.POST})
     public ModelAndView searchDictionaryController(HttpServletRequest request, HttpServletResponse response)
             throws IOException {
@@ -502,14 +530,13 @@ public class PolicyRestController extends RestrictedBaseController {
         }
         uri = ONAP + uri.substring(uri.indexOf('/'));
         try {
-            String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
+            String body = callPap(request, "POST", uri.replaceFirst("/", "").trim());
             if (body.contains("CouldNotConnectException")) {
                 List<String> data = new ArrayList<>();
                 data.add("Elastic Search Server is down");
                 resultList = data;
             } else {
-                JSONObject json = new JSONObject(body);
-                resultList = json.get("policyresult");
+                resultList = new JSONObject(body).get("policyresult");
             }
         } catch (Exception e) {
             policyLogger.error(
@@ -521,12 +548,18 @@ public class PolicyRestController extends RestrictedBaseController {
 
         response.setCharacterEncoding(PolicyController.getCharacterencoding());
         response.setContentType(PolicyController.getContenttype());
-        PrintWriter out = response.getWriter();
-        JSONObject j = new JSONObject("{result: " + resultList + "}");
-        out.write(j.toString());
+        response.getWriter().write(new JSONObject("{result: " + resultList + "}").toString());
         return null;
     }
 
+    /**
+     * searchPolicy.
+     *
+     * @param request request
+     * @param response response
+     * @return ModelAndView object
+     * @throws IOException IO exception
+     */
     @RequestMapping(value = {"/searchPolicy"}, method = {RequestMethod.POST})
     public ModelAndView searchPolicy(HttpServletRequest request, HttpServletResponse response) throws IOException {
         Object resultList;
@@ -535,7 +568,7 @@ public class PolicyRestController extends RestrictedBaseController {
             uri = uri.substring(uri.indexOf('/') + 1);
         }
         uri = ONAP + uri.substring(uri.indexOf('/'));
-        String body = callPAP(request, "POST", uri.replaceFirst("/", "").trim());
+        String body = callPap(request, "POST", uri.replaceFirst("/", "").trim());
 
         JSONObject json = new JSONObject(body);
         try {
@@ -553,21 +586,27 @@ public class PolicyRestController extends RestrictedBaseController {
         response.setContentType("application / json");
         request.setCharacterEncoding(UTF_8);
 
-        PrintWriter out = response.getWriter();
-        JSONObject j = new JSONObject("{result: " + resultList + "}");
-        out.write(j.toString());
+        response.getWriter().write(new JSONObject("{result: " + resultList + "}").toString());
         return null;
     }
 
     public void deleteElasticData(String fileName) {
         String uri = "searchPolicy?action=delete&policyName='" + fileName + "'";
-        callPAP(null, "POST", uri.trim());
+        callPap(null, "POST", uri.trim());
     }
 
-    public String notifyOtherPAPSToUpdateConfigurations(String mode, String newName, String oldName) {
+    /**
+     * notifyOtherPAPSToUpdateConfigurations.
+     *
+     * @param mode Mode
+     * @param newName New Name
+     * @param oldName Old Name
+     * @return String
+     */
+    public String notifyOtherPapsToUpdateConfigurations(String mode, String newName, String oldName) {
         String uri =
                 "onap/notifyOtherPAPs?action=" + mode + "&newPolicyName=" + newName + "&oldPolicyName=" + oldName + "";
-        return callPAP(null, "POST", uri.trim());
+        return callPap(null, "POST", uri.trim());
     }
 
 }