]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
Merge branch 'master' of ssh://dev.somenet.org:666/tu/lsdc
[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.checkExtendVM(newSize - reservedSize, newRAM - reservedRAM, newCPU
67                                                 - reservedCPU)) {
68                         // Resize VM
69                         reservedSize = initialSize + newSize;
70                         reservedRAM = initialRAM + newRAM;
71                         reservedCPU = initialCPU + newCPU;
72                         return true;
73                 } else 
74                         return false;
75 //                      throw new VMResizeException("Could not resize VM!", this);
76         }
77         
78         public boolean resizeUp(Application app) throws VMResizeException {
79                 
80                 int newSize = reservedSize + (app.getSize() - availableSize()) - initialSize;
81                 int newRAM = reservedRAM + (app.getRam() - availableRAM()) - initialRAM;
82                 int newCPU = reservedCPU + (app.getCpu() - availableCPU()) - initialCPU;
83                 
84                 int diffSize = newSize + initialSize - reservedSize;
85                 int diffRAM = newRAM + initialRAM - reservedRAM;
86                 int diffCPU = newCPU + initialCPU - reservedCPU;
87                 
88                 if(!(type == VMType.Resizable) || !runningOn.checkVM(newSize, newRAM, newCPU)) {                        
89                         throw new VMResizeException("Could not resize VM!", this);
90                 }
91                 
92                 runningOn.resizeUp(diffSize, diffRAM, diffCPU);
93                 
94                 reservedSize = newSize + initialSize;
95                 reservedRAM = newRAM + initialRAM;
96                 reservedCPU = newCPU + initialCPU;
97                 return true;
98         }
99         
100         public boolean resizeDown(Application app) {
101                 reservedSize = reservedSize - app.getSize();
102                 reservedRAM = reservedRAM - app.getRam();
103                 reservedCPU = reservedCPU - app.getCpu();
104                 return true;
105         }
106
107         public boolean stopApplication(Application app) {
108                 if (applications.containsKey(app.getID())) {
109                         applications.remove(app.getID());
110                         return true;
111                 } else
112                         return false;
113         }
114
115         public boolean enoughResources(Application app) {
116                 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
117         }
118
119         public int availableSize() {
120                 return reservedSize - getSize();
121         }
122
123         public int availableRAM() {
124                 return reservedRAM - getRAM();
125         }
126
127         public int availableCPU() {
128                 return reservedCPU - getCPU();
129         }
130
131         public int getId() {
132                 return id;
133         }
134
135         public PhysicalMachine getRunningOn() {
136                 return runningOn;
137         }
138
139         public int getPositionOnPM() {
140                 return posOnPM;
141         }
142
143         public ArrayList<Application> getApplications() {
144                 return new ArrayList<Application>(applications.values());
145         }
146
147         public int getSize() {
148                 int usedSize = initialSize;
149                 for (Application a : applications.values())
150                         usedSize += a.getSize();
151                 return usedSize;
152         }
153
154         public int getRAM() {
155                 int usedRAM = initialRAM;
156                 for (Application a : applications.values())
157                         usedRAM += a.getRam();
158                 return usedRAM;
159         }
160
161         public int getCPU() {
162                 int usedCPU = initialCPU;
163                 for (Application a : applications.values())
164                         usedCPU += a.getCpu();
165                 return usedCPU;
166         }
167
168         public int getReservedSize() {
169                 return reservedSize;
170         }
171
172         public int getReservedRAM() {
173                 return reservedRAM;
174         }
175
176         public int getReservedCPU() {
177                 return reservedCPU;
178         }
179         
180         public int getDiffSize() {
181                 
182         }
183
184         @Override
185         public String toString() {
186                 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
187                                 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";
188         }
189
190 }