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());
84 public boolean enoughResources(Application app) {
85 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
88 public int availableSize() {
89 return reservedSize - getSize();
92 public int availableRAM() {
93 return reservedRAM - getRAM();
96 public int availableCPU() {
97 return reservedCPU - getCPU();
104 public PhysicalMachine getRunningOn() {
108 public int getPositionOnPM() {
112 public ArrayList<Application> getApplications() {
113 return new ArrayList<Application>(applications.values());
116 public int getSize() {
117 int usedSize = initialSize;
118 for (Application a : applications.values())
119 usedSize += a.getSize();
123 public int getRAM() {
124 int usedRAM = initialRAM;
125 for (Application a : applications.values())
126 usedRAM += a.getRam();
130 public int getCPU() {
131 int usedCPU = initialCPU;
132 for (Application a : applications.values())
133 usedCPU += a.getCpu();
137 public int getReservedSize() {
141 public int getReservedRAM() {
145 public int getReservedCPU() {
150 public String toString() {
151 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
152 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";