From da5a1caf2ba2c5ffc347a8fba5e17174cab13b9a Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Fri, 12 Jun 2020 20:43:17 +0530 Subject: [PATCH 1/2] Execute device detail retreiver and monitoring task for all tenant --- .../task/impl/DeviceDetailsRetrieverTask.java | 66 +++++++++---------- .../mgt/core/task/DeviceTaskManagerTest.java | 1 - .../policy/mgt/core/task/MonitoringTask.java | 19 +++--- 3 files changed, 39 insertions(+), 47 deletions(-) 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 3fd8b3290c..b098fdd8ff 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 @@ -55,8 +55,7 @@ public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private String deviceType; - private boolean executeForTenants = false; - private final String IS_CLOUD = "is.cloud"; + private static final int SUPER_TENANT_ID = -1234; private DeviceManagementProviderService deviceManagementProviderService; @Override @@ -76,27 +75,7 @@ public class DeviceDetailsRetrieverTask implements Task { .getDeviceMonitoringConfig(deviceType); StartupOperationConfig startupOperationConfig = deviceManagementProviderService .getStartupOperationConfig(deviceType); - - if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) { - executeForTenants = true; - } - if (executeForTenants) { - this.executeForAllTenants(operationMonitoringTaskConfig, startupOperationConfig); - } else { - if (log.isDebugEnabled()) { - log.debug("Device details retrieving task started to run."); - } - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, operationMonitoringTaskConfig, - startupOperationConfig); - //pass the configurations also from here, monitoring tasks - try { - 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); - } - } + this.executeForAllTenants(operationMonitoringTaskConfig, startupOperationConfig); } private void executeForAllTenants(OperationMonitoringTaskConfig operationMonitoringTaskConfig, @@ -113,24 +92,17 @@ public class DeviceDetailsRetrieverTask implements Task { } for (Integer tenant : tenants) { + if (SUPER_TENANT_ID == tenant) { + this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); + continue; + } String tenantDomain = DeviceManagementDataHolder.getInstance(). getRealmService().getTenantManager().getDomain(tenant); try { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant); - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, - operationMonitoringTaskConfig, - startupOperationConfig); - //pass the configurations also from here, monitoring tasks - try { - 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); - } + this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); } finally { PrivilegedCarbonContext.endTenantFlow(); } @@ -143,4 +115,28 @@ public class DeviceDetailsRetrieverTask implements Task { } } + /** + * Execute device detail retriever task + * @param operationMonitoringTaskConfig which contains monitoring operations and related details + * @param startupOperationConfig which contains startup operations and realted details + */ + private void executeTask(OperationMonitoringTaskConfig operationMonitoringTaskConfig, + StartupOperationConfig startupOperationConfig) { + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, + operationMonitoringTaskConfig, + startupOperationConfig); + if (log.isDebugEnabled()) { + log.debug("Device details retrieving task started to run."); + } + //pass the configurations also from here, monitoring tasks + try { + 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); + } + } + } 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 eb6e6a4d2b..5ee04dc92d 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 @@ -165,7 +165,6 @@ public class DeviceTaskManagerTest extends BaseDeviceManagementTest { description = "Testing device detail retriever task execution for tenants") public void testDeviceDetailRetrieverTaskExecuteForAllTenants() throws OperationManagementException { DeviceDetailsRetrieverTask deviceDetailsRetrieverTask = new DeviceDetailsRetrieverTask(); - System.setProperty("is.cloud", "true"); Map map = new HashMap<>(); map.put("DEVICE_TYPE", TestDataHolder.TEST_DEVICE_TYPE); deviceDetailsRetrieverTask.setProperties(map); 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 d70c06858b..40e437dfaa 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 @@ -42,10 +42,9 @@ public class MonitoringTask implements Task { private static final Log log = LogFactory.getLog(MonitoringTask.class); - Map properties; - private boolean executeForTenants = false; - private final String IS_CLOUD = "is.cloud"; + private static final int SUPER_TENANT_ID = -1234; + Map properties; @Override public void setProperties(Map map) { @@ -62,14 +61,8 @@ public class MonitoringTask implements Task { if (log.isDebugEnabled()) { log.debug("Monitoring task started to run."); } - if (System.getProperty(IS_CLOUD) != null && Boolean.parseBoolean(System.getProperty(IS_CLOUD))) { - executeForTenants = true; - } - if (executeForTenants) { - this.executeforAllTenants(); - } else { - this.executeTask(); - } + + this.executeforAllTenants(); } /** @@ -95,6 +88,10 @@ public class MonitoringTask implements Task { DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); List tenants = deviceManagementService.getDeviceEnrolledTenants(); for (Integer tenant : tenants) { + if (SUPER_TENANT_ID == tenant) { + this.executeTask(); + continue; + } String tenantDomain = PolicyManagementDataHolder.getInstance(). getRealmService().getTenantManager().getDomain(tenant); try { From 07e56d68f5c4c1dd16fc80662ee80ac1bc9c8354 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Sat, 13 Jun 2020 10:27:23 +0530 Subject: [PATCH 2/2] Set tenant through tenant domain auto resolve method --- .../core/task/impl/DeviceDetailsRetrieverTask.java | 12 +++--------- .../carbon/policy/mgt/core/task/MonitoringTask.java | 12 +++--------- 2 files changed, 6 insertions(+), 18 deletions(-) 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 b098fdd8ff..b3f4b850a6 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 @@ -37,6 +37,7 @@ package org.wso2.carbon.device.mgt.core.task.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; @@ -55,7 +56,6 @@ public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); private String deviceType; - private static final int SUPER_TENANT_ID = -1234; private DeviceManagementProviderService deviceManagementProviderService; @Override @@ -90,25 +90,19 @@ public class DeviceDetailsRetrieverTask implements Task { if (log.isDebugEnabled()) { log.debug("Task is running for " + tenants.size() + " tenants and the device type is " + deviceType); } - for (Integer tenant : tenants) { - if (SUPER_TENANT_ID == tenant) { + if (MultitenantConstants.SUPER_TENANT_ID == tenant) { this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); continue; } - String tenantDomain = DeviceManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getDomain(tenant); try { PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true); this.executeTask(operationMonitoringTaskConfig, startupOperationConfig); } finally { PrivilegedCarbonContext.endTenantFlow(); } } - } catch (UserStoreException e) { - log.error("Error occurred while trying to get the available tenants", e); } catch (DeviceManagementException e) { log.error("Error occurred while trying to get the available tenants " + "from device manager provider service.", 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 40e437dfaa..f75995179b 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 @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.task; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.MultitenantConstants; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; @@ -42,8 +43,6 @@ public class MonitoringTask implements Task { private static final Log log = LogFactory.getLog(MonitoringTask.class); - private static final int SUPER_TENANT_ID = -1234; - Map properties; @Override @@ -88,23 +87,18 @@ public class MonitoringTask implements Task { DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); List tenants = deviceManagementService.getDeviceEnrolledTenants(); for (Integer tenant : tenants) { - if (SUPER_TENANT_ID == tenant) { + if (MultitenantConstants.SUPER_TENANT_ID == tenant) { this.executeTask(); continue; } - String tenantDomain = PolicyManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getDomain(tenant); try { PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true); this.executeTask(); } finally { PrivilegedCarbonContext.endTenantFlow(); } } - } catch (UserStoreException e) { - log.error("Error occurred while trying to get the available tenants", e); } catch (DeviceManagementException e) { log.error("Error occurred while trying to get the available tenants from device manager service ", e); }