Search in sources :

Example 1 with TopologyContext

use of alien4cloud.paas.wf.TopologyContext in project alien4cloud by alien4cloud.

the class EditorTopologyUploadService method processTopologyParseResult.

private void processTopologyParseResult(Path archivePath, ParsingResult<ArchiveRoot> parsingResult, String workspace) {
    // parse the archive.
    parsingResult = postProcessor.process(archivePath, parsingResult, workspace);
    // check if any blocker error has been found during parsing process.
    if (parsingResult.hasError(ParsingErrorLevel.ERROR)) {
        // do not save anything if any blocker error has been found during import.
        throw new EditorToscaYamlParsingException("Uploaded yaml files is not a valid tosca template", ArchiveParserUtil.toSimpleResult(parsingResult));
    }
    if (parsingResult.getResult().hasToscaTypes()) {
        throw new EditorToscaYamlNotSupportedException("Tosca types are currently not supported in the topology editor context.");
    }
    if (!parsingResult.getResult().hasToscaTopologyTemplate()) {
        throw new EditorToscaYamlNotSupportedException("A topology template is required in the topology edition context.");
    }
    Topology currentTopology = EditionContextManager.getTopology();
    Topology parsedTopology = parsingResult.getResult().getTopology();
    final String definitionVersion = parsingResult.getResult().getArchive().getToscaDefinitionsVersion();
    if (!currentTopology.getArchiveName().equals(parsedTopology.getArchiveName()) || !currentTopology.getArchiveVersion().equals(parsedTopology.getArchiveVersion())) {
        throw new EditorToscaYamlNotSupportedException("Template name and version must be set to [" + currentTopology.getArchiveName() + ":" + currentTopology.getArchiveVersion() + "] and cannot be updated to [" + parsedTopology.getArchiveName() + ":" + parsedTopology.getArchiveVersion() + "]");
    }
    // Copy static elements from the topology
    parsedTopology.setId(currentTopology.getId());
    // Update editor tosca context
    ToscaContext.get().resetDependencies(parsedTopology.getDependencies());
    // init the workflows for the topology based on the yaml
    TopologyContext topologyContext = workflowBuilderService.buildCachedTopologyContext(new TopologyContext() {

        @Override
        public String getDSLVersion() {
            return definitionVersion;
        }

        @Override
        public Topology getTopology() {
            return parsedTopology;
        }

        @Override
        public <T extends AbstractToscaType> T findElement(Class<T> clazz, String id) {
            return ToscaContext.get(clazz, id);
        }
    });
    for (Workflow wf : safe(topologyContext.getTopology().getWorkflows()).values()) {
        wf.setHasCustomModifications(true);
    }
    workflowBuilderService.initWorkflows(topologyContext);
    // update the topology in the edition context with the new one
    EditionContextManager.get().setTopology(parsingResult.getResult().getTopology());
}
Also used : EditorToscaYamlParsingException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException) Workflow(org.alien4cloud.tosca.model.workflow.Workflow) Topology(org.alien4cloud.tosca.model.templates.Topology) TopologyContext(alien4cloud.paas.wf.TopologyContext) EditorToscaYamlNotSupportedException(org.alien4cloud.tosca.editor.exception.EditorToscaYamlNotSupportedException)

Example 2 with TopologyContext

use of alien4cloud.paas.wf.TopologyContext 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());
    }
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) Tag(alien4cloud.model.common.Tag) TopologyContext(alien4cloud.paas.wf.TopologyContext) CyclicReferenceException(alien4cloud.exception.CyclicReferenceException)

Example 3 with TopologyContext

use of alien4cloud.paas.wf.TopologyContext 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));
}
Also used : NodeTemplate(org.alien4cloud.tosca.model.templates.NodeTemplate) NodeType(org.alien4cloud.tosca.model.types.NodeType) TopologyContext(alien4cloud.paas.wf.TopologyContext)

Example 4 with TopologyContext

use of alien4cloud.paas.wf.TopologyContext in project alien4cloud by alien4cloud.

the class DuplicateNodeProcessor method processRelationships.

/**
 * Process relationships of the duplicated nodes: Copy what we want to keep and discard the others
 *
 * @param duplicatedNodes Map of nodeToDuplicateName--> duplicatedNodeName
 * @param nodeTemplates
 * @param topology
 */
private void processRelationships(Map<String, String> duplicatedNodes, Map<String, NodeTemplate> nodeTemplates, Topology topology, Csar csar) {
    TopologyContext topologyContext = workflowBuilderService.buildTopologyContext(topology, csar);
    duplicatedNodes.values().forEach(nodeName -> copyAndCleanRelationships(nodeName, duplicatedNodes, nodeTemplates, topologyContext));
}
Also used : TopologyContext(alien4cloud.paas.wf.TopologyContext)

Example 5 with TopologyContext

use of alien4cloud.paas.wf.TopologyContext 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)

Aggregations

TopologyContext (alien4cloud.paas.wf.TopologyContext)9 NodeTemplate (org.alien4cloud.tosca.model.templates.NodeTemplate)4 Topology (org.alien4cloud.tosca.model.templates.Topology)3 NodeType (org.alien4cloud.tosca.model.types.NodeType)3 LinkedHashMap (java.util.LinkedHashMap)2 AlreadyExistException (alien4cloud.exception.AlreadyExistException)1 CyclicReferenceException (alien4cloud.exception.CyclicReferenceException)1 InvalidNameException (alien4cloud.exception.InvalidNameException)1 NotFoundException (alien4cloud.exception.NotFoundException)1 Tag (alien4cloud.model.common.Tag)1 ParsingError (alien4cloud.tosca.parser.ParsingError)1 Maps.newLinkedHashMap (com.google.common.collect.Maps.newLinkedHashMap)1 ArrayDeque (java.util.ArrayDeque)1 Map (java.util.Map)1 CapabilityBoundException (org.alien4cloud.tosca.editor.exception.CapabilityBoundException)1 EditorToscaYamlNotSupportedException (org.alien4cloud.tosca.editor.exception.EditorToscaYamlNotSupportedException)1 EditorToscaYamlParsingException (org.alien4cloud.tosca.editor.exception.EditorToscaYamlParsingException)1 RequirementBoundException (org.alien4cloud.tosca.editor.exception.RequirementBoundException)1 CSARDependency (org.alien4cloud.tosca.model.CSARDependency)1 AbstractPropertyValue (org.alien4cloud.tosca.model.definitions.AbstractPropertyValue)1