@Test public void testGetDvsPortBackingInfoWithUUID() { this.subnet = new SubnetState(); this.subnet.customProperties = new HashMap<>(); this.subnet.customProperties.put(DvsProperties.DVS_UUID, UUID.randomUUID().toString()); this.subnet.customProperties.put(DvsProperties.PORT_GROUP_KEY, UUID.randomUUID().toString()); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(this.subnet); assertTrue(deviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo); VirtualEthernetCardDistributedVirtualPortBackingInfo distributedVirtualPortBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) deviceBackingInfo; DistributedVirtualSwitchPortConnection port = distributedVirtualPortBackingInfo.getPort(); assertNotNull(port); assertEquals(this.subnet.customProperties.get(DvsProperties.DVS_UUID), port.getSwitchUuid()); assertEquals(this.subnet.customProperties.get(DvsProperties.PORT_GROUP_KEY), port.getPortgroupKey()); }
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 checkSameDistributedNetwork(VirtualEthernetCard ethernetCard1, VirtualEthernetCard ethernetCard2) { if (!(ethernetCard1.getBacking() instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo)) { return false; } if (!(ethernetCard2.getBacking() instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo)) { return false; } VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo1 = VirtualEthernetCardDistributedVirtualPortBackingInfo.class .cast(ethernetCard1.getBacking()); VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo2 = VirtualEthernetCardDistributedVirtualPortBackingInfo.class .cast(ethernetCard2.getBacking()); return StringUtils.equals(backingInfo1.getPort().getPortgroupKey(), backingInfo2.getPort().getPortgroupKey()); }
public static VirtualDevice prepareDvNicDevice(VirtualMachineMO vmMo, ManagedObjectReference morNetwork, VirtualEthernetCardType deviceType, String dvPortGroupName, String dvSwitchUuid, String macAddress, int contextNumber, boolean connected, boolean connectOnStart) throws Exception { VirtualEthernetCard nic = createVirtualEthernetCard(deviceType); final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection(); dvPortConnection.setSwitchUuid(dvSwitchUuid); dvPortConnection.setPortgroupKey(morNetwork.getValue()); dvPortBacking.setPort(dvPortConnection); nic.setBacking(dvPortBacking); nic.setAddressType("Manual"); nic.setConnectable(getVirtualDeviceConnectInfo(connected, connectOnStart)); nic.setMacAddress(macAddress); nic.setKey(-contextNumber); return nic; }
/** * NSX-V test case when only port group key is present * * Exception as */ @Test(expected = IllegalArgumentException.class) public void testGetDvsPortBackingInfoWithPortGroupKey() { this.subnet = new SubnetState(); this.subnet.customProperties = new HashMap<>(); List<String> portGroupIds = new ArrayList<>(); portGroupIds.add(UUID.randomUUID().toString()); portGroupIds.add(UUID.randomUUID().toString()); this.subnet.customProperties.put(DvsProperties.PORT_GROUP_KEY, Utils.toJson(portGroupIds)); // will throw NPE here as the querytargetrequest is null VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(this.subnet); assertTrue(deviceBackingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo); VirtualEthernetCardDistributedVirtualPortBackingInfo distributedVirtualPortBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) deviceBackingInfo; DistributedVirtualSwitchPortConnection port = distributedVirtualPortBackingInfo.getPort(); assertNotNull(port); // TODO: mock get DVS switch call to vSphere and match the result // for now we are just using the expected exception as without UUID the backing info // cannot be set. assertNull(port.getSwitchUuid()); assertEquals(this.subnet.customProperties.get(DvsProperties.PORT_GROUP_KEY), port.getPortgroupKey()); }
protected static String getVirtualMachineMacAddress( VirtualMachineConfigInfo vmConfigInfo, DistributedVirtualPortgroup portGroup) { VirtualDevice devices[] = vmConfigInfo.getHardware().getDevice(); for (VirtualDevice device : devices) { // Assuming only one interface if (device instanceof VirtualEthernetCard) { VirtualDeviceBackingInfo backingInfo = device.getBacking(); if (backingInfo == null) continue; // Is it backed by the distributed virtual port group? if (backingInfo instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) { VirtualEthernetCardDistributedVirtualPortBackingInfo dvpBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backingInfo; if ((dvpBackingInfo.getPort() == null) || (dvpBackingInfo.getPort().getPortgroupKey() == null)) continue; if (dvpBackingInfo.getPort().getPortgroupKey(). equals(portGroup.getKey())) { String vmMac = ((VirtualEthernetCard) device). getMacAddress(); return vmMac; } } } } s_logger.error("dvPg: " + portGroup.getName() + " vmConfig: " + vmConfigInfo + " MAC Address NOT found"); return null; }
public VirtualEthernetCardDistributedVirtualPortBackingInfo getDvPortBackingInfo(Pair<ManagedObjectReference, String> networkInfo) throws Exception { assert (networkInfo != null); assert (networkInfo.first() != null && networkInfo.first().getType().equalsIgnoreCase("DistributedVirtualPortgroup")); final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection(); ManagedObjectReference dvsMor = getDvSwitchMor(networkInfo.first()); String dvSwitchUuid = getDvSwitchUuid(dvsMor); dvPortConnection.setSwitchUuid(dvSwitchUuid); dvPortConnection.setPortgroupKey(networkInfo.first().getValue()); dvPortBacking.setPort(dvPortConnection); System.out.println("Plugging NIC device into network " + networkInfo.second() + " backed by dvSwitch: " + dvSwitchUuid); return dvPortBacking; }
public String getDvPortGroupName(VirtualEthernetCard nic) throws Exception { VirtualEthernetCardDistributedVirtualPortBackingInfo dvpBackingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo)nic.getBacking(); DistributedVirtualSwitchPortConnection dvsPort = dvpBackingInfo.getPort(); String dvPortGroupKey = dvsPort.getPortgroupKey(); ManagedObjectReference dvPortGroupMor = new ManagedObjectReference(); dvPortGroupMor.setValue(dvPortGroupKey); dvPortGroupMor.setType("DistributedVirtualPortgroup"); return (String)_context.getVimClient().getDynamicProperty(dvPortGroupMor, "name"); }
public static void updateDvNicDevice(VirtualDevice nic, ManagedObjectReference morNetwork, String dvSwitchUuid) throws Exception { final VirtualEthernetCardDistributedVirtualPortBackingInfo dvPortBacking = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); final DistributedVirtualSwitchPortConnection dvPortConnection = new DistributedVirtualSwitchPortConnection(); dvPortConnection.setSwitchUuid(dvSwitchUuid); dvPortConnection.setPortgroupKey(morNetwork.getValue()); dvPortBacking.setPort(dvPortConnection); nic.setBacking(dvPortBacking); }
/** * Backing info for distributed virtual switch port or portgroup */ private static VirtualEthernetCardDistributedVirtualPortBackingInfo getDistributedPortBackingInfo( CustomProperties props, QueryConfigTargetRequest queryConfigTargetRequest) { DistributedVirtualSwitchPortConnection port = new DistributedVirtualSwitchPortConnection(); String portGroupKey = props.getString(DvsProperties.PORT_GROUP_KEY); String dvsUuid = props.getString(DvsProperties.DVS_UUID); if (StringUtil.isNullOrEmpty(dvsUuid)) { // NSX-V sets the value to a list of dvPortGroupsKeys as the logical switch is // created in a transport zone which could be associated with multiple clusters. // Hence dvPortGroup is created per cluster. The configTarget will filter based on // the cluster where machine is being provisioned. Type listType = new TypeToken<ArrayList<String>>(){}.getType(); final List<String> portGroupIds = Utils.fromJson(portGroupKey, listType); // NSX-V doesn't have UUID information in its API response DistributedVirtualPortgroupInfo info = null; try { ConfigTarget configTarget = queryConfigTargetRequest.getConfigTarget(); info = configTarget.getDistributedVirtualPortgroup() .stream() .filter(d -> { return portGroupIds.contains(d.getPortgroupKey()); }) .findFirst() .orElse(null); } catch (Exception e) { logger.error("getDistributedPortBackingInfo::Failed to get dvportgroup info.", e); } if (info == null) { throw new IllegalArgumentException("getDistributedPortBackingInfo::The port group " + "information is not found for key: " + portGroupKey); } portGroupKey = info.getPortgroupKey(); dvsUuid = info.getSwitchUuid(); } port.setPortgroupKey(portGroupKey); port.setSwitchUuid(dvsUuid); VirtualEthernetCardDistributedVirtualPortBackingInfo backing = new VirtualEthernetCardDistributedVirtualPortBackingInfo(); backing.setPort(port); return backing; }