/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.cassandra.tools.nodetool;

import org.apache.cassandra.tools.NodeProbe;

import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

@Command(name = "rebuild", description = "Rebuild data by streaming from other nodes (similarly to bootstrap)")
public class Rebuild extends AbstractCommand
{
    @Parameters(paramLabel = "src-dc-name",
                description = "Name of DC from which to select sources for streaming. By default, pick any DC (except local DC when --exclude-local-dc is set)",
                index = "0",
                arity = "0..1")
    private String sourceDataCenterName = null;

    @Option(paramLabel = "specific_keyspace",
            names = {"-ks", "--keyspace"},
            description = "Use -ks to rebuild specific keyspace.")
    private String keyspace = null;

    @Option(paramLabel = "specific_tokens",
            names = {"-ts", "--tokens"},
            description = "Use -ts to rebuild specific token ranges, in the format of \"(start_token_1,end_token_1],(start_token_2,end_token_2],...(start_token_n,end_token_n]\".")
    private String tokens = null;

    @Option(paramLabel = "specific_sources",
            names = {"-s", "--sources"},
            description = "Use -s to specify hosts that this node should stream from when -ts is used. Multiple hosts should be separated using commas (e.g. 127.0.0.1,127.0.0.2,...)")
    private String specificSources = null;

    @Option(paramLabel = "exclude_local_dc",
            names = {"--exclude-local-dc"},
            description = "Use --exclude-local-dc to exclude nodes in local data center as source for streaming.")
    private boolean excludeLocalDatacenterNodes = false;

    @Override
    public void execute(NodeProbe probe)
    {
        // check the arguments
        if (keyspace == null && tokens != null)
        {
            throw new IllegalArgumentException("Cannot specify tokens without keyspace.");
        }

        probe.rebuild(sourceDataCenterName, keyspace, tokens, specificSources, excludeLocalDatacenterNodes);
    }
}
