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.checkExtendVM(newSize - reservedSize, newRAM - reservedRAM, newCPU
69 reservedSize = initialSize + newSize;
70 reservedRAM = initialRAM + newRAM;
71 reservedCPU = initialCPU + newCPU;
75 // throw new VMResizeException("Could not resize VM!", this);
78 public boolean resizeUp(Application app) throws VMResizeException {
80 int newSize = reservedSize + (app.getSize() - availableSize()) - initialSize;
81 int newRAM = reservedRAM + (app.getRam() - availableRAM()) - initialRAM;
82 int newCPU = reservedCPU + (app.getCpu() - availableCPU()) - initialCPU;
84 int diffSize = newSize + initialSize - reservedSize;
85 int diffRAM = newRAM + initialRAM - reservedRAM;
86 int diffCPU = newCPU + initialCPU - reservedCPU;
88 if(!(type == VMType.Resizable) || !runningOn.checkVM(newSize, newRAM, newCPU)) {
89 throw new VMResizeException("Could not resize VM!", this);
92 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
94 reservedSize = newSize + initialSize;
95 reservedRAM = newRAM + initialRAM;
96 reservedCPU = newCPU + initialCPU;
100 public boolean resizeDown(Application app) {
101 reservedSize = reservedSize - app.getSize();
102 reservedRAM = reservedRAM - app.getRam();
103 reservedCPU = reservedCPU - app.getCpu();
107 public boolean stopApplication(Application app) {
108 if (applications.containsKey(app.getID())) {
109 applications.remove(app.getID());
115 public boolean enoughResources(Application app) {
116 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
119 public int availableSize() {
120 return reservedSize - getSize();
123 public int availableRAM() {
124 return reservedRAM - getRAM();
127 public int availableCPU() {
128 return reservedCPU - getCPU();
135 public PhysicalMachine getRunningOn() {
139 public int getPositionOnPM() {
143 public ArrayList<Application> getApplications() {
144 return new ArrayList<Application>(applications.values());
147 public int getSize() {
148 int usedSize = initialSize;
149 for (Application a : applications.values())
150 usedSize += a.getSize();
154 public int getRAM() {
155 int usedRAM = initialRAM;
156 for (Application a : applications.values())
157 usedRAM += a.getRam();
161 public int getCPU() {
162 int usedCPU = initialCPU;
163 for (Application a : applications.values())
164 usedCPU += a.getCpu();
168 public int getReservedSize() {
172 public int getReservedRAM() {
176 public int getReservedCPU() {
180 public int getDiffSize() {
185 public String toString() {
186 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
187 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";