use of org.onap.so.openstack.exceptions.MsoNetworkAlreadyExists 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;
}
}
Aggregations