Search in sources :

Example 1 with MsoIOException

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

the class MsoNeutronUtils method getNeutronClient.

// -------------------------------------------------------------------
// PRIVATE UTILITY FUNCTIONS FOR USE WITHIN THIS CLASS
/**
 * Get a Neutron (Quantum) client for the Openstack Network service. This requires a 'member'-level userId +
 * password, which will be retrieved from properties based on the specified cloud Id. The tenant in which to operate
 * must also be provided.
 * <p>
 * On successful authentication, the Quantum object will be cached for the tenantID + cloudId so that it can be
 * reused without reauthenticating with Openstack every time.
 *
 * @param cloudSite - a cloud site definition
 * @param tenantId - Openstack tenant ID
 * @return an authenticated Quantum object
 */
private Quantum getNeutronClient(CloudSite cloudSite, String tenantId) throws MsoException {
    String cloudId = cloudSite.getId();
    String region = cloudSite.getRegionId();
    // Obtain an MSO token for the tenant from the identity service
    CloudIdentity cloudIdentity = cloudSite.getIdentityService();
    MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
    final String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
    String neutronUrl = null;
    String tokenId = null;
    try {
        if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
            Keystone keystoneTenantClient = new Keystone(keystoneUrl);
            Access access = null;
            Authentication credentials = authenticationMethodFactory.getAuthenticationFor(cloudIdentity);
            OpenStackRequest<Access> request = keystoneTenantClient.tokens().authenticate(credentials).withTenantId(tenantId);
            access = executeAndRecordOpenstackRequest(request, true);
            try {
                neutronUrl = KeystoneUtils.findEndpointURL(access.getServiceCatalog(), "network", region, "public");
                if (!neutronUrl.endsWith("/")) {
                    neutronUrl += "/v2.0/";
                }
            } catch (RuntimeException e) {
                // This comes back for not found (probably an incorrect region ID)
                String error = "Network service not found: region=" + region + ",cloud=" + cloudIdentity.getId();
                throw new MsoAdapterException(error, e);
            }
            tokenId = access.getToken().getId();
        } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
            try {
                KeystoneAuthHolder holder = keystoneV3Authentication.getToken(cloudSite, tenantId, "network");
                tokenId = holder.getId();
                neutronUrl = holder.getServiceUrl();
                if (!neutronUrl.endsWith("/")) {
                    neutronUrl += "/v2.0/";
                }
            } catch (ServiceEndpointNotFoundException e) {
                // This comes back for not found (probably an incorrect region ID)
                String error = "Network service not found: region=" + region + ",cloud=" + cloudIdentity.getId();
                throw new MsoAdapterException(error, e);
            }
        }
    } catch (OpenStackResponseException e) {
        if (e.getStatus() == 401) {
            // Authentication error.
            String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId();
            throw new MsoAdapterException(error);
        } else {
            MsoException me = keystoneErrorToMsoException(e, "TokenAuth");
            throw me;
        }
    } catch (OpenStackConnectException e) {
        // Connection to Openstack failed
        MsoIOException me = new MsoIOException(e.getMessage(), e);
        me.addContext("TokenAuth");
        throw me;
    } catch (RuntimeException e) {
        // Catch-all
        MsoException me = runtimeExceptionToMsoException(e, "TokenAuth");
        throw me;
    }
    Quantum neutronClient = new Quantum(neutronUrl);
    neutronClient.token(tokenId);
    return neutronClient;
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) CloudIdentity(org.onap.so.db.catalog.beans.CloudIdentity) Access(com.woorea.openstack.keystone.model.Access) KeystoneAuthHolder(org.onap.so.cloud.authentication.KeystoneAuthHolder) Quantum(com.woorea.openstack.quantum.Quantum) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) ServiceEndpointNotFoundException(org.onap.so.cloud.authentication.ServiceEndpointNotFoundException) Keystone(com.woorea.openstack.keystone.Keystone) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) KeystoneV3Authentication(org.onap.so.cloud.authentication.KeystoneV3Authentication) Authentication(com.woorea.openstack.keystone.model.Authentication) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException)

Example 2 with MsoIOException

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

the class MsoCommonUtils method neutronExceptionToMsoException.

/*
     * Convert an Openstack Exception on a Neutron call to an MsoOpenstackException. This method supports both
     * OpenstackResponseException and OpenStackConnectException.
     */
protected MsoException neutronExceptionToMsoException(OpenStackBaseException e, String context) {
    MsoException me = null;
    if (e instanceof OpenStackResponseException) {
        OpenStackResponseException re = (OpenStackResponseException) e;
        try {
            // Failed Neutron calls return an NeutronError entity body
            NeutronError error = re.getResponse().getErrorEntity(NeutronError.class);
            logger.error("{} {} Openstack Neutron Error on {} {}", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, error);
            me = new MsoOpenstackException(re.getStatus(), error.getType(), error.getMessage());
        } catch (Exception e2) {
            // Couldn't parse body as a NeutronError. Report the HTTP error.
            logger.error("{} {} Openstack HTTP Error on {}: {}, {}", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, re.getStatus(), e.getMessage(), e2);
            me = new MsoOpenstackException(re.getStatus(), re.getMessage(), null);
        }
        // Add the context of the error
        me.addContext(context);
        // Generate an alarm for 5XX and higher errors.
        if (re.getStatus() >= 500) {
            logger.error("{} {} OpenStackBaseException with response code {} on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), re.getStatus(), context, e);
        }
    } else if (e instanceof OpenStackConnectException) {
        OpenStackConnectException ce = (OpenStackConnectException) e;
        me = new MsoIOException(ce.getMessage());
        me.addContext(context);
        // Generate an alarm for all connection errors.
        logger.error("{} {} Openstack Neutron Connection error on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, e);
    }
    return me;
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) NeutronError(com.woorea.openstack.quantum.model.NeutronError) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) ServiceEndpointNotFoundException(org.onap.so.cloud.authentication.ServiceEndpointNotFoundException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) IOException(java.io.IOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException)

Example 3 with MsoIOException

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

the class MsoCommonUtils method heatExceptionToMsoException.

/*
     * Convert an Openstack Exception on a Heat call to an MsoOpenstackException. This method supports both
     * OpenstackResponseException and OpenStackConnectException.
     */
protected MsoException heatExceptionToMsoException(OpenStackBaseException e, String context) {
    MsoException me = null;
    if (e instanceof OpenStackResponseException) {
        OpenStackResponseException re = (OpenStackResponseException) e;
        try {
            // Failed Heat calls return an Explanation entity body.
            Explanation explanation = re.getResponse().getErrorEntity(Explanation.class);
            logger.error("Exception - Openstack Error on {} : {}", context, explanation);
            String fullError = explanation.getExplanation() + ", error.type=" + explanation.getError().getType() + ", error.message=" + explanation.getError().getMessage();
            logger.error(fullError);
            me = new MsoOpenstackException(explanation.getCode(), explanation.getTitle(), fullError);
        } catch (Exception e2) {
            // Couldn't parse the body as an "Explanation". Report the original HTTP error.
            logger.error("{} {} Exception - HTTP Error on {}: {}, ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, re.getStatus(), e.getMessage(), e2);
            me = new MsoOpenstackException(re.getStatus(), re.getMessage(), re.getMessage());
        }
        // Add the context of the error
        me.addContext(context);
    } else if (e instanceof OpenStackConnectException) {
        OpenStackConnectException ce = (OpenStackConnectException) e;
        me = new MsoIOException(ce.getMessage());
        me.addContext(context);
        // Generate an alarm for all connection errors.
        logger.error("{} {} Openstack Heat connection error on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, e);
    }
    return me;
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) Explanation(com.woorea.openstack.heat.model.Explanation) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) ServiceEndpointNotFoundException(org.onap.so.cloud.authentication.ServiceEndpointNotFoundException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) IOException(java.io.IOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException)

Example 4 with MsoIOException

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

the class MsoCommonUtils method keystoneErrorToMsoException.

/*
     * Convert an Openstack Exception on a Keystone call to an MsoException. This method supports both
     * OpenstackResponseException and OpenStackConnectException.
     */
public MsoException keystoneErrorToMsoException(OpenStackBaseException e, String context) {
    MsoException me = null;
    if (e instanceof OpenStackResponseException) {
        OpenStackResponseException re = (OpenStackResponseException) e;
        try {
            // Failed Keystone calls return an Error entity body.
            Error error = re.getResponse().getErrorEntity(Error.class);
            logger.error("{} {} Openstack Keystone Error on {}: {}", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, error);
            me = new MsoOpenstackException(error.getCode(), error.getTitle(), error.getMessage());
        } catch (Exception e2) {
            // Can't parse the body as an "Error". Report the HTTP error
            logger.error("{} {} HTTP Error on {}: {}, {}", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), context, re.getStatus(), re.getMessage(), e2);
            me = new MsoOpenstackException(re.getStatus(), re.getMessage(), "");
        }
        // Add the context of the error
        me.addContext(context);
        // Generate an alarm for 5XX and higher errors.
        if (re.getStatus() >= 500) {
            logger.error("{} {} OpenStackResponseException with response code {} on {}: ", MessageEnum.RA_CONNECTION_EXCEPTION, ErrorCode.DataError.getValue(), re.getStatus(), context, e);
        }
    } else if (e instanceof OpenStackConnectException) {
        OpenStackConnectException ce = (OpenStackConnectException) e;
        me = new MsoIOException(ce.getMessage());
        me.addContext(context);
        // Generate an alarm for all connection errors.
        logger.error("{} {} Openstack Keystone connection error on {}: ", MessageEnum.RA_GENERAL_EXCEPTION_ARG, ErrorCode.DataError.getValue(), context, e);
    }
    return me;
}
Also used : MsoException(org.onap.so.openstack.exceptions.MsoException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) NeutronError(com.woorea.openstack.quantum.model.NeutronError) Error(com.woorea.openstack.keystone.model.Error) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) ServiceEndpointNotFoundException(org.onap.so.cloud.authentication.ServiceEndpointNotFoundException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) IOException(java.io.IOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException)

Example 5 with MsoIOException

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

the class MsoCommonUtils method getKeystoneAuthHolder.

/**
 * Gets the Keystone Authorization
 *
 * @param cloudSite the cloud site
 * @param tenantId the tenant id
 * @return the Neutron client
 * @throws MsoException the mso exception
 */
protected KeystoneAuthHolder getKeystoneAuthHolder(String cloudSiteId, String tenantId, String serviceName) throws MsoException {
    CloudIdentity cloudIdentity = null;
    try {
        CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
        String cloudId = cloudSite.getId();
        String region = cloudSite.getRegionId();
        cloudIdentity = cloudSite.getIdentityService();
        MsoTenantUtils tenantUtils = tenantUtilsFactory.getTenantUtilsByServerType(cloudIdentity.getIdentityServerType());
        String keystoneUrl = tenantUtils.getKeystoneUrl(cloudId, cloudIdentity);
        if (ServerType.KEYSTONE.equals(cloudIdentity.getIdentityServerType())) {
            Access access = getKeystone(tenantId, cloudIdentity, keystoneUrl);
            try {
                KeystoneAuthHolder keystoneAuthV2 = new KeystoneAuthHolder();
                keystoneAuthV2.setServiceUrl(KeystoneUtils.findEndpointURL(access.getServiceCatalog(), serviceName, region, "public"));
                keystoneAuthV2.setId(access.getToken().getId());
                return keystoneAuthV2;
            } catch (RuntimeException e) {
                String error = "Openstack did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
                throw new MsoAdapterException(error, e);
            }
        } else if (ServerType.KEYSTONE_V3.equals(cloudIdentity.getIdentityServerType())) {
            try {
                return keystoneV3Authentication.getToken(cloudSite, tenantId, serviceName);
            } catch (ServiceEndpointNotFoundException e) {
                String error = "cloud did not match an orchestration service for: region=" + region + ",cloud=" + cloudIdentity.getIdentityUrl();
                throw new MsoAdapterException(error, e);
            }
        } else {
            throw new MsoAdapterException("Unknown Keystone Server Type");
        }
    } catch (OpenStackResponseException e) {
        if (e.getStatus() == 401) {
            String error = "Authentication Failure: tenant=" + tenantId + ",cloud=" + cloudIdentity.getId();
            throw new MsoAdapterException(error);
        } else {
            throw keystoneErrorToMsoException(e, TOKEN_AUTH);
        }
    } catch (OpenStackConnectException e) {
        MsoIOException me = new MsoIOException(e.getMessage(), e);
        me.addContext(TOKEN_AUTH);
        throw me;
    } catch (RuntimeException e) {
        throw runtimeExceptionToMsoException(e, TOKEN_AUTH);
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) CloudIdentity(org.onap.so.db.catalog.beans.CloudIdentity) Access(com.woorea.openstack.keystone.model.Access) KeystoneAuthHolder(org.onap.so.cloud.authentication.KeystoneAuthHolder) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) ServiceEndpointNotFoundException(org.onap.so.cloud.authentication.ServiceEndpointNotFoundException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) MsoIOException(org.onap.so.openstack.exceptions.MsoIOException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException)

Aggregations

OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)8 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)8 MsoIOException (org.onap.so.openstack.exceptions.MsoIOException)8 MsoException (org.onap.so.openstack.exceptions.MsoException)7 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)6 MsoOpenstackException (org.onap.so.openstack.exceptions.MsoOpenstackException)6 ServiceEndpointNotFoundException (org.onap.so.cloud.authentication.ServiceEndpointNotFoundException)5 MsoAdapterException (org.onap.so.openstack.exceptions.MsoAdapterException)5 NeutronError (com.woorea.openstack.quantum.model.NeutronError)4 OpenStackResponse (com.woorea.openstack.base.client.OpenStackResponse)3 File (java.io.File)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 BaseTest (org.onap.so.BaseTest)3 Explanation (com.woorea.openstack.heat.model.Explanation)2 Access (com.woorea.openstack.keystone.model.Access)2 Error (com.woorea.openstack.keystone.model.Error)2 KeystoneAuthHolder (org.onap.so.cloud.authentication.KeystoneAuthHolder)2 CloudIdentity (org.onap.so.db.catalog.beans.CloudIdentity)2 Keystone (com.woorea.openstack.keystone.Keystone)1