]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
fixed VM.resize()
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / types / VirtualMachine.java
1 package at.ac.tuwien.lsdc.types;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import at.ac.tuwien.lsdc.exception.VMResizeException;
7
8 public class VirtualMachine {
9
10         public enum VMType {
11                 Resizable, NonResizable
12         }
13
14         private static int count = 0;
15         private int posOnPM;
16
17         private PhysicalMachine runningOn;
18
19         private HashMap<Integer, Application> applications = new HashMap<Integer, Application>();
20
21         private int id;
22
23         public static final int initialSize = 100;
24         public static final int initialRAM = 50;
25         public static final int initialCPU = 150;
26
27         private int reservedSize;
28         private int reservedRAM;
29         private int reservedCPU;
30
31         private VMType type;
32
33         public VirtualMachine(int size, int RAM, int CPU, PhysicalMachine pm, VMType type) {
34                 id = count;
35                 count++;
36                 reservedSize = size + initialSize;
37                 reservedRAM = RAM + initialRAM;
38                 reservedCPU = CPU + initialCPU;
39                 runningOn = pm;
40                 posOnPM = pm.countCurrentlyRunningVMs();
41                 this.type = type;
42         }
43
44         public boolean startApplication(Application app) {
45                 if (enoughResources(app)) {
46                         applications.put(app.getID(), app);
47                         app.setRunningOn(this);
48                         return true;
49                 } else
50                         return false;
51         }
52
53         /**
54          * Set the VM size to the given new values.
55          * 
56          * @param newSize
57          *          the new size of the VM, excluding the initial consumption.
58          * @param newRAM
59          *          the new RAM of the VM, excluding the initial consumption.
60          * @param newCPU
61          *          the new CPU of the VM, excluding the initial consumption.
62          * @return
63          */
64         public boolean resizeVM(int newSize, int newRAM, int newCPU) {
65                 if (type == VMType.Resizable
66                                 && runningOn.checkVM(newSize - reservedSize, newRAM - reservedRAM, newCPU - reservedCPU)) {
67                         // Resize VM
68                         reservedSize = initialSize + newSize;
69                         reservedRAM = initialRAM + newRAM;
70                         reservedCPU = initialCPU + newCPU;
71                         return true;
72                 } else
73                         throw new VMResizeException("Could not resize VM!", this);
74         }
75
76         public boolean stopApplication(Application app) {
77                 if (applications.containsKey(app.getID())) {
78                         applications.remove(app.getID());
79                         app.setRunningOn(null);
80                         return true;
81                 } else
82                         return false;
83         }
84
85         private boolean enoughResources(Application app) {
86                 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
87         }
88
89         private int availableSize() {
90                 return reservedSize - getSize();
91         }
92
93         private int availableRAM() {
94                 return reservedRAM - getRAM();
95         }
96
97         private int availableCPU() {
98                 return reservedCPU - getCPU();
99         }
100
101         public int getId() {
102                 return id;
103         }
104
105         public PhysicalMachine getRunningOn() {
106                 return runningOn;
107         }
108
109         public int getPositionOnPM() {
110                 return posOnPM;
111         }
112
113         public ArrayList<Application> getApplications() {
114                 return new ArrayList<Application>(applications.values());
115         }
116
117         public int getSize() {
118                 int usedSize = initialSize;
119                 for (Application a : applications.values())
120                         usedSize += a.getSize();
121                 return usedSize;
122         }
123
124         public int getRAM() {
125                 int usedRAM = initialRAM;
126                 for (Application a : applications.values())
127                         usedRAM += a.getRam();
128                 return usedRAM;
129         }
130
131         public int getCPU() {
132                 int usedCPU = initialCPU;
133                 for (Application a : applications.values())
134                         usedCPU += a.getCpu();
135                 return usedCPU;
136         }
137
138         public int getReservedSize() {
139                 return reservedSize;
140         }
141
142         public int getReservedRAM() {
143                 return reservedRAM;
144         }
145
146         public int getReservedCPU() {
147                 return reservedCPU;
148         }
149
150         @Override
151         public String toString() {
152                 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
153                                 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";
154         }
155
156 }