Skip to content

Commit 3c8b217

Browse files
Refactoring the LibvirtComputingResource
- Adding LibvirtCheckOnHostCommandWrapper and LibvirtOvsCreateTunnelCommandWrapper - 4 unit tests added - KVM hypervisor plugin with 16.2% coverage
1 parent 6748a73 commit 3c8b217

5 files changed

Lines changed: 252 additions & 85 deletions

File tree

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

Lines changed: 4 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@
8585
import com.cloud.agent.api.Answer;
8686
import com.cloud.agent.api.BackupSnapshotAnswer;
8787
import com.cloud.agent.api.BackupSnapshotCommand;
88-
import com.cloud.agent.api.CheckOnHostCommand;
8988
import com.cloud.agent.api.Command;
9089
import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand;
9190
import com.cloud.agent.api.CreatePrivateTemplateFromVolumeCommand;
@@ -98,8 +97,6 @@
9897
import com.cloud.agent.api.ManageSnapshotCommand;
9998
import com.cloud.agent.api.NetworkUsageAnswer;
10099
import com.cloud.agent.api.NetworkUsageCommand;
101-
import com.cloud.agent.api.OvsCreateTunnelAnswer;
102-
import com.cloud.agent.api.OvsCreateTunnelCommand;
103100
import com.cloud.agent.api.PingCommand;
104101
import com.cloud.agent.api.PingRoutingCommand;
105102
import com.cloud.agent.api.PingRoutingWithNwGroupsCommand;
@@ -430,6 +427,10 @@ public String getOvsTunnelPath() {
430427
return _ovsTunnelPath;
431428
}
432429

430+
public KVMHAMonitor getMonitor() {
431+
return _monitor;
432+
}
433+
433434
private static final class KeyValueInterpreter extends OutputInterpreter {
434435
private final Map<String, String> map = new HashMap<String, String>();
435436

@@ -1309,10 +1310,6 @@ public Answer executeRequest(final Command cmd) {
13091310
return storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
13101311
} else if (cmd instanceof PvlanSetupCommand) {
13111312
return execute((PvlanSetupCommand)cmd);
1312-
} else if (cmd instanceof CheckOnHostCommand) {
1313-
return execute((CheckOnHostCommand)cmd);
1314-
} else if (cmd instanceof OvsCreateTunnelCommand) {
1315-
return execute((OvsCreateTunnelCommand)cmd);
13161313
} else {
13171314
s_logger.warn("Unsupported command ");
13181315
return Answer.createUnsupportedCommandAnswer(cmd);
@@ -1398,39 +1395,6 @@ public synchronized boolean configureTunnelNetwork(final long networkId,
13981395
return true;
13991396
}
14001397

1401-
private OvsCreateTunnelAnswer execute(final OvsCreateTunnelCommand cmd) {
1402-
final String bridge = cmd.getNetworkName();
1403-
try {
1404-
if (!findOrCreateTunnelNetwork(bridge)) {
1405-
s_logger.debug("Error during bridge setup");
1406-
return new OvsCreateTunnelAnswer(cmd, false,
1407-
"Cannot create network", bridge);
1408-
}
1409-
1410-
configureTunnelNetwork(cmd.getNetworkId(), cmd.getFrom(),
1411-
cmd.getNetworkName());
1412-
final Script command = new Script(_ovsTunnelPath, _timeout, s_logger);
1413-
command.add("create_tunnel");
1414-
command.add("--bridge", bridge);
1415-
command.add("--remote_ip", cmd.getRemoteIp());
1416-
command.add("--key", cmd.getKey().toString());
1417-
command.add("--src_host", cmd.getFrom().toString());
1418-
command.add("--dst_host", cmd.getTo().toString());
1419-
1420-
final String result = command.execute();
1421-
if (result != null) {
1422-
return new OvsCreateTunnelAnswer(cmd, true, result, null,
1423-
bridge);
1424-
} else {
1425-
return new OvsCreateTunnelAnswer(cmd, false, result, bridge);
1426-
}
1427-
} catch (final Exception e) {
1428-
s_logger.debug("Error during tunnel setup");
1429-
s_logger.warn("Caught execption when creating ovs tunnel", e);
1430-
return new OvsCreateTunnelAnswer(cmd, false, e.getMessage(), bridge);
1431-
}
1432-
}
1433-
14341398
private CopyVolumeAnswer execute(final CopyVolumeCommand cmd) {
14351399
/**
14361400
This method is only used for copying files from Primary Storage TO Secondary Storage
@@ -1508,26 +1472,6 @@ protected FenceAnswer execute(final FenceCommand cmd) {
15081472

15091473
}
15101474

1511-
protected Answer execute(final CheckOnHostCommand cmd) {
1512-
final ExecutorService executors = Executors.newSingleThreadExecutor();
1513-
final List<NfsStoragePool> pools = _monitor.getStoragePools();
1514-
final KVMHAChecker ha = new KVMHAChecker(pools, cmd.getHost().getPrivateNetwork().getIp());
1515-
final Future<Boolean> future = executors.submit(ha);
1516-
try {
1517-
final Boolean result = future.get();
1518-
if (result) {
1519-
return new Answer(cmd, false, "Heart is still beating...");
1520-
} else {
1521-
return new Answer(cmd);
1522-
}
1523-
} catch (final InterruptedException e) {
1524-
return new Answer(cmd, false, "can't get status of host:");
1525-
} catch (final ExecutionException e) {
1526-
return new Answer(cmd, false, "can't get status of host:");
1527-
}
1528-
1529-
}
1530-
15311475
protected Storage.StorageResourceType getStorageResourceType() {
15321476
return Storage.StorageResourceType.STORAGE_POOL;
15331477
}
@@ -3795,28 +3739,6 @@ private String getHypervisorPath(final Connect conn) {
37953739
return parser.getEmulator();
37963740
}
37973741

3798-
private String getGuestType(final Connect conn, final String vmName) {
3799-
final LibvirtDomainXMLParser parser = new LibvirtDomainXMLParser();
3800-
Domain dm = null;
3801-
try {
3802-
dm = conn.domainLookupByName(vmName);
3803-
final String xmlDesc = dm.getXMLDesc(0);
3804-
parser.parseDomainXML(xmlDesc);
3805-
return parser.getDescription();
3806-
} catch (final LibvirtException e) {
3807-
s_logger.trace("Ignoring libvirt error.", e);
3808-
return null;
3809-
} finally {
3810-
try {
3811-
if (dm != null) {
3812-
dm.free();
3813-
}
3814-
} catch (final LibvirtException l) {
3815-
s_logger.trace("Ignoring libvirt error.", l);
3816-
}
3817-
}
3818-
}
3819-
38203742
boolean isGuestPVEnabled(final String guestOSName) {
38213743
if (guestOSName == null) {
38223744
return false;
@@ -4304,13 +4226,11 @@ private String prettyVersion(final long version) {
43044226
@Override
43054227
public void setName(final String name) {
43064228
// TODO Auto-generated method stub
4307-
43084229
}
43094230

43104231
@Override
43114232
public void setConfigParams(final Map<String, Object> params) {
43124233
// TODO Auto-generated method stub
4313-
43144234
}
43154235

43164236
@Override
@@ -4328,7 +4248,6 @@ public int getRunLevel() {
43284248
@Override
43294249
public void setRunLevel(final int level) {
43304250
// TODO Auto-generated method stub
4331-
43324251
}
43334252

43344253
public HypervisorType getHypervisorType(){
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.util.List;
23+
import java.util.concurrent.ExecutionException;
24+
import java.util.concurrent.ExecutorService;
25+
import java.util.concurrent.Executors;
26+
import java.util.concurrent.Future;
27+
28+
import com.cloud.agent.api.Answer;
29+
import com.cloud.agent.api.CheckOnHostCommand;
30+
import com.cloud.agent.api.to.HostTO;
31+
import com.cloud.agent.api.to.NetworkTO;
32+
import com.cloud.hypervisor.kvm.resource.KVMHABase.NfsStoragePool;
33+
import com.cloud.hypervisor.kvm.resource.KVMHAChecker;
34+
import com.cloud.hypervisor.kvm.resource.KVMHAMonitor;
35+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
36+
import com.cloud.resource.CommandWrapper;
37+
38+
public final class LibvirtCheckOnHostCommandWrapper extends CommandWrapper<CheckOnHostCommand, Answer, LibvirtComputingResource> {
39+
40+
@Override
41+
public Answer execute(final CheckOnHostCommand command, final LibvirtComputingResource libvirtComputingResource) {
42+
final ExecutorService executors = Executors.newSingleThreadExecutor();
43+
final KVMHAMonitor monitor = libvirtComputingResource.getMonitor();
44+
45+
final List<NfsStoragePool> pools = monitor.getStoragePools();
46+
HostTO host = command.getHost();
47+
NetworkTO privateNetwork = host.getPrivateNetwork();
48+
final KVMHAChecker ha = new KVMHAChecker(pools, privateNetwork.getIp());
49+
50+
final Future<Boolean> future = executors.submit(ha);
51+
try {
52+
final Boolean result = future.get();
53+
if (result) {
54+
return new Answer(command, false, "Heart is still beating...");
55+
} else {
56+
return new Answer(command);
57+
}
58+
} catch (final InterruptedException e) {
59+
return new Answer(command, false, "can't get status of host:");
60+
} catch (final ExecutionException e) {
61+
return new Answer(command, false, "can't get status of host:");
62+
}
63+
}
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 org.apache.log4j.Logger;
23+
24+
import com.cloud.agent.api.Answer;
25+
import com.cloud.agent.api.OvsCreateTunnelAnswer;
26+
import com.cloud.agent.api.OvsCreateTunnelCommand;
27+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
28+
import com.cloud.resource.CommandWrapper;
29+
import com.cloud.utils.script.Script;
30+
31+
public final class LibvirtOvsCreateTunnelCommandWrapper extends CommandWrapper<OvsCreateTunnelCommand, Answer, LibvirtComputingResource> {
32+
33+
private static final Logger s_logger = Logger.getLogger(LibvirtOvsCreateTunnelCommandWrapper.class);
34+
35+
@Override
36+
public Answer execute(final OvsCreateTunnelCommand command, final LibvirtComputingResource libvirtComputingResource) {
37+
final String bridge = command.getNetworkName();
38+
try {
39+
if (!libvirtComputingResource.findOrCreateTunnelNetwork(bridge)) {
40+
s_logger.debug("Error during bridge setup");
41+
return new OvsCreateTunnelAnswer(command, false,
42+
"Cannot create network", bridge);
43+
}
44+
45+
libvirtComputingResource.configureTunnelNetwork(command.getNetworkId(), command.getFrom(),
46+
command.getNetworkName());
47+
48+
final Script scriptCommand = new Script(libvirtComputingResource.getOvsTunnelPath(), libvirtComputingResource.getTimeout(), s_logger);
49+
scriptCommand.add("create_tunnel");
50+
scriptCommand.add("--bridge", bridge);
51+
scriptCommand.add("--remote_ip", command.getRemoteIp());
52+
scriptCommand.add("--key", command.getKey().toString());
53+
scriptCommand.add("--src_host", command.getFrom().toString());
54+
scriptCommand.add("--dst_host", command.getTo().toString());
55+
56+
final String result = scriptCommand.execute();
57+
if (result != null) {
58+
return new OvsCreateTunnelAnswer(command, true, result, null,
59+
bridge);
60+
} else {
61+
return new OvsCreateTunnelAnswer(command, false, result, bridge);
62+
}
63+
} catch (final Exception e) {
64+
s_logger.warn("Caught execption when creating ovs tunnel", e);
65+
return new OvsCreateTunnelAnswer(command, false, e.getMessage(), bridge);
66+
}
67+
}
68+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.cloud.agent.api.AttachVolumeCommand;
2626
import com.cloud.agent.api.CheckHealthCommand;
2727
import com.cloud.agent.api.CheckNetworkCommand;
28+
import com.cloud.agent.api.CheckOnHostCommand;
2829
import com.cloud.agent.api.CheckVirtualMachineCommand;
2930
import com.cloud.agent.api.CleanupNetworkRulesCmd;
3031
import com.cloud.agent.api.Command;
@@ -41,6 +42,7 @@
4142
import com.cloud.agent.api.ModifyStoragePoolCommand;
4243
import com.cloud.agent.api.NetworkRulesSystemVmCommand;
4344
import com.cloud.agent.api.NetworkRulesVmSecondaryIpCommand;
45+
import com.cloud.agent.api.OvsCreateTunnelCommand;
4446
import com.cloud.agent.api.OvsDestroyBridgeCommand;
4547
import com.cloud.agent.api.OvsDestroyTunnelCommand;
4648
import com.cloud.agent.api.OvsFetchInterfaceCommand;
@@ -120,6 +122,8 @@ private void init() {
120122
linbvirtCommands.put(CheckSshCommand.class, new LibvirtCheckSshCommandWrapper());
121123
linbvirtCommands.put(CheckNetworkCommand.class, new LibvirtCheckNetworkCommandWrapper());
122124
linbvirtCommands.put(OvsDestroyTunnelCommand.class, new LibvirtOvsDestroyTunnelCommandWrapper());
125+
linbvirtCommands.put(CheckOnHostCommand.class, new LibvirtCheckOnHostCommandWrapper());
126+
linbvirtCommands.put(OvsCreateTunnelCommand.class, new LibvirtOvsCreateTunnelCommandWrapper());
123127

124128
resources.put(LibvirtComputingResource.class, linbvirtCommands);
125129
}

0 commit comments

Comments
 (0)