Skip to content

Commit 0b1b2b6

Browse files
Refactoring the LibvirtComputingResource
- Adding LibvirtCopyVolumeCommandWrapper Refactoring the LibvirtUtilitiesHelper - Changing method name Did not add any test to this commit due to the refactor mentioned abot. Will proceed and add the tests i# Please enter the commit message for your changes. Lines starting
1 parent 885b9e4 commit 0b1b2b6

23 files changed

Lines changed: 215 additions & 173 deletions

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java‎

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@
8181
import com.cloud.agent.api.routing.IpAssocVpcCommand;
8282
import com.cloud.agent.api.routing.NetworkElementCommand;
8383
import com.cloud.agent.api.routing.SetSourceNatCommand;
84-
import com.cloud.agent.api.storage.CopyVolumeAnswer;
85-
import com.cloud.agent.api.storage.CopyVolumeCommand;
8684
import com.cloud.agent.api.storage.ResizeVolumeAnswer;
8785
import com.cloud.agent.api.storage.ResizeVolumeCommand;
8886
import com.cloud.agent.api.to.DataStoreTO;
@@ -119,8 +117,8 @@
119117
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.TermPolicy;
120118
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VideoDef;
121119
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.VirtioSerialDef;
122-
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper;
123120
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtRequestWrapper;
121+
import com.cloud.hypervisor.kvm.resource.wrapper.LibvirtUtilitiesHelper;
124122
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
125123
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
126124
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
@@ -335,7 +333,7 @@ public ExecutionResult cleanupCommand(final NetworkElementCommand cmd) {
335333
return new ExecutionResult(true, null);
336334
}
337335

338-
public LibvirtUtilitiesHelper getLibvirtConnectionWrapper() {
336+
public LibvirtUtilitiesHelper getLibvirtUtilitiesHelper() {
339337
return libvirtUtilitiesHelper;
340338
}
341339

@@ -1250,8 +1248,6 @@ public Answer executeRequest(final Command cmd) {
12501248
return execute((StartCommand)cmd);
12511249
} else if (cmd instanceof NetworkElementCommand) {
12521250
return _virtRouterResource.executeRequest((NetworkElementCommand)cmd);
1253-
} else if (cmd instanceof CopyVolumeCommand) {
1254-
return execute((CopyVolumeCommand)cmd);
12551251
} else if (cmd instanceof ResizeVolumeCommand) {
12561252
return execute((ResizeVolumeCommand)cmd);
12571253
} else if (cmd instanceof StorageSubSystemCommand) {
@@ -1343,61 +1339,6 @@ public synchronized boolean configureTunnelNetwork(final long networkId,
13431339
return true;
13441340
}
13451341

1346-
private CopyVolumeAnswer execute(final CopyVolumeCommand cmd) {
1347-
/**
1348-
This method is only used for copying files from Primary Storage TO Secondary Storage
1349-
1350-
It COULD also do it the other way around, but the code in the ManagementServerImpl shows
1351-
that it always sets copyToSecondary to true
1352-
1353-
*/
1354-
final boolean copyToSecondary = cmd.toSecondaryStorage();
1355-
String volumePath = cmd.getVolumePath();
1356-
final StorageFilerTO pool = cmd.getPool();
1357-
final String secondaryStorageUrl = cmd.getSecondaryStorageURL();
1358-
KVMStoragePool secondaryStoragePool = null;
1359-
KVMStoragePool primaryPool = null;
1360-
try {
1361-
try {
1362-
primaryPool = _storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
1363-
} catch (final CloudRuntimeException e) {
1364-
if (e.getMessage().contains("not found")) {
1365-
primaryPool =
1366-
_storagePoolMgr.createStoragePool(cmd.getPool().getUuid(), cmd.getPool().getHost(), cmd.getPool().getPort(), cmd.getPool().getPath(),
1367-
cmd.getPool().getUserInfo(), cmd.getPool().getType());
1368-
} else {
1369-
return new CopyVolumeAnswer(cmd, false, e.getMessage(), null, null);
1370-
}
1371-
}
1372-
1373-
final String volumeName = UUID.randomUUID().toString();
1374-
1375-
if (copyToSecondary) {
1376-
final String destVolumeName = volumeName + ".qcow2";
1377-
final KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(cmd.getVolumePath());
1378-
final String volumeDestPath = "/volumes/" + cmd.getVolumeId() + File.separator;
1379-
secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
1380-
secondaryStoragePool.createFolder(volumeDestPath);
1381-
_storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
1382-
secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumeDestPath);
1383-
_storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
1384-
return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
1385-
} else {
1386-
volumePath = "/volumes/" + cmd.getVolumeId() + File.separator;
1387-
secondaryStoragePool = _storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumePath);
1388-
final KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(cmd.getVolumePath() + ".qcow2");
1389-
_storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
1390-
return new CopyVolumeAnswer(cmd, true, null, null, volumeName);
1391-
}
1392-
} catch (final CloudRuntimeException e) {
1393-
return new CopyVolumeAnswer(cmd, false, e.toString(), null, null);
1394-
} finally {
1395-
if (secondaryStoragePool != null) {
1396-
_storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
1397-
}
1398-
}
1399-
}
1400-
14011342
protected Storage.StorageResourceType getStorageResourceType() {
14021343
return Storage.StorageResourceType.STORAGE_POOL;
14031344
}

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachIsoCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class LibvirtAttachIsoCommandWrapper extends CommandWrapper<AttachI
3535
@Override
3636
public Answer execute(final AttachIsoCommand command, final LibvirtComputingResource libvirtComputingResource) {
3737
try {
38-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
38+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
3939

4040
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
4141
libvirtComputingResource.attachOrDetachISO(conn, command.getVmName(), command.getIsoPath(), command.isAttach());

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtAttachVolumeCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public final class LibvirtAttachVolumeCommandWrapper extends CommandWrapper<Atta
3636
@Override
3737
public Answer execute(final AttachVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
3838
try {
39-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
39+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
4040

4141
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
4242

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtBackupSnapshotCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public Answer execute(final BackupSnapshotCommand command, final LibvirtComputin
6868
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
6969

7070
try {
71-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
71+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
7272
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
7373

7474
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStoragePoolUrl);

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCheckVirtualMachineCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public final class LibvirtCheckVirtualMachineCommandWrapper extends CommandWrapp
3434
@Override
3535
public Answer execute(final CheckVirtualMachineCommand command, final LibvirtComputingResource libvirtComputingResource) {
3636
try {
37-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
37+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
3838

3939
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getVmName());
4040
final PowerState state = libvirtComputingResource.getVmState(conn, command.getVmName());
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.hypervisor.kvm.resource.wrapper;
21+
22+
import java.io.File;
23+
24+
import com.cloud.agent.api.Answer;
25+
import com.cloud.agent.api.storage.CopyVolumeAnswer;
26+
import com.cloud.agent.api.storage.CopyVolumeCommand;
27+
import com.cloud.agent.api.to.StorageFilerTO;
28+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
29+
import com.cloud.hypervisor.kvm.storage.KVMPhysicalDisk;
30+
import com.cloud.hypervisor.kvm.storage.KVMStoragePool;
31+
import com.cloud.hypervisor.kvm.storage.KVMStoragePoolManager;
32+
import com.cloud.resource.CommandWrapper;
33+
import com.cloud.utils.exception.CloudRuntimeException;
34+
35+
public final class LibvirtCopyVolumeCommandWrapper extends CommandWrapper<CopyVolumeCommand, Answer, LibvirtComputingResource> {
36+
37+
@Override
38+
public Answer execute(final CopyVolumeCommand command, final LibvirtComputingResource libvirtComputingResource) {
39+
/**
40+
This method is only used for copying files from Primary Storage TO Secondary Storage
41+
42+
It COULD also do it the other way around, but the code in the ManagementServerImpl shows
43+
that it always sets copyToSecondary to true
44+
45+
*/
46+
final boolean copyToSecondary = command.toSecondaryStorage();
47+
String volumePath = command.getVolumePath();
48+
final StorageFilerTO pool = command.getPool();
49+
final String secondaryStorageUrl = command.getSecondaryStorageURL();
50+
KVMStoragePool secondaryStoragePool = null;
51+
KVMStoragePool primaryPool = null;
52+
53+
final KVMStoragePoolManager storagePoolMgr = libvirtComputingResource.getStoragePoolMgr();
54+
try {
55+
try {
56+
primaryPool = storagePoolMgr.getStoragePool(pool.getType(), pool.getUuid());
57+
} catch (final CloudRuntimeException e) {
58+
if (e.getMessage().contains("not found")) {
59+
primaryPool =
60+
storagePoolMgr.createStoragePool(command.getPool().getUuid(), command.getPool().getHost(), command.getPool().getPort(), command.getPool().getPath(),
61+
command.getPool().getUserInfo(), command.getPool().getType());
62+
} else {
63+
return new CopyVolumeAnswer(command, false, e.getMessage(), null, null);
64+
}
65+
}
66+
67+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
68+
final String volumeName = libvirtUtilitiesHelper.generatereUUIDName();
69+
70+
if (copyToSecondary) {
71+
final String destVolumeName = volumeName + ".qcow2";
72+
final KVMPhysicalDisk volume = primaryPool.getPhysicalDisk(command.getVolumePath());
73+
final String volumeDestPath = "/volumes/" + command.getVolumeId() + File.separator;
74+
75+
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl);
76+
secondaryStoragePool.createFolder(volumeDestPath);
77+
storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
78+
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumeDestPath);
79+
storagePoolMgr.copyPhysicalDisk(volume, destVolumeName, secondaryStoragePool, 0);
80+
81+
return new CopyVolumeAnswer(command, true, null, null, volumeName);
82+
} else {
83+
volumePath = "/volumes/" + command.getVolumeId() + File.separator;
84+
secondaryStoragePool = storagePoolMgr.getStoragePoolByURI(secondaryStorageUrl + volumePath);
85+
86+
final KVMPhysicalDisk volume = secondaryStoragePool.getPhysicalDisk(command.getVolumePath() + ".qcow2");
87+
storagePoolMgr.copyPhysicalDisk(volume, volumeName, primaryPool, 0);
88+
89+
return new CopyVolumeAnswer(command, true, null, null, volumeName);
90+
}
91+
} catch (final CloudRuntimeException e) {
92+
return new CopyVolumeAnswer(command, false, e.toString(), null, null);
93+
} finally {
94+
if (secondaryStoragePool != null) {
95+
storagePoolMgr.deleteStoragePool(secondaryStoragePool.getType(), secondaryStoragePool.getUuid());
96+
}
97+
}
98+
}
99+
}

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ public final class LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper extend
4848

4949
@Override
5050
public Answer execute(final CreatePrivateTemplateFromSnapshotCommand command, final LibvirtComputingResource libvirtComputingResource) {
51-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
51+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
5252

5353
final String templateFolder = command.getAccountId() + File.separator + command.getNewTemplateId();
5454
final String templateInstallFolder = "template/tmpl/" + templateFolder;
55-
final String tmplName = libvirtUtilitiesHelper.buildTemplateUUIDName();
55+
final String tmplName = libvirtUtilitiesHelper.generatereUUIDName();
5656
final String tmplFileName = tmplName + ".qcow2";
5757

5858
KVMStoragePool secondaryPool = null;

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmDiskStatsCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final class LibvirtGetVmDiskStatsCommandWrapper extends CommandWrapper<Ge
4040
@Override
4141
public Answer execute(final GetVmDiskStatsCommand command, final LibvirtComputingResource libvirtComputingResource) {
4242
final List<String> vmNames = command.getVmNames();
43-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
43+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
4444

4545
try {
4646
final HashMap<String, List<VmDiskStatsEntry>> vmDiskStatsNameMap = new HashMap<String, List<VmDiskStatsEntry>>();

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVmStatsCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public Answer execute(final GetVmStatsCommand command, final LibvirtComputingRes
4444
final HashMap<String, VmStatsEntry> vmStatsNameMap = new HashMap<String, VmStatsEntry>();
4545
for (final String vmName : vmNames) {
4646

47-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
47+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
4848

4949
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(vmName);
5050
final VmStatsEntry statEntry = libvirtComputingResource.getVmStat(conn, vmName);

‎plugins/hypervisors/kvm/src/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtGetVncPortCommandWrapper.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public final class LibvirtGetVncPortCommandWrapper extends CommandWrapper<GetVnc
3333
@Override
3434
public Answer execute(final GetVncPortCommand command, final LibvirtComputingResource libvirtComputingResource) {
3535
try {
36-
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtConnectionWrapper();
36+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
3737

3838
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(command.getName());
3939
final Integer vncPort = libvirtComputingResource.getVncPort(conn, command.getName());

0 commit comments

Comments
 (0)