|
|
@ -21,24 +21,18 @@ package org.wso2.carbon.device.mgt.core.status.task.impl;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
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.device.mgt.common.Device;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DynamicTaskContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceMonitoringData;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
|
|
|
|
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
|
|
|
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationEnrolmentMapping;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOFactory;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
|
|
|
|
import org.wso2.carbon.device.mgt.core.task.impl.DynamicPartitionedScheduleTask;
|
|
|
|
import org.wso2.carbon.ntask.core.Task;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
@ -67,64 +61,65 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
protected void setup() {
|
|
|
|
protected void setup() {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<DeviceMonitoringData> getAllDevicesForMonitoring() throws DeviceManagementException {
|
|
|
|
public void executeDynamicTask() {
|
|
|
|
|
|
|
|
List<OperationEnrolmentMapping> operationEnrolmentMappings;
|
|
|
|
|
|
|
|
List<EnrolmentInfo> enrolmentInfoTobeUpdated = new ArrayList<>();
|
|
|
|
|
|
|
|
Map<Integer, Long> lastActivities = null;
|
|
|
|
|
|
|
|
EnrolmentInfo enrolmentInfo;
|
|
|
|
|
|
|
|
DeviceIdentifier deviceIdentifier;
|
|
|
|
|
|
|
|
Device device;
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
operationEnrolmentMappings = this.getOperationEnrolmentMappings(super.getTaskContext());
|
|
|
|
DeviceManagementDAOFactory.openConnection();
|
|
|
|
if (!operationEnrolmentMappings.isEmpty()) {
|
|
|
|
DynamicTaskContext ctx = getTaskContext();
|
|
|
|
lastActivities = this.getLastDeviceActivities(super.getTaskContext());
|
|
|
|
if (ctx != null && ctx.isPartitioningEnabled()) {
|
|
|
|
}
|
|
|
|
return DeviceManagementDAOFactory.getDeviceDAO()
|
|
|
|
} catch (DeviceStatusTaskException e) {
|
|
|
|
.getAllDevicesForMonitoring(this.deviceTypeId, this.deviceType,
|
|
|
|
log.error("Error occurred while fetching OperationEnrolment mappings of deviceType '" + deviceType + "'", e);
|
|
|
|
ctx.getActiveServerCount(), ctx.getServerHashIndex());
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
for (OperationEnrolmentMapping mapping : operationEnrolmentMappings) {
|
|
|
|
|
|
|
|
long lastActivity = -1;
|
|
|
|
|
|
|
|
if (lastActivities != null && lastActivities.containsKey(mapping.getEnrolmentId())) {
|
|
|
|
|
|
|
|
lastActivity = lastActivities.get(mapping.getEnrolmentId());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
EnrolmentInfo.Status newStatus = this.determineDeviceStatus(mapping, lastActivity);
|
|
|
|
|
|
|
|
if (newStatus != mapping.getDeviceStatus()) {
|
|
|
|
|
|
|
|
deviceIdentifier = new DeviceIdentifier();
|
|
|
|
|
|
|
|
deviceIdentifier.setId(mapping.getDeviceId());
|
|
|
|
|
|
|
|
deviceIdentifier.setId(mapping.getDeviceType());
|
|
|
|
|
|
|
|
device = DeviceCacheManagerImpl.getInstance().getDeviceFromCache(deviceIdentifier, mapping.getTenantId());
|
|
|
|
|
|
|
|
if (device == null) {
|
|
|
|
|
|
|
|
enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setId(mapping.getEnrolmentId());
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(newStatus);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
DeviceManagementProviderService dmps = DeviceManagementDataHolder.getInstance()
|
|
|
|
return DeviceManagementDAOFactory.getDeviceDAO()
|
|
|
|
.getDeviceManagementProvider();
|
|
|
|
.getAllDevicesForMonitoring(this.deviceTypeId, this.deviceType, -1, -1);
|
|
|
|
try {
|
|
|
|
|
|
|
|
Device updatedDevice = dmps
|
|
|
|
|
|
|
|
.getDevice(device.getDeviceIdentifier(), device.getDeviceInfo().getUpdatedTime(), true);
|
|
|
|
|
|
|
|
if (updatedDevice != null) {
|
|
|
|
|
|
|
|
device = updatedDevice;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
} catch (DeviceManagementDAOException e) {
|
|
|
|
log.error(
|
|
|
|
String msg = "Error occurred while retrieving devices list for monitoring.";
|
|
|
|
"Error occurred while getting the updated device information which has device identifier: "
|
|
|
|
log.error(msg, e);
|
|
|
|
+ device.getDeviceIdentifier());
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while opening a connection to the data source";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new DeviceManagementException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
DeviceManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enrolmentInfo = device.getEnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(newStatus);
|
|
|
|
|
|
|
|
device.setEnrolmentInfo(enrolmentInfo);
|
|
|
|
|
|
|
|
DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, device, mapping.getTenantId());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void executeDynamicTask() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
List<EnrolmentInfo> enrolmentInfoTobeUpdated = new ArrayList<>();
|
|
|
|
|
|
|
|
List<DeviceMonitoringData> allDevicesForMonitoring = getAllDevicesForMonitoring();
|
|
|
|
|
|
|
|
long timeMillis = System.currentTimeMillis();
|
|
|
|
|
|
|
|
for (DeviceMonitoringData monitoringData : allDevicesForMonitoring) {
|
|
|
|
|
|
|
|
long lastUpdatedTime = (timeMillis - monitoringData
|
|
|
|
|
|
|
|
.getLastUpdatedTime()) / 1000;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EnrolmentInfo enrolmentInfo = new EnrolmentInfo();
|
|
|
|
|
|
|
|
enrolmentInfo.setId(monitoringData.getDevice().getEnrolmentInfo().getId());
|
|
|
|
|
|
|
|
EnrolmentInfo.Status status = null;
|
|
|
|
|
|
|
|
if (lastUpdatedTime >= this.deviceStatusTaskPluginConfig
|
|
|
|
|
|
|
|
.getIdleTimeToMarkInactive()) {
|
|
|
|
|
|
|
|
status = EnrolmentInfo.Status.INACTIVE;
|
|
|
|
|
|
|
|
} else if (lastUpdatedTime >= this.deviceStatusTaskPluginConfig
|
|
|
|
|
|
|
|
.getIdleTimeToMarkUnreachable()) {
|
|
|
|
|
|
|
|
status = EnrolmentInfo.Status.UNREACHABLE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (status != null) {
|
|
|
|
|
|
|
|
enrolmentInfo.setStatus(status);
|
|
|
|
enrolmentInfoTobeUpdated.add(enrolmentInfo);
|
|
|
|
enrolmentInfoTobeUpdated.add(enrolmentInfo);
|
|
|
|
if(log.isDebugEnabled()){
|
|
|
|
DeviceIdentifier deviceIdentifier =
|
|
|
|
log.debug("Enrollment Information updated for device ID : " + device.getDeviceIdentifier());
|
|
|
|
new DeviceIdentifier(monitoringData.getDevice()
|
|
|
|
}
|
|
|
|
.getDeviceIdentifier(), deviceType);
|
|
|
|
|
|
|
|
DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceIdentifier,
|
|
|
|
|
|
|
|
monitoringData.getTenantId());
|
|
|
|
|
|
|
|
DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier,
|
|
|
|
|
|
|
|
monitoringData.getDevice(), monitoringData.getTenantId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -132,38 +127,19 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
this.updateDeviceStatus(enrolmentInfoTobeUpdated);
|
|
|
|
this.updateDeviceStatus(enrolmentInfoTobeUpdated);
|
|
|
|
} catch (DeviceStatusTaskException e) {
|
|
|
|
} catch (DeviceStatusTaskException e) {
|
|
|
|
log.error("Error occurred while updating non-responsive device-status of devices of type '" + deviceType + "'", e);
|
|
|
|
log.error("Error occurred while updating non-responsive " +
|
|
|
|
}
|
|
|
|
"device-status of devices of type '" + deviceType + "'", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private EnrolmentInfo.Status determineDeviceStatus(OperationEnrolmentMapping opMapping, long lastActivityTime) {
|
|
|
|
|
|
|
|
long lastPendingOpBefore = (System.currentTimeMillis() / 1000) - opMapping.getCreatedTime();
|
|
|
|
|
|
|
|
EnrolmentInfo.Status newStatus = null;
|
|
|
|
|
|
|
|
if (lastPendingOpBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive()) {
|
|
|
|
|
|
|
|
newStatus = EnrolmentInfo.Status.INACTIVE;
|
|
|
|
|
|
|
|
} else if (lastPendingOpBefore >= this.deviceStatusTaskPluginConfig.getIdleTimeToMarkUnreachable()) {
|
|
|
|
|
|
|
|
newStatus = EnrolmentInfo.Status.UNREACHABLE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastActivityTime != -1) {
|
|
|
|
|
|
|
|
long lastActivityBefore = (System.currentTimeMillis() / 1000) - lastActivityTime;
|
|
|
|
|
|
|
|
if (lastActivityBefore < lastPendingOpBefore) {
|
|
|
|
|
|
|
|
return opMapping.getDeviceStatus();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return newStatus;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private long getMinTimeWindow() {
|
|
|
|
} catch (DeviceManagementException e) {
|
|
|
|
return (System.currentTimeMillis() / 1000) - this.deviceStatusTaskPluginConfig.getIdleTimeToMarkUnreachable();
|
|
|
|
String msg = "Error occurred while retrieving devices list for monitoring.";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private long getMaxTimeWindow() {
|
|
|
|
|
|
|
|
//Need to consider the frequency of the task as well
|
|
|
|
|
|
|
|
return (System.currentTimeMillis() / 1000) - this.deviceStatusTaskPluginConfig.getIdleTimeToMarkInactive() -
|
|
|
|
|
|
|
|
this.deviceStatusTaskPluginConfig.getFrequency();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean updateDeviceStatus(List<EnrolmentInfo> enrolmentInfos) throws
|
|
|
|
private boolean updateDeviceStatus(List<EnrolmentInfo> enrolmentInfos) throws
|
|
|
|
DeviceStatusTaskException {
|
|
|
|
DeviceStatusTaskException {
|
|
|
|
boolean updateStatus;
|
|
|
|
boolean updateStatus;
|
|
|
@ -184,56 +160,4 @@ public class DeviceStatusMonitoringTask extends DynamicPartitionedScheduleTask {
|
|
|
|
return updateStatus;
|
|
|
|
return updateStatus;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<OperationEnrolmentMapping> getOperationEnrolmentMappings(DynamicTaskContext ctx) throws DeviceStatusTaskException {
|
|
|
|
|
|
|
|
List<OperationEnrolmentMapping> operationEnrolmentMappings;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
if(ctx != null && ctx.isPartitioningEnabled()){
|
|
|
|
|
|
|
|
operationEnrolmentMappings = OperationManagementDAOFactory.
|
|
|
|
|
|
|
|
getOperationMappingDAO().getFirstPendingOperationMappingsForActiveEnrolments(this.getMinTimeWindow(),
|
|
|
|
|
|
|
|
this.getMaxTimeWindow(), this.deviceTypeId,
|
|
|
|
|
|
|
|
ctx.getActiveServerCount(), ctx.getServerHashIndex());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
operationEnrolmentMappings = OperationManagementDAOFactory.
|
|
|
|
|
|
|
|
getOperationMappingDAO().getFirstPendingOperationMappingsForActiveEnrolments(this.getMinTimeWindow(),
|
|
|
|
|
|
|
|
this.getMaxTimeWindow(), this.deviceTypeId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while getting Enrolment operation mappings for " +
|
|
|
|
|
|
|
|
"determining device status of deviceType '" + deviceType + "'", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new DeviceStatusTaskException("Error occurred obtaining a DB connection for fetching " +
|
|
|
|
|
|
|
|
"operation-enrolment mappings for status monitoring of deviceType '" + deviceType + "'", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return operationEnrolmentMappings;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Map<Integer, Long> getLastDeviceActivities(DynamicTaskContext ctx) throws DeviceStatusTaskException {
|
|
|
|
|
|
|
|
Map<Integer, Long> lastActivities;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.openConnection();
|
|
|
|
|
|
|
|
if(ctx != null && ctx.isPartitioningEnabled()) {
|
|
|
|
|
|
|
|
lastActivities = OperationManagementDAOFactory.
|
|
|
|
|
|
|
|
getOperationMappingDAO().getLastConnectedTimeForActiveEnrolments(this.getMaxTimeWindow(),
|
|
|
|
|
|
|
|
this.deviceTypeId,
|
|
|
|
|
|
|
|
ctx.getActiveServerCount(), ctx.getServerHashIndex());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
lastActivities = OperationManagementDAOFactory.
|
|
|
|
|
|
|
|
getOperationMappingDAO().getLastConnectedTimeForActiveEnrolments(this.getMaxTimeWindow(),
|
|
|
|
|
|
|
|
this.deviceTypeId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
throw new DeviceStatusTaskException("Error occurred while getting last activities for " +
|
|
|
|
|
|
|
|
"determining device status of deviceType '" + deviceType + "'", e);
|
|
|
|
|
|
|
|
} catch (OperationManagementDAOException e) {
|
|
|
|
|
|
|
|
throw new DeviceStatusTaskException("Error occurred obtaining a DB connection for fetching " +
|
|
|
|
|
|
|
|
"last activities for status monitoring of deviceType '" + deviceType + "'", e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
OperationManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastActivities;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|