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;
/**
* 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,
* @param profile

@ -137,6 +137,8 @@ public interface PolicyDAO {
void updateEffectivePolicyToDevice(int deviceId, int enrolmentId, Policy policy)
throws PolicyManagerDAOException;
void deleteEffectivePolicyToDevice(int deviceId, int enrolmentId) throws PolicyManagerDAOException;
boolean checkPolicyAvailable(int deviceId, int enrollmentId) 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
public boolean checkPolicyAvailable(int deviceId, int enrollmentId) throws PolicyManagerDAOException {
Connection conn;

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

@ -72,6 +72,8 @@ public interface PolicyManager {
void addAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier, Policy policy) throws PolicyManagementException;
void removeAppliedPolicyToDevice(DeviceIdentifier deviceIdentifier) throws PolicyManagementException;
boolean checkPolicyAvailable(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<Integer> deviceIds;
int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl();
List<Device> allDevices = service.getAllDevices();
PolicyManagementDAOFactory.openConnection();
//int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
deviceIds = policyDAO.getPolicyAppliedDevicesIds(policyId);
HashMap<Integer, Device> allDeviceMap = new HashMap<>();
if (!allDevices.isEmpty()) {
allDeviceMap = PolicyManagerUtil.covertDeviceListToMap(allDevices);
}
for (int deviceId : deviceIds) {
if (allDeviceMap.containsKey(deviceId)) {
if (log.isDebugEnabled()) {
log.debug("Policy Applied device ids .............: " + deviceId + " - Policy Id " + policyId);
}
deviceList.add(allDeviceMap.get(deviceId));
}
//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));
}
} catch (PolicyManagerDAOException e) {
@ -804,7 +793,6 @@ public class PolicyManagerImpl implements PolicyManager {
} finally {
PolicyManagementDAOFactory.closeConnection();
}
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
public boolean checkPolicyAvailable(DeviceIdentifier deviceIdentifier) throws PolicyManagementException {

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

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

Loading…
Cancel
Save