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;
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;
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;
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);
- }
}
}
}
}
- JSONObject responseJsonObject = null;
+ JSONObject responseJsonObject;
responseJsonObject = this.success();
response.setContentType(CONTENTTYPE);
PrintWriter out = response.getWriter();
}
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")){
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");
policyName = removeExtension.substring(0, removeExtension.lastIndexOf('.'));
}
- String activePolicy = null;
+ String activePolicy;
PolicyController controller = getPolicyControllerInstance();
- if(params.toString().contains("activeVersion")){
- String activeVersion = params.getString("activeVersion");
- String highestVersion = params.get("highestVersion").toString();
- if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){
- return error("The Version shouldn't be greater than Highest Value");
- }else{
- activePolicy = policyName + "." + activeVersion + ".xml";
- String dbCheckName = activePolicy.replace("/", ".");
- if(dbCheckName.contains("Config_")){
- dbCheckName = dbCheckName.replace(".Config_", ":Config_");
- }else if(dbCheckName.contains("Action_")){
- dbCheckName = dbCheckName.replace(".Action_", ":Action_");
- }else if(dbCheckName.contains("Decision_")){
- dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
- }
- String[] splitDBCheckName = dbCheckName.split(":");
- String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0";
- SimpleBindings policyParams = new SimpleBindings();
- policyParams.put("splitDBCheckName_1", splitDBCheckName[1]);
- policyParams.put("splitDBCheckName_0", splitDBCheckName[0]);
- List<Object> policyEntity = controller.getDataByQuery(peQuery, policyParams);
- PolicyEntity pentity = (PolicyEntity) policyEntity.get(0);
- if(pentity.isDeleted()){
- return error("The Policy is Not Existing in Workspace");
- }else{
- if(policyName.contains("/")){
- policyName = policyName.replace("/", File.separator);
- }
- policyName = policyName.substring(policyName.indexOf(File.separator)+1);
- if(policyName.contains("\\")){
- policyName = policyName.replace(File.separator, "\\");
- }
- policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName;
- String watchPolicyName = policyName;
- if(policyName.contains("/")){
- policyName = policyName.replace("/", File.separator);
- }
- if(policyName.contains("\\")){
- policyName = policyName.replace("\\", "\\\\");
- }
- String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0";
- //query the database
- controller.executeQuery(query);
- //Policy Notification
- PolicyVersion entity = new PolicyVersion();
- entity.setPolicyName(watchPolicyName);
- entity.setActiveVersion(Integer.parseInt(activeVersion));
- entity.setModifiedBy(userId);
- controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion");
- return success();
- }
- }
+ if(! params.toString().contains("activeVersion")){
+ return controller.switchVersionPolicyContent(policyName);
+ }
+ String activeVersion = params.getString("activeVersion");
+ String highestVersion = params.get("highestVersion").toString();
+ if(Integer.parseInt(activeVersion) > Integer.parseInt(highestVersion)){
+ return error("The Version shouldn't be greater than Highest Value");
}
- return controller.switchVersionPolicyContent(policyName);
+ activePolicy = policyName + "." + activeVersion + ".xml";
+ String dbCheckName = activePolicy.replace("/", ".");
+ if(dbCheckName.contains("Config_")){
+ dbCheckName = dbCheckName.replace(".Config_", ":Config_");
+ }else if(dbCheckName.contains("Action_")){
+ dbCheckName = dbCheckName.replace(".Action_", ":Action_");
+ }else if(dbCheckName.contains("Decision_")){
+ dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
+ }
+ String[] splitDBCheckName = dbCheckName.split(":");
+ String peQuery = "FROM PolicyEntity where policyName = :splitDBCheckName_1 and scope = :splitDBCheckName_0";
+ SimpleBindings policyParams = new SimpleBindings();
+ policyParams.put("splitDBCheckName_1", splitDBCheckName[1]);
+ policyParams.put("splitDBCheckName_0", splitDBCheckName[0]);
+ List<Object> policyEntity = controller.getDataByQuery(peQuery, policyParams);
+ PolicyEntity pentity = (PolicyEntity) policyEntity.get(0);
+ if(pentity.isDeleted()){
+ return error("The Policy is Not Existing in Workspace");
+ }
+ if(policyName.contains("/")){
+ policyName = policyName.replace("/", File.separator);
+ }
+ policyName = policyName.substring(policyName.indexOf(File.separator)+1);
+ if(policyName.contains("\\")){
+ policyName = policyName.replace(File.separator, "\\");
+ }
+ policyName = splitDBCheckName[0].replace(".", File.separator)+File.separator+policyName;
+ String watchPolicyName = policyName;
+ if(policyName.contains("/")){
+ policyName = policyName.replace("/", File.separator);
+ }
+ if(policyName.contains("\\")){
+ policyName = policyName.replace("\\", "\\\\");
+ }
+ String query = "update PolicyVersion set active_version='"+activeVersion+"' where policy_name ='"+policyName+"' and id >0";
+ //query the database
+ controller.executeQuery(query);
+ //Policy Notification
+ PolicyVersion entity = new PolicyVersion();
+ entity.setPolicyName(watchPolicyName);
+ entity.setActiveVersion(Integer.parseInt(activeVersion));
+ entity.setModifiedBy(userId);
+ controller.watchPolicyFunction(entity, activePolicy, "SwitchVersion");
+ return success();
}
//Describe Policy
}else{
queryData = controller.getDataByQuery(query, peParams);
}
- if(!queryData.isEmpty()){
- PolicyEntity entity = (PolicyEntity) queryData.get(0);
- File temp = null;
- BufferedWriter bw = null;
- try {
- temp = File.createTempFile(policyName, ".tmp");
- bw = new BufferedWriter(new FileWriter(temp));
- bw.write(entity.getPolicyData());
- bw.close();
- object = HumanPolicyComponent.DescribePolicy(temp);
- } catch (IOException e) {
- LOGGER.error("Exception Occured while Describing the Policy"+e);
- }finally{
- if(temp != null){
- temp.delete();
- }
- if(bw != null){
- try {
- bw.close();
- } catch (IOException e) {
- LOGGER.error("Exception Occured while Closing the File Writer"+e);
- }
+ if(queryData.isEmpty()){
+ return error("Error Occured while Describing the Policy - query is empty");
+ }
+ PolicyEntity entity = (PolicyEntity) queryData.get(0);
+ File temp = null;
+ try {
+ temp = File.createTempFile(policyName, ".tmp");
+ } catch (IOException e) {
+ String message = "Failed to create temp file " + policyName + ".tmp";
+ LOGGER.error(message + e);
+ return error(message);
+ }
+ try (BufferedWriter bw = new BufferedWriter(new FileWriter(temp))) {
+ bw.write(entity.getPolicyData());
+ object = HumanPolicyComponent.DescribePolicy(temp);
+ } catch (IOException e) {
+ LOGGER.error("Exception Occured while Describing the Policy"+e);
+ }finally{
+ if(temp != null){
+ try {
+ Files.delete(temp.toPath());
+ } catch (IOException e) {
+ LOGGER.warn("Failed to delete " + temp.getName() + e);
}
}
- }else{
- return error("Error Occured while Describing the Policy");
}
-
return object;
}
//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");
}
private List<Object> queryPolicyEditorScopes(String scopeName){
- String scopeNamequery = "";
+ String scopeNamequery;
SimpleBindings params = new SimpleBindings();
if(scopeName == null){
scopeNamequery = "from PolicyEditorScopes";
params.put("scopeName", scopeName + "%");
}
PolicyController controller = getPolicyControllerInstance();
- List<Object> scopesList = null;
+ List<Object> scopesList;
if(PolicyController.isjUnit()){
scopesList = controller.getDataByQuery(scopeNamequery, null);
}else{
}
//Get Active Policy List based on Scope Selection form Policy Version table
- private void activePolicyList(String scopeName, List<JSONObject> resultList, List<String> roles, Set<String> scopes, boolean onlyFolders){
+ private void activePolicyList(String inScopeName, List<JSONObject> resultList, List<String> roles, Set<String> scopes, boolean onlyFolders){
PolicyController controller = getPolicyControllerInstance();
+ String scopeName = inScopeName;
if(scopeName.contains("/")){
scopeName = scopeName.replace("/", File.separator);
}
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);
}
}
}
- String scopeNameCheck = null;
+ String scopeNameCheck;
for (Object list : activePolicies) {
PolicyVersion policy = (PolicyVersion) list;
String scopeNameValue = policy.getPolicyName().substring(0, policy.getPolicyName().lastIndexOf(File.separator));
if(roles.contains(SUPERADMIN) || roles.contains(SUPEREDITOR) || roles.contains(SUPERGUEST)){
- if((scopeName.contains("\\\\"))){
+ if(scopeName.contains("\\\\")){
scopeNameCheck = scopeName.replace("\\\\", File.separator);
}else{
scopeNameCheck = scopeName;
}
}
- private void renameScope(List<Object> scopesList, String scopeName, String newScopeName, PolicyController controller){
+ private void renameScope(List<Object> scopesList, String inScopeName, String newScopeName, PolicyController controller){
for(Object object : scopesList){
PolicyEditorScopes editorScopeEntity = (PolicyEditorScopes) object;
+ String scopeName = inScopeName;
if(scopeName.contains("\\\\\\\\")){
scopeName = scopeName.replace("\\\\\\\\", File.separator);
newScopeName = newScopeName.replace("\\\\\\\\", File.separator);
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", "");
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.");
}
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.");
}
}
}
- 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;
+ private JSONObject cloneRecord(String newpolicyName, String oldScope, String inRemoveoldPolicyExtension, String newScope, String removenewPolicyExtension, PolicyEntity entity, String userId) throws ServletException{
+ String queryEntityName;
PolicyController controller = getPolicyControllerInstance();
PolicyEntity cloneEntity = new PolicyEntity();
cloneEntity.setPolicyName(newpolicyName);
+ String removeoldPolicyExtension = inRemoveoldPolicyExtension;
removeoldPolicyExtension = removeoldPolicyExtension.replace(".xml", "");
removenewPolicyExtension = removenewPolicyExtension.replace(".xml", "");
cloneEntity.setPolicyData(entity.getPolicyData().replace(oldScope+"."+removeoldPolicyExtension, newScope+"."+removenewPolicyExtension));
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);
+ try (FileWriter 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);
}
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);
+ try (FileWriter 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);
cloneEntity.setModifiedBy(userId);
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('.'));
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{
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{
}
}
LOGGER.debug("addFolder path: {} name: {}" + path +name);
- if(!name.equals("")){
+ if(! "".equals(name)){
if(name.startsWith(File.separator)){
name = name.substring(1);
}