use of com.vmware.vim25.mo.Network in project cloudstack by apache.
the class VmwareResource method prepareNetworkElementCommand.
private ExecutionResult prepareNetworkElementCommand(IpAssocCommand cmd) {
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
IpAddressTO[] ips = cmd.getIpAddresses();
String routerName = cmd.getAccessDetail(NetworkElementCommand.ROUTER_NAME);
String controlIp = VmwareResource.getRouterSshControlIp(cmd);
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(routerName);
// the check and will try to find it within cluster
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(routerName);
}
}
if (vmMo == null) {
String msg = "Router " + routerName + " no longer exists to execute IPAssoc command";
s_logger.error(msg);
throw new Exception(msg);
}
for (IpAddressTO ip : ips) {
/**
* TODO support other networks
*/
URI broadcastUri = BroadcastDomainType.fromString(ip.getBroadcastUri());
if (BroadcastDomainType.getSchemeValue(broadcastUri) != BroadcastDomainType.Vlan) {
throw new InternalErrorException("Unable to assign a public IP to a VIF on network " + ip.getBroadcastUri());
}
String vlanId = BroadcastDomainType.getValue(broadcastUri);
String publicNeworkName = HypervisorHostHelper.getPublicNetworkNamePrefix(vlanId);
Pair<Integer, VirtualDevice> publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Find public NIC index, public network name: " + publicNeworkName + ", index: " + publicNicInfo.first());
}
boolean addVif = false;
if (ip.isAdd() && publicNicInfo.first().intValue() == -1) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Plug new NIC to associate" + controlIp + " to " + ip.getPublicIp());
}
addVif = true;
}
if (addVif) {
plugPublicNic(vmMo, vlanId, ip.getVifMacAddress());
publicNicInfo = vmMo.getNicDeviceIndex(publicNeworkName);
if (publicNicInfo.first().intValue() >= 0) {
networkUsage(controlIp, "addVif", "eth" + publicNicInfo.first());
}
}
if (publicNicInfo.first().intValue() < 0) {
String msg = "Failed to find DomR VIF to associate/disassociate IP with.";
s_logger.error(msg);
throw new InternalErrorException(msg);
}
ip.setNicDevId(publicNicInfo.first().intValue());
ip.setNewNic(addVif);
}
} catch (Throwable e) {
s_logger.error("Unexpected exception: " + e.toString() + " will shortcut rest of IPAssoc commands", e);
return new ExecutionResult(false, e.toString());
}
return new ExecutionResult(true, null);
}
use of com.vmware.vim25.mo.Network in project cloudstack by apache.
the class VmwareResource method setNuageVspVrIpInExtraConfig.
private static void setNuageVspVrIpInExtraConfig(List<OptionValue> extraOptions, NicTO nicTo, String dvSwitchUuid) {
if (nicTo.getBroadcastType() != BroadcastDomainType.Vsp) {
return;
}
OptionValue newVal;
if (nicTo.getType().equals(TrafficType.Guest) && dvSwitchUuid != null && nicTo.getGateway() != null && nicTo.getNetmask() != null) {
String vrIp = nicTo.getBroadcastUri().getPath().substring(1);
newVal = new OptionValue();
newVal.setKey("vsp.vr-ip." + nicTo.getMac());
newVal.setValue(vrIp);
extraOptions.add(newVal);
newVal = new OptionValue();
newVal.setKey("vsp.dvswitch." + nicTo.getMac());
newVal.setValue(dvSwitchUuid);
extraOptions.add(newVal);
if (s_logger.isDebugEnabled()) {
s_logger.debug("NIC with MAC " + nicTo.getMac() + " and BroadcastDomainType " + nicTo.getBroadcastType() + " in network(" + nicTo.getGateway() + "/" + nicTo.getNetmask() + ") is " + nicTo.getType() + " traffic type. So, vsp-vr-ip is set in the extraconfig");
}
}
}
use of com.vmware.vim25.mo.Network in project cloudstack by apache.
the class VmwareResource method execute.
private PlugNicAnswer execute(PlugNicCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource PlugNicCommand " + _gson.toJson(cmd));
}
getServiceContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
VmwareContext context = getServiceContext();
try {
VmwareHypervisorHost hyperHost = getHyperHost(context);
String vmName = cmd.getVmName();
VirtualMachineMO vmMo = hyperHost.findVmOnHyperHost(vmName);
if (vmMo == null) {
if (hyperHost instanceof HostMO) {
ClusterMO clusterMo = new ClusterMO(hyperHost.getContext(), ((HostMO) hyperHost).getParentMor());
vmMo = clusterMo.findVmOnHyperHost(vmName);
}
}
if (vmMo == null) {
String msg = "Router " + vmName + " no longer exists to execute PlugNic command";
s_logger.error(msg);
throw new Exception(msg);
}
/*
if(!isVMWareToolsInstalled(vmMo)){
String errMsg = "vmware tools is not installed or not running, cannot add nic to vm " + vmName;
s_logger.debug(errMsg);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + errMsg);
}
*/
// Fallback to E1000 if no specific nicAdapter is passed
VirtualEthernetCardType nicDeviceType = VirtualEthernetCardType.E1000;
Map<String, String> details = cmd.getDetails();
if (details != null) {
nicDeviceType = VirtualEthernetCardType.valueOf((String) details.get("nicAdapter"));
}
// find a usable device number in VMware environment
VirtualDevice[] nicDevices = vmMo.getNicDevices();
int deviceNumber = -1;
for (VirtualDevice device : nicDevices) {
if (device.getUnitNumber() > deviceNumber)
deviceNumber = device.getUnitNumber();
}
deviceNumber++;
NicTO nicTo = cmd.getNic();
VirtualDevice nic;
Pair<ManagedObjectReference, String> networkInfo = prepareNetworkFromNicInfo(vmMo.getRunningHost(), nicTo, false, cmd.getVMType());
String dvSwitchUuid = null;
if (VmwareHelper.isDvPortGroup(networkInfo.first())) {
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dataCenterMo = new DatacenterMO(context, dcMor);
ManagedObjectReference dvsMor = dataCenterMo.getDvSwitchMor(networkInfo.first());
dvSwitchUuid = dataCenterMo.getDvSwitchUuid(dvsMor);
s_logger.info("Preparing NIC device on dvSwitch : " + dvSwitchUuid);
nic = VmwareHelper.prepareDvNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), dvSwitchUuid, nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
} else {
s_logger.info("Preparing NIC device on network " + networkInfo.second());
nic = VmwareHelper.prepareNicDevice(vmMo, networkInfo.first(), nicDeviceType, networkInfo.second(), nicTo.getMac(), deviceNumber, deviceNumber + 1, true, true);
}
VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
//VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1];
VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec();
deviceConfigSpec.setDevice(nic);
deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.ADD);
vmConfigSpec.getDeviceChange().add(deviceConfigSpec);
setNuageVspVrIpInExtraConfig(vmConfigSpec.getExtraConfig(), nicTo, dvSwitchUuid);
if (!vmMo.configureVm(vmConfigSpec)) {
throw new Exception("Failed to configure devices when running PlugNicCommand");
}
return new PlugNicAnswer(cmd, true, "success");
} catch (Exception e) {
s_logger.error("Unexpected exception: ", e);
return new PlugNicAnswer(cmd, false, "Unable to execute PlugNicCommand due to " + e.toString());
}
}
use of com.vmware.vim25.mo.Network in project cloudstack by apache.
the class VmwareResource method execute.
protected Answer execute(PrepareForMigrationCommand cmd) {
if (s_logger.isInfoEnabled()) {
s_logger.info("Executing resource PrepareForMigrationCommand: " + _gson.toJson(cmd));
}
VirtualMachineTO vm = cmd.getVirtualMachine();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Preparing host for migrating " + vm);
}
final String vmName = vm.getName();
try {
VmwareHypervisorHost hyperHost = getHyperHost(getServiceContext());
VmwareManager mgr = hyperHost.getContext().getStockObject(VmwareManager.CONTEXT_STOCK_NAME);
// find VM through datacenter (VM is not at the target host yet)
VirtualMachineMO vmMo = hyperHost.findVmOnPeerHyperHost(vmName);
if (vmMo == null) {
s_logger.info("VM " + vmName + " was not found in the cluster of host " + hyperHost.getHyperHostName() + ". Looking for the VM in datacenter.");
ManagedObjectReference dcMor = hyperHost.getHyperHostDatacenter();
DatacenterMO dcMo = new DatacenterMO(hyperHost.getContext(), dcMor);
vmMo = dcMo.findVm(vmName);
if (vmMo == null) {
String msg = "VM " + vmName + " does not exist in VMware datacenter";
s_logger.error(msg);
throw new Exception(msg);
}
}
NicTO[] nics = vm.getNics();
for (NicTO nic : nics) {
// prepare network on the host
prepareNetworkFromNicInfo(new HostMO(getServiceContext(), _morHyperHost), nic, false, cmd.getVirtualMachine().getType());
}
Pair<String, Long> secStoreUrlAndId = mgr.getSecondaryStorageStoreUrlAndId(Long.parseLong(_dcId));
String secStoreUrl = secStoreUrlAndId.first();
Long secStoreId = secStoreUrlAndId.second();
if (secStoreUrl == null) {
String msg = "secondary storage for dc " + _dcId + " is not ready yet?";
throw new Exception(msg);
}
mgr.prepareSecondaryStorageStore(secStoreUrl, secStoreId);
ManagedObjectReference morSecDs = prepareSecondaryDatastoreOnHost(secStoreUrl);
if (morSecDs == null) {
String msg = "Failed to prepare secondary storage on host, secondary store url: " + secStoreUrl;
throw new Exception(msg);
}
return new PrepareForMigrationAnswer(cmd);
} catch (Throwable e) {
if (e instanceof RemoteException) {
s_logger.warn("Encounter remote exception to vCenter, invalidate VMware session context");
invalidateServiceContext();
}
String msg = "Unexcpeted exception " + VmwareHelper.getExceptionMessage(e);
s_logger.error(msg, e);
return new PrepareForMigrationAnswer(cmd, msg);
}
}
use of com.vmware.vim25.mo.Network in project cloudstack by apache.
the class HypervisorHostHelperTest method testIsSpecMatchConfigSpecWithHighBandwidthShapingPolicy.
@Test
public void testIsSpecMatchConfigSpecWithHighBandwidthShapingPolicy() throws Exception {
// Tests case of network offering upgrade in terms of bandwidth
int currentNumPorts = 256;
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(400L);
LongPolicy newBurstSize = new LongPolicy();
newBurstSize.setValue(800L);
LongPolicy newPeakBw = new LongPolicy();
newPeakBw.setValue(4000L);
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