From d486ad14fd90b083f1438adc495c6d5bee58ea9d Mon Sep 17 00:00:00 2001 From: madhawap Date: Thu, 22 Dec 2016 13:47:39 +0530 Subject: [PATCH] Task operation config related changes --- .../mgt/common/MonitoringOperation.java | 1 - .../common/OperationMonitoringTaskConfig.java | 2 +- .../DeviceManagementPluginRepository.java | 1 - .../DeviceTaskManagerServiceComponent.java | 19 +++--- .../DeviceManagementProviderServiceImpl.java | 10 +--- .../task/impl/DeviceDetailsRetrieverTask.java | 20 ++++++- .../core/task/impl/DeviceTaskManagerImpl.java | 42 ++++++------- .../impl/DeviceTaskManagerServiceImpl.java | 60 ++++++++++--------- 8 files changed, 83 insertions(+), 72 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java index 7ce7000d815..8d5a8cc4341 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/MonitoringOperation.java @@ -42,6 +42,5 @@ public class MonitoringOperation { 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 index ec50a02b6fb..237516b26ad 100644 --- 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 @@ -52,6 +52,6 @@ public class OperationMonitoringTaskConfig { public void setMonitoringOperation(List monitoringOperation) { this.monitoringOperation = monitoringOperation; - } + } } 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 9e2a81d5c38..ca32fa037b8 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 @@ -195,7 +195,6 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis } else { deviceTaskManagerService.startTask(deviceManagementService.getType(), operationMonitoringTaskConfig); - // TODO: In here a race condition can arise. Need to handle it. } } } catch (DeviceMgtTaskException e) { 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 7dab5413215..344d567f0f8 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 @@ -58,11 +58,11 @@ public class DeviceTaskManagerServiceComponent { log.debug("Initializing device details retrieving task manager bundle."); } // This will start the device details retrieving task. -// DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); -// DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( -// deviceTaskManagerService); -// componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, -// deviceTaskManagerService, null); + // DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); + // DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( + // deviceTaskManagerService); + // componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, + // deviceTaskManagerService, null); getDeviceOperationMonitoringConfig(componentContext); @@ -76,12 +76,12 @@ public class DeviceTaskManagerServiceComponent { } } - private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) throws DeviceMgtTaskException { + private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) + throws DeviceMgtTaskException { DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); - DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService( - deviceTaskManagerService); + DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(deviceTaskManagerService); componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null); @@ -89,11 +89,10 @@ public class DeviceTaskManagerServiceComponent { Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); - for (String platformType : new ArrayList(deviceConfigMap.keySet())) { + for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) { deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); deviceConfigMap.remove(platformType); } - } @SuppressWarnings("unused") 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 210b0927cb3..e3f64f6d595 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 @@ -1222,17 +1222,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getMonitoringOperationList(String deviceType) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// 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(); } 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 37a90f6f823..69e49b78741 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 @@ -19,8 +19,10 @@ 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.device.mgt.common.OperationMonitoringTaskConfig; 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; @@ -32,10 +34,18 @@ public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); // private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); private String deviceType; + private String oppConfig; + private OperationMonitoringTaskConfig operationMonitoringTaskConfig; @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 @@ -49,14 +59,18 @@ public class DeviceDetailsRetrieverTask implements Task { log.debug("Device details retrieving task started to run."); } - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType); - + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, + operationMonitoringTaskConfig); + //pass the configurations also from here, monitoring tasks try { 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); } } + } 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 2606f07fd38..16fff9ce13b 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 @@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; 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.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; @@ -46,30 +47,26 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { private static Log log = LogFactory.getLog(DeviceTaskManagerImpl.class); private String deviceType; private static Map> map = new HashMap<>(); + private OperationMonitoringTaskConfig operationMonitoringTaskConfig; + + public DeviceTaskManagerImpl(String deviceType, + OperationMonitoringTaskConfig operationMonitoringTaskConfig) { + this.operationMonitoringTaskConfig = operationMonitoringTaskConfig; + this.deviceType = deviceType; + } public DeviceTaskManagerImpl(String deviceType) { this.deviceType = deviceType; } //get device type specific operations - public List getOperationList() throws DeviceMgtTaskException { - - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.getMonitoringOperationList( - deviceType);//Get task list from each device type + private List getOperationList() throws DeviceMgtTaskException { + return operationMonitoringTaskConfig.getMonitoringOperation(); } @Override public int getTaskFrequency() throws DeviceMgtTaskException { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.getDeviceMonitoringFrequency(deviceType); - + return operationMonitoringTaskConfig.getFrequency(); } // @Override @@ -80,11 +77,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override public boolean isTaskEnabled() throws DeviceMgtTaskException { - DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder - .getInstance(). - getDeviceManagementProvider(); - - return deviceManagementProviderService.isDeviceMonitoringEnabled(deviceType); + return operationMonitoringTaskConfig.isEnabled(); } @@ -149,13 +142,22 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { return opNames; } + private List getOperationListforTask() throws DeviceMgtTaskException { + + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder + .getInstance(). + getDeviceManagementProvider(); + + return deviceManagementProviderService.getMonitoringOperationList( + deviceType);//Get task list from each device type + } @Override public boolean isTaskOperation(String opName) { try { - List monitoringOperations = this.getOperationList(); + List monitoringOperations = this.getOperationListforTask(); for (MonitoringOperation taop : monitoringOperations) { if (taop.getTaskName().equalsIgnoreCase(opName)) { return true; 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 7a47f8d6bfd..d23939f866b 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 @@ -19,6 +19,7 @@ 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; @@ -46,9 +47,8 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class); @Override - public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { log.info("Task adding for " + deviceType); @@ -60,45 +60,52 @@ 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 : " + operationMonitoringTaskConfig - .getFrequency()); + // 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); + Gson gson = new Gson(); + String operationConfigs = gson.toJson(operationMonitoringTaskConfig); + Map properties = new HashMap<>(); + properties.put(TENANT_ID, String.valueOf(tenantId)); properties.put("DEVICE_TYPE", deviceType); + properties.put("OPPCONFIG", operationConfigs); + String taskName = deviceType + String.valueOf(tenantId); if (!taskManager.isTaskScheduled(deviceType)) { - 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()); } else { - throw new DeviceMgtTaskException("Device details retrieving task is already started for this tenant " + - tenantId); + throw new DeviceMgtTaskException( + "Device details retrieving task is already started for this tenant " + tenantId); } } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, + e); } } @Override - public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); @@ -108,18 +115,18 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { } } catch (TaskException e) { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId, + e); } } @Override - public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) throws DeviceMgtTaskException { - -// String TASK_NAME = deviceType; + public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) + throws DeviceMgtTaskException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); -// deviceTaskManager = new DeviceTaskManagerImpl(); + // deviceTaskManager = new DeviceTaskManagerImpl(); try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); @@ -134,21 +141,20 @@ 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(deviceType, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); taskManager.rescheduleTask(taskInfo.getName()); } else { - throw new DeviceMgtTaskException("Device details retrieving task has not been started for this tenant " + - tenantId + ". Please start the task first."); + throw new DeviceMgtTaskException( + "Device details retrieving task has not been started for this tenant " + + tenantId + ". Please start the task first."); } } catch (TaskException e) { - throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId, e); + throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId, + e); } - - } }