Cloudslab / cloudsim

CloudSim: A Framework For Modeling And Simulation Of Cloud Computing Infrastructures And Services
http://www.cloudbus.org/cloudsim/
812 stars 491 forks source link

Refactor addMigratingInVm Method Using Strategy Pattern on CloudSimExample1 #177

Open dquishpe opened 1 month ago

dquishpe commented 1 month ago
public interface ResourceCheckStrategy {
    boolean check(Vm vm, Host host);
public class StorageCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (host.getStorage() < vm.getSize()) {
            Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
                    host.getId(), " failed by storage");
            return false;
        }
        return true;
    }
}

public class RamCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getRamProvisioner().allocateRamForVm(vm, vm.getCurrentRequestedRam())) {
            Log.printConcatLine("[VmScheduler.addMigratingInVm] Allocation of VM #", vm.getId(), " to Host #",
                    host.getId(), " failed by RAM");
            return false;
        }
        return true;
    }
}

public class BwCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getBwProvisioner().allocateBwForVm(vm, vm.getCurrentRequestedBw())) {
            Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
                    + host.getId() + " failed by BW");
            return false;
        }
        return true;
    }
}

public class MipsCheckStrategy implements ResourceCheckStrategy {
    @Override
    public boolean check(Vm vm, Host host) {
        if (!host.getVmScheduler().allocatePesForVm(vm, vm.getCurrentRequestedMips())) {
            Log.printLine("[VmScheduler.addMigratingInVm] Allocation of VM #" + vm.getId() + " to Host #"
                    + host.getId() + " failed by MIPS");
            return false;
        }
        return true;
    }
}

public class Host {
    private List<ResourceCheckStrategy> strategies;

    public Host() {
        strategies = new ArrayList<>();
        strategies.add(new StorageCheckStrategy());
        strategies.add(new RamCheckStrategy());
        strategies.add(new BwCheckStrategy());
        strategies.add(new MipsCheckStrategy());
    }

    public void addMigratingInVm(Vm vm) {
        vm.setInMigration(true);

        if (!getVmsMigratingIn().contains(vm)) {
            for (ResourceCheckStrategy strategy : strategies) {
                if (!strategy.check(vm, this)) {
                    System.exit(0);
                }
            }

            getVmScheduler().getVmsMigratingIn().add(vm.getUid());
            setStorage(getStorage() - vm.getSize());

            getVmsMigratingIn().add(vm);
            getVmList().add(vm);
            updateVmsProcessing(CloudSim.clock());
            vm.getHost().updateVmsProcessing(CloudSim.clock());
        }
    }

    // Other methods and attributes of the Host class
}

public class Vm {
    private boolean inMigration;
    private double size;
    private double currentRequestedRam;
    private double currentRequestedBw;
    private double currentRequestedMips;
    private Host host;
    private String uid;
    private int id;

    // Getters and setters

    public void setInMigration(boolean inMigration) {
        this.inMigration = inMigration;
    }

    public boolean isInMigration() {
        return inMigration;
    }

    public double getSize() {
        return size;
    }

    public double getCurrentRequestedRam() {
        return currentRequestedRam;
    }

    public double getCurrentRequestedBw() {
        return currentRequestedBw;
    }

    public double getCurrentRequestedMips() {
        return currentRequestedMips;
    }

    public Host getHost() {
        return host;
    }

    public String getUid() {
        return uid;
    }

    public int getId() {
        return id;
    }
}
}