]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/types/VirtualMachine.java
added Migration to SchedB
[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                         return true;
80                 } else
81                         return false;
82         }
83
84         public boolean enoughResources(Application app) {
85                 return (app.getSize() <= availableSize()) && (app.getRam() <= availableRAM()) && (app.getCpu() <= availableCPU());
86         }
87
88         public int availableSize() {
89                 return reservedSize - getSize();
90         }
91
92         public int availableRAM() {
93                 return reservedRAM - getRAM();
94         }
95
96         public int availableCPU() {
97                 return reservedCPU - getCPU();
98         }
99
100         public int getId() {
101                 return id;
102         }
103
104         public PhysicalMachine getRunningOn() {
105                 return runningOn;
106         }
107
108         public int getPositionOnPM() {
109                 return posOnPM;
110         }
111
112         public ArrayList<Application> getApplications() {
113                 return new ArrayList<Application>(applications.values());
114         }
115
116         public int getSize() {
117                 int usedSize = initialSize;
118                 for (Application a : applications.values())
119                         usedSize += a.getSize();
120                 return usedSize;
121         }
122
123         public int getRAM() {
124                 int usedRAM = initialRAM;
125                 for (Application a : applications.values())
126                         usedRAM += a.getRam();
127                 return usedRAM;
128         }
129
130         public int getCPU() {
131                 int usedCPU = initialCPU;
132                 for (Application a : applications.values())
133                         usedCPU += a.getCpu();
134                 return usedCPU;
135         }
136
137         public int getReservedSize() {
138                 return reservedSize;
139         }
140
141         public int getReservedRAM() {
142                 return reservedRAM;
143         }
144
145         public int getReservedCPU() {
146                 return reservedCPU;
147         }
148
149         @Override
150         public String toString() {
151                 return "VirtualMachine [posOnPM=" + posOnPM + ", runningOn=" + runningOn + ", id=" + id + ", reservedSize="
152                                 + reservedSize + ", reservedRAM=" + reservedRAM + ", reservedCPU=" + reservedCPU + ", type=" + type + "]";
153         }
154
155 }