diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConstants.java index a4a0885a2fed..1b1ec51f2544 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIConstants.java @@ -48,6 +48,9 @@ public class APIConstants { public static final String ENDPOINT_CONFIG_SESSION_TIMEOUT = "sessionTimeOut"; public static class AIAPIConstants { + public static final String API_KEY_IDENTIFIER_TYPE = "API_KEY_IDENTIFIER_TYPE"; + public static final String API_KEY_IDENTIFIER_TYPE_HEADER = "HEADER"; + public static final String API_KEY_IDENTIFIER_TYPE_QUERY_PARAMETER = "QUERY_PARAMETER"; public static final String AI_API_REQUEST_METADATA = "AI_API_REQUEST_METADATA"; public static final String AI_API_RESPONSE_METADATA = "AI_API_RESPONSE_METADATA"; public static final String INPUT_SOURCE_PAYLOAD = "payload"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBaseThrottlingCountHolder.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBasedThrottlingCountHolder.java similarity index 92% rename from components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBaseThrottlingCountHolder.java rename to components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBasedThrottlingCountHolder.java index fb8ce308ed1c..727aa319daa7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBaseThrottlingCountHolder.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/TokenBasedThrottlingCountHolder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved. * * WSO2 LLC. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except @@ -18,7 +18,7 @@ package org.wso2.carbon.apimgt.api; -public class TokenBaseThrottlingCountHolder { +public class TokenBasedThrottlingCountHolder { private String productionMaxPromptTokenCount; private String productionMaxCompletionTokenCount; @@ -28,11 +28,11 @@ public class TokenBaseThrottlingCountHolder { private String sandboxMaxTotalTokenCount; private Boolean isTokenBasedThrottlingEnabled = false; - public TokenBaseThrottlingCountHolder() { + public TokenBasedThrottlingCountHolder() { } - public TokenBaseThrottlingCountHolder(String productionMaxPromptTokenCount, String productionMaxCompletionTokenCount, + public TokenBasedThrottlingCountHolder(String productionMaxPromptTokenCount, String productionMaxCompletionTokenCount, String productionMaxTotalTokenCount, String sandboxMaxPromptTokenCount, String sandboxMaxCompletionTokenCount, String sandboxMaxTotalTokenCount, boolean isTokenBasedThrottlingEnabled) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIConfiguration.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIConfiguration.java index c6f1a533f469..59ed55a92723 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIConfiguration.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIConfiguration.java @@ -18,34 +18,10 @@ package org.wso2.carbon.apimgt.api.model; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; - public class AIConfiguration { private String llmProviderId; - private AIEndpointConfiguration aiEndpointConfiguration; private String llmProviderName; private String llmProviderApiVersion; - private TokenBaseThrottlingCountHolder tokenBasedThrottlingConfiguration; - - public AIEndpointConfiguration getAiEndpointConfiguration() { - - return aiEndpointConfiguration; - } - - public void setAiEndpointConfiguration(AIEndpointConfiguration aiEndpointConfiguration) { - - this.aiEndpointConfiguration = aiEndpointConfiguration; - } - - public TokenBaseThrottlingCountHolder getTokenBasedThrottlingConfiguration() { - - return tokenBasedThrottlingConfiguration; - } - - public void setTokenBasedThrottlingConfiguration(TokenBaseThrottlingCountHolder tokenBasedThrottlingConfiguration) { - - this.tokenBasedThrottlingConfiguration = tokenBasedThrottlingConfiguration; - } public String getLlmProviderName() { diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIEndpointConfiguration.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIEndpointConfiguration.java deleted file mode 100644 index 73cfc36b4e2e..000000000000 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/AIEndpointConfiguration.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.apimgt.api.model; - -public class AIEndpointConfiguration { - - private String sandboxAuthValue; - private String productionAuthValue; - - public String getSandboxAuthValue() { - - return sandboxAuthValue; - } - - public void setSandboxAuthValue(String sandboxAuthValue) { - - this.sandboxAuthValue = sandboxAuthValue; - } - - public String getProductionAuthValue() { - - return productionAuthValue; - } - - public void setProductionAuthValue(String productionAuthValue) { - - this.productionAuthValue = productionAuthValue; - } -} diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java index a65e8e9aff6e..a7d852eff7e1 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/API.java @@ -236,6 +236,8 @@ public void setSoapToRestSequences(List soapToRestSequences) private AIConfiguration aiConfiguration; + private BackendThrottlingConfiguration backendThrottlingConfiguration; + public AIConfiguration getAiConfiguration() { return aiConfiguration; @@ -246,6 +248,14 @@ public void setAiConfiguration(AIConfiguration AiConfiguration) { this.aiConfiguration = AiConfiguration; } + public BackendThrottlingConfiguration getBackendThrottlingConfiguration() { + return backendThrottlingConfiguration; + } + + public void setBackendThrottlingConfiguration(BackendThrottlingConfiguration backendThrottlingConfiguration) { + this.backendThrottlingConfiguration = backendThrottlingConfiguration; + } + public String getAudience() { return audience; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/BackendThrottlingConfiguration.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/BackendThrottlingConfiguration.java new file mode 100644 index 000000000000..fd82d5b5bb5e --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/BackendThrottlingConfiguration.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (http://www.wso2.com) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.wso2.carbon.apimgt.api.model; + +import org.wso2.carbon.apimgt.api.TokenBasedThrottlingCountHolder; + +public class BackendThrottlingConfiguration { + + private String productionMaxTps; + private String productionTimeUnit = "1000"; + private String sandboxMaxTps; + private String sandboxTimeUnit = "1000"; + private TokenBasedThrottlingCountHolder tokenBasedThrottlingConfiguration; + + public String getProductionMaxTps() { + + return productionMaxTps; + } + + public void setProductionMaxTps(String productionMaxTps) { + + this.productionMaxTps = productionMaxTps; + } + + public String getProductionTimeUnit() { + return productionTimeUnit; + } + + public void setProductionTimeUnit(String productionTimeUnit) { + + this.productionTimeUnit = productionTimeUnit; + } + + public String getSandboxMaxTps() { + + return sandboxMaxTps; + } + + public void setSandboxMaxTps(String sandboxMaxTps) { + + this.sandboxMaxTps = sandboxMaxTps; + } + + public String getSandboxTimeUnit() { + + return sandboxTimeUnit; + } + + public void setSandboxTimeUnit(String sandboxTimeUnit) { + + this.sandboxTimeUnit = sandboxTimeUnit; + } + + public TokenBasedThrottlingCountHolder getTokenBasedThrottlingConfiguration() { + + return tokenBasedThrottlingConfiguration; + } + + public void setTokenBasedThrottlingConfiguration(TokenBasedThrottlingCountHolder tokenBasedThrottlingConfiguration) { + + this.tokenBasedThrottlingConfiguration = tokenBasedThrottlingConfiguration; + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/EndpointSecurity.java b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/EndpointSecurity.java index 77dd65de39d1..3310180d7a3d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/EndpointSecurity.java +++ b/components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/EndpointSecurity.java @@ -44,6 +44,8 @@ public class EndpointSecurity { private String apiKeyValue = null; + private String apiKeyIdentifierType = null; + private String customParameters = null; private Map additionalProperties = new HashMap(); @@ -205,6 +207,16 @@ public void setApiKeyValue(String apiKeyValue) { this.apiKeyValue = apiKeyValue; } + public String getApiKeyIdentifierType() { + + return apiKeyIdentifierType; + } + + public void setApiKeyIdentifierType(String apiKeyIdentifierType) { + + this.apiKeyIdentifierType = apiKeyIdentifierType; + } + public int getConnectionTimeoutDuration() { return connectionTimeoutDuration; } @@ -244,6 +256,7 @@ public String toString() { ", clientSecret='" + clientSecret + '\'' + ", apiKeyIdentifier='" + apiKeyIdentifier + '\'' + ", apiKeyValue='" + apiKeyValue + '\'' + + ", apiKeyIdentifierType='" + apiKeyIdentifierType + '\'' + ", customParameters='" + customParameters + '\'' + ", additionalProperties=" + additionalProperties + ", connectionTimeoutDuration=" + connectionTimeoutDuration + diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/AiApiHandler.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/AiApiHandler.java index d2dba05ca068..ad16bf8d82b9 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/AiApiHandler.java +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/AiApiHandler.java @@ -19,7 +19,6 @@ import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.http.client.utils.URIBuilder; import org.apache.synapse.MessageContext; import org.apache.synapse.api.ApiUtils; import org.apache.synapse.core.axis2.Axis2MessageContext; @@ -28,14 +27,11 @@ import org.apache.synapse.transport.passthru.util.RelayUtils; import org.json.XML; import org.wso2.carbon.apimgt.api.model.AIConfiguration; -import org.wso2.carbon.apimgt.api.model.AIEndpointConfiguration; import org.wso2.carbon.apimgt.api.APIConstants; import org.wso2.carbon.apimgt.api.APIManagementException; import org.wso2.carbon.apimgt.api.LLMProviderService; import org.wso2.carbon.apimgt.api.model.LLMProvider; import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; -import org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityUtils; -import org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext; import org.wso2.carbon.apimgt.gateway.internal.DataHolder; import org.wso2.carbon.apimgt.gateway.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.api.LLMProviderConfiguration; @@ -48,7 +44,6 @@ import javax.ws.rs.core.MediaType; import javax.xml.stream.XMLStreamException; import java.io.IOException; -import java.net.URI; import java.net.URISyntaxException; import java.util.HashMap; import java.util.Map; @@ -133,8 +128,11 @@ private boolean processMessage(MessageContext messageContext, boolean isRequest) LLMProviderConfiguration providerConfiguration = new Gson().fromJson(config, LLMProviderConfiguration.class); if (isRequest) { - addEndpointConfigurationToMessageContext(messageContext, aiConfiguration.getAiEndpointConfiguration(), - providerConfiguration); + Map transportHeaders = + (Map) ((Axis2MessageContext) messageContext) + .getAxis2MessageContext() + .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); + transportHeaders.remove(HttpHeaders.ACCEPT_ENCODING); } LLMProviderService llmProviderService = ServiceReferenceHolder.getInstance() .getLLMProviderService(providerConfiguration.getConnectorType()); @@ -176,45 +174,6 @@ private boolean processMessage(MessageContext messageContext, boolean isRequest) } } - /** - * Adds endpoint configuration to the message context. - * - * @param messageContext the Synapse MessageContext - * @param aiEndpointConfiguration AI endpoint configuration - * @param providerConfiguration LLM provider configuration - */ - private void addEndpointConfigurationToMessageContext(MessageContext messageContext, - AIEndpointConfiguration aiEndpointConfiguration, - LLMProviderConfiguration providerConfiguration) - throws CryptoException, URISyntaxException { - - if (aiEndpointConfiguration != null) { - org.apache.axis2.context.MessageContext axCtx = - ((Axis2MessageContext) messageContext).getAxis2MessageContext(); - AuthenticationContext authContext = - (AuthenticationContext) messageContext.getProperty(APISecurityUtils.API_AUTH_CONTEXT); - String authValue = authContext.getKeyType().equals(org.wso2.carbon.apimgt.impl. - APIConstants.API_KEY_TYPE_PRODUCTION) - ? aiEndpointConfiguration.getProductionAuthValue() - : aiEndpointConfiguration.getSandboxAuthValue(); - if (providerConfiguration.getAuthHeader() != null) { - Map transportHeaders = - (Map) axCtx.getProperty(org.apache.axis2.context.MessageContext - .TRANSPORT_HEADERS); - transportHeaders.put(providerConfiguration.getAuthHeader(), decryptSecret(authValue)); - - // TODO: Handle encoded scenario - transportHeaders.remove(HttpHeaders.ACCEPT_ENCODING); - } else if (providerConfiguration.getAuthQueryParameter() != null) { - URI updatedFullPath = - new URIBuilder((String) axCtx.getProperty(APIMgtGatewayConstants.REST_URL_POSTFIX)) - .addParameter(providerConfiguration.getAuthQueryParameter(), - decryptSecret(authValue)).build(); - axCtx.setProperty(APIMgtGatewayConstants.REST_URL_POSTFIX, updatedFullPath.toString()); - } - } - } - /** * Decrypts the secret value. * diff --git a/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/ApiKeyMediator.java b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/ApiKeyMediator.java new file mode 100644 index 000000000000..02a6913e03b6 --- /dev/null +++ b/components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/mediators/ApiKeyMediator.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2024 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the License. + */ + +package org.wso2.carbon.apimgt.gateway.mediators; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.client.utils.URIBuilder; +import org.apache.synapse.ManagedLifecycle; +import org.apache.synapse.MessageContext; +import org.apache.synapse.core.SynapseEnvironment; +import org.apache.synapse.core.axis2.Axis2MessageContext; +import org.apache.synapse.mediators.AbstractMediator; +import org.wso2.carbon.apimgt.api.APIConstants; +import org.wso2.carbon.apimgt.gateway.APIMgtGatewayConstants; +import org.wso2.carbon.apimgt.gateway.mediators.oauth.OAuthMediator; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; + +public class ApiKeyMediator extends AbstractMediator implements ManagedLifecycle { + + private static final Log log = LogFactory.getLog(OAuthMediator.class); + private String apiKeyIdentifier; + private String apiKeyValue; + private String apiKeyIdentifierType; + + @Override + public void init(SynapseEnvironment synapseEnvironment) { + + log.debug("ApiKey Mediator init"); + } + + @Override + public void destroy() { + + } + + @Override + public boolean mediate(MessageContext messageContext) { + + if (log.isDebugEnabled()) { + log.debug("ApiKey Mediator is invoked..."); + } + if (apiKeyIdentifier != null && apiKeyValue != null) { + try { + org.apache.axis2.context.MessageContext axCtx = ((Axis2MessageContext) messageContext) + .getAxis2MessageContext(); + if (APIConstants.AIAPIConstants.API_KEY_IDENTIFIER_TYPE_HEADER + .equals(apiKeyIdentifierType)) { + Map transportHeaders = + (Map) ((Axis2MessageContext) messageContext) + .getAxis2MessageContext() + .getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS); + transportHeaders.put(apiKeyIdentifier, apiKeyValue); + } else if (APIConstants.AIAPIConstants.API_KEY_IDENTIFIER_TYPE_QUERY_PARAMETER + .equals(apiKeyIdentifierType)) { + URI updatedFullPath = + new URIBuilder((String) axCtx.getProperty(APIMgtGatewayConstants.REST_URL_POSTFIX)) + .addParameter(apiKeyIdentifier, apiKeyValue).build(); + axCtx.setProperty(APIMgtGatewayConstants.REST_URL_POSTFIX, updatedFullPath.toString()); + } + } catch (URISyntaxException e) { + log.error("Error occurred during parsing query parameters for AI API."); + } + } + return true; + } + + @Override + public boolean isContentAware() { + + return false; + } + + public String getApiKeyIdentifier() { + + return apiKeyIdentifier; + } + + public void setApiKeyIdentifier(String apiKeyIdentifier) { + + this.apiKeyIdentifier = apiKeyIdentifier; + } + + public String getApiKeyValue() { + + return apiKeyValue; + } + + public void setApiKeyValue(String apiKeyValue) { + + this.apiKeyValue = apiKeyValue; + } + + public String getApiKeyIdentifierType() { + + return apiKeyIdentifierType; + } + + public void setApiKeyIdentifierType(String apiKeyIdentifierType) { + + this.apiKeyIdentifierType = apiKeyIdentifierType; + } +} diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 33872b763c0d..cd8038972397 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -1808,6 +1808,7 @@ private ConfigParameters() { public static final String ENDPOINT_SECURITY_TYPE_API_KEY = "apikey"; public static final String ENDPOINT_SECURITY_API_KEY_IDENTIFIER = "apiKeyIdentifier"; public static final String ENDPOINT_SECURITY_API_KEY_VALUE = "apiKeyValue"; + public static final String ENDPOINT_SECURITY_API_KEY_IDENTIFIER_TYPE = "apiKeyIdentifierType"; public static final String ENDPOINT_SECURITY_USERNAME = "username"; public static final String ENDPOINT_SECURITY_CONFIG = "securityConfig"; public static final String ENDPOINT_SECURITY = "endpoint_security"; @@ -1963,6 +1964,10 @@ public enum RegistryResourceTypesForUI { public static final String DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE = "AsyncWHBronze"; public static final String DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED = "AsyncWHUnlimited"; + public static final String DEFAULT_SUB_POLICY_AI_GOLD = "AIGold"; + public static final String DEFAULT_SUB_POLICY_AI_SILVER = "AISilver"; + public static final String DEFAULT_SUB_POLICY_AI_BRONZE = "AIBronze"; + public static final String DEFAULT_SUB_POLICY_GOLD_DESC = "Allows 5000 requests per minute"; public static final String DEFAULT_SUB_POLICY_SILVER_DESC = "Allows 2000 requests per minute"; public static final String DEFAULT_SUB_POLICY_BRONZE_DESC = "Allows 1000 requests per minute"; @@ -1987,6 +1992,10 @@ public enum RegistryResourceTypesForUI { public static final String DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED_DESC = "Allows unlimited events and " + "unlimited active subscriptions"; + public static final String DEFAULT_SUB_POLICY_AI_GOLD_DESC = "Allows 50000 total tokens and 500 requests per minute"; + public static final String DEFAULT_SUB_POLICY_AI_SILVER_DESC = "Allows 10000 total tokens and 100 requests per minute"; + public static final String DEFAULT_SUB_POLICY_AI_BRONZE_DESC = "Allows 1000 total tokens and 10 requests per minute"; + public static final String DEFAULT_API_POLICY_FIFTY_THOUSAND_REQ_PER_MIN = "50KPerMin"; public static final String DEFAULT_API_POLICY_TWENTY_THOUSAND_REQ_PER_MIN = "20KPerMin"; public static final String DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN = "10KPerMin"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java index a7eddeba2522..fd1ac768d378 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java @@ -54,7 +54,6 @@ import org.wso2.carbon.apimgt.api.dto.ClonePolicyMetadataDTO; import org.wso2.carbon.apimgt.api.dto.EnvironmentPropertiesDTO; import org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage; -import org.wso2.carbon.apimgt.api.model.AIEndpointConfiguration; import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.api.model.APIDefinitionContentSearchResult; import org.wso2.carbon.apimgt.api.model.APIIdentifier; @@ -649,115 +648,10 @@ private void addAIConfiguration(API api) throws APIManagementException { AIConfiguration aiConfig = api.getAiConfiguration(); if (aiConfig != null) { - try { - processEndpointConfig(api, aiConfig); - apiMgtDAO.addAIConfiguration(api.getUuid(), null, aiConfig, api.getOrganization()); - } catch (ParseException e) { - throw new APIManagementException("Error parsing endpoint configuration", e); - } - } - } - - /** - * Updates AI Configuration of the given API. - * - * @param api API object to update the AI configuration for - * @throws APIManagementException if an error occurs during the process - */ - private void updateAIConfiguration(API api) throws APIManagementException { - - AIConfiguration aiConfig = api.getAiConfiguration(); - if (aiConfig != null) { - try { - processEndpointConfig(api, aiConfig); - apiMgtDAO.updateAIConfiguration(api.getUuid(), aiConfig); - } catch (ParseException e) { - throw new APIManagementException("Error parsing endpoint configuration", e); - } + apiMgtDAO.addAIConfiguration(api.getUuid(), null, aiConfig, api.getOrganization()); } } - /** - * Processes the endpoint security configuration for both add and update operations. - * - * @param api API object - * @param aiConfig AI Configuration to update or add - * @throws ParseException if parsing the endpoint configuration fails - * @throws APIManagementException if an error occurs during the process - */ - private void processEndpointConfig(API api, AIConfiguration aiConfig) throws ParseException, - APIManagementException { - - String endpointConfig = api.getEndpointConfig(); - if (StringUtils.isNotEmpty(endpointConfig)) { - JSONObject endpointConfigJson = parseEndpointConfig(endpointConfig); - if (endpointConfigJson.containsKey(APIConstants.ENDPOINT_SECURITY)) { - updateEndpointSecurityConfig(api, aiConfig, - (JSONObject) endpointConfigJson.get(APIConstants.ENDPOINT_SECURITY)); - } - } - } - - /** - * Updates the endpoint security configuration for production and sandbox environments. - * - * @param api API object - * @param aiConfig AI Configuration to update - * @param endpointSecurityJson JSON object containing endpoint security details - * @throws APIManagementException if an error occurs during the process - */ - private void updateEndpointSecurityConfig(API api, AIConfiguration aiConfig, JSONObject endpointSecurityJson) - throws APIManagementException { - - AIEndpointConfiguration aiEndpointConfig = aiConfig.getAiEndpointConfiguration(); - if (aiEndpointConfig == null) { - aiEndpointConfig = new AIEndpointConfiguration(); - } - if (endpointSecurityJson.containsKey(APIConstants.ENDPOINT_SECURITY_PRODUCTION)) { - processEndpointSecurity(api, aiEndpointConfig, - (JSONObject) endpointSecurityJson.get(APIConstants.ENDPOINT_SECURITY_PRODUCTION), true); - } - if (endpointSecurityJson.containsKey(APIConstants.ENDPOINT_SECURITY_SANDBOX)) { - processEndpointSecurity(api, aiEndpointConfig, - (JSONObject) endpointSecurityJson.get(APIConstants.ENDPOINT_SECURITY_SANDBOX), false); - } - aiConfig.setAiEndpointConfiguration(aiEndpointConfig); - } - - /** - * Processes the endpoint security configuration for a specific environment (production or sandbox). - * - * @param api API object - * @param aiEndpointConfig AI endpoint configuration to update or add - * @param securityJson JSON object containing security details - * @param isProduction flag indicating if it's for production - * @throws APIManagementException if an error occurs during encryption or invalid config - */ - private void processEndpointSecurity(API api, AIEndpointConfiguration aiEndpointConfig, JSONObject securityJson, - boolean isProduction) throws APIManagementException { - - EndpointSecurity endpointSecurity = new ObjectMapper().convertValue(securityJson, EndpointSecurity.class); - if (APIConstants.ENDPOINT_SECURITY_TYPE_API_KEY.equals(endpointSecurity.getType())) { - if (isProduction) { - aiEndpointConfig.setProductionAuthValue(endpointSecurity.getApiKeyValue()); - } else { - aiEndpointConfig.setSandboxAuthValue(endpointSecurity.getApiKeyValue()); - } - } - } - - /** - * Parses the endpoint configuration string to a JSON object. - * - * @param endpointConfig the endpoint configuration as a string - * @return parsed JSON object - * @throws ParseException if parsing fails - */ - private JSONObject parseEndpointConfig(String endpointConfig) throws ParseException { - - return (JSONObject) new JSONParser().parse(endpointConfig); - } - /** * Add local scopes for the API if the scopes does not exist as shared scopes. The local scopes to add will be * take from the URI templates. @@ -1097,7 +991,6 @@ public API updateAPI(API api, API existingAPI) throws APIManagementException { updateProductResourceMappings(api, organization, productResources); updateAPIPolicies(api, tenantDomain); - updateAIConfiguration(api); if (log.isDebugEnabled()) { log.debug("Successfully updated the API: " + api.getId() + " in the database"); @@ -6653,10 +6546,6 @@ public void restoreAPIRevision(String apiId, String apiRevisionId, String organi ERROR_RESTORING_API_REVISION,apiRevision.getApiUUID())); } apiMgtDAO.restoreAPIRevision(apiRevision); - AIConfiguration configuration = apiMgtDAO.getAIConfiguration(apiRevision.getApiUUID(), apiRevision.getRevisionUUID(), organization); - if (configuration != null) { - apiMgtDAO.updateAIConfiguration(apiRevision.getApiUUID(), configuration); - } } /** diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java index 5f54c8d1be47..afd1cdf13b6d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java @@ -33,7 +33,6 @@ import org.wso2.carbon.apimgt.api.ExceptionCodes; import org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException; import org.wso2.carbon.apimgt.api.SubscriptionBlockedException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; import org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO; import org.wso2.carbon.apimgt.api.dto.ClonePolicyMetadataDTO; import org.wso2.carbon.apimgt.api.dto.ConditionDTO; @@ -41,7 +40,6 @@ import org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO; import org.wso2.carbon.apimgt.api.dto.KeyManagerPermissionConfigurationDTO; import org.wso2.carbon.apimgt.api.dto.UserApplicationAPIUsage; -import org.wso2.carbon.apimgt.api.model.AIEndpointConfiguration; import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.api.model.APICategory; import org.wso2.carbon.apimgt.api.model.APIIdentifier; @@ -19873,8 +19871,6 @@ public void addAIConfiguration(String apiUUID, String revisionUUID, AIConfigurat stmt.setString(2, apiUUID); stmt.setString(3, revisionUUID); stmt.setString(4, provider.getId()); - stmt.setString(5, new Gson().toJson(aiConfiguration.getAiEndpointConfiguration())); - stmt.setString(6, new Gson().toJson(aiConfiguration.getTokenBasedThrottlingConfiguration())); stmt.executeUpdate(); connection.commit(); } catch (SQLException e) { @@ -19882,30 +19878,6 @@ public void addAIConfiguration(String apiUUID, String revisionUUID, AIConfigurat } } - /** - * Updates AI configuration for the given API UUID. - * - * @param apiUUID The UUID of the API. - * @param aiConfiguration The AI configuration to be updated. - * @throws APIManagementException If an error occurs while updating the AI configuration. - */ - public void updateAIConfiguration(String apiUUID, AIConfiguration aiConfiguration) - throws APIManagementException { - - String sql = SQLConstants.UPDATE_AI_CONFIGURATION; - try (Connection connection = APIMgtDBUtil.getConnection(); - PreparedStatement stmt = connection.prepareStatement(sql)) { - connection.setAutoCommit(false); - stmt.setString(1, new Gson().toJson(aiConfiguration.getAiEndpointConfiguration())); - stmt.setString(2, new Gson().toJson(aiConfiguration.getTokenBasedThrottlingConfiguration())); - stmt.setString(3, apiUUID); - stmt.executeUpdate(); - connection.commit(); - } catch (SQLException e) { - throw new APIManagementException("Error while updating AI API configuration for API: " + apiUUID, e); - } - } - /** * Deletes AI configuration for the given API UUID and optionally a revision UUID. * @@ -19957,11 +19929,6 @@ public AIConfiguration getAIConfiguration(String uuid, String revisionUUID, Stri try (ResultSet rs = ps.executeQuery()) { if (rs.next()) { aiConfiguration = new AIConfiguration(); - aiConfiguration.setAiEndpointConfiguration( - new Gson().fromJson(rs.getString("ENDPOINT_CONFIGURATION"), AIEndpointConfiguration.class)); - aiConfiguration.setTokenBasedThrottlingConfiguration( - new Gson().fromJson(rs.getString("THROTTLING_CONFIGURATIONS"), TokenBaseThrottlingCountHolder.class)); - String providerId = rs.getString("LLM_PROVIDER_UUID"); LLMProvider provider = getLLMProvider(organization, providerId); aiConfiguration.setLlmProviderName(provider.getName()); diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java index 205ea250116c..0b0540521388 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/SubscriptionValidationDAO.java @@ -23,12 +23,10 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; import org.wso2.carbon.apimgt.api.SubscriptionAlreadyExistingException; import org.wso2.carbon.apimgt.api.dto.ConditionDTO; import org.wso2.carbon.apimgt.api.dto.ConditionGroupDTO; import org.wso2.carbon.apimgt.api.model.AIConfiguration; -import org.wso2.carbon.apimgt.api.model.AIEndpointConfiguration; import org.wso2.carbon.apimgt.api.model.OperationPolicy; import org.wso2.carbon.apimgt.api.model.policy.AIAPIQuotaLimit; import org.wso2.carbon.apimgt.api.model.policy.BandwidthLimit; @@ -1147,8 +1145,6 @@ public List getAllApis(String organization, String deployment, boolean isEx if (resultSet.getString("AI_CONFIGURATION_UUID") != null) { AIConfiguration aiConfiguration = new AIConfiguration(); aiConfiguration.setLlmProviderId(resultSet.getString("LLM_PROVIDER_UUID")); - aiConfiguration.setAiEndpointConfiguration(new Gson().fromJson(resultSet.getString("ENDPOINT_CONFIGURATION"), AIEndpointConfiguration.class)); - aiConfiguration.setTokenBasedThrottlingConfiguration(new Gson().fromJson(resultSet.getString("THROTTLING_CONFIGURATIONS"), TokenBaseThrottlingCountHolder.class)); api.setAiConfiguration(aiConfiguration); } if (resultSet.getString("IS_EGRESS") != null) { @@ -1284,8 +1280,6 @@ public API getAPIByContextAndVersion(String context, String version, String depl if (resultSet.getString("AI_CONFIGURATION_UUID") != null) { AIConfiguration aiConfiguration = new AIConfiguration(); aiConfiguration.setLlmProviderId(resultSet.getString("LLM_PROVIDER_UUID")); - aiConfiguration.setAiEndpointConfiguration(new Gson().fromJson(resultSet.getString("ENDPOINT_CONFIGURATION"), AIEndpointConfiguration.class)); - aiConfiguration.setTokenBasedThrottlingConfiguration(new Gson().fromJson(resultSet.getString("THROTTLING_CONFIGURATIONS"), TokenBaseThrottlingCountHolder.class)); api.setAiConfiguration(aiConfiguration); } if (resultSet.getString("IS_EGRESS") != null) { @@ -1506,8 +1500,6 @@ public API getApiByUUID(String apiId, String deployment, String organization, bo if (resultSet.getString("AI_CONFIGURATION_UUID") != null) { AIConfiguration aiConfiguration = new AIConfiguration(); aiConfiguration.setLlmProviderId(resultSet.getString("LLM_PROVIDER_UUID")); - aiConfiguration.setAiEndpointConfiguration(new Gson().fromJson(resultSet.getString("ENDPOINT_CONFIGURATION"), AIEndpointConfiguration.class)); - aiConfiguration.setTokenBasedThrottlingConfiguration(new Gson().fromJson(resultSet.getString("THROTTLING_CONFIGURATIONS"), TokenBaseThrottlingCountHolder.class)); api.setAiConfiguration(aiConfiguration); } if (resultSet.getString("IS_EGRESS") != null) { @@ -1608,8 +1600,6 @@ public List getAllApisByLabel(String gatewayLabel, Boolean expand) { if (resultSet.getString("AI_CONFIGURATION_UUID") != null) { AIConfiguration aiConfiguration = new AIConfiguration(); aiConfiguration.setLlmProviderId(resultSet.getString("LLM_PROVIDER_UUID")); - aiConfiguration.setAiEndpointConfiguration(new Gson().fromJson(resultSet.getString("ENDPOINT_CONFIGURATION"), AIEndpointConfiguration.class)); - aiConfiguration.setTokenBasedThrottlingConfiguration(new Gson().fromJson(resultSet.getString("THROTTLING_CONFIGURATIONS"), TokenBaseThrottlingCountHolder.class)); api.setAiConfiguration(aiConfiguration); } if (resultSet.getString("IS_EGRESS") != null) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java index 25a61a296135..a0b3cb641d3d 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java @@ -2785,18 +2785,14 @@ public class SQLConstants { public static final String INSERT_AI_CONFIGURATION = "INSERT INTO AM_API_AI_CONFIGURATION (AI_CONFIGURATION_UUID, API_UUID, API_REVISION_UUID, " + - "LLM_PROVIDER_UUID, ENDPOINT_CONFIGURATION, THROTTLING_CONFIGURATIONS) VALUES (?, ?, ?, ?, ?, ?)"; - - public static final String UPDATE_AI_CONFIGURATION = - "UPDATE AM_API_AI_CONFIGURATION SET ENDPOINT_CONFIGURATION = ?, THROTTLING_CONFIGURATIONS = ? " + - "WHERE API_UUID = ? AND API_REVISION_UUID IS NULL"; + "LLM_PROVIDER_UUID) VALUES (?, ?, ?, ?)"; public static final String GET_AI_CONFIGURATION = - "SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID, ENDPOINT_CONFIGURATION, THROTTLING_CONFIGURATIONS " + + "SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID " + "FROM AM_API_AI_CONFIGURATION WHERE API_UUID = ? AND API_REVISION_UUID IS NULL"; public static final String GET_AI_CONFIGURATION_REVISION = - "SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID, ENDPOINT_CONFIGURATION, THROTTLING_CONFIGURATIONS " + + "SELECT AI_CONFIGURATION_UUID, LLM_PROVIDER_UUID " + "FROM AM_API_AI_CONFIGURATION WHERE API_UUID = ? AND API_REVISION_UUID = ?"; public static final String DELETE_AI_CONFIGURATION_REVISION = diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java index 9555ddeba660..dd5d2104b99f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SubscriptionValidationSQLConstants.java @@ -603,8 +603,6 @@ public class SubscriptionValidationSQLConstants { "AM_REVISION.REVISION_UUID AS REVISION_UUID,AM_DEPLOYMENT_REVISION_MAPPING.NAME AS " + "DEPLOYMENT_NAME,AM_API.ORGANIZATION, " + "AM_API_AI_CONFIGURATION.AI_CONFIGURATION_UUID AS AI_CONFIGURATION_UUID, " + - "AM_API_AI_CONFIGURATION.ENDPOINT_CONFIGURATION AS ENDPOINT_CONFIGURATION, " + - "AM_API_AI_CONFIGURATION.THROTTLING_CONFIGURATIONS AS THROTTLING_CONFIGURATIONS, " + "AM_API_AI_CONFIGURATION.LLM_PROVIDER_UUID AS LLM_PROVIDER_UUID, " + "AM_API.API_SUBTYPE AS API_SUBTYPE, " + "AM_API.IS_EGRESS AS IS_EGRESS " + @@ -644,8 +642,6 @@ public class SubscriptionValidationSQLConstants { "AS DEPLOYMENT_NAME, " + "AM_API_DEFAULT_VERSION.PUBLISHED_DEFAULT_API_VERSION AS PUBLISHED_DEFAULT_API_VERSION,AM_API.ORGANIZATION, " + "AM_API_AI_CONFIGURATION.AI_CONFIGURATION_UUID AS AI_CONFIGURATION_UUID, " + - "AM_API_AI_CONFIGURATION.ENDPOINT_CONFIGURATION AS ENDPOINT_CONFIGURATION, " + - "AM_API_AI_CONFIGURATION.THROTTLING_CONFIGURATIONS AS THROTTLING_CONFIGURATIONS, " + "AM_API_AI_CONFIGURATION.LLM_PROVIDER_UUID AS LLM_PROVIDER_UUID, " + "AM_API.API_SUBTYPE AS API_SUBTYPE, " + "AM_API.IS_EGRESS AS IS_EGRESS " + @@ -667,8 +663,6 @@ public class SubscriptionValidationSQLConstants { "AM_API.API_VERSION, AM_API.API_TYPE, AM_API.STATUS, AM_REVISION.REVISION_UUID AS REVISION_UUID, " + "AM_DEPLOYMENT_REVISION_MAPPING.NAME AS DEPLOYMENT_NAME, AM_API.ORGANIZATION, " + "AM_API_AI_CONFIGURATION.AI_CONFIGURATION_UUID AS AI_CONFIGURATION_UUID, " + - "AM_API_AI_CONFIGURATION.ENDPOINT_CONFIGURATION AS ENDPOINT_CONFIGURATION, " + - "AM_API_AI_CONFIGURATION.THROTTLING_CONFIGURATIONS AS THROTTLING_CONFIGURATIONS, " + "AM_API_AI_CONFIGURATION.LLM_PROVIDER_UUID AS LLM_PROVIDER_UUID, " + "AM_API.API_SUBTYPE AS API_SUBTYPE, " + "AM_API.IS_EGRESS AS IS_EGRESS " + diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index 6fedcef51c7c..06fdd2d8f95e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -6316,6 +6316,59 @@ public static void addDefaultTenantAdvancedThrottlePolicies(String tenantDomain, SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantId); deployRetrievedSubscriptionPolicy(tenantId, retrievedPolicy); } + + //Adding AI API Quota based subscription level policies + long[] totalTokenCountValues = new long[]{50000, 10000, 1000}; + long[] aiPolicyRequestCount = new long[]{500, 100, 10}; + String[] aiApiQuotaSubPolicyNames = new String[]{APIConstants.DEFAULT_SUB_POLICY_AI_GOLD, + APIConstants.DEFAULT_SUB_POLICY_AI_SILVER, APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE}; + String[] aiApiQuotaSubPolicyDescriptions = new String[]{ + APIConstants.DEFAULT_SUB_POLICY_AI_GOLD_DESC, APIConstants.DEFAULT_SUB_POLICY_AI_SILVER_DESC, + APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE_DESC}; + + for (int i = 0; i < aiApiQuotaSubPolicyNames.length; i++) { + policyName = aiApiQuotaSubPolicyNames[i]; + boolean needDeployment = false; + SubscriptionPolicy subscriptionPolicy = new SubscriptionPolicy(policyName); + subscriptionPolicy.setDisplayName(policyName); + subscriptionPolicy.setDescription(aiApiQuotaSubPolicyDescriptions[i]); + subscriptionPolicy.setTenantId(tenantId); + subscriptionPolicy.setDeployed(true); + QuotaPolicy defaultQuotaPolicy = new QuotaPolicy(); + AIAPIQuotaLimit aiApiQuotaLimit = new AIAPIQuotaLimit(); + aiApiQuotaLimit.setRequestCount(aiPolicyRequestCount[i]); + aiApiQuotaLimit.setTotalTokenCount(totalTokenCountValues[i]); + aiApiQuotaLimit.setUnitTime(1); + aiApiQuotaLimit.setTimeUnit(APIConstants.TIME_UNIT_MINUTE); + defaultQuotaPolicy.setType(PolicyConstants.AI_API_QUOTA_TYPE); + defaultQuotaPolicy.setLimit(aiApiQuotaLimit); + subscriptionPolicy.setDefaultQuotaPolicy(defaultQuotaPolicy); + subscriptionPolicy.setStopOnQuotaReach(true); + subscriptionPolicy.setBillingPlan(APIConstants.BILLING_PLAN_FREE); + + if (!apiMgtDAO.isPolicyExist(PolicyConstants.POLICY_LEVEL_SUB, tenantId, policyName)) { + apiMgtDAO.addSubscriptionPolicy(subscriptionPolicy); + needDeployment = true; + } + + if (!needDeployment) { + continue; + } + String superTenantDomain = null; + try { + superTenantDomain = ServiceReferenceHolder.getInstance().getRealmService().getTenantManager(). + getSuperTenantDomain(); + } catch (UserStoreException e) { + handleInternalException("Error in getting the super tenant domain", e); + } + + boolean isSuperTenant = tenantDomain.equals(superTenantDomain); + if (isSuperTenant) { + continue; + } + SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(policyName, tenantId); + deployRetrievedSubscriptionPolicy(tenantId, retrievedPolicy); + } long tenThousandPerMinTier = defualtLimits.containsKey(APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN) ? defualtLimits.get(APIConstants.DEFAULT_API_POLICY_TEN_THOUSAND_REQ_PER_MIN) : 10000; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/utils/APIUtilTierTest.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/utils/APIUtilTierTest.java index ff1ce9d9a1d0..cfc2c362f882 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/utils/APIUtilTierTest.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/test/java/org/wso2/carbon/apimgt/impl/utils/APIUtilTierTest.java @@ -439,7 +439,9 @@ public void testAddDefaultSuperTenantAdvancedThrottlePoliciesSubLevel() throws E APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SUBSCRIPTIONLESS, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, - APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED}; + APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED, + APIConstants.DEFAULT_SUB_POLICY_AI_GOLD, APIConstants.DEFAULT_SUB_POLICY_AI_SILVER, + APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE}; for (String policy : subPolicies) { Mockito.when( @@ -472,7 +474,8 @@ public void testAddDefaultSuperTenantAdvancedThrottlePoliciesSubLevelAlreadyAdde APIConstants.DEFAULT_SUB_POLICY_ASYNC_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, - APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED}; + APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_AI_GOLD, + APIConstants.DEFAULT_SUB_POLICY_AI_SILVER, APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE}; for (String policy : subPolicies) { Mockito.when( @@ -622,7 +625,9 @@ public void testAddDefaultTenantAdvancedThrottlePoliciesSubLevel() throws Except APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SUBSCRIPTIONLESS, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, - APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED}; + APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED, + APIConstants.DEFAULT_SUB_POLICY_AI_GOLD, APIConstants.DEFAULT_SUB_POLICY_AI_SILVER, + APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE}; for (String policy : policies) { Mockito.when( @@ -656,7 +661,9 @@ public void testAddDefaultTenantAdvancedThrottlePoliciesSubLevelAlreadyAdded() t APIConstants.DEFAULT_SUB_POLICY_ASYNC_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_UNLIMITED, APIConstants.DEFAULT_SUB_POLICY_ASYNC_SUBSCRIPTIONLESS, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_GOLD, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_SILVER, - APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED}; + APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_BRONZE, APIConstants.DEFAULT_SUB_POLICY_ASYNC_WH_UNLIMITED, + APIConstants.DEFAULT_SUB_POLICY_AI_GOLD, APIConstants.DEFAULT_SUB_POLICY_AI_SILVER, + APIConstants.DEFAULT_SUB_POLICY_AI_BRONZE}; for (String policy : policies) { Mockito.when( diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIConfigurationDTO.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIConfigurationDTO.java index d6d6a20bea46..ef6d8bb92dea 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIConfigurationDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIConfigurationDTO.java @@ -2,8 +2,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonCreator; -import org.wso2.carbon.apimgt.internal.service.dto.AIEndpointConfigurationDTO; -import org.wso2.carbon.apimgt.internal.service.dto.TokenBaseThrottlingCountHolderDTO; import javax.validation.constraints.*; @@ -18,27 +16,7 @@ public class AIConfigurationDTO { - private AIEndpointConfigurationDTO aiEndpointConfiguration = null; private String llmProviderId = null; - private TokenBaseThrottlingCountHolderDTO tokenBasedThrottlingConfiguration = null; - - /** - * Configuration for AI endpoints, including sandbox and production. - **/ - public AIConfigurationDTO aiEndpointConfiguration(AIEndpointConfigurationDTO aiEndpointConfiguration) { - this.aiEndpointConfiguration = aiEndpointConfiguration; - return this; - } - - - @ApiModelProperty(value = "Configuration for AI endpoints, including sandbox and production.") - @JsonProperty("aiEndpointConfiguration") - public AIEndpointConfigurationDTO getAiEndpointConfiguration() { - return aiEndpointConfiguration; - } - public void setAiEndpointConfiguration(AIEndpointConfigurationDTO aiEndpointConfiguration) { - this.aiEndpointConfiguration = aiEndpointConfiguration; - } /** * UUID of the LLM (Large Language Model) provider. @@ -58,24 +36,6 @@ public void setLlmProviderId(String llmProviderId) { this.llmProviderId = llmProviderId; } - /** - * Configuration for token-based throttling for AI features. - **/ - public AIConfigurationDTO tokenBasedThrottlingConfiguration(TokenBaseThrottlingCountHolderDTO tokenBasedThrottlingConfiguration) { - this.tokenBasedThrottlingConfiguration = tokenBasedThrottlingConfiguration; - return this; - } - - - @ApiModelProperty(value = "Configuration for token-based throttling for AI features.") - @JsonProperty("tokenBasedThrottlingConfiguration") - public TokenBaseThrottlingCountHolderDTO getTokenBasedThrottlingConfiguration() { - return tokenBasedThrottlingConfiguration; - } - public void setTokenBasedThrottlingConfiguration(TokenBaseThrottlingCountHolderDTO tokenBasedThrottlingConfiguration) { - this.tokenBasedThrottlingConfiguration = tokenBasedThrottlingConfiguration; - } - @Override public boolean equals(java.lang.Object o) { @@ -86,14 +46,12 @@ public boolean equals(java.lang.Object o) { return false; } AIConfigurationDTO aiConfiguration = (AIConfigurationDTO) o; - return Objects.equals(aiEndpointConfiguration, aiConfiguration.aiEndpointConfiguration) && - Objects.equals(llmProviderId, aiConfiguration.llmProviderId) && - Objects.equals(tokenBasedThrottlingConfiguration, aiConfiguration.tokenBasedThrottlingConfiguration); + return Objects.equals(llmProviderId, aiConfiguration.llmProviderId); } @Override public int hashCode() { - return Objects.hash(aiEndpointConfiguration, llmProviderId, tokenBasedThrottlingConfiguration); + return Objects.hash(llmProviderId); } @Override @@ -101,9 +59,7 @@ public String toString() { StringBuilder sb = new StringBuilder(); sb.append("class AIConfigurationDTO {\n"); - sb.append(" aiEndpointConfiguration: ").append(toIndentedString(aiEndpointConfiguration)).append("\n"); sb.append(" llmProviderId: ").append(toIndentedString(llmProviderId)).append("\n"); - sb.append(" tokenBasedThrottlingConfiguration: ").append(toIndentedString(tokenBasedThrottlingConfiguration)).append("\n"); sb.append("}"); return sb.toString(); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIEndpointConfigurationDTO.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIEndpointConfigurationDTO.java deleted file mode 100644 index 1ad497434962..000000000000 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/AIEndpointConfigurationDTO.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.wso2.carbon.apimgt.internal.service.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonCreator; -import javax.validation.constraints.*; - - -import io.swagger.annotations.*; -import java.util.Objects; - -import javax.xml.bind.annotation.*; -import org.wso2.carbon.apimgt.rest.api.common.annotations.Scope; -import com.fasterxml.jackson.annotation.JsonCreator; - - - -public class AIEndpointConfigurationDTO { - - private String sandboxAuthValue = null; - private String productionAuthValue = null; - - /** - * Authorization value for the sandbox environment. - **/ - public AIEndpointConfigurationDTO sandboxAuthValue(String sandboxAuthValue) { - this.sandboxAuthValue = sandboxAuthValue; - return this; - } - - - @ApiModelProperty(value = "Authorization value for the sandbox environment.") - @JsonProperty("sandboxAuthValue") - public String getSandboxAuthValue() { - return sandboxAuthValue; - } - public void setSandboxAuthValue(String sandboxAuthValue) { - this.sandboxAuthValue = sandboxAuthValue; - } - - /** - * Authorization value for the production environment. - **/ - public AIEndpointConfigurationDTO productionAuthValue(String productionAuthValue) { - this.productionAuthValue = productionAuthValue; - return this; - } - - - @ApiModelProperty(value = "Authorization value for the production environment.") - @JsonProperty("productionAuthValue") - public String getProductionAuthValue() { - return productionAuthValue; - } - public void setProductionAuthValue(String productionAuthValue) { - this.productionAuthValue = productionAuthValue; - } - - - @Override - public boolean equals(java.lang.Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - AIEndpointConfigurationDTO aiEndpointConfiguration = (AIEndpointConfigurationDTO) o; - return Objects.equals(sandboxAuthValue, aiEndpointConfiguration.sandboxAuthValue) && - Objects.equals(productionAuthValue, aiEndpointConfiguration.productionAuthValue); - } - - @Override - public int hashCode() { - return Objects.hash(sandboxAuthValue, productionAuthValue); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class AIEndpointConfigurationDTO {\n"); - - sb.append(" sandboxAuthValue: ").append(toIndentedString(sandboxAuthValue)).append("\n"); - sb.append(" productionAuthValue: ").append(toIndentedString(productionAuthValue)).append("\n"); - sb.append("}"); - return sb.toString(); - } - - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(java.lang.Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); - } -} - diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/TokenBaseThrottlingCountHolderDTO.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/TokenBaseThrottlingCountHolderDTO.java deleted file mode 100644 index 718a2c6928c3..000000000000 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/gen/java/org/wso2/carbon/apimgt/internal/service/dto/TokenBaseThrottlingCountHolderDTO.java +++ /dev/null @@ -1,204 +0,0 @@ -package org.wso2.carbon.apimgt.internal.service.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.annotation.JsonCreator; -import javax.validation.constraints.*; - - -import io.swagger.annotations.*; -import java.util.Objects; - -import javax.xml.bind.annotation.*; -import org.wso2.carbon.apimgt.rest.api.common.annotations.Scope; -import com.fasterxml.jackson.annotation.JsonCreator; - - - -public class TokenBaseThrottlingCountHolderDTO { - - private String productionMaxPromptTokenCount = null; - private String productionMaxCompletionTokenCount = null; - private String productionMaxTotalTokenCount = null; - private String sandboxMaxPromptTokenCount = null; - private String sandboxMaxCompletionTokenCount = null; - private String sandboxMaxTotalTokenCount = null; - private Boolean isTokenBasedThrottlingEnabled = null; - - /** - * Maximum allowed prompt token count for production. - **/ - public TokenBaseThrottlingCountHolderDTO productionMaxPromptTokenCount(String productionMaxPromptTokenCount) { - this.productionMaxPromptTokenCount = productionMaxPromptTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum allowed prompt token count for production.") - @JsonProperty("productionMaxPromptTokenCount") - public String getProductionMaxPromptTokenCount() { - return productionMaxPromptTokenCount; - } - public void setProductionMaxPromptTokenCount(String productionMaxPromptTokenCount) { - this.productionMaxPromptTokenCount = productionMaxPromptTokenCount; - } - - /** - * Maximum allowed completion token count for production. - **/ - public TokenBaseThrottlingCountHolderDTO productionMaxCompletionTokenCount(String productionMaxCompletionTokenCount) { - this.productionMaxCompletionTokenCount = productionMaxCompletionTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum allowed completion token count for production.") - @JsonProperty("productionMaxCompletionTokenCount") - public String getProductionMaxCompletionTokenCount() { - return productionMaxCompletionTokenCount; - } - public void setProductionMaxCompletionTokenCount(String productionMaxCompletionTokenCount) { - this.productionMaxCompletionTokenCount = productionMaxCompletionTokenCount; - } - - /** - * Maximum total token count allowed for production. - **/ - public TokenBaseThrottlingCountHolderDTO productionMaxTotalTokenCount(String productionMaxTotalTokenCount) { - this.productionMaxTotalTokenCount = productionMaxTotalTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum total token count allowed for production.") - @JsonProperty("productionMaxTotalTokenCount") - public String getProductionMaxTotalTokenCount() { - return productionMaxTotalTokenCount; - } - public void setProductionMaxTotalTokenCount(String productionMaxTotalTokenCount) { - this.productionMaxTotalTokenCount = productionMaxTotalTokenCount; - } - - /** - * Maximum allowed prompt token count for sandbox. - **/ - public TokenBaseThrottlingCountHolderDTO sandboxMaxPromptTokenCount(String sandboxMaxPromptTokenCount) { - this.sandboxMaxPromptTokenCount = sandboxMaxPromptTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum allowed prompt token count for sandbox.") - @JsonProperty("sandboxMaxPromptTokenCount") - public String getSandboxMaxPromptTokenCount() { - return sandboxMaxPromptTokenCount; - } - public void setSandboxMaxPromptTokenCount(String sandboxMaxPromptTokenCount) { - this.sandboxMaxPromptTokenCount = sandboxMaxPromptTokenCount; - } - - /** - * Maximum allowed completion token count for sandbox. - **/ - public TokenBaseThrottlingCountHolderDTO sandboxMaxCompletionTokenCount(String sandboxMaxCompletionTokenCount) { - this.sandboxMaxCompletionTokenCount = sandboxMaxCompletionTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum allowed completion token count for sandbox.") - @JsonProperty("sandboxMaxCompletionTokenCount") - public String getSandboxMaxCompletionTokenCount() { - return sandboxMaxCompletionTokenCount; - } - public void setSandboxMaxCompletionTokenCount(String sandboxMaxCompletionTokenCount) { - this.sandboxMaxCompletionTokenCount = sandboxMaxCompletionTokenCount; - } - - /** - * Maximum total token count allowed for sandbox. - **/ - public TokenBaseThrottlingCountHolderDTO sandboxMaxTotalTokenCount(String sandboxMaxTotalTokenCount) { - this.sandboxMaxTotalTokenCount = sandboxMaxTotalTokenCount; - return this; - } - - - @ApiModelProperty(value = "Maximum total token count allowed for sandbox.") - @JsonProperty("sandboxMaxTotalTokenCount") - public String getSandboxMaxTotalTokenCount() { - return sandboxMaxTotalTokenCount; - } - public void setSandboxMaxTotalTokenCount(String sandboxMaxTotalTokenCount) { - this.sandboxMaxTotalTokenCount = sandboxMaxTotalTokenCount; - } - - /** - * Whether token-based throttling is enabled. - **/ - public TokenBaseThrottlingCountHolderDTO isTokenBasedThrottlingEnabled(Boolean isTokenBasedThrottlingEnabled) { - this.isTokenBasedThrottlingEnabled = isTokenBasedThrottlingEnabled; - return this; - } - - - @ApiModelProperty(value = "Whether token-based throttling is enabled.") - @JsonProperty("isTokenBasedThrottlingEnabled") - public Boolean isIsTokenBasedThrottlingEnabled() { - return isTokenBasedThrottlingEnabled; - } - public void setIsTokenBasedThrottlingEnabled(Boolean isTokenBasedThrottlingEnabled) { - this.isTokenBasedThrottlingEnabled = isTokenBasedThrottlingEnabled; - } - - - @Override - public boolean equals(java.lang.Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - TokenBaseThrottlingCountHolderDTO tokenBaseThrottlingCountHolder = (TokenBaseThrottlingCountHolderDTO) o; - return Objects.equals(productionMaxPromptTokenCount, tokenBaseThrottlingCountHolder.productionMaxPromptTokenCount) && - Objects.equals(productionMaxCompletionTokenCount, tokenBaseThrottlingCountHolder.productionMaxCompletionTokenCount) && - Objects.equals(productionMaxTotalTokenCount, tokenBaseThrottlingCountHolder.productionMaxTotalTokenCount) && - Objects.equals(sandboxMaxPromptTokenCount, tokenBaseThrottlingCountHolder.sandboxMaxPromptTokenCount) && - Objects.equals(sandboxMaxCompletionTokenCount, tokenBaseThrottlingCountHolder.sandboxMaxCompletionTokenCount) && - Objects.equals(sandboxMaxTotalTokenCount, tokenBaseThrottlingCountHolder.sandboxMaxTotalTokenCount) && - Objects.equals(isTokenBasedThrottlingEnabled, tokenBaseThrottlingCountHolder.isTokenBasedThrottlingEnabled); - } - - @Override - public int hashCode() { - return Objects.hash(productionMaxPromptTokenCount, productionMaxCompletionTokenCount, productionMaxTotalTokenCount, sandboxMaxPromptTokenCount, sandboxMaxCompletionTokenCount, sandboxMaxTotalTokenCount, isTokenBasedThrottlingEnabled); - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append("class TokenBaseThrottlingCountHolderDTO {\n"); - - sb.append(" productionMaxPromptTokenCount: ").append(toIndentedString(productionMaxPromptTokenCount)).append("\n"); - sb.append(" productionMaxCompletionTokenCount: ").append(toIndentedString(productionMaxCompletionTokenCount)).append("\n"); - sb.append(" productionMaxTotalTokenCount: ").append(toIndentedString(productionMaxTotalTokenCount)).append("\n"); - sb.append(" sandboxMaxPromptTokenCount: ").append(toIndentedString(sandboxMaxPromptTokenCount)).append("\n"); - sb.append(" sandboxMaxCompletionTokenCount: ").append(toIndentedString(sandboxMaxCompletionTokenCount)).append("\n"); - sb.append(" sandboxMaxTotalTokenCount: ").append(toIndentedString(sandboxMaxTotalTokenCount)).append("\n"); - sb.append(" isTokenBasedThrottlingEnabled: ").append(toIndentedString(isTokenBasedThrottlingEnabled)).append("\n"); - sb.append("}"); - return sb.toString(); - } - - /** - * Convert the given object to string with each line indented by 4 spaces - * (except the first line). - */ - private String toIndentedString(java.lang.Object o) { - if (o == null) { - return "null"; - } - return o.toString().replace("\n", "\n "); - } -} - diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java index fbec4b95c767..bf08e7741d77 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/java/org/wso2/carbon/apimgt/internal/service/utils/SubscriptionValidationDataUtil.java @@ -23,11 +23,9 @@ import org.apache.cxf.jaxrs.ext.MessageContext; import org.wso2.carbon.apimgt.api.APIConsumer; import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; import org.wso2.carbon.apimgt.api.APIProvider; import org.wso2.carbon.apimgt.api.dto.ConditionDTO; import org.wso2.carbon.apimgt.api.model.AIConfiguration; -import org.wso2.carbon.apimgt.api.model.AIEndpointConfiguration; import org.wso2.carbon.apimgt.api.model.ApiTypeWrapper; import org.wso2.carbon.apimgt.api.model.OperationPolicy; import org.wso2.carbon.apimgt.api.model.Scope; @@ -124,26 +122,7 @@ private static APIDTO fromAPItoDTO(API model) throws APIManagementException { AIConfiguration aiConfiguration = model.getAiConfiguration(); if (aiConfiguration != null) { AIConfigurationDTO aiConfigurationDTO = new AIConfigurationDTO(); - AIEndpointConfiguration aiEndpointConfiguration = aiConfiguration.getAiEndpointConfiguration(); - if (aiEndpointConfiguration != null) { - AIEndpointConfigurationDTO aiEndpointConfigurationDTO = new AIEndpointConfigurationDTO(); - aiEndpointConfigurationDTO.setSandboxAuthValue(aiEndpointConfiguration.getSandboxAuthValue()); - aiEndpointConfigurationDTO.setProductionAuthValue(aiEndpointConfiguration.getProductionAuthValue()); - aiConfigurationDTO.setAiEndpointConfiguration(aiEndpointConfigurationDTO); - } aiConfigurationDTO.setLlmProviderId(aiConfiguration.getLlmProviderId()); - TokenBaseThrottlingCountHolder tokenBaseThrottlingCountHolder = aiConfiguration.getTokenBasedThrottlingConfiguration(); - if (tokenBaseThrottlingCountHolder != null) { - TokenBaseThrottlingCountHolderDTO tokenBaseThrottlingCountHolderDTO = new TokenBaseThrottlingCountHolderDTO(); - tokenBaseThrottlingCountHolderDTO.setProductionMaxPromptTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount()); - tokenBaseThrottlingCountHolderDTO.setProductionMaxCompletionTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount()); - tokenBaseThrottlingCountHolderDTO.setProductionMaxTotalTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxPromptTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxCompletionTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxTotalTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount()); - tokenBaseThrottlingCountHolderDTO.setIsTokenBasedThrottlingEnabled(tokenBaseThrottlingCountHolder.isTokenBasedThrottlingEnabled()); - aiConfigurationDTO.setTokenBasedThrottlingConfiguration(tokenBaseThrottlingCountHolderDTO); - } apidto.setAiConfiguration(aiConfigurationDTO); } apidto.setIsEgress(model.isEgress() != 0); @@ -211,26 +190,7 @@ public static APIListDTO fromAPIToAPIListDTO(API model) throws APIManagementExce AIConfiguration aiConfiguration = model.getAiConfiguration(); if (aiConfiguration != null) { AIConfigurationDTO aiConfigurationDTO = new AIConfigurationDTO(); - AIEndpointConfiguration aiEndpointConfiguration = aiConfiguration.getAiEndpointConfiguration(); - if (aiEndpointConfiguration != null) { - AIEndpointConfigurationDTO aiEndpointConfigurationDTO = new AIEndpointConfigurationDTO(); - aiEndpointConfigurationDTO.setSandboxAuthValue(aiEndpointConfiguration.getSandboxAuthValue()); - aiEndpointConfigurationDTO.setProductionAuthValue(aiEndpointConfiguration.getProductionAuthValue()); - aiConfigurationDTO.setAiEndpointConfiguration(aiEndpointConfigurationDTO); - } aiConfigurationDTO.setLlmProviderId(aiConfiguration.getLlmProviderId()); - TokenBaseThrottlingCountHolder tokenBaseThrottlingCountHolder = aiConfiguration.getTokenBasedThrottlingConfiguration(); - if (tokenBaseThrottlingCountHolder != null) { - TokenBaseThrottlingCountHolderDTO tokenBaseThrottlingCountHolderDTO = new TokenBaseThrottlingCountHolderDTO(); - tokenBaseThrottlingCountHolderDTO.setProductionMaxPromptTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount()); - tokenBaseThrottlingCountHolderDTO.setProductionMaxCompletionTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount()); - tokenBaseThrottlingCountHolderDTO.setProductionMaxTotalTokenCount(tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxPromptTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxCompletionTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); - tokenBaseThrottlingCountHolderDTO.setSandboxMaxTotalTokenCount(tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount()); - tokenBaseThrottlingCountHolderDTO.setIsTokenBasedThrottlingEnabled(tokenBaseThrottlingCountHolder.isTokenBasedThrottlingEnabled()); - aiConfigurationDTO.setTokenBasedThrottlingConfiguration(tokenBaseThrottlingCountHolderDTO); - } apidto.setAiConfiguration(aiConfigurationDTO); } apidto.setIsEgress(model.isEgress() != 0); diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml index 628d12e1e715..7bbf78b67548 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/src/main/resources/api.yaml @@ -1087,54 +1087,9 @@ definitions: #----------------------------------------------------- AIConfiguration: properties: - aiEndpointConfiguration: - $ref: '#/definitions/AIEndpointConfiguration' - description: Configuration for AI endpoints, including sandbox and production. llmProviderId: type: string description: UUID of the LLM (Large Language Model) provider. - tokenBasedThrottlingConfiguration: - $ref: '#/definitions/TokenBaseThrottlingCountHolder' - description: Configuration for token-based throttling for AI features. - - #----------------------------------------------------- - # The AIEndpointConfiguration resource - #----------------------------------------------------- - AIEndpointConfiguration: - properties: - sandboxAuthValue: - type: string - description: Authorization value for the sandbox environment. - productionAuthValue: - type: string - description: Authorization value for the production environment. - - #----------------------------------------------------- - # The TokenBaseThrottlingCountHolder resource - #----------------------------------------------------- - TokenBaseThrottlingCountHolder: - properties: - productionMaxPromptTokenCount: - type: string - description: Maximum allowed prompt token count for production. - productionMaxCompletionTokenCount: - type: string - description: Maximum allowed completion token count for production. - productionMaxTotalTokenCount: - type: string - description: Maximum total token count allowed for production. - sandboxMaxPromptTokenCount: - type: string - description: Maximum allowed prompt token count for sandbox. - sandboxMaxCompletionTokenCount: - type: string - description: Maximum allowed completion token count for sandbox. - sandboxMaxTotalTokenCount: - type: string - description: Maximum total token count allowed for sandbox. - isTokenBasedThrottlingEnabled: - type: boolean - description: Whether token-based throttling is enabled. #----------------------------------------------------- # synapse Artifact resource diff --git a/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json b/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json index 40ed2d922011..731c8d92babf 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json +++ b/components/apimgt/org.wso2.carbon.apimgt.internal.service/swagger.json @@ -1394,61 +1394,9 @@ }, "AIConfiguration" : { "properties" : { - "aiEndpointConfiguration" : { - "description" : "Configuration for AI endpoints, including sandbox and production.", - "$ref" : "#/definitions/AIEndpointConfiguration" - }, "llmProviderId" : { "type" : "string", "description" : "UUID of the LLM (Large Language Model) provider." - }, - "tokenBasedThrottlingConfiguration" : { - "description" : "Configuration for token-based throttling for AI features.", - "$ref" : "#/definitions/TokenBaseThrottlingCountHolder" - } - } - }, - "AIEndpointConfiguration" : { - "properties" : { - "sandboxAuthValue" : { - "type" : "string", - "description" : "Authorization value for the sandbox environment." - }, - "productionAuthValue" : { - "type" : "string", - "description" : "Authorization value for the production environment." - } - } - }, - "TokenBaseThrottlingCountHolder" : { - "properties" : { - "productionMaxPromptTokenCount" : { - "type" : "string", - "description" : "Maximum allowed prompt token count for production." - }, - "productionMaxCompletionTokenCount" : { - "type" : "string", - "description" : "Maximum allowed completion token count for production." - }, - "productionMaxTotalTokenCount" : { - "type" : "string", - "description" : "Maximum total token count allowed for production." - }, - "sandboxMaxPromptTokenCount" : { - "type" : "string", - "description" : "Maximum allowed prompt token count for sandbox." - }, - "sandboxMaxCompletionTokenCount" : { - "type" : "string", - "description" : "Maximum allowed completion token count for sandbox." - }, - "sandboxMaxTotalTokenCount" : { - "type" : "string", - "description" : "Maximum total token count allowed for sandbox." - }, - "isTokenBasedThrottlingEnabled" : { - "type" : "boolean", - "description" : "Whether token-based throttling is enabled." } } }, diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/APIConstants.java index d202d5165187..31726e4a75a6 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/APIConstants.java @@ -115,13 +115,13 @@ public final class APIConstants { public static final String API_PRODUCTION_THROTTLE_TIMEUNIT = "overview_productionTimeUnit"; public static final String API_SANDBOX_THROTTLE_MAXTPS = "overview_sandboxTps"; public static final String API_SANDBOX_THROTTLE_TIMEUNIT = "overview_sandboxTimeUnit"; - public static final String AI_PRODUCTION_MAX_PROMPT_TOKEN_COUNT = "overview_aiProductionMaxPromptTokenCount"; - public static final String AI_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT = "overview_aiProductionMaxCompletionTokenCount"; - public static final String AI_PRODUCTION_MAX_TOTAL_TOKEN_COUNT = "overview_aiProductionMaxTotalTokenCount"; - public static final String AI_SANDBOX_MAX_PROMPT_TOKEN_COUNT = "overview_aiSandboxMaxPromptTokenCount"; - public static final String AI_SANDBOX_MAX_COMPLETION_TOKEN_COUNT = "overview_aiSandboxMaxCompletionTokenCount"; - public static final String AI_SANDBOX_MAX_TOTAL_TOKEN_COUNT = "overview_aiSandboxMaxTotalTokenCount"; - public static final String AI_TOKEN_BASED_THROTTLING_ENABLED = "overview_aiTokenBasedThrottlingEnabled"; + public static final String API_PRODUCTION_MAX_PROMPT_TOKEN_COUNT = "overview_productionMaxPromptTokenCount"; + public static final String API_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT = "overview_productionMaxCompletionTokenCount"; + public static final String API_PRODUCTION_MAX_TOTAL_TOKEN_COUNT = "overview_productionMaxTotalTokenCount"; + public static final String API_SANDBOX_MAX_PROMPT_TOKEN_COUNT = "overview_sandboxMaxPromptTokenCount"; + public static final String API_SANDBOX_MAX_COMPLETION_TOKEN_COUNT = "overview_sandboxMaxCompletionTokenCount"; + public static final String API_SANDBOX_MAX_TOTAL_TOKEN_COUNT = "overview_sandboxMaxTotalTokenCount"; + public static final String API_TOKEN_BASED_THROTTLING_ENABLED = "overview_isTokenBasedThrottlingEnabled"; public static final String SUPER_TENANT_DOMAIN = "carbon.super"; public static final String VERSION_PLACEHOLDER = "{version}"; public static final String TENANT_PREFIX = "/t/"; @@ -397,6 +397,10 @@ public enum APITransportType { public static final String API_TYPE_GRAPHQL = "GRAPHQL"; public static final String API_TYPE_SOAP = "SOAP"; + // API Subtype constants + public static final String API_SUBTYPE_DEFAULT = "DEFAULT"; + public static final String API_SUBTYPE_AI_API = "AIAPI"; + public static final String API_ASYNC_API_DEFINITION_RESOURCE_NAME = "asyncapi.json"; public static final String API_OVERVIEW_GATEWAY_VENDOR = "overview_gatewayVendor"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/dto/PublisherAPI.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/dto/PublisherAPI.java index 4f95041d1021..1f5c98f6dbd8 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/dto/PublisherAPI.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/dto/PublisherAPI.java @@ -20,6 +20,7 @@ import org.json.simple.JSONObject; import org.wso2.carbon.apimgt.api.model.AIConfiguration; +import org.wso2.carbon.apimgt.api.model.BackendThrottlingConfiguration; import org.wso2.carbon.apimgt.api.model.SOAPToRestSequence; import java.util.ArrayList; @@ -105,6 +106,7 @@ public class PublisherAPI extends PublisherAPIInfo { private String apiOwner; private String vendor; private AIConfiguration aiConfiguration; + private BackendThrottlingConfiguration backendThrottlingConfiguration; public AIConfiguration getAiConfiguration() { @@ -116,6 +118,14 @@ public void setAiConfiguration(AIConfiguration aiConfiguration) { this.aiConfiguration = aiConfiguration; } + public BackendThrottlingConfiguration getBackendThrottlingConfiguration() { + return backendThrottlingConfiguration; + } + + public void setBackendThrottlingConfiguration(BackendThrottlingConfiguration backendThrottlingConfiguration) { + this.backendThrottlingConfiguration = backendThrottlingConfiguration; + } + public String getAudience() { return audience; } diff --git a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java index 72b28b2658dc..a7a4658da4da 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.persistence/src/main/java/org/wso2/carbon/apimgt/persistence/utils/RegistryPersistenceUtil.java @@ -28,13 +28,13 @@ import org.json.simple.parser.ParseException; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; -import org.wso2.carbon.apimgt.api.model.AIConfiguration; +import org.wso2.carbon.apimgt.api.TokenBasedThrottlingCountHolder; import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.api.model.APICategory; import org.wso2.carbon.apimgt.api.model.APIIdentifier; import org.wso2.carbon.apimgt.api.model.APIProduct; import org.wso2.carbon.apimgt.api.model.APIProductIdentifier; +import org.wso2.carbon.apimgt.api.model.BackendThrottlingConfiguration; import org.wso2.carbon.apimgt.api.model.Identifier; import org.wso2.carbon.apimgt.api.model.Tier; import org.wso2.carbon.apimgt.api.model.URITemplate; @@ -214,37 +214,37 @@ public static GenericArtifact createAPIArtifactContent(GenericArtifact artifact, policyBuilder.append("||"); } - if (api.getAiConfiguration() != null - && api.getAiConfiguration().getTokenBasedThrottlingConfiguration() != null - && api.getAiConfiguration().getTokenBasedThrottlingConfiguration() + if (api.getBackendThrottlingConfiguration() != null + && api.getBackendThrottlingConfiguration().getTokenBasedThrottlingConfiguration() != null + && api.getBackendThrottlingConfiguration().getTokenBasedThrottlingConfiguration() .isTokenBasedThrottlingEnabled()) { - TokenBaseThrottlingCountHolder tokenBaseThrottlingCountHolder = api.getAiConfiguration() + TokenBasedThrottlingCountHolder tokenBasedThrottlingCountHolder = api.getBackendThrottlingConfiguration() .getTokenBasedThrottlingConfiguration(); - artifact.setAttribute(APIConstants.AI_TOKEN_BASED_THROTTLING_ENABLED, - tokenBaseThrottlingCountHolder.isTokenBasedThrottlingEnabled().toString()); - if (tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_PRODUCTION_MAX_PROMPT_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount()); + artifact.setAttribute(APIConstants.API_TOKEN_BASED_THROTTLING_ENABLED, + tokenBasedThrottlingCountHolder.isTokenBasedThrottlingEnabled().toString()); + if (tokenBasedThrottlingCountHolder.getProductionMaxPromptTokenCount() != null) { + artifact.setAttribute(APIConstants.API_PRODUCTION_MAX_PROMPT_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getProductionMaxPromptTokenCount()); } - if (tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount()); + if (tokenBasedThrottlingCountHolder.getProductionMaxCompletionTokenCount() != null) { + artifact.setAttribute(APIConstants.API_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getProductionMaxCompletionTokenCount()); } - if (tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_PRODUCTION_MAX_TOTAL_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount()); + if (tokenBasedThrottlingCountHolder.getProductionMaxTotalTokenCount() != null) { + artifact.setAttribute(APIConstants.API_PRODUCTION_MAX_TOTAL_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getProductionMaxTotalTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_SANDBOX_MAX_PROMPT_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount()); + if (tokenBasedThrottlingCountHolder.getSandboxMaxPromptTokenCount() != null) { + artifact.setAttribute(APIConstants.API_SANDBOX_MAX_PROMPT_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getSandboxMaxPromptTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_SANDBOX_MAX_COMPLETION_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); + if (tokenBasedThrottlingCountHolder.getSandboxMaxCompletionTokenCount() != null) { + artifact.setAttribute(APIConstants.API_SANDBOX_MAX_COMPLETION_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount() != null) { - artifact.setAttribute(APIConstants.AI_SANDBOX_MAX_TOTAL_TOKEN_COUNT, - tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount()); + if (tokenBasedThrottlingCountHolder.getSandboxMaxTotalTokenCount() != null) { + artifact.setAttribute(APIConstants.API_SANDBOX_MAX_TOTAL_TOKEN_COUNT, + tokenBasedThrottlingCountHolder.getSandboxMaxTotalTokenCount()); } } @@ -676,26 +676,31 @@ public static API getAPI(GovernanceArtifact artifact, Registry registry) api.setSandboxMaxTps(artifact.getAttribute(APIConstants.API_SANDBOX_THROTTLE_MAXTPS)); api.setSandboxTimeUnit(artifact.getAttribute(APIConstants.API_SANDBOX_THROTTLE_TIMEUNIT)); - if (artifact.getAttribute(APIConstants.AI_TOKEN_BASED_THROTTLING_ENABLED) != null && Boolean.parseBoolean( - artifact.getAttribute(APIConstants.AI_TOKEN_BASED_THROTTLING_ENABLED))) { - TokenBaseThrottlingCountHolder aiThrottlingConfiguration = new TokenBaseThrottlingCountHolder(); - aiThrottlingConfiguration.setTokenBasedThrottlingEnabled(true); - aiThrottlingConfiguration.setProductionMaxPromptTokenCount( - artifact.getAttribute(APIConstants.AI_PRODUCTION_MAX_PROMPT_TOKEN_COUNT)); - aiThrottlingConfiguration.setProductionMaxCompletionTokenCount( - artifact.getAttribute(APIConstants.AI_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT)); - aiThrottlingConfiguration.setProductionMaxTotalTokenCount( - artifact.getAttribute(APIConstants.AI_PRODUCTION_MAX_TOTAL_TOKEN_COUNT)); - aiThrottlingConfiguration.setSandboxMaxPromptTokenCount( - artifact.getAttribute(APIConstants.AI_SANDBOX_MAX_PROMPT_TOKEN_COUNT)); - aiThrottlingConfiguration.setSandboxMaxCompletionTokenCount( - artifact.getAttribute(APIConstants.AI_SANDBOX_MAX_COMPLETION_TOKEN_COUNT)); - aiThrottlingConfiguration.setSandboxMaxTotalTokenCount( - artifact.getAttribute(APIConstants.AI_SANDBOX_MAX_TOTAL_TOKEN_COUNT)); - AIConfiguration aiConfiguration = new AIConfiguration(); - aiConfiguration.setTokenBasedThrottlingConfiguration(aiThrottlingConfiguration); - api.setAiConfiguration(aiConfiguration); + BackendThrottlingConfiguration backendThrottlingConfiguration = new BackendThrottlingConfiguration(); + backendThrottlingConfiguration.setProductionMaxTps(artifact.getAttribute(APIConstants.API_PRODUCTION_THROTTLE_MAXTPS)); + backendThrottlingConfiguration.setProductionTimeUnit(artifact.getAttribute(APIConstants.API_PRODUCTION_THROTTLE_TIMEUNIT)); + backendThrottlingConfiguration.setSandboxMaxTps(artifact.getAttribute(APIConstants.API_SANDBOX_THROTTLE_MAXTPS)); + backendThrottlingConfiguration.setSandboxTimeUnit(artifact.getAttribute(APIConstants.API_SANDBOX_THROTTLE_TIMEUNIT)); + + if (artifact.getAttribute(APIConstants.API_TOKEN_BASED_THROTTLING_ENABLED) != null && Boolean.parseBoolean( + artifact.getAttribute(APIConstants.API_TOKEN_BASED_THROTTLING_ENABLED))) { + TokenBasedThrottlingCountHolder tokenBasedThrottlingCountHolder = new TokenBasedThrottlingCountHolder(); + tokenBasedThrottlingCountHolder.setTokenBasedThrottlingEnabled(true); + tokenBasedThrottlingCountHolder.setProductionMaxPromptTokenCount( + artifact.getAttribute(APIConstants.API_PRODUCTION_MAX_PROMPT_TOKEN_COUNT)); + tokenBasedThrottlingCountHolder.setProductionMaxCompletionTokenCount( + artifact.getAttribute(APIConstants.API_PRODUCTION_MAX_COMPLETION_TOKEN_COUNT)); + tokenBasedThrottlingCountHolder.setProductionMaxTotalTokenCount( + artifact.getAttribute(APIConstants.API_PRODUCTION_MAX_TOTAL_TOKEN_COUNT)); + tokenBasedThrottlingCountHolder.setSandboxMaxPromptTokenCount( + artifact.getAttribute(APIConstants.API_SANDBOX_MAX_PROMPT_TOKEN_COUNT)); + tokenBasedThrottlingCountHolder.setSandboxMaxCompletionTokenCount( + artifact.getAttribute(APIConstants.API_SANDBOX_MAX_COMPLETION_TOKEN_COUNT)); + tokenBasedThrottlingCountHolder.setSandboxMaxTotalTokenCount( + artifact.getAttribute(APIConstants.API_SANDBOX_MAX_TOTAL_TOKEN_COUNT)); + backendThrottlingConfiguration.setTokenBasedThrottlingConfiguration(tokenBasedThrottlingCountHolder); } + api.setBackendThrottlingConfiguration(backendThrottlingConfiguration); api.setGatewayVendor(artifact.getAttribute(APIConstants.API_OVERVIEW_GATEWAY_VENDOR)); api.setAsyncTransportProtocols(artifact.getAttribute(APIConstants.ASYNC_API_TRANSPORT_PROTOCOLS)); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/TemplateBuilderUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/TemplateBuilderUtil.java index 260b54c5eed9..209ef348754f 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/TemplateBuilderUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/TemplateBuilderUtil.java @@ -35,7 +35,7 @@ import org.wso2.carbon.apimgt.api.APIDefinition; import org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse; import org.wso2.carbon.apimgt.api.APIManagementException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; +import org.wso2.carbon.apimgt.api.TokenBasedThrottlingCountHolder; import org.wso2.carbon.apimgt.api.dto.ClientCertificateDTO; import org.wso2.carbon.apimgt.api.gateway.CredentialDto; import org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO; @@ -296,42 +296,41 @@ public static APITemplateBuilderImpl getAPITemplateBuilder(API api, String tenan properties.put("sandboxUnitTime", api.getSandboxTimeUnit()); } - if (api.getAiConfiguration() != null - && api.getAiConfiguration().getTokenBasedThrottlingConfiguration() != null - && api.getAiConfiguration().getTokenBasedThrottlingConfiguration() + if (api.getBackendThrottlingConfiguration() != null + && api.getBackendThrottlingConfiguration().getTokenBasedThrottlingConfiguration() != null + && api.getBackendThrottlingConfiguration().getTokenBasedThrottlingConfiguration() .isTokenBasedThrottlingEnabled()) { - TokenBaseThrottlingCountHolder tokenBaseThrottlingCountHolder = api.getAiConfiguration() - .getTokenBasedThrottlingConfiguration(); + TokenBasedThrottlingCountHolder tokenBasedThrottlingCountHolder + = api.getBackendThrottlingConfiguration().getTokenBasedThrottlingConfiguration(); properties.put("isTokenBasedThrottlingEnabled", - tokenBaseThrottlingCountHolder.isTokenBasedThrottlingEnabled().toString()); - if (tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount() != null) { + tokenBasedThrottlingCountHolder.isTokenBasedThrottlingEnabled().toString()); + if (tokenBasedThrottlingCountHolder.getProductionMaxPromptTokenCount() != null) { properties.put("productionMaxPromptTokenCount", - tokenBaseThrottlingCountHolder.getProductionMaxPromptTokenCount()); + tokenBasedThrottlingCountHolder.getProductionMaxPromptTokenCount()); } - if (tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount() != null) { + if (tokenBasedThrottlingCountHolder.getProductionMaxCompletionTokenCount() != null) { properties.put("productionMaxCompletionTokenCount", - tokenBaseThrottlingCountHolder.getProductionMaxCompletionTokenCount()); + tokenBasedThrottlingCountHolder.getProductionMaxCompletionTokenCount()); } - if (tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount() != null) { + if (tokenBasedThrottlingCountHolder.getProductionMaxTotalTokenCount() != null) { properties.put("productionMaxTotalTokenCount", - tokenBaseThrottlingCountHolder.getProductionMaxTotalTokenCount()); + tokenBasedThrottlingCountHolder.getProductionMaxTotalTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount() != null) { + if (tokenBasedThrottlingCountHolder.getSandboxMaxPromptTokenCount() != null) { properties.put("sandboxMaxPromptTokenCount", - tokenBaseThrottlingCountHolder.getSandboxMaxPromptTokenCount()); + tokenBasedThrottlingCountHolder.getSandboxMaxPromptTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount() != null) { + if (tokenBasedThrottlingCountHolder.getSandboxMaxCompletionTokenCount() != null) { properties.put("sandboxMaxCompletionTokenCount", - tokenBaseThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); + tokenBasedThrottlingCountHolder.getSandboxMaxCompletionTokenCount()); } - if (tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount() != null) { + if (tokenBasedThrottlingCountHolder.getSandboxMaxTotalTokenCount() != null) { properties.put("sandboxMaxTotalTokenCount", - tokenBaseThrottlingCountHolder.getSandboxMaxTotalTokenCount()); + tokenBasedThrottlingCountHolder.getSandboxMaxTotalTokenCount()); } } - vtb.addHandler("org.wso2.carbon.apimgt.gateway.handlers.throttling.ThrottleHandler" , properties); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/APIMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/APIMappingUtil.java index d844355e92aa..153a049e8af7 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/APIMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/APIMappingUtil.java @@ -37,7 +37,6 @@ import org.wso2.carbon.apimgt.api.ExceptionCodes; import org.wso2.carbon.apimgt.api.WorkflowStatus; import org.wso2.carbon.apimgt.api.dto.APIEndpointValidationDTO; -import org.wso2.carbon.apimgt.api.model.AIConfiguration; import org.wso2.carbon.apimgt.api.model.API; import org.wso2.carbon.apimgt.api.model.APICategory; import org.wso2.carbon.apimgt.api.model.APIIdentifier; @@ -48,6 +47,7 @@ import org.wso2.carbon.apimgt.api.model.APIRevision; import org.wso2.carbon.apimgt.api.model.APIRevisionDeployment; import org.wso2.carbon.apimgt.api.model.APIStateChangeResponse; +import org.wso2.carbon.apimgt.api.model.BackendThrottlingConfiguration; import org.wso2.carbon.apimgt.api.model.CORSConfiguration; import org.wso2.carbon.apimgt.api.model.LifeCycleEvent; import org.wso2.carbon.apimgt.api.model.Mediation; @@ -151,6 +151,7 @@ import static org.wso2.carbon.apimgt.impl.utils.APIUtil.getDefaultWebsubSubscriptionConfiguration; import static org.wso2.carbon.apimgt.impl.utils.APIUtil.handleException; +import static org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.PublisherCommonUtils.buildThrottlingConfiguration; import static org.wso2.carbon.apimgt.rest.api.publisher.v1.common.mappings.PublisherCommonUtils.buildThrottlingConfigurationDTO; /** @@ -474,7 +475,7 @@ public static API fromDTOtoAPI(APIDTO dto, String provider) throws APIManagement } if (dto.getAiConfiguration() != null) { model.setAiConfiguration( - PublisherCommonUtils.convertToAiConfiguration(dto.getAiConfiguration(), dto.getMaxTps())); + PublisherCommonUtils.convertToAiConfiguration(dto.getAiConfiguration())); } return model; } @@ -1000,20 +1001,35 @@ private static String updateContextWithVersion(String version, String contextVal private static void setMaxTpsFromApiDTOToModel(APIDTO dto, API api) { APIMaxTpsDTO maxTpsDTO = dto.getMaxTps(); + BackendThrottlingConfiguration backendThrottlingConfiguration = new BackendThrottlingConfiguration(); if (maxTpsDTO != null) { if (maxTpsDTO.getProduction() != null) { api.setProductionMaxTps(maxTpsDTO.getProduction().toString()); + backendThrottlingConfiguration.setProductionMaxTps(maxTpsDTO.getProduction().toString()); } if (maxTpsDTO.getProductionTimeUnit() != null) { api.setProductionTimeUnit(getTimeUnitInMilliseconds(maxTpsDTO.getProductionTimeUnit().toString())); + backendThrottlingConfiguration.setProductionTimeUnit( + getTimeUnitInMilliseconds(maxTpsDTO.getProductionTimeUnit().toString())); } if (maxTpsDTO.getSandbox() != null) { api.setSandboxMaxTps(maxTpsDTO.getSandbox().toString()); + backendThrottlingConfiguration.setSandboxMaxTps(maxTpsDTO.getSandbox().toString()); } if (maxTpsDTO.getSandboxTimeUnit() != null) { api.setSandboxTimeUnit(getTimeUnitInMilliseconds(maxTpsDTO.getSandboxTimeUnit().toString())); + backendThrottlingConfiguration.setSandboxTimeUnit( + getTimeUnitInMilliseconds(maxTpsDTO.getSandboxTimeUnit().toString())); + } + APIMaxTpsTokenBasedThrottlingConfigurationDTO tokenBasedThrottlingConfigurationDTO + = maxTpsDTO.getTokenBasedThrottlingConfiguration(); + if (tokenBasedThrottlingConfigurationDTO != null + && tokenBasedThrottlingConfigurationDTO.isIsTokenBasedThrottlingEnabled()) { + backendThrottlingConfiguration.setTokenBasedThrottlingConfiguration( + buildThrottlingConfiguration(tokenBasedThrottlingConfigurationDTO)); } } + api.setBackendThrottlingConfiguration(backendThrottlingConfiguration); } /** @@ -1060,8 +1076,7 @@ public static APIDTO fromAPItoDTO(API model, APIProvider apiProvider) } public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, - APIProvider apiProviderParam) - throws APIManagementException { + APIProvider apiProviderParam) throws APIManagementException { APIProvider apiProvider; if (apiProviderParam != null) { @@ -1154,7 +1169,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, new String(cryptoUtil.base64DecodeAndDecrypt(clientSecret))); } } - if (APIConstants.ENDPOINT_SECURITY_TYPE_API_KEY.equals(productionEndpointType)) { String apiKeyValue = (String) productionEndpointSecurity .get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE); @@ -1163,7 +1177,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, new String(cryptoUtil.base64DecodeAndDecrypt(apiKeyValue))); } } - endpointSecurity.put(APIConstants.OAuthConstants.ENDPOINT_SECURITY_PRODUCTION, productionEndpointSecurity); endpointConfigJson.put(APIConstants.ENDPOINT_SECURITY, endpointSecurity); @@ -1173,7 +1186,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, .get(APIConstants.OAuthConstants.ENDPOINT_SECURITY_SANDBOX); String sandboxEndpointType = (String) sandboxEndpointSecurity .get(APIConstants.OAuthConstants.ENDPOINT_SECURITY_TYPE); - if (sandboxEndpointSecurity .get(APIConstants.OAuthConstants.OAUTH_CUSTOM_PARAMETERS) != null) { String customParametersString = (String) sandboxEndpointSecurity @@ -1182,7 +1194,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, sandboxEndpointSecurity.put( APIConstants.OAuthConstants.OAUTH_CUSTOM_PARAMETERS, customParameters); } - if (APIConstants.OAuthConstants.OAUTH.equals(sandboxEndpointType)) { String clientSecret = (String) sandboxEndpointSecurity .get(APIConstants.OAuthConstants.OAUTH_CLIENT_SECRET); @@ -1192,7 +1203,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, new String(cryptoUtil.base64DecodeAndDecrypt(clientSecret))); } } - if (APIConstants.ENDPOINT_SECURITY_TYPE_API_KEY.equals(sandboxEndpointType)) { String apiKeyValue = (String) sandboxEndpointSecurity .get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE); @@ -1201,7 +1211,6 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials, new String(cryptoUtil.base64DecodeAndDecrypt(apiKeyValue))); } } - endpointSecurity.put(APIConstants.OAuthConstants.ENDPOINT_SECURITY_SANDBOX, sandboxEndpointSecurity); endpointConfigJson.put(APIConstants.ENDPOINT_SECURITY, endpointSecurity); @@ -1690,12 +1699,14 @@ private static void setMaxTpsFromModelToApiDTO(API api, APIDTO dto) { maxTpsDTO.setSandboxTimeUnit(APIMaxTpsDTO.SandboxTimeUnitEnum.valueOf( convertFromMilliseconds(api.getSandboxTimeUnit()))); } - AIConfiguration aiConfiguration = api.getAiConfiguration(); - if (aiConfiguration != null - && aiConfiguration.getTokenBasedThrottlingConfiguration() != null - && aiConfiguration.getTokenBasedThrottlingConfiguration().isTokenBasedThrottlingEnabled()) { + BackendThrottlingConfiguration backendThrottlingConfiguration = api.getBackendThrottlingConfiguration(); + if (backendThrottlingConfiguration != null + && backendThrottlingConfiguration.getTokenBasedThrottlingConfiguration() != null + && backendThrottlingConfiguration.getTokenBasedThrottlingConfiguration() + .isTokenBasedThrottlingEnabled()) { APIMaxTpsTokenBasedThrottlingConfigurationDTO throttlingConfigurationsDTO - = buildThrottlingConfigurationDTO(aiConfiguration.getTokenBasedThrottlingConfiguration()); + = buildThrottlingConfigurationDTO(backendThrottlingConfiguration + .getTokenBasedThrottlingConfiguration()); maxTpsDTO.setTokenBasedThrottlingConfiguration(throttlingConfigurationsDTO); } dto.setMaxTps(maxTpsDTO); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java index 420b324cf873..4363a8440fe1 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java @@ -48,7 +48,7 @@ import org.wso2.carbon.apimgt.api.ErrorHandler; import org.wso2.carbon.apimgt.api.ExceptionCodes; import org.wso2.carbon.apimgt.api.FaultGatewaysException; -import org.wso2.carbon.apimgt.api.TokenBaseThrottlingCountHolder; +import org.wso2.carbon.apimgt.api.TokenBasedThrottlingCountHolder; import org.wso2.carbon.apimgt.api.doc.model.APIResource; import org.wso2.carbon.apimgt.api.model.AIConfiguration; import org.wso2.carbon.apimgt.api.model.API; @@ -87,7 +87,6 @@ import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIInfoAdditionalPropertiesMapDTO; -import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMaxTpsDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIMaxTpsTokenBasedThrottlingConfigurationDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO; import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO; @@ -314,8 +313,9 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A .get(APIConstants.OAuthConstants.OAUTH_CLIENT_SECRET).toString(); } else if (oldEndpointSecurityProduction .get(APIConstants.ENDPOINT_SECURITY_API_KEY_IDENTIFIER) != null - && oldEndpointSecurityProduction.get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE) - != null) { + && oldEndpointSecurityProduction.get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE) != null + && oldEndpointSecurityProduction + .get(APIConstants.ENDPOINT_SECURITY_API_KEY_IDENTIFIER_TYPE) != null) { oldProductionApiKeyValue = oldEndpointSecurityProduction .get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE).toString(); } @@ -331,8 +331,9 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A oldSandboxApiSecret = oldEndpointSecuritySandbox .get(APIConstants.OAuthConstants.OAUTH_CLIENT_SECRET).toString(); } else if (oldEndpointSecuritySandbox.get(APIConstants.ENDPOINT_SECURITY_API_KEY_IDENTIFIER) != null - && oldEndpointSecuritySandbox.get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE) - != null) { + && oldEndpointSecuritySandbox.get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE) != null + && oldEndpointSecuritySandbox + .get(APIConstants.ENDPOINT_SECURITY_API_KEY_IDENTIFIER_TYPE) != null) { oldSandboxApiKeyValue = oldEndpointSecuritySandbox .get(APIConstants.ENDPOINT_SECURITY_API_KEY_VALUE).toString(); } @@ -1596,7 +1597,7 @@ public static API prepareToCreateAPIByDTO(APIDTO body, APIProvider apiProvider, apiToAdd.setOrganization(organization); apiToAdd.setGatewayType(body.getGatewayType()); if (body.getAiConfiguration() != null) { - apiToAdd.setAiConfiguration(convertToAiConfiguration(body.getAiConfiguration(), body.getMaxTps())); + apiToAdd.setAiConfiguration(convertToAiConfiguration(body.getAiConfiguration())); apiToAdd.setSubtype(APIConstants.API_SUBTYPE_AI_API); } else { apiToAdd.setSubtype(APIConstants.API_SUBTYPE_DEFAULT); @@ -1612,30 +1613,24 @@ public static API prepareToCreateAPIByDTO(APIDTO body, APIProvider apiProvider, * @return The converted AIConfiguration object. * @throws APIManagementException If an error occurs during the conversion. */ - public static AIConfiguration convertToAiConfiguration(APIAiConfigurationDTO dto, APIMaxTpsDTO maxTpsDTO) { + public static AIConfiguration convertToAiConfiguration(APIAiConfigurationDTO dto) { AIConfiguration aiConfiguration = new AIConfiguration(); aiConfiguration.setLlmProviderName(dto.getLlmProviderName()); aiConfiguration.setLlmProviderApiVersion(dto.getLlmProviderApiVersion()); - if (maxTpsDTO != null - && maxTpsDTO.getTokenBasedThrottlingConfiguration().isIsTokenBasedThrottlingEnabled()) { - TokenBaseThrottlingCountHolder throttlingConfig = buildThrottlingConfiguration(maxTpsDTO); - aiConfiguration.setTokenBasedThrottlingConfiguration(throttlingConfig); - } - return aiConfiguration; } /** - * Builds the throttling configuration from APIAiConfigurationDTO. + * Builds the token based throttling configuration from APIMaxTpsTokenBasedThrottlingConfigurationDTO. * - * @param dto The APIAiConfigurationDTO to extract data from. - * @return The TokenBaseThrottlingCountHolder object. + * @param throttlingConfigDTO The APIMaxTpsTokenBasedThrottlingConfigurationDTO to extract data from. + * @return The TokenBasedThrottlingCountHolder object. */ - private static TokenBaseThrottlingCountHolder buildThrottlingConfiguration(APIMaxTpsDTO dto) { + public static TokenBasedThrottlingCountHolder buildThrottlingConfiguration ( + APIMaxTpsTokenBasedThrottlingConfigurationDTO throttlingConfigDTO) { - APIMaxTpsTokenBasedThrottlingConfigurationDTO throttlingConfigDTO = dto.getTokenBasedThrottlingConfiguration(); - TokenBaseThrottlingCountHolder throttlingConfig = new TokenBaseThrottlingCountHolder(); + TokenBasedThrottlingCountHolder throttlingConfig = new TokenBasedThrottlingCountHolder(); if (throttlingConfigDTO.getProductionMaxPromptTokenCount() != null) { throttlingConfig.setProductionMaxPromptTokenCount( @@ -1687,7 +1682,7 @@ public static APIAiConfigurationDTO convertToApiAiConfigurationDTO(AIConfigurati * @return The built APIAiConfigurationThrottlingConfigurationDTO object. */ public static APIMaxTpsTokenBasedThrottlingConfigurationDTO buildThrottlingConfigurationDTO( - TokenBaseThrottlingCountHolder throttlingConfig) { + TokenBasedThrottlingCountHolder throttlingConfig) { APIMaxTpsTokenBasedThrottlingConfigurationDTO throttlingConfigurationsDTO = new APIMaxTpsTokenBasedThrottlingConfigurationDTO(); diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/db2.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/db2.sql index 457892dcc732..53cd1a656d93 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/db2.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/db2.sql @@ -3292,8 +3292,6 @@ CREATE TABLE AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql index 4e34d6541e27..be3964c8d3a0 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/h2.sql @@ -2485,8 +2485,6 @@ CREATE TABLE IF NOT EXISTS AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql index 26fa5627cf71..4bba6df72ba4 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mssql.sql @@ -2771,8 +2771,6 @@ CREATE TABLE AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql index 0e90cc8e76cb..5cd59479688a 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql.sql @@ -2519,8 +2519,6 @@ CREATE TABLE IF NOT EXISTS AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql_cluster.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql_cluster.sql index 04a32a0e225c..a3124cdb92c3 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql_cluster.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/mysql_cluster.sql @@ -2671,8 +2671,6 @@ CREATE TABLE IF NOT EXISTS AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql index 8a8000dac571..0ac850bbab5c 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle.sql @@ -3873,8 +3873,6 @@ CREATE TABLE AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql index 6d9ee5f2cd47..e59a90c6fa81 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_23c.sql @@ -3873,8 +3873,6 @@ CREATE TABLE AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql index b8ae243eac84..a28d2bfa6eb8 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/oracle_rac.sql @@ -3844,8 +3844,6 @@ CREATE TABLE AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID) diff --git a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql index 19a3c8559568..32cca8323481 100644 --- a/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql +++ b/features/apimgt/org.wso2.carbon.apimgt.core.feature/src/main/resources/sql/postgresql.sql @@ -2860,8 +2860,6 @@ CREATE TABLE IF NOT EXISTS AM_API_AI_CONFIGURATION ( API_UUID VARCHAR(256) NOT NULL, API_REVISION_UUID VARCHAR(255), LLM_PROVIDER_UUID VARCHAR(255) NOT NULL, - ENDPOINT_CONFIGURATION VARCHAR(2048) NOT NULL, - THROTTLING_CONFIGURATIONS VARCHAR(1024) NOT NULL, PRIMARY KEY (AI_CONFIGURATION_UUID), FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID), FOREIGN KEY (LLM_PROVIDER_UUID) REFERENCES AM_LLM_PROVIDER(UUID)