protected RestChannelConsumer prepareRequest()

in src/main/java/org/opensearch/performanceanalyzer/http_action/config/PerformanceAnalyzerConfigAction.java [155:239]


    protected RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client)
            throws IOException {
        if (request.method() == RestRequest.Method.POST && request.content().length() > 0) {
            // Let's try to find the name from the body
            Map<String, Object> map = XContentHelper.convertToMap(request.content(), false).v2();
            Object value = map.get(ENABLED);
            LOG.debug(
                    "PerformanceAnalyzer:Value (Object) Received as Part of Request: {} current value: {}",
                    value,
                    performanceAnalyzerController.isPerformanceAnalyzerEnabled());
            if (value instanceof Boolean) {
                boolean shouldEnable = (Boolean) value;
                if (request.path().contains(RCA_CONFIG_PATH)
                        || request.path().contains(LEGACY_RCA_CONFIG_PATH)) {
                    // If RCA needs to be turned on, we need to have PA turned on also.
                    // If this is not the case, return error.
                    if (shouldEnable
                            && !performanceAnalyzerController.isPerformanceAnalyzerEnabled()) {
                        return getChannelConsumerWithError(
                                "Error: PA not enabled. Enable PA before turning RCA on");
                    }

                    performanceAnalyzerController.updateRcaState(shouldEnable);
                } else if (request.path().contains(LOGGING_CONFIG_PATH)
                        || request.path().contains(LEGACY_LOGGING_CONFIG_PATH)) {
                    if (shouldEnable
                            && !performanceAnalyzerController.isPerformanceAnalyzerEnabled()) {
                        return getChannelConsumerWithError(
                                "Error: PA not enabled. Enable PA before turning Logging on");
                    }

                    performanceAnalyzerController.updateLoggingState(shouldEnable);
                } else if (request.path().contains(BATCH_METRICS_CONFIG_PATH)
                        || request.path().contains(LEGACY_BATCH_METRICS_CONFIG_PATH)) {
                    if (shouldEnable
                            && !performanceAnalyzerController.isPerformanceAnalyzerEnabled()) {
                        return getChannelConsumerWithError(
                                "Error: PA not enabled. Enable PA before turning Batch Metrics on");
                    }

                    performanceAnalyzerController.updateBatchMetricsState(shouldEnable);
                } else {
                    // Disabling Performance Analyzer should disable the RCA framework as well.
                    if (!shouldEnable) {
                        performanceAnalyzerController.updateRcaState(false);
                        performanceAnalyzerController.updateLoggingState(false);
                        performanceAnalyzerController.updateBatchMetricsState(false);
                    }
                    performanceAnalyzerController.updatePerformanceAnalyzerState(shouldEnable);
                }
            }
            // update node stats setting if exists
            if (map.containsKey(SHARDS_PER_COLLECTION)) {
                Object shardPerCollectionValue = map.get(SHARDS_PER_COLLECTION);
                if (shardPerCollectionValue instanceof Integer) {
                    performanceAnalyzerController.updateNodeStatsShardsPerCollection(
                            (Integer) shardPerCollectionValue);
                }
            }
        }

        return channel -> {
            try {
                XContentBuilder builder = channel.newBuilder();
                builder.startObject();
                builder.field(
                        PA_ENABLED, performanceAnalyzerController.isPerformanceAnalyzerEnabled());
                builder.field(RCA_ENABLED, performanceAnalyzerController.isRcaEnabled());
                builder.field(PA_LOGGING_ENABLED, performanceAnalyzerController.isLoggingEnabled());
                builder.field(
                        SHARDS_PER_COLLECTION,
                        performanceAnalyzerController.getNodeStatsShardsPerCollection());
                builder.field(
                        BATCH_METRICS_ENABLED,
                        performanceAnalyzerController.isBatchMetricsEnabled());
                builder.field(
                        BATCH_METRICS_RETENTION_PERIOD_MINUTES,
                        PluginSettings.instance().getBatchMetricsRetentionPeriodMinutes());
                builder.endObject();
                channel.sendResponse(new BytesRestResponse(RestStatus.OK, builder));
            } catch (IOException ioe) {
                LOG.error("Error sending response", ioe);
            }
        };
    }