use of com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec in project cloudstack by apache.
the class VmwareResource method postNvpConfigBeforeStart.
private static void postNvpConfigBeforeStart(VirtualMachineMO vmMo, VirtualMachineTO vmSpec) throws Exception {
/**
* We need to configure the port on the DV switch after the host is
* connected. So make this happen between the configure and start of
* the VM
*/
int nicIndex = 0;
for (NicTO nicTo : sortNicsByDeviceId(vmSpec.getNics())) {
if (nicTo.getBroadcastType() == BroadcastDomainType.Lswitch) {
// We need to create a port with a unique vlan and pass the key to the nic device
s_logger.trace("Nic " + nicTo.toString() + " is connected to an NVP logicalswitch");
VirtualDevice nicVirtualDevice = vmMo.getNicDeviceByIndex(nicIndex);
if (nicVirtualDevice == null) {
//FIXME Generic exceptions are bad
throw new Exception("Failed to find a VirtualDevice for nic " + nicIndex);
}
VirtualDeviceBackingInfo backing = nicVirtualDevice.getBacking();
if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
// This NIC is connected to a Distributed Virtual Switch
VirtualEthernetCardDistributedVirtualPortBackingInfo portInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
DistributedVirtualSwitchPortConnection port = portInfo.getPort();
String portKey = port.getPortKey();
String portGroupKey = port.getPortgroupKey();
String dvSwitchUuid = port.getSwitchUuid();
s_logger.debug("NIC " + nicTo.toString() + " is connected to dvSwitch " + dvSwitchUuid + " pg " + portGroupKey + " port " + portKey);
ManagedObjectReference dvSwitchManager = vmMo.getContext().getVimClient().getServiceContent().getDvSwitchManager();
ManagedObjectReference dvSwitch = vmMo.getContext().getVimClient().getService().queryDvsByUuid(dvSwitchManager, dvSwitchUuid);
// Get all ports
DistributedVirtualSwitchPortCriteria criteria = new DistributedVirtualSwitchPortCriteria();
criteria.setInside(true);
criteria.getPortgroupKey().add(portGroupKey);
List<DistributedVirtualPort> dvPorts = vmMo.getContext().getVimClient().getService().fetchDVPorts(dvSwitch, criteria);
DistributedVirtualPort vmDvPort = null;
List<Integer> usedVlans = new ArrayList<Integer>();
for (DistributedVirtualPort dvPort : dvPorts) {
// Find the port for this NIC by portkey
if (portKey.equals(dvPort.getKey())) {
vmDvPort = dvPort;
}
VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
usedVlans.add(vlanId.getVlanId());
}
}
if (vmDvPort == null) {
throw new Exception("Empty port list from dvSwitch for nic " + nicTo.toString());
}
DVPortConfigInfo dvPortConfigInfo = vmDvPort.getConfig();
VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPortConfigInfo.getSetting();
VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
BoolPolicy blocked = settings.getBlocked();
if (blocked.isValue() == Boolean.TRUE) {
s_logger.trace("Port is blocked, set a vlanid and unblock");
DVPortConfigSpec dvPortConfigSpec = new DVPortConfigSpec();
VMwareDVSPortSetting edittedSettings = new VMwareDVSPortSetting();
// Unblock
blocked.setValue(Boolean.FALSE);
blocked.setInherited(Boolean.FALSE);
edittedSettings.setBlocked(blocked);
// Set vlan
int i;
for (i = 1; i < 4095; i++) {
if (!usedVlans.contains(i))
break;
}
// FIXME should be a determined
vlanId.setVlanId(i);
// based on usage
vlanId.setInherited(false);
edittedSettings.setVlan(vlanId);
dvPortConfigSpec.setSetting(edittedSettings);
dvPortConfigSpec.setOperation("edit");
dvPortConfigSpec.setKey(portKey);
List<DVPortConfigSpec> dvPortConfigSpecs = new ArrayList<DVPortConfigSpec>();
dvPortConfigSpecs.add(dvPortConfigSpec);
ManagedObjectReference task = vmMo.getContext().getVimClient().getService().reconfigureDVPortTask(dvSwitch, dvPortConfigSpecs);
if (!vmMo.getContext().getVimClient().waitForTask(task)) {
throw new Exception("Failed to configure the dvSwitch port for nic " + nicTo.toString());
}
s_logger.debug("NIC " + nicTo.toString() + " connected to vlan " + i);
} else {
s_logger.trace("Port already configured and set to vlan " + vlanId.getVlanId());
}
} else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
// This NIC is connected to a Virtual Switch
// Nothing to do
} else if (backing instanceof VirtualEthernetCardOpaqueNetworkBackingInfo) {
//if NSX API VERSION >= 4.2, connect to br-int (nsx.network), do not create portgroup else previous behaviour
//OK, connected to OpaqueNetwork
} else {
s_logger.error("nic device backing is of type " + backing.getClass().getName());
//FIXME Generic exceptions are bad
throw new Exception("Incompatible backing for a VirtualDevice for nic " + nicIndex);
}
}
nicIndex++;
}
}
use of com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec in project cloudstack by apache.
the class HypervisorHostHelper method createDVPortVlanIdSpec.
public static VmwareDistributedVirtualSwitchVlanIdSpec createDVPortVlanIdSpec(Integer vlanId) {
VmwareDistributedVirtualSwitchVlanIdSpec vlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
vlanIdSpec.setVlanId(vlanId == null ? 0 : vlanId.intValue());
return vlanIdSpec;
}
use of com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec in project cloudstack by apache.
the class HypervisorHostHelper method isSpecMatch.
public static boolean isSpecMatch(DVPortgroupConfigInfo currentDvPortgroupInfo, DVPortgroupConfigSpec newDvPortGroupSpec) {
String dvPortGroupName = newDvPortGroupSpec.getName();
s_logger.debug("Checking if configuration of dvPortGroup [" + dvPortGroupName + "] has changed.");
boolean specMatches = true;
DVSTrafficShapingPolicy currentTrafficShapingPolicy;
currentTrafficShapingPolicy = currentDvPortgroupInfo.getDefaultPortConfig().getInShapingPolicy();
assert (currentTrafficShapingPolicy != null);
LongPolicy oldAverageBandwidthPolicy = currentTrafficShapingPolicy.getAverageBandwidth();
LongPolicy oldBurstSizePolicy = currentTrafficShapingPolicy.getBurstSize();
LongPolicy oldPeakBandwidthPolicy = currentTrafficShapingPolicy.getPeakBandwidth();
BoolPolicy oldIsEnabledPolicy = currentTrafficShapingPolicy.getEnabled();
Long oldAverageBandwidth = null;
Long oldBurstSize = null;
Long oldPeakBandwidth = null;
Boolean oldIsEnabled = null;
if (oldAverageBandwidthPolicy != null) {
oldAverageBandwidth = oldAverageBandwidthPolicy.getValue();
}
if (oldBurstSizePolicy != null) {
oldBurstSize = oldBurstSizePolicy.getValue();
}
if (oldPeakBandwidthPolicy != null) {
oldPeakBandwidth = oldPeakBandwidthPolicy.getValue();
}
if (oldIsEnabledPolicy != null) {
oldIsEnabled = oldIsEnabledPolicy.isValue();
}
DVSTrafficShapingPolicy newTrafficShapingPolicyInbound = newDvPortGroupSpec.getDefaultPortConfig().getInShapingPolicy();
LongPolicy newAverageBandwidthPolicy = newTrafficShapingPolicyInbound.getAverageBandwidth();
LongPolicy newBurstSizePolicy = newTrafficShapingPolicyInbound.getBurstSize();
LongPolicy newPeakBandwidthPolicy = newTrafficShapingPolicyInbound.getPeakBandwidth();
BoolPolicy newIsEnabledPolicy = newTrafficShapingPolicyInbound.getEnabled();
Long newAverageBandwidth = null;
Long newBurstSize = null;
Long newPeakBandwidth = null;
Boolean newIsEnabled = null;
if (newAverageBandwidthPolicy != null) {
newAverageBandwidth = newAverageBandwidthPolicy.getValue();
}
if (newBurstSizePolicy != null) {
newBurstSize = newBurstSizePolicy.getValue();
}
if (newPeakBandwidthPolicy != null) {
newPeakBandwidth = newPeakBandwidthPolicy.getValue();
}
if (newIsEnabledPolicy != null) {
newIsEnabled = newIsEnabledPolicy.isValue();
}
if (!oldIsEnabled.equals(newIsEnabled)) {
s_logger.info("Detected change in state of shaping policy (enabled/disabled) [" + newIsEnabled + "]");
specMatches = false;
}
if (oldIsEnabled || newIsEnabled) {
if (oldAverageBandwidth != null && !oldAverageBandwidth.equals(newAverageBandwidth)) {
s_logger.info("Average bandwidth setting in new shaping policy doesn't match the existing setting.");
specMatches = false;
} else if (oldBurstSize != null && !oldBurstSize.equals(newBurstSize)) {
s_logger.info("Burst size setting in new shaping policy doesn't match the existing setting.");
specMatches = false;
} else if (oldPeakBandwidth != null && !oldPeakBandwidth.equals(newPeakBandwidth)) {
s_logger.info("Peak bandwidth setting in new shaping policy doesn't match the existing setting.");
specMatches = false;
}
}
boolean oldAutoExpandSetting = currentDvPortgroupInfo.isAutoExpand();
boolean autoExpandEnabled = newDvPortGroupSpec.isAutoExpand();
if (oldAutoExpandSetting != autoExpandEnabled) {
specMatches = false;
}
if (!autoExpandEnabled) {
// Allow update of number of dvports per dvPortGroup is auto expand is not enabled.
int oldNumPorts = currentDvPortgroupInfo.getNumPorts();
int newNumPorts = newDvPortGroupSpec.getNumPorts();
if (oldNumPorts < newNumPorts) {
s_logger.info("Need to update the number of dvports for dvPortGroup :[" + dvPortGroupName + "] from existing number of dvports " + oldNumPorts + " to " + newNumPorts);
specMatches = false;
} else if (oldNumPorts > newNumPorts) {
s_logger.warn("Detected that new number of dvports [" + newNumPorts + "] in dvPortGroup [" + dvPortGroupName + "] is less than existing number of dvports [" + oldNumPorts + "]. Attempt to update this dvPortGroup may fail!");
specMatches = false;
}
}
VmwareDistributedVirtualSwitchVlanIdSpec oldVlanSpec = (VmwareDistributedVirtualSwitchVlanIdSpec) ((VMwareDVSPortSetting) currentDvPortgroupInfo.getDefaultPortConfig()).getVlan();
VmwareDistributedVirtualSwitchVlanIdSpec newVlanSpec = (VmwareDistributedVirtualSwitchVlanIdSpec) ((VMwareDVSPortSetting) newDvPortGroupSpec.getDefaultPortConfig()).getVlan();
int oldVlanId = oldVlanSpec.getVlanId();
int newVlanId = newVlanSpec.getVlanId();
if (oldVlanId != newVlanId) {
s_logger.info("Detected that new VLAN [" + newVlanId + "] of dvPortGroup [" + dvPortGroupName + "] is different from current VLAN [" + oldVlanId + "]");
specMatches = false;
}
return specMatches;
}
use of com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec in project cloudstack by apache.
the class HypervisorHostHelperTest method testIsSpecMatchConfigSpecWithMoreDvPortsAndAutoExpandEnabled.
@Test
public void testIsSpecMatchConfigSpecWithMoreDvPortsAndAutoExpandEnabled() throws Exception {
int currentNumPorts = 512;
int currentvlanId = 100;
boolean currentAutoExpand = true;
DVSTrafficShapingPolicy currentTrafficShapingPolicy = new DVSTrafficShapingPolicy();
BoolPolicy currentIsEnabled = new BoolPolicy();
currentIsEnabled.setValue(true);
LongPolicy currentAvgBw = new LongPolicy();
currentAvgBw.setValue(200L);
LongPolicy currentBurstSize = new LongPolicy();
currentBurstSize.setValue(400L);
LongPolicy currentPeakBw = new LongPolicy();
currentPeakBw.setValue(2000L);
VMwareDVSPortSetting currentVmwareDvsPortSetting = new VMwareDVSPortSetting();
VmwareDistributedVirtualSwitchVlanIdSpec currentVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
currentVlanIdSpec.setVlanId(currentvlanId);
currentVmwareDvsPortSetting.setVlan(currentVlanIdSpec);
currentTrafficShapingPolicy.setAverageBandwidth(currentAvgBw);
currentTrafficShapingPolicy.setBurstSize(currentBurstSize);
currentTrafficShapingPolicy.setPeakBandwidth(currentPeakBw);
currentTrafficShapingPolicy.setEnabled(currentIsEnabled);
currentVmwareDvsPortSetting.setInShapingPolicy(currentTrafficShapingPolicy);
when(currentDvPortgroupInfo.getNumPorts()).thenReturn(currentNumPorts);
when(currentDvPortgroupInfo.isAutoExpand()).thenReturn(currentAutoExpand);
when(currentDvPortgroupInfo.getDefaultPortConfig()).thenReturn(currentVmwareDvsPortSetting);
int newNumPorts = 256;
int newvlanId = 100;
boolean newAutoExpand = true;
DVSTrafficShapingPolicy newTrafficShapingPolicy = new DVSTrafficShapingPolicy();
BoolPolicy newIsEnabled = new BoolPolicy();
newIsEnabled.setValue(true);
LongPolicy newAvgBw = new LongPolicy();
newAvgBw.setValue(200L);
LongPolicy newBurstSize = new LongPolicy();
newBurstSize.setValue(400L);
LongPolicy newPeakBw = new LongPolicy();
newPeakBw.setValue(2000L);
VMwareDVSPortSetting newVmwareDvsPortSetting = new VMwareDVSPortSetting();
VmwareDistributedVirtualSwitchVlanIdSpec newVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
newVlanIdSpec.setVlanId(newvlanId);
newVmwareDvsPortSetting.setVlan(newVlanIdSpec);
newTrafficShapingPolicy.setAverageBandwidth(newAvgBw);
newTrafficShapingPolicy.setBurstSize(newBurstSize);
newTrafficShapingPolicy.setPeakBandwidth(newPeakBw);
newTrafficShapingPolicy.setEnabled(newIsEnabled);
newVmwareDvsPortSetting.setInShapingPolicy(newTrafficShapingPolicy);
when(dvPortgroupConfigSpec.getNumPorts()).thenReturn(newNumPorts);
when(dvPortgroupConfigSpec.isAutoExpand()).thenReturn(newAutoExpand);
when(dvPortgroupConfigSpec.getDefaultPortConfig()).thenReturn(newVmwareDvsPortSetting);
boolean specCompareResult = HypervisorHostHelper.isSpecMatch(currentDvPortgroupInfo, dvPortgroupConfigSpec);
assertTrue(specCompareResult);
}
use of com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec in project cloudstack by apache.
the class HypervisorHostHelperTest method testIsSpecMatchConfigSpecWithCurrentShapingPolicyDisabled.
@Test
public void testIsSpecMatchConfigSpecWithCurrentShapingPolicyDisabled() throws Exception {
int currentNumPorts = 512;
int currentvlanId = 100;
boolean currentAutoExpand = true;
DVSTrafficShapingPolicy currentTrafficShapingPolicy = new DVSTrafficShapingPolicy();
BoolPolicy currentIsEnabled = new BoolPolicy();
currentIsEnabled.setValue(false);
VMwareDVSPortSetting currentVmwareDvsPortSetting = new VMwareDVSPortSetting();
VmwareDistributedVirtualSwitchVlanIdSpec currentVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
currentVlanIdSpec.setVlanId(currentvlanId);
currentVmwareDvsPortSetting.setVlan(currentVlanIdSpec);
currentTrafficShapingPolicy.setEnabled(currentIsEnabled);
currentVmwareDvsPortSetting.setInShapingPolicy(currentTrafficShapingPolicy);
when(currentDvPortgroupInfo.getNumPorts()).thenReturn(currentNumPorts);
when(currentDvPortgroupInfo.isAutoExpand()).thenReturn(currentAutoExpand);
when(currentDvPortgroupInfo.getDefaultPortConfig()).thenReturn(currentVmwareDvsPortSetting);
int newNumPorts = 256;
int newvlanId = 100;
boolean newAutoExpand = true;
DVSTrafficShapingPolicy newTrafficShapingPolicy = new DVSTrafficShapingPolicy();
BoolPolicy newIsEnabled = new BoolPolicy();
newIsEnabled.setValue(true);
LongPolicy newAvgBw = new LongPolicy();
newAvgBw.setValue(200L);
LongPolicy newBurstSize = new LongPolicy();
newBurstSize.setValue(400L);
LongPolicy newPeakBw = new LongPolicy();
newPeakBw.setValue(2000L);
VMwareDVSPortSetting newVmwareDvsPortSetting = new VMwareDVSPortSetting();
VmwareDistributedVirtualSwitchVlanIdSpec newVlanIdSpec = new VmwareDistributedVirtualSwitchVlanIdSpec();
newVlanIdSpec.setVlanId(newvlanId);
newVmwareDvsPortSetting.setVlan(newVlanIdSpec);
newTrafficShapingPolicy.setAverageBandwidth(newAvgBw);
newTrafficShapingPolicy.setBurstSize(newBurstSize);
newTrafficShapingPolicy.setPeakBandwidth(newPeakBw);
newTrafficShapingPolicy.setEnabled(newIsEnabled);
newVmwareDvsPortSetting.setInShapingPolicy(newTrafficShapingPolicy);
when(dvPortgroupConfigSpec.getNumPorts()).thenReturn(newNumPorts);
when(dvPortgroupConfigSpec.isAutoExpand()).thenReturn(newAutoExpand);
when(dvPortgroupConfigSpec.getDefaultPortConfig()).thenReturn(newVmwareDvsPortSetting);
boolean specCompareResult = HypervisorHostHelper.isSpecMatch(currentDvPortgroupInfo, dvPortgroupConfigSpec);
assertFalse(specCompareResult);
}
Aggregations