Skip to content

Commit

Permalink
Fix import subscription flow
Browse files Browse the repository at this point in the history
  • Loading branch information
chashikajw committed Jun 12, 2024
1 parent 08a6a87 commit 13a072e
Showing 1 changed file with 75 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class ImportUtils {
private static final Log log = LogFactory.getLog(ImportUtils.class);
Expand Down Expand Up @@ -152,28 +154,90 @@ public static APIKey getAPIKeyFromApplicationKeyDTO(ApplicationKeyDTO applicatio
* @throws UserStoreException if an error occurs while checking whether the tenant domain exists
*/
public static List<APIIdentifier> importSubscriptions(Set<ExportedSubscribedAPI> subscribedAPIs, String userId,
Application application, Boolean update, APIConsumer apiConsumer, String organization)
Application application, Boolean update,
APIConsumer apiConsumer, String organization)
throws APIManagementException,
UserStoreException {
List<APIIdentifier> skippedAPIList = new ArrayList<>();
// removing existing subscribed apis

// Create a map of the imported subscriptions. The key is a combination of apiName and version
Map<String, ExportedSubscribedAPI> importedSubscriptionMap = subscribedAPIs.stream()
.collect(Collectors.toMap(
api -> api.getApiId().getApiName() + "_" + api.getApiId().getVersion(),
api -> api));

// If update flag is set, remove the existing subscriptions that are not in the imported list
if (update) {
Subscriber subscriber = apiConsumer.getSubscriber(userId);
Set<SubscribedAPI> currentSubscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber, application.getName(),
application.getGroupId());
for (SubscribedAPI subscribedAPI : currentSubscribedAPIs) {
apiConsumer.removeSubscription(subscribedAPI.getAPIIdentifier(), userId, application.getId(),
application.getGroupId(), application.getOrganization());
Set<SubscribedAPI> currentSubscribedAPIs = apiConsumer.getSubscribedAPIs(subscriber,
application.getName(), application.getGroupId());

for (SubscribedAPI existingSubscribedAPI : currentSubscribedAPIs) {
String existingSubscriptionKey = existingSubscribedAPI.getIdentifier().getName() + "_" +
existingSubscribedAPI.getIdentifier().getVersion();
APIIdentifier existingApi = existingSubscribedAPI.getAPIIdentifier();
if (importedSubscriptionMap.containsKey(existingSubscriptionKey)) {

// Update the tier if the existing tier is different from the imported tier
if (!existingSubscribedAPI.getTier().getName().equals(
importedSubscriptionMap.get(existingSubscriptionKey).getThrottlingPolicy())) {


String tenantDomain = MultitenantUtils
.getTenantDomain(APIUtil.replaceEmailDomainBack(existingApi.getProviderName()));
if (!StringUtils.isEmpty(tenantDomain) && APIUtil.isTenantAvailable(tenantDomain)) {
String uuidFromIdentifier = ApiMgtDAO.getInstance().getUUIDFromIdentifier(existingApi,
tenantDomain);
if (StringUtils.isNotEmpty(uuidFromIdentifier)) {
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(
uuidFromIdentifier, tenantDomain);
// Tier of the imported subscription
String targetTier = importedSubscriptionMap.get(existingSubscriptionKey).
getThrottlingPolicy();
// Checking whether the target tier is available
if (isTierAvailable(targetTier, apiTypeWrapper) && apiTypeWrapper.getStatus() != null
&& APIConstants.PUBLISHED.equals(apiTypeWrapper.getStatus())) {

apiConsumer.updateSubscription(apiTypeWrapper, userId, application,
existingSubscribedAPI.getUUID(), existingSubscribedAPI.getTier().getName(),
targetTier);
} else {
log.error("Failed to import Subscription as API/API Product "
+ existingApi.getName() + "-" + existingApi.getVersion() +
" as one or more tiers may be unavailable or the API/API Product may " +
"not have been published ");
}
} else {
log.error("Failed to import Subscription as API " + existingApi.getName() + "-" +
existingApi.getVersion() + " is not available");
}
} else {
log.error("Failed to import Subscription as Tenant domain: " + tenantDomain +
" is not available");
}
}

// Remove the existing subscription from the imported list
importedSubscriptionMap.remove(existingSubscriptionKey);
} else {
// Remove the subscription if it is not in the imported list
apiConsumer.removeSubscription(existingApi, userId, application.getId(), application.getGroupId(),
organization);

}
}
}
for (ExportedSubscribedAPI subscribedAPI : subscribedAPIs) {

for (String importedSubscriptionKey : importedSubscriptionMap.keySet()) {
ExportedSubscribedAPI subscribedAPI = importedSubscriptionMap.get(importedSubscriptionKey);
APIIdentifier apiIdentifier = subscribedAPI.getApiId();
String tenantDomain = MultitenantUtils
.getTenantDomain(APIUtil.replaceEmailDomainBack(apiIdentifier.getProviderName()));
if (!StringUtils.isEmpty(tenantDomain) && APIUtil.isTenantAvailable(tenantDomain)) {
String uuidFromIdentifier = ApiMgtDAO.getInstance().getUUIDFromIdentifier(apiIdentifier, tenantDomain);
if (StringUtils.isNotEmpty(uuidFromIdentifier)) {
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(uuidFromIdentifier, tenantDomain);
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(uuidFromIdentifier,
tenantDomain);
// Tier of the imported subscription
String targetTier = subscribedAPI.getThrottlingPolicy();
// Checking whether the target tier is available
Expand All @@ -191,7 +255,8 @@ public static List<APIIdentifier> importSubscriptions(Set<ExportedSubscribedAPI>
}
} else {
log.error("Failed to import Subscription as API/API Product "
+ apiIdentifier.getName() + "-" + apiIdentifier.getVersion() + " as one or more tiers may "
+ apiIdentifier.getName() + "-" + apiIdentifier.getVersion() + " as one or more " +
"tiers may "
+ "be unavailable or the API/API Product may not have been published ");
skippedAPIList.add(subscribedAPI.getApiId());
}
Expand Down

0 comments on commit 13a072e

Please sign in to comment.