Add and update api layer related to hierarchical changes

(cherry picked from commit e18aa57fe8)
4.x.x
Saad Sahibjan 3 years ago committed by Pahansith
parent 90d8b94455
commit a12b942d86

@ -15,6 +15,22 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.api; package org.wso2.carbon.device.mgt.jaxrs.service.api;
@ -45,6 +61,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import javax.validation.Valid; import javax.validation.Valid;
import javax.ws.rs.Consumes; import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.PUT; import javax.ws.rs.PUT;
@ -243,6 +260,87 @@ public interface GroupManagementService {
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps);
@GET
@Path("/hierarchy")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting the List of Hierarchical Groups",
notes = "Returns all groups enrolled with the system hierarchically.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:groups")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device hierarchical groups.",
response = DeviceGroupList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the groups list.",
response = ErrorResponse.class)
})
Response getGroupsWithHierarchy(
@ApiParam(
name = "name",
value = "Name of the group.")
@QueryParam("name")
String name,
@ApiParam(
name = "owner",
value = "Owner of the group.")
@QueryParam("owner")
String owner,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps,
@ApiParam(
name = "depth",
value = "Depth of the group hierarchy.")
@DefaultValue("3")
@QueryParam("depth")
int depth,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
defaultValue = "0")
@DefaultValue("0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many records require from the starting pagination index/offset.",
defaultValue = "5")
@DefaultValue("5")
@QueryParam("limit")
int limit);
@Path("/count") @Path("/count")
@GET @GET
@ApiOperation( @ApiOperation(
@ -412,7 +510,14 @@ public interface GroupManagementService {
value = "Request group properties to include in the response", value = "Request group properties to include in the response",
defaultValue = "false") defaultValue = "false")
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps,
@ApiParam(
name = "depth",
value = "Depth of the group hierarchy.",
defaultValue = "1")
@DefaultValue("1")
@QueryParam("depth")
int depth);
@Path("/name/{groupName}") @Path("/name/{groupName}")
@GET @GET
@ -471,7 +576,14 @@ public interface GroupManagementService {
value = "Request group properties to include in the response", value = "Request group properties to include in the response",
defaultValue = "false") defaultValue = "false")
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps,
@ApiParam(
name = "depth",
value = "Depth of the group hierarchy.",
defaultValue = "1")
@DefaultValue("1")
@QueryParam("depth")
int depth);
@Path("/id/{groupId}") @Path("/id/{groupId}")
@PUT @PUT
@ -580,7 +692,12 @@ public interface GroupManagementService {
name = "groupId", name = "groupId",
value = "ID of the group to be deleted.", value = "ID of the group to be deleted.",
required = true) required = true)
@PathParam("groupId") int groupId); @PathParam("groupId") int groupId,
@ApiParam(
name = "isDeleteChildren",
value = "Is the children groups needs to be deleted.",
required = true)
@QueryParam("isDeleteChildren") boolean isDeleteChildren);
@Path("/id/{groupId}/share") @Path("/id/{groupId}/share")
@POST @POST

@ -15,6 +15,22 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.api.admin; package org.wso2.carbon.device.mgt.jaxrs.service.api.admin;
@ -163,6 +179,90 @@ public interface GroupManagementAdminService {
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps);
@GET
@Path("hierarchy")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting the List of Hierarchical Groups",
notes = "Returns all groups enrolled with the system hierarchically.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:admin-groups:view")
})
}
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device groups hierarchically.",
response = DeviceGroupList.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource has been modified the last time.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 304,
message = "Not Modified. \n Empty body because the client has already the latest version of " +
"the requested resource."),
@ApiResponse(
code = 406,
message = "Not Acceptable.\n The requested media type is not supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Server error occurred while fetching the groups list.",
response = ErrorResponse.class)
})
Response getGroupsWithHierarchy(@ApiParam(
name = "name",
value = "Name of the group.")
@QueryParam("name")
String name,
@ApiParam(
name = "owner",
value = "Owner of the group.")
@QueryParam("owner")
String owner,
@ApiParam(
name = "status",
value = "status of group to be retrieve.")
@QueryParam("status")
String status,
@ApiParam(
name = "requireGroupProps",
value = "Request group properties to include in the response",
defaultValue = "false")
@QueryParam("requireGroupProps")
boolean requireGroupProps,
@ApiParam(
name = "depth",
value = "Depth of the group hierarchy.")
@DefaultValue("3")
@QueryParam("depth")
int depth,
@ApiParam(
name = "offset",
value = "The starting pagination index for the complete list of qualified items.",
defaultValue = "0")
@DefaultValue("0")
@QueryParam("offset")
int offset,
@ApiParam(
name = "limit",
value = "Provide how many records require from the starting pagination index/offset.",
defaultValue = "5")
@DefaultValue("5")
@QueryParam("limit")
int limit);
@Path("/count") @Path("/count")
@GET @GET
@ApiOperation( @ApiOperation(

@ -15,6 +15,22 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl; package org.wso2.carbon.device.mgt.jaxrs.service.impl;
@ -36,7 +52,6 @@ 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.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.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.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;
@ -47,10 +62,12 @@ 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.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 org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -89,11 +106,41 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
} }
@GET
@Path("/hierarchy")
@Override
public Response getGroupsWithHierarchy(
@QueryParam("name") String name,
@QueryParam("owner") String owner,
@QueryParam("requireGroupProps") boolean requireGroupProps,
@DefaultValue("3") @QueryParam("depth") int depth,
@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("5") @QueryParam("limit") int limit) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
request.setGroupName(name);
request.setOwner(owner);
request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
return Response.status(Response.Status.OK).entity(deviceGroupList).build();
} catch (GroupManagementException e) {
String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
}
}
@Override @Override
public Response getGroupCount() { public Response getGroupCount() {
try { try {
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser); int count = DeviceMgtAPIUtils.getGroupManagementProviderService().getGroupCount(currentUser, null);
return Response.status(Response.Status.OK).entity(count).build(); return Response.status(Response.Status.OK).entity(count).build();
} catch (GroupManagementException e) { } catch (GroupManagementException e) {
String msg = "Error occurred while retrieving group count."; String msg = "Error occurred while retrieving group count.";
@ -125,10 +172,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response getGroup(int groupId, boolean requireGroupProps) { public Response getGroup(int groupId, boolean requireGroupProps, int depth) {
try { try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps); DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps, depth);
if (deviceGroup != null) { if (deviceGroup != null) {
return Response.status(Response.Status.OK).entity(deviceGroup).build(); return Response.status(Response.Status.OK).entity(deviceGroup).build();
} else { } else {
@ -142,7 +189,7 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response getGroup(String groupName, boolean requireGroupProps) { public Response getGroup(String groupName, boolean requireGroupProps, int depth) {
try { try {
GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService();
DeviceGroup deviceGroup = service.getGroup(groupName, requireGroupProps); DeviceGroup deviceGroup = service.getGroup(groupName, requireGroupProps);
@ -178,9 +225,9 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
@Override @Override
public Response deleteGroup(int groupId) { public Response deleteGroup(int groupId, boolean isDeleteChildren) {
try { try {
if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId)) { if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId, isDeleteChildren)) {
return Response.status(Response.Status.OK).build(); return Response.status(Response.Status.OK).build();
} else { } else {
return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build();

@ -15,6 +15,22 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
* *
*
* Copyright (c) 2021, Entgra (pvt) Ltd. (https://entgra.io) All Rights Reserved.
*
* Entgra (Pvt) Ltd. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/ */
package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin; package org.wso2.carbon.device.mgt.jaxrs.service.impl.admin;
@ -31,6 +47,10 @@ import org.wso2.carbon.device.mgt.jaxrs.service.api.admin.GroupManagementAdminSe
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 javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
@ -74,6 +94,37 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
} }
} }
@GET
@Path("/hierarchy")
@Override
public Response getGroupsWithHierarchy(
@QueryParam("name") String name,
@QueryParam("owner") String owner,
@QueryParam("status") String status,
@QueryParam("requireGroupProps") boolean requireGroupProps,
@DefaultValue("3") @QueryParam("depth") int depth,
@DefaultValue("0") @QueryParam("offset") int offset,
@DefaultValue("5") @QueryParam("limit") int limit) {
try {
RequestValidationUtil.validatePaginationParameters(offset, limit);
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
request.setGroupName(name);
request.setOwner(owner);
request.setStatus(status);
request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
return Response.status(Response.Status.OK).entity(deviceGroupList).build();
} catch (GroupManagementException e) {
String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
}
}
@Override @Override
public Response getGroupCount(String status) { public Response getGroupCount(String status) {
try { try {

Loading…
Cancel
Save