From e76fe92ebe870b235d8b7b7fea282a284626891f Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Oct 2018 08:29:04 +0530 Subject: [PATCH] Fix device monitoring task disabling issue --- .../DeviceManagementPluginRepository.java | 2 +- .../DeviceTaskManagerServiceComponent.java | 4 ++- .../DeviceManagementProviderService.java | 3 ++ .../DeviceManagementProviderServiceImpl.java | 7 ++++ .../task/impl/DeviceDetailsRetrieverTask.java | 33 ++++++++----------- .../impl/DeviceTaskManagerServiceImpl.java | 10 +++--- .../mgt/core/TestDeviceManagementService.java | 2 +- .../mgt/core/task/DeviceTaskManagerTest.java | 8 ++--- .../devicemgt/app/conf/config.json | 3 ++ 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 627ea432a9..44b740a097 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -321,7 +321,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis getDeviceTaskManagerService(); OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. getOperationMonitoringConfig(); - if (operationMonitoringTaskConfig != null) { + if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { deviceTaskManagerService.stopTask(deviceManagementService.getType(), deviceManagementService.getOperationMonitoringConfig()); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java index ae39c83109..ee3072170c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java @@ -89,7 +89,9 @@ public class DeviceTaskManagerServiceComponent { Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) { - deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); + if (deviceConfigMap.get(platformType).isEnabled()){ + deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); + } deviceConfigMap.remove(platformType); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index c3a1b37008..9786b8f73d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -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.InvalidDeviceException; 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.PaginationResult; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; @@ -589,6 +590,8 @@ public interface DeviceManagementProviderService { int getDeviceMonitoringFrequency(String deviceType); + OperationMonitoringTaskConfig getDeviceMonitoringConfig(String deviceType); + boolean isDeviceMonitoringEnabled(String deviceType); PolicyMonitoringManager getPolicyMonitoringManager(String deviceType); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index c7d23ecaaa..a22242796c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -1597,6 +1597,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return operationMonitoringTaskConfig.getFrequency(); } + @Override + public OperationMonitoringTaskConfig getDeviceMonitoringConfig(String deviceType) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId); + return dms.getOperationMonitoringConfig(); + } + @Override public boolean isDeviceMonitoringEnabled(String deviceType) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 7b65a3b6ee..fb91f62a52 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -16,16 +16,15 @@ * under the License. */ - 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.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; 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.DeviceTaskManager; 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 String deviceType; - private String oppConfig; - private OperationMonitoringTaskConfig operationMonitoringTaskConfig; private boolean executeForTenants = false; private final String IS_CLOUD = "is.cloud"; @Override public void setProperties(Map map) { deviceType = map.get("DEVICE_TYPE"); - oppConfig = map.get("OPPCONFIG"); - - Gson gson = new Gson(); - - operationMonitoringTaskConfig = gson.fromJson(oppConfig, - OperationMonitoringTaskConfig.class); } @Override @@ -60,29 +51,33 @@ public class DeviceDetailsRetrieverTask implements Task { @Override 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; } - if(executeForTenants){ - this.executeForAllTenants(); + if (executeForTenants) { + this.executeForAllTenants(operationMonitoringTaskConfig); } else { if (log.isDebugEnabled()) { log.debug("Device details retrieving task started to run."); } - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, - operationMonitoringTaskConfig); + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, operationMonitoringTaskConfig); //pass the configurations also from here, monitoring tasks try { - deviceTaskManager.addOperations(); + if (deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType)) { + deviceTaskManager.addOperations(); + } } catch (DeviceMgtTaskException e) { - log.error( - "Error occurred while trying to add the operations to device to retrieve device details.", e); + log.error("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()) { log.debug("Device details retrieving task started to run for all tenants."); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java index b8119222db..d564fd7542 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java @@ -107,10 +107,12 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { throws DeviceMgtTaskException { try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); if (taskService.isServerInit()) { TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - taskManager.deleteTask(deviceType); + String taskName = deviceType + String.valueOf(tenantId); + taskManager.deleteTask(taskName); } } catch (TaskException e) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -131,8 +133,8 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); if (taskManager.isTaskScheduled(deviceType)) { - - taskManager.deleteTask(deviceType); + String taskName = deviceType + String.valueOf(tenantId); + taskManager.deleteTask(taskName); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); triggerInfo.setRepeatCount(-1); @@ -140,7 +142,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { Map properties = new HashMap<>(); 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.rescheduleTask(taskInfo.getName()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java index 29ebe9c493..d9449f6a49 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManagementService.java @@ -43,7 +43,7 @@ public class TestDeviceManagementService implements DeviceManagementService { public TestDeviceManagementService(String deviceType, String tenantDomain) { providerType = deviceType; this.tenantDomain = tenantDomain; - this.operationCode = "default"; + this.operationCode = "DEVICE_INFO"; } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java index d76783991c..4b558f0708 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerTest.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.task; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.mockito.Mockito; import org.powermock.api.mockito.PowerMockito; import org.testng.Assert; 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.DeviceIdentifier; 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.OperationManagementException; 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 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 deviceIds; private DeviceTaskManager deviceTaskManager; private DeviceManagementProviderService deviceMgtProviderService; @@ -76,7 +77,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { } List devices = TestDataHolder.generateDummyDeviceData(this.deviceIds); this.deviceMgtProviderService = new DeviceManagementProviderServiceImpl(); - DeviceManagementServiceComponent.notifyStartupListeners(); DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(this.deviceMgtProviderService); DeviceManagementDataHolder.getInstance() @@ -152,7 +152,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); - map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); deviceDetailsRetrieverTask.setProperties(map); deviceDetailsRetrieverTask.execute(); for (DeviceIdentifier deviceId : deviceIds) { @@ -172,7 +171,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { System.setProperty("is.cloud", "true"); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); - map.put("OPPCONFIG", DEVICE_DETAIL_RETRIEVER_OPPCONFIG); deviceDetailsRetrieverTask.setProperties(map); deviceDetailsRetrieverTask.execute(); for (DeviceIdentifier deviceId : deviceIds) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json index 406dde9f3a..92ee019334 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/config.json @@ -15,6 +15,9 @@ "iOSConfigRoot": "%https.ip%/ios-enrollment/", "iOSAPIRoot": "%https.ip%/ios/", "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, "oauthProvider": { "appRegistration": {