Search in sources :

Example 11 with GraphqlComplexityInfo

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.

the class GraphQLSchemaDefinition method policyDefinitionToJson.

/**
 * Method to convert GraphqlComplexityInfo object to a JSONObject
 *
 * @param graphqlComplexityInfo GraphqlComplexityInfo object
 * @return json object which contains the policy definition
 */
public JSONObject policyDefinitionToJson(GraphqlComplexityInfo graphqlComplexityInfo) {
    JSONObject policyDefinition = new JSONObject();
    HashMap<String, HashMap<String, Integer>> customComplexityMap = new HashMap<>();
    List<CustomComplexityDetails> list = graphqlComplexityInfo.getList();
    for (CustomComplexityDetails customComplexityDetails : list) {
        String type = customComplexityDetails.getType();
        String field = customComplexityDetails.getField();
        int complexityValue = customComplexityDetails.getComplexityValue();
        if (customComplexityMap.containsKey(type)) {
            customComplexityMap.get(type).put(field, complexityValue);
        } else {
            HashMap<String, Integer> complexityValueMap = new HashMap<>();
            complexityValueMap.put(field, complexityValue);
            customComplexityMap.put(type, complexityValueMap);
        }
    }
    Map<String, Map<String, Object>> customComplexityObject = new LinkedHashMap<>(customComplexityMap.size());
    for (HashMap.Entry<String, HashMap<String, Integer>> entry : customComplexityMap.entrySet()) {
        HashMap<String, Integer> fieldValueMap = entry.getValue();
        String type = entry.getKey();
        Map<String, Object> fieldValueObject = new LinkedHashMap<>(fieldValueMap.size());
        for (HashMap.Entry<String, Integer> subEntry : fieldValueMap.entrySet()) {
            String field = subEntry.getKey();
            int complexityValue = subEntry.getValue();
            fieldValueObject.put(field, complexityValue);
        }
        customComplexityObject.put(type, fieldValueObject);
    }
    policyDefinition.put(APIConstants.QUERY_ANALYSIS_COMPLEXITY, customComplexityObject);
    return policyDefinition;
}
Also used : CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 12 with GraphqlComplexityInfo

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.

the class GraphqlQueryAnalysisMappingUtil method fromDTOtoValidatedGraphqlComplexityInfo.

/**
 * Converts a GraphQLQueryComplexityInfo DTO object into a GraphqlComplexityInfo object. During this process a
 * basic validation is done comparing with the types of the schema
 *
 * @param graphQLQueryComplexityInfoDTO GraphQLQueryComplexityInfoDTO object
 * @param schema                        GraphQL Schema
 * @return a new GraphqlComplexityInfo object corresponding to given GraphQLQueryComplexityInfoDTO object
 */
public static GraphqlComplexityInfo fromDTOtoValidatedGraphqlComplexityInfo(GraphQLQueryComplexityInfoDTO graphQLQueryComplexityInfoDTO, String schema) {
    SchemaParser schemaParser = new SchemaParser();
    Set<String> complexityInfoTypeSet = schemaParser.parse(schema).types().keySet();
    GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
    List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
    for (GraphQLCustomComplexityInfoDTO graphQLCustomComplexityInfoDTO : graphQLQueryComplexityInfoDTO.getList()) {
        String complexityType = graphQLCustomComplexityInfoDTO.getType();
        if (complexityInfoTypeSet.contains(complexityType)) {
            CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
            customComplexityDetails.setType(complexityType);
            customComplexityDetails.setField(graphQLCustomComplexityInfoDTO.getField());
            customComplexityDetails.setComplexityValue(graphQLCustomComplexityInfoDTO.getComplexityValue());
            customComplexityDetailsList.add(customComplexityDetails);
        } else {
            log.error("Complexity Type : " + complexityType + " is not included in the original schema. Hence " + "skipped.");
        }
    }
    graphqlComplexityInfo.setList(customComplexityDetailsList);
    return graphqlComplexityInfo;
}
Also used : GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) ArrayList(java.util.ArrayList) GraphQLCustomComplexityInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLCustomComplexityInfoDTO) SchemaParser(graphql.schema.idl.SchemaParser) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)

Example 13 with GraphqlComplexityInfo

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.

the class SynapseArtifactGenerator method generateGatewayArtifact.

@Override
public RuntimeArtifactDto generateGatewayArtifact(List<APIRuntimeArtifactDto> apiRuntimeArtifactDtoList) throws APIManagementException {
    RuntimeArtifactDto runtimeArtifactDto = new RuntimeArtifactDto();
    List<String> synapseArtifacts = new ArrayList<>();
    for (APIRuntimeArtifactDto runTimeArtifact : apiRuntimeArtifactDtoList) {
        if (runTimeArtifact.isFile()) {
            String tenantDomain = runTimeArtifact.getTenantDomain();
            String label = runTimeArtifact.getLabel();
            Environment environment = APIUtil.getEnvironments(tenantDomain).get(label);
            GatewayAPIDTO gatewayAPIDTO = null;
            if (environment != null) {
                try (InputStream artifact = (InputStream) runTimeArtifact.getArtifact()) {
                    File baseDirectory = CommonUtil.createTempDirectory(null);
                    try {
                        String extractedFolderPath = ImportUtils.getArchivePathOfExtractedDirectory(baseDirectory.getAbsolutePath(), artifact);
                        if (APIConstants.API_PRODUCT.equals(runTimeArtifact.getType())) {
                            APIProductDTO apiProductDTO = ImportUtils.retrieveAPIProductDto(extractedFolderPath);
                            apiProductDTO.setId(runTimeArtifact.getApiId());
                            APIProduct apiProduct = APIMappingUtil.fromDTOtoAPIProduct(apiProductDTO, apiProductDTO.getProvider());
                            APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
                            apiProduct.setDefinition(apiDefinitionValidationResponse.getContent());
                            gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(apiProduct, environment, tenantDomain, extractedFolderPath);
                        } else {
                            APIDTO apidto = ImportUtils.retrievedAPIDto(extractedFolderPath);
                            API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
                            api.setUUID(apidto.getId());
                            if (APIConstants.APITransportType.GRAPHQL.toString().equals(api.getType())) {
                                APIDefinition parser = new OAS3Parser();
                                SwaggerData swaggerData = new SwaggerData(api);
                                String apiDefinition = parser.generateAPIDefinition(swaggerData);
                                api.setSwaggerDefinition(apiDefinition);
                                GraphqlComplexityInfo graphqlComplexityInfo = APIUtil.getComplexityDetails(api);
                                String graphqlSchema = ImportUtils.retrieveValidatedGraphqlSchemaFromArchive(extractedFolderPath);
                                api.setGraphQLSchema(graphqlSchema);
                                GraphQLSchemaDefinition graphQLSchemaDefinition = new GraphQLSchemaDefinition();
                                graphqlSchema = graphQLSchemaDefinition.buildSchemaWithAdditionalInfo(api, graphqlComplexityInfo);
                                api.setGraphQLSchema(graphqlSchema);
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath);
                            } else if (api.getType() != null && (APIConstants.APITransportType.HTTP.toString().equals(api.getType()) || APIConstants.API_TYPE_SOAP.equals(api.getType()) || APIConstants.API_TYPE_SOAPTOREST.equals(api.getType()) || APIConstants.APITransportType.WEBHOOK.toString().equals(api.getType()))) {
                                APIDefinitionValidationResponse apiDefinitionValidationResponse = ImportUtils.retrieveValidatedSwaggerDefinitionFromArchive(extractedFolderPath);
                                api.setSwaggerDefinition(apiDefinitionValidationResponse.getContent());
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDto(api, environment, tenantDomain, apidto, extractedFolderPath, apiDefinitionValidationResponse);
                            } else if (api.getType() != null && (APIConstants.APITransportType.WS.toString().equals(api.getType()) || APIConstants.APITransportType.SSE.toString().equals(api.getType()) || APIConstants.APITransportType.WEBSUB.toString().equals(api.getType()))) {
                                APIDefinitionValidationResponse asyncApiDefinition = ImportUtils.retrieveValidatedAsyncApiDefinitionFromArchive(extractedFolderPath);
                                api.setAsyncApiDefinition(asyncApiDefinition.getContent());
                                gatewayAPIDTO = TemplateBuilderUtil.retrieveGatewayAPIDtoForStreamingAPI(api, environment, tenantDomain, apidto, extractedFolderPath);
                            }
                        }
                        if (gatewayAPIDTO != null) {
                            String content = new Gson().toJson(gatewayAPIDTO);
                            synapseArtifacts.add(content);
                        }
                    } finally {
                        FileUtils.deleteQuietly(baseDirectory);
                    }
                } catch (Exception e) {
                    // only do error since we need to continue for other apis
                    log.error("Error while creating Synapse configurations", e);
                }
            }
        }
    }
    runtimeArtifactDto.setFile(false);
    runtimeArtifactDto.setArtifact(synapseArtifacts);
    return runtimeArtifactDto;
}
Also used : GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) InputStream(java.io.InputStream) SwaggerData(org.wso2.carbon.apimgt.api.model.SwaggerData) APIProductDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIProductDTO) ArrayList(java.util.ArrayList) OAS3Parser(org.wso2.carbon.apimgt.impl.definitions.OAS3Parser) RuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.RuntimeArtifactDto) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) Gson(com.google.gson.Gson) GraphQLSchemaDefinition(org.wso2.carbon.apimgt.impl.definitions.GraphQLSchemaDefinition) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIDefinitionValidationResponse(org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse) APIProduct(org.wso2.carbon.apimgt.api.model.APIProduct) GatewayAPIDTO(org.wso2.carbon.apimgt.api.gateway.GatewayAPIDTO) APIDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIDTO) APIRuntimeArtifactDto(org.wso2.carbon.apimgt.impl.dto.APIRuntimeArtifactDto) APIDefinition(org.wso2.carbon.apimgt.api.APIDefinition) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) File(java.io.File)

Example 14 with GraphqlComplexityInfo

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.

the class ApiMgtDAO method getComplexityDetails.

/**
 * Get custom complexity details for a particular API
 *
 * @param uuid API UUID to retrieve API ID
 * @return info about the complexity details
 * @throws APIManagementException
 */
public GraphqlComplexityInfo getComplexityDetails(String uuid) throws APIManagementException {
    GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
    String getCustomComplexityDetailsQuery = SQLConstants.GET_CUSTOM_COMPLEXITY_DETAILS_SQL;
    List<CustomComplexityDetails> customComplexityDetailsList = new ArrayList<CustomComplexityDetails>();
    try (Connection conn = APIMgtDBUtil.getConnection();
        PreparedStatement getCustomComplexityDetails = conn.prepareStatement(getCustomComplexityDetailsQuery)) {
        int apiId = getAPIID(uuid, conn);
        getCustomComplexityDetails.setInt(1, apiId);
        try (ResultSet rs1 = getCustomComplexityDetails.executeQuery()) {
            while (rs1.next()) {
                CustomComplexityDetails customComplexityDetails = new CustomComplexityDetails();
                customComplexityDetails.setType(rs1.getString("TYPE"));
                customComplexityDetails.setField(rs1.getString("FIELD"));
                customComplexityDetails.setComplexityValue(rs1.getInt("COMPLEXITY_VALUE"));
                customComplexityDetailsList.add(customComplexityDetails);
            }
        }
        graphqlComplexityInfo.setList(customComplexityDetailsList);
    } catch (SQLException ex) {
        handleException("Error while retrieving custom complexity details: ", ex);
    }
    return graphqlComplexityInfo;
}
Also used : GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)

Example 15 with GraphqlComplexityInfo

use of org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo in project carbon-apimgt by wso2.

the class APIMgtDAOTest method getGraphqlComplexityInfoDetails.

private GraphqlComplexityInfo getGraphqlComplexityInfoDetails() {
    GraphqlComplexityInfo graphqlComplexityInfo = new GraphqlComplexityInfo();
    CustomComplexityDetails customComplexity1 = new CustomComplexityDetails();
    customComplexity1.setType("type1");
    customComplexity1.setField("field1");
    customComplexity1.setComplexityValue(5);
    CustomComplexityDetails customComplexity2 = new CustomComplexityDetails();
    customComplexity2.setType("type1");
    customComplexity2.setField("field2");
    customComplexity2.setComplexityValue(4);
    List<CustomComplexityDetails> list = new ArrayList<>();
    list.add(customComplexity1);
    list.add(customComplexity2);
    graphqlComplexityInfo.setList(list);
    return graphqlComplexityInfo;
}
Also used : GraphqlComplexityInfo(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo) ArrayList(java.util.ArrayList) CustomComplexityDetails(org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)

Aggregations

GraphqlComplexityInfo (org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.GraphqlComplexityInfo)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)7 CustomComplexityDetails (org.wso2.carbon.apimgt.api.model.graphql.queryanalysis.CustomComplexityDetails)7 ArrayList (java.util.ArrayList)6 API (org.wso2.carbon.apimgt.api.model.API)6 Gson (com.google.gson.Gson)4 GraphQLQueryComplexityInfoDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.GraphQLQueryComplexityInfoDTO)4 JsonElement (com.google.gson.JsonElement)3 IOException (java.io.IOException)3 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 SQLException (java.sql.SQLException)3 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)3 JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 JSONObject (org.json.simple.JSONObject)2 APIDefinitionValidationResponse (org.wso2.carbon.apimgt.api.APIDefinitionValidationResponse)2 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)2 ImportExportAPI (org.wso2.carbon.apimgt.impl.importexport.ImportExportAPI)2 GsonBuilder (com.google.gson.GsonBuilder)1