Search in sources :

Example 1 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceEnvironmentImpl method getExternalEndpointURLs.

/**
 * Get endpoint URLs of the Solace environment
 *
 * @return List of protocol endpoint URLs map of Solace
 */
@Override
public List<AsyncProtocolEndpoint> getExternalEndpointURLs(Environment environment) {
    SolaceAdminApis solaceAdminApis = new SolaceAdminApis(environment.getServerURL(), environment.getUserName(), environment.getPassword(), environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    HttpResponse response = solaceAdminApis.environmentGET(environment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION), environment.getName());
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String responseString = null;
        try {
            responseString = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        JSONObject jsonObject = new JSONObject(responseString);
        if (jsonObject.has("messagingProtocols")) {
            JSONArray protocols = jsonObject.getJSONArray("messagingProtocols");
            List<AsyncProtocolEndpoint> asyncProtocolEndpoints = new ArrayList<>();
            for (int i = 0; i < protocols.length(); i++) {
                JSONObject protocolDetails = protocols.getJSONObject(i);
                String protocolName = protocolDetails.getJSONObject("protocol").getString("name");
                String endpointURI = protocolDetails.getString("uri");
                AsyncProtocolEndpoint asyncProtocolEndpoint = new AsyncProtocolEndpoint();
                asyncProtocolEndpoint.setProtocol(protocolName);
                asyncProtocolEndpoint.setProtocolUrl(endpointURI);
                asyncProtocolEndpoints.add(asyncProtocolEndpoint);
            }
            return asyncProtocolEndpoints;
        }
    }
    return null;
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) AsyncProtocolEndpoint(org.wso2.carbon.apimgt.api.model.AsyncProtocolEndpoint) AsyncProtocolEndpoint(org.wso2.carbon.apimgt.api.model.AsyncProtocolEndpoint)

Example 2 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceStoreUtils method getSolaceDeployedEnvsInfo.

/**
 * Get SolaceDeployedEnvironmentDTO using admin APIs and map into DTOs to parse Devportal
 *
 * @param solaceEnvironment Solace environment Object
 * @param solaceOrganization Solace broker organization name
 * @param applicationUuid      Subscribed Application UUID
 * @return List of SolaceDeployedEnvironmentDTO to use in Devportal
 * @throws APIManagementException if error occurred when creating SolaceDeployedEnvironmentDTO
 */
public static List<SolaceDeployedEnvironmentDTO> getSolaceDeployedEnvsInfo(Environment solaceEnvironment, String solaceOrganization, String applicationUuid) throws APIManagementException {
    Map<String, Environment> gatewayEnvironmentMap = APIUtil.getReadOnlyGatewayEnvironments();
    // Create solace admin APIs instance
    SolaceAdminApis solaceAdminApis = new SolaceAdminApis(solaceEnvironment.getServerURL(), solaceEnvironment.getUserName(), solaceEnvironment.getPassword(), solaceEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_DEV_NAME));
    HttpResponse response = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, "default");
    List<SolaceDeployedEnvironmentDTO> solaceEnvironments = new ArrayList<>();
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        try {
            String responseString = EntityUtils.toString(response.getEntity());
            org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
            // Get solace environments attached with the Solace application
            if (jsonObject.getJSONArray("environments") != null) {
                JSONArray environmentsArray = jsonObject.getJSONArray("environments");
                for (int i = 0; i < environmentsArray.length(); i++) {
                    SolaceDeployedEnvironmentDTO solaceDeployedEnvironmentsDTO = new SolaceDeployedEnvironmentDTO();
                    org.json.JSONObject environmentObject = environmentsArray.getJSONObject(i);
                    // Get details of Solace environment attached to the solace application
                    if (environmentObject.getString("name") != null) {
                        String environmentName = environmentObject.getString("name");
                        Environment gatewayEnvironment = gatewayEnvironmentMap.get(environmentName);
                        if (gatewayEnvironment != null) {
                            // Set Solace environment details
                            solaceDeployedEnvironmentsDTO.setEnvironmentName(gatewayEnvironment.getName());
                            solaceDeployedEnvironmentsDTO.setEnvironmentDisplayName(gatewayEnvironment.getDisplayName());
                            solaceDeployedEnvironmentsDTO.setOrganizationName(gatewayEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION));
                            boolean containsMQTTProtocol = false;
                            // Get messaging protocols from the response body
                            if (environmentObject.getJSONArray("messagingProtocols") != null) {
                                List<SolaceURLsDTO> endpointUrls = new ArrayList<>();
                                JSONArray protocolsArray = environmentObject.getJSONArray("messagingProtocols");
                                for (int j = 0; j < protocolsArray.length(); j++) {
                                    SolaceURLsDTO solaceURLsDTO = new SolaceURLsDTO();
                                    String protocol = protocolsArray.getJSONObject(j).getJSONObject("protocol").getString("name");
                                    if (SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.equalsIgnoreCase(protocol)) {
                                        containsMQTTProtocol = true;
                                    }
                                    String uri = protocolsArray.getJSONObject(j).getString("uri");
                                    solaceURLsDTO.setProtocol(protocol);
                                    solaceURLsDTO.setEndpointURL(uri);
                                    endpointUrls.add(solaceURLsDTO);
                                }
                                solaceDeployedEnvironmentsDTO.setSolaceURLs(endpointUrls);
                            }
                            // Get topic permissions from the solace application response body
                            if (environmentObject.getJSONObject("permissions") != null) {
                                org.json.JSONObject permissionsObject = environmentObject.getJSONObject("permissions");
                                SolaceTopicsObjectDTO solaceTopicsObjectDTO = new SolaceTopicsObjectDTO();
                                populateSolaceTopics(solaceTopicsObjectDTO, permissionsObject, "default");
                                // Handle the special case of MQTT protocol
                                if (containsMQTTProtocol) {
                                    HttpResponse responseForMqtt = solaceAdminApis.applicationGet(solaceOrganization, applicationUuid, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
                                    org.json.JSONObject permissionsObjectForMqtt = extractPermissionsFromSolaceApplicationGetResponse(responseForMqtt, i, gatewayEnvironmentMap);
                                    if (permissionsObjectForMqtt != null) {
                                        populateSolaceTopics(solaceTopicsObjectDTO, permissionsObjectForMqtt, SolaceConstants.MQTT_TRANSPORT_PROTOCOL_NAME.toUpperCase());
                                    }
                                }
                                solaceDeployedEnvironmentsDTO.setSolaceTopicsObject(solaceTopicsObjectDTO);
                            }
                        }
                    }
                    solaceEnvironments.add(solaceDeployedEnvironmentsDTO);
                }
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        return solaceEnvironments;
    } else {
        throw new APIManagementException("Solace Environment configurations are not provided properly");
    }
}
Also used : ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) SolaceURLsDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO) IOException(java.io.IOException) SolaceTopicsObjectDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceTopicsObjectDTO) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) SolaceDeployedEnvironmentDTO(org.wso2.carbon.apimgt.solace.dtos.SolaceDeployedEnvironmentDTO)

Example 3 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis 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());
    }
}
Also used : ArrayList(java.util.ArrayList) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment) NotifierException(org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException) SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) Environment(org.wso2.carbon.apimgt.api.model.Environment) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method renameSolaceApplication.

/**
 * Rename the Solace application
 *
 * @param organization Name of the Organization
 * @param application  Solace application
 * @throws APIManagementException is error occurs when renaming the application
 */
public static void renameSolaceApplication(String organization, Application application) throws APIManagementException {
    SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
    if (log.isDebugEnabled()) {
        log.info("Renaming solace application display name....");
    }
    CloseableHttpResponse response = solaceAdminApis.renameApplication(organization, application);
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        log.info("Renamed solace application display name into '" + application.getName() + "'");
    } else {
        if (log.isDebugEnabled()) {
            log.error("Error while renaming solace Application display name. : " + response.getStatusLine().toString());
        }
        throw new APIManagementException(response.getStatusLine().getStatusCode() + "-" + response.getStatusLine().getReasonPhrase());
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 5 with SolaceAdminApis

use of org.wso2.carbon.apimgt.solace.SolaceAdminApis in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method deployApplicationToSolaceBroker.

/**
 * Deploy an application to Solace broker
 *
 * @param application  Application to be deployed
 * @param apiProducts  Api products to be subscribed to Application
 * @param organization Name of the organization
 * @throws IOException            If an error occurs when deploying the application
 * @throws APIManagementException if an error occurs when getting Solace config
 */
public static void deployApplicationToSolaceBroker(Application application, ArrayList<String> apiProducts, String organization) throws IOException, APIManagementException {
    SolaceAdminApis solaceAdminApis = SolaceNotifierUtils.getSolaceAdminApis();
    // check existence of the developer
    CloseableHttpResponse response1 = solaceAdminApis.developerGet(organization);
    if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        if (log.isDebugEnabled()) {
            log.info("Developer found in Solace Broker");
        }
        // check application status
        CloseableHttpResponse response2 = solaceAdminApis.applicationGet(organization, application.getUUID(), "default");
        if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            // app already exists
            if (log.isDebugEnabled()) {
                log.info("Solace application '" + application.getName() + "' already exists in Solace." + " Updating Application......");
            }
            CloseableHttpResponse response3 = solaceAdminApis.applicationPatchAddSubscription(organization, application, apiProducts);
            if (response3.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                log.info("Solace application '" + application.getName() + "' updated successfully");
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while updating Solace application '" + application.getName() + ". : " + response3.getStatusLine().toString());
                }
                throw new HttpResponseException(response3.getStatusLine().getStatusCode(), response3.getStatusLine().getReasonPhrase());
            }
        } else if (response2.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
            // If application keys are not generated we are not doing anything in Solace.
            if (application.getKeys().isEmpty()) {
                return;
            }
            String responseString = EntityUtils.toString(response2.getEntity());
            if (responseString.contains(String.valueOf(HttpStatus.SC_NOT_FOUND))) {
                // create new app
                if (log.isDebugEnabled()) {
                    log.info("Solace application '" + application.getName() + "' not found in Solace Broker." + "Creating new application......");
                }
                CloseableHttpResponse response4 = solaceAdminApis.createApplication(organization, application, apiProducts);
                if (response4.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED) {
                    log.info("Solace application '" + application.getName() + "' created successfully");
                } else {
                    if (log.isDebugEnabled()) {
                        log.error("Error while creating Solace application '" + application.getName() + ". : " + response4.getStatusLine().toString());
                    }
                    throw new HttpResponseException(response4.getStatusLine().getStatusCode(), response4.getStatusLine().getReasonPhrase());
                }
            } else {
                if (log.isDebugEnabled()) {
                    log.error("Error while searching for application '" + application.getName() + ". : " + response2.getStatusLine().toString());
                }
                throw new HttpResponseException(response2.getStatusLine().getStatusCode(), response2.getStatusLine().getReasonPhrase());
            }
        } else {
            if (log.isDebugEnabled()) {
                log.error("Error while searching for application '" + application.getName() + ". : " + response2.getStatusLine().toString());
            }
            throw new HttpResponseException(response2.getStatusLine().getStatusCode(), response2.getStatusLine().getReasonPhrase());
        }
    } else if (response1.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
        if (log.isDebugEnabled()) {
            log.error("Developer not found in Solace Broker");
        }
        throw new HttpResponseException(response1.getStatusLine().getStatusCode(), response1.getStatusLine().getReasonPhrase());
    } else {
        if (log.isDebugEnabled()) {
            log.error("Developer not found in Solace Broker");
        }
        throw new HttpResponseException(response1.getStatusLine().getStatusCode(), response1.getStatusLine().getReasonPhrase());
    }
}
Also used : SolaceAdminApis(org.wso2.carbon.apimgt.solace.SolaceAdminApis) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) HttpResponseException(org.apache.http.client.HttpResponseException)

Aggregations

SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)13 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)9 Environment (org.wso2.carbon.apimgt.api.model.Environment)6 ArrayList (java.util.ArrayList)5 IOException (java.io.IOException)4 HttpResponse (org.apache.http.HttpResponse)3 HttpResponseException (org.apache.http.client.HttpResponseException)3 JSONArray (org.json.JSONArray)3 DeployerException (org.wso2.carbon.apimgt.impl.deployer.exceptions.DeployerException)2 SolaceURLsDTO (org.wso2.carbon.apimgt.solace.dtos.SolaceURLsDTO)2 Aai20Document (io.apicurio.datamodels.asyncapi.v2.models.Aai20Document)1 List (java.util.List)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)1 AsyncProtocolEndpoint (org.wso2.carbon.apimgt.api.model.AsyncProtocolEndpoint)1 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)1 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)1 NotifierException (org.wso2.carbon.apimgt.impl.notifier.exceptions.NotifierException)1