[477] | 1 | package simulator.reader; |
---|
| 2 | |
---|
| 3 | import java.io.File; |
---|
| 4 | import java.io.FileNotFoundException; |
---|
| 5 | import java.io.FileReader; |
---|
| 6 | import java.io.IOException; |
---|
| 7 | import java.util.ArrayDeque; |
---|
| 8 | import java.util.ArrayList; |
---|
[512] | 9 | import java.util.Collections; |
---|
| 10 | import java.util.Comparator; |
---|
[477] | 11 | import java.util.Deque; |
---|
| 12 | import java.util.HashMap; |
---|
[490] | 13 | import java.util.LinkedHashSet; |
---|
[477] | 14 | import java.util.LinkedList; |
---|
| 15 | import java.util.List; |
---|
| 16 | import java.util.Map; |
---|
[490] | 17 | import java.util.Set; |
---|
[477] | 18 | |
---|
[1207] | 19 | import org.apache.commons.io.FilenameUtils; |
---|
[477] | 20 | import org.exolab.castor.xml.MarshalException; |
---|
| 21 | import org.exolab.castor.xml.ValidationException; |
---|
| 22 | import org.qcg.broker.schemas.exception.UnknownParameter; |
---|
| 23 | |
---|
| 24 | import schedframe.Initializable; |
---|
| 25 | import schedframe.Parameter; |
---|
| 26 | import schedframe.Parameters; |
---|
[1207] | 27 | import schedframe.SimulatedEnvironment; |
---|
| 28 | import schedframe.resources.CoolEmAllResourceFactory; |
---|
[512] | 29 | import schedframe.resources.Resource; |
---|
[477] | 30 | import schedframe.resources.StandardResourceType; |
---|
| 31 | import schedframe.resources.computing.ComputingResource; |
---|
| 32 | import schedframe.resources.computing.ResourceFactory; |
---|
[1207] | 33 | import schedframe.resources.computing.StandardResourceFactory; |
---|
[477] | 34 | import schedframe.resources.computing.description.ComputingResourceDescription; |
---|
[1207] | 35 | import schedframe.resources.computing.description.ResourceDescription; |
---|
[477] | 36 | import schedframe.resources.units.ResourceUnit; |
---|
| 37 | import schedframe.resources.units.ResourceUnitName; |
---|
| 38 | import schedframe.scheduling.Scheduler; |
---|
| 39 | import schedframe.scheduling.manager.resources.ManagedResources; |
---|
| 40 | import schedframe.scheduling.plugin.SchedulingPlugin; |
---|
| 41 | import schedframe.scheduling.plugin.estimation.ExecutionTimeEstimationPlugin; |
---|
| 42 | import schedframe.scheduling.queue.TaskQueue; |
---|
| 43 | import schedframe.scheduling.queue.TaskQueueList; |
---|
| 44 | import schemas.Environment; |
---|
| 45 | import schemas.ManagedComputingResources; |
---|
| 46 | import schemas.StringValueWithUnit; |
---|
[1207] | 47 | import simulator.DataCenterWorkloadSimulator; |
---|
[477] | 48 | import simulator.utils.InstanceFactory; |
---|
[1207] | 49 | import test.DEBBTranslator.src.PLMXMLTranslator; |
---|
[477] | 50 | |
---|
| 51 | public class ResourceReader { |
---|
| 52 | |
---|
[1042] | 53 | private static String COOLEMALL_RESDESC_PREFIX = "DCWORMS"; |
---|
[1207] | 54 | private String resDescFileName; |
---|
| 55 | private ResourceFactory resFactory; |
---|
| 56 | |
---|
[477] | 57 | private ExecutionTimeEstimationPlugin execTimeEstimationPlugin; |
---|
| 58 | |
---|
[490] | 59 | private Set<String> compResLayers; |
---|
[1207] | 60 | private List<Initializable> toInit; |
---|
[490] | 61 | |
---|
[1207] | 62 | public ResourceReader(String resdescFileName) throws IOException { |
---|
[477] | 63 | |
---|
[1207] | 64 | this.resDescFileName = resdescFileName; |
---|
| 65 | this.resFactory = new StandardResourceFactory(); |
---|
| 66 | this.compResLayers = new LinkedHashSet<String>(); |
---|
| 67 | this.toInit = new ArrayList<Initializable>(); |
---|
[477] | 68 | } |
---|
| 69 | |
---|
[1207] | 70 | public SimulatedEnvironment read() throws MarshalException, ValidationException, FileNotFoundException, Exception, |
---|
[477] | 71 | UnknownParameter { |
---|
| 72 | |
---|
| 73 | File file = new File(resDescFileName); |
---|
[1207] | 74 | |
---|
| 75 | Environment env = null; |
---|
[1039] | 76 | try{ |
---|
| 77 | env = Environment.unmarshal(new FileReader(file)); |
---|
[1207] | 78 | |
---|
[1039] | 79 | } catch (Exception e){ |
---|
[1207] | 80 | //e.printStackTrace(); |
---|
| 81 | try{ |
---|
| 82 | File dcwormsFile = new File(FilenameUtils.getFullPath(file.getAbsolutePath()) + COOLEMALL_RESDESC_PREFIX + "_" + file.getName()); |
---|
| 83 | env = Environment.unmarshal(new FileReader(dcwormsFile)); |
---|
| 84 | } catch (Exception e2){ |
---|
| 85 | PLMXMLTranslator plmxmlTranslator = new PLMXMLTranslator(); |
---|
| 86 | String trasnlatedFileName = plmxmlTranslator.translate(new String[]{file.getAbsolutePath()}); |
---|
| 87 | File dcwormsFile = new File(trasnlatedFileName); |
---|
| 88 | //File dcwormsFile = new File(FilenameUtils.getFullPath(file.getAbsolutePath()) + COOLEMALL_RESDESC_PREFIX + "_" + file.getName()); |
---|
| 89 | |
---|
| 90 | env = Environment.unmarshal(new FileReader(dcwormsFile)); |
---|
| 91 | } |
---|
[1039] | 92 | } |
---|
| 93 | |
---|
[1207] | 94 | if(env.getResources().getMode().equals("CoolEmAll")){ |
---|
| 95 | DataCenterWorkloadSimulator.MODE = "CoolEmAll"; |
---|
| 96 | resFactory = new CoolEmAllResourceFactory(); |
---|
| 97 | } |
---|
[477] | 98 | System.out.println("started creating environment description"); |
---|
| 99 | List<ComputingResourceDescription> mainCompResDescList = createEnvironmentDescription(env); |
---|
| 100 | System.out.println("finished creating environment description"); |
---|
| 101 | |
---|
| 102 | System.out.println("started creating resources"); |
---|
| 103 | List<ComputingResource> computingResources = createResources(mainCompResDescList); |
---|
| 104 | System.out.println("finished creating resource"); |
---|
| 105 | |
---|
[1346] | 106 | System.out.println("started creating scheduling entities"); |
---|
[477] | 107 | Scheduler mainScheduler = createSchedulers(env.getResources().getScheduler(), computingResources); |
---|
[1346] | 108 | System.out.println("finished creating scheduling entities"); |
---|
[477] | 109 | |
---|
[1207] | 110 | SimulatedEnvironment simEnv = new SimulatedEnvironment(mainScheduler, computingResources); |
---|
[512] | 111 | Collections.sort(toInit, new ResourceTypeComparator(new ArrayList<String>(compResLayers))); |
---|
[1207] | 112 | simEnv.setInitList(toInit); |
---|
| 113 | simEnv.setCompResLayers(compResLayers); |
---|
| 114 | return simEnv; |
---|
[477] | 115 | } |
---|
| 116 | |
---|
| 117 | protected List<ComputingResourceDescription> createEnvironmentDescription(Environment environment) throws Exception { |
---|
| 118 | |
---|
| 119 | List<ComputingResourceDescription> mainCompResDescList = new ArrayList<ComputingResourceDescription>(); |
---|
| 120 | |
---|
| 121 | LinkedList<ComputingResourceDescription> resDescStructure = new LinkedList<ComputingResourceDescription>(); |
---|
| 122 | LinkedList<schemas.ComputingResource> toExamine = new LinkedList<schemas.ComputingResource>(); |
---|
| 123 | EnvironmentWrapper environmentWrapper = new EnvironmentWrapper(); |
---|
| 124 | environmentWrapper.wrap(environment); |
---|
| 125 | |
---|
| 126 | String execTimeEstimationPluginClassName = null; |
---|
| 127 | if(environmentWrapper.getTimeEstimationPlugin() != null){ |
---|
| 128 | execTimeEstimationPluginClassName = environmentWrapper.getTimeEstimationPlugin().getName(); |
---|
[912] | 129 | |
---|
| 130 | } else { |
---|
| 131 | execTimeEstimationPluginClassName = "example.timeestimation.DefaultTimeEstimationPlugin"; |
---|
[477] | 132 | } |
---|
| 133 | |
---|
[912] | 134 | execTimeEstimationPlugin = (ExecutionTimeEstimationPlugin) InstanceFactory.createInstance( |
---|
| 135 | execTimeEstimationPluginClassName, ExecutionTimeEstimationPlugin.class); |
---|
| 136 | if(execTimeEstimationPlugin != null) { |
---|
| 137 | if(environmentWrapper.getTimeEstimationPlugin() != null){ |
---|
[477] | 138 | Parameters params = EnvironmentWrapper.extractParameters(environmentWrapper.getTimeEstimationPlugin().getParameter()); |
---|
| 139 | execTimeEstimationPlugin.init(params); |
---|
| 140 | } |
---|
| 141 | } |
---|
[912] | 142 | else { |
---|
| 143 | throw new Exception("Can not create execution time estimation plugin instance."); |
---|
| 144 | } |
---|
[477] | 145 | |
---|
[912] | 146 | |
---|
[477] | 147 | if(environmentWrapper.getComputingResources() != null) { |
---|
| 148 | for(int i = 0; i < environmentWrapper.getComputingResources().length; i++){ |
---|
| 149 | schemas.ComputingResource compResDef = environmentWrapper.getComputingResources()[i]; |
---|
[1464] | 150 | long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1; |
---|
| 151 | for(int j = 0; j < compResCount; j++){ |
---|
| 152 | toExamine.push(compResDef); |
---|
| 153 | |
---|
| 154 | ComputingResourceDescription compResDesc = new ComputingResourceDescription(compResDef); |
---|
| 155 | resDescStructure.push(compResDesc); |
---|
| 156 | mainCompResDescList.add(compResDesc); |
---|
| 157 | } |
---|
[477] | 158 | } |
---|
| 159 | } |
---|
| 160 | while (!toExamine.isEmpty()) { |
---|
| 161 | schemas.ComputingResource parentCompResDef = toExamine.pop(); |
---|
| 162 | ComputingResourceDescription parentExecResDesc = resDescStructure.pop(); |
---|
| 163 | if(parentCompResDef.getComputingResourceTypeChoiceSequence() != null) |
---|
| 164 | { |
---|
| 165 | int computingResourceCount = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResourceCount(); |
---|
| 166 | for (int i = 0; i < computingResourceCount; i++) { |
---|
| 167 | |
---|
| 168 | schemas.ComputingResource compResDef = parentCompResDef.getComputingResourceTypeChoiceSequence().getComputingResource(i); |
---|
| 169 | if(compResDef == null) |
---|
| 170 | continue; |
---|
| 171 | long compResCount = compResDef.getCount() > 1 ? compResDef.getCount() : 1; |
---|
| 172 | |
---|
| 173 | for(int j = 0; j < compResCount; j++){ |
---|
| 174 | if(compResDef.getComputingResourceTypeChoiceSequence2() != null){ |
---|
[925] | 175 | String templateId = compResDef.getComputingResourceTypeChoiceSequence2().getTemplateId(); |
---|
[477] | 176 | schemas.ComputingResourceTemplate template = environmentWrapper.getTemplate(templateId); |
---|
| 177 | environmentWrapper.initWithCompResTemplate(compResDef, template); |
---|
| 178 | } |
---|
| 179 | //toExamine.insertElementAt(compResDef, 0); |
---|
| 180 | toExamine.addLast(compResDef); |
---|
| 181 | ComputingResourceDescription resDesc = new ComputingResourceDescription(compResDef); |
---|
| 182 | parentExecResDesc.addChildren(resDesc); |
---|
| 183 | //resDescStructure.insertElementAt(resDesc, 0); |
---|
| 184 | resDescStructure.addLast(resDesc); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } else |
---|
| 188 | continue; |
---|
| 189 | } |
---|
| 190 | return mainCompResDescList; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | protected List<ComputingResource> createResources(List<ComputingResourceDescription> mainCompResDesList) { |
---|
| 194 | |
---|
| 195 | List<ComputingResource> mainCompResourceList = new ArrayList<ComputingResource>(); |
---|
| 196 | Deque<ComputingResourceDescription> toExamine = new ArrayDeque<ComputingResourceDescription>(); |
---|
| 197 | Deque<ComputingResource> resStructure = new ArrayDeque<ComputingResource>(); |
---|
| 198 | |
---|
| 199 | for(ComputingResourceDescription mainExecResDes : mainCompResDesList){ |
---|
[1207] | 200 | ComputingResource mainResource = resFactory.createComputingResource(mainExecResDes); |
---|
[477] | 201 | toExamine.push(mainExecResDes); |
---|
| 202 | resStructure.push(mainResource); |
---|
| 203 | mainCompResourceList.add(mainResource); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | while (!toExamine.isEmpty()) { |
---|
| 207 | ComputingResourceDescription parentResDesc = toExamine.pop(); |
---|
| 208 | ComputingResource parentResource = resStructure.pop(); |
---|
| 209 | toInit.add(parentResource); |
---|
[490] | 210 | compResLayers.add(parentResource.getType().getName()); |
---|
[1207] | 211 | List<ResourceDescription> childrenResDesc = parentResDesc.getChildren(); |
---|
[477] | 212 | if (childrenResDesc == null){ |
---|
| 213 | continue; |
---|
| 214 | } |
---|
| 215 | int compResCount = childrenResDesc.size(); |
---|
| 216 | for (int i = 0; i < compResCount; i++) { |
---|
| 217 | ComputingResourceDescription compResDesc = (ComputingResourceDescription) childrenResDesc.get(i); |
---|
| 218 | toExamine.push(compResDesc); |
---|
[1207] | 219 | ComputingResource resource = resFactory.createComputingResource(compResDesc); |
---|
[477] | 220 | parentResource.addChild(resource); |
---|
| 221 | resStructure.push(resource); |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | return mainCompResourceList; |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | protected Scheduler createSchedulers(schemas.Scheduler[] schedulersDef, List<ComputingResource> mainCompResourceList) throws Exception{ |
---|
| 228 | |
---|
| 229 | List<Scheduler> mainSchedulers = new ArrayList<Scheduler>(); |
---|
| 230 | |
---|
| 231 | Deque<schemas.Scheduler> toExamine = new ArrayDeque<schemas.Scheduler>(); |
---|
| 232 | Deque<Scheduler> schedulersStructure = new ArrayDeque<Scheduler>(); |
---|
| 233 | for(int i = 0; i < schedulersDef.length; i++){ |
---|
| 234 | schemas.Scheduler schedulerDef = schedulersDef[i]; |
---|
| 235 | Scheduler mainScheduler = initScheduler(schedulerDef, mainCompResourceList); |
---|
| 236 | toExamine.push(schedulerDef); |
---|
| 237 | schedulersStructure.push(mainScheduler); |
---|
| 238 | mainSchedulers.add(mainScheduler); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | while (!toExamine.isEmpty()) { |
---|
| 242 | schemas.Scheduler parentSchedulerDef = toExamine.pop(); |
---|
| 243 | |
---|
| 244 | Scheduler parentScheduler = null; |
---|
| 245 | if(!schedulersStructure.isEmpty()) |
---|
| 246 | parentScheduler = schedulersStructure.pop(); |
---|
| 247 | |
---|
| 248 | if(parentSchedulerDef.getSchedulerTypeChoice() == null) |
---|
| 249 | continue; |
---|
| 250 | int schedulerChoiceItemCount = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount(); |
---|
| 251 | for (int i = 0; i < schedulerChoiceItemCount; i++) { |
---|
| 252 | schemas.Scheduler schedulerDef = parentSchedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getScheduler(); |
---|
| 253 | if(schedulerDef == null) |
---|
| 254 | continue; |
---|
| 255 | else |
---|
| 256 | { |
---|
| 257 | Scheduler scheduler = initScheduler(schedulerDef, mainCompResourceList); |
---|
| 258 | schedulersStructure.push(scheduler); |
---|
| 259 | parentScheduler.addChild(scheduler); |
---|
| 260 | toExamine.push(schedulerDef); |
---|
| 261 | } |
---|
| 262 | } |
---|
| 263 | //necessary if children list isn't initialized in Scheduler |
---|
| 264 | //parentScheduler.init(); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | //TODO - refactor (remove - create scheduler on the basis of resource description) |
---|
| 268 | Scheduler mainScheduler = null; |
---|
[506] | 269 | if(mainSchedulers.size() == 1 /*&& mainSchedulers.get(0).get_name().equals("grid")*/){ |
---|
[477] | 270 | mainScheduler = mainSchedulers.get(0); |
---|
| 271 | } |
---|
| 272 | else{ |
---|
[1588] | 273 | throw new Exception("Wrong scheduler definition"); |
---|
[477] | 274 | } |
---|
| 275 | return mainScheduler; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | protected Scheduler initScheduler(schemas.Scheduler schedulerDef, List<ComputingResource> mainCompResourceList) throws Exception{ |
---|
| 279 | Scheduler scheduler = null; |
---|
| 280 | |
---|
| 281 | ManagedResources managedResources = null; |
---|
| 282 | //List<ComputingResource> managedCompResources = new ArrayList<ComputingResource>(); |
---|
| 283 | TaskQueueList queues = new TaskQueueList(1); |
---|
| 284 | |
---|
| 285 | if(schedulerDef.getQueues()!= null){ |
---|
| 286 | int queueCount = schedulerDef.getQueues().getQueueCount(); |
---|
| 287 | for(int i = 0; i < queueCount; i++){ |
---|
| 288 | schemas.QueueType queueDef = schedulerDef.getQueues().getQueue(i); |
---|
| 289 | TaskQueue queue = new TaskQueue(queueDef.getReservation()); |
---|
| 290 | queue.setName(queueDef.getName()); |
---|
| 291 | queue.setPriority(queueDef.getPriority()); |
---|
| 292 | queues.add(queue); |
---|
| 293 | } |
---|
| 294 | } else { |
---|
| 295 | TaskQueue queue = new TaskQueue(false); |
---|
| 296 | queues.add(queue); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | if(schedulerDef.getSchedulerTypeChoice() != null) { |
---|
| 300 | int schedulerChoiceItemCount = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItemCount(); |
---|
| 301 | for (int i = 0; i < schedulerChoiceItemCount; i++) { |
---|
| 302 | ManagedComputingResources mcr = schedulerDef.getSchedulerTypeChoice().getSchedulerTypeChoiceItem(i).getManagedComputingResources(); |
---|
| 303 | if(mcr == null) |
---|
| 304 | continue; |
---|
| 305 | |
---|
| 306 | List<String> managedCompResourcesIds = new ArrayList<String>(); |
---|
| 307 | int resourceNameCount = mcr.getResourceNameCount(); |
---|
| 308 | for(int j = 0; j < resourceNameCount; j++) { |
---|
| 309 | managedCompResourcesIds.add(mcr.getResourceName(j)); |
---|
| 310 | } |
---|
| 311 | managedResources = matchResourcesForScheduler(mainCompResourceList, managedCompResourcesIds, mcr.getInclude()); |
---|
| 312 | //managedResources = new ManagedResources(managedCompResources, new HashMap<ResourceUnitName, List<ResourceUnit>>()); |
---|
| 313 | } |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | SchedulingPlugin schedulingPlugin = null; |
---|
| 317 | if(schedulerDef.getSchedulingPlugin() != null){ |
---|
| 318 | String schedulingPluginName = schedulerDef.getSchedulingPlugin().getName(); |
---|
| 319 | schedulingPlugin = (SchedulingPlugin) InstanceFactory.createInstance(schedulingPluginName, |
---|
| 320 | SchedulingPlugin.class); |
---|
| 321 | Parameters params = EnvironmentWrapper.extractParameters(schedulerDef.getSchedulingPlugin().getParameter()); |
---|
| 322 | if(schedulerDef.getSchedulingPlugin().getFrequency() != null) |
---|
| 323 | { |
---|
| 324 | Parameter param = new Parameter("frequency"); |
---|
| 325 | StringValueWithUnit sv = new StringValueWithUnit(); |
---|
| 326 | sv.setContent(String.valueOf(schedulerDef.getSchedulingPlugin().getFrequency().getContent())); |
---|
| 327 | sv.setUnit(schedulerDef.getSchedulingPlugin().getFrequency().getUnit()); |
---|
| 328 | param.add(sv); |
---|
| 329 | |
---|
| 330 | if(params == null) |
---|
[1423] | 331 | params = new Parameters(2); |
---|
[477] | 332 | params.put("frequency", param); |
---|
| 333 | } |
---|
| 334 | schedulingPlugin.init(params); |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | //TODO - refactor (create scheduler in 1 line) |
---|
| 338 | if(schedulerDef.getClazz().equals("GridBroker")){ |
---|
[1207] | 339 | scheduler = resFactory.createScheduler(StandardResourceType.GS, "grid", schedulingPlugin, execTimeEstimationPlugin, queues, managedResources); |
---|
[477] | 340 | } else { |
---|
[1207] | 341 | scheduler = resFactory.createScheduler(StandardResourceType.LS, schedulerDef.getName(), schedulingPlugin, execTimeEstimationPlugin, queues, managedResources); |
---|
[477] | 342 | } |
---|
| 343 | return scheduler; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /*private List<ComputingResource> matchCompResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){ |
---|
| 347 | List<ComputingResource> compResources = new ArrayList<ComputingResource>(); |
---|
| 348 | for(ComputingResource mainCompRes: mainCompResourceList){ |
---|
| 349 | for(String resourceName : resIdList){ |
---|
| 350 | ComputingResource computingResource; |
---|
| 351 | try { |
---|
| 352 | if(resourceName.equals(mainCompRes.getName())) |
---|
| 353 | computingResource = mainCompRes; |
---|
| 354 | else |
---|
| 355 | computingResource = mainCompRes.getDescendantByName(resourceName); |
---|
| 356 | } catch (ResourceException e) { |
---|
| 357 | computingResource = null; |
---|
| 358 | } |
---|
| 359 | if(computingResource != null) |
---|
| 360 | { |
---|
| 361 | if(include){ |
---|
| 362 | compResources.add(computingResource); |
---|
| 363 | } else{ |
---|
| 364 | compResources.addAll(computingResource.getChildren()); |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | return compResources; |
---|
| 370 | }*/ |
---|
| 371 | |
---|
| 372 | private ManagedResources matchResourcesForScheduler(List<ComputingResource> mainCompResourceList, List<String> resIdList, boolean include){ |
---|
| 373 | |
---|
| 374 | List<ComputingResource> compResources = new ArrayList<ComputingResource>(); |
---|
| 375 | Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(); |
---|
| 376 | |
---|
| 377 | for(ComputingResource mainCompRes: mainCompResourceList){ |
---|
| 378 | for(String resourceName : resIdList){ |
---|
| 379 | ComputingResource computingResource; |
---|
[1374] | 380 | |
---|
| 381 | if(mainCompRes.getFullName().contains(resourceName)) |
---|
| 382 | computingResource = mainCompRes; |
---|
| 383 | else |
---|
| 384 | computingResource = mainCompRes.getDescendantByName(resourceName); |
---|
| 385 | |
---|
[477] | 386 | if(computingResource != null) |
---|
| 387 | { |
---|
| 388 | if(include){ |
---|
| 389 | compResources.add(computingResource); |
---|
| 390 | } else{ |
---|
| 391 | compResources.addAll(computingResource.getChildren()); |
---|
| 392 | resourceUnits = getSharedResourceUnits(computingResource); |
---|
| 393 | } |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | return new ManagedResources(compResources, resourceUnits); |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | |
---|
| 401 | private Map<ResourceUnitName, List<ResourceUnit>> getSharedResourceUnits(ComputingResource compResources){ |
---|
| 402 | Map<ResourceUnitName, List<ResourceUnit>> resourceUnits = new HashMap<ResourceUnitName, List<ResourceUnit>>(); |
---|
| 403 | List<ResourceUnit> list; |
---|
| 404 | boolean resourceNotVisited = true; |
---|
| 405 | ComputingResource parent = compResources; |
---|
| 406 | while(parent != null && resourceNotVisited){ |
---|
| 407 | Map<ResourceUnitName, List<ResourceUnit>> resUnits = parent.getResourceCharacteristic().getResourceUnits(); |
---|
| 408 | for(ResourceUnitName run : resUnits.keySet()){ |
---|
| 409 | for(ResourceUnit resUnit : resUnits.get(run)){ |
---|
| 410 | if((resourceUnits.get(run) == null)){ |
---|
| 411 | list = new ArrayList<ResourceUnit>(1); |
---|
| 412 | resourceUnits.put(resUnit.getName(), list); |
---|
| 413 | list.add(resUnit); |
---|
| 414 | } else if(!resourceUnits.get(run).contains(resUnit)){ |
---|
| 415 | list = resourceUnits.get(resUnit.getName()); |
---|
| 416 | list.add(resUnit); |
---|
| 417 | } else { |
---|
| 418 | resourceNotVisited = false; |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | parent = parent.getParent(); |
---|
| 423 | } |
---|
| 424 | return resourceUnits; |
---|
| 425 | } |
---|
| 426 | |
---|
| 427 | |
---|
[512] | 428 | class ResourceTypeComparator implements Comparator<Initializable>{ |
---|
| 429 | |
---|
| 430 | List<String> order; |
---|
| 431 | ResourceTypeComparator(List<String> order) { |
---|
| 432 | this.order = order; |
---|
| 433 | } |
---|
| 434 | public int compare(Initializable init1, Initializable init2) { |
---|
| 435 | String type1 = ((Resource)init1).getType().getName(); |
---|
| 436 | String type2 = ((Resource)init2).getType().getName(); |
---|
| 437 | if(order.indexOf(type1) > order.indexOf(type2)) |
---|
| 438 | return -1; |
---|
| 439 | else if (order.indexOf(type1) < order.indexOf(type2)) |
---|
| 440 | return 1; |
---|
| 441 | else return 0; |
---|
| 442 | |
---|
| 443 | } |
---|
| 444 | } |
---|
| 445 | |
---|
[477] | 446 | } |
---|