Search in sources :

Example 1 with GuestNicInfo

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);
}
Also used : ObjectContent(com.vmware.vim25.ObjectContent) HashMap(java.util.HashMap) GuestNicInfo(com.vmware.vim25.GuestNicInfo) ArrayOfGuestNicInfo(com.vmware.vim25.ArrayOfGuestNicInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) ArrayOfGuestNicInfo(com.vmware.vim25.ArrayOfGuestNicInfo) Before(org.junit.Before)

Example 2 with GuestNicInfo

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;
}
Also used : VMwareDVSPortSetting(com.vmware.vim25.VMwareDVSPortSetting) VirtualEthernetCardDistributedVirtualPortBackingInfo(com.vmware.vim25.VirtualEthernetCardDistributedVirtualPortBackingInfo) VirtualEthernetCardNetworkBackingInfo(com.vmware.vim25.VirtualEthernetCardNetworkBackingInfo) HashMap(java.util.HashMap) VirtualPCNet32(com.vmware.vim25.VirtualPCNet32) HostMO(com.cloud.hypervisor.vmware.mo.HostMO) ArrayList(java.util.ArrayList) VirtualVmxnet2(com.vmware.vim25.VirtualVmxnet2) HostPortGroupSpec(com.vmware.vim25.HostPortGroupSpec) VirtualVmxnet3(com.vmware.vim25.VirtualVmxnet3) DistributedVirtualSwitchPortConnection(com.vmware.vim25.DistributedVirtualSwitchPortConnection) GuestNicInfo(com.vmware.vim25.GuestNicInfo) DistributedVirtualSwitchMO(com.cloud.hypervisor.vmware.mo.DistributedVirtualSwitchMO) UnmanagedInstanceTO(org.apache.cloudstack.vm.UnmanagedInstanceTO) HypervisorHostHelper(com.cloud.hypervisor.vmware.mo.HypervisorHostHelper) DistributedVirtualSwitchPortCriteria(com.vmware.vim25.DistributedVirtualSwitchPortCriteria) ArrayList(java.util.ArrayList) List(java.util.List) VmwareDistributedVirtualSwitchPvlanSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchPvlanSpec) VirtualEthernetCard(com.vmware.vim25.VirtualEthernetCard) VmwareDistributedVirtualSwitchVlanIdSpec(com.vmware.vim25.VmwareDistributedVirtualSwitchVlanIdSpec) Pair(com.cloud.utils.Pair) DistributedVirtualPort(com.vmware.vim25.DistributedVirtualPort) VirtualDevice(com.vmware.vim25.VirtualDevice) VirtualDeviceBackingInfo(com.vmware.vim25.VirtualDeviceBackingInfo) ConnectException(java.net.ConnectException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) InternalErrorException(com.cloud.exception.InternalErrorException) CloudException(com.cloud.exception.CloudException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ConfigurationException(javax.naming.ConfigurationException) GuestInfo(com.vmware.vim25.GuestInfo) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Aggregations

GuestNicInfo (com.vmware.vim25.GuestNicInfo)2 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)2 HashMap (java.util.HashMap)2 CloudException (com.cloud.exception.CloudException)1 InternalErrorException (com.cloud.exception.InternalErrorException)1 DistributedVirtualSwitchMO (com.cloud.hypervisor.vmware.mo.DistributedVirtualSwitchMO)1 HostMO (com.cloud.hypervisor.vmware.mo.HostMO)1 HypervisorHostHelper (com.cloud.hypervisor.vmware.mo.HypervisorHostHelper)1 Pair (com.cloud.utils.Pair)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 ArrayOfGuestNicInfo (com.vmware.vim25.ArrayOfGuestNicInfo)1 DistributedVirtualPort (com.vmware.vim25.DistributedVirtualPort)1 DistributedVirtualSwitchPortConnection (com.vmware.vim25.DistributedVirtualSwitchPortConnection)1 DistributedVirtualSwitchPortCriteria (com.vmware.vim25.DistributedVirtualSwitchPortCriteria)1 GuestInfo (com.vmware.vim25.GuestInfo)1 HostPortGroupSpec (com.vmware.vim25.HostPortGroupSpec)1 ObjectContent (com.vmware.vim25.ObjectContent)1 VMwareDVSPortSetting (com.vmware.vim25.VMwareDVSPortSetting)1 VirtualDevice (com.vmware.vim25.VirtualDevice)1 VirtualDeviceBackingInfo (com.vmware.vim25.VirtualDeviceBackingInfo)1