/*
 * Copyright © 2021-2022 Cask Data, Inc.
 *
 * 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 io.cdap.cdap.master.environment.k8s;

import com.google.common.util.concurrent.Service;
import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Module;
import com.google.inject.PrivateModule;
import com.google.inject.Scopes;
import io.cdap.cdap.app.guice.ProgramRunnerRuntimeModule;
import io.cdap.cdap.common.conf.CConfiguration;
import io.cdap.cdap.common.conf.Constants;
import io.cdap.cdap.common.guice.DFSLocationModule;
import io.cdap.cdap.common.guice.RemoteAuthenticatorModules;
import io.cdap.cdap.common.logging.LoggingContext;
import io.cdap.cdap.common.logging.ServiceLoggingContext;
import io.cdap.cdap.common.namespace.guice.NamespaceQueryAdminModule;
import io.cdap.cdap.data.runtime.DataSetsModules;
import io.cdap.cdap.data.runtime.SystemDatasetRuntimeModule;
import io.cdap.cdap.internal.app.store.StoreProgramRunRecordFetcher;
import io.cdap.cdap.internal.tethering.TetheringAgentService;
import io.cdap.cdap.internal.tethering.TetheringProgramEventPublisher;
import io.cdap.cdap.logging.gateway.handlers.ProgramRunRecordFetcher;
import io.cdap.cdap.master.spi.environment.MasterEnvironment;
import io.cdap.cdap.master.spi.environment.MasterEnvironmentContext;
import io.cdap.cdap.messaging.guice.MessagingClientModule;
import io.cdap.cdap.proto.id.NamespaceId;
import io.cdap.cdap.security.authorization.AuthorizationEnforcementModule;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nullable;
import org.apache.twill.zookeeper.ZKClientService;

/**
 * Main class for running remote agent service in Kubernetes.
 */
public class TetheringAgentServiceMain extends AbstractServiceMain<EnvironmentOptions> {

  /**
   * Main entry point
   */
  public static void main(String[] args) throws Exception {
    main(TetheringAgentServiceMain.class, args);
  }

  @Override
  protected List<Module> getServiceModules(MasterEnvironment masterEnv,
      EnvironmentOptions options, CConfiguration cConf) {
    return Arrays.asList(
        RemoteAuthenticatorModules.getDefaultModule(
            TetheringAgentService.REMOTE_TETHERING_AUTHENTICATOR,
            Constants.Tethering.CLIENT_AUTHENTICATOR_NAME),
        new MessagingClientModule(),
        new NamespaceQueryAdminModule(),
        getDataFabricModule(),
        // Always use local table implementations, which use LevelDB.
        // In K8s, there won't be HBase and the cdap-site should be set to use SQL store for StructuredTable.
        new SystemDatasetRuntimeModule().getStandaloneModules(),
        // The Dataset set modules are only needed to satisfy dependency injection
        new DataSetsModules().getStandaloneModules(),
        new AuthorizationEnforcementModule().getDistributedModules(),
        new ProgramRunnerRuntimeModule.ProgramStateWriterModule(),
        new DFSLocationModule(),
        new PrivateModule() {
          @Override
          protected void configure() {
            bind(TetheringAgentService.class).in(Scopes.SINGLETON);
            expose(TetheringAgentService.class);
            bind(TetheringProgramEventPublisher.class).in(Scopes.SINGLETON);
            expose(TetheringProgramEventPublisher.class);
            bind(ProgramRunRecordFetcher.class).to(StoreProgramRunRecordFetcher.class)
                .in(Scopes.SINGLETON);
            expose(ProgramRunRecordFetcher.class);
          }
        });
  }

  @Override
  protected void addServices(Injector injector, List<? super Service> services,
      List<? super AutoCloseable> closeableResources,
      MasterEnvironment masterEnv, MasterEnvironmentContext masterEnvContext,
      EnvironmentOptions options) {
    Binding<ZKClientService> zkBinding = injector.getExistingBinding(
        Key.get(ZKClientService.class));
    if (zkBinding != null) {
      services.add(zkBinding.getProvider().get());
    }
    services.add(injector.getInstance(TetheringAgentService.class));
    services.add(injector.getInstance(TetheringProgramEventPublisher.class));
  }

  @Nullable
  @Override
  protected LoggingContext getLoggingContext(EnvironmentOptions options) {
    return new ServiceLoggingContext(NamespaceId.SYSTEM.getNamespace(),
        Constants.Logging.COMPONENT_NAME,
        Constants.Service.REMOTE_AGENT_SERVICE);
  }
}
