Search in sources :

Example 1 with SubscriptionPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent in project carbon-apimgt by wso2.

the class APIProviderImpl method updatePolicy.

public void updatePolicy(Policy policy) throws APIManagementException {
    String oldKeyTemplate = null;
    String newKeyTemplate = null;
    if (policy instanceof APIPolicy) {
        APIPolicy apiPolicy = (APIPolicy) policy;
        apiPolicy.setUserLevel(PolicyConstants.ACROSS_ALL);
        // TODO this has done due to update policy method not deleting the second level entries when delete on cascade
        // TODO Need to fix appropriately
        List<Pipeline> pipelineList = apiPolicy.getPipelines();
        if (pipelineList != null && pipelineList.size() != 0) {
            Iterator<Pipeline> pipelineIterator = pipelineList.iterator();
            while (pipelineIterator.hasNext()) {
                Pipeline pipeline = pipelineIterator.next();
                if (!pipeline.isEnabled()) {
                    pipelineIterator.remove();
                } else {
                    if (pipeline.getConditions() != null && pipeline.getConditions().size() != 0) {
                        Iterator<Condition> conditionIterator = pipeline.getConditions().iterator();
                        while (conditionIterator.hasNext()) {
                            Condition condition = conditionIterator.next();
                            if (JavaUtils.isFalseExplicitly(condition.getConditionEnabled())) {
                                conditionIterator.remove();
                            }
                        }
                    } else {
                        pipelineIterator.remove();
                    }
                }
            }
        }
        APIPolicy existingPolicy = apiMgtDAO.getAPIPolicy(policy.getPolicyName(), policy.getTenantId());
        apiPolicy = apiMgtDAO.updateAPIPolicy(apiPolicy);
        // TODO rename level to  resource or appropriate name
        APIManagerConfiguration config = getAPIManagerConfiguration();
        if (log.isDebugEnabled()) {
            log.debug("Calling invalidation cache for API Policy for tenant ");
        }
        String policyContext = APIConstants.POLICY_CACHE_CONTEXT + "/t/" + apiPolicy.getTenantDomain() + "/";
        invalidateResourceCache(policyContext, null, Collections.EMPTY_SET);
        List<Integer> addedConditionGroupIds = new ArrayList<>();
        List<Integer> deletedConditionGroupIds = new ArrayList<>();
        for (Pipeline pipeline : existingPolicy.getPipelines()) {
            deletedConditionGroupIds.add(pipeline.getId());
        }
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            addedConditionGroupIds.add(pipeline.getId());
        }
        APIPolicyEvent apiPolicyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), tenantId, apiPolicy.getTenantDomain(), apiPolicy.getPolicyId(), apiPolicy.getPolicyName(), apiPolicy.getDefaultQuotaPolicy().getType(), addedConditionGroupIds, deletedConditionGroupIds);
        APIUtil.sendNotification(apiPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof ApplicationPolicy) {
        ApplicationPolicy appPolicy = (ApplicationPolicy) policy;
        apiMgtDAO.updateApplicationPolicy(appPolicy);
        // policy id is not set. retrieving policy to get the id.
        ApplicationPolicy retrievedPolicy = apiMgtDAO.getApplicationPolicy(appPolicy.getPolicyName(), tenantId);
        ApplicationPolicyEvent applicationPolicyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), tenantId, appPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), appPolicy.getPolicyName(), appPolicy.getDefaultQuotaPolicy().getType());
        APIUtil.sendNotification(applicationPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof SubscriptionPolicy) {
        SubscriptionPolicy subPolicy = (SubscriptionPolicy) policy;
        apiMgtDAO.updateSubscriptionPolicy(subPolicy);
        String monetizationPlan = subPolicy.getMonetizationPlan();
        Map<String, String> monetizationPlanProperties = subPolicy.getMonetizationPlanProperties();
        // call the monetization extension point to create plans (if any)
        if (StringUtils.isNotBlank(monetizationPlan) && MapUtils.isNotEmpty(monetizationPlanProperties)) {
            updateMonetizationPlan(subPolicy);
        }
        // policy id is not set. retrieving policy to get the id.
        SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(subPolicy.getPolicyName(), tenantId);
        SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), tenantId, subPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), subPolicy.getPolicyName(), subPolicy.getDefaultQuotaPolicy().getType(), subPolicy.getRateLimitCount(), subPolicy.getRateLimitTimeUnit(), subPolicy.isStopOnQuotaReach(), subPolicy.getGraphQLMaxDepth(), subPolicy.getGraphQLMaxComplexity(), subPolicy.getSubscriberCount());
        APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof GlobalPolicy) {
        GlobalPolicy globalPolicy = (GlobalPolicy) policy;
        // getting key templates before updating database
        GlobalPolicy oldGlobalPolicy = apiMgtDAO.getGlobalPolicy(policy.getPolicyName());
        oldKeyTemplate = oldGlobalPolicy.getKeyTemplate();
        newKeyTemplate = globalPolicy.getKeyTemplate();
        apiMgtDAO.updateGlobalPolicy(globalPolicy);
        GlobalPolicy retrievedPolicy = apiMgtDAO.getGlobalPolicy(globalPolicy.getPolicyName());
        GlobalPolicyEvent globalPolicyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_UPDATE.name(), tenantId, globalPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), globalPolicy.getPolicyName());
        APIUtil.sendNotification(globalPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else {
        String msg = "Policy type " + policy.getClass().getName() + " is not supported";
        log.error(msg);
        throw new UnsupportedPolicyTypeException(msg);
    }
    // publishing keytemplate after update
    if (oldKeyTemplate != null && newKeyTemplate != null) {
        publishKeyTemplateEvent(oldKeyTemplate, "remove");
        publishKeyTemplateEvent(newKeyTemplate, "add");
    }
}
Also used : Condition(org.wso2.carbon.apimgt.api.model.policy.Condition) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) ArrayList(java.util.ArrayList) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)

Example 2 with SubscriptionPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent in project carbon-apimgt by wso2.

the class APIProviderImpl method addPolicy.

/**
 * Deploy policy to global CEP and persist the policy object
 *
 * @param policy policy object
 */
public void addPolicy(Policy policy) throws APIManagementException {
    if (policy instanceof APIPolicy) {
        APIPolicy apiPolicy = (APIPolicy) policy;
        // Check if there's a policy exists before adding the new policy
        Policy existingPolicy = getAPIPolicy(userNameWithoutChange, apiPolicy.getPolicyName());
        if (existingPolicy != null) {
            handleException("Advanced Policy with name " + apiPolicy.getPolicyName() + " already exists");
        }
        apiPolicy.setUserLevel(PolicyConstants.ACROSS_ALL);
        apiPolicy = apiMgtDAO.addAPIPolicy(apiPolicy);
        List<Integer> addedConditionGroupIds = new ArrayList<>();
        for (Pipeline pipeline : apiPolicy.getPipelines()) {
            addedConditionGroupIds.add(pipeline.getId());
        }
        APIPolicyEvent apiPolicyEvent = new APIPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, apiPolicy.getTenantDomain(), apiPolicy.getPolicyId(), apiPolicy.getPolicyName(), apiPolicy.getDefaultQuotaPolicy().getType(), addedConditionGroupIds, null);
        APIUtil.sendNotification(apiPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof ApplicationPolicy) {
        ApplicationPolicy appPolicy = (ApplicationPolicy) policy;
        // Check if there's a policy exists before adding the new policy
        Policy existingPolicy = getApplicationPolicy(userNameWithoutChange, appPolicy.getPolicyName());
        if (existingPolicy != null) {
            handleException("Application Policy with name " + appPolicy.getPolicyName() + " already exists");
        }
        apiMgtDAO.addApplicationPolicy(appPolicy);
        // policy id is not set. retrieving policy to get the id.
        ApplicationPolicy retrievedPolicy = apiMgtDAO.getApplicationPolicy(appPolicy.getPolicyName(), tenantId);
        ApplicationPolicyEvent applicationPolicyEvent = new ApplicationPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, appPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), appPolicy.getPolicyName(), appPolicy.getDefaultQuotaPolicy().getType());
        APIUtil.sendNotification(applicationPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof SubscriptionPolicy) {
        SubscriptionPolicy subPolicy = (SubscriptionPolicy) policy;
        // Check if there's a policy exists before adding the new policy
        Policy existingPolicy = getSubscriptionPolicy(userNameWithoutChange, subPolicy.getPolicyName());
        if (existingPolicy != null) {
            handleException("Subscription Policy with name " + subPolicy.getPolicyName() + " already exists");
        }
        apiMgtDAO.addSubscriptionPolicy(subPolicy);
        String monetizationPlan = subPolicy.getMonetizationPlan();
        Map<String, String> monetizationPlanProperties = subPolicy.getMonetizationPlanProperties();
        if (StringUtils.isNotBlank(monetizationPlan) && MapUtils.isNotEmpty(monetizationPlanProperties)) {
            createMonetizationPlan(subPolicy);
        }
        // policy id is not set. retrieving policy to get the id.
        SubscriptionPolicy retrievedPolicy = apiMgtDAO.getSubscriptionPolicy(subPolicy.getPolicyName(), tenantId);
        SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, subPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), subPolicy.getPolicyName(), subPolicy.getDefaultQuotaPolicy().getType(), subPolicy.getRateLimitCount(), subPolicy.getRateLimitTimeUnit(), subPolicy.isStopOnQuotaReach(), subPolicy.getGraphQLMaxDepth(), subPolicy.getGraphQLMaxComplexity(), subPolicy.getSubscriberCount());
        APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else if (policy instanceof GlobalPolicy) {
        GlobalPolicy globalPolicy = (GlobalPolicy) policy;
        // checking if policy already exist
        Policy existingPolicy = getGlobalPolicy(globalPolicy.getPolicyName());
        if (existingPolicy != null) {
            throw new APIManagementException("Policy name already exists");
        }
        apiMgtDAO.addGlobalPolicy(globalPolicy);
        publishKeyTemplateEvent(globalPolicy.getKeyTemplate(), "add");
        GlobalPolicy retrievedPolicy = apiMgtDAO.getGlobalPolicy(globalPolicy.getPolicyName());
        GlobalPolicyEvent globalPolicyEvent = new GlobalPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, globalPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), globalPolicy.getPolicyName());
        APIUtil.sendNotification(globalPolicyEvent, APIConstants.NotifierType.POLICY.name());
    } else {
        String msg = "Policy type " + policy.getClass().getName() + " is not supported";
        log.error(msg);
        throw new UnsupportedPolicyTypeException(msg);
    }
}
Also used : ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) OperationPolicy(org.wso2.carbon.apimgt.api.model.OperationPolicy) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) Policy(org.wso2.carbon.apimgt.api.model.policy.Policy) GlobalPolicy(org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy) UnsupportedPolicyTypeException(org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException) ArrayList(java.util.ArrayList) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) Pipeline(org.wso2.carbon.apimgt.api.model.policy.Pipeline) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) SubscriptionPolicy(org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy) ApplicationPolicy(org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy) APIPolicy(org.wso2.carbon.apimgt.api.model.policy.APIPolicy) GlobalPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)

Example 3 with SubscriptionPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent in project carbon-apimgt by wso2.

the class KeyManagerDataServiceImpl method getSubscriptionPolicyFromSubscriptionPolicyEvent.

private SubscriptionPolicy getSubscriptionPolicyFromSubscriptionPolicyEvent(SubscriptionPolicyEvent event) {
    SubscriptionPolicy policy = new SubscriptionPolicy();
    policy.setId(event.getPolicyId());
    policy.setQuotaType(event.getQuotaType());
    policy.setRateLimitCount(event.getRateLimitCount());
    policy.setRateLimitTimeUnit(event.getRateLimitTimeUnit());
    policy.setStopOnQuotaReach(event.isStopOnQuotaReach());
    policy.setTenantId(event.getTenantId());
    policy.setTierName(event.getPolicyName());
    policy.setGraphQLMaxComplexity(event.getGraphQLMaxComplexity());
    policy.setGraphQLMaxDepth(event.getGraphQLMaxDepth());
    if (log.isDebugEnabled()) {
        log.debug("Event: " + event.toString());
        log.debug("Converted : " + policy.toString());
    }
    return policy;
}
Also used : SubscriptionPolicy(org.wso2.carbon.apimgt.keymgt.model.entity.SubscriptionPolicy)

Example 4 with SubscriptionPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent in project carbon-apimgt by wso2.

the class APIUtil method deployRetrievedSubscriptionPolicy.

private static void deployRetrievedSubscriptionPolicy(int tenantId, SubscriptionPolicy retrievedPolicy) {
    SubscriptionPolicyEvent subscriptionPolicyEvent = new SubscriptionPolicyEvent(UUID.randomUUID().toString(), System.currentTimeMillis(), APIConstants.EventType.POLICY_CREATE.name(), tenantId, retrievedPolicy.getTenantDomain(), retrievedPolicy.getPolicyId(), retrievedPolicy.getPolicyName(), retrievedPolicy.getDefaultQuotaPolicy().getType(), retrievedPolicy.getRateLimitCount(), retrievedPolicy.getRateLimitTimeUnit(), retrievedPolicy.isStopOnQuotaReach(), retrievedPolicy.getGraphQLMaxDepth(), retrievedPolicy.getGraphQLMaxComplexity(), retrievedPolicy.getSubscriberCount());
    APIUtil.sendNotification(subscriptionPolicyEvent, APIConstants.NotifierType.POLICY.name());
}
Also used : SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)

Example 5 with SubscriptionPolicyEvent

use of org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent 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());
    }
}
Also used : SubscriptionEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionEvent) ApplicationEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationEvent) Gson(com.google.gson.Gson) DeployAPIInGatewayEvent(org.wso2.carbon.apimgt.impl.notifier.events.DeployAPIInGatewayEvent) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) EndpointCertificateDeployer(org.wso2.carbon.apimgt.gateway.EndpointCertificateDeployer) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) ApplicationRegistrationEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationRegistrationEvent) GoogleAnalyticsConfigDeployer(org.wso2.carbon.apimgt.gateway.GoogleAnalyticsConfigDeployer) APIEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIEvent) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) CertificateEvent(org.wso2.carbon.apimgt.impl.notifier.events.CertificateEvent) APIPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent) PolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent) SubscriptionPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent) ApplicationPolicyEvent(org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent) ScopeEvent(org.wso2.carbon.apimgt.impl.notifier.events.ScopeEvent) GoogleAnalyticsConfigEvent(org.wso2.carbon.apimgt.impl.notifier.events.GoogleAnalyticsConfigEvent) HashSet(java.util.HashSet)

Aggregations

SubscriptionPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.SubscriptionPolicyEvent)9 APIPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.APIPolicyEvent)5 ApplicationPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.ApplicationPolicyEvent)5 GlobalPolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.GlobalPolicyEvent)4 SubscriptionPolicy (org.wso2.carbon.apimgt.throttle.policy.deployer.dto.SubscriptionPolicy)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 APIPolicy (org.wso2.carbon.apimgt.api.model.policy.APIPolicy)3 ApplicationPolicy (org.wso2.carbon.apimgt.api.model.policy.ApplicationPolicy)3 GlobalPolicy (org.wso2.carbon.apimgt.api.model.policy.GlobalPolicy)3 Pipeline (org.wso2.carbon.apimgt.api.model.policy.Pipeline)3 SubscriptionPolicy (org.wso2.carbon.apimgt.api.model.policy.SubscriptionPolicy)3 Gson (com.google.gson.Gson)2 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)2 UnsupportedPolicyTypeException (org.wso2.carbon.apimgt.api.UnsupportedPolicyTypeException)2 PolicyEvent (org.wso2.carbon.apimgt.impl.notifier.events.PolicyEvent)2 SubscriptionDataStore (org.wso2.carbon.apimgt.keymgt.model.SubscriptionDataStore)2 HashSet (java.util.HashSet)1 OperationPolicy (org.wso2.carbon.apimgt.api.model.OperationPolicy)1