Search in sources :

Example 26 with ContainerBasedGatewayException

use of org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method getContainerBasedGatewayGenerator.

/**
 * Get Container Based Gateway Generator
 *
 * @return containerBasedGatewayGenerator
 * @throws ContainerBasedGatewayException if error occurred while initializing container based gateway generator
 */
public ContainerBasedGatewayGenerator getContainerBasedGatewayGenerator() throws ContainerBasedGatewayException {
    if (containerBasedGatewayGenerator == null) {
        try {
            ContainerBasedGatewayConfiguration containerBasedGatewayConfiguration = ContainerBasedGatewayConfigBuilder.getContainerBasedGatewayConfiguration();
            String implClassName = containerBasedGatewayConfiguration.getImplClass();
            Map<String, String> implParameters = containerBasedGatewayConfiguration.getImplParameters();
            containerBasedGatewayGenerator = (ContainerBasedGatewayGenerator) Class.forName(implClassName).newInstance();
            containerBasedGatewayGenerator.initImpl(implParameters);
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            throw new ContainerBasedGatewayException("Error occurred while initializing container based gateway " + "generator", e, ExceptionCodes.ERROR_INITIALIZING_DEDICATED_CONTAINER_BASED_GATEWAY);
        }
    }
    return containerBasedGatewayGenerator;
}
Also used : ContainerBasedGatewayConfiguration(org.wso2.carbon.apimgt.core.configuration.models.ContainerBasedGatewayConfiguration) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)

Example 27 with ContainerBasedGatewayException

use of org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException in project carbon-apimgt by wso2.

the class APIStateChangeWorkflow method completeWorkflow.

@Override
public WorkflowResponse completeWorkflow(WorkflowExecutor workflowExecutor) throws APIManagementException {
    WorkflowResponse response = workflowExecutor.complete(this);
    setStatus(response.getWorkflowStatus());
    if (WorkflowStatus.APPROVED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("API state change workflow complete: Approved");
        }
        String invoker = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LC_INVOKER);
        String currentState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_CUR_STATE);
        String targetState = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_TARGET_STATE);
        boolean hasOwnGateway = Boolean.valueOf(getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_HAS_OWN_GATEWAY));
        String label = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_AUTOGEN_LABEL);
        if (hasOwnGateway) {
            // (CREATED to DEPRECATED)
            if ((currentState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus()))) {
                try {
                    // No need to auto-generate the label again As hasOwnGateway is true.
                    // create the gateway
                    API api = apiDAO.getAPI(getWorkflowReference());
                    apiGateway.createContainerBasedGateway(label, api);
                } catch (ContainerBasedGatewayException e) {
                    // Revert already added labels
                    DedicatedGateway dedicatedGateway = new DedicatedGateway();
                    dedicatedGateway.setEnabled(false);
                    dedicatedGateway.setApiId(getWorkflowReference());
                    dedicatedGateway.setUpdatedBy(invoker);
                    List<String> labels = new ArrayList<>();
                    labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_GATEWAY));
                    labels.add(labelDAO.getLabelIdByNameAndType(APIMgtConstants.DEFAULT_LABEL_NAME, APIMgtConstants.LABEL_TYPE_STORE));
                    apiDAO.updateDedicatedGateway(dedicatedGateway, labels);
                    throw new APIManagementException("Error while updating lifecycle state in Private Jet Mode", e, ExceptionCodes.DEDICATED_CONTAINER_GATEWAY_CREATION_FAILED);
                }
            }
        }
        String localTime = getAttribute(APIMgtConstants.WorkflowConstants.ATTRIBUTE_API_LAST_UPTIME);
        LocalDateTime time = LocalDateTime.parse(localTime);
        updateAPIStatusForWorkflowComplete(getWorkflowReference(), targetState, invoker, time);
        // After publishing the state change to the Gateway, remove the gateway for following occasions.
        if (hasOwnGateway) {
            if ((currentState.equalsIgnoreCase(APIStatus.PUBLISHED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus()) || currentState.equalsIgnoreCase(APIStatus.DEPRECATED.getStatus())) && (targetState.equalsIgnoreCase(APIStatus.CREATED.getStatus()) || targetState.equalsIgnoreCase(APIStatus.MAINTENANCE.getStatus()) || targetState.equalsIgnoreCase(APIStatus.PROTOTYPED.getStatus())) || targetState.equalsIgnoreCase(APIStatus.RETIRED.getStatus())) {
                // remove gateway
                API api = apiDAO.getAPI(getWorkflowReference());
                apiGateway.removeContainerBasedGateway(label, api);
            }
        }
    } else if (WorkflowStatus.REJECTED == response.getWorkflowStatus()) {
        if (log.isDebugEnabled()) {
            log.debug("API state change workflow complete: Rejected");
        }
        apiDAO.updateAPIWorkflowStatus(getWorkflowReference(), APIMgtConstants.APILCWorkflowStatus.REJECTED);
    }
    updateWorkflowEntries(this);
    return response;
}
Also used : LocalDateTime(java.time.LocalDateTime) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) ContainerBasedGatewayException(org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException) API(org.wso2.carbon.apimgt.core.models.API) ArrayList(java.util.ArrayList) List(java.util.List) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Aggregations

ContainerBasedGatewayException (org.wso2.carbon.apimgt.core.exception.ContainerBasedGatewayException)18 Test (org.junit.Test)16 API (org.wso2.carbon.apimgt.core.models.API)15 ArrayList (java.util.ArrayList)9 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)8 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)7 Service (io.fabric8.kubernetes.api.model.Service)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 NonNamespaceOperation (io.fabric8.kubernetes.client.dsl.NonNamespaceOperation)5 Broker (org.wso2.carbon.apimgt.core.api.Broker)5 ScalableResource (io.fabric8.kubernetes.client.dsl.ScalableResource)4 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)3 ContainerBasedGatewayConfiguration (org.wso2.carbon.apimgt.core.configuration.models.ContainerBasedGatewayConfiguration)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 ConfigProvider (org.wso2.carbon.config.provider.ConfigProvider)2 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)1 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)1 BaseOperation (io.fabric8.kubernetes.client.dsl.base.BaseOperation)1 DefaultOpenShiftClient (io.fabric8.openshift.client.DefaultOpenShiftClient)1 LocalDateTime (java.time.LocalDateTime)1