package test.rewolucja.resources.manager.implementation; import gridsim.GridSim; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; import java.util.Stack; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import schedframe.resources.units.Memory; import schedframe.resources.units.ResourceUnit; import schedframe.scheduling.plugin.local.ResourceAllocationInterface; import schedframe.scheduling.utils.ResourceParameterName; import test.rewolucja.resources.ProcessingElements; import test.rewolucja.resources.ResourceCharacteristics; import test.rewolucja.resources.ResourceStatus; import test.rewolucja.resources.ResourceType; import test.rewolucja.resources.UnitState; import test.rewolucja.resources.exception.ResourceException; import test.rewolucja.resources.logical.LogicalResource; import test.rewolucja.resources.manager.interfaces.ResourceManagerInterface; import test.rewolucja.resources.physical.base.ComputingResource; import test.rewolucja.resources.utils.validator.ResourcePropertiesValidator; import test.rewolucja.resources.utils.validator.ResourceValidator; public class ResourceManager implements ResourceAllocationInterface, ResourceManagerInterface { //private Log log = LogFactory.getLog(ResourceManager.class); protected List resources; protected List logicalResource; protected ResourceCharacteristics resourceCharacteristic; public ResourceManager(List resources, List logicalLayers) { this.resources = resources; this.logicalResource = logicalLayers; this.resourceCharacteristic = new ResourceCharacteristics(); for(ComputingResource resource : resources){ Map> resUnits = resource.getResourceCharacteristic().getResourceUnits(); for(ResourceParameterName rpn : resUnits.keySet()){ resourceCharacteristic.addResourceUnit(resUnits.get(rpn).get(0)); } } /*(Resource parentResource = resources.get(0).getParent(); if (parentResource.getChildren().size() == resources.size()) { setResourceCharacteristic(parentResource.getResourceCharacteristic()); }*/ } public ResourceManager(ComputingResource resource) { this.resources = resource.getChildren(); this.resourceCharacteristic = resource.getResourceCharacteristic(); // this.logicalLayers = resource.getLogaicaLayer().getChildren(); } public List getResources() { return resources; } public boolean areResourcesAchievable(ResourceType type) { if (resources != null) { Stack toExamine = new Stack(); for (ComputingResource resource : resources) toExamine.push(resource); while (!toExamine.isEmpty()) { ComputingResource resource = toExamine.pop(); List resources = resource.getChildren(); if (resources == null) continue; int numberOfResComp = resources.size(); for (int i = 0; i < numberOfResComp; i++) { ComputingResource resourceChild = resources.get(i); if (resourceChild.getType() == type) { return true; } else toExamine.push(resourceChild); } } } return false; } public List getResourcesOfType(ResourceType type) throws ResourceException { List resourcesOfType = new ArrayList(); for (ComputingResource resource : resources) { if (resource.getType() == type) { resourcesOfType.add(resource); } else resourcesOfType.addAll(resource.getDescendantsByType(type)); } return resourcesOfType; } public List getResourcesOfTypeWithStatus(ResourceType type, ResourceStatus status) throws ResourceException { List resourcesOfType = new ArrayList(); for (ComputingResource resource : resources) { if (resource.getType() == type) { if (resource.getStatus() == status) { resourcesOfType.add(resource); } } else resourcesOfType.addAll(resource.getDescendantsByTypeAndStatus(type, status)); } return resourcesOfType; } public ComputingResource getResourceByName(String resourceName) throws ResourceException { ComputingResource resourceWithName = null; for (int i = 0; i < resources.size() && resourceWithName == null; i++) { ComputingResource resource = resources.get(i); if (resource.getName().compareTo(resourceName) == 0) resourceWithName = resource; else resourceWithName = resource.getDescendantsByName(resourceName); } return resourceWithName; } public List getResourceUnits(ResourceParameterName unitName) throws ResourceException { List resourceUnit = new ArrayList(); Stack toExamine = new Stack(); for (ComputingResource resource : resources) toExamine.push(resource); while (!toExamine.isEmpty()) { ComputingResource resource = toExamine.pop(); ResourceCharacteristics resourceCharacteristic = resource.getResourceCharacteristic(); ResourceUnit unit = resourceCharacteristic.getResourceUnit(unitName); if (unit != null) resourceUnit.add(unit); // else { List resources = resource.getChildren(); if (resources == null) continue; int numberOfResComp = resources.size(); for (int i = 0; i < numberOfResComp; i++) { ComputingResource resourceChild = resources.get(i); toExamine.push(resourceChild); } // } } return resourceUnit; } public List getAvailableResourceUnits(String resourceName) throws Exception { ComputingResource resource = getResourceByName(resourceName); List resourceUnits = new ArrayList(); while(resource != null){ for(List resUnits: resource.getResourceCharacteristic().getResourceUnits().values()) resUnits.addAll(resourceUnits); resource = resource.getParent(); } return resourceUnits; } public ResourceCharacteristics getResourceCharacteristic() { return resourceCharacteristic; } public List filterResources(Properties properties) { List descendants = new ArrayList(); for (ComputingResource resource : resources) { ResourceValidator resourceValidator = new ResourcePropertiesValidator(properties); if (resourceValidator.validate(resource)) descendants.add(resource); else descendants.addAll(resource.filterDescendants(properties)); } return descendants; } public List getResourceProviders() { return logicalResource; } public String getResourceProvider(String resName){ if(GridSim.getEntityId(resName) != -1){ return resName; } ComputingResource resourceWithName = null; for(int i = 0 ; i < resources.size() && resourceWithName == null; i++){ ComputingResource resource = resources.get(i); if(resource.getName().equals(resName)) resourceWithName = resource; else try { resourceWithName = resource.getDescendantsByName(resName); } catch (ResourceException e) { return null; } } if(resourceWithName == null) return null; List children = resourceWithName.getChildren(); Set childrenControllers = new HashSet(); for(ComputingResource child:children) { childrenControllers.add(child.getLogicalResource()); } Set tempChildrenControllers = new HashSet(childrenControllers); while(childrenControllers.size() != 1){ childrenControllers = new HashSet(); for(LogicalResource lr:tempChildrenControllers){ childrenControllers.add(lr.getParent()); } tempChildrenControllers = new HashSet(childrenControllers); } Iterator it = childrenControllers.iterator(); LogicalResource potentialLogicalResource = it.next(); if(potentialLogicalResource.getResources().containsAll(children)) return potentialLogicalResource.get_name(); return null; } public boolean allocateResources(Map resources) { if (resources == null) { return false; } ResourceUnit processingElements = resources.get(ResourceParameterName.PROCESSINGELEMENTS); if (processingElements != null) { if (processingElements instanceof ProcessingElements) { ProcessingElements choosenProcessors = (ProcessingElements) processingElements; for (int i = 0; i < choosenProcessors.size(); i++) { choosenProcessors.get(i).setStatus(ResourceStatus.BUSY); } } } Memory m = (Memory) resources.get(ResourceParameterName.MEMORY); if (m != null) { m.setState(UnitState.BUSY); } return true; } public void freeResources(Map lastUsedResources) { ResourceUnit resUnit = lastUsedResources.get(ResourceParameterName.PROCESSINGELEMENTS); if (resUnit instanceof ProcessingElements) { ProcessingElements processingElements = (ProcessingElements) resUnit; for (int i = 0; i < processingElements.size(); i++) { processingElements.get(i).setStatus(ResourceStatus.FREE); } } Memory m = (Memory) lastUsedResources.get(ResourceParameterName.MEMORY); if (m != null) { m.setState(UnitState.FREE); } } }