Skip to content

Commit

Permalink
Add: Custom Backend to APICTL file created on revision deployment
Browse files Browse the repository at this point in the history
  • Loading branch information
BLasan committed Sep 4, 2024
1 parent 8ea3663 commit 2d71961
Show file tree
Hide file tree
Showing 19 changed files with 515 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,14 @@ List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersion, Str
*/
API updateAPI(API api, API existingAPI) throws APIManagementException, FaultGatewaysException;

void updateCustomBackend(API api,String type, InputStream sequence, String fileName) throws APIManagementException;
void updateCustomBackendByRevisionID(API api, String type, InputStream sequence, String revision, String fileName) throws APIManagementException;
void updateCustomBackend(API api,String type, InputStream sequence, String fileName, String customBackendUUID) throws APIManagementException;
Map<String, String> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException;

void updateCustomBackendByRevisionID(String apiUUID, String type, String revision,
String seqName, String backendUUID) throws APIManagementException;

void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID,
Map<String, Object> config) throws APIManagementException;

/**
* Create a new version of the <code>api</code>, with version <code>newVersion</code>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import org.apache.synapse.transport.dynamicconfigurations.DynamicProfileReloaderHolder;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.ExceptionCodes;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO;
import org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO;
import org.wso2.carbon.apimgt.api.gateway.GraphQLSchemaDTO;
Expand Down Expand Up @@ -350,7 +349,8 @@ private void unDeployAPI(APIGatewayAdmin apiGatewayAdmin, DeployAPIInGatewayEven
}

GatewayUtils.setCustomSequencesToBeRemoved(api, gatewayAPIDTO);
GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO);
// GatewayUtils.setCustomBackendToBeRemoved(api, gatewayAPIDTO);

}
gatewayAPIDTO.setLocalEntriesToBeRemove(
GatewayUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1543,8 +1543,6 @@ private OAuthConstants() {
public static final String MEDIATION_SEQUENCE_ELEM = "sequence";
public static final String MEDIATION_CONFIG_EXT = ".xml";
public static final String API_CUSTOM_SEQ_IN_EXT = "--In";

public static final String API_CUSTOM_BACKEND_SEQ_EXT = "--Custom_Backend";
public static final String API_CUSTOM_SEQ_OUT_EXT = "--Out";
public static final String API_CUSTOM_SEQ_FAULT_EXT = "--Fault";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import jdk.internal.util.xml.impl.Input;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.util.AXIOMUtil;
import org.apache.axis2.Constants;
Expand Down Expand Up @@ -1041,15 +1044,29 @@ private void updateAPIPolicies(API api, String tenantDomain) throws APIManagemen
}

@Override
public void updateCustomBackend(API api, String type, InputStream sequence, String fileName)
public void updateCustomBackend(API api, String type, InputStream sequence, String seqName, String customBackendUUID)
throws APIManagementException {
apiMgtDAO.updateCustomBackend(api.getUuid(), fileName, sequence, type);
apiMgtDAO.updateCustomBackend(api.getUuid(), seqName, sequence, type, customBackendUUID);
}

@Override
public void updateCustomBackendByRevisionID(API api, String type, InputStream sequence,
String revision, String fileName) throws APIManagementException {
apiMgtDAO.updateCustomBackendByRevision(api.getUuid(), fileName, sequence, type, revision);
public Map<String, String> getCustomBackendOfAPIByUUID(String customBackendUUID, String apiUUID, boolean isInfoOnly) throws APIManagementException {
return apiMgtDAO.getCustomBackendOfAPIByUUID(customBackendUUID, apiUUID, isInfoOnly);
}

@Override
public void updateCustomBackendByRevisionID(String apiUUID, String type, String revision,
String seqName, String backendUUID) throws APIManagementException {
String customBackendUUID = UUID.randomUUID().toString();
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(backendUUID, apiUUID);
apiMgtDAO.updateCustomBackendByRevision(apiUUID, seqName, sequence, type, revision, customBackendUUID);
}

@Override public void addNewCustomBackendForRevision(String revisionUUID, String updatedBackendUUID, String apiUUID,
Map<String, Object> config) throws APIManagementException {
InputStream sequence = apiMgtDAO.getCustomBackendSequenceOfAPIByUUID(updatedBackendUUID, apiUUID);
config.put("sequence", sequence);
apiMgtDAO.addNewCustomBackendForAPIRevision(apiUUID, revisionUUID, config);
}

private void validateKeyManagers(API api) throws APIManagementException {
Expand Down Expand Up @@ -5242,6 +5259,7 @@ public API getAPIbyUUID(String uuid, String organization) throws APIManagementEx
populateRevisionInformation(api, uuid);
populateAPIInformation(uuid, organization, api);
populateAPILevelPolicies(api);

if (APIUtil.isSequenceDefined(api.getInSequence()) || APIUtil.isSequenceDefined(api.getOutSequence())
|| APIUtil.isSequenceDefined(api.getFaultSequence())) {
if (migrationEnabled == null) {
Expand Down Expand Up @@ -5287,6 +5305,23 @@ private void populateAPILevelPolicies(API api) throws APIManagementException {
}
}

private void populateCustomBackend(API api) throws APIManagementException {
JsonObject endpointConfig = JsonParser.parseString(api.getEndpointConfig()).getAsJsonObject();
if(endpointConfig != null) {
if(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE) != null &&
APIConstants.ENDPOINT_TYPE_SEQUENCE.equals(endpointConfig.get(APIConstants.API_ENDPOINT_CONFIG_PROTOCOL_TYPE).getAsString())) {
ObjectMapper mapper = new ObjectMapper();
// TODO: Check DB Queries. Following fetches data by API UUID and Revision ID = null
Map<String, Object> conf = apiMgtDAO.retrieveCustomBackendOfAPI(api.getUuid());
try {
api.setEndpointConfig(mapper.writeValueAsString(conf));
} catch (IOException ex) {
handleException("Error while converting endpointConfig to json", ex);
}
}
}
}

@Override
public APISearchResult searchPaginatedAPIsByFQDN(String endpoint, String tenantDomain,
int offset, int limit) throws APIManagementException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import jdk.internal.util.xml.impl.Input;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -2791,9 +2792,8 @@ public List<SubscribedAPI> getSubscriptionsOfAPI(String apiName, String apiVersi
}

/**
* @param apiName Name of the API
* @param apiVersion Version of the API
* @param provider Name of API creator
* @param apiUUID UUID of the API
* @param organization Organization of the API
* @return All subscriptions of a given API
* @throws org.wso2.carbon.apimgt.api.APIManagementException
*/
Expand Down Expand Up @@ -11194,6 +11194,66 @@ public Map<String, Object> retrieveCustomBackendOfAPIRevision(String apiUUID, St
return map;
}

public InputStream getCustomBackendSequenceOfAPIByUUID(String backendUUID, String apiUUID) throws APIManagementException {
String sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
ResultSet rs = null;
InputStream sequence = null;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(sqlQuery)) {
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
rs = ps.executeQuery();
while(rs.next()) {
try (InputStream in = rs.getBinaryStream("SEQUENCE")) {
sequence = in;
} catch (IOException ex) {
handleException("Error reading the sequence of Custom Backend: "+ backendUUID + ", API: " + apiUUID, ex);
}
}
} catch (SQLException ex) {
handleException("Error when fetching Custom Backend data: "+ backendUUID + " of API: " + apiUUID, ex);
}

if(sequence == null) {
throw new APIManagementException("Custom Backend Content cannot be empty");
}
return sequence;
}

public Map<String, String> getCustomBackendOfAPIByUUID(String backendUUID, String apiUUID, boolean isInfo) throws APIManagementException {
String sqlQuery;
Map<String, String> endpointConfig = new HashMap<>();
boolean isRevisioned = checkAPIUUIDIsARevisionUUID(apiUUID) != null;
if(isRevisioned) {
sqlQuery = SQLConstants.CustomBackendConstants.GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
} else {
sqlQuery = SQLConstants.CustomBackendConstants.GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID;
}
ResultSet resultSet = null;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement ps = connection.prepareStatement(sqlQuery)) {
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
resultSet = ps.executeQuery();
while (resultSet.next()) {
if(!isInfo) {
try (InputStream in = resultSet.getBinaryStream("SEQUENCE")) {
endpointConfig.put("sequence", IOUtils.toString(in));
} catch (IOException ex) {
handleException("Error reading Sequence Content of Custom Backend: " + backendUUID + " API: " + apiUUID, ex);
}
}
endpointConfig.put("type", resultSet.getString("TYPE"));
endpointConfig.put("sequence_name", resultSet.getString("NAME"));
endpointConfig.put("endpoint_type", "custom_backend");
endpointConfig.put("sequence_id", resultSet.getString("ID"));
}
} catch (SQLException ex) {
handleException("Error when retrieving Custom Backend of API: " + apiUUID, ex);
}
return endpointConfig;
}

/**
*
* @param apiUUID API UUID
Expand All @@ -11209,9 +11269,9 @@ public Map<String, Object> retrieveCustomBackendOfAPI(String apiUUID) throws API
ps.setString(1, apiUUID);
resultSet = ps.executeQuery();
while (resultSet.next()) {
map.put("sequence", resultSet.getString("SEQUENCE"));
map.put("endpoint_type", resultSet.getString("TYPE"));
map.put("type", resultSet.getString("TYPE"));
map.put("sequence_name", resultSet.getString("NAME"));
map.put("endpoint_type", "custom_backend");
}
} catch (SQLException ex) {
handleException("Error retrieving Custom Backend of an API: " + apiUUID, ex);
Expand Down Expand Up @@ -20794,14 +20854,15 @@ public void updateAPIPoliciesMapping(String apiUUID, Set<URITemplate> uriTemplat
}
}

public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type) throws APIManagementException {
public void updateCustomBackend(String apiUUID, String sequenceName, InputStream sequence, String type, String backendUUID) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setString(2, backendUUID);
prepStmt.executeUpdate();
addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection);
addCustomBackend(apiUUID, sequenceName, null, sequence, type, connection, backendUUID);
connection.commit();
} catch (SQLException e) {
handleException("Error while adding Custom Backend for API : " + apiUUID, e);
Expand All @@ -20822,30 +20883,54 @@ public void deleteCustomBackend(String apiUUID, String revisionId) throws APIMan
}
}

public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type, String revision) throws APIManagementException {
public void updateCustomBackendByRevision(String apiUUID, String sequenceName, InputStream sequence, String type,
String revision, String backendUUID) throws APIManagementException {
String deleteCustomBackedQuery = SQLConstants.CustomBackendConstants.DELETE_CUSTOM_BACKEND_BY_REVISION;
try (Connection connection = APIMgtDBUtil.getConnection();
PreparedStatement prepStmt = connection.prepareStatement(deleteCustomBackedQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setString(2, revision);
prepStmt.executeUpdate();
addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection);
addCustomBackend(apiUUID, sequenceName, revision, sequence, type, connection, backendUUID);
connection.commit();
} catch (SQLException e) {
handleException("Error while adding Custom Backend for API : " + apiUUID, e);
}
}

public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence, String type, Connection connection) throws APIManagementException {
public void addNewCustomBackendForAPIRevision(String apiUUID, String revisionID, Map<String, Object> endpointConfig) throws APIManagementException {
String addSqlQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
try (Connection con = APIMgtDBUtil.getConnection();
PreparedStatement ps = con.prepareStatement(addSqlQuery)) {
String backendUUID = endpointConfig.get("sequence_id").toString();
InputStream sequence = (InputStream) endpointConfig.get("sequence");
String type = endpointConfig.get("type").toString();
String sequenceName = endpointConfig.get("sequence_name").toString();
con.setAutoCommit(false);
ps.setString(1, backendUUID);
ps.setString(2, apiUUID);
ps.setBinaryStream(3, sequence);
ps.setString(4, type);
ps.setString(5, revisionID);
ps.setString(6, sequenceName);
ps.executeUpdate();
} catch (SQLException ex) {
handleException("Error when inserting Custom Backend data for API: " + apiUUID, ex);
}
}

public void addCustomBackend(String apiUUID, String sequenceName, String revision, InputStream sequence,
String type, Connection connection, String backendUUID) throws APIManagementException {
String insertCustomBackendQuery = SQLConstants.CustomBackendConstants.ADD_CUSTOM_BACKEND;
try (PreparedStatement prepStmt = connection.prepareStatement(insertCustomBackendQuery)) {
connection.setAutoCommit(false);
prepStmt.setString(1, apiUUID);
prepStmt.setBinaryStream(2, sequence);
prepStmt.setString(3, type);
prepStmt.setString(4, revision);
prepStmt.setString(5, sequenceName);
prepStmt.setString(1, backendUUID);
prepStmt.setString(2, apiUUID);
prepStmt.setBinaryStream(3, sequence);
prepStmt.setString(4, type);
prepStmt.setString(5, revision);
prepStmt.setString(6, sequenceName);
prepStmt.executeUpdate();
} catch (SQLException e) {
handleException("Error while adding Custom Backend for API : " + apiUUID, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4403,12 +4403,18 @@ public static class SystemConfigsConstants {

public static class CustomBackendConstants {
public static final String ADD_CUSTOM_BACKEND =
"INSERT INTO AM_API_CUSTOM_BACKEND (API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?)";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL";
"INSERT INTO AM_API_CUSTOM_BACKEND (ID,API_UUID,SEQUENCE,TYPE,REVISION_UUID,NAME) " + "VALUES (?,?,?,?,?,?)";
public static final String DELETE_CUSTOM_BACKEND = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND ID = ? AND REVISION_UUID IS NULL";
public static final String DELETE_CUSTOM_BACKEND_BY_REVISION = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String DELETE_CUSTOM_BACKEND_BY_API = "DELETE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID = ?";
public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT NAME, SEQUENCE, TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_CUSTOM_BACKEND_OF_API_DEFAULT_REVISION = "SELECT ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND WHERE API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.SEQUENCE, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ? AND REVISION_UUID IS NULL";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_META_DATA_FROM_SEQUENCE_ID = "SELECT ACB.ID, ACB.NAME, ACB.TYPE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
public static final String GET_API_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND API_UUID = ?";
public static final String GET_REVISION_SPECIFIC_CUSTOM_BACKEND_SEQUENCE_FROM_SEQUENCE_ID = "SELECT ACB.SEQUENCE FROM AM_API_CUSTOM_BACKEND ACB WHERE ACB.ID = ? AND ACB.REVISION_UUID = ?";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public final class ImportExportConstants {
public static final String DISPLAY_ON_DEVPORTAL_OPTION = "displayOnDevportal";

public static final String POLICIES_DIRECTORY = "Policies";
public static final String CUSTOM_BACKEND_DIRECTORY = "Custom-Backend";
public static final String SWAGGER_X_WSO2_APICTL_INIT = "x-wso2-apictl-init";

public static final String EXPORT_POLICY_TYPE_YAML = "YAML";
Expand Down
Loading

0 comments on commit 2d71961

Please sign in to comment.