diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml
index d975ad3715..df0c5cc586 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.api/pom.xml
@@ -460,5 +460,15 @@
io.entgra.device.mgt.core.apimgt.webapp.publisher
provided
+
+ io.entgra.device.mgt.core
+ io.entgra.device.mgt.core.task.mgt.core
+ provided
+
+
+ io.entgra.device.mgt.core
+ io.entgra.device.mgt.core.task.mgt.common
+ provided
+
\ No newline at end of file
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java
index d077b69922..ff2885c9b9 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.common/src/main/java/io/entgra/device/mgt/core/device/mgt/common/OperationMonitoringTaskConfig.java
@@ -29,6 +29,15 @@ public class OperationMonitoringTaskConfig {
private boolean isEnabled;
private int frequency;
private List monitoringOperation = new ArrayList<>();
+ private String cronExpression;
+
+ public String getCronExpression() {
+ return cronExpression;
+ }
+
+ public void setCronExpression(String cronExpression) {
+ this.cronExpression = cronExpression;
+ }
public boolean isEnabled() {
return isEnabled;
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml
index 9827ec7084..306f7adb2f 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml
@@ -115,7 +115,8 @@
org.apache.commons.codec.binary,
io.entgra.device.mgt.core.server.bootup.heartbeat.beacon,
io.entgra.device.mgt.core.device.mgt.extensions.logger.*,
- io.entgra.device.mgt.core.notification.logger.*
+ io.entgra.device.mgt.core.notification.logger.*,
+ io.entgra.device.mgt.core.task.mgt.core.*
!io.entgra.device.mgt.core.device.mgt.core.internal,
@@ -371,6 +372,16 @@
io.entgra.device.mgt.core
io.entgra.device.mgt.core.apimgt.extension.rest.api
+
+ io.entgra.device.mgt.core
+ io.entgra.device.mgt.core.task.mgt.core
+ provided
+
+
+ io.entgra.device.mgt.core
+ io.entgra.device.mgt.core.task.mgt.common
+ provided
+
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java
index 6ad7b53c74..dc8927563d 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/DeviceManagementPluginRepository.java
@@ -18,6 +18,10 @@
package io.entgra.device.mgt.core.device.mgt.core;
import com.google.gson.Gson;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper;
+import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException;
+import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
@@ -52,6 +56,7 @@ import io.entgra.device.mgt.core.device.mgt.core.util.DeviceManagerUtil;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
@@ -299,14 +304,16 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService.
getOperationMonitoringConfig();
+ DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper();
+ deviceTaskManagerWrapper.setDeviceType(deviceManagementService.getType());
+ deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(operationMonitoringTaskConfig);
if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
if (deviceTaskManagerService == null) {
DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap(
deviceManagementService.getType(), operationMonitoringTaskConfig);
} else {
- deviceTaskManagerService.startTask(deviceManagementService.getType(),
- operationMonitoringTaskConfig);
+ deviceTaskManagerService.startTask(deviceTaskManagerWrapper);
}
}
} catch (DeviceMgtTaskException e) {
@@ -322,14 +329,24 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService.
getOperationMonitoringConfig();
+ TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService();
if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
- deviceTaskManagerService.stopTask(deviceManagementService.getType(),
- deviceManagementService.getOperationMonitoringConfig());
+ Map> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants();
+
+ for (Map.Entry> entry : dynamicTasksForAllTenants.entrySet()) {
+ List dynamicTasks = entry.getValue();
+ for (DynamicTask task : dynamicTasks) {
+ deviceTaskManagerService.stopTask(task.getDynamicTaskId());
+ }
+ }
}
} catch (DeviceMgtTaskException e) {
throw new DeviceManagementException("Error occurred while removing task service for '" +
deviceManagementService.getType() + "'", e);
- }
+ } catch (TaskManagementException e) {
+ throw new DeviceManagementException("Error occurred while deleting task service for '" +
+ deviceManagementService.getType() + "'", e);
+ }
}
private void registerDeviceStatusMonitoringTask(DeviceType deviceType, DeviceManagementService deviceManagementService) throws
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/DeviceTaskManagerWrapper.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/DeviceTaskManagerWrapper.java
new file mode 100644
index 0000000000..898c1fbbef
--- /dev/null
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dto/DeviceTaskManagerWrapper.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2018 - 2024 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
+ *
+ * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
+ *
+ * Licensed under the Entgra Commercial License, Version 1.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * https://entgra.io/licenses/entgra-commercial/1.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 io.entgra.device.mgt.core.device.mgt.core.dto;
+
+import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+
+@ApiModel(
+ value = "DeviceTaskManagerWrapper",
+ description = "DeviceTaskManagerWrapper represents request payload of scheduling device details retriever task")
+public class DeviceTaskManagerWrapper {
+
+ @ApiModelProperty(name = "cron",
+ value = "Cron to be scheduled",
+ example = "0 0 * * * ? *, 0 0 0/8 * * ? *",
+ required = true)
+ private String cron;
+
+ @ApiModelProperty(name = "payload",
+ value = "Operation payload")
+ private Object payload;
+
+ @ApiModelProperty(name = "deviceType",
+ value = "Device type")
+ private String deviceType;
+
+ public OperationMonitoringTaskConfig getOperationMonitoringTaskConfig() {
+ return operationMonitoringTaskConfig;
+ }
+
+ public void setOperationMonitoringTaskConfig(OperationMonitoringTaskConfig operationMonitoringTaskConfig) {
+ this.operationMonitoringTaskConfig = operationMonitoringTaskConfig;
+ }
+
+ private OperationMonitoringTaskConfig operationMonitoringTaskConfig;
+
+
+ public String getDeviceType() {
+ return deviceType;
+ }
+
+ public void setDeviceType(String deviceType) {
+ this.deviceType = deviceType;
+ }
+
+ public String getCron() {
+ return cron;
+ }
+
+ public void setCron(String cron) {
+ this.cron = cron;
+ }
+
+ public Object getPayload() {
+ return payload;
+ }
+
+ public void setPayload(Object payload) {
+ this.payload = payload;
+ }
+}
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java
index 48d573a5fa..c4a2c2324c 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceManagementDataHolder.java
@@ -23,6 +23,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServi
import io.entgra.device.mgt.core.device.mgt.common.authorization.GroupAccessAuthorizationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
+import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.DeviceStatusTaskPluginConfig;
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
@@ -100,6 +101,7 @@ public class DeviceManagementDataHolder {
private DeviceStatusManagementService deviceStatusManagementService;
private APIApplicationServices apiApplicationServices;
private PublisherRESTAPIServices publisherRESTAPIServices;
+ private TaskManagementService taskManagementService;
private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap(
new HashMap<>());
@@ -457,4 +459,12 @@ public class DeviceManagementDataHolder {
public void setGroupAccessAuthorizationService(GroupAccessAuthorizationService groupAccessAuthorizationService) {
this.groupAccessAuthorizationService = groupAccessAuthorizationService;
}
+
+ public TaskManagementService getTaskManagementService() {
+ return taskManagementService;
+ }
+
+ public void setTaskManagementService(TaskManagementService taskManagementService) {
+ this.taskManagementService = taskManagementService;
+ }
}
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java
index 602e0700a1..05ce19cc21 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java
@@ -18,6 +18,10 @@
package io.entgra.device.mgt.core.device.mgt.core.internal;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper;
+import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException;
+import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext;
@@ -38,8 +42,16 @@ import io.entgra.device.mgt.core.device.mgt.core.status.task.impl.DeviceStatusTa
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService;
import io.entgra.device.mgt.core.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl;
+import org.wso2.carbon.base.MultitenantConstants;
+import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.ntask.core.service.TaskService;
+import org.wso2.carbon.user.api.Tenant;
+import org.wso2.carbon.user.api.UserStoreException;
+import org.wso2.carbon.user.core.service.RealmService;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
import java.util.Map;
/**
@@ -50,6 +62,12 @@ import java.util.Map;
* policy="dynamic"
* bind="setTaskService"
* unbind="unsetTaskService"
+ * @scr.reference name="io.entgra.task.mgt.service"
+ * interface="io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService"
+ * cardinality="1..1"
+ * policy="dynamic"
+ * bind="setTaskMgtService"
+ * unbind="unsetTaskMgtService"
*/
@SuppressWarnings("unused")
@@ -87,14 +105,41 @@ public class DeviceTaskManagerServiceComponent {
bundleContext.registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null);
Map deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap();
- for (String platformType : deviceConfigMap.keySet()) {
- OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType);
- if (taskConfig.isEnabled()) {
- deviceTaskManagerService.startTask(platformType, taskConfig);
+ List tenants = getAllTenants();
+ for (Tenant tenant : tenants) {
+ if (MultitenantConstants.SUPER_TENANT_ID == tenant.getId()) {
+ startTasksForTenant(deviceConfigMap,deviceTaskManagerService);
+ continue;
+ }
+ try {
+ PrivilegedCarbonContext.startTenantFlow();
+ PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId(), true);
+ startTasksForTenant(deviceConfigMap,deviceTaskManagerService);
+ } finally {
+ PrivilegedCarbonContext.endTenantFlow();
}
}
}
+ private List getAllTenants() {
+ List tenants = new ArrayList<>();
+ try {
+ RealmService realmService = DeviceManagementDataHolder.getInstance().getRealmService();
+ Tenant[] tenantArray = realmService.getTenantManager().getAllTenants();
+ if (tenantArray != null && tenantArray.length != 0) {
+ tenants.addAll(Arrays.asList(tenantArray));
+ }
+ Tenant superTenant = new Tenant();
+ superTenant.setId(-1234);
+ tenants.add(superTenant);
+ return tenants;
+ } catch (UserStoreException e) {
+ String msg = "Unable to load tenants";
+ log.error(msg, e);
+ return new ArrayList<>();
+ }
+ }
+
private void startDeviceStatusMonitoringTask(BundleContext bundleContext) {
DeviceStatusTaskManagerService deviceStatusTaskManagerService = new DeviceStatusTaskManagerServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceStatusTaskManagerService(deviceStatusTaskManagerService);
@@ -111,6 +156,18 @@ public class DeviceTaskManagerServiceComponent {
}
}
+ private void startTasksForTenant(Map deviceConfigMap,DeviceTaskManagerService deviceTaskManagerService) throws DeviceMgtTaskException {
+ DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper();
+ for (String platformType : deviceConfigMap.keySet()) {
+ deviceTaskManagerWrapper.setDeviceType(platformType);
+ OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType);
+ deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(taskConfig);
+ if (taskConfig.isEnabled()) {
+ deviceTaskManagerService.startTask(deviceTaskManagerWrapper);
+ }
+ }
+ }
+
private void startOperationTimeoutTask(BundleContext bundleContext) {
OperationTimeoutTaskManagerService operationTimeoutTaskManagerService =
new OperationTimeoutTaskManagerServiceImpl();
@@ -148,15 +205,19 @@ public class DeviceTaskManagerServiceComponent {
}
private void stopOperationMonitoringTask()
- throws DeviceMgtTaskException {
+ throws DeviceMgtTaskException, TaskManagementException {
DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance()
.getDeviceTaskManagerService();
+ TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService();
Map deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap();
- for (String platformType : deviceConfigMap.keySet()) {
- OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType);
- if (taskConfig.isEnabled()) {
- deviceTaskManagerService.stopTask(platformType, taskConfig);
+ Map> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants();
+
+ for (Map.Entry> entry : dynamicTasksForAllTenants.entrySet()) {
+ Integer tenantId = entry.getKey();
+ List dynamicTasks = entry.getValue();
+ for (DynamicTask task : dynamicTasks) {
+ deviceTaskManagerService.stopTask(task.getDynamicTaskId());
}
}
}
@@ -205,4 +266,20 @@ public class DeviceTaskManagerServiceComponent {
}
DeviceManagementDataHolder.getInstance().setTaskService(null);
}
+
+ @SuppressWarnings("unused")
+ protected void setTaskMgtService(TaskManagementService taskManagementService) {
+ if (log.isDebugEnabled()) {
+ log.debug("Setting the task service");
+ }
+ DeviceManagementDataHolder.getInstance().setTaskManagementService(taskManagementService);
+ }
+
+ @SuppressWarnings("unused")
+ protected void unsetTaskMgtService(TaskManagementService taskManagementService) {
+ if (log.isDebugEnabled()) {
+ log.debug("Removing the task service");
+ }
+ DeviceManagementDataHolder.getInstance().setTaskManagementService(null);
+ }
}
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerService.java
index b49fd30922..d83bf7aaa5 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerService.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/DeviceTaskManagerService.java
@@ -19,33 +19,34 @@
package io.entgra.device.mgt.core.device.mgt.core.task;
-import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskNotFoundException;
public interface DeviceTaskManagerService {
/**
* This method will start the task.
- * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
+ * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper
* @throws DeviceMgtTaskException
*/
- void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
+ void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException;
/**
* This method will stop the task.
- * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
* @throws DeviceMgtTaskException
*/
- void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
+ void stopTask(int taskId)
throws DeviceMgtTaskException;
/**
* This will update the task frequency which it runs.
- * @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
+ * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper
* @throws DeviceMgtTaskException
*/
- void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
- throws DeviceMgtTaskException;
+ void updateTask(int taskId,DeviceTaskManagerWrapper deviceTaskManagerWrapper)
+ throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException;
}
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java
index 6ae29437fb..3c078efa5f 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java
@@ -20,19 +20,19 @@ package io.entgra.device.mgt.core.device.mgt.core.task.impl;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.core.task.Utils;
+import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask;
+import io.entgra.device.mgt.core.task.mgt.common.constant.TaskMgtConstants;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException;
+import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.wso2.carbon.base.MultitenantConstants;
-import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
import io.entgra.device.mgt.core.device.mgt.common.StartupOperationConfig;
-import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManager;
-
-import java.util.List;
+import org.wso2.carbon.context.PrivilegedCarbonContext;
public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
@@ -42,44 +42,62 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
@Override
public void executeDynamicTask() {
- deviceType = getProperty("DEVICE_TYPE");
+ String taskId = getProperty(TaskMgtConstants.Task.DYNAMIC_TASK_ID);
+ String taskName = getProperty(TaskMgtConstants.Task.LOCAL_TASK_NAME);
+
+ if (StringUtils.isEmpty(taskId)) {
+ log.warn("Not executing the task as Task ID is undefined");
+ if (log.isDebugEnabled()) {
+ log.debug("[Undefined Task] Task Name: " + taskName);
+ }
+ return;
+ }
+
+ DynamicTask dynamicTask;
+ try {
+ dynamicTask = DeviceManagementDataHolder.getInstance().getTaskManagementService()
+ .getDynamicTask(Integer.parseInt(taskId));
+ } catch (TaskManagementException e) {
+ log.error("Task [" + taskId + "] " + taskName
+ + " won't execute as unable to get task management service.", e);
+ return;
+ }
+
+ if (dynamicTask == null) {
+ log.error("Task [" + taskId + "] " + taskName
+ + " won't execute as definition not available in the database.");
+ return;
+ }
+
+ deviceType = dynamicTask.getProperties().get("DEVICE_TYPE");
+ int tenantId = dynamicTask.getTenantId();
+
deviceManagementProviderService = DeviceManagementDataHolder.getInstance()
.getDeviceManagementProvider();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService
.getDeviceMonitoringConfig(deviceType);
StartupOperationConfig startupOperationConfig = deviceManagementProviderService
.getStartupOperationConfig(deviceType);
- this.executeForAllTenants(operationMonitoringTaskConfig, startupOperationConfig);
+ this.executeForATenant(operationMonitoringTaskConfig, startupOperationConfig, tenantId);
}
- private void executeForAllTenants(OperationMonitoringTaskConfig operationMonitoringTaskConfig,
- StartupOperationConfig startupOperationConfig) {
+ private void executeForATenant(OperationMonitoringTaskConfig operationMonitoringTaskConfig,
+ StartupOperationConfig startupOperationConfig,int tenantId) {
if (log.isDebugEnabled()) {
- log.debug("Device details retrieving task started to run for all tenants.");
+ log.debug("Device details retrieving task started to run for tenant " + tenantId);
}
try {
- List tenants = DeviceManagementDataHolder.getInstance().
- getDeviceManagementProvider().getDeviceEnrolledTenants();
- if (log.isDebugEnabled()) {
- log.debug("Task is running for " + tenants.size() + " tenants and the device type is " + deviceType);
+ try {
+ PrivilegedCarbonContext.startTenantFlow();
+ PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
+ this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenantId, deviceType));
+ } finally {
+ PrivilegedCarbonContext.endTenantFlow();
}
- for (Integer tenant : tenants) {
- if (MultitenantConstants.SUPER_TENANT_ID == tenant) {
- this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(MultitenantConstants.SUPER_TENANT_ID, deviceType));
- continue;
- }
- try {
- PrivilegedCarbonContext.startTenantFlow();
- PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant, true);
- this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenant, deviceType));
- } finally {
- PrivilegedCarbonContext.endTenantFlow();
- }
+ if (log.isDebugEnabled()) {
+ log.debug("Task is running for " + tenantId+ " tenants and the device type is " + deviceType);
}
- } catch (DeviceManagementException e) {
- log.error("Error occurred while trying to get the available tenants " +
- "from device manager provider service.", e);
} catch (MetadataManagementException e) {
String msg = "Error occurred while getting task frequency metadata entry.";
log.error(msg, e);
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java
index 03e5932423..efe1337f9e 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java
@@ -20,17 +20,17 @@
package io.entgra.device.mgt.core.device.mgt.core.task.impl;
import com.google.gson.Gson;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper;
+import io.entgra.device.mgt.core.task.mgt.common.bean.DynamicTask;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskManagementException;
+import io.entgra.device.mgt.core.task.mgt.common.exception.TaskNotFoundException;
+import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
-import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService;
-import org.wso2.carbon.ntask.common.TaskException;
-import org.wso2.carbon.ntask.core.TaskInfo;
-import org.wso2.carbon.ntask.core.TaskManager;
-import org.wso2.carbon.ntask.core.service.TaskService;
import java.util.HashMap;
import java.util.Map;
@@ -39,122 +39,77 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService {
public static final String TASK_TYPE = "DEVICE_MONITORING";
public static final String TENANT_ID = "TENANT_ID";
- private static String TASK_CLASS = "io.entgra.device.mgt.core.device.mgt.core.task.impl.DeviceDetailsRetrieverTask";
+ public static final String SCHEDULED_BY = "SCHEDULED_BY";
+ public static final String SCHEDULED_ON = "SCHEDULED_ON";
+ public static final String DEVICE_TYPE = "DEVICE_TYPE";
+ public static final String OPPCONFIG = "OPPCONFIG";
+ private final TaskManagementService taskManagementService = DeviceManagementDataHolder.getInstance().getTaskManagementService();;
-// private DeviceTaskManager deviceTaskManager;
private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class);
@Override
- public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
+ public void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException {
- log.info("Task adding for " + deviceType);
-
- int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
- TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
- taskService.registerTaskType(TASK_TYPE);
-
- 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());
- }
-
- TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
-
- TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
- // triggerInfo.setIntervalMillis(deviceTaskManager.getTaskFrequency());
- triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());
- triggerInfo.setRepeatCount(-1);
-
+ PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
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(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);
+ String operationConfigs = gson.toJson(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig());
+ Map taskProperties = new HashMap<>();
+ taskProperties.put(TENANT_ID,String.valueOf(carbonContext.getTenantId()));
+ taskProperties.put(SCHEDULED_BY,carbonContext.getUsername());
+ taskProperties.put(SCHEDULED_ON,String.valueOf(System.currentTimeMillis()));
+ taskProperties.put(DEVICE_TYPE, deviceTaskManagerWrapper.getDeviceType());
+ taskProperties.put(TASK_TYPE,TASK_TYPE);
+ taskProperties.put(OPPCONFIG,operationConfigs);
+
+ DynamicTask dynamicTask = new DynamicTask();
+ dynamicTask.setTaskClassName(DeviceDetailsRetrieverTask.class.getName());
+ dynamicTask.setCronExpression(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig().getCronExpression());
+ dynamicTask.setEnabled(true);
+ dynamicTask.setName(deviceTaskManagerWrapper.getDeviceType() + carbonContext.getTenantId());
+ dynamicTask.setProperties(taskProperties);
+ if(!taskManagementService.isTaskExist(dynamicTask.getName())){
+ taskManagementService.createTask(dynamicTask);
}
-
- } catch (TaskException e) {
- throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId,
- e);
+ log.info("Task adding for " + deviceTaskManagerWrapper.getDeviceType() + " tenant " + carbonContext.getTenantId());
+ } catch (TaskManagementException e) {
+ throw new DeviceMgtTaskException("Error occurred while creating the task for tenant ", e);
}
-
}
@Override
- public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
+ public void stopTask(int taskId)
throws DeviceMgtTaskException {
-
try {
- int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
- TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
- if (taskService != null && taskService.isServerInit()) {
- TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
- String taskName = deviceType + tenantId;
- taskManager.deleteTask(taskName);
- }
- } catch (TaskException e) {
- int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
- throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId,
- e);
+ taskManagementService.deleteTask(taskId);
+ } catch (TaskManagementException e) {
+ String msg = "Error occurred while retrieving task manager to delete task '" + taskId + "'";
+ log.error(msg);
+ throw new DeviceMgtTaskException(msg, e);
+ } catch (TaskNotFoundException e) {
+ String msg = "Task '" + taskId + "' not found while deleting the task";
+ log.error(msg, e);
+ throw new DeviceMgtTaskException(msg, e);
}
-
}
@Override
- public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig)
- throws DeviceMgtTaskException {
-
- int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
- // deviceTaskManager = new DeviceTaskManagerImpl();
+ public void updateTask(int taskId, DeviceTaskManagerWrapper deviceTaskManagerWrapper)
+ throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException {
try {
- TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService();
- TaskManager taskManager = taskService.getTaskManager(TASK_TYPE);
-
- if (taskManager.isTaskScheduled(deviceType)) {
- String taskName = deviceType + tenantId;
- taskManager.deleteTask(taskName);
- TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo();
- triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());
- triggerInfo.setRepeatCount(-1);
-
- Map properties = new HashMap<>();
- properties.put(TENANT_ID, String.valueOf(tenantId));
-
- TaskInfo taskInfo = new TaskInfo(taskName, 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.");
- }
-
- } catch (TaskException e) {
- throw new DeviceMgtTaskException("Error occurred while updating the task for tenant " + tenantId,
- e);
+ DynamicTask dynamicTask = taskManagementService.getDynamicTask(taskId);
+ dynamicTask.setEnabled(true);
+ dynamicTask.setCronExpression(deviceTaskManagerWrapper.getCron());
+ taskManagementService.updateTask(taskId,dynamicTask);
+ } catch (TaskManagementException e) {
+ String msg = "Error occurred while updating task '" + taskId + "'";
+ log.error(msg);
+ } catch (TaskNotFoundException e) {
+ String msg = "Task '" + taskId + "' not found while updating the task";
+ log.error(msg, e);
}
}
}
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java
index b357c94cca..4ad82b1e35 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/DeviceTypeManagerService.java
@@ -131,6 +131,7 @@ public class DeviceTypeManagerService implements DeviceManagementService {
if (taskConfiguration != null) {
operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled());
operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency());
+ operationMonitoringConfigs.setCronExpression(taskConfiguration.getCronExpression());
List ops = taskConfiguration.getOperations();
if (ops != null && !ops.isEmpty()) {
monitoringOperations = new ArrayList<>();
diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java
index 2f87930f7a..7b0ce2092e 100644
--- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java
+++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.extensions/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/type/template/config/TaskConfiguration.java
@@ -32,6 +32,16 @@ public class TaskConfiguration {
private boolean enabled;
private int frequency;
private List operations;
+ private String cronExpression;
+
+ @XmlElement(name = "cronExpression", required = true)
+ public String getCronExpression() {
+ return cronExpression;
+ }
+
+ public void setCronExpression(String cronExpression) {
+ this.cronExpression = cronExpression;
+ }
@XmlElement(name = "Enable", required = true)
public boolean isEnabled() {
diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java
index ab8a02199f..3216dba9b7 100755
--- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java
+++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.common/src/main/java/io/entgra/device/mgt/core/task/mgt/common/spi/TaskManagementService.java
@@ -43,4 +43,6 @@ public interface TaskManagementService {
DynamicTask getDynamicTask(int dynamicTaskId) throws TaskManagementException;
List getActiveDynamicTasks() throws TaskManagementException;
+
+ boolean isTaskExist(String taskName) throws TaskManagementException;
}
diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java
index 7493e9a025..600292a8d3 100755
--- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java
+++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/DynamicTaskDAO.java
@@ -41,4 +41,6 @@ public interface DynamicTaskDAO {
List getActiveDynamicTasks(int tenantId) throws TaskManagementDAOException;
+ boolean isTaskExist(String taskName, int tenantId) throws TaskManagementDAOException;
+
}
diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java
index 843b5c9189..8364f13540 100755
--- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java
+++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/dao/impl/DynamicTaskDAOImpl.java
@@ -200,4 +200,27 @@ public class DynamicTaskDAOImpl implements DynamicTaskDAO {
return dynamicTasks;
}
+ @Override
+ public boolean isTaskExist(String taskName, int tenantId) throws TaskManagementDAOException {
+ PreparedStatement stmt;
+ ResultSet rs;
+ try {
+ Connection conn = TaskManagementDAOFactory.getConnection();
+ String sql = "SELECT COUNT(*) FROM DYNAMIC_TASK WHERE NAME = ? AND TENANT_ID = ?";
+ stmt = conn.prepareStatement(sql);
+ stmt.setString(1, taskName);
+ stmt.setInt(2, tenantId);
+ rs = stmt.executeQuery();
+
+ if (rs.next()) {
+ return rs.getInt(1) > 0;
+ }
+ return false;
+ } catch (SQLException e) {
+ String msg = "Error occurred while checking if task '" + taskName + "' exists";
+ log.error(msg, e);
+ throw new TaskManagementDAOException(msg, e);
+ }
+ }
+
}
diff --git a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java
index c794a4e934..271102f731 100755
--- a/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java
+++ b/components/task-mgt/task-manager/io.entgra.device.mgt.core.task.mgt.core/src/main/java/io/entgra/device/mgt/core/task/mgt/core/service/TaskManagementServiceImpl.java
@@ -93,8 +93,11 @@ public class TaskManagementServiceImpl implements TaskManagementService {
dynamicTaskPropDAO.addTaskProperties(dynamicTaskId, dynamicTask.getProperties(), tenantId);
try {
- serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService()
- .getServerCtxInfo().getLocalServerHashIdx();
+ serverHashIdx = 0;
+ if(TaskManagerDataHolder.getInstance().getHeartBeatService().isTaskPartitioningEnabled()){
+ serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService()
+ .getServerCtxInfo().getLocalServerHashIdx();
+ }
nTaskName = TaskManagementUtil.generateNTaskName(dynamicTaskId, serverHashIdx);
} catch (HeartBeatManagementException e) {
String msg = "Unexpected exception when getting server hash index.";
@@ -136,7 +139,13 @@ public class TaskManagementServiceImpl implements TaskManagementService {
String msg = "Error occurred while scheduling task '" + dynamicTask.getName() + "'";
log.error(msg, e);
throw new TaskManagementException(msg, e);
- } finally {
+ } catch (Exception e) {
+ TaskManagementDAOFactory.rollbackTransaction();
+ String msg = "Unexpected error occurred while scheduling task '" + dynamicTask.getName() + "'";
+ log.error(msg, e);
+ throw new TaskManagementException(msg, e);
+ }
+ finally {
TaskManagementDAOFactory.closeConnection();
}
}
@@ -430,6 +439,28 @@ public class TaskManagementServiceImpl implements TaskManagementService {
return dynamicTasks;
}
+ @Override
+ public boolean isTaskExist(String taskName) throws TaskManagementException {
+ int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
+ try {
+ TaskManagementDAOFactory.beginTransaction();
+ if (log.isDebugEnabled()) {
+ log.debug("Fetching the details of all active dynamic tasks");
+ }
+ boolean taskExists = dynamicTaskDAO.isTaskExist(taskName, tenantId);
+ TaskManagementDAOFactory.commitTransaction();
+ return taskExists;
+ } catch (TaskManagementDAOException e) {
+ String msg = "Error occurred while fetching all active dynamic tasks";
+ log.error(msg, e);
+ throw new TaskManagementException(msg, e);
+ } catch (TransactionManagementException e) {
+ throw new RuntimeException(e);
+ } finally {
+ TaskManagementDAOFactory.closeConnection();
+ }
+ }
+
// check whether task exist in the ntask core
private boolean isTaskExists(String taskName) throws TaskManagementException, TaskException {
if (StringUtils.isEmpty(taskName)) {
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java
index 9f459412b4..0a17b3285b 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.common/src/main/java/io/entgra/device/mgt/core/tenant/mgt/common/spi/TenantManagerService.java
@@ -29,4 +29,5 @@ public interface TenantManagerService {
void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException;
void deleteTenantApplicationData(int tenantId) throws TenantMgtException;
void deleteTenantDeviceData(int tenantId) throws TenantMgtException;
+ void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException;
}
\ No newline at end of file
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml
index 7eafae8b99..cdd7f07108 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/pom.xml
@@ -68,7 +68,11 @@
io.entgra.device.mgt.core.device.mgt.core.config,
io.entgra.device.mgt.core.device.mgt.core.dao.*,
org.wso2.carbon.user.core.service,
- org.wso2.carbon.context
+ org.wso2.carbon.context,
+ io.entgra.device.mgt.core.device.mgt.common.spi.*,
+ io.entgra.device.mgt.core.device.mgt.core.service.*,
+ io.entgra.device.mgt.core.device.mgt.core.task.*,
+ io.entgra.device.mgt.core.device.mgt.core.dto.*
!io.entgra.device.mgt.core.tenant.mgt.core.internal,
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java
index dce4d36e28..0c732925a3 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/TenantManager.java
@@ -17,6 +17,7 @@
*/
package io.entgra.device.mgt.core.tenant.mgt.core;
+import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException;
import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
@@ -64,4 +65,11 @@ public interface TenantManager {
* @throws TenantMgtException Throws when deleting Tenant related device data
*/
void deleteTenantDeviceData(int tenantId) throws TenantMgtException;
+
+ /**
+ * Add deviceDetailsRetrieverTask of a tenant
+ * @param tenantInfoBean ID of the tenant
+ * @throws TenantMgtException Throws when deleting Tenant related device data
+ */
+ void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException;
}
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java
index 76e6058f68..62b5e20fd3 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerImpl.java
@@ -20,10 +20,15 @@ package io.entgra.device.mgt.core.tenant.mgt.core.impl;
import io.entgra.device.mgt.core.application.mgt.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
+import io.entgra.device.mgt.core.device.mgt.common.exceptions.DeviceManagementException;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceTaskManagerWrapper;
+import io.entgra.device.mgt.core.device.mgt.core.dto.DeviceType;
+import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
+import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException;
import io.entgra.device.mgt.core.tenant.mgt.core.internal.TenantMgtDataHolder;
@@ -42,7 +47,6 @@ import io.entgra.device.mgt.core.device.mgt.common.roles.config.Role;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
-import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -231,6 +235,31 @@ public class TenantManagerImpl implements TenantManager {
}
+ @Override
+ public void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException{
+ initTenantFlow(tenantInfoBean);
+ try {
+ DeviceManagementProviderService dms = TenantMgtDataHolder.getInstance().getDeviceManagementProvider();
+ List deviceTypes = dms.getDeviceTypes();
+ DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper();
+ for (DeviceType deviceType : deviceTypes) {
+ deviceTaskManagerWrapper.setDeviceType(deviceType.getName());
+ deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(dms.getDeviceMonitoringConfig(deviceType.getName()));
+ TenantMgtDataHolder.getInstance().getDeviceTaskManagerService().startTask(deviceTaskManagerWrapper);
+ }
+ } catch (DeviceMgtTaskException e) {
+ String msg = "Error occurred while adding default TaskFrequencies";
+ log.error(msg, e);
+ throw new TenantMgtException(msg, e);
+ } catch (DeviceManagementException e) {
+ String msg = "Error occurred while getting DeviceManagementService";
+ log.error(msg, e);
+ throw new TenantMgtException(msg, e);
+ } finally {
+ endTenantFlow();
+ }
+ }
+
private void initTenantFlow(TenantInfoBean tenantInfoBean) {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java
index 848e7ed3a6..a284acea56 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/impl/TenantManagerServiceImpl.java
@@ -53,4 +53,9 @@ public class TenantManagerServiceImpl implements TenantManagerService {
public void deleteTenantDeviceData(int tenantId) throws TenantMgtException {
TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantDeviceData(tenantId);
}
+
+ @Override
+ public void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException {
+ TenantMgtDataHolder.getInstance().getTenantManager().addDeviceDetailsRetrieverTask(tenantInfoBean);
+ }
}
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java
index b386b436e7..0f54c8d6c2 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtDataHolder.java
@@ -20,6 +20,9 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
+import io.entgra.device.mgt.core.device.mgt.common.spi.DeviceManagementService;
+import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
+import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
import org.wso2.carbon.user.core.service.RealmService;
@@ -37,6 +40,12 @@ public class TenantMgtDataHolder {
private DeviceStatusManagementService deviceStatusManagementService;
private DeviceTaskConfigurationService deviceTaskConfigurationService;
+ private DeviceTaskManagerService deviceTaskManagerService;
+
+ private DeviceManagementService deviceManagementService;
+
+ private DeviceManagementProviderService deviceManagerProvider;
+
public RealmService getRealmService() {
return realmService;
}
@@ -45,6 +54,30 @@ public class TenantMgtDataHolder {
this.realmService = realmService;
}
+ public DeviceTaskManagerService getDeviceTaskManagerService() {
+ return deviceTaskManagerService;
+ }
+
+ public void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) {
+ this.deviceTaskManagerService = deviceTaskManagerService;
+ }
+
+ public DeviceManagementService getDeviceManagementService() {
+ return deviceManagementService;
+ }
+
+ public void setDeviceManagementService(DeviceManagementService deviceManagementService) {
+ this.deviceManagementService = deviceManagementService;
+ }
+
+ public DeviceManagementProviderService getDeviceManagementProvider() {
+ return deviceManagerProvider;
+ }
+
+ public void setDeviceManagementProvider(DeviceManagementProviderService deviceManagerProvider) {
+ this.deviceManagerProvider = deviceManagerProvider;
+ }
+
public ApplicationManager getApplicationManager() {
return applicationManager;
}
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java
index 9b7cc879fa..abbc06798a 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/internal/TenantMgtServiceComponent.java
@@ -20,8 +20,11 @@ package io.entgra.device.mgt.core.tenant.mgt.core.internal;
import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService;
+import io.entgra.device.mgt.core.device.mgt.core.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceTaskConfigurationServiceImpl;
+import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService;
+import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService;
import io.entgra.device.mgt.core.tenant.mgt.common.spi.TenantManagerService;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl;
@@ -49,6 +52,18 @@ import org.wso2.carbon.user.core.service.RealmService;
* policy="dynamic"
* bind="setRealmService"
* unbind="unsetRealmService"
+ * @scr.reference name="org.wso2.carbon.device.manager"
+ * interface="io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService"
+ * cardinality="1..1"
+ * policy="dynamic"
+ * bind="setDeviceManagementProviderService"
+ * unbind="unsetDeviceManagementProviderService"
+ * @scr.reference name="org.wso2.carbon.device.task.manager"
+ * interface="io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService"
+ * cardinality="1..1"
+ * policy="dynamic"
+ * bind="setDeviceTaskManagerService"
+ * unbind="unsetDeviceTaskManagerService"
*/
@SuppressWarnings("unused")
@@ -121,4 +136,28 @@ public class TenantMgtServiceComponent {
}
TenantMgtDataHolder.getInstance().setRealmService(null);
}
+
+ protected void setDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) {
+ if (log.isDebugEnabled()) {
+ }
+ TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(deviceTaskManagerService);
+ }
+
+ protected void unsetDeviceTaskManagerService(DeviceTaskManagerService deviceTaskManagerService) {
+ if (log.isDebugEnabled()) {
+ }
+ TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(null);
+ }
+
+ protected void setDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) {
+ if (log.isDebugEnabled()) {
+ }
+ TenantMgtDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProviderService);
+ }
+
+ protected void unsetDeviceManagementProviderService(DeviceManagementProviderService deviceManagementProviderService) {
+ if (log.isDebugEnabled()) {
+ }
+ TenantMgtDataHolder.getInstance().setDeviceTaskManagerService(null);
+ }
}
diff --git a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java
index e2e9c88864..3bd2edd2e5 100644
--- a/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java
+++ b/components/tenant-mgt/io.entgra.device.mgt.core.tenant.mgt.core/src/main/java/io/entgra/device/mgt/core/tenant/mgt/core/listener/DeviceMgtTenantListener.java
@@ -40,6 +40,7 @@ public class DeviceMgtTenantListener implements TenantMgtListener {
tenantManager.addDefaultAppCategories(tenantInfoBean);
tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean);
tenantManager.addDefaultDeviceTaskConfiguration(tenantInfoBean);
+ tenantManager.addDeviceDetailsRetrieverTask(tenantInfoBean);
} catch (TenantMgtException e) {
String msg = "Error occurred while executing tenant creation flow";
log.error(msg, e);