/**
   * Creates instances of CloudSim's Cloudlet class from a customer's utilization profile.
   *
   * @param ugr the utilization profile.
   * @param brokerId the id of the broker that owns the cloudlets.
   * @param numOfVms the number of virtual machines.
   * @return a list of Cloudlet instances.
   * @since 1.0
   */
  static List<Cloudlet> createCloudlets(UtilizationProfile ugr, int brokerId, long numOfVms)
      throws IOException, ServiceDeniedException {
    List<Cloudlet> list = new ArrayList<Cloudlet>();

    for (int i = 0; i < numOfVms; i++) {

      Optional<UtilizationModel> cpu =
          UTILIZATION_MODEL.getExtensionInstanceByName(ugr.getUtilizationModelCpuAlias());

      if (!cpu.isPresent()) {
        Dialog.showErrorMessage(
            null,
            format(
                "Error on loading the CPU utilization model [%s]",
                ugr.getUtilizationModelCpuAlias()));
        return null;
      }

      Optional<UtilizationModel> ram =
          UTILIZATION_MODEL.getExtensionInstanceByName(ugr.getUtilizationModelRamAlias());

      if (!ram.isPresent()) {
        Dialog.showErrorMessage(
            null,
            format(
                "Error on loading the RAM utilization model [%s]",
                ugr.getUtilizationModelRamAlias()));
        return null;
      }

      Optional<UtilizationModel> bw =
          UTILIZATION_MODEL.getExtensionInstanceByName(ugr.getUtilizationModelBwAlias());

      if (!bw.isPresent()) {
        Dialog.showErrorMessage(
            null,
            format(
                "Error on loading the bandwidth utilization model [%s]",
                ugr.getUtilizationModelBwAlias()));
        return null;
      }

      Cloudlet cloudlet =
          new Cloudlet(
              i,
              (long) ((long) ugr.getLength() * RandomNumberGenerator.getRandomNumbers(1).get(0)),
              ugr.getCloudletsPesNumber(),
              ugr.getFileSize(),
              ugr.getOutputSize(),
              cpu.get(),
              ram.get(),
              bw.get());

      cloudlet.setUserId(brokerId);
      cloudlet.setVmId(i);
      list.add(cloudlet);
    }

    return list;
  }
  /**
   * Creates the cloudlet list.
   *
   * @param brokerId the broker id
   * @param cloudletsNumber the cloudlets number
   * @return the list< cloudlet>
   */
  public static List<Cloudlet> createCloudletList(int brokerId, int cloudletsNumber) {
    List<Cloudlet> list = new ArrayList<Cloudlet>();

    long fileSize = 300;
    long outputSize = 300;
    long seed = RandomConstants.CLOUDLET_UTILIZATION_SEED;
    UtilizationModel utilizationModelNull = new UtilizationModelNull();

    for (int i = 0; i < cloudletsNumber; i++) {
      Cloudlet cloudlet = null;
      if (seed == -1) {
        cloudlet =
            new Cloudlet(
                i,
                Constants.CLOUDLET_LENGTH,
                Constants.CLOUDLET_PES,
                fileSize,
                outputSize,
                new UtilizationModelStochastic(),
                utilizationModelNull,
                utilizationModelNull);
      } else {
        cloudlet =
            new Cloudlet(
                i,
                Constants.CLOUDLET_LENGTH,
                Constants.CLOUDLET_PES,
                fileSize,
                outputSize,
                new UtilizationModelStochastic(seed * i),
                utilizationModelNull,
                utilizationModelNull);
      }
      cloudlet.setUserId(brokerId);
      cloudlet.setVmId(i);
      list.add(cloudlet);
    }

    return list;
  }
  /** Creates main() to run this example */
  public static void main(String[] args) {

    Log.printLine("Starting CloudSimExample2...");

    try {
      // First step: Initialize the CloudSim package. It should be called
      // before creating any entities.
      int num_user = 8; // number of cloud users
      Calendar calendar = Calendar.getInstance();
      boolean trace_flag = false; // mean trace events

      // Initialize the CloudSim library
      CloudSim.init(num_user, calendar, trace_flag);

      // Second step: Create Datacenters
      // Datacenters are the resource providers in CloudSim. We need at list one of them to run a
      // CloudSim simulation
      Datacenter datacenter0 = createDatacenter("Datacenter_0");
      Datacenter datacenter1 = createDatacenter("Datacenter_1");
      Datacenter datacenter2 = createDatacenter("Datacenter_2");
      Datacenter datacenter3 = createDatacenter("Datacenter_3");

      // Third step: Create Broker
      DatacenterBroker broker = createBroker();
      int brokerId = broker.getId();

      // Fourth step: Create one virtual machine
      vmlist = new ArrayList<Vm>();

      // VM description
      int vmid = 0;
      int mips = 250;
      double costpersec = 20.0;
      long size = 10000; // image size (MB)
      int ram = 512; // vm memory (MB)
      long bw = 1000;
      int pesNumber = 1; // number of cpus
      String vmm = "Xen"; // VMM name

      // create two VMs
      Vm vm1 =
          new Vm(
              vmid,
              brokerId,
              mips,
              costpersec,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());
      costpersec = 28.0;
      mips = 450;
      ram = 768;
      bw = 512;
      vmid++;
      Vm vm2 =
          new Vm(
              vmid,
              brokerId,
              mips,
              costpersec,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());
      costpersec = 25.0;
      mips = 580;
      ram = 256;
      bw = 768;
      vmid++;
      Vm vm3 =
          new Vm(
              vmid,
              brokerId,
              mips,
              costpersec,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());
      costpersec = 30.0;
      mips = 300;
      ram = 768;
      bw = 512;
      vmid++;
      Vm vm4 =
          new Vm(
              vmid,
              brokerId,
              mips,
              costpersec,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());
      costpersec = 22.0;
      mips = 280;
      ram = 1000;
      bw = 128;
      vmid++;
      Vm vm5 =
          new Vm(
              vmid,
              brokerId,
              mips,
              costpersec,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());
      vmid++;
      costpersec = 50.0;
      mips = 220;
      ram = 128;
      bw = 128;

      //                        Vm vm6 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw,
      // size, vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	costpersec=22.0;
      //                        mips =280;
      //                        ram = 2048;
      //                        bw = 64;
      //                        Vm vm7 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw,
      // size, vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm8 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm9 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm10 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm11 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm12 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm13 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm14 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm15 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());
      //                        vmid++;
      //	            	Vm vm16 = new Vm(vmid, brokerId, mips,costpersec, pesNumber, ram, bw, size,
      // vmm, new CloudletSchedulerTimeShared());

      // add the VMs to the vmList
      vmlist.add(vm1);
      vmlist.add(vm2);
      vmlist.add(vm3);
      vmlist.add(vm4);
      vmlist.add(vm5);

      //                        vmlist.add(vm6);
      //                        vmlist.add(vm7);
      //                        vmlist.add(vm9);
      //                        vmlist.add(vm10);
      //                        vmlist.add(vm11);
      //                        vmlist.add(vm12);
      //                        vmlist.add(vm13);
      //                        vmlist.add(vm14);
      //                        vmlist.add(vm15);
      //                        vmlist.add(vm16);

      // submit vm list to the broker
      broker.submitVmList(vmlist);

      // Fifth step: Create two Cloudlets
      cloudletList = new ArrayList<Cloudlet>();

      // Cloudlet properties
      int id = 0;
      pesNumber = 1;
      long length = 2500;
      long fileSize = 300;
      long outputSize = 450;
      UtilizationModel utilizationModel = new UtilizationModelFull();
      double wt = 1.0;
      double wm = 0;

      Cloudlet cloudlet1 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet1.setUserId(brokerId);

      id++;
      pesNumber = 1;
      length = 2800;
      fileSize = 600;
      outputSize = 600;
      Cloudlet cloudlet2 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet2.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 1250;
      fileSize = 800;
      outputSize = 800;
      Cloudlet cloudlet3 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet3.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 2480;
      fileSize = 300;
      outputSize = 300;
      Cloudlet cloudlet4 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet4.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 7000;
      fileSize = 500;
      outputSize = 500;
      Cloudlet cloudlet5 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet5.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 1500;
      fileSize = 500;
      outputSize = 500;
      Cloudlet cloudlet6 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet6.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 800;
      fileSize = 500;
      outputSize = 500;
      Cloudlet cloudlet7 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet7.setUserId(brokerId);
      id++;
      pesNumber = 1;
      length = 7500;
      fileSize = 500;
      outputSize = 500;
      Cloudlet cloudlet8 =
          new Cloudlet(
              wt,
              wm,
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet8.setUserId(brokerId);

      // add the cloudlets to the list
      cloudletList.add(cloudlet1);
      cloudletList.add(cloudlet2);
      cloudletList.add(cloudlet3);
      cloudletList.add(cloudlet4);
      cloudletList.add(cloudlet5);

      cloudletList.add(cloudlet6);
      cloudletList.add(cloudlet7);
      cloudletList.add(cloudlet8);

      // submit cloudlet list to the broker
      broker.submitCloudletList(cloudletList);

      // bind the cloudlets to the vms. This way, the broker
      // will submit the bound cloudlets only to the specific VM

      // broker.bindCloudletToVm(cloudlet1.getCloudletId(),vm1.getId());
      // broker.bindCloudletToVm(cloudlet2.getCloudletId(),vm2.getId());

      // Sixth step: Starts the simulation
      CloudSim.startSimulation();

      // Final step: Print results when simulation is over
      List<Cloudlet> newList = broker.getCloudletReceivedList();

      CloudSim.stopSimulation();

      printCloudletList(newList);

      // Print the debt of each user to each datacenter
      // datacenter0.printDebts();

      Log.printLine("CloudSimExample2 finished!");
    } catch (Exception e) {
      e.printStackTrace();
      Log.printLine("The simulation has been terminated due to an unexpected error");
    }
  }
  /** Creates main() to run this example */
  public static void main(String[] args) {

    Log.printLine("Starting NetworkExample3...");

    try {
      // First step: Initialize the CloudSim package. It should be called
      // before creating any entities.
      int num_user = 2; // number of cloud users
      Calendar calendar = Calendar.getInstance();
      boolean trace_flag = false; // mean trace events

      // Initialize the CloudSim library
      CloudSim.init(num_user, calendar, trace_flag);

      // Second step: Create Datacenters
      // Datacenters are the resource providers in CloudSim. We need at list one of them to run a
      // CloudSim simulation
      Datacenter datacenter0 = createDatacenter("Datacenter_0");
      Datacenter datacenter1 = createDatacenter("Datacenter_1");

      // Third step: Create Brokers
      DatacenterBroker broker1 = createBroker(1);
      int brokerId1 = broker1.getId();

      DatacenterBroker broker2 = createBroker(2);
      int brokerId2 = broker2.getId();

      // Fourth step: Create one virtual machine for each broker/user
      vmlist1 = new ArrayList<Vm>();
      vmlist2 = new ArrayList<Vm>();

      // VM description
      int vmid = 0;
      long size = 10000; // image size (MB)
      int mips = 250;
      int ram = 512; // vm memory (MB)
      long bw = 1000;
      int pesNumber = 1; // number of cpus
      String vmm = "Xen"; // VMM name

      // create two VMs: the first one belongs to user1
      Vm vm1 =
          new Vm(
              vmid,
              brokerId1,
              mips,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());

      // the second VM: this one belongs to user2
      Vm vm2 =
          new Vm(
              vmid,
              brokerId2,
              mips,
              pesNumber,
              ram,
              bw,
              size,
              vmm,
              new CloudletSchedulerTimeShared());

      // add the VMs to the vmlists
      vmlist1.add(vm1);
      vmlist2.add(vm2);

      // submit vm list to the broker
      broker1.submitVmList(vmlist1);
      broker2.submitVmList(vmlist2);

      // Fifth step: Create two Cloudlets
      cloudletList1 = new ArrayList<Cloudlet>();
      cloudletList2 = new ArrayList<Cloudlet>();

      // Cloudlet properties
      int id = 0;
      long length = 40000;
      long fileSize = 300;
      long outputSize = 300;
      UtilizationModel utilizationModel = new UtilizationModelFull();

      Cloudlet cloudlet1 =
          new Cloudlet(
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet1.setUserId(brokerId1);

      Cloudlet cloudlet2 =
          new Cloudlet(
              id,
              length,
              pesNumber,
              fileSize,
              outputSize,
              utilizationModel,
              utilizationModel,
              utilizationModel);
      cloudlet2.setUserId(brokerId2);

      // add the cloudlets to the lists: each cloudlet belongs to one user
      cloudletList1.add(cloudlet1);
      cloudletList2.add(cloudlet2);

      // submit cloudlet list to the brokers
      broker1.submitCloudletList(cloudletList1);
      broker2.submitCloudletList(cloudletList2);

      // Sixth step: configure network
      // load the network topology file
      NetworkTopology.buildNetworkTopology("topology.brite");

      // maps CloudSim entities to BRITE entities
      // Datacenter0 will correspond to BRITE node 0
      int briteNode = 0;
      NetworkTopology.mapNode(datacenter0.getId(), briteNode);

      // Datacenter1 will correspond to BRITE node 2
      briteNode = 2;
      NetworkTopology.mapNode(datacenter1.getId(), briteNode);

      // Broker1 will correspond to BRITE node 3
      briteNode = 3;
      NetworkTopology.mapNode(broker1.getId(), briteNode);

      // Broker2 will correspond to BRITE node 4
      briteNode = 4;
      NetworkTopology.mapNode(broker2.getId(), briteNode);

      // Sixth step: Starts the simulation
      CloudSim.startSimulation();

      // Final step: Print results when simulation is over
      List<Cloudlet> newList1 = broker1.getCloudletReceivedList();
      List<Cloudlet> newList2 = broker2.getCloudletReceivedList();

      CloudSim.stopSimulation();

      Log.print("=============> User " + brokerId1 + "    ");
      printCloudletList(newList1);

      Log.print("=============> User " + brokerId2 + "    ");
      printCloudletList(newList2);

      // Print the debt of each user to each datacenter
      datacenter0.printDebts();
      datacenter1.printDebts();

      Log.printLine("NetworkExample3 finished!");
    } catch (Exception e) {
      e.printStackTrace();
      Log.printLine("Unwanted errors happen");
    }
  }