use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class NotAValidCommand method testCleanupNetworkRulesCmd.
@Test
public void testCleanupNetworkRulesCmd() {
final Connection conn = Mockito.mock(Connection.class);
final XsHost xsHost = Mockito.mock(XsHost.class);
final CleanupNetworkRulesCmd cleanupNets = new CleanupNetworkRulesCmd(20);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.canBridgeFirewall()).thenReturn(true);
when(citrixResourceBase.getConnection()).thenReturn(conn);
when(citrixResourceBase.getHost()).thenReturn(xsHost);
when(citrixResourceBase.getVMInstanceName()).thenReturn("VM");
when(citrixResourceBase.callHostPlugin(conn, "vmops", "cleanup_rules", "instance", citrixResourceBase.getVMInstanceName())).thenReturn("1");
final Answer answer = wrapper.execute(cleanupNets, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
assertTrue(answer.getResult());
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class CitrixPrepareForMigrationCommandWrapper method execute.
@Override
public Answer execute(final PrepareForMigrationCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final VirtualMachineTO vm = command.getVirtualMachine();
List<String[]> vmDataList = vm.getVmData();
String configDriveLabel = vm.getConfigDriveLabel();
if (configDriveLabel == null) {
configDriveLabel = "config";
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Preparing host for migrating " + vm);
}
final NicTO[] nics = vm.getNics();
try {
citrixResourceBase.prepareISO(conn, vm.getName(), vmDataList, configDriveLabel);
for (final NicTO nic : nics) {
citrixResourceBase.getNetwork(conn, nic);
}
s_logger.debug("4. The VM " + vm.getName() + " is in Migrating state");
return new PrepareForMigrationAnswer(command);
} catch (final Exception e) {
s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e);
return new PrepareForMigrationAnswer(command, e);
}
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class CitrixPrimaryStorageDownloadCommandWrapper method execute.
@Override
public Answer execute(final PrimaryStorageDownloadCommand command, final CitrixResourceBase citrixResourceBase) {
final String tmplturl = command.getUrl();
final String poolName = command.getPoolUuid();
final int wait = command.getWait();
try {
final URI uri = new URI(tmplturl);
final String tmplpath = uri.getHost() + ":" + uri.getPath();
final Connection conn = citrixResourceBase.getConnection();
SR poolsr = null;
final Set<SR> srs = SR.getByNameLabel(conn, poolName);
if (srs.size() != 1) {
final String msg = "There are " + srs.size() + " SRs with same name: " + poolName;
s_logger.warn(msg);
return new PrimaryStorageDownloadAnswer(msg);
} else {
poolsr = srs.iterator().next();
}
final String pUuid = poolsr.getUuid(conn);
final boolean isISCSI = citrixResourceBase.IsISCSI(poolsr.getType(conn));
final String uuid = citrixResourceBase.copyVhdFromSecondaryStorage(conn, tmplpath, pUuid, wait);
final VDI tmpl = citrixResourceBase.getVDIbyUuid(conn, uuid);
final VDI snapshotvdi = tmpl.snapshot(conn, new HashMap<String, String>());
final String snapshotUuid = snapshotvdi.getUuid(conn);
snapshotvdi.setNameLabel(conn, "Template " + command.getName());
final String parentuuid = citrixResourceBase.getVhdParent(conn, pUuid, snapshotUuid, isISCSI);
final VDI parent = citrixResourceBase.getVDIbyUuid(conn, parentuuid);
final Long phySize = parent.getPhysicalUtilisation(conn);
tmpl.destroy(conn);
poolsr.scan(conn);
try {
Thread.sleep(5000);
} catch (final Exception e) {
}
return new PrimaryStorageDownloadAnswer(snapshotvdi.getUuid(conn), phySize);
} catch (final Exception e) {
final String msg = "Catch Exception " + e.getClass().getName() + " on host:" + citrixResourceBase.getHost().getUuid() + " for template: " + tmplturl + " due to " + e.toString();
s_logger.warn(msg, e);
return new PrimaryStorageDownloadAnswer(msg);
}
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class CitrixPvlanSetupCommandWrapper method execute.
@Override
public Answer execute(final PvlanSetupCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final String primaryPvlan = command.getPrimary();
final String isolatedPvlan = command.getIsolated();
final String op = command.getOp();
final String dhcpName = command.getDhcpName();
final String dhcpMac = command.getDhcpMac();
final String dhcpIp = command.getDhcpIp();
final String vmMac = command.getVmMac();
final String networkTag = command.getNetworkTag();
String nwNameLabel = null;
try {
final XsLocalNetwork nw = citrixResourceBase.getNativeNetworkForTraffic(conn, TrafficType.Guest, networkTag);
if (nw == null) {
s_logger.error("Network is not configured on the backend for pvlan " + primaryPvlan);
throw new CloudRuntimeException("Network for the backend is not configured correctly for pvlan primary: " + primaryPvlan);
}
nwNameLabel = nw.getNetwork().getNameLabel(conn);
} catch (final XenAPIException e) {
s_logger.warn("Fail to get network", e);
return new Answer(command, false, e.toString());
} catch (final XmlRpcException e) {
s_logger.warn("Fail to get network", e);
return new Answer(command, false, e.toString());
}
String result = null;
if (command.getType() == PvlanSetupCommand.Type.DHCP) {
result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-dhcp", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "dhcp-name", dhcpName, "dhcp-ip", dhcpIp, "dhcp-mac", dhcpMac);
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
s_logger.warn("Failed to program pvlan for dhcp server with mac " + dhcpMac);
return new Answer(command, false, result);
} else {
s_logger.info("Programmed pvlan for dhcp server with mac " + dhcpMac);
}
} else if (command.getType() == PvlanSetupCommand.Type.VM) {
result = citrixResourceBase.callHostPlugin(conn, "ovs-pvlan", "setup-pvlan-vm", "op", op, "nw-label", nwNameLabel, "primary-pvlan", primaryPvlan, "isolated-pvlan", isolatedPvlan, "vm-mac", vmMac);
if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) {
s_logger.warn("Failed to program pvlan for vm with mac " + vmMac);
return new Answer(command, false, result);
} else {
s_logger.info("Programmed pvlan for vm with mac " + vmMac);
}
}
return new Answer(command, true, result);
}
use of com.xensource.xenapi.Connection in project cloudstack by apache.
the class CitrixReadyCommandWrapper method execute.
@Override
public Answer execute(final ReadyCommand command, final CitrixResourceBase citrixResourceBase) {
final Connection conn = citrixResourceBase.getConnection();
final Long dcId = command.getDataCenterId();
// Ignore the result of the callHostPlugin. Even if unmounting the
// snapshots dir fails, let Ready command
// succeed.
citrixResourceBase.umountSnapshotDir(conn, dcId);
citrixResourceBase.setupLinkLocalNetwork(conn);
// try to destroy CD-ROM device for all system VMs on this host
try {
final Host host = Host.getByUuid(conn, citrixResourceBase.getHost().getUuid());
final Set<VM> vms = host.getResidentVMs(conn);
for (final VM vm : vms) {
citrixResourceBase.destroyPatchVbd(conn, vm.getNameLabel(conn));
}
} catch (final Exception e) {
}
try {
final boolean result = citrixResourceBase.cleanupHaltedVms(conn);
if (!result) {
return new ReadyAnswer(command, "Unable to cleanup halted vms");
}
} catch (final XenAPIException e) {
s_logger.warn("Unable to cleanup halted vms", e);
return new ReadyAnswer(command, "Unable to cleanup halted vms");
} catch (final XmlRpcException e) {
s_logger.warn("Unable to cleanup halted vms", e);
return new ReadyAnswer(command, "Unable to cleanup halted vms");
}
return new ReadyAnswer(command);
}
Aggregations