/**
   * 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;
  }
  @Override
  public void run() {

    int size = getCloudletList().size();

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

      Cloudlet cloudlet = (Cloudlet) getCloudletList().get(i);

      int vmSize = getVmList().size();
      CondorVM closestVm = null; // (CondorVM)getVmList().get(0);
      double minTime = Double.MAX_VALUE;
      for (int j = 0; j < vmSize; j++) {
        CondorVM vm = (CondorVM) getVmList().get(j);
        if (vm.getState() == WorkflowSimTags.VM_STATUS_IDLE) {
          Job job = (Job) cloudlet;
          double time = dataTransferTime(job.getFileList(), cloudlet, vm.getId());
          if (time < minTime) {
            minTime = time;
            closestVm = vm;
          }
        }
      }

      if (closestVm != null) {
        closestVm.setState(WorkflowSimTags.VM_STATUS_BUSY);
        cloudlet.setVmId(closestVm.getId());
        getScheduledList().add(cloudlet);
      }
    }
  }
  /**
   * 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;
  }
  @Override
  public void run() {
    // Round Robin

    for (Iterator it = getCloudletList().iterator(); it.hasNext(); ) {
      Cloudlet cloudlet = (Cloudlet) it.next();
      boolean stillHasVm = false;
      for (Iterator itc = getVmList().iterator(); itc.hasNext(); ) {

        CondorVM vm = (CondorVM) itc.next();
        if (vm.getState() == WorkflowSimTags.VM_STATUS_IDLE) {
          stillHasVm = true;
          vm.setState(WorkflowSimTags.VM_STATUS_BUSY);
          cloudlet.setVmId(vm.getId());
          this.scheduledList.add(cloudlet);
          break;
        }
      }
      // no vm available
      if (!stillHasVm) {
        break;
      }
    }
  }