From dd7803522780286d1a16ecae559c1be3f133e4d1 Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Thu, 30 May 2019 21:43:17 +0530 Subject: [PATCH] Improve APPM subscription handeling --- .../common/ApplicationInstallResponse.java | 22 +- .../mgt/common/SubscribingDeviceIdHolder.java | 22 +- .../common/services/SubscriptionManager.java | 56 +-- .../core/impl/SubscriptionManagerImpl.java | 438 ++++-------------- .../src/main/webapp/WEB-INF/cxf-servlet.xml | 2 + .../services/SubscriptionManagementAPI.java | 300 +----------- .../impl/SubscriptionManagementAPIImpl.java | 115 +---- 7 files changed, 139 insertions(+), 816 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 6bc83c9a57e..0b60de01ddb 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 @@ -26,11 +26,11 @@ import java.util.List; public class ApplicationInstallResponse { @ApiModelProperty( - name = "alreadyInstalledDevices", + name = "ignoredDeviceIdentifiers", value = "List of devices that application release is already installed.", dataType = "List[org.wso2.carbon.device.mgt.common.DeviceIdentifier]" ) - private List alreadyInstalledDevices; + private List ignoredDeviceIdentifiers; @ApiModelProperty( name = "errorDevices", @@ -44,22 +44,22 @@ public class ApplicationInstallResponse { name = "activity", value = "Activity corresponding to the operation" ) - private Activity activity; + private List activities; - public Activity getActivity() { - return activity; + public List getActivities() { + return activities; } - public void setActivity(Activity activity) { - this.activity = activity; + public void setActivities(List activity) { + this.activities = activity; } - public List getAlreadyInstalledDevices() { - return alreadyInstalledDevices; + public List getIgnoredDeviceIdentifiers() { + return ignoredDeviceIdentifiers; } - public void setAlreadyInstalledDevices(List alreadyInstalledDevices) { - this.alreadyInstalledDevices = alreadyInstalledDevices; + public void setIgnoredDeviceIdentifiers(List ignoredDeviceIdentifiers) { + this.ignoredDeviceIdentifiers = ignoredDeviceIdentifiers; } public List getErrorDevices() { return errorDevices; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java index 6c7a3cb04b4..82e87c8b680 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/SubscribingDeviceIdHolder.java @@ -20,30 +20,28 @@ package org.wso2.carbon.device.application.mgt.common; import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; public class SubscribingDeviceIdHolder { - private List installedDevices = new ArrayList<>(); - private Map compatibleDevices = new HashMap<>(); + private Map subscribedDevices = new HashMap<>(); + private Map subscribableDevices = new HashMap<>(); private Map deviceSubscriptions = new HashMap<>(); - public List getInstalledDevices() { - return installedDevices; + public Map getSubscribedDevices() { + return subscribedDevices; } - public void setInstalledDevices(List installedDevices) { - this.installedDevices = installedDevices; + public void setSubscribedDevices(Map subscribedDevices) { + this.subscribedDevices = subscribedDevices; } - public Map getCompatibleDevices() { - return compatibleDevices; + public Map getSubscribableDevices() { + return subscribableDevices; } - public void setCompatibleDevices(Map compatibleDevices) { - this.compatibleDevices = compatibleDevices; + public void setSubscribableDevices(Map subscribableDevices) { + this.subscribableDevices = subscribableDevices; } public Map getDeviceSubscriptions() { 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 1f0e86b5842..e1c10ea9a9c 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 @@ -19,9 +19,7 @@ package org.wso2.carbon.device.application.mgt.common.services; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; -import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import java.util.List; @@ -29,56 +27,6 @@ import java.util.List; * This interface manages all the operations related with ApplicationDTO Subscription. */ 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 ApplicationInstallResponse} object which contains installed application and devices - * @throws ApplicationManagementException if unable to install the application to the given devices - */ - ApplicationInstallResponse installApplicationForDevices(String applicationUUID, List deviceList) - throws ApplicationManagementException; - - ApplicationInstallResponse performBulkAppInstallation(String applicationUUID, List params, String subType) - throws ApplicationManagementException; - - /** - * To install an application to given list of users. - * @param applicationUUID ID of the application to install - * @param userList list of users to install the application - * @return {@link ApplicationInstallResponseTmp} object which contains installed application and devices - * @throws ApplicationManagementException if unable to install the application to devices belong to given users - */ - ApplicationInstallResponse installApplicationForUsers(String applicationUUID, List userList) - throws ApplicationManagementException; - - /** - * To install an application to given list of roles. - * @param applicationUUID ID of the application to install - * @param roleList list of roles to install the application - * @return {@link ApplicationInstallResponseTmp} object which contains installed application and devices - * @throws ApplicationManagementException if unable to install the application to devices belong to given roles - */ - ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID, List roleList) - throws ApplicationManagementException; - - /** - * To install an application to given list of roles. - * @param applicationUUID ID of the application to install - * @param deviceGroupList list of device groups to install the application - * @return {@link ApplicationInstallResponseTmp} object which contains installed application and devices - * @throws ApplicationManagementException if unable to install the application to devices belong to given groups - */ - ApplicationInstallResponseTmp installApplicationForGroups(String applicationUUID, List deviceGroupList) - throws ApplicationManagementException; - - /** - * To uninstall an application from a given list of devices. - * @param applicationUUID ApplicationDTO ID - * @param deviceList Device list - * @return Failed Device List which the application was unable to uninstall - * @throws ApplicationManagementException ApplicationDTO Management Exception - */ - List uninstallApplication(String applicationUUID, List deviceList) - throws ApplicationManagementException; + ApplicationInstallResponse performBulkAppInstallation(String applicationUUID, List params, String subType, + String action) 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/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 9141cd083bd..1a4d2991e5a 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 @@ -21,7 +21,6 @@ import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.common.AppOperation; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; import org.wso2.carbon.device.application.mgt.common.ApplicationType; import org.wso2.carbon.device.application.mgt.common.DeviceTypes; @@ -29,13 +28,11 @@ import org.wso2.carbon.device.application.mgt.common.SubAction; import org.wso2.carbon.device.application.mgt.common.SubsciptionType; import org.wso2.carbon.device.application.mgt.common.SubscribingDeviceIdHolder; import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; -import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp; import org.wso2.carbon.device.application.mgt.common.dto.DeviceSubscriptionDTO; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.DBConnectionException; import org.wso2.carbon.device.application.mgt.common.exception.LifecycleManagementException; import org.wso2.carbon.device.application.mgt.common.exception.TransactionManagementException; -import org.wso2.carbon.device.application.mgt.common.services.ApplicationManager; import org.wso2.carbon.device.application.mgt.common.services.SubscriptionManager; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; @@ -51,23 +48,18 @@ import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.application.mgt.core.util.HelperUtil; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.MobileApp; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.exceptions.UnknownApplicationTypeException; -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.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.ActivityStatus; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; -import org.wso2.carbon.device.mgt.core.operation.mgt.util.DeviceIDHolder; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.MDMAndroidOperationUtil; import org.wso2.carbon.device.mgt.core.util.MDMIOSOperationUtil; @@ -84,7 +76,6 @@ import java.util.stream.Collectors; public class SubscriptionManagerImpl implements SubscriptionManager { private static final Log log = LogFactory.getLog(SubscriptionManagerImpl.class); - private static final String INSTALL_APPLICATION = "INSTALL_APPLICATION"; private SubscriptionDAO subscriptionDAO; private ApplicationDAO applicationDAO; private LifecycleStateManager lifecycleStateManager; @@ -95,32 +86,9 @@ public class SubscriptionManagerImpl implements SubscriptionManager { this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); } - @Override - public ApplicationInstallResponse installApplicationForDevices(String applicationUUID, - List deviceIdentifiers) throws ApplicationManagementException { - if (log.isDebugEnabled()) { - log.debug("Install application which has UUID: " + applicationUUID + " to " + deviceIdentifiers.size() - + "devices."); - } - if (deviceIdentifiers.isEmpty()){ - String msg = "In order to install application release which has UUID " + applicationUUID + ", you should " - + "provide list of device identifiers. But found an empty list of identifiers."; - log.error(msg); - throw new BadRequestException(msg); - } - ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); - DeviceIDHolder filteredDeviceHolder = filterDeviceIdentifiers(applicationDTO, deviceIdentifiers); - List errorDeviceIdentifiers = filteredDeviceHolder.getErrorDeviceIdList(); - ApplicationInstallResponse applicationInstallResponse = performBulkAppInstallation(applicationUUID, - filteredDeviceHolder.getValidDeviceIDList(), SubsciptionType.DEVICE.toString()); - applicationInstallResponse.setErrorDevices(errorDeviceIdentifiers); - return applicationInstallResponse; - } - - @Override public ApplicationInstallResponse performBulkAppInstallation(String applicationUUID, List params, - String subType) throws ApplicationManagementException { + String subType, String action) throws ApplicationManagementException { if (log.isDebugEnabled()) { log.debug("Install application release which has UUID " + applicationUUID + " to " + params.size() + " users."); @@ -149,12 +117,31 @@ public class SubscriptionManagerImpl implements SubscriptionManager { List filteredDevices = new ArrayList<>(); List devices = new ArrayList<>(); List subscribers = new ArrayList<>(); + List errorDeviceIdentifiers = new ArrayList<>(); + ApplicationInstallResponse applicationInstallResponse; ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); - if (SubsciptionType.DEVICE.toString().equals(subType)) { for (T param : params) { DeviceIdentifier deviceIdentifier = (DeviceIdentifier) param; + if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils + .isEmpty(deviceIdentifier.getType())) { + log.warn("Found a device identifier which has either empty identity of the device or empty" + + " device type. Hence ignoring the device identifier. "); + } + if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { + DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); + + if (!deviceType.getName().equals(deviceIdentifier.getType())) { + String msg = + "Found a device identifier which is not matched with the application device Type. " + + "Application device type is " + deviceType.getName() + " and the " + + "identifier of which has a " + "different device type is " + + deviceIdentifier.getId(); + log.warn(msg); + errorDeviceIdentifiers.add(deviceIdentifier); + } + } devices.add(deviceManagementProviderService.getDevice(deviceIdentifier, false)); } } else if (SubsciptionType.USER.toString().equals(subType)) { @@ -185,18 +172,18 @@ public class SubscriptionManagerImpl implements SubscriptionManager { filteredDevices.add(device); } } -// if (SubsciptionType.DEVICE.toString().equals(subType)) { -// return installToDevices(deviceTypeName, filteredDevices, applicationDTO, subType, null); -// -// } - return installToDevices(deviceTypeName, filteredDevices, applicationDTO, subType, subscribers); + applicationInstallResponse = performActionOnDevices(deviceTypeName, filteredDevices, applicationDTO, + subType, subscribers, action); + + //todo add db insert to here } else { //todo improve this -// if (SubsciptionType.DEVICE.toString().equals(subType)) { -// return installToDevices(null, filteredDevices, applicationDTO, subType, null); -// } - return installToDevices(null, devices, applicationDTO, subType, subscribers); + applicationInstallResponse = performActionOnDevices(null, devices, applicationDTO, subType, + subscribers, action); + //todo add db insert to here } + applicationInstallResponse.setErrorDevices(errorDeviceIdentifiers); + return applicationInstallResponse; } catch (DeviceManagementException e) { String msg = "Error occurred while getting devices of given users or given roles."; log.error(msg); @@ -208,56 +195,98 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - @Override - public List uninstallApplication(String applicationUUID, - List deviceList) throws ApplicationManagementException { - return null; - } - - private ApplicationInstallResponse installToDevices(String deviceType, List devices, - ApplicationDTO applicationDTO, String subType, List subscribers) + private ApplicationInstallResponse performActionOnDevices(String deviceType, List devices, + ApplicationDTO applicationDTO, String subType, List subscribers, String action) throws ApplicationManagementException { SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(devices); - if (subscribingDeviceIdHolder.getCompatibleDevices().isEmpty()) { + List activityList = new ArrayList<>(); + List deviceIdentifiers = new ArrayList<>(); + List ignoredDeviceIdentifiers = new ArrayList<>(); + Map> deviceIdentifierMap = new HashMap<>(); + + if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { + deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribableDevices().keySet()); + ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribedDevices().keySet()); + + if (deviceIdentifiers.isEmpty()) { + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); + applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); + return applicationInstallResponse; + } + } else if (SubAction.UNINSTALL.toString().equalsIgnoreCase(action)) { + deviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribedDevices().keySet()); + ignoredDeviceIdentifiers = new ArrayList<>(subscribingDeviceIdHolder.getSubscribableDevices().keySet()); + if (deviceIdentifiers.isEmpty()) { + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); + applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); + return applicationInstallResponse; + } + } + + if (deviceType == null) { + for (DeviceIdentifier identifier : deviceIdentifiers) { + List identifiers; + if (!deviceIdentifierMap.containsKey(identifier.getType())) { + identifiers = new ArrayList<>(); + identifiers.add(identifier); + deviceIdentifierMap.put(identifier.getType(), identifiers); + } else { + identifiers = deviceIdentifierMap.get(identifier.getType()); + identifiers.add(identifier); + deviceIdentifierMap.put(identifier.getType(), identifiers); + } + } + + for (String type : deviceIdentifierMap.keySet()) { + Activity activity = addAppInstallOperationToDevices(applicationDTO, + new ArrayList<>(deviceIdentifierMap.get(type)), type); + activityList.add(activity); + } + ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices()); + applicationInstallResponse.setActivities(activityList); + applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); return applicationInstallResponse; } - Activity activity = addAppInstallOperationToDevices(applicationDTO, - new ArrayList<>(subscribingDeviceIdHolder.getCompatibleDevices().keySet()), deviceType); + //todo consider action + + Activity activity = addAppInstallOperationToDevices(applicationDTO, deviceIdentifiers, deviceType); + activityList.add(activity); ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setActivity(activity); - applicationInstallResponse.setAlreadyInstalledDevices(subscribingDeviceIdHolder.getInstalledDevices()); + applicationInstallResponse.setActivities(activityList); + applicationInstallResponse.setIgnoredDeviceIdentifiers(ignoredDeviceIdentifiers); + //todo addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity, - subscribingDeviceIdHolder.getCompatibleDevices(), + subscribingDeviceIdHolder.getSubscribableDevices(), new ArrayList<>(subscribingDeviceIdHolder.getDeviceSubscriptions().keySet()), subscribers, subType); return applicationInstallResponse; } private SubscribingDeviceIdHolder getSubscribingDeviceIdHolder(List devices) throws ApplicationManagementException { - List installedDevices = new ArrayList<>(); - Map compatibleDevices = new HashMap<>(); + Map subscribedDevices = new HashMap<>(); + Map subscribableDevices = new HashMap<>(); List filteredDeviceIds = devices.stream().map(Device::getId).collect(Collectors.toList()); + //get device subscriptions for given device id list. Map deviceSubscriptions = getDeviceSubscriptions(filteredDeviceIds); for (Device device : devices) { 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())) { - installedDevices.add(deviceIdentifier); + subscribedDevices.put(deviceIdentifier, device.getId()); } else { - compatibleDevices.put(deviceIdentifier, device.getId()); + subscribableDevices.put(deviceIdentifier, device.getId()); } } SubscribingDeviceIdHolder subscribingDeviceIdHolder = new SubscribingDeviceIdHolder(); - subscribingDeviceIdHolder.setCompatibleDevices(compatibleDevices); - subscribingDeviceIdHolder.setInstalledDevices(installedDevices); + subscribingDeviceIdHolder.setSubscribableDevices(subscribableDevices); + subscribingDeviceIdHolder.setSubscribedDevices(subscribedDevices); subscribingDeviceIdHolder.setDeviceSubscriptions(deviceSubscriptions); return subscribingDeviceIdHolder; } @@ -297,51 +326,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - private DeviceIDHolder filterDeviceIdentifiers(ApplicationDTO applicationDTO, - List deviceIdentifiers) throws ApplicationManagementException { - List errorDeviceIdList = new ArrayList<>(); - List validDeviceIDList = new ArrayList<>(); - if (!ApplicationType.WEB_CLIP.toString().equals(applicationDTO.getType())) { - DeviceType deviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) { - log.warn("Found a device identifier which has either empty identity of the device or empty device " - + "type. Hence ignoring the device identifier. "); - continue; - } - if (!deviceType.getName().equals(deviceIdentifier.getType())) { - String msg = - "Found a device identifier which is not matched with the application device Type. Application " - + "device type is " + deviceType.getName() + " and the identifier of which has a " - + "different device type is " + deviceIdentifier.getId(); - log.warn(msg); - errorDeviceIdList.add(deviceIdentifier); - } else { - validDeviceIDList.add(deviceIdentifier); - } - } - } else { - for (DeviceIdentifier deviceIdentifier : deviceIdentifiers) { - if (StringUtils.isEmpty(deviceIdentifier.getId()) || StringUtils.isEmpty(deviceIdentifier.getType())) { - log.warn("Found a device identifier which has either empty identity of the device or empty device " - + "type. Hence ignoring the device identifier. "); - continue; - } - validDeviceIDList.add(deviceIdentifier); - } - } - - if (validDeviceIDList.isEmpty()) { - String msg = "Couldn't found at least one valid device identifier to install the application."; - log.error(msg); - throw new BadRequestException(msg); - } - DeviceIDHolder deviceIDHolder = new DeviceIDHolder(); - deviceIDHolder.setValidDeviceIDList(validDeviceIDList); - deviceIDHolder.setErrorDeviceIdList(errorDeviceIdList); - return deviceIDHolder; - } - private void addSubscriptions(int applicationReleaseId, Activity activity, Map compatibleDevices, List subDeviceIds, List subscribers, String subType) throws ApplicationManagementException { @@ -426,153 +410,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } - //todo remove below methods - - @Override public ApplicationInstallResponse installApplicationForUsers(String applicationUUID, - List userList) throws ApplicationManagementException { - if (log.isDebugEnabled()) { - log.debug("Install application release which has UUID " + applicationUUID + " to " + userList.size() - + " users."); - } - - //todo check valid user list - throw BadRequest exception - ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); - DeviceType appDeviceType = APIUtil.getDeviceTypeData(applicationDTO.getDeviceTypeId()); - Map compatibleDevices = new HashMap<>(); - List installedDeviceIdentifiers = new ArrayList<>(); - Map deviceSubscriptions = new HashMap<>(); - - for (String user : userList) { - try { - List userDevices = HelperUtil.getDeviceManagementProviderService().getDevicesOfUser(user); - List filteredDevices = new ArrayList<>(); - - //todo improve for web clips - for (Device device : userDevices) { - if (appDeviceType.getName().equals(device.getType())) { - filteredDevices.add(device); - } - } - - SubscribingDeviceIdHolder subscribingDeviceIdHolder = getSubscribingDeviceIdHolder(filteredDevices); - compatibleDevices.putAll(subscribingDeviceIdHolder.getCompatibleDevices()); - deviceSubscriptions.putAll(subscribingDeviceIdHolder.getDeviceSubscriptions()); - installedDeviceIdentifiers.addAll(subscribingDeviceIdHolder.getInstalledDevices()); - } catch (DeviceManagementException e) { - String msg = "Error occurred when extracting the device list of user[" + user + "]."; - log.error(msg); - throw new ApplicationManagementException(msg, e); - } - } - - if (compatibleDevices.keySet().isEmpty()) { - ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); - return applicationInstallResponse; - } - Activity activity = addAppInstallOperationToDevices(applicationDTO, new ArrayList<>(compatibleDevices.keySet()), - appDeviceType.getName()); - - ApplicationInstallResponse applicationInstallResponse = new ApplicationInstallResponse(); - applicationInstallResponse.setActivity(activity); - applicationInstallResponse.setAlreadyInstalledDevices(installedDeviceIdentifiers); - - addSubscriptions(applicationDTO.getApplicationReleaseDTOs().get(0).getId(), activity, compatibleDevices, - new ArrayList<>(deviceSubscriptions.keySet()), userList, SubsciptionType.USER.toString()); - return applicationInstallResponse; - } - - //todo not implemented below code properly - - @Override public ApplicationInstallResponseTmp installApplicationForRoles(String applicationUUID, - List roleList) throws ApplicationManagementException { - if (log.isDebugEnabled()) { - log.debug("Install application: " + applicationUUID + " to " + roleList.size() + " roles."); - } - ApplicationManager applicationManager = DataHolder.getInstance().getApplicationManager(); - ApplicationDTO application = applicationManager.getApplicationByRelease(applicationUUID); - List deviceList = new ArrayList<>(); - for (String role : roleList) { - try { - List devicesOfRole = HelperUtil.getDeviceManagementProviderService().getAllDevicesOfRole(role); - devicesOfRole.stream() - .map(device -> new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())) - .forEach(deviceList::add); - if (log.isDebugEnabled()) { - log.debug(devicesOfRole.size() + " found for role: " + role); - } - } catch (DeviceManagementException e) { - throw new ApplicationManagementException( - "Error when extracting the device list from role[" + role + "].", e); - } - } - - ApplicationInstallResponseTmp response = installToDevicesTmp(application, deviceList); - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - int applicationReleaseId = application.getApplicationReleaseDTOs().get(0).getId(); - - try { - ConnectionManagerUtil.openDBConnection(); - subscriptionDAO.subscribeRoleToApplication(tenantId, subscriber, roleList, application.getId(), - applicationReleaseId); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - - return response; - } - - @Override public ApplicationInstallResponseTmp installApplicationForGroups(String applicationUUID, - List deviceGroupList) throws ApplicationManagementException { - if (log.isDebugEnabled()) { - log.debug("Install application: " + applicationUUID + " to " + deviceGroupList.size() + " groups."); - } - ApplicationManager applicationManager = DataHolder.getInstance().getApplicationManager(); - ApplicationDTO application = applicationManager.getApplicationByRelease(applicationUUID); - GroupManagementProviderService groupManagementProviderService = HelperUtil.getGroupManagementProviderService(); - List groupList = new ArrayList<>(); - List deviceList = new ArrayList<>(); - for (String groupName : deviceGroupList) { - try { - DeviceGroup deviceGroup = groupManagementProviderService.getGroup(groupName); - groupList.add(deviceGroup); - int deviceCount = groupManagementProviderService.getDeviceCount(deviceGroup.getGroupId()); - List devicesOfGroups = groupManagementProviderService - .getDevices(deviceGroup.getGroupId(), 0, deviceCount); - devicesOfGroups.stream() - .map(device -> new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())) - .forEach(deviceList::add); - } catch (GroupManagementException e) { - throw new ApplicationManagementException( - "Error when extracting the device list from group[" + groupName + "].", e); - } - } - - ApplicationInstallResponseTmp response = installToDevicesTmp(application, deviceList); - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - int applicationReleaseId = application.getApplicationReleaseDTOs().get(0).getId(); - - try { - ConnectionManagerUtil.openDBConnection(); - subscriptionDAO.subscribeGroupToApplication(tenantId, subscriber, groupList, application.getId(), - applicationReleaseId); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - - return response; - } - private Activity addAppInstallOperationToDevices(ApplicationDTO application, List deviceIdentifierList, String deviceType) throws ApplicationManagementException { DeviceManagementProviderService deviceManagementProviderService = HelperUtil @@ -590,61 +427,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - private ApplicationInstallResponseTmp installToDevicesTmp(ApplicationDTO application, - List deviceIdentifierList) throws ApplicationManagementException { - DeviceManagementProviderService deviceManagementProviderService = HelperUtil - .getDeviceManagementProviderService(); - - ApplicationInstallResponseTmp response = validateDevices(deviceIdentifierList); - /* - Group the valid device list by device type. Following lambda expression produces a map containing device type - as the key and the list of device identifiers as the corresponding value. - */ - Map> deviceTypeIdentifierMap = response.getSuccessfulDevices().stream() - .collect(Collectors.groupingBy(DeviceIdentifier::getType)); - - for (Map.Entry> entry : deviceTypeIdentifierMap.entrySet()) { - Operation operation = generateOperationPayloadByDeviceType(entry.getKey(), application, null); - try { - Activity activity = deviceManagementProviderService - .addOperation(entry.getKey(), operation, entry.getValue()); - response.setActivity(activity); - } catch (OperationManagementException e) { - response.setSuccessfulDevices(null); - response.setFailedDevices(deviceIdentifierList); - throw new ApplicationManagementException( - "Error occurred while adding the application install " + "operation to devices", e); - } catch (InvalidDeviceException e) { - //This exception should not occur because the validation has already been done. - throw new ApplicationManagementException("The list of device identifiers are invalid"); - } - } - - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - String subscriber = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - int applicationReleaseId = application.getApplicationReleaseDTOs().get(0).getId(); - try { - ConnectionManagerUtil.openDBConnection(); - List deviceList = new ArrayList<>(); - for (DeviceIdentifier deviceIdentifier : response.getSuccessfulDevices()) { - try { - deviceList.add(deviceManagementProviderService.getDevice(deviceIdentifier)); - } catch (DeviceManagementException e) { - log.error("Unable to fetch device for device identifier: " + deviceIdentifier.toString()); - } - } - subscriptionDAO.subscribeDeviceToApplicationTmp(tenantId, subscriber, deviceList, application.getId(), - applicationReleaseId, String.valueOf(AppOperation.InstallState.UNINSTALLED)); - } catch (ApplicationManagementDAOException e) { - //todo - throw new ApplicationManagementException(""); - } finally { - ConnectionManagerUtil.closeDBConnection(); - } - - return response; - } - private Operation generateOperationPayloadByDeviceType(String deviceType, ApplicationDTO application, String action) throws ApplicationManagementException { try { @@ -684,38 +466,4 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - /** - * Validates the preconditions which is required to satisfy from the device which is required to install the - * application. - *

- * This method check two preconditions whether the application type is compatible to install in the device and - * whether the device is enrolled in the system. - * - * @param deviceIdentifierList List of {@link DeviceIdentifier} which the validation happens - * @return {@link ApplicationInstallResponseTmp} which contains compatible and incompatible device identifiers - */ - private ApplicationInstallResponseTmp validateDevices(List deviceIdentifierList) { - ApplicationInstallResponseTmp applicationInstallResponseTmp = new ApplicationInstallResponseTmp(); - List failedDevices = new ArrayList<>(); - List compatibleDevices = new ArrayList<>(); - - for (DeviceIdentifier deviceIdentifier : deviceIdentifierList) { - try { - if (!DeviceManagerUtil.isValidDeviceIdentifier(deviceIdentifier)) { - log.error("Device with ID: [" + deviceIdentifier.getId() + "] is not valid to install the " - + "application."); - applicationInstallResponseTmp.getFailedDevices().add(deviceIdentifier); - } - } catch (DeviceManagementException e) { - log.error("Error occurred while validating the device: [" + deviceIdentifier.getId() + "]", e); - failedDevices.add(deviceIdentifier); - } - compatibleDevices.add(deviceIdentifier); - } - applicationInstallResponseTmp.setFailedDevices(failedDevices); - applicationInstallResponseTmp.setSuccessfulDevices(compatibleDevices); - - return applicationInstallResponseTmp; - } - } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/WEB-INF/cxf-servlet.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/WEB-INF/cxf-servlet.xml index 7b060ca0894..661cc26f85b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -26,6 +26,7 @@ + @@ -53,6 +54,7 @@ + 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/SubscriptionManagementAPI.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/SubscriptionManagementAPI.java index 8ced8ef49ed..b6a4878789c 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/SubscriptionManagementAPI.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/SubscriptionManagementAPI.java @@ -20,7 +20,6 @@ package org.wso2.carbon.device.application.mgt.store.api.services; 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.Extension; import io.swagger.annotations.ExtensionProperty; @@ -28,22 +27,14 @@ import io.swagger.annotations.Info; import io.swagger.annotations.SwaggerDefinition; import io.swagger.annotations.Tag; import org.wso2.carbon.apimgt.annotations.api.Scopes; -import org.wso2.carbon.device.application.mgt.common.ErrorResponse; -import org.wso2.carbon.device.application.mgt.common.PaginationResult; -import org.wso2.carbon.device.application.mgt.common.dto.ApplicationDTO; -import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponseTmp; -import org.wso2.carbon.device.application.mgt.common.EnterpriseInstallationDetails; -import org.wso2.carbon.device.application.mgt.common.InstallationDetails; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import javax.validation.Valid; import javax.ws.rs.Consumes; -import javax.ws.rs.GET; import javax.ws.rs.POST; 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.List; @@ -84,16 +75,14 @@ import java.util.List; } ) @Path("/subscription") -@Api(value = "Subscription Management", description = "This API carries all subscription management related " + - "operations " + - "such as install application to device, uninstall application from device, etc.") +@Api(value = "Subscription Management") @Produces(MediaType.APPLICATION_JSON) public interface SubscriptionManagementAPI { String SCOPE = "scope"; @POST - @Path("/install/{uuid}/devices") + @Path("/install/{uuid}/devices/{action}") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) @ApiOperation( @@ -113,13 +102,19 @@ public interface SubscriptionManagementAPI { value = { }) - Response installApplicationForDevices( + Response performAppOperationForDevices( @ApiParam( name = "installationDetails", value = "The application ID and list of devices/users/roles", required = true ) @PathParam("uuid") String uuid, + @ApiParam( + name = "action", + value = "Performing action.", + required = true + ) + @PathParam("action") String action, @ApiParam( name = "installationDetails", value = "The application ID and list of devices/users/roles", @@ -149,7 +144,7 @@ public interface SubscriptionManagementAPI { value = { }) - Response performBulkAppInstallation( + Response performBulkAppOperation( @ApiParam( name = "uuid", value = "The application release UUID", @@ -175,279 +170,4 @@ public interface SubscriptionManagementAPI { ) @Valid List subscribers ); - - @POST - @Path("/install/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install") - }) - } - ) - @ApiResponses( - value = { - - }) - Response installApplicationForUsers( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List users - ); - - @POST - @Path("/install/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install") - }) - } - ) - @ApiResponses( - value = { - @ApiResponse( - code = 200, - message = "OK. \n Successfully add an operation to install application for user devices..", - response = PaginationResult.class, - responseContainer = "PaginationResult"), - @ApiResponse( - code = 400, - message = - "Bad Request. \n Found invalid payload with the request."), - @ApiResponse( - code = 403, - message = "Don't have permission to install application release."), - @ApiResponse( - code = 404, - message = "Not Found. \n Not found an application release for requested UUID."), - @ApiResponse( - code = 500, - message = "Internal Server Error. \n Error occurred while adding operation to install " - + "application for users.", - response = ErrorResponse.class) - - }) - Response installApplicationForRoles ( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List roles - ); - - @POST - @Path("/install/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:install") - }) - } - ) - @ApiResponses( - value = { - - }) - Response installApplicationForGroups ( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List groups - ); - -// ########################### - - - @POST - @Path("/uninstall/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:uninstall") - }) - } - ) - @ApiResponses( - value = { - - }) - Response uninstallApplicationForDevices( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List deviceIdentifiers - ); - - @POST - @Path("/uninstall/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:uninstall") - }) - } - ) - @ApiResponses( - value = { - - }) - Response uninstallApplicationForUsers( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List users - ); - - @POST - @Path("/uninstall/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:uninstall") - }) - } - ) - @ApiResponses( - value = { - - }) - Response uninstallApplicationForRoles ( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List roles - ); - - @POST - @Path("/uninstall/{uuid}/devices") - @Produces(MediaType.APPLICATION_JSON) - @Consumes(MediaType.APPLICATION_JSON) - @ApiOperation( - consumes = MediaType.APPLICATION_JSON, - produces = MediaType.APPLICATION_JSON, - httpMethod = "POST", - value = "Install an application for devices", - notes = "This will install an application to a given list of devices", - tags = "Subscription Management", - extensions = { - @Extension(properties = { - @ExtensionProperty(name = SCOPE, value = "perm:app:subscription:uninstall") - }) - } - ) - @ApiResponses( - value = { - - }) - Response uninstallApplicationForGroups ( - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @PathParam("uuid") String uuid, - @ApiParam( - name = "installationDetails", - value = "The application ID and list of devices/users/roles", - required = true - ) - @Valid List groups - ); } 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 0a50229615b..94d70c08d10 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.store.api.services.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; +import org.wso2.carbon.device.application.mgt.common.SubsciptionType; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; import org.wso2.carbon.device.application.mgt.core.exception.NotFoundException; @@ -48,14 +49,15 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Override @POST - @Path("/install/{uuid}/devices") - public Response installApplicationForDevices( + @Path("/install/{uuid}/devices/{action}") + public Response performAppOperationForDevices( @PathParam("uuid") String uuid, + @PathParam("action") String action, @Valid List deviceIdentifiers) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); ApplicationInstallResponse response = subscriptionManager - .installApplicationForDevices(uuid, deviceIdentifiers); + .performBulkAppInstallation(uuid, deviceIdentifiers, SubsciptionType.DEVICE.toString(), action); return Response.status(Response.Status.OK).entity(response).build(); } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUI: " + uuid; @@ -67,7 +69,8 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ log.error(msg); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ForbiddenException e) { - String msg = "Application release is not in the installable state. Hence you are not permitted to install the aplication."; + String msg = "Application release is not in the installable state. Hence you are not permitted to install " + + "the application."; log.error(msg); return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); } catch (ApplicationManagementException e) { @@ -81,54 +84,15 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ @Override @POST @Path("/install/{uuid}/{subType}/{action}") - public Response performBulkAppInstallation( + public Response performBulkAppOperation( @PathParam("uuid") String uuid, @PathParam("subType") String subType, - @PathParam("action") String sction, + @PathParam("action") String action, @Valid List subscribers) { try { SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - ApplicationInstallResponse response = subscriptionManager.performBulkAppInstallation(uuid, subscribers, subType); - return Response.status(Response.Status.OK).entity(response).build(); - } catch (NotFoundException e) { - String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; - log.error(msg); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } catch (BadRequestException e) { - String msg = "Found invalid payload for installing application which has UUID: " + uuid - + ". Hence verify the payload"; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } catch (ForbiddenException e) { - String msg = "Application release is not in the installable state. Hence you are not permitted to install " - + "the application."; - log.error(msg); - return Response.status(Response.Status.FORBIDDEN).entity(msg).build(); - } catch (ApplicationManagementException e) { - String msg = "Error occurred while installing the application release which has UUID: " + uuid - + " for user devices"; - log.error(msg); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - //todo remove following unwanted APIs - - @Override - @POST - @Path("/install/{uuid}/users") - public Response installApplicationForUsers( - @PathParam("uuid") String uuid, - @Valid List users) { - if (users.isEmpty()) { - String msg = "In order to install application release which has UUID " + uuid + ", you should provide list " - + "of users. But found an empty list of users."; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); - } - try { - SubscriptionManager subscriptionManager = APIUtil.getSubscriptionManager(); - ApplicationInstallResponse response = subscriptionManager.installApplicationForUsers(uuid, users); + ApplicationInstallResponse response = subscriptionManager + .performBulkAppInstallation(uuid, subscribers, subType, action); return Response.status(Response.Status.OK).entity(response).build(); } catch (NotFoundException e) { String msg = "Couldn't found an application release for UUID: " + uuid + ". Hence, verify the payload"; @@ -151,61 +115,4 @@ public class SubscriptionManagementAPIImpl implements SubscriptionManagementAPI{ return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } - - @Override - @POST - @Path("/install/{uuid}/roles") - public Response installApplicationForRoles( - @PathParam("uuid") String uuid, - @Valid List roles) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - } - - @Override - @POST - @Path("/install/{uuid}/groups") - public Response installApplicationForGroups( - @PathParam("uuid") String uuid, - @Valid List groups) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - } - - - @Override - @POST - @Path("/uninstall/{uuid}/devices") - public Response uninstallApplicationForDevices( - @PathParam("uuid") String uuid, - @Valid List deviceIdentifiers) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - } - - @Override - @POST - @Path("/uninstall/{uuid}/users") - public Response uninstallApplicationForUsers( - @PathParam("uuid") String uuid, - @Valid List users) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - } - - @Override - @POST - @Path("/uninstall/{uuid}/roles") - public Response uninstallApplicationForRoles( - @PathParam("uuid") String uuid, - @Valid List roles) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - } - - @Override - @POST - @Path("/uninstall/{uuid}/groups") - public Response uninstallApplicationForGroups( - @PathParam("uuid") String uuid, - @Valid List groups) { - return Response.status(Response.Status.BAD_REQUEST).entity("").build(); - - } - }