1 package at.ac.tuwien.lsdc.types;
3 import java.util.ArrayList;
4 import java.util.HashMap;
6 import at.ac.tuwien.lsdc.exception.VMResizeException;
8 public class VirtualMachine {
11 Resizable, NonResizable
14 private static int count = 0;
17 private PhysicalMachine runningOn;
19 private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
23 public static final int initialSize = 100;
24 public static final int initialRAM = 50;
25 public static final int initialCPU = 150;
27 private int reservedSize;
28 private int reservedRAM;
29 private int reservedCPU;
33 public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
36 reservedSize = size + initialSize;
37 reservedRAM = RAM + initialRAM;
38 reservedCPU = CPU + initialCPU;
40 posOnPM = pm.countCurrentlyRunningVMs();
44 public boolean startApplication(Application app) {
45 if (enoughResources(app)) {
46 applications.put(app.getID(), app);
47 app.setRunningOn(this);
54 * Set the VM size to the given new values.
57 * the new size of the VM, excluding the initial consumption.
59 * the new RAM of the VM, excluding the initial consumption.
61 * the new CPU of the VM, excluding the initial consumption.
64 public boolean resizeVM(int newSize, int newRAM, int newCPU) {
65 if (type == VMType.Resizable
66 && runningOn.checkVM(newSize - reservedSize, newRAM - reservedRAM, newCPU - reservedCPU)) {
68 reservedSize = initialSize + newSize;
69 reservedRAM = initialRAM + newRAM;
70 reservedCPU = initialCPU + newCPU;
73 throw new VMResizeException("Could not resize VM!", this);
76 public boolean stopApplication(Application app) {
77 if (applications.containsKey(app.getID())) {
78 applications.remove(app.getID());
79 app.setRunningOn(null);
85 private boolean enoughResources(Application app) {
86 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
89 private int availableSize() {
90 return reservedSize - getSize();
93 private int availableRAM() {
94 return reservedRAM - getRAM();
97 private int availableCPU() {
98 return reservedCPU - getCPU();
105 public PhysicalMachine getRunningOn() {
109 public int getPositionOnPM() {
113 public ArrayList<Application> getApplications() {
114 return new ArrayList<Application>(applications.values());
117 public int getSize() {
118 int usedSize = initialSize;
119 for (Application a : applications.values())
120 usedSize += a.getSize();
124 public int getRAM() {
125 int usedRAM = initialRAM;
126 for (Application a : applications.values())
127 usedRAM += a.getRam();
131 public int getCPU() {
132 int usedCPU = initialCPU;
133 for (Application a : applications.values())
134 usedCPU += a.getCpu();
138 public int getReservedSize() {
142 public int getReservedRAM() {
146 public int getReservedCPU() {
151 public String toString() {
152 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
153 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";