@Override
  public GetClusterMetricsResponse getClusterMetrics(
      GetClusterMetricsRequest request) throws YarnException {
    GetClusterMetricsResponse response = recordFactory
        .newRecordInstance(GetClusterMetricsResponse.class);
    YarnClusterMetrics ymetrics = recordFactory
        .newRecordInstance(YarnClusterMetrics.class);
    ymetrics.setNumNodeManagers(this.rmContext.getRMNodes().size());
    ClusterMetrics clusterMetrics = ClusterMetrics.getMetrics();
    ymetrics.setNumDecommissioningNodeManagers(clusterMetrics.getNumDecommissioningNMs());
    ymetrics.setNumDecommissionedNodeManagers(clusterMetrics
      .getNumDecommisionedNMs());
    ymetrics.setNumActiveNodeManagers(clusterMetrics.getNumActiveNMs());
    ymetrics.setNumLostNodeManagers(clusterMetrics.getNumLostNMs());
    ymetrics.setNumUnhealthyNodeManagers(clusterMetrics.getUnhealthyNMs());
    ymetrics.setNumRebootedNodeManagers(clusterMetrics.getNumRebootedNMs());
    ymetrics.setNumShutdownNodeManagers(clusterMetrics.getNumShutdownNMs());
    response.setClusterMetrics(ymetrics);
    return response;
  }
@Test
  public void testGetClusterMetrics() throws Exception {
    MockRM rm = new MockRM() {
      protected ClientRMService createClientRMService() {
        return new ClientRMService(this.rmContext, scheduler,
          this.rmAppManager, this.applicationACLsManager, this.queueACLsManager,
          this.getRMContext().getRMDelegationTokenSecretManager());
      };
    };
    resourceManager = rm;
    rm.start();

    ClusterMetrics clusterMetrics = ClusterMetrics.getMetrics();
    clusterMetrics.incrDecommissioningNMs();
    repeat(2, clusterMetrics::incrDecommisionedNMs);
    repeat(3, clusterMetrics::incrNumActiveNodes);
    repeat(4, clusterMetrics::incrNumLostNMs);
    repeat(5, clusterMetrics::incrNumUnhealthyNMs);
    repeat(6, clusterMetrics::incrNumRebootedNMs);
    repeat(7, clusterMetrics::incrNumShutdownNMs);

    // Create a client.
    conf = new Configuration();
    rpc = YarnRPC.create(conf);
    InetSocketAddress rmAddress = rm.getClientRMService().getBindAddress();
    LOG.info("Connecting to ResourceManager at " + rmAddress);
    client = (ApplicationClientProtocol) rpc.getProxy(
        ApplicationClientProtocol.class, rmAddress, conf);

    YarnClusterMetrics ymetrics = client.getClusterMetrics(
        GetClusterMetricsRequest.newInstance()).getClusterMetrics();

    Assert.assertEquals(0, ymetrics.getNumNodeManagers());
    Assert.assertEquals(1, ymetrics.getNumDecommissioningNodeManagers());
    Assert.assertEquals(2, ymetrics.getNumDecommissionedNodeManagers());
    Assert.assertEquals(3, ymetrics.getNumActiveNodeManagers());
    Assert.assertEquals(4, ymetrics.getNumLostNodeManagers());
    Assert.assertEquals(5, ymetrics.getNumUnhealthyNodeManagers());
    Assert.assertEquals(6, ymetrics.getNumRebootedNodeManagers());
    Assert.assertEquals(7, ymetrics.getNumShutdownNodeManagers());
  }