use of com.vmware.vim25.GuestNicInfo in project photon-model by vmware.
the class VmOverlayTest method setup.
@Before
public void setup() {
ObjectContent cont = new ObjectContent();
ManagedObjectReference ref = new ManagedObjectReference();
ref.setType(VimNames.TYPE_VM);
ref.setValue("vm-123");
cont.setObj(ref);
Map<String, Object> props = new HashMap<>();
ArrayOfGuestNicInfo arrayOfGuestNicInfo = new ArrayOfGuestNicInfo();
List<GuestNicInfo> listGuestNicInfo = arrayOfGuestNicInfo.getGuestNicInfo();
GuestNicInfo nic1 = new GuestNicInfo();
List<String> ipsNic1 = nic1.getIpAddress();
String mac1Address = "00:50:56:8b:54:bd";
String mac2Address = "98:87:fd:9e:ed:6d";
nic1.setMacAddress(mac1Address);
ipsNic1.add("192.168.1.10");
ipsNic1.add("192.168.1.11");
GuestNicInfo nic2 = new GuestNicInfo();
List<String> ipsNic2 = nic2.getIpAddress();
nic2.setMacAddress(mac2Address);
ipsNic2.add("10.10.10.20");
listGuestNicInfo.add(nic1);
listGuestNicInfo.add(nic2);
props.put(vm_guest_net, arrayOfGuestNicInfo);
this.overlay = new VmOverlay(ref, props);
}
use of com.vmware.vim25.GuestNicInfo in project cloudstack by apache.
the class VmwareResource method getUnmanageInstanceNics.
private List<UnmanagedInstanceTO.Nic> getUnmanageInstanceNics(VmwareHypervisorHost hyperHost, VirtualMachineMO vmMo) {
List<UnmanagedInstanceTO.Nic> instanceNics = new ArrayList<>();
HashMap<String, List<String>> guestNicMacIPAddressMap = new HashMap<>();
try {
GuestInfo guestInfo = vmMo.getGuestInfo();
if (guestInfo.getToolsStatus() == VirtualMachineToolsStatus.TOOLS_OK) {
for (GuestNicInfo nicInfo : guestInfo.getNet()) {
if (CollectionUtils.isNotEmpty(nicInfo.getIpAddress())) {
List<String> ipAddresses = new ArrayList<>();
for (String ipAddress : nicInfo.getIpAddress()) {
if (NetUtils.isValidIp4(ipAddress)) {
ipAddresses.add(ipAddress);
}
}
guestNicMacIPAddressMap.put(nicInfo.getMacAddress(), ipAddresses);
}
}
} else {
s_logger.info(String.format("Unable to retrieve guest nics for instance: %s from VMware tools as tools status: %s", vmMo.getName(), guestInfo.getToolsStatus().toString()));
}
} catch (Exception e) {
s_logger.info("Unable to retrieve guest nics for instance from VMware tools. " + e.getMessage());
}
VirtualDevice[] nics = null;
try {
nics = vmMo.getNicDevices();
} catch (Exception e) {
s_logger.info("Unable to retrieve unmanaged instance nics. " + e.getMessage());
}
if (nics != null) {
for (VirtualDevice nic : nics) {
try {
VirtualEthernetCard ethCardDevice = (VirtualEthernetCard) nic;
s_logger.error(nic.getClass().getCanonicalName() + " " + nic.getBacking().getClass().getCanonicalName() + " " + ethCardDevice.getMacAddress());
UnmanagedInstanceTO.Nic instanceNic = new UnmanagedInstanceTO.Nic();
instanceNic.setNicId(ethCardDevice.getDeviceInfo().getLabel());
if (ethCardDevice instanceof VirtualPCNet32) {
instanceNic.setAdapterType(VirtualEthernetCardType.PCNet32.toString());
} else if (ethCardDevice instanceof VirtualVmxnet2) {
instanceNic.setAdapterType(VirtualEthernetCardType.Vmxnet2.toString());
} else if (ethCardDevice instanceof VirtualVmxnet3) {
instanceNic.setAdapterType(VirtualEthernetCardType.Vmxnet3.toString());
} else {
instanceNic.setAdapterType(VirtualEthernetCardType.E1000.toString());
}
instanceNic.setMacAddress(ethCardDevice.getMacAddress());
if (guestNicMacIPAddressMap.containsKey(instanceNic.getMacAddress())) {
instanceNic.setIpAddress(guestNicMacIPAddressMap.get(instanceNic.getMacAddress()));
}
if (ethCardDevice.getSlotInfo() != null) {
instanceNic.setPciSlot(ethCardDevice.getSlotInfo().toString());
}
VirtualDeviceBackingInfo backing = ethCardDevice.getBacking();
if (backing instanceof VirtualEthernetCardDistributedVirtualPortBackingInfo) {
VirtualEthernetCardDistributedVirtualPortBackingInfo backingInfo = (VirtualEthernetCardDistributedVirtualPortBackingInfo) backing;
DistributedVirtualSwitchPortConnection port = backingInfo.getPort();
String portKey = port.getPortKey();
String portGroupKey = port.getPortgroupKey();
String dvSwitchUuid = port.getSwitchUuid();
s_logger.debug("NIC " + nic.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);
for (DistributedVirtualPort dvPort : dvPorts) {
// Find the port for this NIC by portkey
if (portKey.equals(dvPort.getKey())) {
VMwareDVSPortSetting settings = (VMwareDVSPortSetting) dvPort.getConfig().getSetting();
if (settings.getVlan() instanceof VmwareDistributedVirtualSwitchVlanIdSpec) {
VmwareDistributedVirtualSwitchVlanIdSpec vlanId = (VmwareDistributedVirtualSwitchVlanIdSpec) settings.getVlan();
s_logger.trace("Found port " + dvPort.getKey() + " with vlan " + vlanId.getVlanId());
if (vlanId.getVlanId() > 0 && vlanId.getVlanId() < 4095) {
instanceNic.setVlan(vlanId.getVlanId());
}
} else if (settings.getVlan() instanceof VmwareDistributedVirtualSwitchPvlanSpec) {
VmwareDistributedVirtualSwitchPvlanSpec pvlanSpec = (VmwareDistributedVirtualSwitchPvlanSpec) settings.getVlan();
s_logger.trace("Found port " + dvPort.getKey() + " with pvlan " + pvlanSpec.getPvlanId());
if (pvlanSpec.getPvlanId() > 0 && pvlanSpec.getPvlanId() < 4095) {
DistributedVirtualSwitchMO dvSwitchMo = new DistributedVirtualSwitchMO(vmMo.getContext(), dvSwitch);
Pair<Integer, HypervisorHostHelper.PvlanType> vlanDetails = dvSwitchMo.retrieveVlanFromPvlan(pvlanSpec.getPvlanId(), dvSwitch);
if (vlanDetails != null && vlanDetails.first() != null && vlanDetails.second() != null) {
instanceNic.setVlan(vlanDetails.first());
instanceNic.setPvlan(pvlanSpec.getPvlanId());
instanceNic.setPvlanType(vlanDetails.second().toString());
}
}
}
break;
}
}
} else if (backing instanceof VirtualEthernetCardNetworkBackingInfo) {
VirtualEthernetCardNetworkBackingInfo backingInfo = (VirtualEthernetCardNetworkBackingInfo) backing;
instanceNic.setNetwork(backingInfo.getDeviceName());
if (hyperHost instanceof HostMO) {
HostMO hostMo = (HostMO) hyperHost;
HostPortGroupSpec portGroupSpec = hostMo.getHostPortGroupSpec(backingInfo.getDeviceName());
instanceNic.setVlan(portGroupSpec.getVlanId());
}
}
instanceNics.add(instanceNic);
} catch (Exception e) {
s_logger.info("Unable to retrieve unmanaged instance nic info. " + e.getMessage());
}
}
Collections.sort(instanceNics, new Comparator<UnmanagedInstanceTO.Nic>() {
@Override
public int compare(final UnmanagedInstanceTO.Nic nic1, final UnmanagedInstanceTO.Nic nic2) {
return extractInt(nic1) - extractInt(nic2);
}
int extractInt(UnmanagedInstanceTO.Nic nic) {
String num = nic.getNicId().replaceAll("\\D", "");
// return 0 if no digits found
return num.isEmpty() ? 0 : Integer.parseInt(num);
}
});
}
return instanceNics;
}
Aggregations