Search in sources :

Example 1 with NetworkInfoMapper

use of org.onap.so.openstack.mappers.NetworkInfoMapper in project so by onap.

the class MsoNeutronUtils method createNetwork.

/**
 * Create a network with the specified parameters in the given cloud/tenant.
 *
 * If a network already exists with the same name, an exception will be thrown. Note that this is an MSO-imposed
 * restriction. Openstack does not require uniqueness on network names.
 * <p>
 *
 * @param cloudSiteId The cloud identifier (may be a region) in which to create the network.
 * @param tenantId The tenant in which to create the network
 * @param type The type of network to create (Basic, Provider, Multi-Provider)
 * @param networkName The network name to create
 * @param provider The provider network name (for Provider or Multi-Provider networks)
 * @param vlans A list of VLAN segments for the network (for Provider or Multi-Provider networks)
 * @return a NetworkInfo object which describes the newly created network
 * @throws MsoNetworkAlreadyExists Thrown if a network with the same name already exists
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 * @throws MsoCloudSiteNotFound Thrown if the cloudSite is invalid or unknown
 */
public NetworkInfo createNetwork(String cloudSiteId, String tenantId, NetworkType type, String networkName, String provider, List<Integer> vlans) throws MsoException {
    // Obtain the cloud site information where we will create the stack
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Quantum neutronClient = getNeutronClient(cloudSite, tenantId);
    // Check if a network already exists with this name
    // Openstack will allow duplicate name, so require explicit check
    Network network = findNetworkByName(neutronClient, networkName);
    if (network != null) {
        // Network already exists. Throw an exception
        logger.error("{} Network {} on Cloud site {} for tenant {} already exists {}", MessageEnum.RA_NETWORK_ALREADY_EXIST, networkName, cloudSiteId, tenantId, ErrorCode.DataError.getValue());
        throw new MsoNetworkAlreadyExists(networkName, tenantId, cloudSiteId);
    }
    // Does not exist, create a new one
    network = new Network();
    network.setName(networkName);
    network.setAdminStateUp(true);
    if (type == NetworkType.PROVIDER) {
        if (provider != null && vlans != null && !vlans.isEmpty()) {
            network.setProviderPhysicalNetwork(provider);
            network.setProviderNetworkType("vlan");
            network.setProviderSegmentationId(vlans.get(0));
        }
    } else if (type == NetworkType.MULTI_PROVIDER) {
        if (provider != null && vlans != null && !vlans.isEmpty()) {
            List<Segment> segments = new ArrayList<>(vlans.size());
            for (int vlan : vlans) {
                Segment segment = new Segment();
                segment.setProviderPhysicalNetwork(provider);
                segment.setProviderNetworkType("vlan");
                segment.setProviderSegmentationId(vlan);
                segments.add(segment);
            }
            network.setSegments(segments);
        }
    }
    try {
        OpenStackRequest<Network> request = neutronClient.networks().create(network);
        Network newNetwork = executeAndRecordOpenstackRequest(request);
        return new NetworkInfoMapper(newNetwork).map();
    } catch (OpenStackBaseException e) {
        // Convert Neutron exception to an MsoOpenstackException
        MsoException me = neutronExceptionToMsoException(e, "CreateNetwork");
        throw me;
    } catch (RuntimeException e) {
        // Catch-all
        MsoException me = runtimeExceptionToMsoException(e, "CreateNetwork");
        throw me;
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoException(org.onap.so.openstack.exceptions.MsoException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) Segment(com.woorea.openstack.quantum.model.Segment) Quantum(com.woorea.openstack.quantum.Quantum) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Network(com.woorea.openstack.quantum.model.Network) MsoNetworkAlreadyExists(org.onap.so.openstack.exceptions.MsoNetworkAlreadyExists) NetworkInfoMapper(org.onap.so.openstack.mappers.NetworkInfoMapper) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with NetworkInfoMapper

use of org.onap.so.openstack.mappers.NetworkInfoMapper in project so by onap.

the class MsoNeutronUtils method queryNetwork.

/**
 * Query for a network with the specified name or ID in the given cloud. If the network exists, return an
 * NetworkInfo object. If not, return null.
 * <p>
 * Whenever possible, the network ID should be used as it is much more efficient. Query by name requires retrieval
 * of all networks for the tenant and search for matching name.
 * <p>
 *
 * @param networkNameOrId The network to query
 * @param tenantId The Openstack tenant to look in for the network
 * @param cloudSiteId The cloud identifier (may be a region) in which to query the network.
 * @return a NetworkInfo object describing the queried network, or null if not found
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 * @throws MsoCloudSiteNotFound
 */
public NetworkInfo queryNetwork(String networkNameOrId, String tenantId, String cloudSiteId) throws MsoException {
    logger.debug("In queryNetwork");
    // Obtain the cloud site information
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Quantum neutronClient = getNeutronClient(cloudSite, tenantId);
    // Check if the network exists and return its info
    try {
        Network network = findNetworkByNameOrId(neutronClient, networkNameOrId);
        if (network == null) {
            logger.debug("Query Network: {} not found in tenant {}", networkNameOrId, tenantId);
            return null;
        }
        return new NetworkInfoMapper(network).map();
    } catch (OpenStackBaseException e) {
        // Convert Neutron exception to an MsoOpenstackException
        MsoException me = neutronExceptionToMsoException(e, "QueryNetwork");
        throw me;
    } catch (RuntimeException e) {
        // Catch-all
        MsoException me = runtimeExceptionToMsoException(e, "QueryNetwork");
        throw me;
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) Quantum(com.woorea.openstack.quantum.Quantum) MsoException(org.onap.so.openstack.exceptions.MsoException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Network(com.woorea.openstack.quantum.model.Network) NetworkInfoMapper(org.onap.so.openstack.mappers.NetworkInfoMapper)

Example 3 with NetworkInfoMapper

use of org.onap.so.openstack.mappers.NetworkInfoMapper in project so by onap.

the class MsoNeutronUtils method updateNetwork.

/**
 * Update a network with the specified parameters in the given cloud/tenant.
 *
 * Specifically, this call is intended to update the VLAN segments on a multi-provider network. The provider
 * segments will be replaced with the supplied list of VLANs.
 * <p>
 * Note that updating the 'segments' array is not normally supported by Neutron. This method relies on a Platform
 * Orchestration extension (using SDN controller to manage the virtual networking).
 *
 * @param cloudSiteId The cloud site ID (may be a region) in which to update the network.
 * @param tenantId Openstack ID of the tenant in which to update the network
 * @param networkId The unique Openstack ID of the network to be updated
 * @param type The network type (Basic, Provider, Multi-Provider)
 * @param provider The provider network name. This should not change.
 * @param vlans The list of VLAN segments to replace
 * @return a NetworkInfo object which describes the updated network
 * @throws MsoNetworkNotFound Thrown if the requested network does not exist
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 * @throws MsoCloudSiteNotFound
 */
public NetworkInfo updateNetwork(String cloudSiteId, String tenantId, String networkId, NetworkType type, String provider, List<Integer> vlans) throws MsoException {
    // Obtain the cloud site information where we will create the stack
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Quantum neutronClient = getNeutronClient(cloudSite, tenantId);
    // Check that the network exists
    Network network = findNetworkById(neutronClient, networkId);
    if (network == null) {
        // Network not found. Throw an exception
        logger.error("{} Network {} on Cloud site {} for Tenant {} not found {}", MessageEnum.RA_NETWORK_NOT_FOUND, networkId, cloudSiteId, tenantId, ErrorCode.DataError.getValue());
        throw new MsoNetworkNotFound(networkId, tenantId, cloudSiteId);
    }
    // Overwrite the properties to be updated
    if (type == NetworkType.PROVIDER) {
        if (provider != null && vlans != null && vlans.size() > 0) {
            network.setProviderPhysicalNetwork(provider);
            network.setProviderNetworkType("vlan");
            network.setProviderSegmentationId(vlans.get(0));
        }
    } else if (type == NetworkType.MULTI_PROVIDER) {
        if (provider != null && vlans != null && vlans.size() > 0) {
            List<Segment> segments = new ArrayList<>(vlans.size());
            for (int vlan : vlans) {
                Segment segment = new Segment();
                segment.setProviderPhysicalNetwork(provider);
                segment.setProviderNetworkType("vlan");
                segment.setProviderSegmentationId(vlan);
                segments.add(segment);
            }
            network.setSegments(segments);
        }
    }
    try {
        OpenStackRequest<Network> request = neutronClient.networks().update(network);
        Network newNetwork = executeAndRecordOpenstackRequest(request);
        return new NetworkInfoMapper(newNetwork).map();
    } catch (OpenStackBaseException e) {
        // Convert Neutron exception to an MsoOpenstackException
        MsoException me = neutronExceptionToMsoException(e, "UpdateNetwork");
        throw me;
    } catch (RuntimeException e) {
        // Catch-all
        MsoException me = runtimeExceptionToMsoException(e, "UpdateNetwork");
        throw me;
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoException(org.onap.so.openstack.exceptions.MsoException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) Segment(com.woorea.openstack.quantum.model.Segment) Quantum(com.woorea.openstack.quantum.Quantum) MsoNetworkNotFound(org.onap.so.openstack.exceptions.MsoNetworkNotFound) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Network(com.woorea.openstack.quantum.model.Network) NetworkInfoMapper(org.onap.so.openstack.mappers.NetworkInfoMapper) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)3 Quantum (com.woorea.openstack.quantum.Quantum)3 Network (com.woorea.openstack.quantum.model.Network)3 CloudSite (org.onap.so.db.catalog.beans.CloudSite)3 MsoCloudSiteNotFound (org.onap.so.openstack.exceptions.MsoCloudSiteNotFound)3 MsoException (org.onap.so.openstack.exceptions.MsoException)3 NetworkInfoMapper (org.onap.so.openstack.mappers.NetworkInfoMapper)3 Segment (com.woorea.openstack.quantum.model.Segment)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MsoNetworkAlreadyExists (org.onap.so.openstack.exceptions.MsoNetworkAlreadyExists)1 MsoNetworkNotFound (org.onap.so.openstack.exceptions.MsoNetworkNotFound)1