@Override public Policy addPolicyToDevice(List<Device> devices, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getConnection(); String query = "INSERT INTO DM_DEVICE_POLICY (DEVICE_ID, POLICY_ID, ENROLMENT_ID, DEVICE) VALUES (?, ?, " + "?, ?)"; stmt = conn.prepareStatement(query); for (Device device : devices) { stmt.setInt(1, device.getId()); stmt.setInt(2, policy.getId()); stmt.setInt(3, device.getEnrolmentInfo().getId()); stmt.setBytes(4, PolicyManagerUtil.getBytes(device)); stmt.addBatch(); } stmt.executeBatch(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the device ids with policy to " + "database", e); } catch (IOException e) { throw new PolicyManagerDAOException("Error occurred while getting the byte array from device.", e); } finally { PolicyManagementDAOUtil.cleanupResources(stmt, null); } return policy; }
@Override public void setDevicesAsUnreachable(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.UNREACHABLE); } } catch (DeviceManagementException e) { String msg = "Error occurred while setting the device as unreachable"; log.error(msg, e); throw new PolicyComplianceException(msg, e); } }
@Override public void setDevicesAsInactive(List<DeviceIdentifier> deviceIdentifiers) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { Device device = service.getDevice(deviceIdentifier, false); 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 markDeviceAsCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); } catch (DeviceManagementException e) { String msg = "Error occurred while marking device as compliance " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); } }
@Override public void deactivateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.INACTIVE); } catch (DeviceManagementException e) { String msg = "Error occurred while deactivating the device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); throw new PolicyComplianceException(msg, e); } }
if (device != null && device.getEnrolmentInfo() != null && device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) { toBeNotified.add(device);
@Override public void activateDevice(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); } catch (DeviceManagementException e) { String msg = "Error occurred while activating the device " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); throw new PolicyComplianceException(msg, e); } }
EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); if (status.equals(EnrolmentInfo.Status.BLOCKED) || status.equals(EnrolmentInfo.Status.REMOVED) ||
@Override public void setDeviceAsReachable(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); Device device = service.getDevice(deviceIdentifier, false); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.ACTIVE); } catch (DeviceManagementException e) { String msg = "Error occurred while setting the device as reachable for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(); log.error(msg, e); throw new PolicyComplianceException(msg, e); } }
private String[] getRoleOfDevice(Device device) throws PolicyManagementException { try { UserRealm userRealm = CarbonContext.getThreadLocalCarbonContext().getUserRealm(); if (userRealm != null) { return userRealm.getUserStoreManager().getRoleListOfUser(device.getEnrolmentInfo().getOwner()); } else { return null; } } catch (UserStoreException e) { throw new PolicyManagementException("Error occurred when retrieving roles related to user name.", e); } }
@Override public void setDevicesAsUnreachableWith(List<Device> devices) throws PolicyComplianceException { try { DeviceManagementProviderService service = this.getDeviceManagementProviderService(); for (Device device : devices) { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(device.getDeviceIdentifier()); deviceIdentifier.setType(device.getType()); service.setStatus(deviceIdentifier, device.getEnrolmentInfo().getOwner(), EnrolmentInfo.Status.UNREACHABLE); } } catch (DeviceManagementException e) { String msg = "Error occurred while setting the device as unreachable"; log.error(msg, e); throw new PolicyComplianceException(msg, e); } }
@Override public void addAppliedPolicyFeaturesToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); boolean exist = policyDAO.checkPolicyAvailable(deviceId, device.getEnrolmentInfo().getId()); if (exist) { policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } else { policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + deviceId + " - " + policy.getId() + ")", e); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } }
@Override public void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); if (policySaved != null && policySaved.getId() != 0) { policyDAO.updateEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } else { policyDAO.addEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId(), policy); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while adding the evaluated policy to device (" + deviceId + " - " + policy.getId() + ")", e); } catch (DeviceManagementException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } }
@Override public Policy getAppliedPolicyToDevice(DeviceIdentifier deviceId) throws PolicyManagementException { Policy policy; DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device; try { device = service.getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + "' and type '" + deviceId.getType() + "'. Therefore returning null"); } return null; } } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while getting device id.", e); } try { //int policyId = policyDAO.getAppliedPolicyId(device.getId()); PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getAppliedPolicy(device.getId(), device.getEnrolmentInfo().getId()); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting policy id or policy.", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return policy; }
@Override public void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { int deviceId = -1; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier, false); deviceId = device.getId(); PolicyManagementDAOFactory.beginTransaction(); Policy policySaved = policyDAO.getAppliedPolicy(deviceId, device.getEnrolmentInfo().getId()); if (policySaved != null) { policyDAO.deleteEffectivePolicyToDevice(deviceId, device.getEnrolmentInfo().getId()); } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while removing the applied policy to device (" + deviceId + ")", e); } catch (DeviceManagementException e) { PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); } finally { PolicyManagementDAOFactory.closeConnection(); } }
pipDevice.setDeviceType(deviceType); pipDevice.setDeviceIdentifier(deviceIdentifier); pipDevice.setUserId(device.getEnrolmentInfo().getOwner()); pipDevice.setOwnershipType(device.getEnrolmentInfo().getOwnership().toString()); pipDevice.setDeviceGroups(groupManagementProviderService.getGroups(pipDevice.getDeviceIdentifier()));
@Override public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { boolean exist; try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); exist = policyDAO.checkPolicyAvailable(device.getId(), device.getEnrolmentInfo().getId()); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while checking whether device has a policy " + "to apply.", e); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return exist; }
@Override public boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { try { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); Device device = service.getDevice(deviceIdentifier, false); PolicyManagementDAOFactory.openConnection(); policyDAO.setPolicyApplied(device.getId(), device.getEnrolmentInfo().getId()); return true; } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while setting the policy has applied to device (" + deviceIdentifier.getId() + ")", e); } catch (DeviceManagementException e) { throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); } catch (SQLException e) { throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } }
@Override public boolean isCompliant(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { Device device; try { DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); device = service.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } try { PolicyManagementDAOFactory.openConnection(); NonComplianceData complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo() .getId()); if (complianceData != null && !complianceData.isStatus()) { return false; } } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance status for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return true; }
@Override public NonComplianceData getDevicePolicyCompliance(DeviceIdentifier deviceIdentifier) throws PolicyComplianceException { NonComplianceData complianceData; try { PolicyManagementDAOFactory.openConnection(); DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance().getDeviceManagementService(); Device device = service.getDevice(deviceIdentifier, false); complianceData = monitoringDAO.getCompliance(device.getId(), device.getEnrolmentInfo().getId()); List<ComplianceFeature> complianceFeatures = monitoringDAO.getNoneComplianceFeatures(complianceData.getId()); complianceData.setComplianceFeatures(complianceFeatures); } catch (DeviceManagementException e) { throw new PolicyComplianceException("Unable to retrieve device data for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (MonitoringDAOException e) { throw new PolicyComplianceException("Unable to retrieve compliance data for " + deviceIdentifier.getId() + " - " + deviceIdentifier.getType(), e); } catch (SQLException e) { throw new PolicyComplianceException("Error occurred while opening a connection to the data source", e); } finally { PolicyManagementDAOFactory.closeConnection(); } return complianceData; }