package cloudsimulator.clustersimulator;

import cloudsimulator.clustersimulator.dto.SimulatedCluster;

import java.util.Random;

public class SimulateFailures {

    private static final int numberOfDaysForSla = 30;

    public SimulatedCluster simulateFailures(SimulatedCluster simulatedCluster) {

        simulatedCluster.getCluster().forEach(tcg -> {
            // Test each machine in the treatment category.
            for (int i = 0; i < tcg.getVmNumber(); i++) {
                if (testVmFarFailure(tcg.getSla())) {
                    tcg.setVmNumber(tcg.getVmNumber() - 1);
                }
            }
        });

        // Remove a treatment category if there are no VMs in it.
        simulatedCluster.getCluster().removeIf(tcg -> tcg.getVmNumber() == 0);

        return simulatedCluster;
    }

    private boolean testVmFarFailure(double sla) {
        int seconds;                                                                      // 24 hours * 60 minutes * 60 seconds;
        long secondsOffline;

        seconds = numberOfDaysForSla * 86400;
        secondsOffline = (long) (seconds *  (100 - sla)) / 100;

        Random random = new Random();
        int randomInt = random.nextInt(seconds);
        long numberOfRestart = secondsOffline / 60;

        if (randomInt <= (secondsOffline / 60)) {
            return true;
        }
        return false;
    }


    public static void main(String[] args) {
        SimulateFailures simulateFailures = new SimulateFailures();
        int failedVmCount = 0;

        for (int i = 0; i < 86400 * numberOfDaysForSla; i++) {
            boolean isVmFailed = simulateFailures.testVmFarFailure(80.00);
            //System.out.println("Is vm available: " + isVmFailed);
            if (isVmFailed) {
                failedVmCount++;
            }
        }
        System.out.println("Failed VMs: " + failedVmCount);
    }
}

@@ -189,32 +189,146 @@
 int 
-numberOfDaysForSla = 30;
+NUMBER_OF_DAYS_FOR_SLA = 30;%0A    private static final int NUMBER_OF_SECONDS_IN_DAY = 86400;          // 24 hours * 60 minutes * 60 seconds
 %0A%0A  
@@ -1001,191 +1001,106 @@
 nds;
-                                                                      // 24 hours * 60 minutes * 60 seconds;%0A        long secondsOffline;%0A%0A        seconds = numberOfDaysForSla * 86400
+%0A        long secondsOffline;%0A%0A        seconds = NUMBER_OF_DAYS_FOR_SLA * NUMBER_OF_SECONDS_IN_DAY
 ;%0A  
@@ -1420,511 +1420,6 @@
  %7D%0A%0A
-%0A    public static void main(String%5B%5D args) %7B%0A        SimulateFailures simulateFailures = new SimulateFailures();%0A        int failedVmCount = 0;%0A%0A        for (int i = 0; i %3C 86400 * numberOfDaysForSla; i++) %7B%0A            boolean isVmFailed = simulateFailures.testVmFarFailure(80.00);%0A            //System.out.println(%22Is vm available: %22 + isVmFailed);%0A            if (isVmFailed) %7B%0A                failedVmCount++;%0A            %7D%0A        %7D%0A        System.out.println(%22Failed VMs: %22 + failedVmCount);%0A    %7D%0A
 %7D%0A

Remove main method used for debugging in development.