private boolean addGroupingStages(List<ExecuteStage> stagesToAdd, String after) {
    if (after == null) {
      executionStages.addAll(stagesToAdd);
      return true;
    }
    else {
      // Check the current stages, if the "after" stage is there then add these
      for (int index = executionStages.size() - 1; index >= 0; index--) {
        ExecuteStage stage = executionStages.get(index);
        if ((stage.service != null && stage.service.equals(after)) || stage.title.equals(after)) {
          executionStages.addAll(index + 1, stagesToAdd);
          return true;
        }
      }
    }
    return false;
  }
Adds the given stages if the stage they are supposed to come after has been added.
ambari-server/src/main/java/org/apache/ambari/server/stack/upgrade/ClusterGrouping.java