]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/management/MachineManager.java
readded maven for QA purpose AND easier integration with Jenkins.
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / management / MachineManager.java
1 package at.ac.tuwien.lsdc.management;
2
3 import java.util.Collection;
4 import java.util.HashMap;
5
6 import at.ac.tuwien.lsdc.exception.OutOfPMsException;
7 import at.ac.tuwien.lsdc.exception.VMsRunningException;
8 import at.ac.tuwien.lsdc.types.PhysicalMachine;
9 import at.ac.tuwien.lsdc.util.NumberUtils;
10
11 /**
12  * This class is responsible to start and stop PMs & VMs also it will be used to
13  * put an application on a VM move an application and get utilization data
14  * 
15  */
16 public class MachineManager {
17
18         private HashMap<Integer, PhysicalMachine> PMs = new HashMap<Integer, PhysicalMachine>();
19
20         private int numPMs;
21
22         private int totalPMs = 0;
23         private int totalVMs = 0;
24
25         public MachineManager(int numPMs) {
26                 this.numPMs = numPMs;
27         }
28
29         /**
30          * Start a physical machine
31          * 
32          * @return the PM that was started, null if all machines already running
33          */
34         public PhysicalMachine startPhysicalMachine() {
35                 if (PMs.size() >= numPMs)
36                         throw new OutOfPMsException("We cannot start another PM. All out PMs are already running!: " + numPMs);
37
38                 PhysicalMachine pm = new PhysicalMachine();
39                 PMs.put(pm.getId(), pm);
40                 pm.start();
41                 totalPMs++;
42                 return pm;
43         }
44
45         /**
46          * Stops a physical machine with the given id
47          * 
48          * @param id
49          *          the id of the PM to stop
50          * @throws VMsRunningException
51          *           is thrown when there are still VMs running on this PM
52          */
53         public void stopPhysicalMachine(int id) throws VMsRunningException {
54                 if (PMs.containsKey(id)) {
55                         PhysicalMachine pm = PMs.get(id);
56                         totalVMs += pm.getTotalVMs();
57                         pm.stop();
58                         PMs.remove(id);
59                 }
60         }
61
62         /**
63          * Returns all running physical machines
64          * 
65          * @return the currently active PMs
66          */
67         public Collection<PhysicalMachine> getPMs() {
68                 return PMs.values();
69         }
70
71         /**
72          * Returns the maximum number of available physical machines
73          * 
74          * @return the maximum number of PMs
75          */
76         public int getMaxPMs() {
77                 return numPMs;
78         }
79
80         /**
81          * Get the total size on all PMs
82          * 
83          * @return the total size on all PMs
84          */
85         public int getTotalSize() {
86                 int totalSize = 0;
87                 for (PhysicalMachine pm : PMs.values()) {
88                         if (pm.isRunning())
89                                 totalSize += pm.getCurrentSize();
90                 }
91                 return totalSize;
92         }
93
94         /**
95          * Get the total Ram usage of all PMs
96          * 
97          * @return the total usage of Ram
98          */
99         public int getTotalRam() {
100                 int totalRam = 0;
101                 for (PhysicalMachine pm : PMs.values()) {
102                         if (pm.isRunning())
103                                 totalRam += pm.getCurrentRam();
104                 }
105                 return totalRam;
106         }
107
108         /**
109          * Get the total Cpu usage of all PMs
110          * 
111          * @return the total usage of Cpu power
112          */
113         public int getTotalCpu() {
114                 int totalCpu = 0;
115                 for (PhysicalMachine pm : PMs.values()) {
116                         if (pm.isRunning())
117                                 totalCpu += pm.getCurrentCPU();
118                 }
119                 return totalCpu;
120         }
121
122         /**
123          * Gets the total power consumption summed up from each PM
124          * 
125          * @return the total power consumption
126          */
127         public double getCurrentConsumption() {
128                 double consumption = 0;
129                 for (PhysicalMachine pm : PMs.values()) {
130                         if (pm.isRunning())
131                                 consumption += pm.getConsumption();
132                 }
133                 consumption = NumberUtils.roundDouble(consumption);
134                 return consumption;
135         }
136
137         /**
138          * Gets the number of currently running PMs
139          * 
140          * @return the number of currently running PMs
141          */
142         public int countCurrentlyRunningPMs() {
143                 int running = 0;
144                 for (PhysicalMachine pm : PMs.values()) {
145                         if (pm.isRunning())
146                                 running++;
147                 }
148                 return running;
149         }
150
151         /**
152          * Gets the number of currently running VMs
153          * 
154          * @return the number of currently running VMs
155          */
156         public int countCurrentlyRunningVMs() {
157                 int running = 0;
158                 for (PhysicalMachine pm : PMs.values()) {
159                         if (pm.isRunning()) {
160                                 running += pm.countCurrentlyRunningVMs();
161                         }
162                 }
163                 return running;
164         }
165
166         public int getTotalPMs() {
167                 return totalPMs;
168         }
169
170         public int getTotalVMs() {
171                 return totalVMs;
172         }
173 }