use of org.alien4cloud.tosca.model.templates.NodeTemplate in project alien4cloud by alien4cloud.
the class AddNodeProcessor method process.
@Override
public void process(Csar csar, Topology topology, AddNodeOperation operation) {
NameValidationUtils.validateNodeName(operation.getNodeName());
AlienUtils.failIfExists(topology.getNodeTemplates(), operation.getNodeName(), "A node template with the given name {} already exists in the topology {}.", operation.getNodeName(), topology.getId());
NodeType nodeType = toscaTypeSearchService.findByIdOrFail(NodeType.class, operation.getIndexedNodeTypeId());
if (nodeType.getSubstitutionTopologyId() != null) {
// it's a try to add this topology's type
if (nodeType.getSubstitutionTopologyId().equals(topology.getId())) {
throw new CyclicReferenceException("Cyclic reference : a topology template can not reference itself");
}
// detect try to add a substitution topology that indirectly reference this one
topologyCompositionService.recursivelyDetectTopologyCompositionCyclicReference(topology.getId(), nodeType.getSubstitutionTopologyId());
}
if (topology.getNodeTemplates() == null) {
topology.setNodeTemplates(new LinkedHashMap<>());
}
log.debug("Create node template [ {} ]", operation.getNodeName());
NodeType loadedIndexedNodeType = topologyService.loadType(topology, nodeType);
NodeTemplate nodeTemplate = TemplateBuilder.buildNodeTemplate(loadedIndexedNodeType);
nodeTemplate.setName(operation.getNodeName());
if (operation.getCoords() != null) {
// Set the position information of the node as meta-data.
nodeTemplate.setTags(Lists.newArrayList(new Tag(Constants.X_META, String.valueOf(operation.getCoords().getX())), new Tag(Constants.Y_META, String.valueOf(operation.getCoords().getY()))));
}
topology.getNodeTemplates().put(operation.getNodeName(), nodeTemplate);
log.debug("Adding a new Node template <" + operation.getNodeName() + "> bound to the node type <" + operation.getIndexedNodeTypeId() + "> to the topology <" + topology.getId() + "> .");
TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
workflowBuilderService.addNode(topologyContext, operation.getNodeName());
if (!operation.isSkipAutoCompletion()) {
danglingRequirementService.addDanglingRequirements(topology, topologyContext, nodeTemplate, operation.getRequirementSkipAutoCompletion());
}
}
use of org.alien4cloud.tosca.model.templates.NodeTemplate in project alien4cloud by alien4cloud.
the class DuplicateNodeProcessor method duplicateNodeTemplate.
private void duplicateNodeTemplate(NodeTemplate nodeTemplateToDuplicate, Map<String, String> duplicatedNodesNameMappings, Map<String, NodeTemplate> nodeTemplates, Topology topology, Csar csar) {
// Build the new one
NodeTemplate newNodeTemplate = CloneUtil.clone(nodeTemplateToDuplicate);
newNodeTemplate.setName(copyName(nodeTemplateToDuplicate.getName(), nodeTemplates.keySet()));
// load type
NodeType type = ToscaContext.getOrFail(NodeType.class, nodeTemplateToDuplicate.getType());
topologyService.loadType(topology, type);
log.debug("Duplicating node template [ {} ] into [ {} ] on the topology [ {} ] .", nodeTemplateToDuplicate.getName(), newNodeTemplate.getName(), topology.getId());
// Put the new one in the topology
nodeTemplates.put(newNodeTemplate.getName(), newNodeTemplate);
// register the name mapping for further use
duplicatedNodesNameMappings.put(nodeTemplateToDuplicate.getName(), newNodeTemplate.getName());
// copy outputs
copyOutputs(topology, nodeTemplateToDuplicate.getName(), newNodeTemplate.getName());
TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
// add the new node to the workflow
workflowBuilderService.addNode(topologyContext, newNodeTemplate.getName());
// copy hosted nodes
safe(TopologyNavigationUtil.getHostedNodes(topology, nodeTemplateToDuplicate.getName())).forEach(nodeTemplate -> duplicateNodeTemplate(nodeTemplate, duplicatedNodesNameMappings, nodeTemplates, topology, csar));
}
use of org.alien4cloud.tosca.model.templates.NodeTemplate in project alien4cloud by alien4cloud.
the class DuplicateNodeProcessor method copyAndCleanRelationships.
/**
* Discard all relationship targeting an "external" node. External here in terms of the hostedOn hierarchy
* Copy the valid ones
*
* @param nodeName
* @param validTargets A map of oldNodeName -> duplicatedNodeName, we should keep relationships targeting one of these nodes.
*/
private void copyAndCleanRelationships(String nodeName, Map<String, String> validTargets, Map<String, NodeTemplate> nodeTemplates, TopologyContext topologyContext) {
NodeTemplate nodeTemplate = nodeTemplates.get(nodeName);
if (MapUtils.isNotEmpty(nodeTemplate.getRelationships())) {
Map<String, RelationshipTemplate> relationships = nodeTemplate.getRelationships();
Set<String> keys = Sets.newHashSet(relationships.keySet());
for (String key : keys) {
RelationshipTemplate rel = relationships.remove(key);
// If so, then rename it, its target and keep it
if (validTargets.containsKey(rel.getTarget())) {
rel.setName(copyName(rel.getName(), relationships.keySet()));
rel.setTarget(validTargets.get(rel.getTarget()));
relationships.put(rel.getName(), rel);
workflowBuilderService.addRelationship(topologyContext, nodeName, rel.getName());
}
}
if (relationships.isEmpty()) {
nodeTemplate.setRelationships(null);
}
}
}
use of org.alien4cloud.tosca.model.templates.NodeTemplate in project alien4cloud by alien4cloud.
the class DuplicateNodeProcessor method process.
@Override
public void process(Csar csar, Topology topology, DuplicateNodeOperation operation) {
Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
// Retrieve existing node template
NodeTemplate nodeTemplateToDuplicate = TopologyUtils.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates);
// map that will contains a mapping of the duplicated node and their new names
Map<String, String> duplicatedNodesNameMappings = Maps.newHashMap();
// first duplicate the node templates
duplicateNodeTemplate(nodeTemplateToDuplicate, duplicatedNodesNameMappings, nodeTemplates, topology, csar);
// then clean the relationships, discarding all that targets a node not in hostedNodes
processRelationships(duplicatedNodesNameMappings, nodeTemplates, topology, csar);
}
use of org.alien4cloud.tosca.model.templates.NodeTemplate in project alien4cloud by alien4cloud.
the class ResetNodeDeploymentArtifactProcessor method process.
@Override
public void process(Csar csar, Topology topology, ResetNodeDeploymentArtifactOperation operation) {
// Get the node template's artifacts to reset
Map<String, NodeTemplate> nodeTemplates = TopologyUtils.getNodeTemplates(topology);
NodeTemplate nodeTemplate = TopologyUtils.getNodeTemplate(topology.getId(), operation.getNodeName(), nodeTemplates);
DeploymentArtifact currentArtifact = nodeTemplate.getArtifacts() == null ? null : nodeTemplate.getArtifacts().get(operation.getArtifactName());
if (currentArtifact == null) {
throw new NotFoundException("Artifact with key [" + operation.getArtifactName() + "] do not exist in node template [" + nodeTemplate.getName() + "].");
}
// Get the node type's artifact
Map<String, NodeType> nodeTypes = topologyServiceCore.getIndexedNodeTypesFromTopology(topology, false, false, true);
NodeType nodeType = nodeTypes.get(nodeTemplate.getType());
DeploymentArtifact artifactFromNodeType = nodeType.getArtifacts() == null ? null : nodeType.getArtifacts().get(operation.getArtifactName());
if (artifactFromNodeType == null) {
throw new NotFoundException("Artifact with key [" + operation.getArtifactName() + "] do not exist in node type [" + nodeType.getId() + "].");
}
currentArtifact.setArtifactRef(artifactFromNodeType.getArtifactRef());
currentArtifact.setArtifactName(artifactFromNodeType.getArtifactName());
currentArtifact.setArtifactType(artifactFromNodeType.getArtifactType());
currentArtifact.setArtifactRepository(artifactFromNodeType.getArtifactRepository());
currentArtifact.setRepositoryName(artifactFromNodeType.getRepositoryName());
currentArtifact.setRepositoryURL(artifactFromNodeType.getRepositoryURL());
currentArtifact.setRepositoryCredential(artifactFromNodeType.getRepositoryCredential());
}
Aggregations