use of com.xensource.xenapi.Network in project cloudstack by apache.
the class NotAValidCommand method testOvsDeleteFlowCommandFailure.
@Test
public void testOvsDeleteFlowCommandFailure() {
final String bridge = "gre";
final Connection conn = Mockito.mock(Connection.class);
final Network network = Mockito.mock(Network.class);
final OvsDeleteFlowCommand deleteFlowCommand = new OvsDeleteFlowCommand("Test");
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.getConnection()).thenReturn(conn);
when(citrixResourceBase.setupvSwitchNetwork(conn)).thenReturn(network);
try {
when(network.getBridge(conn)).thenReturn(bridge);
when(citrixResourceBase.callHostPlugin(conn, "ovsgre", "ovs_delete_flow", "bridge", bridge, "vmName", deleteFlowCommand.getVmName())).thenReturn("FAILED");
} catch (final BadServerResponse e) {
fail(e.getMessage());
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
final Answer answer = wrapper.execute(deleteFlowCommand, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
verify(citrixResourceBase, times(1)).setIsOvs(true);
assertFalse(answer.getResult());
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixResourceBase method enableVlanNetwork.
/**
* enableVlanNetwork creates a Network object, Vlan object, and thereby a
* tagged PIF object in Xapi.
*
* In XenServer, VLAN is added by - Create a network, which is unique
* cluster wide. - Find the PIF that you want to create the VLAN on. -
* Create a VLAN using the network and the PIF. As a result of this
* operation, a tagged PIF object is also created.
*
* Here is a list of problems with clustered Xapi implementation that we are
* trying to circumvent. - There can be multiple Networks with the same
* name-label so searching using name-label is not unique. - There are no
* other ways to search for Networks other than listing all of them which is
* not efficient in our implementation because we can have over 4000 VLAN
* networks. - In a clustered situation, it's possible for both hosts to
* detect that the Network is missing and both creates it. This causes a lot
* of problems as one host may be using one Network and another may be using
* a different network for their VMs. This causes problems in migration
* because the VMs are logically attached to different networks in Xapi's
* database but in reality, they are attached to the same network.
*
* To work around these problems, we do the following.
*
* - When creating the VLAN network, we name it as VLAN-UUID of the Network
* it is created on-VLAN Tag. Because VLAN tags is unique with one
* particular network, this is a unique name-label to quickly retrieve the
* the VLAN network with when we need it again. - When we create the VLAN
* network, we add a timestamp and a random number as a tag into the
* network. Then instead of creating VLAN on that network, we actually
* retrieve the Network again and this time uses the VLAN network with
* lowest timestamp or lowest random number as the VLAN network. This allows
* VLAN creation to happen on multiple hosts concurrently but even if two
* VLAN networks were created with the same name, only one of them is used.
*
* One cavaet about this approach is that it relies on the timestamp to be
* relatively accurate among different hosts.
*
* @param conn
* Xapi Connection
* @param tag
* VLAN tag
* @param network
* network on this host to create the VLAN on.
* @return VLAN Network created.
* @throws XenAPIException
* @throws XmlRpcException
*/
protected Network enableVlanNetwork(final Connection conn, final long tag, final XsLocalNetwork network) throws XenAPIException, XmlRpcException {
Network vlanNetwork = null;
final String oldName = "VLAN" + Long.toString(tag);
final String newName = "VLAN-" + network.getNetworkRecord(conn).uuid + "-" + tag;
XsLocalNetwork vlanNic = getNetworkByName(conn, newName);
if (vlanNic == null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Couldn't find vlan network with the new name so trying old name: " + oldName);
}
vlanNic = getNetworkByName(conn, oldName);
if (vlanNic != null) {
s_logger.info("Renaming VLAN with old name " + oldName + " to " + newName);
vlanNic.getNetwork().setNameLabel(conn, newName);
}
}
if (vlanNic == null) {
// Can't find it, then create it.
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating VLAN network for " + tag + " on host " + _host.getIp());
}
final Network.Record nwr = new Network.Record();
nwr.nameLabel = newName;
nwr.tags = new HashSet<String>();
nwr.tags.add(generateTimeStamp());
vlanNetwork = Network.create(conn, nwr);
vlanNic = getNetworkByName(conn, newName);
if (vlanNic == null) {
// capture happened.
throw new CloudRuntimeException("Could not find/create vlan network with name: " + newName);
}
}
final PIF nPif = network.getPif(conn);
final PIF.Record nPifr = network.getPifRecord(conn);
vlanNetwork = vlanNic.getNetwork();
if (vlanNic.getPif(conn) != null) {
return vlanNetwork;
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Creating VLAN " + tag + " on host " + _host.getIp() + " on device " + nPifr.device);
}
final VLAN vlan = VLAN.create(conn, nPif, tag, vlanNetwork);
if (vlan != null) {
final VLAN.Record vlanr = vlan.getRecord(conn);
if (vlanr != null) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("VLAN is created for " + tag + ". The uuid is " + vlanr.uuid);
}
}
}
return vlanNetwork;
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixResourceBase method setupLinkLocalNetwork.
public void setupLinkLocalNetwork(final Connection conn) {
try {
final Network.Record rec = new Network.Record();
final Set<Network> networks = Network.getByNameLabel(conn, _linkLocalPrivateNetworkName);
Network linkLocal = null;
if (networks.size() == 0) {
rec.nameDescription = "link local network used by system vms";
rec.nameLabel = _linkLocalPrivateNetworkName;
final Map<String, String> configs = new HashMap<String, String>();
configs.put("ip_begin", NetUtils.getLinkLocalGateway());
configs.put("ip_end", NetUtils.getLinkLocalIpEnd());
configs.put("netmask", NetUtils.getLinkLocalNetMask());
configs.put("vswitch-disable-in-band", "true");
rec.otherConfig = configs;
linkLocal = Network.create(conn, rec);
} else {
linkLocal = networks.iterator().next();
if (!linkLocal.getOtherConfig(conn).containsKey("vswitch-disable-in-band")) {
linkLocal.addToOtherConfig(conn, "vswitch-disable-in-band", "true");
}
}
/* Make sure there is a physical bridge on this network */
VIF dom0vif = null;
final Pair<VM, VM.Record> vm = getControlDomain(conn);
final VM dom0 = vm.first();
final Set<VIF> vifs = dom0.getVIFs(conn);
if (vifs.size() != 0) {
for (final VIF vif : vifs) {
final Map<String, String> otherConfig = vif.getOtherConfig(conn);
if (otherConfig != null) {
final String nameLabel = otherConfig.get("nameLabel");
if (nameLabel != null && nameLabel.equalsIgnoreCase("link_local_network_vif")) {
dom0vif = vif;
}
}
}
}
/* create temp VIF0 */
if (dom0vif == null) {
s_logger.debug("Can't find a vif on dom0 for link local, creating a new one");
final VIF.Record vifr = new VIF.Record();
vifr.VM = dom0;
vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0);
if (vifr.device == null) {
s_logger.debug("Failed to create link local network, no vif available");
return;
}
final Map<String, String> config = new HashMap<String, String>();
config.put("nameLabel", "link_local_network_vif");
vifr.otherConfig = config;
vifr.MAC = "FE:FF:FF:FF:FF:FF";
vifr.network = linkLocal;
vifr.lockingMode = Types.VifLockingMode.NETWORK_DEFAULT;
dom0vif = VIF.create(conn, vifr);
plugDom0Vif(conn, dom0vif);
} else {
s_logger.debug("already have a vif on dom0 for link local network");
if (!dom0vif.getCurrentlyAttached(conn)) {
plugDom0Vif(conn, dom0vif);
}
}
final String brName = linkLocal.getBridge(conn);
callHostPlugin(conn, "vmops", "setLinkLocalIP", "brName", brName);
_host.setLinkLocalNetwork(linkLocal.getUuid(conn));
} catch (final XenAPIException e) {
s_logger.warn("Unable to create local link network", e);
throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
} catch (final XmlRpcException e) {
s_logger.warn("Unable to create local link network", e);
throw new CloudRuntimeException("Unable to create local link network due to " + e.toString(), e);
}
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class CitrixResourceBase method getCorrectVif.
protected VIF getCorrectVif(final Connection conn, final VM router, final IpAddressTO ip) throws XmlRpcException, XenAPIException {
final NicTO nic = new NicTO();
nic.setType(ip.getTrafficType());
nic.setName(ip.getNetworkName());
if (ip.getBroadcastUri() == null) {
nic.setBroadcastType(BroadcastDomainType.Native);
} else {
final URI uri = BroadcastDomainType.fromString(ip.getBroadcastUri());
nic.setBroadcastType(BroadcastDomainType.getSchemeValue(uri));
nic.setBroadcastUri(uri);
}
final Network network = getNetwork(conn, nic);
// Determine the correct VIF on DomR to associate/disassociate the
// IP address with
final Set<VIF> routerVIFs = router.getVIFs(conn);
for (final VIF vif : routerVIFs) {
final Network vifNetwork = vif.getNetwork(conn);
if (vifNetwork.getUuid(conn).equals(network.getUuid(conn))) {
return vif;
}
}
return null;
}
use of com.xensource.xenapi.Network in project cloudstack by apache.
the class XenServer610MigrateWithStorageReceiveCommandWrapper method execute.
@Override
public Answer execute(final MigrateWithStorageReceiveCommand command, final XenServer610Resource xenServer610Resource) {
final Connection connection = xenServer610Resource.getConnection();
final VirtualMachineTO vmSpec = command.getVirtualMachine();
final List<Pair<VolumeTO, String>> volumeToStorageUuid = command.getVolumeToStorageUuid();
try {
// In a cluster management server setup, the migrate with storage receive and send
// commands and answers may have to be forwarded to another management server. This
// happens when the host/resource on which the command has to be executed is owned
// by the second management server. The serialization/deserialization of the command
// and answers fails as the xapi SR and Network class type isn't understand by the
// agent attache. Seriliaze the SR and Network objects here to a string and pass in
// the answer object. It'll be deserialzed and object created in migrate with
// storage send command execution.
Gson gson = new Gson();
// Get a map of all the SRs to which the vdis will be migrated.
final List<Pair<VolumeTO, Object>> volumeToSr = new ArrayList<>();
for (final Pair<VolumeTO, String> entry : volumeToStorageUuid) {
final String storageUuid = entry.second();
final SR sr = xenServer610Resource.getStorageRepository(connection, storageUuid);
volumeToSr.add(new Pair<VolumeTO, Object>(entry.first(), sr));
}
// Get the list of networks to which the vifs will attach.
final List<Pair<NicTO, Object>> nicToNetwork = new ArrayList<Pair<NicTO, Object>>();
for (final NicTO nicTo : vmSpec.getNics()) {
final Network network = xenServer610Resource.getNetwork(connection, nicTo);
nicToNetwork.add(new Pair<NicTO, Object>(nicTo, network));
}
final XsLocalNetwork nativeNetworkForTraffic = xenServer610Resource.getNativeNetworkForTraffic(connection, TrafficType.Storage, null);
final Network network = nativeNetworkForTraffic.getNetwork();
final XsHost xsHost = xenServer610Resource.getHost();
final String uuid = xsHost.getUuid();
final Map<String, String> other = new HashMap<String, String>();
other.put("live", "true");
final Host host = Host.getByUuid(connection, uuid);
final Map<String, String> token = host.migrateReceive(connection, network, other);
return new MigrateWithStorageReceiveAnswer(command, volumeToSr, nicToNetwork, token);
} catch (final CloudRuntimeException e) {
s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
return new MigrateWithStorageReceiveAnswer(command, e);
} catch (final Exception e) {
s_logger.error("Migration of vm " + vmSpec.getName() + " with storage failed due to " + e.toString(), e);
return new MigrateWithStorageReceiveAnswer(command, e);
}
}
Aggregations