Search in sources :

Example 11 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIProviderImpl method deleteAPIRevision.

/**
 * Delete an API Revision
 *
 * @param apiId API UUID
 * @param apiRevisionId API Revision UUID
 * @param organization identifier of the organization
 * @throws APIManagementException if failed to delete APIRevision
 */
@Override
public void deleteAPIRevision(String apiId, String apiRevisionId, String organization) throws APIManagementException {
    APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
    if (apiIdentifier == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
    }
    APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
    if (apiRevision == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
    }
    List<APIRevisionDeployment> apiRevisionDeploymentsResponse = getAPIRevisionDeploymentList(apiRevisionId);
    if (apiRevisionDeploymentsResponse.size() != 0) {
        String errorMessage = "Couldn't delete API revision since API revision is currently deployed to a gateway" + "." + "You need to undeploy the API Revision from the gateway before attempting deleting API Revision: " + apiRevision.getRevisionUUID();
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.EXISTING_API_REVISION_DEPLOYMENT_FOUND, apiRevisionId));
    }
    apiIdentifier.setUuid(apiId);
    try {
        apiPersistenceInstance.deleteAPIRevision(new Organization(organization), apiIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
    } catch (APIPersistenceException e) {
        String errorMessage = "Failed to delete registry artifacts";
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_DELETING_API_REVISION, apiRevision.getApiUUID()));
    }
    apiMgtDAO.deleteAPIRevision(apiRevision);
    gatewayArtifactsMgtDAO.deleteGatewayArtifact(apiRevision.getApiUUID(), apiRevision.getRevisionUUID());
    if (artifactSaver != null) {
        try {
            artifactSaver.removeArtifact(apiRevision.getApiUUID(), apiIdentifier.getApiName(), apiIdentifier.getVersion(), apiRevision.getRevisionUUID(), organization);
        } catch (ArtifactSynchronizerException e) {
            log.error("Error while deleting Runtime artifacts from artifact Store", e);
        }
    }
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArtifactSynchronizerException(org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 12 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIProviderImpl method restoreAPIRevision.

/**
 * Restore a provided API Revision as the current API of the API
 *
 * @param apiId API UUID
 * @param apiRevisionId API Revision UUID
 * @throws APIManagementException if failed to restore APIRevision
 */
@Override
public void restoreAPIRevision(String apiId, String apiRevisionId, String organization) throws APIManagementException {
    APIIdentifier apiIdentifier = APIUtil.getAPIIdentifierFromUUID(apiId);
    if (apiIdentifier == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
    }
    APIRevision apiRevision = apiMgtDAO.getRevisionByRevisionUUID(apiRevisionId);
    if (apiRevision == null) {
        throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API Revision with Revision UUID: " + apiRevisionId, ExceptionCodes.from(ExceptionCodes.API_REVISION_NOT_FOUND, apiRevisionId));
    }
    apiIdentifier.setUuid(apiId);
    try {
        apiPersistenceInstance.restoreAPIRevision(new Organization(organization), apiIdentifier.getUUID(), apiRevision.getRevisionUUID(), apiRevision.getId());
    } catch (APIPersistenceException e) {
        String errorMessage = "Failed to restore registry artifacts";
        throw new APIManagementException(errorMessage, ExceptionCodes.from(ExceptionCodes.ERROR_RESTORING_API_REVISION, apiRevision.getApiUUID()));
    }
    apiMgtDAO.restoreAPIRevision(apiRevision);
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIPersistenceException(org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException) Organization(org.wso2.carbon.apimgt.persistence.dto.Organization) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.api.APIMgtResourceNotFoundException)

Example 13 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class APIUtil method notifyAPIStateChangeToAssociatedDocuments.

/**
 * Notify document artifacts if an api state change occured. This change is required to re-trigger the document
 * indexer so that the documnet indexes will be updated with the new associated api status.
 *
 * @param apiArtifact
 * @param registry
 * @throws RegistryException
 * @throws APIManagementException
 */
public static void notifyAPIStateChangeToAssociatedDocuments(GenericArtifact apiArtifact, Registry registry) throws RegistryException, APIManagementException {
    Association[] docAssociations = registry.getAssociations(apiArtifact.getPath(), APIConstants.DOCUMENTATION_ASSOCIATION);
    for (Association association : docAssociations) {
        String documentResourcePath = association.getDestinationPath();
        Resource docResource = registry.get(documentResourcePath);
        String oldStateChangeIndicatorStatus = docResource.getProperty(APIConstants.API_STATE_CHANGE_INDICATOR);
        String newStateChangeIndicatorStatus = "false";
        if (oldStateChangeIndicatorStatus != null) {
            newStateChangeIndicatorStatus = String.valueOf(!Boolean.parseBoolean(oldStateChangeIndicatorStatus));
        }
        docResource.setProperty(APIConstants.API_STATE_CHANGE_INDICATOR, "false");
        registry.put(documentResourcePath, docResource);
    }
}
Also used : Association(org.wso2.carbon.registry.core.Association) Resource(org.wso2.carbon.registry.core.Resource) APIProductResource(org.wso2.carbon.apimgt.api.model.APIProductResource) APIResource(org.wso2.carbon.apimgt.api.doc.model.APIResource)

Example 14 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts in project carbon-apimgt by wso2.

the class RuntimeArtifactGeneratorUtil method getRuntimeArtifacts.

private static List<APIRuntimeArtifactDto> getRuntimeArtifacts(String apiId, String gatewayLabel, String tenantDomain) throws APIManagementException {
    List<APIRuntimeArtifactDto> gatewayArtifacts;
    if (StringUtils.isNotEmpty(gatewayLabel)) {
        byte[] decodedValue = Base64.decodeBase64(gatewayLabel.getBytes());
        String[] gatewayLabels = new String(decodedValue).split("\\|");
        if (StringUtils.isNotEmpty(apiId)) {
            gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByAPIIDAndLabel(apiId, gatewayLabels, tenantDomain);
        } else {
            gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifactsByLabel(gatewayLabels, tenantDomain);
        }
    } else {
        gatewayArtifacts = gatewayArtifactsMgtDAO.retrieveGatewayArtifacts(tenantDomain);
    }
    if (gatewayArtifacts != null) {
        if (gatewayArtifacts.isEmpty()) {
            throw new APIManagementException("No API Artifacts", ExceptionCodes.NO_API_ARTIFACT_FOUND);
        }
        for (APIRuntimeArtifactDto apiRuntimeArtifactDto : gatewayArtifacts) {
            String organizationId = gatewayArtifactsMgtDAO.retrieveOrganization(apiRuntimeArtifactDto.getApiId());
            if (organizationId != null) {
                apiRuntimeArtifactDto.setOrganization(organizationId);
            }
        }
    }
    if (gatewayArtifacts == null || gatewayArtifacts.isEmpty()) {
        return null;
    }
    return gatewayArtifacts;
}
Also used : APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException)

Example 15 with Artifacts

use of org.wso2.ei.dashboard.core.rest.model.Artifacts 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

File (java.io.File)23 IOException (java.io.IOException)18 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)18 ArrayList (java.util.ArrayList)17 CAppArtifacts (org.wso2.ei.dashboard.core.rest.model.CAppArtifacts)16 Operation (io.swagger.v3.oas.annotations.Operation)15 ApiResponse (io.swagger.v3.oas.annotations.responses.ApiResponse)15 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)15 Response (javax.ws.rs.core.Response)15 Artifact (org.wso2.carbon.application.deployer.config.Artifact)15 Artifacts (org.wso2.ei.dashboard.core.rest.model.Artifacts)15 DeploymentException (org.apache.axis2.deployment.DeploymentException)11 CappFile (org.wso2.carbon.application.deployer.config.CappFile)11 Deployer (org.apache.axis2.deployment.Deployer)10 ArtifactSynchronizerException (org.wso2.carbon.apimgt.impl.gatewayartifactsynchronizer.exception.ArtifactSynchronizerException)10 Organization (org.wso2.carbon.apimgt.persistence.dto.Organization)10 InputStream (java.io.InputStream)9 FileInputStream (java.io.FileInputStream)7 APIRuntimeArtifactDto (org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto)7 APIPersistenceException (org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException)7