Removed applied policy upon removal of the last policy applicable for a device

merge-requests/7/head
harshanl 8 years ago
parent 6ba6c20803
commit 370f6de260

@ -131,6 +131,13 @@ public interface PolicyAdministratorPoint {
*/ */
void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; void setPolicyUsed(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
/**
* This method will remove the policy applied to the device.
* @param deviceIdentifier
* @throws PolicyManagementException
*/
void removePolicyUsed(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
/** /**
* This method will add the profile to database, * This method will add the profile to database,
* @param profile * @param profile

@ -137,6 +137,8 @@ public interface PolicyDAO {
void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy) void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy)
throws PolicyManagerDAOException; throws PolicyManagerDAOException;
void deleteEffectivePolicyToDevice(int deviceId, int enrolmentId) throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException; boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException;
int getPolicyCount() throws PolicyManagerDAOException; int getPolicyCount() throws PolicyManagerDAOException;

@ -1163,6 +1163,30 @@ public class PolicyDAOImpl implements PolicyDAO {
} }
} }
@Override
public void deleteEffectivePolicyToDevice(int deviceId, int enrolmentId) throws PolicyManagerDAOException {
Connection conn;
PreparedStatement stmt = null;
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query = "DELETE FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ? " +
"AND ENROLMENT_ID = ?";
stmt = conn.prepareStatement(query);
stmt.setInt(1, deviceId);
stmt.setInt(2, tenantId);
stmt.setInt(3, enrolmentId);
stmt.executeUpdate();
} catch (SQLException e) {
throw new PolicyManagerDAOException("Error occurred while deleting the effective policy " +
"to device", e);
} finally {
PolicyManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override @Override
public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException { public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
Connection conn; Connection conn;

@ -274,6 +274,11 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint {
policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy); policyManager.addAppliedPolicyToDevice(deviceIdentifier, policy);
} }
@Override
public void removePolicyUsed(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
policyManager.removeAppliedPolicyToDevice(deviceIdentifier);
}
@Override @Override
public Profile addProfile(Profile profile) throws PolicyManagementException { public Profile addProfile(Profile profile) throws PolicyManagementException {
try { try {

@ -72,6 +72,8 @@ public interface PolicyManager {
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException; void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException; boolean setPolicyApplied(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;

@ -763,34 +763,23 @@ public class PolicyManagerImpl implements PolicyManager {
List<Device> deviceList = new ArrayList<>(); List<Device> deviceList = new ArrayList<>();
List<Integer> deviceIds; List<Integer> deviceIds;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
List<Device> allDevices = service.getAllDevices(); List<Device> allDevices = service.getAllDevices();
PolicyManagementDAOFactory.openConnection(); PolicyManagementDAOFactory.openConnection();
//int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId); deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
HashMap<Integer, Device> allDeviceMap = new HashMap<>(); HashMap<Integer, Device> allDeviceMap = new HashMap<>();
if (!allDevices.isEmpty()) { if (!allDevices.isEmpty()) {
allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices); allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices);
} }
for (int deviceId : deviceIds) { for (int deviceId : deviceIds) {
if (allDeviceMap.containsKey(deviceId)) { if (allDeviceMap.containsKey(deviceId)) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId); log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId);
} }
deviceList.add(allDeviceMap.get(deviceId)); deviceList.add(allDeviceMap.get(deviceId));
} }
//TODO FIX ME -- This is wrong, Device id is not device identifier, so converting is wrong. //TODO FIX ME -- This is wrong, Device id is not device identifier, so converting is wrong.
//deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); //deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId));
} }
} catch (PolicyManagerDAOException e) { } catch (PolicyManagerDAOException e) {
@ -804,7 +793,6 @@ public class PolicyManagerImpl implements PolicyManager {
} finally { } finally {
PolicyManagementDAOFactory.closeConnection(); PolicyManagementDAOFactory.closeConnection();
} }
return deviceList; return deviceList;
} }
@ -912,6 +900,34 @@ public class PolicyManagerImpl implements PolicyManager {
} }
} }
@Override
public void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {
int deviceId = -1;
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
Device device = service.getDevice(deviceIdentifier);
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();
}
}
@Override @Override
public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException { public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {

@ -48,7 +48,7 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint {
policyInformationPoint = policyManagerService.getPIP(); policyInformationPoint = policyManagerService.getPIP();
PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
policyList = policyInformationPoint.getRelatedPolicies(pipDevice); policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
policyAdministratorPoint = policyManagerService.getPAP();
for(Policy pol : policyList) { for(Policy pol : policyList) {
log.debug("Policy used in evaluation - Name : " + pol.getPolicyName() ); log.debug("Policy used in evaluation - Name : " + pol.getPolicyName() );
} }
@ -57,12 +57,10 @@ public class SimplePolicyEvaluationTest implements PolicyEvaluationPoint {
if(!policyList.isEmpty()) { if(!policyList.isEmpty()) {
policy = policyList.get(0); policy = policyList.get(0);
} else { } else {
policyAdministratorPoint.removePolicyUsed(deviceIdentifier);
return null; return null;
} }
policyAdministratorPoint = policyManagerService.getPAP();
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy);
} }
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {

@ -53,15 +53,15 @@ public class SimpleEvaluationImpl implements SimpleEvaluation {
policyInformationPoint = policyManagerService.getPIP(); policyInformationPoint = policyManagerService.getPIP();
PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier); PIPDevice pipDevice = policyInformationPoint.getDeviceData(deviceIdentifier);
policyList = policyInformationPoint.getRelatedPolicies(pipDevice); policyList = policyInformationPoint.getRelatedPolicies(pipDevice);
policyAdministratorPoint = policyManagerService.getPAP();
sortPolicies(); sortPolicies();
if(!policyList.isEmpty()) { if(!policyList.isEmpty()) {
policy = policyList.get(0); policy = policyList.get(0);
} else { } else {
policyAdministratorPoint.removePolicyUsed(deviceIdentifier);
return null; return null;
} }
//TODO : UNCOMMENT THE FOLLOWING CASE //TODO : UNCOMMENT THE FOLLOWING CASE
policyAdministratorPoint = policyManagerService.getPAP();
policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy); policyAdministratorPoint.setPolicyUsed(deviceIdentifier, policy);
} }

Loading…
Cancel
Save