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());
}
}
}
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() + "]");
}
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);
}
}
}
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);
}
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();
}
}
Aggregations