class FixCapacitySchedulerOrderingPolicy extends AbstractUpgradeServerAction {

  @Override
  public CommandReport execute(ConcurrentMap<String, Object> requestSharedDataContext)
      throws AmbariException, InterruptedException {


    String clusterName = getExecutionCommand().getClusterName();
    Cluster cluster = getClusters().getCluster(clusterName);
    Config config = cluster.getDesiredConfigByType(SOURCE_CONFIG_TYPE);

    if (null == config) {
      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}",
          String.format("The cluster does not have %s defined.", SOURCE_CONFIG_TYPE), "");
    }

    Map<String, String> properties = config.getProperties();

    Set<String> parentQueueNames = new HashSet<>();

    // first find the parent queue names
    for (String key : properties.keySet()) {
      Matcher matcher = ROOT_QUEUE_REGEX.matcher(key);
      if (matcher.matches() && 1 == matcher.groupCount()) {
        parentQueueNames.add(matcher.group(1));
      }
    }

    if (parentQueueNames.isEmpty()) {
      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}",
          String.format("The %s has no root queue names.", SOURCE_CONFIG_TYPE), "");
    }

    boolean changedProperties = false;
    StringBuilder stdout = new StringBuilder();


    for (String queueName : parentQueueNames) {
      String orderingPolicyKey = String.format("%s.%s.%s", CAPACITY_SCHEDULER_PREFIX, queueName, ORDERING_POLICY_SUFFIX);

      String orderingPolicyValue = properties.get(orderingPolicyKey);
      if (null == orderingPolicyValue) {
        stdout.append("Ordering policy not found for ").append(orderingPolicyKey).append(',')
          .append(" value will not be set.").append(System.lineSeparator());

      } else if (!orderingPolicyValue.equals(UTILIZATION) || !orderingPolicyValue.equals(PRIORITY_UTILIZATION)) {
        properties.put(orderingPolicyKey, UTILIZATION);
        changedProperties = true;

        stdout.append("Changed ordering policy on ").append(orderingPolicyKey)
          .append(" from '").append(orderingPolicyValue).append("' to '").append(UTILIZATION)
          .append('\'').append(System.lineSeparator());
      }
    }

    if (!changedProperties) {
      return createCommandReport(0, HostRoleStatus.COMPLETED, "{}",
          String.format("No root queues required updating to %s.", UTILIZATION), "");
    }

    config.setProperties(properties);
    config.save();
    agentConfigsHolder.updateData(cluster.getClusterId(), cluster.getHosts().stream().map(Host::getHostId).collect(Collectors.toList()));

    return createCommandReport(0, HostRoleStatus.COMPLETED, "{}", stdout.toString(), "");
  }

}