Search in sources :

Example 6 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class RenameInputArtifactProcessor method process.

@Override
public void process(Csar csar, Topology topology, RenameInputArtifactOperation operation) {
    if (operation.getNewInputName() == null || operation.getNewInputName().isEmpty() || !operation.getNewInputName().matches("\\w+")) {
        throw new InvalidNameException("newInputName", operation.getNewInputName(), "\\w+");
    }
    if (safe(topology.getInputArtifacts()).containsKey(operation.getNewInputName())) {
        throw new AlreadyExistException("Input artifact with name <" + operation.getNewInputName() + "> already exists.");
    }
    if (!safe(topology.getInputArtifacts()).containsKey(operation.getInputName())) {
        throw new NotFoundException("Input artifact with name <" + operation.getInputName() + "> does not exists.");
    }
    DeploymentArtifact inputArtifact = topology.getInputArtifacts().remove(operation.getInputName());
    topology.getInputArtifacts().put(operation.getNewInputName(), inputArtifact);
    // change the value of concerned node template artifacts
    for (NodeTemplate nodeTemplate : safe(topology.getNodeTemplates()).values()) {
        for (DeploymentArtifact dArtifact : safe(nodeTemplate.getArtifacts()).values()) {
            InputArtifactUtil.updateInputArtifactIdIfNeeded(dArtifact, operation.getInputName(), operation.getNewInputName());
        }
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) NotFoundException(alien4cloud.exception.NotFoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) DeploymentArtifact(org.alien4cloud.tosca.model.definitions.DeploymentArtifact)

Example 7 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class AddRelationshipProcessor method processNodeOperation.

@Override
protected void processNodeOperation(Csar csar, Topology topology, AddRelationshipOperation operation, NodeTemplate sourceNode) {
    if (operation.getRelationshipName() == null || operation.getRelationshipName().isEmpty()) {
        throw new InvalidNameException("relationshipName", operation.getRelationshipName(), "Not null or empty");
    }
    if (AlienUtils.safe(sourceNode.getRelationships()).containsKey(operation.getRelationshipName())) {
        throw new AlreadyExistException("Relationship " + operation.getRelationshipName() + " already exist on node " + operation.getNodeName());
    }
    if (sourceNode.getRequirements() == null || sourceNode.getRequirements().get(operation.getRequirementName()) == null) {
        throw new NotFoundException("Unable to find requirement with name <" + operation.getRequirementName() + "> on the source node" + operation.getNodeName());
    }
    Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
    // ensure that the target node exists
    TopologyUtils.getNodeTemplate(topology.getId(), operation.getTarget(), nodeTemplates);
    // We don't use the tosca context as the relationship type may not be in dependencies yet (that's why we use the load type below).
    RelationshipType indexedRelationshipType = toscaTypeSearchService.find(RelationshipType.class, operation.getRelationshipType(), operation.getRelationshipVersion());
    if (indexedRelationshipType == null) {
        throw new NotFoundException(RelationshipType.class.getName(), operation.getRelationshipType() + ":" + operation.getRelationshipVersion(), "Unable to find relationship type to create template in topology.");
    }
    boolean upperBoundReachedSource = topologyRequirementBoundsValidationServices.isRequirementUpperBoundReachedForSource(sourceNode, operation.getRequirementName(), topology.getDependencies());
    if (upperBoundReachedSource) {
        // throw exception here
        throw new RequirementBoundException(operation.getNodeName(), operation.getRequirementName());
    }
    boolean upperBoundReachedTarget = topologyCapabilityBoundsValidationServices.isCapabilityUpperBoundReachedForTarget(operation.getTarget(), nodeTemplates, operation.getTargetedCapabilityName(), topology.getDependencies());
    // return with a rest response error
    if (upperBoundReachedTarget) {
        throw new CapabilityBoundException(operation.getTarget(), operation.getTargetedCapabilityName());
    }
    topologyService.loadType(topology, indexedRelationshipType);
    NodeTemplate newSourceNode = topology.getNodeTemplates().get(sourceNode.getName());
    if (sourceNode != newSourceNode) {
        // topology has been reloaded
        sourceNode = newSourceNode;
    }
    Map<String, RelationshipTemplate> relationships = sourceNode.getRelationships();
    if (relationships == null) {
        relationships = Maps.newHashMap();
        sourceNode.setRelationships(relationships);
    }
    RelationshipTemplate relationshipTemplate = new RelationshipTemplate();
    relationshipTemplate.setName(operation.getRelationshipName());
    relationshipTemplate.setTarget(operation.getTarget());
    relationshipTemplate.setTargetedCapabilityName(operation.getTargetedCapabilityName());
    relationshipTemplate.setRequirementName(operation.getRequirementName());
    relationshipTemplate.setRequirementType(sourceNode.getRequirements().get(operation.getRequirementName()).getType());
    relationshipTemplate.setType(indexedRelationshipType.getElementId());
    relationshipTemplate.setArtifacts(newLinkedHashMap(indexedRelationshipType.getArtifacts()));
    relationshipTemplate.setAttributes(newLinkedHashMap(indexedRelationshipType.getAttributes()));
    Map<String, AbstractPropertyValue> properties = new LinkedHashMap<String, AbstractPropertyValue>();
    TemplateBuilder.fillProperties(properties, indexedRelationshipType.getProperties(), null);
    relationshipTemplate.setProperties(properties);
    relationships.put(operation.getRelationshipName(), relationshipTemplate);
    TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
    workflowBuilderService.addRelationship(topologyContext, operation.getNodeName(), operation.getRelationshipName());
    log.debug("Added relationship to the topology [" + topology.getId() + "], node name [" + operation.getNodeName() + "], relationship name [" + operation.getRelationshipName() + "]");
}
Also used : CapabilityBoundException(org.alien4cloud.tosca.editor.exception.CapabilityBoundException) RelationshipType(org.alien4cloud.tosca.model.types.RelationshipType) NotFoundException(alien4cloud.exception.NotFoundException) LinkedHashMap(java.util.LinkedHashMap) NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) InvalidNameException(alien4cloud.exception.InvalidNameException) RequirementBoundException(org.alien4cloud.tosca.editor.exception.RequirementBoundException) AlreadyExistException(alien4cloud.exception.AlreadyExistException) TopologyContext(alien4cloud.paas.wf.TopologyContext) AbstractPropertyValue(org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)

Example 8 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class AbstractWorkflowBuilder method renameStep.

public void renameStep(Workflow wf, String stepId, String newStepName) {
    if (wf.getSteps().containsKey(newStepName)) {
        throw new AlreadyExistException(String.format("A step named ''%s'' already exists in workflow '%s'", newStepName, wf.getName()));
    }
    WorkflowStep step = wf.getSteps().remove(stepId);
    step.setName(newStepName);
    wf.addStep(step);
    // now explore the links
    if (step.getPrecedingSteps() != null) {
        for (String precedingId : step.getPrecedingSteps()) {
            WorkflowStep precedingStep = wf.getSteps().get(precedingId);
            precedingStep.removeFollowing(stepId);
            precedingStep.addFollowing(newStepName);
        }
    }
    if (step.getOnSuccess() != null) {
        for (String followingId : step.getOnSuccess()) {
            WorkflowStep followingStep = wf.getSteps().get(followingId);
            followingStep.removePreceding(stepId);
            followingStep.addPreceding(newStepName);
        }
    }
}
Also used : NodeWorkflowStep(org.alien4cloud.tosca.model.workflow.NodeWorkflowStep) RelationshipWorkflowStep(org.alien4cloud.tosca.model.workflow.RelationshipWorkflowStep) WorkflowStep(org.alien4cloud.tosca.model.workflow.WorkflowStep) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Example 9 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class TopologyCompositionService method renameNodeTemplate.

private void renameNodeTemplate(Topology topology, String oldName, String newName) {
    // quite improbable but ...
    if (topology.getNodeTemplates().containsKey(newName)) {
        throw new AlreadyExistException(String.format("A node with name '%s' already exists in this topology", newName));
    }
    NodeTemplate nodeTemplate = topology.getNodeTemplates().remove(oldName);
    // manage relationships that target this node
    for (NodeTemplate otherNodes : topology.getNodeTemplates().values()) {
        if (otherNodes.getRelationships() == null || otherNodes.getRelationships().isEmpty()) {
            continue;
        }
        for (RelationshipTemplate relationshipTemplate : otherNodes.getRelationships().values()) {
            if (relationshipTemplate.getTarget().equals(oldName)) {
                relationshipTemplate.setTarget(newName);
            }
        }
    }
    // all output stuffs
    MapUtil.replaceKey(topology.getOutputProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputCapabilityProperties(), oldName, newName);
    MapUtil.replaceKey(topology.getOutputAttributes(), oldName, newName);
    // group members must be updated
    if (topology.getGroups() != null) {
        for (NodeGroup nodeGroup : topology.getGroups().values()) {
            Set<String> members = nodeGroup.getMembers();
            if (members != null && members.remove(oldName)) {
                members.add(newName);
            }
        }
    }
    // substitutions
    if (topology.getSubstitutionMapping() != null) {
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getCapabilities(), oldName, newName);
        renameNodeTemplateInSubstitutionTargets(topology.getSubstitutionMapping().getRequirements(), oldName, newName);
    }
    // finally the node itself
    topology.getNodeTemplates().put(newName, nodeTemplate);
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) RelationshipTemplate(org.alien4cloud.tosca.model.templates.RelationshipTemplate) AlreadyExistException(alien4cloud.exception.AlreadyExistException) NodeGroup(org.alien4cloud.tosca.model.templates.NodeGroup)

Example 10 with AlreadyExistException

use of alien4cloud.exception.AlreadyExistException in project alien4cloud by alien4cloud.

the class InitialLoader method loadArchives.

private void loadArchives(Path rootDirectory) {
    if (!Files.exists(rootDirectory) || !Files.isDirectory(rootDirectory)) {
        log.warn("Skipping archives' initial loading: directory cannot be found {}.", rootDirectory.toString());
        return;
    }
    // this operation is performed using admin role
    SecurityContextImpl adminContext = new SecurityContextImpl();
    Set<SimpleGrantedAuthority> authorities = Sets.newHashSet();
    authorities.add(new SimpleGrantedAuthority(Role.ADMIN.name()));
    UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken("alien4cloud_init_loader", "alien4cloud_init_loader", authorities);
    adminContext.setAuthentication(auth);
    SecurityContextHolder.setContext(adminContext);
    // archives must be in zip format and placed in the actual folder
    try {
        List<Path> archives = FileUtil.listFiles(rootDirectory, ".+\\.(zip|csar)");
        Collections.sort(archives);
        for (Path archive : archives) {
            try {
                log.debug("Initial load of archives from [ {} ].", archive.toString());
                csarUploadService.upload(archive, CSARSource.ALIEN, AlienConstants.GLOBAL_WORKSPACE_ID);
            } catch (AlreadyExistException e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive has already been loaded.", archive.toString(), e);
            } catch (CSARUsedInActiveDeployment e) {
                log.debug("Skipping initial upload of archive [ {} ]. Archive is used in an active depoyment, and then cannot be overrided.", archive.toString(), e);
            } catch (ParsingException e) {
                log.error("Initial upload of archive [ {} ] has failed.", archive.toString(), e);
            } catch (ToscaTypeAlreadyDefinedInOtherCSAR e) {
                log.debug("Skipping initial upload of archive [ {} ], it's archive contain's a tosca type already defined in an other archive." + e.getMessage(), archive.toString(), e);
            }
        }
    } catch (IOException e) {
        log.error("Failed to load initial archives", e);
    } finally {
        // clear the security context
        SecurityContextHolder.clearContext();
    }
}
Also used : Path(java.nio.file.Path) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) ParsingException(alien4cloud.tosca.parser.ParsingException) CSARUsedInActiveDeployment(alien4cloud.component.repository.exception.CSARUsedInActiveDeployment) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ToscaTypeAlreadyDefinedInOtherCSAR(alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR) IOException(java.io.IOException) AlreadyExistException(alien4cloud.exception.AlreadyExistException)

Aggregations

AlreadyExistException (alien4cloud.exception.AlreadyExistException)26 NotFoundException (alien4cloud.exception.NotFoundException)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)7 CSARUsedInActiveDeployment (alien4cloud.component.repository.exception.CSARUsedInActiveDeployment)6 ToscaTypeAlreadyDefinedInOtherCSAR (alien4cloud.component.repository.exception.ToscaTypeAlreadyDefinedInOtherCSAR)6 IOException (java.io.IOException)5 Path (java.nio.file.Path)5 InvalidNameException (alien4cloud.exception.InvalidNameException)4 SubstitutionTarget (org.alien4cloud.tosca.model.templates.SubstitutionTarget)4 Audit (alien4cloud.audit.annotation.Audit)3 ParsingException (alien4cloud.tosca.parser.ParsingException)3 ApiOperation (io.swagger.annotations.ApiOperation)3 RelationshipTemplate (org.alien4cloud.tosca.model.templates.RelationshipTemplate)3 ApplicationEnvironment (alien4cloud.model.application.ApplicationEnvironment)2 DeploymentTopology (alien4cloud.model.deployment.DeploymentTopology)2 PluginConfigurationException (alien4cloud.paas.exception.PluginConfigurationException)2 ParsingError (alien4cloud.tosca.parser.ParsingError)2 Csar (org.alien4cloud.tosca.model.Csar)2 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)2 Application (alien4cloud.model.application.Application)1