|
25 | 25 | import org.wso2.carbon.apimgt.api.APIManagementException;
|
26 | 26 | import org.wso2.carbon.apimgt.api.ExceptionCodes;
|
27 | 27 | import org.wso2.carbon.apimgt.api.model.BlockConditionsDTO;
|
28 |
| -import org.wso2.carbon.apimgt.api.model.policy.AIAPIQuotaLimit; |
29 | 28 | import org.wso2.carbon.apimgt.api.model.policy.Policy;
|
30 |
| -import org.wso2.carbon.apimgt.api.model.policy.QuotaPolicy; |
31 | 29 | import org.wso2.carbon.apimgt.impl.APIAdminImpl;
|
32 | 30 | import org.wso2.carbon.apimgt.impl.APIConstants;
|
33 | 31 | import org.wso2.carbon.apimgt.impl.utils.APIUtil;
|
34 |
| -import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AIAPIQuotaLimitDTO; |
35 | 32 | import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.CustomRuleDTO;
|
36 | 33 | import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleConditionDTO;
|
37 | 34 | import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottleLimitDTO;
|
| 35 | +import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ThrottlePolicyDTO; |
| 36 | +import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.AdvancedThrottlePolicyDTO; |
| 37 | +import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.ApplicationThrottlePolicyDTO; |
| 38 | +import org.wso2.carbon.apimgt.rest.api.admin.v1.dto.SubscriptionThrottlePolicyDTO; |
38 | 39 | import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants;
|
39 | 40 | import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
40 | 41 |
|
|
43 | 44 | import java.io.IOException;
|
44 | 45 | import java.io.InputStream;
|
45 | 46 | import java.util.Arrays;
|
| 47 | +import java.util.ArrayList; |
46 | 48 | import java.util.HashSet;
|
| 49 | +import java.util.List; |
| 50 | +import java.util.Set; |
47 | 51 | import java.util.regex.Matcher;
|
48 | 52 | import java.util.regex.Pattern;
|
49 |
| -import java.util.Set; |
50 | 53 | import java.util.zip.ZipEntry;
|
51 | 54 | import java.util.zip.ZipInputStream;
|
52 | 55 |
|
@@ -142,6 +145,92 @@ public static void validateThrottlePolicyNameProperty(String policyName)
|
142 | 145 | }
|
143 | 146 | }
|
144 | 147 |
|
| 148 | + public static void validateThrottlePolicyDefaultLimitProperty(ThrottlePolicyDTO throttlePolicyDTO) |
| 149 | + throws APIManagementException { |
| 150 | + ThrottleLimitDTO throttleLimitDTO; |
| 151 | + if (throttlePolicyDTO instanceof AdvancedThrottlePolicyDTO) { |
| 152 | + throttleLimitDTO = ((AdvancedThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit(); |
| 153 | + validateRequestCountLimit(throttleLimitDTO); |
| 154 | + validateBandwidthLimit(throttleLimitDTO); |
| 155 | + } else if (throttlePolicyDTO instanceof ApplicationThrottlePolicyDTO) { |
| 156 | + throttleLimitDTO = ((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit(); |
| 157 | + validateRequestCountLimit(throttleLimitDTO); |
| 158 | + validateBandwidthLimit(throttleLimitDTO); |
| 159 | + if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit() != null) { |
| 160 | + if (((ApplicationThrottlePolicyDTO) throttlePolicyDTO).getBurstLimit().getRateLimitCount() < 0) { |
| 161 | + throw new APIManagementException("Burst Control rate limit should be a non-negative value", |
| 162 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 163 | + String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount()))); |
| 164 | + } |
| 165 | + } |
| 166 | + } else if (throttlePolicyDTO instanceof SubscriptionThrottlePolicyDTO) { |
| 167 | + throttleLimitDTO = ((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getDefaultLimit(); |
| 168 | + validateRequestCountLimit(throttleLimitDTO); |
| 169 | + validateBandwidthLimit(throttleLimitDTO); |
| 170 | + validateEventCountLimit(throttleLimitDTO); |
| 171 | + validateAiQuotaLimit(throttleLimitDTO); |
| 172 | + if (((SubscriptionThrottlePolicyDTO) throttlePolicyDTO).getRateLimitCount() < 0) { |
| 173 | + throw new APIManagementException("Rate limit count should be a non-negative value", |
| 174 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 175 | + String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount()))); |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private static void validateRequestCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException { |
| 181 | + if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.REQUESTCOUNTLIMIT)) { |
| 182 | + if (throttleLimitDTO.getRequestCount().getRequestCount() < 0) { |
| 183 | + throw new APIManagementException("Request count should be a non-negative value", |
| 184 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 185 | + String.valueOf(throttleLimitDTO.getRequestCount().getRequestCount()))); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private static void validateBandwidthLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException { |
| 191 | + if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.BANDWIDTHLIMIT)) { |
| 192 | + if (throttleLimitDTO.getBandwidth().getDataAmount() < 0) { |
| 193 | + throw new APIManagementException("Bandwidth should be a non-negative value", |
| 194 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 195 | + String.valueOf(throttleLimitDTO.getBandwidth().getDataAmount()))); |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + private static void validateEventCountLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException { |
| 201 | + if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.EVENTCOUNTLIMIT)) { |
| 202 | + if (throttleLimitDTO.getEventCount().getEventCount() < 0) { |
| 203 | + throw new APIManagementException("Event count should be a non-negative value", |
| 204 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 205 | + String.valueOf(throttleLimitDTO.getEventCount().getEventCount()))); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + private static void validateAiQuotaLimit(ThrottleLimitDTO throttleLimitDTO) throws APIManagementException { |
| 211 | + if (throttleLimitDTO.getType().equals(ThrottleLimitDTO.TypeEnum.AIAPIQUOTALIMIT)) { |
| 212 | + List<String> paramNames = new ArrayList<>(); |
| 213 | + if (throttleLimitDTO.getAiApiQuota().getRequestCount() < 0) { |
| 214 | + paramNames.add("Request Count"); |
| 215 | + } |
| 216 | + if (throttleLimitDTO.getAiApiQuota().getTotalTokenCount() < 0) { |
| 217 | + paramNames.add("Total Token Count"); |
| 218 | + } |
| 219 | + if (throttleLimitDTO.getAiApiQuota().getPromptTokenCount() < 0) { |
| 220 | + paramNames.add("Prompt Token Count"); |
| 221 | + } |
| 222 | + if (throttleLimitDTO.getAiApiQuota().getCompletionTokenCount() < 0) { |
| 223 | + paramNames.add("Complete Token Count"); |
| 224 | + } |
| 225 | + |
| 226 | + if (!paramNames.isEmpty()) { |
| 227 | + throw new APIManagementException("AI quota limit should be a non-negative value", |
| 228 | + ExceptionCodes.from(ExceptionCodes.INVALID_QUOTA_LIMIT, |
| 229 | + String.join(",", paramNames))); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | + |
145 | 234 | public static void validateIPAddress(String ipAddress) throws APIManagementException {
|
146 | 235 | String ip4 = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}" +
|
147 | 236 | "([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
|
|
0 commit comments