Adding fix for policy application upon adding devices to group

merge-requests/600/merge
Ace 4 years ago
parent 4b58b46d8b
commit c1c736b607

@ -100,11 +100,8 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis()); device.getEnrolmentInfo().setDateOfLastUpdate(System.currentTimeMillis());
boolean status = dms.enrollDevice(device); boolean status = dms.enrollDevice(device);
PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP();
List<DeviceIdentifier> deviceIdentifierList = new ArrayList<>();
DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType());
deviceIdentifierList.add(deviceId); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceId);
Policy effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceId);
pap.addPolicyToDevice(deviceIdentifierList, effectivePolicy);
pap.publishChanges(); pap.publishChanges();
return Response.status(Response.Status.OK).entity(status).build(); return Response.status(Response.Status.OK).entity(status).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
@ -118,9 +115,6 @@ public class DeviceAgentServiceImpl implements DeviceAgentService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
log.error("failed to add designated policies against newly enrolled device.", e); log.error("failed to add designated policies against newly enrolled device.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); 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();
} }
} }

@ -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.device.mgt.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint; import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; 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 org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -268,12 +269,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers);
PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP();
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
Policy effectivePolicy = null;
for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) { for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
Device device = dms.getDevice(deviceIdentifier, false); Device device = dms.getDevice(deviceIdentifier, false);
if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); pap.removePolicyUsed(deviceIdentifier);
pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier);
} }
} }
pap.publishChanges(); pap.publishChanges();
@ -287,9 +287,9 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
log.error("Error occurred while adding policies against device(s).", e); log.error("Error occurred while adding policies against device(s).", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (PolicyEvaluationException e) { // } catch (PolicyEvaluationException e) {
log.error("Error occurred while retrieving policies against device(s).", e); // log.error("Error occurred while retrieving policies against device(s).", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); // return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
log.error("Error occurred while retrieving device information.", e); log.error("Error occurred while retrieving device information.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
@ -302,12 +302,11 @@ public class GroupManagementServiceImpl implements GroupManagementService {
DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers); DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers);
PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP();
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
Policy effectivePolicy = null;
for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) { for(DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
Device device = dms.getDevice(deviceIdentifier, false); Device device = dms.getDevice(deviceIdentifier, false);
if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { if(!device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) {
effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); pap.removePolicyUsed(deviceIdentifier);
pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier);
} }
} }
pap.publishChanges(); pap.publishChanges();
@ -321,10 +320,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
}catch (PolicyManagementException e) { }catch (PolicyManagementException e) {
log.error("Error occurred while adding policies against device(s).", e); log.error("Error occurred while adding policies against device(s).", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (PolicyEvaluationException e) { }catch (DeviceManagementException 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); log.error("Error occurred while retrieving device information.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} }
@ -339,8 +335,6 @@ public class GroupManagementServiceImpl implements GroupManagementService {
List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier(), false); List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier(), false);
PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP(); PolicyAdministratorPoint pap = DeviceMgtAPIUtils.getPolicyManagementService().getPAP();
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
Device device = null;
Policy effectivePolicy = null;
for (DeviceGroup group : deviceGroups) { for (DeviceGroup group : deviceGroups) {
Integer groupId = group.getGroupId(); Integer groupId = group.getGroupId();
if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) { if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) {
@ -352,8 +346,8 @@ public class GroupManagementServiceImpl implements GroupManagementService {
for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) { for (int groupId : deviceToGroupsAssignment.getDeviceGroupIds()) {
DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers); DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers);
for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) {
effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier); pap.removePolicyUsed(deviceIdentifier);
pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy); DeviceMgtAPIUtils.getPolicyManagementService().getEffectivePolicy(deviceIdentifier);
} }
} }
pap.publishChanges(); pap.publishChanges();
@ -367,9 +361,6 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} catch (PolicyManagementException e) { } catch (PolicyManagementException e) {
log.error("Failed to add policies for device assigned to group.", e); log.error("Failed to add policies for device assigned to group.", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); 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();
} }
} }

@ -930,6 +930,7 @@ public class PolicyManagerImpl implements PolicyManager {
try { try {
device = deviceManagementService.getDevice(deviceIdentifier, false); device = deviceManagementService.getDevice(deviceIdentifier, false);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
PolicyManagementDAOFactory.rollbackTransaction();
throw new PolicyManagementException("Error occurred while getting the device details (" + throw new PolicyManagementException("Error occurred while getting the device details (" +
deviceIdentifier.getId() + ")", e); deviceIdentifier.getId() + ")", e);
} }

Loading…
Cancel
Save