Fix device monitoring task disabling issue

4.x.x
lasanthaDLPDS 6 years ago
parent a18b4550ef
commit e76fe92ebe

@ -321,7 +321,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
getDeviceTaskManagerService(); getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService.
getOperationMonitoringConfig(); getOperationMonitoringConfig();
if (operationMonitoringTaskConfig != null) { if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
deviceTaskManagerService.stopTask(deviceManagementService.getType(), deviceTaskManagerService.stopTask(deviceManagementService.getType(),
deviceManagementService.getOperationMonitoringConfig()); deviceManagementService.getOperationMonitoringConfig());
} }

@ -89,7 +89,9 @@ public class DeviceTaskManagerServiceComponent {
Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap(); .getInstance().getOperationMonitoringConfigFromMap();
for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) { for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) {
if (deviceConfigMap.get(platformType).isEnabled()){
deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType));
}
deviceConfigMap.remove(platformType); deviceConfigMap.remove(platformType);
} }
} }

@ -24,6 +24,7 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.FeatureManager;
import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation; import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
@ -589,6 +590,8 @@ public interface DeviceManagementProviderService {
int getDeviceMonitoringFrequency(String deviceType); int getDeviceMonitoringFrequency(String deviceType);
OperationMonitoringTaskConfig getDeviceMonitoringConfig(String deviceType);
boolean isDeviceMonitoringEnabled(String deviceType); boolean isDeviceMonitoringEnabled(String deviceType);
PolicyMonitoringManager getPolicyMonitoringManager(String deviceType); PolicyMonitoringManager getPolicyMonitoringManager(String deviceType);

@ -1597,6 +1597,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
return operationMonitoringTaskConfig.getFrequency(); return operationMonitoringTaskConfig.getFrequency();
} }
@Override
public OperationMonitoringTaskConfig getDeviceMonitoringConfig(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId);
return dms.getOperationMonitoringConfig();
}
@Override @Override
public boolean isDeviceMonitoringEnabled(String deviceType) { public boolean isDeviceMonitoringEnabled(String deviceType) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();

@ -16,16 +16,15 @@
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.core.task.impl; package org.wso2.carbon.device.mgt.core.task.impl;
import com.google.gson.Gson;
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.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
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.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.ntask.core.Task;
@ -38,20 +37,12 @@ public class DeviceDetailsRetrieverTask implements Task {
private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class);
private String deviceType; private String deviceType;
private String oppConfig;
private OperationMonitoringTaskConfig operationMonitoringTaskConfig;
private boolean executeForTenants = false; private boolean executeForTenants = false;
private final String IS_CLOUD = "is.cloud"; private final String IS_CLOUD = "is.cloud";
@Override @Override
public void setProperties(Map<String, String> map) { public void setProperties(Map<String, String> map) {
deviceType = map.get("DEVICE_TYPE"); deviceType = map.get("DEVICE_TYPE");
oppConfig = map.get("OPPCONFIG");
Gson gson = new Gson();
operationMonitoringTaskConfig = gson.fromJson(oppConfig,
OperationMonitoringTaskConfig.class);
} }
@Override @Override
@ -60,29 +51,33 @@ public class DeviceDetailsRetrieverTask implements Task {
@Override @Override
public void execute() { public void execute() {
DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance()
.getDeviceManagementProvider();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService
.getDeviceMonitoringConfig(deviceType);
if(System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))){ if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) {
executeForTenants = true; executeForTenants = true;
} }
if(executeForTenants){ if (executeForTenants) {
this.executeForAllTenants(); this.executeForAllTenants(operationMonitoringTaskConfig);
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device details retrieving task started to run."); log.debug("Device details retrieving task started to run.");
} }
DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, operationMonitoringTaskConfig);
operationMonitoringTaskConfig);
//pass the configurations also from here, monitoring tasks //pass the configurations also from here, monitoring tasks
try { try {
if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) {
deviceTaskManager.addOperations(); deviceTaskManager.addOperations();
}
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
log.error( log.error("Error occurred while trying to add the operations to device to retrieve device details.", e);
"Error occurred while trying to add the operations to device to retrieve device details.", e);
} }
} }
} }
private void executeForAllTenants() { private void executeForAllTenants(OperationMonitoringTaskConfig operationMonitoringTaskConfig) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Device details retrieving task started to run for all tenants."); log.debug("Device details retrieving task started to run for all tenants.");

@ -107,10 +107,12 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
throws DeviceMgtTaskException { throws DeviceMgtTaskException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
if (taskService.isServerInit()) { if (taskService.isServerInit()) {
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
taskManager.deleteTask(deviceType); String taskName = deviceType + String.valueOf(tenantId);
taskManager.deleteTask(taskName);
} }
} catch (TaskException e) { } catch (TaskException e) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
@ -131,8 +133,8 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
if (taskManager.isTaskScheduled(deviceType)) { if (taskManager.isTaskScheduled(deviceType)) {
String taskName = deviceType + String.valueOf(tenantId);
taskManager.deleteTask(deviceType); taskManager.deleteTask(taskName);
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());
triggerInfo.setRepeatCount(-1); triggerInfo.setRepeatCount(-1);
@ -140,7 +142,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
Map<String, String> properties = new HashMap<>(); Map<String, String> properties = new HashMap<>();
properties.put(TENANT_ID, String.valueOf(tenantId)); properties.put(TENANT_ID, String.valueOf(tenantId));
TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo); TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo);
taskManager.registerTask(taskInfo); taskManager.registerTask(taskInfo);
taskManager.rescheduleTask(taskInfo.getName()); taskManager.rescheduleTask(taskInfo.getName());

@ -43,7 +43,7 @@ public class TestDeviceManagementService implements DeviceManagementService {
public TestDeviceManagementService(String deviceType, String tenantDomain) { public TestDeviceManagementService(String deviceType, String tenantDomain) {
providerType = deviceType; providerType = deviceType;
this.tenantDomain = tenantDomain; this.tenantDomain = tenantDomain;
this.operationCode = "default"; this.operationCode = "DEVICE_INFO";
} }
@Override @Override

@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.task;
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.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito; import org.powermock.api.mockito.PowerMockito;
import org.testng.Assert; import org.testng.Assert;
import org.testng.annotations.AfterClass; import org.testng.annotations.AfterClass;
@ -27,6 +28,8 @@ import org.testng.annotations.Test;
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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
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.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager;
@ -60,8 +63,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class); private static final Log log = LogFactory.getLog(DeviceTaskManagerTest.class);
private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE"; private static final String NEW_DEVICE_TYPE = "NEW-DEVICE-TYPE";
private static final String DEVICE_DETAIL_RETRIEVER_OPPCONFIG = "{\"isEnabled\":true,\"frequency\":60000," +
"\"monitoringOperation\":[{\"taskName\":\"DEVICE_INFO\",\"recurrentTimes\":2}]}";
private List<DeviceIdentifier> deviceIds; private List<DeviceIdentifier> deviceIds;
private DeviceTaskManager deviceTaskManager; private DeviceTaskManager deviceTaskManager;
private DeviceManagementProviderService deviceMgtProviderService; private DeviceManagementProviderService deviceMgtProviderService;
@ -76,7 +77,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
} }
List<Device> devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); List<Device> devices = TestDataHolder.generateDummyDeviceData(this.deviceIds);
this.deviceMgtProviderService = new DeviceManagementProviderServiceImpl(); this.deviceMgtProviderService = new DeviceManagementProviderServiceImpl();
DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementServiceComponent.notifyStartupListeners();
DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService);
DeviceManagementDataHolder.getInstance() DeviceManagementDataHolder.getInstance()
@ -152,7 +152,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask();
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE);
map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG);
deviceDetailsRetrieverTask.setProperties(map); deviceDetailsRetrieverTask.setProperties(map);
deviceDetailsRetrieverTask.execute(); deviceDetailsRetrieverTask.execute();
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {
@ -172,7 +171,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest {
System.setProperty("is.cloud", "true"); System.setProperty("is.cloud", "true");
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE);
map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG);
deviceDetailsRetrieverTask.setProperties(map); deviceDetailsRetrieverTask.setProperties(map);
deviceDetailsRetrieverTask.execute(); deviceDetailsRetrieverTask.execute();
for (DeviceIdentifier deviceId : deviceIds) { for (DeviceIdentifier deviceId : deviceIds) {

@ -15,6 +15,9 @@
"iOSConfigRoot": "%https.ip%/ios-enrollment/", "iOSConfigRoot": "%https.ip%/ios-enrollment/",
"iOSAPIRoot": "%https.ip%/ios/", "iOSAPIRoot": "%https.ip%/ios/",
"adminService": "%https.ip%", "adminService": "%https.ip%",
"deviceInfoServiceAPI" : "/api/device-mgt/%device-type%/v1.0/admin/devices/info",
"deviceLocationServiceAPI" : "/api/device-mgt/%device-type%/v1.0/admin/devices/location",
"iOSDeviceInfoServiceAPI" : "/api/device-mgt/%device-type%/v1.0/admin/devices/info",
"gatewayEnabled": true, "gatewayEnabled": true,
"oauthProvider": { "oauthProvider": {
"appRegistration": { "appRegistration": {

Loading…
Cancel
Save