Search in sources :

Example 36 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project k-NN by opensearch-project.

the class RestKNNWarmupHandler method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) {
    KNNWarmupRequest knnWarmupRequest = createKNNWarmupRequest(request);
    logger.info("[KNN] Warmup started for the following indices: " + String.join(",", knnWarmupRequest.indices()));
    return channel -> client.execute(KNNWarmupAction.INSTANCE, knnWarmupRequest, new RestToXContentListener<>(channel));
}
Also used : Arrays(java.util.Arrays) NodeClient(org.opensearch.client.node.NodeClient) KNNWarmupRequest(org.opensearch.knn.plugin.transport.KNNWarmupRequest) RestController(org.opensearch.rest.RestController) Index(org.opensearch.index.Index) RestRequest(org.opensearch.rest.RestRequest) Settings(org.opensearch.common.settings.Settings) KNN_INDEX(org.opensearch.knn.index.KNNSettings.KNN_INDEX) KNNInvalidIndicesException(org.opensearch.knn.common.exception.KNNInvalidIndicesException) Strings(org.opensearch.common.Strings) ArrayList(java.util.ArrayList) List(java.util.List) Logger(org.apache.logging.log4j.Logger) ImmutableList(com.google.common.collect.ImmutableList) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) KNNWarmupAction(org.opensearch.knn.plugin.transport.KNNWarmupAction) ClusterService(org.opensearch.cluster.service.ClusterService) IndicesOptions.strictExpandOpen(org.opensearch.action.support.IndicesOptions.strictExpandOpen) KNNPlugin(org.opensearch.knn.plugin.KNNPlugin) BaseRestHandler(org.opensearch.rest.BaseRestHandler) LogManager(org.apache.logging.log4j.LogManager) IndexNameExpressionResolver(org.opensearch.cluster.metadata.IndexNameExpressionResolver) KNNWarmupRequest(org.opensearch.knn.plugin.transport.KNNWarmupRequest)

Example 37 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project k-NN by opensearch-project.

the class RestSearchModelHandler method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    checkUnSupportedParamsExists(request);
    SearchRequest searchRequest = new SearchRequest();
    IntConsumer setSize = size -> searchRequest.source().size(size);
    request.withContentOrSourceParamParserOrNull(parser -> RestSearchAction.parseSearchRequest(searchRequest, request, parser, client.getNamedWriteableRegistry(), setSize));
    return channel -> {
        RestCancellableNodeClient cancelClient = new RestCancellableNodeClient(client, request.getHttpChannel());
        cancelClient.execute(SearchModelAction.INSTANCE, searchRequest, new RestToXContentListener<>(channel));
    };
}
Also used : SearchAction(org.opensearch.action.search.SearchAction) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) IntConsumer(java.util.function.IntConsumer) ArrayList(java.util.ArrayList) RestSearchAction(org.opensearch.rest.action.search.RestSearchAction) ImmutableList(com.google.common.collect.ImmutableList) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) Locale(java.util.Locale) SearchRequest(org.opensearch.action.search.SearchRequest) Model(org.opensearch.knn.indices.Model) RestCancellableNodeClient(org.opensearch.rest.action.RestCancellableNodeClient) SearchResponse(org.opensearch.action.search.SearchResponse) KNNPlugin(org.opensearch.knn.plugin.KNNPlugin) BaseRestHandler(org.opensearch.rest.BaseRestHandler) RestResponseListener(org.opensearch.rest.action.RestResponseListener) MODELS(org.opensearch.knn.common.KNNConstants.MODELS) SearchModelAction(org.opensearch.knn.plugin.transport.SearchModelAction) NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) SearchHit(org.opensearch.search.SearchHit) IOException(java.io.IOException) RestStatus(org.opensearch.rest.RestStatus) EMPTY_PARAMS(org.opensearch.common.xcontent.ToXContent.EMPTY_PARAMS) BytesRestResponse(org.opensearch.rest.BytesRestResponse) RestResponse(org.opensearch.rest.RestResponse) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) List(java.util.List) ToXContentObject(org.opensearch.common.xcontent.ToXContentObject) RestChannel(org.opensearch.rest.RestChannel) MODEL_INDEX_NAME(org.opensearch.knn.common.KNNConstants.MODEL_INDEX_NAME) SearchRequest(org.opensearch.action.search.SearchRequest) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) RestCancellableNodeClient(org.opensearch.rest.action.RestCancellableNodeClient) IntConsumer(java.util.function.IntConsumer)

Example 38 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project anomaly-detection by opensearch-project.

the class RestValidateAnomalyDetectorAction method prepareRequest.

@Override
protected BaseRestHandler.RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (!EnabledSetting.isADPluginEnabled()) {
        throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
    }
    XContentParser parser = request.contentParser();
    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
    String typesStr = request.param(TYPE);
    // if type param isn't blank and isn't a part of possible validation types throws exception
    if (!StringUtils.isBlank(typesStr)) {
        if (!validationTypesAreAccepted(typesStr)) {
            throw new IllegalStateException(CommonErrorMessages.NOT_EXISTENT_VALIDATION_TYPE);
        }
    }
    return channel -> {
        AnomalyDetector detector;
        try {
            detector = AnomalyDetector.parse(parser);
        } catch (Exception ex) {
            if (ex instanceof ADValidationException) {
                ADValidationException ADException = (ADValidationException) ex;
                DetectorValidationIssue issue = new DetectorValidationIssue(ADException.getAspect(), ADException.getType(), ADException.getMessage());
                sendAnomalyDetectorValidationParseResponse(issue, channel);
                return;
            } else {
                throw ex;
            }
        }
        ValidateAnomalyDetectorRequest validateAnomalyDetectorRequest = new ValidateAnomalyDetectorRequest(detector, typesStr, maxSingleEntityDetectors, maxMultiEntityDetectors, maxAnomalyFeatures, requestTimeout);
        client.execute(ValidateAnomalyDetectorAction.INSTANCE, validateAnomalyDetectorRequest, new RestToXContentListener<>(channel));
    };
}
Also used : Arrays(java.util.Arrays) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) StringUtils(org.apache.commons.lang3.StringUtils) HashSet(java.util.HashSet) XContentParser(org.opensearch.common.xcontent.XContentParser) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ImmutableList(com.google.common.collect.ImmutableList) Locale(java.util.Locale) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ValidateAnomalyDetectorAction(org.opensearch.ad.transport.ValidateAnomalyDetectorAction) BaseRestHandler(org.opensearch.rest.BaseRestHandler) ValidationAspect(org.opensearch.ad.model.ValidationAspect) ValidateAnomalyDetectorResponse(org.opensearch.ad.transport.ValidateAnomalyDetectorResponse) NodeClient(org.opensearch.client.node.NodeClient) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) RestRequest(org.opensearch.rest.RestRequest) Set(java.util.Set) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) IOException(java.io.IOException) Settings(org.opensearch.common.settings.Settings) RestStatus(org.opensearch.rest.RestStatus) Collectors(java.util.stream.Collectors) BytesRestResponse(org.opensearch.rest.BytesRestResponse) VALIDATE(org.opensearch.ad.util.RestHandlerUtils.VALIDATE) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) ValidateAnomalyDetectorRequest(org.opensearch.ad.transport.ValidateAnomalyDetectorRequest) RestChannel(org.opensearch.rest.RestChannel) ClusterService(org.opensearch.cluster.service.ClusterService) AnomalyDetectorPlugin(org.opensearch.ad.AnomalyDetectorPlugin) TYPE(org.opensearch.ad.util.RestHandlerUtils.TYPE) Collections(java.util.Collections) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ValidateAnomalyDetectorRequest(org.opensearch.ad.transport.ValidateAnomalyDetectorRequest) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) DetectorValidationIssue(org.opensearch.ad.model.DetectorValidationIssue) XContentParser(org.opensearch.common.xcontent.XContentParser) AnomalyDetector(org.opensearch.ad.model.AnomalyDetector) ADValidationException(org.opensearch.ad.common.exception.ADValidationException) IOException(java.io.IOException)

Example 39 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project anomaly-detection by opensearch-project.

the class RestDeleteAnomalyDetectorAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (!EnabledSetting.isADPluginEnabled()) {
        throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
    }
    String detectorId = request.param(DETECTOR_ID);
    DeleteAnomalyDetectorRequest deleteAnomalyDetectorRequest = new DeleteAnomalyDetectorRequest(detectorId);
    return channel -> client.execute(DeleteAnomalyDetectorAction.INSTANCE, deleteAnomalyDetectorRequest, new RestToXContentListener<>(channel));
}
Also used : NodeClient(org.opensearch.client.node.NodeClient) RestRequest(org.opensearch.rest.RestRequest) AnomalyDetectorActionHandler(org.opensearch.ad.rest.handler.AnomalyDetectorActionHandler) IOException(java.io.IOException) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) DeleteAnomalyDetectorAction(org.opensearch.ad.transport.DeleteAnomalyDetectorAction) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) DeleteAnomalyDetectorRequest(org.opensearch.ad.transport.DeleteAnomalyDetectorRequest) Logger(org.apache.logging.log4j.Logger) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ImmutableList(com.google.common.collect.ImmutableList) Locale(java.util.Locale) AnomalyDetectorPlugin(org.opensearch.ad.AnomalyDetectorPlugin) BaseRestHandler(org.opensearch.rest.BaseRestHandler) LogManager(org.apache.logging.log4j.LogManager) DETECTOR_ID(org.opensearch.ad.util.RestHandlerUtils.DETECTOR_ID) DeleteAnomalyDetectorRequest(org.opensearch.ad.transport.DeleteAnomalyDetectorRequest)

Example 40 with RestToXContentListener

use of org.opensearch.rest.action.RestToXContentListener in project anomaly-detection by opensearch-project.

the class RestGetAnomalyDetectorAction method prepareRequest.

@Override
protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
    if (!EnabledSetting.isADPluginEnabled()) {
        throw new IllegalStateException(CommonErrorMessages.DISABLED_ERR_MSG);
    }
    String detectorId = request.param(DETECTOR_ID);
    String typesStr = request.param(TYPE);
    String rawPath = request.rawPath();
    boolean returnJob = request.paramAsBoolean("job", false);
    boolean returnTask = request.paramAsBoolean("task", false);
    boolean all = request.paramAsBoolean("_all", false);
    GetAnomalyDetectorRequest getAnomalyDetectorRequest = new GetAnomalyDetectorRequest(detectorId, RestActions.parseVersion(request), returnJob, returnTask, typesStr, rawPath, all, buildEntity(request, detectorId));
    return channel -> client.execute(GetAnomalyDetectorAction.INSTANCE, getAnomalyDetectorRequest, new RestToXContentListener<>(channel));
}
Also used : GetAnomalyDetectorRequest(org.opensearch.ad.transport.GetAnomalyDetectorRequest) CommonName(org.opensearch.ad.constant.CommonName) NodeClient(org.opensearch.client.node.NodeClient) PROFILE(org.opensearch.ad.util.RestHandlerUtils.PROFILE) RestRequest(org.opensearch.rest.RestRequest) IOException(java.io.IOException) EnabledSetting(org.opensearch.ad.settings.EnabledSetting) Strings(org.opensearch.common.Strings) List(java.util.List) CommonErrorMessages(org.opensearch.ad.constant.CommonErrorMessages) Logger(org.apache.logging.log4j.Logger) RestActions(org.opensearch.rest.action.RestActions) Entity(org.opensearch.ad.model.Entity) RestToXContentListener(org.opensearch.rest.action.RestToXContentListener) ImmutableList(com.google.common.collect.ImmutableList) Locale(java.util.Locale) Optional(java.util.Optional) AnomalyDetectorPlugin(org.opensearch.ad.AnomalyDetectorPlugin) GetAnomalyDetectorAction(org.opensearch.ad.transport.GetAnomalyDetectorAction) BaseRestHandler(org.opensearch.rest.BaseRestHandler) TYPE(org.opensearch.ad.util.RestHandlerUtils.TYPE) LogManager(org.apache.logging.log4j.LogManager) DETECTOR_ID(org.opensearch.ad.util.RestHandlerUtils.DETECTOR_ID) GetAnomalyDetectorRequest(org.opensearch.ad.transport.GetAnomalyDetectorRequest)

Aggregations

RestToXContentListener (org.opensearch.rest.action.RestToXContentListener)70 List (java.util.List)69 BaseRestHandler (org.opensearch.rest.BaseRestHandler)69 RestRequest (org.opensearch.rest.RestRequest)69 NodeClient (org.opensearch.client.node.NodeClient)68 IOException (java.io.IOException)67 Strings (org.opensearch.common.Strings)37 Arrays.asList (java.util.Arrays.asList)35 Collections.unmodifiableList (java.util.Collections.unmodifiableList)35 POST (org.opensearch.rest.RestRequest.Method.POST)30 GET (org.opensearch.rest.RestRequest.Method.GET)23 IndicesOptions (org.opensearch.action.support.IndicesOptions)21 Collections.singletonList (java.util.Collections.singletonList)18 PUT (org.opensearch.rest.RestRequest.Method.PUT)14 ImmutableList (com.google.common.collect.ImmutableList)11 Locale (java.util.Locale)11 Settings (org.opensearch.common.settings.Settings)10 Set (java.util.Set)9 XContentParser (org.opensearch.common.xcontent.XContentParser)9 DELETE (org.opensearch.rest.RestRequest.Method.DELETE)8