From ad2a558d5c8bbe0f0dacf73089e68b95a1605067 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Mon, 11 Nov 2024 18:37:09 +0530 Subject: [PATCH 1/2] Add validation for having pending operations while app installation --- .../core/impl/SubscriptionManagerImpl.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 92f0734c9f..556d19b0e3 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -180,6 +180,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); ApplicationSubscriptionInfo applicationSubscriptionInfo = getAppSubscriptionInfo(applicationDTO, subType, params); + validatePendingAppSubscription(applicationSubscriptionInfo, applicationDTO); performExternalStoreSubscription(applicationDTO, applicationSubscriptionInfo); ApplicationInstallResponse applicationInstallResponse = performActionOnDevices( applicationSubscriptionInfo.getAppSupportingDeviceTypeName(), applicationSubscriptionInfo.getDevices(), @@ -707,6 +708,37 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } + private void validatePendingAppSubscription(ApplicationSubscriptionInfo applicationSubscriptionInfo, ApplicationDTO applicationDTO) throws BadRequestException, ApplicationManagementException{ + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); + try { + List deviceSubscriptionDTOS = subscriptionDAO + .getDeviceSubscriptions(applicationReleaseId, tenantId, null, null); + List deviceIdList = new ArrayList<>(); + deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { + if (!deviceSubscriptionDTO.isUnsubscribed() && + Operation.Status.PENDING.toString().equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) { + deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); + } + }); + + List subscriptionDeviceIds = applicationSubscriptionInfo.getDevices().stream() + .map(Device::getId) + .collect(Collectors.toList()); + boolean allDevicesPending = subscriptionDeviceIds.stream() + .allMatch(deviceIdList::contains); + if (allDevicesPending) { + String msg = "All devices in the subscription have pending operations for this application."; + log.error(msg); + throw new BadRequestException(msg); + } + } catch (ApplicationManagementDAOException e) { + String msg = "Error while retrieving device subscriptions"; + log.error(msg, e); + throw new ApplicationManagementException("Error while validating application subscription", e); + } + } + /** * This method perform given action (i.e APP INSTALL or APP UNINSTALL) on given set of devices. * -- 2.36.3 From 79c3837d8a70075fde34d2aa90509c2cd012d299 Mon Sep 17 00:00:00 2001 From: prathabanKavin Date: Tue, 19 Nov 2024 10:00:54 +0530 Subject: [PATCH 2/2] Fix issues with pending ops validation --- .../core/impl/SubscriptionManagerImpl.java | 35 +++++-------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java index 556d19b0e3..a19dc27658 100644 --- a/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/io.entgra.device.mgt.core.application.mgt.core/src/main/java/io/entgra/device/mgt/core/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -180,7 +180,6 @@ public class SubscriptionManagerImpl implements SubscriptionManager { ApplicationDTO applicationDTO = getApplicationDTO(applicationUUID); ApplicationSubscriptionInfo applicationSubscriptionInfo = getAppSubscriptionInfo(applicationDTO, subType, params); - validatePendingAppSubscription(applicationSubscriptionInfo, applicationDTO); performExternalStoreSubscription(applicationDTO, applicationSubscriptionInfo); ApplicationInstallResponse applicationInstallResponse = performActionOnDevices( applicationSubscriptionInfo.getAppSupportingDeviceTypeName(), applicationSubscriptionInfo.getDevices(), @@ -708,35 +707,16 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } - private void validatePendingAppSubscription(ApplicationSubscriptionInfo applicationSubscriptionInfo, ApplicationDTO applicationDTO) throws BadRequestException, ApplicationManagementException{ - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - int applicationReleaseId = applicationDTO.getApplicationReleaseDTOs().get(0).getId(); - try { - List deviceSubscriptionDTOS = subscriptionDAO - .getDeviceSubscriptions(applicationReleaseId, tenantId, null, null); - List deviceIdList = new ArrayList<>(); - deviceSubscriptionDTOS.forEach(deviceSubscriptionDTO -> { - if (!deviceSubscriptionDTO.isUnsubscribed() && - Operation.Status.PENDING.toString().equalsIgnoreCase(deviceSubscriptionDTO.getStatus())) { - deviceIdList.add(deviceSubscriptionDTO.getDeviceId()); - } - }); - - List subscriptionDeviceIds = applicationSubscriptionInfo.getDevices().stream() - .map(Device::getId) - .collect(Collectors.toList()); - boolean allDevicesPending = subscriptionDeviceIds.stream() - .allMatch(deviceIdList::contains); - if (allDevicesPending) { - String msg = "All devices in the subscription have pending operations for this application."; + private void validatePendingAppSubscription(List devices, SubscribingDeviceIdHolder subscribingDeviceIdHolder) throws BadRequestException{ + Set deviceIdentifiers = devices.stream() + .map(device -> new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())) + .collect(Collectors.toSet()); + Set skippedDevices = subscribingDeviceIdHolder.getSkippedDevices().keySet(); + if (skippedDevices.containsAll(deviceIdentifiers) && deviceIdentifiers.containsAll(skippedDevices)) { + String msg = "All devices in the subscription have pending operations for this application."; log.error(msg); throw new BadRequestException(msg); } - } catch (ApplicationManagementDAOException e) { - String msg = "Error while retrieving device subscriptions"; - log.error(msg, e); - throw new ApplicationManagementException("Error while validating application subscription", e); - } } /** @@ -773,6 +753,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { Map> deviceIdentifierMap = new HashMap<>(); if (SubAction.INSTALL.toString().equalsIgnoreCase(action)) { + validatePendingAppSubscription(devices, subscribingDeviceIdHolder); deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppInstallableDevices().keySet())); deviceIdentifiers.addAll(new ArrayList<>(subscribingDeviceIdHolder.getAppReInstallableDevices().keySet())); if (!isOperationReExecutingDisabled) { -- 2.36.3