Merge "Continue addressing technical debt for ONAP-XACML"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyManagerServlet.java
index 91f8293..54a14cb 100644 (file)
@@ -25,7 +25,6 @@ import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.FileWriter;
 import java.io.IOException;
@@ -69,7 +68,6 @@ import org.onap.policy.common.logging.flexlogger.Logger;
 import org.onap.policy.components.HumanPolicyComponent;
 import org.onap.policy.controller.PolicyController;
 import org.onap.policy.controller.PolicyExportAndImportController;
-import org.onap.policy.model.Roles;
 import org.onap.policy.rest.XACMLRest;
 import org.onap.policy.rest.XACMLRestProperties;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
@@ -80,6 +78,7 @@ import org.onap.policy.rest.jpa.PolicyEntity;
 import org.onap.policy.rest.jpa.PolicyVersion;
 import org.onap.policy.rest.jpa.UserInfo;
 import org.onap.policy.utils.PolicyUtils;
+import org.onap.policy.utils.UserUtils.Pair;
 import org.onap.policy.xacml.api.XACMLErrorConstants;
 import org.onap.policy.xacml.util.XACMLPolicyScanner;
 import org.onap.portalsdk.core.web.support.UserUtils;
@@ -150,34 +149,22 @@ public class PolicyManagerServlet extends HttpServlet {
        protected static void initializeJSONLoad() {
                closedLoopJsonLocation = Paths.get(XACMLProperties
                                .getProperty(XACMLRestProperties.PROP_ADMIN_CLOSEDLOOP));
-               FileInputStream inputStream = null;
-               JsonReader jsonReader = null;
                String location = closedLoopJsonLocation.toString();
-               try {
-                       inputStream = new FileInputStream(location);
-                       if (location.endsWith("json")) {
-                               jsonReader = Json.createReader(inputStream);
-                               policyNames = jsonReader.readArray();
-                               serviceTypeNamesList = new ArrayList<>();
-                               for (int i = 0; i < policyNames.size(); i++) {
-                                       javax.json.JsonObject policyName = policyNames.getJsonObject(i);
-                                       String name = policyName.getJsonString("serviceTypePolicyName").getString();
-                                       serviceTypeNamesList.add(name);
-                               }
+               if (! location.endsWith("json")) {
+                       LOGGER.warn("JSONConfig file does not end with extension .json");
+                       return;
+               }
+               try (FileInputStream inputStream = new FileInputStream(location);
+                       JsonReader jsonReader = Json.createReader(inputStream)) {
+                       policyNames = jsonReader.readArray();
+                       serviceTypeNamesList = new ArrayList<>();
+                       for (int i = 0; i < policyNames.size(); i++) {
+                               javax.json.JsonObject policyName = policyNames.getJsonObject(i);
+                               String name = policyName.getJsonString("serviceTypePolicyName").getString();
+                               serviceTypeNamesList.add(name);
                        }
-               } catch (FileNotFoundException e) {
+               } catch (IOException e) {
                        LOGGER.error("Exception Occured while initializing the JSONConfig file"+e);
-               }finally{
-                       try {
-                               if(inputStream != null){
-                                       inputStream.close();
-                               }
-                               if(jsonReader != null){
-                                       jsonReader.close();
-                               }
-                       } catch (IOException e) {
-                               LOGGER.error("Exception Occured while closing the File InputStream"+e);
-                       }
                }
        }
 
@@ -248,7 +235,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
 
-                       JSONObject responseJsonObject = null;
+                       JSONObject responseJsonObject;
                        responseJsonObject = this.success();
                        response.setContentType(CONTENTTYPE);
                        PrintWriter out = response.getWriter();
@@ -327,8 +314,8 @@ public class PolicyManagerServlet extends HttpServlet {
        }
 
        private JSONObject searchPolicyList(JSONObject params, HttpServletRequest request) {
-               Set<String> scopes = null;
-               List<String> roles = null;
+               Set<String> scopes;
+               List<String> roles;
                List<Object> policyData = new ArrayList<>();
                JSONArray policyList = null;
                if(params.has("policyList")){
@@ -339,24 +326,10 @@ public class PolicyManagerServlet extends HttpServlet {
                try {
                        //Get the Login Id of the User from Request
                        String userId =  UserUtils.getUserSession(request).getOrgUserId();
-                       //Check if the Role and Scope Size are Null get the values from db.
                        List<Object> userRoles = controller.getRoles(userId);
-                       roles = new ArrayList<>();
-                       scopes = new HashSet<>();
-                       for(Object role: userRoles){
-                               Roles userRole = (Roles) role;
-                               roles.add(userRole.getRole());
-                               if(userRole.getScope() != null){
-                                       if(userRole.getScope().contains(",")){
-                                               String[] multipleScopes = userRole.getScope().split(",");
-                                               for(int i =0; i < multipleScopes.length; i++){
-                                                       scopes.add(multipleScopes[i]);
-                                               }
-                                       }else{
-                                               scopes.add(userRole.getScope());
-                                       }
-                               }
-                       }
+                       Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+                       roles = pair.u;
+                       scopes = pair.t;
                        if (roles.contains(ADMIN) || roles.contains(EDITOR) || roles.contains(GUEST) ) {
                                if(scopes.isEmpty()){
                                        return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
@@ -455,7 +428,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        policyName = removeExtension.substring(0, removeExtension.lastIndexOf('.'));
                }
 
-               String activePolicy = null;
+               String activePolicy;
                PolicyController controller = getPolicyControllerInstance();
                if(params.toString().contains("activeVersion")){
                        String activeVersion = params.getString("activeVersion");
@@ -585,24 +558,10 @@ public class PolicyManagerServlet extends HttpServlet {
                        //Get the Login Id of the User from Request
                        String testUserID = getTestUserId();
                        String userId =  testUserID != null ? testUserID : UserUtils.getUserSession(request).getOrgUserId();
-                       //Check if the Role and Scope Size are Null get the values from db.
                        List<Object> userRoles = controller.getRoles(userId);
-                       roles = new ArrayList<>();
-                       scopes = new HashSet<>();
-                       for(Object role: userRoles){
-                               Roles userRole = (Roles) role;
-                               roles.add(userRole.getRole());
-                               if(userRole.getScope() != null){
-                                       if(userRole.getScope().contains(",")){
-                                               String[] multipleScopes = userRole.getScope().split(",");
-                                               for(int i =0; i < multipleScopes.length; i++){
-                                                       scopes.add(multipleScopes[i]);
-                                               }
-                                       }else{
-                                               scopes.add(userRole.getScope());
-                                       }
-                               }
-                       }
+                       Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+                       roles = pair.u;
+                       scopes = pair.t;
 
                        List<JSONObject> resultList = new ArrayList<>();
                        boolean onlyFolders = params.getBoolean("onlyFolders");
@@ -672,7 +631,7 @@ public class PolicyManagerServlet extends HttpServlet {
        }
 
        private List<Object> queryPolicyEditorScopes(String scopeName){
-               String scopeNamequery = "";
+               String scopeNamequery;
                SimpleBindings params = new SimpleBindings();
                if(scopeName == null){
                        scopeNamequery = "from PolicyEditorScopes";
@@ -681,7 +640,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        params.put("scopeName", scopeName + "%");
                }
                PolicyController controller = getPolicyControllerInstance();
-               List<Object> scopesList = null;
+               List<Object> scopesList;
                if(PolicyController.isjUnit()){
                        scopesList = controller.getDataByQuery(scopeNamequery, null);
                }else{
@@ -705,8 +664,8 @@ public class PolicyManagerServlet extends HttpServlet {
                SimpleBindings params = new SimpleBindings();
                params.put("scopeName", scopeName + "%");
 
-               List<Object> activePolicies = null;
-               List<Object> scopesList = null;
+               List<Object> activePolicies;
+               List<Object> scopesList;
                if(PolicyController.isjUnit()){
                        activePolicies = controller.getDataByQuery(query, null);
                        scopesList = controller.getDataByQuery(scopeNamequery, null);
@@ -740,7 +699,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
                }
-               String scopeNameCheck = null;
+               String scopeNameCheck;
                for (Object list : activePolicies) {
                        PolicyVersion policy = (PolicyVersion) list;
                        String scopeNameValue = policy.getPolicyName().substring(0, policy.getPolicyName().lastIndexOf(File.separator));
@@ -875,7 +834,7 @@ public class PolicyManagerServlet extends HttpServlet {
 
        private JSONObject policyRename(String oldPath, String newPath, String userId) throws ServletException {
                try {
-                       PolicyEntity entity = null;
+                       PolicyEntity entity;
                        PolicyController controller = getPolicyControllerInstance();
 
                        String policyVersionName = newPath.replace(".xml", "");
@@ -913,7 +872,6 @@ public class PolicyManagerServlet extends HttpServlet {
                        policyParams.put("newPolicySplit_0", newPolicySplit[0]);
                        List<Object> queryData = controller.getDataByQuery(policyEntityquery, policyParams);
                        if(!queryData.isEmpty()){
-                               entity = (PolicyEntity) queryData.get(0);
                                return error("Policy rename failed. Since, the policy with same name already exists.");
                        }
 
@@ -925,20 +883,21 @@ public class PolicyManagerServlet extends HttpServlet {
                        params.put("oldPolicySplit_0", oldPolicySplit[0]);
                        List<Object> oldEntityData = controller.getDataByQuery(oldpolicyEntityquery, params);
                        if(!oldEntityData.isEmpty()){
-                               String groupQuery = "FROM PolicyGroupEntity where (";
+                               StringBuilder groupQuery = new StringBuilder();
+                               groupQuery.append("FROM PolicyGroupEntity where (");
                                SimpleBindings geParams = new SimpleBindings();
                                for(int i=0; i<oldEntityData.size(); i++){
                                        entity = (PolicyEntity) oldEntityData.get(i);
                                        if(i == 0){
-                                               groupQuery = groupQuery +  "policyid = :policyId";
+                                               groupQuery.append("policyid = :policyId");
                                                geParams.put("policyId", entity.getPolicyId());
                                        }else{
-                                               groupQuery = groupQuery +  " or policyid = :policyId" + i;
+                                               groupQuery.append(" or policyid = :policyId" + i);
                                                geParams.put("policyId" + i, entity.getPolicyId());
                                        }
                                }
-                               groupQuery = groupQuery + ")";
-                               List<Object> groupEntityData = controller.getDataByQuery(groupQuery, geParams);
+                               groupQuery.append(")");
+                               List<Object> groupEntityData = controller.getDataByQuery(groupQuery.toString(), geParams);
                                if(! groupEntityData.isEmpty()){
                                        return error("Policy rename failed. Since the policy or its version is active in PDP Groups.");
                                }
@@ -1017,7 +976,7 @@ public class PolicyManagerServlet extends HttpServlet {
 
        private JSONObject cloneRecord(String newpolicyName, String oldScope, String removeoldPolicyExtension, String newScope, String removenewPolicyExtension, PolicyEntity entity, String userId) throws ServletException{
                FileWriter fw = null;
-               String queryEntityName = null;
+               String queryEntityName;
                PolicyController controller = getPolicyControllerInstance();
                PolicyEntity cloneEntity = new PolicyEntity();
                cloneEntity.setPolicyName(newpolicyName);
@@ -1187,7 +1146,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        path = path.substring(path.indexOf('/')+1);
                        String policyNamewithExtension = path.replace("/", File.separator);
                        String policyVersionName = policyNamewithExtension.replace(".xml", "");
-                       String query = "";
+                       String query;
                        SimpleBindings policyParams = new SimpleBindings();
                        if(path.endsWith(".xml")){
                                policyNamewithoutExtension = policyVersionName.substring(0, policyVersionName.lastIndexOf('.'));
@@ -1426,7 +1385,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        SimpleBindings peParams = new SimpleBindings();
                        peParams.put("split_1", split[1]);
                        peParams.put("split_0", split[0]);
-                       List<Object> queryData = null;
+                       List<Object> queryData;
                        if(PolicyController.isjUnit()){
                                queryData = controller.getDataByQuery(query, null);
                        }else{
@@ -1478,7 +1437,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        String path = params.getString("path");
                        try{
                                if(params.has("subScopename")){
-                                       if(!params.getString("subScopename").equals("")){
+                                       if(! "".equals(params.getString("subScopename"))) {
                                                name = params.getString("path").replace("/", File.separator) + File.separator +params.getString("subScopename");
                                        }
                                }else{
@@ -1501,7 +1460,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                }
                        }
                        LOGGER.debug("addFolder path: {} name: {}" + path +name);
-                       if(!name.equals("")){
+                       if(! "".equals(name)){
                                if(name.startsWith(File.separator)){
                                        name = name.substring(1);
                                }