package cloudsimulator.controllersimulator;

import cloudsimulator.clustersimulator.ClusterManager;
import org.springframework.stereotype.Component;

@Component
public class ClusterFormationController {

    private long time = 0;
    private long startTimeOfAllocation = -1, startTimeOfRemoval = -1;
    private ClusterManager clusterManager;
    private long numberOfVmToAllocate;
    private long numberOfVmToRemove;

    public void allocateVMs(long numberOfVmToAllocate, ClusterManager clusterManager) {
        startTimeOfAllocation = time;
        this.clusterManager = clusterManager;
        this.numberOfVmToAllocate = numberOfVmToAllocate;
    }


    public void removeVMs(long numberOfVmToRemove, ClusterManager clusterManager) {
        startTimeOfRemoval = time;
        this.clusterManager = clusterManager;
        this.numberOfVmToRemove = numberOfVmToRemove;
    }

    public void incrementTime() {
        time++;
        if (startTimeOfAllocation != -1 && time >=  startTimeOfAllocation + 40) {
            allocate();
            startTimeOfAllocation = -1;
        }
        if (startTimeOfRemoval != -1 && time >= startTimeOfRemoval + 15) {
            remove();
            startTimeOfRemoval = -1;
        }
    }

    private void allocate () {
        clusterManager.getCluster().getTgGroup().stream()
                .findFirst()
                .ifPresent(tgGroup -> {
                    tgGroup.setVmNumber(tgGroup.getVmNumber() + numberOfVmToAllocate);
                });
    }

    private void remove() {
        clusterManager.getCluster().getTgGroup().stream()
                .findFirst()
                .ifPresent(tgGroup -> {
                    tgGroup.setVmNumber(tgGroup.getVmNumber() - numberOfVmToRemove);
                });
    }
}

@@ -143,16 +143,71 @@
 onent;%0A%0A
+/**%0A * Take decisions regarding cluster formation.%0A */%0A
 @Compone
@@ -469,16 +469,188 @@
 emove;%0A%0A
+    /**%0A     * Mark VMs for allocation, but some time is required before they are booted up.%0A     *%0A     * @param numberOfVmToAllocate%0A     * @param clusterManager%0A     */%0A
     publ
@@ -878,17 +878,183 @@
 %0A    %7D%0A%0A
+    /**%0A     * Mark VMs for removal, but some time is required before they are shut down.%0A     *%0A     * @param numberOfVmToRemove%0A     * @param clusterManager%0A     */
 %0A
-
     publ
@@ -1267,24 +1267,104 @@
 ove;%0A    %7D%0A%0A
+    /**%0A     * Simulate passing of time in order to start or stop a VM.%0A     */%0A
     public v
@@ -1700,32 +1700,110 @@
        %7D%0A    %7D%0A%0A
+    /**%0A     * After a VM is started, it is allocated to the cluster.%0A     */%0A
     private void
@@ -2054,24 +2054,101 @@
  %7D);%0A    %7D%0A%0A
+    /**%0A     * After a VM is stoped, it is removed from the cluster.%0A     */%0A
     private 
@@ -2395,16 +2395,21 @@
     %7D);%0A
-
     %7D%0A
+    %0A
 %7D%0A

Add comments.