From d1d6eefd174e73a2b275943fbfbf78c3964a8434 Mon Sep 17 00:00:00 2001 From: charitha Date: Tue, 4 Oct 2016 12:24:40 +0530 Subject: [PATCH] Upgrading grouping backend --- .../mgt/jaxrs/beans/DeviceGroupList.java | 57 ++ .../mgt/jaxrs/beans/DeviceGroupShare.java | 58 ++ .../mgt/jaxrs/beans/DeviceGroupUsersList.java | 55 ++ .../service/api/GroupManagementService.java | 548 ++++++++++++++++-- .../impl/GroupManagementServiceImpl.java | 45 +- .../mgt/common/group/mgt/DeviceGroup.java | 24 +- .../mgt/common/group/mgt/GroupUser.java | 13 +- 7 files changed, 713 insertions(+), 87 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupShare.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java new file mode 100644 index 0000000000..15a3e1698c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupList.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceGroupList extends BasePaginatedResult { + + @ApiModelProperty(value = "List of device groups returned") + @JsonProperty("groups") + private List deviceGroups = new ArrayList<>(); + + public List getList() { + return deviceGroups; + } + + public void setList(List deviceGroups) { + this.deviceGroups = deviceGroups; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" groups: [").append(deviceGroups).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupShare.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupShare.java new file mode 100644 index 0000000000..af436c80f8 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupShare.java @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.beans; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; +import java.util.List; + + +/** + * This class holds Device Group user name and assigned roles of user for particular group. Exposed to external access. + */ +@ApiModel(value = "DeviceGroupShare", description = "This class carries roles assigned to a user of a managed device group.") +public class DeviceGroupShare implements Serializable { + + private static final long serialVersionUID = 1998141711L; + + @ApiModelProperty(name = "username", value = "Username of the user.", required = true) + private String username; + + @ApiModelProperty(name = "roles", value = "List of roles assigned to user from the group.") + private List groupRoles; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public List getGroupRoles() { + return groupRoles; + } + + public void setGroupRoles(List groupRoles) { + this.groupRoles = groupRoles; + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java new file mode 100644 index 0000000000..ed369ced60 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/beans/DeviceGroupUsersList.java @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.beans; + +import com.fasterxml.jackson.annotation.JsonProperty; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +public class DeviceGroupUsersList extends BasePaginatedResult { + + @ApiModelProperty(value = "List of device group users returned") + @JsonProperty("users") + private List users = new ArrayList<>(); + + public List getList() { + return users; + } + + public void setList(List users) { + this.users = users; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("{\n"); + + sb.append(" count: ").append(getCount()).append(",\n"); + sb.append(" next: ").append(getNext()).append(",\n"); + sb.append(" previous: ").append(getPrevious()).append(",\n"); + sb.append(" users: [").append(users).append("\n"); + sb.append("]}\n"); + return sb.toString(); + } + +} + 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 f21d1c9a4b..c6c050fa71 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 @@ -18,11 +18,24 @@ */ package org.wso2.carbon.device.mgt.jaxrs.service.api; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import io.swagger.annotations.ResponseHeader; +import org.apache.axis2.transport.http.HTTPConstants; import org.wso2.carbon.apimgt.annotations.api.API; import org.wso2.carbon.apimgt.annotations.api.Permission; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupList; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupUsersList; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceList; +import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; +import javax.validation.Valid; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; import javax.ws.rs.GET; @@ -34,80 +47,529 @@ import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; +import java.util.List; +/** + * Device group related REST-API. This can be used to manipulated device group related details. + */ @API(name = "Group Management", version = "1.0.0", context = "/api/device-mgt/v1.0/groups", tags = {"devicemgt_admin"}) @Path("/groups") +@Api(value = "Device Group Management", description = "This API carries all device group management related operations " + + "such as get all the available groups, etc.") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) public interface GroupManagementService { @GET - @Permission(name = "View Group", permission = "/permission/admin/device-mgt/user/groups/list") - Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "Get the list of groups belongs to current user.", + notes = "Returns all permitted groups enrolled with the system.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the list of device 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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) + }) + @Permission(name = "View Groups", permission = "/device-mgt/groups/view") + Response getGroups(@ApiParam( + name = "offset", + value = "Starting point within the complete list of items qualified.") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Maximum size of resource array to return.") @QueryParam("limit") int limit); @POST - @Permission(name = "Add Group", permission = "/permission/admin/device-mgt/user/groups/add") - Response createGroup(DeviceGroup group); + @ApiOperation( + consumes = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Add new device group to the system.", + notes = "Add device group with current user as the owner.", + tags = "Device Group Management") + @ApiResponses( + value = { + @ApiResponse( + code = 201, + message = "Created. \n Device group has successfully been created", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The URL of the added group."), + @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 = 303, + message = "See Other. \n Source can be retrieved from the URL specified at the Location header.", + responseHeaders = { + @ResponseHeader( + name = "Content-Location", + description = "The Source URL of the document.")}), + @ApiResponse( + code = 400, + message = "Bad Request. \n Invalid request or validation error.", + response = ErrorResponse.class), + @ApiResponse( + code = 401, + message = "Unauthorized. \n Current logged in user is not authorized to add device groups.", + response = ErrorResponse.class), + @ApiResponse( + code = 415, + message = "Unsupported media type. \n The entity of the request was in a not supported format."), + @ApiResponse( + code = 500, + message = "Internal Server Error. \n " + + "Server error occurred while adding a new device group.", + response = ErrorResponse.class) + }) + @Permission(name = "Add Group", permission = "/device-mgt/groups/add") + Response createGroup(@ApiParam( + name = "group", + value = "Group object with data.", + required = true) + @Valid DeviceGroup group); @Path("/{groupName}") @GET - @Permission(name = "View Group", permission = "/permission/admin/device-mgt/user/groups/view") - Response getGroup(@PathParam("groupName") String groupName); + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "View group specified with name.", + notes = "Returns details of group enrolled with the system.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the device group.", + response = DeviceGroup.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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 group details.", + response = ErrorResponse.class) + }) + @Permission(name = "View Groups", permission = "/device-mgt/groups/view") + Response getGroup(@ApiParam( + name = "groupName", + value = "Name of the group to view.", + required = true) + @PathParam("groupName") String groupName); @Path("/{groupName}") @PUT - @Permission(name = "Update Group", permission = "/permission/admin/device-mgt/user/groups/update") - Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup); + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_PUT, + value = "Update a group.", + notes = "If you wish to make changes to an existing group, that can be done by updating the group using " + + "this resource.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Group has been updated successfully.", + 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 updating the group.", + response = ErrorResponse.class) + }) + @Permission(name = "Update Group", permission = "/device-mgt/groups/update") + Response updateGroup(@ApiParam( + name = "groupName", + value = "Name of the group to be updated.", + required = true) + @PathParam("groupName") String groupName, + @ApiParam( + name = "group", + value = "Group object with data.", + required = true) + @Valid DeviceGroup deviceGroup); @Path("/{groupName}") @DELETE - @Permission(name = "Remove Groups", permission = "/permission/admin/device-mgt/user/groups/remove") - Response deleteGroup(@PathParam("groupName") String groupName); - - @Path("/{groupName}/share-with-user") - @POST - @Permission(name = "Share Group to a User", permission = "/permission/admin/device-mgt/user/groups/share") - Response shareGroupWithUser(@PathParam("groupName") String groupName, String targetUser); - - @Path("/{groupName}/share-with-role") - @POST - @Permission(name = "Share Group to a Role", permission = "/permission/admin/device-mgt/user/groups/share") - Response shareGroupWithRole(@PathParam("groupName") String groupName, String targetRole); - - @Path("/{groupName}/remove-share-with-user") - @POST - @Permission(name = "Unshare a Group", permission = "/permission/admin/device-mgt/user/groups/unshare") - Response removeShareWithUser(@PathParam("groupName") String groupName, String targetUser); + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_DELETE, + value = "Delete a group.", + notes = "If you wish to remove an existing group, that can be done by updating the group using " + + "this resource.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Group has been deleted successfully.", + 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 removing the group.", + response = ErrorResponse.class) + }) + @Permission(name = "Remove Group", permission = "/device-mgt/groups/remove") + Response deleteGroup(@ApiParam( + name = "groupName", + value = "Name of the group to be deleted.", + required = true) + @PathParam("groupName") String groupName); - @Path("/{groupName}/remove-share-with-role") + @Path("/{groupName}/share") @POST - @Permission(name = "Unshare a Group", permission = "/permission/admin/device-mgt/user/groups/unshare") - Response removeShareWithRole(@PathParam("groupName") String groupName, String targetUser); + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Manage group sharing with a user.", + notes = "If you wish to share /un share an existing group with a user under defined sharing roles, " + + "that can be done using this resource.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Sharing has been updated successfully.", + 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 sharing the group.", + response = ErrorResponse.class) + }) + @Permission(name = "Share Group", permission = "/device-mgt/groups/share") + Response manageGroupSharing(@ApiParam( + name = "groupName", + value = "Name of the group to be shared or unshared.", + required = true) + @PathParam("groupName") String groupName, + @ApiParam( + name = "deviceGroupShare", + value = "User name and the assigned roles for the share.", + required = true) + @Valid DeviceGroupShare deviceGroupShare); - @GET @Path("/{groupName}/users") - @Permission(name = "Get Users of Group", permission = "/permission/admin/device-mgt/user/groups/list") - Response getUsersOfGroup(@PathParam("groupName") String groupName); - @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "View list of users of a device group.", + notes = "Returns details of users which particular group has been shared with.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the users.", + response = DeviceGroupUsersList.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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 users.", + response = ErrorResponse.class) + }) + @Permission(name = "View users", permission = "/device-mgt/groups/users/view") + Response getUsersOfGroup(@ApiParam( + name = "groupName", + value = "Name of the group.", + required = true) + @PathParam("groupName") String groupName); + @Path("/{groupName}/devices") - @Permission(name = "Get Devices of Group", permission = "/permission/admin/device-mgt/groups/roles") - Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, + @GET + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_GET, + value = "View list of devices in the device group.", + notes = "Returns list of devices in the device group.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully fetched the devices.", + response = DeviceList.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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 devices.", + response = ErrorResponse.class) + }) + @Permission(name = "View devices", permission = "/device-mgt/groups/devices/view") + Response getDevicesOfGroup(@ApiParam( + name = "groupName", + value = "Name of the group.", + required = true) + @PathParam("groupName") String groupName, + @ApiParam( + name = "offset", + value = "Starting point within the complete list of items qualified.") + @QueryParam("offset") int offset, + @ApiParam( + name = "limit", + value = "Maximum size of resource array to return.") @QueryParam("limit") int limit); - @POST @Path("/{groupName}/devices") - @Produces("application/json") - @Permission(name = "Add Device to a Group", permission = "/permission/admin/device-mgt/user/groups/devices/add") - Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier); + @POST + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_POST, + value = "Add devices to group.", + notes = "Add existing devices to the device group.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully add devices to the group.", + 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 adding devices to the group.", + response = ErrorResponse.class) + }) + @Permission(name = "Add devices", permission = "/device-mgt/groups/devices/add") + Response addDevicesToGroup(@ApiParam( + name = "groupName", + value = "Name of the group.", + required = true) + @PathParam("groupName") String groupName, + @ApiParam( + name = "deviceIdentifiers", + value = "Device identifiers of the devices which needed be added.", + required = true) + @Valid List deviceIdentifiers); - @DELETE @Path("/{groupName}/devices") - @Permission(name = "Remove Devices from Group", - permission = "/permission/admin/device-mgt/user/groups/devices/remove") - Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, - @QueryParam("id") String id); + @DELETE + @ApiOperation( + produces = MediaType.APPLICATION_JSON, + httpMethod = HTTPConstants.HEADER_DELETE, + value = "Remove devices from group.", + notes = "Remove existing devices from the device group.", + tags = "Device Group Management") + @ApiResponses(value = { + @ApiResponse(code = 200, message = "OK. \n Successfully removed devices from the group.", + 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 = 404, + message = "No groups found.", + response = ErrorResponse.class), + @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 removing devices from the group.", + response = ErrorResponse.class) + }) + @Permission(name = "Remove devices", permission = "/device-mgt/groups/devices/remove") + Response removeDevicesFromGroup(@ApiParam( + name = "groupName", + value = "Name of the group.", + required = true) + @PathParam("groupName") String groupName, + @ApiParam( + name = "deviceIdentifiers", + value = "Device identifiers of the devices which needed to be removed.", + required = true) + @Valid List deviceIdentifiers); } 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 10e6fef2c9..02ec2514c4 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 @@ -20,20 +20,20 @@ package org.wso2.carbon.device.mgt.jaxrs.service.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceGroupShare; import org.wso2.carbon.device.mgt.jaxrs.service.api.GroupManagementService; import org.wso2.carbon.device.mgt.jaxrs.util.DeviceMgtAPIUtils; import org.wso2.carbon.policy.mgt.common.DeviceGroupWrapper; import javax.ws.rs.Consumes; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.util.ArrayList; @@ -47,12 +47,12 @@ public class GroupManagementServiceImpl implements GroupManagementService { private static final Log log = LogFactory.getLog(GroupManagementServiceImpl.class); @Override - public Response getGroups(@QueryParam("user") String user, @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { + public Response getGroups(int offset, int limit) { try { List groupWrappers = new ArrayList<>(); GroupManagementProviderService service = DeviceMgtAPIUtils.getGroupManagementProviderService(); - List deviceGroups = service.getGroups(user); + String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); + List deviceGroups = service.getGroups(currentUser); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); for (DeviceGroup dg : deviceGroups) { DeviceGroupWrapper gw = new DeviceGroupWrapper(); @@ -76,61 +76,42 @@ public class GroupManagementServiceImpl implements GroupManagementService { } @Override - public Response getGroup(@PathParam("groupName") String groupName) { + public Response getGroup(String groupName) { return null; } @Override - public Response updateGroup(@PathParam("groupName") String groupName, DeviceGroup deviceGroup) { + public Response updateGroup(String groupName, DeviceGroup deviceGroup) { return null; } @Override - public Response deleteGroup(@PathParam("groupName") String groupName) { + public Response deleteGroup(String groupName) { return null; } @Override - public Response shareGroupWithUser(String groupName, String targetUser) { + public Response manageGroupSharing(String groupName, DeviceGroupShare deviceGroupShare) { return null; } @Override - public Response shareGroupWithRole(String groupName, String targetRole) { + public Response getUsersOfGroup(String groupName) { return null; } @Override - public Response removeShareWithUser(@PathParam("groupName") String groupName, - @QueryParam("username") String targetUser) { + public Response getDevicesOfGroup(String groupName, int offset, int limit) { return null; } @Override - public Response removeShareWithRole(@PathParam("groupName") String groupName, - @QueryParam("roleName") String targetUser) { + public Response addDeviceToGroup(String groupName, DeviceIdentifier deviceIdentifier) { return null; } @Override - public Response getUsersOfGroup(@PathParam("groupName") String groupName) { - return null; - } - - @Override - public Response getDevicesOfGroup(@PathParam("groupName") String groupName, @QueryParam("offset") int offset, - @QueryParam("limit") int limit) { - return null; - } - - @Override - public Response addDeviceToGroup(@PathParam("groupName") String groupName, DeviceIdentifier deviceIdentifier) { - return null; - } - - @Override - public Response removeDeviceFromGroup(@PathParam("groupName") String groupName, @QueryParam("type") String type, - @QueryParam("id") String id) { + public Response removeDevicesFromGroup(String groupName, String type, String id) { return null; } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java index cb404d1b92..4f54a37705 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/DeviceGroup.java @@ -17,6 +17,9 @@ */ package org.wso2.carbon.device.mgt.common.group.mgt; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; @@ -25,19 +28,29 @@ import java.util.List; /** * Holds Device Group details and expose to external access */ -@XmlRootElement +@ApiModel(value = "DeviceGroup", description = "This class carries all information related to a managed device group.") public class DeviceGroup implements Serializable { + private static final long serialVersionUID = 1998121711L; + + @ApiModelProperty(name = "id", value = "ID of the device group in the device group information database.", + required = true) private int id; + + @ApiModelProperty(name = "description", value = "The device group description that can be set on the device group by the user.", + required = true) private String description; + + @ApiModelProperty(name = "name", value = "The device group name that can be set on the device group by the user.", + required = true) private String name; + private Long dateOfCreation; private Long dateOfLastUpdate; private String owner; private List users; private List roles; - @XmlElement public int getId() { return id; } @@ -46,7 +59,6 @@ public class DeviceGroup implements Serializable { this.id = id; } - @XmlElement public String getDescription() { return description; } @@ -55,7 +67,6 @@ public class DeviceGroup implements Serializable { this.description = description; } - @XmlElement public String getName() { return name; } @@ -64,7 +75,6 @@ public class DeviceGroup implements Serializable { this.name = name; } - @XmlElement public Long getDateOfCreation() { return dateOfCreation; } @@ -73,7 +83,6 @@ public class DeviceGroup implements Serializable { this.dateOfCreation = dateOfCreation; } - @XmlElement public Long getDateOfLastUpdate() { return dateOfLastUpdate; } @@ -82,7 +91,6 @@ public class DeviceGroup implements Serializable { this.dateOfLastUpdate = dateOfLastUpdate; } - @XmlElement public String getOwner() { return owner; } @@ -91,7 +99,6 @@ public class DeviceGroup implements Serializable { this.owner = owner; } - @XmlElement public List getUsers() { return users; } @@ -100,7 +107,6 @@ public class DeviceGroup implements Serializable { this.users = users; } - @XmlElement public List getRoles() { return roles; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupUser.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupUser.java index 1932c50309..2d80b30574 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupUser.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/group/mgt/GroupUser.java @@ -18,6 +18,9 @@ */ package org.wso2.carbon.device.mgt.common.group.mgt; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import java.io.Serializable; @@ -26,12 +29,17 @@ import java.util.List; /** * This class holds Device Group user name and assigned group roles of user. Exposed to external access */ -@XmlRootElement +@ApiModel(value = "GroupUser", description = "This class carries all information related to a user of a managed device group.") public class GroupUser implements Serializable { + + private static final long serialVersionUID = 1998131711L; + + @ApiModelProperty(name = "username", value = "Username of the user.", required = true) private String username; + + @ApiModelProperty(name = "roles", value = "List of roles assigned to the user.") private List groupRoles; - @XmlElement public String getUsername() { return username; } @@ -40,7 +48,6 @@ public class GroupUser implements Serializable { this.username = username; } - @XmlElement public List getGroupRoles() { return groupRoles; }