Renaming LibvirtConnectionWrapper to LibvirtUtilitiesHelper - Gave it a better, more suggestive, name since I now added other methods to the class. - It makes easier to mock objects and get a better coverage of the classes
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/885b9e45 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/885b9e45 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/885b9e45 Branch: refs/heads/CLOUDSTACK-8301 Commit: 885b9e45d767d9c1e97f9f89323733446b0815f0 Parents: b3913ca Author: wilderrodrigues <[email protected]> Authored: Tue May 5 13:44:52 2015 +0200 Committer: wilderrodrigues <[email protected]> Committed: Wed May 6 19:24:14 2015 +0200 ---------------------------------------------------------------------- .../kvm-compute/spring-kvm-compute-context.xml | 4 +- .../kvm/resource/LibvirtComputingResource.java | 8 +- .../wrapper/LibvirtAttachIsoCommandWrapper.java | 4 +- .../LibvirtAttachVolumeCommandWrapper.java | 4 +- .../LibvirtBackupSnapshotCommandWrapper.java | 4 +- ...ibvirtCheckVirtualMachineCommandWrapper.java | 4 +- .../wrapper/LibvirtConnectionWrapper.java | 68 ---- ...ivateTemplateFromSnapshotCommandWrapper.java | 8 +- .../LibvirtGetVmDiskStatsCommandWrapper.java | 4 +- .../LibvirtGetVmStatsCommandWrapper.java | 4 +- .../LibvirtGetVncPortCommandWrapper.java | 4 +- .../LibvirtManageSnapshotCommandWrapper.java | 4 +- .../wrapper/LibvirtMigrateCommandWrapper.java | 4 +- ...bvirtNetworkRulesSystemVmCommandWrapper.java | 4 +- ...NetworkRulesVmSecondaryIpCommandWrapper.java | 4 +- .../wrapper/LibvirtPlugNicCommandWrapper.java | 4 +- ...ibvirtPrepareForMigrationCommandWrapper.java | 4 +- .../wrapper/LibvirtRebootCommandWrapper.java | 4 +- ...LibvirtSecurityGroupRulesCommandWrapper.java | 4 +- .../wrapper/LibvirtStopCommandWrapper.java | 6 +- .../wrapper/LibvirtUnPlugNicCommandWrapper.java | 4 +- .../wrapper/LibvirtUtilitiesHelper.java | 68 ++++ .../resource/LibvirtComputingResourceTest.java | 376 +++++++++---------- 23 files changed, 301 insertions(+), 301 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/resources/META-INF/cloudstack/kvm-compute/spring-kvm-compute-context.xml ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/resources/META-INF/cloudstack/kvm-compute/spring-kvm-compute-context.xml b/plugins/hypervisors/kvm/resources/META-INF/cloudstack/kvm-compute/spring-kvm-compute-context.xml index de6853e..10f33ec 100644 --- a/plugins/hypervisors/kvm/resources/META-INF/cloudstack/kvm-compute/spring-kvm-compute-context.xml +++ b/plugins/hypervisors/kvm/resources/META-INF/cloudstack/kvm-compute/spring-kvm-compute-context.xml @@ -31,7 +31,7 @@ <property name="name" value="KVMInvestigator" /> </bean> - <bean id="libvirtConnectionWrapper" - class="com.cloud.hypervisor.kvm.resource.wrapper.LibvirtConnectionWrapper" /> + <bean id="libvirtUtilitiesHelper" + class="com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper" /> </beans> http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java index 977288b..a9410fb 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java @@ -119,7 +119,7 @@ import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.SerialDef; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TermPolicy; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VideoDef; import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VirtioSerialDef; -import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtConnectionWrapper; +import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper; import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper; import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk; import com.cloud.hypervisor.kvm.storage.KVMStoragePool; @@ -272,7 +272,7 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv protected int _stopTimeout; @Inject - private LibvirtConnectionWrapper libvirtConnectionWrapper; + private LibvirtUtilitiesHelper libvirtUtilitiesHelper; @Override public ExecutionResult executeInVR(final String routerIp, final String script, final String args) { @@ -335,8 +335,8 @@ public class LibvirtComputingResource extends ServerResourceBase implements Serv return new ExecutionResult(true, null); } - public LibvirtConnectionWrapper getLibvirtConnectionWrapper() { - return libvirtConnectionWrapper; + public LibvirtUtilitiesHelper getLibvirtConnectionWrapper() { + return libvirtUtilitiesHelper; } public VirtualRoutingResource getVirtRouterResource() { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java index 4380b66..9c12845 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java @@ -35,9 +35,9 @@ public final class LibvirtAttachIsoCommandWrapper extends CommandWrapper<AttachI @Override public Answer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) { try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach()); } catch (final LibvirtException e) { return new Answer(command, false, e.toString()); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java index 861bccc..038670d 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java @@ -36,9 +36,9 @@ public final class LibvirtAttachVolumeCommandWrapper extends CommandWrapper<Atta @Override public Answer execute(final AttachVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) { try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); final KVMStoragePool primary = libvirtComputingResource.getStoragePoolMgr().getStoragePool(command.getPooltype(), command.getPoolUuid()); final KVMPhysicalDisk disk = primary.getPhysicalDisk(command.getVolumePath()); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java index afbbf73..89fdea0 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java @@ -68,8 +68,8 @@ public final class LibvirtBackupSnapshotCommandWrapper extends CommandWrapper<Ba final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java index 3ad3300..d59ea5d 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java @@ -34,9 +34,9 @@ public final class LibvirtCheckVirtualMachineCommandWrapper extends CommandWrapp @Override public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) { try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName()); Integer vncPort = null; if (state == PowerState.PowerOn) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConnectionWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConnectionWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConnectionWrapper.java deleted file mode 100644 index 3a51a37..0000000 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtConnectionWrapper.java +++ /dev/null @@ -1,68 +0,0 @@ -// Licensed to the Apache Software Foundation (ASF) under one -// or more contributor license agreements. See the NOTICE file -// distributed with this work for additional information -// regarding copyright ownership. The ASF licenses this file -// to you under the Apache License, Version 2.0 (the -// "License"); you may not use this file except in compliance -// with the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. -package com.cloud.hypervisor.kvm.resource.wrapper; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import javax.naming.ConfigurationException; - -import org.libvirt.Connect; -import org.libvirt.LibvirtException; - -import com.cloud.hypervisor.kvm.resource.LibvirtConnection; -import com.cloud.storage.StorageLayer; -import com.cloud.storage.template.Processor; -import com.cloud.storage.template.QCOW2Processor; -import com.cloud.storage.template.TemplateLocation; - -/** - * This class is used to wrap the calls to several static methods. By doing so, we make easier to mock this class - * and the methods wrapped here. - * - * Please do not instantiate this class directly, but inject it using the {@code @Inject} annotation. - */ -public class LibvirtConnectionWrapper { - - public Connect getConnectionByVmName(final String vmName) throws LibvirtException { - return LibvirtConnection.getConnectionByVmName(vmName); - } - - public Connect getConnection() throws LibvirtException { - return LibvirtConnection.getConnection(); - } - - public TemplateLocation buildTemplateLocation(final StorageLayer storage, final String templatePath) { - final TemplateLocation location = new TemplateLocation(storage, templatePath); - return location; - } - - public Processor buildQCOW2Processor(final StorageLayer storage) throws ConfigurationException { - final Map<String, Object> params = new HashMap<String, Object>(); - params.put(StorageLayer.InstanceConfigKey, storage); - - final Processor qcow2Processor = new QCOW2Processor(); - qcow2Processor.configure("QCOW2 Processor", params); - - return qcow2Processor; - } - - public String buildTemplateUUIDName() { - return UUID.randomUUID().toString(); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java index 20bebad..0778586 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java @@ -48,11 +48,11 @@ public final class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper extend @Override public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId(); final String templateInstallFolder = "template/tmpl/" + templateFolder; - final String tmplName = libvirtConnectionWrapper.buildTemplateUUIDName(); + final String tmplName = libvirtUtilitiesHelper.buildTemplateUUIDName(); final String tmplFileName = tmplName + ".qcow2"; KVMStoragePool secondaryPool = null; @@ -84,9 +84,9 @@ public final class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper extend scriptCommand.add("-f", snapshot.getPath()); scriptCommand.execute(); - final Processor qcow2Processor = libvirtConnectionWrapper.buildQCOW2Processor(storage); + final Processor qcow2Processor = libvirtUtilitiesHelper.buildQCOW2Processor(storage); final FormatInfo info = qcow2Processor.process(templatePath, null, tmplName); - final TemplateLocation loc = libvirtConnectionWrapper.buildTemplateLocation(storage, templatePath); + final TemplateLocation loc = libvirtUtilitiesHelper.buildTemplateLocation(storage, templatePath); loc.create(1, true, tmplName); loc.addFormat(info); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java index 32ff7df..7c76833 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java @@ -40,11 +40,11 @@ public final class LibvirtGetVmDiskStatsCommandWrapper extends CommandWrapper<Ge @Override public Answer execute(final GetVmDiskStatsCommand command, final LibvirtComputingResource libvirtComputingResource) { final List<String> vmNames = command.getVmNames(); - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); try { final HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsNameMap = new HashMap<String, List<VmDiskStatsEntry>>(); - final Connect conn = libvirtConnectionWrapper.getConnection(); + final Connect conn = libvirtUtilitiesHelper.getConnection(); for (final String vmName : vmNames) { final List<VmDiskStatsEntry> statEntry = libvirtComputingResource.getVmDiskStat(conn, vmName); if (statEntry == null) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java index e0649da..f1ddca1 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java @@ -44,9 +44,9 @@ public final class LibvirtGetVmStatsCommandWrapper extends CommandWrapper<GetVmS final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>(); for (final String vmName : vmNames) { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); final VmStatsEntry statEntry = libvirtComputingResource.getVmStat(conn, vmName); if (statEntry == null) { continue; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java index 9b76374..d7c1820 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java @@ -33,9 +33,9 @@ public final class LibvirtGetVncPortCommandWrapper extends CommandWrapper<GetVnc @Override public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) { try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getName()); final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName()); return new GetVncPortAnswer(command, libvirtComputingResource.getPrivateIp(), 5900 + vncPort); } catch (final LibvirtException e) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java index 9a991a1..154e67b 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtManageSnapshotCommandWrapper.java @@ -53,8 +53,8 @@ public final class LibvirtManageSnapshotCommandWrapper extends CommandWrapper<Ma final String snapshotPath = command.getSnapshotPath(); final String vmName = command.getVmName(); try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); DomainState state = null; Domain vm = null; if (vmName != null) { http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java index d3637e6..e30e976 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java @@ -63,9 +63,9 @@ public final class LibvirtMigrateCommandWrapper extends CommandWrapper<MigrateCo Connect conn = null; String xmlDesc = null; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); ifaces = libvirtComputingResource.getInterfaces(conn, vmName); disks = libvirtComputingResource.getDisks(conn, vmName); dm = conn.domainLookupByName(vmName); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesSystemVmCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesSystemVmCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesSystemVmCommandWrapper.java index b8ef8a8..a328c37 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesSystemVmCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesSystemVmCommandWrapper.java @@ -36,9 +36,9 @@ public final class LibvirtNetworkRulesSystemVmCommandWrapper extends CommandWrap public Answer execute(final NetworkRulesSystemVmCommand command, final LibvirtComputingResource libvirtComputingResource) { boolean success = false; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); success = libvirtComputingResource.configureDefaultNetworkRulesForSystemVm(conn, command.getVmName()); } catch (final LibvirtException e) { s_logger.trace("Ignoring libvirt error.", e); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesVmSecondaryIpCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesVmSecondaryIpCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesVmSecondaryIpCommandWrapper.java index 6f15345..19eb3d3 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesVmSecondaryIpCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtNetworkRulesVmSecondaryIpCommandWrapper.java @@ -36,9 +36,9 @@ public final class LibvirtNetworkRulesVmSecondaryIpCommandWrapper extends Comman public Answer execute(final NetworkRulesVmSecondaryIpCommand command, final LibvirtComputingResource libvirtComputingResource) { boolean result = false; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); result = libvirtComputingResource.configureNetworkRulesVMSecondaryIP(conn, command.getVmName(), command.getVmSecIp(), command.getAction()); } catch (final LibvirtException e) { s_logger.debug("Could not configure VM secondary IP! => " + e.getLocalizedMessage()); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java index 7e6f642..692f79e 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPlugNicCommandWrapper.java @@ -46,8 +46,8 @@ public final class LibvirtPlugNicCommandWrapper extends CommandWrapper<PlugNicCo final String vmName = command.getVmName(); Domain vm = null; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); vm = libvirtComputingResource.getDomain(conn, vmName); final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java index 108b0d2..b9bf770 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtPrepareForMigrationCommandWrapper.java @@ -54,9 +54,9 @@ public final class LibvirtPrepareForMigrationCommandWrapper extends CommandWrapp final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr(); try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vm.getName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vm.getName()); for (final NicTO nic : nics) { libvirtComputingResource.getVifDriver(nic.getType()).plug(nic, null, ""); } http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootCommandWrapper.java index e91604a..414fcd6 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRebootCommandWrapper.java @@ -35,10 +35,10 @@ public final class LibvirtRebootCommandWrapper extends CommandWrapper<RebootComm @Override public Answer execute(final RebootCommand command, final LibvirtComputingResource libvirtComputingResource) { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); try { - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); final String result = libvirtComputingResource.rebootVM(conn, command.getVmName()); if (result == null) { Integer vncPort = null; http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSecurityGroupRulesCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSecurityGroupRulesCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSecurityGroupRulesCommandWrapper.java index 5bdafe7..c661a16 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSecurityGroupRulesCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtSecurityGroupRulesCommandWrapper.java @@ -41,9 +41,9 @@ public final class LibvirtSecurityGroupRulesCommandWrapper extends CommandWrappe String vif = null; String brname = null; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName()); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName()); final List<InterfaceDef> nics = libvirtComputingResource.getInterfaces(conn, command.getVmName()); vif = nics.get(0).getDevName(); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java index 280c7f1..0d5f892 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStopCommandWrapper.java @@ -44,11 +44,11 @@ public final class LibvirtStopCommandWrapper extends CommandWrapper<StopCommand, public Answer execute(final StopCommand command, final LibvirtComputingResource libvirtComputingResource) { final String vmName = command.getVmName(); - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); if (command.checkBeforeCleanup()) { try { - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); final Domain vm = conn.domainLookupByName(command.getVmName()); if (vm != null && vm.getInfo().state == DomainState.VIR_DOMAIN_RUNNING) { return new StopAnswer(command, "vm is still running on host", false); @@ -59,7 +59,7 @@ public final class LibvirtStopCommandWrapper extends CommandWrapper<StopCommand, } try { - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); final List<DiskDef> disks = libvirtComputingResource.getDisks(conn, vmName); final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, vmName); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java index 4ce14f2..b3016fa 100644 --- a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUnPlugNicCommandWrapper.java @@ -45,9 +45,9 @@ public final class LibvirtUnPlugNicCommandWrapper extends CommandWrapper<UnPlugN final String vmName = command.getVmName(); Domain vm = null; try { - final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper(); + final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper(); - final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(vmName); + final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName); vm = libvirtComputingResource.getDomain(conn, vmName); final List<InterfaceDef> pluggedNics = libvirtComputingResource.getInterfaces(conn, vmName); http://git-wip-us.apache.org/repos/asf/cloudstack/blob/885b9e45/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java ---------------------------------------------------------------------- diff --git a/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java new file mode 100644 index 0000000..5930bf0 --- /dev/null +++ b/plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtUtilitiesHelper.java @@ -0,0 +1,68 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.hypervisor.kvm.resource.wrapper; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import javax.naming.ConfigurationException; + +import org.libvirt.Connect; +import org.libvirt.LibvirtException; + +import com.cloud.hypervisor.kvm.resource.LibvirtConnection; +import com.cloud.storage.StorageLayer; +import com.cloud.storage.template.Processor; +import com.cloud.storage.template.QCOW2Processor; +import com.cloud.storage.template.TemplateLocation; + +/** + * This class is used to wrap the calls to several static methods. By doing so, we make easier to mock this class + * and the methods wrapped here. + * + * Please do not instantiate this class directly, but inject it using the {@code @Inject} annotation. + */ +public class LibvirtUtilitiesHelper { + + public Connect getConnectionByVmName(final String vmName) throws LibvirtException { + return LibvirtConnection.getConnectionByVmName(vmName); + } + + public Connect getConnection() throws LibvirtException { + return LibvirtConnection.getConnection(); + } + + public TemplateLocation buildTemplateLocation(final StorageLayer storage, final String templatePath) { + final TemplateLocation location = new TemplateLocation(storage, templatePath); + return location; + } + + public Processor buildQCOW2Processor(final StorageLayer storage) throws ConfigurationException { + final Map<String, Object> params = new HashMap<String, Object>(); + params.put(StorageLayer.InstanceConfigKey, storage); + + final Processor qcow2Processor = new QCOW2Processor(); + qcow2Processor.configure("QCOW2 Processor", params); + + return qcow2Processor; + } + + public String buildTemplateUUIDName() { + return UUID.randomUUID().toString(); + } +} \ No newline at end of file
