Minor refactoring on policy merging

revert-70aa11f8
Supun94 8 years ago
parent 451b2fd1dc
commit eb1575126f

@ -383,7 +383,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier();
deviceIdentifier.setId(deviceId);
deviceIdentifier.setType(deviceType);
policy = policyManagementService.getEffectivePolicy(deviceIdentifier);
policy = policyManagementService.getAppliedPolicyToDevice(deviceIdentifier);
if (policy == null) {
return Response.status(Response.Status.NOT_FOUND).entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(

@ -33,7 +33,7 @@ public class PolicyConfiguration {
private int minRetriesToMarkUnreachable;
private int minRetriesToMarkInactive;
private List<String> platforms;
private String policyEvaluationPointName;
private String policyEvaluationPoint;
@XmlElement(name = "MonitoringClass", required = true)
public String getMonitoringClass() {
@ -99,13 +99,13 @@ public class PolicyConfiguration {
this.platforms = platforms;
}
@XmlElement(name = "PolicyEvaluationPointName", required = true)
@XmlElement(name = "PolicyEvaluationPoint", required = true)
public String getPolicyEvaluationPointName() {
return policyEvaluationPointName;
return policyEvaluationPoint;
}
public void setPolicyEvaluationPointName(String policyEvaluationPointName) {
this.policyEvaluationPointName = policyEvaluationPointName;
this.policyEvaluationPoint = policyEvaluationPointName;
}
}

@ -23,7 +23,7 @@ var stepBackFrom = {};
var policy = {};
var configuredOperations = [];
var baseApiUrl = "/api/device-mgt/v1.0";
var base_api_url = "/api/device-mgt/v1.0";
// Constants to define platform types available
var platformTypeConstants = {
@ -119,7 +119,6 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
policyDescriptionInput.val(policyPayloadObj["description"]);
// updating next-page wizard title with selected platform
$("#policy-heading").text(policy["platform"].toUpperCase() + " POLICY - " + policy["name"].toUpperCase());
// $("#policy-heading").text("Android" + " POLICY - " + "Merged Policy");
$("#policy-platform").text(policy["platform"].toUpperCase());
$("#policy-assignment").text(policyPayloadObj.ownershipType);
$("#policy-action").text(policyPayloadObj.compliance.toUpperCase());
@ -149,9 +148,9 @@ skipStep["policy-platform"] = function (policyPayloadObj) {
// $("#roles-row").addClass("hidden");
// }
var deviceType = "android";
var hiddenOperationsByDeviceType = $("#hidden-operations-" + "android");
var hiddenOperationsByDeviceTypeCacheKey = "android" + "HiddenOperations";
var deviceType = policy["platform"];
var hiddenOperationsByDeviceType = $("#hidden-operations-" + deviceType);
var hiddenOperationsByDeviceTypeCacheKey = deviceType + "HiddenOperations";
var hiddenOperationsByDeviceTypeSrc = hiddenOperationsByDeviceType.attr("src");
setTimeout(
@ -1815,12 +1814,7 @@ var updatePolicy = function (policy, state) {
payload["users"] = [];
payload["roles"] = [];
}
//var serviceURL = baseApiUrl + "/policies/effective-policy/" + policy["platform"] + "/" + getParameterByName("id");
// role management
//set service url
//api/device-mgt/v1.0/ policy?........
var serviceURL = baseApiUrl + "/policy/effective-policy/" + policy["platform"] + "/" + getParameterByName("id");
var serviceURL = base_api_url + "/policy/effective-policy/" + policy["platform"] + "/" + getParameterByName("id");
invokerUtil.put(
serviceURL,
payload,
@ -1829,7 +1823,7 @@ var updatePolicy = function (policy, state) {
if (state == "save") {
var policyList = [];
policyList.push(getParameterByName("id"));
serviceURL = baseApiUrl + "/policies/deactivate-policy";
serviceURL = base_api_url + "/policies/deactivate-policy";
invokerUtil.post(
serviceURL,
policyList,
@ -1846,7 +1840,7 @@ var updatePolicy = function (policy, state) {
} else if (state == "publish") {
var policyList = [];
policyList.push(getParameterByName("id"));
serviceURL = baseApiUrl + "/policies/activate-policy";
serviceURL = base_api_url + "/policies/activate-policy";
invokerUtil.post(
serviceURL,
policyList,
@ -1960,14 +1954,12 @@ var slideDownPaneAgainstValueSet = function (selectElement, paneID, valueSet) {
var slideDownPaneAgainstValueSetForRadioButtons = function (selectElement, paneID, valueSet) {
var selectedValueOnChange = selectElement.value;
var i, slideDownVotes = 0;
for (i = 0; i < valueSet.length; i++) {
var slideDownVotes = 0;
for (var i = 0; i < valueSet.length; i++) {
if (selectedValueOnChange == valueSet[i]) {
slideDownVotes++;
}
}
var paneSelector = "#" + paneID;
if(slideDownVotes > 0) {
$(paneSelector).removeClass("hidden");
@ -2057,10 +2049,7 @@ $(document).ready(function () {
var policyPayloadObj;
invokerUtil.get(
// baseApiUrl + "/policies/" + getParameterByName("id"),
baseApiUrl + "/policies/effective-policy/" + getParameterByName("type") + "/" + getParameterByName("id"),
base_api_url + "/policies/effective-policy/" + getParameterByName("type") + "/" + getParameterByName("id"),
// on success
function (data, textStatus, jqXHR) {
if (jqXHR.status == 200 && data) {

@ -104,9 +104,9 @@ public class MergedPolicyEvaluationServiceComponent {
PolicyDecisionPointDataHolder.getInstance().setPolicyManagerService(null);
}
protected String getName() {
return MergedPolicyEvaluationServiceComponent.class.getName();
}
// protected String getName() {
// return MergedPolicyEvaluationServiceComponent.class.getName();
// }
}

@ -29,74 +29,63 @@ import org.wso2.carbon.policy.decision.point.internal.PolicyDecisionPointDataHol
import java.sql.Timestamp;
import java.util.*;
/**
* This class helps to merge related policies and return as a effective policy.
*/
public class MergedEvaluationPoint implements PolicyEvaluationPoint {
private static final Log log = LogFactory.getLog(MergedEvaluationPoint.class);
private PolicyManagerService policyManagerService;
private List<Policy> policyList;
PIPDevice pipDevice;
private static final String effectivePolicyName = "Effective-Policy";
private static final String policyEvaluationPoint = "Merged";
@Override
public List<ProfileFeature> getEffectiveFeatures(List<Policy> policyList, DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
PolicyAdministratorPoint policyAdministratorPoint;
policyManagerService = getPolicyManagerService();
try {
if (policyManagerService != null) {
if (!policyList.isEmpty()) {
Policy effectivePolicy = policyResolve(policyList);
effectivePolicy.setActive(true);
policyAdministratorPoint = policyManagerService.getPAP();
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, effectivePolicy);
return effectivePolicy.getProfile().getProfileFeaturesList();
}
}
return null;
} catch (PolicyManagementException e) {
String msg = "Error occurred when retrieving the policy related data from policy management service.";
log.error(msg, e);
throw new PolicyEvaluationException(msg, e);
}
public List<ProfileFeature> getEffectiveFeatures(List<Policy> policyList, DeviceIdentifier deviceIdentifier)
throws PolicyEvaluationException {
return this.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList();
}
@Override
public String getName() {
return "MergedPolicyEvaluationServiceComponent";
return policyEvaluationPoint;
}
@Override
public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException {
PIPDevice pipDevice;
List<Policy> policyList;
Policy policy;
try {
policyManagerService = getPolicyManagerService();
if (policyManagerService == null) {
return null;
}
PolicyInformationPoint policyInformationPoint = policyManagerService.getPIP();
pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
if (policyManagerService == null || policyList.size() == 0) {
if (policyList.size() == 0) {
return null;
}
Policy policy = new Policy();
// Set effective-policy information
Profile profile = new Profile();
profile.setProfileFeaturesList(getEffectiveFeatures(policyList, deviceIdentifier));
policy = policyResolve(policyList);
profile.setProfileFeaturesList(policy.getProfile().getProfileFeaturesList());
policy.setProfile(profile);
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
profile.setCreatedDate(currentTimestamp);
profile.setUpdatedDate(currentTimestamp);
profile.setDeviceType(deviceIdentifier.getType());
profile.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
policy.setPolicyName("Effective-Policy");
policy.setPolicyName(effectivePolicyName);
policy.setOwnershipType(pipDevice.getOwnershipType());
policy.setRoles(null);
policy.setDevices(null);
policy.setUsers(null);
policy.setActive(true);
policy.setUpdated(true);
policy.setTenantId(PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId());
policy.setDescription("This is a system generated effective policy by merging relevant policies.");
policy.setCompliance(policyList.get(0).getCompliance());
policy.setId(-1);
return policy;
} catch (PolicyManagementException e) {
String msg = "Error occurred when retrieving the policy related data from policy management service.";
@ -106,52 +95,30 @@ public class MergedEvaluationPoint implements PolicyEvaluationPoint {
}
private Policy policyResolve(List<Policy> policyList) throws PolicyEvaluationException, PolicyManagementException {
sortPolicies();
Collections.sort(policyList, Collections.reverseOrder());
// Iterate through all policies
Map<String, ProfileFeature> featureMap = new HashMap<>();
// Merge roles of policies
//Map<String, java.lang.Integer> rolesMap = new HashMap<>();
Iterator<Policy> policyIterator = policyList.iterator();
while (policyIterator.hasNext()) {
Policy policy = policyIterator.next();
for (Policy policy : policyList) {
List<ProfileFeature> profileFeaturesList = policy.getProfile().getProfileFeaturesList();
if (profileFeaturesList != null) {
Iterator<ProfileFeature> featureIterator = profileFeaturesList.iterator();
while (featureIterator.hasNext()) {
ProfileFeature feature = featureIterator.next();
for (ProfileFeature feature : profileFeaturesList) {
featureMap.put(feature.getFeatureCode(), feature);
}
}
// List<String> policyRolesList = policy.getRoles();
//
// if (policyRolesList != null) {
// Iterator<String> roleIterator = policyRolesList.iterator();
// while (roleIterator.hasNext()) {
// String role = roleIterator.next();
// rolesMap.put(role,policy.getId());
// }
// }
}
// Get prioritized features list
List<ProfileFeature> newFeaturesList = new ArrayList<>(featureMap.values());
Profile profile = new Profile();
profile.setProfileFeaturesList(newFeaturesList);
Policy effectivePolicy = new Policy();
effectivePolicy.setProfile(profile);
//effectivePolicy.setRoles(rolesList);
return effectivePolicy;
}
public void sortPolicies() throws PolicyEvaluationException {
Collections.sort(policyList, Collections.reverseOrder());
}
private PolicyManagerService getPolicyManagerService() {
return PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService();
}
}

@ -29,6 +29,7 @@ import java.util.List;
public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint {
private SimpleEvaluationImpl evaluation;
private static final String policyEvaluationPoint = "Simple";
public PolicyEvaluationServiceImpl() {
evaluation = new SimpleEvaluationImpl();
@ -50,6 +51,6 @@ public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint {
@Override
public String getName() {
return "SimplePolicyEvaluationServiceComponent";
return policyEvaluationPoint;
}
}

@ -39,10 +39,15 @@ public interface PolicyEvaluationPoint {
/**
* This class will return the effective feature set from the list.
*
* @param deviceIdentifier device information.
* @return returns the effective feature set.
*/
List<ProfileFeature> getEffectiveFeatures(List<Policy> policyList,DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException;
/**
* This method returns the name of the Policy Evaluation Point
* @return returns Policy Evaluation Point name
*/
String getName();
}

@ -100,6 +100,7 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
if (policy == null) {
return null;
}
this.getPAP().setPolicyUsed(deviceIdentifier, policy);
} else {
throw new PolicyEvaluationException("Error occurred while getting the policy evaluation point " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType());

@ -30,6 +30,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
@ -84,7 +85,12 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato
public Policy getEffectivePolicy(DeviceIdentifier identifier) throws PolicyDelegationException {
try {
PolicyManagerService policyManagerService = new PolicyManagerServiceImpl();
return policyManagerService.getPEP().getEffectivePolicy(identifier);
PolicyAdministratorPoint policyAdministratorPoint;
Policy policy = policyManagerService.getPEP().getEffectivePolicy(identifier);
policyAdministratorPoint = policyManagerService.getPAP();
policyAdministratorPoint.setPolicyUsed(identifier, policy);
return policy;
//return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().getEffectivePolicy(identifier);
} catch (PolicyEvaluationException e) {
String msg = "Error occurred while retrieving the effective policy for devices.";

@ -102,7 +102,7 @@ public class PolicyManagementDataHolder {
}
public void removePolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) {
policyEvaluationPoints.put(policyEvaluationPoint.getName(), this.policyEvaluationPoint);
policyEvaluationPoints.remove(policyEvaluationPoint.getName());
}

@ -166,7 +166,7 @@ public class PolicyManagementServiceComponent {
if (log.isDebugEnabled()) {
log.debug("Removing Policy Information Service");
}
PolicyManagementDataHolder.getInstance().removePolicyEvaluationPoint(null);
PolicyManagementDataHolder.getInstance().removePolicyEvaluationPoint(pepService);
}
protected void setDeviceManagementService(DeviceManagementProviderService deviceManagerService) {

@ -879,10 +879,10 @@ public class PolicyManagerImpl implements PolicyManager {
PolicyManagementDAOFactory.beginTransaction();
Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId());
if (policySaved != null && policySaved.getId() != 0) {
// if (policy.getId() != policySaved.getId()) {
if (policySaved != null) {
if (policySaved.getId() != 0) {
policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
// }
}
} else {
policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy);
}

@ -79,7 +79,7 @@ SimplePolicyEvaluationTest implements PolicyEvaluationPoint {
@Override
public String getName() {
return "SimplePolicyEvaluationServiceComponent";
return "SimplePolicy";
}
public void sortPolicies(List<Policy> policyList) throws PolicyEvaluationException {

@ -42,8 +42,8 @@
<maxRetries>5</maxRetries>
<minRetriesToMarkUnreachable>8</minRetriesToMarkUnreachable>
<minRetriesToMarkInactive>20</minRetriesToMarkInactive>
<!--<PolicyEvaluationPointName>SimplePolicyEvaluationServiceComponent</PolicyEvaluationPointName>-->
<PolicyEvaluationPointName>MergedPolicyEvaluationServiceComponent</PolicyEvaluationPointName>
<!--<PolicyEvaluationPoint>Simple</PolicyEvaluationPoint>-->
<PolicyEvaluationPoint>Merged</PolicyEvaluationPoint>
</PolicyConfiguration>
</ManagementRepository>
</DeviceMgtConfiguration>

@ -0,0 +1,15 @@
diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml
index addf38e..4e2ad6b 100644
--- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml
+++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml
@@ -43,8 +43,8 @@
<MaxRetries>5</MaxRetries>
<MinRetriesToMarkUnreachable>8</MinRetriesToMarkUnreachable>
<MinRetriesToMarkInactive>20</MinRetriesToMarkInactive>
- <!--<PolicyEvaluationPointName>SimplePolicyEvaluationServiceComponent</PolicyEvaluationPointName>-->
- <PolicyEvaluationPointName>MergedPolicyEvaluationServiceComponent</PolicyEvaluationPointName>
+ <!--<PolicyEvaluationPoint>Simple</PolicyEvaluationPoint>-->
+ <PolicyEvaluationPoint>Merged</PolicyEvaluationPoint>
<Platforms>
<Platform>android</Platform>
<Platform>ios</Platform>

@ -43,8 +43,8 @@
<MaxRetries>5</MaxRetries>
<MinRetriesToMarkUnreachable>8</MinRetriesToMarkUnreachable>
<MinRetriesToMarkInactive>20</MinRetriesToMarkInactive>
<!--<PolicyEvaluationPointName>SimplePolicyEvaluationServiceComponent</PolicyEvaluationPointName>-->
<PolicyEvaluationPointName>MergedPolicyEvaluationServiceComponent</PolicyEvaluationPointName>
<!--<PolicyEvaluationPoint>Simple</PolicyEvaluationPoint>-->
<PolicyEvaluationPoint>Merged</PolicyEvaluationPoint>
<Platforms>
<Platform>android</Platform>
<Platform>ios</Platform>

Loading…
Cancel
Save