@Override
  public int run(String[] args) throws Exception {
    YarnConfiguration yarnConf =
        getConf() == null ? new YarnConfiguration() : new YarnConfiguration(
            getConf());
    boolean isHAEnabled =
        yarnConf.getBoolean(YarnConfiguration.RM_HA_ENABLED,
            YarnConfiguration.DEFAULT_RM_HA_ENABLED);

    if (args.length < 1) {
      printUsage("", isHAEnabled);
      return -1;
    }

    int exitCode = -1;
    int i = 0;
    String cmd = args[i++];

    exitCode = 0;
    if ("-help".equals(cmd)) {
      if (i < args.length) {
        printUsage(args[i], isHAEnabled);
      } else {
        printHelp("", isHAEnabled);
      }
      return exitCode;
    }

    if (USAGE.containsKey(cmd)) {
      if (isHAEnabled) {
        return super.run(args);
      }
      System.out.println("Cannot run " + cmd
          + " when ResourceManager HA is not enabled");
      return -1;
    }

    //
    // verify that we have enough command line parameters
    //
    String subClusterId = StringUtils.EMPTY;
    if ("-refreshAdminAcls".equals(cmd) || "-refreshQueues".equals(cmd) ||
        "-refreshNodesResources".equals(cmd) ||
        "-refreshServiceAcl".equals(cmd) ||
        "-refreshUserToGroupsMappings".equals(cmd) ||
        "-refreshSuperUserGroupsConfiguration".equals(cmd) ||
        "-refreshClusterMaxPriority".equals(cmd)) {
      subClusterId = parseSubClusterId(args, isHAEnabled);
      // If we enable Federation mode, the number of args may be either one or three.
      // Example: -refreshQueues or -refreshQueues -subClusterId SC-1
      if (isYarnFederationEnabled(getConf()) && args.length != 1 && args.length != 3) {
        printUsage(cmd, isHAEnabled);
        return exitCode;
      } else if (!isYarnFederationEnabled(getConf()) && args.length != 1) {
        // If Federation mode is not enabled, then the number of args can only be one.
        // Example: -refreshQueues
        printUsage(cmd, isHAEnabled);
        return exitCode;
      }
    }

    // If it is federation mode, we will print federation mode information
    if (isYarnFederationEnabled(getConf())) {
      System.out.println("Using YARN Federation mode.");
    }

    try {
      if ("-refreshQueues".equals(cmd)) {
        exitCode = refreshQueues(subClusterId);
      } else if ("-refreshNodes".equals(cmd)) {
        exitCode = handleRefreshNodes(args, cmd, isHAEnabled);
      } else if ("-refreshNodesResources".equals(cmd)) {
        exitCode = refreshNodesResources(subClusterId);
      } else if ("-refreshUserToGroupsMappings".equals(cmd)) {
        exitCode = refreshUserToGroupsMappings(subClusterId);
      } else if ("-refreshSuperUserGroupsConfiguration".equals(cmd)) {
        exitCode = refreshSuperUserGroupsConfiguration(subClusterId);
      } else if ("-refreshAdminAcls".equals(cmd)) {
        exitCode = refreshAdminAcls(subClusterId);
      } else if ("-refreshServiceAcl".equals(cmd)) {
        exitCode = refreshServiceAcls(subClusterId);
      } else if ("-refreshClusterMaxPriority".equals(cmd)) {
        exitCode = refreshClusterMaxPriority(subClusterId);
      } else if ("-getGroups".equals(cmd)) {
        String[] usernames = Arrays.copyOfRange(args, i, args.length);
        exitCode = getGroups(usernames);
      } else if ("-updateNodeResource".equals(cmd)) {
        exitCode = handleUpdateNodeResource(args, cmd, isHAEnabled, subClusterId);
      } else if ("-addToClusterNodeLabels".equals(cmd)) {
        exitCode = handleAddToClusterNodeLabels(args, cmd, isHAEnabled);
      } else if ("-removeFromClusterNodeLabels".equals(cmd)) {
        exitCode = handleRemoveFromClusterNodeLabels(args, cmd, isHAEnabled);
      } else if ("-replaceLabelsOnNode".equals(cmd)) {
        exitCode = handleReplaceLabelsOnNodes(args, cmd, isHAEnabled);
      } else {
        exitCode = -1;
        System.err.println(cmd.substring(1) + ": Unknown command");
        printUsage("", isHAEnabled);
      }

    } catch (IllegalArgumentException arge) {
      exitCode = -1;
      System.err.println(cmd.substring(1) + ": " + arge.getLocalizedMessage());
      printUsage(cmd, isHAEnabled);
    } catch (RemoteException e) {
      //
      // This is a error returned by hadoop server. Print
      // out the first line of the error message, ignore the stack trace.
      exitCode = -1;
      try {
        String[] content;
        content = e.getLocalizedMessage().split("\n");
        System.err.println(cmd.substring(1) + ": "
                           + content[0]);
      } catch (Exception ex) {
        System.err.println(cmd.substring(1) + ": "
                           + ex.getLocalizedMessage());
      }
    } catch (Exception e) {
      exitCode = -1;
      System.err.println(cmd.substring(1) + ": "
                         + e.getLocalizedMessage());
    }
    if (null != localNodeLabelsManager) {
      localNodeLabelsManager.stop();
    }
    return exitCode;
  }
@Test
  public void testAddToClusterNodeLabelsWithExclusivitySetting()
      throws Exception {
    // Parenthese not match
    String[] args = new String[] { "-addToClusterNodeLabels", "x(" };
    assertTrue(0 != rmAdminCLI.run(args));

    args = new String[] { "-addToClusterNodeLabels", "x)" };
    assertTrue(0 != rmAdminCLI.run(args));

    // Not expected key=value specifying inner parentese
    args = new String[] { "-addToClusterNodeLabels", "x(key=value)" };
    assertTrue(0 != rmAdminCLI.run(args));

    // Not key is expected, but value not
    args = new String[] { "-addToClusterNodeLabels", "x(exclusive=)" };
    assertTrue(0 != rmAdminCLI.run(args));

    // key=value both set
    args =
        new String[] { "-addToClusterNodeLabels",
            "w,x(exclusive=true), y(exclusive=false),z()",
            "-directlyAccessNodeLabelStore" };
    assertTrue(0 == rmAdminCLI.run(args));

    assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("w"));
    assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("x"));
    assertFalse(dummyNodeLabelsManager.isExclusiveNodeLabel("y"));
    assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("z"));

    // key=value both set, and some spaces need to be handled
    args =
        new String[] { "-addToClusterNodeLabels",
            "a (exclusive= true) , b( exclusive =false),c  ",
            "-directlyAccessNodeLabelStore" };
    assertTrue(0 == rmAdminCLI.run(args));

    assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("a"));
    assertFalse(dummyNodeLabelsManager.isExclusiveNodeLabel("b"));
    assertTrue(dummyNodeLabelsManager.isExclusiveNodeLabel("c"));
  }