private Runnable createImportTask(ImportOvfRequest req, Operation patch) {
    return () -> {
      URI ovfUri;

      HttpClient client = OvfRetriever.newInsecureClient();
      OvfRetriever retriever = new OvfRetriever(client);
      OvfParser parser = new OvfParser();

      Document doc;
      try {
        ovfUri = retriever.downloadIfOva(req.ovfUri);
        doc = parser.retrieveDescriptor(ovfUri);
      } catch (Exception e) {
        patch.fail(e);
        return;
      }

      CustomProperties.of(req.template).put(OvfParser.PROP_OVF_URI, ovfUri);

      if (!Objects.equals(ovfUri, req.ovfUri)) {
        // this is an ova archive, store original OVA uri
        CustomProperties.of(req.template).put(OvfParser.PROP_OVF_ARCHIVE_URI, req.ovfUri);
      }

      List<ComputeDescription> ovfDescriptions = parser.parse(doc, req.template);
      Stream<Operation> opStream =
          ovfDescriptions
              .stream()
              .map(
                  desc ->
                      Operation.createPost(getHost(), ComputeDescriptionService.FACTORY_LINK)
                          .setBodyNoCloning(desc));

      OperationJoin join =
          OperationJoin.create()
              .setOperations(opStream)
              .setCompletion(
                  (os, es) -> {
                    if (es != null && !es.isEmpty()) {
                      patch.fail(es.values().iterator().next());
                    } else {
                      patch.complete();
                    }
                  });

      join.sendWith(this);
    };
  }
Esempio n. 2
0
  private void buildImageReference() {
    XmlNodeList list = selectNodes(_document, "//*/File", _xmlNS);
    for (XmlNode node : list) {
      // If the disk storage type is Cinder then override the disk image with Cinder object,
      // otherwise use the disk image.
      DiskImage disk = new DiskImage();

      // If the OVF is old and does not contain any storage type reference then we assume we can
      // only have disk image.
      if (node.attributes.get("ovf:disk_storage_type") != null) {
        String diskStorageType = node.attributes.get("ovf:disk_storage_type").getValue();
        if (diskStorageType != null && diskStorageType.equals(DiskStorageType.CINDER.name())) {
          disk = new CinderDisk();
          if (node.attributes.get("ovf:cinder_volume_type") != null) {
            String cinderVolumeType = node.attributes.get("ovf:cinder_volume_type").getValue();
            disk.setCinderVolumeType(cinderVolumeType);
          }
        }
      }
      disk.setImageId(new Guid(node.attributes.get("ovf:id").getValue()));
      disk.setId(
          OvfParser.getImageGroupIdFromImageFile(node.attributes.get("ovf:href").getValue()));
      // Default values:
      disk.setActive(true);
      disk.setImageStatus(ImageStatus.OK);
      disk.setDescription(node.attributes.get("ovf:description").getValue());

      disk.setDiskVmElements(
          Collections.singletonList(new DiskVmElement(disk.getId(), vmBase.getId())));

      _images.add(disk);
    }
  }
Esempio n. 3
0
  @Override
  protected void readDiskImageItem(XmlNode node) {
    final Guid guid = new Guid(node.SelectSingleNode("rasd:InstanceId", _xmlNS).innerText);

    DiskImage image =
        LinqUtils.firstOrNull(
            _images,
            new Predicate<DiskImage>() {
              @Override
              public boolean eval(DiskImage diskImage) {
                return diskImage.getImageId().equals(guid);
              }
            });
    image.setId(
        OvfParser.GetImageGrupIdFromImageFile(
            node.SelectSingleNode("rasd:HostResource", _xmlNS).innerText));
    if (StringUtils.isNotEmpty(node.SelectSingleNode("rasd:Parent", _xmlNS).innerText)) {
      image.setParentId(new Guid(node.SelectSingleNode("rasd:Parent", _xmlNS).innerText));
    }
    if (StringUtils.isNotEmpty(node.SelectSingleNode("rasd:Template", _xmlNS).innerText)) {
      image.setImageTemplateId(new Guid(node.SelectSingleNode("rasd:Template", _xmlNS).innerText));
    }
    image.setAppList(node.SelectSingleNode("rasd:ApplicationList", _xmlNS).innerText);
    if (StringUtils.isNotEmpty(node.SelectSingleNode("rasd:StorageId", _xmlNS).innerText)) {
      image.setStorageIds(
          new ArrayList<Guid>(
              Arrays.asList(new Guid(node.SelectSingleNode("rasd:StorageId", _xmlNS).innerText))));
    }
    if (StringUtils.isNotEmpty(node.SelectSingleNode("rasd:StoragePoolId", _xmlNS).innerText)) {
      image.setStoragePoolId(
          new Guid(node.SelectSingleNode("rasd:StoragePoolId", _xmlNS).innerText));
    }
    final Date creationDate =
        OvfParser.UtcDateStringToLocaDate(
            node.SelectSingleNode("rasd:CreationDate", _xmlNS).innerText);
    if (creationDate != null) {
      image.setCreationDate(creationDate);
    }
    final Date lastModified =
        OvfParser.UtcDateStringToLocaDate(
            node.SelectSingleNode("rasd:LastModified", _xmlNS).innerText);
    if (lastModified != null) {
      image.setLastModified(lastModified);
    }
    readManagedVmDevice(node, image.getId());
  }
Esempio n. 4
0
  protected void readGeneralData() {
    XmlNode content = selectSingleNode(_document, "//*/Content");
    XmlNode node;
    vmBase.setVmInit(new VmInit());

    // set ovf version to the ovf object
    vmBase.setOvfVersion(getVersion());

    node = selectSingleNode(content, OvfProperties.DESCRIPTION);
    if (node != null) {
      vmBase.setDescription(node.innerText);
    }

    node = selectSingleNode(content, OvfProperties.COMMENT);
    if (node != null) {
      vmBase.setComment(node.innerText);
    }

    node = selectSingleNode(content, OvfProperties.DOMAIN);
    if (node != null) {
      vmBase.getVmInit().setDomain(node.innerText);
    }

    node = selectSingleNode(content, OvfProperties.CREATION_DATE);
    if (node != null) {
      Date creationDate = OvfParser.utcDateStringToLocaDate(node.innerText);
      if (creationDate != null) {
        vmBase.setCreationDate(creationDate);
      }
    }

    node = selectSingleNode(content, OvfProperties.EXPORT_DATE);
    if (node != null) {
      Date exportDate = OvfParser.utcDateStringToLocaDate(node.innerText);
      if (exportDate != null) {
        vmBase.setExportDate(exportDate);
      }
    }

    node = selectSingleNode(content, OvfProperties.DEFAULT_BOOT_SEQUENCE);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setDefaultBootSequence(BootSequence.forValue(Integer.parseInt(node.innerText)));
      }
    }

    node = selectSingleNode(content, OvfProperties.INITRD_URL);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setInitrdUrl(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.KERNEL_URL);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setKernelUrl(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.KERNEL_PARAMS);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setKernelParams(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.GENERATION);
    if (node != null) {
      vmBase.setDbGeneration(Long.parseLong(node.innerText));
    } else {
      vmBase.setDbGeneration(1L);
    }

    node = selectSingleNode(content, OvfProperties.CUSTOM_COMPATIBILITY_VERSION);
    if (node != null) {
      vmBase.setCustomCompatibilityVersion(new Version(node.innerText));
    }

    Version originVersion = new Version(getVersion()); // the originating ENGINE version
    node = selectSingleNode(content, OvfProperties.CLUSTER_COMPATIBILITY_VERSION);
    if (node != null) {
      originVersion = new Version(node.innerText);
    }
    vmBase.setClusterCompatibilityVersionOrigin(originVersion);

    // Note: the fetching of 'default display type' should happen before reading
    // the hardware section
    node = selectSingleNode(content, getDefaultDisplayTypeStringRepresentation());
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setDefaultDisplayType(DisplayType.forValue(Integer.parseInt(node.innerText)));
      }
    }

    XmlNodeList list = selectNodes(content, "Section");

    if (list != null) {
      // The Os need to be read before the hardware
      node = getNode(list, "xsi:type", "ovf:OperatingSystemSection_Type");
      if (node != null) {
        readOsSection(node);
        if (!osRepository.isLinux(vmBase.getOsId())
            || vmBase.getDefaultDisplayType() != DisplayType.qxl) {
          vmBase.setSingleQxlPci(false);
        }
      }

      node = getNode(list, "xsi:type", "ovf:VirtualHardwareSection_Type");
      if (node != null) {
        readHardwareSection(node);
      }

      node = getNode(list, "xsi:type", "ovf:SnapshotsSection_Type");
      if (node != null) {
        readSnapshotsSection(node);
      }
    }

    // after reading the hardware section, if graphics device is still absent, add a default one
    addDefaultGraphicsDevice();
    // if boot order is not set, figure out some default based on the set of bootable disks
    setDefaultBootDevice();

    // due to dependency on vmBase.getOsId() must be read AFTER readOsSection
    node = selectSingleNode(content, OvfProperties.TIMEZONE);
    if (node != null && StringUtils.isNotEmpty(node.innerText)) {
      vmBase.setTimeZone(node.innerText);
    } else {
      if (osRepository.isWindows(vmBase.getOsId())) {
        vmBase.setTimeZone(Config.<String>getValue(ConfigValues.DefaultWindowsTimeZone));
      } else {
        vmBase.setTimeZone(Config.<String>getValue(ConfigValues.DefaultGeneralTimeZone));
      }
    }

    node = selectSingleNode(content, OvfProperties.ORIGIN);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setOrigin(OriginType.forValue(Integer.parseInt(node.innerText)));
      }
    }

    node = selectSingleNode(content, OvfProperties.VM_TYPE);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setVmType(VmType.forValue(Integer.parseInt(node.innerText)));
      }
    }

    node = selectSingleNode(content, OvfProperties.IS_SMARTCARD_ENABLED);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setSmartcardEnabled(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.NUM_OF_IOTHREADS);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setNumOfIoThreads(Integer.parseInt(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.DELETE_PROTECTED);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setDeleteProtected(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.SSO_METHOD);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setSsoMethod(SsoMethod.fromString(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.TUNNEL_MIGRATION);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setTunnelMigration(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.VNC_KEYBOARD_LAYOUT);
    if (node != null) {
      if (!StringUtils.isEmpty(node.innerText)) {
        vmBase.setVncKeyboardLayout(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.MIN_ALLOCATED_MEMORY);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setMinAllocatedMem(Integer.parseInt(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.IS_STATELESS);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setStateless(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.IS_RUN_AND_PAUSE);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setRunAndPause(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.CREATED_BY_USER_ID);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setCreatedByUserId(Guid.createGuidFromString(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.MIGRATION_DOWNTIME);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setMigrationDowntime(Integer.parseInt(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.MIGRATION_SUPPORT);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        MigrationSupport migrationSupport =
            MigrationSupport.forValue(Integer.parseInt(node.innerText));
        vmBase.setMigrationSupport(migrationSupport);
      }
    }

    // TODO dedicated to multiple hosts
    readDedicatedHostsList();

    node = selectSingleNode(content, OvfProperties.SERIAL_NUMBER_POLICY);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setSerialNumberPolicy(SerialNumberPolicy.forValue(Integer.parseInt(node.innerText)));
      }
    }

    node = selectSingleNode(content, OvfProperties.CUSTOM_SERIAL_NUMBER);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setCustomSerialNumber(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.AUTO_STARTUP);
    if (node != null) {
      vmBase.setAutoStartup(Boolean.parseBoolean(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.PRIORITY);
    if (node != null) {
      vmBase.setPriority(Integer.parseInt(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.IS_BOOT_MENU_ENABLED);
    if (node != null) {
      vmBase.setBootMenuEnabled(Boolean.parseBoolean(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.IS_SPICE_FILE_TRANSFER_ENABLED);
    if (node != null) {
      vmBase.setSpiceFileTransferEnabled(Boolean.parseBoolean(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.IS_SPICE_COPY_PASTE_ENABLED);
    if (node != null) {
      vmBase.setSpiceCopyPasteEnabled(Boolean.parseBoolean(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.ALLOW_CONSOLE_RECONNECT);
    if (node != null) {
      vmBase.setAllowConsoleReconnect(Boolean.parseBoolean(node.innerText));
    }

    node = selectSingleNode(content, OvfProperties.IS_AUTO_CONVERGE);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setAutoConverge(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.IS_MIGRATE_COMPRESSED);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setMigrateCompressed(Boolean.parseBoolean(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.MIGRATION_POLICY_ID);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setMigrationPolicyId(Guid.createGuidFromString(node.innerText));
      }
    }

    node = selectSingleNode(content, OvfProperties.CUSTOM_EMULATED_MACHINE);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setCustomEmulatedMachine(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.CUSTOM_CPU_NAME);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setCustomCpuName(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.PREDEFINED_PROPERTIES);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setPredefinedProperties(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.USER_DEFINED_PROPERTIES);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setUserDefinedProperties(node.innerText);
      }
    }

    node = selectSingleNode(content, OvfProperties.MAX_MEMORY_SIZE_MB);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setMaxMemorySizeMb(Integer.parseInt(node.innerText));
      }
    }

    vmBase.setCustomProperties(
        VmPropertiesUtils.getInstance()
            .customProperties(vmBase.getPredefinedProperties(), vmBase.getUserDefinedProperties()));

    node = selectSingleNode(content, OvfProperties.VM_LEASE);
    if (node != null) {
      if (StringUtils.isNotEmpty(node.innerText)) {
        vmBase.setLeaseStorageDomainId(new Guid(node.innerText));
      }
    }

    readGeneralData(content);

    readVmInit(content);
  }
Esempio n. 5
0
 @Test
 public void utcDateStringToLocaDateNoDep() {
   Date date = OvfParser.utcDateStringToLocaDate("1984/06/19 14:25:11");
   assertEquals(456503111000L, date.getTime());
 }