/*
 * Copyright [2017] Wikimedia Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.o19s.es.ltr.rest;

import com.o19s.es.ltr.action.CachesStatsAction;
import com.o19s.es.ltr.action.ClearCachesAction;
import com.o19s.es.ltr.action.ClearCachesAction.ClearCachesNodesResponse;
import org.elasticsearch.client.node.NodeClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.action.RestActions.NodesResponseRestListener;
import org.elasticsearch.rest.action.RestBuilderListener;

import java.io.IOException;

import static org.elasticsearch.rest.RestStatus.OK;

/**
 * Clear cache (default store):
 * POST /_ltr/_clearcache
 *
 * Clear cache (custom store):
 * POST /_ltr/{store}/_clearcache
 *
 * Get cache stats (all stores)
 * GET /_ltr/_cachestats
 */
public class RestFeatureStoreCaches extends FeatureStoreBaseRestHandler {

    public RestFeatureStoreCaches(Settings settings, RestController controller) {
        super(settings);
        controller.registerHandler(RestRequest.Method.POST, "/_ltr/_clearcache", this);
        controller.registerHandler(RestRequest.Method.POST, "/_ltr/{store}/_clearcache", this);
        controller.registerHandler(RestRequest.Method.GET, "/_ltr/_cachestats", this);
    }

    /**
     * Prepare the request for execution. Implementations should consume all request params before
     * returning the runnable for actual execution. Unconsumed params will immediately terminate
     * execution of the request. However, some params are only used in processing the response;
     * implementations can override {@link BaseRestHandler#responseParams()} to indicate such
     * params.
     *
     * @param request the request to execute
     * @param client  client for executing actions on the local node
     * @return the action to execute
     * @throws IOException if an I/O exception occurred parsing the request and preparing for
     *                     execution
     */
    @Override
    protected RestChannelConsumer prepareRequest(RestRequest request, NodeClient client) throws IOException {
        if (request.method() == RestRequest.Method.PUT) {
            return clearCache(request, client);
        } else {
            return getStats(client);
        }
    }

    private RestChannelConsumer getStats(NodeClient client) {
        return (channel) -> CachesStatsAction.INSTANCE.newRequestBuilder(client).execute(new NodesResponseRestListener<>(channel));
    }

    private RestChannelConsumer clearCache(RestRequest request, NodeClient client) {
        String storeName = indexName(request);
        ClearCachesAction.RequestBuilder builder = ClearCachesAction.INSTANCE.newRequestBuilder(client);
        builder.request().clearStore(storeName);
        return (channel) -> builder.execute(new RestBuilderListener<ClearCachesNodesResponse>(channel) {
            @Override
            public RestResponse buildResponse(ClearCachesNodesResponse clearCachesNodesResponse,
                                              XContentBuilder builder) throws Exception {
                builder.startObject()
                        .field("acknowledged", true);
                builder.endObject();
                return new BytesRestResponse(OK, builder);
            }
        });
    }
}

@@ -966,55 +966,8 @@
 er;%0A
-import org.elasticsearch.rest.BaseRestHandler;%0A
 impo
@@ -2019,714 +2019,8 @@
  %7D%0A%0A
-    /**%0A     * Prepare the request for execution. Implementations should consume all request params before%0A     * returning the runnable for actual execution. Unconsumed params will immediately terminate%0A     * execution of the request. However, some params are only used in processing the response;%0A     * implementations can override %7B@link BaseRestHandler#responseParams()%7D to indicate such%0A     * params.%0A     *%0A     * @param request the request to execute%0A     * @param client  client for executing actions on the local node%0A     * @return the action to execute%0A     * @throws IOException if an I/O exception occurred parsing the request and preparing for%0A     *                     execution%0A     */%0A
     
@@ -2025,24 +2025,24 @@
   @Override%0A
+
     protecte
@@ -2191,17 +2191,18 @@
 Method.P
-U
+OS
 T) %7B%0A   

Fix clear cache rest API