use of com.xensource.xenapi.PIF in project cloudstack by apache.
the class NotAValidCommand method testOvsFetchInterfaceCommand.
@Test
public void testOvsFetchInterfaceCommand() {
final String label = "[abc]";
final String uuid = "befc4dcd-f5c6-4015-8791-3c18622b7c7f";
final Connection conn = Mockito.mock(Connection.class);
final XsLocalNetwork network = Mockito.mock(XsLocalNetwork.class);
final Network network2 = Mockito.mock(Network.class);
final PIF pif = Mockito.mock(PIF.class);
final PIF.Record pifRec = Mockito.mock(PIF.Record.class);
final XsHost xsHost = Mockito.mock(XsHost.class);
final OvsFetchInterfaceCommand fetchInterCommand = new OvsFetchInterfaceCommand(label);
final CitrixRequestWrapper wrapper = CitrixRequestWrapper.getInstance();
assertNotNull(wrapper);
when(citrixResourceBase.isXcp()).thenReturn(true);
when(citrixResourceBase.getLabel()).thenReturn("[abc]");
when(citrixResourceBase.getConnection()).thenReturn(conn);
when(citrixResourceBase.getHost()).thenReturn(xsHost);
try {
when(network.getNetwork()).thenReturn(network2);
when(network.getPif(conn)).thenReturn(pif);
when(network.getPif(conn)).thenReturn(pif);
when(pif.getRecord(conn)).thenReturn(pifRec);
when(network.getNetwork().getUuid(conn)).thenReturn(uuid);
when(citrixResourceBase.getNetworkByName(conn, label)).thenReturn(network);
} catch (final XenAPIException e) {
fail(e.getMessage());
} catch (final XmlRpcException e) {
fail(e.getMessage());
}
final Answer answer = wrapper.execute(fetchInterCommand, citrixResourceBase);
verify(citrixResourceBase, times(1)).getConnection();
assertTrue(answer.getResult());
}
use of com.xensource.xenapi.PIF in project cosmic by MissionCriticalCloud.
the class XsLocalNetwork method getPif.
public PIF getPif(final Connection conn) throws XenAPIException, XmlRpcException {
if (_p == null) {
final Network.Record nr = getNetworkRecord(conn);
for (final PIF pif : nr.PIFs) {
final PIF.Record pr = pif.getRecord(conn);
if (_citrixResourceBase.getHost().getUuid().equals(pr.host.getUuid(conn))) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Found a network called " + nr.nameLabel + " on host=" + _citrixResourceBase.getHost().getIp() + "; Network=" + nr.uuid + "; pif=" + pr.uuid);
}
_p = pif;
_pr = pr;
break;
}
}
}
return _p;
}
use of com.xensource.xenapi.PIF in project cloudstack by apache.
the class XenServer56Resource method disableVlanNetwork.
@Override
public void disableVlanNetwork(final Connection conn, final Network network) {
try {
final Network.Record networkr = network.getRecord(conn);
if (!networkr.nameLabel.startsWith("VLAN")) {
return;
}
final String bridge = networkr.bridge.trim();
for (final PIF pif : networkr.PIFs) {
final PIF.Record pifr = pif.getRecord(conn);
if (!pifr.host.getUuid(conn).equalsIgnoreCase(_host.getUuid())) {
continue;
}
final VLAN vlan = pifr.VLANMasterOf;
if (vlan != null) {
final String vlannum = pifr.VLAN.toString();
final String device = pifr.device.trim();
if (vlannum.equals("-1")) {
return;
}
try {
vlan.destroy(conn);
final Host host = Host.getByUuid(conn, _host.getUuid());
host.forgetDataSourceArchives(conn, "pif_" + bridge + "_tx");
host.forgetDataSourceArchives(conn, "pif_" + bridge + "_rx");
host.forgetDataSourceArchives(conn, "pif_" + device + "." + vlannum + "_tx");
host.forgetDataSourceArchives(conn, "pif_" + device + "." + vlannum + "_rx");
} catch (final XenAPIException e) {
s_logger.trace("Catch " + e.getClass().getName() + ": failed to destory VLAN " + device + " on host " + _host.getUuid() + " due to " + e.toString());
}
}
return;
}
} catch (final XenAPIException e) {
final String msg = "Unable to disable VLAN network due to " + e.toString();
s_logger.warn(msg, e);
} catch (final Exception e) {
final String msg = "Unable to disable VLAN network due to " + e.getMessage();
s_logger.warn(msg, e);
}
}
use of com.xensource.xenapi.PIF in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method enableVlanNetwork.
/**
* enableVlanNetwork creates a Network object, Vlan object, and thereby a tagged PIF object in Xapi.
* <p>
* 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.
* <p>
* 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.
* <p>
* To work around these problems, we do the following.
* <p>
* - 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.
* <p>
* 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<>();
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.PIF in project cosmic by MissionCriticalCloud.
the class CitrixResourceBase method getManagementNetwork.
protected XsLocalNetwork getManagementNetwork(final Connection conn) throws XmlRpcException, XenAPIException {
PIF mgmtPif = null;
PIF.Record mgmtPifRec = null;
final Host host = Host.getByUuid(conn, _host.getUuid());
final Set<PIF> hostPifs = host.getPIFs(conn);
for (final PIF pif : hostPifs) {
final PIF.Record rec = pif.getRecord(conn);
if (rec.management) {
if (rec.VLAN != null && rec.VLAN != -1) {
final String msg = new StringBuilder("Unsupported configuration. Management network is on a VLAN. host=").append(_host.getUuid()).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN).toString();
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
if (s_logger.isDebugEnabled()) {
s_logger.debug("Management network is on pif=" + rec.uuid);
}
mgmtPif = pif;
mgmtPifRec = rec;
break;
}
}
if (mgmtPif == null) {
final String msg = "Unable to find management network for " + _host.getUuid();
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
final Bond bond = mgmtPifRec.bondSlaveOf;
if (!isRefNull(bond)) {
final String msg = "Management interface is on slave(" + mgmtPifRec.uuid + ") of bond(" + bond.getUuid(conn) + ") on host(" + _host.getUuid() + "), please move management interface to bond!";
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
final Network nk = mgmtPifRec.network;
final Network.Record nkRec = nk.getRecord(conn);
return new XsLocalNetwork(this, nk, nkRec, mgmtPif, mgmtPifRec);
}
Aggregations