|
|
@ -25,15 +25,19 @@ import org.wso2.carbon.CarbonConstants;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
|
|
|
|
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList;
|
|
|
@ -42,6 +46,9 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.RoleList;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService;
|
|
|
|
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
|
|
|
|
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.PolicyEvaluationException;
|
|
|
|
|
|
|
|
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
|
|
|
|
|
|
|
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
@ -259,6 +266,17 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
public Response addDevicesToGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
|
|
|
|
public Response addDevicesToGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceMgtAPIUtils.getGroupManagementProviderService().addDevices(groupId, deviceIdentifiers);
|
|
|
|
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.publishChanges();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
String msg = "Error occurred while adding devices to group.";
|
|
|
|
String msg = "Error occurred while adding devices to group.";
|
|
|
@ -266,6 +284,15 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
|
|
|
|
} 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) {
|
|
|
|
|
|
|
|
log.error("Error occurred while retrieving device information.", e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -273,6 +300,17 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
public Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
|
|
|
|
public Response removeDevicesFromGroup(int groupId, List<DeviceIdentifier> deviceIdentifiers) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
DeviceMgtAPIUtils.getGroupManagementProviderService().removeDevice(groupId, deviceIdentifiers);
|
|
|
|
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.publishChanges();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
String msg = "Error occurred while removing devices from group.";
|
|
|
|
String msg = "Error occurred while removing devices from group.";
|
|
|
@ -280,6 +318,15 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
|
|
|
|
}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) {
|
|
|
|
|
|
|
|
log.error("Error occurred while retrieving device information.", e);
|
|
|
|
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -290,6 +337,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier());
|
|
|
|
deviceIdentifiers.add(deviceToGroupsAssignment.getDeviceIdentifier());
|
|
|
|
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
|
|
|
|
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
|
|
|
|
List<DeviceGroup> deviceGroups = service.getGroups(deviceToGroupsAssignment.getDeviceIdentifier(), false);
|
|
|
|
List<DeviceGroup> 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) {
|
|
|
|
for (DeviceGroup group : deviceGroups) {
|
|
|
|
Integer groupId = group.getGroupId();
|
|
|
|
Integer groupId = group.getGroupId();
|
|
|
|
if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) {
|
|
|
|
if (deviceToGroupsAssignment.getDeviceGroupIds().contains(groupId)) {
|
|
|
@ -300,7 +351,12 @@ 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) {
|
|
|
|
|
|
|
|
effectivePolicy = DeviceMgtAPIUtils.getPolicyManagementService().getPEP().getEffectivePolicy(deviceIdentifier);
|
|
|
|
|
|
|
|
pap.addPolicyToDevice(deviceIdentifiers, effectivePolicy);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pap.publishChanges();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
return Response.status(Response.Status.OK).build();
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
} catch (GroupManagementException e) {
|
|
|
|
String msg = "Error occurred while assigning device to groups.";
|
|
|
|
String msg = "Error occurred while assigning device to groups.";
|
|
|
@ -308,6 +364,12 @@ public class GroupManagementServiceImpl implements GroupManagementService {
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
} catch (DeviceNotFoundException e) {
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
|
|
|
|
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|