Search in sources :

Example 1 with Network

use of com.woorea.openstack.quantum.model.Network in project so by onap.

the class MsoNeutronUtils method deleteNetwork.

/**
 * Delete the specified Network (by ID) in the given cloud. If the network does not exist, success is returned.
 * <p>
 *
 * @param networkId Openstack ID of the network to delete
 * @param tenantId The Openstack tenant.
 * @param cloudSiteId The cloud identifier (may be a region) from which to delete the network.
 * @return true if the network was deleted, false if the network did not exist
 * @throws MsoOpenstackException If the Openstack API call returns an exception, this local exception will be
 *         thrown.
 * @throws MsoCloudSiteNotFound
 */
public boolean deleteNetwork(String networkId, String tenantId, String cloudSiteId) 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);
    try {
        // Check that the network exists.
        Network network = findNetworkById(neutronClient, networkId);
        if (network == null) {
            logger.info("{} Network not found! Network id: {} Cloud site: {} Tenant: {} ", MessageEnum.RA_DELETE_NETWORK_EXC, networkId, cloudSiteId, tenantId);
            return false;
        }
        OpenStackRequest<Void> request = neutronClient.networks().delete(network.getId());
        executeAndRecordOpenstackRequest(request);
        logger.debug("Deleted Network {} ({})", network.getId(), network.getName());
    } catch (OpenStackBaseException e) {
        // Convert Neutron exception to an MsoOpenstackException
        MsoException me = neutronExceptionToMsoException(e, "Delete Network");
        throw me;
    } catch (RuntimeException e) {
        // Catch-all
        MsoException me = runtimeExceptionToMsoException(e, "DeleteNetwork");
        throw me;
    }
    return true;
}
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)

Example 2 with Network

use of com.woorea.openstack.quantum.model.Network in project so by onap.

the class NetworkInfoMapperTest method mapFields.

@Test
public void mapFields() {
    Network network = new Network();
    network.setId("id");
    network.setName("name");
    network.setSubnets(Arrays.asList("string1", "string2"));
    NetworkInfoMapper mapper = new NetworkInfoMapper(network);
    NetworkInfo mapped = mapper.map();
    assertEquals("name", mapped.getName());
    assertEquals("id", mapped.getId());
    assertEquals(network.getSubnets(), mapped.getSubnets());
}
Also used : NetworkInfo(org.onap.so.openstack.beans.NetworkInfo) Network(com.woorea.openstack.quantum.model.Network) Test(org.junit.Test)

Example 3 with Network

use of com.woorea.openstack.quantum.model.Network in project so by onap.

the class MsoNeutronUtils method findNetworkByName.

/*
     * Find a network (or query its existence) by its Name. This method avoids an initial lookup by ID when it's known
     * that we have the network Name.
     *
     * Neutron does not support 'name=*' query parameter for Network query (show). The only way to query by name is to
     * retrieve all networks and look for the match. While inefficient, this capability will be provided as it is needed
     * by MSO, but should be avoided in favor of ID whenever possible.
     *
     * TODO: Network names are not required to be unique, though MSO will attempt to enforce uniqueness. This call
     * probably needs to return an error (instead of returning the first match).
     *
     * @param neutronClient an authenticated Quantum object
     * 
     * @param networkName the network name to query
     * 
     * @return a Network object or null if not found
     */
public Network findNetworkByName(Quantum neutronClient, String networkName) {
    if (networkName == null) {
        return null;
    }
    try {
        OpenStackRequest<Networks> request = neutronClient.networks().list();
        Networks networks = executeAndRecordOpenstackRequest(request);
        for (Network network : networks.getList()) {
            if (network.getName().equals(networkName)) {
                logger.debug("Found match on network name: {}", networkName);
                return network;
            }
        }
        logger.debug("findNetworkByName - no match found for {}", networkName);
        return null;
    } catch (OpenStackResponseException e) {
        if (e.getStatus() == 404) {
            return null;
        } else {
            logger.error("{} {} Openstack Error, GET Network By Name ({}): ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), networkName, e);
            throw e;
        }
    }
}
Also used : Networks(com.woorea.openstack.quantum.model.Networks) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) Network(com.woorea.openstack.quantum.model.Network)

Example 4 with Network

use of com.woorea.openstack.quantum.model.Network in project so by onap.

the class MsoNeutronUtils method findNetworkById.

/*
     * Find a network (or query its existence) by its Id.
     *
     * @param neutronClient an authenticated Quantum object
     * 
     * @param networkId the network ID to query
     * 
     * @return a Network object or null if not found
     */
private Network findNetworkById(Quantum neutronClient, String networkId) {
    if (networkId == null) {
        return null;
    }
    try {
        OpenStackRequest<Network> request = neutronClient.networks().show(networkId);
        Network network = executeAndRecordOpenstackRequest(request);
        return network;
    } catch (OpenStackResponseException e) {
        if (e.getStatus() == 404) {
            return null;
        } else {
            logger.error("{} {} Openstack Error, GET Network By ID ({}): ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), networkId, e);
            throw e;
        }
    }
}
Also used : OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) Network(com.woorea.openstack.quantum.model.Network)

Example 5 with Network

use of com.woorea.openstack.quantum.model.Network 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)

Aggregations

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