From c1c821e0f742b96ede116c77cd4043d7b5059337 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 23 May 2019 14:03:42 +0530 Subject: [PATCH] Add device subscription functionality for APPM --- .../common/ApplicationInstallResponse.java | 16 --- .../mgt/common/SubsciptionType.java | 2 +- .../common/services/SubscriptionManager.java | 4 +- .../GenericSubscriptionDAOImpl.java | 2 +- .../core/impl/SubscriptionManagerImpl.java | 106 ++++++++++++++---- .../impl/SubscriptionManagementAPIImpl.java | 4 +- 6 files changed, 89 insertions(+), 45 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java index e1ca9df104..5d3bc260bf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/ApplicationInstallResponse.java @@ -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 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 getInstalledDevices() { - return installedDevices; - } - - public void setInstalledDevices(List installedDevices) { - this.installedDevices = installedDevices; - } - public Activity getActivity() { return activity; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubsciptionType.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubsciptionType.java index 2cee2c5b88..0e6e772ec5 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubsciptionType.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubsciptionType.java @@ -18,5 +18,5 @@ package org.wso2.carbon.device.application.mgt.common; public enum SubsciptionType { - USER, ROLE, DEVICE_GROUP + USER, ROLE, GROUP, DEVICE } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java index 036b442024..48dff00135 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/SubscriptionManager.java @@ -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 deviceList) + ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List deviceList) throws ApplicationManagementException; /** diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index b1904b68fd..3206ebdb55 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -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)) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index 46e0bc11f5..0b97838dd2 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -86,15 +86,55 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } @Override - public ApplicationInstallResponseTmp installApplicationForDevices(String applicationUUID, + public ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List 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 operationTriggeredDeviceIds = new ArrayList<>(); + List filteredDevices = validateAppInstallingForDevicesRequest(applicationDTO, deviceIdentifiers); + List filteredDeviceIds = new ArrayList<>(); + List installedDeviceIdentifiers = new ArrayList<>(); + Map compatibleDevices = new HashMap<>(); + Map 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 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 validateAppInstallingForDevicesRequest(ApplicationDTO applicationDTO, List deviceIdentifiers) throws ApplicationManagementException { DeviceType deviceType = null; + List 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 operationTriggeredDeviceIdentifiers = new ArrayList<>(); Map compatibleDevices = new HashMap<>(); List operationTriggeredDeviceIds = new ArrayList<>(); List installedDeviceIdentifiers = new ArrayList<>(); + Map deviceSubscriptions = new HashMap<>(); for (String user : userList) { try { @@ -174,13 +233,14 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List filteredDeviceIds = new ArrayList<>(); List 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 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 deviceIds, List userList, int operationId) + private void addDeviceSubscriptionForUser(int applicationReleaseId, List deviceIds, + List subDeviceIds, List 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 deviceResubscribingIds = new ArrayList<>(); List deviceSubscriptingIds; - List 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 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 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(); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java index 4dfdb27f76..59ec1bddee 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/SubscriptionManagementAPIImpl.java @@ -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) {