Skip to content

Commit dd11942

Browse files
dakshina99msm1992
authored andcommitted
Add API - AWS API mapping, add gateway feature catalog to the publisher settings api
1 parent c27581a commit dd11942

File tree

13 files changed

+334
-34
lines changed

13 files changed

+334
-34
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -3203,8 +3203,10 @@ public enum ConfigType {
32033203

32043204

32053205
public static final String WSO2_GATEWAY_ENVIRONMENT = "wso2";
3206+
public static final String EXTERNAL_GATEWAY_VENDOR = "external";
32063207
public static final String WSO2_APK_GATEWAY = "wso2/apk";
32073208
public static final String WSO2_SYNAPSE_GATEWAY = "wso2/synapse";
3209+
public static final String AWS_GATEWAY = "AWS";
32083210

32093211
public static final String PERMISSION_ALLOW = "ALLOW";
32103212
public static final String PERMISSION_DENY = "DENY";

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

+40
Original file line numberDiff line numberDiff line change
@@ -15388,6 +15388,46 @@ public Environment updateEnvironment(Environment environment) throws APIManageme
1538815388
return environment;
1538915389
}
1539015390

15391+
/**
15392+
* Add API - AWS API mapping
15393+
*
15394+
* @param apiId API ID
15395+
* @param aWSApiId AWS API ID
15396+
* @param environmentId Gateway environment ID
15397+
* @throws APIManagementException if failed to add the mapping
15398+
*/
15399+
public void addApiAWSApiMapping(String apiId, String aWSApiId, String environmentId)
15400+
throws APIManagementException {
15401+
Connection connection = null;
15402+
PreparedStatement prepStmt = null;
15403+
String query = SQLConstants.ADD_API_AWS_API_MAPPING;
15404+
15405+
try {
15406+
connection = APIMgtDBUtil.getConnection();
15407+
connection.setAutoCommit(false);
15408+
15409+
prepStmt = connection.prepareStatement(query);
15410+
prepStmt.setString(1, apiId);
15411+
prepStmt.setString(2, aWSApiId);
15412+
prepStmt.setString(3, environmentId);
15413+
prepStmt.execute();
15414+
15415+
connection.commit();
15416+
} catch (SQLException e) {
15417+
try {
15418+
if (connection != null) {
15419+
connection.rollback();
15420+
}
15421+
} catch (SQLException ex) {
15422+
log.error("Failed to rollback the add API - AWS API Mapping: API ID: "
15423+
+ apiId + " AWS API ID: " + aWSApiId, ex);
15424+
}
15425+
handleException("Error while adding mapping between API ID: " + apiId + " and AWS API ID: " + aWSApiId, e);
15426+
} finally {
15427+
APIMgtDBUtil.closeAllConnections(prepStmt, connection, null);
15428+
}
15429+
}
15430+
1539115431
private boolean isEmptyValuesInApplicationAttributesEnabled() {
1539215432
return Boolean.parseBoolean(ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().
1539315433
getAPIManagerConfiguration().getFirstProperty(APIConstants.ApplicationAttributes.

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

+3
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,9 @@ public class SQLConstants {
27612761
"SET DISPLAY_NAME = ?, DESCRIPTION = ?, CONFIGURATION = ?" +
27622762
"WHERE UUID = ?";
27632763

2764+
public static final String ADD_API_AWS_API_MAPPING = "INSERT INTO AM_API_AWS_API_MAPPING " +
2765+
"(API_ID, AWS_API_ID, GATEWAY_ENV_ID) VALUES (?, ?, ?)";
2766+
27642767
public static final String ADD_GATEWAY_VISIBILITY_PERMISSION_SQL =
27652768
" INSERT INTO" +
27662769
" AM_GATEWAY_PERMISSIONS (GATEWAY_UUID, PERMISSIONS_TYPE, ROLE)" +

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/deployer/ExternalGatewayDeployer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public interface ExternalGatewayDeployer {
3535
* @param environment Environment to be deployed
3636
* @throws DeployerException if error occurs when deploying APIs to in the external gateway
3737
*/
38-
public boolean deploy(API api, Environment environment) throws DeployerException;
38+
public String deploy(API api, Environment environment) throws DeployerException;
3939

4040
/**
4141
* Undeploy API artifact from provided environment

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/notifier/ExternalGatewayNotifier.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class ExternalGatewayNotifier extends DeployAPIInGatewayNotifier {
4343

4444
@Override
4545
public boolean publishEvent(Event event) throws NotifierException {
46-
if (APIUtil.isAnyExternalGateWayProviderExists()) {
46+
if (APIUtil.isAnyExternalGateWayProviderExists(event.getTenantDomain())) {
4747
apiMgtDAO = ApiMgtDAO.getInstance();
4848
process(event);
4949
}
@@ -75,7 +75,7 @@ private void process(Event event) throws NotifierException {
7575
*/
7676
private void deployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws NotifierException {
7777

78-
boolean deployed;
78+
String deployedID;
7979
Set<String> gateways = deployAPIInGatewayEvent.getGatewayLabels();
8080
String apiId = deployAPIInGatewayEvent.getUuid();
8181

@@ -91,13 +91,18 @@ private void deployApi(DeployAPIInGatewayEvent deployAPIInGatewayEvent) throws N
9191
(environments.get(deploymentEnv).getProvider());
9292
if (deployer != null) {
9393
try {
94-
deployed = deployer.deploy(api, environments.get(deploymentEnv));
95-
if (!deployed) {
96-
throw new APIManagementException("Error while deploying API product to Solace broker");
94+
Environment gatewayEnvironment = environments.get(deploymentEnv);
95+
deployedID = deployer.deploy(api, gatewayEnvironment);
96+
if (deployedID == null) {
97+
throw new APIManagementException("Error while deploying API to the external gateway");
98+
}
99+
if (deployer.getType().equals(APIConstants.AWS_GATEWAY)) {
100+
APIUtil.addApiAWSApiMapping(apiId, deployedID, gatewayEnvironment.getUuid());
97101
}
98102
} catch (DeployerException e) {
99103
throw new APIManagementException(e.getMessage());
100104
}
105+
101106
}
102107
}
103108
}

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

+205-16
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,112 @@ public final class APIUtil {
355355
private static final String contextRegex = "^[a-zA-Z0-9_${}/.;()-]+$";
356356
private static String hashingAlgorithm = SHA_256;
357357

358+
private static final String SYNAPSE_GATEWAY_FEATURES = "{\n" +
359+
" \"runtime\": [\n" +
360+
" \"cors\",\n" +
361+
" \"schemaValidation\",\n" +
362+
" \"queryAnalysis\",\n" +
363+
" \"responseCaching\",\n" +
364+
" \"transportsHTTP\",\n" +
365+
" \"transportsHTTPS\",\n" +
366+
" \"transportsMutualSSL\",\n" +
367+
" \"oauth2\",\n" +
368+
" \"apikey\",\n" +
369+
" \"basicAuth\",\n" +
370+
" \"audienceValidation\",\n" +
371+
" \"backendThroughput\",\n" +
372+
" \"keyManagerConfig\"\n" +
373+
" ],\n" +
374+
" \"resources\": [\n" +
375+
" \"apiLevelRateLimiting\",\n" +
376+
" \"operationLevelRateLimiting\",\n" +
377+
" \"operationSecurity\"\n" +
378+
" ],\n" +
379+
" \"localScopes\": [\n" +
380+
" \"operationScopes\"\n" +
381+
" ],\n" +
382+
" \"monetization\": [\n" +
383+
" \"monetization\"\n" +
384+
" ],\n" +
385+
" \"subscriptions\": [\n" +
386+
" \"subscriptions\"\n" +
387+
" ],\n" +
388+
" \"endpoints\": [\n" +
389+
" \"http\",\n" +
390+
" \"service\",\n" +
391+
" \"address\",\n" +
392+
" \"default\",\n" +
393+
" \"INLINE\",\n" +
394+
" \"dynamic\",\n" +
395+
" \"awslambda\",\n" +
396+
" \"sequence_backend\",\n" +
397+
" \"advancedConfigurations\",\n" +
398+
" \"loadBalanceAndFailoverConfigurations\",\n" +
399+
" \"endpointSecurity\"\n" +
400+
" ]\n" +
401+
" }";
402+
private static final String APK_GATEWAY_FEATURES = "{\n" +
403+
" \"runtime\": [\n" +
404+
" \"cors\",\n" +
405+
" \"transportsHTTP\",\n" +
406+
" \"transportsHTTPS\",\n" +
407+
" \"transportsMutualSSL\",\n" +
408+
" \"oauth2\",\n" +
409+
" \"apikey\",\n" +
410+
" \"basicAuth\",\n" +
411+
" \"audienceValidation\"\n" +
412+
" ],\n" +
413+
" \"resources\": [\n" +
414+
" \"apiLevelRateLimiting\",\n" +
415+
" \"operationLevelRateLimiting\"\n" +
416+
" ],\n" +
417+
" \"localScopes\": [\n" +
418+
" \"operationScopes\"\n" +
419+
" ],\n" +
420+
" \"monetization\": [\n" +
421+
" \"monetization\"\n" +
422+
" ],\n" +
423+
" \"subscriptions\": [\n" +
424+
" \"subscriptions\"\n" +
425+
" ],\n" +
426+
" \"endpoints\": [\n" +
427+
" \"http\"\n" +
428+
" ]\n" +
429+
" }";
430+
private static final String API_DATA = "{\n" +
431+
" \"rest\": [\n" +
432+
" \"wso2/synapse\",\n" +
433+
" \"wso2/apk\",\n" +
434+
" \"AWS\"\n" +
435+
" ],\n" +
436+
" \"soap\": [\n" +
437+
" \"wso2/synapse\",\n" +
438+
" \"wso2/apk\"\n" +
439+
" ],\n" +
440+
" \"graphql\": [\n" +
441+
" \"wso2/synapse\",\n" +
442+
" \"wso2/apk\"\n" +
443+
" ],\n" +
444+
" \"ws\": [\n" +
445+
" \"wso2/synapse\",\n" +
446+
" \"wso2/apk\",\n" +
447+
" \"AWS\"\n" +
448+
" ],\n" +
449+
" \"wh\": [\n" +
450+
" \"wso2/synapse\",\n" +
451+
" \"wso2/apk\"\n" +
452+
" ],\n" +
453+
" \"sse\": [\n" +
454+
" \"wso2/synapse\",\n" +
455+
" \"wso2/apk\"\n" +
456+
" ],\n" +
457+
" \"ai\": [\n" +
458+
" \"wso2/synapse\",\n" +
459+
" \"wso2/apk\",\n" +
460+
" \"AWS\"\n" +
461+
" ]\n" +
462+
" }";
463+
358464
private APIUtil() {
359465

360466
}
@@ -389,6 +495,7 @@ private APIUtil() {
389495
private static double retryProgressionFactor;
390496
private static String gatewayTypes;
391497
private static int maxRetryCount;
498+
private static Gson gatewayFeatureCatalog;
392499

393500
//constants for getting masked token
394501
private static final int MAX_LEN = 36;
@@ -3334,11 +3441,84 @@ public static List<String> getGatewayTypes () {
33343441
// Get the gateway types from the deployment.toml
33353442
List<String> gatewayTypesList = new ArrayList<>();
33363443
if (gatewayTypes != null && !gatewayTypes.isEmpty()) {
3337-
gatewayTypesList = Arrays.asList(gatewayTypes.split(","));
3444+
gatewayTypesList = Arrays.asList(gatewayTypes.replace(" ", "").split(","));
33383445
}
33393446
return gatewayTypesList;
33403447
}
33413448

3449+
public static String getGatewayFeatureCatalog() {
3450+
Gson gson = new Gson();
3451+
3452+
JsonObject synapseJSON = gson.fromJson(SYNAPSE_GATEWAY_FEATURES, JsonObject.class);
3453+
JsonObject apkJSON = gson.fromJson(APK_GATEWAY_FEATURES, JsonObject.class);
3454+
JsonObject apiData = gson.fromJson(API_DATA, JsonObject.class);
3455+
3456+
JsonObject gatewayFeatures = new JsonObject();
3457+
gatewayFeatures.add(APIConstants.WSO2_SYNAPSE_GATEWAY, synapseJSON);
3458+
gatewayFeatures.add(APIConstants.WSO2_APK_GATEWAY, apkJSON);
3459+
3460+
// Loop this part for each federated gateway type
3461+
String awsConfigs = "{\n" +
3462+
" \"AWS\":{\n" +
3463+
" \"apiTypes\":[\n" +
3464+
" \"rest\",\n" +
3465+
" \"ws\",\n" +
3466+
" \"ai\"\n" +
3467+
" ],\n" +
3468+
" \"gatewayFeatures\":{\n" +
3469+
" \"runtime\":[\n" +
3470+
" \"cors\",\n" +
3471+
" \"transportsHTTP\",\n" +
3472+
" \"transportsHTTPS\",\n" +
3473+
" \"transportsMutualSSL\",\n" +
3474+
" \"oauth2\",\n" +
3475+
" \"apikey\",\n" +
3476+
" \"basicAuth\",\n" +
3477+
" \"audienceValidation\"\n" +
3478+
" ],\n" +
3479+
" \"resources\":[\n" +
3480+
" \"apiLevelRateLimiting\"\n" +
3481+
" ],\n" +
3482+
" \"localScopes\":[\n" +
3483+
" \"operationScopes\"\n" +
3484+
" ],\n" +
3485+
" \"monetization\":[\n" +
3486+
" \"monetization\"\n" +
3487+
" ],\n" +
3488+
" \"subscriptions\":[\n" +
3489+
" \"subscriptions\"\n" +
3490+
" ],\n" +
3491+
" \"endpoints\":[\n" +
3492+
" \"http\"\n" +
3493+
" ]\n" +
3494+
" }\n" +
3495+
" }\n" +
3496+
"}"; // comes from API call
3497+
JsonObject awsConfigsJSON = gson.fromJson(awsConfigs, JsonObject.class);
3498+
Set<String> keys = awsConfigsJSON.keySet();
3499+
String gatewayType = keys.iterator().next();
3500+
3501+
JsonObject awsConfigsJSONValue = awsConfigsJSON.getAsJsonObject(gatewayType);
3502+
3503+
JsonObject awsGatewayFeatures = awsConfigsJSONValue.getAsJsonObject("gatewayFeatures");
3504+
gatewayFeatures.add(gatewayType, awsGatewayFeatures);
3505+
3506+
JsonArray awsApiTypes = awsConfigsJSONValue.getAsJsonArray("apiTypes");
3507+
for (int i = 0; i < awsApiTypes.size(); i++) {
3508+
String apiType = awsApiTypes.get(i).getAsString();
3509+
if (apiData.has(apiType)) {
3510+
JsonArray existingGateways = apiData.getAsJsonArray(apiType);
3511+
existingGateways.add(gatewayType);
3512+
}
3513+
}
3514+
3515+
JsonObject result = new JsonObject();
3516+
result.add("gatewayFeatures", gatewayFeatures);
3517+
result.add("apiTypes", apiData);
3518+
3519+
return result.toString();
3520+
}
3521+
33423522
/**
33433523
* Returns whether API Publisher Access Control is enabled or not
33443524
*
@@ -8156,6 +8336,12 @@ public static Map<String, Environment> getEnvironments(String organization) thro
81568336
return allEnvironments;
81578337
}
81588338

8339+
public static void addApiAWSApiMapping(String apiId, String aWSApiId, String environmentId)
8340+
throws APIManagementException {
8341+
8342+
ApiMgtDAO.getInstance().addApiAWSApiMapping(apiId, aWSApiId, environmentId);
8343+
}
8344+
81598345
/**
81608346
* Get gateway environments defined in the configuration: api-manager.xml
81618347
* @return map of configured environments against environment name
@@ -10181,13 +10367,16 @@ public static Map<String, org.wso2.carbon.apimgt.api.APIDefinition> getApiDefini
1018110367
/**
1018210368
* Check whether there are external environments registered
1018310369
*/
10184-
public static boolean isAnyExternalGateWayProviderExists() {
10185-
10186-
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
10187-
for (Environment environment : gatewayEnvironments.values()) {
10188-
if (!APIConstants.WSO2_GATEWAY_ENVIRONMENT.equals(environment.getProvider())) {
10189-
return true;
10370+
public static boolean isAnyExternalGateWayProviderExists(String tenantDomain) {
10371+
try {
10372+
Map<String, Environment> environments = getEnvironments(tenantDomain);
10373+
for (Environment environment : environments.values()) {
10374+
if (!APIConstants.WSO2_GATEWAY_ENVIRONMENT.equals(environment.getProvider())) {
10375+
return true;
10376+
}
1019010377
}
10378+
} catch (APIManagementException e) {
10379+
throw new RuntimeException(e);
1019110380
}
1019210381
return false;
1019310382
}
@@ -10600,10 +10789,8 @@ public static String setSubscriptionValidationStatusBeforeInsert(Set<Tier> tiers
1060010789
* @return gateway vendor for the API
1060110790
*/
1060210791
public static String setGatewayVendorBeforeInsertion(String gatewayVendorType, String gatewayType) {
10603-
if (gatewayType != null && APIConstants.WSO2_APK_GATEWAY.equals(gatewayType)) {
10604-
gatewayVendorType = APIConstants.WSO2_APK_GATEWAY;
10605-
}
10606-
return gatewayVendorType;
10792+
10793+
return !APIConstants.WSO2_SYNAPSE_GATEWAY.equals(gatewayType) ? gatewayType: gatewayVendorType;
1060710794
}
1060810795

1060910796
/**
@@ -10616,8 +10803,8 @@ public static String getGatewayType(String gatewayVendor) {
1061610803
String gatewayType = null;
1061710804
if (APIConstants.WSO2_GATEWAY_ENVIRONMENT.equals(gatewayVendor)) {
1061810805
gatewayType = APIConstants.WSO2_SYNAPSE_GATEWAY;
10619-
} else if (APIConstants.WSO2_APK_GATEWAY.equals(gatewayVendor)) {
10620-
gatewayType = APIConstants.WSO2_APK_GATEWAY;
10806+
} else {
10807+
return gatewayVendor;
1062110808
}
1062210809
return gatewayType;
1062310810
}
@@ -10629,10 +10816,12 @@ public static String getGatewayType(String gatewayVendor) {
1062910816
* @return wso2 gateway vendor type
1063010817
*/
1063110818
public static String handleGatewayVendorRetrieval(String gatewayVendor) {
10632-
if (APIConstants.WSO2_APK_GATEWAY.equals(gatewayVendor)) {
10633-
gatewayVendor = APIConstants.WSO2_GATEWAY_ENVIRONMENT;
10819+
if (APIConstants.WSO2_APK_GATEWAY.equals(gatewayVendor) ||
10820+
APIConstants.WSO2_GATEWAY_ENVIRONMENT.equals(gatewayVendor)) {
10821+
return APIConstants.WSO2_GATEWAY_ENVIRONMENT;
10822+
} else {
10823+
return APIConstants.EXTERNAL_GATEWAY_VENDOR;
1063410824
}
10635-
return gatewayVendor;
1063610825
}
1063710826

1063810827
/**

0 commit comments

Comments
 (0)