From 84938791517531c0a44b766bac1535caf89908d6 Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 8 Oct 2020 17:14:00 +0530 Subject: [PATCH 1/3] Fixes for device enrollment status change issues --- .../mgt/dao/impl/GenericOperationDAOImpl.java | 1 + .../DeviceManagementProviderServiceImpl.java | 22 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java index fe26b7677b..37984c8709 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/GenericOperationDAOImpl.java @@ -674,6 +674,7 @@ public class GenericOperationDAOImpl implements OperationDAO { while (rs.next()) { if (enrolmentId == 0) { activity = new Activity(); + activity.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); activity.setType(Activity.Type.valueOf(rs.getString("OPERATION_TYPE"))); activity.setCreatedTimeStamp(new java.util.Date(rs.getLong(("CREATED_TIMESTAMP")) * 1000).toString()); activity.setCode(rs.getString("OPERATION_CODE")); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 1539c91e5b..20bb832532 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -2750,14 +2750,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (EnrolmentInfo.Status.REMOVED == newStatus) { isDeviceUpdated = disenrollDevice(deviceIdentifier); } else { - enrolmentInfo = device.getEnrolmentInfo(); - if (enrolmentInfo.getStatus() != newStatus) { - enrolmentInfo.setStatus(newStatus); - isDeviceUpdated = updateEnrollment(deviceId, enrolmentInfo, tenantId); - } else { - isDeviceUpdated = false; - } + enrolmentInfo.setStatus(newStatus); + isDeviceUpdated = updateEnrollment(deviceId, enrolmentInfo, tenantId); } + this.removeDeviceFromCache(deviceIdentifier); return isDeviceUpdated; } @@ -2793,15 +2789,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } boolean isUpdatedEnrollment = false; try { - DeviceManagementDAOFactory.openConnection(); + DeviceManagementDAOFactory.beginTransaction(); int updatedRows = enrollmentDAO.updateEnrollment(enrolmentInfo, tenantId); + DeviceManagementDAOFactory.commitTransaction(); if (updatedRows > 0) { isUpdatedEnrollment = true; } - } catch (SQLException e) { - String msg = "Error occurred while opening a connection to the data source"; - log.error(msg, e); - throw new DeviceManagementException(msg, e); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while updating the enrollment information device for" + @@ -3319,6 +3312,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); success = deviceDAO.setEnrolmentStatusInBulk(deviceType, status, getTenantId(), deviceList); DeviceManagementDAOFactory.commitTransaction(); + if (success) { + for (String id : deviceList) { + this.removeDeviceFromCache(new DeviceIdentifier(id, deviceType)); + } + } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred in while updating status of devices :" + deviceType + " status : " + status; From ce81eac4132cebcc8f1148d43fb7c7642b6062c9 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 8 Oct 2020 18:47:23 +0530 Subject: [PATCH 2/3] Improve endpoint-mgt functionalities --- .../mgt/common/device/details/DeviceInfo.java | 9 -- .../details/mgt/dao/DeviceDetailsDAO.java | 22 ---- .../mgt/dao/impl/DeviceDetailsDAOImpl.java | 23 +--- .../operation/mgt/OperationManagerImpl.java | 104 +++++++++--------- .../task/impl/DeviceStatusMonitoringTask.java | 6 +- 5 files changed, 56 insertions(+), 108 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java index 05c8345183..41b9439d16 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceInfo.java @@ -33,15 +33,6 @@ public class DeviceInfo implements Serializable { private static final long serialVersionUID = 1998101733L; -// @ApiModelProperty(name = "deviceId", value = "Device Id.", required = false) -// private int deviceId; -// -// @ApiModelProperty(name = "deviceType", value = "Type of the device.", required = true) -// private String deviceType; -// -// @ApiModelProperty(name = "deviceId", value = "Device identifier.", required = true) -// private DeviceIdentifier deviceIdentifier; - @ApiModelProperty(name = "IMEI", value = "IMEI number of the device.", required = true) private String IMEI; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index af0c930fcd..d3a30b0a50 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -129,26 +129,4 @@ public interface DeviceDetailsDAO { void updateDeviceInformation(int deviceId, int enrollmentId, DeviceInfo newDeviceInfo) throws DeviceDetailsMgtDAOException; void updateDeviceLocation(DeviceLocation deviceLocation, int enrollmentId) throws DeviceDetailsMgtDAOException; - -// /** -// * This method will add device application to database. -// * @param deviceApplication - Device application -// * @throws DeviceDetailsMgtDAOException -// */ -// void addDeviceApplications(DeviceApplication deviceApplication) throws DeviceDetailsMgtDAOException; -// -// /** -// * This method will return the device application list once device id is provided. -// * @param deviceId -// * @return -// * @throws DeviceDetailsMgtDAOException -// */ -// DeviceApplication getDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException; -// -// /** -// * This method will delete the application list from the database. -// * @param deviceId - Integer -// * @throws DeviceDetailsMgtDAOException -// */ -// void deleteDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index d192f3c55b..a37d7ba588 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -41,7 +41,7 @@ import java.util.Map; public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { - private static Log log = LogFactory.getLog(DeviceDetailsDAOImpl.class); + private static final Log log = LogFactory.getLog(DeviceDetailsDAOImpl.class); @Override public void addDeviceInformation(int deviceId, int enrolmentId, DeviceInfo deviceInfo) @@ -168,8 +168,6 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { rs = stmt.executeQuery(); if (rs.next()) { -// deviceInfo.setIMEI(rs.getString("IMEI")); -// deviceInfo.setIMSI(rs.getString("IMSI")); deviceInfo = new DeviceInfo(); deviceInfo.setDeviceModel(rs.getString("DEVICE_MODEL")); deviceInfo.setVendor(rs.getString("VENDOR")); @@ -180,9 +178,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { deviceInfo.setInternalAvailableMemory(rs.getDouble("INTERNAL_AVAILABLE_MEMORY")); deviceInfo.setExternalTotalMemory(rs.getDouble("EXTERNAL_TOTAL_MEMORY")); deviceInfo.setExternalAvailableMemory(rs.getDouble("EXTERNAL_AVAILABLE_MEMORY")); -// deviceInfo.setOperator(rs.getString("OPERATOR")); deviceInfo.setConnectionType(rs.getString("CONNECTION_TYPE")); -// deviceInfo.setMobileSignalStrength(rs.getDouble("MOBILE_SIGNAL_STRENGTH")); deviceInfo.setSsid(rs.getString("SSID")); deviceInfo.setCpuUsage(rs.getDouble("CPU_USAGE")); deviceInfo.setTotalRAMMemory(rs.getDouble("TOTAL_RAM_MEMORY")); @@ -190,7 +186,6 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { deviceInfo.setPluggedIn(rs.getBoolean("PLUGGED_IN")); deviceInfo.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); } - return deviceInfo; } catch (SQLException e) { throw new DeviceDetailsMgtDAOException("Error occurred while fetching the details of the registered devices.", e); @@ -508,21 +503,5 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } - - -// @Override -// public void addDeviceApplications(DeviceApplication deviceApplication) throws DeviceDetailsMgtDAOException { -// -// } -// -// @Override -// public DeviceApplication getDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException { -// return null; -// } -// -// @Override -// public void deleteDeviceApplications(int deviceId) throws DeviceDetailsMgtDAOException { -// -// } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index b2e7b769ae..f7e5908498 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -165,52 +165,52 @@ public class OperationManagerImpl implements OperationManager { if (log.isDebugEnabled()) { log.debug("operation:[" + operation.toString() + "]"); for (DeviceIdentifier deviceIdentifier : deviceIds) { - log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + - deviceIdentifier.getType() + "]"); + log.debug("device identifier id:[" + deviceIdentifier.getId() + "] type:[" + deviceIdentifier.getType() + + "]"); } } - try { - DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); - List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); - if (validDeviceIds.size() > 0) { - DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds); - List authorizedDeviceIds = deviceAuthorizationResult.getValidDeviceIDList(); - if (authorizedDeviceIds.size() <= 0) { - log.warn("User : " + getUser() + " is not authorized to perform operations on given device-list."); - Activity activity = new Activity(); - //Send the operation statuses only for admin triggered operations - activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, - deviceAuthorizationResult)); - return activity; - } - boolean isScheduledOperation = this.isTaskScheduledOperation(operation); - String initiatedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (initiatedBy == null && isScheduledOperation) { - if (log.isDebugEnabled()) { - log.debug("initiatedBy : " + SYSTEM); - } - operation.setInitiatedBy(SYSTEM); - } else { - if (log.isDebugEnabled()) { - log.debug("initiatedBy : " + initiatedBy); - } - operation.setInitiatedBy(initiatedBy); - } + DeviceIDHolder deviceValidationResult = DeviceManagerUtil.validateDeviceIdentifiers(deviceIds); + List validDeviceIds = deviceValidationResult.getValidDeviceIDList(); + if (!validDeviceIds.isEmpty()) { + DeviceIDHolder deviceAuthorizationResult = this.authorizeDevices(operation, validDeviceIds); + List authorizedDeviceIds = deviceAuthorizationResult.getValidDeviceIDList(); + if (authorizedDeviceIds.isEmpty()) { + log.warn("User : " + getUser() + " is not authorized to perform operations on given device-list."); + Activity activity = new Activity(); + //Send the operation statuses only for admin triggered operations + activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult)); + return activity; + } - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = - OperationDAOUtil.convertOperation(operation); - String operationCode = operationDto.getCode(); - Map enrolments = new HashMap<>(); - Device device; - for (DeviceIdentifier deviceId : authorizedDeviceIds) { - device = getDevice(deviceId); - enrolments.put(device.getEnrolmentInfo().getId(), device); + boolean isScheduledOperation = this.isTaskScheduledOperation(operation); + String initiatedBy = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + if (initiatedBy == null && isScheduledOperation) { + if (log.isDebugEnabled()) { + log.debug("initiatedBy : " + SYSTEM); + } + operation.setInitiatedBy(SYSTEM); + } else { + if (log.isDebugEnabled()) { + log.debug("initiatedBy : " + initiatedBy); } + operation.setInitiatedBy(initiatedBy); + } + + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil + .convertOperation(operation); + String operationCode = operationDto.getCode(); + Map enrolments = new HashMap<>(); + Device device; + for (DeviceIdentifier deviceId : authorizedDeviceIds) { + device = getDevice(deviceId); + enrolments.put(device.getEnrolmentInfo().getId(), device); + } + try { OperationManagementDAOFactory.beginTransaction(); - if (operationDto.getControl() == - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { + if (operationDto.getControl() + == org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { Map pendingOperationIDs = operationDAO .getExistingOperationIDs(enrolments.keySet().toArray(new Integer[0]), operationCode); for (Integer enrolmentId : pendingOperationIDs.keySet()) { @@ -227,8 +227,8 @@ public class OperationManagerImpl implements OperationManager { + operationCode); } Activity activity = new Activity(); - activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, - deviceAuthorizationResult)); + activity.setActivityStatus( + this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult)); return activity; } } @@ -242,20 +242,20 @@ public class OperationManagerImpl implements OperationManager { activity.setType(Activity.Type.valueOf(operationDto.getType().toString())); //For now set the operation statuses only for admin triggered operations if (!isScheduledOperation) { - activity.setActivityStatus(this.getActivityStatus(deviceValidationResult, - deviceAuthorizationResult)); + activity.setActivityStatus( + this.getActivityStatus(deviceValidationResult, deviceAuthorizationResult)); } return activity; - } else { - throw new InvalidDeviceException("Invalid device Identifiers found."); + } catch (OperationManagementDAOException e) { + OperationManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException("Error occurred while adding operation", e); + } catch (TransactionManagementException e) { + throw new OperationManagementException("Error occurred while initiating the transaction", e); + } finally { + OperationManagementDAOFactory.closeConnection(); } - } catch (OperationManagementDAOException e) { - OperationManagementDAOFactory.rollbackTransaction(); - throw new OperationManagementException("Error occurred while adding operation", e); - } catch (TransactionManagementException e) { - throw new OperationManagementException("Error occurred while initiating the transaction", e); - } finally { - OperationManagementDAOFactory.closeConnection(); + } else { + throw new InvalidDeviceException("Invalid device Identifiers found."); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java index 1bffb03eb9..f2deae4810 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java @@ -75,7 +75,7 @@ public class DeviceStatusMonitoringTask implements Task { Device device; try { operationEnrolmentMappings = this.getOperationEnrolmentMappings(); - if (operationEnrolmentMappings.size() > 0) { + if (!operationEnrolmentMappings.isEmpty()) { lastActivities = this.getLastDeviceActivities(); } } catch (DeviceStatusTaskException e) { @@ -107,7 +107,7 @@ public class DeviceStatusMonitoringTask implements Task { } } - if (enrolmentInfoTobeUpdated.size() > 0) { + if (!enrolmentInfoTobeUpdated.isEmpty()) { try { this.updateDeviceStatus(enrolmentInfoTobeUpdated); } catch (DeviceStatusTaskException e) { @@ -200,4 +200,4 @@ public class DeviceStatusMonitoringTask implements Task { } return lastActivities; } -} \ No newline at end of file +} From f85224f4ad620b9fb3a280273ebe1ffa40ea3ad7 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 8 Oct 2020 20:00:20 +0530 Subject: [PATCH 3/3] Update device cache if the device status has updated and the device info has updated --- .../task/impl/DeviceStatusMonitoringTask.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java index f2deae4810..71cc06a6d1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java @@ -25,13 +25,16 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; 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.dao.DeviceManagementDAOException; 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.ntask.core.Task; @@ -98,6 +101,19 @@ public class DeviceStatusMonitoringTask implements Task { enrolmentInfo.setId(mapping.getEnrolmentId()); enrolmentInfo.setStatus(newStatus); } else { + DeviceManagementProviderService dmps = DeviceManagementDataHolder.getInstance() + .getDeviceManagementProvider(); + try { + Device updatedDevice = dmps + .getDevice(device.getDeviceIdentifier(), device.getDeviceInfo().getUpdatedTime(), true); + if (updatedDevice != null) { + device = updatedDevice; + } + } catch (DeviceManagementException e) { + log.error( + "Error occurred while getting the updated device information which has device identifier: " + + device.getDeviceIdentifier()); + } enrolmentInfo = device.getEnrolmentInfo(); enrolmentInfo.setStatus(newStatus); device.setEnrolmentInfo(enrolmentInfo);