|
|
|
@ -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<String, String> 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<String, String> 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<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);
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|