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 bd2b6d6f526..6944d605113 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 @@ -120,8 +120,8 @@ public class OperationManagerImpl implements OperationManager { boolean isNotRepeated = false; boolean hasExistingTaskOperation; int enrolmentId; - if (operationDto.getControl() == - org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { + if (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT == operationDto. + getControl()) { isNotRepeated = true; } @@ -357,12 +357,25 @@ public class OperationManagerImpl implements OperationManager { deviceId.getId() + "'"); } - int enrolmentId = this.getEnrolmentByStatus(deviceId, EnrolmentInfo.Status.ACTIVE); - if (enrolmentId < 0) { + // + EnrolmentInfo enrolmentInfo = this.getEnrolmentInfo(deviceId); + if (enrolmentInfo == null) { throw new OperationManagementException("Device not found for the given device Identifier:" + deviceId.getId() + " and given type:" + deviceId.getType()); } + int enrolmentId = enrolmentInfo.getId(); + //Changing the enrollment status & attempt count if the device is marked as inactive or unreachable + switch (enrolmentInfo.getStatus()) { + case ACTIVE: + this.resetAttemptCount(enrolmentId); + break; + case INACTIVE: + case UNREACHABLE: + this.resetAttemptCount(enrolmentId); + this.setEnrolmentStatus(deviceId, EnrolmentInfo.Status.ACTIVE); + break; + } try { OperationManagementDAOFactory.openConnection(); @@ -886,6 +899,65 @@ public class OperationManagerImpl implements OperationManager { return enrolmentId; } + private EnrolmentInfo getEnrolmentInfo(DeviceIdentifier deviceId) throws OperationManagementException { + EnrolmentInfo enrolmentInfo; + try { + DeviceManagementDAOFactory.openConnection(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + String user = this.getUser(); + enrolmentInfo = deviceDAO.getEnrolment(deviceId, user, tenantId); + } catch (DeviceManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving enrollment data of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return enrolmentInfo; + } + + private boolean setEnrolmentStatus(DeviceIdentifier deviceId, EnrolmentInfo.Status status) throws OperationManagementException { + boolean updateStatus; + try { + DeviceManagementDAOFactory.beginTransaction(); + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + String user = this.getUser(); + updateStatus = deviceDAO.setEnrolmentStatus(deviceId, user, status, tenantId); + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException("Error occurred while updating enrollment status of '" + + deviceId.getType() + "' device carrying the identifier '" + + deviceId.getId() + "'", e); + } catch (TransactionManagementException e) { + throw new OperationManagementException("Error occurred while initiating a transaction", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return updateStatus; + } + + private boolean resetAttemptCount(int enrolmentId) throws OperationManagementException { + boolean resetStatus; + try { + OperationManagementDAOFactory.beginTransaction(); + resetStatus = operationDAO.resetAttemptCount(enrolmentId); + OperationManagementDAOFactory.commitTransaction(); + } catch (OperationManagementDAOException e) { + OperationManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException("Error occurred while resetting attempt count of device id : '" + + enrolmentId + "'", e); + } catch (TransactionManagementException e) { + throw new OperationManagementException("Error occurred while initiating a transaction", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + return resetStatus; + } + private boolean isTaskScheduledOperation(Operation operation) { TaskConfiguration taskConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). getTaskConfiguration(); @@ -896,5 +968,4 @@ public class OperationManagerImpl implements OperationManager { } return false; } - -} +} \ 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/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 e07d251319d..99c71a5ba16 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 @@ -79,4 +79,6 @@ public interface OperationDAO { int getActivityCountUpdatedAfter(long timestamp) throws OperationManagementDAOException; + boolean resetAttemptCount(int enrolmentId) 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 8a162200328..760a2f6e754 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 @@ -37,6 +37,7 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import java.io.*; import java.sql.*; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; @@ -1072,4 +1073,29 @@ public class GenericOperationDAOImpl implements OperationDAO { } return operations; } + + @Override + public boolean resetAttemptCount(int enrolmentId) throws OperationManagementDAOException { + boolean status = false; + Connection conn; + PreparedStatement stmt = null; + Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + conn = OperationManagementDAOFactory.getConnection(); + String query = "UPDATE DM_POLICY_COMPLIANCE_STATUS SET ATTEMPTS = 0, LAST_REQUESTED_TIME = ? " + + "WHERE ENROLMENT_ID = ? AND TENANT_ID = ?"; + stmt = conn.prepareStatement(query); + stmt.setTimestamp(1, currentTimestamp); + stmt.setInt(2, enrolmentId); + stmt.setInt(3, tenantId); + stmt.executeUpdate(); + status = true; + } catch (SQLException e) { + throw new OperationManagementDAOException("Unable to reset the attempt count in database.", e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, null); + } + return status; + } } 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 83eae4cff44..42de5bba3cd 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 @@ -43,6 +43,16 @@ public interface DeviceManagementProviderService { */ List getAllDevices(String deviceType) throws DeviceManagementException; + /** + * Method to retrieve all the devices of a given device type without other device information. + * + * @param deviceType Device-type of the required devices + * @return List of devices of given device-type. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the + * devices. + */ + List getAllDevicesWithoutInfo(String deviceType) throws DeviceManagementException; + /** * Method to retrieve all the devices registered in the system. * 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 1e308f508c7..ccbb524820d 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 @@ -713,6 +713,80 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return devices; } + @Override + public List getAllDevicesWithoutInfo(String deviceType) throws DeviceManagementException { + List devices = new ArrayList<>(); + List allDevices; + try { + DeviceManagementDAOFactory.openConnection(); + allDevices = deviceDAO.getDevices(deviceType, this.getTenantId()); + if (allDevices == null) { + if (log.isDebugEnabled()) { + log.debug("No device is found upon the type '" + deviceType + "'"); + } + return null; + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + + deviceType + "' that are being managed within the scope of current tenant", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + for (Device device : allDevices) { + DeviceInfo info = null; + try { + DeviceManagementDAOFactory.openConnection(); + info = deviceInfoDAO.getDeviceInformation(device.getId()); + DeviceLocation location = deviceInfoDAO.getDeviceLocation(device.getId()); + if (info != null) { + info.setLocation(location); + } + } catch (DeviceDetailsMgtDAOException e) { + log.error("Error occurred while retrieving advance info of '" + device.getType() + + "' that carries the id '" + device.getDeviceIdentifier() + "'"); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + device.setDeviceInfo(info); + + try { + DeviceManagementDAOFactory.openConnection(); + List applications = applicationDAO.getInstalledApplications(device.getId()); + device.setApplications(applications); + } catch (DeviceManagementDAOException e) { + log.error("Error occurred while retrieving the application list of '" + device.getType() + "', " + + "which carries the id '" + device.getId() + "'", e); + } catch (SQLException e) { + log.error("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + DeviceManager deviceManager = this.getDeviceManager(deviceType); + if (deviceManager == null) { + if (log.isDebugEnabled()) { + log.debug("Device Manager associated with the device type '" + deviceType + "' is null. " + + "Therefore, not attempting method 'isEnrolled'"); + } + devices.add(device); + continue; + } + Device dmsDevice = + deviceManager.getDevice(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + if (dmsDevice != null) { + device.setFeatures(dmsDevice.getFeatures()); + device.setProperties(dmsDevice.getProperties()); + } + devices.add(device); + } + return devices; + } + @Override public void sendEnrolmentInvitation(EmailMetaInfo metaInfo) throws DeviceManagementException { Map, Object>> params = new HashMap<>(); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceDecisionPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceDecisionPoint.java index 8799653fe30..57f52e8fc04 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceDecisionPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/monitor/ComplianceDecisionPoint.java @@ -32,6 +32,8 @@ public interface ComplianceDecisionPoint { void setDevicesAsUnreachable(List deviceIdentifiers) throws PolicyComplianceException; + void setDevicesAsInactive(List deviceIdentifiers) throws PolicyComplianceException; + void setDevicesAsUnreachableWith(List devices) throws PolicyComplianceException; void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java index 28ab6040c7e..a3079fcb4c9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/PolicyManagerServiceImpl.java @@ -204,4 +204,4 @@ public class PolicyManagerServiceImpl implements PolicyManagerService { public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { return monitoringManager.isCompliance(deviceIdentifier); } -} +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index a7f42df06c7..c097494d2a2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -60,9 +60,8 @@ public interface MonitoringDAO { void deleteNoneComplianceData(int policyComplianceStatusId) throws MonitoringDAOException; - void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; + boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException; void updateAttempts(List deviceId, boolean reset) throws MonitoringDAOException; - -} +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index 20dac46f784..4a5d0e0bb92 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -400,7 +400,8 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public void updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { + public boolean updateAttempts(int deviceId, boolean reset) throws MonitoringDAOException { + boolean status = false; Connection conn; PreparedStatement stmt = null; Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); @@ -420,11 +421,13 @@ public class MonitoringDAOImpl implements MonitoringDAO { stmt.setInt(2, deviceId); stmt.setInt(3, tenantId); stmt.executeUpdate(); + status = true; } catch (SQLException e) { throw new MonitoringDAOException("Unable to update the attempts data in database.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } + return status; } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java index 77c00033d44..7baf29bf797 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/ComplianceDecisionPointImpl.java @@ -66,6 +66,22 @@ public class ComplianceDecisionPointImpl implements ComplianceDecisionPoint { } + @Override + public void setDevicesAsInactive(List deviceIdentifiers) throws PolicyComplianceException { + try { + DeviceManagementProviderService service = this.getDeviceManagementProviderService(); + for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { + Device device = service.getDevice(deviceIdentifier); + service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), + EnrolmentInfo.Status.INACTIVE); + } + } catch (DeviceManagementException e) { + String msg = "Error occurred while setting the device as inactive"; + log.error(msg, e); + throw new PolicyComplianceException(msg, e); + } + } + @Override public void setDevicesAsUnreachableWith(List devices) throws PolicyComplianceException { try { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index b83839e587a..91d16cb01a9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -16,7 +16,6 @@ * under the License. */ - package org.wso2.carbon.policy.mgt.core.mgt.impl; import org.apache.commons.logging.Log; @@ -75,7 +74,8 @@ public class MonitoringManagerImpl implements MonitoringManager { List complianceFeatures = new ArrayList<>(); try { - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + DeviceManagementProviderService service = + PolicyManagementDataHolder.getInstance().getDeviceManagementService(); PolicyManager manager = PolicyManagementDataHolder.getInstance().getPolicyManager(); Device device = service.getDevice(deviceIdentifier); Policy policy = manager.getAppliedPolicyToDevice(deviceIdentifier); @@ -90,7 +90,7 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.openConnection(); ComplianceData cmd = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); complianceData = monitoringService.checkPolicyCompliance(deviceIdentifier, - policy, deviceResponse); + policy, deviceResponse); complianceData.setId(cmd.getId()); complianceData.setPolicy(policy); @@ -100,8 +100,9 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a data source connection", e); } catch (MonitoringDAOException e) { - throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + throw new PolicyComplianceException( + "Unable to add the none compliance features to database for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -112,19 +113,20 @@ public class MonitoringManagerImpl implements MonitoringManager { try { PolicyManagementDAOFactory.beginTransaction(); monitoringDAO.setDeviceAsNoneCompliance(device.getId(), device.getEnrolmentInfo().getId(), - policy.getId()); + policy.getId()); if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } monitoringDAO.deleteNoneComplianceData(complianceData.getId()); monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(), - complianceFeatures); + complianceFeatures); PolicyManagementDAOFactory.commitTransaction(); } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyComplianceException("Unable to add the none compliance features to database for device " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + throw new PolicyComplianceException( + "Unable to add the none compliance features to database for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -146,8 +148,9 @@ public class MonitoringManagerImpl implements MonitoringManager { PolicyManagementDAOFactory.commitTransaction(); } catch (MonitoringDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); - throw new PolicyComplianceException("Unable to remove the none compliance features from database for device " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + throw new PolicyComplianceException( + "Unable to remove the none compliance features from database for device " + + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } finally { PolicyManagementDAOFactory.closeConnection(); } @@ -159,10 +162,10 @@ public class MonitoringManagerImpl implements MonitoringManager { } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable tor retrieve device data from DB for " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (PolicyManagerDAOException | PolicyManagementException e) { throw new PolicyComplianceException("Unable tor retrieve policy data from DB for device " + - deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } return complianceFeatures; } @@ -170,21 +173,22 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + DeviceManagementProviderService service = + PolicyManagementDataHolder.getInstance().getDeviceManagementService(); Device device = service.getDevice(deviceIdentifier); PolicyManagementDAOFactory.openConnection(); ComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() - .getId()); + .getId()); if (complianceData == null || !complianceData.isStatus()) { return false; } } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { @@ -195,12 +199,12 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public ComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws - PolicyComplianceException { - + PolicyComplianceException { ComplianceData complianceData; try { PolicyManagementDAOFactory.openConnection(); - DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + DeviceManagementProviderService service = + PolicyManagementDataHolder.getInstance().getDeviceManagementService(); Device device = service.getDevice(deviceIdentifier); complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); List complianceFeatures = @@ -209,11 +213,11 @@ public class MonitoringManagerImpl implements MonitoringManager { } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() + - " - " + deviceIdentifier.getType(), e); + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { @@ -232,6 +236,7 @@ public class MonitoringManagerImpl implements MonitoringManager { try { PolicyManagementDAOFactory.openConnection(); + //TODO: Return a map from getCompliance to reduce O(n^2) -> O(n) List cd = monitoringDAO.getCompliance(); for (Device device : devices) { @@ -266,7 +271,7 @@ public class MonitoringManagerImpl implements MonitoringManager { Map deviceIdsToAddOperation = new HashMap<>(); Map deviceIdsWithExistingOperation = new HashMap<>(); Map inactiveDeviceIds = new HashMap<>(); - Map deviceToMarkUnreachable = new HashMap<>(); + Map devicesToMarkUnreachable = new HashMap<>(); //Map firstTimeDeviceIdsWithPolicyIds = new HashMap<>(); List firstTimeDevices = new ArrayList<>(); @@ -281,18 +286,18 @@ public class MonitoringManagerImpl implements MonitoringManager { if (complianceData.getAttempts() == 0) { deviceIdsToAddOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + deviceIds.get(complianceData.getDeviceId())); } else { deviceIdsWithExistingOperation.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + deviceIds.get(complianceData.getDeviceId())); if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkUnreachable()) { - deviceToMarkUnreachable.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + devicesToMarkUnreachable.put(complianceData.getDeviceId(), + deviceIds.get(complianceData.getDeviceId())); } } if (complianceData.getAttempts() >= policyConfiguration.getMinRetriesToMarkInactive()) { inactiveDeviceIds.put(complianceData.getDeviceId(), - deviceIds.get(complianceData.getDeviceId())); + deviceIds.get(complianceData.getDeviceId())); } } } @@ -316,14 +321,14 @@ public class MonitoringManagerImpl implements MonitoringManager { log.debug("These devices are in the system for the first time"); for (PolicyDeviceWrapper wrapper : firstTimeDevices) { log.debug("First time device primary key : " + wrapper.getDeviceId() + " & policy id " + - wrapper.getPolicyId()); + wrapper.getPolicyId()); } } PolicyManagementDAOFactory.beginTransaction(); if (!deviceIdsToAddOperation.isEmpty()) { -// monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); + // monitoringDAO.addComplianceDetails(firstTimeDeviceIdsWithPolicyIds); monitoringDAO.addComplianceDetails(firstTimeDevices); monitoringDAO.updateAttempts(new ArrayList<>(deviceIdsToAddOperation.keySet()), false); } @@ -356,11 +361,17 @@ public class MonitoringManagerImpl implements MonitoringManager { // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current // implementation we are not able to do so. -// if(!deviceToMarkUnreachable.isEmpty()) { -// ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); -// decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( -// new ArrayList<>(deviceToMarkUnreachable.values()))); -// } + if (!devicesToMarkUnreachable.isEmpty()) { + ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); + decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( + new ArrayList<>(devicesToMarkUnreachable.values()))); + } + + if (!inactiveDeviceIds.isEmpty()) { + ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); + decisionPoint.setDevicesAsInactive(this.getDeviceIdentifiersFromDevices( + new ArrayList<>(inactiveDeviceIds.values()))); + } } @@ -369,7 +380,8 @@ public class MonitoringManagerImpl implements MonitoringManager { List deviceTypes = new ArrayList<>(); try { - deviceTypes = PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes(); + deviceTypes = + PolicyManagementDataHolder.getInstance().getDeviceManagementService().getAvailableDeviceTypes(); } catch (DeviceManagementException e) { throw new PolicyComplianceException("Error occurred while getting the device types.", e); } @@ -384,14 +396,14 @@ public class MonitoringManagerImpl implements MonitoringManager { monitoringOperation.setEnabled(true); monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setCode(OPERATION_MONITOR); -// CommandOperation infoOperation = new CommandOperation(); -// infoOperation.setEnabled(true); -// infoOperation.setType(Operation.Type.COMMAND);\\ -// infoOperation.setCode(OPERATION_INFO); -// CommandOperation appListOperation = new CommandOperation(); -// appListOperation.setEnabled(true); -// appListOperation.setType(Operation.Type.COMMAND); -// appListOperation.setCode(OPERATION_APP_LIST); + // CommandOperation infoOperation = new CommandOperation(); + // infoOperation.setEnabled(true); + // infoOperation.setType(Operation.Type.COMMAND);\\ + // infoOperation.setCode(OPERATION_INFO); + // CommandOperation appListOperation = new CommandOperation(); + // appListOperation.setEnabled(true); + // appListOperation.setType(Operation.Type.COMMAND); + // appListOperation.setCode(OPERATION_APP_LIST); //TODO: Fix this properly later adding device type to be passed in when the task manage executes "addOperations()" String type = null; @@ -400,8 +412,8 @@ public class MonitoringManagerImpl implements MonitoringManager { } DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); service.addOperation(type, monitoringOperation, deviceIdentifiers); -// service.addOperation(infoOperation, deviceIdentifiers); -// service.addOperation(appListOperation, deviceIdentifiers); + // service.addOperation(infoOperation, deviceIdentifiers); + // service.addOperation(appListOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java deleted file mode 100644 index 5ed2bf0f658..00000000000 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/service/PolicyManagementService.java +++ /dev/null @@ -1,146 +0,0 @@ -/* -* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. -* -* WSO2 Inc. licenses this file to you under the Apache License, -* Version 2.0 (the "License"); you may not use this file except -* in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, -* software distributed under the License is distributed on an -* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -* KIND, either express or implied. See the License for the -* specific language governing permissions and limitations -* under the License. -*/ - -package org.wso2.carbon.policy.mgt.core.service; - -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.Feature; -import org.wso2.carbon.policy.mgt.common.*; -import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; -import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; -import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; -import org.wso2.carbon.policy.mgt.core.PolicyManagerService; -import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; -import org.wso2.carbon.policy.mgt.core.task.TaskScheduleService; - -import java.util.List; - -public class PolicyManagementService implements PolicyManagerService { - - - PolicyManagerService policyManagerService; - - public PolicyManagementService() { - policyManagerService = new PolicyManagerServiceImpl(); - } - - - @Override - public Profile addProfile(Profile profile) throws PolicyManagementException { - return policyManagerService.addProfile(profile); - } - - @Override - public Profile updateProfile(Profile profile) throws PolicyManagementException { - return policyManagerService.updateProfile(profile); - } - - @Override - public Policy addPolicy(Policy policy) throws PolicyManagementException { - return policyManagerService.addPolicy(policy); - } - - @Override - public Policy updatePolicy(Policy policy) throws PolicyManagementException { - return policyManagerService.updatePolicy(policy); - } - - @Override - public boolean deletePolicy(Policy policy) throws PolicyManagementException { - return policyManagerService.deletePolicy(policy); - } - - @Override - public boolean deletePolicy(int policyId) throws PolicyManagementException { - return policyManagerService.deletePolicy(policyId); - } - - @Override - public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { - return policyManagerService.getEffectivePolicy(deviceIdentifier); - } - - @Override - public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws - FeatureManagementException { - return policyManagerService.getEffectiveFeatures(deviceIdentifier); - } - - @Override - public List getPolicies(String deviceType) throws PolicyManagementException { - return policyManagerService.getPolicies(deviceType); - } - - @Override - public List getFeatures() throws FeatureManagementException { - return policyManagerService.getFeatures(); - } - - @Override - public PolicyAdministratorPoint getPAP() throws PolicyManagementException { - return policyManagerService.getPAP(); - } - - @Override - public PolicyInformationPoint getPIP() throws PolicyManagementException { - return policyManagerService.getPIP(); - } - - @Override - public PolicyEvaluationPoint getPEP() throws PolicyManagementException { - return policyManagerService.getPEP(); - } - - @Override - public TaskScheduleService getTaskScheduleService() throws PolicyMonitoringTaskException { - return policyManagerService.getTaskScheduleService(); - } - - @Override - public int getPolicyCount() throws PolicyManagementException { - return policyManagerService.getPolicyCount(); - } - - @Override - public Policy getAppliedPolicyToDevice( - DeviceIdentifier deviceIdentifier) throws PolicyManagementException { - return policyManagerService.getAppliedPolicyToDevice(deviceIdentifier); - } - - @Override - public List checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Object - deviceResponse) throws PolicyComplianceException { - return policyManagerService.checkPolicyCompliance(deviceIdentifier, deviceResponse); - } - - @Override - public boolean checkCompliance(DeviceIdentifier deviceIdentifier, Object response) throws - PolicyComplianceException { - return policyManagerService.checkCompliance(deviceIdentifier, response); - } - - @Override - public ComplianceData getDeviceCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { - return policyManagerService.getDeviceCompliance(deviceIdentifier); - } - - @Override - public boolean isCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { - return policyManagerService.isCompliance(deviceIdentifier); - } -} diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index d579cd746d9..492e771acd3 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -97,8 +97,7 @@ public class MonitoringTask implements Task { } for (Device device : devices) { EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); - if (status.equals(EnrolmentInfo.Status.INACTIVE) || - status.equals(EnrolmentInfo.Status.BLOCKED) || + if (status.equals(EnrolmentInfo.Status.BLOCKED) || status.equals(EnrolmentInfo.Status.REMOVED) || status.equals(EnrolmentInfo.Status.UNCLAIMED) || status.equals(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED) ||