aboutsummaryrefslogtreecommitdiffstats
path: root/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java
diff options
context:
space:
mode:
Diffstat (limited to 'POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java')
-rw-r--r--POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java165
1 files changed, 78 insertions, 87 deletions
diff --git a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java
index 700aa3a57..dbf744829 100644
--- a/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java
+++ b/POLICY-SDK-APP/src/main/java/org/onap/policy/controller/PolicyController.java
@@ -24,6 +24,7 @@ package org.onap.policy.controller;
import com.att.research.xacml.util.XACMLProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
+
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
@@ -37,12 +38,15 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
+
import javax.annotation.PostConstruct;
import javax.script.SimpleBindings;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+
import org.json.JSONObject;
import org.onap.policy.admin.PolicyNotificationMail;
import org.onap.policy.admin.RESTfulPAPEngine;
@@ -52,7 +56,7 @@ import org.onap.policy.common.logging.flexlogger.FlexLogger;
import org.onap.policy.common.logging.flexlogger.Logger;
import org.onap.policy.model.PDPGroupContainer;
import org.onap.policy.model.Roles;
-import org.onap.policy.rest.XACMLRestProperties;
+import org.onap.policy.rest.XacmlRestProperties;
import org.onap.policy.rest.dao.CommonClassDao;
import org.onap.policy.rest.jpa.Datatype;
import org.onap.policy.rest.jpa.FunctionDefinition;
@@ -60,6 +64,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.PeCryptoUtils;
+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.api.pap.PAPPolicyEngine;
@@ -99,7 +104,7 @@ public class PolicyController extends RestrictedBaseController {
}
public static String getDomain() {
- return XACMLProperties.getProperty(XACMLRestProperties.PROP_ADMIN_DOMAIN, "urn");
+ return XACMLProperties.getProperty(XacmlRestProperties.PROP_ADMIN_DOMAIN, "urn");
}
private static final Object mapAccess = new Object();
@@ -108,8 +113,6 @@ public class PolicyController extends RestrictedBaseController {
// Constant variables used across Policy-sdk
private static final String policyData = "policyData";
- private static final String characterEncoding = "UTF-8";
- private static final String contentType = "application/json";
private static final String file = "file";
private static final String SUPERADMIN = "super-admin";
private static final String POLICYGUEST = "Policy Guest";
@@ -160,8 +163,8 @@ public class PolicyController extends RestrictedBaseController {
return jUnit;
}
- public static void setjUnit(boolean jUnit) {
- PolicyController.jUnit = jUnit;
+ public static void setjUnit(boolean isJunit) {
+ PolicyController.jUnit = isJunit;
}
@Autowired
@@ -183,9 +186,8 @@ public class PolicyController extends RestrictedBaseController {
try {
String fileName;
if (jUnit) {
- fileName = new File(".").getCanonicalPath() + File.separator + "src"
- + File.separator + "test" + File.separator + "resources" + File.separator
- + "JSONConfig.json";
+ fileName = new File(".").getCanonicalPath() + File.separator + "src" + File.separator + "test"
+ + File.separator + "resources" + File.separator + "JSONConfig.json";
} else {
fileName = "xacml.admin.properties";
}
@@ -236,19 +238,17 @@ public class PolicyController extends RestrictedBaseController {
// Get the Property Values for Dashboard tab Limit
try {
setLogTableLimit(prop.getProperty("xacml.onap.dashboard.logTableLimit"));
- setSystemAlertTableLimit(
- prop.getProperty("xacml.onap.dashboard.systemAlertTableLimit"));
+ setSystemAlertTableLimit(prop.getProperty("xacml.onap.dashboard.systemAlertTableLimit"));
} catch (Exception e) {
- policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE
- + "Dashboard tab Property fields are missing" + e);
+ policyLogger
+ .error(XACMLErrorConstants.ERROR_DATA_ISSUE + "Dashboard tab Property fields are missing" + e);
setLogTableLimit("5000");
setSystemAlertTableLimit("2000");
}
System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME, "xacml.admin.properties");
} catch (IOException ex) {
policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE
- + "Exception Occured while reading the Smtp properties from xacml.admin.properties file"
- + ex);
+ + "Exception Occured while reading the Smtp properties from xacml.admin.properties file" + ex);
}
// Initialize the FunctionDefinition table at Server Start up
@@ -298,8 +298,7 @@ public class PolicyController extends RestrictedBaseController {
FunctionDefinition value = (FunctionDefinition) functiondefinitions.get(i);
mapID2Function.put(value.getXacmlid(), value);
if (!mapDatatype2Function.containsKey(value.getDatatypeBean())) {
- mapDatatype2Function.put(value.getDatatypeBean(),
- new ArrayList<FunctionDefinition>());
+ mapDatatype2Function.put(value.getDatatypeBean(), new ArrayList<FunctionDefinition>());
}
mapDatatype2Function.get(value.getDatatypeBean()).add(value);
}
@@ -311,22 +310,20 @@ public class PolicyController extends RestrictedBaseController {
* @param request HttpServletRequest.
* @param response HttpServletResponse.
*/
- @RequestMapping(value = {"/get_FunctionDefinitionDataByName"},
+ @RequestMapping(
+ value = {"/get_FunctionDefinitionDataByName"},
method = {org.springframework.web.bind.annotation.RequestMethod.GET},
produces = MediaType.APPLICATION_JSON_VALUE)
- public void getFunctionDefinitionData(HttpServletRequest request,
- HttpServletResponse response) {
+ public void getFunctionDefinitionData(HttpServletRequest request, HttpServletResponse response) {
try {
Map<String, Object> model = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
- model.put("functionDefinitionDatas", mapper.writeValueAsString(
- commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
- JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
- JSONObject j = new JSONObject(msg);
- response.getWriter().write(j.toString());
+ model.put("functionDefinitionDatas",
+ mapper.writeValueAsString(commonClassDao.getDataByColumn(FunctionDefinition.class, "shortname")));
+ response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
} catch (Exception e) {
- policyLogger.error(XACMLErrorConstants.ERROR_DATA_ISSUE
- + "Error while retriving the Function Definition data" + e);
+ policyLogger.error(
+ XACMLErrorConstants.ERROR_DATA_ISSUE + "Error while retriving the Function Definition data" + e);
}
}
@@ -368,7 +365,8 @@ public class PolicyController extends RestrictedBaseController {
* @param request HttpServletRequest.
* @param response HttpServletResponse.
*/
- @RequestMapping(value = {"/get_UserRolesData"},
+ @RequestMapping(
+ value = {"/get_UserRolesData"},
method = {org.springframework.web.bind.annotation.RequestMethod.GET},
produces = MediaType.APPLICATION_JSON_VALUE)
public void getUserRolesEntityData(HttpServletRequest request, HttpServletResponse response) {
@@ -377,9 +375,7 @@ public class PolicyController extends RestrictedBaseController {
Map<String, Object> model = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
model.put("userRolesDatas", mapper.writeValueAsString(getRolesOfUser(userId)));
- JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
- JSONObject j = new JSONObject(msg);
- response.getWriter().write(j.toString());
+ response.getWriter().write(new JSONObject(new JsonMessage(mapper.writeValueAsString(model))).toString());
} catch (Exception e) {
policyLogger.error("Exception Occured" + e);
}
@@ -402,8 +398,7 @@ public class PolicyController extends RestrictedBaseController {
setPapEngine(new RESTfulPAPEngine(myRequestUrl));
new PDPGroupContainer(new RESTfulPAPEngine(myRequestUrl));
} catch (Exception e) {
- policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR
- + "Exception Occured while loading PAP" + e);
+ policyLogger.error(XACMLErrorConstants.ERROR_SYSTEM_ERROR + "Exception Occured while loading PAP" + e);
}
Map<String, Object> model = new HashMap<>();
return new ModelAndView("policy_Editor", "model", model);
@@ -438,9 +433,8 @@ public class PolicyController extends RestrictedBaseController {
savePolicyRoles(name, filteredRole, userId);
} else {
userRoles = getRoles(userId);
- Pair<Set<String>, List<String>> pair =
- org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
- roles = pair.u;
+ Pair<Set<String>, List<String>> pair = org.onap.policy.utils.UserUtils.checkRoleAndScope(userRoles);
+ roles = pair.second;
if (!roles.contains(filteredRole)) {
savePolicyRoles(name, filteredRole, userId);
}
@@ -511,8 +505,7 @@ public class PolicyController extends RestrictedBaseController {
roles.clear();
roles.add(SUPERADMIN);
}
- if (!roles.contains(SUPERADMIN)
- || (POLICYGUEST.equalsIgnoreCase(role) && !superCheck)) {
+ if (!roles.contains(SUPERADMIN) || (POLICYGUEST.equalsIgnoreCase(role) && !superCheck)) {
if ("Policy Admin".equalsIgnoreCase(role.trim())) {
roles.add("admin");
} else if ("Policy Editor".equalsIgnoreCase(role.trim())) {
@@ -576,8 +569,7 @@ public class PolicyController extends RestrictedBaseController {
}
public PolicyVersion getPolicyEntityFromPolicyVersion(String query) {
- return (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName",
- query);
+ return (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", query);
}
public List<Object> getDataByQuery(String query, SimpleBindings params) {
@@ -604,11 +596,11 @@ public class PolicyController extends RestrictedBaseController {
/**
* Switch Version Policy Content.
*
- * @param pName which is used to find associated versions.
+ * @param thePolicyName which is used to find associated versions.
* @return list of available versions based on policy name.
*/
- public JSONObject switchVersionPolicyContent(String pName) {
- String policyName = pName;
+ public JSONObject switchVersionPolicyContent(String thePolicyName) {
+ String policyName = thePolicyName;
String dbCheckName = policyName.replace("/", ".");
if (dbCheckName.contains("Config_")) {
dbCheckName = dbCheckName.replace(".Config_", ":Config_");
@@ -620,25 +612,23 @@ public class PolicyController extends RestrictedBaseController {
dbCheckName = dbCheckName.replace(".Decision_", ":Decision_");
}
String[] splitDbCheckName = dbCheckName.split(":");
- String query =
- "FROM PolicyEntity where policyName like :splitDBCheckName1 and scope = :splitDBCheckName0";
+ String query = "FROM PolicyEntity where policyName like :splitDBCheckName1 and scope = :splitDBCheckName0";
SimpleBindings params = new SimpleBindings();
params.put("splitDBCheckName1", splitDbCheckName[1] + "%");
params.put("splitDBCheckName0", splitDbCheckName[0]);
- List<Object> policyEntity = commonClassDao.getDataByQuery(query, params);
List<String> av = new ArrayList<>();
- for (Object entity : policyEntity) {
- PolicyEntity pEntity = (PolicyEntity) entity;
- String removeExtension = pEntity.getPolicyName().replace(".xml", "");
+ for (Object entity : commonClassDao.getDataByQuery(query, params)) {
+ PolicyEntity policyEntity = (PolicyEntity) entity;
+ String removeExtension = policyEntity.getPolicyName().replace(".xml", "");
String version = removeExtension.substring(removeExtension.lastIndexOf('.') + 1);
- String userName = getUserId(pEntity, "@ModifiedBy:");
- av.add(version + " | " + pEntity.getModifiedDate() + " | " + userName);
+ String userName = getUserId(policyEntity, "@ModifiedBy:");
+ av.add(version + " | " + policyEntity.getModifiedDate() + " | " + userName);
}
if (policyName.contains("/")) {
policyName = policyName.replace("/", File.separator);
}
- PolicyVersion entity = (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class,
- "policyName", policyName);
+ PolicyVersion entity =
+ (PolicyVersion) commonClassDao.getEntityItem(PolicyVersion.class, "policyName", policyName);
JSONObject el = new JSONObject();
el.put("activeVersion", entity.getActiveVersion());
el.put("availableVersions", av);
@@ -646,13 +636,20 @@ public class PolicyController extends RestrictedBaseController {
return el;
}
+ /**
+ * getUserId.
+ *
+ * @param data PolicyEntity
+ * @param value String
+ * @return String
+ */
public String getUserId(PolicyEntity data, String value) {
String userId = "";
- String uValue = value;
+ String userValue = value; // Why?
String description = getDescription(data);
- if (description.contains(uValue)) {
- userId = description.substring(description.indexOf(uValue) + uValue.length(),
- description.lastIndexOf(uValue));
+ if (description.contains(userValue)) {
+ userId = description.substring(description.indexOf(userValue) + userValue.length(),
+ description.lastIndexOf(userValue));
}
UserInfo userInfo = (UserInfo) getEntityItem(UserInfo.class, "userLoginId", userId);
if (userInfo == null) {
@@ -661,35 +658,43 @@ public class PolicyController extends RestrictedBaseController {
return userInfo.getUserName();
}
+ /**
+ * getDescription.
+ *
+ * @param data PolicyEntity
+ * @return String
+ */
public String getDescription(PolicyEntity data) {
- InputStream stream =
- new ByteArrayInputStream(data.getPolicyData().getBytes(StandardCharsets.UTF_8));
+ InputStream stream = new ByteArrayInputStream(data.getPolicyData().getBytes(StandardCharsets.UTF_8));
Object policy = XACMLPolicyScanner.readPolicy(stream);
if (policy instanceof PolicySetType) {
return ((PolicySetType) policy).getDescription();
} else if (policy instanceof PolicyType) {
return ((PolicyType) policy).getDescription();
- } else {
- PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE
- + "Expecting a PolicySet/Policy/Rule object. Got: "
- + policy.getClass().getCanonicalName());
- return null;
}
+ PolicyLogger.error(MessageCodes.ERROR_DATA_ISSUE + "Expecting a PolicySet/Policy/Rule object. Got: "
+ + policy.getClass().getCanonicalName());
+ return null;
}
+ /**
+ * getUserInfo.
+ *
+ * @param data PolicyEntity
+ * @param activePolicies list of active policies
+ * @return array of String
+ */
public String[] getUserInfo(PolicyEntity data, List<PolicyVersion> activePolicies) {
String policyName = data.getScope().replace(".", File.separator) + File.separator
+ data.getPolicyName().substring(0, data.getPolicyName().indexOf('.'));
- PolicyVersion polVersion = activePolicies.stream()
- .filter(a -> policyName.equals(a.getPolicyName())).findAny().orElse(null);
+ PolicyVersion polVersion =
+ activePolicies.stream().filter(a -> policyName.equals(a.getPolicyName())).findAny().orElse(null);
String[] result = new String[2];
UserInfo userCreate = null;
UserInfo userModify = null;
if (polVersion != null) {
- userCreate = (UserInfo) getEntityItem(UserInfo.class, "userLoginId",
- polVersion.getCreatedBy());
- userModify = (UserInfo) getEntityItem(UserInfo.class, "userLoginId",
- polVersion.getModifiedBy());
+ userCreate = (UserInfo) getEntityItem(UserInfo.class, "userLoginId", polVersion.getCreatedBy());
+ userModify = (UserInfo) getEntityItem(UserInfo.class, "userLoginId", polVersion.getModifiedBy());
}
result[0] = userCreate != null ? userCreate.getUserName() : SUPERADMIN;
@@ -726,8 +731,7 @@ public class PolicyController extends RestrictedBaseController {
return mapDatatype2Function;
}
- public static void setMapDatatype2Function(
- Map<Datatype, List<FunctionDefinition>> mapDatatype2Function) {
+ public static void setMapDatatype2Function(Map<Datatype, List<FunctionDefinition>> mapDatatype2Function) {
PolicyController.mapDatatype2Function = mapDatatype2Function;
}
@@ -940,11 +944,11 @@ public class PolicyController extends RestrictedBaseController {
}
public static String getCharacterencoding() {
- return characterEncoding;
+ return PolicyUtils.CHARACTER_ENCODING;
}
public static String getContenttype() {
- return contentType;
+ return PolicyUtils.APPLICATION_JSON;
}
public static String getFile() {
@@ -969,17 +973,4 @@ public class PolicyController extends RestrictedBaseController {
return fileSizeLimit;
}
- /**
- * Function to convert date.
- *
- * @param dateTTL input date value.
- * @return
- */
- public String convertDate(String dateTTL) {
- String formateDate = null;
- if (dateTTL.contains("-")) {
- formateDate = dateTTL.replace("-", "/");
- }
- return formateDate;
- }
}