/*
 * 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.distributed.test.log;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.db.guardrails.Guardrails;
import org.apache.cassandra.dht.Murmur3Partitioner;
import org.apache.cassandra.distributed.api.IIsolatedExecutor;
import org.apache.cassandra.harry.model.TokenPlacementModel;
import org.apache.cassandra.locator.MetaStrategy;
import org.apache.cassandra.schema.DistributedSchema;
import org.apache.cassandra.schema.KeyspaceMetadata;
import org.apache.cassandra.schema.Keyspaces;
import org.apache.cassandra.schema.SchemaTestUtil;
import org.apache.cassandra.tcm.ClusterMetadata;
import org.apache.cassandra.tcm.ClusterMetadataService;
import org.apache.cassandra.tcm.Commit;
import org.apache.cassandra.tcm.MetadataSnapshots;
import org.apache.cassandra.tcm.Processor;
import org.apache.cassandra.tcm.Transformation;
import org.apache.cassandra.tcm.log.LocalLog;
import org.apache.cassandra.tcm.ownership.UniformRangePlacement;
import org.apache.cassandra.tcm.transformations.AlterSchema;
import org.apache.cassandra.tcm.transformations.cms.Initialize;
import org.apache.cassandra.utils.FBUtilities;

public class CMSTestBase
{
    static
    {
        DatabaseDescriptor.daemonInitialization();
        DatabaseDescriptor.setDefaultKeyspaceRF(1);
        Guardrails.instance.setMinimumReplicationFactorThreshold(1, 1);

        try
        {
            DatabaseDescriptor.setBroadcastAddress(InetAddress.getByName("127.0.0.1"));
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }

    public static class CMSSut implements AutoCloseable
    {
        public final Murmur3Partitioner partitioner;
        public final LocalLog log;
        public final ClusterMetadataService service;
        public final TokenPlacementModel.ReplicationFactor rf;

        public CMSSut(IIsolatedExecutor.SerializableFunction<LocalLog, Processor> processorFactory, boolean addListeners, TokenPlacementModel.ReplicationFactor rf)
        {
            partitioner = Murmur3Partitioner.instance;
            this.rf = rf;
            ClusterMetadata initial = new ClusterMetadata(partitioner);
            log = LocalLog.logSpec()
                          .sync()
                          .withInitialState(initial)
                          .withDefaultListeners(addListeners)
                          .createLog();

            service = new ClusterMetadataService(new UniformRangePlacement(),
                                                 MetadataSnapshots.NO_OP,
                                                 log,
                                                 processorFactory.apply(log),
                                                 Commit.Replicator.NO_OP,
                                                 true);

            ClusterMetadataService.setInstance(service);
            log.readyUnchecked();
            log.unsafeBootstrapForTesting(FBUtilities.getBroadcastAddressAndPort());
            service.commit(new Initialize(ClusterMetadata.current()) {
                public Result execute(ClusterMetadata prev)
                {
                    ClusterMetadata next = baseState;
                    DistributedSchema initialSchema = new DistributedSchema(prev.schema.getKeyspaces());
                    ClusterMetadata.Transformer transformer = next.transformer().with(initialSchema);
                    return Transformation.success(transformer, MetaStrategy.affectedRanges(prev));
                }

            });
            service.commit(new AlterSchema(SchemaTestUtil.toTransformation(metadata -> metadata.schema.getKeyspaces().with(Keyspaces.of(KeyspaceMetadata.create("test", rf.asKeyspaceParams()))))));
        }

        public void close()
        {
            ClusterMetadataService.unsetInstance();
        }
    }
}
