added /priorities end-point to PolicyManagement

merge-requests/7/head
madhawap 9 years ago
parent ee11bde8b0
commit 9c215c4ac9

@ -23,6 +23,7 @@ import org.wso2.carbon.apimgt.annotations.api.Permission;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@ -387,4 +388,32 @@ public interface PolicyManagementService {
required = true) List<Integer> policyIds);
@PUT
@Path("/priorities")
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "PUT",
value = "Prioritizing policies.",
notes = "",
tags = "Device Policy Management"
)
@ApiResponses(value = {
@ApiResponse(
code = 200,
message = "Policy Priorities successfully updated."),
@ApiResponse(
code = 400,
message = "Policy priorities did not update. Bad Request.",
response = ErrorResponse.class),
@ApiResponse(
code = 500,
message = "Exception in updating policy priorities.",
response = ErrorResponse.class)
})
@Permission(scope = "", permissions = {})
Response updatePolicyPriorities(@ApiParam(name = "priorityUpdatedPolicies", value = "",
required = true) List<PriorityUpdatedPolicyWrapper> priorityUpdatedPolicies);
}

@ -40,10 +40,12 @@ import org.wso2.carbon.policy.mgt.common.Policy;
import org.wso2.carbon.policy.mgt.common.PolicyAdministratorPoint;
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.PolicyManagerService;
import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@Path("/policies")
@ -301,4 +303,37 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
}
}
@PUT
@Path("/priorities")
public Response updatePolicyPriorities(List<PriorityUpdatedPolicyWrapper> priorityUpdatedPolicies) {
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
List<Policy> policiesToUpdate = new ArrayList<>(priorityUpdatedPolicies.size());
int i;
for (i = 0; i < priorityUpdatedPolicies.size(); i++) {
Policy policyObj = new Policy();
policyObj.setId(priorityUpdatedPolicies.get(i).getId());
policyObj.setPriorityId(priorityUpdatedPolicies.get(i).getPriority());
policiesToUpdate.add(policyObj);
}
boolean policiesUpdated;
try {
PolicyAdministratorPoint pap = policyManagementService.getPAP();
policiesUpdated = pap.updatePolicyPriorities(policiesToUpdate);
} catch (PolicyManagementException e) {
String error = "Exception in updating policy priorities.";
log.error(error, e);
throw new UnexpectedServerErrorException(
new ErrorResponse.ErrorResponseBuilder().setCode(500l).setMessage(error).build());
}
if (policiesUpdated) {
return Response.status(Response.Status.OK).entity("Policy Priorities successfully "
+ "updated.").build();
} else {
throw new NotFoundException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy priorities did "
+ "not update. Bad Request.").build());
}
}
}

Loading…
Cancel
Save