Skip to content

Commit ae505e7

Browse files
Refactoring the LibvirtComputingResource
- Addin LibvirtPvlanSetupCommandWrapper - 6 unit tests added - KVM hypervisor plugin with 21% coverage From the 6 tests added, 2 were extra tests to increase the coverage of the LibvirtStopCommandWrapper - Increased from 35% to 78.7%
1 parent 7fd43f3 commit ae505e7

4 files changed

Lines changed: 352 additions & 61 deletions

File tree

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

Lines changed: 12 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import com.cloud.agent.api.PingCommand;
6969
import com.cloud.agent.api.PingRoutingCommand;
7070
import com.cloud.agent.api.PingRoutingWithNwGroupsCommand;
71-
import com.cloud.agent.api.PvlanSetupCommand;
7271
import com.cloud.agent.api.SetupGuestNetworkCommand;
7372
import com.cloud.agent.api.StartAnswer;
7473
import com.cloud.agent.api.StartCommand;
@@ -397,6 +396,18 @@ public String manageSnapshotPath() {
397396
return _manageSnapshotPath;
398397
}
399398

399+
public String getGuestBridgeName() {
400+
return _guestBridgeName;
401+
}
402+
403+
public String getOvsPvlanDhcpHostPath() {
404+
return _ovsPvlanDhcpHostPath;
405+
}
406+
407+
public String getOvsPvlanVmPath() {
408+
return _ovsPvlanVmPath;
409+
}
410+
400411
private static final class KeyValueInterpreter extends OutputInterpreter {
401412
private final Map<String, String> map = new HashMap<String, String>();
402413

@@ -1252,8 +1263,6 @@ public Answer executeRequest(final Command cmd) {
12521263
return execute((ResizeVolumeCommand)cmd);
12531264
} else if (cmd instanceof StorageSubSystemCommand) {
12541265
return storageHandler.handleStorageCommands((StorageSubSystemCommand)cmd);
1255-
} else if (cmd instanceof PvlanSetupCommand) {
1256-
return execute((PvlanSetupCommand)cmd);
12571266
} else {
12581267
s_logger.warn("Unsupported command ");
12591268
return Answer.createUnsupportedCommandAnswer(cmd);
@@ -1519,61 +1528,6 @@ private String getBroadcastUriFromBridge(final String brName) {
15191528
}
15201529
}
15211530

1522-
private Answer execute(final PvlanSetupCommand cmd) {
1523-
final String primaryPvlan = cmd.getPrimary();
1524-
final String isolatedPvlan = cmd.getIsolated();
1525-
final String op = cmd.getOp();
1526-
final String dhcpName = cmd.getDhcpName();
1527-
final String dhcpMac = cmd.getDhcpMac();
1528-
final String dhcpIp = cmd.getDhcpIp();
1529-
final String vmMac = cmd.getVmMac();
1530-
boolean add = true;
1531-
1532-
String opr = "-A";
1533-
if (op.equals("delete")) {
1534-
opr = "-D";
1535-
add = false;
1536-
}
1537-
1538-
String result = null;
1539-
Connect conn;
1540-
try {
1541-
if (cmd.getType() == PvlanSetupCommand.Type.DHCP) {
1542-
final Script script = new Script(_ovsPvlanDhcpHostPath, _timeout, s_logger);
1543-
if (add) {
1544-
conn = LibvirtConnection.getConnectionByVmName(dhcpName);
1545-
final List<InterfaceDef> ifaces = getInterfaces(conn, dhcpName);
1546-
final InterfaceDef guestNic = ifaces.get(0);
1547-
script.add(opr, "-b", _guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac, "-I",
1548-
guestNic.getDevName());
1549-
} else {
1550-
script.add(opr, "-b", _guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac);
1551-
}
1552-
result = script.execute();
1553-
if (result != null) {
1554-
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
1555-
return new Answer(cmd, false, result);
1556-
} else {
1557-
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
1558-
}
1559-
} else if (cmd.getType() == PvlanSetupCommand.Type.VM) {
1560-
final Script script = new Script(_ovsPvlanVmPath, _timeout, s_logger);
1561-
script.add(opr, "-b", _guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-v", vmMac);
1562-
result = script.execute();
1563-
if (result != null) {
1564-
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
1565-
return new Answer(cmd, false, result);
1566-
} else {
1567-
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
1568-
}
1569-
}
1570-
} catch (final LibvirtException e) {
1571-
// TODO Auto-generated catch block
1572-
e.printStackTrace();
1573-
}
1574-
return new Answer(cmd, true, result);
1575-
}
1576-
15771531
private void VifHotPlug(final Connect conn, final String vmName, final String broadcastUri, final String macAddr) throws InternalErrorException, LibvirtException {
15781532
final NicTO nicTO = new NicTO();
15791533
nicTO.setMac(macAddr);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 org.apache.log4j.Logger;
25+
import org.libvirt.Connect;
26+
import org.libvirt.LibvirtException;
27+
28+
import com.cloud.agent.api.Answer;
29+
import com.cloud.agent.api.PvlanSetupCommand;
30+
import com.cloud.hypervisor.kvm.resource.LibvirtComputingResource;
31+
import com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef;
32+
import com.cloud.resource.CommandWrapper;
33+
import com.cloud.utils.script.Script;
34+
35+
public final class LibvirtPvlanSetupCommandWrapper extends CommandWrapper<PvlanSetupCommand, Answer, LibvirtComputingResource> {
36+
37+
private static final Logger s_logger = Logger.getLogger(LibvirtPvlanSetupCommandWrapper.class);
38+
39+
@Override
40+
public Answer execute(final PvlanSetupCommand command, final LibvirtComputingResource libvirtComputingResource) {
41+
final String primaryPvlan = command.getPrimary();
42+
final String isolatedPvlan = command.getIsolated();
43+
final String op = command.getOp();
44+
final String dhcpName = command.getDhcpName();
45+
final String dhcpMac = command.getDhcpMac();
46+
final String dhcpIp = command.getDhcpIp();
47+
final String vmMac = command.getVmMac();
48+
boolean add = true;
49+
50+
String opr = "-A";
51+
if (op.equals("delete")) {
52+
opr = "-D";
53+
add = false;
54+
}
55+
56+
String result = null;
57+
try {
58+
final String guestBridgeName = libvirtComputingResource.getGuestBridgeName();
59+
final int timeout = libvirtComputingResource.getTimeout();
60+
61+
if (command.getType() == PvlanSetupCommand.Type.DHCP) {
62+
final String ovsPvlanDhcpHostPath = libvirtComputingResource.getOvsPvlanDhcpHostPath();
63+
final Script script = new Script(ovsPvlanDhcpHostPath, timeout, s_logger);
64+
65+
if (add) {
66+
final LibvirtUtilitiesHelper libvirtUtilitiesHelper = libvirtComputingResource.getLibvirtUtilitiesHelper();
67+
final Connect conn = libvirtUtilitiesHelper.getConnectionByVmName(dhcpName);
68+
69+
final List<InterfaceDef> ifaces = libvirtComputingResource.getInterfaces(conn, dhcpName);
70+
final InterfaceDef guestNic = ifaces.get(0);
71+
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac, "-I",
72+
guestNic.getDevName());
73+
} else {
74+
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-n", dhcpName, "-d", dhcpIp, "-m", dhcpMac);
75+
}
76+
77+
result = script.execute();
78+
79+
if (result != null) {
80+
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
81+
return new Answer(command, false, result);
82+
} else {
83+
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
84+
}
85+
} else if (command.getType() == PvlanSetupCommand.Type.VM) {
86+
final String ovsPvlanVmPath = libvirtComputingResource.getOvsPvlanVmPath();
87+
88+
final Script script = new Script(ovsPvlanVmPath, timeout, s_logger);
89+
script.add(opr, "-b", guestBridgeName, "-p", primaryPvlan, "-i", isolatedPvlan, "-v", vmMac);
90+
result = script.execute();
91+
92+
if (result != null) {
93+
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
94+
return new Answer(command, false, result);
95+
} else {
96+
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
97+
}
98+
}
99+
} catch (final LibvirtException e) {
100+
s_logger.error("Error whislt executing OVS Setup command! ==> " + e.getMessage());
101+
return new Answer(command, false, e.getMessage());
102+
}
103+
return new Answer(command, true, result);
104+
}
105+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import com.cloud.agent.api.PingTestCommand;
6060
import com.cloud.agent.api.PlugNicCommand;
6161
import com.cloud.agent.api.PrepareForMigrationCommand;
62+
import com.cloud.agent.api.PvlanSetupCommand;
6263
import com.cloud.agent.api.ReadyCommand;
6364
import com.cloud.agent.api.RebootCommand;
6465
import com.cloud.agent.api.RebootRouterCommand;
@@ -146,6 +147,7 @@ private void init() {
146147
linbvirtCommands.put(BackupSnapshotCommand.class, new LibvirtBackupSnapshotCommandWrapper());
147148
linbvirtCommands.put(CreatePrivateTemplateFromSnapshotCommand.class, new LibvirtCreatePrivateTemplateFromSnapshotCommandWrapper());
148149
linbvirtCommands.put(CopyVolumeCommand.class, new LibvirtCopyVolumeCommandWrapper());
150+
linbvirtCommands.put(PvlanSetupCommand.class, new LibvirtPvlanSetupCommandWrapper());
149151

150152
resources.put(LibvirtComputingResource.class, linbvirtCommands);
151153
}

0 commit comments

Comments
 (0)