package com.cloudera.cmf.service.yarn;

import com.cloudera.cmf.command.flow.CmdWorkCtx;
import com.cloudera.cmf.command.flow.WorkOutput;
import com.cloudera.cmf.command.flow.work.OneOffRoleProcCmdWork;
import com.cloudera.cmf.model.DbProcess;
import com.cloudera.cmf.model.DbRole;
import com.cloudera.cmf.model.RoleState;
import com.cloudera.cmf.service.ServiceDataProvider;
import com.cloudera.cmf.service.components.ProcessHelper;
import com.cloudera.enterprise.MessageWithArgs;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.util.List;

public class RmFormatStateStoreCmdWork extends OneOffRoleProcCmdWork
{
  private RmFormatStateStoreCmdWork(@JsonProperty("roleId") Long roleId)
  {
    super(roleId);
  }

  public MessageWithArgs getDescription(CmdWorkCtx ctx)
  {
    return MessageWithArgs.of("message.command.rmFormatStateStoreCmdWork.desc", new String[0]);
  }

  public String getProcessName()
  {
    return "rm-format-state-store";
  }

  protected void beforeProcessCreation(CmdWorkCtx ctx, DbProcess proc, DbRole role)
  {
    ctx.getServiceDataProvider().getProcessHelper().runAsRole(proc, role);
    List args = ImmutableList.of("resourcemanager", "-format-state-store");
    proc.setProgram("yarn/yarn.sh");
    proc.setArguments(args);
  }

  protected RoleState getRoleStateAfterProcess(WorkOutput output, CmdWorkCtx ctx)
  {
    return RoleState.STOPPED;
  }

  public static RmFormatStateStoreCmdWork of(DbRole r) {
    Preconditions.checkNotNull(r);
    Preconditions.checkNotNull(r.getId());
    Preconditions.checkArgument(YarnServiceHandler.RoleNames.RESOURCEMANAGER.name().equals(r.getRoleType()));

    return new RmFormatStateStoreCmdWork(r.getId());
  }
}