コード例 #1
0
  /**
   * Returns a list of virtual machines builded from a virtual system list
   *
   * @param vsc VirtualSystemCollectionDto list to transform
   * @return list of virtual machines builded from a virtual system list
   */
  private List<VirtualMachine> transportVSCollectionToVMs(final VirtualSystemCollectionDto vsc) {
    long MEGABYTE = 1024L * 1024L;

    List<VirtualMachine> vms = new ArrayList<VirtualMachine>();

    for (VirtualSystemDto vs : vsc.getVirtualSystems()) {
      VirtualMachine vm =
          new VirtualMachine(
              vs.getName(),
              null,
              null,
              null,
              null,
              UUID.fromString(vs.getUuid()),
              VirtualMachine.NOT_MANAGED);

      vm.setCpu(new Long(vs.getCpu()).intValue());
      vm.setRam(new Long(vs.getRam() / MEGABYTE).intValue());
      vm.setVdrpPort(new Long(vs.getVport()).intValue());
      vm.setState(VirtualMachineState.valueOf(vs.getStatus().value()));
      for (ResourceType rt : vs.getResources()) {
        if (rt.getResourceType().equals(ResourceEnumType.STORAGE_DISK)) {
          long bytesHD = rt.getUnits();
          vm.setHdInBytes(bytesHD);

          if (StringUtils.hasText(rt.getAddress()) && StringUtils.hasText(rt.getConnection())) {
            Datastore ds = new Datastore();
            ds.setDirectory(rt.getAddress());
            ds.setRootPath(rt.getConnection());
            ds.setName(rt.getElementName());
            vm.setDatastore(ds);
          }

          if (rt.getResourceSubType() != null) {

            VirtualImage vi = new VirtualImage(null);
            VirtualDiskEnumType diskFormatType =
                VirtualDiskEnumType.fromValue(rt.getResourceSubType().toString());
            if (diskFormatType.equals(VirtualDiskEnumType.STATEFUL)) {
              vi.setStateful(1);
            }
            vi.setDiskFormatType(DiskFormatType.fromURI(diskFormatType.value()));
            vm.setVirtualImage(vi);
          }
        }
      }

      vms.add(vm);
    }

    return vms;
  }
コード例 #2
0
  /**
   * Converts the type of {@link HostDto} from the nodecollector response to the {@link Machine}
   * type for the API model.
   *
   * @param datacenter datacenter where the machine will be assigned.
   * @param host hostDto object.
   * @return a {@link Machine} entity.
   */
  protected Machine hostToMachine(final Datacenter datacenter, final HostDto host) {

    final Integer MEGABYTE = 1048576;

    int ram = (int) (host.getRam() / MEGABYTE);
    int cpus = (int) host.getCpu();
    Machine machine =
        new Machine(
            datacenter,
            host.getName(),
            host.getDescription(),
            ram,
            0,
            cpus,
            0,
            0,
            transfromToState(host.getStatus()),
            "");

    // Long totalStorage = 0L;
    String switches = "";
    for (ResourceType resource : host.getResources()) {
      // TODO remove code
      if (resource.getResourceType().equals(ResourceEnumType.STORAGE_DISK)) {
        Datastore datastore =
            new Datastore(machine, resource.getElementName(), resource.getAddress(), "");
        datastore.setEnabled(Boolean.FALSE);
        datastore.setSize(resource.getUnits());
        datastore.setUsedSize(resource.getUnits() - resource.getAvailableUnits());
        if (resource.getConnection() == null) {
          datastore.setDatastoreUUID(UUID.randomUUID().toString());
        } else {
          datastore.setDatastoreUUID(resource.getConnection());
        }
        // totalStorage += datastore.getSize();
      } else {
        if (resource.getResourceType().equals(ResourceEnumType.NETWORK_INTERFACE)) {
          switches = switches.concat(resource.getElementName()) + "/";
          machine.getListOfMacs().add(resource.getAddress());
        }
      }
    }

    switches = switches.substring(0, switches.lastIndexOf('/'));
    machine.setVirtualSwitch(switches);
    return machine;
  }