From 71ca8a6c0854ee479b4ae8e78430ef28b6c53d0e Mon Sep 17 00:00:00 2001 From: Manoel Campos Date: Wed, 15 Mar 2023 21:45:31 -0300 Subject: [PATCH] Refactoring Signed-off-by: Manoel Campos --- .../automation/CloudSimulation.java | 106 ++++++++---------- .../org/cloudsimplus/automation/LogUtils.java | 2 +- .../cloudsimplus/automation/PolicyLoader.java | 31 +++-- .../org/cloudsimplus/automation/Start.java | 14 +-- .../automation/YamlCloudScenarioReader.java | 10 +- .../automation/examples/Example1.java | 2 +- 6 files changed, 75 insertions(+), 90 deletions(-) diff --git a/src/main/java/org/cloudsimplus/automation/CloudSimulation.java b/src/main/java/org/cloudsimplus/automation/CloudSimulation.java index aca6d14..63a0841 100755 --- a/src/main/java/org/cloudsimplus/automation/CloudSimulation.java +++ b/src/main/java/org/cloudsimplus/automation/CloudSimulation.java @@ -24,7 +24,6 @@ import ch.qos.logback.classic.Level; import cloudreports.models.*; -import org.cloudsimplus.allocationpolicies.VmAllocationPolicy; import org.cloudsimplus.brokers.DatacenterBroker; import org.cloudsimplus.brokers.DatacenterBrokerSimple; import org.cloudsimplus.builders.tables.CloudletsTableBuilder; @@ -35,15 +34,11 @@ import org.cloudsimplus.datacenters.DatacenterSimple; import org.cloudsimplus.hosts.Host; import org.cloudsimplus.hosts.HostSimple; -import org.cloudsimplus.provisioners.ResourceProvisioner; import org.cloudsimplus.resources.DatacenterStorage; import org.cloudsimplus.resources.Pe; import org.cloudsimplus.resources.PeSimple; import org.cloudsimplus.resources.SanStorage; -import org.cloudsimplus.schedulers.cloudlet.CloudletScheduler; -import org.cloudsimplus.schedulers.vm.VmScheduler; import org.cloudsimplus.util.Log; -import org.cloudsimplus.utilizationmodels.UtilizationModel; import org.cloudsimplus.vms.Vm; import org.cloudsimplus.vms.VmSimple; @@ -98,9 +93,6 @@ public CloudSimulation(YamlCloudScenario scenario, final String label) { this.datacenters = new ArrayList<>(); this.logEnabled = false; - this.vmsToBrokerMap = new HashMap<>(); - this.cloudletsToBrokerMap = new HashMap<>(); - this.brokers = new HashMap<>(); this.vmsToBrokerMap = new HashMap<>(); this.cloudletsToBrokerMap = new HashMap<>(); @@ -115,15 +107,14 @@ public CloudSimulation(YamlCloudScenario scenario, final String label) { */ private Map createBrokers() { final int totalBrokerAmount = scenario.getCustomers().stream().mapToInt(CustomerRegistry::getAmount).sum(); - final Map list = new HashMap<>(totalBrokerAmount); - int brokerCount = 0; - for (CustomerRegistry cr: scenario.getCustomers()) { + final var map = new HashMap(totalBrokerAmount); + for (final CustomerRegistry cr: scenario.getCustomers()) { for (int i = 0; i < cr.getAmount(); i++) { - list.put(new DatacenterBrokerSimple(cloudsimplus), cr); + map.put(new DatacenterBrokerSimple(cloudsimplus), cr); } } - return list; + return map; } /** @@ -131,18 +122,18 @@ private Map createBrokers() { * * @param crMap a Map between a {@link DatacenterBroker} representing a customer in CloudSim Plus * and the {@link CustomerRegistry} object used to create VMs and Cloudlets for such a broker. - * @return the a map containing the list of created VMs for each customer (DatacenterBroker). + * @return a map containing the list of created VMs for each customer (DatacenterBroker). * @see #createBrokers() */ private Map> createVmListForAllBrokers( final Map crMap) { - final Map> vmMap = new HashMap<>(crMap.size()); + final var vmMap = new HashMap>(crMap.size()); int createdVms = 0; - for (DatacenterBroker broker : crMap.keySet()) { - List vms = createVmListForOneBroker(broker, crMap.get(broker), createdVms++); - vmMap.put(broker, vms); + for (var broker : crMap.keySet()) { + final var vmList = createVmListForOneBroker(broker, crMap.get(broker), createdVms++); + vmMap.put(broker, vmList); } return vmMap; @@ -154,7 +145,7 @@ private Map> createVmListForAllBrokers( * @param broker {@link DatacenterBroker} representing a customer in CloudSim Plus, for who VMs will be created * @param cr {@link CustomerRegistry} object used to create VMs and Cloudlets for such a broker * @param createdVms the number of VMs already created - * @return the a map containing the list of created VMs for the given customer (DatacenterBroker) + * @return a map containing the list of created VMs for the given customer (DatacenterBroker) * @see #createBrokers() */ private List createVmListForOneBroker( @@ -163,21 +154,21 @@ private List createVmListForOneBroker( final int createdVms) throws RuntimeException { final int totalVmsAmount = cr.getVms().stream().mapToInt(VmRegistry::getAmount).sum(); - final List list = new ArrayList<>(totalVmsAmount); + final var vmList = new ArrayList(totalVmsAmount); for (VmRegistry vmr : cr.getVms()) { for (int i = 0; i < vmr.getAmount(); i++) { - list.add(createVm(createdVms+i, broker, vmr)); + vmList.add(createVm(createdVms+i, broker, vmr)); } } - return list; + return vmList; } private Vm createVm(final int id, final DatacenterBroker broker, final VmRegistry vmr) throws RuntimeException { - CloudletScheduler scheduler = PolicyLoader.cloudletScheduler(vmr); + final var scheduler = PolicyLoader.cloudletScheduler(vmr); final Vm vm = new VmSimple(id, vmr.getMips(), vmr.getPes()); vm .setRam(vmr.getRam()) @@ -199,22 +190,22 @@ private Vm createVm(final int id, private Map> createCloudlets( final Map brokerRegistries) { - final Map> map = new HashMap<>(brokerRegistries.size()); + final var map = new HashMap>(brokerRegistries.size()); int createdCloudlets = 0; - for (DatacenterBroker broker : brokerRegistries.keySet()) { + for (var broker : brokerRegistries.keySet()) { final int cloudletsNum = brokerRegistries.get(broker) .getCloudlets() .stream() .mapToInt(CloudletRegistry::getAmount) .sum(); - List cloudlets = new ArrayList<>(cloudletsNum); + final var cloudletList = new ArrayList(cloudletsNum); for (CloudletRegistry up : brokerRegistries.get(broker).getCloudlets()) { for (int i = 0; i < up.getAmount(); i++) { - cloudlets.add(createCloudlet(++createdCloudlets, up, broker)); + cloudletList.add(createCloudlet(++createdCloudlets, up, broker)); } } - map.put(broker, cloudlets); + map.put(broker, cloudletList); } return map; @@ -225,11 +216,11 @@ private Cloudlet createCloudlet( final CloudletRegistry up, final DatacenterBroker broker) throws RuntimeException { - UtilizationModel cpuUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelCpu()); - UtilizationModel ramUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelRam()); - UtilizationModel bwUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelBw()); + final var cpuUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelCpu()); + final var ramUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelRam()); + final var bwUtilization = PolicyLoader.utilizationModel(up.getUtilizationModelBw()); - final Cloudlet cloudlet = new CloudletSimple(id, up.getLength(), up.getPes()); + final var cloudlet = new CloudletSimple(id, up.getLength(), up.getPes()); cloudlet .setFileSize(up.getFileSize()) .setOutputSize(up.getOutputSize()) @@ -267,7 +258,7 @@ private List createDatacenters() throws IllegalArgumentException { String datacenterName; int datacenterCount = 0; final int datacenterNumber = scenario.getDatacenters().stream().mapToInt(DatacenterRegistry::getAmount).sum(); - final List datacenterList = new ArrayList<>(datacenterNumber); + final var datacenterList = new ArrayList(datacenterNumber); for (DatacenterRegistry dcr : scenario.getDatacenters()) { int hostCount = 0; for (int i = 0; i < dcr.getAmount(); i++) { @@ -277,7 +268,7 @@ private List createDatacenters() throws IllegalArgumentException { hostCount += hostList.size(); try { - Datacenter dc = createDataCenter(datacenterName, dcr, hostList); + final var dc = createDataCenter(datacenterName, dcr, hostList); datacenterList.add(dc); } catch (Exception e) { e.printStackTrace(System.out); @@ -304,7 +295,7 @@ private List createHosts( { int hostId; final int hostNumber = dcr.getHosts().stream().mapToInt(HostRegistry::getAmount).sum(); - final List hostList = new ArrayList<>(hostNumber); + final var hostList = new ArrayList(hostNumber); for (HostRegistry hr : dcr.getHosts()) { for (int i = 0; i < hr.getAmount(); i++) { List peList = createHostProcessingElements(hr); @@ -330,10 +321,10 @@ private Datacenter createDataCenter( final List hostList) { - final List storageList = createSan(dcr); - final VmAllocationPolicy allocationPolicy = PolicyLoader.vmAllocationPolicy(dcr); + final var storageList = createSan(dcr); + final var allocationPolicy = PolicyLoader.vmAllocationPolicy(dcr); - Datacenter dc = new DatacenterSimple(cloudsimplus, hostList, allocationPolicy); + final var dc = new DatacenterSimple(cloudsimplus, hostList, allocationPolicy); dc.setSchedulingInterval(dcr.getSchedulingInterval()) .setDatacenterStorage(new DatacenterStorage(storageList)); setDatacenterCharacteristics(dc, dcr); @@ -341,11 +332,11 @@ private Datacenter createDataCenter( } private Host createHost(final int hostId, final HostRegistry hr, final List peList) throws RuntimeException { - ResourceProvisioner ramProvisioner = PolicyLoader.newResourceProvisioner(hr); - ResourceProvisioner bwProvisioner = PolicyLoader.newResourceProvisioner(hr); - VmScheduler vmScheduler = PolicyLoader.vmScheduler(hr.getVmScheduler()); + final var ramProvisioner = PolicyLoader.newResourceProvisioner(hr); + final var bwProvisioner = PolicyLoader.newResourceProvisioner(hr); + final var vmScheduler = PolicyLoader.vmScheduler(hr.getVmScheduler()); - Host host = new HostSimple(hr.getRam(), hr.getBw(), hr.getStorage(), peList); + final var host = new HostSimple(hr.getRam(), hr.getBw(), hr.getStorage(), peList); host .setRamProvisioner(ramProvisioner) .setBwProvisioner(bwProvisioner) @@ -372,13 +363,13 @@ private int generateHostId(final HostRegistry hr, final int currentHostCount) { * @see YamlCloudScenario#getDatacenters() */ private List createSan(final DatacenterRegistry dcr) throws IllegalArgumentException { - final List list = new ArrayList<>(dcr.getSans().size()); + final var storageList = new ArrayList(dcr.getSans().size()); for (SanStorageRegistry sr : dcr.getSans()) { - SanStorage san = new SanStorage(sr.getCapacity(), sr.getBandwidth(), sr.getNetworkLatency()); - list.add(san); + final var san = new SanStorage(sr.getCapacity(), sr.getBandwidth(), sr.getNetworkLatency()); + storageList.add(san); } - return list; + return storageList; } /** @@ -390,8 +381,7 @@ private List createSan(final DatacenterRegistry dcr) throws IllegalA * @param dcr The abstract DatacenterRegistry information, obtained from the YAML file, * to be used to define the Datacenter characteristics. */ - private void setDatacenterCharacteristics( - Datacenter dc, final DatacenterRegistry dcr) + private void setDatacenterCharacteristics(final Datacenter dc, final DatacenterRegistry dcr) { dc.getCharacteristics() .setArchitecture(dcr.getArchitecture()) @@ -411,12 +401,12 @@ private void setDatacenterCharacteristics( * @return Returns the list of PEs created. */ private List createHostProcessingElements(final HostRegistry hr) { - final List list = new ArrayList<>(hr.getPes()); + final var peList = new ArrayList(hr.getPes()); for (int i = 0; i < hr.getPes(); i++) { - list.add(new PeSimple(hr.getMips(), PolicyLoader.newPeProvisioner(hr))); + peList.add(new PeSimple(hr.getMips(), PolicyLoader.newPeProvisioner(hr))); } - return list; + return peList; } /** @@ -437,7 +427,7 @@ public void run() { this.vmsToBrokerMap = createVmListForAllBrokers(brokers); this.cloudletsToBrokerMap = createCloudlets(brokers); - for (DatacenterBroker broker : brokers.keySet()) { + for (final var broker : brokers.keySet()) { broker.submitVmList(vmsToBrokerMap.get(broker)); broker.submitCloudletList(cloudletsToBrokerMap.get(broker)); } @@ -445,10 +435,10 @@ public void run() { cloudsimplus.start(); if(showResults) { - for (DatacenterBroker broker : brokers.keySet()) { - List list = broker.getCloudletFinishedList(); - list.sort(comparingLong((Cloudlet c) -> c.getVm().getId()).thenComparingLong(Cloudlet::getId)); - new CloudletsTableBuilder(list) + for (final var broker : brokers.keySet()) { + final var cloudletList = broker.getCloudletFinishedList(); + cloudletList.sort(comparingLong((Cloudlet c) -> c.getVm().getId()).thenComparingLong(Cloudlet::getId)); + new CloudletsTableBuilder(cloudletList) .setTitle(broker.getName()) .build(); } @@ -607,7 +597,7 @@ public boolean isLogEnabled() { return logEnabled; } - public CloudSimulation setLogEnabled(boolean logEnabled) { + public CloudSimulation setLogEnabled(final boolean logEnabled) { this.logEnabled = logEnabled; return this; } @@ -616,7 +606,7 @@ public boolean isPrintScenariosConfiguration() { return printScenariosConfiguration; } - public CloudSimulation setPrintScenariosConfiguration(boolean printScenariosConfiguration) { + public CloudSimulation setPrintScenariosConfiguration(final boolean printScenariosConfiguration) { this.printScenariosConfiguration = printScenariosConfiguration; return this; } diff --git a/src/main/java/org/cloudsimplus/automation/LogUtils.java b/src/main/java/org/cloudsimplus/automation/LogUtils.java index f598889..974b4f8 100755 --- a/src/main/java/org/cloudsimplus/automation/LogUtils.java +++ b/src/main/java/org/cloudsimplus/automation/LogUtils.java @@ -36,7 +36,7 @@ public class LogUtils { private static String colSeparator="|\t"; /** - * Print a array of objects like a table. + * Print an array of objects like a table. * @param captions The captions of the table * @param dataArray The data to be printed. * @see LogUtils#printCaptions(java.lang.String[]) diff --git a/src/main/java/org/cloudsimplus/automation/PolicyLoader.java b/src/main/java/org/cloudsimplus/automation/PolicyLoader.java index 4283af6..074ad4b 100755 --- a/src/main/java/org/cloudsimplus/automation/PolicyLoader.java +++ b/src/main/java/org/cloudsimplus/automation/PolicyLoader.java @@ -36,7 +36,6 @@ import org.cloudsimplus.schedulers.vm.VmScheduler; import org.cloudsimplus.utilizationmodels.UtilizationModel; -import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.HashMap; import java.util.Map; @@ -93,9 +92,9 @@ private static Class loadClass(final String fullClassName){ public static VmScheduler vmScheduler(final String classSuffix) throws RuntimeException { try { final String className = generateFullClassName(PKG+".schedulers.vm","VmScheduler", classSuffix); - Class klass = PolicyLoader.loadClass(className); - Constructor cons = klass.getConstructor(new Class[]{}); - return (VmScheduler) cons.newInstance(); + final Class klass = PolicyLoader.loadClass(className); + final var constructor = klass.getConstructor(new Class[]{}); + return constructor.newInstance(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(PolicyLoader.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); @@ -121,9 +120,9 @@ private static T resourceProvisioner( final String classPrefix, final String classSufix) throws RuntimeException { try { final String className = generateFullProvisionerClassName(classPrefix, classSufix); - Class klass = PolicyLoader.loadClass(className); - Constructor cons = klass.getConstructor(new Class[]{}); - return (T)cons.newInstance(); + final Class klass = PolicyLoader.loadClass(className); + final var constructor = klass.getConstructor(new Class[]{}); + return (T)constructor.newInstance(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(PolicyLoader.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); @@ -141,9 +140,9 @@ public static PeProvisioner newPeProvisioner(final HostRegistry hr) throws Runti public static VmAllocationPolicy vmAllocationPolicy(final DatacenterRegistry dcr) throws RuntimeException { try { final String className = generateFullClassName(PKG+".allocationpolicies","VmAllocationPolicy", dcr.getVmAllocationPolicy()); - Class klass = PolicyLoader.loadClass(className); - Constructor cons = klass.getConstructor(new Class[]{}); - return (VmAllocationPolicy) cons.newInstance(); + final Class klass = PolicyLoader.loadClass(className); + final var constructor = klass.getConstructor(new Class[]{}); + return constructor.newInstance(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(PolicyLoader.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); @@ -153,9 +152,9 @@ public static VmAllocationPolicy vmAllocationPolicy(final DatacenterRegistry dcr public static CloudletScheduler cloudletScheduler(final VmRegistry vmr) throws RuntimeException { try { final String className = generateFullClassName(PKG+".schedulers.cloudlet","CloudletScheduler", vmr.getCloudletScheduler()); - Class klass = PolicyLoader.loadClass(className); - Constructor cons = klass.getConstructor(); - return (CloudletScheduler) cons.newInstance(); + final Class klass = PolicyLoader.loadClass(className); + final var constructor = klass.getConstructor(); + return constructor.newInstance(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(PolicyLoader.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); @@ -174,9 +173,9 @@ private static String generateFullProvisionerClassName(String classPrefix, Strin public static UtilizationModel utilizationModel(final String classSuffix) throws RuntimeException { try { final String className = generateFullClassName(PKG+".utilizationmodels", "UtilizationModel", classSuffix); - Class klass = PolicyLoader.loadClass(className); - Constructor cons = klass.getConstructor(); - return (UtilizationModel) cons.newInstance(); + final Class klass = PolicyLoader.loadClass(className); + final var constructor = klass.getConstructor(); + return constructor.newInstance(); } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { Logger.getLogger(PolicyLoader.class.getName()).log(Level.SEVERE, null, ex); throw new RuntimeException(ex); diff --git a/src/main/java/org/cloudsimplus/automation/Start.java b/src/main/java/org/cloudsimplus/automation/Start.java index f620828..cd635d0 100755 --- a/src/main/java/org/cloudsimplus/automation/Start.java +++ b/src/main/java/org/cloudsimplus/automation/Start.java @@ -27,7 +27,6 @@ import org.cloudsimplus.core.CloudSimPlus; import java.io.FileNotFoundException; -import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -62,8 +61,7 @@ private Start(final String[] args){ return; } - this.reader = - new YamlCloudScenarioReader(getFileNameFromCommandLine()); + this.reader = new YamlCloudScenarioReader(getFileNameFromCommandLine()); if(reader.getScenarios().isEmpty()) { System.err.println("Your YAML file is empty.\n"); } @@ -77,11 +75,10 @@ private Start(final String[] args){ } catch (Exception e){ System.err.printf("An unexpected error happened: %s\n", e.getMessage()); } - } private void showUsageHelp() { - HelpFormatter formatter = new HelpFormatter(); + final var formatter = new HelpFormatter(); formatter.printHelp(getApplicationStartCmd() +" [options] YamlFilePath", options); } @@ -91,7 +88,7 @@ private void showUsageHelp() { */ private boolean parseCommandLineOptions(String[] args) throws ParseException { options = new Options(); - Option option = new Option("v", "Enables CloudSim Plus Log (disabled by default)"); + final var option = new Option("v", "Enables CloudSim Plus Log (disabled by default)"); option.setLongOpt("verbose"); options.addOption(option); @@ -99,7 +96,7 @@ private boolean parseCommandLineOptions(String[] args) throws ParseException { options.addOption("h", "Show usage help"); options.addOption("c", "Print scenario configuration"); - CommandLineParser parser = new DefaultParser(); + final var parser = new DefaultParser(); this.cmd = parser.parse(options, args); /* @@ -130,7 +127,7 @@ private String getApplicationStartCmd() { } private String regexMatch(final String regex, final String text) { - final Matcher matcher = Pattern.compile(regex).matcher(text); + final var matcher = Pattern.compile(regex).matcher(text); if(matcher.find()){ return matcher.group(1); } @@ -189,6 +186,5 @@ private boolean isToEnableLog() { private boolean isToPrintScenariosConfiguration() { return cmd.hasOption("c"); } - } diff --git a/src/main/java/org/cloudsimplus/automation/YamlCloudScenarioReader.java b/src/main/java/org/cloudsimplus/automation/YamlCloudScenarioReader.java index a32cfd9..62c8982 100755 --- a/src/main/java/org/cloudsimplus/automation/YamlCloudScenarioReader.java +++ b/src/main/java/org/cloudsimplus/automation/YamlCloudScenarioReader.java @@ -79,15 +79,15 @@ public YamlCloudScenarioReader(final String filePath) throws IllegalArgumentExce * @throws YamlException when there is any error parsing the YAML file. */ private List readYamlFile() throws FileNotFoundException, YamlException { - final List scenarios = new ArrayList(); - final YamlReader reader = createYamlReader(); + final var scenariosList = new ArrayList(); + final var reader = createYamlReader(); YamlCloudScenario scenario; while ((scenario = reader.read(YamlCloudScenario.class)) != null) { - scenarios.add(scenario); + scenariosList.add(scenario); } - return scenarios; + return scenariosList; } /** @@ -98,7 +98,7 @@ private List readYamlFile() throws FileNotFoundException, Yam * @throws FileNotFoundException */ private YamlReader createYamlReader() throws FileNotFoundException { - final YamlReader reader = new YamlReader(new FileReader(file)); + final var reader = new YamlReader(new FileReader(file)); final YamlConfig cfg = reader.getConfig(); //Defines the aliases in the YAML file that refers to specific java Classes. diff --git a/src/main/java/org/cloudsimplus/automation/examples/Example1.java b/src/main/java/org/cloudsimplus/automation/examples/Example1.java index 795c02d..9efdb81 100755 --- a/src/main/java/org/cloudsimplus/automation/examples/Example1.java +++ b/src/main/java/org/cloudsimplus/automation/examples/Example1.java @@ -25,7 +25,7 @@ private Example1(){ final String yamlFilePath = ResourceLoader.getResourcePath(getClass(), "CloudEnvironment1.yml"); try { //Loads the YAML file containing 1 or more simulation scenarios. - final YamlCloudScenarioReader reader = new YamlCloudScenarioReader(yamlFilePath); + final var reader = new YamlCloudScenarioReader(yamlFilePath); //Gets the list or parsed scenarios. final List simulationScenarios = reader.getScenarios(); //For each existing scenario, creates and runs it in CloudSim Plus, printing results.