Search in sources :

Example 1 with StackInfo

use of org.onap.so.openstack.beans.StackInfo in project so by onap.

the class MsoHeatUtils method queryVdu.

/**
 * VduPlugin interface for query function.
 */
@Override
public VduInstance queryVdu(CloudInfo cloudInfo, String instanceId) throws VduException {
    String cloudSiteId = cloudInfo.getCloudSiteId();
    String cloudOwner = cloudInfo.getCloudOwner();
    String tenantId = cloudInfo.getTenantId();
    try {
        // Query the Cloudify Deployment object and populate a VduInstance
        StackInfo stackInfo = queryStack(cloudSiteId, cloudOwner, tenantId, instanceId);
        return stackInfoToVduInstance(stackInfo);
    } catch (Exception e) {
        throw new VduException("MsoHeatUtile (queryVdu): queryStack Exception ", e);
    }
}
Also used : StackInfo(org.onap.so.openstack.beans.StackInfo) VduException(org.onap.so.adapters.vdu.VduException) OpenStackConnectException(com.woorea.openstack.base.client.OpenStackConnectException) OpenStackResponseException(com.woorea.openstack.base.client.OpenStackResponseException) MsoException(org.onap.so.openstack.exceptions.MsoException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) IOException(java.io.IOException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) VduException(org.onap.so.adapters.vdu.VduException)

Example 2 with StackInfo

use of org.onap.so.openstack.beans.StackInfo in project so by onap.

the class MsoHeatUtils method createStack.

/**
 * Create a new Stack in the specified cloud location and tenant. The Heat template and parameter map are passed in
 * as arguments, along with the cloud access credentials. It is expected that parameters have been validated and
 * contain at minimum the required parameters for the given template with no extra (undefined) parameters..
 *
 * The Stack name supplied by the caller must be unique in the scope of this tenant. However, it should also be
 * globally unique, as it will be the identifier for the resource going forward in Inventory. This latter is managed
 * by the higher levels invoking this function.
 *
 * The caller may choose to let this function poll Openstack for completion of the stack creation, or may handle
 * polling itself via separate calls to query the status. In either case, a StackInfo object will be returned
 * containing the current status. When polling is enabled, a status of CREATED is expected. When not polling, a
 * status of BUILDING is expected.
 *
 * An error will be thrown if the requested Stack already exists in the specified Tenant and Cloud.
 *
 * For 1510 - add "environment", "files" (nested templates), and "heatFiles" (get_files) as parameters for
 * createStack. If environment is non-null, it will be added to the stack. The nested templates and get_file entries
 * both end up being added to the "files" on the stack. We must combine them before we add them to the stack if
 * they're both non-null.
 *
 * @param cloudSiteId The cloud (may be a region) in which to create the stack.
 * @param cloudOwner the cloud owner of the cloud site in which to create the stack
 * @param tenantId The Openstack ID of the tenant in which to create the Stack
 * @param stackName The name of the stack to create
 * @param vduModel contains information about the vdu model (added for plugin adapter)
 * @param heatTemplate The Heat template
 * @param stackInputs A map of key/value inputs
 * @param pollForCompletion Indicator that polling should be handled in Java vs. in the client
 * @param environment An optional yaml-format string to specify environmental parameters
 * @param files a Map<String, Object> that lists the child template IDs (file is the string, object is an int of
 *        Template id)
 * @param heatFiles a Map<String, Object> that lists the get_file entries (fileName, fileBody)
 * @param backout Donot delete stack on create Failure - defaulted to True
 * @return A StackInfo object
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
 */
public StackInfo createStack(String cloudSiteId, String cloudOwner, String tenantId, String stackName, VduModelInfo vduModel, String heatTemplate, Map<String, ?> stackInputs, boolean pollForCompletion, int timeoutMinutes, String environment, Map<String, Object> nestedTemplates, Map<String, Object> heatFiles, boolean backout, boolean failIfExists) throws MsoException {
    stripMultiCloudInputs(stackInputs);
    CreateStackParam createStack = createStackParam(stackName, heatTemplate, stackInputs, timeoutMinutes, environment, nestedTemplates, heatFiles);
    Stack currentStack = queryHeatStack(stackName, cloudSiteId, tenantId);
    boolean operationPerformed = false;
    if (currentStack != null) {
        logger.debug("Existing Stack found with Status: {} ", currentStack.getStackStatus());
        if (CREATE_COMPLETE.equals(currentStack.getStackStatus())) {
            new StackInfoMapper(currentStack).map();
        } else if (CREATE_IN_PROGRESS.equals(currentStack.getStackStatus())) {
            // TODO should check poll for completion right here
            currentStack = processCreateStack(cloudSiteId, tenantId, timeoutMinutes, backout, currentStack, createStack, true);
        } else if (CREATE_FAILED.equals(currentStack.getStackStatus()) || DELETE_FAILED.equals(currentStack.getStackStatus())) {
            try {
                if (pollForCompletion) {
                    processCreateStack(cloudSiteId, tenantId, timeoutMinutes, backout, currentStack, createStack, true);
                }
            } catch (MsoException e) {
                if (e instanceof StackCreationException) {
                    logger.warn("Error during Stack will attempt to recreate stack");
                    currentStack = createStack(createStack, cloudSiteId, tenantId);
                    currentStack.setStackName(stackName);
                    if (pollForCompletion) {
                        currentStack = processCreateStack(cloudSiteId, tenantId, timeoutMinutes, backout, currentStack, createStack, true);
                    }
                } else {
                    throw e;
                }
            }
        }
    } else {
        currentStack = createStack(createStack, cloudSiteId, tenantId);
        currentStack.setStackName(stackName);
        if (pollForCompletion) {
            currentStack = processCreateStack(cloudSiteId, tenantId, timeoutMinutes, backout, currentStack, createStack, true);
        }
        operationPerformed = true;
    }
    StackInfo stackInfo = new StackInfoMapper(currentStack).map();
    stackInfo.setOperationPerformed(operationPerformed);
    return stackInfo;
}
Also used : StackInfoMapper(org.onap.so.openstack.mappers.StackInfoMapper) MsoException(org.onap.so.openstack.exceptions.MsoException) CreateStackParam(com.woorea.openstack.heat.model.CreateStackParam) StackInfo(org.onap.so.openstack.beans.StackInfo) Stack(com.woorea.openstack.heat.model.Stack)

Example 3 with StackInfo

use of org.onap.so.openstack.beans.StackInfo in project so by onap.

the class MsoMulticloudUtils method queryStack.

/**
 * Query for a single stack (by ID) 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 cloud owner of the cloud site in which to query
 * @param stackId The ID of the stack to query
 * @return A StackInfo object
 * @throws MsoOpenstackException Thrown if the Openstack API call returns an exception.
 */
@Override
public StackInfo queryStack(String cloudSiteId, String cloudOwner, String tenantId, String instanceId) throws MsoException {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Query multicloud HEAT stack: %s in tenant %s", instanceId, tenantId));
    }
    String stackName = null;
    String stackId = null;
    boolean byName = false;
    int offset = instanceId.indexOf('/');
    if (offset > 0 && offset < (instanceId.length() - 1)) {
        stackName = instanceId.substring(0, offset);
        stackId = instanceId.substring(offset + 1);
    } else {
        stackName = instanceId;
        stackId = instanceId;
        byName = true;
    }
    StackInfo returnInfo = new StackInfo();
    returnInfo.setName(stackName);
    String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, stackId, byName);
    RestClient multicloudClient = getMulticloudClient(multicloudEndpoint, tenantId);
    if (multicloudClient != null) {
        Response response = multicloudClient.get();
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Multicloud GET Response: %s", response.toString()));
        }
        MulticloudQueryResponse responseBody = null;
        if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {
            returnInfo.setStatus(HeatStatus.NOTFOUND);
            returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
        } else if (response.getStatus() == Response.Status.OK.getStatusCode() && response.hasEntity()) {
            responseBody = getQueryBody((java.io.InputStream) response.getEntity());
            if (responseBody != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Multicloud Create Response Body: {}", responseBody);
                }
                Stack workloadStack = getWorkloadStack(responseBody.getWorkloadStatusReason());
                if (workloadStack != null && !responseBody.getWorkloadStatus().equals("GET_FAILED") && !responseBody.getWorkloadStatus().contains("UPDATE")) {
                    returnInfo = new StackInfoMapper(workloadStack).map();
                } else {
                    returnInfo.setCanonicalName(stackName + "/" + responseBody.getWorkloadId());
                    returnInfo.setStatus(getHeatStatus(responseBody.getWorkloadStatus()));
                    returnInfo.setStatusMessage(responseBody.getWorkloadStatus());
                }
            } else {
                returnInfo.setName(stackName);
                if (!byName)
                    returnInfo.setCanonicalName(instanceId);
                returnInfo.setStatus(HeatStatus.FAILED);
                returnInfo.setStatusMessage(MULTICLOUD_QUERY_BODY_NULL);
            }
        } else {
            returnInfo.setName(stackName);
            if (!byName)
                returnInfo.setCanonicalName(instanceId);
            returnInfo.setStatus(HeatStatus.FAILED);
            returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
        }
    }
    return returnInfo;
}
Also used : Response(javax.ws.rs.core.Response) StackInfoMapper(org.onap.so.openstack.mappers.StackInfoMapper) RestClient(org.onap.so.client.RestClient) StackInfo(org.onap.so.openstack.beans.StackInfo) Stack(com.woorea.openstack.heat.model.Stack)

Example 4 with StackInfo

use of org.onap.so.openstack.beans.StackInfo in project so by onap.

the class MsoMulticloudUtils method instantiateVdu.

/**
 * VduPlugin interface for instantiate function.
 *
 * Translate the VduPlugin parameters to the corresponding 'createStack' parameters, and then invoke the existing
 * function.
 */
@Override
public VduInstance instantiateVdu(CloudInfo cloudInfo, String instanceName, Map<String, Object> inputs, VduModelInfo vduModel, boolean rollbackOnFailure) throws VduException {
    String cloudSiteId = cloudInfo.getCloudSiteId();
    String cloudOwner = cloudInfo.getCloudOwner();
    String tenantId = cloudInfo.getTenantId();
    // Translate the VDU ModelInformation structure to that which is needed for
    // creating the Heat stack. Loop through the artifacts, looking specifically
    // for MAIN_TEMPLATE and ENVIRONMENT. Any other artifact will
    // be attached as a FILE.
    String heatTemplate = null;
    Map<String, Object> nestedTemplates = new HashMap<>();
    Map<String, Object> files = new HashMap<>();
    String heatEnvironment = null;
    for (VduArtifact vduArtifact : vduModel.getArtifacts()) {
        if (vduArtifact.getType() == ArtifactType.MAIN_TEMPLATE) {
            heatTemplate = new String(vduArtifact.getContent());
        } else if (vduArtifact.getType() == ArtifactType.NESTED_TEMPLATE) {
            nestedTemplates.put(vduArtifact.getName(), new String(vduArtifact.getContent()));
        } else if (vduArtifact.getType() == ArtifactType.ENVIRONMENT) {
            heatEnvironment = new String(vduArtifact.getContent());
        }
    }
    try {
        StackInfo stackInfo = createStack(cloudSiteId, cloudOwner, tenantId, instanceName, vduModel, heatTemplate, // poll
        inputs, // poll
        true, // completion
        vduModel.getTimeoutMinutes(), heatEnvironment, nestedTemplates, files, rollbackOnFailure, false);
        // Populate a vduInstance from the StackInfo
        return stackInfoToVduInstance(stackInfo);
    } catch (Exception e) {
        throw new VduException("MsoMulticloudUtils (instantiateVDU): createStack Exception", e);
    }
}
Also used : HashMap(java.util.HashMap) StackInfo(org.onap.so.openstack.beans.StackInfo) VduArtifact(org.onap.so.adapters.vdu.VduArtifact) MsoAdapterException(org.onap.so.openstack.exceptions.MsoAdapterException) VduException(org.onap.so.adapters.vdu.VduException) MsoOpenstackException(org.onap.so.openstack.exceptions.MsoOpenstackException) MalformedURLException(java.net.MalformedURLException) UriBuilderException(javax.ws.rs.core.UriBuilderException) MsoException(org.onap.so.openstack.exceptions.MsoException) VduException(org.onap.so.adapters.vdu.VduException)

Example 5 with StackInfo

use of org.onap.so.openstack.beans.StackInfo in project so by onap.

the class MsoMulticloudUtils method deleteStack.

public StackInfo deleteStack(String cloudSiteId, String cloudOwner, String tenantId, String instanceId) throws MsoException {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Delete multicloud HEAT stack: %s in tenant %s", instanceId, tenantId));
    }
    String stackName = null;
    String stackId = null;
    int offset = instanceId.indexOf('/');
    if (offset > 0 && offset < (instanceId.length() - 1)) {
        stackName = instanceId.substring(0, offset);
        stackId = instanceId.substring(offset + 1);
    } else {
        stackName = instanceId;
        stackId = instanceId;
    }
    StackInfo returnInfo = new StackInfo();
    returnInfo.setName(stackName);
    Response response = null;
    String multicloudEndpoint = getMulticloudEndpoint(cloudSiteId, cloudOwner, stackId, false);
    RestClient multicloudClient = getMulticloudClient(multicloudEndpoint, tenantId);
    if (multicloudClient != null) {
        response = multicloudClient.delete();
        if (logger.isDebugEnabled()) {
            logger.debug(String.format("Multicloud Delete response is: %s", response.getEntity().toString()));
        }
        if (response.getStatus() == Response.Status.NOT_FOUND.getStatusCode()) {
            returnInfo.setStatus(HeatStatus.NOTFOUND);
            returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
        } else if (response.getStatus() == Response.Status.NO_CONTENT.getStatusCode()) {
            return getStackStatus(cloudSiteId, cloudOwner, tenantId, instanceId);
        } else {
            returnInfo.setStatus(HeatStatus.FAILED);
            returnInfo.setStatusMessage(response.getStatusInfo().getReasonPhrase());
        }
    }
    returnInfo.setStatus(mapResponseToHeatStatus(response));
    return returnInfo;
}
Also used : Response(javax.ws.rs.core.Response) RestClient(org.onap.so.client.RestClient) StackInfo(org.onap.so.openstack.beans.StackInfo)

Aggregations

StackInfo (org.onap.so.openstack.beans.StackInfo)38 MsoException (org.onap.so.openstack.exceptions.MsoException)17 HashMap (java.util.HashMap)11 Test (org.junit.Test)11 Stack (com.woorea.openstack.heat.model.Stack)9 CloudSite (org.onap.so.db.catalog.beans.CloudSite)8 MsoOpenstackException (org.onap.so.openstack.exceptions.MsoOpenstackException)8 VduException (org.onap.so.adapters.vdu.VduException)7 BaseTest (org.onap.so.BaseTest)6 MsoAdapterException (org.onap.so.openstack.exceptions.MsoAdapterException)6 Heat (com.woorea.openstack.heat.Heat)4 MalformedURLException (java.net.MalformedURLException)4 Response (javax.ws.rs.core.Response)4 UriBuilderException (javax.ws.rs.core.UriBuilderException)4 RestClient (org.onap.so.client.RestClient)4 HeatTemplate (org.onap.so.db.catalog.beans.HeatTemplate)4 StackInfoMapper (org.onap.so.openstack.mappers.StackInfoMapper)4 OpenStackConnectException (com.woorea.openstack.base.client.OpenStackConnectException)3 OpenStackRequest (com.woorea.openstack.base.client.OpenStackRequest)3 OpenStackResponseException (com.woorea.openstack.base.client.OpenStackResponseException)3