package simulator.factory; import qcg.qcg_broker.types.HostParametersItem; import qcg.qcg_broker.types.hostspec.Machine; import qcg.qcg_broker.types.hostspec.SingleResource; import org.qcg.broker.schemas.hostparams.HostParameters; import java.util.ArrayList; import java.util.HashMap; import simulator.RandomNumbers; import simulator.hostspec.ForecastFinishTimePluginName; import simulator.hostspec.LocalAllocationPolicyPluginName; import simulator.hostspec.Plugins; /** * This factory can create qcg.qcg_broker.types.hostspec.SingleResource * based on simulator.hostspec.SingleResource description * * @author Marcin Krystek * */ public class SingleResourceFactory { public static String CPU_COUNT = "cpuCount"; public static String CPU_SPEED = "cpuSpeed"; public static String CPU_LOAD = "cpuLoad"; public static String MEMORY_TOTAL = "memoryTotal"; public static String MEMORY_FREE = "memoryFree"; public static String SWAP_TOTAL = "swapTotal"; public static String SWAP_FREE = "swapFree"; public static String DISK_TOTAL = "diskTotal"; public static String DISK_FREE = "diskFree"; protected simulator.hostspec.SingleResource singleRes; protected RandomNumbers randNumbers; protected HashMap forecastFinishTimePlugin; protected HashMap localAllocationPolicyPlugin; /** * * @param singleRes */ public SingleResourceFactory(simulator.hostspec.SingleResource singleRes, RandomNumbers randNumbers){ this.singleRes = singleRes; this.randNumbers = randNumbers; this.forecastFinishTimePlugin = new HashMap(); this.localAllocationPolicyPlugin = new HashMap(); } public HostParametersItem[] createResources() throws IllegalAccessException{ if(singleRes == null) return null; ArrayList retList = new ArrayList(); int resCount = singleRes.getTotalResourceCount(); int arResCount = singleRes.getARResourceCount(); String resName; Plugins plugins = singleRes.getPlugins(); int timePluginIndex = 0; ForecastFinishTimePluginName timePlugin; ForecastFinishTimePluginName timePluginName[] = plugins.getForecastFinishTimePluginName(); int allocationPluginIndex = 0; LocalAllocationPolicyPluginName allocationPlugin; LocalAllocationPolicyPluginName allocationPluginName[] = plugins.getLocalAllocationPolicyPluginName(); for(int i = 0; i < resCount; i++){ HostParametersItem item = new HostParametersItem(); SingleResource singleResource = new SingleResource(); Machine machine = createMachine(randNumbers); singleResource.setMachine(machine); if(arResCount > 0) singleResource.setReservationAvailable(true); else singleResource.setReservationAvailable(false); arResCount--; resName = "single_resource_"+i; singleResource = setName(singleResource, resName); item.setSingleResource(singleResource); retList.add(item); timePlugin = timePluginName[timePluginIndex]; forecastFinishTimePlugin.put(resName, timePlugin.getName()); timePlugin.setResourcesCountWithThisPlugin( timePlugin.getResourcesCountWithThisPlugin() - 1); if(timePlugin.getResourcesCountWithThisPlugin() == 0) timePluginIndex++; allocationPlugin = allocationPluginName[allocationPluginIndex]; localAllocationPolicyPlugin.put(resName, allocationPlugin.getName()); allocationPlugin.setResourcesCountWithThisPlugin( allocationPlugin.getResourcesCountWithThisPlugin() - 1); if(allocationPlugin.getResourcesCountWithThisPlugin() == 0) allocationPluginIndex++; } HostParametersItem tab[] = new HostParametersItem[retList.size()]; return retList.toArray(tab); } public HostParameters createResources(HostParameters hostParameters) throws IllegalAccessException{ HostParametersItem tab[] = null; tab = createResources(); if(tab == null) return hostParameters; for(int i = 0; i < tab.length; i++) hostParameters.addHostParametersItem(tab[i]); return hostParameters; } protected Machine createMachine(RandomNumbers randNumbers) throws IllegalAccessException{ Double value; Machine machine = new Machine(); // --- CPU_COUNT value = randNumbers.getRandomValue(SingleResourceFactory.CPU_COUNT); machine.setCpuCount(value.intValue()); // --- CPU_LOAD value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_LOAD)); qcg.qcg_broker.types.hostspec.CpuLoad cpuLoad = new qcg.qcg_broker.types.hostspec.CpuLoad(); value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_LOAD)); cpuLoad.setValue(value); machine.setCpuLoad(cpuLoad); // --- CPU_SPEED value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_SPEED)); machine.setCpuSpeed(value.intValue()); // --- DISK_FREE qcg.qcg_broker.types.hostspec.DiskFree diskFree = new qcg.qcg_broker.types.hostspec.DiskFree(); value = new Double(randNumbers.getRandomValue(SingleResourceFactory.DISK_FREE)); diskFree.setValue(value.doubleValue()); machine.setDiskFree(diskFree); // --- DISK_TOTAL value = new Double(randNumbers.getRandomValue(SingleResourceFactory.DISK_TOTAL)); machine.setDiskTotal(value.intValue()); // --- MEMORY_FREE qcg.qcg_broker.types.hostspec.MemoryFree memoryFree = new qcg.qcg_broker.types.hostspec.MemoryFree(); value = new Double(randNumbers.getRandomValue(SingleResourceFactory.MEMORY_FREE)); memoryFree.setValue(value.doubleValue()); machine.setMemoryFree(memoryFree); // --- MEMORY_TOTAL value = new Double(randNumbers.getRandomValue(SingleResourceFactory.MEMORY_TOTAL)); machine.setMemoryTotal(value.intValue()); // --- SWAP_FREE qcg.qcg_broker.types.hostspec.SwapFree swapFree = new qcg.qcg_broker.types.hostspec.SwapFree(); value = new Double(randNumbers.getRandomValue(SingleResourceFactory.SWAP_FREE)); swapFree.setValue(value.doubleValue()); machine.setSwapFree(swapFree); // --- SWAP_TOTAL value = new Double(randNumbers.getRandomValue(SingleResourceFactory.SWAP_TOTAL)); machine.setSwapTotal(value.intValue()); // machine.setHostName("random_host_name"); // machine.setCpuType("random_cpu_type"); // machine.setOsName("random_os_name"); // machine.setOsRelease("random_os_release"); // machine.setOsType("random_os_type"); // machine.setOsVersion("random_os_version"); return machine; } /** * Name of SingleResource is name of first Machine * @param resource * @return */ public static String getName(SingleResource resource){ String ret; ret = resource.getResManContact(); return ret; } public static SingleResource setName(SingleResource resource, String name){ resource.setResManContact(name); return resource; } public HashMap getForecastFinishTimePlugin(){ return forecastFinishTimePlugin; } public HashMap getLocalAllocationPolicyPlugin(){ return localAllocationPolicyPlugin; } }