package cloudsimulator.clustersimulator;

import cloudsimulator.clustersimulator.dto.SimulatedCluster;

import java.util.Random;

public class SimulateFailures {

    private static final int NUMBER_OF_DAYS_FOR_SLA = 30;
    private static final int NUMBER_OF_SECONDS_IN_DAY = 86400;          // 24 hours * 60 minutes * 60 seconds

    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;
        long secondsOffline;

        seconds = NUMBER_OF_DAYS_FOR_SLA * NUMBER_OF_SECONDS_IN_DAY;
        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;
    }

}

@@ -123,16 +123,122 @@
 andom;%0A%0A
+/**%0A * Simulate failures for VMs. The lower is up time in SLA the higher the chance for a Vm to fail.%0A */%0A
 public c
@@ -431,16 +431,227 @@
 econds%0A%0A
+    /**%0A     * Simulate failures for all the machines in the cluster.%0A     *%0A     * @param simulatedCluster Cluster for which failures will be simulated.%0A     * @return Cluster after failure simulation.%0A     */%0A
     publ
@@ -1243,16 +1243,220 @@
 %0A    %7D%0A%0A
+    /**%0A     * Test a particular VM in the cluster for failure.%0A     *%0A     * @param sla specifying the uptime for a VM.%0A     * @return boolean value indicating the the machine has failed or not.%0A     */%0A
     priv

Add comments.