use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method deletePolicy.
@Override
public void deletePolicy(PolicyValidationData policyValidationData) throws GatewayException {
if (policyValidationData != null) {
PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_DELETE);
policyEvent.setId(policyValidationData.getId());
policyEvent.setName(policyValidationData.getName());
policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
publishToThrottleTopic(policyEvent);
if (log.isDebugEnabled()) {
log.debug("Policy : " + policyValidationData.getName() + " delete event has been successfully " + "published " + "to broker");
}
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent in project carbon-apimgt by wso2.
the class APIGatewayPublisherImpl method addPolicy.
@Override
public void addPolicy(PolicyValidationData policyValidationData) throws GatewayException {
if (policyValidationData != null) {
PolicyEvent policyEvent = new PolicyEvent(APIMgtConstants.GatewayEventTypes.POLICY_CREATE);
policyEvent.setId(policyValidationData.getId());
policyEvent.setName(policyValidationData.getName());
policyEvent.setStopOnQuotaReach(policyValidationData.isStopOnQuotaReach());
publishToThrottleTopic(policyEvent);
if (log.isDebugEnabled()) {
log.debug("Policy : " + policyValidationData.getName() + " add event has been successfully published " + "to broker");
}
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent 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.PolicyEvent in project carbon-apimgt by wso2.
the class PolicyUtil method deployPolicy.
/**
* Deploy the given throttle policy in the Traffic Manager.
*
* @param policy policy object
* @param policyEvent policy event object which was triggered
*/
public static void deployPolicy(Policy policy, PolicyEvent policyEvent) {
EventProcessorService eventProcessorService = ServiceReferenceHolder.getInstance().getEventProcessorService();
ThrottlePolicyTemplateBuilder policyTemplateBuilder = new ThrottlePolicyTemplateBuilder();
Map<String, String> policiesToDeploy = new HashMap<>();
List<String> policiesToUndeploy = new ArrayList<>();
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(APIConstants.SUPER_TENANT_DOMAIN, true);
String policyFile;
String policyString;
if (Policy.PolicyType.SUBSCRIPTION.equals(policy.getType()) && policy instanceof SubscriptionPolicy) {
// Add Subscription policy
policyFile = String.join(APIConstants.DELEM_UNDERSCORE, policy.getTenantDomain(), PolicyConstants.POLICY_LEVEL_SUB, policy.getName());
policyString = policyTemplateBuilder.getThrottlePolicyForSubscriptionLevel((SubscriptionPolicy) policy);
policiesToDeploy.put(policyFile, policyString);
} else if (Policy.PolicyType.APPLICATION.equals(policy.getType()) && policy instanceof ApplicationPolicy) {
// Add Application policy
policyFile = String.join(APIConstants.DELEM_UNDERSCORE, policy.getTenantDomain(), PolicyConstants.POLICY_LEVEL_APP, policy.getName());
policyString = policyTemplateBuilder.getThrottlePolicyForAppLevel((ApplicationPolicy) policy);
policiesToDeploy.put(policyFile, policyString);
} else if (Policy.PolicyType.API.equals(policy.getType()) && policy instanceof ApiPolicy) {
// Add API policy
policiesToDeploy = policyTemplateBuilder.getThrottlePolicyForAPILevel((ApiPolicy) policy);
String defaultPolicy = policyTemplateBuilder.getThrottlePolicyForAPILevelDefault((ApiPolicy) policy);
policyFile = String.join(APIConstants.DELEM_UNDERSCORE, policy.getTenantDomain(), PolicyConstants.POLICY_LEVEL_RESOURCE, policy.getName());
String defaultPolicyName = policyFile + APIConstants.THROTTLE_POLICY_DEFAULT;
policiesToDeploy.put(defaultPolicyName, defaultPolicy);
if (policyEvent instanceof APIPolicyEvent) {
List<Integer> deletedConditionGroupIds = ((APIPolicyEvent) policyEvent).getDeletedConditionGroupIds();
// Undeploy removed condition groups
if (deletedConditionGroupIds != null) {
for (int conditionGroupId : deletedConditionGroupIds) {
policiesToUndeploy.add(policyFile + APIConstants.THROTTLE_POLICY_CONDITION + conditionGroupId);
}
}
}
} else if (Policy.PolicyType.GLOBAL.equals(policy.getType()) && policy instanceof GlobalPolicy) {
// Add Global policy
GlobalPolicy globalPolicy = (GlobalPolicy) policy;
policyFile = String.join(APIConstants.DELEM_UNDERSCORE, PolicyConstants.POLICY_LEVEL_GLOBAL, policy.getName());
policyString = policyTemplateBuilder.getThrottlePolicyForGlobalLevel(globalPolicy);
policiesToDeploy.put(policyFile, policyString);
}
// Undeploy removed policies
undeployPolicies(policiesToUndeploy);
for (Map.Entry<String, String> pair : policiesToDeploy.entrySet()) {
String policyPlanName = pair.getKey();
String flowString = pair.getValue();
String executionPlan = null;
try {
executionPlan = eventProcessorService.getActiveExecutionPlan(policyPlanName);
} catch (ExecutionPlanConfigurationException e) {
// Deploy new policies
eventProcessorService.deployExecutionPlan(flowString);
}
if (executionPlan != null) {
// Update existing policies
eventProcessorService.editActiveExecutionPlan(flowString, policyPlanName);
}
}
} catch (APITemplateException e) {
log.error("Error in creating execution plan", e);
} catch (ExecutionPlanConfigurationException | ExecutionPlanDependencyValidationException e) {
log.error("Error in deploying execution plan", e);
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
use of org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent in project carbon-apimgt by wso2.
the class PolicyUtilTest method testUpdatePolicy_SubType.
@Test
public void testUpdatePolicy_SubType() throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
Mockito.when(eventProcessorService.getActiveExecutionPlan(Mockito.anyString())).thenReturn("EXECUTION_PLAN");
SubscriptionPolicy policy = TestUtil.getPolicySubLevel();
SubscriptionPolicyEvent policyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), -1234, policy.getTenantDomain(), policy.getId(), policy.getName(), policy.getDefaultLimit().getQuotaType(), policy.getRateLimitCount(), policy.getRateLimitTimeUnit(), policy.isStopOnQuotaReach(), policy.getGraphQLMaxDepth(), policy.getGraphQLMaxComplexity(), policy.getSubscriberCount());
PolicyUtil.deployPolicy(policy, policyEvent);
Mockito.verify(eventProcessorService, Mockito.times(1)).editActiveExecutionPlan(Mockito.anyString(), Mockito.anyString());
}
Aggregations