From 89bd784a57c6ee24486a0ad7dde5678163857f3b Mon Sep 17 00:00:00 2001 From: isuri Date: Mon, 4 Mar 2024 17:07:21 +0530 Subject: [PATCH] deviceOrg: get children of root nodes api implementation --- .../api/DeviceOrganizationMgtService.java | 92 +++++++++++++++++-- .../api/DeviceOrganizationMgtServiceImpl.java | 24 +++++ .../organization/dto/RootChildrenRequest.java | 27 ++++++ .../impl/DeviceOrganizationServiceImpl.java | 32 ++++++- .../spi/DeviceOrganizationService.java | 10 ++ .../device/organization/ServiceTest.java | 16 ++++ 6 files changed, 190 insertions(+), 11 deletions(-) create mode 100644 components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/RootChildrenRequest.java diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java index cfc669c767..3761a157d9 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtService.java @@ -346,14 +346,14 @@ public interface DeviceOrganizationMgtService { Response getDeviceOrganizationLeafs( @ApiParam( name = "offset", - value = "The starting pagination index for the complete list of qualified items", + value = "leaf node offset", required = false, defaultValue = "0") @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam( name = "limit", - value = "Provide how many policy details you require from the starting pagination index/offset.", + value = "leaf node limit", required = false, defaultValue = "5") @DefaultValue("20") @QueryParam("limit") @@ -369,8 +369,8 @@ public interface DeviceOrganizationMgtService { @ApiOperation( produces = MediaType.APPLICATION_JSON, httpMethod = "GET", - value = "Retrieve leaf Device Organizations", - notes = "Get a list of leaf device organizations.", + value = "Retrieve root Device Organizations", + notes = "Get a list of root device organizations.", tags = "Device Organization Management", extensions = { @Extension(properties = { @@ -414,19 +414,97 @@ public interface DeviceOrganizationMgtService { Response getDeviceOrganizationRoots( @ApiParam( name = "offset", - value = "The starting pagination index for the complete list of qualified items", + value = "root node offset", required = false, defaultValue = "0") @DefaultValue("0") @QueryParam("offset") int offset, @ApiParam( name = "limit", - value = "Provide how many policy details you require from the starting pagination index/offset.", + value = "root node limit", required = false, - defaultValue = "5") + defaultValue = "20") @DefaultValue("20") @QueryParam("limit") int limit); + /** + * Retrieves a list of root device organizations. + * + * @return A response containing a list of root device organizations. + */ + @GET + @Path("roots-children") + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = "GET", + value = "Retrieve children for root Device Organizations", + notes = "Get a list of children for root device organizations.", + tags = "Device Organization Management", + extensions = { + @Extension(properties = { + @ExtensionProperty(name = SCOPE, value = "dm:device-org:view") + }) + } + ) + @ApiResponses( + value = { + @ApiResponse( + code = 200, + message = "OK. \n Successfully fetched the all devices.", + 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 was last modified.\n" + + "Used by caches, or in conditional requests."), + } + ), + @ApiResponse( + code = 400, + message = + "Bad Request. \n"), + @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 " + + "list of supported device types.", + response = ErrorResponse.class) + }) + Response getDeviceOrganizationChildrenForRoots( + @ApiParam( + name = "offset", + value = "root offset", + required = false, + defaultValue = "0") + @DefaultValue("0") @QueryParam("offset") + int offset, + @ApiParam( + name = "limit", + value = "root limit.", + required = false, + defaultValue = "20") + @DefaultValue("20") @QueryParam("limit") + int limit, + @ApiParam( + name= "maxDepth", + value = "The maximum depth of child nodes to retrieve for each root.", + required = true) @QueryParam("maxDepth") + int maxDepth, + @ApiParam( + name= "includeDevice", + value = "Indicates whether to include device information in the retrieved child nodes.", + required = true) @QueryParam("includeDevice") + boolean includeDevice); + /** * Retrieves a specific device organization by its organization ID. * diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java index 9a4b2590be..5dcda6c4a0 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/api/DeviceOrganizationMgtServiceImpl.java @@ -24,6 +24,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.api.u import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNodeResult; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.RootChildrenRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi.DeviceOrganizationService; import org.apache.commons.logging.Log; @@ -154,6 +155,29 @@ public class DeviceOrganizationMgtServiceImpl implements DeviceOrganizationMgtSe } } + @GET + @Path("roots-children") + @Override + public Response getDeviceOrganizationChildrenForRoots( + @DefaultValue("0") @QueryParam("offset") int offset, + @DefaultValue("20") @QueryParam("limit") int limit, + @QueryParam("maxDepth") int maxDepth, + @QueryParam("includeDevice") boolean includeDevice) { + RequestValidationUtil.validatePaginationParameters(offset, limit); + try { + DeviceOrganizationService deviceOrganizationService = DeviceOrgAPIUtils.getDeviceOrganizationService(); + RootChildrenRequest request = new RootChildrenRequest(offset, limit); + request.setMaxDepth(maxDepth); + request.setIncludeDevice(includeDevice); + List nodeResultList = deviceOrganizationService.getAllDeviceOrganizationsForRoots(request); + return Response.status(Response.Status.OK).entity(nodeResultList).build(); + } catch (DeviceOrganizationMgtPluginException e) { + String errorMessage = "get children for root organizations failed"; + log.error(errorMessage); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorMessage).build(); + } + } + @GET @Override @Path("{organizationId}") diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/RootChildrenRequest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/RootChildrenRequest.java new file mode 100644 index 0000000000..5547f42509 --- /dev/null +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dto/RootChildrenRequest.java @@ -0,0 +1,27 @@ +package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto; + +public class RootChildrenRequest extends PaginationRequest{ + + int maxDepth; + boolean includeDevice; + + public RootChildrenRequest(int start, int limit) { + super(start, limit); + } + + public int getMaxDepth() { + return maxDepth; + } + + public void setMaxDepth(int maxDepth) { + this.maxDepth = maxDepth; + } + + public boolean isIncludeDevice() { + return includeDevice; + } + + public void setIncludeDevice(boolean includeDevice) { + this.includeDevice = includeDevice; + } +} diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java index 1cc4838d97..a97adb578d 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java @@ -20,10 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAO; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.DeviceOrganizationDAOFactory; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.util.ConnectionManagerUtil; -import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.AdditionResult; -import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNodeResult; -import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; -import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.*; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DBConnectionException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtDAOException; @@ -34,6 +31,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import java.sql.Connection; +import java.util.ArrayList; import java.util.List; public class DeviceOrganizationServiceImpl implements DeviceOrganizationService { @@ -164,6 +162,32 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } } + /** + * {@inheritDoc} + */ + @Override + public List getAllDeviceOrganizationsForRoots(RootChildrenRequest request) throws DeviceOrganizationMgtPluginException { + List allDeviceOrganizations = new ArrayList<>(); + + try { + // Get all root device organizations + PaginationRequest paginationRequest = new PaginationRequest(request.getOffSet(), request.getLimit()); + List roots = getDeviceOrganizationRoots(paginationRequest); + + // Iterate over each root and fetch its children + for (DeviceOrganization root : roots) { + DeviceNodeResult childrenResult = getChildrenOfDeviceNode(root.getDeviceId(), request.getMaxDepth(), request.isIncludeDevice()); + allDeviceOrganizations.add(childrenResult); + } + return allDeviceOrganizations; + } catch (Exception e) { + String msg = "Error occurred while retrieving all device organizations for roots."; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } + } + + /** * {@inheritDoc} */ diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java index b69e386773..58b80f39c0 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.device.mgt.extensions.device.organization.spi; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNodeResult; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.RootChildrenRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import java.util.List; @@ -71,6 +72,15 @@ public interface DeviceOrganizationService { */ List getAllDeviceOrganizations() throws DeviceOrganizationMgtPluginException; + /** + * Retrieves a list of all device organizations for roots. + * @param request + * @return + * @throws DeviceOrganizationMgtPluginException + */ + List getAllDeviceOrganizationsForRoots(RootChildrenRequest request) + throws DeviceOrganizationMgtPluginException; + /** * Retrieves device Organization Leafs * diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 351b21d537..0cca105a0a 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -22,6 +22,7 @@ import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dao.D import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceNodeResult; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.DeviceOrganization; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.PaginationRequest; +import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.dto.RootChildrenRequest; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.exception.DeviceOrganizationMgtPluginException; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.impl.DeviceOrganizationServiceImpl; import io.entgra.device.mgt.core.device.mgt.extensions.device.organization.mock.BaseDeviceOrganizationTest; @@ -285,6 +286,21 @@ public class ServiceTest extends BaseDeviceOrganizationTest { Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); } + @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") + public void testGetAllOrganizationsForRoots() throws DeviceOrganizationMgtPluginException { + + int offSet = 0; + int limit = 100; + boolean includeDevice = true; + int maxDepth =100; + RootChildrenRequest request = new RootChildrenRequest(offSet, limit); + request.setMaxDepth(maxDepth); + request.setIncludeDevice(includeDevice); + List nodeResultList = deviceOrganizationService.getAllDeviceOrganizationsForRoots(request); + Assert.assertNotNull(nodeResultList, "Cannot be null"); + Assert.assertFalse(nodeResultList.isEmpty(), "List of node result should not be empty"); + } + @Test(dependsOnMethods = "testAddDeviceOrganizationWithNullParent") public void testGetRootOrganizations() throws DeviceOrganizationMgtPluginException { int offset = 0;