[Policy-52, Policy-92, Policy-93] Policy Enhancements and bugfixes
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / admin / PolicyManagerServlet.java
index fdf4326..1821e10 100644 (file)
@@ -95,13 +95,13 @@ public class PolicyManagerServlet extends HttpServlet {
                LIST, RENAME, COPY, DELETE, EDITFILE, ADDFOLDER, DESCRIBEPOLICYFILE, VIEWPOLICY, ADDSUBSCOPE, SWITCHVERSION, EXPORT, SEARCHLIST
        }
 
-       private PolicyController policyController;
-       public PolicyController getPolicyController() {
+       private static PolicyController policyController;
+       public synchronized PolicyController getPolicyController() {
                return policyController;
        }
 
-       public void setPolicyController(PolicyController policyController) {
-               this.policyController = policyController;
+       public synchronized static void setPolicyController(PolicyController policyController) {
+               PolicyManagerServlet.policyController = policyController;
        }
 
        private static String CONTENTTYPE = "application/json";
@@ -115,7 +115,7 @@ public class PolicyManagerServlet extends HttpServlet {
        
        private static Path closedLoopJsonLocation;
        private static JsonArray policyNames;
-       private String testUserId = null;
+       private static String testUserId = null;
        
        public static JsonArray getPolicyNames() {
                return policyNames;
@@ -126,7 +126,6 @@ public class PolicyManagerServlet extends HttpServlet {
        }
 
        private static List<String> serviceTypeNamesList = new ArrayList<>();
-       private List<Object> policyData;
 
        public static List<String> getServiceTypeNamesList() {
                return serviceTypeNamesList;
@@ -192,7 +191,11 @@ public class PolicyManagerServlet extends HttpServlet {
                                fileOperation(request, response);
                        }
                } catch (Exception e) {
-                       setError(e, response);
+                       try {
+                               setError(e, response);
+                       }catch(Exception e1){
+                               LOGGER.error("Exception Occured"+e1);
+                       }
                }
        }
 
@@ -270,6 +273,7 @@ public class PolicyManagerServlet extends HttpServlet {
                        Mode mode = Mode.valueOf(params.getString("mode"));
                        switch (mode) {
                        case ADDFOLDER:
+                       case ADDSUBSCOPE:
                                responseJsonObject = addFolder(params, request);
                                break;
                        case COPY:
@@ -278,12 +282,10 @@ public class PolicyManagerServlet extends HttpServlet {
                        case DELETE:
                                responseJsonObject = delete(params, request);
                                break;
-                       case EDITFILE: 
+                       case EDITFILE:
+                       case VIEWPOLICY:
                                responseJsonObject = editFile(params);
                                break;
-                       case VIEWPOLICY: 
-                               responseJsonObject = editFile(params);
-                               break;  
                        case LIST:
                                responseJsonObject = list(params, request);
                                break;
@@ -293,9 +295,6 @@ public class PolicyManagerServlet extends HttpServlet {
                        case DESCRIBEPOLICYFILE:
                                responseJsonObject = describePolicy(params);
                                break;
-                       case ADDSUBSCOPE:
-                               responseJsonObject = addFolder(params, request);
-                               break;
                        case SWITCHVERSION:
                                responseJsonObject = switchVersion(params, request);
                                break;
@@ -321,7 +320,7 @@ public class PolicyManagerServlet extends HttpServlet {
        private JSONObject searchPolicyList(JSONObject params, HttpServletRequest request) {
                Set<String> scopes = null;
                List<String> roles = null;
-               policyData = new ArrayList<>();
+               List<Object> policyData = new ArrayList<>();
                JSONArray policyList = null;
                if(params.has("policyList")){
                        policyList = (JSONArray) params.get("policyList");
@@ -353,6 +352,16 @@ public class PolicyManagerServlet extends HttpServlet {
                                if(scopes.isEmpty()){
                                        return error("No Scopes has been Assigned to the User. Please, Contact Super-Admin");
                                }
+                               Set<String> tempScopes = scopes;
+                               for(String scope : tempScopes){
+                                       List<Object> scopesList = queryPolicyEditorScopes(scope);
+                                       if(!scopesList.isEmpty()){
+                                               for(int i = 0; i < scopesList.size(); i++){
+                                                       PolicyEditorScopes tempScope = (PolicyEditorScopes) scopesList.get(i);
+                                                       scopes.add(tempScope.getScopeName());
+                                               }
+                                       }
+                               }
                        } 
                        if(policyList!= null){
                                for(int i = 0; i < policyList.length(); i++){
@@ -384,9 +393,13 @@ public class PolicyManagerServlet extends HttpServlet {
                                        List<Object> filterdatas = controller.getData(PolicyVersion.class);
                                        for(Object filter : filterdatas){
                                                PolicyVersion filterdata = (PolicyVersion) filter;
-                                               String scopeName = filterdata.getPolicyName().substring(0, filterdata.getPolicyName().lastIndexOf(File.separator));
-                                               if(scopes.contains(scopeName)){ 
-                                                       policyData.add(filterdata);
+                                               try{
+                                                       String scopeName = filterdata.getPolicyName().substring(0, filterdata.getPolicyName().lastIndexOf(File.separator));
+                                                       if(scopes.contains(scopeName)){ 
+                                                               policyData.add(filterdata);
+                                                       }
+                                               }catch(Exception e){
+                                                       LOGGER.error("Exception occured while filtering policyversion data"+e);
                                                }
                                        }
                                }
@@ -567,11 +580,6 @@ public class PolicyManagerServlet extends HttpServlet {
                                        }               
                                }
                        }
-                       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");
-                               }
-                       } 
 
                        List<JSONObject> resultList = new ArrayList<>();
                        boolean onlyFolders = params.getBoolean("onlyFolders");
@@ -580,7 +588,18 @@ public class PolicyManagerServlet extends HttpServlet {
                                path = path.replaceAll("..xml", "").trim();
                        }
 
-
+                       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");
+                               }else{
+                                       if(!"/".equals(path)){
+                                               String tempScope = path.substring(1, path.length());
+                                               tempScope = tempScope.replace("/", File.separator);
+                                               scopes.add(tempScope);
+                                       }
+                               }
+                       } 
+                       
                        if("/".equals(path)){
                                if(roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)){
                                        List<Object> scopesList = queryPolicyEditorScopes(null);
@@ -601,14 +620,16 @@ public class PolicyManagerServlet extends HttpServlet {
                                        for(Object scope : scopes){
                                                JSONObject el = new JSONObject();
                                                List<Object> scopesList = queryPolicyEditorScopes(scope.toString());
-                                               PolicyEditorScopes scopeById = (PolicyEditorScopes) scopesList.get(0);
-                                               el.put("name", scopeById.getScopeName());       
-                                               el.put("date", scopeById.getCreatedDate());
-                                               el.put("size", "");
-                                               el.put("type", "dir");
-                                               el.put("createdBy", scopeById.getUserCreatedBy().getUserName());
-                                               el.put("modifiedBy", scopeById.getUserModifiedBy().getUserName());
-                                               resultList.add(el);
+                                               if(!scopesList.isEmpty()){
+                                                       PolicyEditorScopes scopeById = (PolicyEditorScopes) scopesList.get(0);
+                                                       el.put("name", scopeById.getScopeName());       
+                                                       el.put("date", scopeById.getCreatedDate());
+                                                       el.put("size", "");
+                                                       el.put("type", "dir");
+                                                       el.put("createdBy", scopeById.getUserCreatedBy().getUserName());
+                                                       el.put("modifiedBy", scopeById.getUserModifiedBy().getUserName());
+                                                       resultList.add(el);
+                                               }
                                        }
                                }
                        }else{
@@ -632,7 +653,7 @@ public class PolicyManagerServlet extends HttpServlet {
                if(scopeName == null){
                        scopeNamequery = "from PolicyEditorScopes";
                }else{
-                       scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"'";
+                       scopeNamequery = "from PolicyEditorScopes where SCOPENAME like'" +scopeName+"%'";
                }
                PolicyController controller = getPolicyControllerInstance();
                List<Object> scopesList = controller.getDataByQuery(scopeNamequery);
@@ -945,6 +966,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;
                PolicyController controller = getPolicyControllerInstance();
                PolicyEntity cloneEntity = new PolicyEntity();
@@ -967,6 +989,15 @@ public class PolicyManagerServlet extends HttpServlet {
                        controller.saveData(configurationDataEntity);
                        ConfigurationDataEntity configEntiy = (ConfigurationDataEntity) controller.getEntityItem(ConfigurationDataEntity.class, "configurationName", queryEntityName);
                        cloneEntity.setConfigurationData(configEntiy);
+                       String newConfigurationName = configEntiy.getConfigurationName();
+                       try {
+                               fw = new FileWriter(PolicyController.getConfigHome() + File.separator + newConfigurationName);
+                               BufferedWriter bw = new BufferedWriter(fw);
+                               bw.write(configEntiy.getConfigBody());
+                               bw.close();
+                       } catch (IOException e) {
+                               LOGGER.error("Exception Occured While cloning the configuration file"+e);
+                       }
                }else if(newpolicyName.contains("Action_")){
                        ActionBodyEntity actionBodyEntity = new ActionBodyEntity();
                        actionBodyEntity.setActionBodyName(entity.getActionBodyEntity().getActionBodyName().replace(oldScope+"."+oldConfigRemoveExtension, newScope+"."+newConfigRemoveExtension));
@@ -978,6 +1009,22 @@ public class PolicyManagerServlet extends HttpServlet {
                        controller.saveData(actionBodyEntity);
                        ActionBodyEntity actionEntiy = (ActionBodyEntity) controller.getEntityItem(ActionBodyEntity.class, "actionBodyName", queryEntityName);
                        cloneEntity.setActionBodyEntity(actionEntiy);
+                       String newConfigurationName = actionEntiy.getActionBodyName();
+                       try {
+                               fw = new FileWriter(PolicyController.getActionHome() + File.separator + newConfigurationName);
+                               BufferedWriter bw = new BufferedWriter(fw);
+                               bw.write(actionEntiy.getActionBody());
+                               bw.close();
+                       } catch (IOException e) {
+                               LOGGER.error("Exception Occured While cloning the configuration file"+e);
+                       }
+               }
+               if(fw != null){
+                       try {
+                               fw.close();
+                       } catch (IOException e) {
+                               LOGGER.error("Exception Occured While closing the File input stream"+e);
+                       }
                }
                cloneEntity.setDeleted(entity.isDeleted());
                cloneEntity.setCreatedBy(userId);
@@ -1032,46 +1079,21 @@ public class PolicyManagerServlet extends HttpServlet {
                        //Check PolicyEntity table with newPolicy Name
                        String policyEntityquery = "FROM PolicyEntity where policyName = '"+newPolicySplit[1]+"' and scope ='"+newPolicySplit[0]+"'";
                        List<Object> queryData = controller.getDataByQuery(policyEntityquery);
+                       if(!queryData.isEmpty()){
+                               return error("Policy already exists with same name");
+                       }
+
+                       //Query the Policy Entity with oldPolicy Name
+                       policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
+                       queryData = controller.getDataByQuery(policyEntityquery);
                        if(!queryData.isEmpty()){
                                entity = (PolicyEntity) queryData.get(0);
                        }
                        if(entity != null){
-                               //if a policy exists with new name check if it is deleted or not
-                               if(entity.isDeleted()){
-                                       //Check Policy Group Entity table if policy has been pushed or not
-                                       String query = "from PolicyGroupEntity where policyid = '"+entity.getPolicyId()+"'";
-                                       List<Object> object = controller.getDataByQuery(query);
-                                       if(object == null){
-                                               //if PolicyGroupEntity data is empty delete the entry from database
-                                               controller.deleteData(entity);
-                                               //Query the Policy Entity with oldPolicy Name
-                                               policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
-                                               queryData = controller.getDataByQuery(policyEntityquery);
-                                               if(!queryData.isEmpty()){
-                                                       entity = (PolicyEntity) queryData.get(0);
-                                               }
-                                               if(entity != null){
-                                                       cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1], newPolicySplit[0], newPolicySplit[1], entity, userId);
-                                                       success = true;
-                                               }
-                                       }else{
-                                               return error("Policy Clone failed due to policy with new name existing in PDP Group.");
-                                       }
-                               }else{
-                                       return error("Policy Clone failed due to same name existing.");
-                               }
-                       }else{
-                               //Query the Policy Entity with oldPolicy Name
-                               policyEntityquery = "FROM PolicyEntity where policyName = '"+oldPolicySplit[1]+"' and scope ='"+oldPolicySplit[0]+"'";
-                               queryData = controller.getDataByQuery(policyEntityquery);
-                               if(!queryData.isEmpty()){
-                                       entity = (PolicyEntity) queryData.get(0);
-                               }
-                               if(entity != null){
-                                       cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1],  newPolicySplit[0], newPolicySplit[1], entity, userId);
-                                       success = true;
-                               }
+                               cloneRecord(newPolicySplit[1], oldPolicySplit[0], oldPolicySplit[1],  newPolicySplit[0], newPolicySplit[1], entity, userId);
+                               success = true;
                        }
+                       
                        if(success){
                                PolicyVersion entityItem = new PolicyVersion();
                                entityItem.setActiveVersion(Integer.parseInt(version));
@@ -1207,7 +1229,7 @@ public class PolicyManagerServlet extends HttpServlet {
                                                                                policyEntity = (PolicyEntity) object;
                                                                                String policyEntityName = policyEntity.getPolicyName().replace(".xml", "");
                                                                                int policyEntityVersion = Integer.parseInt(policyEntityName.substring(policyEntityName.lastIndexOf(".")+1));
-                                                                               if(policyEntityVersion > highestVersion){
+                                                                               if(policyEntityVersion > highestVersion && policyEntityVersion != version){
                                                                                        highestVersion = policyEntityVersion;
                                                                                }
                                                                        }
@@ -1394,18 +1416,15 @@ public class PolicyManagerServlet extends HttpServlet {
                        }
                        LOGGER.debug("addFolder path: {} name: {}" + path +name);
                        if(!name.equals("")){
+                               if(name.startsWith(File.separator)){
+                                       name = name.substring(1);
+                               }
                                PolicyEditorScopes entity = (PolicyEditorScopes) controller.getEntityItem(PolicyEditorScopes.class, "scopeName", name);
                                if(entity == null){
                                        UserInfo userInfo = new UserInfo();
                                        userInfo.setUserLoginId(userId);
                                        PolicyEditorScopes newScope = new PolicyEditorScopes();
-                                       String scopeName = null;
-                                       if(name.startsWith(File.separator)){
-                                               scopeName = name.substring(1);
-                                       }else{
-                                               scopeName = name;
-                                       } 
-                                       newScope.setScopeName(scopeName);
+                                       newScope.setScopeName(name);
                                        newScope.setUserCreatedBy(userInfo);
                                        newScope.setUserModifiedBy(userInfo);
                                        controller.saveData(newScope);  
@@ -1452,7 +1471,7 @@ public class PolicyManagerServlet extends HttpServlet {
                return testUserId;
        }
 
-       public void setTestUserId(String testUserId) {
-               this.testUserId = testUserId;
+       public static void setTestUserId(String testUserId) {
+               PolicyManagerServlet.testUserId = testUserId;
        }
 }
\ No newline at end of file