Adding the momitoring task partialy

revert-70aa11f8
geethkokila 9 years ago
parent a886430005
commit d048913e06

@ -19,6 +19,8 @@
package org.wso2.carbon.policy.mgt.common.monitor;
import org.wso2.carbon.policy.mgt.common.Policy;
import java.util.List;
public class ComplianceData {
@ -30,6 +32,13 @@ public class ComplianceData {
private boolean status;
private String message;
/**
* This parameter is to inform the policy core, weather related device type plugins does need the full policy or
* the part which is none compliance.
*/
private boolean completePolicy;
private Policy policy;
public int getId() {
return id;
}
@ -77,4 +86,20 @@ public class ComplianceData {
public void setMessage(String message) {
this.message = message;
}
public boolean isCompletePolicy() {
return completePolicy;
}
public void setCompletePolicy(boolean completePolicy) {
this.completePolicy = completePolicy;
}
public Policy getPolicy() {
return policy;
}
public void setPolicy(Policy policy) {
this.policy = policy;
}
}

@ -31,7 +31,8 @@ public interface ComplianceDecisionPoint {
void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
void reEnforcePolicy(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
void reEnforcePolicy(DeviceIdentifier deviceIdentifier, ComplianceData complianceData) throws
PolicyComplianceException;
void markDeviceAsNoneCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
@ -41,7 +42,7 @@ public interface ComplianceDecisionPoint {
void activateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException;
void validateDevicePolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy) throws
void validateDevicePolicyCompliance(DeviceIdentifier deviceIdentifier, ComplianceData complianceData) throws
PolicyComplianceException;

@ -19,10 +19,11 @@
package org.wso2.carbon.policy.mgt.common;
import java.io.Serializable;
import org.wso2.carbon.device.mgt.common.Feature;
public class ProfileFeature implements Serializable {
private static final long serialVersionUID = 19981018L;
private int id;
private String featureCode;
private int profileId;

@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.common.spi;
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;
import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException;
import org.wso2.carbon.policy.mgt.common.Policy;
@ -31,6 +32,6 @@ public interface PolicyMonitoringService {
void notifyDevices(List<Device> devices) throws PolicyComplianceException;
List<ComplianceFeature> checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response)
ComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response)
throws PolicyComplianceException;
}

@ -37,5 +37,9 @@ public interface MonitoringDAO {
List<ComplianceFeature> getNoneComplianceFeatures(int policyComplianceStatusId) throws MonitoringDAOException;
void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException;
void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException;
void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException;
}

@ -80,9 +80,10 @@ public class MonitoringDAOImpl implements MonitoringDAO {
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?";
String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET STATUS = ? WHERE DEVICE_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(1, 1);
stmt.setInt(2, deviceId);
stmt.executeUpdate();
@ -193,15 +194,15 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
@Override
public void deleteNoneComplianceData(int deviceId) throws MonitoringDAOException {
public void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException {
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
String query = "DELETE FROM DM_POLICY_COMPLIANCE_STATUS WHERE DEVICE_ID = ?";
String query = "DELETE FROM DM_POLICY_COMPLIANCE_FEATURES WHERE COMPLIANCE_STATUS_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(1, policyComplianceStatusId);
stmt.executeUpdate();
} catch (SQLException e) {
@ -214,6 +215,11 @@ public class MonitoringDAOImpl implements MonitoringDAO {
}
@Override
public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException {
}
private Connection getConnection() throws MonitoringDAOException {
try {

@ -21,15 +21,31 @@ package org.wso2.carbon.policy.mgt.core.impl;
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.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.policy.mgt.common.Policy;
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.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;
public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
@ -54,41 +70,191 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint {
@Override
public void setDeviceAsUnreachable(DeviceIdentifier deviceIdentifier) 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();
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) {
String msg = "Error occurred while setting the device as reachable for " +
deviceIdentifier.getId() + " - " + deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
public void reEnforcePolicy(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException {
public void reEnforcePolicy(DeviceIdentifier deviceIdentifier, ComplianceData complianceData) throws
PolicyComplianceException {
try {
Policy policy = complianceData.getPolicy();
if (policy != null) {
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<DeviceIdentifier>();
deviceIdentifiers.add(deviceIdentifier);
List<ProfileOperation> profileOperationList = new ArrayList<ProfileOperation>();
PolicyOperation policyOperation = new PolicyOperation();
policyOperation.setEnabled(true);
policyOperation.setType(Operation.Type.POLICY);
policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE);
if (complianceData.isCompletePolicy()) {
List<ProfileFeature> effectiveFeatures = policy.getProfile().getProfileFeaturesList();
for (ProfileFeature feature : effectiveFeatures) {
ProfileOperation profileOperation = new ProfileOperation();
profileOperation.setCode(feature.getFeatureCode());
profileOperation.setEnabled(true);
profileOperation.setStatus(Operation.Status.PENDING);
profileOperation.setType(Operation.Type.PROFILE);
profileOperation.setPayLoad(feature.getContent());
profileOperationList.add(profileOperation);
}
} else {
List<ComplianceFeature> noneComplianceFeatures = complianceData.getComplianceFeatures();
for (ComplianceFeature feature : noneComplianceFeatures) {
ProfileOperation profileOperation = new ProfileOperation();
profileOperation.setCode(feature.getFeatureCode());
profileOperation.setEnabled(true);
profileOperation.setStatus(Operation.Status.PENDING);
profileOperation.setType(Operation.Type.PROFILE);
profileOperation.setPayLoad(feature.getFeature().getContent());
profileOperationList.add(profileOperation);
}
}
policyOperation.setProfileOperations(profileOperationList);
policyOperation.setPayLoad(policyOperation.getProfileOperations());
PolicyManagementDataHolder.getInstance().getDeviceManagementService().
addOperation(policyOperation, deviceIdentifiers);
}
} catch (OperationManagementException e) {
String msg = "Error occurred while re-enforcing the policy to device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
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);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while marking device as none compliance " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
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);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while marking device as compliance " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
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) {
String msg = "Error occurred while deactivating the device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
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);
} catch (DeviceManagementDAOException e) {
String msg = "Error occurred while activating the device " + deviceIdentifier.getId() + " - " +
deviceIdentifier.getType();
log.error(msg, e);
throw new PolicyComplianceException(msg, e);
}
}
@Override
public void validateDevicePolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy) throws
public void validateDevicePolicyCompliance(DeviceIdentifier deviceIdentifier, ComplianceData complianceData) throws
PolicyComplianceException {
Policy policy = complianceData.getPolicy();
String compliance = this.getNoneComplianceRule(policy);
if (compliance.equals("")) {
String msg = "Compliance rule is empty for the policy " + policy.getPolicyName() + ". Therefore " +
"Monitoring Engine cannot run.";
throw new PolicyComplianceException(msg);
}
if (PolicyManagementConstants.ENFORCE.equalsIgnoreCase(compliance)) {
this.reEnforcePolicy(deviceIdentifier, complianceData);
}
if (PolicyManagementConstants.WARN.equalsIgnoreCase(compliance)) {
this.markDeviceAsNoneCompliance(deviceIdentifier);
}
if (PolicyManagementConstants.BLOCK.equalsIgnoreCase(compliance)) {
this.markDeviceAsNoneCompliance(deviceIdentifier);
}
}
}

@ -27,6 +27,7 @@ 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.policy.mgt.common.monitor.ComplianceData;
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.common.Policy;
@ -37,6 +38,7 @@ import org.wso2.carbon.policy.mgt.core.dao.MonitoringDAOException;
import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory;
import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException;
import org.wso2.carbon.policy.mgt.core.impl.ComplianceDecisionPointImpl;
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;
@ -48,6 +50,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
private PolicyDAO policyDAO;
private DeviceDAO deviceDAO;
private MonitoringDAO monitoringDAO;
private ComplianceDecisionPoint complianceDecisionPoint;
private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class);
@ -55,6 +58,7 @@ public class MonitoringManagerImpl implements MonitoringManager {
this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO();
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO();
this.complianceDecisionPoint = new ComplianceDecisionPointImpl();
}
@Override
@ -71,13 +75,15 @@ public class MonitoringManagerImpl implements MonitoringManager {
PolicyMonitoringService monitoringService = PolicyManagementDataHolder.getInstance().
getPolicyMonitoringService(deviceIdentifier.getType());
complianceFeatures = monitoringService.checkPolicyCompliance(deviceIdentifier,
ComplianceData complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier,
policy, deviceResponse);
complianceData.setPolicy(policy);
complianceFeatures = complianceData.getComplianceFeatures();
if (!complianceFeatures.isEmpty()) {
int complianceId = monitoringDAO.setDeviceAsNoneCompliance(device.getId(), policy.getId());
monitoringDAO.addNoneComplianceFeatures(complianceId, device.getId(), complianceFeatures);
complianceDecisionPoint.validateDevicePolicyCompliance(deviceIdentifier, complianceData);
List<ProfileFeature> profileFeatures = policy.getProfile().getProfileFeaturesList();
for (ComplianceFeature compFeature : complianceFeatures) {
for (ProfileFeature profFeature : profileFeatures) {

@ -19,12 +19,22 @@
package org.wso2.carbon.policy.mgt.core.task;
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;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
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 java.util.List;
import java.util.Map;
public class MonitoringTask implements Task {
private DeviceTypeDAO deviceTypeDAO;
@Override
public void setProperties(Map<String, String> map) {
@ -32,11 +42,29 @@ public class MonitoringTask implements Task {
@Override
public void init() {
deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO();
}
@Override
public void execute() {
try {
List<DeviceType> deviceTypes = deviceTypeDAO.getDeviceTypes();
DeviceManagementProviderService deviceManagementProviderService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService();
for (DeviceType deviceType : deviceTypes) {
PolicyMonitoringService monitoringService =
PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName());
List<Device> devices = deviceManagementProviderService.getAllDevices(deviceType.getName());
monitoringService.notifyDevices(devices);
}
} catch (Exception e) {
}
}
}

@ -24,4 +24,10 @@ public final class PolicyManagementConstants {
public static final String ANY = "ANY";
public static final String POLICY_BUNDLE = "POLICY_BUNDLE";
public static final String MONITOR = "MONITOR";
public static final String ENFORCE = "ENFORCE";
public static final String WARN = "WARN";
public static final String BLOCK = "BLOCK";
}

Loading…
Cancel
Save