source: xssim/src/simulator/factory/SingleResourceFactory.java @ 104

Revision 104, 6.8 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.factory;
2
3import qcg.qcg_broker.types.HostParametersItem;
4import qcg.qcg_broker.types.hostspec.Machine;
5import qcg.qcg_broker.types.hostspec.SingleResource;
6import org.qcg.broker.schemas.hostparams.HostParameters;
7
8import java.util.ArrayList;
9import java.util.HashMap;
10
11import simulator.RandomNumbers;
12import simulator.hostspec.ForecastFinishTimePluginName;
13import simulator.hostspec.LocalAllocationPolicyPluginName;
14import simulator.hostspec.Plugins;
15
16/**
17 * This factory can create qcg.qcg_broker.types.hostspec.SingleResource
18 * based on  simulator.hostspec.SingleResource description
19 *
20 * @author Marcin Krystek
21 *
22 */
23public class SingleResourceFactory {
24        public static String CPU_COUNT = "cpuCount";
25        public static String CPU_SPEED = "cpuSpeed";
26        public static String CPU_LOAD = "cpuLoad";
27        public static String MEMORY_TOTAL = "memoryTotal";
28        public static String MEMORY_FREE = "memoryFree";
29        public static String SWAP_TOTAL = "swapTotal";
30        public static String SWAP_FREE = "swapFree";
31        public static String DISK_TOTAL = "diskTotal";
32        public static String DISK_FREE = "diskFree";
33       
34        protected simulator.hostspec.SingleResource singleRes;
35        protected RandomNumbers randNumbers;
36        protected HashMap <String, String>forecastFinishTimePlugin;
37        protected HashMap <String, String>localAllocationPolicyPlugin;
38       
39        /**
40         *
41         * @param singleRes
42         */
43        public SingleResourceFactory(simulator.hostspec.SingleResource singleRes, RandomNumbers randNumbers){
44                this.singleRes = singleRes;
45                this.randNumbers = randNumbers;
46                this.forecastFinishTimePlugin = new HashMap<String, String>();
47                this.localAllocationPolicyPlugin = new HashMap<String, String>();
48        }
49       
50        public HostParametersItem[] createResources() throws IllegalAccessException{
51                if(singleRes == null)
52                        return null;
53               
54                ArrayList <HostParametersItem>retList = new ArrayList<HostParametersItem>();
55               
56                int resCount = singleRes.getTotalResourceCount();
57                int arResCount = singleRes.getARResourceCount();
58                String resName;
59               
60                Plugins plugins = singleRes.getPlugins();
61               
62                int timePluginIndex = 0;
63                ForecastFinishTimePluginName timePlugin;
64                ForecastFinishTimePluginName timePluginName[]  =
65                                plugins.getForecastFinishTimePluginName();
66               
67                int allocationPluginIndex = 0;
68                LocalAllocationPolicyPluginName allocationPlugin;
69                LocalAllocationPolicyPluginName allocationPluginName[] =
70                                plugins.getLocalAllocationPolicyPluginName();
71               
72                for(int i = 0; i < resCount; i++){
73                       
74                        HostParametersItem item = new HostParametersItem();
75                                SingleResource singleResource = new SingleResource();
76                                        Machine machine = createMachine(randNumbers);
77                                singleResource.setMachine(machine);
78
79                                if(arResCount > 0)
80                                        singleResource.setReservationAvailable(true);
81                                else
82                                        singleResource.setReservationAvailable(false);
83                                arResCount--;
84                       
85                        resName = "single_resource_"+i;
86                        singleResource = setName(singleResource, resName);
87                        item.setSingleResource(singleResource);
88
89                        retList.add(item);
90                       
91                        timePlugin = timePluginName[timePluginIndex];
92                        forecastFinishTimePlugin.put(resName, timePlugin.getName());
93                       
94                        timePlugin.setResourcesCountWithThisPlugin(
95                                        timePlugin.getResourcesCountWithThisPlugin() - 1);
96                        if(timePlugin.getResourcesCountWithThisPlugin() == 0)
97                                timePluginIndex++;
98                       
99                        allocationPlugin = allocationPluginName[allocationPluginIndex];
100                        localAllocationPolicyPlugin.put(resName, allocationPlugin.getName());
101                       
102                        allocationPlugin.setResourcesCountWithThisPlugin(
103                                        allocationPlugin.getResourcesCountWithThisPlugin() - 1);
104                        if(allocationPlugin.getResourcesCountWithThisPlugin() == 0)
105                                allocationPluginIndex++;
106                       
107                }
108                HostParametersItem tab[] = new HostParametersItem[retList.size()];
109                return retList.toArray(tab);
110        }
111       
112        public HostParameters createResources(HostParameters hostParameters) throws IllegalAccessException{
113                HostParametersItem tab[] = null;
114                tab = createResources();
115               
116                if(tab == null)
117                        return hostParameters;
118               
119                for(int i = 0; i < tab.length; i++)
120                        hostParameters.addHostParametersItem(tab[i]);
121               
122                return hostParameters;
123        }
124       
125       
126        protected Machine createMachine(RandomNumbers randNumbers) throws IllegalAccessException{
127                Double value;
128                Machine machine = new Machine();
129               
130                // --- CPU_COUNT
131                value = randNumbers.getRandomValue(SingleResourceFactory.CPU_COUNT);
132                machine.setCpuCount(value.intValue());
133               
134                // --- CPU_LOAD
135                value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_LOAD));
136                qcg.qcg_broker.types.hostspec.CpuLoad cpuLoad = new qcg.qcg_broker.types.hostspec.CpuLoad();
137                        value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_LOAD));
138                        cpuLoad.setValue(value);
139                machine.setCpuLoad(cpuLoad);
140               
141                // ---  CPU_SPEED
142                value = new Double(randNumbers.getRandomValue(SingleResourceFactory.CPU_SPEED));
143                machine.setCpuSpeed(value.intValue());
144               
145                // ---  DISK_FREE       
146                qcg.qcg_broker.types.hostspec.DiskFree diskFree = new qcg.qcg_broker.types.hostspec.DiskFree();
147                        value = new Double(randNumbers.getRandomValue(SingleResourceFactory.DISK_FREE));
148                        diskFree.setValue(value.doubleValue());
149                machine.setDiskFree(diskFree);
150               
151                // ---  DISK_TOTAL     
152                value = new Double(randNumbers.getRandomValue(SingleResourceFactory.DISK_TOTAL));
153                machine.setDiskTotal(value.intValue());
154               
155                // --- MEMORY_FREE
156                qcg.qcg_broker.types.hostspec.MemoryFree memoryFree = new qcg.qcg_broker.types.hostspec.MemoryFree();
157                        value = new Double(randNumbers.getRandomValue(SingleResourceFactory.MEMORY_FREE));
158                        memoryFree.setValue(value.doubleValue());
159                machine.setMemoryFree(memoryFree);
160               
161                // --- MEMORY_TOTAL
162                value = new Double(randNumbers.getRandomValue(SingleResourceFactory.MEMORY_TOTAL));
163                machine.setMemoryTotal(value.intValue());
164               
165                // ---  SWAP_FREE       
166                qcg.qcg_broker.types.hostspec.SwapFree swapFree = new qcg.qcg_broker.types.hostspec.SwapFree();
167                        value = new Double(randNumbers.getRandomValue(SingleResourceFactory.SWAP_FREE));
168                        swapFree.setValue(value.doubleValue());
169                machine.setSwapFree(swapFree);
170
171                // ---  SWAP_TOTAL
172                value = new Double(randNumbers.getRandomValue(SingleResourceFactory.SWAP_TOTAL));
173                machine.setSwapTotal(value.intValue());
174
175
176//              machine.setHostName("random_host_name");
177//              machine.setCpuType("random_cpu_type");
178//              machine.setOsName("random_os_name");
179//              machine.setOsRelease("random_os_release");
180//              machine.setOsType("random_os_type");
181//              machine.setOsVersion("random_os_version");
182               
183                return machine;
184        }
185       
186       
187        /**
188         * Name of SingleResource is name of first Machine
189         * @param resource
190         * @return
191         */
192        public static String getName(SingleResource resource){
193                String ret;
194                ret = resource.getResManContact();
195                return ret;
196        }
197       
198        public static SingleResource setName(SingleResource resource, String name){
199                resource.setResManContact(name);
200                return resource;
201        }
202       
203        public HashMap<String, String> getForecastFinishTimePlugin(){
204                return forecastFinishTimePlugin;
205        }
206        public HashMap<String, String> getLocalAllocationPolicyPlugin(){
207                return localAllocationPolicyPlugin;
208        }
209}
Note: See TracBrowser for help on using the repository browser.