use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.
the class SolaceStoreUtils method getSolaceDeployedEnvsInfo.
/**
* Get SolaceDeployedEnvironmentDTO using admin APIs and map into DTOs to parse Devportal
*
* @param solaceEnvironment Solace environment Object
* @param solaceOrganization Solace broker organization name
* @param applicationUuid Subscribed Application UUID
* @return List of SolaceDeployedEnvironmentDTO to use in Devportal
* @throws APIManagementException if error occurred when creating SolaceDeployedEnvironmentDTO
*/
public static List<SolaceDeployedEnvironmentDTO> getSolaceDeployedEnvsInfo(Environment solaceEnvironment, String solaceOrganization, String applicationUuid) throws APIManagementException {
Map<String, Environment> gatewayEnvironmentMap = APIUtil.getReadOnlyGatewayEnvironments();
// Create solace admin APIs instance
SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
HttpResponse response = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, "default");
List<SolaceDeployedEnvironmentDTO> solaceEnvironments = new ArrayList<>();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
String responseString = EntityUtils.toString(response.getEntity());
org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
// Get solace environments attached with the Solace application
if (jsonObject.getJSONArray("environments") != null) {
JSONArray environmentsArray = jsonObject.getJSONArray("environments");
for (int i = 0; i < environmentsArray.length(); i++) {
SolaceDeployedEnvironmentDTO solaceDeployedEnvironmentsDTO = new SolaceDeployedEnvironmentDTO();
org.json.JSONObject environmentObject = environmentsArray.getJSONObject(i);
// Get details of Solace environment attached to the solace application
if (environmentObject.getString("name") != null) {
String environmentName = environmentObject.getString("name");
Environment gatewayEnvironment = gatewayEnvironmentMap.get(environmentName);
if (gatewayEnvironment != null) {
// Set Solace environment details
solaceDeployedEnvironmentsDTO.setEnvironmentName(gatewayEnvironment.getName());
solaceDeployedEnvironmentsDTO.setEnvironmentDisplayName(gatewayEnvironment.getDisplayName());
solaceDeployedEnvironmentsDTO.setOrganizationName(gatewayEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION));
boolean containsMQTTProtocol = false;
// Get messaging protocols from the response body
if (environmentObject.getJSONArray("messagingProtocols") != null) {
List<SolaceURLsDTO> endpointUrls = new ArrayList<>();
JSONArray protocolsArray = environmentObject.getJSONArray("messagingProtocols");
for (int j = 0; j < protocolsArray.length(); j++) {
SolaceURLsDTO solaceURLsDTO = new SolaceURLsDTO();
String protocol = protocolsArray.getJSONObject(j).getJSONObject("protocol").getString("name");
if (SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.equalsIgnoreCase(protocol)) {
containsMQTTProtocol = true;
}
String uri = protocolsArray.getJSONObject(j).getString("uri");
solaceURLsDTO.setProtocol(protocol);
solaceURLsDTO.setEndpointURL(uri);
endpointUrls.add(solaceURLsDTO);
}
solaceDeployedEnvironmentsDTO.setSolaceURLs(endpointUrls);
}
// Get topic permissions from the solace application response body
if (environmentObject.getJSONObject("permissions") != null) {
org.json.JSONObject permissionsObject = environmentObject.getJSONObject("permissions");
SolaceTopicsObjectDTO solaceTopicsObjectDTO = new SolaceTopicsObjectDTO();
populateSolaceTopics(solaceTopicsObjectDTO, permissionsObject, "default");
// Handle the special case of MQTT protocol
if (containsMQTTProtocol) {
HttpResponse responseForMqtt = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
org.json.JSONObject permissionsObjectForMqtt = extractPermissionsFromSolaceApplicationGetResponse(responseForMqtt, i, gatewayEnvironmentMap);
if (permissionsObjectForMqtt != null) {
populateSolaceTopics(solaceTopicsObjectDTO, permissionsObjectForMqtt, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
}
}
solaceDeployedEnvironmentsDTO.setSolaceTopicsObject(solaceTopicsObjectDTO);
}
}
}
solaceEnvironments.add(solaceDeployedEnvironmentsDTO);
}
}
} catch (IOException e) {
log.error(e.getMessage());
}
return solaceEnvironments;
} else {
throw new APIManagementException("Solace Environment configurations are not provided properly");
}
}
use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.
the class SolaceApplicationNotifier method removeSolaceApplication.
/**
* Remove applications from Solace broker
*
* @param event ApplicationEvent to remove Solace applications
* @throws NotifierException if error occurs when removing applications from Solace broker
*/
private void removeSolaceApplication(ApplicationEvent event) throws NotifierException {
// get list of subscribed APIs in the application
Subscriber subscriber = new Subscriber(event.getSubscriber());
try {
Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
List<SubscribedAPI> subscribedApiList = new ArrayList<>(subscriptions);
boolean hasSubscribedAPIDeployedInSolace = false;
String organizationNameOfSolaceDeployment = null;
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
labelOne: for (SubscribedAPI api : subscribedApiList) {
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUUID());
for (APIRevisionDeployment deployment : deployments) {
if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
hasSubscribedAPIDeployedInSolace = true;
organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
break labelOne;
}
}
}
}
boolean applicationFoundInSolaceBroker = false;
if (hasSubscribedAPIDeployedInSolace) {
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
// check existence of application in Solace Broker
CloseableHttpResponse response1 = solaceAdminApis.applicationGet(organizationNameOfSolaceDeployment, event.getUuid(), "default");
if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
applicationFoundInSolaceBroker = true;
if (log.isDebugEnabled()) {
log.info("Found application '" + event.getApplicationName() + "' in Solace broker");
log.info("Waiting until application removing workflow gets finished");
}
} else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
throw new NotifierException("Application '" + event.getApplicationName() + "' cannot be found in " + "Solace Broker");
} else {
if (log.isDebugEnabled()) {
log.error("Error while searching for application '" + event.getApplicationName() + "'" + " in Solace Broker. : " + response1.getStatusLine().toString());
}
throw new NotifierException("Error while searching for application '" + event.getApplicationName() + "' in Solace Broker");
}
}
if (applicationFoundInSolaceBroker) {
log.info("Deleting application from Solace Broker");
// delete application from solace
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(organizationNameOfSolaceDeployment, event.getUuid());
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
log.info("Successfully deleted application '" + event.getApplicationName() + "' " + "in Solace Broker");
} else {
if (log.isDebugEnabled()) {
log.error("Error while deleting application " + event.getApplicationName() + " in Solace. :" + response2.getStatusLine().toString());
}
throw new NotifierException("Error while deleting application '" + event.getApplicationName() + "' in Solace");
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.
the class SolaceNotifierUtils method getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment.
/**
* Get third party Solace broker organization Name for API deployment
*
* @param api Name of the API
* @return String of the name of organization in Solace broker
* @throws APIManagementException is error occurs when getting the name of the organization name
*/
public static String getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment(API api) throws APIManagementException {
apiMgtDAO = ApiMgtDAO.getInstance();
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUuid());
for (APIRevisionDeployment deployment : deployments) {
if (deployment.isDisplayOnDevportal()) {
String environmentName = deployment.getDeployment();
if (gatewayEnvironments.containsKey(environmentName)) {
Environment deployedEnvironment = gatewayEnvironments.get(environmentName);
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(deployedEnvironment.getProvider())) {
return deployedEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
}
}
}
}
return null;
}
use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.
the class SolaceNotifierUtils method deployApplicationToSolaceBroker.
/**
* Deploy an application to Solace broker
*
* @param application Application to be deployed
* @param apiProducts Api products to be subscribed to Application
* @param organization Name of the organization
* @throws IOException If an error occurs when deploying the application
* @throws APIManagementException if an error occurs when getting Solace config
*/
public static void deployApplicationToSolaceBroker(Application application, ArrayList<String> apiProducts, String organization) throws IOException, APIManagementException {
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
// check existence of the developer
CloseableHttpResponse response1 = solaceAdminApis.developerGet(organization);
if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (log.isDebugEnabled()) {
log.info("Developer found in Solace Broker");
}
// check application status
CloseableHttpResponse response2 = solaceAdminApis.applicationGet(organization, application.getUUID(), "default");
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// app already exists
if (log.isDebugEnabled()) {
log.info("Solace application '" + application.getName() + "' already exists in Solace." + " Updating Application......");
}
CloseableHttpResponse response3 = solaceAdminApis.applicationPatchAddSubscription(organization, application, apiProducts);
if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
log.info("Solace application '" + application.getName() + "' updated successfully");
} else {
if (log.isDebugEnabled()) {
log.error("Error while updating Solace application '" + application.getName() + ". : " + response3.getStatusLine().toString());
}
throw new HttpResponseException(response3.getStatusLine().getStatusCode(), response3.getStatusLine().getReasonPhrase());
}
} else if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// If application keys are not generated we are not doing anything in Solace.
if (application.getKeys().isEmpty()) {
return;
}
String responseString = EntityUtils.toString(response2.getEntity());
if (responseString.contains(String.valueOf(HttpStatus.SC_NOT_FOUND))) {
// create new app
if (log.isDebugEnabled()) {
log.info("Solace application '" + application.getName() + "' not found in Solace Broker." + "Creating new application......");
}
CloseableHttpResponse response4 = solaceAdminApis.createApplication(organization, application, apiProducts);
if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
log.info("Solace application '" + application.getName() + "' created successfully");
} else {
if (log.isDebugEnabled()) {
log.error("Error while creating Solace application '" + application.getName() + ". : " + response4.getStatusLine().toString());
}
throw new HttpResponseException(response4.getStatusLine().getStatusCode(), response4.getStatusLine().getReasonPhrase());
}
} else {
if (log.isDebugEnabled()) {
log.error("Error while searching for application '" + application.getName() + ". : " + response2.getStatusLine().toString());
}
throw new HttpResponseException(response2.getStatusLine().getStatusCode(), response2.getStatusLine().getReasonPhrase());
}
} else {
if (log.isDebugEnabled()) {
log.error("Error while searching for application '" + application.getName() + ". : " + response2.getStatusLine().toString());
}
throw new HttpResponseException(response2.getStatusLine().getStatusCode(), response2.getStatusLine().getReasonPhrase());
}
} else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
if (log.isDebugEnabled()) {
log.error("Developer not found in Solace Broker");
}
throw new HttpResponseException(response1.getStatusLine().getStatusCode(), response1.getStatusLine().getReasonPhrase());
} else {
if (log.isDebugEnabled()) {
log.error("Developer not found in Solace Broker");
}
throw new HttpResponseException(response1.getStatusLine().getStatusCode(), response1.getStatusLine().getReasonPhrase());
}
}
use of org.wso2.broker.core.Broker in project carbon-apimgt by wso2.
the class SolaceNotifierUtils method unsubscribeAPIProductFromSolaceApplication.
/**
* Unsubscribe the given API product from the Solace application
*
* @param api API object to be unsubscribed
* @param application Solace application
* @throws APIManagementException is error occurs when unsubscribing the API from application
*/
public static void unsubscribeAPIProductFromSolaceApplication(API api, Application application) throws APIManagementException {
List<Environment> deployedSolaceEnvironments = getDeployedSolaceEnvironmentsFromRevisionDeployments(api);
String applicationOrganizationName = getSolaceOrganizationName(deployedSolaceEnvironments);
ArrayList<String> solaceApiProducts = new ArrayList<>();
if (applicationOrganizationName != null) {
for (Environment environment : deployedSolaceEnvironments) {
solaceApiProducts.add(generateApiProductNameForSolaceBroker(api, environment.getName()));
}
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
CloseableHttpResponse response = solaceAdminApis.applicationPatchRemoveSubscription(applicationOrganizationName, application, solaceApiProducts);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
try {
String responseString = EntityUtils.toString(response.getEntity());
org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
String applicationName = String.valueOf(jsonObject.get("displayName"));
log.info("API product unsubscribed from Solace application '" + applicationName + "'");
if (jsonObject.getJSONArray("apiProducts") != null) {
if (jsonObject.getJSONArray("apiProducts").length() == 0) {
// delete application in Solace because of 0 number of api products
CloseableHttpResponse response2 = solaceAdminApis.deleteApplication(applicationOrganizationName, application.getUUID());
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
log.info("Successfully deleted application '" + applicationName + "' in " + "Solace Broker");
} else {
if (log.isDebugEnabled()) {
log.error("Error while deleting application '" + applicationName + "' " + "in Solace. : " + response2.getStatusLine().toString());
}
throw new APIManagementException("Error while deleting application '" + applicationName + "' in Solace");
}
}
}
} catch (IOException e) {
log.error(e.getMessage());
throw new APIManagementException("Error while deleting application in Solace");
}
} else {
if (log.isDebugEnabled()) {
log.error("Error while unsubscribing API product from Solace Application '" + application.getName() + " : " + response.getStatusLine().toString());
}
throw new APIManagementException(response.getStatusLine().getStatusCode() + "-" + response.getStatusLine().getReasonPhrase());
}
} else {
throw new APIManagementException("Multiple Solace organizations found");
}
}
Aggregations