[477] | 1 | package schedframe.resources.computing; |
---|
| 2 | |
---|
| 3 | import gridsim.GridSimTags; |
---|
[481] | 4 | import gridsim.gssim.DCWormsTags; |
---|
[477] | 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.power.ui.PowerInterface; |
---|
| 31 | import schedframe.resources.computing.properties.DefaultPropertiesBuilder; |
---|
| 32 | import schedframe.resources.computing.properties.PropertiesDirector; |
---|
| 33 | import schedframe.resources.computing.validator.ResourceNameValidator; |
---|
| 34 | import schedframe.resources.computing.validator.ResourcePropertiesValidator; |
---|
| 35 | import schedframe.resources.computing.validator.ResourceStatusValidator; |
---|
| 36 | import schedframe.resources.computing.validator.ResourceTypeValidator; |
---|
| 37 | import schedframe.resources.computing.validator.ResourceValidator; |
---|
| 38 | import schedframe.resources.units.PEUnit; |
---|
| 39 | import schedframe.resources.units.ResourceUnit; |
---|
| 40 | import schedframe.resources.units.StandardResourceUnitName; |
---|
| 41 | import schedframe.scheduling.Scheduler; |
---|
| 42 | |
---|
| 43 | public class ComputingResource implements Resource, Initializable{ |
---|
| 44 | |
---|
| 45 | protected String name; |
---|
| 46 | protected ResourceType type; |
---|
| 47 | protected ResourceStatus status; |
---|
| 48 | |
---|
| 49 | protected ComputingResource parent; |
---|
| 50 | protected List<ComputingResource> children; |
---|
| 51 | |
---|
| 52 | protected Scheduler scheduler; |
---|
| 53 | protected ResourceCharacteristics resourceCharacteristic; |
---|
| 54 | protected ExtensionList extensionList; |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | public ExtensionList getExtensionList() { |
---|
| 58 | return extensionList; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | public ComputingResource(ComputingResourceDescription resDesc) { |
---|
| 62 | this.type = resDesc.getType(); |
---|
| 63 | this.name = resDesc.getId(); |
---|
| 64 | this.status = ResourceStatus.FREE; |
---|
| 65 | this.extensionList = new ExtensionListImpl(1); |
---|
| 66 | initCharacteristics(resDesc); |
---|
| 67 | addFakeProcessors(); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | //TODO remove if possible (check if all scenarios can be realized - statistics issue) , since it's temporary method |
---|
| 71 | private void addFakeProcessors() { |
---|
| 72 | if(getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE) != null){ |
---|
| 73 | for(ResourceUnit resUnit: getResourceCharacteristic().getResourceUnits().get(StandardResourceUnitName.PE)){ |
---|
| 74 | PEUnit peUnit = (PEUnit) resUnit; |
---|
| 75 | for(int i = 0; i < peUnit.getAmount(); i++){ |
---|
| 76 | schemas.ComputingResource fakeCompResource = new schemas.ComputingResource(); |
---|
| 77 | fakeCompResource.setClazz("Processor"); |
---|
| 78 | addChild(ResourceFactory.createResource(new ComputingResourceDescription(fakeCompResource))); |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | protected void initCharacteristics(ComputingResourceDescription resDesc){ |
---|
| 85 | resourceCharacteristic = new ResourceCharacteristics.Builder().resourceUnits(resDesc.getResourceUnits()).location(resDesc.getLocation()).parameters(resDesc.getParameters()).build(); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public ComputingResource getParent() { |
---|
| 89 | return parent; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | public void setParent(ComputingResource newParent) { |
---|
| 93 | parent = newParent; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | public List<ComputingResource> getChildren() { |
---|
| 97 | if (children == null) |
---|
| 98 | return new ArrayList<ComputingResource>(1); |
---|
| 99 | return children; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | public void addChild(ComputingResource child) { |
---|
| 103 | child.setParent(this); |
---|
| 104 | if (children == null) |
---|
| 105 | children = new ArrayList<ComputingResource>(); |
---|
| 106 | children.add(child); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | public String getName() { |
---|
| 110 | return name; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | public ResourceType getType() { |
---|
| 114 | return type; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | public ResourceCharacteristics getResourceCharacteristic() { |
---|
| 118 | return resourceCharacteristic; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public ResourceStatus getStatus() { |
---|
| 122 | return status; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | public void setStatus(ResourceStatus newStatus) { |
---|
| 126 | if(newStatus != status){ |
---|
| 127 | status = newStatus; |
---|
| 128 | if(children != null) { |
---|
| 129 | for(ComputingResource child: children){ |
---|
| 130 | child.setStatus(status); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | private void triggerEventUp(Event event) { |
---|
| 137 | if(parent != null) |
---|
| 138 | parent.handleEvent(event); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | public void handleEvent(Event event){ |
---|
| 142 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
| 143 | rec.execute(event); |
---|
| 144 | SchedulingEventCommand sec = new SchedulingEventCommand(this); |
---|
| 145 | sec.execute(event); |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | //old, correctly working method |
---|
| 149 | /*if (extensionList != null) { |
---|
| 150 | for (Extension extension : extensionList) { |
---|
| 151 | if (extension.supportsEvent(event)) { |
---|
| 152 | extension.handleEvent(event); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | }*/ |
---|
| 156 | //TODO - delete, check before |
---|
| 157 | //if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
| 158 | // String src = event.getSource() != null ? event.getSource() : name; |
---|
| 159 | // scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, GssimTags.UPDATE, src); |
---|
| 160 | //} |
---|
| 161 | //triggerEventUp(event); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | public List <? extends ComputingResource> getDescendantsByType(ResourceType type) { |
---|
| 165 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
| 166 | validators.add(new ResourceTypeValidator(type)); |
---|
| 167 | return searchDescendants(validators, true); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | public List<? extends ComputingResource> getDescendantsByTypeAndStatus(ResourceType type, ResourceStatus status) { |
---|
| 171 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
| 172 | validators.add(new ResourceStatusValidator(status)); |
---|
| 173 | validators.add(new ResourceTypeValidator(type)); |
---|
| 174 | return searchDescendants(validators, true); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | public ComputingResource getDescendantByName(String resourceName) throws ResourceException { |
---|
| 178 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
| 179 | validators.add(new ResourceNameValidator(resourceName)); |
---|
| 180 | List<? extends ComputingResource> resources = searchDescendants(validators, true); |
---|
| 181 | return resources.size() == 0 ? null : resources.get(0); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | public List<? extends ComputingResource> filterDescendants(Properties properties) { |
---|
| 185 | List<ResourceValidator> validators = new ArrayList<ResourceValidator>(); |
---|
| 186 | validators.add(new ResourcePropertiesValidator(properties)); |
---|
| 187 | return searchDescendants(validators, false); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | protected List<? extends ComputingResource> searchDescendants(List<ResourceValidator> validators, boolean cutoff) { |
---|
| 191 | |
---|
| 192 | List<ComputingResource> descendants = new ArrayList<ComputingResource>(); |
---|
| 193 | if (children != null) { |
---|
| 194 | LinkedList<ComputingResource> toExamine = new LinkedList<ComputingResource>(); |
---|
| 195 | toExamine.push(this); |
---|
| 196 | |
---|
| 197 | while (!toExamine.isEmpty()) { |
---|
| 198 | ComputingResource resource = toExamine.pop(); |
---|
| 199 | List<ComputingResource> resources = resource.getChildren(); |
---|
| 200 | /*if (resources == null) |
---|
| 201 | continue;*/ |
---|
| 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 PowerInterface getPowerInterface(){ |
---|
| 235 | if (extensionList != null) { |
---|
| 236 | for (Extension extension : extensionList) { |
---|
| 237 | if (extension.getType() == ExtensionType.ENERGY_EXTENSION) { |
---|
| 238 | EnergyExtension ee = (EnergyExtension)extension; |
---|
| 239 | return ee.getPowerInterface(); |
---|
| 240 | } |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | return null; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | public Scheduler getScheduler() { |
---|
| 247 | return scheduler; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | public void setScheduler(Scheduler scheduler) { |
---|
| 251 | this.scheduler = scheduler; |
---|
| 252 | if(children != null){ |
---|
| 253 | for(ComputingResource child: children){ |
---|
| 254 | child.setScheduler(scheduler); |
---|
| 255 | } |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | class SimpleEventHandler implements EventHandler{ |
---|
| 260 | |
---|
| 261 | public void handleResourceEvent(Event event){ |
---|
| 262 | if (extensionList != null) { |
---|
| 263 | for (Extension extension : extensionList) { |
---|
| 264 | if (extension.supportsEvent(event)) { |
---|
| 265 | extension.handleEvent(event); |
---|
| 266 | } |
---|
| 267 | } |
---|
| 268 | } |
---|
| 269 | if(event.getReason() != EventReason.SIM_INIT) |
---|
| 270 | triggerEventUp(event); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | public void handleSchedulingEvent(SchedulingEvent event){ |
---|
| 274 | |
---|
| 275 | if(scheduler != null && (parent != null && scheduler != parent.getScheduler())/*scheduler.getResources().contains(this)*/){ |
---|
| 276 | String src = event.getSource() != null ? event.getSource() : name; |
---|
[481] | 277 | scheduler.sendInternal(GridSimTags.SCHEDULE_NOW, DCWormsTags.UPDATE, src); |
---|
[477] | 278 | } else if(parent != null) |
---|
| 279 | parent.getEventHandler().handleSchedulingEvent(event); |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | public EventHandler getEventHandler(){ |
---|
| 284 | return new SimpleEventHandler(); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | public void initiate(){ |
---|
| 288 | ResourceEventCommand rec = new ResourceEventCommand(this); |
---|
| 289 | EnergyEvent event = new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller"); |
---|
| 290 | event.setReason(EventReason.SIM_INIT); |
---|
| 291 | rec.execute(event); |
---|
| 292 | //alternative way |
---|
| 293 | //getEventHandler().handleResourceEvent(new EnergyEvent(EnergyEventType.POWER_STATE_CHANGED, "Resource controller")); |
---|
| 294 | } |
---|
| 295 | } |
---|