diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java index 5511604078e..0304e690f01 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceData.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.common.monitor; import org.wso2.carbon.policy.mgt.common.Policy; +import java.sql.Timestamp; import java.util.List; public class ComplianceData { @@ -30,6 +31,10 @@ public class ComplianceData { private int policyId; List complianceFeatures; private boolean status; + private Timestamp lastRequestedTime; + private Timestamp lastSucceededTime; + private Timestamp lastFailedTime; + private int attempts; private String message; /** @@ -47,6 +52,38 @@ public class ComplianceData { this.id = id; } + public Timestamp getLastRequestedTime() { + return lastRequestedTime; + } + + public void setLastRequestedTime(Timestamp lastRequestedTime) { + this.lastRequestedTime = lastRequestedTime; + } + + public Timestamp getLastSucceededTime() { + return lastSucceededTime; + } + + public void setLastSucceededTime(Timestamp lastSucceededTime) { + this.lastSucceededTime = lastSucceededTime; + } + + public Timestamp getLastFailedTime() { + return lastFailedTime; + } + + public void setLastFailedTime(Timestamp lastFailedTime) { + 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.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceDecisionPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceDecisionPoint.java index ada9b19a892..8799653fe30 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceDecisionPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Monitor/ComplianceDecisionPoint.java @@ -20,14 +20,19 @@ package org.wso2.carbon.policy.mgt.common.monitor; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Policy; +import java.util.List; + public interface ComplianceDecisionPoint { String getNoneComplianceRule(Policy policy) throws PolicyComplianceException; - void setDeviceAsUnreachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + void setDevicesAsUnreachable(List deviceIdentifiers) throws PolicyComplianceException; + + void setDevicesAsUnreachableWith(List devices) throws PolicyComplianceException; void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyVerificationException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyMonitoringTaskException.java similarity index 51% rename from components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyVerificationException.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyMonitoringTaskException.java index f0a25966c12..7fc01603ea8 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyVerificationException.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyMonitoringTaskException.java @@ -19,38 +19,38 @@ package org.wso2.carbon.policy.mgt.common; -public class PolicyVerificationException extends Exception { +public class PolicyMonitoringTaskException extends Exception { - private String policyVerificationErrorMessage; + private String policyMonitoingErrorMessage; - public String getPolicyVerificationErrorMessage() { - return policyVerificationErrorMessage; + public String getPolicyMonitoingErrorMessage() { + return policyMonitoingErrorMessage; } - public void setPolicyVerificationErrorMessage(String policyVerificationErrorMessage) { - this.policyVerificationErrorMessage = policyVerificationErrorMessage; + public void setPolicyMonitoingErrorMessage(String policyMonitoingErrorMessage) { + this.policyMonitoingErrorMessage = policyMonitoingErrorMessage; } - public PolicyVerificationException(String message) { + public PolicyMonitoringTaskException(String message) { super(message); - setPolicyVerificationErrorMessage(message); + setPolicyMonitoingErrorMessage(message); } - public PolicyVerificationException(String message, Exception ex) { + public PolicyMonitoringTaskException(String message, Exception ex) { super(message, ex); - setPolicyVerificationErrorMessage(message); + setPolicyMonitoingErrorMessage(message); } - public PolicyVerificationException(String message, Throwable cause) { + public PolicyMonitoringTaskException(String message, Throwable cause) { super(message, cause); - setPolicyVerificationErrorMessage(message); + setPolicyMonitoingErrorMessage(message); } - public PolicyVerificationException() { + public PolicyMonitoringTaskException() { super(); } - public PolicyVerificationException(Throwable cause) { + public PolicyMonitoringTaskException(Throwable cause) { super(cause); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java index 81478b6ba47..c1ca58a291e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/spi/PolicyMonitoringService.java @@ -28,7 +28,7 @@ import org.wso2.carbon.policy.mgt.common.Policy; import java.util.List; -public interface PolicyMonitoringService { +public interface PolicyMonitoringService { void notifyDevices(List devices) throws PolicyComplianceException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java index 59e0c7a30d2..405fe73f7ab 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerService.java @@ -21,17 +21,11 @@ package org.wso2.carbon.policy.mgt.core; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import java.util.List; @@ -69,6 +63,8 @@ public interface PolicyManagerService { PolicyEvaluationPoint getPEP() throws PolicyManagementException; + TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException; + int getPolicyCount() throws PolicyManagementException; List CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 70e76eb1f2c..f3045bb2127 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -35,6 +35,8 @@ import org.wso2.carbon.policy.mgt.core.impl.PolicyInformationPointImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleServiceImpl; import java.util.ArrayList; import java.util.List; @@ -203,6 +205,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); } + @Override + public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException { + return new TaskScheduleServiceImpl(); + } + @Override public int getPolicyCount() throws PolicyManagementException { return policyAdministratorPoint.getPolicyCount(); 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 426935e4e0a..3286ed39455 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 @@ -30,16 +30,21 @@ public interface MonitoringDAO { void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; - void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) + void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + complianceFeatures) throws MonitoringDAOException; ComplianceData getCompliance(int deviceId) throws MonitoringDAOException; + List getCompliance(List deviceIds) throws MonitoringDAOException; + List getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException; void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; + void updateAttempts(List deviceId, boolean reset) throws MonitoringDAOException; + } 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 e6bb638537f..c39bf57ab9d 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 @@ -29,11 +29,9 @@ 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.util.PolicyManagementDAOUtil; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; +import java.util.Calendar; import java.util.List; public class MonitoringDAOImpl implements MonitoringDAO { @@ -46,14 +44,17 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; ResultSet generatedKeys = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS) VALUES" + - " (?, ?, ?) "; + String query = "INSERT INTO DM_POLICY_COMPLIANCE_STATUS (DEVICE_ID, POLICY_ID, STATUS, LAST_FAILED_TIME, " + + "ATTEMPTS) VALUES (?, ?, ?, ?, ?) "; stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, policyId); stmt.setInt(3, 0); + stmt.setTimestamp(4, currentTimestamp); + stmt.setInt(5, 0); stmt.executeUpdate(); generatedKeys = stmt.getGeneratedKeys(); @@ -78,12 +79,15 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); try { conn = this.getConnection(); - String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ? WHERE DEVICE_ID = ?"; + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ?, ATTEMPTS=0, LAST_SUCCESS_TIME = ?" + + " WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, 1); - stmt.setInt(2, deviceId); + stmt.setTimestamp(2, currentTimestamp); + stmt.setInt(3, deviceId); stmt.executeUpdate(); @@ -105,8 +109,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " + - "VALUES" + - " (?, ?, ?) "; + "VALUES (?, ?, ?) "; + stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); for (ComplianceFeature feature : complianceFeatures) { stmt.setInt(1, policyComplianceStatusId); @@ -145,6 +149,10 @@ public class MonitoringDAOImpl implements MonitoringDAO { complianceData.setDeviceId(resultSet.getInt("DEVICE_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")); } return complianceData; @@ -158,6 +166,48 @@ public class MonitoringDAOImpl implements MonitoringDAO { } } + @Override + public List getCompliance(List deviceIds) throws MonitoringDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + List complianceDataList = null; + + try { + conn = this.getConnection(); + String query = "SELECT * FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID IN (?)"; + stmt = conn.prepareStatement(query); + stmt.setString(1, makeString(deviceIds)); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + + ComplianceData complianceData = new ComplianceData(); + + complianceData.setId(resultSet.getInt("ID")); + complianceData.setDeviceId(resultSet.getInt("DEVICE_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); + } + return complianceDataList; + + } catch (SQLException e) { + String msg = "Unable to retrieve compliance data from database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); + this.closeConnection(); + } + } + @Override public List getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException { @@ -217,9 +267,68 @@ public class MonitoringDAOImpl implements MonitoringDAO { @Override public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { + Connection conn; + PreparedStatement stmt = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + try { + + conn = this.getConnection(); + String query = ""; + if (reset) { + query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + + "WHERE DEVICE_ID = ?"; + } else { + query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + + "WHERE DEVICE_ID = ?"; + } + stmt = conn.prepareStatement(query); + stmt.setTimestamp(1, currentTimestamp); + stmt.setInt(2, deviceId); + stmt.executeUpdate(); + + } catch (SQLException e) { + String msg = "Unable to update the attempts data in database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } } + @Override + public void updateAttempts(List deviceIds, boolean reset) throws MonitoringDAOException { + Connection conn; + PreparedStatement stmt = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + try { + + conn = this.getConnection(); + String query = ""; + if (reset) { + query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + + "WHERE DEVICE_ID = ?"; + } else { + query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = ATTEMPTS + 1, LAST_REQUESTED_TIME = ? " + + "WHERE DEVICE_ID = ?"; + } + stmt = conn.prepareStatement(query); + for (int deviceId : deviceIds) { + stmt.setTimestamp(1, currentTimestamp); + stmt.setInt(2, deviceId); + stmt.addBatch(); + } + stmt.executeBatch(); + + } catch (SQLException e) { + String msg = "Unable to update the attempts data in database."; + log.error(msg, e); + throw new MonitoringDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(stmt, null); + } + } + private Connection getConnection() throws MonitoringDAOException { try { @@ -238,4 +347,14 @@ public class MonitoringDAOImpl implements MonitoringDAO { log.warn("Unable to close the database connection."); } } + + private String makeString(List values) { + + StringBuilder buff = new StringBuilder(); + for (int value : values) { + buff.append(value).append(","); + } + buff.deleteCharAt(buff.length() - 1); + return buff.toString(); + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java index 5506bcb363c..3036be50507 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java @@ -23,15 +23,13 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.EnrolmentDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; @@ -39,10 +37,7 @@ import org.wso2.carbon.policy.mgt.common.monitor.ComplianceDecisionPoint; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; 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.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; -import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import java.util.ArrayList; import java.util.List; @@ -51,50 +46,58 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { private static final Log log = LogFactory.getLog(ComplianceDecisionPointImpl.class); - private EnrolmentDAO enrolmentDAO; - private DeviceDAO deviceDAO; - - private PolicyManager policyManager; - - public ComplianceDecisionPointImpl() { - enrolmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); - deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - policyManager = new PolicyManagerImpl(); - } - @Override public String getNoneComplianceRule(Policy policy) throws PolicyComplianceException { return policy.getCompliance(); } @Override - public void setDeviceAsUnreachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { + public void setDevicesAsUnreachable(List deviceIdentifiers) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.UNREACHABLE, tenantId); - - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while setting the device as unreachable for " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.UNREACHABLE); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while setting the device as unreachable"; log.error(msg, e); throw new PolicyComplianceException(msg, e); } } + @Override + public void setDevicesAsUnreachableWith(List devices) throws PolicyComplianceException { + try { + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + for (Device device : devices) { + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(device.getDeviceIdentifier()); + deviceIdentifier.setType(device.getType()); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.UNREACHABLE); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while setting the device as unreachable"; + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + } + @Override public void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.ACTIVE, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.ACTIVE); + + } catch (DeviceManagementException e) { String msg = "Error occurred while setting the device as reachable for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); @@ -167,12 +170,12 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { public void markDeviceAsNoneCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.BLOCKED, tenantId); + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.BLOCKED); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementException e) { String msg = "Error occurred while marking device as none compliance " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); @@ -184,16 +187,15 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { public void markDeviceAsCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.ACTIVE, tenantId); + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.ACTIVE); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementException e) { String msg = "Error occurred while marking device as compliance " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); - throw new PolicyComplianceException(msg, e); } } @@ -202,12 +204,13 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { public void deactivateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.INACTIVE, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.INACTIVE); + + } catch (DeviceManagementException e) { String msg = "Error occurred while deactivating the device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); @@ -219,12 +222,12 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { public void activateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - int tenantId = PolicyManagerUtil.getTenantId(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), - EnrolmentInfo.Status.ACTIVE, tenantId); + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.ACTIVE); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementException e) { String msg = "Error occurred while activating the device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); @@ -257,4 +260,8 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { this.markDeviceAsNoneCompliance(deviceIdentifier); } } + + private DeviceManagementProviderService getDeviceManagementProviderService() { + return PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + } } 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 4f2d664494c..af03b36d566 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; @@ -35,6 +36,7 @@ public class PolicyManagementDataHolder { private PolicyInformationPoint policyInformationPoint; private DeviceManagementProviderService deviceManagementService; private Map policyMonitoringServiceMap; + private TaskService taskService; private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); @@ -99,4 +101,12 @@ public class PolicyManagementDataHolder { public void unsetPolicyMonitoringService(String deviceType) { this.policyMonitoringServiceMap.remove(deviceType); } + + public TaskService getTaskService() { + return taskService; + } + + public void setTaskService(TaskService taskService) { + this.taskService = taskService; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index d6f72dba744..4ea26e0a1b7 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -58,6 +59,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setPolicyMonitoringService" * unbind="unsetPolicyMonitoringService" + * @scr.reference name="ntask.component" + * interface="org.wso2.carbon.ntask.core.service.TaskService" + * cardinality="1..1" + * policy="dynamic" + * bind="setTaskService" + * unbind="unsetTaskService" */ @SuppressWarnings("unused") public class PolicyManagementServiceComponent { @@ -131,7 +138,7 @@ public class PolicyManagementServiceComponent { protected void unsetPEPService(PolicyEvaluationPoint pepService) { if (log.isDebugEnabled()) { - log.debug("Unsetting Policy Information Service"); + log.debug("Removing Policy Information Service"); } PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null); } @@ -145,7 +152,7 @@ public class PolicyManagementServiceComponent { protected void unsetDeviceManagementService(DeviceManagementProviderService deviceManagementService) { if (log.isDebugEnabled()) { - log.debug("Unsetting Device Management Service"); + log.debug("Removing Device Management Service"); } PolicyManagementDataHolder.getInstance().setDeviceManagementService(null); } @@ -161,10 +168,24 @@ public class PolicyManagementServiceComponent { protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { if (log.isDebugEnabled()) { - log.debug("Setting Policy Monitoring Service"); + log.debug("Removing the Policy Monitoring Service"); } // TODO: FIX THE device type by taking from properties PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(""); } + protected void setTaskService(TaskService taskService) { + if (log.isDebugEnabled()) { + log.debug("Setting the task service."); + } + PolicyManagementDataHolder.getInstance().setTaskService(taskService); + } + + protected void unsetTaskService(TaskService taskService) { + if (log.isDebugEnabled()) { + log.debug("Removing the task service."); + } + PolicyManagementDataHolder.getInstance().setTaskService(null); + } + } 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 8ad323f2eb8..9da602f2613 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 @@ -19,6 +19,7 @@ package org.wso2.carbon.policy.mgt.core.mgt; +import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; @@ -36,4 +37,6 @@ public interface MonitoringManager { ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; + void addMonitoringOperation(List devices) 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 37b3b6491d0..ec6714cb902 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 @@ -43,7 +43,7 @@ import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; -import java.util.List; +import java.util.*; public class MonitoringManagerImpl implements MonitoringManager { @@ -186,4 +186,87 @@ public class MonitoringManagerImpl implements MonitoringManager { } return complianceData; } + + @Override + public void addMonitoringOperation(List devices) throws PolicyComplianceException { + + try { + + ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); + + int tenantId = PolicyManagerUtil.getTenantId(); + Map deviceIds = new HashMap<>(); + + for (Device device : devices) { + deviceIds.put(device.getId(), device); + } + + List complianceDatas = monitoringDAO.getCompliance(new ArrayList<>(deviceIds.keySet())); + + Map deviceIdsToAddOperation = new HashMap<>(); + Map deviceIdsWithExistingOperation = new HashMap<>(); + Map inactiveDeviceIds = new HashMap<>(); + + Map tempMap = new HashMap<>(); + + + for (ComplianceData 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())); + } + if (complianceData.getAttempts() >= 20) { + inactiveDeviceIds.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } + } + + for (Device device : devices) { + if (!tempMap.containsKey(device.getId())) { + deviceIdsToAddOperation.put(device.getId(), device); + } + } + + if (!deviceIdsToAddOperation.isEmpty()) { + this.addMonitoringOperationsToDatabase(new ArrayList<>(deviceIdsToAddOperation.values())); + } + + if (!deviceIdsWithExistingOperation.isEmpty()) { + monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); + decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( + new ArrayList<>(deviceIdsWithExistingOperation.values()))); + } + + } catch (MonitoringDAOException e) { + String msg = "Error occurred from monitoring dao."; + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + + } + + + private void addMonitoringOperationsToDatabase(List devices) throws PolicyComplianceException { + + } + + private List getDeviceIdentifiersFromDevices(List devices) { + + List deviceIdentifiers = new ArrayList<>(); + for (Device device : devices) { + DeviceIdentifier identifier = new DeviceIdentifier(); + identifier.setId(device.getDeviceIdentifier()); + identifier.setType(device.getType()); + + deviceIdentifiers.add(identifier); + } + return deviceIdentifiers; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java index 5c73fcedea4..f2b738b1efc 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java @@ -20,19 +20,13 @@ package org.wso2.carbon.policy.mgt.core.service; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.FeatureManagementException; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; -import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; +import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; import java.util.List; @@ -112,6 +106,11 @@ public class PolicyManagementService implements PolicyManagerService { return policyManagerService.getPEP(); } + @Override + public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException { + return policyManagerService.getTaskScheduleService(); + } + @Override public int getPolicyCount() throws PolicyManagementException { return policyManagerService.getPolicyCount(); 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 cf62d08b07c..5780966645c 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 @@ -19,6 +19,8 @@ package org.wso2.carbon.policy.mgt.core.task; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; @@ -27,6 +29,8 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; +import org.wso2.carbon.policy.mgt.core.mgt.impl.MonitoringManagerImpl; import java.util.List; import java.util.Map; @@ -34,10 +38,14 @@ import java.util.Map; public class MonitoringTask implements Task { private DeviceTypeDAO deviceTypeDAO; + private static Log log = LogFactory.getLog(MonitoringTask.class); + + Map properties; + @Override public void setProperties(Map map) { - + this.properties = map; } @Override @@ -47,23 +55,34 @@ public class MonitoringTask implements Task { @Override public void execute() { - try { - List deviceTypes = deviceTypeDAO.getDeviceTypes(); + if(log.isDebugEnabled()) { + log.debug("Monitoring task started to run."); + } + try { + List deviceTypes = deviceTypeDAO.getDeviceTypes(); DeviceManagementProviderService deviceManagementProviderService = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + MonitoringManager monitoringManager = new MonitoringManagerImpl(); for (DeviceType deviceType : deviceTypes) { PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); - List devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); - monitoringService.notifyDevices(devices); + if (monitoringService != null && !devices.isEmpty()) { + monitoringManager.addMonitoringOperation(devices); + monitoringService.notifyDevices(devices); + } } - } catch (Exception e) { + if(log.isDebugEnabled()) { + log.debug("Monitoring task running completed."); + } + } catch (Exception e) { + String msg = "Error occurred while trying to run a task."; + log.error(msg, e); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java new file mode 100644 index 00000000000..29d45149eb2 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleService.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.task; + +import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; + +public interface TaskScheduleService { + + void startTask(int monitoringFrequency) throws PolicyMonitoringTaskException; + + void stopTask() throws PolicyMonitoringTaskException; + + void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException; +} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java new file mode 100644 index 00000000000..ac0eaf1f67f --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -0,0 +1,129 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.wso2.carbon.policy.mgt.core.task; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.ntask.common.TaskException; +import org.wso2.carbon.ntask.core.TaskInfo; +import org.wso2.carbon.ntask.core.TaskManager; +import org.wso2.carbon.ntask.core.service.TaskService; +import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; +import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; +import org.wso2.carbon.ntask.core.TaskInfo.TriggerInfo; + +import java.util.HashMap; +import java.util.Map; + +public class TaskScheduleServiceImpl implements TaskScheduleService { + + private static Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); + + @Override + public void startTask(int monitoringFrequency) throws PolicyMonitoringTaskException { + + if (monitoringFrequency <= 0) { + throw new PolicyMonitoringTaskException("Time interval cannot be 0 or less than 0."); + } + try { + int tenantId = PolicyManagerUtil.getTenantId(); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + taskService.registerTaskType(PolicyManagementConstants.TASK_TYPE); + + if (log.isDebugEnabled()) { + log.debug("Monitoring task is started for the tenant id " + tenantId); + } + + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + + TriggerInfo triggerInfo = new TriggerInfo(); + + triggerInfo.setIntervalMillis(monitoringFrequency); + triggerInfo.setRepeatCount(-1); + + Map properties = new HashMap<>(); + properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); + + String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.TASK_CLAZZ, properties, triggerInfo); + + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + + + } catch (TaskException e) { + String msg = "Error occurred while creating the task for tenant " + PolicyManagerUtil.getTenantId(); + log.error(msg, e); + throw new PolicyMonitoringTaskException(msg ,e); + } + + + } + + @Override + public void stopTask() throws PolicyMonitoringTaskException { + try { + int tenantId = PolicyManagerUtil.getTenantId(); + String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + taskManager.deleteTask(taskName); + } catch (TaskException e) { + String msg = "Error occurred while deleting the task for tenant " + PolicyManagerUtil.getTenantId(); + log.error(msg, e); + throw new PolicyMonitoringTaskException(msg ,e); + } + } + + @Override + public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException { + try{ + int tenantId = PolicyManagerUtil.getTenantId(); + String taskName = PolicyManagementConstants.TASK_NAME + "_" + String.valueOf(tenantId); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.TASK_TYPE); + + taskManager.deleteTask(taskName); + + TriggerInfo triggerInfo = new TriggerInfo(); + + triggerInfo.setIntervalMillis(monitoringFrequency); + triggerInfo.setRepeatCount(-1); + + Map properties = new HashMap<>(); + properties.put("tenantId", String.valueOf(tenantId)); + + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.TASK_CLAZZ, properties, triggerInfo); + + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + + } catch (TaskException e) { + String msg = "Error occurred while updating the task for tenant " + PolicyManagerUtil.getTenantId(); + log.error(msg, e); + throw new PolicyMonitoringTaskException(msg ,e); + } + + } +} 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 477e9bcb322..e6fe25c9a45 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 @@ -24,10 +24,17 @@ public final class PolicyManagementConstants { public static final String ANY = "ANY"; public static final String POLICY_BUNDLE = "POLICY_BUNDLE"; + public static final String TENANT_ID = "TENANT_ID"; + public static final String MONITOR = "MONITOR"; public static final String ENFORCE = "ENFORCE"; public static final String WARN = "WARN"; public static final String BLOCK = "BLOCK"; + public static final String TASK_TYPE = "MONITORING_TASK"; + public static final String TASK_NAME = "MONITORING"; + public static final String TASK_CLAZZ = "org.wso2.carbon.policy.mgt.core.task.MonitoringTask"; + + }