Add support to perform group operations (#59)

## Purpose
* Fixes https://roadmap.entgra.net/issues/8916

## Description
* Created a new API to check whether groups has relevant(Android, iOS, Windows) device types.

Co-authored-by: Arshana <arshana790@gmail.com>
Co-authored-by: prathabanKavin <kavinprathaban025@gmail.com>
Reviewed-on: community/device-mgt-core#59
Co-authored-by: Kavin Prathaban <kavin@entgra.io>
Co-committed-by: Kavin Prathaban <kavin@entgra.io>
improvement/fix-task-deletion
Kavin Prathaban 2 years ago committed by Pahansith Gunathilake
parent 7f327bac08
commit 846d7a0f59

@ -1209,7 +1209,7 @@ public interface DeviceManagementService {
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@Path("/{type}/{id}/features") @Path("/device-type/{type}/features")
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,
httpMethod = "GET", httpMethod = "GET",
@ -1280,14 +1280,6 @@ public interface DeviceManagementService {
@PathParam("type") @PathParam("type")
@Size(max = 45) @Size(max = 45)
String type, String type,
@ApiParam(
name = "id",
value = "The device identifier of the device.\n" +
"INFO: Make sure to add the ID of a device that is already registered with WSO2 IoTS.",
required = true)
@PathParam("id")
@Size(max = 45)
String id,
@ApiParam( @ApiParam(
name = "If-Modified-Since", name = "If-Modified-Since",
value = "Checks if the requested variant was modified, since the specified date-time. \n" + value = "Checks if the requested variant was modified, since the specified date-time. \n" +

@ -192,6 +192,13 @@ import java.util.List;
key = "perm:groups:device", key = "perm:groups:device",
roles = {"Internal/devicemgt-user"}, roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/groups/devices/view"} permissions = {"/device-mgt/groups/devices/view"}
),
@Scope(
name = "View whether the groups has relevant device types",
description = "View whether the groups has relevant device types",
key = "perm:groups:devices-types",
roles = {"Internal/devicemgt-user"},
permissions = {"/device-mgt/groups/devices/types"}
) )
} }
) )
@ -1185,4 +1192,58 @@ public interface GroupManagementService {
@QueryParam("requireGroupProps") @QueryParam("requireGroupProps")
boolean requireGroupProps); boolean requireGroupProps);
@Path("/device-types")
@POST
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = HTTPConstants.HEADER_GET,
value = "Getting Details whether the groups has relevant device type or not",
notes = "Getting Details whether the groups has relevant device type or not.",
tags = "Device Group Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:groups:devices-types")
})
},
nickname = "getGroupByGroupNameFilter"
)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "OK. \n Successfully fetched the device types of groups.",
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 = "Error occurred",
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)
})
Response getGroupHasDeviceTypes(
@ApiParam(
name = "identifiers",
value = "GET list of identifiers.",
required = true)
List<String> identifiers);
} }

@ -890,16 +890,14 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
@GET @GET
@Path("/{type}/{id}/features") @Path("/device-type/{type}/features")
@Override @Override
public Response getFeaturesOfDevice( public Response getFeaturesOfDevice(
@PathParam("type") @Size(max = 45) String type, @PathParam("type") @Size(max = 45) String type,
@PathParam("id") @Size(max = 45) String id,
@HeaderParam("If-Modified-Since") String ifModifiedSince) { @HeaderParam("If-Modified-Since") String ifModifiedSince) {
List<Feature> features = new ArrayList<>(); List<Feature> features = new ArrayList<>();
DeviceManagementProviderService dms; DeviceManagementProviderService dms;
try { try {
RequestValidationUtil.validateDeviceIdentifier(type, id);
dms = DeviceMgtAPIUtils.getDeviceManagementService(); dms = DeviceMgtAPIUtils.getDeviceManagementService();
FeatureManager fm; FeatureManager fm;
try { try {
@ -913,8 +911,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
features = fm.getFeatures(); features = fm.getFeatures();
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
String msg = "Error occurred while retrieving the list of features of '" + type + "' device, which " + String msg = "Error occurred while retrieving the list of features of '" + type + "'";
"carries the id '" + id + "'";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();

@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceTypesOfGroups;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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;
@ -66,6 +67,7 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -429,4 +431,20 @@ public class GroupManagementServiceImpl implements GroupManagementService {
} }
} }
@POST
@Path("/device-types")
@Override
public Response getGroupHasDeviceTypes(List<String> identifiers) {
try {
DeviceTypesOfGroups deviceTypesOfGroups = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getDeviceTypesOfGroups(identifiers);
return Response.status(Response.Status.OK).entity(deviceTypesOfGroups).build();
} catch (GroupManagementException e) {
String msg = "Only numbers can exists in a group ID or Invalid Group ID provided.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
}
}
} }

@ -590,7 +590,7 @@ public class DeviceManagementServiceImplTest {
PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService")) PowerMockito.stub(PowerMockito.method(DeviceMgtAPIUtils.class, "getDeviceManagementService"))
.toReturn(this.deviceManagementProviderService); .toReturn(this.deviceManagementProviderService);
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getFeaturesOfDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString(), null); .getFeaturesOfDevice(TEST_DEVICE_TYPE, null);
Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode());
} }
@ -601,7 +601,7 @@ public class DeviceManagementServiceImplTest {
Mockito.when(this.deviceManagementProviderService.getFeatureManager(Mockito.anyString())) Mockito.when(this.deviceManagementProviderService.getFeatureManager(Mockito.anyString()))
.thenThrow(new DeviceTypeNotFoundException()); .thenThrow(new DeviceTypeNotFoundException());
Response response = this.deviceManagementService Response response = this.deviceManagementService
.getFeaturesOfDevice(TEST_DEVICE_TYPE, UUID.randomUUID().toString(), null); .getFeaturesOfDevice(TEST_DEVICE_TYPE, null);
Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode()); Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode());
Mockito.reset(this.deviceManagementProviderService); Mockito.reset(this.deviceManagementProviderService);
} }

@ -0,0 +1,60 @@
/*
* Copyright (c) 2022, 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.common.group.mgt;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.io.Serializable;
@ApiModel(value = "DeviceTypesOfGroups", description = "This class carries whether the groups has device type or not.")
public class DeviceTypesOfGroups implements Serializable {
private static final long serialVersionUID = 5562356373277828099L;
@ApiModelProperty(name = "hasAndroid", value = "groups has Android devices.")
private boolean hasAndroid;
@ApiModelProperty(name = "id", value = "groups has iOS devices.")
private boolean hasIos;
@ApiModelProperty(name = "hasAndroid", value = "groups has Windows devices.")
private boolean hasWindows;
public boolean isHasAndroid() {
return hasAndroid;
}
public void setHasAndroid(boolean hasAndroid) {
this.hasAndroid = hasAndroid;
}
public boolean isHasIos() {
return hasIos;
}
public void setHasIos(boolean hasIos) {
this.hasIos = hasIos;
}
public boolean isHasWindows() {
return hasWindows;
}
public void setHasWindows(boolean hasWindows) {
this.hasWindows = hasWindows;
}
}

@ -41,6 +41,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.PaginationResult;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceTypesOfGroups;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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;
@ -335,4 +336,11 @@ public interface GroupManagementProviderService {
*/ */
boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) throws GroupManagementException; boolean isDeviceMappedToGroup(int groupId, DeviceIdentifier deviceIdentifier) throws GroupManagementException;
/**
*
* @param identifiers identifiers of groups
* @return whether the groups has android, iOS, Windows device types
* @throws GroupManagementException
*/
DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException;
} }

@ -43,6 +43,7 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest; import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
@ -51,6 +52,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceTypesOfGroups;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException; 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;
@ -79,6 +81,7 @@ import java.util.stream.Collectors;
public class GroupManagementProviderServiceImpl implements GroupManagementProviderService { public class GroupManagementProviderServiceImpl implements GroupManagementProviderService {
private static final Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class); private static final Log log = LogFactory.getLog(GroupManagementProviderServiceImpl.class);
private static final String DEVICE_STATUS_REMOVED = "REMOVED";
private final GroupDAO groupDAO; private final GroupDAO groupDAO;
private final DeviceDAO deviceDAO; private final DeviceDAO deviceDAO;
@ -1379,4 +1382,60 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
createGroupWithChildren(nextParentGroup, childrenGroups, requireGroupProps, tenantId, depth, counter); createGroupWithChildren(nextParentGroup, childrenGroups, requireGroupProps, tenantId, depth, counter);
} }
} }
@Override
public DeviceTypesOfGroups getDeviceTypesOfGroups(List<String> identifiers) throws GroupManagementException {
DeviceTypesOfGroups deviceTypesOfGroups = new DeviceTypesOfGroups();
List<Integer> groupsIDs = new ArrayList<>();
try {
for (String id : identifiers) {
groupsIDs.add(Integer.parseInt(id));
}
List<String> deviceIDs = new ArrayList<>();
List<Device> allDevices = new ArrayList<>();
for (Integer groupID : groupsIDs) {
DeviceGroup deviceGroup = getGroup(groupID, false);
if (deviceGroup == null) {
String errorMessage = "Invalid Group ID provided.";
log.error(errorMessage);
throw new GroupManagementException(errorMessage);
}
List<Device> devices = getAllDevicesOfGroup(deviceGroup.getName(), false);
for (Device device : devices) {
if (!DEVICE_STATUS_REMOVED.equals(device.getEnrolmentInfo().getStatus().toString())
&& !deviceIDs.contains(device.getDeviceIdentifier())) {
deviceIDs.add(device.getDeviceIdentifier());
allDevices.add(device);
}
}
}
for (Device device : allDevices) {
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID.equals(device.getType())) {
deviceTypesOfGroups.setHasAndroid(true);
break;
}
}
for (Device device : allDevices) {
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_IOS.equals(device.getType())) {
deviceTypesOfGroups.setHasIos(true);
break;
}
}
for (Device device : allDevices) {
if (DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS.equals(device.getType())) {
deviceTypesOfGroups.setHasWindows(true);
break;
}
}
} catch (NumberFormatException e) {
String errorMessage = "Only numbers can exists in a group ID";
log.error(errorMessage);
throw new GroupManagementException(errorMessage, e);
}
return deviceTypesOfGroups;
}
} }

@ -130,6 +130,7 @@
<Scope>perm:groups:add</Scope> <Scope>perm:groups:add</Scope>
<Scope>perm:groups:device</Scope> <Scope>perm:groups:device</Scope>
<Scope>perm:groups:devices-count</Scope> <Scope>perm:groups:devices-count</Scope>
<Scope>perm:groups:devices-types</Scope>
<Scope>perm:groups:remove</Scope> <Scope>perm:groups:remove</Scope>
<Scope>perm:groups:groups</Scope> <Scope>perm:groups:groups</Scope>
<Scope>perm:groups:groups-view</Scope> <Scope>perm:groups:groups-view</Scope>

Loading…
Cancel
Save