From a1a313608f441d78cd10ded61485201dafd80183 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 12 May 2020 15:34:55 +0530 Subject: [PATCH] Remove attempt count incrementing logic to optimize performance --- .../policy/mgt/monitor/NonComplianceData.java | 9 -- .../org.wso2.carbon.policy.mgt.core/pom.xml | 2 +- .../policy/mgt/core/dao/MonitoringDAO.java | 6 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 114 +++----------- .../mgt/core/dao/impl/PolicyDAOImpl.java | 6 +- .../mgt/core/mgt/MonitoringManager.java | 9 +- .../core/mgt/impl/MonitoringManagerImpl.java | 145 +++++------------- .../policy/mgt/core/task/MonitoringTask.java | 21 ++- .../core/PolicyManagerServiceImplTest.java | 4 +- .../mgt/impl/MonitoringManagerImplTest.java | 2 +- 10 files changed, 79 insertions(+), 239 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java index ffbe36cb8d..09f835bd92 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/monitor/NonComplianceData.java @@ -35,7 +35,6 @@ public class NonComplianceData { private Timestamp lastRequestedTime; private Timestamp lastSucceededTime; private Timestamp lastFailedTime; - private int attempts; private String message; /** @@ -85,14 +84,6 @@ public class NonComplianceData { this.lastFailedTime = lastFailedTime; } - public int getAttempts() { - return attempts; - } - - public void setAttempts(int attempts) { - this.attempts = attempts; - } - public int getDeviceId() { return deviceId; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml index 869d0ead09..1460c114cb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/pom.xml @@ -61,7 +61,7 @@ javax.cache, javax.naming, javax.xml.*, - javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, + javax.xml.parsers;version="${javax.xml.parsers.import.pkg.version}";resolution:=optional, org.w3c.dom, org.wso2.carbon.policy.mgt.common.*, org.wso2.carbon.user.core.*, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index d7af966f28..ad9f9e02eb 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -55,7 +55,7 @@ public interface MonitoringDAO { List getCompliance(List deviceIds) throws MonitoringDAOException; - List getCompliance() throws MonitoringDAOException; + Map getCompliance() throws MonitoringDAOException; List getAllComplianceDevices( PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, String fromDate, String toDate) @@ -65,8 +65,4 @@ public interface MonitoringDAO { void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; - boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; - - void updateAttempts(List deviceId, boolean reset) throws MonitoringDAOException; - } \ 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/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 662d3ac349..e9f8abcf77 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -49,15 +49,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?) "; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, policyId); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); stmt.executeUpdate(); generatedKeys = stmt.getGeneratedKeys(); @@ -90,16 +89,15 @@ public class MonitoringDAOImpl implements MonitoringDAO { } try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?,?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID) VALUES (?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(query); for (Map.Entry map : devicePolicyMap.entrySet()) { stmt.setInt(1, map.getKey()); stmt.setInt(2, map.getValue()); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); stmt.addBatch(); } stmt.executeBatch(); @@ -119,23 +117,22 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (log.isDebugEnabled()) { - for (PolicyDeviceWrapper wrapper : policyDeviceWrapper){ + for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) { log.debug("Policy Id : " + wrapper.getPolicyId() + " - " + " Device Id : " + wrapper.getDeviceId()); } } try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, ATTEMPTS, " + - "LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, " + + "LAST_REQUESTED_TIME, TENANT_ID, ENROLMENT_ID) VALUES (?, ?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(query); for (PolicyDeviceWrapper wrapper : policyDeviceWrapper) { stmt.setInt(1, wrapper.getDeviceId()); stmt.setInt(2, wrapper.getPolicyId()); stmt.setInt(3, 1); - stmt.setInt(4, 1); - stmt.setTimestamp(5, currentTimestamp); - stmt.setInt(6, tenantId); - stmt.setInt(7, wrapper.getEnrolmentId()); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, tenantId); + stmt.setInt(6, wrapper.getEnrolmentId()); stmt.addBatch(); } stmt.executeBatch(); @@ -155,8 +152,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ?," + - " ATTEMPTS=0 WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = 0, LAST_FAILED_TIME = ?, POLICY_ID = ? " + + "WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setTimestamp(1, currentTimestamp); stmt.setInt(2, policyId); @@ -182,8 +179,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + - " WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, LAST_SUCCESS_TIME = ?" + + " WHERE DEVICE_ID = ? AND TENANT_ID = ? AND ENROLMENT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, 1); stmt.setTimestamp(2, currentTimestamp); @@ -260,9 +257,9 @@ public class MonitoringDAOImpl implements MonitoringDAO { while (resultSet.next()) { complianceData.setId(resultSet.getInt("ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); + complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); @@ -298,7 +295,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); @@ -314,11 +310,11 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public List getCompliance() throws MonitoringDAOException { + public Map getCompliance() throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List complianceDataList = new ArrayList<>(); + Map complianceDataList = new HashMap<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); @@ -327,19 +323,19 @@ public class MonitoringDAOImpl implements MonitoringDAO { stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); + NonComplianceData complianceData; while (resultSet.next()) { - NonComplianceData complianceData = new NonComplianceData(); + complianceData = new NonComplianceData(); complianceData.setId(resultSet.getInt("ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); complianceData.setEnrolmentId(resultSet.getInt("ENROLMENT_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setStatus(resultSet.getBoolean("STATUS")); - complianceData.setAttempts(resultSet.getInt("ATTEMPTS")); complianceData.setLastRequestedTime(resultSet.getTimestamp("LAST_REQUESTED_TIME")); complianceData.setLastSucceededTime(resultSet.getTimestamp("LAST_SUCCESS_TIME")); complianceData.setLastFailedTime(resultSet.getTimestamp("LAST_FAILED_TIME")); - complianceDataList.add(complianceData); + complianceDataList.put(complianceData.getEnrolmentId(), complianceData); } return complianceDataList; } catch (SQLException e) { @@ -492,68 +488,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { } - @Override - public boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { - boolean status = false; - Connection conn; - PreparedStatement stmt = null; - Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - conn = this.getConnection(); - String query = ""; - if (reset) { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } else { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } - stmt = conn.prepareStatement(query); - stmt.setTimestamp(1, currentTimestamp); - stmt.setInt(2, deviceId); - stmt.setInt(3, tenantId); - stmt.executeUpdate(); - status = true; - } catch (SQLException e) { - throw new MonitoringDAOException("Unable to update the attempts data in database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - return status; - } - - @Override - public void updateAttempts(List deviceIds, boolean reset) throws MonitoringDAOException { - Connection conn; - PreparedStatement stmt = null; - Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - try { - conn = this.getConnection(); - String query = ""; - if (reset) { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } else { - query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + - "WHERE DEVICE_ID = ? AND TENANT_ID = ?"; - } - stmt = conn.prepareStatement(query); - for (int deviceId : deviceIds) { - stmt.setTimestamp(1, currentTimestamp); - stmt.setInt(2, deviceId); - stmt.setInt(3, tenantId); - stmt.addBatch(); - } - stmt.executeBatch(); - } catch (SQLException e) { - throw new MonitoringDAOException("Unable to update the attempts data in database.", e); - } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); - } - } - private Connection getConnection() throws MonitoringDAOException { return PolicyManagementDAOFactory.getConnection(); } 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 a244d3b6a6..766c512eb0 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 @@ -1778,13 +1778,13 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; + String query = "SELECT POLICY_ID, ENROLMENT_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { - devicePolicyIds.put(resultSet.getInt("DEVICE_ID"), resultSet.getInt("POLICY_ID")); + devicePolicyIds.put(resultSet.getInt("ENROLMENT_ID"), resultSet.getInt("POLICY_ID")); } } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while getting the applied policy", e); @@ -1805,7 +1805,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; + String query = "SELECT POLICY_ID, DEVICE_ID FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java index b058ff5fcb..c0773fa080 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/MonitoringManager.java @@ -44,15 +44,8 @@ public interface MonitoringManager { NonComplianceData getDevicePolicyCompliance(Device device) throws PolicyComplianceException; - void addMonitoringOperation(List devices) throws PolicyComplianceException; + void addMonitoringOperation(String deviceType, List devices) throws PolicyComplianceException; List getDeviceTypes() throws PolicyComplianceException; - PaginationResult getPolicyCompliance( - PaginationRequest paginationRequest, String policyId, boolean complianceStatus, boolean isPending, - String fromDate, String toDate) throws PolicyComplianceException; - - List getNoneComplianceFeatures(int complianceStatusId) - throws PolicyComplianceException; - } 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 a2d4039826..1edca6715c 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 @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceData; +import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; @@ -58,9 +59,9 @@ import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { - private PolicyDAO policyDAO; - private MonitoringDAO monitoringDAO; - private ComplianceDecisionPoint complianceDecisionPoint; + private final PolicyDAO policyDAO; + private final MonitoringDAO monitoringDAO; + private final ComplianceDecisionPoint complianceDecisionPoint; private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final String OPERATION_MONITOR = "MONITOR"; @@ -113,6 +114,7 @@ public class MonitoringManagerImpl implements MonitoringManager { complianceData.setPolicy(policy); complianceFeatures = complianceData.getComplianceFeatures(); complianceData.setDeviceId(device.getId()); + complianceData.setEnrolmentId(cmd.getEnrolmentId()); complianceData.setPolicyId(policy.getId()); } @@ -121,7 +123,7 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (MonitoringDAOException e) { throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + - device.getDeviceIdentifier() + " - " + device.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -145,7 +147,7 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException( "Unable to add the none compliance features to database for device " + - device.getDeviceIdentifier() + " - " + device.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -253,106 +255,46 @@ public class MonitoringManagerImpl implements MonitoringManager { } @Override - public void addMonitoringOperation(List devices) throws PolicyComplianceException { + public void addMonitoringOperation(String deviceType, List devices) throws PolicyComplianceException { - //int tenantId = PolicyManagerUtil.getTenantId(); - Map deviceIds = new HashMap<>(); - List complianceDatas = new ArrayList<>(); - HashMap devicePolicyIdMap = new HashMap<>(); + Map notifiableDeviceEnrollments = new HashMap<>(); + List firstTimeComplianceData = new ArrayList<>(); try { PolicyManagementDAOFactory.openConnection(); - //TODO: Return a map from getCompliance to reduce O(n^2) -> O(n) - List cd = monitoringDAO.getCompliance(); + Map persistedComplianceData = monitoringDAO.getCompliance(); + HashMap appliedPolicyIds = policyDAO.getAppliedPolicyIds(); + int enrollmentId; for (Device device : devices) { - deviceIds.put(device.getId(), device); - - for (NonComplianceData data : cd) { - if (device.getId() == data.getDeviceId() && device.getEnrolmentInfo().getId() == data - .getEnrolmentId()) { - complianceDatas.add(data); - } - } - } - List deviceIDs = new ArrayList<>(deviceIds.keySet()); - - HashMap temp = policyDAO.getAppliedPolicyIds(); - for (Integer id : deviceIDs) { - if (temp != null && !temp.isEmpty() && temp.containsKey(id)) { - devicePolicyIdMap.put(id, temp.get(id)); + enrollmentId = device.getEnrolmentInfo().getId(); + if (persistedComplianceData.containsKey(enrollmentId)) { + notifiableDeviceEnrollments.put(enrollmentId, device); + } else if (appliedPolicyIds.containsKey(enrollmentId)){ + PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper(); + policyDeviceWrapper.setDeviceId(device.getId()); + policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId()); + policyDeviceWrapper.setPolicyId(appliedPolicyIds.get(enrollmentId)); + firstTimeComplianceData.add(policyDeviceWrapper); + notifiableDeviceEnrollments.put(enrollmentId, device); } } - } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (MonitoringDAOException e) { - throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); + throw new PolicyComplianceException("Error occurred while getting monitoring details.", e); } catch (PolicyManagerDAOException e) { throw new PolicyComplianceException("SQL error occurred while getting policy details.", e); } finally { PolicyManagementDAOFactory.closeConnection(); } - Map deviceIdsToAddOperation = new HashMap<>(); - Map deviceIdsWithExistingOperation = new HashMap<>(); - - List firstTimeDevices = new ArrayList<>(); - - Map tempMap = new HashMap<>(); - try { - if (complianceDatas != null || !complianceDatas.isEmpty()) { - for (NonComplianceData complianceData : complianceDatas) { - - 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())); - } - } - } - - for (Device device : devices) { - if ((!tempMap.containsKey(device.getId())) && (devicePolicyIdMap.containsKey(device.getId()))) { - deviceIdsToAddOperation.put(device.getId(), device); - - PolicyDeviceWrapper policyDeviceWrapper = new PolicyDeviceWrapper(); - policyDeviceWrapper.setDeviceId(device.getId()); - policyDeviceWrapper.setEnrolmentId(device.getEnrolmentInfo().getId()); - policyDeviceWrapper.setPolicyId(devicePolicyIdMap.get(device.getId())); - - firstTimeDevices.add(policyDeviceWrapper); - - // firstTimeDeviceIdsWithPolicyIds.put(device.getId(), devicePolicyIdMap.get(device.getId())); - } - } - - if (log.isDebugEnabled()) { - log.debug("These devices are in the system for the first time"); - for (PolicyDeviceWrapper wrapper : firstTimeDevices) { - log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + - wrapper.getPolicyId()); - } - } - PolicyManagementDAOFactory.beginTransaction(); - - if (!deviceIdsToAddOperation.isEmpty()) { - // monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); - monitoringDAO.addComplianceDetails(firstTimeDevices); - monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false); + if (!firstTimeComplianceData.isEmpty()) { + monitoringDAO.addComplianceDetails(firstTimeComplianceData); + PolicyManagementDAOFactory.commitTransaction(); } - - if (!deviceIdsWithExistingOperation.isEmpty()) { - monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); - } - PolicyManagementDAOFactory.commitTransaction(); - } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyComplianceException("Error occurred from monitoring dao.", e); @@ -363,9 +305,9 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.closeConnection(); } - if (!deviceIdsToAddOperation.isEmpty()) { + if (!notifiableDeviceEnrollments.isEmpty()) { try { - this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); + this.addMonitoringOperationsToDatabase(deviceType, new ArrayList<>(notifiableDeviceEnrollments.values())); } catch (InvalidDeviceException e) { throw new PolicyComplianceException("Invalid Device Identifiers found.", e); } catch (OperationManagementException e) { @@ -381,8 +323,8 @@ public class MonitoringManagerImpl implements MonitoringManager { try { //when shutdown, it sets DeviceManagementService to null, therefore need to have a null check if (PolicyManagementDataHolder.getInstance().getDeviceManagementService() != null) { - deviceTypes = - PolicyManagementDataHolder.getInstance().getDeviceManagementService().getPolicyMonitoringEnableDeviceTypes(); + deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService() + .getPolicyMonitoringEnableDeviceTypes(); } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Error occurred while getting the device types.", e); @@ -436,32 +378,17 @@ public class MonitoringManagerImpl implements MonitoringManager { return complianceFeatureList; } - private void addMonitoringOperationsToDatabase(List devices) - throws PolicyComplianceException, OperationManagementException, InvalidDeviceException { + private void addMonitoringOperationsToDatabase(String deviceType, List devices) + throws OperationManagementException, InvalidDeviceException { List deviceIdentifiers = this.getDeviceIdentifiersFromDevices(devices); CommandOperation monitoringOperation = new CommandOperation(); monitoringOperation.setEnabled(true); monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setCode(OPERATION_MONITOR); - // CommandOperation infoOperation = new CommandOperation(); - // infoOperation.setEnabled(true); - // infoOperation.setType(Operation.Type.COMMAND);\\ - // infoOperation.setCode(OPERATION_INFO); - // CommandOperation appListOperation = new CommandOperation(); - // appListOperation.setEnabled(true); - // appListOperation.setType(Operation.Type.COMMAND); - // appListOperation.setCode(OPERATION_APP_LIST); - - //TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()" - String type = null; - if (deviceIdentifiers.size() > 0) { - type = deviceIdentifiers.get(0).getType(); - } - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - service.addOperation(type, monitoringOperation, deviceIdentifiers); - // service.addOperation(infoOperation, deviceIdentifiers); - // service.addOperation(appListOperation, deviceIdentifiers); + monitoringOperation.setControl(Operation.Control.NO_REPEAT); + PolicyManagementDataHolder.getInstance().getDeviceManagementService() + .addOperation(deviceType, monitoringOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index 3fbe2d5834..7ed5e683d3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -23,7 +23,7 @@ import org.apache.commons.logging.Log; 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.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; @@ -40,7 +40,7 @@ import java.util.Map; public class MonitoringTask implements Task { - private static Log log = LogFactory.getLog(MonitoringTask.class); + private static final Log log = LogFactory.getLog(MonitoringTask.class); Map properties; private boolean executeForTenants = false; @@ -62,10 +62,10 @@ public class MonitoringTask implements Task { if (log.isDebugEnabled()) { log.debug("Monitoring task started to run."); } - if(System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))){ + if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) { executeForTenants = true; } - if(executeForTenants) { + if (executeForTenants) { this.executeforAllTenants(); } else { this.executeTask(); @@ -83,10 +83,7 @@ public class MonitoringTask implements Task { private boolean isPlatformExist(String deviceType) { PolicyMonitoringManager policyMonitoringManager = PolicyManagementDataHolder.getInstance() .getDeviceManagementService().getPolicyMonitoringManager(deviceType); - if (policyMonitoringManager != null) { - return true; - } - return false; + return policyMonitoringManager != null; } private void executeforAllTenants() { @@ -116,7 +113,7 @@ public class MonitoringTask implements Task { } } - private void executeTask(){ + private void executeTask() { MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); List deviceTypes = new ArrayList<>(); @@ -158,13 +155,14 @@ public class MonitoringTask implements Task { } } if (log.isDebugEnabled()) { - log.debug("Following devices selected to send the notification for " + deviceType); + log.debug("Following '" + deviceType + "' devices selected to send the notification " + + "for policy monitoring"); for (Device device : notifiableDevices) { log.debug(device.getDeviceIdentifier()); } } if (!notifiableDevices.isEmpty()) { - monitoringManager.addMonitoringOperation(notifiableDevices); + monitoringManager.addMonitoringOperation(deviceType, notifiableDevices); } } } @@ -178,4 +176,5 @@ public class MonitoringTask implements Task { log.info("No device types registered currently. So did not run the monitoring task."); } } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java index 1bd4d6591e..2ffacebbac 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImplTest.java @@ -259,7 +259,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { deviceList.add(device); MonitoringManager manager = new MonitoringManagerImpl(); - manager.addMonitoringOperation(deviceList); + manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); @@ -289,7 +289,7 @@ public class PolicyManagerServiceImplTest extends BasePolicyManagementDAOTest { deviceList.add(device); MonitoringManager manager = new MonitoringManagerImpl(); - manager.addMonitoringOperation(deviceList); + manager.addMonitoringOperation(DEVICE_TYPE_A, deviceList); policyManagerService.checkCompliance(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A), complianceFeatures); boolean deviceCompliance = policyManagerService.isCompliant(new DeviceIdentifier(DEVICE1, DEVICE_TYPE_A)); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java index 07dc2dd992..1faed27398 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImplTest.java @@ -316,7 +316,7 @@ public class MonitoringManagerImplTest extends BasePolicyManagementDAOTest{ @Test public void testAddMonitoringOperation() throws Exception { - monitoringManager.addMonitoringOperation(deviceMgtService.getAllDevices()); + monitoringManager.addMonitoringOperation(DEVICE_TYPE_E, deviceMgtService.getAllDevices()); } @Test