From b889f6e384b5a732cc621b6e9ed01c000de35f9b Mon Sep 17 00:00:00 2001 From: madhawap Date: Thu, 8 Dec 2016 17:21:07 +0530 Subject: [PATCH] adding separate tasks for each device type --- .../deployer/config/TaskConfiguration.java | 11 -- .../template/DeviceTypeManagerService.java | 28 ++-- .../src/test/resources/sample.xml | 2 + ...peration.java => MonitoringOperation.java} | 3 +- .../common/OperationMonitoringTaskConfig.java | 40 ++++++ .../common/spi/DeviceManagementService.java | 6 +- .../DeviceManagementPluginRepository.java | 126 ++++++++++++----- .../internal/DeviceManagementDataHolder.java | 22 +++ .../DeviceManagementServiceComponent.java | 15 ++ .../DeviceMonitoringOperationDataHolder.java | 48 +++++++ .../DeviceTaskManagerServiceComponent.java | 43 ++++-- .../operation/mgt/OperationManagerImpl.java | 56 ++++---- .../DeviceManagementProviderService.java | 19 ++- .../DeviceManagementProviderServiceImpl.java | 50 ++++--- .../mgt/core/task/DeviceTaskManager.java | 46 +++--- .../core/task/DeviceTaskManagerService.java | 19 ++- .../task/impl/DeviceDetailsRetrieverTask.java | 6 +- .../core/task/impl/DeviceTaskManagerImpl.java | 132 +++++++----------- .../impl/DeviceTaskManagerServiceImpl.java | 52 +++---- .../mgt/core/TestDeviceManagementService.java | 6 +- .../src/main/resources/conf/cdm-config.xml | 8 +- 21 files changed, 476 insertions(+), 262 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/{TaskOperation.java => MonitoringOperation.java} (96%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMonitoringOperationDataHolder.java diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/TaskConfiguration.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/TaskConfiguration.java index c2539d2e6e..6de303e557 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/TaskConfiguration.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/TaskConfiguration.java @@ -30,10 +30,8 @@ import java.util.List; @XmlRootElement(name = "TaskConfiguration") public class TaskConfiguration { - private boolean enabled; private int frequency; - private String taskClazz; private List operations; @XmlElement(name = "Enable", required = true) @@ -54,15 +52,6 @@ public class TaskConfiguration { this.frequency = frequency; } - @XmlElement(name = "TaskClass", required = true) - public String getTaskClazz() { - return taskClazz; - } - - public void setTaskClazz(String taskClazz) { - this.taskClazz = taskClazz; - } - @XmlElementWrapper(name="Operations") @XmlElement(name = "Operation", required = true) public List getOperations() { diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java index c83318ca9b..62d03ffeb5 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java @@ -22,8 +22,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.MonitoringOperation; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.TaskOperation; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; @@ -51,7 +52,8 @@ public class DeviceTypeManagerService implements DeviceManagementService { private PushNotificationConfig pushNotificationConfig; private ProvisioningConfig provisioningConfig; private String type; - private List taskOperations; + private OperationMonitoringTaskConfig operationMonitoringConfigs; + private List monitoringOperations; public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, DeviceTypeConfiguration deviceTypeConfiguration) { @@ -59,7 +61,8 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.deviceManager = new DeviceTypeManager(deviceTypeConfigIdentifier, deviceTypeConfiguration); this.setType(deviceTypeConfiguration.getName()); this.populatePushNotificationConfig(deviceTypeConfiguration.getPushNotificationProvider()); - this.setTask(deviceTypeConfiguration); + this.operationMonitoringConfigs = new OperationMonitoringTaskConfig(); + this.setOperationMonitoringConfig(deviceTypeConfiguration); } @Override @@ -68,24 +71,27 @@ public class DeviceTypeManagerService implements DeviceManagementService { } @Override - public List getTasksForPlatform(){ - return taskOperations; + public OperationMonitoringTaskConfig getOperationMonitoringConfig(){ + return operationMonitoringConfigs; } - private void setTask(DeviceTypeConfiguration deviceTypeConfiguration) { + private void setOperationMonitoringConfig(DeviceTypeConfiguration deviceTypeConfiguration) { //Read the config file and take the list of operations there in the config TaskConfiguration taskConfiguration = deviceTypeConfiguration.getTaskConfiguration(); if (taskConfiguration != null) { + operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled()); + operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency()); List ops = taskConfiguration.getOperations(); if (ops != null && !ops.isEmpty()) { - taskOperations = new ArrayList<>(); + monitoringOperations = new ArrayList<>(); for (TaskConfiguration.Operation op : ops) { - TaskOperation taskOperation = new TaskOperation(); - taskOperation.setTaskName(op.getOperationName()); - taskOperation.setRecurrentTimes(op.getRecurrency()); - taskOperations.add(taskOperation); + MonitoringOperation monitoringOperation = new MonitoringOperation(); + monitoringOperation.setTaskName(op.getOperationName()); + monitoringOperation.setRecurrentTimes(op.getRecurrency()); + monitoringOperations.add(monitoringOperation); } } + operationMonitoringConfigs.setMonitoringOperation(monitoringOperations); } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml index a3d19a34ac..31b54ec20a 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml @@ -102,6 +102,8 @@ + true + 600000 DEVICE_INFO diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TaskOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TaskOperation.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java index a7143b7173..7ce7000d81 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TaskOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java @@ -21,7 +21,7 @@ package org.wso2.carbon.device.mgt.common; /** * */ -public class TaskOperation { +public class MonitoringOperation { private String taskName; private int recurrentTimes; @@ -42,5 +42,6 @@ public class TaskOperation { this.recurrentTimes = recurrentTimes; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java new file mode 100644 index 0000000000..7195d89dcb --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/OperationMonitoringTaskConfig.java @@ -0,0 +1,40 @@ +package org.wso2.carbon.device.mgt.common; + +import java.util.List; + +/** + * + */ +public class OperationMonitoringTaskConfig { + + private boolean isEnabled; + private int frequency; + private List monitoringOperation; + + public boolean isEnabled() { + return isEnabled; + } + + public void setEnabled(boolean enabled) { + isEnabled = enabled; + } + + public int getFrequency() { + return frequency; + } + + public void setFrequency(int frequency) { + this.frequency = frequency; + } + + public List getMonitoringOperation() { + return monitoringOperation; + } + + public void setMonitoringOperation(List monitoringOperation) { + this.monitoringOperation = monitoringOperation; + } + + + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java index 24836a02ae..5870436678 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/DeviceManagementService.java @@ -20,13 +20,11 @@ package org.wso2.carbon.device.mgt.common.spi; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.TaskOperation; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; -import java.util.List; - /** * Composite interface that acts as the SPI exposing all device management as well as application management * functionalities. @@ -37,7 +35,7 @@ public interface DeviceManagementService { String getType(); - List getTasksForPlatform();//getTasksConfiguraitons + OperationMonitoringTaskConfig getOperationMonitoringConfig(); DeviceManager getDeviceManager(); 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 2526ece305..9e2a81d5c3 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 @@ -22,6 +22,7 @@ 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.DeviceTypeIdentifier; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; @@ -31,8 +32,11 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; +import org.wso2.carbon.device.mgt.core.internal.DeviceMonitoringOperationDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerRepository; +import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; +import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.util.Collections; @@ -72,6 +76,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis provider.getDeviceManager() .requireDeviceAuthorization()); registerPushNotificationStrategy(provider); + registerMonitoringTask(provider); } } catch (DeviceManagementException e) { throw new DeviceManagementException("Error occurred while adding device management provider '" + @@ -87,19 +92,26 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis } } - public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { - String deviceTypeName = provider.getType().toLowerCase(); - DeviceTypeIdentifier deviceTypeIdentifier; - ProvisioningConfig provisioningConfig = provider.getProvisioningConfig(); - if (provisioningConfig.isSharedWithAllTenants()) { - deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName); - providers.remove(deviceTypeIdentifier); - } else { - int providerTenantId = DeviceManagerUtil.getTenantId(provisioningConfig.getProviderTenantDomain()); - deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId); - providers.remove(deviceTypeIdentifier); - } - OperationManager operationManager = operationManagerRepository.getOperationManager(deviceTypeIdentifier); + public void removeDeviceManagementProvider(DeviceManagementService provider) + throws DeviceManagementException { + String deviceTypeName = provider.getType().toLowerCase(); + DeviceTypeIdentifier deviceTypeIdentifier; + ProvisioningConfig provisioningConfig = provider.getProvisioningConfig(); + if (provisioningConfig.isSharedWithAllTenants()) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName); + providers.remove(deviceTypeIdentifier); + } else { + int providerTenantId = DeviceManagerUtil.getTenantId(provisioningConfig.getProviderTenantDomain()); + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId); + providers.remove(deviceTypeIdentifier); + } + unregisterPushNotificationStrategy(deviceTypeIdentifier); + unregisterMonitoringTask(provider); + } + + private void unregisterPushNotificationStrategy(DeviceTypeIdentifier deviceTypeIdentifier) { + OperationManager operationManager = operationManagerRepository.getOperationManager( + deviceTypeIdentifier); if (operationManager != null) { NotificationStrategy notificationStrategy = operationManager.getNotificationStrategy(); if (notificationStrategy != null) { @@ -137,34 +149,78 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( deviceManagementService.getProvisioningConfig().getProviderTenantDomain(), true); try { - boolean isSharedWithAllTenants = deviceManagementService.getProvisioningConfig().isSharedWithAllTenants(); - DeviceTypeIdentifier deviceTypeIdentifier; - if (isSharedWithAllTenants) { - deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType()); - } else { - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType(), tenantId); - } + boolean isSharedWithAllTenants = deviceManagementService.getProvisioningConfig().isSharedWithAllTenants(); + DeviceTypeIdentifier deviceTypeIdentifier; + if (isSharedWithAllTenants) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType()); + } else { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType(), tenantId); + } - if (pushNoteConfig != null) { - PushNotificationProvider provider = DeviceManagementDataHolder.getInstance() - .getPushNotificationProviderRepository().getProvider(pushNoteConfig.getType()); - if (provider == null) { - throw new DeviceManagementException( - "No registered push notification provider found for the type: '" + - pushNoteConfig.getType() + "'."); - } - NotificationStrategy notificationStrategy = provider.getNotificationStrategy(pushNoteConfig); - operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl( - notificationStrategy)); - } else { - operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl()); + if (pushNoteConfig != null) { + PushNotificationProvider provider = DeviceManagementDataHolder.getInstance() + .getPushNotificationProviderRepository().getProvider(pushNoteConfig.getType()); + if (provider == null) { + throw new DeviceManagementException( + "No registered push notification provider found for the type: '" + + pushNoteConfig.getType() + "'."); } + NotificationStrategy notificationStrategy = provider.getNotificationStrategy(pushNoteConfig); + operationManagerRepository.addOperationManager(deviceTypeIdentifier, + new OperationManagerImpl(deviceTypeIdentifier.getDeviceType(), notificationStrategy)); + } else { + operationManagerRepository.addOperationManager(deviceTypeIdentifier, + new OperationManagerImpl(deviceTypeIdentifier.getDeviceType())); + } } finally { PrivilegedCarbonContext.endTenantFlow(); } } + private void registerMonitoringTask(DeviceManagementService deviceManagementService) + throws DeviceManagementException { + try { + DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance() + .getDeviceTaskManagerService(); + + OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService + .getOperationMonitoringConfig(); + + if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { + + if (deviceTaskManagerService == null) { + DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap( + deviceManagementService.getType(), operationMonitoringTaskConfig); + } else { + deviceTaskManagerService.startTask(deviceManagementService.getType(), + operationMonitoringTaskConfig); + // TODO: In here a race condition can arise. Need to handle it. + } + } + } catch (DeviceMgtTaskException e) { + throw new DeviceManagementException("Error occurred while adding task service for '" + + deviceManagementService.getType() + "'", e); + } + } + + private void unregisterMonitoringTask(DeviceManagementService deviceManagementService) + throws DeviceManagementException { + try { + DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance() + .getDeviceTaskManagerService(); + OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService + .getOperationMonitoringConfig(); + if (operationMonitoringTaskConfig != null) { + deviceTaskManagerService.stopTask(deviceManagementService.getType(), + deviceManagementService.getOperationMonitoringConfig()); + } + } catch (DeviceMgtTaskException e) { + throw new DeviceManagementException("Error occurred while removing task service for '" + + deviceManagementService.getType() + "'", e); + } + } + public OperationManager getOperationManager(String deviceType, int tenantId) { //Priority need to be given to the tenant before public. DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType.toLowerCase(), tenantId); @@ -189,6 +245,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis DeviceManagerUtil.registerDeviceType(deviceTypeName, tenantId, provisioningConfig.isSharedWithAllTenants()); registerPushNotificationStrategy(provider); + registerMonitoringTask(provider); //TODO: //This is a temporory fix. //windows and IOS cannot resolve user info by extracting certs @@ -207,5 +264,4 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis this.isInited = true; } } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index ea50220644..0ca96ec4d2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.internal; import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; @@ -28,6 +29,7 @@ import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -36,6 +38,7 @@ import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.utils.ConfigurationContextService; import java.util.HashMap; +import java.util.Map; public class DeviceManagementDataHolder { @@ -56,6 +59,18 @@ public class DeviceManagementDataHolder { private TaskService taskService; private EmailSenderService emailSenderService; private PushNotificationProviderRepository pushNotificationProviderRepository; + private DeviceTaskManagerService deviceTaskManagerService; + + private Map map = new HashMap<>(); + + + public void addToMap(OperationMonitoringTaskConfig taskConfig) { + this.map.put("aa", taskConfig); + } + + public Map getMap(){ + return this.map; + } private APIManagerConfiguration apiManagerConfiguration; @@ -210,4 +225,11 @@ public class DeviceManagementDataHolder { return pushNotificationProviderRepository; } + public DeviceTaskManagerService getDeviceTaskManagerService() { + return deviceTaskManagerService; + } + + public void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) { + this.deviceTaskManagerService = deviceTaskManagerService; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index e1b1389a78..adbf6cda43 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -53,6 +53,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; +import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ndatasource.core.DataSourceService; @@ -402,4 +403,18 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setEmailSenderService(null); } + + protected void setDeviceTaskManagerService(DeviceTaskManagerService emailSenderService) { + if (log.isDebugEnabled()) { + } + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(emailSenderService); + } + + protected void unsetDeviceTaskManagerService(DeviceTaskManagerService emailSenderService) { + if (log.isDebugEnabled()) { + } + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(null); + } } + + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMonitoringOperationDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMonitoringOperationDataHolder.java new file mode 100644 index 0000000000..5ae804d93f --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceMonitoringOperationDataHolder.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.device.mgt.core.internal; + +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * + */ +public class DeviceMonitoringOperationDataHolder { + + private static DeviceMonitoringOperationDataHolder thisInstance = new DeviceMonitoringOperationDataHolder(); + private Map OperationConfigMap = Collections.synchronizedMap( + new HashMap()); + + + public static DeviceMonitoringOperationDataHolder getInstance() { + return thisInstance; + } + + public void addOperationMonitoringConfigToMap(String deviceType, OperationMonitoringTaskConfig OperationConfig) { + this.OperationConfigMap.put(deviceType, OperationConfig); + } + + public Map getOperationMonitoringConfigFromMap(){ + return this.OperationConfigMap; + } +} 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 7051d72f51..7dab541321 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 @@ -22,15 +22,19 @@ package org.wso2.carbon.device.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.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; +import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; import org.wso2.carbon.ntask.core.service.TaskService; +import java.util.ArrayList; +import java.util.Map; + /** * @scr.component name="org.wso2.carbon.device.task.manager" immediate="true" * @scr.reference name="device.ntask.component" @@ -45,6 +49,8 @@ public class DeviceTaskManagerServiceComponent { private static Log log = LogFactory.getLog(DeviceManagementServiceComponent.class); + + @SuppressWarnings("unused") protected void activate(ComponentContext componentContext) { try { @@ -52,13 +58,14 @@ public class DeviceTaskManagerServiceComponent { log.debug("Initializing device details retrieving task manager bundle."); } // This will start the device details retrieving task. - boolean taskEnable = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). - isEnabled(); - if (taskEnable) { - DeviceTaskManagerService taskManagerService = new DeviceTaskManagerServiceImpl(); - taskManagerService.startTask(); - } +// DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); +// DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( +// deviceTaskManagerService); +// componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, +// deviceTaskManagerService, null); + + getDeviceOperationMonitoringConfig(componentContext); + componentContext.getBundleContext().registerService(DeviceInformationManager.class, new DeviceInformationManagerImpl(), null); @@ -69,6 +76,26 @@ public class DeviceTaskManagerServiceComponent { } } + private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) throws DeviceMgtTaskException { + + DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); + + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( + deviceTaskManagerService); + + componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, + deviceTaskManagerService, null); + + Map deviceConfigMap = DeviceMonitoringOperationDataHolder + .getInstance().getOperationMonitoringConfigFromMap(); + + for (String platformType : new ArrayList(deviceConfigMap.keySet())) { + deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); + deviceConfigMap.remove(platformType); + } + + } + @SuppressWarnings("unused") protected void deactivate(ComponentContext componentContext) { try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7f4e2f7a99..e79d95c5cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -25,9 +25,9 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.MonitoringOperation; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.TaskOperation; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; @@ -61,9 +61,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Collections; import java.util.Date; -import java.util.HashMap; import java.util.List; -import java.util.Map; /** * This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated @@ -83,6 +81,7 @@ public class OperationManagerImpl implements OperationManager { private DeviceDAO deviceDAO; private EnrollmentDAO enrollmentDAO; private NotificationStrategy notificationStrategy; + private String deviceType; public OperationManagerImpl() { commandOperationDAO = OperationManagementDAOFactory.getCommandOperationDAO(); @@ -95,6 +94,11 @@ public class OperationManagerImpl implements OperationManager { enrollmentDAO = DeviceManagementDAOFactory.getEnrollmentDAO(); } + public OperationManagerImpl(String deviceType) { + this(); + this.deviceType = deviceType; + } + public NotificationStrategy getNotificationStrategy() { return notificationStrategy; } @@ -103,8 +107,8 @@ public class OperationManagerImpl implements OperationManager { this.notificationStrategy = notificationStrategy; } - public OperationManagerImpl(NotificationStrategy notificationStrategy) { - this(); + public OperationManagerImpl(String deviceType, NotificationStrategy notificationStrategy) { + this(deviceType); this.notificationStrategy = notificationStrategy; } @@ -243,7 +247,7 @@ public class OperationManagerImpl implements OperationManager { List unAuthorizedDeviceList = new ArrayList<>(); DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); try { - if (operation != null && isAuthenticationSkippedOperation(operation, deviceIds)) { + if (operation != null && isAuthenticationSkippedOperation(operation)) { authorizedDeviceList = deviceIds; } else { boolean isAuthorized; @@ -885,11 +889,11 @@ public class OperationManagerImpl implements OperationManager { return CarbonContext.getThreadLocalCarbonContext().getUsername(); } - private boolean isAuthenticationSkippedOperation(Operation operation, List deviceIds) { + private boolean isAuthenticationSkippedOperation(Operation operation) { //This is to check weather operations are coming from the task related to retrieving device information. - DeviceTaskManager taskManager = new DeviceTaskManagerImpl(); - if (taskManager.isTaskOperation(operation.getCode(), deviceIds)) { + DeviceTaskManager taskManager = new DeviceTaskManagerImpl(deviceType); + if (taskManager.isTaskOperation(operation.getCode())) { return true; } @@ -1034,29 +1038,31 @@ public class OperationManagerImpl implements OperationManager { } private boolean isTaskScheduledOperation(Operation operation, List deviceIds) { - List taskOperations = new ArrayList<>(); - Map> deviceTypeSpecificTasks = new HashMap<>(); DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); - deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList();//Get task list from each device type + List monitoringOperations = deviceManagementProviderService.getMonitoringOperationList(deviceType);//Get task list from each device type - for(DeviceIdentifier deviceIdentifier : deviceIds){ - String deviceType = deviceIdentifier.getType(); - for(String dti : deviceTypeSpecificTasks.keySet()){ - if (dti.equals(deviceType)) { - taskOperations = deviceTypeSpecificTasks.get(dti); - for(TaskOperation op : taskOperations){ - if (operation.getCode().equals(op.getTaskName())) { - return true; - } - } - } + for(MonitoringOperation op : monitoringOperations){ + if (operation.getCode().equals(op.getTaskName())) { + return true; } - - } +// for(String dti : taskOperation){ +// if (dti.equals(deviceType)) { +// monitoringOperations = deviceTypeSpecificTasks.get(dti); +// +// } +// } +// +// for(DeviceIdentifier deviceIdentifier : deviceIds){ +// String deviceType = deviceIdentifier.getType(); +// +// +// +// } + // TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). // getTaskConfiguration(); // for (TaskConfiguration.Operation op : taskConfiguration.getOperations()) { 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 626203c304..73e22e1c5e 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 @@ -17,17 +17,24 @@ */ package org.wso2.carbon.device.mgt.core.service; -import org.wso2.carbon.device.mgt.common.*; +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.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.PaginationRequest; +import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import java.util.HashMap; import java.util.Date; +import java.util.HashMap; import java.util.List; -import java.util.Map; /** * Proxy class for all Device Management related operations that take the corresponding plugin type in @@ -282,6 +289,10 @@ public interface DeviceManagementProviderService { int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; - Map> getTaskList(); + List getMonitoringOperationList(String deviceType); + + int getDeviceMonitoringFrequency(String deviceType); + + boolean isDeviceMonitoringEnabled(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 d0b4f58a36..940cdba351 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 @@ -31,9 +31,10 @@ import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; 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.TaskOperation; import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; @@ -1217,24 +1218,37 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public Map> getTaskList() { + public List getMonitoringOperationList(String deviceType) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - Map deviceManagementServiceMap = - pluginRepository.getAllDeviceManagementServices(tenantId); - DeviceManagementService dms; - String deviceType; - List taskOperations; - Map> deviceTypeSpecificTasks = new HashMap<>(); - - for(DeviceTypeIdentifier dti : deviceManagementServiceMap.keySet()){ - dms = deviceManagementServiceMap.get(dti); - taskOperations = dms.getTasksForPlatform(); - if (taskOperations != null) { - deviceType = dms.getType(); - deviceTypeSpecificTasks.put(deviceType, taskOperations); - } - } - return deviceTypeSpecificTasks; +// Map deviceManagementServiceMap = +// pluginRepository.getAllDeviceManagementServices(tenantId); + DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId); + // ; + // OperationMonitoringTaskConfig operationMonitoringTaskConfig; + //Map> deviceTypeSpecificMonitoringOperations = new HashMap<>(); + +// for(DeviceTypeIdentifier dti : deviceManagementServiceMap.keySet()){ +// dms = deviceManagementServiceMap.get(dti); +// +// } + OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig(); + return operationMonitoringTaskConfig.getMonitoringOperation(); + } + + @Override + public int getDeviceMonitoringFrequency(String deviceType) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId); + OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig(); + return operationMonitoringTaskConfig.getFrequency(); + } + + @Override + public boolean isDeviceMonitoringEnabled(String deviceType) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceType, tenantId); + OperationMonitoringTaskConfig operationMonitoringTaskConfig = dms.getOperationMonitoringConfig(); + return operationMonitoringTaskConfig.isEnabled(); } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java index b7d59c9072..46128a37aa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java @@ -19,20 +19,16 @@ package org.wso2.carbon.device.mgt.core.task; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; - -import java.util.List; - public interface DeviceTaskManager { - /** - * This method will get the operation list from configurations. - * - * @return - list of Task Operations. - * @throws DeviceMgtTaskException - */ - List getOperationList(String deviceType) - throws DeviceMgtTaskException; +// /** +// * This method will get the operation list from configurations. +// * +// * @return - list of Task Operations. +// * @throws DeviceMgtTaskException +// */ +// List getOperationList(String deviceType) +// throws DeviceMgtTaskException; /** * This method will take the monitoring frequency. @@ -41,12 +37,12 @@ public interface DeviceTaskManager { */ int getTaskFrequency() throws DeviceMgtTaskException; - /** - * This method will return the task clazz from configurations. - * @return - Fully qualified class name. - * @throws DeviceMgtTaskException - */ - String getTaskImplementedClazz() throws DeviceMgtTaskException; +// /** +// * This method will return the task clazz from configurations. +// * @return - Fully qualified class name. +// * @throws DeviceMgtTaskException +// */ +// String getTaskImplementedClazz() throws DeviceMgtTaskException; /** * This method checks wheather task is enabled in config file. @@ -63,12 +59,12 @@ public interface DeviceTaskManager { void addOperations() throws DeviceMgtTaskException; - /** - * This method will return the operation names which should be added in each iterations. - * @return - * @throws DeviceMgtTaskException - */ - List getValidOperationNames(String deviceType) throws DeviceMgtTaskException; +// /** +// * This method will return the operation names which should be added in each iterations. +// * @return +// * @throws DeviceMgtTaskException +// */ +// List getValidOperationNames(String deviceType) throws DeviceMgtTaskException; /** * This method will check wheather give operation is added by the task. @@ -76,6 +72,6 @@ public interface DeviceTaskManager { * @return - true or false * @throws DeviceMgtTaskException */ - boolean isTaskOperation(String opName, List deviceIds); + boolean isTaskOperation(String opName); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerService.java index c327f0af38..1563516085 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManagerService.java @@ -19,26 +19,33 @@ package org.wso2.carbon.device.mgt.core.task; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; + public interface DeviceTaskManagerService { /** - * This method will stop the task. + * This method will start the task. + * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @throws DeviceMgtTaskException */ - void startTask() throws DeviceMgtTaskException; + void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException; /** - * This method will start the task. + * This method will stop the task. + * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @throws DeviceMgtTaskException */ - void stopTask() throws DeviceMgtTaskException; + void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException; /** * This will update the task frequency which it runs. - * @param frequency - integer + * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @throws DeviceMgtTaskException */ - void updateTask(int frequency) throws DeviceMgtTaskException; + void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException; } 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 bba13ca307..37a90f6f82 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 @@ -31,15 +31,15 @@ public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); // private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); + private String deviceType; @Override public void setProperties(Map map) { - + deviceType = map.get("DEVICE_TYPE"); } @Override public void init() { - } @Override @@ -49,7 +49,7 @@ public class DeviceDetailsRetrieverTask implements Task { log.debug("Device details retrieving task started to run."); } - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType); try { deviceTaskManager.addOperations(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 106159ef06..2606f07fd3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -22,13 +22,11 @@ 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.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.InvalidDeviceException; -import org.wso2.carbon.device.mgt.common.TaskOperation; +import org.wso2.carbon.device.mgt.common.MonitoringOperation; 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.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; @@ -42,65 +40,51 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; public class DeviceTaskManagerImpl implements DeviceTaskManager { private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class); - + private String deviceType; private static Map> map = new HashMap<>(); - - @Override - //get device type specific operations - public List getOperationList(String deviceType) throws DeviceMgtTaskException { - - List taskOperations = new ArrayList<>(); - Map> deviceTypeSpecificTasks; - //This Map contains task list against device type - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider(); - - deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList();//Get task list from each device type - for(String dti : deviceTypeSpecificTasks.keySet()){ - if (dti.equals(deviceType)) { - taskOperations = deviceTypeSpecificTasks.get(dti); - } - } - return taskOperations; + public DeviceTaskManagerImpl(String deviceType) { + this.deviceType = deviceType; } - private List getDeviceTypes() { - List operationPlatforms = new ArrayList<>(); - Map> deviceTypeSpecificTasks; + //get device type specific operations + public List getOperationList() throws DeviceMgtTaskException { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider(); - deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList(); + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder + .getInstance(). + getDeviceManagementProvider(); - Set platformTypes = deviceTypeSpecificTasks.keySet(); - for(String platformType : platformTypes ){ - operationPlatforms.add(platformType); - } - return operationPlatforms; + return deviceManagementProviderService.getMonitoringOperationList( + deviceType);//Get task list from each device type } @Override public int getTaskFrequency() throws DeviceMgtTaskException { - return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). - getFrequency(); - } + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder + .getInstance(). + getDeviceManagementProvider(); + + return deviceManagementProviderService.getDeviceMonitoringFrequency(deviceType); - @Override - public String getTaskImplementedClazz() throws DeviceMgtTaskException { - return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). - getTaskClazz(); } +// @Override +// public String getTaskImplementedClazz() throws DeviceMgtTaskException { +// return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). +// getTaskClazz(); +// } + @Override public boolean isTaskEnabled() throws DeviceMgtTaskException { - return DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(). - isEnabled(); + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder + .getInstance(). + getDeviceManagementProvider(); + + return deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType); } @@ -111,26 +95,22 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { try { List devices; List operations; - List deviceTypes = this.getDeviceTypes();//list available device types - - for(String deviceType : deviceTypes){ - operations = this.getValidOperationNames(deviceType); //list operations for each device type - devices = deviceManagementProviderService.getAllDevices(deviceType);//list devices for each type - if (!devices.isEmpty()) { - for (String str : operations) { - CommandOperation operation = new CommandOperation(); - operation.setEnabled(true); - operation.setType(Operation.Type.COMMAND); - operation.setCode(str); - deviceManagementProviderService.addOperation(deviceType, operation, - DeviceManagerUtil.getValidDeviceIdentifiers(devices)); - } - } else { - if (log.isDebugEnabled()) { - log.debug("No devices are available to perform the operations."); - } - } + operations = this.getValidOperationNames(); //list operations for each device type + devices = deviceManagementProviderService.getAllDevices(deviceType);//list devices for each type + if (!devices.isEmpty()) { + for (String str : operations) { + CommandOperation operation = new CommandOperation(); + operation.setEnabled(true); + operation.setType(Operation.Type.COMMAND); + operation.setCode(str); + deviceManagementProviderService.addOperation(deviceType, operation, + DeviceManagerUtil.getValidDeviceIdentifiers(devices)); + } + } else { + if (log.isDebugEnabled()) { + log.debug("No devices are available to perform the operations."); + } } } catch (InvalidDeviceException e) { throw new DeviceMgtTaskException("Invalid DeviceIdentifiers found.", e); @@ -141,16 +121,15 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } } - @Override - public List getValidOperationNames(String deviceType) throws DeviceMgtTaskException { + private List getValidOperationNames() throws DeviceMgtTaskException { - List taskOperations = this.getOperationList(deviceType); + List monitoringOperations = this.getOperationList(); List opNames = new ArrayList<>(); Long milliseconds = System.currentTimeMillis(); int frequency = this.getTaskFrequency(); Map mp = Utils.getTenantedTaskOperationMap(map); - for (TaskOperation top : taskOperations) { + for (MonitoringOperation top : monitoringOperations) { if (!mp.containsKey(top.getTaskName())) { opNames.add(top.getTaskName()); mp.put(top.getTaskName(), milliseconds); @@ -173,20 +152,17 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override - public boolean isTaskOperation(String opName, List deviceIds) { - - for(DeviceIdentifier deviceIdentifier : deviceIds){ - String deviceType = deviceIdentifier.getType(); - try { - List taskOperations = this.getOperationList(deviceType); - for (TaskOperation taop : taskOperations) { - if (taop.getTaskName().equalsIgnoreCase(opName)) { - return true; - } + public boolean isTaskOperation(String opName) { + + try { + List monitoringOperations = this.getOperationList(); + for (MonitoringOperation taop : monitoringOperations) { + if (taop.getTaskName().equalsIgnoreCase(opName)) { + return true; } - } catch (DeviceMgtTaskException e) { - // ignoring the error, no need to throw, If error occurs, return value will be false. } + } catch (DeviceMgtTaskException e) { + // ignoring the error, no need to throw, If error occurs, return value will be false. } return false; 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 87c45d079e..7a47f8d6bf 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 @@ -22,9 +22,9 @@ 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.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; 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.DeviceTaskManagerService; import org.wso2.carbon.ntask.common.TaskException; import org.wso2.carbon.ntask.core.TaskInfo; @@ -36,23 +36,21 @@ import java.util.Map; public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { - public static final String TASK_TYPE = "DEVICE_DETAILS"; - public static final String TASK_NAME = "DEVICE_DETAILS_TASK"; + public static final String TASK_TYPE = "DEVICE_MONITORING"; public static final String TENANT_ID = "TENANT_ID"; private static String TASK_CLASS = "org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask"; - private DeviceTaskManager deviceTaskManager; +// private DeviceTaskManager deviceTaskManager; private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class); @Override - public void startTask() throws DeviceMgtTaskException { + public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - deviceTaskManager = new DeviceTaskManagerImpl(); - if (!deviceTaskManager.isTaskEnabled()) { - throw new DeviceMgtTaskException("Task cannot be started, Please enable the task in cdm-config.xml file."); - } +// String TASK_NAME = deviceType; + + log.info("Task adding for " + deviceType); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -62,23 +60,27 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (log.isDebugEnabled()) { log.debug("Device details retrieving task is started for the tenant id " + tenantId); - log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager.getTaskFrequency()); +// log.debug("Device details retrieving task is at frequency of : " + deviceTaskManager +// .getTaskFrequency()); + log.debug("Device details retrieving task is at frequency of : " + operationMonitoringTaskConfig + .getFrequency()); } TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); +// triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency()); + triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); triggerInfo.setRepeatCount(-1); Map properties = new HashMap<>(); properties.put(TENANT_ID, String.valueOf(tenantId)); + properties.put("DEVICE_TYPE", deviceType); - if (!taskManager.isTaskScheduled(TASK_NAME)) { + if (!taskManager.isTaskScheduled(deviceType)) { - TaskInfo taskInfo = new TaskInfo(TASK_NAME, TASK_CLASS, - properties, triggerInfo); + TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); @@ -94,13 +96,15 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } @Override - public void stopTask() throws DeviceMgtTaskException { + public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { + +// String TASK_NAME = deviceType; try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); if (taskService.isServerInit()) { TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - taskManager.deleteTask(TASK_NAME); + taskManager.deleteTask(deviceType); } } catch (TaskException e) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -110,29 +114,27 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } @Override - public void updateTask(int frequency) throws DeviceMgtTaskException { + public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - if (!deviceTaskManager.isTaskEnabled()) { - throw new DeviceMgtTaskException("Task cannot be updated, Please enable the task in cdm-config.xml file."); - } +// String TASK_NAME = deviceType; int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - deviceTaskManager = new DeviceTaskManagerImpl(); +// deviceTaskManager = new DeviceTaskManagerImpl(); try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - if (taskManager.isTaskScheduled(TASK_NAME)) { + if (taskManager.isTaskScheduled(deviceType)) { - taskManager.deleteTask(TASK_NAME); + taskManager.deleteTask(deviceType); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); - triggerInfo.setIntervalMillis(frequency); + triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); triggerInfo.setRepeatCount(-1); Map properties = new HashMap<>(); properties.put(TENANT_ID, String.valueOf(tenantId)); - TaskInfo taskInfo = new TaskInfo(TASK_NAME, TASK_CLASS, properties, + TaskInfo taskInfo = new TaskInfo(deviceType, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); 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 fcba894206..86433ce141 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 @@ -19,14 +19,12 @@ package org.wso2.carbon.device.mgt.core; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; -import org.wso2.carbon.device.mgt.common.TaskOperation; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; -import java.util.List; - public class TestDeviceManagementService implements DeviceManagementService { private String providerType; @@ -42,7 +40,7 @@ public class TestDeviceManagementService implements DeviceManagementService { } @Override - public List getTasksForPlatform(){ + public OperationMonitoringTaskConfig getOperationMonitoringConfig(){ return null; } 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 462258c681..67505dd633 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 @@ -48,10 +48,10 @@ windows - - true - 600000 - + + + + 20