From bec4ae681d8fae1f2fe75384970a2ce9bcdbb114 Mon Sep 17 00:00:00 2001 From: madhawap Date: Thu, 1 Dec 2016 15:28:22 +0530 Subject: [PATCH 1/3] Seperate out task operations --- .../config/DeviceTypeConfiguration.java | 26 ++++ .../deployer/config/TaskConfiguration.java | 83 ++++++++++++ .../template/DeviceTypeManagerService.java | 27 ++++ .../src/test/resources/sample.xml | 17 ++- .../service/api/DeviceManagementService.java | 4 +- .../device/mgt/common/TaskOperation.java | 28 ++++ .../common/spi/DeviceManagementService.java | 5 + .../operation/mgt/OperationManagerImpl.java | 63 +++++++-- .../DeviceManagementProviderService.java | 3 + .../DeviceManagementProviderServiceImpl.java | 23 ++++ .../mgt/core/task/DeviceTaskManager.java | 10 +- .../core/task/impl/DeviceTaskManagerImpl.java | 123 ++++++++++-------- .../impl/DeviceTaskManagerServiceImpl.java | 6 +- .../mgt/core/TestDeviceManagementService.java | 8 +- .../src/main/resources/conf/cdm-config.xml | 27 ---- 15 files changed, 345 insertions(+), 108 deletions(-) create mode 100644 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 create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TaskOperation.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/DeviceTypeConfiguration.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/DeviceTypeConfiguration.java index 4e8cfc4a88..c972d8de2e 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/DeviceTypeConfiguration.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/DeviceTypeConfiguration.java @@ -66,9 +66,35 @@ public class DeviceTypeConfiguration { protected License license; @XmlElement(name = "DataSource", required = true) protected DataSource dataSource; + @XmlElement(name = "TaskConfiguration", required = true) + private TaskConfiguration taskConfiguration; @XmlAttribute(name = "name") protected String name; + /** + * Gets the value of the taskConfiguration property. + * + * @return + * possible object is + * {@link TaskConfiguration } + * + */ + public TaskConfiguration getTaskConfiguration() { + return taskConfiguration; + } + + /** + * Sets the value of the taskConfiguration property. + * + * @param taskConfiguration + * allowed object is + * {@link TaskConfiguration } + * + */ + public void setTaskConfiguration(TaskConfiguration taskConfiguration) { + this.taskConfiguration = taskConfiguration; + } + /** * Gets the value of the deviceDetails property. * 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 new file mode 100644 index 0000000000..41532fa52b --- /dev/null +++ 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 @@ -0,0 +1,83 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +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) + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + @XmlElement(name = "Frequency", required = true) + public int getFrequency() { + return frequency; + } + + public void setFrequency(int frequency) { + 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() { + return operations; + } + + public void setOperations(List operations) { + this.operations = operations; + } + + @XmlRootElement(name = "Operation") + public static class Operation { + + private String operationName; + private int recurrency; + + @XmlElement(name = "Name", required = true) + public String getOperationName() { + return operationName; + } + + public void setOperationName(String operationName) { + this.operationName = operationName; + } + + @XmlElement(name = "RecurrentTimes", required = true) + public int getRecurrency() { + return recurrency; + } + + public void setRecurrency(int recurrency) { + this.recurrency = recurrency; + } + + } +} + 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 6602141b0e..c83318ca9b 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 @@ -23,6 +23,7 @@ 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.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; @@ -31,7 +32,9 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceTypeConfiguration; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.Property; import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.PushNotificationProvider; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.TaskConfiguration; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,6 +51,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { private PushNotificationConfig pushNotificationConfig; private ProvisioningConfig provisioningConfig; private String type; + private List taskOperations; public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, DeviceTypeConfiguration deviceTypeConfiguration) { @@ -55,6 +59,7 @@ public class DeviceTypeManagerService implements DeviceManagementService { this.deviceManager = new DeviceTypeManager(deviceTypeConfigIdentifier, deviceTypeConfiguration); this.setType(deviceTypeConfiguration.getName()); this.populatePushNotificationConfig(deviceTypeConfiguration.getPushNotificationProvider()); + this.setTask(deviceTypeConfiguration); } @Override @@ -62,6 +67,28 @@ public class DeviceTypeManagerService implements DeviceManagementService { return type; } + @Override + public List getTasksForPlatform(){ + return taskOperations; + } + + private void setTask(DeviceTypeConfiguration deviceTypeConfiguration) { + //Read the config file and take the list of operations there in the config + TaskConfiguration taskConfiguration = deviceTypeConfiguration.getTaskConfiguration(); + if (taskConfiguration != null) { + List ops = taskConfiguration.getOperations(); + if (ops != null && !ops.isEmpty()) { + taskOperations = new ArrayList<>(); + for (TaskConfiguration.Operation op : ops) { + TaskOperation taskOperation = new TaskOperation(); + taskOperation.setTaskName(op.getOperationName()); + taskOperation.setRecurrentTimes(op.getRecurrency()); + taskOperations.add(taskOperation); + } + } + } + } + @Override public void init() throws DeviceManagementException { } 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 1f0584227c..c3e88c32db 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 @@ -97,5 +97,20 @@ - + + + + DEVICE_INFO + 1 + + + APPLICATION_LIST + 5 + + + DEVICE_LOCATION + 1 + + + \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 650ae34f7b..411ce6b34a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -447,8 +447,8 @@ public interface DeviceManagementService { produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "Advanced Search for Devices", - notes = "Search for devices by filtering the search result through the specified search terms.", + value = "Advanced search for Devices", + notes = "search for devices by filtering the search result through the specified search terms.", tags = "Device Management", authorizations = { @Authorization( 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/TaskOperation.java new file mode 100644 index 0000000000..5fec851dc2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TaskOperation.java @@ -0,0 +1,28 @@ +package org.wso2.carbon.device.mgt.common; + +/** + * + */ +public class TaskOperation { + + private String taskName; + private int recurrentTimes; + + public String getTaskName() { + return taskName; + } + + public void setTaskName(String taskName) { + this.taskName = taskName; + } + + public int getRecurrentTimes() { + return recurrentTimes; + } + + public void setRecurrentTimes(int recurrentTimes) { + this.recurrentTimes = recurrentTimes; + } + +} + 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 25f11458c1..24836a02ae 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 @@ -21,9 +21,12 @@ 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.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. @@ -34,6 +37,8 @@ public interface DeviceManagementService { String getType(); + List getTasksForPlatform();//getTasksConfiguraitons + DeviceManager getDeviceManager(); ApplicationManager getApplicationManager(); 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 b5a7bad285..7f4e2f7a99 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 @@ -21,16 +21,25 @@ package org.wso2.carbon.device.mgt.core.operation.mgt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.CarbonContext; -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.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.InvalidDeviceException; +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; -import org.wso2.carbon.device.mgt.common.operation.mgt.*; +import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; +import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; @@ -43,6 +52,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator; +import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -51,7 +61,9 @@ 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 @@ -127,7 +139,7 @@ public class OperationManagerImpl implements OperationManager { org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil.convertOperation(operation); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); - boolean isScheduledOperation = this.isTaskScheduledOperation(operation); + boolean isScheduledOperation = this.isTaskScheduledOperation(operation, deviceIds); boolean isNotRepeated = false; boolean hasExistingTaskOperation; int enrolmentId; @@ -231,7 +243,7 @@ public class OperationManagerImpl implements OperationManager { List unAuthorizedDeviceList = new ArrayList<>(); DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); try { - if (operation != null && isAuthenticationSkippedOperation(operation)) { + if (operation != null && isAuthenticationSkippedOperation(operation, deviceIds)) { authorizedDeviceList = deviceIds; } else { boolean isAuthorized; @@ -873,11 +885,11 @@ public class OperationManagerImpl implements OperationManager { return CarbonContext.getThreadLocalCarbonContext().getUsername(); } - private boolean isAuthenticationSkippedOperation(Operation operation) { + private boolean isAuthenticationSkippedOperation(Operation operation, List deviceIds) { //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())) { + if (taskManager.isTaskOperation(operation.getCode(), deviceIds)) { return true; } @@ -1021,14 +1033,37 @@ public class OperationManagerImpl implements OperationManager { return resetStatus; } - private boolean isTaskScheduledOperation(Operation operation) { - TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getTaskConfiguration(); - for (TaskConfiguration.Operation op : taskConfiguration.getOperations()) { - if (operation.getCode().equals(op.getOperationName())) { - return true; + 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 + + 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; + } + } + } } + + } + +// TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). +// getTaskConfiguration(); +// for (TaskConfiguration.Operation op : taskConfiguration.getOperations()) { +// if (operation.getCode().equals(op.getOperationName())) { +// return true; +// } +// } return false; } 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 17732fea86..317f875b1c 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 @@ -27,6 +27,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import java.util.HashMap; import java.util.Date; import java.util.List; +import java.util.Map; /** * Proxy class for all Device Management related operations that take the corresponding plugin type in @@ -275,4 +276,6 @@ public interface DeviceManagementProviderService { int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementException; + Map> getTaskList(); + } 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 4053c641e3..30cf92dc36 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 @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -32,6 +33,7 @@ import org.wso2.carbon.device.mgt.common.FeatureManager; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; 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; @@ -1213,6 +1215,27 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return DeviceManagementDataHolder.getInstance().getOperationManager().getActivityCountUpdatedAfter(timestamp); } + @Override + public Map> getTaskList() { + 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; + } + @Override public List getDevicesOfUser(String username) throws DeviceManagementException { List devices = new ArrayList<>(); 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 9e3306cbc1..b7d59c9072 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,6 +19,8 @@ package org.wso2.carbon.device.mgt.core.task; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + import java.util.List; public interface DeviceTaskManager { @@ -29,8 +31,8 @@ public interface DeviceTaskManager { * @return - list of Task Operations. * @throws DeviceMgtTaskException */ - List getOperationList() throws DeviceMgtTaskException; - + List getOperationList(String deviceType) + throws DeviceMgtTaskException; /** * This method will take the monitoring frequency. @@ -66,7 +68,7 @@ public interface DeviceTaskManager { * @return * @throws DeviceMgtTaskException */ - List getValidOperationNames() throws DeviceMgtTaskException; + List getValidOperationNames(String deviceType) throws DeviceMgtTaskException; /** * This method will check wheather give operation is added by the task. @@ -74,6 +76,6 @@ public interface DeviceTaskManager { * @return - true or false * @throws DeviceMgtTaskException */ - boolean isTaskOperation(String opName); + boolean isTaskOperation(String opName, List deviceIds); } 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 dafd763e61..106159ef06 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,22 +22,27 @@ 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.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.config.task.TaskConfiguration; 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; 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.TaskOperation; import org.wso2.carbon.device.mgt.core.task.Utils; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import java.util.*; +import java.util.ArrayList; +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 { @@ -47,34 +52,35 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override - public List getOperationList() throws DeviceMgtTaskException { - TaskConfiguration taskConfiguration = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(); + //get device type specific operations + public List getOperationList(String deviceType) throws DeviceMgtTaskException { - List ops = taskConfiguration.getOperations(); List taskOperations = new ArrayList<>(); - for (TaskConfiguration.Operation op : ops) { - TaskOperation taskOperation = new TaskOperation(); - taskOperation.setTaskName(op.getOperationName()); - taskOperation.setRecurrentTimes(op.getRecurrency()); - taskOperation.setTaskPlatforms(op.getPlatforms()); - taskOperations.add(taskOperation); + 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 List getPlatformsForOperations(String opName) { + private List getDeviceTypes() { List operationPlatforms = new ArrayList<>(); - TaskConfiguration taskConfiguration = - DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getTaskConfiguration(); - List ops = taskConfiguration.getOperations(); - for (TaskConfiguration.Operation op : ops) { - if (op.getOperationName().equals(opName)) { - List platform = op.getPlatforms(); - for (String operationPlatform : platform) { - operationPlatforms.add(operationPlatform); - } - } + Map> deviceTypeSpecificTasks; + + DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). + getDeviceManagementProvider(); + deviceTypeSpecificTasks = deviceManagementProviderService.getTaskList(); + + Set platformTypes = deviceTypeSpecificTasks.keySet(); + for(String platformType : platformTypes ){ + operationPlatforms.add(platformType); } return operationPlatforms; } @@ -100,32 +106,31 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override public void addOperations() throws DeviceMgtTaskException { - List deviceTypes; DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); try { List devices; - List operations = this.getValidOperationNames(); - for (String taskOperation : operations) { - deviceTypes = getPlatformsForOperations(taskOperation); - for (String deviceType : deviceTypes) { - devices = deviceManagementProviderService.getAllDevices(deviceType); - 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."); - } - } - - } + 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."); + } + } + } } catch (InvalidDeviceException e) { throw new DeviceMgtTaskException("Invalid DeviceIdentifiers found.", e); @@ -137,8 +142,9 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } @Override - public List getValidOperationNames() throws DeviceMgtTaskException { - List taskOperations = this.getOperationList(); + public List getValidOperationNames(String deviceType) throws DeviceMgtTaskException { + + List taskOperations = this.getOperationList(deviceType); List opNames = new ArrayList<>(); Long milliseconds = System.currentTimeMillis(); int frequency = this.getTaskFrequency(); @@ -167,17 +173,22 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override - public boolean isTaskOperation(String opName) { - try { - List taskOperations = this.getOperationList(); - for (TaskOperation taop : taskOperations) { - if (taop.getTaskName().equalsIgnoreCase(opName)) { - return true; + 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; + } } + } 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 9a0b1b630f..87c45d079e 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 @@ -39,6 +39,8 @@ 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 TENANT_ID = "TENANT_ID"; + private static String TASK_CLASS = "org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask"; + private DeviceTaskManager deviceTaskManager; @@ -75,7 +77,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { if (!taskManager.isTaskScheduled(TASK_NAME)) { - TaskInfo taskInfo = new TaskInfo(TASK_NAME, deviceTaskManager.getTaskImplementedClazz(), + TaskInfo taskInfo = new TaskInfo(TASK_NAME, TASK_CLASS, properties, triggerInfo); taskManager.registerTask(taskInfo); @@ -130,7 +132,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { Map properties = new HashMap<>(); properties.put(TENANT_ID, String.valueOf(tenantId)); - TaskInfo taskInfo = new TaskInfo(TASK_NAME, deviceTaskManager.getTaskImplementedClazz(), properties, + TaskInfo taskInfo = new TaskInfo(TASK_NAME, 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 67e7d2a4f8..fcba894206 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 @@ -17,12 +17,11 @@ */ package org.wso2.carbon.device.mgt.core; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManager; 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.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; @@ -42,6 +41,11 @@ public class TestDeviceManagementService implements DeviceManagementService { return providerType; } + @Override + public List getTasksForPlatform(){ + return null; + } + @Override public void init() throws DeviceManagementException { 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 04924435fa..462258c681 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 @@ -51,33 +51,6 @@ true 600000 - org.wso2.carbon.device.mgt.core.task.impl.DeviceDetailsRetrieverTask - - - DEVICE_INFO - 1 - - android - ios - - - - APPLICATION_LIST - 5 - - android - ios - - - - DEVICE_LOCATION - 1 - - android - ios - - - From 2b00645779795d07cf8ca9eafd80529276cb9368 Mon Sep 17 00:00:00 2001 From: madhawap Date: Thu, 1 Dec 2016 16:31:27 +0530 Subject: [PATCH 2/3] refactored code --- .../deployer/config/TaskConfiguration.java | 19 +++++++++++++++++++ .../service/api/DeviceManagementService.java | 4 ++-- .../device/mgt/common/TaskOperation.java | 18 ++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) 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 41532fa52b..c2539d2e6e 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 @@ -1,3 +1,22 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.config; import javax.xml.bind.annotation.XmlElement; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 411ce6b34a..650ae34f7b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -447,8 +447,8 @@ public interface DeviceManagementService { produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON, httpMethod = "POST", - value = "Advanced search for Devices", - notes = "search for devices by filtering the search result through the specified search terms.", + value = "Advanced Search for Devices", + notes = "Search for devices by filtering the search result through the specified search terms.", tags = "Device Management", authorizations = { @Authorization( 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/TaskOperation.java index 5fec851dc2..a7143b7173 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/TaskOperation.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2015, 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.common; /** From 20ca76640d7fd58d2f5fa58488d861cbe59ae1af Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 1 Dec 2016 16:35:59 +0530 Subject: [PATCH 3/3] Adding changes for the grouping and ownership changes --- .../wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java | 7 +++++-- .../policy/mgt/core/impl/PolicyInformationPointImpl.java | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index bb2edf3586..18cf7a1560 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -76,7 +76,7 @@ public class PolicyFilterImpl implements PolicyFilter { continue; } else { for (DeviceGroupWrapper deviceGroupWrapper : wrappers) { - if (groupMap.containsKey(deviceGroupWrapper.getId()) && policyMap.containsKey(policy.getId())) { + if (groupMap.containsKey(deviceGroupWrapper.getId()) && !policyMap.containsKey(policy.getId())) { temp.add(policy); policyMap.put(policy.getId(), policy); } @@ -140,6 +140,9 @@ public class PolicyFilterImpl implements PolicyFilter { @Override public List filterOwnershipTypeBasedPolicies(String ownershipType, List policies) { + if (ownershipType == null) { + return policies; + } if (log.isDebugEnabled()) { log.debug("No of policies went in to filterOwnershipTypeBasedPolicies : " + policies.size()); log.debug("Ownership type : " + ownershipType); @@ -150,7 +153,7 @@ public class PolicyFilterImpl implements PolicyFilter { List temp = new ArrayList(); for (Policy policy : policies) { - if (ownershipType.equalsIgnoreCase(policy.getOwnershipType()) || + if (policy.getOwnershipType() == null || ownershipType.equalsIgnoreCase(policy.getOwnershipType()) || PolicyManagementConstants.ANY.equalsIgnoreCase(policy.getOwnershipType())) { temp.add(policy); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 4da09fbb09..d39822cb2c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -128,6 +128,12 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { } if (pipDevice.getDeviceGroups() != null && !pipDevice.getDeviceGroups().isEmpty()) { + Map groupMap = new HashMap<>(); + List groups = pipDevice.getDeviceGroups(); + for(DeviceGroup gr: groups){ + groupMap.put(gr.getGroupId(), gr); + } + policies = policyFilter.filterDeviceGroupsPolicies(groupMap, policies); } if (log.isDebugEnabled()) {