Optimize policy pick strategy for corrective action implementation

corrective-policy
Pahansith 5 years ago
parent 684ff5df7f
commit 2724d8ac03

@ -55,6 +55,8 @@ public class CorrectiveAction implements Serializable {
)
private List<ProfileFeature> operations;
private int featureId;
public String getActionType() {
return actionType;
}
@ -78,4 +80,12 @@ public class CorrectiveAction implements Serializable {
public void setOperations(List<ProfileFeature> operations) {
this.operations = operations;
}
public int getFeatureId() {
return featureId;
}
public void setFeatureId(int featureId) {
this.featureId = featureId;
}
}

@ -23,7 +23,7 @@ import java.util.ArrayList;
import java.util.List;
public class ProfileOperation extends ConfigOperation implements Serializable {
private List<Integer> correctiveActionIds = new ArrayList<>();
private List<Integer> correctiveActionIds;
public Type getType() {
return Type.PROFILE;

@ -9,7 +9,7 @@
<div class="col-md-12">
<div class="wr-steps hidden">
<div class="col-md-2 col-xs-2 col-md-offset-1">
<div class="col-md-2 col-xs-2 col-md-offset-2">
<div class="itm-wiz itm-wiz-current" data-step="policy-platform">
<div class="wiz-no">1</div>
<div class="wiz-lbl hidden-xs"><span>Select a platform</span></div>
@ -23,16 +23,9 @@
</div>
<br class="c-both"/>
</div>
<div class="col-md-2 col-xs-2">
<div class="itm-wiz" data-step="policy-type">
<div class="wiz-no">3</div>
<div class="wiz-lbl hidden-xs"><span>Select policy type</span></div>
</div>
<br class="c-both"/>
</div>
<div class="col-md-2 col-xs-2">
<div class="itm-wiz" data-step="policy-criteria">
<div class="wiz-no">4</div>
<div class="wiz-no">3</div>
<div class="wiz-lbl hidden-xs"><span>Assign to groups</span></div>
</div>
<br class="c-both"/>
@ -209,7 +202,7 @@
</div>
<div class="wr-input-control wr-btn-grp">
<a href="javascript:void(0)" class="wr-btn wizard-stepper" data-is-back-btn="true"
data-current="policy-criteria" data-next="policy-type">Back</a>
data-current="policy-criteria" data-next="policy-profile">Back</a>
<a href="javascript:void(0)" class="wr-btn wizard-stepper"
data-current="policy-criteria" data-next="policy-naming" data-validate="true">Continue</a>
</div>
@ -217,45 +210,6 @@
</div>
</div>
</div>
<div class="col-centered wr-content policy-type hidden">
<div class="wr-form">
<h1 id="policy-type-page-wizard-title" class="page-sub-title">ADD POLICY</h1>
<hr>
<div id="policy-type-wizard-steps" class="row wr-wizard"></div>
<hr>
<div class="row">
<div class="col-lg-12">
<h4 class="hidden-xs"> Step 3: Select policy type</h4>
<br>
<div id="policy-type-main-error-msg" class="alert alert-danger hidden" role="alert">
<i class="icon fw fw-error"></i><span></span>
</div>
<div>
<div class="wr-input-control">
<label class="wr-input-control radio light">
<input type="radio" id="policy-type-general" name="policy-type-radio-btn"
value="GENERAL" checked/>
<span class="helper">General Policy</span>
</label>
<label class="wr-input-control radio light">
<input type="radio" id="policy-type-corrective" name="policy-type-radio-btn"
value="CORRECTIVE"/>
<span class="helper">Corrective Policy</span>
</label>
</div>
</div>
<div id="select-general-policy-type"></div>
<div class="wr-input-control wr-btn-grp">
<a href="javascript:void(0)" class="wr-btn wizard-stepper" data-is-back-btn="true"
data-current="policy-type" data-next="policy-profile">Back</a>
<a href="javascript:void(0)" class="wr-btn wizard-stepper"
data-current="policy-type"
data-next="policy-criteria" data-validate="false">Continue</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-centered wr-content policy-profile hidden">
<div class="wr-form">
<h1 id="policy-profile-page-wizard-title" class="page-sub-title">ADD POLICY</h1>
@ -288,7 +242,7 @@
data-current="policy-profile" data-next="policy-platform">Back</a>
<a href="javascript:void(0)" class="wr-btn wizard-stepper"
data-current="policy-profile"
data-next="policy-type" data-validate="true">Continue</a>
data-next="policy-criteria" data-validate="true">Continue</a>
</div>
</div>
</div>

@ -222,28 +222,6 @@ stepForwardFrom["policy-profile"] = function () {
policy["profile"] = generatePolicyProfile();
}
// add policy correction action page
var policyCorrectiveActionTemplateSrc =
"/public/cdmf.unit.policy.corrective-action/templates/policy-corrective-action.hbs";
var policyCorrectiveActionScriptSrc =
"/public/cdmf.unit.policy.corrective-action/js/policy-corrective-action.js";
var policyCorrectiveActionTemplateCacheKey = "policy-corrective-action";
$.template(policyCorrectiveActionTemplateCacheKey, context + policyCorrectiveActionTemplateSrc,
function (template) {
var content = template(
{
"deviceType": policy["platform"],
"correctivePolicies": $("#logged-in-user").data("corrective-policies")
}
);
$("#select-general-policy-type").html(content);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = context + policyCorrectiveActionScriptSrc;
document.head.append(script);
});
// updating next-page wizard title with selected platform
$("#policy-type-page-wizard-title").text("ADD " + policy["platform"] + " POLICY");
};
@ -261,28 +239,6 @@ stepBackFrom["policy-profile"] = function () {
}
};
/**
* Forward action of policy type page.
*/
stepForwardFrom["policy-type"] = function () {
policy["type"] = $("input[name=policy-type-radio-btn]:checked").val();
var correctiveActionList = [];
if (policy.type === "GENERAL") {
var selectedCorrectivePolicyId = $("#corrective-policy-input").val();
if (selectedCorrectivePolicyId && selectedCorrectivePolicyId !== "none") {
var correctiveAction = {
"actionType": "POLICY",
"policyId": selectedCorrectivePolicyId
};
correctiveActionList.push(correctiveAction);
}
}
policy["correctiveActionList"] = correctiveActionList;
//updating next-page wizard title with selected platform
$("#policy-criteria-page-wizard-title").text("ADD " + policy["platform"] + " POLICY");
};
/**
* Forward action of policy criteria page.
*/
@ -483,8 +439,7 @@ var savePolicy = function (policy, isActive, serviceURL) {
"compliance": policy["selectedNonCompliantAction"],
"ownershipType": null,
"active": isActive,
"policyType": policy["type"],
"correctiveActions": policy["correctiveActionList"],
"policyType": "GENERAL",
"profile": {
"profileName": policy["policyName"],
"deviceType": policy["platform"],
@ -639,6 +594,7 @@ $(document).ready(function () {
}
});
//todo
$('input[type=radio][name=policy-type-radio-btn]').change(function() {
if ($(this).val() === "CORRECTIVE") {
$("#select-general-policy-type").addClass("hidden");

@ -1,38 +1,30 @@
{{#zone "content"}}
{{#if isAuthorized }}
<span id="logged-in-user" class="hidden" data-username="{{@user.username}}" data-domain="{{@user.domain}}"
data-tenant-id="{{@user.tenantId}}" data-iscloud="{{isCloud}}" data-storeapps="{{storeApps}}"
data-corrective-policies="{{correctivePolicies}}"></span>
data-tenant-id="{{@user.tenantId}}" data-iscloud="{{isCloud}}" data-storeapps="{{storeApps}}"></span>
<span id="policy-operations" class="hidden" data-template="{{policyOperations.template}}"
data-script="{{policyOperations.script}}" data-style="{{policyOperations.style}}"></span>
<div class="row">
<div class="col-md-12">
<div class="wr-steps hidden">
<div class="col-md-3 col-xs-3">
<div class="col-md-3 col-xs-3 col-md-offset-2">
<div class="itm-wiz itm-wiz-current" data-step="policy-profile">
<div class="wiz-no">1</div>
<div class="wiz-lbl hidden-xs"><span>Edit current profile</span></div>
</div>
<br class="c-both"/>
</div>
<div class="col-md-3 col-xs-3">
<div class="itm-wiz itm-wiz" data-step="policy-type">
<div class="wiz-no">2</div>
<div class="wiz-lbl hidden-xs"><span>Edit policy type</span></div>
</div>
<br class="c-both"/>
</div>
<div class="col-md-3 col-xs-3">
<div class="itm-wiz" data-step="policy-criteria">
<div class="wiz-no">3</div>
<div class="wiz-no">2</div>
<div class="wiz-lbl hidden-xs"><span>Edit assignment groups</span></div>
</div>
<br class="c-both"/>
</div>
<div class="col-md-3 col-xs-3">
<div class="itm-wiz" data-step="policy-naming">
<div class="wiz-no">4</div>
<div class="wiz-no">3</div>
<div class="wiz-lbl hidden-xs"><span>Republish to devices</span></div>
</div>
<br class="c-both"/>
@ -56,7 +48,7 @@
<hr>
<div class="row">
<div class="col-lg-12">
<h4 class="visible-xs">Step 4: Republish to devices</h4>
<h4 class="visible-xs">Step 3: Republish to devices</h4>
<br>
<div id="policy-naming-main-error-msg" class="alert alert-danger hidden" role="alert">
<i class="icon fw fw-error"></i><span></span>
@ -204,7 +196,7 @@
</div>
<div class="wr-input-control wr-btn-grp">
<a href="#" class="wr-btn wizard-stepper" data-is-back-btn="true"
data-current="policy-criteria" data-next="policy-type">
data-current="policy-criteria" data-next="policy-profile">
Back
</a>
<a href="#" class="wr-btn wizard-stepper" data-current="policy-criteria"
@ -217,53 +209,6 @@
</div>
</div>
<div class="col-centered wr-content policy-type hidden">
<div class="wr-form">
<h1 id="policy-type-page-wizard-title" class="page-sub-title">EDIT POLICY</h1>
<hr>
<div id="policy-type-wizard-steps" class="row wr-wizard"></div>
<hr>
<div class="row">
<div class="col-lg-12">
<h4 class="visible-xs"> Step 2: Select policy type</h4>
<br>
<div id="policy-type-main-error-msg" class="alert alert-danger hidden" role="alert">
<i class="icon fw fw-error"></i><span></span>
</div>
<div>
<div class="wr-input-control">
<label class="wr-input-control radio light">
<input type="radio" id="policy-type-general" name="policy-type-radio-btn"
value="GENERAL"/>
<span class="helper">General Policy</span>
</label>
<label class="wr-input-control radio light">
<input type="radio" id="policy-type-corrective" name="policy-type-radio-btn"
value="CORRECTIVE"/>
<span class="helper">Corrective Policy</span>
</label>
</div>
</div>
<div class='policy-type-loading-corrective-actions'>
<br/>
<i class='fw fw-settings fw-spin fw-2x'></i>
Loading corrective actions . . .
<br/>
<br/>
</div>
<div id="select-general-policy-type"></div>
<div class="wr-input-control wr-btn-grp">
<a href="#" class="wr-btn wizard-stepper" data-is-back-btn="true"
data-current="policy-type" data-next="policy-profile">Back</a>
<a href="#" class="wr-btn wizard-stepper"
data-current="policy-type"
data-next="policy-criteria" data-validate="false">Continue</a>
</div>
</div>
</div>
</div>
</div>
<div class="col-centered wr-content policy-profile">
<div class="wr-form">
<h1 id="policy-profile-page-wizard-title" class="page-sub-title">EDIT POLICY</h1>
@ -293,7 +238,7 @@
</div>
<div class="wr-input-control wr-btn-grp">
<a href="#" class="wr-btn wizard-stepper" data-current="policy-profile"
data-next="policy-type" data-validate="true">
data-next="policy-criteria" data-validate="true">
Continue
</a>
</div>

@ -85,18 +85,5 @@ function onRequest(context) {
var enrollmentApps = policyModule.getStoreAppsForPolicy();
context["storeApps"] = JSON.stringify(enrollmentApps["content"]);
var correctivePolicies = policyModule.getAllPoliciesByType("CORRECTIVE")["content"];
if (correctivePolicies) {
var i;
for (i = 0; i < correctivePolicies.length; i++) {
if (correctivePolicies[i].id.toString() === policyId) {
correctivePolicies.splice(i, 1);
break;
}
}
}
context["correctivePolicies"] = JSON.stringify(correctivePolicies);
return context;
}

@ -280,72 +280,10 @@ stepForwardFrom["policy-profile"] = function () {
policy["profile"] = generatePolicyProfile();
}
var policyType = currentlyEffected.policyType;
$("input[name=policy-type-radio-btn][value=" + policyType + "]").prop("checked", true).trigger('change');
// add policy correction action page
var policyCorrectiveActionTemplateSrc =
"/public/cdmf.unit.policy.corrective-action/templates/policy-corrective-action.hbs";
var policyCorrectiveActionScriptSrc =
"/public/cdmf.unit.policy.corrective-action/js/policy-corrective-action.js";
var policyCorrectiveActionTemplateCacheKey = "policy-corrective-action";
$.template(policyCorrectiveActionTemplateCacheKey, context + policyCorrectiveActionTemplateSrc,
function (template) {
var content = template(
{
"deviceType": policy["platform"],
"correctivePolicies": $("#logged-in-user").data("corrective-policies")
}
);
$("#select-general-policy-type").html(content);
if ("GENERAL" === policyType && currentlyEffected.correctiveActions &&
currentlyEffected.correctiveActions.length > 0) {
currentlyEffected.correctiveActions.forEach(function (correctiveAction) {
if ("POLICY" === correctiveAction.actionType) {
if ($("#corrective-policy-input option[value=" + correctiveAction.policyId + "]").length > 0) {
$("#corrective-policy-input").val(correctiveAction.policyId);
} else {
$("#corrective-action-policy-id-missing-msg").removeClass("hidden");
}
// returned from for each since currently only supported corrective action type is
// POLICY.
return true;
}
});
}
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = context + policyCorrectiveActionScriptSrc;
document.head.prepend(script);
});
$(".policy-type-loading-corrective-actions").addClass("hidden");
// updating next-page wizard title with selected platform
$("#policy-type-page-wizard-title").text("EDIT " + policy["platform"] + " POLICY - " + policy["name"]);
};
/**
* Forward action of policy type page.
*/
stepForwardFrom["policy-type"] = function () {
policy["type"] = $("input[name=policy-type-radio-btn]:checked").val();
var correctiveActionList = [];
if (policy.type === "GENERAL") {
var selectedCorrectivePolicyId = $("#corrective-policy-input").val();
if (selectedCorrectivePolicyId && selectedCorrectivePolicyId !== "none") {
var correctiveAction = {
"actionType": "POLICY",
"policyId": selectedCorrectivePolicyId
};
correctiveActionList.push(correctiveAction);
}
}
policy["correctiveActionList"] = correctiveActionList;
// updating next-page wizard title with selected platform
$("#policy-criteria-page-wizard-title").text("EDIT " + policy["platform"] + " POLICY - " + policy["name"]);
};
/**
* Forward action of policy criteria page.
@ -556,8 +494,7 @@ var updatePolicy = function (policy, state) {
"description": policy["description"],
"compliance": policy["selectedNonCompliantAction"],
"ownershipType": null,
"policyType": policy["type"],
"correctiveActions": policy["correctiveActionList"],
"policyType": "GENERAL",
"profile": {
"profileName": policy["policyName"],
"deviceType": policy["platform"],

@ -69,30 +69,6 @@ var displayPolicy = function (policyPayloadObj) {
$("#policy-roles").text(policyPayloadObj.roles.toString().split(",").join(", "));
}
if ("GENERAL" === policyPayloadObj.policyType &&
policyPayloadObj.correctiveActions && policyPayloadObj.correctiveActions.length > 0) {
policyPayloadObj.correctiveActions.forEach(function (correctiveAction) {
if ("POLICY" === correctiveAction.actionType) {
$("#corrective-action-type-policy-id").html(correctiveAction.policyId);
var correctivePolicies = $("#logged-in-user").data("corrective-policies");
if (correctivePolicies) {
var i;
for (i = 0; i < correctivePolicies.length; i++) {
if (correctiveAction.policyId === correctivePolicies[i].id) {
$("#corrective-action-policy-id-missing-msg").addClass("hidden");
break;
}
}
}
return true;
}
});
$("#policy-corrective-actions-list").removeClass("hidden");
} else {
$("#policy-corrective-actions-list").addClass("hidden");
}
var policyId = policyPayloadObj["id"];
var deviceType = policy["platform"];
var policyOperations = $("#policy-operations");

@ -1,8 +1,7 @@
{{#zone "content"}}
{{#if isAuthorized}}
<span id="logged-in-user" class="hidden" data-username="{{@user.username}}" data-domain="{{@user.domain}}"
data-tenant-id="{{@user.tenantId}}" data-iscloud="{{isCloud}}"
data-corrective-policies="{{correctivePolicies}}"></span>
data-tenant-id="{{@user.tenantId}}" data-iscloud="{{isCloud}}"></span>
{{#defineZone "policy-profile-top"}}
<div class="row wr-device-board">
<div class="col-lg-12 wr-secondary-bar">
@ -47,42 +46,9 @@
<td class="sorting_1" style="padding:10px 15px;">Assigned Roles</td>
<td id="policy-roles" style="padding:10px 15px;"></td>
</tr>
<tr role="row" id="roles-row" class="even">
<td class="sorting_1" style="padding:10px 15px;">Policy Type</td>
<td id="policy-type" style="padding:10px 15px;"></td>
</tr>
</tbody>
</table>
{{/defineZone}}
<div id="policy-corrective-actions-list" class="hidden">
<div style="background: #008cc4; color: #fff; padding: 10px; margin-bottom: 5px">
Corrective Actions
</div>
<div class="add-margin-top-4x">
<div id="policy-corrective-actions">
<table class="table table-responsive table-striped">
<tbody>
<tr role="row">
<td class="sorting_1" style="padding:10px 15px; width: 14%;">Action Type</td>
<td style="padding:10px 15px;">POLICY</td>
<td class="sorting_1" style="padding:10px 15px; width: 14%;">Policy ID</td>
<td id="corrective-action-type-policy-id" style="padding:10px 15px;"></td>
</tr>
<tr id="corrective-action-policy-id-missing-msg">
<td class="alert alert-danger" colspan="4" style="padding:10px 15px;">
<strong>
<i class="icon fw fw-error"></i>
Corrective policy having the above Policy ID has been updated to
a GENERAL policy or it has been deleted. Hence this policy will
not be applied till modified correctly.
</strong>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div style="background: #008cc4; color: #fff; padding: 10px; margin-bottom: 5px">Description
</div>
<div class="add-margin-top-4x">

@ -45,7 +45,7 @@ function onRequest(context) {
var devicemgtProps = require("/app/modules/conf-reader/main.js")["conf"];
page["isCloud"] = devicemgtProps.isCloud;
page["correctivePolicies"] = JSON.stringify(policyModule.getAllPoliciesByType("CORRECTIVE")["content"]);
/*page["correctivePolicies"] = JSON.stringify(policyModule.getAllPoliciesByType("CORRECTIVE")["content"]);*/
return page;
}

@ -87,7 +87,7 @@ public interface PolicyDAO {
* @return list of retrieved {@link CorrectiveAction}
* @throws PolicyManagerDAOException is thrown when there is an error in retrieving corrective actions to database
*/
List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException;
List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException;
/**
* This method is used to update corrective actions of policy in the database based on the policy ID

@ -290,24 +290,25 @@ public class PolicyDAOImpl implements PolicyDAO {
}
@Override
public List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException {
public List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException {
try {
Connection conn = this.getConnection();
String query = "SELECT " +
"ACTION_TYPE, " +
"CORRECTIVE_POLICY_ID " +
"CORRECTIVE_POLICY_ID, " +
"FEATURE_ID "+
"FROM DM_POLICY_CORRECTIVE_ACTION " +
"WHERE POLICY_ID = ? AND FEATURE_ID = ?";
"WHERE POLICY_ID = ?";
try (PreparedStatement selectStmt = conn.prepareStatement(query)) {
List<CorrectiveAction> correctiveActions = new ArrayList<>();
selectStmt.setInt(1, policyId);
selectStmt.setInt(2, featureId);
try (ResultSet rs = selectStmt.executeQuery()) {
CorrectiveAction correctiveAction;
while (rs.next()) {
correctiveAction = new CorrectiveAction();
correctiveAction.setActionType(rs.getString("ACTION_TYPE"));
correctiveAction.setPolicyId(rs.getInt("CORRECTIVE_POLICY_ID"));
correctiveAction.setFeatureId(rs.getInt("FEATURE_ID"));
correctiveActions.add(correctiveAction);
}
}

@ -90,7 +90,6 @@ public class PolicyManagerImpl implements PolicyManager {
@Override
public Policy addPolicy(Policy policy) throws PolicyManagementException {
try {
PolicyManagementDAOFactory.beginTransaction();
if (policy.getProfile() != null && policy.getProfile().getProfileId() == 0) {
@ -102,6 +101,10 @@ public class PolicyManagerImpl implements PolicyManager {
profileDAO.addProfile(profile);
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
}
policy = policyDAO.addPolicy(policy);
if (policy.getProfile() != null) {
Profile profile = policy.getProfile();
List<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
for (ProfileFeature profileFeature : profileFeaturesList) {
if (profileFeature.getCorrectiveActions() != null &&
@ -115,7 +118,6 @@ public class PolicyManagerImpl implements PolicyManager {
}
}
}
policy = policyDAO.addPolicy(policy);
if (policy.getUsers() != null) {
policyDAO.addPolicyToUser(policy.getUsers(), policy);
@ -728,14 +730,17 @@ public class PolicyManagerImpl implements PolicyManager {
try {
Profile profile = profileManager.getProfile(policy.getProfileId());
policy.setProfile(profile);
PolicyManagementDAOFactory.openConnection();
for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) {
List<CorrectiveAction> correctiveActionsOfPolicy = policyDAO
.getCorrectiveActionsOfPolicy(policyId, profileFeature.getId());
if (correctiveActionsOfPolicy != null) {
profileFeature.setCorrectiveActions(correctiveActionsOfPolicy);
.getCorrectiveActionsOfPolicy(policyId);
for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) {
List<CorrectiveAction> correctiveActions = new ArrayList<>();
for (CorrectiveAction correctiveAction : correctiveActionsOfPolicy) {
if (correctiveAction.getFeatureId() == profileFeature.getId()) {
correctiveActions.add(correctiveAction);
}
}
profileFeature.setCorrectiveActions(correctiveActions);
}
} catch (ProfileManagementException e) {
throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
@ -1339,12 +1344,19 @@ public class PolicyManagerImpl implements PolicyManager {
private void buildPolicyList(List<Policy> policyList, List<Profile> profileList)
throws PolicyManagerDAOException, GroupManagementException {
for (Policy policy : policyList) {
List<CorrectiveAction> correctiveActionsOfPolicy = policyDAO
.getCorrectiveActionsOfPolicy(policy.getId());
for (Profile profile : profileList) {
if (policy.getProfileId() == profile.getProfileId()) {
policy.setProfile(profile);
for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) {
profileFeature.setCorrectiveActions(policyDAO
.getCorrectiveActionsOfPolicy(policy.getId(), profileFeature.getId()));
List<CorrectiveAction> correctiveActionList = new ArrayList<>();
for (CorrectiveAction correctiveAction : correctiveActionsOfPolicy) {
if (profileFeature.getId() == correctiveAction.getFeatureId()) {
correctiveActionList.add(correctiveAction);
}
}
profileFeature.setCorrectiveActions(correctiveActionList);
}
}
}

@ -65,8 +65,8 @@ public final class PolicyManagementConstants {
public static final String GENERAL_POLICY_TYPE = "GENERAL";
public static final String CORRECTIVE_POLICY_TYPE = "CORRECTIVE";
public static final String POLICY_CORRECTIVE_ACTION_TYPE = "POLICY";
public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY";
public static final String CORRECTIVE_POLICY_LIST = "CORRECTIVE_ACTIONS";
public static final String POLICY_FEATURE_CODE = "POLICY_ACTION";
public static final String POLICY_ACTIONS = "POLICY_ACTIONS";
/**
Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME,

@ -35,7 +35,6 @@
package org.wso2.carbon.policy.mgt.core.util;
import com.google.gson.Gson;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
@ -140,51 +139,10 @@ public class PolicyManagerUtil {
public static Operation transformPolicy(Policy policy) throws PolicyTransformException {
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
PolicyOperation policyOperation = new PolicyOperation();
policyOperation.setEnabled(true);
policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY);
policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE);
/*if (policy.getPolicyType() != null &&
PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) &&
policy.getCorrectiveActions() != null) {
for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) {
if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE
.equalsIgnoreCase(correctiveAction.getActionType())) {
PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
try {
Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId());
if (correctivePolicy == null || !PolicyManagementConstants.CORRECTIVE_POLICY_TYPE
.equalsIgnoreCase(correctivePolicy.getPolicyType() )) {
String msg = "No corrective policy was found for the policy " + policy.getPolicyName() +
" and policy ID " + policy.getId();
log.error(msg);
throw new PolicyTransformException(msg);
} else {
List<ProfileOperation> correctiveProfileOperations = createProfileOperations(
correctivePolicy.getProfile().getProfileFeaturesList());
ProfileFeature correctivePolicyFeature = new ProfileFeature();
correctivePolicyFeature.setProfileId(correctivePolicy.getProfileId());
correctivePolicyFeature.setContent(new Gson().toJson(correctiveProfileOperations));
correctivePolicyFeature.setDeviceType(correctivePolicy.getProfile().getDeviceType());
correctivePolicyFeature.setFeatureCode(
PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE);
correctivePolicyFeature.setId(correctivePolicy.getId());
effectiveFeatures.add(correctivePolicyFeature);
}
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving corrective policy for policy " +
policy.getPolicyName() + " and policy ID " + policy.getId();
log.error(msg, e);
throw new PolicyTransformException(msg, e);
}
// Currently only supported POLICY corrective action type so the break is added. This should be
// removed when we start supporting other corrective action types
break;
}
}
}*/
policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures));
if (policy.getPolicyType() != null &&
PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType())) {
@ -197,69 +155,32 @@ public class PolicyManagerUtil {
private static void setCorrectiveActions(List<ProfileFeature> features,
PolicyOperation policyOperation, Policy policy)
throws PolicyTransformException {
ProfileOperation correctiveOperations = new ProfileOperation();
correctiveOperations.setCode(PolicyManagementConstants.CORRECTIVE_POLICY_LIST);
for (ProfileFeature effectiveFeature : features) {
if (effectiveFeature.getCorrectiveActions() != null) {
for (CorrectiveAction correctiveAction : effectiveFeature.getCorrectiveActions()) {
if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE
.equalsIgnoreCase(correctiveAction.getActionType())) {
PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
ProfileOperation correctiveProfileOperation = new ProfileOperation();
correctiveProfileOperation.setCode(PolicyManagementConstants.POLICY_ACTIONS);
Set<Integer> correctivePolicyIdSet = new HashSet<>();
try {
Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId());
if (correctivePolicy == null || !PolicyManagementConstants
.CORRECTIVE_POLICY_TYPE.equalsIgnoreCase(correctivePolicy
.getPolicyType())) {
String msg = "No corrective policy was found for the policy " +
policy.getPolicyName() + " and policy ID " + policy.getId();
log.error(msg);
throw new PolicyTransformException(msg);
} else {
createCorrectiveProfileOperations(correctivePolicy, correctiveOperations);
policyOperation.getProfileOperations().add(correctiveOperations);
}
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving corrective policy for policy " +
policy.getPolicyName() + " and policy ID " + policy.getId();
log.error(msg, e);
throw new PolicyTransformException(msg, e);
for (ProfileFeature feature : features) {
List<CorrectiveAction> correctiveActions = feature.getCorrectiveActions();
for (CorrectiveAction correctiveAction : correctiveActions) {
if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE
.equals(correctiveAction.getActionType())) {
correctivePolicyIdSet.add(correctiveAction.getPolicyId());
}
//Add check for another action type in future implementation
}
}
/*for (CorrectiveAction correctiveAction : effectiveFeature.getCorrectiveActions()) {
if (PolicyManagementConstants.POLICY_CORRECTIVE_ACTION_TYPE
.equalsIgnoreCase(correctiveAction.getActionType())) {
PolicyAdministratorPoint pap = new PolicyAdministratorPointImpl();
try {
Policy correctivePolicy = pap.getPolicy(correctiveAction.getPolicyId());
if (correctivePolicy == null || !PolicyManagementConstants
.CORRECTIVE_POLICY_TYPE.equalsIgnoreCase(correctivePolicy
.getPolicyType() )) {
String msg = "No corrective policy was found for the policy " +
policy.getPolicyName() + " and policy ID " + policy.getId();
log.error(msg);
throw new PolicyTransformException(msg);
} else {
*//*List<ProfileOperation> correctiveProfileOperations =
createProfileOperations();*//*
createCorrectiveProfileOperations(correctivePolicy.getProfile()
.getProfileFeaturesList());
ProfileFeature correctivePolicyFeature = new ProfileFeature();
correctivePolicyFeature.setProfileId(correctivePolicy.getProfileId());
correctivePolicyFeature.setContent(new Gson()
.toJson(correctiveProfileOperations));
correctivePolicyFeature.setDeviceType(correctivePolicy
.getProfile().getDeviceType());
correctivePolicyFeature.setFeatureCode(
PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE);
correctivePolicyFeature.setId(correctivePolicy.getId());
effectiveFeatures.add(correctivePolicyFeature);
List<Policy> allCorrectivePolicies = pap
.getPolicies(PolicyManagementConstants.CORRECTIVE_POLICY_TYPE);
idLoop:
for (Integer policyId : correctivePolicyIdSet) {
for (Policy correctivePolicy : allCorrectivePolicies) {
if (policyId == correctivePolicy.getId()) {
createCorrectiveProfileOperations(correctivePolicy, correctiveProfileOperation);
policyOperation.getProfileOperations().add(correctiveProfileOperation);
continue idLoop;
}
}
}
} catch (PolicyManagementException e) {
String msg = "Error occurred while retrieving corrective policy for policy " +
@ -267,37 +188,27 @@ public class PolicyManagerUtil {
log.error(msg, e);
throw new PolicyTransformException(msg, e);
}
// Currently only supported POLICY corrective action type so the break
// is added. This should be removed when we start supporting other
// corrective action types
break;
}
}*/
}
}
}
private static void createCorrectiveProfileOperations(Policy correctivePolicy,
ProfileOperation correctiveOperationList) {
ProfileOperation profileOperation = new ProfileOperation();
profileOperation.setId(correctivePolicy.getId());
profileOperation.setCode(PolicyManagementConstants.CORRECTIVE_POLICY_FEATURE_CODE);
profileOperation.setCode(PolicyManagementConstants.POLICY_FEATURE_CODE);
profileOperation.setEnabled(true);
profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING);
profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE);
String correctivePayload =(String) correctiveOperationList.getPayLoad();
if (correctivePayload == null) {
correctivePayload = "";
}
List<ProfileOperation> profileOperations = createProfileOperations(correctivePolicy
.getProfile().getProfileFeaturesList());
String payload = new Gson().toJson(profileOperations);
correctiveOperationList.setPayLoad(correctivePayload.concat(payload));
profileOperation.setPayLoad(profileOperations);
List<ProfileOperation> payLoad;
if (correctiveOperationList.getPayLoad() != null) {
payLoad = (List<ProfileOperation>) correctiveOperationList.getPayLoad();
} else {
payLoad = new ArrayList<>();
}
payLoad.add(profileOperation);
correctiveOperationList.setPayLoad(payLoad);
}
@ -307,10 +218,14 @@ public class PolicyManagerUtil {
ProfileOperation profileOperation = new ProfileOperation();
profileOperation.setCode(feature.getFeatureCode());
profileOperation.setEnabled(true);
profileOperation.setId(feature.getId());
profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING);
profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE);
profileOperation.setPayLoad(feature.getContent());
for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) {
if (profileOperation.getCorrectiveActionIds() == null) {
profileOperation.setCorrectiveActionIds(new ArrayList<>());
}
profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId());
}
profileOperations.add(profileOperation);

Loading…
Cancel
Save