Adding the 90% completed monitoring task

revert-70aa11f8
geethkokila 9 years ago
parent 4049d1a2b4
commit e0301e4ce3

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.common.monitor;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import java.sql.Timestamp;
import java.util.List; import java.util.List;
public class ComplianceData { public class ComplianceData {
@ -30,6 +31,10 @@ public class ComplianceData {
private int policyId; private int policyId;
List<ComplianceFeature> complianceFeatures; List<ComplianceFeature> complianceFeatures;
private boolean status; private boolean status;
private Timestamp lastRequestedTime;
private Timestamp lastSucceededTime;
private Timestamp lastFailedTime;
private int attempts;
private String message; private String message;
/** /**
@ -47,6 +52,38 @@ public class ComplianceData {
this.id = id; 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() { public int getDeviceId() {
return deviceId; return deviceId;
} }

@ -20,14 +20,19 @@
package org.wso2.carbon.policy.mgt.common.monitor; 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.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.Policy;
import java.util.List;
public interface ComplianceDecisionPoint { public interface ComplianceDecisionPoint {
String getNoneComplianceRule(Policy policy) throws PolicyComplianceException; String getNoneComplianceRule(Policy policy) throws PolicyComplianceException;
void setDeviceAsUnreachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; void setDevicesAsUnreachable(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException;
void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException;
void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;

@ -19,38 +19,38 @@
package org.wso2.carbon.policy.mgt.common; 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() { public String getPolicyMonitoingErrorMessage() {
return policyVerificationErrorMessage; return policyMonitoingErrorMessage;
} }
public void setPolicyVerificationErrorMessage(String policyVerificationErrorMessage) { public void setPolicyMonitoingErrorMessage(String policyMonitoingErrorMessage) {
this.policyVerificationErrorMessage = policyVerificationErrorMessage; this.policyMonitoingErrorMessage = policyMonitoingErrorMessage;
} }
public PolicyVerificationException(String message) { public PolicyMonitoringTaskException(String message) {
super(message); super(message);
setPolicyVerificationErrorMessage(message); setPolicyMonitoingErrorMessage(message);
} }
public PolicyVerificationException(String message, Exception ex) { public PolicyMonitoringTaskException(String message, Exception ex) {
super(message, ex); super(message, ex);
setPolicyVerificationErrorMessage(message); setPolicyMonitoingErrorMessage(message);
} }
public PolicyVerificationException(String message, Throwable cause) { public PolicyMonitoringTaskException(String message, Throwable cause) {
super(message, cause); super(message, cause);
setPolicyVerificationErrorMessage(message); setPolicyMonitoingErrorMessage(message);
} }
public PolicyVerificationException() { public PolicyMonitoringTaskException() {
super(); super();
} }
public PolicyVerificationException(Throwable cause) { public PolicyMonitoringTaskException(Throwable cause) {
super(cause); super(cause);
} }

@ -28,7 +28,7 @@ import org.wso2.carbon.policy.mgt.common.Policy;
import java.util.List; import java.util.List;
public interface PolicyMonitoringService { public interface PolicyMonitoringService {
void notifyDevices(List<Device> devices) throws PolicyComplianceException; void notifyDevices(List<Device> devices) throws PolicyComplianceException;

@ -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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.Feature; 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.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; 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.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
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 java.util.List; import java.util.List;
@ -69,6 +63,8 @@ public interface PolicyManagerService {
PolicyEvaluationPoint getPEP() throws PolicyManagementException; PolicyEvaluationPoint getPEP() throws PolicyManagementException;
TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException;
int getPolicyCount() throws PolicyManagementException; int getPolicyCount() throws PolicyManagementException;
List<ComplianceFeature> CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object List<ComplianceFeature> CheckPolicyCompliance(DeviceIdentifier deviceIdentifier, Object

@ -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.internal.PolicyManagementDataHolder;
import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; 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.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.ArrayList;
import java.util.List; import java.util.List;
@ -203,6 +205,11 @@ public class PolicyManagerServiceImpl implements PolicyManagerService {
return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint(); return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint();
} }
@Override
public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException {
return new TaskScheduleServiceImpl();
}
@Override @Override
public int getPolicyCount() throws PolicyManagementException { public int getPolicyCount() throws PolicyManagementException {
return policyAdministratorPoint.getPolicyCount(); return policyAdministratorPoint.getPolicyCount();

@ -30,16 +30,21 @@ public interface MonitoringDAO {
void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException; void setDeviceAsCompliance(int deviceId, int policyId) throws MonitoringDAOException;
void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature> complianceFeatures) void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List<ComplianceFeature>
complianceFeatures)
throws MonitoringDAOException; throws MonitoringDAOException;
ComplianceData getCompliance(int deviceId) throws MonitoringDAOException; ComplianceData getCompliance(int deviceId) throws MonitoringDAOException;
List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException;
List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException; List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException;
void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException;
void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException;
void updateAttempts(List<Integer> deviceId, boolean reset) throws MonitoringDAOException;
} }

@ -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.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.List; import java.util.List;
public class MonitoringDAOImpl implements MonitoringDAO { public class MonitoringDAOImpl implements MonitoringDAO {
@ -46,14 +44,17 @@ public class MonitoringDAOImpl implements MonitoringDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet generatedKeys = null; ResultSet generatedKeys = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
try { try {
conn = this.getConnection(); 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 = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, policyId); stmt.setInt(2, policyId);
stmt.setInt(3, 0); stmt.setInt(3, 0);
stmt.setTimestamp(4, currentTimestamp);
stmt.setInt(5, 0);
stmt.executeUpdate(); stmt.executeUpdate();
generatedKeys = stmt.getGeneratedKeys(); generatedKeys = stmt.getGeneratedKeys();
@ -78,12 +79,15 @@ public class MonitoringDAOImpl implements MonitoringDAO {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime());
try { try {
conn = this.getConnection(); 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 = conn.prepareStatement(query);
stmt.setInt(1, 1); stmt.setInt(1, 1);
stmt.setInt(2, deviceId); stmt.setTimestamp(2, currentTimestamp);
stmt.setInt(3, deviceId);
stmt.executeUpdate(); stmt.executeUpdate();
@ -105,8 +109,8 @@ public class MonitoringDAOImpl implements MonitoringDAO {
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " + String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS) " +
"VALUES" + "VALUES (?, ?, ?) ";
" (?, ?, ?) ";
stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
for (ComplianceFeature feature : complianceFeatures) { for (ComplianceFeature feature : complianceFeatures) {
stmt.setInt(1, policyComplianceStatusId); stmt.setInt(1, policyComplianceStatusId);
@ -145,6 +149,10 @@ public class MonitoringDAOImpl implements MonitoringDAO {
complianceData.setDeviceId(resultSet.getInt("DEVICE_ID")); complianceData.setDeviceId(resultSet.getInt("DEVICE_ID"));
complianceData.setPolicyId(resultSet.getInt("POLICY_ID")); complianceData.setPolicyId(resultSet.getInt("POLICY_ID"));
complianceData.setStatus(resultSet.getBoolean("STATUS")); 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; return complianceData;
@ -158,6 +166,48 @@ public class MonitoringDAOImpl implements MonitoringDAO {
} }
} }
@Override
public List<ComplianceData> getCompliance(List<Integer> deviceIds) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
List<ComplianceData> 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 @Override
public List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws public List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws
MonitoringDAOException { MonitoringDAOException {
@ -217,9 +267,68 @@ public class MonitoringDAOImpl implements MonitoringDAO {
@Override @Override
public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { 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<Integer> 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 { private Connection getConnection() throws MonitoringDAOException {
try { try {
@ -238,4 +347,14 @@ public class MonitoringDAOImpl implements MonitoringDAO {
log.warn("Unable to close the database connection."); log.warn("Unable to close the database connection.");
} }
} }
private String makeString(List<Integer> values) {
StringBuilder buff = new StringBuilder();
for (int value : values) {
buff.append(value).append(",");
}
buff.deleteCharAt(buff.length() - 1);
return buff.toString();
}
} }

@ -23,15 +23,13 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; 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.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; 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.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.PolicyOperation;
import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; 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.Policy;
import org.wso2.carbon.policy.mgt.common.ProfileFeature; import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; 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.ComplianceFeature;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; 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.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.PolicyManagementConstants;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -51,50 +46,58 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
private static final Log log = LogFactory.getLog(ComplianceDecisionPointImpl.class); 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 @Override
public String getNoneComplianceRule(Policy policy) throws PolicyComplianceException { public String getNoneComplianceRule(Policy policy) throws PolicyComplianceException {
return policy.getCompliance(); return policy.getCompliance();
} }
@Override @Override
public void setDeviceAsUnreachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public void setDevicesAsUnreachable(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException {
try { try {
int tenantId = PolicyManagerUtil.getTenantId(); DeviceManagementProviderService service = this.getDeviceManagementProviderService();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), Device device = service.getDevice(deviceIdentifier);
EnrolmentInfo.Status.UNREACHABLE, tenantId); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(),
EnrolmentInfo.Status.UNREACHABLE);
} catch (DeviceManagementDAOException e) { }
String msg = "Error occurred while setting the device as unreachable for " + } catch (DeviceManagementException e) {
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); String msg = "Error occurred while setting the device as unreachable";
log.error(msg, e); log.error(msg, e);
throw new PolicyComplianceException(msg, e); throw new PolicyComplianceException(msg, e);
} }
} }
@Override
public void setDevicesAsUnreachableWith(List<Device> 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 @Override
public void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { 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 " + String msg = "Error occurred while setting the device as reachable for " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); deviceIdentifier.getId() + " - " + deviceIdentifier.getType();
log.error(msg, e); log.error(msg, e);
@ -167,12 +170,12 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
public void markDeviceAsNoneCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public void markDeviceAsNoneCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { try {
int tenantId = PolicyManagerUtil.getTenantId(); DeviceManagementProviderService service = this.getDeviceManagementProviderService();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = service.getDevice(deviceIdentifier);
enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(),
EnrolmentInfo.Status.BLOCKED, tenantId); EnrolmentInfo.Status.BLOCKED);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while marking device as none compliance " + deviceIdentifier.getId() + " - " + String msg = "Error occurred while marking device as none compliance " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType(); deviceIdentifier.getType();
log.error(msg, e); log.error(msg, e);
@ -184,16 +187,15 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
public void markDeviceAsCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public void markDeviceAsCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { try {
int tenantId = PolicyManagerUtil.getTenantId(); DeviceManagementProviderService service = this.getDeviceManagementProviderService();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = service.getDevice(deviceIdentifier);
enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(),
EnrolmentInfo.Status.ACTIVE, tenantId); EnrolmentInfo.Status.ACTIVE);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while marking device as compliance " + deviceIdentifier.getId() + " - " + String msg = "Error occurred while marking device as compliance " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType(); deviceIdentifier.getType();
log.error(msg, e); 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 { public void deactivateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { 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() + " - " + String msg = "Error occurred while deactivating the device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType(); deviceIdentifier.getType();
log.error(msg, e); log.error(msg, e);
@ -219,12 +222,12 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
public void activateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { public void activateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
try { try {
int tenantId = PolicyManagerUtil.getTenantId(); DeviceManagementProviderService service = this.getDeviceManagementProviderService();
Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); Device device = service.getDevice(deviceIdentifier);
enrolmentDAO.setStatus(device.getId(), device.getEnrolmentInfo().getOwner(), service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(),
EnrolmentInfo.Status.ACTIVE, tenantId); EnrolmentInfo.Status.ACTIVE);
} catch (DeviceManagementDAOException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while activating the device " + deviceIdentifier.getId() + " - " + String msg = "Error occurred while activating the device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType(); deviceIdentifier.getType();
log.error(msg, e); log.error(msg, e);
@ -257,4 +260,8 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
this.markDeviceAsNoneCompliance(deviceIdentifier); this.markDeviceAsNoneCompliance(deviceIdentifier);
} }
} }
private DeviceManagementProviderService getDeviceManagementProviderService() {
return PolicyManagementDataHolder.getInstance().getDeviceManagementService();
}
} }

@ -19,6 +19,7 @@
package org.wso2.carbon.policy.mgt.core.internal; package org.wso2.carbon.policy.mgt.core.internal;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
@ -35,6 +36,7 @@ public class PolicyManagementDataHolder {
private PolicyInformationPoint policyInformationPoint; private PolicyInformationPoint policyInformationPoint;
private DeviceManagementProviderService deviceManagementService; private DeviceManagementProviderService deviceManagementService;
private Map<String, PolicyMonitoringService> policyMonitoringServiceMap; private Map<String, PolicyMonitoringService> policyMonitoringServiceMap;
private TaskService taskService;
private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder();
@ -99,4 +101,12 @@ public class PolicyManagementDataHolder {
public void unsetPolicyMonitoringService(String deviceType) { public void unsetPolicyMonitoringService(String deviceType) {
this.policyMonitoringServiceMap.remove(deviceType); this.policyMonitoringServiceMap.remove(deviceType);
} }
public TaskService getTaskService() {
return taskService;
}
public void setTaskService(TaskService taskService) {
this.taskService = taskService;
}
} }

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.service.component.ComponentContext; import org.osgi.service.component.ComponentContext;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; 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.PolicyEvaluationPoint;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
@ -58,6 +59,12 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic" * policy="dynamic"
* bind="setPolicyMonitoringService" * bind="setPolicyMonitoringService"
* unbind="unsetPolicyMonitoringService" * 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") @SuppressWarnings("unused")
public class PolicyManagementServiceComponent { public class PolicyManagementServiceComponent {
@ -131,7 +138,7 @@ public class PolicyManagementServiceComponent {
protected void unsetPEPService(PolicyEvaluationPoint pepService) { protected void unsetPEPService(PolicyEvaluationPoint pepService) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Unsetting Policy Information Service"); log.debug("Removing Policy Information Service");
} }
PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null); PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null);
} }
@ -145,7 +152,7 @@ public class PolicyManagementServiceComponent {
protected void unsetDeviceManagementService(DeviceManagementProviderService deviceManagementService) { protected void unsetDeviceManagementService(DeviceManagementProviderService deviceManagementService) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Unsetting Device Management Service"); log.debug("Removing Device Management Service");
} }
PolicyManagementDataHolder.getInstance().setDeviceManagementService(null); PolicyManagementDataHolder.getInstance().setDeviceManagementService(null);
} }
@ -161,10 +168,24 @@ public class PolicyManagementServiceComponent {
protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) {
if (log.isDebugEnabled()) { 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 // TODO: FIX THE device type by taking from properties
PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(""); 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);
}
} }

@ -19,6 +19,7 @@
package org.wso2.carbon.policy.mgt.core.mgt; 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.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; 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.ComplianceFeature;
@ -36,4 +37,6 @@ public interface MonitoringManager {
ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException;
} }

@ -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.mgt.MonitoringManager;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.util.List; import java.util.*;
public class MonitoringManagerImpl implements MonitoringManager { public class MonitoringManagerImpl implements MonitoringManager {
@ -186,4 +186,87 @@ public class MonitoringManagerImpl implements MonitoringManager {
} }
return complianceData; return complianceData;
} }
@Override
public void addMonitoringOperation(List<Device> devices) throws PolicyComplianceException {
try {
ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl();
int tenantId = PolicyManagerUtil.getTenantId();
Map<Integer, Device> deviceIds = new HashMap<>();
for (Device device : devices) {
deviceIds.put(device.getId(), device);
}
List<ComplianceData> complianceDatas = monitoringDAO.getCompliance(new ArrayList<>(deviceIds.keySet()));
Map<Integer, Device> deviceIdsToAddOperation = new HashMap<>();
Map<Integer, Device> deviceIdsWithExistingOperation = new HashMap<>();
Map<Integer, Device> inactiveDeviceIds = new HashMap<>();
Map<Integer, ComplianceData> 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<Device> devices) throws PolicyComplianceException {
}
private List<DeviceIdentifier> getDeviceIdentifiersFromDevices(List<Device> devices) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
for (Device device : devices) {
DeviceIdentifier identifier = new DeviceIdentifier();
identifier.setId(device.getDeviceIdentifier());
identifier.setType(device.getType());
deviceIdentifiers.add(identifier);
}
return deviceIdentifiers;
}
} }

@ -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.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.Feature; 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.ComplianceData;
import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; 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.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.PolicyManagerService;
import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl;
import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService;
import java.util.List; import java.util.List;
@ -112,6 +106,11 @@ public class PolicyManagementService implements PolicyManagerService {
return policyManagerService.getPEP(); return policyManagerService.getPEP();
} }
@Override
public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException {
return policyManagerService.getTaskScheduleService();
}
@Override @Override
public int getPolicyCount() throws PolicyManagementException { public int getPolicyCount() throws PolicyManagementException {
return policyManagerService.getPolicyCount(); return policyManagerService.getPolicyCount();

@ -19,6 +19,8 @@
package org.wso2.carbon.policy.mgt.core.task; 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.common.Device;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; 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.ntask.core.Task;
import org.wso2.carbon.policy.mgt.common.spi.PolicyMonitoringService; 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.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.List;
import java.util.Map; import java.util.Map;
@ -34,10 +38,14 @@ import java.util.Map;
public class MonitoringTask implements Task { public class MonitoringTask implements Task {
private DeviceTypeDAO deviceTypeDAO; private DeviceTypeDAO deviceTypeDAO;
private static Log log = LogFactory.getLog(MonitoringTask.class);
Map<String, String> properties;
@Override @Override
public void setProperties(Map<String, String> map) { public void setProperties(Map<String, String> map) {
this.properties = map;
} }
@Override @Override
@ -47,23 +55,34 @@ public class MonitoringTask implements Task {
@Override @Override
public void execute() { public void execute() {
try {
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
if(log.isDebugEnabled()) {
log.debug("Monitoring task started to run.");
}
try {
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManagementDataHolder.getInstance().getDeviceManagementService();
MonitoringManager monitoringManager = new MonitoringManagerImpl();
for (DeviceType deviceType : deviceTypes) { for (DeviceType deviceType : deviceTypes) {
PolicyMonitoringService monitoringService = PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName());
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); List<Device> 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);
} }
} }

@ -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;
}

@ -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<String, String> 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<String, String> 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);
}
}
}

@ -24,10 +24,17 @@ public final class PolicyManagementConstants {
public static final String ANY = "ANY"; public static final String ANY = "ANY";
public static final String POLICY_BUNDLE = "POLICY_BUNDLE"; 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 MONITOR = "MONITOR";
public static final String ENFORCE = "ENFORCE"; public static final String ENFORCE = "ENFORCE";
public static final String WARN = "WARN"; public static final String WARN = "WARN";
public static final String BLOCK = "BLOCK"; 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";
} }

Loading…
Cancel
Save