public List<Map<String, List<HostRoleCommand>>> getOrderedHostRoleCommands(
      Map<String, Map<String, HostRoleCommand>> unorderedCommands) {
    build(unorderedCommands);

    // represents an ordered list of stages
    List<Map<String, List<HostRoleCommand>>> orderedCommands = new ArrayList<>();

    List<RoleGraphNode> firstStageNodes = new ArrayList<>();
    while (!graph.isEmpty()) {
      for (String role : graph.keySet()) {
        RoleGraphNode rgn = graph.get(role);
        if (rgn.getInDegree() == 0) {
          firstStageNodes.add(rgn);
        }
      }

      // represents a stage
      Map<String, List<HostRoleCommand>> commandsPerHost = new HashMap<>();

      for (RoleGraphNode rgn : firstStageNodes) {
        // for every host for this stage, create the ordered commands
        for (String host : rgn.getHosts()) {
          List<HostRoleCommand> commands = commandsPerHost.get(host);
          if (null == commands) {
            commands = new ArrayList<>();
            commandsPerHost.put(host, commands);
          }

          HostRoleCommand hrc = hrcFactory.create(host, rgn.getRole(), null, rgn.getCommand());
          commands.add(hrc);
        }
      }

      // add the stage to the list of stages
      orderedCommands.add(commandsPerHost);

      // Remove first stage nodes from the graph, we know that none of
      // these nodes have an incoming edges.
      for (RoleGraphNode rgn : firstStageNodes) {
        removeZeroInDegreeNode(rgn.getRole().toString());
      }

      firstStageNodes.clear();
    }

    return orderedCommands;
  }
Gets a representation of the role ordering of the specified commands
without constructing {@link Stage} instances. The commands to order are
supplied as mapping of host to role/command. Each item of the returned list
represents a single stage. The map is of host to commands. For example:
<br/>
<br/>
Input:
<pre>
{c6401={NAMENODE=STOP}, c6402={DATANODE=STOP}, NODEMANAGER=STOP}}
</pre>

Output:
<pre>
[{c6402=[NODEMANAGER/STOP, DATANODE-STOP]}, c6401=[NAMENODE/STOP]]

<pre>

@param unorderedCommands
         a mapping of {@link Role} to {@link HostRoleCommand} by host.
@return and ordered list where each item represents a single stage and each
        stage's commands are mapped by host.
ambari-server/src/main/java/org/apache/ambari/server/stageplanner/RoleGraph.java