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