use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class SolaceAdminApis method deleteApplication.
/**
* Delete application from Solace Broker
*
* @param organization name of the Organization
* @param uuid UUID of Application object to be deleted
* @return CloseableHttpResponse of the DELETE call
*/
public CloseableHttpResponse deleteApplication(String organization, String uuid) {
URL serviceEndpointURL = new URL(baseUrl);
HttpClient httpClient = APIUtil.getHttpClient(serviceEndpointURL.getPort(), serviceEndpointURL.getProtocol());
HttpDelete httpDelete = new HttpDelete(baseUrl + "/" + organization + "/developers/" + developerUserName + "/apps/" + uuid);
httpDelete.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedCredentials());
try {
return APIUtil.executeHTTPRequest(httpDelete, httpClient);
} catch (IOException | APIManagementException e) {
log.error(e.getMessage());
}
return null;
}
use of org.wso2.carbon.apimgt.core.api.Broker in project carbon-apimgt by wso2.
the class SolaceBrokerDeployer method undeployWhenRetire.
/**
* Undeploy API artifact from provided environment in the external gateway when Api is retired
*
* @param api API to be undeployed from the external gateway
* @param environment Environment needed to be undeployed API from the external gateway
* @throws DeployerException if error occurs when undeploying APIs from the external gateway
*/
public boolean undeployWhenRetire(API api, Environment environment) throws DeployerException {
Application application;
APIProvider apiProvider;
// Remove subscription
try {
apiProvider = APIManagerFactory.getInstance().getAPIProvider(CarbonContext.getThreadLocalCarbonContext().getUsername());
List<SubscribedAPI> apiUsages = apiProvider.getAPIUsageByAPIId(api.getUuid(), api.getOrganization());
for (SubscribedAPI usage : apiUsages) {
application = usage.getApplication();
// Check whether the subscription is belongs to an API deployed in Solace
if (SolaceConstants.SOLACE_ENVIRONMENT.equals(api.getGatewayVendor())) {
SolaceNotifierUtils.unsubscribeAPIProductFromSolaceApplication(api, application);
}
}
} catch (APIManagementException e) {
throw new DeployerException("Error occurred when removing subscriptions of the API " + api.getUuid() + "to be retired", e);
}
// undeploy API from Solace
boolean deletedFromSolace = undeploy(api.getId().getName(), api.getId().getVersion(), api.getContext(), environment);
if (!deletedFromSolace) {
throw new DeployerException("Error while deleting API product of API " + api.getUuid() + "from Solace " + "broker");
}
return true;
}
use of org.wso2.carbon.apimgt.core.api.Broker 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.core.api.Broker 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.core.api.Broker 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();
}
Aggregations