Change policy

corrective-policy
Pahansith 5 years ago
parent eecb3704d5
commit e486db868e

@ -126,12 +126,6 @@ public class PolicyWrapper {
@NotNull @NotNull
private String policyType; private String policyType;
@ApiModelProperty(
name = "correctiveActions",
value = "List of corrective actions to be applied when the policy is violated"
)
private List<CorrectiveAction> correctiveActions;
public String getPolicyType() { public String getPolicyType() {
return policyType; return policyType;
} }
@ -219,12 +213,4 @@ public class PolicyWrapper {
public void setDeviceGroups(List<DeviceGroupWrapper> deviceGroups) { public void setDeviceGroups(List<DeviceGroupWrapper> deviceGroups) {
this.deviceGroups = deviceGroups; this.deviceGroups = deviceGroups;
} }
public List<CorrectiveAction> getCorrectiveActions() {
return correctiveActions;
}
public void setCorrectiveActions(List<CorrectiveAction> correctiveActions) {
this.correctiveActions = correctiveActions;
}
} }

@ -21,8 +21,10 @@ package org.wso2.carbon.device.mgt.jaxrs.beans;
import com.google.gson.Gson; import com.google.gson.Gson;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
@ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile "
+ "features") + "features")
@ -57,6 +59,10 @@ public class ProfileFeature implements Serializable {
value = "The payload which is submitted to each feature", value = "The payload which is submitted to each feature",
required = true) required = true)
private String payLoad; private String payLoad;
@ApiModelProperty(name = "correctiveActions",
value = "List of corrective actions to be applied when the policy is violated",
required = true)
private List<CorrectiveAction> correctiveActions;
public int getId() { public int getId() {
return id; return id;
@ -109,4 +115,12 @@ public class ProfileFeature implements Serializable {
public void setContent(Object content) { public void setContent(Object content) {
this.content = content; this.content = content;
} }
public List<CorrectiveAction> getCorrectiveActions() {
return correctiveActions;
}
public void setCorrectiveActions(List<CorrectiveAction> correctiveActions) {
this.correctiveActions = correctiveActions;
}
} }

@ -140,7 +140,6 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
policy.setPolicyName(policyWrapper.getPolicyName()); policy.setPolicyName(policyWrapper.getPolicyName());
policy.setDescription(policyWrapper.getDescription()); policy.setDescription(policyWrapper.getDescription());
policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile())); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile()));
policy.setCorrectiveActions(policyWrapper.getCorrectiveActions());
policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setOwnershipType(policyWrapper.getOwnershipType());
policy.setActive(policyWrapper.isActive()); policy.setActive(policyWrapper.isActive());
policy.setRoles(policyWrapper.getRoles()); policy.setRoles(policyWrapper.getRoles());

@ -60,6 +60,7 @@ public class DeviceMgtUtil {
profileFeature.setDeviceType(mdmProfileFeature.getDeviceTypeId()); profileFeature.setDeviceType(mdmProfileFeature.getDeviceTypeId());
profileFeature.setFeatureCode(mdmProfileFeature.getFeatureCode()); profileFeature.setFeatureCode(mdmProfileFeature.getFeatureCode());
profileFeature.setId(mdmProfileFeature.getId()); profileFeature.setId(mdmProfileFeature.getId());
profileFeature.setCorrectiveActions(mdmProfileFeature.getCorrectiveActions());
return profileFeature; return profileFeature;
} }

@ -201,13 +201,6 @@ public class Policy implements Comparable<Policy>, Serializable {
example = "GENERAL") example = "GENERAL")
private String policyType; private String policyType;
@ApiModelProperty(
name = "correctiveActions",
value = "List of corrective actions to be applied when the policy is violated",
example = "[{'actionType': 'POLICY', 'policyId': 1}]"
)
private List<CorrectiveAction> correctiveActions;
@XmlElement @XmlElement
public int getId() { public int getId() {
return id; return id;
@ -379,16 +372,6 @@ public class Policy implements Comparable<Policy>, Serializable {
this.policyType = policyType; this.policyType = policyType;
} }
@XmlElement
public List<CorrectiveAction> getCorrectiveActions() {
return correctiveActions;
}
public void setCorrectiveActions(
List<CorrectiveAction> correctiveActions) {
this.correctiveActions = correctiveActions;
}
@Override @Override
public int compareTo(Policy o) { public int compareTo(Policy o) {
if (this.priorityId == o.priorityId) if (this.priorityId == o.priorityId)

@ -22,6 +22,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
@ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile "
+ "features") + "features")
@ -64,6 +65,11 @@ public class ProfileFeature implements Serializable {
example = "{\\\"enabled\\\":false}") example = "{\\\"enabled\\\":false}")
private Object content; private Object content;
@ApiModelProperty(name = "correctiveActions",
value = "List of corrective actions to be applied when the policy is violated",
required = true)
private List<CorrectiveAction> correctiveActions;
public int getId() { public int getId() {
return id; return id;
} }
@ -103,4 +109,12 @@ public class ProfileFeature implements Serializable {
public void setContent(Object content) { public void setContent(Object content) {
this.content = content; this.content = content;
} }
public List<CorrectiveAction> getCorrectiveActions() {
return correctiveActions;
}
public void setCorrectiveActions(List<CorrectiveAction> correctiveActions) {
this.correctiveActions = correctiveActions;
}
} }

@ -77,7 +77,8 @@ public interface PolicyDAO {
* @param policyId is used uniquely identify the policy to which corrective actions are to be added * @param policyId is used uniquely identify the policy to which corrective actions are to be added
* @throws PolicyManagerDAOException is thrown when there is an error in adding corrective actions to database * @throws PolicyManagerDAOException is thrown when there is an error in adding corrective actions to database
*/ */
void addCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) void addCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId,
int featureId)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
/** /**
@ -86,7 +87,7 @@ public interface PolicyDAO {
* @return list of retrieved {@link CorrectiveAction} * @return list of retrieved {@link CorrectiveAction}
* @throws PolicyManagerDAOException is thrown when there is an error in retrieving corrective actions to database * @throws PolicyManagerDAOException is thrown when there is an error in retrieving corrective actions to database
*/ */
List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException; List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException;
/** /**
* This method is used to update corrective actions of policy in the database based on the policy ID * This method is used to update corrective actions of policy in the database based on the policy ID
@ -94,7 +95,8 @@ public interface PolicyDAO {
* @param policyId is used uniquely identify the policy to which corrective actions are to be updated * @param policyId is used uniquely identify the policy to which corrective actions are to be updated
* @throws PolicyManagerDAOException is thrown when there is an error in updating corrective actions to database * @throws PolicyManagerDAOException is thrown when there is an error in updating corrective actions to database
*/ */
void updateCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) void updateCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId,
int featureId)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
/** /**
@ -103,7 +105,8 @@ public interface PolicyDAO {
* @param policyId is used uniquely identify the policy to which corrective actions are to be deleted * @param policyId is used uniquely identify the policy to which corrective actions are to be deleted
* @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database * @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database
*/ */
void deleteCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) void deleteCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId,
int featureId)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
Policy updateUserOfPolicy(List<String> usersToAdd, Policy policy) throws PolicyManagerDAOException; Policy updateUserOfPolicy(List<String> usersToAdd, Policy policy) throws PolicyManagerDAOException;

@ -263,19 +263,21 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public void addCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) public void addCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions,
int policyId, int featureId)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_CORRECTIVE_ACTION " + String query = "INSERT INTO DM_POLICY_CORRECTIVE_ACTION " +
"(ACTION_TYPE, " + "(ACTION_TYPE, " +
"CORRECTIVE_POLICY_ID, " + "CORRECTIVE_POLICY_ID, " +
"POLICY_ID) VALUES (?, ?, ?)"; "POLICY_ID, FEATURE_ID) VALUES (?, ?, ?, ?)";
try (PreparedStatement insertStmt = conn.prepareStatement(query)) { try (PreparedStatement insertStmt = conn.prepareStatement(query)) {
for (CorrectiveAction correctiveAction : correctiveActions) { for (CorrectiveAction correctiveAction : correctiveActions) {
insertStmt.setString(1, correctiveAction.getActionType()); insertStmt.setString(1, correctiveAction.getActionType());
insertStmt.setInt(2, correctiveAction.getPolicyId()); insertStmt.setInt(2, correctiveAction.getPolicyId());
insertStmt.setInt(3, policyId); insertStmt.setInt(3, policyId);
insertStmt.setInt(4, featureId);
insertStmt.addBatch(); insertStmt.addBatch();
} }
insertStmt.executeBatch(); insertStmt.executeBatch();
@ -288,17 +290,18 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException { public List<CorrectiveAction> getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException {
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String query = "SELECT " + String query = "SELECT " +
"ACTION_TYPE, " + "ACTION_TYPE, " +
"CORRECTIVE_POLICY_ID " + "CORRECTIVE_POLICY_ID " +
"FROM DM_POLICY_CORRECTIVE_ACTION " + "FROM DM_POLICY_CORRECTIVE_ACTION " +
"WHERE POLICY_ID = ?"; "WHERE POLICY_ID = ? AND FEATURE_ID = ?";
try (PreparedStatement selectStmt = conn.prepareStatement(query)) { try (PreparedStatement selectStmt = conn.prepareStatement(query)) {
List<CorrectiveAction> correctiveActions = new ArrayList<>(); List<CorrectiveAction> correctiveActions = new ArrayList<>();
selectStmt.setInt(1, policyId); selectStmt.setInt(1, policyId);
selectStmt.setInt(2, featureId);
try (ResultSet rs = selectStmt.executeQuery()) { try (ResultSet rs = selectStmt.executeQuery()) {
CorrectiveAction correctiveAction; CorrectiveAction correctiveAction;
while (rs.next()) { while (rs.next()) {
@ -318,19 +321,21 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public void updateCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) public void updateCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions,
int policyId, int featureId)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " + String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " +
"SET CORRECTIVE_POLICY_ID = ? " + "SET CORRECTIVE_POLICY_ID = ? " +
"WHERE ACTION_TYPE = ? " + "WHERE ACTION_TYPE = ? " +
"AND POLICY_ID = ?"; "AND POLICY_ID = ? AND FEATURE_ID = ?";
try (PreparedStatement updateStmt = conn.prepareStatement(query)) { try (PreparedStatement updateStmt = conn.prepareStatement(query)) {
for (CorrectiveAction correctiveAction : correctiveActions) { for (CorrectiveAction correctiveAction : correctiveActions) {
updateStmt.setInt(1, correctiveAction.getPolicyId()); updateStmt.setInt(1, correctiveAction.getPolicyId());
updateStmt.setString(2, correctiveAction.getActionType()); updateStmt.setString(2, correctiveAction.getActionType());
updateStmt.setInt(3, policyId); updateStmt.setInt(3, policyId);
updateStmt.setInt(4, featureId);
updateStmt.addBatch(); updateStmt.addBatch();
} }
updateStmt.executeBatch(); updateStmt.executeBatch();
@ -343,17 +348,19 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
@Override @Override
public void deleteCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions, int policyId) public void deleteCorrectiveActionsOfPolicy(List<CorrectiveAction> correctiveActions,
int policyId, int featureId)
throws PolicyManagerDAOException { throws PolicyManagerDAOException {
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " + String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " +
"WHERE ACTION_TYPE = ? " + "WHERE ACTION_TYPE = ? " +
"AND POLICY_ID = ?"; "AND POLICY_ID = ? AND FEATURE_ID = ?";
try (PreparedStatement deleteStmt = conn.prepareStatement(query)) { try (PreparedStatement deleteStmt = conn.prepareStatement(query)) {
for (CorrectiveAction correctiveAction : correctiveActions) { for (CorrectiveAction correctiveAction : correctiveActions) {
deleteStmt.setString(1, correctiveAction.getActionType()); deleteStmt.setString(1, correctiveAction.getActionType());
deleteStmt.setInt(2, policyId); deleteStmt.setInt(2, policyId);
deleteStmt.setInt(3, featureId);
deleteStmt.addBatch(); deleteStmt.addBatch();
} }
deleteStmt.executeBatch(); deleteStmt.executeBatch();

@ -35,6 +35,7 @@
package org.wso2.carbon.policy.mgt.core.mgt.impl; package org.wso2.carbon.policy.mgt.core.mgt.impl;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
@ -101,6 +102,18 @@ public class PolicyManagerImpl implements PolicyManager {
profileDAO.addProfile(profile); profileDAO.addProfile(profile);
featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId());
List<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList();
for (ProfileFeature profileFeature : profileFeaturesList) {
if (profileFeature.getCorrectiveActions() != null &&
!profileFeature.getCorrectiveActions().isEmpty()) {
if (log.isDebugEnabled()) {
log.debug("Adding corrective actions for policy " + policy.getPolicyName() +
" having policy id " + policy.getId());
}
policyDAO.addCorrectiveActionsOfPolicy(profileFeature.getCorrectiveActions(),
policy.getId(), profileFeature.getId());
}
}
} }
policy = policyDAO.addPolicy(policy); policy = policyDAO.addPolicy(policy);
@ -140,13 +153,13 @@ public class PolicyManagerImpl implements PolicyManager {
policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias());
} }
if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { /*if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Adding corrective actions for policy " + policy.getPolicyName() + log.debug("Adding corrective actions for policy " + policy.getPolicyName() +
" having policy id " + policy.getId()); " having policy id " + policy.getId());
} }
policyDAO.addCorrectiveActionsOfPolicy(policy.getCorrectiveActions(), policy.getId()); policyDAO.addCorrectiveActionsOfPolicy(policy.getCorrectiveActions(), policy.getId());
} }*/
if (policy.isActive()) { if (policy.isActive()) {
policyDAO.activatePolicy(policy.getId()); policyDAO.activatePolicy(policy.getId());
@ -189,6 +202,8 @@ public class PolicyManagerImpl implements PolicyManager {
List<ProfileFeature> featuresToDelete = new ArrayList<>(); List<ProfileFeature> featuresToDelete = new ArrayList<>();
List<String> temp = new ArrayList<>(); List<String> temp = new ArrayList<>();
List<String> updateDFes = new ArrayList<>(); List<String> updateDFes = new ArrayList<>();
Map<Integer, List<CorrectiveAction>> updatedCorrectiveActionsMap = new HashMap<>();
Map<Integer, List<CorrectiveAction>> existingCorrectiveActionsMap = new HashMap<>();
List<ProfileFeature> updatedFeatureList = policy.getProfile().getProfileFeaturesList(); List<ProfileFeature> updatedFeatureList = policy.getProfile().getProfileFeaturesList();
@ -196,7 +211,9 @@ public class PolicyManagerImpl implements PolicyManager {
// Checks for the existing features // Checks for the existing features
for (ProfileFeature feature : updatedFeatureList) { for (ProfileFeature feature : updatedFeatureList) {
updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions());
for (ProfileFeature fe : existingProfileFeaturesList) { for (ProfileFeature fe : existingProfileFeaturesList) {
existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions());
if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) {
existingFeaturesList.add(feature); existingFeaturesList.add(feature);
temp.add(feature.getFeatureCode()); temp.add(feature.getFeatureCode());
@ -272,35 +289,44 @@ public class PolicyManagerImpl implements PolicyManager {
policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias());
} }
List<CorrectiveAction> updatedCorrectiveActions = policy.getCorrectiveActions(); /*List<CorrectiveAction> updatedCorrectiveActions = policy.getCorrectiveActions();
List<CorrectiveAction> existingCorrectiveActions = previousPolicy.getCorrectiveActions(); List<CorrectiveAction> existingCorrectiveActions = previousPolicy.getCorrectiveActions();*/
//Iterate all corrective actions in the new policy payload against it's features
for (Integer featureId : updatedCorrectiveActionsMap.keySet()) {
List<CorrectiveAction> correctiveActionsToUpdate = new ArrayList<>(); List<CorrectiveAction> correctiveActionsToUpdate = new ArrayList<>();
List<CorrectiveAction> correctiveActionsToDelete = new ArrayList<>(); List<CorrectiveAction> correctiveActionsToDelete = new ArrayList<>();
List<CorrectiveAction> correctiveActionsToAdd = new ArrayList<>(); List<CorrectiveAction> correctiveActionsToAdd = new ArrayList<>();
List<String> correctiveActionTypesToUpdate = new ArrayList<>(); List<String> correctiveActionTypesToUpdate = new ArrayList<>();
List<String> existingCorrectiveActionTypes = new ArrayList<>(); List<String> existingCorrectiveActionTypes = new ArrayList<>();
List<CorrectiveAction> updatedCorrectiveActions = updatedCorrectiveActionsMap.get(featureId);
if (updatedCorrectiveActions != null) { //Check this feature already have a corrective action
if (existingCorrectiveActionsMap.containsKey(featureId)) {
//Existing corrective actions of the selected feature
List<CorrectiveAction> existingCorrectiveActions = existingCorrectiveActionsMap.get(featureId);
for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) {
for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) {
if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) {
//If old action type is same as new action type, put them into
// updating list
correctiveActionsToUpdate.add(updatedCorrectiveAction); correctiveActionsToUpdate.add(updatedCorrectiveAction);
existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType());
} }
} }
//Newly added action types added to this list
correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType());
} }
for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) {
for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) {
if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) { correctiveActionsToDelete.add(existingCorrectiveAction);
correctiveActionsToAdd.add(updatedCorrectiveAction);
} }
} }
} }
for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) {
if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) {
correctiveActionsToDelete.add(existingCorrectiveAction); correctiveActionsToAdd.add(updatedCorrectiveAction);
} }
} }
@ -310,17 +336,36 @@ public class PolicyManagerImpl implements PolicyManager {
} }
if (!correctiveActionsToUpdate.isEmpty()) { if (!correctiveActionsToUpdate.isEmpty()) {
policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, previousPolicy.getId()); policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate,
previousPolicy.getId(), featureId);
} }
if (!correctiveActionsToAdd.isEmpty()) { if (!correctiveActionsToAdd.isEmpty()) {
policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, previousPolicy.getId()); policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd,
previousPolicy.getId(), featureId);
} }
if (!correctiveActionsToDelete.isEmpty()) { if (!correctiveActionsToDelete.isEmpty()) {
policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, previousPolicy.getId()); policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete,
previousPolicy.getId(), featureId);
}
} }
/*for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) {
if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) {
correctiveActionsToAdd.add(updatedCorrectiveAction);
}
}
for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) {
if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) {
correctiveActionsToDelete.add(existingCorrectiveAction);
}
}*/
PolicyManagementDAOFactory.commitTransaction(); PolicyManagementDAOFactory.commitTransaction();
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
PolicyManagementDAOFactory.rollbackTransaction(); PolicyManagementDAOFactory.rollbackTransaction();
@ -656,7 +701,6 @@ public class PolicyManagerImpl implements PolicyManager {
log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() + log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() +
" having policy id " + policy.getId()); " having policy id " + policy.getId());
} }
policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId));
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" +
@ -681,6 +725,10 @@ public class PolicyManagerImpl implements PolicyManager {
// PolicyManagementDAOFactory.openConnection(); // PolicyManagementDAOFactory.openConnection();
Profile profile = profileManager.getProfile(policy.getProfileId()); Profile profile = profileManager.getProfile(policy.getProfileId());
policy.setProfile(profile); policy.setProfile(profile);
for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) {
profileFeature.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId, profileFeature.getId()));
}
} catch (ProfileManagementException e) { } catch (ProfileManagementException e) {
throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" +
policyId + ")", e); policyId + ")", e);
@ -688,8 +736,12 @@ public class PolicyManagerImpl implements PolicyManager {
// throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); // throw new PolicyManagementException("Error occurred while opening a connection to the data source", e);
// } finally { // } finally {
// PolicyManagementDAOFactory.closeConnection(); // PolicyManagementDAOFactory.closeConnection();
} catch (PolicyManagerDAOException e) {
throw new PolicyManagementException("Error occurred while getting the corrective " +
"actions related to policy ID (" + policyId + ")", e);
} }
return policy; return policy;
} }
@ -1278,6 +1330,10 @@ public class PolicyManagerImpl implements PolicyManager {
for (Profile profile : profileList) { for (Profile profile : profileList) {
if (policy.getProfileId() == profile.getProfileId()) { if (policy.getProfileId() == profile.getProfileId()) {
policy.setProfile(profile); policy.setProfile(profile);
for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) {
profileFeature.setCorrectiveActions(policyDAO
.getCorrectiveActionsOfPolicy(policy.getId(), profileFeature.getId()));
}
} }
} }
policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId()));
@ -1293,7 +1349,6 @@ public class PolicyManagerImpl implements PolicyManager {
log.debug("Retrieving corrective actions for policy " + policy.getPolicyName() + log.debug("Retrieving corrective actions for policy " + policy.getPolicyName() +
" having policy id " + policy.getId()); " having policy id " + policy.getId());
} }
policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policy.getId()));
} }
Collections.sort(policyList); Collections.sort(policyList);
} }

@ -145,8 +145,7 @@ public class PolicyManagerUtil {
policyOperation.setEnabled(true); policyOperation.setEnabled(true);
policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY); policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY);
policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE); policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE);
/*if (policy.getPolicyType() != null &&
if (policy.getPolicyType() != null &&
PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) && PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) &&
policy.getCorrectiveActions() != null) { policy.getCorrectiveActions() != null) {
for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) { for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) {
@ -184,13 +183,63 @@ public class PolicyManagerUtil {
break; break;
} }
} }
} }*/
policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures));
if (policy.getPolicyType() != null &&
PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType())) {
setCorrectiveActions(effectiveFeatures, policyOperation, policy);
}
policyOperation.setPayLoad(policyOperation.getProfileOperations()); policyOperation.setPayLoad(policyOperation.getProfileOperations());
return policyOperation; return policyOperation;
} }
private static void setCorrectiveActions(List<ProfileFeature> features,
PolicyOperation policyOperation, Policy policy) throws PolicyTransformException {
List<ProfileFeature> effectiveFeatures = new ArrayList<>(features);
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();
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());
List<ProfileOperation> profileOperations = policyOperation.getProfileOperations();
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;
}
}
}
}
}
public static List<ProfileOperation> createProfileOperations(List<ProfileFeature> effectiveFeatures) { public static List<ProfileOperation> createProfileOperations(List<ProfileFeature> effectiveFeatures) {
List<ProfileOperation> profileOperations = new ArrayList<>(); List<ProfileOperation> profileOperations = new ArrayList<>();
for (ProfileFeature feature : effectiveFeatures) { for (ProfileFeature feature : effectiveFeatures) {

@ -205,10 +205,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION (
ACTION_TYPE VARCHAR(45) NOT NULL, ACTION_TYPE VARCHAR(45) NOT NULL,
CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL,
POLICY_ID INT(11) NOT NULL, POLICY_ID INT(11) NOT NULL,
FEATURE_ID INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID ) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID ) REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION
); );

@ -85,11 +85,17 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` (
`ACTION_TYPE` VARCHAR(45) NOT NULL, `ACTION_TYPE` VARCHAR(45) NOT NULL,
`CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL, `CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL,
`POLICY_ID` INT(11) NOT NULL, `POLICY_ID` INT(11) NOT NULL,
`FEATURE_ID` INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID) REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB )ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1; DEFAULT CHARACTER SET = latin1;

@ -199,10 +199,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION (
ACTION_TYPE VARCHAR(45) NOT NULL, ACTION_TYPE VARCHAR(45) NOT NULL,
CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL,
POLICY_ID INT(11) NOT NULL, POLICY_ID INT(11) NOT NULL,
FEATURE_ID INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID ) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID ) REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION
); );

@ -249,11 +249,17 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION (
ACTION_TYPE VARCHAR(45) NOT NULL, ACTION_TYPE VARCHAR(45) NOT NULL,
CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL,
POLICY_ID INTEGER NOT NULL, POLICY_ID INTEGER NOT NULL,
FEATURE_ID INTEGER NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID) REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION
); );

@ -233,11 +233,17 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION (
ACTION_TYPE VARCHAR(45) NOT NULL, ACTION_TYPE VARCHAR(45) NOT NULL,
CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL,
POLICY_ID INT(11) NOT NULL, POLICY_ID INT(11) NOT NULL,
FEATURE_ID INT(11) NOT NULL,
PRIMARY KEY (ID), PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID) REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION ON UPDATE NO ACTION
)ENGINE = InnoDB; )ENGINE = InnoDB;

@ -340,10 +340,14 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION (
ACTION_TYPE VARCHAR2(45) NOT NULL, ACTION_TYPE VARCHAR2(45) NOT NULL,
CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL, CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL,
POLICY_ID NUMBER(10) NOT NULL, POLICY_ID NUMBER(10) NOT NULL,
FEATURE_ID NUMBER(10) NOT NULL,
CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID), CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID) FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID) REFERENCES DM_POLICY (ID),
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
) )
/ /

@ -237,6 +237,23 @@ CREATE TABLE IF NOT EXISTS DM_POLICY (
CREATE SEQUENCE DM_DEVICE_POLICY_seq; CREATE SEQUENCE DM_DEVICE_POLICY_seq;
CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION (
ID BIGSERIAL NOT NULL PRIMARY KEY,
ACTION_TYPE VARCHAR(45) NOT NULL,
CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL,
POLICY_ID INTEGER NOT NULL,
FEATURE_ID INTEGER NOT NULL,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (POLICY_ID)
REFERENCES DM_POLICY (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION
FOREIGN KEY (FEATURE_ID)
REFERENCES DM_PROFILE_FEATURES (ID)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);
CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY (
ID INTEGER NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_POLICY_seq') , ID INTEGER NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_POLICY_seq') ,

Loading…
Cancel
Save