public List<Stage> getStages() throws AmbariException {
    long initialStageId = initialStage.getStageId();
    List<Stage> stageList = new ArrayList<>();
    List<RoleGraphNode> firstStageNodes = new ArrayList<>();
    if(!graph.isEmpty()){
      LOG.info("Detecting cycle graphs");
      LOG.info(stringifyGraph());
      breakCycleGraph();
    }
    while (!graph.isEmpty()) {
      if (LOG.isDebugEnabled()) {
        LOG.debug(stringifyGraph());
      }

      for (String role: graph.keySet()) {
        RoleGraphNode rgn = graph.get(role);
        if (rgn.getInDegree() == 0) {
          firstStageNodes.add(rgn);
        }
      }
      Stage aStage = getStageFromGraphNodes(initialStage, firstStageNodes);
      aStage.setStageId(++initialStageId);
      stageList.add(aStage);
      //Remove first stage nodes from the graph, we know that none of
      //these nodes have an incoming edges.
      for (RoleGraphNode rgn : firstStageNodes) {
        if (sameHostOptimization) {
          //Perform optimization
        }
        removeZeroInDegreeNode(rgn.getRole().toString());
      }
      firstStageNodes.clear();
    }
    return stageList;
  }
Returns a list of stages that need to be executed one after another
to execute the DAG generated in the last {@link #build(Stage)} call.
ambari-server/src/main/java/org/apache/ambari/server/stageplanner/RoleGraph.java