use of org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent in project carbon-apimgt by wso2.
the class GatewayJMSMessageListener method handleNotificationMessage.
private void handleNotificationMessage(String eventType, long timestamp, String encodedEvent) {
byte[] eventDecoded = Base64.decodeBase64(encodedEvent);
String eventJson = new String(eventDecoded);
if (APIConstants.EventType.DEPLOY_API_IN_GATEWAY.name().equals(eventType) || APIConstants.EventType.REMOVE_API_FROM_GATEWAY.name().equals(eventType)) {
DeployAPIInGatewayEvent gatewayEvent = new Gson().fromJson(new String(eventDecoded), DeployAPIInGatewayEvent.class);
String tenantDomain = gatewayEvent.getTenantDomain();
boolean tenantLoaded = ServiceReferenceHolder.getInstance().isTenantLoaded(tenantDomain);
if (!tenantLoaded) {
String syncKey = tenantDomain.concat("__").concat(this.getClass().getName());
synchronized (syncKey.intern()) {
tenantLoaded = ServiceReferenceHolder.getInstance().isTenantLoaded(tenantDomain);
if (!tenantLoaded) {
APIUtil.loadTenantConfigBlockingMode(tenantDomain);
}
}
}
if (tenantLoaded) {
Set<String> systemConfiguredGatewayLabels = new HashSet(gatewayEvent.getGatewayLabels());
systemConfiguredGatewayLabels.retainAll(gatewayArtifactSynchronizerProperties.getGatewayLabels());
if (!systemConfiguredGatewayLabels.isEmpty()) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().updateDeployedAPIRevision(gatewayEvent);
if (EventType.DEPLOY_API_IN_GATEWAY.name().equals(eventType)) {
boolean tenantFlowStarted = false;
try {
startTenantFlow(tenantDomain);
tenantFlowStarted = true;
inMemoryApiDeployer.deployAPI(gatewayEvent);
} catch (ArtifactSynchronizerException e) {
log.error("Error in deploying artifacts for " + gatewayEvent.getUuid() + "in the Gateway");
} finally {
if (tenantFlowStarted) {
endTenantFlow();
}
}
}
if (APIConstants.EventType.REMOVE_API_FROM_GATEWAY.name().equals(eventType)) {
boolean tenantFlowStarted = false;
try {
startTenantFlow(tenantDomain);
tenantFlowStarted = true;
inMemoryApiDeployer.unDeployAPI(gatewayEvent);
} catch (ArtifactSynchronizerException e) {
log.error("Error in undeploying artifacts");
} finally {
if (tenantFlowStarted) {
endTenantFlow();
}
}
}
}
if (debugEnabled) {
log.debug("Event with ID " + gatewayEvent.getEventId() + " is received and " + gatewayEvent.getUuid() + " is successfully deployed/undeployed");
}
}
}
if (EventType.APPLICATION_CREATE.toString().equals(eventType) || EventType.APPLICATION_UPDATE.toString().equals(eventType)) {
ApplicationEvent event = new Gson().fromJson(eventJson, ApplicationEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateApplication(event);
;
} else if (EventType.SUBSCRIPTIONS_CREATE.toString().equals(eventType) || EventType.SUBSCRIPTIONS_UPDATE.toString().equals(eventType)) {
SubscriptionEvent event = new Gson().fromJson(eventJson, SubscriptionEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateSubscription(event);
} else if (EventType.API_UPDATE.toString().equals(eventType)) {
APIEvent event = new Gson().fromJson(eventJson, APIEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateAPI(event);
} else if (EventType.API_LIFECYCLE_CHANGE.toString().equals(eventType)) {
APIEvent event = new Gson().fromJson(eventJson, APIEvent.class);
if (APIStatus.CREATED.toString().equals(event.getApiStatus()) || APIStatus.RETIRED.toString().equals(event.getApiStatus())) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeAPI(event);
} else {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateAPI(event);
}
} else if (EventType.APPLICATION_REGISTRATION_CREATE.toString().equals(eventType)) {
ApplicationRegistrationEvent event = new Gson().fromJson(eventJson, ApplicationRegistrationEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateApplicationKeyMapping(event);
} else if (EventType.SUBSCRIPTIONS_DELETE.toString().equals(eventType)) {
SubscriptionEvent event = new Gson().fromJson(eventJson, SubscriptionEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeSubscription(event);
} else if (EventType.APPLICATION_DELETE.toString().equals(eventType)) {
ApplicationEvent event = new Gson().fromJson(eventJson, ApplicationEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeApplication(event);
} else if (EventType.REMOVE_APPLICATION_KEYMAPPING.toString().equals(eventType)) {
ApplicationRegistrationEvent event = new Gson().fromJson(eventJson, ApplicationRegistrationEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeApplicationKeyMapping(event);
} else if (EventType.SCOPE_CREATE.toString().equals(eventType)) {
ScopeEvent event = new Gson().fromJson(eventJson, ScopeEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addScope(event);
} else if (EventType.SCOPE_UPDATE.toString().equals(eventType)) {
ScopeEvent event = new Gson().fromJson(eventJson, ScopeEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addScope(event);
} else if (EventType.SCOPE_DELETE.toString().equals(eventType)) {
ScopeEvent event = new Gson().fromJson(eventJson, ScopeEvent.class);
ServiceReferenceHolder.getInstance().getKeyManagerDataService().deleteScope(event);
} else if (EventType.POLICY_CREATE.toString().equals(eventType) || EventType.POLICY_DELETE.toString().equals(eventType)) {
PolicyEvent event = new Gson().fromJson(eventJson, PolicyEvent.class);
boolean updatePolicy = false;
boolean deletePolicy = false;
if (EventType.POLICY_CREATE.toString().equals(eventType) || EventType.POLICY_UPDATE.toString().equals(eventType)) {
updatePolicy = true;
} else if (EventType.POLICY_DELETE.toString().equals(eventType)) {
deletePolicy = true;
}
if (event.getPolicyType() == PolicyType.API) {
APIPolicyEvent policyEvent = new Gson().fromJson(eventJson, APIPolicyEvent.class);
if (updatePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateAPIPolicy(policyEvent);
} else if (deletePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeAPIPolicy(policyEvent);
}
} else if (event.getPolicyType() == PolicyType.SUBSCRIPTION) {
SubscriptionPolicyEvent policyEvent = new Gson().fromJson(eventJson, SubscriptionPolicyEvent.class);
if (updatePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateSubscriptionPolicy(policyEvent);
} else if (deletePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeSubscriptionPolicy(policyEvent);
}
} else if (event.getPolicyType() == PolicyType.APPLICATION) {
ApplicationPolicyEvent policyEvent = new Gson().fromJson(eventJson, ApplicationPolicyEvent.class);
if (updatePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().addOrUpdateApplicationPolicy(policyEvent);
} else if (deletePolicy) {
ServiceReferenceHolder.getInstance().getKeyManagerDataService().removeApplicationPolicy(policyEvent);
}
}
} else if (EventType.ENDPOINT_CERTIFICATE_ADD.toString().equals(eventType) || EventType.ENDPOINT_CERTIFICATE_REMOVE.toString().equals(eventType)) {
CertificateEvent certificateEvent = new Gson().fromJson(eventJson, CertificateEvent.class);
if (EventType.ENDPOINT_CERTIFICATE_ADD.toString().equals(eventType)) {
try {
new EndpointCertificateDeployer(certificateEvent.getTenantDomain()).deployCertificate(certificateEvent.getAlias());
} catch (APIManagementException e) {
log.error(e);
}
} else if (EventType.ENDPOINT_CERTIFICATE_REMOVE.toString().equals(eventType)) {
boolean tenantFlowStarted = false;
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(certificateEvent.getTenantDomain(), true);
tenantFlowStarted = true;
CertificateManagerImpl.getInstance().deleteCertificateFromGateway(certificateEvent.getAlias());
} finally {
if (tenantFlowStarted) {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
} else if (EventType.GA_CONFIG_UPDATE.toString().equals(eventType)) {
GoogleAnalyticsConfigEvent googleAnalyticsConfigEvent = new Gson().fromJson(eventJson, GoogleAnalyticsConfigEvent.class);
try {
new GoogleAnalyticsConfigDeployer(googleAnalyticsConfigEvent.getTenantDomain()).deploy();
} catch (APIManagementException e) {
log.error(e);
}
} else if (EventType.UDATE_API_LOG_LEVEL.toString().equals(eventType)) {
APIEvent apiEvent = new Gson().fromJson(eventJson, APIEvent.class);
APILoggerManager.getInstance().updateLoggerMap(apiEvent.getApiContext(), apiEvent.getLogLevel());
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent in project carbon-apimgt by wso2.
the class SolaceApplicationNotifier method renameSolaceApplication.
/**
* Rename applications on the Solace broker
*
* @param event ApplicationEvent to rename Solace applications
* @throws NotifierException if error occurs when renaming applications on the Solace broker
*/
private void renameSolaceApplication(ApplicationEvent event) throws NotifierException {
// get list of subscribed APIs in the application
Subscriber subscriber = new Subscriber(event.getSubscriber());
try {
Application application = apiMgtDAO.getApplicationByUUID(event.getUuid());
Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(subscriber, event.getApplicationName(), event.getGroupId());
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
boolean isContainsSolaceApis = false;
String organizationNameOfSolaceDeployment = null;
labelOne: // Check whether the application needs to be updated has a Solace API subscription
for (SubscribedAPI api : subscriptions) {
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
for (APIRevisionDeployment deployment : deployments) {
if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
isContainsSolaceApis = true;
organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
break labelOne;
}
}
}
}
// Renaming application using Solace Admin Apis
if (isContainsSolaceApis) {
SolaceNotifierUtils.renameSolaceApplication(organizationNameOfSolaceDeployment, application);
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent 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.carbon.apimgt.impl.notifier.events.ApplicationEvent in project carbon-apimgt by wso2.
the class SolaceKeyGenNotifier method syncSolaceApplicationClientId.
/**
* Syncing consumer key of the dev portal applications with applications on the Solace broker
*
* @param event ApplicationEvent to sync Solace applications with dev portal applications
* @throws NotifierException if error occurs when patching applications on the Solace broker
*/
private void syncSolaceApplicationClientId(ApplicationRegistrationEvent event) throws NotifierException {
// get list of subscribed APIs in the application
try {
Application application = apiMgtDAO.getApplicationByUUID(event.getApplicationUUID());
Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
Set<SubscribedAPI> subscriptions = apiMgtDAO.getSubscribedAPIs(application.getSubscriber(), application.getName(), application.getGroupId());
boolean isContainsSolaceApis = false;
String organizationNameOfSolaceDeployment = null;
labelOne: // Check whether the application needs to be updated has a Solace API subscription
for (SubscribedAPI api : subscriptions) {
List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getIdentifier().getUUID());
for (APIRevisionDeployment deployment : deployments) {
if (gatewayEnvironments.containsKey(deployment.getDeployment())) {
if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(gatewayEnvironments.get(deployment.getDeployment()).getProvider())) {
isContainsSolaceApis = true;
organizationNameOfSolaceDeployment = gatewayEnvironments.get(deployment.getDeployment()).getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
break labelOne;
}
}
}
}
// Patching consumerKey to Solace application using Admin Apis
if (isContainsSolaceApis) {
if (application.getKeys() != null) {
String consumerSecret = null;
APIConsumer apiConsumer = APIManagerFactory.getInstance().getAPIConsumer(CarbonContext.getThreadLocalCarbonContext().getUsername());
Set<APIKey> consumerKeys = apiConsumer.getApplicationKeysOfApplication(application.getId());
for (APIKey key : consumerKeys) {
if (key.getConsumerKey().equals(event.getConsumerKey())) {
consumerSecret = key.getConsumerSecret();
}
}
SolaceNotifierUtils.patchSolaceApplicationClientId(organizationNameOfSolaceDeployment, application, event.getConsumerKey(), consumerSecret);
} else {
throw new NotifierException("Application keys are not found in the application : " + application.getName());
}
}
} catch (APIManagementException e) {
throw new NotifierException(e.getMessage());
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method deleteApplication.
@Override
public void deleteApplication(String applicationId) throws GatewayException {
if (applicationId != null) {
ApplicationEvent applicationEvent = new ApplicationEvent(APIMgtConstants.GatewayEventTypes.APPLICATION_DELETE);
applicationEvent.setApplicationId(applicationId);
publishToStoreTopic(applicationEvent);
if (log.isDebugEnabled()) {
log.debug("Application : " + applicationId + " deleted event has been successfully published " + "to broker");
}
}
}
Aggregations