From c1c736b6079b200599cc795100b56eff13fb0e6f Mon Sep 17 00:00:00 2001 From: Ace Date: Tue, 21 Jul 2020 08:23:30 +0530 Subject: [PATCH] Adding fix for policy application upon adding devices to group --- .../service/impl/DeviceAgentServiceImpl.java | 8 +---- .../impl/GroupManagementServiceImpl.java | 31 +++++++------------ .../mgt/core/mgt/impl/PolicyManagerImpl.java | 1 + 3 files changed, 13 insertions(+), 27 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java index 5c59a48982a..f759ed13a9c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceAgentServiceImpl.java @@ -100,11 +100,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis()); boolean status = dms.enrollDevice(device); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); - List deviceIdentifierList = new ArrayList<>(); DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); - deviceIdentifierList.add(deviceId); - Policy effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceId); - pap.addPolicyToDevice(deviceIdentifierList, effectivePolicy); + DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId); pap.publishChanges(); return Response.status(Response.Status.OK).entity(status).build(); } catch (DeviceManagementException e) { @@ -118,9 +115,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService { } catch (PolicyManagementException e) { log.error("failed to add designated policies against newly enrolled device.", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (PolicyEvaluationException e) { - log.error("failed while retrieving policies for newly enrolled device.", e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java index 9d15dfe3625..0502f37ab34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GroupManagementServiceImpl.java @@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; import javax.ws.rs.core.Response; @@ -268,12 +269,11 @@ public class GroupManagementServiceImpl implements GroupManagementService { DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - Policy effectivePolicy = null; for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) { Device device = dms.getDevice(deviceIdentifier, false); if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { - effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); - pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); + pap.removePolicyUsed(deviceIdentifier); + DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier); } } pap.publishChanges(); @@ -287,9 +287,9 @@ public class GroupManagementServiceImpl implements GroupManagementService { } catch (PolicyManagementException e) { log.error("Error occurred while adding policies against device(s).", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (PolicyEvaluationException e) { - log.error("Error occurred while retrieving policies against device(s).", e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); +// } catch (PolicyEvaluationException e) { +// log.error("Error occurred while retrieving policies against device(s).", e); +// return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (DeviceManagementException e) { log.error("Error occurred while retrieving device information.", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); @@ -302,12 +302,11 @@ public class GroupManagementServiceImpl implements GroupManagementService { DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - Policy effectivePolicy = null; for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) { Device device = dms.getDevice(deviceIdentifier, false); if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { - effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); - pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); + pap.removePolicyUsed(deviceIdentifier); + DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier); } } pap.publishChanges(); @@ -321,10 +320,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { }catch (PolicyManagementException e) { log.error("Error occurred while adding policies against device(s).", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (PolicyEvaluationException e) { - log.error("Error occurred while retrieving policies against device(s).", e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (DeviceManagementException e) { + }catch (DeviceManagementException e) { log.error("Error occurred while retrieving device information.", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } @@ -339,8 +335,6 @@ public class GroupManagementServiceImpl implements GroupManagementService { List deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier(), false); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - Device device = null; - Policy effectivePolicy = null; for (DeviceGroup group : deviceGroups) { Integer groupId = group.getGroupId(); if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) { @@ -352,8 +346,8 @@ public class GroupManagementServiceImpl implements GroupManagementService { for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) { DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); - pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); + pap.removePolicyUsed(deviceIdentifier); + DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier); } } pap.publishChanges(); @@ -367,9 +361,6 @@ public class GroupManagementServiceImpl implements GroupManagementService { } catch (PolicyManagementException e) { log.error("Failed to add policies for device assigned to group.", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); - } catch (PolicyEvaluationException e) { - log.error("Failed while retrieving policies device assigned to group.", e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index c8737f1468f..ffacfe87874 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -930,6 +930,7 @@ public class PolicyManagerImpl implements PolicyManager { try { device = deviceManagementService.getDevice(deviceIdentifier, false); } catch (DeviceManagementException e) { + PolicyManagementDAOFactory.rollbackTransaction(); throw new PolicyManagementException("Error occurred while getting the device details (" + deviceIdentifier.getId() + ")", e); }