Add TaskManagementService for Device Task manager

pull/370/head
Pramila Niroshan 4 months ago
parent cb244642ea
commit dd71027f4b

@ -460,5 +460,15 @@
<artifactId>io.entgra.device.mgt.core.apimgt.webapp.publisher</artifactId> <artifactId>io.entgra.device.mgt.core.apimgt.webapp.publisher</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.task.mgt.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.task.mgt.common</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -29,6 +29,15 @@ public class OperationMonitoringTaskConfig {
private boolean isEnabled; private boolean isEnabled;
private int frequency; private int frequency;
private List<MonitoringOperation> monitoringOperation = new ArrayList<>(); private List<MonitoringOperation> monitoringOperation = new ArrayList<>();
private String cronExpression;
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
public boolean isEnabled() { public boolean isEnabled() {
return isEnabled; return isEnabled;

@ -115,7 +115,8 @@
org.apache.commons.codec.binary, org.apache.commons.codec.binary,
io.entgra.device.mgt.core.server.bootup.heartbeat.beacon, io.entgra.device.mgt.core.server.bootup.heartbeat.beacon,
io.entgra.device.mgt.core.device.mgt.extensions.logger.*, 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.*
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!io.entgra.device.mgt.core.device.mgt.core.internal, !io.entgra.device.mgt.core.device.mgt.core.internal,
@ -371,6 +372,16 @@
<groupId>io.entgra.device.mgt.core</groupId> <groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId> <artifactId>io.entgra.device.mgt.core.apimgt.extension.rest.api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.task.mgt.core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.entgra.device.mgt.core</groupId>
<artifactId>io.entgra.device.mgt.core.task.mgt.common</artifactId>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -18,6 +18,10 @@
package io.entgra.device.mgt.core.device.mgt.core; package io.entgra.device.mgt.core.device.mgt.core;
import com.google.gson.Gson; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
public class DeviceManagementPluginRepository implements DeviceManagerStartupListener { public class DeviceManagementPluginRepository implements DeviceManagerStartupListener {
@ -299,14 +304,16 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
getDeviceTaskManagerService(); getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService.
getOperationMonitoringConfig(); getOperationMonitoringConfig();
DeviceTaskManagerWrapper deviceTaskManagerWrapper = new DeviceTaskManagerWrapper();
deviceTaskManagerWrapper.setDeviceType(deviceManagementService.getType());
deviceTaskManagerWrapper.setOperationMonitoringTaskConfig(operationMonitoringTaskConfig);
if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
if (deviceTaskManagerService == null) { if (deviceTaskManagerService == null) {
DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap( DeviceMonitoringOperationDataHolder.getInstance().addOperationMonitoringConfigToMap(
deviceManagementService.getType(), operationMonitoringTaskConfig); deviceManagementService.getType(), operationMonitoringTaskConfig);
} else { } else {
deviceTaskManagerService.startTask(deviceManagementService.getType(), deviceTaskManagerService.startTask(deviceTaskManagerWrapper);
operationMonitoringTaskConfig);
} }
} }
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
@ -322,14 +329,24 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis
getDeviceTaskManagerService(); getDeviceTaskManagerService();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService. OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementService.
getOperationMonitoringConfig(); getOperationMonitoringConfig();
TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService();
if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) { if (operationMonitoringTaskConfig != null && operationMonitoringTaskConfig.isEnabled()) {
deviceTaskManagerService.stopTask(deviceManagementService.getType(), Map<Integer, List<DynamicTask>> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants();
deviceManagementService.getOperationMonitoringConfig());
for (Map.Entry<Integer, List<DynamicTask>> entry : dynamicTasksForAllTenants.entrySet()) {
List<DynamicTask> dynamicTasks = entry.getValue();
for (DynamicTask task : dynamicTasks) {
deviceTaskManagerService.stopTask(task.getDynamicTaskId());
}
}
} }
} catch (DeviceMgtTaskException e) { } catch (DeviceMgtTaskException e) {
throw new DeviceManagementException("Error occurred while removing task service for '" + throw new DeviceManagementException("Error occurred while removing task service for '" +
deviceManagementService.getType() + "'", e); 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 private void registerDeviceStatusMonitoringTask(DeviceType deviceType, DeviceManagementService deviceManagementService) throws

@ -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;
}
}

@ -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.authorization.GroupAccessAuthorizationService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceStatusManagementService; 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.server.bootup.heartbeat.beacon.service.HeartBeatManagementService;
import io.entgra.device.mgt.core.task.mgt.common.spi.TaskManagementService;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.DeviceStatusTaskPluginConfig;
import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig; import io.entgra.device.mgt.core.device.mgt.common.OperationMonitoringTaskConfig;
@ -100,6 +101,7 @@ public class DeviceManagementDataHolder {
private DeviceStatusManagementService deviceStatusManagementService; private DeviceStatusManagementService deviceStatusManagementService;
private APIApplicationServices apiApplicationServices; private APIApplicationServices apiApplicationServices;
private PublisherRESTAPIServices publisherRESTAPIServices; private PublisherRESTAPIServices publisherRESTAPIServices;
private TaskManagementService taskManagementService;
private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap( private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap(
new HashMap<>()); new HashMap<>());
@ -457,4 +459,12 @@ public class DeviceManagementDataHolder {
public void setGroupAccessAuthorizationService(GroupAccessAuthorizationService groupAccessAuthorizationService) { public void setGroupAccessAuthorizationService(GroupAccessAuthorizationService groupAccessAuthorizationService) {
this.groupAccessAuthorizationService = groupAccessAuthorizationService; this.groupAccessAuthorizationService = groupAccessAuthorizationService;
} }
public TaskManagementService getTaskManagementService() {
return taskManagementService;
}
public void setTaskManagementService(TaskManagementService taskManagementService) {
this.taskManagementService = taskManagementService;
}
} }

@ -18,6 +18,10 @@
package io.entgra.device.mgt.core.device.mgt.core.internal; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext; 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.DeviceMgtTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManagerService;
import io.entgra.device.mgt.core.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; 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.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; import java.util.Map;
/** /**
@ -50,6 +62,12 @@ import java.util.Map;
* policy="dynamic" * policy="dynamic"
* bind="setTaskService" * bind="setTaskService"
* unbind="unsetTaskService" * 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") @SuppressWarnings("unused")
@ -87,14 +105,41 @@ public class DeviceTaskManagerServiceComponent {
bundleContext.registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null); bundleContext.registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null);
Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap(); .getInstance().getOperationMonitoringConfigFromMap();
for (String platformType : deviceConfigMap.keySet()) { List<Tenant> tenants = getAllTenants();
OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); for (Tenant tenant : tenants) {
if (taskConfig.isEnabled()) { if (MultitenantConstants.SUPER_TENANT_ID == tenant.getId()) {
deviceTaskManagerService.startTask(platformType, taskConfig); startTasksForTenant(deviceConfigMap,deviceTaskManagerService);
continue;
}
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId(), true);
startTasksForTenant(deviceConfigMap,deviceTaskManagerService);
} finally {
PrivilegedCarbonContext.endTenantFlow();
} }
} }
} }
private List<Tenant> getAllTenants() {
List<Tenant> 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) { private void startDeviceStatusMonitoringTask(BundleContext bundleContext) {
DeviceStatusTaskManagerService deviceStatusTaskManagerService = new DeviceStatusTaskManagerServiceImpl(); DeviceStatusTaskManagerService deviceStatusTaskManagerService = new DeviceStatusTaskManagerServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceStatusTaskManagerService(deviceStatusTaskManagerService); DeviceManagementDataHolder.getInstance().setDeviceStatusTaskManagerService(deviceStatusTaskManagerService);
@ -111,6 +156,18 @@ public class DeviceTaskManagerServiceComponent {
} }
} }
private void startTasksForTenant(Map<String, OperationMonitoringTaskConfig> 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) { private void startOperationTimeoutTask(BundleContext bundleContext) {
OperationTimeoutTaskManagerService operationTimeoutTaskManagerService = OperationTimeoutTaskManagerService operationTimeoutTaskManagerService =
new OperationTimeoutTaskManagerServiceImpl(); new OperationTimeoutTaskManagerServiceImpl();
@ -148,15 +205,19 @@ public class DeviceTaskManagerServiceComponent {
} }
private void stopOperationMonitoringTask() private void stopOperationMonitoringTask()
throws DeviceMgtTaskException { throws DeviceMgtTaskException, TaskManagementException {
DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance() DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance()
.getDeviceTaskManagerService(); .getDeviceTaskManagerService();
TaskManagementService taskManagementService= DeviceManagementDataHolder.getInstance().getTaskManagementService();
Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder Map<String, OperationMonitoringTaskConfig> deviceConfigMap = DeviceMonitoringOperationDataHolder
.getInstance().getOperationMonitoringConfigFromMap(); .getInstance().getOperationMonitoringConfigFromMap();
for (String platformType : deviceConfigMap.keySet()) { Map<Integer, List<DynamicTask>> dynamicTasksForAllTenants = taskManagementService.getDynamicTasksForAllTenants();
OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType);
if (taskConfig.isEnabled()) { for (Map.Entry<Integer, List<DynamicTask>> entry : dynamicTasksForAllTenants.entrySet()) {
deviceTaskManagerService.stopTask(platformType, taskConfig); Integer tenantId = entry.getKey();
List<DynamicTask> dynamicTasks = entry.getValue();
for (DynamicTask task : dynamicTasks) {
deviceTaskManagerService.stopTask(task.getDynamicTaskId());
} }
} }
} }
@ -205,4 +266,20 @@ public class DeviceTaskManagerServiceComponent {
} }
DeviceManagementDataHolder.getInstance().setTaskService(null); 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);
}
} }

@ -19,33 +19,34 @@
package io.entgra.device.mgt.core.device.mgt.core.task; 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 { public interface DeviceTaskManagerService {
/** /**
* This method will start the task. * This method will start the task.
* @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException; throws DeviceMgtTaskException;
/** /**
* This method will stop the task. * This method will stop the task.
* @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) void stopTask(int taskId)
throws DeviceMgtTaskException; throws DeviceMgtTaskException;
/** /**
* This will update the task frequency which it runs. * This will update the task frequency which it runs.
* @param operationMonitoringTaskConfig - OperationMonitoringTaskConfig * @param deviceTaskManagerWrapper - DeviceTaskManagerWrapper
* @throws DeviceMgtTaskException * @throws DeviceMgtTaskException
*/ */
void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) void updateTask(int taskId,DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException; throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException;
} }

@ -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.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.core.task.Utils; 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.Log;
import org.apache.commons.logging.LogFactory; 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.OperationMonitoringTaskConfig;
import io.entgra.device.mgt.core.device.mgt.common.StartupOperationConfig; 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.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.service.DeviceManagementProviderService; 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.DeviceMgtTaskException;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManager; import io.entgra.device.mgt.core.device.mgt.core.task.DeviceTaskManager;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import java.util.List;
public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask { public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
@ -42,44 +42,62 @@ public class DeviceDetailsRetrieverTask extends DynamicPartitionedScheduleTask {
@Override @Override
public void executeDynamicTask() { 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() deviceManagementProviderService = DeviceManagementDataHolder.getInstance()
.getDeviceManagementProvider(); .getDeviceManagementProvider();
OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService OperationMonitoringTaskConfig operationMonitoringTaskConfig = deviceManagementProviderService
.getDeviceMonitoringConfig(deviceType); .getDeviceMonitoringConfig(deviceType);
StartupOperationConfig startupOperationConfig = deviceManagementProviderService StartupOperationConfig startupOperationConfig = deviceManagementProviderService
.getStartupOperationConfig(deviceType); .getStartupOperationConfig(deviceType);
this.executeForAllTenants(operationMonitoringTaskConfig, startupOperationConfig); this.executeForATenant(operationMonitoringTaskConfig, startupOperationConfig, tenantId);
} }
private void executeForAllTenants(OperationMonitoringTaskConfig operationMonitoringTaskConfig, private void executeForATenant(OperationMonitoringTaskConfig operationMonitoringTaskConfig,
StartupOperationConfig startupOperationConfig) { StartupOperationConfig startupOperationConfig,int tenantId) {
if (log.isDebugEnabled()) { 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 { try {
List<Integer> tenants = DeviceManagementDataHolder.getInstance(). try {
getDeviceManagementProvider().getDeviceEnrolledTenants(); PrivilegedCarbonContext.startTenantFlow();
if (log.isDebugEnabled()) { PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId, true);
log.debug("Task is running for " + tenants.size() + " tenants and the device type is " + deviceType); this.executeTask(operationMonitoringTaskConfig, startupOperationConfig, Utils.getTenantedTaskFrequency(tenantId, deviceType));
} finally {
PrivilegedCarbonContext.endTenantFlow();
} }
for (Integer tenant : tenants) { if (log.isDebugEnabled()) {
if (MultitenantConstants.SUPER_TENANT_ID == tenant) { log.debug("Task is running for " + tenantId+ " tenants and the device type is " + deviceType);
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();
}
} }
} catch (DeviceManagementException e) {
log.error("Error occurred while trying to get the available tenants " +
"from device manager provider service.", e);
} catch (MetadataManagementException e) { } catch (MetadataManagementException e) {
String msg = "Error occurred while getting task frequency metadata entry."; String msg = "Error occurred while getting task frequency metadata entry.";
log.error(msg, e); log.error(msg, e);

@ -20,17 +20,17 @@
package io.entgra.device.mgt.core.device.mgt.core.task.impl; package io.entgra.device.mgt.core.device.mgt.core.task.impl;
import com.google.gson.Gson; 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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; 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.internal.DeviceManagementDataHolder;
import io.entgra.device.mgt.core.device.mgt.core.task.DeviceMgtTaskException; 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.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.HashMap;
import java.util.Map; 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 TASK_TYPE = "DEVICE_MONITORING";
public static final String TENANT_ID = "TENANT_ID"; 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); private static Log log = LogFactory.getLog(DeviceTaskManagerServiceImpl.class);
@Override @Override
public void startTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) public void startTask(DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException { throws DeviceMgtTaskException {
log.info("Task adding for " + deviceType);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
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);
Gson gson = new Gson(); Gson gson = new Gson();
String operationConfigs = gson.toJson(operationMonitoringTaskConfig); String operationConfigs = gson.toJson(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig());
Map<String, String> taskProperties = new HashMap<>();
Map<String, String> properties = new HashMap<>(); taskProperties.put(TENANT_ID,String.valueOf(carbonContext.getTenantId()));
taskProperties.put(SCHEDULED_BY,carbonContext.getUsername());
properties.put(TENANT_ID, String.valueOf(tenantId)); taskProperties.put(SCHEDULED_ON,String.valueOf(System.currentTimeMillis()));
properties.put("DEVICE_TYPE", deviceType); taskProperties.put(DEVICE_TYPE, deviceTaskManagerWrapper.getDeviceType());
properties.put("OPPCONFIG", operationConfigs); taskProperties.put(TASK_TYPE,TASK_TYPE);
taskProperties.put(OPPCONFIG,operationConfigs);
String taskName = deviceType + String.valueOf(tenantId);
DynamicTask dynamicTask = new DynamicTask();
if (!taskManager.isTaskScheduled(deviceType)) { dynamicTask.setTaskClassName(DeviceDetailsRetrieverTask.class.getName());
dynamicTask.setCronExpression(deviceTaskManagerWrapper.getOperationMonitoringTaskConfig().getCronExpression());
TaskInfo taskInfo = new TaskInfo(taskName, TASK_CLASS, properties, triggerInfo); dynamicTask.setEnabled(true);
dynamicTask.setName(deviceTaskManagerWrapper.getDeviceType() + carbonContext.getTenantId());
taskManager.registerTask(taskInfo); dynamicTask.setProperties(taskProperties);
taskManager.rescheduleTask(taskInfo.getName()); if(!taskManagementService.isTaskExist(dynamicTask.getName())){
} else { taskManagementService.createTask(dynamicTask);
throw new DeviceMgtTaskException(
"Device details retrieving task is already started for this tenant " + tenantId);
} }
log.info("Task adding for " + deviceTaskManagerWrapper.getDeviceType() + " tenant " + carbonContext.getTenantId());
} catch (TaskException e) { } catch (TaskManagementException e) {
throw new DeviceMgtTaskException("Error occurred while creating the task for tenant " + tenantId, throw new DeviceMgtTaskException("Error occurred while creating the task for tenant ", e);
e);
} }
} }
@Override @Override
public void stopTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) public void stopTask(int taskId)
throws DeviceMgtTaskException { throws DeviceMgtTaskException {
try { try {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); taskManagementService.deleteTask(taskId);
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); } catch (TaskManagementException e) {
if (taskService != null && taskService.isServerInit()) { String msg = "Error occurred while retrieving task manager to delete task '" + taskId + "'";
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); log.error(msg);
String taskName = deviceType + tenantId; throw new DeviceMgtTaskException(msg, e);
taskManager.deleteTask(taskName); } catch (TaskNotFoundException e) {
} String msg = "Task '" + taskId + "' not found while deleting the task";
} catch (TaskException e) { log.error(msg, e);
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); throw new DeviceMgtTaskException(msg, e);
throw new DeviceMgtTaskException("Error occurred while deleting the task for tenant " + tenantId,
e);
} }
} }
@Override @Override
public void updateTask(String deviceType, OperationMonitoringTaskConfig operationMonitoringTaskConfig) public void updateTask(int taskId, DeviceTaskManagerWrapper deviceTaskManagerWrapper)
throws DeviceMgtTaskException { throws DeviceMgtTaskException, TaskNotFoundException, TaskManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
// deviceTaskManager = new DeviceTaskManagerImpl();
try { try {
TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); DynamicTask dynamicTask = taskManagementService.getDynamicTask(taskId);
TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); dynamicTask.setEnabled(true);
dynamicTask.setCronExpression(deviceTaskManagerWrapper.getCron());
if (taskManager.isTaskScheduled(deviceType)) { taskManagementService.updateTask(taskId,dynamicTask);
String taskName = deviceType + tenantId; } catch (TaskManagementException e) {
taskManager.deleteTask(taskName); String msg = "Error occurred while updating task '" + taskId + "'";
TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); log.error(msg);
triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency()); } catch (TaskNotFoundException e) {
triggerInfo.setRepeatCount(-1); String msg = "Task '" + taskId + "' not found while updating the task";
log.error(msg, e);
Map<String, String> 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);
} }
} }
} }

@ -131,6 +131,7 @@ public class DeviceTypeManagerService implements DeviceManagementService {
if (taskConfiguration != null) { if (taskConfiguration != null) {
operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled()); operationMonitoringConfigs.setEnabled(taskConfiguration.isEnabled());
operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency()); operationMonitoringConfigs.setFrequency(taskConfiguration.getFrequency());
operationMonitoringConfigs.setCronExpression(taskConfiguration.getCronExpression());
List<TaskConfiguration.Operation> ops = taskConfiguration.getOperations(); List<TaskConfiguration.Operation> ops = taskConfiguration.getOperations();
if (ops != null && !ops.isEmpty()) { if (ops != null && !ops.isEmpty()) {
monitoringOperations = new ArrayList<>(); monitoringOperations = new ArrayList<>();

@ -32,6 +32,16 @@ public class TaskConfiguration {
private boolean enabled; private boolean enabled;
private int frequency; private int frequency;
private List<Operation> operations; private List<Operation> 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) @XmlElement(name = "Enable", required = true)
public boolean isEnabled() { public boolean isEnabled() {

@ -43,4 +43,6 @@ public interface TaskManagementService {
DynamicTask getDynamicTask(int dynamicTaskId) throws TaskManagementException; DynamicTask getDynamicTask(int dynamicTaskId) throws TaskManagementException;
List<DynamicTask> getActiveDynamicTasks() throws TaskManagementException; List<DynamicTask> getActiveDynamicTasks() throws TaskManagementException;
boolean isTaskExist(String taskName) throws TaskManagementException;
} }

@ -41,4 +41,6 @@ public interface DynamicTaskDAO {
List<DynamicTask> getActiveDynamicTasks(int tenantId) throws TaskManagementDAOException; List<DynamicTask> getActiveDynamicTasks(int tenantId) throws TaskManagementDAOException;
boolean isTaskExist(String taskName, int tenantId) throws TaskManagementDAOException;
} }

@ -200,4 +200,27 @@ public class DynamicTaskDAOImpl implements DynamicTaskDAO {
return dynamicTasks; 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);
}
}
} }

@ -93,8 +93,11 @@ public class TaskManagementServiceImpl implements TaskManagementService {
dynamicTaskPropDAO.addTaskProperties(dynamicTaskId, dynamicTask.getProperties(), tenantId); dynamicTaskPropDAO.addTaskProperties(dynamicTaskId, dynamicTask.getProperties(), tenantId);
try { try {
serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService() serverHashIdx = 0;
.getServerCtxInfo().getLocalServerHashIdx(); if(TaskManagerDataHolder.getInstance().getHeartBeatService().isTaskPartitioningEnabled()){
serverHashIdx = TaskManagerDataHolder.getInstance().getHeartBeatService()
.getServerCtxInfo().getLocalServerHashIdx();
}
nTaskName = TaskManagementUtil.generateNTaskName(dynamicTaskId, serverHashIdx); nTaskName = TaskManagementUtil.generateNTaskName(dynamicTaskId, serverHashIdx);
} catch (HeartBeatManagementException e) { } catch (HeartBeatManagementException e) {
String msg = "Unexpected exception when getting server hash index."; 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() + "'"; String msg = "Error occurred while scheduling task '" + dynamicTask.getName() + "'";
log.error(msg, e); log.error(msg, e);
throw new TaskManagementException(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(); TaskManagementDAOFactory.closeConnection();
} }
} }
@ -430,6 +439,28 @@ public class TaskManagementServiceImpl implements TaskManagementService {
return dynamicTasks; 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 // check whether task exist in the ntask core
private boolean isTaskExists(String taskName) throws TaskManagementException, TaskException { private boolean isTaskExists(String taskName) throws TaskManagementException, TaskException {
if (StringUtils.isEmpty(taskName)) { if (StringUtils.isEmpty(taskName)) {

@ -29,4 +29,5 @@ public interface TenantManagerService {
void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException; void addDefaultDeviceTaskConfiguration(TenantInfoBean tenantInfoBean) throws TenantMgtException;
void deleteTenantApplicationData(int tenantId) throws TenantMgtException; void deleteTenantApplicationData(int tenantId) throws TenantMgtException;
void deleteTenantDeviceData(int tenantId) throws TenantMgtException; void deleteTenantDeviceData(int tenantId) throws TenantMgtException;
void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException;
} }

@ -68,7 +68,11 @@
io.entgra.device.mgt.core.device.mgt.core.config, io.entgra.device.mgt.core.device.mgt.core.config,
io.entgra.device.mgt.core.device.mgt.core.dao.*, io.entgra.device.mgt.core.device.mgt.core.dao.*,
org.wso2.carbon.user.core.service, 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.*
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!io.entgra.device.mgt.core.tenant.mgt.core.internal, !io.entgra.device.mgt.core.tenant.mgt.core.internal,

@ -17,6 +17,7 @@
*/ */
package io.entgra.device.mgt.core.tenant.mgt.core; 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 io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException;
import org.wso2.carbon.stratos.common.beans.TenantInfoBean; import org.wso2.carbon.stratos.common.beans.TenantInfoBean;
@ -64,4 +65,11 @@ public interface TenantManager {
* @throws TenantMgtException Throws when deleting Tenant related device data * @throws TenantMgtException Throws when deleting Tenant related device data
*/ */
void deleteTenantDeviceData(int tenantId) throws TenantMgtException; 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;
} }

@ -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.common.exception.ApplicationManagementException;
import io.entgra.device.mgt.core.application.mgt.core.config.ConfigurationManager; 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.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.common.exceptions.TransactionManagementException;
import io.entgra.device.mgt.core.device.mgt.core.dao.DeviceManagementDAOException; 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.DeviceManagementDAOFactory;
import io.entgra.device.mgt.core.device.mgt.core.dao.TenantDAO; 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.core.TenantManager;
import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException; import io.entgra.device.mgt.core.tenant.mgt.common.exception.TenantMgtException;
import io.entgra.device.mgt.core.tenant.mgt.core.internal.TenantMgtDataHolder; 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.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.user.api.UserStoreManager;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; 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<DeviceType> 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) { private void initTenantFlow(TenantInfoBean tenantInfoBean) {
PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();

@ -53,4 +53,9 @@ public class TenantManagerServiceImpl implements TenantManagerService {
public void deleteTenantDeviceData(int tenantId) throws TenantMgtException { public void deleteTenantDeviceData(int tenantId) throws TenantMgtException {
TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantDeviceData(tenantId); TenantMgtDataHolder.getInstance().getTenantManager().deleteTenantDeviceData(tenantId);
} }
@Override
public void addDeviceDetailsRetrieverTask(TenantInfoBean tenantInfoBean) throws TenantMgtException {
TenantMgtDataHolder.getInstance().getTenantManager().addDeviceDetailsRetrieverTask(tenantInfoBean);
}
} }

@ -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.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.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService; 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.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.WhiteLabelManagementService;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
@ -37,6 +40,12 @@ public class TenantMgtDataHolder {
private DeviceStatusManagementService deviceStatusManagementService; private DeviceStatusManagementService deviceStatusManagementService;
private DeviceTaskConfigurationService deviceTaskConfigurationService; private DeviceTaskConfigurationService deviceTaskConfigurationService;
private DeviceTaskManagerService deviceTaskManagerService;
private DeviceManagementService deviceManagementService;
private DeviceManagementProviderService deviceManagerProvider;
public RealmService getRealmService() { public RealmService getRealmService() {
return realmService; return realmService;
} }
@ -45,6 +54,30 @@ public class TenantMgtDataHolder {
this.realmService = realmService; 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() { public ApplicationManager getApplicationManager() {
return applicationManager; return applicationManager;
} }

@ -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.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.DeviceStatusManagementService;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.DeviceTaskConfigurationService; 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.DeviceStatusManagementServiceImpl;
import io.entgra.device.mgt.core.device.mgt.core.metadata.mgt.DeviceTaskConfigurationServiceImpl; 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.common.spi.TenantManagerService;
import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager; import io.entgra.device.mgt.core.tenant.mgt.core.TenantManager;
import io.entgra.device.mgt.core.tenant.mgt.core.impl.TenantManagerImpl; 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" * policy="dynamic"
* bind="setRealmService" * bind="setRealmService"
* unbind="unsetRealmService" * 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") @SuppressWarnings("unused")
@ -121,4 +136,28 @@ public class TenantMgtServiceComponent {
} }
TenantMgtDataHolder.getInstance().setRealmService(null); 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);
}
} }

@ -40,6 +40,7 @@ public class DeviceMgtTenantListener implements TenantMgtListener {
tenantManager.addDefaultAppCategories(tenantInfoBean); tenantManager.addDefaultAppCategories(tenantInfoBean);
tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean); tenantManager.addDefaultDeviceStatusFilters(tenantInfoBean);
tenantManager.addDefaultDeviceTaskConfiguration(tenantInfoBean); tenantManager.addDefaultDeviceTaskConfiguration(tenantInfoBean);
tenantManager.addDeviceDetailsRetrieverTask(tenantInfoBean);
} catch (TenantMgtException e) { } catch (TenantMgtException e) {
String msg = "Error occurred while executing tenant creation flow"; String msg = "Error occurred while executing tenant creation flow";
log.error(msg, e); log.error(msg, e);

Loading…
Cancel
Save