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;
  }


}

@@ -592,16 +592,66 @@
 Boolean;
+%0Aimport java.util.concurrent.atomic.AtomicInteger;
 %0A%0A/**%0A *
@@ -1103,16 +1103,54 @@
 alse);%0A%0A
+ private Integer clientAttempts = 0;%0A%0A
 %0A  publi
@@ -1966,24 +1966,97 @@
 esource() %7B%0A
+    if(clouderaRootResource == null)%7B%0A      this.clientAttempts++;%0A    %7D%0A
     if(cloud
@@ -2139,24 +2139,38 @@
 .set(true);%0A
+      try %7B%0A  
       ApiRoo
@@ -2233,24 +2233,26 @@
 er.build();%0A
+  
       cloude
@@ -2329,24 +2329,26 @@
 rce);%0A      
+  
 LOG.info(%22Su
@@ -2381,24 +2381,205 @@
 a Client%22);%0A
+      %7Dcatch (Exception e)%7B%0A        creatingResource.set(false);%0A      %7D%0A    %7D%0A    if(clientAttempts %3E=3)%7B%0A      //rest%0A      clientAttempts = 0;%0A      creatingResource.set(false);%0A
     %7D%0A    re

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