Search in sources :

Example 1 with MsoCloudSiteNotFound

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

the class MsoKeystoneUtils method queryTenant.

/**
 * Query for a tenant by ID in the given cloud. If the tenant exists, return an MsoTenant object. If not, return
 * null.
 * <p>
 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin requests go to the centralized
 * identity service in DCP. However, if some artifact must exist in each local LCP instance as well, then it will be
 * needed to access the correct region.
 * <p>
 *
 * @param tenantId The Openstack ID of the tenant to query
 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
 * @return the tenant properties of the queried tenant, or null if not found
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 */
public MsoTenant queryTenant(String tenantId, String cloudSiteId) throws MsoException {
    // Obtain the cloud site information where we will query the tenant
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSite);
    // Check if the tenant exists and return its Tenant Id
    try {
        Tenant tenant = findTenantById(keystoneAdminClient, tenantId);
        if (tenant == null) {
            return null;
        }
        Map<String, String> metadata = new HashMap<>();
        if (cloudSite.getIdentityService().getTenantMetadata()) {
            OpenStackRequest<Metadata> request = keystoneAdminClient.tenants().showMetadata(tenant.getId());
            Metadata tenantMetadata = executeAndRecordOpenstackRequest(request);
            if (tenantMetadata != null) {
                metadata = tenantMetadata.getMetadata();
            }
        }
        return new MsoTenant(tenant.getId(), tenant.getName(), metadata);
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, "QueryTenant");
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, "QueryTenant");
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) Keystone(com.woorea.openstack.keystone.Keystone) HashMap(java.util.HashMap) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Metadata(com.woorea.openstack.keystone.model.Metadata) MsoTenant(org.onap.so.openstack.beans.MsoTenant)

Example 2 with MsoCloudSiteNotFound

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

the class MsoKeystoneUtils method queryTenantByName.

/**
 * Query for a tenant with the specified name in the given cloud. If the tenant exists, return an MsoTenant object.
 * If not, return null. This query is useful if the client knows it has the tenant name, skipping an initial lookup
 * by ID that would always fail.
 * <p>
 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin requests go to the centralized
 * identity service in DCP. However, if some artifact must exist in each local LCP instance as well, then it will be
 * needed to access the correct region.
 * <p>
 *
 * @param tenantName The name of the tenant to query
 * @param cloudSiteId The cloud identifier (may be a region) in which to query the tenant.
 * @return the tenant properties of the queried tenant, or null if not found
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 */
public MsoTenant queryTenantByName(String tenantName, String cloudSiteId) throws MsoException {
    // Obtain the cloud site information where we will query the tenant
    CloudSite cloudSite = cloudConfig.getCloudSite(cloudSiteId).orElseThrow(() -> new MsoCloudSiteNotFound(cloudSiteId));
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSite);
    try {
        Tenant tenant = findTenantByName(keystoneAdminClient, tenantName);
        if (tenant == null) {
            return null;
        }
        Map<String, String> metadata = new HashMap<>();
        if (cloudSite.getIdentityService().getTenantMetadata()) {
            OpenStackRequest<Metadata> request = keystoneAdminClient.tenants().showMetadata(tenant.getId());
            Metadata tenantMetadata = executeAndRecordOpenstackRequest(request);
            if (tenantMetadata != null) {
                metadata = tenantMetadata.getMetadata();
            }
        }
        return new MsoTenant(tenant.getId(), tenant.getName(), metadata);
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, "QueryTenantName");
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, "QueryTenantName");
    }
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) Keystone(com.woorea.openstack.keystone.Keystone) HashMap(java.util.HashMap) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite) Metadata(com.woorea.openstack.keystone.model.Metadata) MsoTenant(org.onap.so.openstack.beans.MsoTenant)

Example 3 with MsoCloudSiteNotFound

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

the class MsoKeystoneUtils method deleteTenantByName.

/**
 * Delete the specified Tenant (by Name) in the given cloud. This method returns true or false, depending on whether
 * the tenant existed and was successfully deleted, or if the tenant already did not exist. Both cases are treated
 * as success (no Exceptions).
 * <p>
 * Note for the AIC Cloud (DCP/LCP): all admin requests go to the centralized identity service in DCP. So deleting a
 * tenant from one cloudSiteId will remove it from all sites managed by that identity service.
 * <p>
 *
 * @param tenantName The name of the tenant to delete
 * @param cloudSiteId The cloud identifier from which to delete the tenant.
 * @return true if the tenant was deleted, false if the tenant did not exist.
 * @throws MsoOpenstackException If the Openstack API call returns an exception.
 */
public boolean deleteTenantByName(String tenantName, String cloudSiteId) throws MsoException {
    // Obtain the cloud site information where we will query the tenant
    Optional<CloudSite> cloudSite = cloudConfig.getCloudSite(cloudSiteId);
    if (!cloudSite.isPresent()) {
        throw new MsoCloudSiteNotFound(cloudSiteId);
    }
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSite.get());
    try {
        // Need the Tenant ID to delete (can't directly delete by name)
        Tenant tenant = findTenantByName(keystoneAdminClient, tenantName);
        if (tenant == null) {
            // OK if tenant already doesn't exist.
            LOGGER.error("{} Tenant {} not found on Cloud site id {}, {}", MessageEnum.RA_TENANT_NOT_FOUND, tenantName, cloudSiteId, ErrorCode.DataError.getValue());
            return false;
        }
        // Execute the Delete. It has no return value.
        OpenStackRequest<Void> request = keystoneAdminClient.tenants().delete(tenant.getId());
        executeAndRecordOpenstackRequest(request);
        LOGGER.debug("Deleted Tenant {} ({})", tenant.getId(), tenant.getName());
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, DELETE_TENANT);
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, DELETE_TENANT);
    }
    return true;
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) Keystone(com.woorea.openstack.keystone.Keystone) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudSite(org.onap.so.db.catalog.beans.CloudSite)

Example 4 with MsoCloudSiteNotFound

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

the class MsoKeystoneUtils method createTenant.

/**
 * Create a tenant with the specified name in the given cloud. If the tenant already exists, an Exception will be
 * thrown. The MSO User will also be added to the "member" list of the new tenant to perform subsequent Nova/Heat
 * commands in the tenant. If the MSO User association fails, the entire transaction will be rolled back.
 * <p>
 * For the AIC Cloud (DCP/LCP): it is not clear that cloudId is needed, as all admin requests go to the centralized
 * identity service in DCP. However, if some artifact must exist in each local LCP instance as well, then it will be
 * needed to access the correct region.
 * <p>
 *
 * @param tenantName The tenant name to create
 * @param cloudSiteId The cloud identifier (may be a region) in which to create the tenant.
 * @return the tenant ID of the newly created tenant
 * @throws MsoTenantAlreadyExists Thrown if the requested tenant already exists
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception
 */
public String createTenant(String tenantName, String cloudSiteId, Map<String, String> metadata, boolean backout) throws MsoException {
    // Obtain the cloud site information where we will create the tenant
    Optional<CloudSite> cloudSiteOpt = cloudConfig.getCloudSite(cloudSiteId);
    if (!cloudSiteOpt.isPresent()) {
        LOGGER.error("{} MSOCloudSite {} not found {} ", MessageEnum.RA_CREATE_TENANT_ERR, cloudSiteId, ErrorCode.DataError.getValue());
        throw new MsoCloudSiteNotFound(cloudSiteId);
    }
    Keystone keystoneAdminClient = getKeystoneAdminClient(cloudSiteOpt.get());
    Tenant tenant = null;
    try {
        // Check if the tenant already exists
        tenant = findTenantByName(keystoneAdminClient, tenantName);
        if (tenant != null) {
            // Tenant already exists. Throw an exception
            LOGGER.error("{} Tenant name {} already exists on Cloud site id {}, {}", MessageEnum.RA_TENANT_ALREADY_EXIST, tenantName, cloudSiteId, ErrorCode.DataError.getValue());
            throw new MsoTenantAlreadyExists(tenantName, cloudSiteId);
        }
        // Does not exist, create a new one
        tenant = new Tenant();
        tenant.setName(tenantName);
        tenant.setDescription("SDN Tenant (via MSO)");
        tenant.setEnabled(true);
        OpenStackRequest<Tenant> request = keystoneAdminClient.tenants().create(tenant);
        tenant = executeAndRecordOpenstackRequest(request);
    } catch (OpenStackBaseException e) {
        // Convert Keystone OpenStackResponseException to MsoOpenstackException
        throw keystoneErrorToMsoException(e, "CreateTenant");
    } catch (RuntimeException e) {
        // Catch-all
        throw runtimeExceptionToMsoException(e, "CreateTenant");
    }
    // apply tenant metadata if supported by the cloud site
    try {
        CloudIdentity cloudIdentity = cloudSiteOpt.get().getIdentityService();
        User msoUser = findUserByNameOrId(keystoneAdminClient, cloudIdentity.getMsoId());
        Role memberRole = findRoleByNameOrId(keystoneAdminClient, cloudIdentity.getMemberRole());
        if (msoUser != null && memberRole != null) {
            OpenStackRequest<Void> request = keystoneAdminClient.tenants().addUser(tenant.getId(), msoUser.getId(), memberRole.getId());
            executeAndRecordOpenstackRequest(request);
        }
        if (cloudIdentity.getTenantMetadata() && metadata != null && !metadata.isEmpty()) {
            Metadata tenantMetadata = new Metadata();
            tenantMetadata.setMetadata(metadata);
            OpenStackRequest<Metadata> metaRequest = keystoneAdminClient.tenants().createOrUpdateMetadata(tenant.getId(), tenantMetadata);
            executeAndRecordOpenstackRequest(metaRequest);
        }
    } catch (Exception e) {
        // so roll back the tenant.
        if (!backout) {
            LOGGER.warn("{} Create Tenant errored, Tenant deletion suppressed {} ", MessageEnum.RA_CREATE_TENANT_ERR, ErrorCode.DataError.getValue());
        } else {
            try {
                OpenStackRequest<Void> request = keystoneAdminClient.tenants().delete(tenant.getId());
                executeAndRecordOpenstackRequest(request);
            } catch (Exception e2) {
                // Just log this one. We will report the original exception.
                LOGGER.error("{} Nested exception rolling back tenant {} ", MessageEnum.RA_CREATE_TENANT_ERR, ErrorCode.DataError.getValue(), e2);
            }
        }
        // Propagate the original exception on user/role/tenant mapping
        if (e instanceof OpenStackBaseException) {
            // Convert Keystone Exception to MsoOpenstackException
            throw keystoneErrorToMsoException((OpenStackBaseException) e, "CreateTenantUser");
        } else {
            MsoAdapterException me = new MsoAdapterException(e.getMessage(), e);
            me.addContext("CreateTenantUser");
            throw me;
        }
    }
    return tenant.getId();
}
Also used : MsoCloudSiteNotFound(org.onap.so.openstack.exceptions.MsoCloudSiteNotFound) MsoTenantAlreadyExists(org.onap.so.openstack.exceptions.MsoTenantAlreadyExists) User(com.woorea.openstack.keystone.model.User) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) CloudIdentity(org.onap.so.db.catalog.beans.CloudIdentity) Metadata(com.woorea.openstack.keystone.model.Metadata) OpenStackRequest(com.woorea.openstack.base.client.OpenStackRequest) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) OpenStackBaseException(com.woorea.openstack.base.client.OpenStackBaseException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoException(org.onap.so.openstack.exceptions.MsoException) Role(com.woorea.openstack.keystone.model.Role) MsoTenant(org.onap.so.openstack.beans.MsoTenant) Tenant(com.woorea.openstack.keystone.model.Tenant) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) Keystone(com.woorea.openstack.keystone.Keystone) CloudSite(org.onap.so.db.catalog.beans.CloudSite)

Example 5 with MsoCloudSiteNotFound

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

Aggregations

MsoCloudSiteNotFound (org.onap.so.openstack.exceptions.MsoCloudSiteNotFound)14 CloudSite (org.onap.so.db.catalog.beans.CloudSite)13 OpenStackBaseException (com.woorea.openstack.base.client.OpenStackBaseException)9 MsoException (org.onap.so.openstack.exceptions.MsoException)6 Keystone (com.woorea.openstack.keystone.Keystone)5 Tenant (com.woorea.openstack.keystone.model.Tenant)5 Quantum (com.woorea.openstack.quantum.Quantum)5 MsoTenant (org.onap.so.openstack.beans.MsoTenant)5 Network (com.woorea.openstack.quantum.model.Network)4 CloudIdentity (org.onap.so.db.catalog.beans.CloudIdentity)4 Metadata (com.woorea.openstack.keystone.model.Metadata)3 ArrayList (java.util.ArrayList)3 NetworkInfoMapper (org.onap.so.openstack.mappers.NetworkInfoMapper)3 OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)2 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)2 Segment (com.woorea.openstack.quantum.model.Segment)2 HashMap (java.util.HashMap)2 List (java.util.List)2 HeatBridgeApi (org.onap.so.heatbridge.HeatBridgeApi)2 MsoAdapterException (org.onap.so.openstack.exceptions.MsoAdapterException)2