From 7ea609612ce9336e002f8fa728a2727bb8f597da Mon Sep 17 00:00:00 2001 From: charitha Date: Wed, 12 Sep 2018 13:29:23 +0530 Subject: [PATCH 1/3] Revert "This is the fix for the intermittent issues when applying policies on devices" This reverts commit ba8537e --- .../operation/mgt/OperationManager.java | 8 +- .../operation/mgt/OperationManagerImpl.java | 164 +----------------- .../core/operation/mgt/dao/OperationDAO.java | 4 +- .../mgt/dao/impl/GenericOperationDAOImpl.java | 31 ---- .../mgt/dao/impl/PolicyOperationDAOImpl.java | 75 +------- .../DeviceManagementProviderService.java | 5 - .../DeviceManagementProviderServiceImpl.java | 7 - .../PolicyEnforcementDelegator.java | 3 - .../PolicyEnforcementDelegatorImpl.java | 24 +-- 9 files changed, 12 insertions(+), 309 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java index 567cc1a1d6..d85108e960 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/operation/mgt/OperationManager.java @@ -22,8 +22,6 @@ import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; - import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import java.util.List; @@ -46,10 +44,6 @@ public interface OperationManager { Activity addOperation(Operation operation, List devices) throws OperationManagementException, InvalidDeviceException; - - void addOperationsForPolicyRevoke(Policy policy, List devices) throws OperationManagementException, - InvalidDeviceException; - /** * Method to retrieve the list of all operations to a device. * @@ -120,4 +114,4 @@ public interface OperationManager { */ NotificationStrategy getNotificationStrategy(); -} +} \ No newline at end of file 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 88d5e291f3..816948ea61 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 @@ -38,8 +38,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; -import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -66,7 +64,13 @@ import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.sql.SQLException; -import java.util.*; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * This class implements all the functionality exposed as part of the OperationManager. Any transaction initiated @@ -279,160 +283,6 @@ public class OperationManagerImpl implements OperationManager { } } - private Operation getPolicyRevokeOperation() { - CommandOperation policyRevokeOperation = new CommandOperation(); - policyRevokeOperation.setEnabled(true); - policyRevokeOperation.setCode(OperationMgtConstants.OperationCodes.POLICY_REVOKE); - policyRevokeOperation.setType(Operation.Type.COMMAND); - return policyRevokeOperation; - } - private Operation transformPolicy(Policy policy) { - List effectiveFeatures = policy.getProfile().getProfileFeaturesList(); - List profileOperationList = new ArrayList(); - PolicyOperation policyOperation = new PolicyOperation(); - policyOperation.setEnabled(true); - policyOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.POLICY); - policyOperation.setCode(PolicyOperation.POLICY_OPERATION_CODE); - for (ProfileFeature feature : effectiveFeatures) { - ProfileOperation profileOperation = new ProfileOperation(); - profileOperation.setCode(feature.getFeatureCode()); - profileOperation.setEnabled(true); - profileOperation.setStatus(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Status.PENDING); - profileOperation.setType(org.wso2.carbon.device.mgt.common.operation.mgt.Operation.Type.PROFILE); - profileOperation.setPayLoad(feature.getContent()); - profileOperationList.add(profileOperation); - } - policyOperation.setProfileOperations(profileOperationList); - policyOperation.setPayLoad(policyOperation.getProfileOperations()); - return policyOperation; - } - @Override - public void addOperationsForPolicyRevoke(Policy policy, List deviceIds) - throws OperationManagementException, InvalidDeviceException { - Operation revokeOperation = getPolicyRevokeOperation(); - Operation operation = transformPolicy(policy); - if (log.isDebugEnabled()) { - log.debug("operation:[" + operation.toString() + "]"); - for (DeviceIdentifier deviceIdentifier : deviceIds) { - 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 authorizedDeviceList = deviceAuthorizationResult.getValidDeviceIDList(); - OperationManagementDAOFactory.beginTransaction(); - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation policyOperationDto = - OperationDAOUtil.convertOperation(operation); - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation revokeOperationDto = - OperationDAOUtil.convertOperation(revokeOperation); - boolean isScheduledOperation = this.isTaskScheduledOperation(operation); - boolean isNotRepeated = false; - boolean isScheduled = false; - NotificationStrategy notificationStrategy = getNotificationStrategy(); - // check whether device list is greater than batch size notification strategy has enable to send push - // notification using scheduler task - if (DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). - getPushNotificationConfiguration().getSchedulerBatchSize() <= authorizedDeviceList.size() && - notificationStrategy != null) { - isScheduled = notificationStrategy.getConfig().isScheduled(); - } - List operationList = new LinkedList(); - operationList.add(revokeOperationDto); - operationList.add(policyOperationDto); - List operationIds = this.lookupOperationDAO(operation).addOperations(operationList); - List devices = new ArrayList<>(); - if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == policyOperationDto. - getControl()) { - isNotRepeated = true; - } - //Need to happen for both revoke and new policy operation - addOperationMappings(authorizedDeviceList, revokeOperationDto, operationIds.get(0), isScheduledOperation, - isNotRepeated, isScheduled, devices); - sendPushNotifications(revokeOperation, operationIds.get(0), isScheduled, notificationStrategy, devices); - //Need to happen for both revoke and new policy operation - addOperationMappings(authorizedDeviceList, policyOperationDto, operationIds.get(1), isScheduledOperation, - isNotRepeated, isScheduled, devices); - sendPushNotifications(operation, operationIds.get(1), isScheduled, notificationStrategy, devices); - OperationManagementDAOFactory.commitTransaction(); - } 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(); - } - } - private String addOperationMappings(List authorizedDeviceList, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto, int operationId, boolean isScheduledOperation, boolean isNotRepeated, boolean isScheduled, List devices) throws OperationManagementException, OperationManagementDAOException { - int enrolmentId; - int existingTaskOperationId;//TODO have to create a sql to load device details from deviceDAO using single query. - String operationCode = operationDto.getCode(); - for (DeviceIdentifier deviceId : authorizedDeviceList) { - Device device = getDevice(deviceId); - devices.add(device); - enrolmentId = device.getEnrolmentInfo().getId(); - //Do not repeat the task operations - if (isScheduledOperation) { - existingTaskOperationId = operationDAO.getExistingOperationID(enrolmentId, operationCode); - if (existingTaskOperationId != -1) { - operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); - } - } else if (isNotRepeated) { - operationDAO.updateEnrollmentOperationsStatus(enrolmentId, operationCode, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); - operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); - } else { - operationMappingDAO.addOperationMapping(operationId, enrolmentId, isScheduled); - } - } - return operationCode; - } - /* - * If notification strategy has not enable to send push notification using scheduler task we will send - * notification immediately. This is done in separate loop inorder to prevent overlap with DB insert - * operations with the possible db update operations trigger followed by pending operation call. - * Otherwise device may call pending operation while DB is locked for write and deadlock can occur. - */ - private void sendPushNotifications(Operation operation, int operationId, boolean isScheduled, NotificationStrategy notificationStrategy, List devices) { - int enrolmentId; - if (notificationStrategy != null && !isScheduled) { - for (Device device : devices) { - DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); - if (log.isDebugEnabled()) { - log.debug("Sending push notification to " + deviceId + " from add operation method."); - } - operation.setId(operationId); - operation.setActivityId(DeviceManagementConstants.OperationAttributes.ACTIVITY + operationId); - try { - notificationStrategy.execute(new NotificationContext(deviceId, operation)); - } catch (PushNotificationExecutionFailedException e) { - log.error("Error occurred while sending push notifications to " + deviceId.getType() + - " device carrying id '" + deviceId + "'", e); - /* - Reschedule if push notification failed. Doing db transactions in atomic way to prevent - deadlocks. - */ - enrolmentId = device.getEnrolmentInfo().getId(); - try { - operationMappingDAO.updateOperationMapping(operationId, enrolmentId, org.wso2.carbon - .device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED); - } catch (OperationManagementDAOException ex) { - // Not throwing this exception in order to keep sending remaining notifications if any. - log.error("Error occurred while setting push notification status to SCHEDULED.", ex); - } - } - } - } - } - private void sendNotification(Operation operation, Device device) { NotificationStrategy notificationStrategy = getNotificationStrategy(); /* diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java index a40c0c5e8f..67dcd517c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/OperationDAO.java @@ -30,8 +30,6 @@ public interface OperationDAO { int addOperation(Operation operation) throws OperationManagementDAOException; - List addOperations(List operations) throws OperationManagementDAOException; - Operation getOperation(int operationId) throws OperationManagementDAOException; Operation getOperationByDeviceAndId(int enrolmentId, int operationId) throws OperationManagementDAOException; @@ -90,4 +88,4 @@ public interface OperationDAO { Map> getOperationMappingsByStatus(Operation.Status opStatus, Operation.PushNotificationStatus pushNotificationStatus, int limit) throws OperationManagementDAOException; -} +} \ No newline at end of file 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 3ea9ba1f5e..5337c39f75 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 @@ -86,37 +86,6 @@ public class GenericOperationDAOImpl implements OperationDAO { } } - //This implementation has been done this way due to H2 not supporting batch inserts properly. - //Even though records are added in batch mode, only the id of the last added record will be returned, which is a problem. - public List addOperations(List operations) throws OperationManagementDAOException { - List ids = new LinkedList(); - for (Operation operation : operations) { - PreparedStatement stmt = null; - ResultSet rs = null; - try { - Connection connection = OperationManagementDAOFactory.getConnection(); - String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE) " + - "VALUES (?, ?, ?, ?)"; - stmt = connection.prepareStatement(sql, new String[]{"id"}); - stmt.setString(1, operation.getType().toString()); - stmt.setTimestamp(2, new Timestamp(new Date().getTime())); - stmt.setTimestamp(3, null); - stmt.setString(4, operation.getCode()); - stmt.executeUpdate(); - rs = stmt.getGeneratedKeys(); - int id = -1; - if (rs.next()) { - ids.add(rs.getInt(1)); - } - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding operation metadata", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt, rs); - } - } - return ids; - } - public boolean updateOperationStatus(int enrolmentId, int operationId, Operation.Status status) throws OperationManagementDAOException { PreparedStatement stmt = null; 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/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 15bf7cbe65..7f142b7d04 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.mgt.core.operation.mgt.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.core.dto.operation.mgt.CommandOperation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation; import org.wso2.carbon.device.mgt.core.dto.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOException; @@ -47,6 +46,7 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); operation.setId(operationId); operation.setEnabled(true); + PolicyOperation policyOperation = (PolicyOperation) operation; Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); @@ -82,79 +82,6 @@ public class PolicyOperationDAOImpl extends GenericOperationDAOImpl { return operationId; } - - private int addCommandOperation(int operationId, Operation operation) throws OperationManagementDAOException { - CommandOperation commandOp = (CommandOperation) operation; - PreparedStatement stmt = null; - try { - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_COMMAND_OPERATION(OPERATION_ID, ENABLED) VALUES(?, ?)"); - stmt.setInt(1, operationId); - stmt.setBoolean(2, commandOp.isEnabled()); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding command operation", e); - } finally { - OperationManagementDAOUtil.cleanupResources(stmt); - } - return operationId; - } - @Override - public List addOperations(List operations) throws OperationManagementDAOException { - List operationIds; - int counter = 0; - operationIds = super.addOperations(operations); - for(Operation operation : operations) { - if(operation.getType().equals(Operation.Type.COMMAND)){ - addCommandOperation(operationIds.get(counter), operation); - } else if(operation.getType().equals(Operation.Type.POLICY)){ - addPolicyOperation(operationIds.get(counter), operation); - } - counter++; - } - return operationIds; - } - private void addPolicyOperation(int operationId, Operation operation) throws OperationManagementDAOException { - PreparedStatement stmt = null; - ByteArrayOutputStream bao = null; - ObjectOutputStream oos = null; - try { - operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); - operation.setId(operationId); - operation.setEnabled(true); - Connection conn = OperationManagementDAOFactory.getConnection(); - stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + - "VALUES(?, ?)"); - bao = new ByteArrayOutputStream(); - oos = new ObjectOutputStream(bao); - oos.writeObject(operation); - stmt.setInt(1, operationId); - stmt.setBytes(2, bao.toByteArray()); - stmt.executeUpdate(); - } catch (SQLException e) { - throw new OperationManagementDAOException("Error occurred while adding policy operation", e); - } catch (IOException e) { - throw new OperationManagementDAOException("Error occurred while serializing policy operation object", e); - } finally { - if (bao != null) { - try { - bao.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ByteArrayOutputStream", e); - } - } - if (oos != null) { - try { - oos.close(); - } catch (IOException e) { - log.warn("Error occurred while closing ObjectOutputStream", e); - } - } - OperationManagementDAOUtil.cleanupResources(stmt); - } - } - - @Override public Operation getOperation(int operationId) throws OperationManagementDAOException { PreparedStatement stmt = null; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 97871a73f0..aac946a409 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -32,7 +32,6 @@ import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; @@ -542,10 +541,6 @@ public interface DeviceManagementProviderService { Activity addOperation(String type, Operation operation, List devices) throws OperationManagementException, InvalidDeviceException; - void addPolicyOperations(String type, Policy policy, - List devices) throws OperationManagementException, InvalidDeviceException; - - List getOperations(DeviceIdentifier deviceId) throws OperationManagementException; PaginationResult getOperations(DeviceIdentifier deviceId, 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 d772d02ae3..ae2b5d422f 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 @@ -53,7 +53,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; -import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationExecutionFailedException; import org.wso2.carbon.device.mgt.common.pull.notification.PullNotificationSubscriber; @@ -1424,12 +1423,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return pluginRepository.getOperationManager(type, this.getTenantId()).addOperation(operation, devices); } - @Override - public void addPolicyOperations(String type, Policy policy, - List devices) throws OperationManagementException, InvalidDeviceException { - pluginRepository.getOperationManager(type, this.getTenantId()).addOperationsForPolicyRevoke(policy, devices); - } - @Override public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()).getOperations(deviceId); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java index c603be7d84..aac07ce600 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegator.java @@ -33,7 +33,4 @@ public interface PolicyEnforcementDelegator { void addPolicyRevokeOperation(List deviceIdentifiers) throws PolicyDelegationException; - void revokePolicyOperation(List deviceIdentifiers, Policy policy) throws PolicyDelegationException; - - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java index 7471b7ae9f..fa02314460 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java @@ -81,7 +81,8 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato */ if (devicePolicy == null || devicePolicy.getId() != policy.getId() || updatedPolicyIds.contains (policy.getId())) { - this.revokePolicyOperation(deviceIdentifiers, policy); + this.addPolicyRevokeOperation(deviceIdentifiers); + this.addPolicyOperation(deviceIdentifiers, policy); } } else { //This means all the applicable policies have been removed from device. Hence calling a policy revoke. @@ -166,27 +167,6 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato return policyRevokeOperation; } - @Override - public void revokePolicyOperation(List deviceIdentifiers, Policy policy) throws - PolicyDelegationException { - try { - String type = null; - if (deviceIdentifiers.size() > 0) { - type = deviceIdentifiers.get(0).getType(); - } - PolicyManagementDataHolder.getInstance().getDeviceManagementService().addPolicyOperations(type, policy, - deviceIdentifiers); - } catch (InvalidDeviceException e) { - String msg = "Invalid DeviceIdentifiers found."; - log.error(msg, e); - throw new PolicyDelegationException(msg, e); - } catch (OperationManagementException e) { - String msg = "Error occurred while adding the operation to device."; - log.error(msg, e); - throw new PolicyDelegationException(msg, e); - } - } - /** * Provides the applied policy for give device * From fd8e73480d95033384a39233d2c5c7db426e4ebf Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 13 Sep 2018 15:28:58 +0530 Subject: [PATCH 2/3] Order by on operation mapping ID --- .../core/operation/mgt/dao/impl/GenericOperationDAOImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 5337c39f75..6725ca232b 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 @@ -1379,7 +1379,7 @@ public class GenericOperationDAOImpl implements OperationDAO { "OPERATION_CODE, om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + "WHERE dm.ENROLMENT_ID = ? AND dm.STATUS = ?) om ON o.ID = om.OPERATION_ID " + - "ORDER BY om.UPDATED_TIMESTAMP ASC LIMIT 1"); + "ORDER BY om.UPDATED_TIMESTAMP ASC, om.ID ASC LIMIT 1"); stmt.setInt(1, enrolmentId); stmt.setString(2, Operation.Status.PENDING.toString()); rs = stmt.executeQuery(); From 4c7bd0664d936921313f0ff45e7d8d57c23543e1 Mon Sep 17 00:00:00 2001 From: Milan Perera Date: Wed, 19 Sep 2018 16:30:41 +0530 Subject: [PATCH 3/3] Fix issue in order of get pending operations --- .../device/mgt/core/operation/mgt/OperationManagerImpl.java | 4 ++-- .../operation/mgt/dao/impl/GenericOperationDAOImpl.java | 6 ++++-- ...CreateTimeComparator.java => OperationIdComparator.java} | 6 ++---- 3 files changed, 8 insertions(+), 8 deletions(-) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/{OperationCreateTimeComparator.java => OperationIdComparator.java} (74%) 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 816948ea61..0e5306f6c2 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 @@ -57,7 +57,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder; -import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator; +import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationIdComparator; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; @@ -521,7 +521,7 @@ public class OperationManagerImpl implements OperationManager { operation = OperationDAOUtil.convertOperation(dtoOperation); operations.add(operation); } - Collections.sort(operations, new OperationCreateTimeComparator()); + Collections.sort(operations, new OperationIdComparator()); } catch (OperationManagementDAOException e) { throw new OperationManagementException("Error occurred while retrieving the list of " + "pending operations assigned for '" + deviceId.getType() + 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 6725ca232b..900c41cf96 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 @@ -1272,7 +1272,8 @@ public class GenericOperationDAOImpl implements OperationDAO { String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + "OPERATION_CODE, om.STATUS, om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + - "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC"; + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID " + + "ORDER BY o.CREATED_TIMESTAMP DESC, o.ID DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); rs = stmt.executeQuery(); @@ -1313,7 +1314,8 @@ public class GenericOperationDAOImpl implements OperationDAO { String sql = "SELECT o.ID, TYPE, o.CREATED_TIMESTAMP, o.RECEIVED_TIMESTAMP, " + "OPERATION_CODE, om.STATUS, om.ID AS OM_MAPPING_ID, om.UPDATED_TIMESTAMP FROM DM_OPERATION o " + "INNER JOIN (SELECT * FROM DM_ENROLMENT_OP_MAPPING dm " + - "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID ORDER BY o.CREATED_TIMESTAMP DESC LIMIT ?,?"; + "WHERE dm.ENROLMENT_ID = ?) om ON o.ID = om.OPERATION_ID " + + "ORDER BY o.CREATED_TIMESTAMP DESC, o.ID DESC LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, enrolmentId); stmt.setInt(2, request.getStartIndex()); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationIdComparator.java similarity index 74% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationIdComparator.java index 5f7e1a9eac..bbb565c3ef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationCreateTimeComparator.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/util/OperationIdComparator.java @@ -23,13 +23,11 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import java.io.Serializable; import java.util.Comparator; -public class OperationCreateTimeComparator implements Comparator, Serializable { +public class OperationIdComparator implements Comparator, Serializable { @Override public int compare(Operation o1, Operation o2) { - long createdTime1 = java.sql.Timestamp.valueOf(o1.getCreatedTimeStamp()).getTime(); - long createdTime2 = java.sql.Timestamp.valueOf(o1.getCreatedTimeStamp()).getTime(); - return (int) (createdTime1 - createdTime2); + return o1.getId() - o2.getId(); } }