From a24879f551c9150750a038d143d4095a5b1ed2d7 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Fri, 24 Apr 2020 16:30:15 +0530 Subject: [PATCH] Add payload parsing for old type corrective actions --- .../device/mgt/jaxrs/beans/PolicyWrapper.java | 26 ++ .../mgt/jaxrs/beans/ProfileFeature.java | 3 +- .../impl/PolicyManagementServiceImpl.java | 2 + .../common/policy/mgt/CorrectiveAction.java | 16 +- .../device/mgt/common/policy/mgt/Policy.java | 27 ++ .../modules/business-controllers/policy.js | 5 +- .../units/cdmf.unit.policy.create/create.hbs | 3 +- .../public/js/policy-edit.js | 1 - .../carbon/policy/mgt/core/dao/PolicyDAO.java | 6 + .../mgt/core/dao/impl/PolicyDAOImpl.java | 111 +++-- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 424 +++++++++++++----- .../core/util/PolicyManagementConstants.java | 1 + .../mgt/core/util/PolicyManagerUtil.java | 90 +++- .../src/test/resources/sql/CreateH2TestDB.sql | 1 + .../test/resources/sql/CreateMySqlTestDB.sql | 8 +- .../src/main/resources/dbscripts/cdm/h2.sql | 8 +- .../main/resources/dbscripts/cdm/mssql.sql | 8 +- .../main/resources/dbscripts/cdm/mysql.sql | 8 +- .../main/resources/dbscripts/cdm/oracle.sql | 8 +- .../resources/dbscripts/cdm/postgresql.sql | 19 +- 20 files changed, 594 insertions(+), 181 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 6c8358f257..ca15114bac 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 @@ -49,6 +49,12 @@ import java.util.List; + "Wrappers") public class PolicyWrapper { + @ApiModelProperty( + name = "payloadVersion", + value = "Payload version of the Policy") + @Size(max = 45) + private String payloadVersion; + @ApiModelProperty( name = "policyName", value = "The name of the policy", @@ -126,6 +132,18 @@ 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 getPayloadVersion() { + return payloadVersion; + } + + public void setPayloadVersion(String payloadVersion) { + this.payloadVersion = payloadVersion; + } + public String getPolicyType() { return policyType; } @@ -213,4 +231,12 @@ 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 754caa201f..3ced3879a0 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 @@ -60,8 +60,7 @@ public class ProfileFeature implements Serializable { required = true) private String payLoad; @ApiModelProperty(name = "correctiveActions", - value = "List of corrective actions to be applied when the policy is violated", - required = true) + value = "List of corrective actions to be applied when the policy is violated") private List correctiveActions; public int getId() { 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 79d8d93e39..51a2408998 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 @@ -147,6 +147,8 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setCompliance(policyWrapper.getCompliance()); policy.setDeviceGroups(policyWrapper.getDeviceGroups()); policy.setPolicyType(policyWrapper.getPolicyType()); + policy.setPolicyPayloadVersion(policyWrapper.getPayloadVersion()); + policy.setCorrectiveActions(policyWrapper.getCorrectiveActions()); //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. List devices = new ArrayList(); List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java index 6ebe74e17c..022fc7d082 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/CorrectiveAction.java @@ -55,7 +55,9 @@ public class CorrectiveAction implements Serializable { ) private List operations; - private int featureId; + private Integer featureId; + + private Integer associatedGeneralPolicyId; public String getActionType() { return actionType; @@ -81,11 +83,19 @@ public class CorrectiveAction implements Serializable { this.operations = operations; } - public int getFeatureId() { + public Integer getFeatureId() { return featureId; } - public void setFeatureId(int featureId) { + public void setFeatureId(Integer featureId) { this.featureId = featureId; } + + public Integer getAssociatedGeneralPolicyId() { + return associatedGeneralPolicyId; + } + + public void setAssociatedGeneralPolicyId(Integer associatedGeneralPolicyId) { + this.associatedGeneralPolicyId = associatedGeneralPolicyId; + } } 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 f4585604ff..6b7dc48738 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 @@ -53,6 +53,12 @@ public class Policy implements Comparable, Serializable { private static final long serialVersionUID = 19981017L; + @ApiModelProperty( + name = "payloadVersion", + value = "Payload version of the Policy", + example = "1") + private String policyPayloadVersion; + @ApiModelProperty( name = "id", value = "The policy ID", @@ -201,6 +207,19 @@ 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") + private List correctiveActions; + + @XmlElement + public String getPolicyPayloadVersion() { + return policyPayloadVersion; + } + + public void setPolicyPayloadVersion(String policyPayloadVersion) { + this.policyPayloadVersion = policyPayloadVersion; + } + @XmlElement public int getId() { return id; @@ -372,6 +391,14 @@ public class Policy implements Comparable, Serializable { this.policyType = policyType; } + 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.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js index 8c881ef3df..95bf9c1d22 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/business-controllers/policy.js @@ -63,7 +63,10 @@ policyModule = function () { policyObjectToView["name"] = policyObjectFromRestEndpoint["policyName"]; policyObjectToView["platform"] = policyObjectFromRestEndpoint["profile"]["deviceType"]; policyObjectFromRestEndpoint["policyType"] = policyListFromRestEndpoint["policyType"]; - policyObjectFromRestEndpoint["correctiveActions"] = policyListFromRestEndpoint["correctiveActions"]; + var payloadVersion = policyObjectFromRestEndpoint["policyPayloadVersion"]; + if (!parseFloat(payloadVersion) >= 2.0) { + policyObjectFromRestEndpoint["correctiveActions"] = policyListFromRestEndpoint["correctiveActions"]; + } if (policyObjectToView["platform"] == "ios") { policyObjectToView["deviceTypeIcon"] = "apple"; } else { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs index 54e89548f5..fab22e06d3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.create/create.hbs @@ -2,8 +2,7 @@ {{#if isAuthorized}}
diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/public/js/policy-edit.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/public/js/policy-edit.js index 55fdb04c61..d8d5812b1c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/public/js/policy-edit.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/units/cdmf.unit.policy.edit/public/js/policy-edit.js @@ -161,7 +161,6 @@ skipStep["policy-platform"] = function (policyPayloadObj) { currentlyEffected["users"] = policyPayloadObj.users; currentlyEffected["groups"] = []; currentlyEffected["policyType"] = policyPayloadObj.policyType; - currentlyEffected["correctiveActions"] = policyPayloadObj.correctiveActions; if (policyPayloadObj.deviceGroups) { var deviceGroups = policyPayloadObj.deviceGroups; 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 2261a1c033..20b53f81a9 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 @@ -109,6 +109,12 @@ public interface PolicyDAO { int featureId) throws PolicyManagerDAOException; + /** + * This method is used get all corrective actions from DB + * @throws PolicyManagerDAOException is thrown when there is an error in deleting corrective actions to database + */ + List getAllCorrectiveActions() throws PolicyManagerDAOException; + Policy updateUserOfPolicy(List usersToAdd, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List devices, 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 2446fdbca4..32c5464d93 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 @@ -40,10 +40,10 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.policy.mgt.CorrectiveAction; -import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.device.mgt.common.policy.mgt.DeviceGroupWrapper; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyCriterion; +import org.wso2.carbon.policy.mgt.common.Criterion; import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; @@ -54,8 +54,17 @@ import org.wso2.carbon.policy.mgt.core.util.SetReferenceTransformer; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; -import java.sql.*; -import java.util.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.sql.Types; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.List; +import java.util.Properties; public class PolicyDAOImpl implements PolicyDAO { @@ -277,7 +286,11 @@ public class PolicyDAOImpl implements PolicyDAO { insertStmt.setString(1, correctiveAction.getActionType()); insertStmt.setInt(2, correctiveAction.getPolicyId()); insertStmt.setInt(3, policyId); - insertStmt.setInt(4, featureId); + if (featureId == -1) { + insertStmt.setNull(4, Types.INTEGER); + } else { + insertStmt.setInt(4, featureId); + } insertStmt.addBatch(); } insertStmt.executeBatch(); @@ -293,24 +306,14 @@ public class PolicyDAOImpl implements PolicyDAO { public List getCorrectiveActionsOfPolicy(int policyId) throws PolicyManagerDAOException { try { Connection conn = this.getConnection(); - String query = "SELECT " + - "ACTION_TYPE, " + - "CORRECTIVE_POLICY_ID, " + - "FEATURE_ID "+ - "FROM DM_POLICY_CORRECTIVE_ACTION " + - "WHERE POLICY_ID = ?"; + String query = "SELECT * " + + "FROM DM_POLICY_CORRECTIVE_ACTION " + + "WHERE POLICY_ID = ?"; try (PreparedStatement selectStmt = conn.prepareStatement(query)) { List correctiveActions = new ArrayList<>(); selectStmt.setInt(1, policyId); 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); - } + extractCorrectivePolicies(selectStmt, correctiveActions); } return correctiveActions; } @@ -321,22 +324,62 @@ public class PolicyDAOImpl implements PolicyDAO { } } + @Override + public List getAllCorrectiveActions() throws PolicyManagerDAOException { + try { + Connection conn = this.getConnection(); + String query = "SELECT * " + + "FROM DM_POLICY_CORRECTIVE_ACTION "; + try (PreparedStatement stmt = conn.prepareStatement(query)) { + List correctiveActions = new ArrayList<>(); + extractCorrectivePolicies(stmt, correctiveActions); + return correctiveActions; + } + } catch (SQLException e) { + String msg = "Error occurred while retrieving all corrective actions"; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + private void extractCorrectivePolicies(PreparedStatement stmt, List + correctiveActions) throws SQLException { + try (ResultSet rs = stmt.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")); + correctiveAction.setAssociatedGeneralPolicyId(rs.getInt("POLICY_ID")); + correctiveActions.add(correctiveAction); + } + } + } + @Override public void updateCorrectiveActionsOfPolicy(List correctiveActions, int policyId, int featureId) throws PolicyManagerDAOException { try { + boolean isFeatureIdContains = false; Connection conn = this.getConnection(); String query = "UPDATE DM_POLICY_CORRECTIVE_ACTION " + "SET CORRECTIVE_POLICY_ID = ? " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ? AND FEATURE_ID = ?"; + "AND POLICY_ID = ? "; + if (featureId != -1) { + isFeatureIdContains = true; + query = query.concat("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); + if (isFeatureIdContains) { + updateStmt.setInt(4, featureId); + } updateStmt.addBatch(); } updateStmt.executeBatch(); @@ -356,12 +399,19 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn = this.getConnection(); String query = "DELETE FROM DM_POLICY_CORRECTIVE_ACTION " + "WHERE ACTION_TYPE = ? " + - "AND POLICY_ID = ? AND FEATURE_ID = ?"; + "AND POLICY_ID = ? "; + boolean isFeatueIdContains = false; + if (featureId != -1) { + isFeatueIdContains = true; + query = query.concat("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); + if (isFeatueIdContains) { + deleteStmt.setInt(3, featureId); + } deleteStmt.addBatch(); } deleteStmt.executeBatch(); @@ -929,7 +979,8 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "UPDATE DM_POLICY SET NAME = ?, PROFILE_ID = ?, PRIORITY = ?, COMPLIANCE = ?," + - " UPDATED = ?, DESCRIPTION = ?, OWNERSHIP_TYPE = ?, POLICY_TYPE = ? WHERE ID = ? AND TENANT_ID = ?"; + " UPDATED = ?, DESCRIPTION = ?, OWNERSHIP_TYPE = ?, POLICY_TYPE = ?, " + + "PAYLOAD_VERSION = ? WHERE ID = ? AND TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getProfile().getProfileId()); @@ -939,8 +990,9 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setString(6, policy.getDescription()); stmt.setString(7, policy.getOwnershipType()); stmt.setString(8, policy.getPolicyType()); - stmt.setInt(9, policy.getId()); - stmt.setInt(10, tenantId); + stmt.setString(9, policy.getPolicyPayloadVersion()); + stmt.setInt(10, policy.getId()); + stmt.setInt(11, tenantId); stmt.executeUpdate(); } catch (SQLException e) { @@ -1049,6 +1101,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setPolicyType(resultSet.getString("POLICY_TYPE")); policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); + policy.setPolicyPayloadVersion(resultSet.getString("PAYLOAD_VERSION")); } return policy; @@ -1560,8 +1613,10 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," + - "UPDATED, ACTIVE, DESCRIPTION, POLICY_TYPE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, " + + "PRIORITY, COMPLIANCE, OWNERSHIP_TYPE, " + + "UPDATED, ACTIVE, DESCRIPTION, POLICY_TYPE, PAYLOAD_VERSION) " + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query, new String[]{"id"}); stmt.setString(1, policy.getPolicyName()); @@ -1574,6 +1629,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setInt(8, 0); stmt.setString(9, policy.getDescription()); stmt.setString(10, policy.getPolicyType()); + stmt.setString(11, policy.getPolicyPayloadVersion()); int affectedRows = stmt.executeUpdate(); @@ -1878,6 +1934,7 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); policy.setDescription(resultSet.getString("DESCRIPTION")); policy.setPolicyType(resultSet.getString("POLICY_TYPE")); + policy.setPolicyPayloadVersion(resultSet.getString("PAYLOAD_VERSION")); policies.add(policy); } return policies; 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 eec8e38eff..e0d8df80d3 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,7 +35,7 @@ package org.wso2.carbon.policy.mgt.core.mgt.impl; -import org.apache.commons.collections.map.HashedMap; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; @@ -58,18 +58,32 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; -import org.wso2.carbon.policy.mgt.common.*; +import org.wso2.carbon.policy.mgt.common.Criterion; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.common.ProfileManagementException; import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; -import org.wso2.carbon.policy.mgt.core.dao.*; +import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO; +import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; +import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; +import org.wso2.carbon.policy.mgt.core.dao.ProfileDAO; +import org.wso2.carbon.policy.mgt.core.dao.ProfileManagerDAOException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import org.wso2.carbon.policy.mgt.core.mgt.bean.UpdatedPolicyDeviceListBean; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.sql.SQLException; import java.sql.Timestamp; -import java.util.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class PolicyManagerImpl implements PolicyManager { @@ -102,6 +116,7 @@ public class PolicyManagerImpl implements PolicyManager { profileDAO.addProfile(profile); featureDAO.addProfileFeatures(profile.getProfileFeaturesList(), profile.getProfileId()); } + policy.setPolicyPayloadVersion("2.0"); policy = policyDAO.addPolicy(policy); if (policy.getProfile() != null) { Profile profile = policy.getProfile(); @@ -213,13 +228,7 @@ public class PolicyManagerImpl implements PolicyManager { // Checks for the existing features for (ProfileFeature feature : updatedFeatureList) { - if (feature.getCorrectiveActions() != null) { - updatedCorrectiveActionsMap.put(feature.getId(), feature.getCorrectiveActions()); - } for (ProfileFeature fe : existingProfileFeaturesList) { - if (fe.getCorrectiveActions() != null) { - existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); - } if (feature.getFeatureCode().equalsIgnoreCase(fe.getFeatureCode())) { existingFeaturesList.add(feature); temp.add(feature.getFeatureCode()); @@ -248,7 +257,8 @@ public class PolicyManagerImpl implements PolicyManager { Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); policy.getProfile().setUpdatedDate(currentTimestamp); policy.setPriorityId(previousPolicy.getPriorityId()); - policyDAO.updatePolicy(policy); + policy.setPolicyPayloadVersion(previousPolicy.getPolicyPayloadVersion()); + Policy updatedPolicy = policyDAO.updatePolicy(policy); profileDAO.updateProfile(policy.getProfile()); featureDAO.updateProfileFeatures(existingFeaturesList, profileId); @@ -294,84 +304,40 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteria(policy); policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + String policyPayloadVersion = previousPolicy.getPolicyPayloadVersion(); + float payloadVersion = 0f; + if (policyPayloadVersion != null && !StringUtils.isEmpty(policyPayloadVersion)) { + payloadVersion = Float.parseFloat(policyPayloadVersion); + } - /*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 (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { - correctiveActionsToDelete.add(existingCorrectiveAction); - } - } - } - - for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { - if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) { - correctiveActionsToAdd.add(updatedCorrectiveAction); + List updatedFeatures = policy.getProfile().getProfileFeaturesList(); + List features = featureDAO.getFeaturesForProfile(profileId); + for (ProfileFeature updatedFeature : updatedFeatures) { + for (ProfileFeature feature : features) { + if (updatedFeature.getFeatureCode().equals(feature.getFeatureCode())) { + updatedFeature.setId(feature.getId()); + break; } } - 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 (!correctiveActionsToAdd.isEmpty()) { - policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, - previousPolicy.getId(), featureId); - } - - if (!correctiveActionsToDelete.isEmpty()) { - policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, - previousPolicy.getId(), featureId); + if (updatedFeature.getCorrectiveActions() != null) { + updatedCorrectiveActionsMap.put(updatedFeature.getId(), + updatedFeature.getCorrectiveActions()); } } - - /*for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { - if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction.getActionType())) { - correctiveActionsToAdd.add(updatedCorrectiveAction); + for (ProfileFeature fe : existingProfileFeaturesList) { + if (fe.getCorrectiveActions() != null && !fe.getCorrectiveActions().isEmpty()) { + existingCorrectiveActionsMap.put(fe.getId(), fe.getCorrectiveActions()); } } - for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { - if (!correctiveActionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { - correctiveActionsToDelete.add(existingCorrectiveAction); - } - }*/ - - - + if (payloadVersion >= 2.0f) { + updateMultipleCorrectiveActions(updatedCorrectiveActionsMap, + existingCorrectiveActionsMap, policy, previousPolicy); + } else { + updateSingleCorrectiveActionList(policy, previousPolicy); + } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); @@ -392,6 +358,218 @@ public class PolicyManagerImpl implements PolicyManager { return policy; } + private void updateSingleCorrectiveActionList(Policy policy, Policy previousPolicy) + throws PolicyManagerDAOException { + 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) { + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (updatedCorrectiveAction.getActionType() + .equals(existingCorrectiveAction.getActionType())) { + correctiveActionsToUpdate.add(updatedCorrectiveAction); + existingCorrectiveActionTypes.add(updatedCorrectiveAction.getActionType()); + } + } + correctiveActionTypesToUpdate.add(updatedCorrectiveAction.getActionType()); + } + + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + if (!existingCorrectiveActionTypes.contains(updatedCorrectiveAction + .getActionType())) { + 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 (!correctiveActionsToUpdate.isEmpty()) { + try { + policyDAO.updateCorrectiveActionsOfPolicy(correctiveActionsToUpdate, + previousPolicy.getId(), -1); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while updating corrective policies of the policy" + + " "+ previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + if (!correctiveActionsToAdd.isEmpty()) { + try { + policyDAO.addCorrectiveActionsOfPolicy(correctiveActionsToAdd, + previousPolicy.getId(), -1); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while adding new corrective policies to the " + + "policy the policy " + previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + if (!correctiveActionsToDelete.isEmpty()) { + try { + policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActionsToDelete, + previousPolicy.getId(), -1); + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while deleting corrective actions from the " + + "policy " + previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + } + + private void updateMultipleCorrectiveActions( + Map> updatedCorrectiveActionsMap, + Map> existingCorrectiveActionsMap, + Policy policy, Policy previousPolicy) throws PolicyManagerDAOException { + + Map> correctiveActionsToUpdate = new HashMap<>(); + Map> existingCorrectiveActionTypes = new HashMap<>(); + Map> correctiveActionTypesToUpdate = new HashMap<>(); + Map> correctiveActionsToAdd = new HashMap<>(); + Map> correctiveActionsToDelete = new HashMap<>(); + + for (Integer featureId : updatedCorrectiveActionsMap.keySet()) { + List correctiveActionListToUpdate = new ArrayList<>(); + List updatedCorrectiveActions = updatedCorrectiveActionsMap + .get(featureId); + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + List existingCorrectiveActions = existingCorrectiveActionsMap + .get(featureId); + if (existingCorrectiveActions != null) { + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (existingCorrectiveAction.getActionType().equals(updatedCorrectiveAction + .getActionType())) { + correctiveActionListToUpdate.add(updatedCorrectiveAction); + List existingTypes = existingCorrectiveActionTypes + .get(featureId); + if (existingTypes == null) { + existingTypes = new ArrayList<>(); + } + existingTypes.add(updatedCorrectiveAction.getActionType()); + existingCorrectiveActionTypes.put(featureId, existingTypes); + } + } + } + + List toUpdateTypes = correctiveActionTypesToUpdate.get(featureId); + if (toUpdateTypes == null) { + toUpdateTypes = new ArrayList<>(); + } + toUpdateTypes.add(updatedCorrectiveAction.getActionType()); + correctiveActionTypesToUpdate.put(featureId, toUpdateTypes); + } + if (!correctiveActionListToUpdate.isEmpty()) { + correctiveActionsToUpdate.put(featureId, correctiveActionListToUpdate); + } + + List existingTypes = existingCorrectiveActionTypes.get(featureId); + for (CorrectiveAction updatedCorrectiveAction : updatedCorrectiveActions) { + if (existingTypes == null || !existingTypes.contains(updatedCorrectiveAction + .getActionType())) { + List correctiveActions = correctiveActionsToAdd + .get(featureId); + if (correctiveActions == null) { + correctiveActions = new ArrayList<>(); + } + correctiveActions.add(updatedCorrectiveAction); + correctiveActionsToAdd.put(featureId, correctiveActions); + } + } + } + + + for (Integer featureId : existingCorrectiveActionsMap.keySet()) { + List existingCorrectiveActions = existingCorrectiveActionsMap + .get(featureId); + List actionTypesToUpdate = correctiveActionTypesToUpdate.get(featureId); + for (CorrectiveAction existingCorrectiveAction : existingCorrectiveActions) { + if (actionTypesToUpdate == null || + !actionTypesToUpdate.contains(existingCorrectiveAction.getActionType())) { + List correctiveActionListToDelete = correctiveActionsToDelete + .get(featureId); + if (correctiveActionListToDelete == null) { + correctiveActionListToDelete = new ArrayList<>(); + } + correctiveActionListToDelete.add(existingCorrectiveAction); + correctiveActionsToDelete.put(featureId, correctiveActionListToDelete); + } + } + } + + if (log.isDebugEnabled()) { + log.debug("Updating corrective actions for policy " + policy.getPolicyName() + + " having policy id " + policy.getId()); + } + + if (!correctiveActionsToUpdate.isEmpty()) { + try { + for (Integer featureId : correctiveActionsToUpdate.keySet()) { + List correctiveActions = correctiveActionsToUpdate + .get(featureId); + policyDAO.updateCorrectiveActionsOfPolicy(correctiveActions, + previousPolicy.getId(), featureId); + } + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while updating corrective policies of the policy" + + " "+ previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + if (!correctiveActionsToAdd.isEmpty()) { + try { + for (Integer featureId : correctiveActionsToAdd.keySet()) { + List correctiveActions = correctiveActionsToAdd + .get(featureId); + policyDAO.addCorrectiveActionsOfPolicy(correctiveActions, + previousPolicy.getId(), featureId); + } + + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while adding new corrective policies to the " + + "policy the policy " + previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + + if (!correctiveActionsToDelete.isEmpty()) { + try { + for (Integer featureId : correctiveActionsToDelete.keySet()) { + List correctiveActions = correctiveActionsToDelete + .get(featureId); + policyDAO.deleteCorrectiveActionsOfPolicy(correctiveActions, + previousPolicy.getId(), featureId); + } + } catch (PolicyManagerDAOException e) { + String msg = "Error occurred while deleting corrective actions from the " + + "policy " + previousPolicy.getId(); + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } + } + } + @Override public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { boolean bool; @@ -733,14 +911,16 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.openConnection(); List correctiveActionsOfPolicy = policyDAO .getCorrectiveActionsOfPolicy(policyId); - for (ProfileFeature profileFeature : policy.getProfile().getProfileFeaturesList()) { - List correctiveActions = new ArrayList<>(); - for (CorrectiveAction correctiveAction : correctiveActionsOfPolicy) { - if (correctiveAction.getFeatureId() == profileFeature.getId()) { - correctiveActions.add(correctiveAction); - } - } - profileFeature.setCorrectiveActions(correctiveActions); + String policyPayloadVersion = policy.getPolicyPayloadVersion(); + float payloadVersion = 0f; + if (policyPayloadVersion != null && !StringUtils.isEmpty(policyPayloadVersion)) { + payloadVersion = Float.parseFloat(policyPayloadVersion); + } + if (payloadVersion >= 2.0f) { + setMultipleCorrectiveActions(correctiveActionsOfPolicy, policy.getProfile()); + } else { + policy.setCorrectiveActions(getSingleCorrectiveAction + (correctiveActionsOfPolicy, policyId)); } } catch (ProfileManagementException e) { throw new PolicyManagementException("Error occurred while getting the profile related to policy ID (" + @@ -750,10 +930,14 @@ public class PolicyManagerImpl implements PolicyManager { // } finally { // PolicyManagementDAOFactory.closeConnection(); } catch (PolicyManagerDAOException e) { - throw new PolicyManagementException("Error occurred while getting the corrective " + - "actions related to policy ID (" + policyId + ")", e); + String msg = "Error occurred while getting the corrective actions related to policy " + + "ID (" + policyId + ")"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); } catch (SQLException e) { - e.printStackTrace(); + String msg = "Error occurred while opening DB connection"; + log.error(msg, e); + throw new PolicyManagementException(msg, e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -1342,27 +1526,32 @@ public class PolicyManagerImpl implements PolicyManager { } private void buildPolicyList(List policyList, List profileList) - throws PolicyManagerDAOException, GroupManagementException { + throws PolicyManagerDAOException, GroupManagementException, PolicyManagementException { + List allCorrectiveActions = policyDAO.getAllCorrectiveActions(); for (Policy policy : policyList) { - List correctiveActionsOfPolicy = policyDAO - .getCorrectiveActionsOfPolicy(policy.getId()); + String policyPayloadVersion = policy.getPolicyPayloadVersion(); + float payloadVersion = 0f; + if (policyPayloadVersion != null && + !StringUtils.isEmpty(policyPayloadVersion)) { + payloadVersion = Float.parseFloat(policyPayloadVersion); + } for (Profile profile : profileList) { if (policy.getProfileId() == profile.getProfileId()) { policy.setProfile(profile); - for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) { - List correctiveActionList = new ArrayList<>(); - for (CorrectiveAction correctiveAction : correctiveActionsOfPolicy) { - if (profileFeature.getId() == correctiveAction.getFeatureId()) { - correctiveActionList.add(correctiveAction); - } - } - profileFeature.setCorrectiveActions(correctiveActionList); + if (payloadVersion >= 2.0f && PolicyManagementConstants.GENERAL_POLICY_TYPE + .equals(policy.getPolicyType())) { + setMultipleCorrectiveActions(allCorrectiveActions, profile); } } } policy.setRoles(policyDAO.getPolicyAppliedRoles(policy.getId())); policy.setUsers(policyDAO.getPolicyAppliedUsers(policy.getId())); policy.setPolicyCriterias(policyDAO.getPolicyCriteria(policy.getId())); + if (payloadVersion < 2.0f && PolicyManagementConstants.GENERAL_POLICY_TYPE + .equals(policy.getPolicyType())) { + policy.setCorrectiveActions + (getSingleCorrectiveAction(allCorrectiveActions, policy.getId())); + } List deviceGroupWrappers = policyDAO.getDeviceGroupsOfPolicy(policy.getId()); if (!deviceGroupWrappers.isEmpty()) { @@ -1376,4 +1565,37 @@ public class PolicyManagerImpl implements PolicyManager { } Collections.sort(policyList); } + + private List getSingleCorrectiveAction + (List allCorrectiveActions, int policyId) { + List correctiveActionsOfPolicy = new ArrayList<>(); + for (CorrectiveAction correctiveAction : allCorrectiveActions) { + if (correctiveAction.getAssociatedGeneralPolicyId() != null && + correctiveAction.getAssociatedGeneralPolicyId() == policyId) { + clearMetaDataValues(correctiveAction); + correctiveActionsOfPolicy.add(correctiveAction); + } + } + return correctiveActionsOfPolicy; + } + + private void setMultipleCorrectiveActions(List allCorrectiveActions, + Profile profile) { + for (ProfileFeature profileFeature : profile.getProfileFeaturesList()) { + List correctiveActionList = new ArrayList<>(); + for (CorrectiveAction correctiveAction : allCorrectiveActions) { + if (correctiveAction.getFeatureId() != null && + profileFeature.getId() == correctiveAction.getFeatureId()) { + clearMetaDataValues(correctiveAction); + correctiveActionList.add(correctiveAction); + } + } + profileFeature.setCorrectiveActions(correctiveActionList); + } + } + + private void clearMetaDataValues(CorrectiveAction correctiveAction) { + correctiveAction.setAssociatedGeneralPolicyId(null); //avoiding send in payload + correctiveAction.setFeatureId(null); //avoiding send in payload + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java index 535d3f7ffd..fb97d097f9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagementConstants.java @@ -67,6 +67,7 @@ public final class PolicyManagementConstants { public static final String POLICY_CORRECTIVE_ACTION_TYPE = "POLICY"; public static final String POLICY_FEATURE_CODE = "POLICY_ACTION"; public static final String POLICY_ACTIONS = "POLICY_ACTIONS"; + public static final String CORRECTIVE_POLICY_FEATURE_CODE = "CORRECTIVE_POLICY"; /** Caller would reference the constants using PolicyManagementConstants.DEVICE_CONFIG_XML_NAME, 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 0e0fe1ae59..9d45e8e7b8 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 @@ -35,6 +35,8 @@ package org.wso2.carbon.policy.mgt.core.util; +import com.google.gson.Gson; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; @@ -146,13 +148,81 @@ public class PolicyManagerUtil { policyOperation.setProfileOperations(createProfileOperations(effectiveFeatures)); if (policy.getPolicyType() != null && PolicyManagementConstants.GENERAL_POLICY_TYPE.equals(policy.getPolicyType())) { - setCorrectiveActions(effectiveFeatures, policyOperation, policy); + String policyPayloadVersion = policy.getPolicyPayloadVersion(); + float payloadVersion = 0f; + if (policyPayloadVersion != null && !StringUtils.isEmpty(policyPayloadVersion)) { + payloadVersion = Float.parseFloat(policyPayloadVersion); + } + if (payloadVersion >= 2.0f) { + setMultipleCorrectiveActions(effectiveFeatures, policyOperation, policy); + } else { + setSingleCorrectiveAction(policy, effectiveFeatures); + } } policyOperation.setPayLoad(policyOperation.getProfileOperations()); return policyOperation; } - private static void setCorrectiveActions(List features, + /** + * This method is used for generate single corrective action set for a single policy which is + * bind to the policy payload + * @param policy regarding policy object + * @param effectiveFeatures effective feature list + * @throws PolicyTransformException when transforming of the policy have issues + */ + private static void setSingleCorrectiveAction(Policy policy, List + effectiveFeatures) throws PolicyTransformException { + if (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 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); + } + break; + } + } + } + } + + /** + * This method is use for generate multiple corrective actions per policy which is attached + * to the feature of the policy + * @param features regarding feature list of the policy + * @param policyOperation operation list which to be sent to the device + * @param policy regarding policy + * @throws PolicyTransformException + */ + private static void setMultipleCorrectiveActions(List features, PolicyOperation policyOperation, Policy policy) throws PolicyTransformException { ProfileOperation correctiveProfileOperation = new ProfileOperation(); @@ -190,6 +260,12 @@ public class PolicyManagerUtil { } } + /** + * This method is using for generate profile operations list which to be sent to the device. + * this method is only using multiple corrective actions + * @param correctivePolicy regarding corrective policy + * @param correctiveOperationList regarding operations list of the corrective policy + */ private static void createCorrectiveProfileOperations(Policy correctivePolicy, ProfileOperation correctiveOperationList) { ProfileOperation profileOperation = new ProfileOperation(); @@ -222,11 +298,13 @@ public class PolicyManagerUtil { 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<>()); + if (feature.getCorrectiveActions() != null) { + for (CorrectiveAction correctiveAction : feature.getCorrectiveActions()) { + if (profileOperation.getCorrectiveActionIds() == null) { + profileOperation.setCorrectiveActionIds(new ArrayList<>()); + } + profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId()); } - profileOperation.getCorrectiveActionIds().add(correctiveAction.getPolicyId()); } profileOperations.add(profileOperation); } 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 4658e2158f..102cf838a8 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 @@ -182,6 +182,7 @@ DROP TABLE IF EXISTS DM_POLICY; CREATE TABLE IF NOT EXISTS DM_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , NAME VARCHAR(45) DEFAULT NULL , + PAYLOAD_VERSION VARCHAR (45) DEFAULT NULL, DESCRIPTION VARCHAR(1000) NULL, TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , 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 3d659d099f..a0f5b55ab3 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 @@ -63,6 +63,7 @@ DROP TABLE IF EXISTS `WSO2CDM`.`DM_POLICY` ; CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( `ID` INT(11) NOT NULL AUTO_INCREMENT, `NAME` VARCHAR(45) NULL DEFAULT NULL, + `PAYLOAD_VERSION` VARCHAR(45) NULL DEFAULT NULL, `TENANT_ID` INT(11) NOT NULL, `PROFILE_ID` INT(11) NOT NULL, `COMPLIANCE` VARCHAR(100) NULL, @@ -212,17 +213,12 @@ 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, + `FEATURE_ID` INT(11) DEFAULT 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_FEATURE - 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 0a8f646fdf..f69ea8b0f6 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 @@ -178,6 +178,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , NAME VARCHAR(45) DEFAULT NULL , DESCRIPTION VARCHAR(1000) NULL, + PAYLOAD_VERSION VARCHAR (45) NULL, TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , OWNERSHIP_TYPE VARCHAR(45) NULL, @@ -245,17 +246,12 @@ 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, + FEATURE_ID INT(11) DEFAULT 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_FEATURE - 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 3b4f65e50e..ff6e2c7500 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 @@ -229,6 +229,7 @@ IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[D CREATE TABLE DM_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(45) DEFAULT NULL , + PAYLOAD_VERSION VARCHAR (45) DEFAULT NULL, DESCRIPTION VARCHAR(1000) NULL, TENANT_ID INTEGER NOT NULL , PROFILE_ID INTEGER NOT NULL , @@ -286,17 +287,12 @@ 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, + FEATURE_ID INTEGER DEFAULT 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_FEATURE - 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 0ce1b6353b..9f11167c76 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 @@ -212,6 +212,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY ( ID INT(11) NOT NULL AUTO_INCREMENT , NAME VARCHAR(45) DEFAULT NULL , DESCRIPTION VARCHAR(1000) NULL, + PAYLOAD_VERSION VARCHAR (45) DEFAULT NULL, TENANT_ID INT(11) NOT NULL , PROFILE_ID INT(11) NOT NULL , OWNERSHIP_TYPE VARCHAR(45) NULL, @@ -282,17 +283,12 @@ 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, + FEATURE_ID INT(11) DEFAULT 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_FEATURE - 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 04fd233713..e787703082 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 @@ -320,6 +320,7 @@ CREATE TABLE DM_POLICY ( ID NUMBER(10) NOT NULL , NAME VARCHAR2(45) DEFAULT NULL , DESCRIPTION VARCHAR2(1000) NULL, + PAYLOAD_VERSION VARCHAR(45) DEFAULT NULL, TENANT_ID NUMBER(10) NOT NULL , PROFILE_ID NUMBER(10) NOT NULL , OWNERSHIP_TYPE VARCHAR2(45) NULL, @@ -411,14 +412,11 @@ 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, + FEATURE_ID NUMBER(10) DEFAULT 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), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION_FEATURE - FOREIGN KEY (FEATURE_ID) - REFERENCES DM_PROFILE_FEATURES (ID) + REFERENCES DM_POLICY (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 ebb7e55992..452eb34793 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 @@ -219,15 +219,16 @@ CREATE SEQUENCE DM_POLICY_seq; CREATE TABLE IF NOT EXISTS DM_POLICY ( ID INTEGER NOT NULL DEFAULT NEXTVAL ('DM_POLICY_seq') , NAME VARCHAR(45) DEFAULT NULL , - DESCRIPTION VARCHAR(1000) NULL, - TENANT_ID INTEGER NOT NULL , - PROFILE_ID INTEGER NOT NULL , - OWNERSHIP_TYPE VARCHAR(45) NULL, - COMPLIANCE VARCHAR(100) NULL, - PRIORITY INTEGER NOT NULL, - ACTIVE INTEGER NOT NULL, - UPDATED INTEGER NULL, - PRIMARY KEY (ID) , + PAYLOAD_VERSION VARCHAR(45) DEFAULT NULL, + DESCRIPTION VARCHAR(1000) NULL, + TENANT_ID INTEGER NOT NULL , + PROFILE_ID INTEGER NOT NULL , + OWNERSHIP_TYPE VARCHAR(45) NULL, + COMPLIANCE VARCHAR(100) NULL, + PRIORITY INTEGER NOT NULL, + ACTIVE INTEGER NOT NULL, + UPDATED INTEGER NULL, + PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) REFERENCES DM_PROFILE (ID )