Search in sources :

Example 1 with V3NIC

use of org.ovirt.engine.api.v3.types.V3NIC in project ovirt-engine by oVirt.

the class V3NICHelper method setNetworkAndPortMirroring.

/**
 * Populates the {@code network} and {@code port_mirroring} attributes used in V3.
 *
 * @param v4Nic the V4 NIC where the details of the NIC profile will be extracted from
 * @param v3Nic the V3 NIC object that will be populated
 */
public static void setNetworkAndPortMirroring(Nic v4Nic, V3NIC v3Nic) {
    // Do nothing if the V4 NIC doesn't specify a profile:
    VnicProfile v4Profile = v4Nic.getVnicProfile();
    if (v4Profile == null) {
        return;
    }
    // Retrieve the complete representation of the profile, as we need it to compute the "network" and
    // "port_mirroring" attributes used in V3:
    BackendApiResource systemService = BackendApiResource.getInstance();
    VnicProfilesResource profilesService = systemService.getVnicProfilesResource();
    VnicProfileResource profileService = profilesService.getProfileResource(v4Profile.getId());
    v4Profile = profileService.get();
    // Populate the "network" and "port_mirroring" attributes of the V3 object:
    Network v4Network = v4Profile.getNetwork();
    V3Network v3Network = new V3Network();
    v3Network.setId(v4Network.getId());
    v4Network.setHref(v4Network.getHref());
    v3Nic.setNetwork(v3Network);
    if (v4Profile.isSetPortMirroring() && v4Profile.isPortMirroring()) {
        V3PortMirroring v3PortMirroring = new V3PortMirroring();
        V3Networks v3PortMirroringNetworks = new V3Networks();
        V3Network v3PortMirroringNetwork = new V3Network();
        v3PortMirroringNetwork.setId(v4Network.getId());
        v3PortMirroringNetwork.setHref(v4Network.getHref());
        v3PortMirroringNetworks.getNetworks().add(v3PortMirroringNetwork);
        v3PortMirroring.setNetworks(v3PortMirroringNetworks);
        v3Nic.setPortMirroring(v3PortMirroring);
    }
}
Also used : BackendApiResource(org.ovirt.engine.api.restapi.resource.BackendApiResource) VnicProfilesResource(org.ovirt.engine.api.resource.VnicProfilesResource) V3Networks(org.ovirt.engine.api.v3.types.V3Networks) V3Network(org.ovirt.engine.api.v3.types.V3Network) V3Network(org.ovirt.engine.api.v3.types.V3Network) Network(org.ovirt.engine.api.model.Network) VnicProfile(org.ovirt.engine.api.model.VnicProfile) V3PortMirroring(org.ovirt.engine.api.v3.types.V3PortMirroring) VnicProfileResource(org.ovirt.engine.api.resource.VnicProfileResource)

Example 2 with V3NIC

use of org.ovirt.engine.api.v3.types.V3NIC in project ovirt-engine by oVirt.

the class V3NICHelper method setVnicProfile.

/**
 * Finds the VNIC profile that correspond to the given V3 NIC and assigns it to the given V4 NIC.
 *
 * @param vmId the identifier of the virtual machine that the NIC is going to be added to
 * @param v3Nic the V3 NIC where the information will be extracted from
 * @param v4Nic the V4 NIC that will be populated
 */
public static void setVnicProfile(String vmId, V3NIC v3Nic, Nic v4Nic) {
    // Do nothing if the profile is already set:
    if (v4Nic.isSetVnicProfile()) {
        return;
    }
    // In version 4 of the API the "network" and "port_mirroring" properties of the NIC have been completely
    // removed, and instead of using them it is required to specify a VNIC profile. This means that if the user
    // isn't explicitly indicating the VNIC profile we need to find one that is compatible with the given network
    // and port mirroring configuration. The only VNIC profiles to consider are the ones corresponding to the
    // networks of the cluster where the VM resides, so we need to retrieve the VM, then the cluster, and then the
    // identifiers of the networks:
    SystemResource systemService = BackendApiResource.getInstance();
    VmsResource vmsService = systemService.getVmsResource();
    VmResource vmService = vmsService.getVmResource(vmId);
    Vm vm = vmService.get();
    ClustersResource clustersService = systemService.getClustersResource();
    ClusterResource clusterService = clustersService.getClusterResource(vm.getCluster().getId());
    ClusterNetworksResource networksService = clusterService.getNetworksResource();
    Set<String> validNetworkIds = networksService.list().getNetworks().stream().map(Network::getId).collect(toSet());
    // Find a VNIC profile that is in the set of valid networks and that is compatible with the NIC:
    VnicProfilesResource profilesService = systemService.getVnicProfilesResource();
    profilesService.list().getVnicProfiles().stream().filter(profile -> validNetworkIds.contains(profile.getNetwork().getId())).filter(profile -> isProfileCompatible(profile, v3Nic)).sorted(comparing(VnicProfile::getName)).map(VnicProfile::getId).findFirst().ifPresent(id -> {
        VnicProfile v4Profile = new VnicProfile();
        v4Profile.setId(id);
        v4Nic.setVnicProfile(v4Profile);
    });
}
Also used : VmResource(org.ovirt.engine.api.resource.VmResource) VnicProfilesResource(org.ovirt.engine.api.resource.VnicProfilesResource) VnicProfileResource(org.ovirt.engine.api.resource.VnicProfileResource) BackendApiResource(org.ovirt.engine.api.restapi.resource.BackendApiResource) SystemResource(org.ovirt.engine.api.resource.SystemResource) V3NIC(org.ovirt.engine.api.v3.types.V3NIC) V3PortMirroring(org.ovirt.engine.api.v3.types.V3PortMirroring) Vm(org.ovirt.engine.api.model.Vm) Set(java.util.Set) ClusterResource(org.ovirt.engine.api.resource.ClusterResource) NetworkResource(org.ovirt.engine.api.resource.NetworkResource) V3Network(org.ovirt.engine.api.v3.types.V3Network) VnicProfile(org.ovirt.engine.api.model.VnicProfile) Objects(java.util.Objects) V3Networks(org.ovirt.engine.api.v3.types.V3Networks) Nic(org.ovirt.engine.api.model.Nic) ClusterNetworksResource(org.ovirt.engine.api.resource.ClusterNetworksResource) Network(org.ovirt.engine.api.model.Network) VnicProfilesResource(org.ovirt.engine.api.resource.VnicProfilesResource) Comparator.comparing(java.util.Comparator.comparing) NetworksResource(org.ovirt.engine.api.resource.NetworksResource) VmResource(org.ovirt.engine.api.resource.VmResource) Collectors.toSet(java.util.stream.Collectors.toSet) VmsResource(org.ovirt.engine.api.resource.VmsResource) ClustersResource(org.ovirt.engine.api.resource.ClustersResource) VmsResource(org.ovirt.engine.api.resource.VmsResource) Vm(org.ovirt.engine.api.model.Vm) VnicProfile(org.ovirt.engine.api.model.VnicProfile) SystemResource(org.ovirt.engine.api.resource.SystemResource) ClusterNetworksResource(org.ovirt.engine.api.resource.ClusterNetworksResource) ClustersResource(org.ovirt.engine.api.resource.ClustersResource) ClusterResource(org.ovirt.engine.api.resource.ClusterResource)

Example 3 with V3NIC

use of org.ovirt.engine.api.v3.types.V3NIC in project ovirt-engine by oVirt.

the class V3NICHelper method isProfileCompatible.

/**
 * Checks if the given VNIC profile is compatible with the given NIC.
 *
 * @param profile the VNIC profile to check
 * @param nic the NIC to check
 * @return {@code true} iff the profile is compatible with the network and port mirroring configuration of the NIC
 */
private static boolean isProfileCompatible(VnicProfile profile, V3NIC nic) {
    // Retrieve the representation of the network corresponding to the profile, as we are going to need it in
    // order to check the name:
    SystemResource systemService = BackendApiResource.getInstance();
    NetworksResource networksService = systemService.getNetworksResource();
    NetworkResource networkService = networksService.getNetworkResource(profile.getNetwork().getId());
    Network profileNetwork = networkService.get();
    // If the NIC configuration explicitly specifies a network then the profile has to correspond to that same
    // network:
    V3Network nicNetwork = nic.getNetwork();
    if (nicNetwork != null) {
        if (nicNetwork.isSetId()) {
            if (!Objects.equals(profileNetwork.getId(), nicNetwork.getId())) {
                return false;
            }
        }
        if (nicNetwork.isSetName()) {
            if (!Objects.equals(profileNetwork.getName(), nicNetwork.getName())) {
                return false;
            }
        }
    }
    // If the NIC configuration explicitly specifies a port mirroring configuration then the profile must have
    // port mirroring enabled, and all the networks included in the port mirroring configuration must be the same
    // network used by the profile:
    V3PortMirroring nicPortMirroring = nic.getPortMirroring();
    if (nicPortMirroring != null) {
        if (!profile.isSetPortMirroring() || !profile.isPortMirroring()) {
            return false;
        }
        V3Networks nicPortMirroringNetworks = nicPortMirroring.getNetworks();
        if (nicPortMirroringNetworks != null) {
            for (V3Network nicPortMirroringNetwork : nicPortMirroringNetworks.getNetworks()) {
                if (nicPortMirroringNetwork.isSetId()) {
                    if (!Objects.equals(profileNetwork.getId(), nicPortMirroringNetwork.getId())) {
                        return false;
                    }
                }
                if (nicPortMirroringNetwork.isSetName()) {
                    if (!Objects.equals(profileNetwork.getName(), nicPortMirroringNetwork.getName())) {
                        return false;
                    }
                }
            }
        }
    }
    // All checks passed, so the profile is compatible:
    return true;
}
Also used : NetworkResource(org.ovirt.engine.api.resource.NetworkResource) V3Networks(org.ovirt.engine.api.v3.types.V3Networks) V3Network(org.ovirt.engine.api.v3.types.V3Network) V3Network(org.ovirt.engine.api.v3.types.V3Network) Network(org.ovirt.engine.api.model.Network) V3PortMirroring(org.ovirt.engine.api.v3.types.V3PortMirroring) SystemResource(org.ovirt.engine.api.resource.SystemResource) ClusterNetworksResource(org.ovirt.engine.api.resource.ClusterNetworksResource) NetworksResource(org.ovirt.engine.api.resource.NetworksResource)

Example 4 with V3NIC

use of org.ovirt.engine.api.v3.types.V3NIC in project ovirt-engine by oVirt.

the class V3NicOutAdapter method adapt.

@Override
public V3NIC adapt(Nic from) {
    V3NIC to = new V3NIC();
    if (from.isSetLinks()) {
        to.getLinks().addAll(adaptOut(from.getLinks()));
    }
    if (from.isSetActions()) {
        to.setActions(adaptOut(from.getActions()));
    }
    if (from.isSetBootProtocol()) {
        to.setBootProtocol(from.getBootProtocol().value());
    }
    if (from.isSetComment()) {
        to.setComment(from.getComment());
    }
    if (from.isSetDescription()) {
        to.setDescription(from.getDescription());
    }
    if (from.isSetId()) {
        to.setId(from.getId());
    }
    if (from.isSetHref()) {
        to.setHref(from.getHref());
    }
    if (from.isSetInstanceType()) {
        to.setInstanceType(adaptOut(from.getInstanceType()));
    }
    if (from.isSetInterface()) {
        to.setInterface(from.getInterface().value());
    }
    if (from.isSetLinked()) {
        to.setLinked(from.isLinked());
    }
    if (from.isSetMac()) {
        to.setMac(adaptOut(from.getMac()));
    }
    if (from.isSetName()) {
        to.setName(from.getName());
    }
    if (from.isSetNetwork()) {
        to.setNetwork(adaptOut(from.getNetwork()));
    }
    if (from.isSetOnBoot()) {
        to.setOnBoot(from.isOnBoot());
    }
    if (from.isSetPlugged()) {
        to.setPlugged(from.isPlugged());
        // In V3 the "active" property used to be a synonym of "plugged":
        to.setActive(from.isPlugged());
    }
    if (from.isSetReportedDevices()) {
        to.setReportedDevices(new V3ReportedDevices());
        to.getReportedDevices().getReportedDevices().addAll(adaptOut(from.getReportedDevices().getReportedDevices()));
    }
    if (from.isSetStatistics()) {
        to.setStatistics(new V3Statistics());
        to.getStatistics().getStatistics().addAll(adaptOut(from.getStatistics().getStatistics()));
    }
    if (from.isSetTemplate()) {
        to.setTemplate(adaptOut(from.getTemplate()));
    }
    if (from.isSetVm()) {
        to.setVm(adaptOut(from.getVm()));
    }
    if (from.isSetVms()) {
        to.setVms(new V3VMs());
        to.getVms().getVMs().addAll(adaptOut(from.getVms().getVms()));
    }
    if (from.isSetVnicProfile()) {
        to.setVnicProfile(adaptOut(from.getVnicProfile()));
        // In V4 the "network" and "port_mirroring" properties of the NIC have been removed, but in V3 they must
        // be populated:
        setNetworkAndPortMirroring(from, to);
    }
    return to;
}
Also used : V3ReportedDevices(org.ovirt.engine.api.v3.types.V3ReportedDevices) V3Statistics(org.ovirt.engine.api.v3.types.V3Statistics) V3VMs(org.ovirt.engine.api.v3.types.V3VMs) V3NIC(org.ovirt.engine.api.v3.types.V3NIC)

Aggregations

Network (org.ovirt.engine.api.model.Network)3 V3Network (org.ovirt.engine.api.v3.types.V3Network)3 V3Networks (org.ovirt.engine.api.v3.types.V3Networks)3 V3PortMirroring (org.ovirt.engine.api.v3.types.V3PortMirroring)3 VnicProfile (org.ovirt.engine.api.model.VnicProfile)2 ClusterNetworksResource (org.ovirt.engine.api.resource.ClusterNetworksResource)2 NetworkResource (org.ovirt.engine.api.resource.NetworkResource)2 NetworksResource (org.ovirt.engine.api.resource.NetworksResource)2 SystemResource (org.ovirt.engine.api.resource.SystemResource)2 VnicProfileResource (org.ovirt.engine.api.resource.VnicProfileResource)2 VnicProfilesResource (org.ovirt.engine.api.resource.VnicProfilesResource)2 BackendApiResource (org.ovirt.engine.api.restapi.resource.BackendApiResource)2 V3NIC (org.ovirt.engine.api.v3.types.V3NIC)2 Comparator.comparing (java.util.Comparator.comparing)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 Nic (org.ovirt.engine.api.model.Nic)1 Vm (org.ovirt.engine.api.model.Vm)1 ClusterResource (org.ovirt.engine.api.resource.ClusterResource)1