package com.thinkbiganalytics.servicemonitor.rest.client.cdh;

import com.cloudera.api.ApiRootResource;
import com.cloudera.api.ClouderaManagerClientBuilder;
import com.thinkbiganalytics.servicemonitor.rest.client.RestClientConfig;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by sr186054 on 10/1/15.
 */
@Component
public class ClouderaClient {

  private static final Logger LOG = LoggerFactory.getLogger(ClouderaClient.class);

  @Autowired
  @Qualifier("clouderaRestClientConfig")
  private RestClientConfig clientConfig;

  private ClouderaManagerClientBuilder clouderaManagerClientBuilder;

  private ClouderaRootResource clouderaRootResource;

  private AtomicBoolean creatingResource = new AtomicBoolean(false);


  public ClouderaClient() {

  }

  public ClouderaClient(RestClientConfig clientConfig) {

    this.clientConfig = clientConfig;
  }

  @PostConstruct
  public void setClouderaManagerClientBuilder() {
    String host = clientConfig.getServerUrl();
    String portString = clientConfig.getPort();
    if (StringUtils.isBlank(portString)) {
      portString = "7180";
    }
    Integer port = new Integer(portString);
    String username = clientConfig.getUsername();
    String password = clientConfig.getPassword();
    LOG.info("Created New Cloudera Client for Host [" + host + "], user: [" + username + "]");
    this.clouderaManagerClientBuilder =
        new ClouderaManagerClientBuilder().withHost(host).withPort(port).withUsernamePassword(username, password);

  }


  public ClouderaRootResource getClouderaResource() {
    if(clouderaRootResource == null && !creatingResource.get()) {
      creatingResource.set(true);
      ApiRootResource rootResource = this.clouderaManagerClientBuilder.build();
      clouderaRootResource = ClouderaRootResourceManager.getRootResource(rootResource);
      LOG.info("Successfully Created Cloudera Client");
    }
    return  clouderaRootResource;
  }

  public void setClientConfig(RestClientConfig clientConfig) {
    this.clientConfig = clientConfig;
  }


}

package com.thinkbiganalytics.servicemonitor.rest.client.cdh;

import com.cloudera.api.ApiRootResource;
import com.cloudera.api.ClouderaManagerClientBuilder;
import com.thinkbiganalytics.servicemonitor.rest.client.RestClientConfig;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by sr186054 on 10/1/15.
 */
@Component
public class ClouderaClient {

  private static final Logger LOG = LoggerFactory.getLogger(ClouderaClient.class);

  @Autowired
  @Qualifier("clouderaRestClientConfig")
  private RestClientConfig clientConfig;

  private ClouderaManagerClientBuilder clouderaManagerClientBuilder;

  private ClouderaRootResource clouderaRootResource;

  private AtomicBoolean creatingResource = new AtomicBoolean(false);

 private Integer clientAttempts = 0;


  public ClouderaClient() {

  }

  public ClouderaClient(RestClientConfig clientConfig) {

    this.clientConfig = clientConfig;
  }

  @PostConstruct
  public void setClouderaManagerClientBuilder() {
    String host = clientConfig.getServerUrl();
    String portString = clientConfig.getPort();
    if (StringUtils.isBlank(portString)) {
      portString = "7180";
    }
    Integer port = new Integer(portString);
    String username = clientConfig.getUsername();
    String password = clientConfig.getPassword();
    LOG.info("Created New Cloudera Client for Host [" + host + "], user: [" + username + "]");
    this.clouderaManagerClientBuilder =
        new ClouderaManagerClientBuilder().withHost(host).withPort(port).withUsernamePassword(username, password);

  }


  public ClouderaRootResource getClouderaResource() {
    if(clouderaRootResource == null){
      this.clientAttempts++;
    }
    if(clouderaRootResource == null && !creatingResource.get()) {
      creatingResource.set(true);
      try {
        ApiRootResource rootResource = this.clouderaManagerClientBuilder.build();
        clouderaRootResource = ClouderaRootResourceManager.getRootResource(rootResource);
        LOG.info("Successfully Created Cloudera Client");
      }catch (Exception e){
        creatingResource.set(false);
      }
    }
    if(clientAttempts >=3){
      //rest
      clientAttempts = 0;
      creatingResource.set(false);
    }
    return  clouderaRootResource;
  }

  public void setClientConfig(RestClientConfig clientConfig) {
    this.clientConfig = clientConfig;
  }


}

fix service monitor bug if thinkbig-services comes up before cloudera is up