Skip to content

Commit 6748a73

Browse files
Refactoring the LibvirtComputingResource
- Adding 4 new command wrappers - 12 unit tests added - KVM hypervisor plugin with 15.5% coverage
1 parent 5499eec commit 6748a73

7 files changed

Lines changed: 573 additions & 100 deletions

File tree

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

Lines changed: 9 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +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.CheckNetworkAnswer;
89-
import com.cloud.agent.api.CheckNetworkCommand;
9088
import com.cloud.agent.api.CheckOnHostCommand;
9189
import com.cloud.agent.api.Command;
9290
import com.cloud.agent.api.CreatePrivateTemplateFromSnapshotCommand;
@@ -98,12 +96,10 @@
9896
import com.cloud.agent.api.HostVmStateReportEntry;
9997
import com.cloud.agent.api.ManageSnapshotAnswer;
10098
import com.cloud.agent.api.ManageSnapshotCommand;
101-
import com.cloud.agent.api.NetworkRulesSystemVmCommand;
10299
import com.cloud.agent.api.NetworkUsageAnswer;
103100
import com.cloud.agent.api.NetworkUsageCommand;
104101
import com.cloud.agent.api.OvsCreateTunnelAnswer;
105102
import com.cloud.agent.api.OvsCreateTunnelCommand;
106-
import com.cloud.agent.api.OvsDestroyTunnelCommand;
107103
import com.cloud.agent.api.PingCommand;
108104
import com.cloud.agent.api.PingRoutingCommand;
109105
import com.cloud.agent.api.PingRoutingWithNwGroupsCommand;
@@ -122,8 +118,6 @@
122118
import com.cloud.agent.api.UnPlugNicCommand;
123119
import com.cloud.agent.api.VmDiskStatsEntry;
124120
import com.cloud.agent.api.VmStatsEntry;
125-
import com.cloud.agent.api.check.CheckSshAnswer;
126-
import com.cloud.agent.api.check.CheckSshCommand;
127121
import com.cloud.agent.api.routing.IpAssocCommand;
128122
import com.cloud.agent.api.routing.IpAssocVpcCommand;
129123
import com.cloud.agent.api.routing.NetworkElementCommand;
@@ -178,7 +172,6 @@
178172
import com.cloud.network.Networks.IsolationType;
179173
import com.cloud.network.Networks.RouterPrivateIpStrategy;
180174
import com.cloud.network.Networks.TrafficType;
181-
import com.cloud.network.PhysicalNetworkSetupInfo;
182175
import com.cloud.resource.ServerResource;
183176
import com.cloud.resource.ServerResourceBase;
184177
import com.cloud.storage.JavaStorageLayer;
@@ -1161,7 +1154,7 @@ private String matchPifFileInDirectory(final String bridgeName) {
11611154
return "";
11621155
}
11631156

1164-
private boolean checkNetwork(final String networkName) {
1157+
public boolean checkNetwork(final String networkName) {
11651158
if (networkName == null) {
11661159
return true;
11671160
}
@@ -1306,18 +1299,12 @@ public Answer executeRequest(final Command cmd) {
13061299
return execute((UnPlugNicCommand)cmd);
13071300
} else if (cmd instanceof NetworkElementCommand) {
13081301
return _virtRouterResource.executeRequest((NetworkElementCommand)cmd);
1309-
} else if (cmd instanceof CheckSshCommand) {
1310-
return execute((CheckSshCommand)cmd);
13111302
} else if (cmd instanceof NetworkUsageCommand) {
13121303
return execute((NetworkUsageCommand)cmd);
1313-
} else if (cmd instanceof NetworkRulesSystemVmCommand) {
1314-
return execute((NetworkRulesSystemVmCommand)cmd);
13151304
} else if (cmd instanceof CopyVolumeCommand) {
13161305
return execute((CopyVolumeCommand)cmd);
13171306
} else if (cmd instanceof ResizeVolumeCommand) {
13181307
return execute((ResizeVolumeCommand)cmd);
1319-
} else if (cmd instanceof CheckNetworkCommand) {
1320-
return execute((CheckNetworkCommand)cmd);
13211308
} else if (cmd instanceof StorageSubSystemCommand) {
13221309
return storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
13231310
} else if (cmd instanceof PvlanSetupCommand) {
@@ -1326,8 +1313,6 @@ public Answer executeRequest(final Command cmd) {
13261313
return execute((CheckOnHostCommand)cmd);
13271314
} else if (cmd instanceof OvsCreateTunnelCommand) {
13281315
return execute((OvsCreateTunnelCommand)cmd);
1329-
} else if (cmd instanceof OvsDestroyTunnelCommand) {
1330-
return execute((OvsDestroyTunnelCommand)cmd);
13311316
} else {
13321317
s_logger.warn("Unsupported command ");
13331318
return Answer.createUnsupportedCommandAnswer(cmd);
@@ -1367,14 +1352,19 @@ public synchronized boolean findOrCreateTunnelNetwork(final String nwName) {
13671352
s_logger.debug("### KVM network for tunnels created:" + nwName);
13681353
} catch (final Exception e) {
13691354
s_logger.warn("createTunnelNetwork failed", e);
1355+
return false;
13701356
}
13711357
return true;
13721358
}
13731359

13741360
public synchronized boolean configureTunnelNetwork(final long networkId,
13751361
final long hostId, final String nwName) {
13761362
try {
1377-
findOrCreateTunnelNetwork(nwName);
1363+
final boolean findResult = findOrCreateTunnelNetwork(nwName);
1364+
if (!findResult) {
1365+
s_logger.warn("LibvirtComputingResource.findOrCreateTunnelNetwork() failed! Cannot proceed creating the tunnel.");
1366+
return false;
1367+
}
13781368
final String configuredHosts = Script
13791369
.runSimpleBashScript("ovs-vsctl get bridge " + nwName
13801370
+ " other_config:ovs-host-setup");
@@ -1441,53 +1431,6 @@ private OvsCreateTunnelAnswer execute(final OvsCreateTunnelCommand cmd) {
14411431
}
14421432
}
14431433

1444-
private Answer execute(final OvsDestroyTunnelCommand cmd) {
1445-
try {
1446-
if (!findOrCreateTunnelNetwork(cmd.getBridgeName())) {
1447-
s_logger.warn("Unable to find tunnel network for GRE key:"
1448-
+ cmd.getBridgeName());
1449-
return new Answer(cmd, false, "No network found");
1450-
}
1451-
1452-
final Script command = new Script(_ovsTunnelPath, _timeout, s_logger);
1453-
command.add("destroy_tunnel");
1454-
command.add("--bridge", cmd.getBridgeName());
1455-
command.add("--iface_name", cmd.getInPortName());
1456-
final String result = command.execute();
1457-
if (result == null) {
1458-
return new Answer(cmd, true, result);
1459-
} else {
1460-
return new Answer(cmd, false, result);
1461-
}
1462-
} catch (final Exception e) {
1463-
s_logger.warn("caught execption when destroy ovs tunnel", e);
1464-
return new Answer(cmd, false, e.getMessage());
1465-
}
1466-
}
1467-
1468-
private CheckNetworkAnswer execute(final CheckNetworkCommand cmd) {
1469-
final List<PhysicalNetworkSetupInfo> phyNics = cmd.getPhysicalNetworkInfoList();
1470-
String errMsg = null;
1471-
for (final PhysicalNetworkSetupInfo nic : phyNics) {
1472-
if (!checkNetwork(nic.getGuestNetworkName())) {
1473-
errMsg = "Can not find network: " + nic.getGuestNetworkName();
1474-
break;
1475-
} else if (!checkNetwork(nic.getPrivateNetworkName())) {
1476-
errMsg = "Can not find network: " + nic.getPrivateNetworkName();
1477-
break;
1478-
} else if (!checkNetwork(nic.getPublicNetworkName())) {
1479-
errMsg = "Can not find network: " + nic.getPublicNetworkName();
1480-
break;
1481-
}
1482-
}
1483-
1484-
if (errMsg != null) {
1485-
return new CheckNetworkAnswer(cmd, false, errMsg);
1486-
} else {
1487-
return new CheckNetworkAnswer(cmd, true, null);
1488-
}
1489-
}
1490-
14911434
private CopyVolumeAnswer execute(final CopyVolumeCommand cmd) {
14921435
/**
14931436
This method is only used for copying files from Primary Storage TO Secondary Storage
@@ -2957,7 +2900,7 @@ protected StartAnswer execute(final StartCommand cmd) {
29572900
for (final NicTO nic : nics) {
29582901
if (nic.isSecurityGroupEnabled() || nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) {
29592902
if (vmSpec.getType() != VirtualMachine.Type.User) {
2960-
default_network_rules_for_systemvm(conn, vmName);
2903+
configureDefaultNetworkRulesForSystemVm(conn, vmName);
29612904
break;
29622905
} else {
29632906
final List<String> nicSecIps = nic.getNicSecIps();
@@ -3193,26 +3136,6 @@ private void createVif(final LibvirtVMDef vm, final NicTO nic, final String nicA
31933136
vm.getDevices().addDevice(getVifDriver(nic.getType()).plug(nic, vm.getPlatformEmulator().toString(), nicAdapter).toString());
31943137
}
31953138

3196-
protected CheckSshAnswer execute(final CheckSshCommand cmd) {
3197-
final String vmName = cmd.getName();
3198-
final String privateIp = cmd.getIp();
3199-
final int cmdPort = cmd.getPort();
3200-
3201-
if (s_logger.isDebugEnabled()) {
3202-
s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort);
3203-
}
3204-
3205-
if (!_virtRouterResource.connect(privateIp, cmdPort)) {
3206-
return new CheckSshAnswer(cmd, "Can not ping System vm " + vmName + " because of a connection failure");
3207-
}
3208-
3209-
if (s_logger.isDebugEnabled()) {
3210-
s_logger.debug("Ping command port succeeded for vm " + vmName);
3211-
}
3212-
3213-
return new CheckSshAnswer(cmd);
3214-
}
3215-
32163139
public boolean cleanupDisk(final DiskDef disk) {
32173140
final String path = disk.getDiskPath();
32183141

@@ -4240,7 +4163,7 @@ protected boolean post_default_network_rules(final Connect conn, final String vm
42404163
return true;
42414164
}
42424165

4243-
protected boolean default_network_rules_for_systemvm(final Connect conn, final String vmName) {
4166+
public boolean configureDefaultNetworkRulesForSystemVm(final Connect conn, final String vmName) {
42444167
if (!_canBridgeFirewall) {
42454168
return false;
42464169
}
@@ -4371,19 +4294,6 @@ static double readDouble(final String nicName, final String fileName) {
43714294
}
43724295
}
43734296

4374-
private Answer execute(final NetworkRulesSystemVmCommand cmd) {
4375-
boolean success = false;
4376-
Connect conn;
4377-
try {
4378-
conn = LibvirtConnection.getConnectionByVmName(cmd.getVmName());
4379-
success = default_network_rules_for_systemvm(conn, cmd.getVmName());
4380-
} catch (final LibvirtException e) {
4381-
s_logger.trace("Ignoring libvirt error.", e);
4382-
}
4383-
4384-
return new Answer(cmd, success, "");
4385-
}
4386-
43874297
private String prettyVersion(final long version) {
43884298
final long major = version / 1000000;
43894299
final long minor = version % 1000000 / 1000;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
24+
import com.cloud.agent.api.Answer;
25+
import com.cloud.agent.api.CheckNetworkAnswer;
26+
import com.cloud.agent.api.CheckNetworkCommand;
27+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
28+
import com.cloud.network.PhysicalNetworkSetupInfo;
29+
import com.cloud.resource.CommandWrapper;
30+
31+
public final class LibvirtCheckNetworkCommandWrapper extends CommandWrapper<CheckNetworkCommand, Answer, LibvirtComputingResource> {
32+
33+
@Override
34+
public Answer execute(final CheckNetworkCommand command, final LibvirtComputingResource libvirtComputingResource) {
35+
final List<PhysicalNetworkSetupInfo> phyNics = command.getPhysicalNetworkInfoList();
36+
String errMsg = null;
37+
38+
for (final PhysicalNetworkSetupInfo nic : phyNics) {
39+
if (!libvirtComputingResource.checkNetwork(nic.getGuestNetworkName())) {
40+
errMsg = "Can not find network: " + nic.getGuestNetworkName();
41+
break;
42+
} else if (!libvirtComputingResource.checkNetwork(nic.getPrivateNetworkName())) {
43+
errMsg = "Can not find network: " + nic.getPrivateNetworkName();
44+
break;
45+
} else if (!libvirtComputingResource.checkNetwork(nic.getPublicNetworkName())) {
46+
errMsg = "Can not find network: " + nic.getPublicNetworkName();
47+
break;
48+
}
49+
}
50+
51+
if (errMsg != null) {
52+
return new CheckNetworkAnswer(command, false, errMsg);
53+
} else {
54+
return new CheckNetworkAnswer(command, true, null);
55+
}
56+
}
57+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.check.CheckSshAnswer;
26+
import com.cloud.agent.api.check.CheckSshCommand;
27+
import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
28+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
29+
import com.cloud.resource.CommandWrapper;
30+
31+
public final class LibvirtCheckSshCommandWrapper extends CommandWrapper<CheckSshCommand, Answer, LibvirtComputingResource> {
32+
33+
private static final Logger s_logger = Logger.getLogger(LibvirtOvsVpcRoutingPolicyConfigCommandWrapper.class);
34+
35+
@Override
36+
public Answer execute(final CheckSshCommand command, final LibvirtComputingResource libvirtComputingResource) {
37+
final String vmName = command.getName();
38+
final String privateIp = command.getIp();
39+
final int cmdPort = command.getPort();
40+
41+
if (s_logger.isDebugEnabled()) {
42+
s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort);
43+
}
44+
45+
final VirtualRoutingResource virtRouterResource = libvirtComputingResource.getVirtRouterResource();
46+
if (!virtRouterResource.connect(privateIp, cmdPort)) {
47+
return new CheckSshAnswer(command, "Can not ping System vm " + vmName + " because of a connection failure");
48+
}
49+
50+
if (s_logger.isDebugEnabled()) {
51+
s_logger.debug("Ping command port succeeded for vm " + vmName);
52+
}
53+
54+
return new CheckSshAnswer(command);
55+
}
56+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
import org.libvirt.Connect;
24+
import org.libvirt.LibvirtException;
25+
26+
import com.cloud.agent.api.Answer;
27+
import com.cloud.agent.api.NetworkRulesSystemVmCommand;
28+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
29+
import com.cloud.resource.CommandWrapper;
30+
31+
public final class LibvirtNetworkRulesSystemVmCommandWrapper extends CommandWrapper<NetworkRulesSystemVmCommand, Answer, LibvirtComputingResource> {
32+
33+
private static final Logger s_logger = Logger.getLogger(LibvirtOvsVpcRoutingPolicyConfigCommandWrapper.class);
34+
35+
@Override
36+
public Answer execute(final NetworkRulesSystemVmCommand command, final LibvirtComputingResource libvirtComputingResource) {
37+
boolean success = false;
38+
try {
39+
final LibvirtConnectionWrapper libvirtConnectionWrapper = libvirtComputingResource.getLibvirtConnectionWrapper();
40+
41+
final Connect conn = libvirtConnectionWrapper.getConnectionByVmName(command.getVmName());
42+
success = libvirtComputingResource.configureDefaultNetworkRulesForSystemVm(conn, command.getVmName());
43+
} catch (final LibvirtException e) {
44+
s_logger.trace("Ignoring libvirt error.", e);
45+
}
46+
47+
return new Answer(command, success, "");
48+
}
49+
}

0 commit comments

Comments
 (0)