Search in sources :

Example 51 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment 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 52 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class SolaceStoreUtils method extractPermissionsFromSolaceApplicationGetResponse.

/**
 * Populate Solace permissions related to Topics from the response body
 *
 * @param response Response of the admin request
 * @param environmentIndex index value of registered environment
 * @param gatewayEnvironmentMap registered gateway envs map
 * @return org.json.JSONObject of permissions
 */
private static org.json.JSONObject extractPermissionsFromSolaceApplicationGetResponse(HttpResponse response, int environmentIndex, Map<String, Environment> gatewayEnvironmentMap) throws IOException {
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String responseString = EntityUtils.toString(response.getEntity());
        org.json.JSONObject jsonObject = new org.json.JSONObject(responseString);
        if (jsonObject.getJSONArray("environments") != null) {
            JSONArray environmentsArray = jsonObject.getJSONArray("environments");
            org.json.JSONObject environmentObject = environmentsArray.getJSONObject(environmentIndex);
            if (environmentObject.getString("name") != null) {
                String environmentName = environmentObject.getString("name");
                Environment gatewayEnvironment = gatewayEnvironmentMap.get(environmentName);
                if (gatewayEnvironment != null) {
                    if (environmentObject.getJSONObject("permissions") != null) {
                        return environmentObject.getJSONObject("permissions");
                    }
                }
            }
        }
    }
    return null;
}
Also used : JSONArray(org.json.JSONArray) Environment(org.wso2.carbon.apimgt.api.model.Environment)

Example 53 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment 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 54 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment.

/**
 * Get third party Solace broker organization Name for API deployment
 *
 * @param api Name of the API
 * @return String of the name of organization in Solace broker
 * @throws APIManagementException is error occurs when getting the name of the organization name
 */
public static String getThirdPartySolaceBrokerOrganizationNameOfAPIDeployment(API api) throws APIManagementException {
    apiMgtDAO = ApiMgtDAO.getInstance();
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    List<APIRevisionDeployment> deployments = apiMgtDAO.getAPIRevisionDeploymentByApiUUID(api.getUuid());
    for (APIRevisionDeployment deployment : deployments) {
        if (deployment.isDisplayOnDevportal()) {
            String environmentName = deployment.getDeployment();
            if (gatewayEnvironments.containsKey(environmentName)) {
                Environment deployedEnvironment = gatewayEnvironments.get(environmentName);
                if (SolaceConstants.SOLACE_ENVIRONMENT.equalsIgnoreCase(deployedEnvironment.getProvider())) {
                    return deployedEnvironment.getAdditionalProperties().get(SolaceConstants.SOLACE_ENVIRONMENT_ORGANIZATION);
                }
            }
        }
    }
    return null;
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) APIRevisionDeployment(org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)

Example 55 with Environment

use of org.wso2.carbon.apimgt.api.model.Environment in project carbon-apimgt by wso2.

the class SolaceNotifierUtils method isSolaceEnvironmentAdded.

/**
 * Check whether the Solace is Added as a third party environment
 *
 * @return true if Solace is Added as a third party environment
 */
private boolean isSolaceEnvironmentAdded() {
    Map<String, Environment> gatewayEnvironments = APIUtil.getReadOnlyGatewayEnvironments();
    if (gatewayEnvironments.isEmpty()) {
        return false;
    }
    Environment solaceEnvironment = null;
    for (Map.Entry<String, Environment> entry : gatewayEnvironments.entrySet()) {
        if (SolaceConstants.SOLACE_ENVIRONMENT.equals(entry.getValue().getProvider())) {
            solaceEnvironment = entry.getValue();
        }
    }
    return solaceEnvironment != null;
}
Also used : Environment(org.wso2.carbon.apimgt.api.model.Environment) Map(java.util.Map)

Aggregations

Environment (org.wso2.carbon.apimgt.api.model.Environment)67 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)53 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)28 API (org.wso2.carbon.apimgt.api.model.API)22 IOException (java.io.IOException)21 APIRevisionDeployment (org.wso2.carbon.apimgt.api.model.APIRevisionDeployment)19 Map (java.util.Map)17 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)12 JsonObject (com.google.gson.JsonObject)11 Gson (com.google.gson.Gson)10 PreparedStatement (java.sql.PreparedStatement)10 SQLException (java.sql.SQLException)10 HashSet (java.util.HashSet)10 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)10 SolaceAdminApis (org.wso2.carbon.apimgt.solace.SolaceAdminApis)10 JSONObject (org.json.simple.JSONObject)9 Test (org.junit.Test)9 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)9