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 diffSize = (app.getSize() - availableSize());
81 int diffRAM = (app.getRam() - availableRAM());
82 int diffCPU = (app.getCpu() - availableCPU());
84 if(!(type == VMType.Resizable) || !runningOn.checkExtendVM(diffSize, diffRAM, diffCPU)) {
85 throw new VMResizeException("Could not resize VM!", this);
88 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
90 reservedSize = reservedSize + diffSize;
91 reservedRAM = reservedRAM + diffRAM;
92 reservedCPU = reservedCPU + diffCPU;
96 public boolean resizeDown(Application app) {
97 runningOn.resizeDown(app);
99 reservedSize = reservedSize - app.getSize();
100 reservedRAM = reservedRAM - app.getRam();
101 reservedCPU = reservedCPU - app.getCpu();
105 public boolean stopApplication(Application app) {
106 if (applications.containsKey(app.getID())) {
107 applications.remove(app.getID());
113 public boolean enoughResources(Application app) {
114 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
117 public int availableSize() {
118 return reservedSize - getSize();
121 public int availableRAM() {
122 return reservedRAM - getRAM();
125 public int availableCPU() {
126 return reservedCPU - getCPU();
133 public PhysicalMachine getRunningOn() {
137 public int getPositionOnPM() {
141 public ArrayList<Application> getApplications() {
142 return new ArrayList<Application>(applications.values());
145 public int getSize() {
146 int usedSize = initialSize;
147 for (Application a : applications.values())
148 usedSize += a.getSize();
152 public int getRAM() {
153 int usedRAM = initialRAM;
154 for (Application a : applications.values())
155 usedRAM += a.getRam();
159 public int getCPU() {
160 int usedCPU = initialCPU;
161 for (Application a : applications.values())
162 usedCPU += a.getCpu();
166 public int getReservedSize() {
170 public int getReservedRAM() {
174 public int getReservedCPU() {
179 public String toString() {
180 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
181 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";