Merge branch 'fix-881' into 'master'

Fix executing policy monitoring continuously

See merge request entgra/carbon-device-mgt!753
merge-requests/752/merge
Charitha Goonetilleke 4 years ago
commit cd578884e6

@ -52,6 +52,7 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
@Override @Override
public void setProperties(Map<String, String> properties) { public void setProperties(Map<String, String> properties) {
super.setProperties(properties);
deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE); deviceType = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE);
deviceTypeId = Integer.parseInt(properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE_ID)); deviceTypeId = Integer.parseInt(properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_TYPE_ID));
String deviceStatusTaskConfigStr = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_STATUS_TASK_CONFIG); String deviceStatusTaskConfigStr = properties.get(DeviceStatusTaskManagerServiceImpl.DEVICE_STATUS_TASK_CONFIG);

@ -39,28 +39,26 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.StartupOperationConfig; import org.wso2.carbon.device.mgt.common.StartupOperationConfig;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager;
import org.wso2.carbon.ntask.core.Task;
import org.wso2.carbon.user.api.UserStoreException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private static final Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class);
private String deviceType; private String deviceType;
private DeviceManagementProviderService deviceManagementProviderService; private DeviceManagementProviderService deviceManagementProviderService;
@Override @Override
public void setProperties(Map<String, String> map) { public void setProperties(Map<String, String> map) {
super.setProperties(map);
deviceType = map.get("DEVICE_TYPE"); deviceType = map.get("DEVICE_TYPE");
} }
@ -122,7 +120,7 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
//pass the configurations also from here, monitoring tasks //pass the configurations also from here, monitoring tasks
try { try {
if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) { if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) {
deviceTaskManager.addOperations(super.getTaskContext()); deviceTaskManager.addOperations(getTaskContext());
} }
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
log.error("Error occurred while trying to add the operations to " + log.error("Error occurred while trying to add the operations to " +

@ -26,12 +26,27 @@ import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.ntask.core.Task;
import java.util.Map;
public abstract class DynamicPartitionedScheduleTask implements Task { public abstract class DynamicPartitionedScheduleTask implements Task {
private static final Log log = LogFactory.getLog(DynamicPartitionedScheduleTask.class); private static final Log log = LogFactory.getLog(DynamicPartitionedScheduleTask.class);
private static DynamicTaskContext taskContext = null; private static DynamicTaskContext taskContext = null;
private Map<String, String> properties;
@Override
public void setProperties(Map<String, String> properties) {
this.properties = properties;
}
public String getProperty(String name) {
if (properties == null) {
return null;
}
return properties.get(name);
}
@Override @Override
public final void init() { public final void init() {
@ -97,11 +112,7 @@ public abstract class DynamicPartitionedScheduleTask implements Task {
} }
public static boolean isDynamicTaskEligible(){ public static boolean isDynamicTaskEligible(){
if(taskContext != null && taskContext.isPartitioningEnabled()) { return taskContext != null && taskContext.isPartitioningEnabled();
return true;
} else {
return false;
}
} }
} }

@ -21,8 +21,8 @@ package org.wso2.carbon.policy.mgt.core.enforcement;
import org.apache.commons.logging.Log; 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.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; 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.config.policy.PolicyConfiguration;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
@ -36,19 +36,13 @@ import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class DelegationTask extends DynamicPartitionedScheduleTask { public class DelegationTask extends DynamicPartitionedScheduleTask {
private static final Log log = LogFactory.getLog(DelegationTask.class); private static final Log log = LogFactory.getLog(DelegationTask.class);
private PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance() private final PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance()
.getDeviceManagementConfig().getPolicyConfiguration(); .getDeviceManagementConfig().getPolicyConfiguration();
@Override
public void setProperties(Map<String, String> map) {
}
@Override @Override
public void executeDynamicTask() { public void executeDynamicTask() {
try { try {
@ -70,10 +64,10 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
try { try {
devices = new ArrayList<>(); devices = new ArrayList<>();
toBeNotified = new ArrayList<>(); toBeNotified = new ArrayList<>();
if (super.isDynamicTaskEligible()) { if (isDynamicTaskEligible()) {
devices.addAll(service.getAllocatedDevices(deviceType, devices.addAll(service.getAllocatedDevices(deviceType,
super.getTaskContext().getActiveServerCount(), getTaskContext().getActiveServerCount(),
super.getTaskContext().getServerHashIndex())); getTaskContext().getServerHashIndex()));
} else { } else {
devices.addAll(service.getAllDevices(deviceType, false)); devices.addAll(service.getAllDevices(deviceType, false));
} }
@ -81,9 +75,9 @@ public class DelegationTask extends DynamicPartitionedScheduleTask {
if (device != null && device.getEnrolmentInfo() != null if (device != null && device.getEnrolmentInfo() != null
&& device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) { && device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) {
toBeNotified.add(device); toBeNotified.add(device);
} if (log.isDebugEnabled()) {
if (log.isDebugEnabled()) { log.debug("Adding policy operation to device : " + device.getDeviceIdentifier());
log.debug("Adding policy operation to device : " + device.getDeviceIdentifier()); }
} }
} }
if (!toBeNotified.isEmpty()) { if (!toBeNotified.isEmpty()) {

@ -24,33 +24,22 @@ import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager;
import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask; import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
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.util.PolicyManagementConstants;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
public class MonitoringTask extends DynamicPartitionedScheduleTask { public class MonitoringTask extends DynamicPartitionedScheduleTask {
private static final Log log = LogFactory.getLog(MonitoringTask.class); private static final Log log = LogFactory.getLog(MonitoringTask.class);
@Override private String tenant;
public void setProperties(Map<String, String> map) {
}
@Override
public void executeDynamicTask() {
if (log.isDebugEnabled()) {
log.debug("Monitoring task started to run.");
}
this.executeforAllTenants();
}
/** /**
* Check whether Device platform (ex: android) is exist in the cdm-config.xml file before adding a * Check whether Device platform (ex: android) is exist in the cdm-config.xml file before adding a
@ -66,30 +55,27 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
return policyMonitoringManager != null; return policyMonitoringManager != null;
} }
private void executeforAllTenants() { @Override
public void executeDynamicTask() {
tenant = getProperty(PolicyManagementConstants.TENANT_ID);
if (tenant == null) {
log.warn("Tenant id of the Monitoring Task got null");
return;
}
int tenantId = Integer.parseInt(tenant);
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Monitoring task started to run for all tenants."); log.debug("Monitoring task started to run for tenant: " + tenant);
}
if (MultitenantConstants.SUPER_TENANT_ID == tenantId) {
this.executeTask();
return;
} }
try { try {
DeviceManagementProviderService deviceManagementService = PolicyManagementDataHolder PrivilegedCarbonContext.startTenantFlow();
.getInstance().getDeviceManagementService(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
List<Integer> tenants = deviceManagementService.getDeviceEnrolledTenants(); this.executeTask();
for (Integer tenant : tenants) { } finally {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) { PrivilegedCarbonContext.endTenantFlow();
this.executeTask();
continue;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true);
this.executeTask();
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
} catch (DeviceManagementException e) {
log.error("Error occurred while trying to get the available tenants from device manager service ", e);
} }
} }
@ -105,7 +91,7 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
} }
} }
} catch (PolicyComplianceException e) { } catch (PolicyComplianceException e) {
log.error("Error occurred while getting the device types."); log.error("TID:[" + tenant + "] Error occurred while getting the device types.");
} }
if (!deviceTypes.isEmpty()) { if (!deviceTypes.isEmpty()) {
try { try {
@ -113,42 +99,42 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManagementDataHolder.getInstance().getDeviceManagementService();
for (String deviceType : configDeviceTypes) { for (String deviceType : configDeviceTypes) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Running task for device type : " + deviceType); log.debug("TID:[" + tenant + "] Running task for device type : " + deviceType);
} }
PolicyMonitoringManager monitoringService = PolicyMonitoringManager monitoringService =
PolicyManagementDataHolder.getInstance().getDeviceManagementService() PolicyManagementDataHolder.getInstance().getDeviceManagementService()
.getPolicyMonitoringManager(deviceType); .getPolicyMonitoringManager(deviceType);
List<Device> devices; List<Device> devices;
if(super.isDynamicTaskEligible()){ if(isDynamicTaskEligible()){
devices = deviceManagementProviderService.getAllocatedDevices(deviceType, devices = deviceManagementProviderService
super.getTaskContext().getActiveServerCount(), .getAllocatedDevices(deviceType, getTaskContext().getActiveServerCount(),
super.getTaskContext().getServerHashIndex()); getTaskContext().getServerHashIndex());
} else { } else {
devices = deviceManagementProviderService.getAllDevices(deviceType, false); devices = deviceManagementProviderService.getAllDevices(deviceType, false);
} }
if (monitoringService != null && !devices.isEmpty()) { if (monitoringService != null && !devices.isEmpty()) {
List<Device> notifiableDevices = new ArrayList<>(); List<Device> notifiableDevices = new ArrayList<>();
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Removing inactive and blocked devices from the list for the device type : " + log.debug("TID:[" + tenant + "] Removing inactive and blocked devices from " +
deviceType); "the list for the device type : " + deviceType);
} }
StringBuilder sb = new StringBuilder();
for (Device device : devices) { for (Device device : devices) {
EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus();
if (status.equals(EnrolmentInfo.Status.ACTIVE) || if (status.equals(EnrolmentInfo.Status.ACTIVE) ||
status.equals(EnrolmentInfo.Status.INACTIVE) ||
status.equals(EnrolmentInfo.Status.UNREACHABLE)) { status.equals(EnrolmentInfo.Status.UNREACHABLE)) {
notifiableDevices.add(device); notifiableDevices.add(device);
} if (log.isDebugEnabled()) {
if (log.isDebugEnabled()) { if (sb.length() > 0) {
log.debug("Adding monitoring operation to device : " + device.getDeviceIdentifier()); sb.append(", ");
}
sb.append(device.getDeviceIdentifier());
}
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Following '" + deviceType + "' devices selected to send the notification " + log.debug("TID:[" + tenant + "] Sending monitoring to '" + deviceType +
"for policy monitoring"); "' devices with ids [" + sb + "]");
for (Device device : notifiableDevices) {
log.debug(device.getDeviceIdentifier());
}
} }
if (!notifiableDevices.isEmpty()) { if (!notifiableDevices.isEmpty()) {
monitoringManager.addMonitoringOperation(deviceType, notifiableDevices); monitoringManager.addMonitoringOperation(deviceType, notifiableDevices);
@ -156,13 +142,14 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
} }
} }
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Monitoring task running completed."); log.debug("TID:[" + tenant + "] Monitoring task running completed.");
} }
} catch (Exception e) { } catch (Exception e) {
log.error("Error occurred while trying to run a task.", e); log.error("TID:[" + tenant + "] Error occurred while trying to run a task.", e);
} }
} else { } else {
log.info("No device types registered currently. So did not run the monitoring task."); log.info("TID:[" + tenant + "] No device types registered currently. " +
"So did not run the monitoring task.");
} }
} }
@ -170,4 +157,5 @@ public class MonitoringTask extends DynamicPartitionedScheduleTask {
protected void setup() { protected void setup() {
} }
} }

@ -39,7 +39,7 @@ import java.util.Map;
public class TaskScheduleServiceImpl implements TaskScheduleService { public class TaskScheduleServiceImpl implements TaskScheduleService {
private static final Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); private static final Log log = LogFactory.getLog(TaskScheduleServiceImpl.class);
private PolicyConfiguration policyConfiguration; private final PolicyConfiguration policyConfiguration;
public TaskScheduleServiceImpl() { public TaskScheduleServiceImpl() {
@ -72,7 +72,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId)); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
if (!taskManager.isTaskScheduled(taskName)) { if (!taskManager.isTaskScheduled(taskName)) {
@ -101,7 +101,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
public void stopTask() throws PolicyMonitoringTaskException { public void stopTask() throws PolicyMonitoringTaskException {
int tenantId = getTenantId(); int tenantId = getTenantId();
try { try {
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
if (taskService.isServerInit()) { if (taskService.isServerInit()) {
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
@ -117,7 +117,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException { public void updateTask(int monitoringFrequency) throws PolicyMonitoringTaskException {
int tenantId = getTenantId(); int tenantId = getTenantId();
try { try {
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(PolicyManagementConstants.MONITORING_TASK_TYPE);
@ -129,7 +129,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put("tenantId", String.valueOf(tenantId)); properties.put(PolicyManagementConstants.TENANT_ID, String.valueOf(tenantId));
TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties, TaskInfo taskInfo = new TaskInfo(taskName, PolicyManagementConstants.MONITORING_TASK_CLAZZ, properties,
triggerInfo); triggerInfo);
@ -150,7 +150,7 @@ public class TaskScheduleServiceImpl implements TaskScheduleService {
@Override @Override
public boolean isTaskScheduled() throws PolicyMonitoringTaskException { public boolean isTaskScheduled() throws PolicyMonitoringTaskException {
int tenantId = getTenantId(); int tenantId = getTenantId();
String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + String.valueOf(tenantId); String taskName = PolicyManagementConstants.MONITORING_TASK_NAME + "_" + tenantId;
TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService(); TaskService taskService = PolicyManagementDataHolder.getInstance().getTaskService();
TaskManager taskManager; TaskManager taskManager;
try { try {

Loading…
Cancel
Save