1 | package schedframe.resources.computing; |
---|
2 | |
---|
3 | import gridsim.GridSimTags; |
---|
4 | import gridsim.dcworms.DCWormsTags; |
---|
5 | |
---|
6 | import java.util.ArrayList; |
---|
7 | import java.util.LinkedList; |
---|
8 | import java.util.List; |
---|
9 | import java.util.Properties; |
---|
10 | |
---|
11 | import schedframe.Initializable; |
---|
12 | import schedframe.events.Event; |
---|
13 | import schedframe.events.EventHandler; |
---|
14 | import schedframe.events.ResourceEventCommand; |
---|
15 | import schedframe.events.scheduling.EventReason; |
---|
16 | import schedframe.events.scheduling.SchedulingEvent; |
---|
17 | import schedframe.events.scheduling.SchedulingEventCommand; |
---|
18 | import schedframe.exceptions.ResourceException; |
---|
19 | import schedframe.resources.Resource; |
---|
20 | import schedframe.resources.ResourceStatus; |
---|
21 | import schedframe.resources.ResourceType; |
---|
22 | import schedframe.resources.computing.description.ComputingResourceDescription; |
---|
23 | import schedframe.resources.computing.extensions.Extension; |
---|
24 | import schedframe.resources.computing.extensions.ExtensionList; |
---|
25 | import schedframe.resources.computing.extensions.ExtensionListImpl; |
---|
26 | import schedframe.resources.computing.extensions.ExtensionType; |
---|
27 | import schedframe.resources.computing.profiles.energy.EnergyEvent; |
---|
28 | import schedframe.resources.computing.profiles.energy.EnergyEventType; |
---|
29 | import schedframe.resources.computing.profiles.energy.EnergyExtension; |
---|
30 | import schedframe.resources.computing.profiles.energy.airthroughput.ui.AirThroughputInterface; |
---|
31 | import schedframe.resources.computing.profiles.energy.power.ui.PowerInterface; |
---|
32 | import schedframe.resources.computing.properties.DefaultPropertiesBuilder; |
---|
33 | import schedframe.resources.computing.properties.PropertiesDirector; |
---|
34 | import schedframe.resources.computing.validator.ResourceNameValidator; |
---|
35 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
36 | import schedframe.resources.computing.validator.ResourceStatusValidator; |
---|
37 | import schedframe.resources.computing.validator.ResourceTypeValidator; |
---|
38 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
39 | import schedframe.resources.units.PEUnit; |
---|
40 | import schedframe.resources.units.ResourceUnit; |
---|
41 | import schedframe.resources.units.StandardResourceUnitName; |
---|
42 | import schedframe.scheduling.Scheduler; |
---|
43 | |
---|
44 | public class ComputingResource implements Resource, Initializable{ |
---|
45 | |
---|
46 | protected String name; |
---|
47 | protected ResourceType type; |
---|
48 | protected String category; |
---|
49 | protected ResourceStatus status; |
---|
50 | |
---|
51 | protected ComputingResource parent; |
---|
52 | protected List<ComputingResource> children; |
---|
53 | |
---|
54 | protected Scheduler scheduler; |
---|
55 | protected ResourceCharacteristics resourceCharacteristic; |
---|
56 | protected ExtensionList extensionList; |
---|
57 | |
---|
58 | |
---|
59 | public ExtensionList getExtensionList() { |
---|
60 | return extensionList; |
---|
61 | } |
---|
62 | |
---|
63 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
64 | this.type = resDesc.getType(); |
---|
65 | this.name = resDesc.getId(); |
---|
66 | this.category = resDesc.getCategory(); |
---|
67 | this.status = ResourceStatus.FREE; |
---|
68 | this.extensionList = new ExtensionListImpl(1); |
---|
69 | initCharacteristics(resDesc); |
---|
70 | accept(new EnergyExtension(this, resDesc.getPowerProfile(), resDesc.getAirThroughputProfile())); |
---|
71 | addFakeProcessors(); |
---|
72 | } |
---|
73 | |
---|
74 | //TODO remove if possible (check if all scenarios can be realized - statistics issue), since it's a temporary method |
---|
75 | private void addFakeProcessors() { |
---|
76 | if(getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE) != null){ |
---|
77 | for(ResourceUnit resUnit: getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE)){ |
---|
78 | PEUnit peUnit = (PEUnit) resUnit; |
---|
79 | for(int i = 0; i < peUnit.getAmount(); i++){ |
---|
80 | schemas.ComputingResource fakeCompResource = new schemas.ComputingResource(); |
---|
81 | fakeCompResource.setClazz("Processor"); |
---|
82 | addChild(ResourceFactory.createResource(new ComputingResourceDescription(fakeCompResource))); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
89 | resourceCharacteristic = new ResourceCharacteristics.Builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).build(); |
---|
90 | } |
---|
91 | |
---|
92 | public ComputingResource getParent() { |
---|
93 | return parent; |
---|
94 | } |
---|
95 | |
---|
96 | public void setParent(ComputingResource newParent) { |
---|
97 | parent = newParent; |
---|
98 | } |
---|
99 | |
---|
100 | public List<ComputingResource> getChildren() { |
---|
101 | if (children == null) |
---|
102 | return new ArrayList<ComputingResource>(1); |
---|
103 | return children; |
---|
104 | } |
---|
105 | |
---|
106 | public void addChild(ComputingResource child) { |
---|
107 | child.setParent(this); |
---|
108 | if (children == null) |
---|
109 | children = new ArrayList<ComputingResource>(); |
---|
110 | children.add(child); |
---|
111 | } |
---|
112 | |
---|
113 | public String getName() { |
---|
114 | return name; |
---|
115 | } |
---|
116 | |
---|
117 | public ResourceType getType() { |
---|
118 | return type; |
---|
119 | } |
---|
120 | |
---|
121 | public ResourceCharacteristics getResourceCharacteristic() { |
---|
122 | return resourceCharacteristic; |
---|
123 | } |
---|
124 | |
---|
125 | public ResourceStatus getStatus() { |
---|
126 | return status; |
---|
127 | } |
---|
128 | |
---|
129 | public void setStatus(ResourceStatus newStatus) { |
---|
130 | if(newStatus != status){ |
---|
131 | status = newStatus; |
---|
132 | if(children != null) { |
---|
133 | for(ComputingResource child: children){ |
---|
134 | child.setStatus(status); |
---|
135 | } |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | private void triggerEventUp(Event event) { |
---|
141 | if(parent != null) |
---|
142 | parent.handleEvent(event); |
---|
143 | } |
---|
144 | |
---|
145 | public void handleEvent(Event event){ |
---|
146 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
147 | rec.execute(event); |
---|
148 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
149 | sec.execute(event); |
---|
150 | |
---|
151 | //old, correctly working method |
---|
152 | /*if (extensionList != null) { |
---|
153 | for (Extension extension : extensionList) { |
---|
154 | if (extension.supportsEvent(event)) { |
---|
155 | extension.handleEvent(event); |
---|
156 | } |
---|
157 | } |
---|
158 | }*/ |
---|
159 | //TODO - delete, check before |
---|
160 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
161 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
162 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
163 | //} |
---|
164 | //triggerEventUp(event); |
---|
165 | } |
---|
166 | |
---|
167 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
168 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
169 | validators.add(new ResourceTypeValidator(type)); |
---|
170 | return searchDescendants(validators, true); |
---|
171 | } |
---|
172 | |
---|
173 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
174 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
175 | validators.add(new ResourceStatusValidator(status)); |
---|
176 | validators.add(new ResourceTypeValidator(type)); |
---|
177 | return searchDescendants(validators, true); |
---|
178 | } |
---|
179 | |
---|
180 | public ComputingResource getDescendantByName(String resourceName) throws ResourceException { |
---|
181 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
182 | validators.add(new ResourceNameValidator(resourceName)); |
---|
183 | List<? extends ComputingResource> resources = searchDescendants(validators, true); |
---|
184 | return resources.size() == 0 ? null : resources.get(0); |
---|
185 | } |
---|
186 | |
---|
187 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
188 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
189 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
190 | return searchDescendants(validators, false); |
---|
191 | } |
---|
192 | |
---|
193 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutOff) { |
---|
194 | |
---|
195 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
196 | if (children != null) { |
---|
197 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
198 | toExamine.push(this); |
---|
199 | |
---|
200 | while (!toExamine.isEmpty()) { |
---|
201 | ComputingResource resource = toExamine.pop(); |
---|
202 | List<ComputingResource> resources = resource.getChildren(); |
---|
203 | int numberOfRes = resources.size(); |
---|
204 | for (int i = 0; i < numberOfRes; i++) { |
---|
205 | ComputingResource resourceChild = resources.get(i); |
---|
206 | if (resourceChild.match(validators)) { |
---|
207 | descendants.add(resourceChild); |
---|
208 | if(cutOff == false) { |
---|
209 | toExamine.addLast(resourceChild); |
---|
210 | } |
---|
211 | } else |
---|
212 | //toExamine.insertElementAt(resourceChild, 0); |
---|
213 | toExamine.addLast(resourceChild); |
---|
214 | } |
---|
215 | } |
---|
216 | } |
---|
217 | return descendants; |
---|
218 | } |
---|
219 | |
---|
220 | protected boolean match(List<ResourceValidator> validators){ |
---|
221 | for(ResourceValidator validator: validators){ |
---|
222 | if(validator.validate(this) == false) |
---|
223 | return false; |
---|
224 | } |
---|
225 | return true; |
---|
226 | } |
---|
227 | |
---|
228 | public Properties getProperties(){ |
---|
229 | PropertiesDirector propDirector = new PropertiesDirector(); |
---|
230 | propDirector.setPropertiesBuilder(new DefaultPropertiesBuilder()); |
---|
231 | propDirector.constructProperties(this); |
---|
232 | return propDirector.getProperties(); |
---|
233 | } |
---|
234 | |
---|
235 | public String getCategory(){ |
---|
236 | return category; |
---|
237 | } |
---|
238 | |
---|
239 | public PowerInterface getPowerInterface(){ |
---|
240 | Extension extension = getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
241 | if(extension != null){ |
---|
242 | EnergyExtension ee = (EnergyExtension)extension; |
---|
243 | return ee.getPowerInterface(); |
---|
244 | } |
---|
245 | return null; |
---|
246 | } |
---|
247 | |
---|
248 | public AirThroughputInterface getAirThroughputInterface(){ |
---|
249 | Extension extension = getExtension(ExtensionType.ENERGY_EXTENSION); |
---|
250 | if(extension != null){ |
---|
251 | EnergyExtension ee = (EnergyExtension)extension; |
---|
252 | return ee.getAirThroughputInterface(); |
---|
253 | } |
---|
254 | return null; |
---|
255 | } |
---|
256 | |
---|
257 | private Extension getExtension(ExtensionType type){ |
---|
258 | if (extensionList != null) { |
---|
259 | for (Extension extension : extensionList) { |
---|
260 | if (extension.getType() == type) { |
---|
261 | return extension; |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | return null; |
---|
266 | } |
---|
267 | |
---|
268 | public Scheduler getScheduler() { |
---|
269 | return scheduler; |
---|
270 | } |
---|
271 | |
---|
272 | public void setScheduler(Scheduler scheduler) { |
---|
273 | this.scheduler = scheduler; |
---|
274 | if(children != null){ |
---|
275 | for(ComputingResource child: children){ |
---|
276 | child.setScheduler(scheduler); |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | class ComputingResourceEventHandler implements EventHandler{ |
---|
282 | |
---|
283 | public void handleResourceEvent(Event event){ |
---|
284 | if (extensionList != null) { |
---|
285 | for (Extension extension : extensionList) { |
---|
286 | if (extension.supportsEvent(event)) { |
---|
287 | extension.handleEvent(event); |
---|
288 | } |
---|
289 | } |
---|
290 | } |
---|
291 | if(event.getReason() != EventReason.SIM_INIT) |
---|
292 | triggerEventUp(event); |
---|
293 | } |
---|
294 | |
---|
295 | public void handleSchedulingEvent(SchedulingEvent event){ |
---|
296 | |
---|
297 | if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
298 | String src = event.getSource() != null ? event.getSource() : name; |
---|
299 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
300 | } else if(parent != null) |
---|
301 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | public EventHandler getEventHandler(){ |
---|
306 | return new ComputingResourceEventHandler(); |
---|
307 | } |
---|
308 | |
---|
309 | public void initiate(){ |
---|
310 | |
---|
311 | /*ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
312 | EnergyEvent event = new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller"); |
---|
313 | event.setReason(EventReason.SIM_INIT); |
---|
314 | rec.execute(event);*/ |
---|
315 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
316 | EnergyEvent event = new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
317 | event.setReason(EventReason.SIM_INIT); |
---|
318 | rec.execute(event); |
---|
319 | |
---|
320 | //alternative way |
---|
321 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.AIRFLOW_STATE_CHANGED, "Resource controller")); |
---|
322 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
323 | } |
---|
324 | |
---|
325 | |
---|
326 | private void accept(EnergyExtension e){ |
---|
327 | extensionList.add(e); |
---|
328 | } |
---|
329 | } |
---|