001/*
002 * Copyright (C) 2014 Jörg Prante
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.xbib.elasticsearch.action.plugin.jdbc.state.delete;
017
018import org.elasticsearch.ElasticsearchException;
019import org.elasticsearch.action.ActionListener;
020import org.elasticsearch.action.support.master.TransportMasterNodeOperationAction;
021import org.elasticsearch.cluster.ClusterService;
022import org.elasticsearch.cluster.ClusterState;
023import org.elasticsearch.cluster.ack.ClusterStateUpdateResponse;
024import org.elasticsearch.cluster.block.ClusterBlockException;
025import org.elasticsearch.cluster.block.ClusterBlockLevel;
026import org.elasticsearch.common.inject.Inject;
027import org.elasticsearch.common.inject.Injector;
028import org.elasticsearch.common.settings.Settings;
029import org.elasticsearch.threadpool.ThreadPool;
030import org.elasticsearch.transport.TransportService;
031import org.xbib.elasticsearch.plugin.jdbc.state.RiverStateService;
032
033public class TransportDeleteRiverStateAction extends TransportMasterNodeOperationAction<DeleteRiverStateRequest, DeleteRiverStateResponse> {
034
035    private final Injector injector;
036
037    @Inject
038    public TransportDeleteRiverStateAction(Settings settings, ThreadPool threadPool,
039                                           ClusterService clusterService, TransportService transportService,
040                                           Injector injector) {
041        super(settings, DeleteRiverStateAction.NAME, transportService, clusterService, threadPool);
042        this.injector = injector;
043    }
044
045    @Override
046    protected String executor() {
047        return ThreadPool.Names.MANAGEMENT;
048    }
049
050    @Override
051    protected DeleteRiverStateRequest newRequest() {
052        return new DeleteRiverStateRequest();
053    }
054
055    @Override
056    protected DeleteRiverStateResponse newResponse() {
057        return new DeleteRiverStateResponse();
058    }
059
060    @Override
061    protected ClusterBlockException checkBlock(DeleteRiverStateRequest request, ClusterState state) {
062        return state.blocks().indexBlockedException(ClusterBlockLevel.METADATA, "");
063    }
064
065    @Override
066    protected void masterOperation(DeleteRiverStateRequest request, ClusterState state, final ActionListener<DeleteRiverStateResponse> listener) throws ElasticsearchException {
067        RiverStateService riverStateService = injector.getInstance(RiverStateService.class);
068        riverStateService.deleteRiverState(new RiverStateService.DeleteRiverStateRequest("delete_river_state[" + request.getRiverName() + "/" + request.getRiverType() + "]", request.getRiverName(), request.getRiverType())
069                .masterNodeTimeout(request.masterNodeTimeout())
070                .ackTimeout(request.ackTimeout()), new ActionListener<ClusterStateUpdateResponse>() {
071            @Override
072            public void onResponse(ClusterStateUpdateResponse clusterStateUpdateResponse) {
073                listener.onResponse(new DeleteRiverStateResponse(clusterStateUpdateResponse.isAcknowledged()));
074            }
075
076            @Override
077            public void onFailure(Throwable e) {
078                listener.onFailure(e);
079            }
080        });
081    }
082
083}