use of org.wso2.carbon.apimgt.solace.SolaceAdminApis 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");
}
}
use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.
the class SolaceBrokerDeployer method undeploy.
/**
* Undeploy API artifact from provided environment
*
* @param apiName Name of the API to be undeployed from Solace broker
* @param apiVersion Version of the API to be undeployed from Solace broker
* @param apiContext Context of the API to be undeployed from Solace broker
* @param environment Environment needed to be undeployed API from
* @throws DeployerException if error occurs when undeploying APIs from Solace broker
*/
@Override
public boolean undeploy(String apiName, String apiVersion, String apiContext, Environment environment) throws DeployerException {
String apiNameForRegistration = apiName + "-" + apiVersion;
String[] apiContextParts = apiContext.split("/");
String apiNameWithContext = environment.getName() + "-" + apiName + "-" + apiContextParts[1] + "-" + apiContextParts[2];
SolaceAdminApis solaceAdminApis;
try {
solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
} catch (APIManagementException e) {
throw new DeployerException(e.getMessage());
}
// delete API product from Solace
CloseableHttpResponse response1 = solaceAdminApis.deleteApiProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
log.info("API product '" + apiNameWithContext + "' has been deleted from Solace Broker");
// delete registered API from Solace
CloseableHttpResponse response2 = solaceAdminApis.deleteRegisteredAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
if (log.isDebugEnabled()) {
log.info("API product '" + apiNameWithContext + "' and API '" + apiNameForRegistration + "' have " + "been deleted from Solace broker");
}
return true;
} else if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT) {
if (log.isDebugEnabled()) {
log.info("Registered API '" + apiNameWithContext + "' is still referenced for another API product. " + "Skipping API Deletion....");
}
return true;
} else {
if (log.isDebugEnabled()) {
log.error("Error occurred while deleting the API '" + apiNameForRegistration + "' from Solace " + "Broker. :" + response2.getStatusLine().toString());
}
throw new DeployerException(response2.getStatusLine().toString());
}
} else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_CONFLICT) {
if (log.isDebugEnabled()) {
log.error("Cannot undeploy. Solace API product '" + apiNameWithContext + "' is subscribed for a solace " + "Application.");
}
throw new DeployerException(response1.getStatusLine().toString());
} else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
if (log.isDebugEnabled()) {
log.warn("Cannot undeploy. Solace API product '" + apiNameWithContext + "' is already " + " un deployed.");
}
return true;
} else {
if (log.isDebugEnabled()) {
log.error("Error occurred while deleting the API Product '" + apiNameWithContext + "' from Solace " + "Broker. :" + response1.getStatusLine().toString());
}
throw new DeployerException(response1.getStatusLine().toString());
}
}
use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.
the class SolaceBrokerDeployer method deploy.
/**
* Deploy API artifact to provided environment
*
* @param api API to be deployed into Solace broker
* @param environment Environment to be deployed
* @throws DeployerException if error occurs when deploying APIs to Solace broker
*/
@Override
public boolean deploy(API api, Environment environment) throws DeployerException {
String apiDefinition = api.getAsyncApiDefinition();
Aai20Document aai20Document = (Aai20Document) Library.readDocumentFromJSONString(apiDefinition);
String apiNameForRegistration = api.getId().getApiName() + "-" + api.getId().getVersion();
String[] apiContextParts = api.getContext().split("/");
String apiNameWithContext = environment.getName() + "-" + api.getId().getName() + "-" + apiContextParts[1] + "-" + apiContextParts[2];
SolaceAdminApis solaceAdminApis;
try {
solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
} catch (APIManagementException e) {
throw new DeployerException(e.getMessage());
}
// check availability of environment
CloseableHttpResponse response1 = solaceAdminApis.environmentGET(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName());
if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (log.isDebugEnabled()) {
log.info("environment '" + environment.getName() + "' found in Solace broker");
}
// check api product already exists in solace
CloseableHttpResponse response4 = solaceAdminApis.apiProductGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// api Product Already found in solace. No need to deploy again into Solace
if (log.isDebugEnabled()) {
log.info("API product '" + apiNameWithContext + "' already found in Solace. No need to create " + "again");
}
return true;
} else if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// api product not found in solace. check existence of registered API in solace
if (log.isDebugEnabled()) {
log.info("API product '" + apiNameWithContext + "' not found in Solace. Checking the existence " + "of API");
}
CloseableHttpResponse response5 = solaceAdminApis.registeredAPIGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
if (response5.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (log.isDebugEnabled()) {
log.info("API '" + apiNameForRegistration + "' already registered in Solace. Creating API " + "product using registered API");
}
// create API product only
CloseableHttpResponse response3 = solaceAdminApis.createAPIProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName(), aai20Document, apiNameWithContext, apiNameForRegistration);
if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
log.info("API product " + apiNameWithContext + " has been created in Solace broker");
return true;
} else {
if (log.isDebugEnabled()) {
log.error("Error while creating API product" + apiNameWithContext + " in Solace." + response3.getStatusLine().toString());
}
throw new DeployerException(response3.getStatusLine().toString());
}
} else if (response5.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
if (log.isDebugEnabled()) {
log.info("API '" + apiNameForRegistration + "' not registered in Solace. Creating both API " + "and API product. : " + response5.getStatusLine().toString());
}
// register the API in Solace Broker
CloseableHttpResponse response2 = solaceAdminApis.registerAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration, apiDefinition);
if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
if (log.isDebugEnabled()) {
log.info("API '" + apiNameForRegistration + "' has been registered in Solace broker");
}
// create API Product in Solace broker
CloseableHttpResponse response3 = solaceAdminApis.createAPIProduct(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName(), aai20Document, apiNameWithContext, apiNameForRegistration);
if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
log.info("API product '" + apiNameWithContext + "' has been created in Solace broker");
return true;
} else {
if (log.isDebugEnabled()) {
log.error("Error while creating API product in Solace. : " + response2.getStatusLine().toString());
}
// delete registered API in solace
CloseableHttpResponse response6 = solaceAdminApis.deleteRegisteredAPI(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameForRegistration);
if (response6.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
log.info("Successfully deleted registered API '" + apiNameForRegistration + "' " + "from Solace");
} else {
if (log.isDebugEnabled()) {
log.error("Error while deleting registered API '" + apiNameForRegistration + "' " + "in Solace. : " + response6.getStatusLine().toString());
}
throw new DeployerException(response6.getStatusLine().toString());
}
if (log.isDebugEnabled()) {
log.error("Error while deleting registered API '" + apiNameForRegistration + "' " + "in Solace. : " + response3.getStatusLine().toString());
}
throw new DeployerException(response3.getStatusLine().toString());
}
} else {
log.error("Error while registering API in Solace - '" + apiNameForRegistration + "'");
throw new DeployerException(response2.getStatusLine().toString());
}
} else {
if (log.isDebugEnabled()) {
log.error("Error while finding API '" + apiNameForRegistration + "' in Solace. : " + response5.getStatusLine().toString());
}
throw new DeployerException(response5.getStatusLine().toString());
}
} else {
if (log.isDebugEnabled()) {
log.error("Error while finding API product '" + apiNameWithContext + "' in Solace. : " + response4.getStatusLine().toString());
}
throw new DeployerException(response4.getStatusLine().toString());
}
} else {
if (log.isDebugEnabled()) {
log.error("Cannot find specified Solace environment - '" + environment.getName() + ". : " + response1.getStatusLine().toString());
}
throw new DeployerException(response1.getStatusLine().toString());
}
}
use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.
the class SolaceNotifierUtils method checkApiProductAlreadyDeployedIntoSolaceEnvironments.
/**
* Check whether the given API product is already deployed in the Solace environment
*
* @param api Name of the API
* @param environments List of the environments
* @return returns true if the given API product is already deployed in one of environments
* @throws IOException If an error occurs when checking API product availability
* @throws APIManagementException if an error occurs when getting Solace config
*/
public static boolean checkApiProductAlreadyDeployedIntoSolaceEnvironments(API api, List<Environment> environments) throws IOException, APIManagementException {
int numberOfDeployedEnvironmentsInSolace = 0;
for (Environment environment : environments) {
String apiNameWithContext = generateApiProductNameForSolaceBroker(api, environment.getName());
SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
CloseableHttpResponse response = solaceAdminApis.apiProductGet(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), apiNameWithContext);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
if (log.isDebugEnabled()) {
log.info("API product found in Solace Broker");
}
numberOfDeployedEnvironmentsInSolace++;
} else if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
if (log.isDebugEnabled()) {
log.error("API product not found in Solace broker");
log.error(EntityUtils.toString(response.getEntity()));
}
throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
} else {
if (log.isDebugEnabled()) {
log.error("Cannot find API product in Solace Broker");
log.error(EntityUtils.toString(response.getEntity()));
}
throw new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase());
}
}
return numberOfDeployedEnvironmentsInSolace == environments.size();
}
use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.
the class SolaceNotifierUtils method getSolaceAdminApis.
/**
* Get and patch client id for Solace application
*
* @return SolaceAdminApis object to invoke Solace
* @throws APIManagementException If the Solace env configuration if not provided properly
*/
public static SolaceAdminApis getSolaceAdminApis() throws APIManagementException {
Map<String, Environment> thirdPartyEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
Environment solaceEnvironment = null;
for (Map.Entry<String, Environment> entry : thirdPartyEnvironments.entrySet()) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
solaceEnvironment = entry.getValue();
}
}
if (solaceEnvironment != null) {
return new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
} else {
throw new APIManagementException("Solace Environment configurations are not provided properly");
}
}
Aggregations