Add device subscription functionality for APPM

feature/appm-store/pbac
lasanthaDLPDS 5 years ago
parent ab841939a4
commit c1c821e0f7

@ -25,14 +25,6 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import java.util.List;
public class ApplicationInstallResponse {
@ApiModelProperty(
name = "installedDevices",
value = "List of successful devices",
dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]"
)
private List<DeviceIdentifier> installedDevices;
@ApiModelProperty(
name = "alreadyInstalledDevices",
value = "List of devices that application release is already installed.",
@ -46,14 +38,6 @@ public class ApplicationInstallResponse {
)
private Activity activity;
public List<DeviceIdentifier> getInstalledDevices() {
return installedDevices;
}
public void setInstalledDevices(List<DeviceIdentifier> installedDevices) {
this.installedDevices = installedDevices;
}
public Activity getActivity() {
return activity;
}

@ -18,5 +18,5 @@
package org.wso2.carbon.device.application.mgt.common;
public enum SubsciptionType {
USER, ROLE, DEVICE_GROUP
USER, ROLE, GROUP, DEVICE
}

@ -33,10 +33,10 @@ public interface SubscriptionManager {
* To install an application to given list of devices.
* @param applicationUUID ID of the application to install
* @param deviceList list of device ID's to install the application
* @return {@link ApplicationInstallResponseTmp} object which contains installed application and devices
* @return {@link ApplicationInstallResponse} object which contains installed application and devices
* @throws ApplicationManagementException if unable to install the application to the given devices
*/
ApplicationInstallResponseTmp installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List<DeviceIdentifier> deviceList)
throws ApplicationManagementException;
/**

@ -443,7 +443,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc
StringJoiner joiner = new StringJoiner(",",
"SELECT DS.DM_DEVICE_ID "
+ "FROM AP_DEVICE_SUBSCRIPTION DS "
+ "WHERE US.DM_DEVICE_ID IN (", ") AND TENANT_ID = ?");
+ "WHERE DS.DM_DEVICE_ID IN (", ") AND TENANT_ID = ?");
deviceIds.stream().map(ignored -> "?").forEach(joiner::add);
String query = joiner.toString();
try (PreparedStatement ps = conn.prepareStatement(query)) {

@ -86,15 +86,55 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
@Override
public ApplicationInstallResponseTmp installApplicationForDevices(String applicationUUID,
public ApplicationInstallResponse installApplicationForDevices(String applicationUUID,
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
if (log.isDebugEnabled()) {
log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size()
+ "devices.");
}
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers);
return installToDevicesTmp(applicationDTO, deviceIdentifiers);
List<Integer> operationTriggeredDeviceIds = new ArrayList<>();
List <Device> filteredDevices = validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers);
List<Integer> filteredDeviceIds = new ArrayList<>();
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
Map<DeviceIdentifier , Integer> compatibleDevices = new HashMap<>();
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions;
for (Device device : filteredDevices){
filteredDeviceIds.add(device.getId());
}
deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
for (Device device : filteredDevices) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),
device.getType());
DeviceSubscriptionDTO deviceSubscriptionDTO = deviceSubscriptions.get(device.getId());
if (deviceSubscriptionDTO != null && !deviceSubscriptionDTO.isUnsubscribed()
&& Operation.Status.COMPLETED.toString().equals(deviceSubscriptionDTO.getStatus())) {
installedDeviceIdentifiers.add(deviceIdentifier);
} else {
compatibleDevices.put(deviceIdentifier, device.getId());
}
}
Activity activity = installToDevices(applicationDTO, deviceIdentifiers, deviceIdentifiers.get(0).getType());
List<ActivityStatus> activityStatuses = activity.getActivityStatus();
for (ActivityStatus status : activityStatuses) {
if (status.getStatus().equals(ActivityStatus.Status.PENDING)){
operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier()));
}
}
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setActivity(activity);
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
int operationId = Integer
.parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]);
addDeviceSubscriptionForUser(applicationDTO.getApplicationReleaseDTOs().get(0).getId(),
operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), null, operationId,
SubsciptionType.DEVICE.toString());
return applicationInstallResponse;
}
private ApplicationDTO getApplicationDTO(String uuid) throws ApplicationManagementException {
@ -132,9 +172,12 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
}
}
private void validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO,
private List <Device> validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO,
List<DeviceIdentifier> deviceIdentifiers) throws ApplicationManagementException {
DeviceType deviceType = null;
List <Device> existingDevices = new ArrayList<>();
DeviceManagementProviderService deviceManagementProviderService = HelperUtil
.getDeviceManagementProviderService();
if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) {
deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
}
@ -149,7 +192,23 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
log.error(msg);
throw new BadRequestException(msg);
}
try {
Device device = deviceManagementProviderService.getDevice(deviceIdentifier, false);
if (device == null) {
String msg = "Couldn't found an device for device identifier " + deviceIdentifier.getId()
+ " and device type: " + deviceIdentifier.getType();
log.error(msg);
} else {
existingDevices.add(device);
}
} catch (DeviceManagementException e) {
String msg = "Error occuered when getting device data for divice identifier " + deviceIdentifier.getId()
+ " and device type " + deviceIdentifier.getType();
log.error(msg);
throw new ApplicationManagementException(msg, e);
}
}
return existingDevices;
}
@Override
@ -163,10 +222,10 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
//todo check valid user list - throw BadRequest exception
ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID);
DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId());
List<DeviceIdentifier> operationTriggeredDeviceIdentifiers = new ArrayList<>();
Map<DeviceIdentifier , Integer> compatibleDevices = new HashMap<>();
List<Integer> operationTriggeredDeviceIds = new ArrayList<>();
List<DeviceIdentifier> installedDeviceIdentifiers = new ArrayList<>();
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = new HashMap<>();
for (String user : userList) {
try {
@ -174,13 +233,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<Integer> filteredDeviceIds = new ArrayList<>();
List<Device> filteredDevices = new ArrayList<>();
//todo improve for web clips
for (Device device : userDevices) {
if (appDeviceType.getName().equals(device.getType())) {
filteredDevices.add(device);
filteredDeviceIds.add(device.getId());
}
}
Map<Integer, DeviceSubscriptionDTO> deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds);
for (Device device : filteredDevices) {
DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(),
device.getType());
@ -205,22 +265,21 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
for (ActivityStatus status : activityStatuses) {
if (status.getStatus().equals(ActivityStatus.Status.PENDING)){
operationTriggeredDeviceIds.add(compatibleDevices.get(status.getDeviceIdentifier()));
operationTriggeredDeviceIdentifiers.add(status.getDeviceIdentifier());
}
}
ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse();
applicationInstallResponse.setActivity(activity);
applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers);
applicationInstallResponse.setInstalledDevices(operationTriggeredDeviceIdentifiers);
int operationId = Integer
.parseInt(activity.getActivityId().split(DeviceManagementConstants.OperationAttributes.ACTIVITY)[1]);
addDeviceSubscriptionForUser(applicationDTO.getApplicationReleaseDTOs().get(0).getId(),
operationTriggeredDeviceIds, userList, operationId);
operationTriggeredDeviceIds, new ArrayList<>(deviceSubscriptions.keySet()), userList, operationId, SubsciptionType.USER.toString());
return applicationInstallResponse;
}
private void addDeviceSubscriptionForUser(int applicationReleaseId, List<Integer> deviceIds, List<String> userList, int operationId)
private void addDeviceSubscriptionForUser(int applicationReleaseId, List<Integer> deviceIds,
List<Integer> subDeviceIds, List<String> userList, int operationId, String subType)
throws ApplicationManagementException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
@ -229,24 +288,25 @@ public class SubscriptionManagerImpl implements SubscriptionManager {
List<Integer> deviceResubscribingIds = new ArrayList<>();
List<Integer> deviceSubscriptingIds;
List<String> subscribedUsers = subscriptionDAO.getSubscribedUsernames(userList, tenantId);
if (!subscribedUsers.isEmpty()) {
subscriptionDAO
.updateUserSubscription(tenantId, subscriber, false, subscribedUsers, applicationReleaseId);
userList.removeAll(subscribedUsers);
if (SubsciptionType.USER.toString().equals(subType)){
List<String> subscribedUsers = subscriptionDAO.getSubscribedUsernames(userList, tenantId);
if (!subscribedUsers.isEmpty()) {
subscriptionDAO
.updateUserSubscription(tenantId, subscriber, false, subscribedUsers, applicationReleaseId);
userList.removeAll(subscribedUsers);
}
subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, userList, applicationReleaseId);
}
subscriptionDAO.subscribeUserToApplication(tenantId, subscriber, userList, applicationReleaseId);
List<Integer> subscribedDevices = subscriptionDAO.getSubscribedDeviceIds(deviceIds, tenantId);
if (!subscribedDevices.isEmpty()) {
if (!subDeviceIds.isEmpty()) {
deviceResubscribingIds = subscriptionDAO
.updateDeviceSubscription(subscriber, deviceIds, SubsciptionType.USER.toString(),
Operation.Status.PENDING.toString(), applicationReleaseId, tenantId);
deviceIds.removeAll(subscribedDevices);
.updateDeviceSubscription(subscriber, subDeviceIds, subType, Operation.Status.PENDING.toString(),
applicationReleaseId, tenantId);
deviceIds.removeAll(subDeviceIds);
}
deviceSubscriptingIds = subscriptionDAO
.subscribeDeviceToApplication(subscriber, deviceIds, SubsciptionType.USER.toString(),
Operation.Status.PENDING.toString(), applicationReleaseId, tenantId);
.subscribeDeviceToApplication(subscriber, deviceIds, subType, Operation.Status.PENDING.toString(),
applicationReleaseId, tenantId);
deviceSubscriptingIds.addAll(deviceResubscribingIds);
subscriptionDAO.addOperationMapping(operationId, deviceSubscriptingIds, tenantId);
ConnectionManagerUtil.commitDBTransaction();

@ -64,7 +64,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
}
try {
SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager();
ApplicationInstallResponseTmp response = subscriptionManager
ApplicationInstallResponse response = subscriptionManager
.installApplicationForDevices(uuid, deviceIdentifiers);
return Response.status(Response.Status.OK).entity(response).build();
} catch (NotFoundException e) {
@ -205,7 +205,7 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{
}
try {
ApplicationInstallResponseTmp response = subscriptionManager.installApplicationForDevices(applicationUUID,
ApplicationInstallResponse response = subscriptionManager.installApplicationForDevices(applicationUUID,
installationDetails.getDeviceIdentifiers());
return Response.status(Response.Status.OK).entity(response).build();
} catch (ApplicationManagementException e) {

Loading…
Cancel
Save