private static void replaceNetworkAdapter( VirtualMachineConfigSpec vmConfigSpec, VirtualDevice oldNIC, ManagedObjectReference newNetworkRef, String newNetworkName) throws Exception { logger.debug("new network: " + newNetworkName); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(newNetworkName); nicBacking.setNetwork(newNetworkRef); nicBacking.setUseAutoDetect(true); oldNIC.setBacking(nicBacking); VirtualDeviceConnectInfo info = new VirtualDeviceConnectInfo(); info.setConnected(true); info.setStartConnected(true); info.setAllowGuestControl(true); oldNIC.setConnectable(info); // oldNIC.getConnectable().setConnected(true); // oldNIC.getConnectable().setStartConnected(true); VirtualDeviceConfigSpec vmDeviceSpec = new VirtualDeviceConfigSpec(); vmDeviceSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT); vmDeviceSpec.setDevice(oldNIC); vmConfigSpec.getDeviceChange().add(vmDeviceSpec); }
@Test public void testStandardNetworkBackingInfo() { this.subnet = new SubnetState(); this.subnet.name = UUID.randomUUID().toString(); this.subnet.customProperties = new HashMap<>(); this.subnet.customProperties.put(CustomProperties.TYPE, VimNames.TYPE_NETWORK); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(this.subnet); assertTrue(deviceBackingInfo instanceof VirtualEthernetCardNetworkBackingInfo); VirtualEthernetCardNetworkBackingInfo virtualEthernetCardNetworkBackingInfo = (VirtualEthernetCardNetworkBackingInfo) deviceBackingInfo; assertEquals(this.subnet.name, virtualEthernetCardNetworkBackingInfo.getDeviceName()); }
@Test public void testNetworkStateBackingInfo() { this.network = new NetworkState(); this.network.name = UUID.randomUUID().toString(); this.network.customProperties = new HashMap<>(); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(this.network); assertTrue(deviceBackingInfo instanceof VirtualEthernetCardNetworkBackingInfo); VirtualEthernetCardNetworkBackingInfo virtualEthernetCardNetworkBackingInfo = (VirtualEthernetCardNetworkBackingInfo) deviceBackingInfo; assertEquals(this.network.name, virtualEthernetCardNetworkBackingInfo.getDeviceName()); }
protected VirtualEthernetCard editEthernetCards(VirtualMachine machine, String networkName) { // ネットワークアダプタ E1000を使用する VirtualEthernetCard ethernetCard = new VirtualE1000(); // 分散ポートグループ情報取得 DistributedVirtualPortgroupInfo dvPortgroupInfo = getDVPortgroupInfo(machine, networkName); if (dvPortgroupInfo != null) { // 分散ポートグループの場合 VirtualEthernetCardDistributedVirtualPortBackingInfo nicBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); nicBacking.setPort(new DistributedVirtualSwitchPortConnection()); nicBacking.getPort().setPortgroupKey(dvPortgroupInfo.getPortgroupKey()); nicBacking.getPort().setSwitchUuid(dvPortgroupInfo.getSwitchUuid()); ethernetCard.setBacking(nicBacking); } else { // 標準ポートグループの場合 VirtualEthernetCardNetworkBackingInfo backingInfo = new VirtualEthernetCardNetworkBackingInfo(); backingInfo.setDeviceName(networkName); ethernetCard.setBacking(backingInfo); } return ethernetCard; }
protected boolean checkSameNormalNetwork(VirtualEthernetCard ethernetCard1, VirtualEthernetCard ethernetCard2) { if (!(ethernetCard1.getBacking() instanceof VirtualEthernetCardNetworkBackingInfo)) { return false; } if (!(ethernetCard2.getBacking() instanceof VirtualEthernetCardNetworkBackingInfo)) { return false; } VirtualEthernetCardNetworkBackingInfo backingInfo1 = VirtualEthernetCardNetworkBackingInfo.class .cast(ethernetCard1.getBacking()); VirtualEthernetCardNetworkBackingInfo backingInfo2 = VirtualEthernetCardNetworkBackingInfo.class .cast(ethernetCard2.getBacking()); return StringUtils.equals(backingInfo1.getDeviceName(), backingInfo2.getDeviceName()); }
static VirtualDeviceConfigSpec createNicSpec(String netName, String nicName) throws Exception { VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); nicSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(netName); Description info = new Description(); info.setLabel(nicName); info.setSummary(netName); nic.setDeviceInfo(info); // type: "generated", "manual", "assigned" by VC nic.setAddressType("generated"); nic.setBacking(nicBacking); nic.setKey(0); nicSpec.setDevice(nic); return nicSpec; }
public static VirtualDevice prepareNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String portGroupName, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception { VirtualEthernetCard nic = createVirtualEthernetCard(deviceType); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(portGroupName); nicBacking.setNetwork(morNetwork); nic.setBacking(nicBacking); nic.setAddressType("Manual"); nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart)); nic.setMacAddress(macAddress); nic.setKey(-contextNumber); return nic; }
/** * Backing info for existing network */ private static VirtualEthernetCardNetworkBackingInfo getStandardNetworkBackingInfo( String name) { VirtualEthernetCardNetworkBackingInfo backing = new VirtualEthernetCardNetworkBackingInfo(); backing.setDeviceName(name); return backing; }
private VirtualEthernetCard getEth(String fileName, String addressType, Integer key) { VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(fileName); VirtualEthernetCard nic = new VirtualPCNet32(); nic.setBacking(nicBacking); nic.setAddressType(addressType); nic.setKey(key); return nic; }
static VirtualDeviceConfigSpec getNICDeviceConfigSpec( VirtualMachine vm, String op, String name) throws Exception { VirtualDeviceConfigSpec nicSpec = new VirtualDeviceConfigSpec(); VirtualMachineConfigInfo vmConfigInfo = vm.getConfig(); if("add".equalsIgnoreCase(op) && doesNetworkNameExist(vm, name)) { nicSpec.setOperation(VirtualDeviceConfigSpecOperation.add); VirtualEthernetCard nic = new VirtualPCNet32(); VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(name); nic.setAddressType("generated"); nic.setBacking(nicBacking); nic.setKey(4); nicSpec.setDevice(nic); return nicSpec; } else if("remove".equalsIgnoreCase(op)) { VirtualDevice [] vds = vmConfigInfo.getHardware().getDevice(); nicSpec.setOperation( VirtualDeviceConfigSpecOperation.remove); for(int i=0;i<vds.length;i++) { if((vds[i] instanceof VirtualEthernetCard) && (vds[i].getDeviceInfo().getLabel().equalsIgnoreCase( name))) { nicSpec.setDevice(vds[i]); return nicSpec; } } } return null; }
public static void updateNicDevice(VirtualDevice nic, ManagedObjectReference morNetwork, String portGroupName) throws Exception { VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(portGroupName); nicBacking.setNetwork(morNetwork); nic.setBacking(nicBacking); }
private void plugPublicNic(VirtualMachineMO vmMo, final String vlanId, final IpAddressTO ipAddressTO) throws Exception { // TODO : probably need to set traffic shaping Pair<ManagedObjectReference, String> networkInfo = null; VirtualSwitchType vSwitchType = VirtualSwitchType.StandardVirtualSwitch; if (_publicTrafficInfo != null) { vSwitchType = _publicTrafficInfo.getVirtualSwitchType(); } /** FIXME We have no clue which network this nic is on and that means that we can't figure out the BroadcastDomainType * so we assume that it's VLAN for now */ if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) { networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, ipAddressTO.getNetworkRate(), null, _opsTimeout, true, BroadcastDomainType.Vlan, null, null); } else { networkInfo = HypervisorHostHelper.prepareNetwork(_publicTrafficInfo.getVirtualSwitchName(), "cloud.public", vmMo.getRunningHost(), vlanId, null, ipAddressTO.getNetworkRate(), null, _opsTimeout, vSwitchType, _portsPerDvPortGroup, null, false, BroadcastDomainType.Vlan, _vsmCredentials, null); } int nicIndex = allocPublicNicIndex(vmMo); try { VirtualDevice[] nicDevices = vmMo.getNicDevices(); VirtualEthernetCard device = (VirtualEthernetCard)nicDevices[nicIndex]; if (VirtualSwitchType.StandardVirtualSwitch == vSwitchType) { VirtualEthernetCardNetworkBackingInfo nicBacking = new VirtualEthernetCardNetworkBackingInfo(); nicBacking.setDeviceName(networkInfo.second()); nicBacking.setNetwork(networkInfo.first()); device.setBacking(nicBacking); } else { HostMO hostMo = vmMo.getRunningHost(); DatacenterMO dataCenterMo = new DatacenterMO(hostMo.getContext(), hostMo.getHyperHostDatacenter()); device.setBacking(dataCenterMo.getDvPortBackingInfo(networkInfo)); } VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec(); //VirtualDeviceConfigSpec[] deviceConfigSpecArray = new VirtualDeviceConfigSpec[1]; VirtualDeviceConfigSpec deviceConfigSpec = new VirtualDeviceConfigSpec(); deviceConfigSpec.setDevice(device); deviceConfigSpec.setOperation(VirtualDeviceConfigSpecOperation.EDIT); vmConfigSpec.getDeviceChange().add(deviceConfigSpec); if (!vmMo.configureVm(vmConfigSpec)) { throw new Exception("Failed to configure devices when plugPublicNic"); } } catch (Exception e) { // restore allocation mask in case of exceptions String nicMasksStr = vmMo.getCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK); int nicMasks = Integer.parseInt(nicMasksStr); nicMasks &= ~(1 << nicIndex); vmMo.setCustomFieldValue(CustomFieldConstants.CLOUD_NIC_MASK, String.valueOf(nicMasks)); throw e; } }