Skip to content

Commit 65f44a5

Browse files
AnuGayantharikaGitHub
authored andcommitted
Improve iplementation for Federated API Discovery
1 parent 74483d3 commit 65f44a5

File tree

10 files changed

+64
-28
lines changed

10 files changed

+64
-28
lines changed

components/apimgt/org.wso2.carbon.apimgt.federated.gateway/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
org.wso2.carbon.apimgt.rest.api.publisher.v1.common.*;version="[9.0,10.0)",
7575
*;resolution:=optional
7676
</Import-Package>
77+
<DynamicImport-Package>*</DynamicImport-Package>
7778
</instructions>
7879
</configuration>
7980
</plugin>

components/apimgt/org.wso2.carbon.apimgt.federated.gateway/src/main/java/org/wso2/carbon/apimgt/FederatedAPIDiscoveryRunner.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
package org.wso2.carbon.apimgt;
2222

2323
import com.google.gson.Gson;
24+
import com.google.gson.JsonObject;
2425
import org.apache.commons.lang3.StringUtils;
2526
import org.apache.commons.logging.Log;
2627
import org.apache.commons.logging.LogFactory;
@@ -35,7 +36,9 @@
3536
import org.wso2.carbon.apimgt.api.model.GatewayMode;
3637
import org.wso2.carbon.apimgt.impl.APIConstants;
3738
import org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI;
39+
import org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants;
3840
import org.wso2.carbon.apimgt.impl.importexport.utils.APIImportExportUtil;
41+
import org.wso2.carbon.apimgt.impl.importexport.utils.CommonUtil;
3942
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
4043
import org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO;
4144
import org.wso2.carbon.apimgt.util.FederatedGatewayUtil;
@@ -99,20 +102,28 @@ public void scheduleDiscovery(Environment environment, String organization) {
99102
scheduledFuture.cancel(true);
100103
}
101104
executor.scheduleAtFixedRate(() -> {
102-
List<API> discoveredAPIs = federatedAPIDiscovery.discoverAPI();
103-
if (discoveredAPIs != null && !discoveredAPIs.isEmpty()) {
104-
log.info("Discovered " + discoveredAPIs.size() + " APIs in environment: "
105-
+ environment.getName());
106-
processDiscoveredAPIs(discoveredAPIs, environment, organization);
107-
} else {
108-
log.info("No APIs discovered in environment: " + environment.getName());
105+
try {
106+
List<API> discoveredAPIs = federatedAPIDiscovery.discoverAPI();
107+
if (discoveredAPIs != null && !discoveredAPIs.isEmpty()) {
108+
processDiscoveredAPIs(discoveredAPIs, environment, organization);
109+
} else {
110+
if (log.isDebugEnabled()) {
111+
log.debug("No APIs discovered in environment: " + environment.getName());
112+
}
113+
}
114+
} catch (Exception e) {
115+
log.error("Error during federated API discovery for environment: "
116+
+ environment.getName(), e);
109117
}
110118
}, 0, environment.getApiDiscoveryScheduledWindow(), TimeUnit.MINUTES);
111119
}
112120
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException |
113121
NoSuchMethodException | InvocationTargetException | APIManagementException e) {
114122
log.error("Error while loading federated API discovery for environment "
115123
+ environment.getName(), e);
124+
} catch (Throwable e) {
125+
log.error("Unexpected error while initializing federated API discovery for environment "
126+
+ environment.getName(), e);
116127
}
117128
}
118129
}
@@ -141,9 +152,8 @@ private void processDiscoveredAPIs(List<API> apisToDeployInGatewayEnv, Environme
141152
+ environment.getName() + APIConstants.DELEM_COLON + apidto.getVersion();
142153

143154
// Determine import mode
144-
boolean alreadyExists = alreadyAvailableAPIs.contains(apiKey);
155+
boolean update = alreadyAvailableAPIs.contains(apiKey) || alreadyAvailableAPIs.contains(envScopedKey);
145156
boolean alreadyExistsWithEnvScope = alreadyAvailableAPIs.contains(envScopedKey);
146-
boolean update = alreadyExists || alreadyExistsWithEnvScope;
147157

148158
// Adjust name if needed
149159
if (alreadyExistsWithEnvScope) {
@@ -154,9 +164,11 @@ private void processDiscoveredAPIs(List<API> apisToDeployInGatewayEnv, Environme
154164
}
155165

156166
// Map to DTO and create ZIP
157-
String apiJson = new Gson().toJson(apidto);
167+
JsonObject apiJson = (JsonObject) new Gson().toJsonTree(apidto);
168+
apiJson = CommonUtil.addTypeAndVersionToFile(ImportExportConstants.TYPE_API,
169+
ImportExportConstants.APIM_VERSION, apiJson);
158170
InputStream apiZip = FederatedGatewayUtil.createZipAsInputStream(
159-
apiJson, api.getSwaggerDefinition(),
171+
apiJson.toString(), api.getSwaggerDefinition(),
160172
FederatedGatewayUtil.createDeploymentYaml(environment),
161173
apidto.getName());
162174

@@ -168,7 +180,10 @@ private void processDiscoveredAPIs(List<API> apisToDeployInGatewayEnv, Environme
168180
new String[]{APIConstants.APIM_PUBLISHER_SCOPE, APIConstants.APIM_CREATOR_SCOPE}, organization);
169181

170182
// Track deployed
171-
discoveredAPIs.add(update && alreadyExistsWithEnvScope ? envScopedKey : apiKey);
183+
discoveredAPIs.add(alreadyExistsWithEnvScope ? envScopedKey : apiKey);
184+
if (!update) {
185+
alreadyAvailableAPIs.add(apidto.getName() + APIConstants.DELEM_COLON + apidto.getVersion());
186+
}
172187

173188
log.info((update ? "Updated" : "Created") + " API: " + api.getId().getName()
174189
+ " in environment: " + environment.getName());
@@ -182,8 +197,8 @@ private void processDiscoveredAPIs(List<API> apisToDeployInGatewayEnv, Environme
182197
}
183198
}
184199

185-
for (String apiName : discoveredAPIs) {
186-
if (!alreadyAvailableAPIs.contains(apiName)) {
200+
for (String apiName : alreadyAvailableAPIs) {
201+
if (!discoveredAPIs.contains(apiName)) {
187202
try {
188203
String[] parts = apiName.split(APIConstants.DELEM_COLON);
189204
if (parts.length < 2) {

components/apimgt/org.wso2.carbon.apimgt.federated.gateway/src/main/java/org/wso2/carbon/apimgt/util/FederatedGatewayUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@
4646

4747
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.API_YAML_FILE_NAME;
4848
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.DEPLOYMENT_ENVIRONMENTS_FILE_NAME;
49+
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.DEPLOYMENT_NAME;
50+
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.DISPLAY_ON_DEVPORTAL_OPTION;
4951
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.SWAGGER_YAML_FILE_NAME;
52+
import static org.wso2.carbon.apimgt.impl.importexport.ImportExportConstants.TYPE_DEPLOYMENT_ENVIRONMENTS;
5053

5154
public class FederatedGatewayUtil {
5255
private static Log log = LogFactory.getLog(FederatedGatewayUtil.class);
@@ -67,6 +70,7 @@ public static void deleteAPI(String apiUUID, String organization, Environment en
6770
try {
6871
APIProvider provider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.
6972
getThreadLocalCarbonContext().getUsername());
73+
provider.deleteAPIRevisions(apiUUID, organization);
7074
provider.deleteAPI(apiUUID, organization);
7175
log.debug("Deleted API: " + apiUUID + " organization: " + organization + " from environment: "
7276
+ environment.getName());
@@ -104,12 +108,12 @@ private static void addToZip(ZipOutputStream zos, String path, String content) t
104108
public static String createDeploymentYaml(Environment environment) {
105109
List<Map<String, String>> deploymentEnvData = new ArrayList<>();
106110
Map<String, String> envEntry = new LinkedHashMap<>();
107-
envEntry.put("displayOnDevportal", "true");
108-
envEntry.put("deploymentEnvironment", environment.getName());
111+
envEntry.put(DISPLAY_ON_DEVPORTAL_OPTION, "true");
112+
envEntry.put(DEPLOYMENT_NAME, environment.getName());
109113
deploymentEnvData.add(envEntry);
110114

111115
Map<String, Object> yamlRoot = new LinkedHashMap<>();
112-
yamlRoot.put("type", "deployment_environments");
116+
yamlRoot.put("type", TYPE_DEPLOYMENT_ENVIRONMENTS);
113117
yamlRoot.put("version", "v4.3.0");
114118
yamlRoot.put("data", deploymentEnvData);
115119

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2853,9 +2853,14 @@ public void deleteAPIRevisions(String apiUUID, String organization) throws APIMa
28532853
WorkflowDTO wfDTO;
28542854

28552855
for (APIRevision apiRevision : apiRevisionList) {
2856-
if (!apiRevision.getApiRevisionDeploymentList().isEmpty() && !isAPIInitiatedFromGateway) {
2857-
undeployAPIRevisionDeployment(apiUUID, apiRevision.getRevisionUUID(),
2858-
apiRevision.getApiRevisionDeploymentList(), organization);
2856+
if (!apiRevision.getApiRevisionDeploymentList().isEmpty()) {
2857+
if (!isAPIInitiatedFromGateway) {
2858+
undeployAPIRevisionDeployment(apiUUID, apiRevision.getRevisionUUID(),
2859+
apiRevision.getApiRevisionDeploymentList(), organization);
2860+
} else {
2861+
apiMgtDAO.removeAPIRevisionDeployment(apiRevision.getRevisionUUID(),
2862+
apiRevision.getApiRevisionDeploymentList());
2863+
}
28592864
}
28602865
wfDTO = apiMgtDAO.retrieveWorkflowFromInternalReference(apiRevision.getRevisionUUID(),
28612866
WorkflowConstants.WF_TYPE_AM_REVISION_DEPLOYMENT);

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/internal/APIManagerComponent.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.wso2.carbon.apimgt.common.gateway.jwttransformer.JWTTransformer;
5252
import org.wso2.carbon.apimgt.eventing.EventPublisherException;
5353
import org.wso2.carbon.apimgt.eventing.EventPublisherFactory;
54+
import org.wso2.carbon.apimgt.impl.APIAdminImpl;
5455
import org.wso2.carbon.apimgt.impl.APIConstants;
5556
import org.wso2.carbon.apimgt.impl.APIManagerAnalyticsConfiguration;
5657
import org.wso2.carbon.apimgt.impl.APIManagerConfiguration;
@@ -1086,10 +1087,19 @@ void initializeAPIDiscoveryTasks(String organization) {
10861087
Map<String, Environment> environments = APIUtil.getEnvironments(organization);
10871088
FederatedAPIDiscoveryService federatedAPIDiscoveryService = ServiceReferenceHolder
10881089
.getInstance().getFederatedAPIDiscoveryService();
1090+
APIAdminImpl apiAdmin = new APIAdminImpl();
10891091

10901092
environments.forEach((name, environment) -> {
1091-
if (environment.getType().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
1092-
federatedAPIDiscoveryService.scheduleDiscovery(environment, organization);
1093+
if (environment.getProvider().equals(APIConstants.EXTERNAL_GATEWAY_VENDOR)) {
1094+
try {
1095+
Environment resolvedEnvironment = apiAdmin.getEnvironmentWithoutPropertyMasking(organization,
1096+
environment.getUuid());
1097+
resolvedEnvironment = apiAdmin.decryptGatewayConfigurationValues(resolvedEnvironment);
1098+
federatedAPIDiscoveryService.scheduleDiscovery(resolvedEnvironment, organization);
1099+
} catch (APIManagementException e) {
1100+
log.error("Error while scheduling API Discovery for environment: "
1101+
+ name + " in organization: " + organization, e);
1102+
}
10931103
}
10941104
});
10951105

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12026,7 +12026,7 @@ public static byte[] hmacSHA256(String data, byte[] key) throws NoSuchAlgorithmE
1202612026
}
1202712027

1202812028
public static void validateAndScheduleFederatedGatewayAPIDiscovery(Environment environment, String organization,
12029-
boolean updateEnvFlow) {
12029+
boolean updateEnvFlow) {
1203012030
FederatedAPIDiscoveryService federatedAPIDiscoveryService = ServiceReferenceHolder
1203112031
.getInstance().getFederatedAPIDiscoveryService();
1203212032
try {

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/EnvironmentsApiServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Response environmentsEnvironmentIdPut(String environmentId, EnvironmentDT
9292
try {
9393
this.validatePermissions(gatewayVisibilityPermissionConfigurationDTO);
9494
apiAdmin.updateEnvironment(organization, env);
95-
APIUtil.validateAndScheduleFederatedGatewayAPIDiscovery(env, organization);
95+
APIUtil.validateAndScheduleFederatedGatewayAPIDiscovery(env, organization, true);
9696
location = new URI(RestApiConstants.RESOURCE_PATH_ENVIRONMENT + "/" + environmentId);
9797
} catch (URISyntaxException e) {
9898
String errorMessage = "Error while updating Environment : " + environmentId;
@@ -149,7 +149,7 @@ public Response environmentsPost(EnvironmentDTO body, MessageContext messageCont
149149
env.getPermissions();
150150
validatePermissions(gatewayVisibilityPermissionConfigurationDTO);
151151
EnvironmentDTO envDTO = EnvironmentMappingUtil.fromEnvToEnvDTO(apiAdmin.addEnvironment(organization, env));
152-
APIUtil.validateAndScheduleFederatedGatewayAPIDiscovery(env, organization);
152+
APIUtil.validateAndScheduleFederatedGatewayAPIDiscovery(env, organization, false);
153153
URI location = new URI(RestApiConstants.RESOURCE_PATH_ENVIRONMENT + "/" + envDTO.getId());
154154
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.GATEWAY_ENVIRONMENTS, new Gson().toJson(envDTO),
155155
APIConstants.AuditLogConstants.CREATED, RestApiCommonUtil.getLoggedInUsername());

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,6 +1157,7 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials,
11571157
}
11581158
APIDTO dto = new APIDTO();
11591159
dto.setName(model.getId().getApiName());
1160+
dto.setDisplayName(model.getDisplayName() != null ? model.getDisplayName() : model.getId().getApiName());
11601161
dto.setVersion(model.getId().getVersion());
11611162
String providerName = model.getId().getProviderName();
11621163
dto.setProvider(APIUtil.replaceEmailDomainBack(providerName));
@@ -1614,7 +1615,7 @@ public static APIDTO fromAPItoDTO(API model, boolean preserveCredentials,
16141615
// Set primary endpoints
16151616
dto.setPrimaryProductionEndpointId(model.getPrimaryProductionEndpointId());
16161617
dto.setPrimarySandboxEndpointId(model.getPrimarySandboxEndpointId());
1617-
1618+
dto.setInitiatedFromGateway(model.isInitiatedFromGateway());
16181619
return dto;
16191620
}
16201621

features/apimgt/org.wso2.carbon.apimgt.federated.gateway.feature/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
<goal>p2-feature-gen</goal>
6565
</goals>
6666
<configuration>
67-
<id>org.wso2.carbon.apimgt.federated.gateway.feature</id>
67+
<id>org.wso2.carbon.apimgt.federated.gateway</id>
6868
<propertiesFile>../feature.properties</propertiesFile>
6969
<adviceFile>
7070
<properties>

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2309,7 +2309,7 @@
23092309
<org.wso2.orbit.atlassian.oai.version>2.12.1.wso2v2</org.wso2.orbit.atlassian.oai.version>
23102310
<maven.spotbugsplugin.exclude.file>${project.parent.basedir}/../../spotbugs-exclude.xml</maven.spotbugsplugin.exclude.file>
23112311
<skip.aspectj>false</skip.aspectj>
2312-
<org.aspectj.weaver.openarchives.limit>1500</org.aspectj.weaver.openarchives.limit>
2312+
<org.aspectj.weaver.openarchives.limit>2000</org.aspectj.weaver.openarchives.limit>
23132313
<javax.jms.version>2.0.1</javax.jms.version>
23142314
<com.damnhandy.version>2.1.6</com.damnhandy.version>
23152315
<damnhandy.version>${com.damnhandy.version}.wso2v2</damnhandy.version>

0 commit comments

Comments
 (0)