Search in sources :

Example 1 with ManagedServiceCreatedEvent

use of org.alien4cloud.alm.events.ManagedServiceCreatedEvent in project alien4cloud by alien4cloud.

the class ManagedServiceResourceService method create.

/**
 * Create a Service resource associated with the given environment.
 *
 * @param environmentId The environment to create a service for, the service version will be the one of the environment current associated version.
 * @param serviceName The name of the service as it should appears.
 * @param fromRuntime If we should try to create the service from the runtime topology related to the environment.
 * @return the id of the created service
 *
 * @throws AlreadyExistException if a service with the given name, or related to the given environment already exists
 * @throws alien4cloud.exception.NotFoundException if <b>fromRuntime</b> is set to true, but the environment is not deployed
 * @throws MissingSubstitutionException if topology related to the environment doesn't define a substitution type
 */
public synchronized String create(String serviceName, String environmentId, boolean fromRuntime) {
    ApplicationEnvironment environment = checkAndGetApplicationEnvironment(environmentId);
    // check that the service does not exists already for this environment
    if (alienDAO.buildQuery(ServiceResource.class).setFilters(fromKeyValueCouples("environmentId", environmentId)).count() > 0) {
        throw new AlreadyExistException("A service resource for environment <" + environmentId + "> and version <" + environment.getTopologyVersion() + "> already exists.");
    }
    Topology topology;
    String state = ToscaNodeLifecycleConstants.INITIAL;
    Deployment deployment = null;
    if (fromRuntime) {
        deployment = deploymentService.getActiveDeploymentOrFail(environmentId);
        topology = deploymentRuntimeStateService.getRuntimeTopology(deployment.getId());
        DeploymentStatus currentStatus = deploymentRuntimeStateService.getDeploymentStatus(deployment);
        state = managedServiceResourceEventService.getInstanceStateFromDeploymentStatus(currentStatus);
        if (state == null) {
            // We need a valid deployment state to expose as service.
            throw new InvalidDeploymentStatusException("Creating a service out of a running deployment is possible only when it's status is one of [DEPLOYED, FAILURE, UNDEPLOYED] current was <" + currentStatus + ">", currentStatus);
        }
    } else {
        topology = topologyServiceCore.getOrFail(Csar.createId(environment.getApplicationId(), environment.getTopologyVersion()));
    }
    if (topology.getSubstitutionMapping() == null) {
        throw new MissingSubstitutionException("Substitution is required to expose a topology.");
    }
    // The elementId of the type created out of the substitution is currently the archive name.
    String serviceId = serviceResourceService.create(serviceName, environment.getTopologyVersion(), topology.getArchiveName(), environment.getTopologyVersion(), environmentId);
    // Update the service relationships definition from the topology substitution
    updateServiceRelationship(serviceId, topology);
    if (fromRuntime) {
        managedServiceResourceEventService.updateRunningService((DeploymentTopology) topology, serviceResourceService.getOrFail(serviceId), deployment, state);
    }
    ServiceResource serviceResource = serviceResourceService.getOrFail(serviceId);
    // trigger a ManagedServiceCreatedEvent
    publisher.publishEvent(new ManagedServiceCreatedEvent(this, serviceResource));
    return serviceId;
}
Also used : MissingSubstitutionException(org.alien4cloud.alm.service.exceptions.MissingSubstitutionException) InvalidDeploymentStatusException(org.alien4cloud.alm.service.exceptions.InvalidDeploymentStatusException) Deployment(alien4cloud.model.deployment.Deployment) ServiceResource(alien4cloud.model.service.ServiceResource) DeploymentTopology(alien4cloud.model.deployment.DeploymentTopology) Topology(org.alien4cloud.tosca.model.templates.Topology) AlreadyExistException(alien4cloud.exception.AlreadyExistException) ApplicationEnvironment(alien4cloud.model.application.ApplicationEnvironment) ManagedServiceCreatedEvent(org.alien4cloud.alm.events.ManagedServiceCreatedEvent) DeploymentStatus(alien4cloud.paas.model.DeploymentStatus)

Aggregations

AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)1 Deployment (alien4cloud.model.deployment.Deployment)1 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)1 ServiceResource (alien4cloud.model.service.ServiceResource)1 DeploymentStatus (alien4cloud.paas.model.DeploymentStatus)1 ManagedServiceCreatedEvent (org.alien4cloud.alm.events.ManagedServiceCreatedEvent)1 InvalidDeploymentStatusException (org.alien4cloud.alm.service.exceptions.InvalidDeploymentStatusException)1 MissingSubstitutionException (org.alien4cloud.alm.service.exceptions.MissingSubstitutionException)1 Topology (org.alien4cloud.tosca.model.templates.Topology)1