From e486db868e70585adc5a06e2753239fb948ed33c Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 20 Mar 2020 14:20:24 +0530 Subject: [PATCH] Change policy --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 14 -- .../mgt/jaxrs/beans/ProfileFeature.java | 14 ++ .../impl/PolicyManagementServiceImpl.java | 1 - .../device/mgt/jaxrs/util/DeviceMgtUtil.java | 1 + .../device/mgt/common/policy/mgt/Policy.java | 17 --- .../mgt/common/policy/mgt/ProfileFeature.java | 14 ++ .../carbon/policy/mgt/core/dao/PolicyDAO.java | 11 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 23 ++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 123 +++++++++++++----- .../mgt/core/util/PolicyManagerUtil.java | 55 +++++++- .../src/test/resources/sql/CreateH2TestDB.sql | 10 +- .../test/resources/sql/CreateMySqlTestDB.sql | 6 + .../src/main/resources/dbscripts/cdm/h2.sql | 10 +- .../main/resources/dbscripts/cdm/mssql.sql | 6 + .../main/resources/dbscripts/cdm/mysql.sql | 6 + .../main/resources/dbscripts/cdm/oracle.sql | 6 +- .../resources/dbscripts/cdm/postgresql.sql | 17 +++ 17 files changed, 248 insertions(+), 86 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java index 92e07aeab11..6c8358f2576 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/PolicyWrapper.java @@ -126,12 +126,6 @@ public class PolicyWrapper { @NotNull private String policyType; - @ApiModelProperty( - name = "correctiveActions", - value = "List of corrective actions to be applied when the policy is violated" - ) - private List correctiveActions; - public String getPolicyType() { return policyType; } @@ -219,12 +213,4 @@ public class PolicyWrapper { public void setDeviceGroups(List deviceGroups) { this.deviceGroups = deviceGroups; } - - public List getCorrectiveActions() { - return correctiveActions; - } - - public void setCorrectiveActions(List correctiveActions) { - this.correctiveActions = correctiveActions; - } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java index b32c7613f45..754caa201f6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/ProfileFeature.java @@ -21,8 +21,10 @@ package org.wso2.carbon.device.mgt.jaxrs.beans; import com.google.gson.Gson; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; import java.io.Serializable; +import java.util.List; @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " + "features") @@ -57,6 +59,10 @@ public class ProfileFeature implements Serializable { value = "The payload which is submitted to each feature", required = true) private String payLoad; + @ApiModelProperty(name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated", + required = true) + private List correctiveActions; public int getId() { return id; @@ -109,4 +115,12 @@ public class ProfileFeature implements Serializable { public void setContent(Object content) { this.content = content; } + + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions(List correctiveActions) { + this.correctiveActions = correctiveActions; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index a9fc47691eb..79d8d93e39b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -140,7 +140,6 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setPolicyName(policyWrapper.getPolicyName()); policy.setDescription(policyWrapper.getDescription()); policy.setProfile(DeviceMgtUtil.convertProfile(policyWrapper.getProfile())); - policy.setCorrectiveActions(policyWrapper.getCorrectiveActions()); policy.setOwnershipType(policyWrapper.getOwnershipType()); policy.setActive(policyWrapper.isActive()); policy.setRoles(policyWrapper.getRoles()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java index c11fd758c9f..31d1b38104e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtUtil.java @@ -60,6 +60,7 @@ public class DeviceMgtUtil { profileFeature.setDeviceType(mdmProfileFeature.getDeviceTypeId()); profileFeature.setFeatureCode(mdmProfileFeature.getFeatureCode()); profileFeature.setId(mdmProfileFeature.getId()); + profileFeature.setCorrectiveActions(mdmProfileFeature.getCorrectiveActions()); return profileFeature; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java index 452521286bb..f4585604ff4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/Policy.java @@ -201,13 +201,6 @@ public class Policy implements Comparable, Serializable { example = "GENERAL") 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 correctiveActions; - @XmlElement public int getId() { return id; @@ -379,16 +372,6 @@ public class Policy implements Comparable, Serializable { this.policyType = policyType; } - @XmlElement - public List getCorrectiveActions() { - return correctiveActions; - } - - public void setCorrectiveActions( - List correctiveActions) { - this.correctiveActions = correctiveActions; - } - @Override public int compareTo(Policy o) { if (this.priorityId == o.priorityId) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java index 638814b63d3..4d0bde90b6f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/ProfileFeature.java @@ -22,6 +22,7 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import java.io.Serializable; +import java.util.List; @ApiModel(value = "ProfileFeature", description = "This class carries all information related to profile " + "features") @@ -64,6 +65,11 @@ public class ProfileFeature implements Serializable { example = "{\\\"enabled\\\":false}") private Object content; + @ApiModelProperty(name = "correctiveActions", + value = "List of corrective actions to be applied when the policy is violated", + required = true) + private List correctiveActions; + public int getId() { return id; } @@ -103,4 +109,12 @@ public class ProfileFeature implements Serializable { public void setContent(Object content) { this.content = content; } + + public List getCorrectiveActions() { + return correctiveActions; + } + + public void setCorrectiveActions(List correctiveActions) { + this.correctiveActions = correctiveActions; + } } \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index ec2d89cd717..d33a6572828 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -77,7 +77,8 @@ public interface PolicyDAO { * @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 */ - void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) throws PolicyManagerDAOException; /** @@ -86,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 getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException; + List 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 @@ -94,7 +95,8 @@ public interface PolicyDAO { * @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 */ - void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) 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 * @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database */ - void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId, + int featureId) throws PolicyManagerDAOException; Policy updateUserOfPolicy(List usersToAdd, Policy policy) throws PolicyManagerDAOException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 766c512eb05..3ba4c61a529 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -263,19 +263,21 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void addCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void addCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CORRECTIVE_ACTION " + "(ACTION_TYPE, " + "CORRECTIVE_POLICY_ID, " + - "POLICY_ID) VALUES (?, ?, ?)"; + "POLICY_ID, FEATURE_ID) VALUES (?, ?, ?, ?)"; try (PreparedStatement insertStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { insertStmt.setString(1, correctiveAction.getActionType()); insertStmt.setInt(2, correctiveAction.getPolicyId()); insertStmt.setInt(3, policyId); + insertStmt.setInt(4, featureId); insertStmt.addBatch(); } insertStmt.executeBatch(); @@ -288,17 +290,18 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException { + public List getCorrectiveActionsOfPolicy(int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "SELECT " + "ACTION_TYPE, " + "CORRECTIVE_POLICY_ID " + "FROM DM_POLICY_CORRECTIVE_ACTION " + - "WHERE POLICY_ID = ?"; + "WHERE POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement selectStmt = conn.prepareStatement(query)) { List correctiveActions = new ArrayList<>(); selectStmt.setInt(1, policyId); + selectStmt.setInt(2, featureId); try (ResultSet rs = selectStmt.executeQuery()) { CorrectiveAction correctiveAction; while (rs.next()) { @@ -318,19 +321,21 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void updateCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " + "SET CORRECTIVE_POLICY_ID = ? " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ?"; + "AND POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement updateStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { updateStmt.setInt(1, correctiveAction.getPolicyId()); updateStmt.setString(2, correctiveAction.getActionType()); updateStmt.setInt(3, policyId); + updateStmt.setInt(4, featureId); updateStmt.addBatch(); } updateStmt.executeBatch(); @@ -343,17 +348,19 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public void deleteCorrectiveActionsOfPolicy(List correctiveActions, int policyId) + public void deleteCorrectiveActionsOfPolicy(List correctiveActions, + int policyId, int featureId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ?"; + "AND POLICY_ID = ? AND FEATURE_ID = ?"; try (PreparedStatement deleteStmt = conn.prepareStatement(query)) { for (CorrectiveAction correctiveAction : correctiveActions) { deleteStmt.setString(1, correctiveAction.getActionType()); deleteStmt.setInt(2, policyId); + deleteStmt.setInt(3, featureId); deleteStmt.addBatch(); } deleteStmt.executeBatch(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 1ac0f519d19..b1261daa110 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -35,6 +35,7 @@ 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.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -101,6 +102,18 @@ public class PolicyManagerImpl implements PolicyManager { profileDAO.addProfile(profile); featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); + List 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); @@ -140,13 +153,13 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { + /*if (policy.getCorrectiveActions() != null && !policy.getCorrectiveActions().isEmpty()) { if (log.isDebugEnabled()) { log.debug("Adding corrective actions for policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } policyDAO.addCorrectiveActionsOfPolicy(policy.getCorrectiveActions(), policy.getId()); - } + }*/ if (policy.isActive()) { policyDAO.activatePolicy(policy.getId()); @@ -189,6 +202,8 @@ public class PolicyManagerImpl implements PolicyManager { List featuresToDelete = new ArrayList<>(); List temp = new ArrayList<>(); List updateDFes = new ArrayList<>(); + Map> updatedCorrectiveActionsMap = new HashMap<>(); + Map> existingCorrectiveActionsMap = new HashMap<>(); List updatedFeatureList = policy.getProfile().getProfileFeaturesList(); @@ -196,7 +211,9 @@ public class PolicyManagerImpl implements PolicyManager { // Checks for the existing features for (ProfileFeature feature : updatedFeatureList) { + updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions()); for (ProfileFeature fe : existingProfileFeaturesList) { + existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { existingFeaturesList.add(feature); temp.add(feature.getFeatureCode()); @@ -272,23 +289,39 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } - List updatedCorrectiveActions = policy.getCorrectiveActions(); - List existingCorrectiveActions = previousPolicy.getCorrectiveActions(); - List correctiveActionsToUpdate = new ArrayList<>(); - List correctiveActionsToDelete = new ArrayList<>(); - List correctiveActionsToAdd = new ArrayList<>(); - List correctiveActionTypesToUpdate = new ArrayList<>(); - List existingCorrectiveActionTypes = new ArrayList<>(); - - if (updatedCorrectiveActions != null) { - for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + /*List updatedCorrectiveActions = policy.getCorrectiveActions(); + List existingCorrectiveActions = previousPolicy.getCorrectiveActions();*/ + + //Iterate all corrective actions in the new policy payload against it's features + for (Integer featureId : updatedCorrectiveActionsMap.keySet()) { + List correctiveActionsToUpdate = new ArrayList<>(); + List correctiveActionsToDelete = new ArrayList<>(); + List correctiveActionsToAdd = new ArrayList<>(); + + List correctiveActionTypesToUpdate = new ArrayList<>(); + List existingCorrectiveActionTypes = new ArrayList<>(); + List updatedCorrectiveActions = updatedCorrectiveActionsMap.get(featureId); + //Check this feature already have a corrective action + if (existingCorrectiveActionsMap.containsKey(featureId)) { + //Existing corrective actions of the selected feature + List existingCorrectiveActions = existingCorrectiveActionsMap.get(featureId); + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { + //If old action type is same as new action type, put them into + // updating list + correctiveActionsToUpdate.add(updatedCorrectiveAction); + existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + } + } + //Newly added action types added to this list + correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); + } for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { - if (updatedCorrectiveAction.getActionType().equals(existingCorrectiveAction.getActionType())) { - correctiveActionsToUpdate.add(updatedCorrectiveAction); - existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { + correctiveActionsToDelete.add(existingCorrectiveAction); } } - correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); } for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { @@ -296,31 +329,43 @@ public class PolicyManagerImpl implements PolicyManager { correctiveActionsToAdd.add(updatedCorrectiveAction); } } - } - for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { - if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { - correctiveActionsToDelete.add(existingCorrectiveAction); + if (log.isDebugEnabled()) { + log.debug("Updating corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); } - } - if (log.isDebugEnabled()) { - log.debug("Updating corrective actions for policy " + policy.getPolicyName() + - " having policy id " + policy.getId()); - } + if (!correctiveActionsToUpdate.isEmpty()) { + policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, + previousPolicy.getId(), featureId); + } - if (!correctiveActionsToUpdate.isEmpty()) { - policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, previousPolicy.getId()); - } + if (!correctiveActionsToAdd.isEmpty()) { + policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, + previousPolicy.getId(), featureId); + } - if (!correctiveActionsToAdd.isEmpty()) { - policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, previousPolicy.getId()); + if (!correctiveActionsToDelete.isEmpty()) { + policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, + previousPolicy.getId(), featureId); + } } - if (!correctiveActionsToDelete.isEmpty()) { - policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, previousPolicy.getId()); + + /*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(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -656,7 +701,6 @@ public class PolicyManagerImpl implements PolicyManager { log.debug("Retrieving corrective actions of policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } - policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId)); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + @@ -681,6 +725,10 @@ public class PolicyManagerImpl implements PolicyManager { // PolicyManagementDAOFactory.openConnection(); Profile profile = profileManager.getProfile(policy.getProfileId()); policy.setProfile(profile); + + for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) { + profileFeature.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policyId, profileFeature.getId())); + } } catch (ProfileManagementException e) { throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + 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); // } finally { // PolicyManagementDAOFactory.closeConnection(); + } catch (PolicyManagerDAOException e) { + throw new PolicyManagementException("Error occurred while getting the corrective " + + "actions related to policy ID (" + policyId + ")", e); } + return policy; } @@ -1278,6 +1330,10 @@ public class PolicyManagerImpl implements PolicyManager { 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())); + } } } policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); @@ -1293,7 +1349,6 @@ public class PolicyManagerImpl implements PolicyManager { log.debug("Retrieving corrective actions for policy " + policy.getPolicyName() + " having policy id " + policy.getId()); } - policy.setCorrectiveActions(policyDAO.getCorrectiveActionsOfPolicy(policy.getId())); } Collections.sort(policyList); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 714421c431f..4c089596e90 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -145,8 +145,7 @@ public class PolicyManagerUtil { 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 && + /*if (policy.getPolicyType() != null && PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType()) && policy.getCorrectiveActions() != null) { for (CorrectiveAction correctiveAction : policy.getCorrectiveActions()) { @@ -184,13 +183,63 @@ public class PolicyManagerUtil { break; } } - } + }*/ policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); + if (policy.getPolicyType() != null && + PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType())) { + setCorrectiveActions(effectiveFeatures, policyOperation, policy); + } policyOperation.setPayLoad(policyOperation.getProfileOperations()); return policyOperation; } + private static void setCorrectiveActions(List features, + PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { + List 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 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 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 createProfileOperations(List effectiveFeatures) { List profileOperations = new ArrayList<>(); for (ProfileFeature feature : effectiveFeatures) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql index ccf48c1b0cf..4658e2158f5 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateH2TestDB.sql @@ -205,10 +205,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) + 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 ); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index b25fce19f78..51b4a428f5c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -85,11 +85,17 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY_CORRECTIVE_ACTION` ( `ACTION_TYPE` VARCHAR(45) NOT NULL, `CORRECTIVE_POLICY_ID` INT(11) DEFAULT NULL, `POLICY_ID` INT(11) NOT NULL, + `FEATURE_ID` INT(11) NOT NULL, PRIMARY KEY (ID), 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 )ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index 623bd6b374e..370200ca96e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -199,10 +199,16 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) + 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 ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 058bceb53bd..1d41602fdd1 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -249,11 +249,17 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INTEGER DEFAULT NULL, POLICY_ID INTEGER NOT NULL, + FEATURE_ID INTEGER NOT NULL, PRIMARY KEY (ID), 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 ); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index f1244dd87ea..13bb74c5d8e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -233,11 +233,17 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR(45) NOT NULL, CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) NOT NULL, PRIMARY KEY (ID), 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 )ENGINE = InnoDB; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index 325bb770b36..ffb4892758a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -340,10 +340,14 @@ CREATE TABLE DM_POLICY_CORRECTIVE_ACTION ( ACTION_TYPE VARCHAR2(45) NOT NULL, CORRECTIVE_POLICY_ID NUMBER(10) DEFAULT NULL, POLICY_ID NUMBER(10) NOT NULL, + FEATURE_ID NUMBER(10) NOT NULL, CONSTRAINT PK_DM_POLICY_CORRECTIVE_ACTION PRIMARY KEY (ID), CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION 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) ) / diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index d6956255753..ebb7e55992b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -237,6 +237,23 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( 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 ( ID INTEGER NOT NULL DEFAULT NEXTVAL ('DM_DEVICE_POLICY_seq') ,