package com.hazelcast.simulator.provisioner;

import com.hazelcast.simulator.common.SimulatorProperties;
import com.hazelcast.simulator.utils.CommandLineExitException;
import org.apache.log4j.Logger;
import org.jclouds.compute.ComputeService;
import org.jclouds.compute.domain.Hardware;
import org.jclouds.compute.domain.Image;
import org.jclouds.domain.Location;

import java.util.Set;

import static com.hazelcast.simulator.utils.CommonUtils.exitWithError;
import static com.hazelcast.simulator.utils.CommonUtils.getSimulatorVersion;
import static com.hazelcast.simulator.utils.FileUtils.getSimulatorHome;
import static java.lang.String.format;

/**
 * Commandline tool to retrieve various cloud info.
 */
public class CloudInfo {

    private static final Logger LOGGER = Logger.getLogger(CloudInfo.class);

    SimulatorProperties props = new SimulatorProperties();

    String locationId;
    boolean verbose;

    private ComputeService computeService;

    void init() {
        computeService = new ComputeServiceBuilder(props).build();
    }

    // show all support clouds
    void showLocations() {
        Set<? extends Location> locations = computeService.listAssignableLocations();
        for (Location location : locations) {
            System.out.println(location);
        }
    }

    void showHardware() {
        Set<? extends Hardware> hardwareSet = computeService.listHardwareProfiles();
        for (Hardware hardware : hardwareSet) {
            if (verbose) {
                System.out.println(hardware);
            } else {
                StringBuilder sb = new StringBuilder(hardware.getId());
                sb.append(" Ram: ").append(hardware.getRam());
                sb.append(" Processors: ").append(hardware.getProcessors());
                if (locationId == null) {
                    Location location = hardware.getLocation();
                    if (location != null) {
                        sb.append(" Location: ").append(location.getId());
                    }
                }
                System.out.println(sb.toString());
            }
        }
    }

    void showImages() {
        Set<? extends Image> images = computeService.listImages();
        for (Image image : images) {
            boolean match = show(image);
            if (!match) {
                continue;
            }

            if (verbose) {
                System.out.println(image);
            } else {
                System.out.println(image.getId() + " OS: " + image.getOperatingSystem() + " Version: " + image.getVersion());
            }
        }
    }

    private boolean show(Image image) {
        if (locationId == null) {
            return true;
        }

        Location imageLocation = image.getLocation();
        if (imageLocation == null) {
            return true;
        }

        String imageLocationId = imageLocation.getId();
        if (imageLocationId == null) {
            return true;
        }

        return locationId.equals(imageLocationId);
    }

    public static void main(String[] args) {
        LOGGER.info("Hazelcast Simulator CloudInfo");
        LOGGER.info(format("Version: %s", getSimulatorVersion()));
        LOGGER.info(format("SIMULATOR_HOME: %s", getSimulatorHome()));

        try {
            CloudInfo cloudInfoCli = new CloudInfo();
            CloudInfoCli cli = new CloudInfoCli(cloudInfoCli, args);

            cli.run();
        } catch (Exception e) {
            if (!(e instanceof CommandLineExitException)) {
                LOGGER.fatal(e);
            }
            exitWithError(LOGGER, e.getMessage());
        }
    }
}

@@ -1247,34 +1247,27 @@
         
-System.out.println
+LOGGER.info
 (locatio
@@ -1489,34 +1489,27 @@
         
-System.out.println
+LOGGER.info
 (hardwar
@@ -1524,32 +1524,37 @@
         
-%7D else %7B
+    continue;
 %0A       
         
@@ -1537,32 +1537,39 @@
 ontinue;%0A       
+     %7D%0A
          StringB
@@ -1557,24 +1557,27 @@
  %7D%0A         
+   
 StringBuilde
@@ -1624,36 +1624,32 @@
 ));%0A            
-    
 sb.append(%22 Ram:
@@ -1683,36 +1683,32 @@
 ));%0A            
- 
-   
 sb.append(%22 Proc
@@ -1748,28 +1748,24 @@
 cessors());%0A
-    
             
@@ -1802,28 +1802,24 @@
             
-    
 Location loc
@@ -1846,28 +1846,24 @@
 Location();%0A
-    
             
@@ -1902,36 +1902,32 @@
                 
-    
 sb.append(%22 Loca
@@ -1953,36 +1953,32 @@
 ation.getId());%0A
-    
                 
@@ -1983,36 +1983,32 @@
   %7D%0A            
-    
 %7D%0A              
@@ -2005,38 +2005,27 @@
         
-    System.out.println
+LOGGER.info
 (sb.toSt
@@ -2029,38 +2029,24 @@
 oString());%0A
-            %7D%0A
         %7D%0A  
@@ -2277,33 +2277,32 @@
 ;%0A            %7D%0A
-%0A
             if (
@@ -2328,34 +2328,27 @@
         
-System.out.println
+LOGGER.info
 (image);
@@ -2364,51 +2364,59 @@
     
-%7D else %7B%0A                System.out.println
+    continue;%0A            %7D%0A            LOGGER.info
 (ima
@@ -2503,30 +2503,16 @@
 ion());%0A
-            %7D%0A
         

Use logger in CloudInfo.