diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java index fe38dbbd95..deb10035bc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/policy/PolicyConfiguration.java @@ -24,34 +24,64 @@ import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "PolicyConfiguration") public class PolicyConfiguration { - private String monitoringClass; - private int maxRetries; - private int minRetriesToMarkUnreachable; - - @XmlElement(name = "monitoringClass", required = true) - public String getMonitoringClass() { - return monitoringClass; - } - - public void setMonitoringClass(String monitoringClass) { - this.monitoringClass = monitoringClass; - } - - @XmlElement(name = "maxRetries", required = true) - public int getMaxRetries() { - return maxRetries; - } - - public void setMaxRetries(int maxRetries) { - this.maxRetries = maxRetries; - } - - @XmlElement(name = "minRetriesToMarkUnreachable", required = true) - public int getMinRetriesToMarkUnreachable() { - return minRetriesToMarkUnreachable; - } - - public void setMinRetriesToMarkUnreachable(int minRetriesToMarkUnreachable) { - this.minRetriesToMarkUnreachable = minRetriesToMarkUnreachable; - } + private String monitoringClass; + private boolean monitoringEnable; + private int monitoringFrequency; + private int maxRetries; + private int minRetriesToMarkUnreachable; + private int minRetriesToMarkInactive; + + @XmlElement(name = "monitoringClass", required = true) + public String getMonitoringClass() { + return monitoringClass; + } + + public void setMonitoringClass(String monitoringClass) { + this.monitoringClass = monitoringClass; + } + + @XmlElement(name = "maxRetries", required = true) + public int getMaxRetries() { + return maxRetries; + } + + public void setMaxRetries(int maxRetries) { + this.maxRetries = maxRetries; + } + + @XmlElement(name = "minRetriesToMarkUnreachable", required = true) + public int getMinRetriesToMarkUnreachable() { + return minRetriesToMarkUnreachable; + } + + public void setMinRetriesToMarkUnreachable(int minRetriesToMarkUnreachable) { + this.minRetriesToMarkUnreachable = minRetriesToMarkUnreachable; + } + + @XmlElement(name = "monitoringEnable", required = true) + public boolean getMonitoringEnable() { + return monitoringEnable; + } + + public void setMonitoringEnable(boolean monitoringEnable) { + this.monitoringEnable = monitoringEnable; + } + + @XmlElement(name = "minRetriesToMarkInactive", required = true) + public int getMinRetriesToMarkInactive() { + return minRetriesToMarkInactive; + } + + public void setMinRetriesToMarkInactive(int minRetriesToMarkInactive) { + this.minRetriesToMarkInactive = minRetriesToMarkInactive; + } + + @XmlElement(name = "monitoringFrequency", required = true) + public int getMonitoringFrequency() { + return monitoringFrequency; + } + + public void setMonitoringFrequency(int monitoringFrequency) { + this.monitoringFrequency = monitoringFrequency; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index c6680d1a74..393c869b82 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -127,11 +127,16 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { String taskName = PolicyManagementConstants.DELEGATION_TASK_NAME + "_" + String.valueOf(tenantId); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); + if(!taskManager.isTaskScheduled(taskName)) { - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.DELEGATION_TASK_CLAZZ, properties, triggerInfo); + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); + } else { + throw new PolicyManagementException("There is a task already running for policy changes. Please try to apply " + + "changes after few minutes."); + } } catch (TaskException e) { String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext. 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 676595230b..1b6f10c162 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 @@ -21,6 +21,8 @@ package org.wso2.carbon.policy.mgt.core.internal; 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.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; 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; @@ -84,8 +86,12 @@ public class PolicyManagementServiceComponent { componentContext.getBundleContext().registerService( PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); - TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); - taskScheduleService.startTask(30000); + PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + getDeviceManagementConfigRepository().getPolicyConfiguration(); + if(policyConfiguration.getMonitoringEnable()) { + TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); + taskScheduleService.startTask(policyConfiguration.getMonitoringFrequency()); + } } catch (Throwable t) { log.error("Error occurred while initializing the Policy management core.", t); @@ -165,7 +171,7 @@ public class PolicyManagementServiceComponent { protected void setPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { if (log.isDebugEnabled()) { - log.debug("Setting Policy Monitoring Service"); + log.debug("Setting Policy Monitoring Service for " + policyMonitoringService.getType()); } // TODO: FIX THE device type by taking from properties PolicyManagementDataHolder.getInstance().setPolicyMonitoringService(policyMonitoringService.getType(), @@ -174,7 +180,7 @@ public class PolicyManagementServiceComponent { protected void unsetPolicyMonitoringService(PolicyMonitoringService policyMonitoringService) { if (log.isDebugEnabled()) { - log.debug("Removing the Policy Monitoring Service"); + log.debug("Removing the Policy Monitoring Service for " + policyMonitoringService.getType()); } // TODO: FIX THE device type by taking from properties PolicyManagementDataHolder.getInstance().unsetPolicyMonitoringService(policyMonitoringService.getType()); 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 9da602f261..883d49237d 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 @@ -21,6 +21,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.device.mgt.core.dto.DeviceType; 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; @@ -39,4 +40,6 @@ public interface MonitoringManager { void addMonitoringOperation(List devices) throws PolicyComplianceException; + List getDeviceTypes() 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 b0ba65a2a8..89f96dea2c 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 @@ -26,8 +26,13 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfigRepository; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; 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.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; @@ -54,18 +59,24 @@ import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { private PolicyDAO policyDAO; - private DeviceDAO deviceDAO; +// private DeviceDAO deviceDAO; + private DeviceTypeDAO deviceTypeDAO; private MonitoringDAO monitoringDAO; private ComplianceDecisionPoint complianceDecisionPoint; + private PolicyConfiguration policyConfiguration; private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final String OPERATION_MONITOR = "MONITOR"; public MonitoringManagerImpl() { this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); - this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); +// this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); + this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); this.monitoringDAO = PolicyManagementDAOFactory.getMonitoringDAO(); this.complianceDecisionPoint = new ComplianceDecisionPointImpl(); + this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + getDeviceManagementConfigRepository().getPolicyConfiguration(); + } @Override @@ -242,6 +253,7 @@ public class MonitoringManagerImpl implements MonitoringManager { Map deviceIdsToAddOperation = new HashMap<>(); Map deviceIdsWithExistingOperation = new HashMap<>(); Map inactiveDeviceIds = new HashMap<>(); + Map deviceToMarkUnreachable = new HashMap<>(); Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); Map tempMap = new HashMap<>(); @@ -258,8 +270,12 @@ public class MonitoringManagerImpl implements MonitoringManager { } else { deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), deviceIds.get(complianceData.getDeviceId())); + if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkUnreachable()) { + deviceToMarkUnreachable.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); + } } - if (complianceData.getAttempts() >= 20) { + if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkInactive()) { inactiveDeviceIds.put(complianceData.getDeviceId(), deviceIds.get(complianceData.getDeviceId())); } @@ -288,9 +304,6 @@ public class MonitoringManagerImpl implements MonitoringManager { if (!deviceIdsWithExistingOperation.isEmpty()) { monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsWithExistingOperation.keySet()), false); - //TODO: Add attempts. This has to be fixed in the get pending operation tables too. This will be -// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( -// new ArrayList<>(deviceIdsWithExistingOperation.values()))); } PolicyManagementDAOFactory.commitTransaction(); @@ -312,6 +325,29 @@ public class MonitoringManagerImpl implements MonitoringManager { } } + // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current implementation + // we are not able to do so. + +// if(!deviceToMarkUnreachable.isEmpty()) { +// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( +// new ArrayList<>(deviceToMarkUnreachable.values()))); +// } + + } + + @Override + public List getDeviceTypes() throws PolicyComplianceException { + + List deviceTypes = new ArrayList<>(); + try { + DeviceManagementDAOFactory.openConnection(); + deviceTypes = deviceTypeDAO.getDeviceTypes(); + } catch (Exception e) { + log.error("Error occurred while getting the device types.", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return deviceTypes; } private void addMonitoringOperationsToDatabase(List devices) @@ -325,8 +361,7 @@ public class MonitoringManagerImpl implements MonitoringManager { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); service.addOperation(monitoringOperation, deviceIdentifiers); -// PolicyManagementDataHolder.getInstance().getDeviceManagementService(). -// addOperation(monitoringOperation, deviceIdentifiers); + } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 9c879e5493..89589200e1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -188,8 +188,19 @@ public class PolicyManagerImpl implements PolicyManager { public boolean updatePolicyPriorities(List policies) throws PolicyManagementException { boolean bool; try { + List existingPolicies = this.getPolicies(); PolicyManagementDAOFactory.beginTransaction(); bool = policyDAO.updatePolicyPriorities(policies); + + // This logic is added because ui sends only policy id and priority to update priorities. + + for (Policy policy : policies) { + for(Policy exPolicy: existingPolicies) { + if(policy.getId() == exPolicy.getId()) { + policy.setProfile(exPolicy.getProfile()); + } + } + } policyDAO.recordUpdatedPolicies(policies); PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { 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 1c4492e239..b8146e26e2 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 @@ -29,6 +29,7 @@ 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.monitor.PolicyComplianceException; 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; @@ -63,14 +64,13 @@ public class MonitoringTask implements Task { log.debug("Monitoring task started to run."); } + MonitoringManager monitoringManager = new MonitoringManagerImpl(); + List deviceTypes = new ArrayList<>(); try { - DeviceManagementDAOFactory.openConnection(); - deviceTypes = deviceTypeDAO.getDeviceTypes(); - } catch (Exception e) { - log.error("Error occurred while getting the device types.", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); + deviceTypes = monitoringManager.getDeviceTypes(); + } catch (PolicyComplianceException e) { + log.error("Error occurred while getting the device types."); } if (!deviceTypes.isEmpty()) { @@ -78,7 +78,6 @@ public class MonitoringTask implements Task { DeviceManagementProviderService deviceManagementProviderService = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - MonitoringManager monitoringManager = new MonitoringManagerImpl(); for (DeviceType deviceType : deviceTypes) { PolicyMonitoringService monitoringService = 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 index a8121f99ac..d12ce10d03 100644 --- 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 @@ -22,6 +22,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.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.ntask.core.TaskInfo; import org.wso2.carbon.ntask.core.TaskManager; @@ -37,45 +39,56 @@ import java.util.Map; public class TaskScheduleServiceImpl implements TaskScheduleService { private static Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); + private PolicyConfiguration policyConfiguration; + + + public TaskScheduleServiceImpl() { + this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + getDeviceManagementConfigRepository().getPolicyConfiguration(); + } @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 = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE); - if (log.isDebugEnabled()) { - log.debug("Monitoring task is started for the tenant id " + tenantId); + if (policyConfiguration.getMonitoringEnable()) { + + if (monitoringFrequency <= 0) { + throw new PolicyMonitoringTaskException("Time interval cannot be 0 or less than 0."); } + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); + taskService.registerTaskType(PolicyManagementConstants.MONITORING_TASK_TYPE); - TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); + if (log.isDebugEnabled()) { + log.debug("Monitoring task is started for the tenant id " + tenantId); + } - TriggerInfo triggerInfo = new TriggerInfo(); + TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); - triggerInfo.setIntervalMillis(monitoringFrequency); - triggerInfo.setRepeatCount(-1); + TriggerInfo triggerInfo = new TriggerInfo(); + triggerInfo.setIntervalMillis(monitoringFrequency); + triggerInfo.setRepeatCount(-1); - Map properties = new HashMap<>(); - properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); + Map properties = new HashMap<>(); + properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); - String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); + String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); - TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, + properties, triggerInfo); - taskManager.registerTask(taskInfo); - taskManager.rescheduleTask(taskInfo.getName()); + taskManager.registerTask(taskInfo); + taskManager.rescheduleTask(taskInfo.getName()); - } catch (TaskException e) { - String msg = "Error occurred while creating the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(); - log.error(msg, e); - throw new PolicyMonitoringTaskException(msg, e); + } catch (TaskException e) { + throw new PolicyMonitoringTaskException("Error occurred while creating the task for tenant " + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); + } + } else { + throw new PolicyMonitoringTaskException("Policy monitoring is not enabled in the cdm-config.xml."); } @@ -90,10 +103,8 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); } catch (TaskException e) { - String msg = "Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(); - log.error(msg, e); - throw new PolicyMonitoringTaskException(msg, e); + throw new PolicyMonitoringTaskException("Error occurred while deleting the task for tenant " + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); } } @@ -103,29 +114,25 @@ public class TaskScheduleServiceImpl implements TaskScheduleService { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); - TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_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.MONITORING_TASK_CLAZZ, properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, + triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); } catch (TaskException e) { - String msg = "Error occurred while updating the task for tenant " + PrivilegedCarbonContext. - getThreadLocalCarbonContext().getTenantId(); - log.error(msg, e); - throw new PolicyMonitoringTaskException(msg, e); + throw new PolicyMonitoringTaskException("Error occurred while updating the task for tenant " + PrivilegedCarbonContext. + getThreadLocalCarbonContext().getTenantId(), e); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java index 6b15d82d2e..a01ff15567 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/BasePolicyManagementDAOTest.java @@ -26,6 +26,9 @@ import org.testng.annotations.BeforeSuite; import org.w3c.dom.Document; import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -61,6 +64,7 @@ public abstract class BasePolicyManagementDAOTest { this.initSQLScript(); this.initialize(); this.initiatePrivilegedCaronContext(); + DeviceConfigurationManager.getInstance().initConfig(); } public void initialize() throws TaskException { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml new file mode 100644 index 0000000000..0f5861cc1d --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml @@ -0,0 +1,48 @@ + + + + + + + + jdbc/DM_DS + + + + 8 + 100 + 20 + 1000 + + + https://localhost:9443 + admin + admin + + + org.wso2.carbon.policy.mgt + true + 60000 + 5 + 8 + 20 + + + + diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/notification-messages.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/notification-messages.xml new file mode 100644 index 0000000000..9b13c5d808 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/notification-messages.xml @@ -0,0 +1,39 @@ + + + Enroll your Device with WSO2 MDM +
Dear {first-name},
+ + You have been registered to the WSO2 MDM. Below is the link to enroll. + + {downloadUrl} + + Best Regards, + + + WSO2 MDM Team. + + + http://www.wso2.com + +
+ + Enroll your Device with WSO2 MDM +
Dear {first-name},
+ +You have been registered to WSO2 MDM with following credentials. +Username: {user-name} +Password: {password} +Below is the link to enroll. + + {downloadUrl} + + Best Regards, + + + WSO2 MDM Team. + + + http://www.wso2.com + +
+
\ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 0efa651a73..0f5861cc1d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -37,8 +37,11 @@ org.wso2.carbon.policy.mgt + true + 60000 5 8 + 20