Skip to content

Commit 5144e49

Browse files
authored
Merge pull request #13040 from ashera96/master
Fix AI API versioning issue
2 parents 862ab39 + d32c4f8 commit 5144e49

File tree

24 files changed

+219
-99
lines changed

24 files changed

+219
-99
lines changed

Diff for: components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/APIProvider.java

+22
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,17 @@ APIEndpointInfo getAPIEndpointByUUID(String apiUUID, String endpointUUID, String
16211621
String addAPIEndpoint(String apiUUID, APIEndpointInfo apiEndpoint, String organization)
16221622
throws APIManagementException;
16231623

1624+
/**
1625+
* Add endpoints to the provided API
1626+
*
1627+
* @param apiUUID Unique identifier of API
1628+
* @param apiEndpointList List of API endpoint details
1629+
* @param organization Organization name
1630+
* @throws APIManagementException if an error occurs while adding the endpoints
1631+
*/
1632+
void addAPIEndpoints(String apiUUID, List<APIEndpointInfo> apiEndpointList, String organization)
1633+
throws APIManagementException;
1634+
16241635
/**
16251636
* Delete endpoint by providing the endpoint UUID.
16261637
*
@@ -1657,6 +1668,17 @@ APIEndpointInfo updateAPIEndpoint(String apiId, APIEndpointInfo apiEndpoint, Str
16571668
*/
16581669
void deleteAPIEndpointsByApiUUID(String apiId) throws APIManagementException;
16591670

1671+
/**
1672+
* Add primary endpoint mappings to new API version
1673+
*
1674+
* @param existingApiId Existing API UUID
1675+
* @param newApiId New API UUID
1676+
* @param organization organization
1677+
* @throws APIManagementException if an error occurs while adding primary endpoint mappings
1678+
*/
1679+
void addPrimaryEndpointMappingsToNewAPI(String existingApiId, String newApiId, String organization)
1680+
throws APIManagementException;
1681+
16601682
/**
16611683
* Set existing operation policy mapping to the URI Templates
16621684
*

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -3316,7 +3316,8 @@ public static class TransactionCounter {
33163316
public static class APIEndpoint {
33173317
public static final String PRODUCTION = "PRODUCTION";
33183318
public static final String SANDBOX = "SANDBOX";
3319-
public static final String PRIMARY_ENDPOINT_ID_SEPARATOR = "--";
3319+
public static final String DEFAULT_PROD_ENDPOINT_ID = "default_production_endpoint";
3320+
public static final String DEFAULT_SANDBOX_ENDPOINT_ID = "default_sandbox_endpoint";
33203321
public static final String DEFAULT_PROD_ENDPOINT_NAME = "Default Production Endpoint";
33213322
public static final String DEFAULT_SANDBOX_ENDPOINT_NAME = "Default Sandbox Endpoint";
33223323
public static final String ENDPOINT_CONFIG_PRODUCTION_ENDPOINTS = "production_endpoints";

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIProviderImpl.java

+36-10
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ private void addAPI(API api, int tenantId) throws APIManagementException {
630630
String primarySandboxEndpointId = api.getPrimarySandboxEndpointId();
631631
if (primarySandboxEndpointId == null && primaryProductionEndpointId == null) {
632632
addDefaultPrimaryEndpoints(api, true, false);
633+
} else {
634+
boolean isProductionEndpointFromAPIEndpointConfig = APIConstants.APIEndpoint.DEFAULT_PROD_ENDPOINT_ID.equals(
635+
primaryProductionEndpointId);
636+
boolean isSandboxEndpointFromAPIEndpointConfig = APIConstants.APIEndpoint.DEFAULT_SANDBOX_ENDPOINT_ID.equals(
637+
primarySandboxEndpointId);
638+
if (isProductionEndpointFromAPIEndpointConfig && isSandboxEndpointFromAPIEndpointConfig) {
639+
addDefaultPrimaryEndpoints(api, true, true);
640+
} else if (isProductionEndpointFromAPIEndpointConfig) {
641+
addDefaultPrimaryEndpoints(api, true, false);
642+
} else if (isSandboxEndpointFromAPIEndpointConfig) {
643+
addDefaultPrimaryEndpoints(api, false, true);
644+
}
633645
}
634646
}
635647

@@ -1178,12 +1190,10 @@ private void updateAPIPrimaryEndpointsMapping(API api) throws APIManagementExcep
11781190
if (primarySandboxEndpointId == null && primaryProductionEndpointId == null) {
11791191
addDefaultPrimaryEndpoints(api, true, false);
11801192
} else {
1181-
boolean isProductionEndpointFromAPIEndpointConfig = primaryProductionEndpointId != null &&
1182-
primaryProductionEndpointId.endsWith(APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR +
1183-
APIConstants.APIEndpoint.PRODUCTION);
1184-
boolean isSandboxEndpointFromAPIEndpointConfig = primarySandboxEndpointId != null &&
1185-
primarySandboxEndpointId.endsWith(APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR +
1186-
APIConstants.APIEndpoint.SANDBOX);
1193+
boolean isProductionEndpointFromAPIEndpointConfig = APIConstants.APIEndpoint.DEFAULT_PROD_ENDPOINT_ID.equals(
1194+
primaryProductionEndpointId);
1195+
boolean isSandboxEndpointFromAPIEndpointConfig = APIConstants.APIEndpoint.DEFAULT_SANDBOX_ENDPOINT_ID.equals(
1196+
primarySandboxEndpointId);
11871197

11881198
if (isProductionEndpointFromAPIEndpointConfig && isSandboxEndpointFromAPIEndpointConfig) {
11891199
addDefaultPrimaryEndpoints(api, true, true);
@@ -2234,6 +2244,11 @@ public API createNewAPIVersion(String existingApiId, String newVersion, Boolean
22342244
}
22352245
}
22362246

2247+
// copy endpoints and endpoint mappings
2248+
List<APIEndpointInfo> existingEndpointList = getAllAPIEndpointsByUUID(existingApiId, organization);
2249+
addAPIEndpoints(newAPIId, existingEndpointList, organization);
2250+
addPrimaryEndpointMappingsToNewAPI(existingApiId, newAPIId, organization);
2251+
22372252
// copy icon
22382253
ResourceFile icon = getIcon(existingApiId, organization);
22392254
if (icon != null) {
@@ -6047,6 +6062,7 @@ public Map<String, Object> searchPaginatedContent(String searchQuery, String org
60476062
return result;
60486063
}
60496064

6065+
60506066
@Override
60516067
public void setThumbnailToAPI(String apiId, ResourceFile resource, String organization) throws APIManagementException {
60526068

@@ -8231,6 +8247,12 @@ public String addAPIEndpoint(String apiUUID, APIEndpointInfo apiEndpoint, String
82318247
return apiMgtDAO.addAPIEndpoint(apiUUID, apiEndpoint, organization);
82328248
}
82338249

8250+
@Override
8251+
public void addAPIEndpoints(String apiUUID, List<APIEndpointInfo> apiEndpointList, String organization)
8252+
throws APIManagementException {
8253+
apiMgtDAO.addAPIEndpoints(apiUUID, apiEndpointList, organization);
8254+
}
8255+
82348256
@Override
82358257
public APIEndpointInfo getAPIEndpointByUUID(String apiUUID, String endpointUUID, String organization)
82368258
throws APIManagementException {
@@ -8253,6 +8275,12 @@ public void deleteAPIEndpointsByApiUUID(String apiId) throws APIManagementExcept
82538275
apiMgtDAO.deleteAPIEndpointsByApiUUID(apiId);
82548276
}
82558277

8278+
@Override
8279+
public void addPrimaryEndpointMappingsToNewAPI(String existingApiId, String newApiId, String organization)
8280+
throws APIManagementException {
8281+
apiMgtDAO.addPrimaryEndpointMappingsToNewAPI(existingApiId, newApiId, organization);
8282+
}
8283+
82568284
@Override
82578285
public void deleteAPIEndpointById(String endpointUUID) throws APIManagementException {
82588286
apiMgtDAO.deleteAPIEndpointByEndpointId(endpointUUID);
@@ -8287,11 +8315,9 @@ private void populateAPIPrimaryEndpointsMapping(API api, String uuid) throws API
82878315
List<String> endpointIds = apiMgtDAO.getPrimaryEndpointUUIDByAPIId(currentApiUuid);
82888316
if (endpointIds != null && !endpointIds.isEmpty()) {
82898317
for (String endpointId : endpointIds) {
8290-
if (endpointId.equals(
8291-
currentApiUuid + APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR + APIConstants.APIEndpoint.PRODUCTION)) {
8318+
if (APIConstants.APIEndpoint.DEFAULT_PROD_ENDPOINT_ID.equals(endpointId)) {
82928319
api.setPrimaryProductionEndpointId(endpointId);
8293-
} else if (endpointId.equals(
8294-
currentApiUuid + APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR + APIConstants.APIEndpoint.SANDBOX)) {
8320+
} else if (APIConstants.APIEndpoint.DEFAULT_SANDBOX_ENDPOINT_ID.equals(endpointId)) {
82958321
api.setPrimarySandboxEndpointId(endpointId);
82968322
}
82978323
}

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/ApiMgtDAO.java

+87-11
Original file line numberDiff line numberDiff line change
@@ -24548,6 +24548,42 @@ public void deleteAPIEndpointsByApiUUID(String apiUUID) throws APIManagementExce
2454824548
}
2454924549
}
2455024550

24551+
/**
24552+
* Add primary endpoint mappings to new API version
24553+
*
24554+
* @param existingApiUUID Existing API UUID
24555+
* @param newApiUUID New API UUID
24556+
* @param organization Organization
24557+
* @throws APIManagementException if an error occurs while adding primary endpoint mappings
24558+
*/
24559+
public void addPrimaryEndpointMappingsToNewAPI(String existingApiUUID, String newApiUUID, String organization)
24560+
throws APIManagementException {
24561+
try (Connection connection = APIMgtDBUtil.getConnection()) {
24562+
connection.setAutoCommit(false);
24563+
try (PreparedStatement addPrimaryMapping = connection.prepareStatement(
24564+
SQLConstants.APIEndpointsSQLConstants.ADD_PRIMARY_ENDPOINT_MAPPING);
24565+
PreparedStatement getPrimaryEpMappingsStmt = connection.prepareStatement(
24566+
SQLConstants.APIEndpointsSQLConstants.GET_API_PRIMARY_ENDPOINT_UUIDS_BY_API_UUID)) {
24567+
getPrimaryEpMappingsStmt.setString(1, existingApiUUID);
24568+
getPrimaryEpMappingsStmt.setString(2, organization);
24569+
try (ResultSet resultSet = getPrimaryEpMappingsStmt.executeQuery()) {
24570+
while(resultSet.next()) {
24571+
addPrimaryMapping.setString(1, newApiUUID);
24572+
addPrimaryMapping.setString(2, resultSet.getString("ENDPOINT_UUID"));
24573+
addPrimaryMapping.addBatch();
24574+
}
24575+
addPrimaryMapping.executeBatch();
24576+
}
24577+
connection.commit();
24578+
} catch (SQLException e) {
24579+
connection.rollback();
24580+
handleException("Error while adding primary endpoint mappings to API : " + newApiUUID, e);
24581+
}
24582+
} catch (SQLException e) {
24583+
handleException("Error while adding primary endpoint mappings to API : " + newApiUUID, e);
24584+
}
24585+
}
24586+
2455124587
/**
2455224588
* Update endpoint using the provided apiEndpoint object.
2455324589
*
@@ -24576,7 +24612,7 @@ public APIEndpointInfo updateAPIEndpoint(String apiUUID, APIEndpointInfo apiEndp
2457624612
} else {
2457724613
// Update API Endpoint
2457824614
try (Connection connection = APIMgtDBUtil.getConnection()) {
24579-
apiEndpointUpdated = updateAPIEndpoint(connection, endpointUUID, apiEndpoint, organization);
24615+
apiEndpointUpdated = updateAPIEndpoint(connection, apiUUID, endpointUUID, apiEndpoint, organization);
2458024616
} catch (SQLException e) {
2458124617
handleException("Failed to update the endpoint with ID " + endpointUUID, e);
2458224618
}
@@ -24588,22 +24624,24 @@ public APIEndpointInfo updateAPIEndpoint(String apiUUID, APIEndpointInfo apiEndp
2458824624
* Update API endpoint with provided UUID
2458924625
*
2459024626
* @param connection DB connection
24627+
* @param apiUUID API UUID
2459124628
* @param endpointUUID Endpoint identifier
2459224629
* @param apiEndpoint Endpoint content
2459324630
* @param organization Organization
2459424631
* @return Updated endpoint content
2459524632
* @throws SQLException if an SQL error occurs while updating API endpoint
2459624633
* @throws APIManagementException if an error occurs while updating API endpoint
2459724634
*/
24598-
private APIEndpointInfo updateAPIEndpoint(Connection connection, String endpointUUID, APIEndpointInfo apiEndpoint,
24599-
String organization) throws SQLException, APIManagementException {
24635+
private APIEndpointInfo updateAPIEndpoint(Connection connection, String apiUUID, String endpointUUID,
24636+
APIEndpointInfo apiEndpoint, String organization) throws SQLException, APIManagementException {
2460024637
connection.setAutoCommit(false);
2460124638
try (PreparedStatement statement = connection.prepareStatement(
2460224639
SQLConstants.APIEndpointsSQLConstants.UPDATE_API_ENDPOINT_BY_UUID)) {
2460324640
statement.setString(1, apiEndpoint.getName());
2460424641
statement.setBinaryStream(2, fromEndpointConfigMapToBA(apiEndpoint.getEndpointConfig()));
2460524642
statement.setString(3, endpointUUID);
24606-
statement.setString(4, organization);
24643+
statement.setString(4, apiUUID);
24644+
statement.setString(5, organization);
2460724645
if (statement.executeUpdate() > 0) {
2460824646
return apiEndpoint;
2460924647
}
@@ -24622,7 +24660,7 @@ private APIEndpointInfo updateAPIEndpoint(Connection connection, String endpoint
2462224660
* @param apiEndpoint Endpoint content
2462324661
* @param organization Organization
2462424662
* @return UUID of the added API endpoint
24625-
* @throws APIManagementException if failed to add API endpoint
24663+
* @throws APIManagementException if failed to add API endpoint
2462624664
*/
2462724665
public String addAPIEndpoint(String apiUUID, APIEndpointInfo apiEndpoint, String organization)
2462824666
throws APIManagementException {
@@ -24658,6 +24696,44 @@ private String addAPIEndpoint(String apiUUID, Connection connection, APIEndpoint
2465824696
return null;
2465924697
}
2466024698

24699+
/**
24700+
* Add endpoints to the API
24701+
*
24702+
* @param apiUUID UUID of API
24703+
* @param apiEndpointList List of endpoints to be added
24704+
* @param organization Organization
24705+
* @throws APIManagementException if failed to add endpoints
24706+
*/
24707+
public void addAPIEndpoints(String apiUUID, List<APIEndpointInfo> apiEndpointList, String organization)
24708+
throws APIManagementException {
24709+
if (!apiEndpointList.isEmpty()) {
24710+
try (Connection connection = APIMgtDBUtil.getConnection()) {
24711+
connection.setAutoCommit(false);
24712+
String dbQuery = SQLConstants.APIEndpointsSQLConstants.ADD_NEW_API_ENDPOINT;
24713+
try (PreparedStatement statement = connection.prepareStatement(dbQuery)) {
24714+
for (APIEndpointInfo apiEndpoint : apiEndpointList) {
24715+
statement.setString(1, apiUUID);
24716+
statement.setString(2, apiEndpoint.getId());
24717+
statement.setString(3, "Current API");
24718+
statement.setString(4, apiEndpoint.getName());
24719+
statement.setString(5, apiEndpoint.getDeploymentStage());
24720+
statement.setBinaryStream(6,
24721+
fromEndpointConfigMapToBA(apiEndpoint.getEndpointConfig()));
24722+
statement.setString(7, organization);
24723+
statement.addBatch();
24724+
}
24725+
statement.executeBatch();
24726+
connection.commit();
24727+
} catch (SQLException e) {
24728+
connection.rollback();
24729+
handleException("Failed to add endpoints to API: " + apiUUID, e);
24730+
}
24731+
} catch (SQLException e) {
24732+
handleException("Failed to add endpoints to API: " + apiUUID, e);
24733+
}
24734+
}
24735+
}
24736+
2466124737
/**
2466224738
* Add API primary endpoint mappings
2466324739
*
@@ -24788,21 +24864,21 @@ public void addDefaultPrimaryEndpointMappings(API api, boolean isProductionEndpo
2478824864
if (isProductionEndpoint) {
2478924865
// add primary production endpoint mapping
2479024866
addPrimaryMapping.setString(1, apiUUID);
24791-
addPrimaryMapping.setString(2,
24792-
apiUUID + APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR + APIConstants.APIEndpoint.PRODUCTION);
24867+
addPrimaryMapping.setString(2, APIConstants.APIEndpoint.DEFAULT_PROD_ENDPOINT_ID);
2479324868
addPrimaryMapping.addBatch();
2479424869
}
2479524870

2479624871
if (isSandboxEndpoint) {
2479724872
// add primary sandbox endpoint mapping
2479824873
addPrimaryMapping.setString(1, apiUUID);
24799-
addPrimaryMapping.setString(2,
24800-
apiUUID + APIConstants.APIEndpoint.PRIMARY_ENDPOINT_ID_SEPARATOR + APIConstants.APIEndpoint.SANDBOX);
24874+
addPrimaryMapping.setString(2, APIConstants.APIEndpoint.DEFAULT_SANDBOX_ENDPOINT_ID);
2480124875
addPrimaryMapping.addBatch();
2480224876
}
2480324877

24804-
addPrimaryMapping.executeBatch();
24805-
connection.commit();
24878+
if (isProductionEndpoint || isSandboxEndpoint) {
24879+
addPrimaryMapping.executeBatch();
24880+
connection.commit();
24881+
}
2480624882
} catch (SQLException e) {
2480724883
connection.rollback();
2480824884
handleException("Error while adding primary endpoint mappings for API : " + apiUUID, e);

Diff for: components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/dao/constants/SQLConstants.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -4728,7 +4728,8 @@ public static class APIEndpointsSQLConstants {
47284728
public static final String UPDATE_API_ENDPOINT_BY_UUID =
47294729
"UPDATE AM_API_ENDPOINTS " +
47304730
" SET ENDPOINT_NAME = ?, ENDPOINT_CONFIG = ? " +
4731-
" WHERE ENDPOINT_UUID = ? AND ORGANIZATION = ? AND REVISION_UUID = 'Current API'";
4731+
" WHERE ENDPOINT_UUID = ? AND API_UUID = ? AND ORGANIZATION = ? " +
4732+
" AND REVISION_UUID = 'Current API'";
47324733

47334734
public static final String ADD_NEW_API_ENDPOINT =
47344735
"INSERT INTO AM_API_ENDPOINTS " +
@@ -4749,10 +4750,19 @@ public static class APIEndpointsSQLConstants {
47494750
public static final String GET_PRIMARY_ENDPOINT_MAPPINGS =
47504751
"SELECT ENDPOINT_UUID FROM AM_API_PRIMARY_EP_MAPPING WHERE API_UUID = ?";
47514752

4753+
public static final String GET_API_PRIMARY_ENDPOINT_UUIDS_BY_API_UUID =
4754+
"SELECT AME.ENDPOINT_UUID " +
4755+
"FROM AM_API_ENDPOINTS AME INNER JOIN AM_API_PRIMARY_EP_MAPPING AMPM " +
4756+
"ON (AMPM.ENDPOINT_UUID = AME.ENDPOINT_UUID AND AMPM.API_UUID = AME.API_UUID) " +
4757+
"WHERE " +
4758+
"AME.API_UUID = ? " +
4759+
"AND AME.ORGANIZATION = ? " +
4760+
"AND AME.REVISION_UUID = 'Current API'";
4761+
47524762
public static final String GET_API_PRIMARY_ENDPOINT_UUID_BY_API_UUID_AND_KEY_TYPE =
47534763
"SELECT AME.ENDPOINT_UUID " +
47544764
"FROM AM_API_ENDPOINTS AME INNER JOIN AM_API_PRIMARY_EP_MAPPING AMPM " +
4755-
"ON AMPM.ENDPOINT_UUID = AME.ENDPOINT_UUID " +
4765+
"ON (AMPM.ENDPOINT_UUID = AME.ENDPOINT_UUID AND AMPM.API_UUID = AME.API_UUID) " +
47564766
"WHERE " +
47574767
"AME.API_UUID = ? " +
47584768
"AND AME.ORGANIZATION = ? " +
@@ -4762,7 +4772,7 @@ public static class APIEndpointsSQLConstants {
47624772
public static final String GET_API_PRIMARY_ENDPOINT_UUID_BY_API_UUID_AND_KEY_TYPE_REVISION =
47634773
"SELECT AME.ENDPOINT_UUID " +
47644774
"FROM AM_API_ENDPOINTS AME INNER JOIN AM_API_PRIMARY_EP_MAPPING AMPM " +
4765-
"ON AMPM.ENDPOINT_UUID = AME.ENDPOINT_UUID " +
4775+
"ON (AMPM.ENDPOINT_UUID = AME.ENDPOINT_UUID AND AMPM.API_UUID = AME.API_UUID) " +
47664776
"WHERE " +
47674777
"AME.API_UUID = ? " +
47684778
"AND AME.ORGANIZATION = ? " +

Diff for: components/apimgt/org.wso2.carbon.apimgt.keymgt/src/test/resources/dbscripts/h2.sql

+1-2
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,7 @@ CREATE TABLE IF NOT EXISTS AM_API_ENDPOINTS (
13191319
ENDPOINT_CONFIG LONGBLOB NOT NULL,
13201320
ORGANIZATION VARCHAR(100),
13211321
FOREIGN KEY (API_UUID) REFERENCES AM_API(API_UUID) ON DELETE CASCADE,
1322-
PRIMARY KEY (ENDPOINT_UUID, REVISION_UUID),
1323-
UNIQUE (API_UUID, ENDPOINT_NAME, REVISION_UUID, ORGANIZATION)
1322+
PRIMARY KEY (API_UUID, ENDPOINT_UUID, ENDPOINT_NAME, REVISION_UUID, ORGANIZATION)
13241323
);
13251324

13261325
CREATE TABLE IF NOT EXISTS AM_API_PRIMARY_EP_MAPPING (

0 commit comments

Comments
 (0)