Search in sources :

Example 26 with MsoException

use of org.onap.so.openstack.exceptions.MsoException in project so by onap.

the class MsoHeatUtils method queryStack.

/**
 * Query for a single stack (by Name) in a tenant. This call will always return a StackInfo object. If the stack
 * does not exist, an "empty" StackInfo will be returned - containing only the stack name and a status of NOTFOUND.
 *
 * @param tenantId The Openstack ID of the tenant in which to query
 * @param cloudSiteId The cloud identifier (may be a region) in which to query
 * @param cloudOwner the cloud owner of the cloud site in which to query
 * @param stackName The name of the stack to query (may be simple or canonical)
 * @return A StackInfo object
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
 */
public StackInfo queryStack(String cloudSiteId, String cloudOwner, String tenantId, String stackName) throws MsoException {
    logger.debug("Query HEAT stack: {} in tenant {}", stackName, tenantId);
    Heat heatClient = null;
    try {
        heatClient = getHeatClient(cloudSiteId, tenantId);
    } catch (MsoTenantNotFound e) {
        // Tenant doesn't exist, so stack doesn't either
        logger.debug("Tenant with id " + tenantId + "not found.", e);
        return new StackInfo(stackName, HeatStatus.NOTFOUND);
    } catch (MsoException me) {
        // Got an Openstack error. Propagate it
        logger.error("{} {} Openstack Exception on Token request: ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.AvailabilityError.getValue(), me);
        me.addContext("QueryStack");
        throw me;
    }
    // Query the Stack.
    // An MsoException will propagate transparently to the caller.
    Stack heatStack = queryHeatStack(heatClient, stackName);
    if (heatStack == null) {
        // Stack does not exist. Return a StackInfo with status NOTFOUND
        return new StackInfo(stackName, HeatStatus.NOTFOUND);
    }
    return new StackInfoMapper(heatStack).map();
}
Also used : StackInfoMapper(org.onap.so.openstack.mappers.StackInfoMapper) Heat(com.woorea.openstack.heat.Heat) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoTenantNotFound(org.onap.so.openstack.exceptions.MsoTenantNotFound) StackInfo(org.onap.so.openstack.beans.StackInfo) Stack(com.woorea.openstack.heat.model.Stack)

Example 27 with MsoException

use of org.onap.so.openstack.exceptions.MsoException in project so by onap.

the class MsoHeatUtils method postProcessStackCreate.

public Stack postProcessStackCreate(Stack stack, boolean backout, int timeoutMinutes, boolean cleanUpKeyPair, String cloudSiteId, String tenantId, CreateStackParam stackCreate) throws MsoException {
    boolean stackCreationFailed = false;
    boolean stackRollbackFailed = false;
    if (stack == null) {
        throw new StackCreationException("Unknown Error in Stack Creation");
    } else {
        logger.info("Performing post processing backout: {} cleanUpKeyPair: {}, stack {}", backout, cleanUpKeyPair, stack);
        if (!CREATE_COMPLETE.equals(stack.getStackStatus())) {
            if (cleanUpKeyPair && !Strings.isNullOrEmpty(stack.getStackStatusReason()) && isKeyPairFailure(stack.getStackStatusReason())) {
                return handleKeyPairConflict(cloudSiteId, tenantId, stackCreate, timeoutMinutes, backout, stack);
            }
            if (!backout) {
                logger.info("Status is not CREATE_COMPLETE, stack deletion suppressed");
                stackCreationFailed = true;
                String errorMessage = "Stack Creation Failed Openstack Status: " + stack.getStackStatus() + " Status Reason: " + stack.getStackStatusReason() + ". Stack rollback suppressed, stack not deleted";
                throw new StackCreationException(errorMessage, stackCreationFailed, stackRollbackFailed);
            } else {
                logger.info("Status is not CREATE_COMPLETE, stack deletion will be executed");
                stackCreationFailed = true;
                String errorMessage = "Stack Creation Failed Openstack Status: " + stack.getStackStatus() + " Status Reason: " + stack.getStackStatusReason();
                try {
                    Stack deletedStack = handleUnknownCreateStackFailure(stack, timeoutMinutes, cloudSiteId, tenantId);
                    errorMessage = errorMessage + " , Rollback of Stack Creation completed with status: " + deletedStack.getStackStatus() + " Status Reason: " + deletedStack.getStackStatusReason();
                } catch (MsoException e) {
                    stackRollbackFailed = true;
                    logger.error("Sync Error Deleting Stack during rollback", e);
                    if (e instanceof StackRollbackException) {
                        errorMessage = errorMessage + e.getMessage();
                    } else {
                        errorMessage = errorMessage + " , Rollback of Stack Creation failed with sync error: " + e.getMessage();
                    }
                }
                throw new StackCreationException(errorMessage, stackCreationFailed, stackRollbackFailed);
            }
        } else {
            return stack;
        }
    }
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) Stack(com.woorea.openstack.heat.model.Stack)

Example 28 with MsoException

use of org.onap.so.openstack.exceptions.MsoException in project so by onap.

the class MsoNeutronUtils method getNeutronPort.

public Optional<Port> getNeutronPort(String neutronPortId, String tenantId, String cloudSiteId) {
    try {
        logger.debug("Finding Neutron port:" + neutronPortId);
        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
        Quantum neutronClient = getNeutronClient(cloudSite, tenantId);
        Port port = findPortById(neutronClient, neutronPortId);
        if (port == null) {
            return Optional.empty();
        }
        return Optional.of(port);
    } catch (RuntimeException | MsoException e) {
        logger.error("Error retrieving neutron port", e);
        return Optional.empty();
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) Quantum(com.woorea.openstack.quantum.Quantum) MsoException(org.onap.so.openstack.exceptions.MsoException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Port(com.woorea.openstack.quantum.model.Port)

Example 29 with MsoException

use of org.onap.so.openstack.exceptions.MsoException 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 30 with MsoException

use of org.onap.so.openstack.exceptions.MsoException 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)

Aggregations

MsoException (org.onap.so.openstack.exceptions.MsoException)53 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)12 CloudSite (org.onap.so.db.catalog.beans.CloudSite)12 MsoAdapterException (org.onap.so.openstack.exceptions.MsoAdapterException)12 Nova (com.woorea.openstack.nova.Nova)10 StackInfo (org.onap.so.openstack.beans.StackInfo)10 Quantum (com.woorea.openstack.quantum.Quantum)9 MsoOpenstackException (org.onap.so.openstack.exceptions.MsoOpenstackException)9 OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)8 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 BaseTest (org.onap.so.BaseTest)7 MsoIOException (org.onap.so.openstack.exceptions.MsoIOException)7 HashMap (java.util.HashMap)6 VnfException (org.onap.so.adapters.vnf.exceptions.VnfException)6 MsoCloudSiteNotFound (org.onap.so.openstack.exceptions.MsoCloudSiteNotFound)6 IOException (java.io.IOException)5 NetworkException (org.onap.so.adapters.network.exceptions.NetworkException)5 HeatTemplate (org.onap.so.db.catalog.beans.HeatTemplate)5