private void orderGroups(List<Grouping> groups, Map<String, Grouping> mergedGroupMap) throws AmbariException {
    Map<String, List<Grouping>> skippedGroups = new HashMap<>();

    for (Map.Entry<String, Grouping> entry : mergedGroupMap.entrySet()) {
      Grouping group = entry.getValue();

      if (!groups.contains(group)) {
        boolean added = addGrouping(groups, group);
        if (added) {
          addSkippedGroup(groups, skippedGroups, group);
        } else {
          List<Grouping> tmp = null;

          // store the group until later
          if (skippedGroups.containsKey(group.addAfterGroup)) {
            tmp = skippedGroups.get(group.addAfterGroup);
          } else {
            tmp = new ArrayList<>();
            skippedGroups.put(group.addAfterGroup, tmp);
          }
          tmp.add(group);
        }
      }
    }

    if (!skippedGroups.isEmpty()) {
      throw new AmbariException("Missing groups: " + skippedGroups.keySet());
    }
  }
Orders the upgrade groups.  All new groups specified in a service's upgrade file must
specify after which group they should be placed in the upgrade order.
ambari-server/src/main/java/org/apache/ambari/server/stack/StackModule.java