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