|
153 | 153 | import java.util.regex.Matcher;
|
154 | 154 | import java.util.regex.Pattern;
|
155 | 155 |
|
| 156 | +import static org.wso2.carbon.apimgt.api.model.policy.PolicyConstants.AI_API_QUOTA_TYPE; |
| 157 | +import static org.wso2.carbon.apimgt.api.model.policy.PolicyConstants.EVENT_COUNT_TYPE; |
| 158 | + |
156 | 159 | import static org.wso2.carbon.apimgt.impl.APIConstants.GOVERNANCE_COMPLIANCE_ERROR_MESSAGE;
|
157 | 160 | import static org.wso2.carbon.apimgt.impl.APIConstants.GOVERNANCE_COMPLIANCE_KEY;
|
158 | 161 | import static org.wso2.carbon.apimgt.impl.APIConstants.PUBLISH;
|
@@ -364,6 +367,7 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
|
364 | 367 | || APIConstants.APITransportType.WEBSUB.toString().equals(originalAPI.getType())
|
365 | 368 | || APIConstants.APITransportType.SSE.toString().equals(originalAPI.getType())
|
366 | 369 | || APIConstants.APITransportType.ASYNC.toString().equals(originalAPI.getType()));
|
| 370 | + boolean isAIAPI = APIConstants.API_SUBTYPE_AI_API.equals(originalAPI.getSubtype()); |
367 | 371 |
|
368 | 372 | Scope[] apiDtoClassAnnotatedScopes = APIDTO.class.getAnnotationsByType(Scope.class);
|
369 | 373 | boolean hasClassLevelScope = checkClassScopeAnnotation(apiDtoClassAnnotatedScopes, tokenScopes);
|
@@ -525,9 +529,20 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
|
525 | 529 | if (!APIUtil.isSubscriptionValidationDisablingAllowed(tenantDomain)) {
|
526 | 530 | if (apiSecurity != null && (apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) || apiSecurity
|
527 | 531 | .contains(APIConstants.API_SECURITY_API_KEY)) && condition) {
|
528 |
| - throw new APIManagementException( |
529 |
| - "A tier should be defined if the API is not in CREATED or PROTOTYPED state", |
530 |
| - ExceptionCodes.TIER_CANNOT_BE_NULL); |
| 532 | + Set<Tier> availableThrottlingPolicyList = apiProvider.getTiers(); |
| 533 | + tiersFromDTO = availableThrottlingPolicyList.stream() |
| 534 | + .filter(tier -> isApplicableTier(tier, isAsyncAPI, isAIAPI)) |
| 535 | + .map(Tier::getName) |
| 536 | + .findFirst() |
| 537 | + .map(Collections::singletonList) |
| 538 | + .orElse(Collections.emptyList()); |
| 539 | + apiDtoToUpdate.setPolicies(tiersFromDTO); |
| 540 | + |
| 541 | + if (tiersFromDTO.isEmpty()) { |
| 542 | + throw new APIManagementException( |
| 543 | + "A tier should be defined if the API is not in CREATED or PROTOTYPED state", |
| 544 | + ExceptionCodes.TIER_CANNOT_BE_NULL); |
| 545 | + } |
531 | 546 | }
|
532 | 547 | } else {
|
533 | 548 | if (apiSecurity != null) {
|
@@ -722,6 +737,54 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
|
722 | 737 | return apiToUpdate;
|
723 | 738 | }
|
724 | 739 |
|
| 740 | + private static boolean isApplicableTier(Tier tier, boolean isAsyncAPI, boolean isAIAPI) { |
| 741 | + if (isAsyncAPI) { |
| 742 | + return isAsyncAPITier(tier); |
| 743 | + } |
| 744 | + |
| 745 | + if (isAIAPI) { |
| 746 | + return isAIAPITier(tier); |
| 747 | + } |
| 748 | + |
| 749 | + return isRegularAPITier(tier); |
| 750 | + } |
| 751 | + |
| 752 | + /** |
| 753 | + * Checks if the given tier is an Async API tier. |
| 754 | + * |
| 755 | + * @param tier The tier to evaluate. |
| 756 | + * @return {@code true} if the tier is of type EVENT_COUNT_TYPE, otherwise {@code false}. |
| 757 | + */ |
| 758 | + private static boolean isAsyncAPITier(Tier tier) { |
| 759 | + return EVENT_COUNT_TYPE.equals(tier.getQuotaPolicyType()); |
| 760 | + } |
| 761 | + |
| 762 | + /** |
| 763 | + * Checks if the given tier is an AI API tier. |
| 764 | + * |
| 765 | + * @param tier The tier to evaluate. |
| 766 | + * @return {@code true} if the tier is of type AI_API_QUOTA_TYPE, |
| 767 | + * contains the default subscription-less policy name, |
| 768 | + * or has a null quota policy type. Otherwise, returns {@code false}. |
| 769 | + */ |
| 770 | + private static boolean isAIAPITier(Tier tier) { |
| 771 | + return AI_API_QUOTA_TYPE.equals(tier.getQuotaPolicyType()) || |
| 772 | + tier.getName().contains(APIConstants.DEFAULT_SUB_POLICY_SUBSCRIPTIONLESS) || |
| 773 | + tier.getQuotaPolicyType() == null; |
| 774 | + } |
| 775 | + |
| 776 | + /** |
| 777 | + * Checks if the given tier is a regular API tier. |
| 778 | + * |
| 779 | + * @param tier The tier to evaluate. |
| 780 | + * @return {@code true} if the tier is neither an AI API tier nor an Async API tier, |
| 781 | + * otherwise {@code false}. |
| 782 | + */ |
| 783 | + private static boolean isRegularAPITier(Tier tier) { |
| 784 | + return !AI_API_QUOTA_TYPE.equals(tier.getQuotaPolicyType()) && |
| 785 | + !EVENT_COUNT_TYPE.equals(tier.getQuotaPolicyType()); |
| 786 | + } |
| 787 | + |
725 | 788 | /**
|
726 | 789 | * This method will encrypt the Api Key
|
727 | 790 | *
|
|
0 commit comments