/**
* 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.tez.dag.app.dag;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.yarn.api.records.ContainerId;
import org.apache.hadoop.yarn.api.records.LocalResource;
import org.apache.hadoop.yarn.event.EventHandler;
import org.apache.tez.common.counters.DAGCounter;
import org.apache.tez.common.counters.TezCounters;
import org.apache.tez.dag.api.TezException;
import org.apache.tez.dag.api.client.DAGStatusBuilder;
import org.apache.tez.dag.api.client.StatusGetOpts;
import org.apache.tez.dag.api.client.VertexStatusBuilder;
import org.apache.tez.dag.api.records.DAGProtos.DAGPlan;
import org.apache.tez.common.security.ACLManager;
import org.apache.tez.dag.records.TezDAGID;
import org.apache.tez.dag.records.TezVertexID;
import org.apache.tez.serviceplugins.api.DagInfo;

import javax.annotation.Nullable;

/**
 * Main interface to interact with the job.
 */
public interface DAG extends DagInfo {

  TezDAGID getID();
  Map<String, LocalResource> getLocalResources();
  String getName();
  DAGState getState();
  DAGReport getReport();

  /**
   * Get all the counters of this DAG. This includes job-counters aggregated
   * together with the counters of each task. This creates a clone of the
   * Counters, so use this judiciously.
   * @return job-counters and aggregate task-counters
   */
  TezCounters getAllCounters();
  TezCounters getCachedCounters();

  @SuppressWarnings("rawtypes")
  EventHandler getEventHandler();

  /**
   * Get Vertex by vertex name
   */
  Vertex getVertex(String vertexName);
  Map<TezVertexID,Vertex> getVertices();
  Vertex getVertex(TezVertexID vertexId);
  List<String> getDiagnostics();
  int getSuccessfulVertices();
  float getProgress();
  float getCompletedTaskProgress();
  boolean isUber();
  String getUserName();

  DAGPlan getJobPlan();
  DAGStatusBuilder getDAGStatus(Set<StatusGetOpts> statusOptions);
  DAGStatusBuilder getDAGStatus(Set<StatusGetOpts> statusOptions, long timeout)
      throws TezException;
  VertexStatusBuilder getVertexStatus(String vertexName,
                                      Set<StatusGetOpts> statusOptions);

  boolean isComplete();

  UserGroupInformation getDagUGI();

  ACLManager getACLManager();

  Map<String, TezVertexID> getVertexNameIDMapping();

  long getStartTime();

  StateChangeNotifier getStateChangeNotifier();

  org.apache.tez.dag.api.Vertex.VertexExecutionContext getDefaultExecutionContext();

  /**
   *
   * @return the DAGScheduler that will schedule
   * this DAG, null if it doesn't exist
   */
  @Nullable DAGScheduler getDAGScheduler();

  void incrementDagCounter(DAGCounter counter, int incrValue);
  void setDagCounter(DAGCounter counter, int setValue);
  void addUsedContainer(ContainerId containerId);

  /**
   * Called by the DAGAppMaster when the DAG is started normally or in the event of recovery.
   */
  void onStart();

  /**
   * Called by the DAGAppMaster when the DAG is finished, or there is a currentDAG on AM stop.
   * The implementation of this method should be idempontent.
   */
  void onFinish();
}
