From a12b942d86ce9162c4c0facf069df19988051108 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 24 Jun 2021 20:41:11 +0530 Subject: [PATCH] Add and update api layer related to hierarchical changes (cherry picked from commit e18aa57fe8b37f0c3a5b6701e8108276f28ca6bc) --- .../service/api/GroupManagementService.java | 123 +++++++++++++++++- .../admin/GroupManagementAdminService.java | 100 ++++++++++++++ .../impl/GroupManagementServiceImpl.java | 65 +++++++-- .../GroupManagementAdminServiceImpl.java | 51 ++++++++ 4 files changed, 327 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java index f61d2124932..6ad3b14bd7d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GroupManagementService.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * 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; @@ -45,6 +61,7 @@ import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.PUT; @@ -243,6 +260,87 @@ public interface GroupManagementService { @QueryParam("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") @GET @ApiOperation( @@ -412,7 +510,14 @@ public interface GroupManagementService { value = "Request group properties to include in the response", defaultValue = "false") @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}") @GET @@ -471,7 +576,14 @@ public interface GroupManagementService { value = "Request group properties to include in the response", defaultValue = "false") @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}") @PUT @@ -580,7 +692,12 @@ public interface GroupManagementService { name = "groupId", value = "ID of the group to be deleted.", 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") @POST diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java index f631b1dc510..d7b8553b204 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/admin/GroupManagementAdminService.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * 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; @@ -163,6 +179,90 @@ public interface GroupManagementAdminService { @QueryParam("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") @GET @ApiOperation( 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 0502f37ab34..3fed8e75049 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 @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * 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; @@ -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.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.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.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.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.util.ArrayList; 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 public Response getGroupCount() { try { 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(); } catch (GroupManagementException e) { String msg = "Error occurred while retrieving group count."; @@ -125,10 +172,10 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getGroup(int groupId, boolean requireGroupProps) { + public Response getGroup(int groupId, boolean requireGroupProps, int depth) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps); + DeviceGroup deviceGroup = service.getGroup(groupId, requireGroupProps, depth); if (deviceGroup != null) { return Response.status(Response.Status.OK).entity(deviceGroup).build(); } else { @@ -142,7 +189,7 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getGroup(String groupName, boolean requireGroupProps) { + public Response getGroup(String groupName, boolean requireGroupProps, int depth) { try { GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); DeviceGroup deviceGroup = service.getGroup(groupName, requireGroupProps); @@ -178,9 +225,9 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response deleteGroup(int groupId) { + public Response deleteGroup(int groupId, boolean isDeleteChildren) { try { - if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId)) { + if (DeviceMgtAPIUtils.getGroupManagementProviderService().deleteGroup(groupId, isDeleteChildren)) { return Response.status(Response.Status.OK).build(); } else { return Response.status(Response.Status.NOT_FOUND).entity("Group not found.").build(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java index b742a1d8e19..a461b71b500 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/admin/GroupManagementAdminServiceImpl.java @@ -15,6 +15,22 @@ * specific language governing permissions and limitations * 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; @@ -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.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 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 public Response getGroupCount(String status) { try {