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