/** * Capture virtual cdrom attributes in the disk state for reference. */ public static void updateDiskStateFromVirtualDevice(VirtualDevice vd, DiskService.DiskState disk, VirtualDeviceBackingInfo backing) { fillInControllerUnitNumber(disk, vd.getUnitNumber()); if (backing != null && backing instanceof VirtualDeviceFileBackingInfo) { disk.sourceImageReference = VimUtils .datastorePathToUri(((VirtualDeviceFileBackingInfo) backing).getFileName()); } disk.status = DiskService.DiskStatus.ATTACHED; CustomProperties.of(disk) .put(PROVIDER_DISK_UNIQUE_ID, vd.getDeviceInfo().getLabel()); if (vd.getConnectable() != null) { CustomProperties.of(disk) .put(DEVICE_CONNECTED, vd.getConnectable().isConnected()) .put(DEVICE_STATUS, vd.getConnectable().getStatus()); } }
@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()); }
@Test public void testGetOpaqueNetworkBackingInfo() { this.subnet = new SubnetState(); this.subnet.customProperties = new HashMap<>(); this.subnet.customProperties.put(NsxProperties.OPAQUE_NET_ID, UUID.randomUUID().toString()); this.subnet.customProperties.put(NsxProperties.OPAQUE_NET_TYPE, UUID.randomUUID().toString()); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(this.subnet); assertTrue(deviceBackingInfo instanceof VirtualEthernetCardOpaqueNetworkBackingInfo); VirtualEthernetCardOpaqueNetworkBackingInfo opaqueNetworkBackingInfo = (VirtualEthernetCardOpaqueNetworkBackingInfo) deviceBackingInfo; assertEquals(this.subnet.customProperties.get(NsxProperties.OPAQUE_NET_ID), opaqueNetworkBackingInfo.getOpaqueNetworkId()); assertEquals(this.subnet.customProperties.get(NsxProperties.OPAQUE_NET_TYPE), opaqueNetworkBackingInfo.getOpaqueNetworkType()); }
@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()); }
public VirtualDevice getDeviceByBackingFileName(String name) { if(name==null) { throw new IllegalArgumentException("name must not be null!"); } VirtualDevice[] devices = this.getAllVirtualDevices(); for(VirtualDevice device : devices) { VirtualDeviceBackingInfo bi = device.getBacking(); if(bi instanceof VirtualDeviceFileBackingInfo) { String fileName = ((VirtualDeviceFileBackingInfo)bi).getFileName(); if(name.equals(fileName)) return device; } } return null; }
public String getDiskCurrentTopBackingFileInChain(String deviceBusName) throws Exception { List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { s_logger.info("Test against disk device, controller key: " + device.getControllerKey() + ", unit number: " + device.getUnitNumber()); VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; String deviceNumbering = getDeviceBusName(devices, device); if (deviceNumbering.equals(deviceBusName)) return diskBackingInfo.getFileName(); } } } } return null; }
public VirtualMachineDiskInfoBuilder getDiskInfoBuilder() throws Exception { VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder(); List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; while (diskBackingInfo != null) { String deviceBusName = getDeviceBusName(devices, device); builder.addDisk(deviceBusName, diskBackingInfo.getFileName()); diskBackingInfo = diskBackingInfo.getParent(); } } } } } return builder; }
public List<Pair<Integer, ManagedObjectReference>> getAllDiskDatastores() throws Exception { List<Pair<Integer, ManagedObjectReference>> disks = new ArrayList<Pair<Integer, ManagedObjectReference>>(); List<VirtualDevice> devices = _context.getVimClient().getDynamicProperty(_mor, "config.hardware.device"); if (devices != null && devices.size() > 0) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDeviceBackingInfo backingInfo = ((VirtualDisk)device).getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; disks.add(new Pair<Integer, ManagedObjectReference>(new Integer(device.getKey()), diskBackingInfo.getDatastore())); } } } } return disks; }
public static VirtualMachineDiskInfoBuilder getDiskInfoBuilder(List<VirtualDevice> devices) throws Exception { VirtualMachineDiskInfoBuilder builder = new VirtualMachineDiskInfoBuilder(); if (devices != null) { for (VirtualDevice device : devices) { if (device instanceof VirtualDisk) { VirtualDisk virtualDisk = (VirtualDisk)device; VirtualDeviceBackingInfo backingInfo = virtualDisk.getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; String deviceBusName = VMwareUtil.getDeviceBusName(devices, virtualDisk); while (diskBackingInfo != null) { builder.addDisk(deviceBusName, diskBackingInfo.getFileName()); diskBackingInfo = diskBackingInfo.getParent(); } } } } } return builder; }
/** * Get network device's {@link VirtualDeviceBackingInfo} based on the information available * on resource level. The type of custom properties present on a resource decides the type of * device backing. * * @param resource A {@link ResourceState} of type {@link SubnetState} or {@link NetworkState} * * @param queryConfigTargetRequest A {@link QueryConfigTargetRequest} object so to get * config target when needed * * @return {@link VirtualDeviceBackingInfo} */ public static VirtualDeviceBackingInfo getNetworkDeviceBackingInfo(ResourceState resource, QueryConfigTargetRequest queryConfigTargetRequest) { if (resource == null) { return null; } CustomProperties props = CustomProperties.of(resource); if (props.getString(DvsProperties.DVS_UUID) != null || props.getString(DvsProperties.PORT_GROUP_KEY) != null) { // an isolated network created by vSphere adapter or nsx adapter will set this property // NSX-V can only set PORT_GROUP_KEY as DVS_UUID is not available return getDistributedPortBackingInfo(props, queryConfigTargetRequest); } else if (props.getString(NsxProperties.OPAQUE_NET_ID) != null) { // An opaque network // NSX-T will set OpaqueId to subnet's id return getOpaqueNetworkBackingInfo(props); } else if (props.getString(CustomProperties.TYPE) != null && props.getString(CustomProperties.TYPE).equals(VimNames.TYPE_NETWORK)) { // The default case when we want to connect to existing network return getStandardNetworkBackingInfo(resource.name); } else if (resource instanceof NetworkState) { // satisfying an existing code path from InstanceClient. // Do we really need it? return getStandardNetworkBackingInfo(resource.name); } return null; }
private VirtualEthernetCard createNic(NetworkInterfaceStateWithDetails nicWithDetails, Integer controllerKey) throws FinderException, InvalidPropertyFaultMsg, RuntimeFaultFaultMsg { VirtualEthernetCard nic = new VirtualE1000(); nic.setAddressType(VirtualEthernetCardMacType.GENERATED.value()); nic.setKey(-1); nic.setControllerKey(controllerKey); // Currently the network backing information is stored in both places subnet and network // If it were to exist at one state object, then it would reduce complexity further. // Question: Is it acceptable for querying subnet first and network later? Or the order // should be reversed? QueryConfigTargetRequest queryConfigTargetRequest = new QueryConfigTargetRequest(this.get, getVimPort(), this.ctx.computeMoRef); VirtualDeviceBackingInfo deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(nicWithDetails.subnet, queryConfigTargetRequest); if (deviceBackingInfo == null) { deviceBackingInfo = NetworkDeviceBackingFactory .getNetworkDeviceBackingInfo(nicWithDetails.network, queryConfigTargetRequest); } nic.setBacking(deviceBackingInfo); 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 String getVmdkFileBaseName(VirtualDisk disk) throws Exception { String vmdkFileBaseName = null; VirtualDeviceBackingInfo backingInfo = disk.getBacking(); if(backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; DatastoreFile dsBackingFile = new DatastoreFile(diskBackingInfo.getFileName()); vmdkFileBaseName = dsBackingFile.getFileBaseName(); } return vmdkFileBaseName; }
private static ArrayList<Integer> getIndependentVirtualDiskKeys(VirtualMachine vm) throws Exception { ArrayList<Integer> diskKeys = Lists.newArrayList(); VirtualDevice[] devices = (VirtualDevice[]) vm.getPropertyByPath("config.hardware.device"); for (int i = 0; i < devices.length; i++) { if (devices[i] instanceof VirtualDisk) { VirtualDisk vDisk = (VirtualDisk) devices[i]; String diskMode = ""; VirtualDeviceBackingInfo vdbi = vDisk.getBacking(); if (vdbi instanceof VirtualDiskFlatVer1BackingInfo) { diskMode = ((VirtualDiskFlatVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskFlatVer2BackingInfo) { diskMode = ((VirtualDiskFlatVer2BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskRawDiskMappingVer1BackingInfo) { diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskSparseVer1BackingInfo) { diskMode = ((VirtualDiskSparseVer1BackingInfo) vdbi).getDiskMode(); } else if (vdbi instanceof VirtualDiskSparseVer2BackingInfo) { diskMode = ((VirtualDiskSparseVer2BackingInfo) vdbi).getDiskMode(); } if (diskMode.indexOf("independent") != -1) { diskKeys.add(vDisk.getKey()); } } } return diskKeys; }
public static ManagedObjectReference getDiskDeviceDatastore(VirtualDisk diskDevice) throws Exception { VirtualDeviceBackingInfo backingInfo = diskDevice.getBacking(); assert (backingInfo instanceof VirtualDiskFlatVer2BackingInfo); return ((VirtualDiskFlatVer2BackingInfo)backingInfo).getDatastore(); }
public String getAbsoluteVmdkFile(VirtualDisk disk) { String vmdkAbsFile = null; VirtualDeviceBackingInfo backingInfo = disk.getBacking(); if (backingInfo instanceof VirtualDiskFlatVer2BackingInfo) { VirtualDiskFlatVer2BackingInfo diskBackingInfo = (VirtualDiskFlatVer2BackingInfo)backingInfo; vmdkAbsFile = diskBackingInfo.getFileName(); } return vmdkAbsFile; }
private static ArrayList<Integer> getIndependentVirtualDiskKeys(VirtualMachine vm) throws Exception { ArrayList<Integer> _diskKeys = new ArrayList<Integer>(); VirtualDevice[] devices = (VirtualDevice[]) vm.getPropertyByPath("config.hardware.device"); for(int i=0; i<devices.length; i++) { if(devices[i] instanceof VirtualDisk) { VirtualDisk vDisk = (VirtualDisk) devices[i]; String diskMode = ""; VirtualDeviceBackingInfo vdbi = vDisk.getBacking(); if(vdbi instanceof VirtualDiskFlatVer1BackingInfo) { diskMode = ((VirtualDiskFlatVer1BackingInfo) vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskFlatVer2BackingInfo) { diskMode = ((VirtualDiskFlatVer2BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskRawDiskMappingVer1BackingInfo) { diskMode = ((VirtualDiskRawDiskMappingVer1BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskSparseVer1BackingInfo) { diskMode = ((VirtualDiskSparseVer1BackingInfo)vdbi).getDiskMode(); } else if(vdbi instanceof VirtualDiskSparseVer2BackingInfo) { diskMode = ((VirtualDiskSparseVer2BackingInfo)vdbi).getDiskMode(); } if(diskMode.indexOf("independent") != -1) { _diskKeys.add(vDisk.getKey()); } } } return _diskKeys; }
/** * Get network device's {@link VirtualDeviceBackingInfo} based on the information available * on resource level. The type of custom properties present on a resource decides the type of * device backing. Use other overload for querying portgroup information from vsphere. * * @param resource A {@link ResourceState} of type {@link SubnetState} or * {@link NetworkState} * * @return {@link VirtualDeviceBackingInfo} */ public static VirtualDeviceBackingInfo getNetworkDeviceBackingInfo(ResourceState resource) { return getNetworkDeviceBackingInfo(resource, null); }