diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java index 8c09357f0c9..259a1e19bca 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Profile.java @@ -24,10 +24,11 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import java.io.Serializable; import java.sql.Timestamp; import java.util.List; @XmlRootElement -public class Profile { +public class Profile implements Serializable { private int profileId; private String profileName; 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 92940de313a..6d9bee3b45d 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 @@ -89,12 +89,12 @@ public interface PolicyDAO { List getPolicyAppliedUsers(int policyId) throws PolicyManagerDAOException; - void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException; void setPolicyApplied(int deviceId) throws PolicyManagerDAOException; - void updateEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId) 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 586c9b31c1d..5ef728fba0c 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 @@ -749,7 +749,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void addEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + public void addEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; @@ -761,8 +761,8 @@ public class PolicyDAOImpl implements PolicyDAO { "(DEVICE_ID, POLICY_ID, POLICY_CONTENT, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(query); stmt.setInt(1, deviceId); - stmt.setInt(2, policyId); - stmt.setObject(3, profileFeatures); + stmt.setInt(2, policy.getId()); + stmt.setObject(3, policy); stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); @@ -805,7 +805,7 @@ public class PolicyDAOImpl implements PolicyDAO { @Override - public void updateEffectivePolicyToDevice(int deviceId, int policyId, List profileFeatures) + public void updateEffectivePolicyToDevice(int deviceId, Policy policy) throws PolicyManagerDAOException { Connection conn; @@ -816,8 +816,8 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + "APPLIED = ? WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(query); - stmt.setInt(1, policyId); - stmt.setObject(2, profileFeatures); + stmt.setInt(1, policy.getId()); + stmt.setObject(2, policy); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java index af03b36d566..dd4519eb5b6 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java @@ -26,6 +26,7 @@ import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; +import java.util.HashMap; import java.util.Map; public class PolicyManagementDataHolder { @@ -35,7 +36,7 @@ public class PolicyManagementDataHolder { private PolicyEvaluationPoint policyEvaluationPoint; private PolicyInformationPoint policyInformationPoint; private DeviceManagementProviderService deviceManagementService; - private Map policyMonitoringServiceMap; + private Map policyMonitoringServiceMap = new HashMap<>(); private TaskService taskService; private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java index f398658df9a..edc02bb973e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/PolicyManager.java @@ -38,7 +38,7 @@ public interface PolicyManager { boolean deletePolicy(int policyId) throws PolicyManagementException; Policy addPolicyToDevice(List deviceIdentifierList, Policy policy) throws - PolicyManagementException; + PolicyManagementException; Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagementException; @@ -60,10 +60,10 @@ public interface PolicyManager { List getPolicyAppliedDevicesIds(int policyId) throws PolicyManagementException; - void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, List - profileFeatures) throws PolicyManagementException; + void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) + throws PolicyManagementException; - void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; + void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 2cc6be75894..11db886791d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -214,21 +214,22 @@ public class MonitoringManagerImpl implements MonitoringManager { Map tempMap = new HashMap<>(); + if (complianceDatas != null) { + for (ComplianceData complianceData : complianceDatas) { - for (ComplianceData complianceData : complianceDatas) { + tempMap.put(complianceData.getDeviceId(), complianceData); - tempMap.put(complianceData.getDeviceId(), complianceData); - - if (complianceData.getAttempts() == 0) { - deviceIdsToAddOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } else { - deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); - } - if (complianceData.getAttempts() >= 20) { - inactiveDeviceIds.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + if (complianceData.getAttempts() == 0) { + deviceIdsToAddOperation.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } else { + deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } + if (complianceData.getAttempts() >= 20) { + inactiveDeviceIds.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } } } 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 be174530e77..627b77e8ffe 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 @@ -708,8 +708,8 @@ public class PolicyManagerImpl implements PolicyManager { } @Override - public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, int policyId, - List profileFeatures) throws PolicyManagementException { + public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) + throws PolicyManagementException { int deviceId = -1; try { @@ -719,9 +719,9 @@ public class PolicyManagerImpl implements PolicyManager { boolean exist = policyDAO.checkPolicyAvailable(deviceId); PolicyManagementDAOFactory.beginTransaction(); if (exist) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policyId, profileFeatures); + policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policyId, profileFeatures); + policyDAO.addEffectivePolicyToDevice(deviceId, policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -731,7 +731,7 @@ public class PolicyManagerImpl implements PolicyManager { log.warn("Error occurred while roll backing the transaction."); } String msg = "Error occurred while adding the evaluated policy to device (" + - deviceId + " - " + policyId + ")"; + deviceId + " - " + policy.getId() + ")"; log.error(msg, e); throw new PolicyManagementException(msg, e); } catch (DeviceManagementException e) { @@ -756,12 +756,10 @@ public class PolicyManagerImpl implements PolicyManager { if (exist) { Policy policySaved = policyDAO.getAppliedPolicy(deviceId); if (!policy.equals(policySaved)) { - policyDAO.updateEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). - getProfileFeaturesList()); + policyDAO.updateEffectivePolicyToDevice(deviceId, policy); } } else { - policyDAO.addEffectivePolicyToDevice(deviceId, policy.getId(), policy.getProfile(). - getProfileFeaturesList()); + policyDAO.addEffectivePolicyToDevice(deviceId, policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { 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 16f4dbffb5c..6061e7e8a58 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 @@ -308,6 +308,38 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ); + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + STATUS INT NULL, + LAST_SUCCESS_TIME TIMESTAMP NULL, + LAST_REQUESTED_TIME TIMESTAMP NULL, + LAST_FAILED_TIME TIMESTAMP NULL, + ATTEMPTS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( + ID INT NOT NULL AUTO_INCREMENT, + COMPLIANCE_STATUS_ID INT NOT NULL, + FEATURE_CODE INT NOT NULL, + STATUS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS + FOREIGN KEY (COMPLIANCE_STATUS_ID) + REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + -- POLICY RELATED TABLES FINISHED -- diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index cf8bf647d9b..a599e6989f7 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -295,6 +295,51 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( ON UPDATE NO ACTION ); +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + STATUS INT NULL, + LAST_SUCCESS_TIME TIMESTAMP NULL, + LAST_REQUESTED_TIME TIMESTAMP NULL, + LAST_FAILED_TIME TIMESTAMP NULL, + ATTEMPTS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( + ID INT NOT NULL AUTO_INCREMENT, + COMPLIANCE_STATUS_ID INT NOT NULL, + FEATURE_CODE INT NOT NULL, + STATUS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS + FOREIGN KEY (COMPLIANCE_STATUS_ID) + REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OWNER VARCHAR(50) NOT NULL, + OWNERSHIP VARCHAR(45) NULL DEFAULT NULL, + STATUS VARCHAR(50) NULL, + DATE_OF_ENROLMENT TIMESTAMP NULL DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP NULL DEFAULT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + CREATE TABLE IF NOT EXISTS DM_APPLICATION ( ID INTEGER AUTO_INCREMENT NOT NULL, NAME VARCHAR(50) NOT NULL, @@ -322,36 +367,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_APPLICATION_MAPPING ( DM_APPLICATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( - ID INT NOT NULL AUTO_INCREMENT, - DEVICE_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - STATUS INT NULL, - LAST_SUCCESS_TIME TIMESTAMP NULL, - LAST_REQUESTED_TIME TIMESTAMP NULL, - LAST_FAILED_TIME TIMESTAMP NULL, - ATTEMPTS INT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_POLICY_COMPLIANCE_STATUS_POLICY - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - - -CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( - ID INT NOT NULL AUTO_INCREMENT, - COMPLIANCE_STATUS_ID INT NOT NULL, - FEATURE_CODE INT NOT NULL, - STATUS INT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS - FOREIGN KEY (COMPLIANCE_STATUS_ID) - REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); -- POLICY RELATED TABLES FINISHED --