source: DCWoRMS/branches/coolemall/src/schedframe/resources/computing/description/ComputingResourceDescription.java @ 1113

Revision 1113, 15.9 KB checked in by wojtekp, 12 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package schedframe.resources.computing.description;
2
3import java.io.FileInputStream;
4import java.io.FileNotFoundException;
5import java.io.IOException;
6import java.util.ArrayList;
7import java.util.List;
8import java.util.PropertyResourceBundle;
9import java.util.ResourceBundle;
10
11import schedframe.Parameter;
12import schedframe.Parameters;
13import schedframe.Property;
14import schedframe.resources.ResourceTypeFactory;
15import schedframe.resources.computing.location.Location;
16import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputProfile;
17import schedframe.resources.computing.profiles.energy.airthroughput.AirThroughputState;
18import schedframe.resources.computing.profiles.energy.airthroughput.plugin.AirThroughputEstimationPlugin;
19import schedframe.resources.computing.profiles.energy.airthroughput.plugin.DefaultAirThroughputEstimationPlugin;
20import schedframe.resources.computing.profiles.energy.power.PState;
21import schedframe.resources.computing.profiles.energy.power.PowerProfile;
22import schedframe.resources.computing.profiles.energy.power.PowerState;
23import schedframe.resources.computing.profiles.energy.power.PowerStateNameFactory;
24import schedframe.resources.computing.profiles.energy.power.Transition;
25import schedframe.resources.computing.profiles.energy.power.plugin.EnergyEstimationPlugin;
26import schedframe.resources.computing.profiles.energy.thermal.ThermalProfile;
27import schedframe.resources.computing.profiles.energy.thermal.plugin.DefaultTemperatureEstimationPlugin;
28import schedframe.resources.computing.profiles.energy.thermal.plugin.TemperatureEstimationPlugin;
29import schedframe.resources.units.ResourceUnit;
30import schedframe.resources.units.ResourceUnitFactory;
31import schedframe.resources.utils.ResourceIdGenerator;
32import schemas.ComputingResource;
33import schemas.PowerUsage;
34import schemas.Profile;
35import simulator.utils.InstanceFactory;
36import example.energy.DefaultEnergyEstimationPlugin;
37
38public class ComputingResourceDescription extends ExecutingResourceDescription {
39
40        //protected Map<ResourceUnitName, List<AbstractResourceUnit>> resUnits;
41        protected PowerProfile powerProfile;
42        protected AirThroughputProfile airThroughputProfile;
43        protected ThermalProfile thermalProfile;
44        protected Location location;
45        protected String category;
46        //protected Parameters parameters;
47
48        public ComputingResourceDescription(ComputingResource computingResource) {
49
50                super(ResourceTypeFactory.createResourceType(computingResource.getClazz()));
51                this.category = computingResource.getType();
52               
53                initId(computingResource);
54
55                if (computingResource.getComputingResourceTypeChoiceSequence() != null) {
56                        initResourceUnits(computingResource.getComputingResourceTypeChoiceSequence().getResourceUnit());
57                        try {
58                                if(System.getProperty("coolemall.resdesc") != null){
59                                        schemas.EnergyEstimationPlugin eep = new schemas.EnergyEstimationPlugin();
60                                        eep.setName(getEEP(createEEPQuery(computingResource), System.getProperty("coolemall.resdesc")));
61                                        if(computingResource.getComputingResourceTypeChoiceSequence().getProfile() != null) {
62                                                if(computingResource.getComputingResourceTypeChoiceSequence().getProfile().getPowerProfile() != null) {
63                                                        computingResource.getComputingResourceTypeChoiceSequence().getProfile().getPowerProfile().setEnergyEstimationPlugin(eep);
64                                                }
65                                        } else {
66                                                schemas.Profile p = new schemas.Profile();
67                                                computingResource.getComputingResourceTypeChoiceSequence().setProfile(p);
68                                                schemas.PowerProfile pp = new schemas.PowerProfile();
69                                                computingResource.getComputingResourceTypeChoiceSequence().getProfile().setPowerProfile(pp);
70                                                computingResource.getComputingResourceTypeChoiceSequence().getProfile().getPowerProfile().setEnergyEstimationPlugin(eep);
71                                        }
72                                }
73                        } catch (FileNotFoundException e) {
74                        } catch (IOException e) {
75                        }
76                        initProfiles(computingResource.getComputingResourceTypeChoiceSequence().getProfile());
77                        initLocation(computingResource.getComputingResourceTypeChoiceSequence().getLocation());
78                        this.parameters = extractParameters(computingResource.getComputingResourceTypeChoiceSequence().getParameter());
79                }
80        }
81
82        private void initId(ComputingResource computingResource){
83                this.id = computingResource.getName() != null ? computingResource.getName() : type.getName();
84                if(computingResource.getCount() > 1 || computingResource.getName() == null){
85                        this.id = id + "_" + String.valueOf(ResourceIdGenerator.getId(type.getName()));
86                }
87        }
88       
89        private void initResourceUnits(schemas.ResourceUnit[] resourceUnitCharacteristics) {
90                for (int i = 0; i < resourceUnitCharacteristics.length; i++) {
91                        schemas.ResourceUnit resourceUnitCharacteristic = resourceUnitCharacteristics[i];
92                        ResourceUnit resourceUnit = ResourceUnitFactory.createUnit(resourceUnitCharacteristic.getClazz(), this.id,
93                                         Double.valueOf(resourceUnitCharacteristic.getAmount().getContent()).intValue(), 0);
94                        Parameters params = extractParameters(resourceUnitCharacteristic.getParameter());
95                        resourceUnit.init(params);
96                        addResourceUnit(resourceUnit);
97                }
98        }
99
100        private void initProfiles(Profile profile) {
101                if (profile != null) {
102                        initPowerProfile(profile.getPowerProfile());
103                        initAirThroughputProfile(profile.getAirThroughputProfile());
104                        initThermalProfile(profile.getThermalProfile());
105                }
106        }
107       
108        private void initPowerProfile(schemas.PowerProfile powerProfileCharacteristic) {
109               
110                if (powerProfileCharacteristic != null) {
111                        EnergyEstimationPlugin energyEstimationPlugin = null;
112                        List<PowerState> powerStates = null;
113                        List<PState> pStates = null;
114                        if(powerProfileCharacteristic.getEnergyEstimationPlugin() != null){
115                                String energyEstimationPluginName = powerProfileCharacteristic.getEnergyEstimationPlugin().getName();
116                                if(energyEstimationPluginName != null) {
117                                        energyEstimationPlugin = (EnergyEstimationPlugin) InstanceFactory.createInstance(
118                                                        energyEstimationPluginName, EnergyEstimationPlugin.class);                     
119                                } else {
120                                        energyEstimationPlugin = new DefaultEnergyEstimationPlugin();
121                                }
122                                Parameters params = extractParameters(powerProfileCharacteristic.getEnergyEstimationPlugin().getParameter());
123                                energyEstimationPlugin.init(params);
124                        }
125                       
126                        if(powerProfileCharacteristic.getPowerStates() != null) {
127                                powerStates = new ArrayList<PowerState>();
128                                int powerStateCount = powerProfileCharacteristic.getPowerStates().getPowerStateCount();
129                                for (int i = 0; i < powerStateCount ; i++) {
130                                        schemas.PowerState ps = powerProfileCharacteristic.getPowerStates().getPowerState(i);
131                                        List<Transition> transitions = new ArrayList<Transition>();
132                                        int transitionCount = ps.getTransitionCount();
133                                        for (int j = 0; j < transitionCount; j++) {
134                                                schemas.Transition t = ps.getTransition(j);
135                                                Transition transition = new Transition(PowerStateNameFactory.createPowerStateName(t.getTo()), t
136                                                                .getPowerUsage().getContent(), t.getTime().getContent());
137                                                Parameters params = extractParameters(t.getParameter());
138                                                transition.init(params);
139                                                transitions.add(transition);
140                                        }
141                                        //CoolEmAll DEBB description case
142                                        if(ps.getPowerUsage() == null){
143                                                ps.setPowerUsage(new PowerUsage("0"));
144                                        }
145                                        PowerState powerState = new PowerState(PowerStateNameFactory.createPowerStateName(ps.getName()), ps
146                                                        .getPowerUsage().getContent(), transitions);
147                                        Parameters params = extractParameters(ps.getParameter());
148                                        powerState.init(params);
149                                        powerStates.add(powerState);
150                                }
151                        }
152                       
153                        if(powerProfileCharacteristic.getParameter() != null){
154                                pStates = new ArrayList<PState>();
155                                int parameterCount = powerProfileCharacteristic.getParameterCount();
156                                for(int i = 0; i < parameterCount; i++){
157                                        schemas.Parameter parameter = powerProfileCharacteristic.getParameter(i);
158                                        if(parameter.getName().equals("pState")){
159                                                PState.Builder builder = new PState.Builder();
160                                                int propertyCount = parameter.getParameterTypeSequence().getPropertyCount();
161                                                for(int j = 0; j < propertyCount; j++){
162                                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
163                                                        if(property.getName().equals("name")){
164                                                                 builder = builder.name(property.getStringValueWithUnit(0).getContent());
165                                                        } else if (property.getName().equals("frequency")){
166                                                                 builder = builder.frequency(Double.valueOf(property.getStringValueWithUnit(0).getContent()));
167                                                        } else if (property.getName().equals("voltage")){
168                                                                builder = builder.voltage(Double.valueOf(property.getStringValueWithUnit(0).getContent()));
169                                                        } else if (property.getName().equals("powerUsage")){
170                                                                if(property.getStringValueWithUnit(0).getContent() != null && property.getStringValueWithUnit(0).getContent().length() > 0){
171                                                                        builder = builder.powerUsage(Double.valueOf(property.getStringValueWithUnit(0).getContent())); 
172                                                                }
173                                                        } else if (property.getName().equals("powerUsageMin")){
174                                                                if(property.getStringValueWithUnit(0).getContent() != null && property.getStringValueWithUnit(0).getContent().length() > 0){
175                                                                        builder = builder.powerUsageMin(Double.valueOf(property.getStringValueWithUnit(0).getContent()));       
176                                                                }
177                                                        } else if (property.getName().equals("powerUsageMax")){
178                                                                if(property.getStringValueWithUnit(0).getContent() != null && property.getStringValueWithUnit(0).getContent().length() > 0){
179                                                                        builder = builder.powerUsageMax(Double.valueOf(property.getStringValueWithUnit(0).getContent()));       
180                                                                }
181                                                        }
182                                                }
183                                                PState pState = builder.build();
184                                                pStates.add(pState);
185                                        }
186                                }       
187                        }
188                        this.powerProfile = new PowerProfile(energyEstimationPlugin, powerStates, pStates);
189                        Parameters params = extractParameters(powerProfileCharacteristic.getParameter());
190                        this.powerProfile.init(params);
191                }
192        }
193
194        private void initAirThroughputProfile(schemas.AirThroughputProfile airThroughputProfile) {
195                if (airThroughputProfile != null) {
196                       
197                        AirThroughputEstimationPlugin airThroughputEstimationPlugin = null;
198                        List<AirThroughputState> airThroughputStates = null;
199                        if(airThroughputProfile.getAirThroughputEstimationPlugin() != null){
200                                String airThroughputEstimationPluginName = airThroughputProfile.getAirThroughputEstimationPlugin().getName();
201                                if(airThroughputEstimationPluginName != null) {
202                                        airThroughputEstimationPlugin = (AirThroughputEstimationPlugin) InstanceFactory.createInstance(
203                                                        airThroughputEstimationPluginName, AirThroughputEstimationPlugin.class);                       
204                                } else {
205                                        airThroughputEstimationPlugin = new DefaultAirThroughputEstimationPlugin();
206                                }
207                                Parameters params = extractParameters(airThroughputProfile.getAirThroughputEstimationPlugin().getParameter());
208                                airThroughputEstimationPlugin.init(params);
209                        }
210                        if(airThroughputProfile.getAirThroughputStates() != null){
211                                airThroughputStates = new ArrayList<AirThroughputState>();
212                                int airThrouhputStateCount = airThroughputProfile.getAirThroughputStates().getAirThroughputStateCount();
213                                for (int i = 0; i < airThrouhputStateCount; i++) {
214                                        schemas.AirThroughputState ats = airThroughputProfile.getAirThroughputStates().getAirThroughputState(i);
215                                        AirThroughputState airThroughputState = new AirThroughputState(ats.getName(), ats.getValue()
216                                                        .getContent(), ats.getPowerUsage().getContent());
217                                        Parameters params = extractParameters(ats.getParameter());
218                                        airThroughputState.init(params);
219                                        airThroughputStates.add(airThroughputState);
220                                }
221                        }
222                        this.airThroughputProfile = new AirThroughputProfile(airThroughputEstimationPlugin, airThroughputStates);
223                        Parameters params = extractParameters(airThroughputProfile.getParameter());
224                        this.airThroughputProfile.init(params);
225                }
226        }
227       
228        private void initThermalProfile(schemas.ThermalProfile thermalProfile) {
229               
230                if (thermalProfile != null) {
231                       
232                        TemperatureEstimationPlugin temperatureEstimationPlugin = null;
233
234                        if(thermalProfile.getTemperatureEstimationPlugin() != null){
235                                String temperatureEstimationPluginName = thermalProfile.getTemperatureEstimationPlugin().getName();
236                                if(temperatureEstimationPluginName != null) {
237                                        temperatureEstimationPlugin = (TemperatureEstimationPlugin) InstanceFactory.createInstance(
238                                                        temperatureEstimationPluginName, TemperatureEstimationPlugin.class);                   
239                                } else {
240                                        temperatureEstimationPlugin = new DefaultTemperatureEstimationPlugin();
241                                }
242                                Parameters params = extractParameters(thermalProfile.getTemperatureEstimationPlugin().getParameter());
243                                temperatureEstimationPlugin.init(params);
244                        }
245                        this.thermalProfile = new ThermalProfile();
246                        Parameters params = extractParameters(thermalProfile.getParameter());
247                        this.thermalProfile.init(params);
248                }
249        }
250       
251        private void initLocation(schemas.Location l) {
252                if (location != null) {
253                        this.location = new Location(l.getHorizontal(), l.getVertical(), l.getDepth());
254                        Parameters params = extractParameters(l.getParameter());
255                        this.location.init(params);
256                }
257        }
258
259
260        /*private Properties initProperties(schemas.Parameter[] parameters){
261                Properties prop = new Properties();
262               
263                for(int i = 0; i < parameters.length; i++){
264                        schemas.Parameter parameter = parameters[i];
265                        List values = new ArrayList();
266                        if(parameter.getParameterTypeSequence().getProperty() != null)
267                        {
268                                Map<String, List<StringValueWithUnit>> properties = new HashMap<String, List<StringValueWithUnit>>();
269                                List<StringValueWithUnit> propValues = new ArrayList<StringValueWithUnit>();
270                                for(int j = 0; j < parameter.getParameterTypeSequence().getPropertyCount(); j++){
271                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
272                                        for(int k = 0; k < property.getStringValueWithUnitCount(); k++){
273                                                propValues.add(property.getStringValueWithUnit(k));
274                                        }
275                                        properties.put(property.getName(), propValues);
276                                }
277                                values.add(properties);
278                        }else {
279                                for(int j = 0; j < parameter.getStringValueWithUnitCount(); j++){
280                                        values.add(parameter.getStringValueWithUnit(j));
281                                }
282                        }
283                        prop.put(parameter.getName(), values);
284                }
285                return prop;
286        }*/
287       
288        private Parameters extractParameters(schemas.Parameter[] parameters){
289               
290                Parameters params = null;
291               
292                if(parameters.length != 0)
293                        params = new Parameters();
294               
295                for(int i = 0; i < parameters.length; i++){
296                        schemas.Parameter parameter = parameters[i];
297                        Parameter param = new Parameter(parameter.getName());
298                        if(parameter.getParameterTypeSequence() != null && parameter.getParameterTypeSequence().getProperty() != null)
299                        {                                                       
300                                int propertyCount = parameter.getParameterTypeSequence().getPropertyCount();
301                                for(int j = 0; j < propertyCount; j++){
302                                        schemas.Property property = parameter.getParameterTypeSequence().getProperty(j);
303                                        Property prop = new Property(property.getName());
304                                        int stringValueWithUnitCount = property.getStringValueWithUnitCount();
305                                        for(int k = 0; k < stringValueWithUnitCount; k++){
306                                                prop.add(property.getStringValueWithUnit(k));
307                                        }
308                                        param.addProperty(prop);
309                                }
310                        } else {
311                                int stringValueWithUnitCount =  parameter.getStringValueWithUnitCount();
312                                for(int j = 0; j < stringValueWithUnitCount; j++){
313                                        param.add(parameter.getStringValueWithUnit(j));
314                                }
315                        }
316                        params.put(parameter.getName(), param);
317                }
318                return params;
319        }
320
321        public PowerProfile getPowerProfile() {
322                return powerProfile;
323        }
324
325        public AirThroughputProfile getAirThroughputProfile() {
326                return airThroughputProfile;
327        }
328
329        public ThermalProfile getThermalProfile() {
330                return thermalProfile;
331        }
332       
333        public Location getLocation() {
334                return location;
335        }
336
337        public String getCategory() {
338                return category;
339        }
340
341
342        /***COOLEMALL CASE RELATED***/
343
344        private static ResourceBundle recsBundle;
345       
346        private ResourceBundle getRecsBundle(String fileName) throws FileNotFoundException, IOException{
347                if(recsBundle == null){
348                        recsBundle = new PropertyResourceBundle(new FileInputStream(fileName));
349                }
350                return recsBundle;
351        }
352       
353        protected String getEEP(String query, String fileName) throws FileNotFoundException, IOException{
354                ResourceBundle recsBundle = getRecsBundle(fileName);
355                return recsBundle.getString(query);
356        }
357       
358        protected String createEEPQuery(ComputingResource compRes) {
359                String query = compRes.getClazz() + "EEP";
360                return query;
361        }
362}
Note: See TracBrowser for help on using the repository browser.